1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_FORMULA_TOKEN_HXX
21 #define INCLUDED_FORMULA_TOKEN_HXX
23 #include <sal/config.h>
28 #include <formula/formuladllapi.h>
29 #include <formula/IFunctionDescription.hxx>
30 #include <formula/opcode.hxx>
31 #include <formula/types.hxx>
32 #include <osl/interlck.h>
33 #include <rtl/ustring.hxx>
34 #include <sal/types.h>
35 #include <svl/sharedstring.hxx>
36 #include <tools/mempool.hxx>
40 struct ScComplexRefData
;
41 struct ScSingleRefData
;
42 enum class FormulaError
: sal_uInt16
;
57 svExternal
, // Byte + String
58 svFAP
, // FormulaAutoPilot only, ever exported
59 svJumpMatrix
, // 2003-07-02
60 svRefList
, // ocUnion result
61 svEmptyCell
, // Result is an empty cell, e.g. in LOOKUP()
63 svMatrixCell
, // Result is a matrix with bells and
64 // whistles as needed for _the_ matrix
67 svHybridCell
, // A temporary condition of a formula
68 // cell during import, having a double
69 // and/or string result and a formula
70 // string to be compiled.
77 svSubroutine
, // A token with a subroutine token array.
78 svError
, // error token
79 svMissing
= 0x70, // 0 or ""
80 svSep
, // separator, ocSep, ocOpen, ocClose
81 svUnknown
// unknown StackType
85 // save memory since compilers tend to int an enum
86 typedef sal_uInt8 StackVar
;
88 // have enum names in debugger
89 typedef StackVarEnum StackVar
;
92 class FORMULA_DLLPUBLIC FormulaToken
: public IFormulaToken
95 FormulaToken
& operator=( const FormulaToken
& ) = delete;
98 const StackVar eType
; // type of data
99 mutable oslInterlockedCount mnRefCnt
; // reference count
102 FormulaToken( StackVar eTypeP
,OpCode e
= ocPush
);
103 FormulaToken( const FormulaToken
& r
);
105 virtual ~FormulaToken();
107 inline void Delete() { delete this; }
108 inline void DeleteIfZeroRef() { if (mnRefCnt
== 0) delete this; }
109 inline StackVar
GetType() const { return eType
; }
110 bool IsFunction() const; // pure functions, no operators
112 bool IsExternalRef() const;
115 sal_uInt8
GetParamCount() const;
117 inline void IncRef() const
119 osl_atomic_increment(&mnRefCnt
);
122 inline void DecRef() const
124 if (!osl_atomic_decrement(&mnRefCnt
))
125 const_cast<FormulaToken
*>(this)->Delete();
128 inline oslInterlockedCount
GetRef() const { return mnRefCnt
; }
129 inline OpCode
GetOpCode() const { return eOp
; }
132 Dummy methods to avoid switches and casts where possible,
133 the real token classes have to override the appropriate method[s].
134 The only methods valid anytime if not overridden are:
136 - GetByte() since this represents the count of parameters to a function
137 which of course is 0 on non-functions. FormulaByteToken and ScExternal do
140 - IsInForceArray() since also this is only used for operators and
141 functions and is false for other tokens.
143 Any other non-overridden method pops up an assertion.
146 virtual sal_uInt8
GetByte() const;
147 virtual void SetByte( sal_uInt8 n
);
148 virtual bool IsInForceArray() const;
149 virtual void SetInForceArray( bool b
);
150 virtual double GetDouble() const;
151 virtual double& GetDoubleAsReference();
152 virtual short GetDoubleType() const;
153 virtual svl::SharedString
GetString() const;
154 virtual void SetString( const svl::SharedString
& rStr
);
155 virtual sal_uInt16
GetIndex() const;
156 virtual void SetIndex( sal_uInt16 n
);
157 virtual sal_Int16
GetSheet() const;
158 virtual void SetSheet( sal_Int16 n
);
159 virtual short* GetJump() const;
160 virtual const OUString
& GetExternal() const;
161 virtual FormulaToken
* GetFAPOrigToken() const;
162 virtual FormulaError
GetError() const;
163 virtual void SetError( FormulaError
);
165 virtual const ScSingleRefData
* GetSingleRef() const;
166 virtual ScSingleRefData
* GetSingleRef();
167 virtual const ScComplexRefData
* GetDoubleRef() const;
168 virtual ScComplexRefData
* GetDoubleRef();
169 virtual const ScSingleRefData
* GetSingleRef2() const;
170 virtual ScSingleRefData
* GetSingleRef2();
171 virtual const ScMatrix
* GetMatrix() const;
172 virtual ScMatrix
* GetMatrix();
173 virtual ScJumpMatrix
* GetJumpMatrix() const;
174 virtual const std::vector
<ScComplexRefData
>* GetRefList() const;
175 virtual std::vector
<ScComplexRefData
>* GetRefList();
177 virtual FormulaToken
* Clone() const { return new FormulaToken(*this); }
179 virtual bool TextEqual( const formula::FormulaToken
& rToken
) const;
180 virtual bool operator==( const FormulaToken
& rToken
) const;
182 virtual bool isFunction() const override
187 virtual sal_uInt32
getArgumentCount() const override
189 return GetParamCount();
192 /** This is dirty and only the compiler should use it! */
193 struct PrivateAccess
{ friend class FormulaCompiler
; private: PrivateAccess() { } };
194 inline void NewOpCode( OpCode e
, const PrivateAccess
& ) { eOp
= e
; }
197 inline void intrusive_ptr_add_ref(const FormulaToken
* p
)
202 inline void intrusive_ptr_release(const FormulaToken
* p
)
207 class FORMULA_DLLPUBLIC FormulaByteToken
: public FormulaToken
211 bool bIsInForceArray
;
213 FormulaByteToken( OpCode e
, sal_uInt8 n
, StackVar v
, bool b
) :
214 FormulaToken( v
,e
), nByte( n
),
215 bIsInForceArray( b
) {}
217 FormulaByteToken( OpCode e
, sal_uInt8 n
, bool b
) :
218 FormulaToken( svByte
,e
), nByte( n
),
219 bIsInForceArray( b
) {}
220 FormulaByteToken( OpCode e
, sal_uInt8 n
) :
221 FormulaToken( svByte
,e
), nByte( n
),
222 bIsInForceArray( false ) {}
223 FormulaByteToken( OpCode e
) :
224 FormulaToken( svByte
,e
), nByte( 0 ),
225 bIsInForceArray( false ) {}
226 FormulaByteToken( const FormulaByteToken
& r
) :
227 FormulaToken( r
), nByte( r
.nByte
),
228 bIsInForceArray( r
.bIsInForceArray
) {}
230 virtual FormulaToken
* Clone() const override
{ return new FormulaByteToken(*this); }
231 virtual sal_uInt8
GetByte() const override
;
232 virtual void SetByte( sal_uInt8 n
) override
;
233 virtual bool IsInForceArray() const override
;
234 virtual void SetInForceArray( bool b
) override
;
235 virtual bool operator==( const FormulaToken
& rToken
) const override
;
237 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaByteToken
)
241 // A special token for the FormulaAutoPilot only. Keeps a reference pointer of
242 // the token of which it was created for comparison.
243 class FORMULA_DLLPUBLIC FormulaFAPToken
: public FormulaByteToken
246 FormulaTokenRef pOrigToken
;
248 FormulaFAPToken( OpCode e
, sal_uInt8 n
, FormulaToken
* p
) :
249 FormulaByteToken( e
, n
, svFAP
, false ),
251 FormulaFAPToken( const FormulaFAPToken
& r
) :
252 FormulaByteToken( r
), pOrigToken( r
.pOrigToken
) {}
254 virtual FormulaToken
* Clone() const override
{ return new FormulaFAPToken(*this); }
255 virtual FormulaToken
* GetFAPOrigToken() const override
;
256 virtual bool operator==( const FormulaToken
& rToken
) const override
;
259 class FORMULA_DLLPUBLIC FormulaDoubleToken
: public FormulaToken
264 FormulaDoubleToken( double f
) :
265 FormulaToken( svDouble
), fDouble( f
) {}
266 FormulaDoubleToken( const FormulaDoubleToken
& r
) :
267 FormulaToken( r
), fDouble( r
.fDouble
) {}
269 virtual FormulaToken
* Clone() const override
{ return new FormulaDoubleToken(*this); }
270 virtual double GetDouble() const override
;
271 virtual double& GetDoubleAsReference() override
;
272 virtual short GetDoubleType() const override
; ///< always returns 0 for "not typed"
273 virtual bool operator==( const FormulaToken
& rToken
) const override
;
275 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaDoubleToken
)
278 class FORMULA_DLLPUBLIC FormulaTypedDoubleToken
: public FormulaDoubleToken
281 short mnType
; /**< Can hold, for example, a value
282 of css::util::NumberFormat, or by
286 FormulaTypedDoubleToken( double f
, short nType
) :
287 FormulaDoubleToken( f
), mnType( nType
) {}
288 FormulaTypedDoubleToken( const FormulaTypedDoubleToken
& r
) :
289 FormulaDoubleToken( r
), mnType( r
.mnType
) {}
291 virtual FormulaToken
* Clone() const override
{ return new FormulaTypedDoubleToken(*this); }
292 virtual short GetDoubleType() const override
;
293 virtual bool operator==( const FormulaToken
& rToken
) const override
;
295 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaTypedDoubleToken
)
299 class FORMULA_DLLPUBLIC FormulaStringToken
: public FormulaToken
301 svl::SharedString maString
;
303 FormulaStringToken( const svl::SharedString
& r
);
304 FormulaStringToken( const FormulaStringToken
& r
);
306 virtual FormulaToken
* Clone() const override
;
307 virtual svl::SharedString
GetString() const override
;
308 virtual void SetString( const svl::SharedString
& rStr
) override
;
309 virtual bool operator==( const FormulaToken
& rToken
) const override
;
311 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaStringToken
)
315 /** Identical to FormulaStringToken, but with explicit OpCode instead of implicit
316 ocPush, and an optional sal_uInt8 for ocBad tokens. */
317 class FORMULA_DLLPUBLIC FormulaStringOpToken
: public FormulaByteToken
319 svl::SharedString maString
;
321 FormulaStringOpToken( OpCode e
, const svl::SharedString
& r
);
322 FormulaStringOpToken( const FormulaStringOpToken
& r
);
324 virtual FormulaToken
* Clone() const override
;
325 virtual svl::SharedString
GetString() const override
;
326 virtual void SetString( const svl::SharedString
& rStr
) override
;
327 virtual bool operator==( const FormulaToken
& rToken
) const override
;
330 class FORMULA_DLLPUBLIC FormulaIndexToken
: public FormulaToken
336 FormulaIndexToken( OpCode e
, sal_uInt16 n
, sal_Int16 nSheet
= -1 ) :
337 FormulaToken( svIndex
, e
), nIndex( n
), mnSheet( nSheet
) {}
338 FormulaIndexToken( const FormulaIndexToken
& r
) :
339 FormulaToken( r
), nIndex( r
.nIndex
), mnSheet( r
.mnSheet
) {}
341 virtual FormulaToken
* Clone() const override
{ return new FormulaIndexToken(*this); }
342 virtual sal_uInt16
GetIndex() const override
;
343 virtual void SetIndex( sal_uInt16 n
) override
;
344 virtual sal_Int16
GetSheet() const override
;
345 virtual void SetSheet( sal_Int16 n
) override
;
346 virtual bool operator==( const FormulaToken
& rToken
) const override
;
350 class FORMULA_DLLPUBLIC FormulaExternalToken
: public FormulaToken
356 FormulaExternalToken( OpCode e
, sal_uInt8 n
, const OUString
& r
) :
357 FormulaToken( svExternal
, e
), aExternal( r
),
359 FormulaExternalToken( OpCode e
, const OUString
& r
) :
360 FormulaToken(svExternal
, e
), aExternal( r
),
362 FormulaExternalToken( const FormulaExternalToken
& r
) :
363 FormulaToken( r
), aExternal( r
.aExternal
),
366 virtual FormulaToken
* Clone() const override
{ return new FormulaExternalToken(*this); }
367 virtual const OUString
& GetExternal() const override
;
368 virtual sal_uInt8
GetByte() const override
;
369 virtual void SetByte( sal_uInt8 n
) override
;
370 virtual bool operator==( const FormulaToken
& rToken
) const override
;
374 class FORMULA_DLLPUBLIC FormulaMissingToken
: public FormulaToken
377 FormulaMissingToken() :
378 FormulaToken( svMissing
,ocMissing
) {}
379 FormulaMissingToken( const FormulaMissingToken
& r
) :
382 virtual FormulaToken
* Clone() const override
{ return new FormulaMissingToken(*this); }
383 virtual double GetDouble() const override
;
384 virtual svl::SharedString
GetString() const override
;
385 virtual bool operator==( const FormulaToken
& rToken
) const override
;
388 class FORMULA_DLLPUBLIC FormulaJumpToken
: public FormulaToken
392 bool bIsInForceArray
;
394 FormulaJumpToken( OpCode e
, short* p
) :
395 FormulaToken( formula::svJump
, e
),
396 bIsInForceArray( false)
398 pJump
= new short[ p
[0] + 1 ];
399 memcpy( pJump
, p
, (p
[0] + 1) * sizeof(short) );
401 FormulaJumpToken( const FormulaJumpToken
& r
) :
403 bIsInForceArray( r
.bIsInForceArray
)
405 pJump
= new short[ r
.pJump
[0] + 1 ];
406 memcpy( pJump
, r
.pJump
, (r
.pJump
[0] + 1) * sizeof(short) );
408 virtual ~FormulaJumpToken() override
;
409 virtual short* GetJump() const override
;
410 virtual bool operator==( const formula::FormulaToken
& rToken
) const override
;
411 virtual FormulaToken
* Clone() const override
{ return new FormulaJumpToken(*this); }
412 virtual bool IsInForceArray() const override
;
413 virtual void SetInForceArray( bool b
) override
;
417 class FORMULA_DLLPUBLIC FormulaUnknownToken
: public FormulaToken
420 FormulaUnknownToken( OpCode e
) :
421 FormulaToken( svUnknown
, e
) {}
422 FormulaUnknownToken( const FormulaUnknownToken
& r
) :
425 virtual FormulaToken
* Clone() const override
{ return new FormulaUnknownToken(*this); }
426 virtual bool operator==( const FormulaToken
& rToken
) const override
;
430 class FORMULA_DLLPUBLIC FormulaErrorToken
: public FormulaToken
434 FormulaErrorToken( FormulaError nErr
) :
435 FormulaToken( svError
), nError( nErr
) {}
436 FormulaErrorToken( const FormulaErrorToken
& r
) :
437 FormulaToken( r
), nError( r
.nError
) {}
439 virtual FormulaToken
* Clone() const override
{ return new FormulaErrorToken(*this); }
440 virtual FormulaError
GetError() const override
;
441 virtual void SetError( FormulaError nErr
) override
;
442 virtual bool operator==( const FormulaToken
& rToken
) const override
;
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */