LibreOffice Module formula (master) 1
token.hxx
Go to the documentation of this file.
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 INCLUDED_FORMULA_TOKEN_HXX
21#define INCLUDED_FORMULA_TOKEN_HXX
22
23#include <sal/config.h>
24
25#include <cstring>
26#include <memory>
27#include <utility>
28#include <vector>
29
31#include <formula/opcode.hxx>
32#include <formula/types.hxx>
34#include <osl/interlck.h>
35#include <rtl/ustring.hxx>
36#include <sal/types.h>
37#include <svl/sharedstring.hxx>
38
39class ScJumpMatrix;
40class ScMatrix;
41struct ScComplexRefData;
42struct ScSingleRefData;
43enum class FormulaError : sal_uInt16;
44
45namespace formula
46{
47
49{
58 svExternal, // Byte + String
59 svFAP, // FormulaAutoPilot only, ever exported
61 svRefList, // ocUnion result
62 svEmptyCell, // Result is an empty cell, e.g. in LOOKUP()
63
64 svMatrixCell, // Result is a matrix with bells and
65 // whistles as needed for _the_ matrix
66 // formula result.
67
68 svHybridCell, // A temporary condition of a formula
69 // cell during import, having a double
70 // and/or string result and a formula
71 // string to be compiled.
72
78 svError, // error token
79 svMissing, // 0 or ""
80 svSep, // separator, ocSep, ocOpen, ocClose
81 svUnknown // unknown StackType
82};
83
84// Only to be used for debugging output. No guarantee of stability of the
85// return value.
86
87// Turn this into an operator<< when StackVar becomes a scoped enum
88
89inline std::string StackVarEnumToString(StackVar const e)
90{
91 switch (e)
92 {
93 case svByte: return "Byte";
94 case svDouble: return "Double";
95 case svString: return "String";
96 case svSingleRef: return "SingleRef";
97 case svDoubleRef: return "DoubleRef";
98 case svMatrix: return "Matrix";
99 case svIndex: return "Index";
100 case svJump: return "Jump";
101 case svExternal: return "External";
102 case svFAP: return "FAP";
103 case svJumpMatrix: return "JumpMatrix";
104 case svRefList: return "RefList";
105 case svEmptyCell: return "EmptyCell";
106 case svMatrixCell: return "MatrixCell";
107 case svHybridCell: return "HybridCell";
108 case svExternalSingleRef: return "ExternalSingleRef";
109 case svExternalDoubleRef: return "ExternalDoubleRef";
110 case svExternalName: return "ExternalName";
111 case svSingleVectorRef: return "SingleVectorRef";
112 case svDoubleVectorRef: return "DoubleVectorRef";
113 case svError: return "Error";
114 case svMissing: return "Missing";
115 case svSep: return "Sep";
116 case svUnknown: return "Unknown";
117 }
118 std::ostringstream os;
119 os << static_cast<int>(e);
120 return os.str();
121}
122
124{
126 const StackVar eType; // type of data
127 mutable oslInterlockedCount mnRefCnt; // reference count
128
130public:
131 FormulaToken( StackVar eTypeP,OpCode e = ocPush );
132 FormulaToken( const FormulaToken& r );
133
134 virtual ~FormulaToken();
135
136 void Delete() { delete this; }
137 void DeleteIfZeroRef() { if (mnRefCnt == 0) delete this; }
138 StackVar GetType() const { return eType; }
139 bool IsFunction() const; // pure functions, no operators
140
141 bool IsExternalRef() const;
142 bool IsRef() const;
143
144 sal_uInt8 GetParamCount() const;
145
146 void IncRef() const
147 {
148 osl_atomic_increment(&mnRefCnt);
149 }
150
151 void DecRef() const
152 {
153 if (!osl_atomic_decrement(&mnRefCnt))
154 const_cast<FormulaToken*>(this)->Delete();
155 }
156
157 oslInterlockedCount GetRef() const { return mnRefCnt; }
158 OpCode GetOpCode() const { return eOp; }
159
160 bool IsInForceArray() const;
161
177 virtual sal_uInt8 GetByte() const;
178 virtual void SetByte( sal_uInt8 n );
179 virtual ParamClass GetInForceArray() const;
180 virtual void SetInForceArray( ParamClass c );
181 virtual double GetDouble() const;
182 virtual double& GetDoubleAsReference();
183 virtual sal_Int16 GetDoubleType() const;
184 virtual void SetDoubleType( sal_Int16 nType );
185 virtual const svl::SharedString & GetString() const;
186 virtual void SetString( const svl::SharedString& rStr );
187 virtual sal_uInt16 GetIndex() const;
188 virtual void SetIndex( sal_uInt16 n );
189 virtual sal_Int16 GetSheet() const;
190 virtual void SetSheet( sal_Int16 n );
191 virtual sal_Unicode GetChar() const;
192 virtual short* GetJump() const;
193 virtual const OUString& GetExternal() const;
194 virtual FormulaToken* GetFAPOrigToken() const;
195 virtual FormulaError GetError() const;
196 virtual void SetError( FormulaError );
197
198 virtual const ScSingleRefData* GetSingleRef() const;
199 virtual ScSingleRefData* GetSingleRef();
200 virtual const ScComplexRefData* GetDoubleRef() const;
201 virtual ScComplexRefData* GetDoubleRef();
202 virtual const ScSingleRefData* GetSingleRef2() const;
203 virtual ScSingleRefData* GetSingleRef2();
204 virtual const ScMatrix* GetMatrix() const;
205 virtual ScMatrix* GetMatrix();
206 virtual ScJumpMatrix* GetJumpMatrix() const;
207 virtual const std::vector<ScComplexRefData>* GetRefList() const;
208 virtual std::vector<ScComplexRefData>* GetRefList();
209
210 virtual FormulaToken* Clone() const { return new FormulaToken(*this); }
211
212 virtual bool TextEqual( const formula::FormulaToken& rToken ) const;
213 virtual bool operator==( const FormulaToken& rToken ) const;
214
216 struct PrivateAccess { friend class FormulaCompiler; private: PrivateAccess() { } };
217 void NewOpCode( OpCode e, const PrivateAccess& ) { eOp = e; }
218};
219
221{
222 p->IncRef();
223}
224
226{
227 p->DecRef();
228}
229
231{
232private:
235public:
238 nByte( n ), cChar( c ) {}
240 FormulaToken( r ),
241 nByte( r.nByte ), cChar( r.cChar ) {}
242
243 virtual FormulaToken* Clone() const override { return new FormulaSpaceToken(*this); }
244 virtual sal_uInt8 GetByte() const override;
245 virtual sal_Unicode GetChar() const override;
246 virtual bool operator==( const FormulaToken& rToken ) const override;
247};
248
250{
251private:
254protected:
256 FormulaToken( v,e ), nByte( n ),
257 eInForceArray( c ) {}
258public:
260 FormulaToken( svByte,e ), nByte( n ),
261 eInForceArray( c ) {}
263 FormulaToken( svByte,e ), nByte( n ),
264 eInForceArray( ParamClass::Unknown ) {}
266 FormulaToken( svByte,e ), nByte( 0 ),
267 eInForceArray( ParamClass::Unknown ) {}
269 FormulaToken( r ), nByte( r.nByte ),
270 eInForceArray( r.eInForceArray ) {}
271
272 virtual FormulaToken* Clone() const override { return new FormulaByteToken(*this); }
273 virtual sal_uInt8 GetByte() const override;
274 virtual void SetByte( sal_uInt8 n ) override;
275 virtual ParamClass GetInForceArray() const override;
276 virtual void SetInForceArray( ParamClass c ) override;
277 virtual bool operator==( const FormulaToken& rToken ) const override;
278};
279
280
281// A special token for the FormulaAutoPilot only. Keeps a reference pointer of
282// the token of which it was created for comparison.
284{
285private:
287public:
290 pOrigToken( p ) {}
292 FormulaByteToken( r ), pOrigToken( r.pOrigToken ) {}
293
294 virtual FormulaToken* Clone() const override { return new FormulaFAPToken(*this); }
295 virtual FormulaToken* GetFAPOrigToken() const override;
296 virtual bool operator==( const FormulaToken& rToken ) const override;
297};
298
300{
301private:
302 double fDouble;
303public:
304 FormulaDoubleToken( double f ) :
305 FormulaToken( svDouble ), fDouble( f ) {}
307 FormulaToken( r ), fDouble( r.fDouble ) {}
308
309 virtual FormulaToken* Clone() const override { return new FormulaDoubleToken(*this); }
310 virtual double GetDouble() const override;
311 virtual double& GetDoubleAsReference() override;
312 virtual sal_Int16 GetDoubleType() const override;
313 virtual bool operator==( const FormulaToken& rToken ) const override;
314};
315
317{
318private:
319 sal_Int16 mnType;
323public:
324 FormulaTypedDoubleToken( double f, sal_Int16 nType ) :
327 FormulaDoubleToken( r ), mnType( r.mnType ) {}
328
329 virtual FormulaToken* Clone() const override { return new FormulaTypedDoubleToken(*this); }
330 virtual sal_Int16 GetDoubleType() const override;
331 virtual void SetDoubleType( sal_Int16 nType ) override;
332 virtual bool operator==( const FormulaToken& rToken ) const override;
333};
334
335
337{
339public:
342
343 virtual FormulaToken* Clone() const override;
344 virtual const svl::SharedString & GetString() const override;
345 virtual void SetString( const svl::SharedString& rStr ) override;
346 virtual bool operator==( const FormulaToken& rToken ) const override;
347};
348
349
353{
355public:
358
359 virtual FormulaToken* Clone() const override;
360 virtual const svl::SharedString & GetString() const override;
361 virtual void SetString( const svl::SharedString& rStr ) override;
362 virtual bool operator==( const FormulaToken& rToken ) const override;
363};
364
366{
367private:
368 sal_uInt16 nIndex;
369 sal_Int16 mnSheet;
370public:
371 FormulaIndexToken( OpCode e, sal_uInt16 n, sal_Int16 nSheet = -1 ) :
372 FormulaToken( svIndex, e ), nIndex( n ), mnSheet( nSheet ) {}
374 FormulaToken( r ), nIndex( r.nIndex ), mnSheet( r.mnSheet ) {}
375
376 virtual FormulaToken* Clone() const override { return new FormulaIndexToken(*this); }
377 virtual sal_uInt16 GetIndex() const override;
378 virtual void SetIndex( sal_uInt16 n ) override;
379 virtual sal_Int16 GetSheet() const override;
380 virtual void SetSheet( sal_Int16 n ) override;
381 virtual bool operator==( const FormulaToken& rToken ) const override;
382};
383
384
386{
387private:
388 OUString aExternal;
389public:
392 aExternal(std::move( r )) {}
393 FormulaExternalToken( OpCode e, OUString r ) :
395 aExternal(std::move( r )) {}
397 FormulaByteToken( r ), aExternal( r.aExternal ) {}
398
399 virtual FormulaToken* Clone() const override { return new FormulaExternalToken(*this); }
400 virtual const OUString& GetExternal() const override;
401 virtual bool operator==( const FormulaToken& rToken ) const override;
402};
403
404
406{
407public:
411 FormulaToken( r ) {}
412
413 virtual FormulaToken* Clone() const override { return new FormulaMissingToken(*this); }
414 virtual double GetDouble() const override;
415 virtual const svl::SharedString & GetString() const override;
416 virtual bool operator==( const FormulaToken& rToken ) const override;
417};
418
420{
421private:
422 std::unique_ptr<short[]>
425public:
426 FormulaJumpToken( OpCode e, short const * p ) :
428 eInForceArray( ParamClass::Unknown)
429 {
430 pJump.reset( new short[ p[0] + 1 ] );
431 memcpy( pJump.get(), p, (p[0] + 1) * sizeof(short) );
432 }
434 FormulaToken( r ),
435 eInForceArray( r.eInForceArray)
436 {
437 pJump.reset( new short[ r.pJump[0] + 1 ] );
438 memcpy( pJump.get(), r.pJump.get(), (r.pJump[0] + 1) * sizeof(short) );
439 }
440 virtual ~FormulaJumpToken() override;
441 virtual short* GetJump() const override;
442 virtual bool operator==( const formula::FormulaToken& rToken ) const override;
443 virtual FormulaToken* Clone() const override { return new FormulaJumpToken(*this); }
444 virtual ParamClass GetInForceArray() const override;
445 virtual void SetInForceArray( ParamClass c ) override;
446};
447
448
450{
451public:
453 FormulaToken( svUnknown, e ) {}
455 FormulaToken( r ) {}
456
457 virtual FormulaToken* Clone() const override { return new FormulaUnknownToken(*this); }
458 virtual bool operator==( const FormulaToken& rToken ) const override;
459};
460
461
463{
465public:
467 FormulaToken( svError ), nError( nErr) {}
469 FormulaToken( r ), nError( r.nError) {}
470
471 virtual FormulaToken* Clone() const override { return new FormulaErrorToken(*this); }
472 virtual FormulaError GetError() const override;
473 virtual void SetError( FormulaError nErr ) override;
474 virtual bool operator==( const FormulaToken& rToken ) const override;
475};
476
477
478} // formula
479
480
481#endif
482
483/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
FormulaByteToken(OpCode e, sal_uInt8 n)
Definition: token.hxx:262
FormulaByteToken(OpCode e, sal_uInt8 n, ParamClass c)
Definition: token.hxx:259
FormulaByteToken(OpCode e, sal_uInt8 n, StackVar v, ParamClass c)
Definition: token.hxx:255
virtual FormulaToken * Clone() const override
Definition: token.hxx:272
ParamClass eInForceArray
Definition: token.hxx:253
FormulaByteToken(OpCode e)
Definition: token.hxx:265
FormulaByteToken(const FormulaByteToken &r)
Definition: token.hxx:268
FormulaDoubleToken(double f)
Definition: token.hxx:304
FormulaDoubleToken(const FormulaDoubleToken &r)
Definition: token.hxx:306
virtual FormulaToken * Clone() const override
Definition: token.hxx:309
FormulaErrorToken(const FormulaErrorToken &r)
Definition: token.hxx:468
virtual FormulaToken * Clone() const override
Definition: token.hxx:471
FormulaErrorToken(FormulaError nErr)
Definition: token.hxx:466
FormulaExternalToken(OpCode e, OUString r)
Definition: token.hxx:393
virtual FormulaToken * Clone() const override
Definition: token.hxx:399
FormulaExternalToken(OpCode e, sal_uInt8 n, OUString r)
Definition: token.hxx:390
FormulaExternalToken(const FormulaExternalToken &r)
Definition: token.hxx:396
FormulaFAPToken(const FormulaFAPToken &r)
Definition: token.hxx:291
FormulaFAPToken(OpCode e, sal_uInt8 n, FormulaToken *p)
Definition: token.hxx:288
virtual FormulaToken * Clone() const override
Definition: token.hxx:294
FormulaTokenRef pOrigToken
Definition: token.hxx:286
virtual FormulaToken * Clone() const override
Definition: token.hxx:376
FormulaIndexToken(const FormulaIndexToken &r)
Definition: token.hxx:373
FormulaIndexToken(OpCode e, sal_uInt16 n, sal_Int16 nSheet=-1)
Definition: token.hxx:371
FormulaJumpToken(const FormulaJumpToken &r)
Definition: token.hxx:433
FormulaJumpToken(OpCode e, short const *p)
Definition: token.hxx:426
ParamClass eInForceArray
Definition: token.hxx:424
virtual FormulaToken * Clone() const override
Definition: token.hxx:443
std::unique_ptr< short[]> pJump
Definition: token.hxx:423
virtual FormulaToken * Clone() const override
Definition: token.hxx:413
FormulaMissingToken(const FormulaMissingToken &r)
Definition: token.hxx:410
virtual FormulaToken * Clone() const override
Definition: token.hxx:243
FormulaSpaceToken(sal_uInt8 n, sal_Unicode c)
Definition: token.hxx:236
FormulaSpaceToken(const FormulaSpaceToken &r)
Definition: token.hxx:239
Identical to FormulaStringToken, but with explicit OpCode instead of implicit ocPush,...
Definition: token.hxx:353
svl::SharedString maString
Definition: token.hxx:354
svl::SharedString maString
Definition: token.hxx:338
void NewOpCode(OpCode e, const PrivateAccess &)
Definition: token.hxx:217
void DecRef() const
Definition: token.hxx:151
void DeleteIfZeroRef()
Definition: token.hxx:137
FormulaToken & operator=(const FormulaToken &)=delete
void IncRef() const
Definition: token.hxx:146
oslInterlockedCount GetRef() const
Definition: token.hxx:157
oslInterlockedCount mnRefCnt
Definition: token.hxx:127
OpCode GetOpCode() const
Definition: token.hxx:158
virtual FormulaToken * Clone() const
Definition: token.hxx:210
StackVar GetType() const
Definition: token.hxx:138
const StackVar eType
Definition: token.hxx:126
virtual FormulaToken * Clone() const override
Definition: token.hxx:329
FormulaTypedDoubleToken(const FormulaTypedDoubleToken &r)
Definition: token.hxx:326
FormulaTypedDoubleToken(double f, sal_Int16 nType)
Definition: token.hxx:324
sal_Int16 mnType
Can hold, for example, a value of SvNumFormatType, or by contract any other classification.
Definition: token.hxx:319
FormulaUnknownToken(const FormulaUnknownToken &r)
Definition: token.hxx:454
virtual FormulaToken * Clone() const override
Definition: token.hxx:457
float v
FormulaError
Definition: errorcodes.hxx:32
DocumentType eType
#define FORMULA_DLLPUBLIC
Definition: formuladllapi.h:28
sal_Int32 nIndex
void * p
sal_Int64 n
OUString GetString(int nId)
void SetString(SwCursor &rCursor, std::u16string_view aString)
::boost::intrusive_ptr< FormulaToken > FormulaTokenRef
Definition: types.hxx:27
StackVar
Definition: token.hxx:49
@ svMissing
Definition: token.hxx:79
@ svIndex
Definition: token.hxx:56
@ svExternalDoubleRef
Definition: token.hxx:74
@ svUnknown
Definition: token.hxx:81
@ svExternalName
Definition: token.hxx:75
@ svDouble
Definition: token.hxx:51
@ svJumpMatrix
Definition: token.hxx:60
@ svSep
Definition: token.hxx:80
@ svError
Definition: token.hxx:78
@ svDoubleRef
Definition: token.hxx:54
@ svExternal
Definition: token.hxx:58
@ svExternalSingleRef
Definition: token.hxx:73
@ svMatrixCell
Definition: token.hxx:64
@ svDoubleVectorRef
Definition: token.hxx:77
@ svString
Definition: token.hxx:52
@ svJump
Definition: token.hxx:57
@ svRefList
Definition: token.hxx:61
@ svMatrix
Definition: token.hxx:55
@ svFAP
Definition: token.hxx:59
@ svByte
Definition: token.hxx:50
@ svEmptyCell
Definition: token.hxx:62
@ svSingleRef
Definition: token.hxx:53
@ svHybridCell
Definition: token.hxx:68
@ svSingleVectorRef
Definition: token.hxx:76
void intrusive_ptr_add_ref(const FormulaToken *p)
Definition: token.hxx:220
void intrusive_ptr_release(const FormulaToken *p)
Definition: token.hxx:225
std::string StackVarEnumToString(StackVar const e)
Definition: token.hxx:89
css::uno::Reference< css::animations::XAnimationNode > Clone(const css::uno::Reference< css::animations::XAnimationNode > &xSourceNode, const SdPage *pSource=nullptr, const SdPage *pTarget=nullptr)
OpCode
Definition: opcode.hxx:29
@ ocMissing
Definition: opcode.hxx:52
@ ocPush
Definition: opcode.hxx:31
@ ocWhitespace
Definition: opcode.hxx:56
QPRO_FUNC_TYPE nType
sal_Int32 mnType
This is dirty and only the compiler should use it!
Definition: token.hxx:216
unsigned char sal_uInt8
sal_uInt16 sal_Unicode
bool operator==(const XclFontData &rLeft, const XclFontData &rRight)
int SetError()
int GetError()