1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_BASIC_SOURCE_INC_EXPR_HXX
21 #define INCLUDED_BASIC_SOURCE_INC_EXPR_HXX
23 #include "opcodes.hxx"
38 typedef ::std::vector
<SbiExprList
*> SbiExprListVector
;
41 SbiExprNode
* pNext
; // next element (for structures)
42 SbiSymDef
* pDef
; // symbol definition
43 SbiExprList
* pPar
; // optional parameters (is deleted)
44 SbiExprListVector
* pvMorePar
; // Array of arrays foo(pPar)(avMorePar[0])(avMorePar[1])...
47 struct KeywordSymbolInfo
49 OUString m_aKeywordSymbol
;
50 SbxDataType m_eSbxDataType
;
54 enum SbiExprType
{ // expression types:
55 SbSTDEXPR
, // normal expression
56 SbLVALUE
, // any lValue
57 SbSYMBOL
, // any composite symbol
58 SbOPERAND
// variable/function
61 enum SbiExprMode
{ // Expression context:
62 EXPRMODE_STANDARD
, // default
63 EXPRMODE_STANDALONE
, // a param1, param2 OR a( param1, param2 ) = 42
64 EXPRMODE_LPAREN_PENDING
, // start of parameter list with bracket, special handling
65 EXPRMODE_LPAREN_NOT_NEEDED
, // pending LPAREN has not been used
66 EXPRMODE_ARRAY_OR_OBJECT
, // '=' or '(' or '.' found after ')' on ParenLevel 0, stopping
67 // expression, assuming array syntax a(...)[(...)] = ?
69 EXPRMODE_EMPTY_PAREN
// It turned out that the paren don't contain anything: a()
73 SbxNUMVAL
, // nVal = value
74 SbxSTRVAL
, // aStrVal = value, before #i59791/#i45570: nStringId = value
75 SbxVARVAL
, // aVar = value
76 SbxTYPEOF
, // TypeOf ObjExpr Is Type
78 SbxNEW
, // new <type> expression
89 class SbiExprNode
{ // operators (and operands)
90 friend class SbiExpression
;
91 friend class SbiConstExpression
;
93 sal_uInt16 nTypeStrId
; // pooled String-ID, #i59791/#i45570 Now only for TypeOf
94 double nVal
; // numeric value
95 SbVar aVar
; // or variable
97 OUString aStrVal
; // #i59791/#i45570 Store string directly
98 SbiExprNode
* pLeft
; // right branch
99 SbiExprNode
* pRight
; // right branch (NULL for unary ops)
100 SbiExprNode
* pWithParent
; // node, whose member is "this per with"
101 SbiCodeGen
* pGen
; // code-generator
102 SbiNodeType eNodeType
;
105 bool bError
; // true: error
106 void FoldConstants();
107 void CollectBits(); // converting numbers to strings
109 { return eNodeType
!= SbxNODE
&& eNodeType
!= SbxTYPEOF
&& eNodeType
!= SbxNEW
; }
111 { return eNodeType
== SbxTYPEOF
; }
113 { return eNodeType
== SbxNEW
; }
115 bool IsLvalue(); // true, if usable as Lvalue
116 void GenElement( SbiOpcode
);
117 void BaseInit( SbiParser
* p
); // help function for Ctor, from 17.12.95
120 SbiExprNode( SbiParser
*, double, SbxDataType
);
121 SbiExprNode( SbiParser
*, const OUString
& );
122 SbiExprNode( SbiParser
*, const SbiSymDef
&, SbxDataType
, SbiExprList
* = NULL
);
123 SbiExprNode( SbiParser
*, SbiExprNode
*, SbiToken
, SbiExprNode
* );
124 SbiExprNode( SbiParser
*, SbiExprNode
*, sal_uInt16
); // #120061 TypeOf
125 SbiExprNode( SbiParser
*, sal_uInt16
); // new <type>
126 virtual ~SbiExprNode();
128 bool IsValid() { return !bError
; }
129 bool IsConstant() // true: constant operand
130 { return eNodeType
== SbxSTRVAL
|| eNodeType
== SbxNUMVAL
; }
134 SbiExprNode
* GetWithParent() { return pWithParent
; }
135 void SetWithParent( SbiExprNode
* p
) { pWithParent
= p
; }
137 SbxDataType
GetType() { return eType
; }
138 void SetType( SbxDataType eTp
) { eType
= eTp
; }
139 SbiNodeType
GetNodeType() { return eNodeType
; }
141 SbiSymDef
* GetRealVar(); // last variable in x.y.z
142 SbiExprNode
* GetRealNode(); // last node in x.y.z
143 short GetDepth(); // compute a tree's depth
144 const OUString
& GetString() { return aStrVal
; }
145 short GetNumber() { return (short)nVal
; }
146 SbiExprList
* GetParameters() { return aVar
.pPar
; }
147 SbiExprListVector
* GetMoreParameters() { return aVar
.pvMorePar
; }
149 void Optimize(); // tree matching
151 void Gen( RecursiveMode eRecMode
= UNDEFINED
); // giving out a node
154 class SbiExpression
{
155 friend class SbiExprList
;
156 friend class SbiParameters
;
157 friend class SbiDimList
;
161 SbiExpression
* pNext
; // link at parameter lists
162 SbiExprNode
* pExpr
; // expression tree
163 SbiExprType eCurExpr
; // type of expression
164 SbiExprMode m_eMode
; // expression context
165 bool bBased
; // true: easy DIM-part (+BASE)
167 bool bByVal
; // true: ByVal-Parameter
168 bool bBracket
; // true: Parameter list with brackets
169 sal_uInt16 nParenLevel
;
170 SbiExprNode
* Term( const KeywordSymbolInfo
* pKeywordSymbolInfo
= NULL
);
171 SbiExprNode
* ObjTerm( SbiSymDef
& );
172 SbiExprNode
* Operand( bool bUsedForTypeOf
= false );
173 SbiExprNode
* Unary();
175 SbiExprNode
* MulDiv();
176 SbiExprNode
* IntDiv();
178 SbiExprNode
* AddSub();
181 SbiExprNode
* VBA_Not();
183 SbiExprNode
* Boolean();
185 SbiExpression( SbiParser
*, SbiExprType
= SbSTDEXPR
,
186 SbiExprMode eMode
= EXPRMODE_STANDARD
, const KeywordSymbolInfo
* pKeywordSymbolInfo
= NULL
); // parsing Ctor
187 SbiExpression( SbiParser
*, double, SbxDataType
= SbxDOUBLE
);
188 SbiExpression( SbiParser
*, const SbiSymDef
&, SbiExprList
* = NULL
);
190 OUString
& GetName() { return aArgName
; }
191 void SetBased() { bBased
= true; }
192 bool IsBased() { return bBased
; }
193 void SetByVal() { bByVal
= true; }
194 bool IsByVal() { return bByVal
; }
195 bool IsBracket() { return bBracket
; }
196 bool IsValid() { return pExpr
->IsValid(); }
197 bool IsConstant() { return pExpr
->IsConstant(); }
198 bool IsVariable() { return pExpr
->IsVariable(); }
199 bool IsLvalue() { return pExpr
->IsLvalue(); }
200 bool IsIntConstant() { return pExpr
->IsIntConst(); }
201 const OUString
& GetString() { return pExpr
->GetString(); }
202 SbiSymDef
* GetVar() { return pExpr
->GetVar(); }
203 SbiSymDef
* GetRealVar() { return pExpr
->GetRealVar(); }
204 SbiExprNode
* GetExprNode() { return pExpr
; }
205 SbxDataType
GetType() { return pExpr
->GetType(); }
206 void SetType( SbxDataType eType
){ pExpr
->eType
= eType
; }
207 void Gen( RecursiveMode eRecMode
= UNDEFINED
);
210 class SbiConstExpression
: public SbiExpression
{
214 public: // numeric constant
215 SbiConstExpression( SbiParser
* );
216 SbxDataType
GetType() { return eType
; }
217 const OUString
& GetString() { return aVal
; }
218 double GetValue() { return nVal
; }
219 short GetShortValue();
222 class SbiExprList
{ // base class for parameters and dims
225 SbiExpression
* pFirst
;
231 SbiExprList( SbiParser
* );
232 virtual ~SbiExprList();
233 bool IsBracket() { return bBracket
; }
234 bool IsValid() { return !bError
; }
235 short GetSize() { return nExpr
; }
236 short GetDims() { return nDim
; }
237 SbiExpression
* Get( short );
238 void Gen(); // code generation
239 void addExpression( SbiExpression
* pExpr
);
242 class SbiParameters
: public SbiExprList
{
244 SbiParameters( SbiParser
*, bool bStandaloneExpression
= false, bool bPar
= true);// parsing Ctor
247 class SbiDimList
: public SbiExprList
{
248 bool bConst
; // true: everything integer constants
250 SbiDimList( SbiParser
* ); // parsing Ctor
251 bool IsConstant() { return bConst
; }
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */