xref: /aoo42x/main/basic/source/inc/expr.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _EXPR_HXX
29*cdf0e10cSrcweir #define _EXPR_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "opcodes.hxx"
32*cdf0e10cSrcweir #include "token.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir class SbiExprNode;
35*cdf0e10cSrcweir class SbiExpression;
36*cdf0e10cSrcweir class SbiExprList;
37*cdf0e10cSrcweir class SbiDimList;
38*cdf0e10cSrcweir class SbiParameters;
39*cdf0e10cSrcweir class SbiParser;
40*cdf0e10cSrcweir class SbiCodeGen;
41*cdf0e10cSrcweir class SbiSymDef;
42*cdf0e10cSrcweir class SbiProcDef;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <vector>
46*cdf0e10cSrcweir typedef ::std::vector<SbiExprList*> SbiExprListVector;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir struct SbVar {                      // Variablen-Element:
49*cdf0e10cSrcweir 	SbiExprNode*        pNext;		// Weiteres Element (bei Strukturen)
50*cdf0e10cSrcweir 	SbiSymDef*          pDef;		// Symboldefinition
51*cdf0e10cSrcweir 	SbiExprList*        pPar;		// optionale Parameter (wird geloescht)
52*cdf0e10cSrcweir 	SbiExprListVector*  pvMorePar;  // Array of arrays foo(pPar)(avMorePar[0])(avMorePar[1])...
53*cdf0e10cSrcweir };
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir struct KeywordSymbolInfo
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 	String			m_aKeywordSymbol;
58*cdf0e10cSrcweir 	SbxDataType		m_eSbxDataType;
59*cdf0e10cSrcweir 	SbiToken		m_eTok;
60*cdf0e10cSrcweir };
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir enum SbiExprType {					// Expression-Typen:
63*cdf0e10cSrcweir 	SbSTDEXPR,                      // normaler Ausdruck
64*cdf0e10cSrcweir 	SbLVALUE,                       // beliebiger lValue
65*cdf0e10cSrcweir 	SbSYMBOL,                       // beliebiges zusammengesetztes Symbol
66*cdf0e10cSrcweir 	SbOPERAND						// Variable/Funktion
67*cdf0e10cSrcweir };
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir enum SbiExprMode {					// Expression context:
70*cdf0e10cSrcweir 	EXPRMODE_STANDARD,              // default
71*cdf0e10cSrcweir 	EXPRMODE_STANDALONE,            // a param1, param2 OR a( param1, param2 ) = 42
72*cdf0e10cSrcweir 	EXPRMODE_LPAREN_PENDING,        // start of parameter list with bracket, special handling
73*cdf0e10cSrcweir 	EXPRMODE_LPAREN_NOT_NEEDED,     // pending LPAREN has not been used
74*cdf0e10cSrcweir 	EXPRMODE_ARRAY_OR_OBJECT,       // '=' or '(' or '.' found after ')' on ParenLevel 0, stopping
75*cdf0e10cSrcweir 									// expression, assuming array syntax a(...)[(...)] = ?
76*cdf0e10cSrcweir 									// or a(...).b(...)
77*cdf0e10cSrcweir 	EXPRMODE_EMPTY_PAREN            // It turned out that the paren don't contain anything: a()
78*cdf0e10cSrcweir };
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir enum SbiNodeType {
81*cdf0e10cSrcweir 	SbxNUMVAL,                      // nVal = Wert
82*cdf0e10cSrcweir 	SbxSTRVAL,                      // aStrVal = Wert, before #i59791/#i45570: nStringId = Wert
83*cdf0e10cSrcweir 	SbxVARVAL,                      // aVar = Wert
84*cdf0e10cSrcweir 	SbxTYPEOF,                      // TypeOf ObjExpr Is Type
85*cdf0e10cSrcweir 	SbxNODE,						// Node
86*cdf0e10cSrcweir 	SbxNEW,							// new <type> expression
87*cdf0e10cSrcweir 	SbxDUMMY
88*cdf0e10cSrcweir };
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir enum RecursiveMode
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	UNDEFINED,
93*cdf0e10cSrcweir 	FORCE_CALL,
94*cdf0e10cSrcweir 	PREVENT_CALL
95*cdf0e10cSrcweir };
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir class SbiExprNode {                  // Operatoren (und Operanden)
98*cdf0e10cSrcweir 	friend class SbiExpression;
99*cdf0e10cSrcweir 	friend class SbiConstExpression;
100*cdf0e10cSrcweir 	union {
101*cdf0e10cSrcweir 		sal_uInt16 nTypeStrId; 	     	// gepoolter String-ID, #i59791/#i45570 Now only for TypeOf
102*cdf0e10cSrcweir 		double nVal;                // numerischer Wert
103*cdf0e10cSrcweir 		SbVar  aVar;                // oder Variable
104*cdf0e10cSrcweir 	};
105*cdf0e10cSrcweir 	String aStrVal;					// #i59791/#i45570 Store string directly
106*cdf0e10cSrcweir 	SbiExprNode* pLeft;             // linker Zweig
107*cdf0e10cSrcweir 	SbiExprNode* pRight;            // rechter Zweig (NULL bei unaeren Ops)
108*cdf0e10cSrcweir 	SbiExprNode* pWithParent;       // Knoten, dessen Member this per with ist
109*cdf0e10cSrcweir 	SbiCodeGen*  pGen; 				// Code-Generator
110*cdf0e10cSrcweir 	SbiNodeType  eNodeType;         // Art des Nodes
111*cdf0e10cSrcweir 	SbxDataType eType;              // aktueller Datentyp
112*cdf0e10cSrcweir 	SbiToken     eTok;              // Token des Operators
113*cdf0e10cSrcweir 	sal_Bool  bComposite;				// sal_True: Zusammengesetzter Ausdruck
114*cdf0e10cSrcweir 	sal_Bool  bError; 	                // sal_True: Fehlerhaft
115*cdf0e10cSrcweir 	void  FoldConstants();          // Constant Folding durchfuehren
116*cdf0e10cSrcweir 	void  CollectBits();            // Umwandeln von Zahlen in Strings
117*cdf0e10cSrcweir 	sal_Bool  IsOperand()               // sal_True, wenn Operand
118*cdf0e10cSrcweir 		{ return sal_Bool( eNodeType != SbxNODE && eNodeType != SbxTYPEOF && eNodeType != SbxNEW ); }
119*cdf0e10cSrcweir 	sal_Bool  IsTypeOf()
120*cdf0e10cSrcweir 		{ return sal_Bool( eNodeType == SbxTYPEOF ); }
121*cdf0e10cSrcweir 	sal_Bool  IsNew()
122*cdf0e10cSrcweir 		{ return sal_Bool( eNodeType == SbxNEW ); }
123*cdf0e10cSrcweir 	sal_Bool  IsNumber();               // sal_True bei Zahlen
124*cdf0e10cSrcweir 	sal_Bool  IsString();               // sal_True bei Strings
125*cdf0e10cSrcweir 	sal_Bool  IsLvalue();               // sal_True, falls als Lvalue verwendbar
126*cdf0e10cSrcweir 	void  GenElement( SbiOpcode );  // Element
127*cdf0e10cSrcweir 	void  BaseInit( SbiParser* p );	// Hilfsfunktion fuer Ctor, AB 17.12.95
128*cdf0e10cSrcweir public:
129*cdf0e10cSrcweir 	SbiExprNode( void );
130*cdf0e10cSrcweir 	SbiExprNode( SbiParser*, double, SbxDataType );
131*cdf0e10cSrcweir 	SbiExprNode( SbiParser*, const String& );
132*cdf0e10cSrcweir 	SbiExprNode( SbiParser*, const SbiSymDef&, SbxDataType, SbiExprList* = NULL );
133*cdf0e10cSrcweir 	SbiExprNode( SbiParser*, SbiExprNode*, SbiToken, SbiExprNode* );
134*cdf0e10cSrcweir 	SbiExprNode( SbiParser*, SbiExprNode*, sal_uInt16 );	// #120061 TypeOf
135*cdf0e10cSrcweir 	SbiExprNode( SbiParser*, sal_uInt16 );					// new <type>
136*cdf0e10cSrcweir 	virtual ~SbiExprNode();
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	sal_Bool IsValid()                  { return sal_Bool( !bError ); }
139*cdf0e10cSrcweir 	sal_Bool IsConstant()               // sal_True bei konstantem Operanden
140*cdf0e10cSrcweir 		{ return sal_Bool( eNodeType == SbxSTRVAL || eNodeType == SbxNUMVAL ); }
141*cdf0e10cSrcweir 	sal_Bool IsIntConst();              // sal_True bei Integer-Konstanten
142*cdf0e10cSrcweir 	sal_Bool IsVariable();              // sal_True, wenn Variable
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	SbiExprNode* GetWithParent()			{ return pWithParent; }
145*cdf0e10cSrcweir 	void SetWithParent( SbiExprNode* p )	{ pWithParent = p; }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	SbxDataType GetType()           { return eType; }
148*cdf0e10cSrcweir 	void SetType( SbxDataType eTp )	{ eType = eTp; }
149*cdf0e10cSrcweir 	SbiNodeType GetNodeType()       { return eNodeType; }
150*cdf0e10cSrcweir 	SbiSymDef* GetVar();			// Variable (falls vorhanden)
151*cdf0e10cSrcweir 	SbiSymDef* GetRealVar();		// letzte Variable in x.y.z
152*cdf0e10cSrcweir 	SbiExprNode* GetRealNode();		// letzter Knoten in x.y.z
153*cdf0e10cSrcweir 	short GetDepth();               // Tiefe eines Baumes berechnen
154*cdf0e10cSrcweir 	const String& GetString() 		{ return aStrVal; }
155*cdf0e10cSrcweir 	short GetNumber() 				{ return (short)nVal; }
156*cdf0e10cSrcweir 	SbiExprList* GetParameters()	{ return aVar.pPar; }
157*cdf0e10cSrcweir 	SbiExprListVector* GetMoreParameters()	{ return aVar.pvMorePar; }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	void Optimize();                // Baumabgleich
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	void Gen( RecursiveMode eRecMode = UNDEFINED );	// Ausgabe eines Nodes
162*cdf0e10cSrcweir };
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir class SbiExpression {                // der Ausdruck:
165*cdf0e10cSrcweir 	friend class SbiExprList;
166*cdf0e10cSrcweir 	friend class SbiParameters;
167*cdf0e10cSrcweir 	friend class SbiDimList;
168*cdf0e10cSrcweir protected:
169*cdf0e10cSrcweir 	String 		  aArgName;         // Name fuer bananntes Argument
170*cdf0e10cSrcweir 	SbiParser* 	  pParser;			// fuer Fehlermeldungen, Parsing
171*cdf0e10cSrcweir 	SbiExpression* pNext;            // Link bei Parameterlisten
172*cdf0e10cSrcweir 	SbiExprNode*   pExpr;            // Der Expression-Baum
173*cdf0e10cSrcweir 	SbiExprType	  eCurExpr;			// Art des Ausdrucks
174*cdf0e10cSrcweir 	SbiExprMode   m_eMode;			// Expression context
175*cdf0e10cSrcweir 	sal_Bool  		  bBased;			// sal_True: einfacher DIM-Teil (+BASE)
176*cdf0e10cSrcweir 	sal_Bool          bError;           // sal_True: Fehler
177*cdf0e10cSrcweir 	sal_Bool          bByVal;           // sal_True: ByVal-Parameter
178*cdf0e10cSrcweir 	sal_Bool          bBracket;         // sal_True: Parameter list with brackets
179*cdf0e10cSrcweir 	sal_uInt16        nParenLevel;
180*cdf0e10cSrcweir 	SbiExprNode* Term( const KeywordSymbolInfo* pKeywordSymbolInfo = NULL );
181*cdf0e10cSrcweir 	SbiExprNode* ObjTerm( SbiSymDef& );
182*cdf0e10cSrcweir 	SbiExprNode* Operand( bool bUsedForTypeOf = false );
183*cdf0e10cSrcweir 	SbiExprNode* Unary();
184*cdf0e10cSrcweir 	SbiExprNode* Exp();
185*cdf0e10cSrcweir 	SbiExprNode* MulDiv();
186*cdf0e10cSrcweir 	SbiExprNode* IntDiv();
187*cdf0e10cSrcweir 	SbiExprNode* Mod();
188*cdf0e10cSrcweir 	SbiExprNode* AddSub();
189*cdf0e10cSrcweir 	SbiExprNode* Cat();
190*cdf0e10cSrcweir 	SbiExprNode* Like();
191*cdf0e10cSrcweir 	SbiExprNode* VBA_Not();
192*cdf0e10cSrcweir 	SbiExprNode* Comp();
193*cdf0e10cSrcweir 	SbiExprNode* Boolean();
194*cdf0e10cSrcweir public:
195*cdf0e10cSrcweir 	SbiExpression( SbiParser*, SbiExprType = SbSTDEXPR,
196*cdf0e10cSrcweir 		SbiExprMode eMode = EXPRMODE_STANDARD, const KeywordSymbolInfo* pKeywordSymbolInfo = NULL ); // Parsender Ctor
197*cdf0e10cSrcweir 	SbiExpression( SbiParser*, const String& );
198*cdf0e10cSrcweir 	SbiExpression( SbiParser*, double, SbxDataType = SbxDOUBLE );
199*cdf0e10cSrcweir 	SbiExpression( SbiParser*, const SbiSymDef&, SbiExprList* = NULL );
200*cdf0e10cSrcweir 	SbiExpression( SbiParser*, SbiToken );        // Spezial-Expr mit Spezial-Tokens
201*cdf0e10cSrcweir    ~SbiExpression();
202*cdf0e10cSrcweir 	String& GetName()               { return aArgName;            }
203*cdf0e10cSrcweir 	void SetBased()					{ bBased = sal_True; 			  }
204*cdf0e10cSrcweir 	sal_Bool IsBased()					{ return bBased; 			  }
205*cdf0e10cSrcweir 	void SetByVal()                 { bByVal = sal_True;              }
206*cdf0e10cSrcweir 	sal_Bool IsByVal()                  { return bByVal;              }
207*cdf0e10cSrcweir 	sal_Bool IsBracket()                { return bBracket;            }
208*cdf0e10cSrcweir 	sal_Bool IsValid()                  { return pExpr->IsValid();    }
209*cdf0e10cSrcweir 	sal_Bool IsConstant()               { return pExpr->IsConstant(); }
210*cdf0e10cSrcweir 	sal_Bool IsVariable()               { return pExpr->IsVariable(); }
211*cdf0e10cSrcweir 	sal_Bool IsLvalue()                 { return pExpr->IsLvalue();   }
212*cdf0e10cSrcweir 	sal_Bool IsIntConstant()            { return pExpr->IsIntConst(); }
213*cdf0e10cSrcweir 	const String& GetString()       { return pExpr->GetString();  }
214*cdf0e10cSrcweir 	SbiSymDef* GetVar()             { return pExpr->GetVar();     }
215*cdf0e10cSrcweir 	SbiSymDef* GetRealVar()         { return pExpr->GetRealVar(); }
216*cdf0e10cSrcweir 	SbiExprNode* GetExprNode()		{ return pExpr; }
217*cdf0e10cSrcweir 	SbxDataType GetType()           { return pExpr->GetType();    }
218*cdf0e10cSrcweir 	void SetType( SbxDataType eType){ pExpr->eType = eType;       }
219*cdf0e10cSrcweir 	void Gen( RecursiveMode eRecMode = UNDEFINED );	// Ausgabe eines Nodes
220*cdf0e10cSrcweir };
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir class SbiConstExpression : public SbiExpression {
223*cdf0e10cSrcweir 	double nVal;
224*cdf0e10cSrcweir 	String aVal;
225*cdf0e10cSrcweir 	SbxDataType eType;
226*cdf0e10cSrcweir public:                             // numerische Konstante
227*cdf0e10cSrcweir 	SbiConstExpression( SbiParser* );
228*cdf0e10cSrcweir 	SbxDataType GetType() { return eType; }
229*cdf0e10cSrcweir 	const String& GetString() { return aVal; }
230*cdf0e10cSrcweir 	double GetValue() { return nVal; }
231*cdf0e10cSrcweir 	short GetShortValue();
232*cdf0e10cSrcweir };
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir class SbiExprList {                  // Basisklasse fuer Parameter und Dims
235*cdf0e10cSrcweir protected:
236*cdf0e10cSrcweir 	SbiParser* pParser;				// Parser
237*cdf0e10cSrcweir 	SbiExpression* pFirst;          // Expressions
238*cdf0e10cSrcweir 	short nExpr;                    // Anzahl Expressions
239*cdf0e10cSrcweir 	short nDim;						// Anzahl Dimensionen
240*cdf0e10cSrcweir 	sal_Bool  bError;                   // sal_True: Fehler
241*cdf0e10cSrcweir 	sal_Bool  bBracket;                 // sal_True: Klammern
242*cdf0e10cSrcweir public:
243*cdf0e10cSrcweir 	SbiExprList( SbiParser* );
244*cdf0e10cSrcweir 	virtual ~SbiExprList();
245*cdf0e10cSrcweir 	sal_Bool  IsBracket()               { return bBracket;        }
246*cdf0e10cSrcweir 	sal_Bool  IsValid()                 { return sal_Bool( !bError ); }
247*cdf0e10cSrcweir 	short GetSize()                 { return nExpr;           }
248*cdf0e10cSrcweir 	short GetDims()                 { return nDim;     		  }
249*cdf0e10cSrcweir 	SbiExpression* Get( short );
250*cdf0e10cSrcweir 	sal_Bool  Test( const SbiProcDef& );	// Parameter-Checks
251*cdf0e10cSrcweir 	void  Gen();                    // Code-Erzeugung
252*cdf0e10cSrcweir 	void addExpression( SbiExpression* pExpr );
253*cdf0e10cSrcweir };
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir class SbiParameters : public SbiExprList {
256*cdf0e10cSrcweir public:
257*cdf0e10cSrcweir 	SbiParameters( SbiParser*, sal_Bool bConst = sal_False, sal_Bool bPar = sal_True);// parsender Ctor
258*cdf0e10cSrcweir };
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir class SbiDimList : public SbiExprList {
261*cdf0e10cSrcweir 	sal_Bool  bConst;                   // sal_True: Alles sind Integer-Konstanten
262*cdf0e10cSrcweir public:
263*cdf0e10cSrcweir 	SbiDimList( SbiParser* );         // Parsender Ctor
264*cdf0e10cSrcweir 	sal_Bool  IsConstant()              { return bConst; }
265*cdf0e10cSrcweir };
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir #endif
268