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
24 #include <formula/opcode.hxx>
25 #include <tools/mempool.hxx>
26 #include <formula/IFunctionDescription.hxx>
27 #include <formula/formuladllapi.h>
28 #include <formula/types.hxx>
29 #include <svl/sharedstring.hxx>
30 #include <osl/interlck.h>
45 svExternal
, // Byte + String
46 svFAP
, // FormulaAutoPilot only, ever exported
47 svJumpMatrix
, // 2003-07-02
48 svRefList
, // ocUnion result
49 svEmptyCell
, // Result is an empty cell, e.g. in LOOKUP()
51 svMatrixCell
, // Result is a matrix with bells and
52 // whistles as needed for _the_ matrix
55 svHybridCell
, // A temporary condition of a formula
56 // cell during import, having a double
57 // and/or string result and a formula
58 // string to be compiled.
60 svHybridValueCell
, // A temporary formula cell with an value
61 // and possibily a string representation
68 svSubroutine
, // A token with a subroutine token array.
69 svError
, // error token
70 svMissing
= 0x70, // 0 or ""
71 svSep
, // separator, ocSep, ocOpen, ocClose
72 svUnknown
// unknown StackType
76 // save memory since compilers tend to int an enum
77 typedef sal_uInt8 StackVar
;
79 // have enum names in debugger
80 typedef StackVarEnum StackVar
;
83 class FormulaTokenArray
;
85 class FORMULA_DLLPUBLIC FormulaToken
: public IFormulaToken
88 // not implemented, prevent usage
90 FormulaToken
& operator=( const FormulaToken
& );
93 const StackVar eType
; // type of data
94 mutable oslInterlockedCount mnRefCnt
; // reference count
97 FormulaToken( StackVar eTypeP
,OpCode e
= ocPush
);
98 FormulaToken( const FormulaToken
& r
);
100 virtual ~FormulaToken();
102 inline void Delete() { delete this; }
103 inline StackVar
GetType() const { return eType
; }
104 bool IsFunction() const; // pure functions, no operators
106 bool IsExternalRef() const;
109 sal_uInt8
GetParamCount() const;
111 inline void IncRef() const
113 osl_atomic_increment(&mnRefCnt
);
116 inline void DecRef() const
118 if (!osl_atomic_decrement(&mnRefCnt
))
119 const_cast<FormulaToken
*>(this)->Delete();
122 inline oslInterlockedCount
GetRef() const { return mnRefCnt
; }
123 inline OpCode
GetOpCode() const { return eOp
; }
126 Dummy methods to avoid switches and casts where possible,
127 the real token classes have to overload the appropriate method[s].
128 The only methods valid anytime if not overloaded are:
130 - GetByte() since this represents the count of parameters to a function
131 which of course is 0 on non-functions. FormulaByteToken and ScExternal do
134 - HasForceArray() since also this is only used for operators and
135 functions and is 0 for other tokens.
137 Any other non-overloaded method pops up an assertion.
140 virtual sal_uInt8
GetByte() const;
141 virtual void SetByte( sal_uInt8 n
);
142 virtual bool HasForceArray() const;
143 virtual void SetForceArray( bool b
);
144 virtual double GetDouble() const;
145 virtual double& GetDoubleAsReference();
146 virtual svl::SharedString
GetString() const;
147 virtual sal_uInt16
GetIndex() const;
148 virtual void SetIndex( sal_uInt16 n
);
149 virtual bool IsGlobal() const;
150 virtual void SetGlobal( bool b
);
151 virtual short* GetJump() const;
152 virtual const OUString
& GetExternal() const;
153 virtual FormulaToken
* GetFAPOrigToken() const;
154 virtual sal_uInt16
GetError() const;
155 virtual void SetError( sal_uInt16
);
157 virtual FormulaToken
* Clone() const { return new FormulaToken(*this); }
159 virtual bool Is3DRef() const; // reference with 3D flag set
160 virtual bool TextEqual( const formula::FormulaToken
& rToken
) const;
161 virtual bool operator==( const FormulaToken
& rToken
) const;
163 virtual bool isFunction() const SAL_OVERRIDE
168 virtual sal_uInt32
getArgumentCount() const SAL_OVERRIDE
170 return GetParamCount();
173 /** This is dirty and only the compiler should use it! */
174 struct PrivateAccess
{ friend class FormulaCompiler
; private: PrivateAccess() { } };
175 inline void NewOpCode( OpCode e
, const PrivateAccess
& ) { eOp
= e
; }
177 static sal_Int32
GetStrLenBytes( sal_Int32 nLen
)
178 { return nLen
* sizeof(sal_Unicode
); }
179 static sal_Int32
GetStrLenBytes( const OUString
& rStr
)
180 { return GetStrLenBytes( rStr
.getLength() ); }
183 inline void intrusive_ptr_add_ref(const FormulaToken
* p
)
188 inline void intrusive_ptr_release(const FormulaToken
* p
)
193 class FORMULA_DLLPUBLIC FormulaByteToken
: public FormulaToken
199 FormulaByteToken( OpCode e
, sal_uInt8 n
, StackVar v
, bool b
) :
200 FormulaToken( v
,e
), nByte( n
),
201 bHasForceArray( b
) {}
203 FormulaByteToken( OpCode e
, sal_uInt8 n
, bool b
) :
204 FormulaToken( svByte
,e
), nByte( n
),
205 bHasForceArray( b
) {}
206 FormulaByteToken( OpCode e
, sal_uInt8 n
) :
207 FormulaToken( svByte
,e
), nByte( n
),
208 bHasForceArray( false ) {}
209 FormulaByteToken( OpCode e
) :
210 FormulaToken( svByte
,e
), nByte( 0 ),
211 bHasForceArray( false ) {}
212 FormulaByteToken( const FormulaByteToken
& r
) :
213 FormulaToken( r
), nByte( r
.nByte
),
214 bHasForceArray( r
.bHasForceArray
) {}
216 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaByteToken(*this); }
217 virtual sal_uInt8
GetByte() const SAL_OVERRIDE
;
218 virtual void SetByte( sal_uInt8 n
) SAL_OVERRIDE
;
219 virtual bool HasForceArray() const SAL_OVERRIDE
;
220 virtual void SetForceArray( bool b
) SAL_OVERRIDE
;
221 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
223 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaByteToken
)
227 // A special token for the FormulaAutoPilot only. Keeps a reference pointer of
228 // the token of which it was created for comparison.
229 class FORMULA_DLLPUBLIC FormulaFAPToken
: public FormulaByteToken
232 FormulaTokenRef pOrigToken
;
234 FormulaFAPToken( OpCode e
, sal_uInt8 n
, FormulaToken
* p
) :
235 FormulaByteToken( e
, n
, svFAP
, false ),
237 FormulaFAPToken( const FormulaFAPToken
& r
) :
238 FormulaByteToken( r
), pOrigToken( r
.pOrigToken
) {}
240 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaFAPToken(*this); }
241 virtual FormulaToken
* GetFAPOrigToken() const SAL_OVERRIDE
;
242 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
245 class FORMULA_DLLPUBLIC FormulaDoubleToken
: public FormulaToken
250 FormulaDoubleToken( double f
) :
251 FormulaToken( svDouble
), fDouble( f
) {}
252 FormulaDoubleToken( const FormulaDoubleToken
& r
) :
253 FormulaToken( r
), fDouble( r
.fDouble
) {}
255 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaDoubleToken(*this); }
256 virtual double GetDouble() const SAL_OVERRIDE
;
257 virtual double& GetDoubleAsReference() SAL_OVERRIDE
;
258 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
260 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaDoubleToken
)
264 class FORMULA_DLLPUBLIC FormulaStringToken
: public FormulaToken
266 svl::SharedString maString
;
268 FormulaStringToken( const svl::SharedString
& r
);
269 FormulaStringToken( const FormulaStringToken
& r
);
271 virtual FormulaToken
* Clone() const SAL_OVERRIDE
;
272 virtual svl::SharedString
GetString() const SAL_OVERRIDE
;
273 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
275 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaStringToken
)
279 /** Identical to FormulaStringToken, but with explicit OpCode instead of implicit
280 ocPush, and an optional sal_uInt8 for ocBad tokens. */
281 class FORMULA_DLLPUBLIC FormulaStringOpToken
: public FormulaByteToken
283 svl::SharedString maString
;
285 FormulaStringOpToken( OpCode e
, const svl::SharedString
& r
);
286 FormulaStringOpToken( const FormulaStringOpToken
& r
);
288 virtual FormulaToken
* Clone() const SAL_OVERRIDE
;
289 virtual svl::SharedString
GetString() const SAL_OVERRIDE
;
290 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
293 class FORMULA_DLLPUBLIC FormulaIndexToken
: public FormulaToken
299 FormulaIndexToken( OpCode e
, sal_uInt16 n
, bool bGlobal
= true ) :
300 FormulaToken( svIndex
, e
), nIndex( n
), mbGlobal( bGlobal
) {}
301 FormulaIndexToken( const FormulaIndexToken
& r
) :
302 FormulaToken( r
), nIndex( r
.nIndex
), mbGlobal( r
.mbGlobal
) {}
304 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaIndexToken(*this); }
305 virtual sal_uInt16
GetIndex() const SAL_OVERRIDE
;
306 virtual void SetIndex( sal_uInt16 n
) SAL_OVERRIDE
;
307 virtual bool IsGlobal() const SAL_OVERRIDE
;
308 virtual void SetGlobal( bool b
) SAL_OVERRIDE
;
309 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
313 class FORMULA_DLLPUBLIC FormulaExternalToken
: public FormulaToken
319 FormulaExternalToken( OpCode e
, sal_uInt8 n
, const OUString
& r
) :
320 FormulaToken( svExternal
, e
), aExternal( r
),
322 FormulaExternalToken( OpCode e
, const OUString
& r
) :
323 FormulaToken(svExternal
, e
), aExternal( r
),
325 FormulaExternalToken( const FormulaExternalToken
& r
) :
326 FormulaToken( r
), aExternal( r
.aExternal
),
329 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaExternalToken(*this); }
330 virtual const OUString
& GetExternal() const SAL_OVERRIDE
;
331 virtual sal_uInt8
GetByte() const SAL_OVERRIDE
;
332 virtual void SetByte( sal_uInt8 n
) SAL_OVERRIDE
;
333 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
337 class FORMULA_DLLPUBLIC FormulaMissingToken
: public FormulaToken
340 FormulaMissingToken() :
341 FormulaToken( svMissing
,ocMissing
) {}
342 FormulaMissingToken( const FormulaMissingToken
& r
) :
345 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaMissingToken(*this); }
346 virtual double GetDouble() const SAL_OVERRIDE
;
347 virtual svl::SharedString
GetString() const SAL_OVERRIDE
;
348 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
351 class FORMULA_DLLPUBLIC FormulaJumpToken
: public FormulaToken
357 FormulaJumpToken( OpCode e
, short* p
) :
358 FormulaToken( formula::svJump
, e
),
359 bHasForceArray( false)
361 pJump
= new short[ p
[0] + 1 ];
362 memcpy( pJump
, p
, (p
[0] + 1) * sizeof(short) );
364 FormulaJumpToken( const FormulaJumpToken
& r
) :
366 bHasForceArray( r
.bHasForceArray
)
368 pJump
= new short[ r
.pJump
[0] + 1 ];
369 memcpy( pJump
, r
.pJump
, (r
.pJump
[0] + 1) * sizeof(short) );
371 virtual ~FormulaJumpToken();
372 virtual short* GetJump() const SAL_OVERRIDE
;
373 virtual bool operator==( const formula::FormulaToken
& rToken
) const SAL_OVERRIDE
;
374 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaJumpToken(*this); }
375 virtual bool HasForceArray() const SAL_OVERRIDE
;
376 virtual void SetForceArray( bool b
) SAL_OVERRIDE
;
380 class FORMULA_DLLPUBLIC FormulaSubroutineToken
: public FormulaToken
383 /** Takes ownership of pArray and deletes it upon destruction! */
384 FormulaSubroutineToken( const FormulaTokenArray
* pArray
) :
385 FormulaToken( svSubroutine
, ocCall
), mpArray( pArray
) {}
386 FormulaSubroutineToken( const FormulaSubroutineToken
& r
);
387 virtual ~FormulaSubroutineToken();
388 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaSubroutineToken(*this); }
389 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
392 const FormulaTokenArray
* mpArray
;
396 class FORMULA_DLLPUBLIC FormulaUnknownToken
: public FormulaToken
399 FormulaUnknownToken( OpCode e
) :
400 FormulaToken( svUnknown
, e
) {}
401 FormulaUnknownToken( const FormulaUnknownToken
& r
) :
404 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaUnknownToken(*this); }
405 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
409 class FORMULA_DLLPUBLIC FormulaErrorToken
: public FormulaToken
413 FormulaErrorToken( sal_uInt16 nErr
) :
414 FormulaToken( svError
), nError( nErr
) {}
415 FormulaErrorToken( const FormulaErrorToken
& r
) :
416 FormulaToken( r
), nError( r
.nError
) {}
418 virtual FormulaToken
* Clone() const SAL_OVERRIDE
{ return new FormulaErrorToken(*this); }
419 virtual sal_uInt16
GetError() const SAL_OVERRIDE
;
420 virtual void SetError( sal_uInt16 nErr
) SAL_OVERRIDE
;
421 virtual bool operator==( const FormulaToken
& rToken
) const SAL_OVERRIDE
;
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */