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 INCLUDED_FORMULA_TOKENARRAY_HXX
21 : #define INCLUDED_FORMULA_TOKENARRAY_HXX
22 :
23 : #include <com/sun/star/sheet/FormulaToken.hpp>
24 : #include <formula/token.hxx>
25 : #include <formula/ExternalReferenceHelper.hxx>
26 : #include <limits.h>
27 :
28 : #include <boost/unordered_set.hpp>
29 :
30 : namespace svl {
31 :
32 : class SharedString;
33 : class SharedStringPool;
34 :
35 : }
36 :
37 : namespace formula
38 : {
39 :
40 : // RecalcMode access only via TokenArray SetRecalcMode / IsRecalcMode...
41 :
42 : typedef sal_uInt8 ScRecalcMode;
43 : // Only one of the exclusive bits can be set,
44 : // handled by TokenArray SetRecalcMode... methods
45 : #define RECALCMODE_NORMAL 0x01 // exclusive
46 : #define RECALCMODE_ALWAYS 0x02 // exclusive, always
47 : #define RECALCMODE_ONLOAD 0x04 // exclusive, always after load
48 : #define RECALCMODE_ONLOAD_ONCE 0x08 // exclusive, once after load
49 : #define RECALCMODE_FORCED 0x10 // combined, also if cell isn't visible
50 : #define RECALCMODE_ONREFMOVE 0x20 // combined, if reference was moved
51 : #define RECALCMODE_EMASK 0x0F // mask of exclusive bits
52 : // If new bits are to be defined, AddRecalcMode has to be adjusted!
53 :
54 : class FormulaMissingContext;
55 :
56 : class FORMULA_DLLPUBLIC MissingConvention
57 : {
58 : bool mbODFF; /// TRUE: ODFF, FALSE: PODF
59 : public:
60 1564 : explicit MissingConvention( bool bODFF ) : mbODFF(bODFF) {}
61 : // Implementation and usage only in token.cxx
62 : inline bool isRewriteNeeded( OpCode eOp ) const;
63 12 : inline bool isODFF() const { return mbODFF; }
64 : };
65 :
66 : class FORMULA_DLLPUBLIC FormulaTokenArray
67 : {
68 : friend class FormulaCompiler;
69 : friend class FormulaTokenIterator;
70 : friend class FormulaMissingContext;
71 :
72 : protected:
73 : FormulaToken** pCode; // Token code array
74 : FormulaToken** pRPN; // RPN array
75 : sal_uInt16 nLen; // Length of token array
76 : sal_uInt16 nRPN; // Length of RPN array
77 : sal_uInt16 nIndex; // Current step index
78 : sal_uInt16 nError; // Error code
79 : short nRefs; // Count of cell references
80 : ScRecalcMode nMode; // Flags to indicate when to recalc this code
81 : bool bHyperLink; // If HYPERLINK() occurs in the formula.
82 :
83 : protected:
84 : void Assign( const FormulaTokenArray& );
85 : void Assign( sal_uInt16 nCode, FormulaToken **pTokens );
86 :
87 : /// Also used by the compiler. The token MUST had been allocated with new!
88 : FormulaToken* Add( FormulaToken* );
89 1502 : inline void SetCombinedBitsRecalcMode( ScRecalcMode nBits )
90 1502 : { nMode |= (nBits & ~RECALCMODE_EMASK); }
91 306 : inline ScRecalcMode GetCombinedBitsRecalcMode() const
92 306 : { return nMode & ~RECALCMODE_EMASK; }
93 : /** Exclusive bits already set in nMode are
94 : zero'ed, nBits may contain combined bits, but
95 : only one exclusive bit may be set! */
96 306 : inline void SetMaskedRecalcMode( ScRecalcMode nBits )
97 306 : { nMode = GetCombinedBitsRecalcMode() | nBits; }
98 :
99 : public:
100 : FormulaTokenArray();
101 : /// Assignment with references to FormulaToken entries (not copied!)
102 : FormulaTokenArray( const FormulaTokenArray& );
103 : virtual ~FormulaTokenArray();
104 : FormulaTokenArray* Clone() const; /// True copy!
105 :
106 : void Clear();
107 : void DelRPN();
108 6506 : FormulaToken* First() { nIndex = 0; return Next(); }
109 : FormulaToken* Next();
110 : FormulaToken* FirstNoSpaces() { nIndex = 0; return NextNoSpaces(); }
111 : FormulaToken* NextNoSpaces();
112 : FormulaToken* GetNextName();
113 : FormulaToken* GetNextReference();
114 : FormulaToken* GetNextReferenceRPN();
115 : FormulaToken* GetNextReferenceOrName();
116 : FormulaToken* GetNextColRowName();
117 : /// Peek at nIdx-1 if not out of bounds, decrements nIdx if successful. Returns NULL if not.
118 : FormulaToken* PeekPrev( sal_uInt16 & nIdx );
119 : FormulaToken* PeekNext();
120 : FormulaToken* PeekPrevNoSpaces(); /// Only after Reset/First/Next/Last/Prev!
121 : FormulaToken* PeekNextNoSpaces(); /// Only after Reset/First/Next/Last/Prev!
122 452 : FormulaToken* FirstRPN() { nIndex = 0; return NextRPN(); }
123 : FormulaToken* NextRPN();
124 0 : FormulaToken* LastRPN() { nIndex = nRPN; return PrevRPN(); }
125 : FormulaToken* PrevRPN();
126 :
127 : bool HasReferences() const;
128 :
129 : bool HasExternalRef() const;
130 : bool HasOpCode( OpCode ) const;
131 : bool HasOpCodeRPN( OpCode ) const;
132 : /// Token of type svIndex or opcode ocColRowName
133 : bool HasNameOrColRowName() const;
134 :
135 : /**
136 : * Check if the token array contains any of specified opcode tokens.
137 : *
138 : * @param rOpCodes collection of opcodes to check against.
139 : *
140 : * @return true if the token array contains at least one of the specified
141 : * opcode tokens, false otherwise.
142 : */
143 : bool HasOpCodes( const boost::unordered_set<OpCode>& rOpCodes ) const;
144 :
145 6932 : FormulaToken** GetArray() const { return pCode; }
146 18956 : FormulaToken** GetCode() const { return pRPN; }
147 58002 : sal_uInt16 GetLen() const { return nLen; }
148 200859 : sal_uInt16 GetCodeLen() const { return nRPN; }
149 60824 : void Reset() { nIndex = 0; }
150 421508 : sal_uInt16 GetCodeError() const { return nError; }
151 2310 : void SetCodeError( sal_uInt16 n ) { nError = n; }
152 : short GetRefs() const { return nRefs; }
153 268 : void IncrementRefs() { ++nRefs; }
154 1712 : void SetHyperLink( bool bVal ) { bHyperLink = bVal; }
155 484 : bool IsHyperLink() const { return bHyperLink; }
156 :
157 814 : inline ScRecalcMode GetRecalcMode() const { return nMode; }
158 : /** Bits aren't set directly but validated and
159 : maybe handled according to priority if more
160 : than one exclusive bit was set. */
161 : void AddRecalcMode( ScRecalcMode nBits );
162 :
163 67572 : inline void ClearRecalcMode() { nMode = RECALCMODE_NORMAL; }
164 2 : inline void SetExclusiveRecalcModeNormal()
165 2 : { SetMaskedRecalcMode( RECALCMODE_NORMAL ); }
166 58 : inline void SetExclusiveRecalcModeAlways()
167 58 : { SetMaskedRecalcMode( RECALCMODE_ALWAYS ); }
168 246 : inline void SetExclusiveRecalcModeOnLoad()
169 246 : { SetMaskedRecalcMode( RECALCMODE_ONLOAD ); }
170 0 : inline void SetExclusiveRecalcModeOnLoadOnce()
171 0 : { SetMaskedRecalcMode( RECALCMODE_ONLOAD_ONCE ); }
172 0 : inline void SetRecalcModeForced()
173 0 : { nMode |= RECALCMODE_FORCED; }
174 : inline void ClearRecalcModeForced()
175 : { nMode &= ~RECALCMODE_FORCED; }
176 192 : inline void SetRecalcModeOnRefMove()
177 192 : { nMode |= RECALCMODE_ONREFMOVE; }
178 : inline void ClearRecalcModeOnRefMove()
179 : { nMode &= ~RECALCMODE_ONREFMOVE; }
180 8246 : inline bool IsRecalcModeNormal() const
181 8246 : { return (nMode & RECALCMODE_NORMAL) != 0; }
182 144213 : inline bool IsRecalcModeAlways() const
183 144213 : { return (nMode & RECALCMODE_ALWAYS) != 0; }
184 1674 : inline bool IsRecalcModeOnLoad() const
185 1674 : { return (nMode & RECALCMODE_ONLOAD) != 0; }
186 1460 : inline bool IsRecalcModeOnLoadOnce() const
187 1460 : { return (nMode & RECALCMODE_ONLOAD_ONCE) != 0; }
188 90813 : inline bool IsRecalcModeForced() const
189 90813 : { return (nMode & RECALCMODE_FORCED) != 0; }
190 1126 : inline bool IsRecalcModeOnRefMove() const
191 1126 : { return (nMode & RECALCMODE_ONREFMOVE) != 0; }
192 :
193 : /** Get OpCode of the most outer function */
194 : inline OpCode GetOuterFuncOpCode();
195 :
196 : /** Operators +,-,*,/,^,&,=,<>,<,>,<=,>=
197 : with DoubleRef in Formula? */
198 : bool HasMatrixDoubleRefOps();
199 :
200 : virtual FormulaToken* AddOpCode(OpCode e);
201 :
202 : /** Adds the single token to array.
203 : Derived classes must overload it when they want to support derived classes from FormulaToken.
204 : @return true when an error occurs
205 : */
206 : virtual bool AddFormulaToken(
207 : const css::sheet::FormulaToken& rToken, svl::SharedStringPool& rSPool,
208 : ExternalReferenceHelper* pExtRef );
209 :
210 : /** fill the array with the tokens from the sequence.
211 : It calls AddFormulaToken for each token in the list.
212 : @param _aSequence the token to add
213 : @return true when an error occurs
214 : */
215 : bool Fill(
216 : const css::uno::Sequence<css::sheet::FormulaToken>& rSequence,
217 : svl::SharedStringPool& rSPool, ExternalReferenceHelper* pExtRef );
218 :
219 : /**
220 : * Do some checking based on the individual tokens. For now, we use this
221 : * only to check whether we can vectorize the token array.
222 : */
223 : virtual void CheckToken( const FormulaToken& t );
224 :
225 : FormulaToken* AddToken( const FormulaToken& );
226 : FormulaToken* AddString( const svl::SharedString& rStr );
227 : FormulaToken* AddDouble( double fVal );
228 : FormulaToken* AddExternal( const sal_Unicode* pStr );
229 : /** Xcl import may play dirty tricks with OpCode!=ocExternal.
230 : Others don't use! */
231 : FormulaToken* AddExternal( const OUString& rStr, OpCode eOp = ocExternal );
232 : FormulaToken* AddBad( const OUString& rStr ); /// ocBad with OUString
233 : FormulaToken* AddStringXML( const OUString& rStr ); /// ocStringXML with OUString, temporary during import
234 :
235 : virtual FormulaToken* MergeArray( );
236 :
237 : /// Assignment with references to FormulaToken entries (not copied!)
238 : FormulaTokenArray& operator=( const FormulaTokenArray& );
239 :
240 : /** Determines if this formula needs any changes to convert it to something
241 : previous versions of OOo could consume (Plain Old Formula). */
242 : bool NeedsPofRewrite(const MissingConvention & rConv);
243 :
244 : /** Rewrites to Plain Old Formula, substituting missing parameters. The
245 : FormulaTokenArray* returned is new'ed. */
246 : FormulaTokenArray* RewriteMissingToPof(const MissingConvention & rConv);
247 :
248 : /** Determines if this formula may be followed by a reference. */
249 : bool MayReferenceFollow();
250 : };
251 :
252 0 : inline OpCode FormulaTokenArray::GetOuterFuncOpCode()
253 : {
254 0 : if ( pRPN && nRPN )
255 0 : return pRPN[nRPN-1]->GetOpCode();
256 0 : return ocNone;
257 : }
258 :
259 : class FORMULA_DLLPUBLIC FormulaTokenIterator
260 : {
261 : struct Item
262 : {
263 : public:
264 : const FormulaTokenArray* pArr;
265 : short nPC;
266 : short nStop;
267 :
268 : Item(const FormulaTokenArray* arr, short pc, short stop);
269 : };
270 :
271 : std::vector<Item> *maStack;
272 :
273 : public:
274 : FormulaTokenIterator( const FormulaTokenArray& );
275 : ~FormulaTokenIterator();
276 : void Reset();
277 : const FormulaToken* Next();
278 : const FormulaToken* PeekNextOperator();
279 : bool IsEndOfPath() const; /// if a jump or subroutine path is done
280 8 : bool HasStacked() const { return maStack->size() > 1; }
281 2 : short GetPC() const { return maStack->back().nPC; }
282 :
283 : /** Jump or subroutine call.
284 : Program counter values will be incremented before code is executed =>
285 : positions are to be passed with -1 offset.
286 : @param nStart
287 : Start on code at position nStart+1 (yes, pass with offset -1)
288 : @param nNext
289 : After subroutine continue with instruction at position nNext+1
290 : @param nStop
291 : Stop before reaching code at position nStop. If not specified the
292 : default is to either run the entire code, or to stop if an ocSep or
293 : ocClose is encountered, which are only present in ocIf or ocChose
294 : jumps.
295 : */
296 : void Jump( short nStart, short nNext, short nStop = SHRT_MAX );
297 : void Push( const FormulaTokenArray* );
298 : void Pop();
299 :
300 : private:
301 : const FormulaToken* GetNonEndOfPathToken( short nIdx ) const;
302 : };
303 : } // formula
304 :
305 : #endif // INCLUDED_FORMULA_TOKENARRAY_HXX
306 :
307 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|