Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef FORMULA_TOKEN_HXX
21 : #define FORMULA_TOKEN_HXX
22 :
23 : #include <memory>
24 : #include <string.h>
25 : #include <vector>
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 :
32 : namespace formula
33 : {
34 :
35 : enum StackVarEnum
36 : {
37 : svByte,
38 : svDouble,
39 : svString,
40 : svSingleRef,
41 : svDoubleRef,
42 : svMatrix,
43 : svIndex,
44 : svJump,
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()
50 :
51 : svMatrixCell, // Result is a matrix with bells and
52 : // whistles as needed for _the_ matrix
53 : // formula result.
54 :
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.
59 :
60 : svHybridValueCell, // A temporary formula cell with an value
61 : // and possibily a string representation
62 :
63 : svExternalSingleRef,
64 : svExternalDoubleRef,
65 : svExternalName,
66 : svSingleVectorRef,
67 : svDoubleVectorRef,
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
73 : };
74 :
75 : #ifndef DBG_UTIL
76 : // save memory since compilers tend to int an enum
77 : typedef sal_uInt8 StackVar;
78 : #else
79 : // have enum names in debugger
80 : typedef StackVarEnum StackVar;
81 : #endif
82 :
83 : class FormulaTokenArray;
84 :
85 : class FORMULA_DLLPUBLIC FormulaToken : public IFormulaToken
86 : {
87 : OpCode eOp;
88 : // not implemented, prevent usage
89 : FormulaToken();
90 : FormulaToken& operator=( const FormulaToken& );
91 : protected:
92 :
93 : const StackVar eType; // type of data
94 : mutable sal_uInt16 nRefCnt; // reference count
95 :
96 : public:
97 64230 : FormulaToken( StackVar eTypeP,OpCode e = ocPush ) :
98 64230 : eOp(e), eType( eTypeP ), nRefCnt(0) {}
99 12371 : FormulaToken( const FormulaToken& r ) : IFormulaToken(),
100 12371 : eOp(r.eOp), eType( r.eType ), nRefCnt(0) {}
101 :
102 : virtual ~FormulaToken();
103 :
104 65734 : inline void Delete() { delete this; }
105 364327 : inline StackVar GetType() const { return eType; }
106 : bool IsFunction() const; // pure functions, no operators
107 : bool IsExternalRef() const;
108 : sal_uInt8 GetParamCount() const;
109 223006 : inline void IncRef() const { nRefCnt++; }
110 220206 : inline void DecRef() const
111 : {
112 220206 : if (!--nRefCnt)
113 65734 : const_cast<FormulaToken*>(this)->Delete();
114 220206 : }
115 7418 : inline sal_uInt16 GetRef() const { return nRefCnt; }
116 835377 : inline OpCode GetOpCode() const { return eOp; }
117 :
118 : /**
119 : Dummy methods to avoid switches and casts where possible,
120 : the real token classes have to overload the appropriate method[s].
121 : The only methods valid anytime if not overloaded are:
122 :
123 : - GetByte() since this represents the count of parameters to a function
124 : which of course is 0 on non-functions. FormulaByteToken and ScExternal do
125 : overload it.
126 :
127 : - HasForceArray() since also this is only used for operators and
128 : functions and is 0 for other tokens.
129 :
130 : Any other non-overloaded method pops up an assertion.
131 : */
132 :
133 : virtual sal_uInt8 GetByte() const;
134 : virtual void SetByte( sal_uInt8 n );
135 : virtual bool HasForceArray() const;
136 : virtual void SetForceArray( bool b );
137 : virtual double GetDouble() const;
138 : virtual double& GetDoubleAsReference();
139 : virtual const String& GetString() const;
140 : virtual sal_uInt16 GetIndex() const;
141 : virtual void SetIndex( sal_uInt16 n );
142 : virtual bool IsGlobal() const;
143 : virtual void SetGlobal( bool b );
144 : virtual short* GetJump() const;
145 : virtual const String& GetExternal() const;
146 : virtual FormulaToken* GetFAPOrigToken() const;
147 : virtual sal_uInt16 GetError() const;
148 : virtual void SetError( sal_uInt16 );
149 :
150 1008 : virtual FormulaToken* Clone() const { return new FormulaToken(*this); }
151 :
152 : virtual bool Is3DRef() const; // reference with 3D flag set
153 : virtual bool TextEqual( const formula::FormulaToken& rToken ) const;
154 : virtual bool operator==( const FormulaToken& rToken ) const;
155 :
156 0 : virtual bool isFunction() const
157 : {
158 0 : return IsFunction();
159 : }
160 :
161 0 : virtual sal_uInt32 getArgumentCount() const
162 : {
163 0 : return GetParamCount();
164 : }
165 :
166 : /** This is dirty and only the compiler should use it! */
167 0 : struct PrivateAccess { friend class FormulaCompiler; private: PrivateAccess() { } };
168 0 : inline void NewOpCode( OpCode e, const PrivateAccess& ) { eOp = e; }
169 :
170 : static size_t GetStrLenBytes( xub_StrLen nLen )
171 : { return nLen * sizeof(sal_Unicode); }
172 : static size_t GetStrLenBytes( const String& rStr )
173 : { return GetStrLenBytes( rStr.Len() ); }
174 : };
175 :
176 74898 : inline void intrusive_ptr_add_ref(const FormulaToken* p)
177 : {
178 74898 : p->IncRef();
179 74898 : }
180 :
181 74875 : inline void intrusive_ptr_release(const FormulaToken* p)
182 : {
183 74875 : p->DecRef();
184 74875 : }
185 :
186 33406 : class FORMULA_DLLPUBLIC FormulaByteToken : public FormulaToken
187 : {
188 : private:
189 : sal_uInt8 nByte;
190 : bool bHasForceArray;
191 : protected:
192 463 : FormulaByteToken( OpCode e, sal_uInt8 n, StackVar v, bool b ) :
193 : FormulaToken( v,e ), nByte( n ),
194 463 : bHasForceArray( b ) {}
195 : public:
196 9467 : FormulaByteToken( OpCode e, sal_uInt8 n, bool b ) :
197 : FormulaToken( svByte,e ), nByte( n ),
198 9467 : bHasForceArray( b ) {}
199 2 : FormulaByteToken( OpCode e, sal_uInt8 n ) :
200 : FormulaToken( svByte,e ), nByte( n ),
201 2 : bHasForceArray( false ) {}
202 8021 : FormulaByteToken( OpCode e ) :
203 : FormulaToken( svByte,e ), nByte( 0 ),
204 8021 : bHasForceArray( false ) {}
205 1052 : FormulaByteToken( const FormulaByteToken& r ) :
206 : FormulaToken( r ), nByte( r.nByte ),
207 1052 : bHasForceArray( r.bHasForceArray ) {}
208 :
209 1045 : virtual FormulaToken* Clone() const { return new FormulaByteToken(*this); }
210 : virtual sal_uInt8 GetByte() const;
211 : virtual void SetByte( sal_uInt8 n );
212 : virtual bool HasForceArray() const;
213 : virtual void SetForceArray( bool b );
214 : virtual bool operator==( const FormulaToken& rToken ) const;
215 :
216 : DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaByteToken )
217 : };
218 :
219 :
220 : // A special token for the FormulaAutoPilot only. Keeps a reference pointer of
221 : // the token of which it was created for comparison.
222 0 : class FORMULA_DLLPUBLIC FormulaFAPToken : public FormulaByteToken
223 : {
224 : private:
225 : FormulaTokenRef pOrigToken;
226 : public:
227 0 : FormulaFAPToken( OpCode e, sal_uInt8 n, FormulaToken* p ) :
228 : FormulaByteToken( e, n, svFAP, false ),
229 0 : pOrigToken( p ) {}
230 0 : FormulaFAPToken( const FormulaFAPToken& r ) :
231 0 : FormulaByteToken( r ), pOrigToken( r.pOrigToken ) {}
232 :
233 0 : virtual FormulaToken* Clone() const { return new FormulaFAPToken(*this); }
234 : virtual FormulaToken* GetFAPOrigToken() const;
235 : virtual bool operator==( const FormulaToken& rToken ) const;
236 : };
237 :
238 18650 : class FORMULA_DLLPUBLIC FormulaDoubleToken : public FormulaToken
239 : {
240 : private:
241 : double fDouble;
242 : public:
243 8851 : FormulaDoubleToken( double f ) :
244 8851 : FormulaToken( svDouble ), fDouble( f ) {}
245 774 : FormulaDoubleToken( const FormulaDoubleToken& r ) :
246 774 : FormulaToken( r ), fDouble( r.fDouble ) {}
247 :
248 774 : virtual FormulaToken* Clone() const { return new FormulaDoubleToken(*this); }
249 : virtual double GetDouble() const;
250 : virtual double& GetDoubleAsReference();
251 : virtual bool operator==( const FormulaToken& rToken ) const;
252 :
253 : DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaDoubleToken )
254 : };
255 :
256 :
257 6038 : class FORMULA_DLLPUBLIC FormulaStringToken : public FormulaToken
258 : {
259 : private:
260 : String aString;
261 : public:
262 2995 : FormulaStringToken( const String& r ) :
263 2995 : FormulaToken( svString ), aString( r ) {}
264 141 : FormulaStringToken( const FormulaStringToken& r ) :
265 141 : FormulaToken( r ), aString( r.aString ) {}
266 :
267 141 : virtual FormulaToken* Clone() const { return new FormulaStringToken(*this); }
268 : virtual const String& GetString() const;
269 : virtual bool operator==( const FormulaToken& rToken ) const;
270 :
271 : DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaStringToken )
272 : };
273 :
274 :
275 : /** Identical to FormulaStringToken, but with explicit OpCode instead of implicit
276 : ocPush, and an optional sal_uInt8 for ocBad tokens. */
277 882 : class FORMULA_DLLPUBLIC FormulaStringOpToken : public FormulaByteToken
278 : {
279 : private:
280 : String aString;
281 : public:
282 463 : FormulaStringOpToken( OpCode e, const String& r ) :
283 463 : FormulaByteToken( e, 0, svString, false ), aString( r ) {}
284 7 : FormulaStringOpToken( const FormulaStringOpToken& r ) :
285 7 : FormulaByteToken( r ), aString( r.aString ) {}
286 :
287 7 : virtual FormulaToken* Clone() const { return new FormulaStringOpToken(*this); }
288 : virtual const String& GetString() const;
289 : virtual bool operator==( const FormulaToken& rToken ) const;
290 : };
291 :
292 382 : class FORMULA_DLLPUBLIC FormulaIndexToken : public FormulaToken
293 : {
294 : private:
295 : sal_uInt16 nIndex;
296 : bool mbGlobal;
297 : public:
298 181 : FormulaIndexToken( OpCode e, sal_uInt16 n, bool bGlobal = true ) :
299 181 : FormulaToken( svIndex, e ), nIndex( n ), mbGlobal( bGlobal ) {}
300 17 : FormulaIndexToken( const FormulaIndexToken& r ) :
301 17 : FormulaToken( r ), nIndex( r.nIndex ), mbGlobal( r.mbGlobal ) {}
302 :
303 17 : virtual FormulaToken* Clone() const { return new FormulaIndexToken(*this); }
304 : virtual sal_uInt16 GetIndex() const;
305 : virtual void SetIndex( sal_uInt16 n );
306 : virtual bool IsGlobal() const;
307 : virtual void SetGlobal( bool b );
308 : virtual bool operator==( const FormulaToken& rToken ) const;
309 : };
310 :
311 :
312 2 : class FORMULA_DLLPUBLIC FormulaExternalToken : public FormulaToken
313 : {
314 : private:
315 : String aExternal;
316 : sal_uInt8 nByte;
317 : public:
318 0 : FormulaExternalToken( OpCode e, sal_uInt8 n, const String& r ) :
319 : FormulaToken( svExternal, e ), aExternal( r ),
320 0 : nByte( n ) {}
321 1 : FormulaExternalToken( OpCode e, const String& r ) :
322 : FormulaToken(svExternal, e ), aExternal( r ),
323 1 : nByte( 0 ) {}
324 0 : FormulaExternalToken( const FormulaExternalToken& r ) :
325 : FormulaToken( r ), aExternal( r.aExternal ),
326 0 : nByte( r.nByte ) {}
327 :
328 0 : virtual FormulaToken* Clone() const { return new FormulaExternalToken(*this); }
329 : virtual const String& GetExternal() const;
330 : virtual sal_uInt8 GetByte() const;
331 : virtual void SetByte( sal_uInt8 n );
332 : virtual bool operator==( const FormulaToken& rToken ) const;
333 : };
334 :
335 :
336 28 : class FORMULA_DLLPUBLIC FormulaMissingToken : public FormulaToken
337 : {
338 : public:
339 17 : FormulaMissingToken() :
340 17 : FormulaToken( svMissing,ocMissing ) {}
341 0 : FormulaMissingToken( const FormulaMissingToken& r ) :
342 0 : FormulaToken( r ) {}
343 :
344 0 : virtual FormulaToken* Clone() const { return new FormulaMissingToken(*this); }
345 : virtual double GetDouble() const;
346 : virtual const String& GetString() const;
347 : virtual bool operator==( const FormulaToken& rToken ) const;
348 : };
349 :
350 : class FORMULA_DLLPUBLIC FormulaJumpToken : public FormulaToken
351 : {
352 : private:
353 : short* pJump;
354 : public:
355 50 : FormulaJumpToken( OpCode e, short* p ) :
356 50 : FormulaToken( formula::svJump , e)
357 : {
358 50 : pJump = new short[ p[0] + 1 ];
359 50 : memcpy( pJump, p, (p[0] + 1) * sizeof(short) );
360 50 : }
361 17 : FormulaJumpToken( const FormulaJumpToken& r ) :
362 17 : FormulaToken( r )
363 : {
364 17 : pJump = new short[ r.pJump[0] + 1 ];
365 17 : memcpy( pJump, r.pJump, (r.pJump[0] + 1) * sizeof(short) );
366 17 : }
367 : virtual ~FormulaJumpToken();
368 : virtual short* GetJump() const;
369 : virtual bool operator==( const formula::FormulaToken& rToken ) const;
370 17 : virtual FormulaToken* Clone() const { return new FormulaJumpToken(*this); }
371 : };
372 :
373 :
374 : class FORMULA_DLLPUBLIC FormulaSubroutineToken : public FormulaToken
375 : {
376 : public:
377 : /** Takes ownership of pArray and deletes it upon destruction! */
378 : FormulaSubroutineToken( const FormulaTokenArray* pArray ) :
379 : FormulaToken( svSubroutine, ocCall ), mpArray( pArray) {}
380 : FormulaSubroutineToken( const FormulaSubroutineToken& r );
381 : virtual ~FormulaSubroutineToken();
382 0 : virtual FormulaToken* Clone() const { return new FormulaSubroutineToken(*this); }
383 : virtual bool operator==( const FormulaToken& rToken ) const;
384 :
385 : private:
386 : const FormulaTokenArray* mpArray;
387 : };
388 :
389 :
390 0 : class FORMULA_DLLPUBLIC FormulaUnknownToken : public FormulaToken
391 : {
392 : public:
393 0 : FormulaUnknownToken( OpCode e ) :
394 0 : FormulaToken( svUnknown, e ) {}
395 0 : FormulaUnknownToken( const FormulaUnknownToken& r ) :
396 0 : FormulaToken( r ) {}
397 :
398 0 : virtual FormulaToken* Clone() const { return new FormulaUnknownToken(*this); }
399 : virtual bool operator==( const FormulaToken& rToken ) const;
400 : };
401 :
402 :
403 412 : class FORMULA_DLLPUBLIC FormulaErrorToken : public FormulaToken
404 : {
405 : sal_uInt16 nError;
406 : public:
407 206 : FormulaErrorToken( sal_uInt16 nErr ) :
408 206 : FormulaToken( svError ), nError( nErr) {}
409 0 : FormulaErrorToken( const FormulaErrorToken& r ) :
410 0 : FormulaToken( r ), nError( r.nError) {}
411 :
412 0 : virtual FormulaToken* Clone() const { return new FormulaErrorToken(*this); }
413 : virtual sal_uInt16 GetError() const;
414 : virtual void SetError( sal_uInt16 nErr );
415 : virtual bool operator==( const FormulaToken& rToken ) const;
416 : };
417 :
418 : // =============================================================================
419 : } // formula
420 : // =============================================================================
421 :
422 : #endif
423 :
424 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|