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
26 #include <formula/opcode.hxx>
27 #include <tools/mempool.hxx>
28 #include <formula/IFunctionDescription.hxx>
29 #include <formula/formuladllapi.h>
30 #include <formula/types.hxx>
31 #include <svl/sharedstring.hxx>
32 #include <osl/interlck.h>
47 svExternal
, // Byte + String
48 svFAP
, // FormulaAutoPilot only, ever exported
49 svJumpMatrix
, // 2003-07-02
50 svRefList
, // ocUnion result
51 svEmptyCell
, // Result is an empty cell, e.g. in LOOKUP()
53 svMatrixCell
, // Result is a matrix with bells and
54 // whistles as needed for _the_ matrix
57 svHybridCell
, // A temporary condition of a formula
58 // cell during import, having a double
59 // and/or string result and a formula
60 // string to be compiled.
62 svHybridValueCell
, // A temporary formula cell with an value
63 // and possibily a string representation
70 svSubroutine
, // A token with a subroutine token array.
71 svError
, // error token
72 svMissing
= 0x70, // 0 or ""
73 svSep
, // separator, ocSep, ocOpen, ocClose
74 svUnknown
// unknown StackType
78 // save memory since compilers tend to int an enum
79 typedef sal_uInt8 StackVar
;
81 // have enum names in debugger
82 typedef StackVarEnum StackVar
;
85 class FormulaTokenArray
;
87 class FORMULA_DLLPUBLIC FormulaToken
: public IFormulaToken
90 // not implemented, prevent usage
92 FormulaToken
& operator=( const FormulaToken
& );
95 const StackVar eType
; // type of data
96 mutable oslInterlockedCount mnRefCnt
; // reference count
99 FormulaToken( StackVar eTypeP
,OpCode e
= ocPush
) :
100 eOp(e
), eType( eTypeP
), mnRefCnt(0) {}
101 FormulaToken( const FormulaToken
& r
) : IFormulaToken(),
102 eOp(r
.eOp
), eType( r
.eType
), mnRefCnt(0) {}
104 virtual ~FormulaToken();
106 inline void Delete() { delete this; }
107 inline StackVar
GetType() const { return eType
; }
108 bool IsFunction() const; // pure functions, no operators
110 bool IsExternalRef() const;
113 sal_uInt8
GetParamCount() const;
115 inline void IncRef() const
117 osl_atomic_increment(&mnRefCnt
);
120 inline void DecRef() const
122 if (!osl_atomic_decrement(&mnRefCnt
))
123 const_cast<FormulaToken
*>(this)->Delete();
126 inline oslInterlockedCount
GetRef() const { return mnRefCnt
; }
127 inline OpCode
GetOpCode() const { return eOp
; }
130 Dummy methods to avoid switches and casts where possible,
131 the real token classes have to overload the appropriate method[s].
132 The only methods valid anytime if not overloaded are:
134 - GetByte() since this represents the count of parameters to a function
135 which of course is 0 on non-functions. FormulaByteToken and ScExternal do
138 - HasForceArray() since also this is only used for operators and
139 functions and is 0 for other tokens.
141 Any other non-overloaded method pops up an assertion.
144 virtual sal_uInt8
GetByte() const;
145 virtual void SetByte( sal_uInt8 n
);
146 virtual bool HasForceArray() const;
147 virtual void SetForceArray( bool b
);
148 virtual double GetDouble() const;
149 virtual double& GetDoubleAsReference();
150 virtual svl::SharedString
GetString() const;
151 virtual sal_uInt16
GetIndex() const;
152 virtual void SetIndex( sal_uInt16 n
);
153 virtual bool IsGlobal() const;
154 virtual void SetGlobal( bool b
);
155 virtual short* GetJump() const;
156 virtual const OUString
& GetExternal() const;
157 virtual FormulaToken
* GetFAPOrigToken() const;
158 virtual sal_uInt16
GetError() const;
159 virtual void SetError( sal_uInt16
);
161 virtual FormulaToken
* Clone() const { return new FormulaToken(*this); }
163 virtual bool Is3DRef() const; // reference with 3D flag set
164 virtual bool TextEqual( const formula::FormulaToken
& rToken
) const;
165 virtual bool operator==( const FormulaToken
& rToken
) const;
167 virtual bool isFunction() const
172 virtual sal_uInt32
getArgumentCount() const
174 return GetParamCount();
177 /** This is dirty and only the compiler should use it! */
178 struct PrivateAccess
{ friend class FormulaCompiler
; private: PrivateAccess() { } };
179 inline void NewOpCode( OpCode e
, const PrivateAccess
& ) { eOp
= e
; }
181 static size_t GetStrLenBytes( xub_StrLen nLen
)
182 { return nLen
* sizeof(sal_Unicode
); }
183 static size_t GetStrLenBytes( const OUString
& rStr
)
184 { return GetStrLenBytes( rStr
.getLength() ); }
187 inline void intrusive_ptr_add_ref(const FormulaToken
* p
)
192 inline void intrusive_ptr_release(const FormulaToken
* p
)
197 class FORMULA_DLLPUBLIC FormulaByteToken
: public FormulaToken
203 FormulaByteToken( OpCode e
, sal_uInt8 n
, StackVar v
, bool b
) :
204 FormulaToken( v
,e
), nByte( n
),
205 bHasForceArray( b
) {}
207 FormulaByteToken( OpCode e
, sal_uInt8 n
, bool b
) :
208 FormulaToken( svByte
,e
), nByte( n
),
209 bHasForceArray( b
) {}
210 FormulaByteToken( OpCode e
, sal_uInt8 n
) :
211 FormulaToken( svByte
,e
), nByte( n
),
212 bHasForceArray( false ) {}
213 FormulaByteToken( OpCode e
) :
214 FormulaToken( svByte
,e
), nByte( 0 ),
215 bHasForceArray( false ) {}
216 FormulaByteToken( const FormulaByteToken
& r
) :
217 FormulaToken( r
), nByte( r
.nByte
),
218 bHasForceArray( r
.bHasForceArray
) {}
220 virtual FormulaToken
* Clone() const { return new FormulaByteToken(*this); }
221 virtual sal_uInt8
GetByte() const;
222 virtual void SetByte( sal_uInt8 n
);
223 virtual bool HasForceArray() const;
224 virtual void SetForceArray( bool b
);
225 virtual bool operator==( const FormulaToken
& rToken
) const;
227 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaByteToken
)
231 // A special token for the FormulaAutoPilot only. Keeps a reference pointer of
232 // the token of which it was created for comparison.
233 class FORMULA_DLLPUBLIC FormulaFAPToken
: public FormulaByteToken
236 FormulaTokenRef pOrigToken
;
238 FormulaFAPToken( OpCode e
, sal_uInt8 n
, FormulaToken
* p
) :
239 FormulaByteToken( e
, n
, svFAP
, false ),
241 FormulaFAPToken( const FormulaFAPToken
& r
) :
242 FormulaByteToken( r
), pOrigToken( r
.pOrigToken
) {}
244 virtual FormulaToken
* Clone() const { return new FormulaFAPToken(*this); }
245 virtual FormulaToken
* GetFAPOrigToken() const;
246 virtual bool operator==( const FormulaToken
& rToken
) const;
249 class FORMULA_DLLPUBLIC FormulaDoubleToken
: public FormulaToken
254 FormulaDoubleToken( double f
) :
255 FormulaToken( svDouble
), fDouble( f
) {}
256 FormulaDoubleToken( const FormulaDoubleToken
& r
) :
257 FormulaToken( r
), fDouble( r
.fDouble
) {}
259 virtual FormulaToken
* Clone() const { return new FormulaDoubleToken(*this); }
260 virtual double GetDouble() const;
261 virtual double& GetDoubleAsReference();
262 virtual bool operator==( const FormulaToken
& rToken
) const;
264 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaDoubleToken
)
268 class FORMULA_DLLPUBLIC FormulaStringToken
: public FormulaToken
270 svl::SharedString maString
;
272 FormulaStringToken( const svl::SharedString
& r
);
273 FormulaStringToken( const FormulaStringToken
& r
);
275 virtual FormulaToken
* Clone() const;
276 virtual svl::SharedString
GetString() const;
277 virtual bool operator==( const FormulaToken
& rToken
) const;
279 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaStringToken
)
283 /** Identical to FormulaStringToken, but with explicit OpCode instead of implicit
284 ocPush, and an optional sal_uInt8 for ocBad tokens. */
285 class FORMULA_DLLPUBLIC FormulaStringOpToken
: public FormulaByteToken
287 svl::SharedString maString
;
289 FormulaStringOpToken( OpCode e
, const svl::SharedString
& r
);
290 FormulaStringOpToken( const FormulaStringOpToken
& r
);
292 virtual FormulaToken
* Clone() const;
293 virtual svl::SharedString
GetString() const;
294 virtual bool operator==( const FormulaToken
& rToken
) const;
297 class FORMULA_DLLPUBLIC FormulaIndexToken
: public FormulaToken
303 FormulaIndexToken( OpCode e
, sal_uInt16 n
, bool bGlobal
= true ) :
304 FormulaToken( svIndex
, e
), nIndex( n
), mbGlobal( bGlobal
) {}
305 FormulaIndexToken( const FormulaIndexToken
& r
) :
306 FormulaToken( r
), nIndex( r
.nIndex
), mbGlobal( r
.mbGlobal
) {}
308 virtual FormulaToken
* Clone() const { return new FormulaIndexToken(*this); }
309 virtual sal_uInt16
GetIndex() const;
310 virtual void SetIndex( sal_uInt16 n
);
311 virtual bool IsGlobal() const;
312 virtual void SetGlobal( bool b
);
313 virtual bool operator==( const FormulaToken
& rToken
) const;
317 class FORMULA_DLLPUBLIC FormulaExternalToken
: public FormulaToken
323 FormulaExternalToken( OpCode e
, sal_uInt8 n
, const OUString
& r
) :
324 FormulaToken( svExternal
, e
), aExternal( r
),
326 FormulaExternalToken( OpCode e
, const OUString
& r
) :
327 FormulaToken(svExternal
, e
), aExternal( r
),
329 FormulaExternalToken( const FormulaExternalToken
& r
) :
330 FormulaToken( r
), aExternal( r
.aExternal
),
333 virtual FormulaToken
* Clone() const { return new FormulaExternalToken(*this); }
334 virtual const OUString
& GetExternal() const;
335 virtual sal_uInt8
GetByte() const;
336 virtual void SetByte( sal_uInt8 n
);
337 virtual bool operator==( const FormulaToken
& rToken
) const;
341 class FORMULA_DLLPUBLIC FormulaMissingToken
: public FormulaToken
344 FormulaMissingToken() :
345 FormulaToken( svMissing
,ocMissing
) {}
346 FormulaMissingToken( const FormulaMissingToken
& r
) :
349 virtual FormulaToken
* Clone() const { return new FormulaMissingToken(*this); }
350 virtual double GetDouble() const;
351 virtual svl::SharedString
GetString() const;
352 virtual bool operator==( const FormulaToken
& rToken
) const;
355 class FORMULA_DLLPUBLIC FormulaJumpToken
: public FormulaToken
360 FormulaJumpToken( OpCode e
, short* p
) :
361 FormulaToken( formula::svJump
, e
)
363 pJump
= new short[ p
[0] + 1 ];
364 memcpy( pJump
, p
, (p
[0] + 1) * sizeof(short) );
366 FormulaJumpToken( const FormulaJumpToken
& r
) :
369 pJump
= new short[ r
.pJump
[0] + 1 ];
370 memcpy( pJump
, r
.pJump
, (r
.pJump
[0] + 1) * sizeof(short) );
372 virtual ~FormulaJumpToken();
373 virtual short* GetJump() const;
374 virtual bool operator==( const formula::FormulaToken
& rToken
) const;
375 virtual FormulaToken
* Clone() const { return new FormulaJumpToken(*this); }
379 class FORMULA_DLLPUBLIC FormulaSubroutineToken
: public FormulaToken
382 /** Takes ownership of pArray and deletes it upon destruction! */
383 FormulaSubroutineToken( const FormulaTokenArray
* pArray
) :
384 FormulaToken( svSubroutine
, ocCall
), mpArray( pArray
) {}
385 FormulaSubroutineToken( const FormulaSubroutineToken
& r
);
386 virtual ~FormulaSubroutineToken();
387 virtual FormulaToken
* Clone() const { return new FormulaSubroutineToken(*this); }
388 virtual bool operator==( const FormulaToken
& rToken
) const;
391 const FormulaTokenArray
* mpArray
;
395 class FORMULA_DLLPUBLIC FormulaUnknownToken
: public FormulaToken
398 FormulaUnknownToken( OpCode e
) :
399 FormulaToken( svUnknown
, e
) {}
400 FormulaUnknownToken( const FormulaUnknownToken
& r
) :
403 virtual FormulaToken
* Clone() const { return new FormulaUnknownToken(*this); }
404 virtual bool operator==( const FormulaToken
& rToken
) const;
408 class FORMULA_DLLPUBLIC FormulaErrorToken
: public FormulaToken
412 FormulaErrorToken( sal_uInt16 nErr
) :
413 FormulaToken( svError
), nError( nErr
) {}
414 FormulaErrorToken( const FormulaErrorToken
& r
) :
415 FormulaToken( r
), nError( r
.nError
) {}
417 virtual FormulaToken
* Clone() const { return new FormulaErrorToken(*this); }
418 virtual sal_uInt16
GetError() const;
419 virtual void SetError( sal_uInt16 nErr
);
420 virtual bool operator==( const FormulaToken
& rToken
) const;
423 // =============================================================================
425 // =============================================================================
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */