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