LibreOffice Module basic (master) 1
expr.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#pragma once
21
22#include <memory>
23
24#include "opcodes.hxx"
25#include "token.hxx"
26#include <vector>
27
28class SbiExprNode;
29class SbiExpression;
30class SbiExprList;
31class SbiParser;
32class SbiCodeGen;
33class SbiSymDef;
34class SbiProcDef;
35
36
37typedef std::unique_ptr<SbiExprList> SbiExprListPtr;
38typedef std::vector<SbiExprListPtr> SbiExprListVector;
39
40struct SbVar {
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])...
45};
46
48{
51};
52
53enum SbiExprType { // expression types:
54 SbSTDEXPR, // normal expression
55 SbLVALUE, // any lValue
56 SbSYMBOL, // any composite symbol
57 SbOPERAND // variable/function
58};
59
60enum SbiExprMode { // Expression context:
62 EXPRMODE_STANDALONE, // a param1, param2 OR a( param1, param2 ) = 42
63 EXPRMODE_LPAREN_PENDING, // start of parameter list with bracket, special handling
64 EXPRMODE_LPAREN_NOT_NEEDED, // pending LPAREN has not been used
65 EXPRMODE_ARRAY_OR_OBJECT, // '=' or '(' or '.' found after ')' on ParenLevel 0, stopping
66 // expression, assuming array syntax a(...)[(...)] = ?
67 // or a(...).b(...)
68 EXPRMODE_EMPTY_PAREN // It turned out that the paren don't contain anything: a()
69};
70
72 SbxNUMVAL, // nVal = value
73 SbxSTRVAL, // aStrVal = value, before #i59791/#i45570: nStringId = value
74 SbxVARVAL, // aVar = value
75 SbxTYPEOF, // TypeOf ObjExpr Is Type
76 SbxNODE, // Node
77 SbxNEW, // new <type> expression
79};
80
82{
86};
87
88class SbiExprList final { // class for parameters and dims
89 std::vector<std::unique_ptr<SbiExpression>> aData;
90 short nDim;
91 bool bError;
93public:
96 static SbiExprListPtr ParseParameters(SbiParser*, bool bStandaloneExpression = false, bool bPar = true);
98 bool IsBracket() const { return bBracket; }
99 bool IsValid() const { return !bError; }
100 short GetSize() const { return aData.size(); }
101 short GetDims() const { return nDim; }
102 SbiExpression* Get( size_t );
103 void Gen( SbiCodeGen& rGen); // code generation
104 void addExpression( std::unique_ptr<SbiExpression>&& pExpr );
105};
106
107class SbiExprNode final { // operators (and operands)
108 friend class SbiExpression;
109 friend class SbiConstExpression;
110 union {
111 sal_uInt16 nTypeStrId; // pooled String-ID, #i59791/#i45570 Now only for TypeOf
112 double nVal; // numeric value
113 SbVar aVar; // or variable
114 };
115 OUString aStrVal; // #i59791/#i45570 Store string directly
116 std::unique_ptr<SbiExprNode> pLeft; // left branch
117 std::unique_ptr<SbiExprNode> pRight; // right branch (NULL for unary ops)
118 SbiExprNode* pWithParent; // node, whose member is "this per with"
122 bool bError; // true: error
126 void CollectBits(); // converting numbers to strings
127 bool IsOperand() const
128 { return eNodeType != SbxNODE && eNodeType != SbxTYPEOF && eNodeType != SbxNEW; }
129 bool IsNumber() const;
130 bool IsLvalue() const; // true, if usable as Lvalue
132
133public:
134 SbiExprNode();
135 SbiExprNode( double, SbxDataType );
136 SbiExprNode( OUString );
137 SbiExprNode( const SbiSymDef&, SbxDataType, SbiExprListPtr = nullptr );
138 SbiExprNode( std::unique_ptr<SbiExprNode>, SbiToken, std::unique_ptr<SbiExprNode> );
139 SbiExprNode( std::unique_ptr<SbiExprNode>, sal_uInt16 ); // #120061 TypeOf
140 SbiExprNode( sal_uInt16 ); // new <type>
141 ~SbiExprNode();
142
143 bool IsValid() const { return !bError; }
144 bool IsConstant() const // true: constant operand
145 { return eNodeType == SbxSTRVAL || eNodeType == SbxNUMVAL; }
147 bool IsVariable() const;
148
150
151 SbxDataType GetType() const { return eType; }
152 void SetType( SbxDataType eTp ) { eType = eTp; }
154 SbiSymDef* GetVar();
155 SbiSymDef* GetRealVar(); // last variable in x.y.z
156 SbiExprNode* GetRealNode(); // last node in x.y.z
157 const OUString& GetString() const { return aStrVal; }
158 short GetNumber() const { return static_cast<short>(nVal); }
160
161 void Optimize(SbiParser*); // tree matching
162
163 void Gen( SbiCodeGen& rGen, RecursiveMode eRecMode = UNDEFINED ); // giving out a node
164};
165
167 friend class SbiExprList;
168protected:
169 OUString aArgName;
171 std::unique_ptr<SbiExprNode> pExpr; // expression tree
172 SbiExprType eCurExpr; // type of expression
173 SbiExprMode m_eMode; // expression context
174 bool bBased = false; // true: easy DIM-part (+BASE)
175 bool bError = false;
176 bool bByVal = false; // true: ByVal-Parameter
177 bool bBracket = false; // true: Parameter list with brackets
178 sal_uInt16 nParenLevel = 0;
179 std::unique_ptr<SbiExprNode> Term( const KeywordSymbolInfo* pKeywordSymbolInfo = nullptr );
180 std::unique_ptr<SbiExprNode> ObjTerm( SbiSymDef& );
181 std::unique_ptr<SbiExprNode> Operand( bool bUsedForTypeOf = false );
182 std::unique_ptr<SbiExprNode> Unary();
183 std::unique_ptr<SbiExprNode> Exp();
184 std::unique_ptr<SbiExprNode> MulDiv();
185 std::unique_ptr<SbiExprNode> IntDiv();
186 std::unique_ptr<SbiExprNode> Mod();
187 std::unique_ptr<SbiExprNode> AddSub();
188 std::unique_ptr<SbiExprNode> Cat();
189 std::unique_ptr<SbiExprNode> Like();
190 std::unique_ptr<SbiExprNode> VBA_Not();
191 std::unique_ptr<SbiExprNode> Comp();
192 std::unique_ptr<SbiExprNode> Boolean();
193public:
195 SbiExprMode eMode = EXPRMODE_STANDARD, const KeywordSymbolInfo* pKeywordSymbolInfo = nullptr ); // parsing Ctor
197 SbiExpression( SbiParser*, const SbiSymDef&, SbiExprListPtr = nullptr );
199 OUString& GetName() { return aArgName; }
200 void SetBased() { bBased = true; }
201 bool IsBased() const { return bBased; }
202 void SetByVal() { bByVal = true; }
203 bool IsBracket() const { return bBracket; }
204 bool IsValid() const { return pExpr->IsValid(); }
205 bool IsVariable() const { return pExpr->IsVariable(); }
206 bool IsLvalue() const { return pExpr->IsLvalue(); }
207 void ConvertToIntConstIfPossible() { pExpr->ConvertToIntConstIfPossible(); }
208 const OUString& GetString() const { return pExpr->GetString(); }
209 SbiSymDef* GetRealVar() { return pExpr->GetRealVar(); }
210 SbiExprNode* GetExprNode() { return pExpr.get(); }
211 SbxDataType GetType() const { return pExpr->GetType(); }
212 void Gen( RecursiveMode eRecMode = UNDEFINED );
213};
214
216 double nVal;
217 OUString aVal;
219public: // numeric constant
221 SbxDataType GetType() const { return eType; }
222 const OUString& GetString() const { return aVal; }
223 double GetValue() const { return nVal; }
224 short GetShortValue();
225};
226
227/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString aVal
Definition: expr.hxx:217
double GetValue() const
Definition: expr.hxx:223
SbiConstExpression(SbiParser *)
Definition: exprtree.cxx:802
SbxDataType eType
Definition: expr.hxx:218
SbxDataType GetType() const
Definition: expr.hxx:221
short GetShortValue()
Definition: exprtree.cxx:855
const OUString & GetString() const
Definition: expr.hxx:222
short GetSize() const
Definition: expr.hxx:100
bool bBracket
Definition: expr.hxx:92
void addExpression(std::unique_ptr< SbiExpression > &&pExpr)
Definition: exprtree.cxx:904
bool bError
Definition: expr.hxx:91
void Gen(SbiCodeGen &rGen)
Definition: exprgen.cxx:211
static SbiExprListPtr ParseParameters(SbiParser *, bool bStandaloneExpression=false, bool bPar=true)
Definition: exprtree.cxx:917
short nDim
Definition: expr.hxx:90
static SbiExprListPtr ParseDimList(SbiParser *)
Definition: exprtree.cxx:1075
short GetDims() const
Definition: expr.hxx:101
SbiExpression * Get(size_t)
Definition: exprtree.cxx:899
std::vector< std::unique_ptr< SbiExpression > > aData
Definition: expr.hxx:89
bool IsBracket() const
Definition: expr.hxx:98
bool IsValid() const
Definition: expr.hxx:99
sal_uInt16 nTypeStrId
Definition: expr.hxx:111
SbiSymDef * GetRealVar()
Definition: exprnode.cxx:127
SbxDataType eType
Definition: expr.hxx:120
bool IsVariable() const
Definition: exprnode.cxx:171
void ConvertToIntConstIfPossible()
Definition: exprnode.cxx:152
bool IsConstant() const
Definition: expr.hxx:144
SbiNodeType GetNodeType() const
Definition: expr.hxx:153
void FoldConstantsUnaryNode(SbiParser *)
Definition: exprnode.cxx:428
std::unique_ptr< SbiExprNode > pLeft
Definition: expr.hxx:116
SbiNodeType eNodeType
Definition: expr.hxx:119
SbiToken eTok
Definition: expr.hxx:121
SbiSymDef * GetVar()
Definition: exprnode.cxx:119
std::unique_ptr< SbiExprNode > pRight
Definition: expr.hxx:117
SbiExprList * GetParameters()
Definition: expr.hxx:159
void GenElement(SbiCodeGen &, SbiOpcode)
Definition: exprgen.cxx:177
void Gen(SbiCodeGen &rGen, RecursiveMode eRecMode=UNDEFINED)
Definition: exprgen.cxx:64
SbxDataType GetType() const
Definition: expr.hxx:151
void Optimize(SbiParser *)
Definition: exprnode.cxx:187
void FoldConstants(SbiParser *)
Definition: exprnode.cxx:211
void SetType(SbxDataType eTp)
Definition: expr.hxx:152
bool bError
Definition: expr.hxx:122
SbiExprNode * pWithParent
Definition: expr.hxx:118
SbiExprNode * GetRealNode()
Definition: exprnode.cxx:137
double nVal
Definition: expr.hxx:112
const OUString & GetString() const
Definition: expr.hxx:157
void FoldConstantsBinaryNode(SbiParser *)
Definition: exprnode.cxx:234
bool IsLvalue() const
Definition: exprnode.cxx:176
bool IsValid() const
Definition: expr.hxx:143
void CollectBits()
Definition: exprnode.cxx:195
bool IsOperand() const
Definition: expr.hxx:127
void SetWithParent(SbiExprNode *p)
Definition: expr.hxx:149
OUString aStrVal
Definition: expr.hxx:115
SbVar aVar
Definition: expr.hxx:113
short GetNumber() const
Definition: expr.hxx:158
bool IsNumber() const
Definition: exprnode.cxx:166
OUString & GetName()
Definition: expr.hxx:199
std::unique_ptr< SbiExprNode > VBA_Not()
Definition: exprtree.cxx:742
void Gen(RecursiveMode eRecMode=UNDEFINED)
Definition: exprgen.cxx:260
SbiExprMode m_eMode
Definition: expr.hxx:173
SbiSymDef * GetRealVar()
Definition: expr.hxx:209
std::unique_ptr< SbiExprNode > Comp()
Definition: exprtree.cxx:717
bool bError
Definition: expr.hxx:175
std::unique_ptr< SbiExprNode > Boolean()
Definition: exprtree.cxx:781
std::unique_ptr< SbiExprNode > Like()
Definition: exprtree.cxx:759
bool IsBased() const
Definition: expr.hxx:201
void ConvertToIntConstIfPossible()
Definition: expr.hxx:207
bool IsValid() const
Definition: expr.hxx:204
bool bBased
Definition: expr.hxx:174
std::unique_ptr< SbiExprNode > Unary()
Definition: exprtree.cxx:568
SbiExprNode * GetExprNode()
Definition: expr.hxx:210
bool IsBracket() const
Definition: expr.hxx:203
std::unique_ptr< SbiExprNode > Cat()
Definition: exprtree.cxx:698
SbiExprType eCurExpr
Definition: expr.hxx:172
std::unique_ptr< SbiExprNode > Operand(bool bUsedForTypeOf=false)
Definition: exprtree.cxx:482
std::unique_ptr< SbiExprNode > IntDiv()
Definition: exprtree.cxx:651
std::unique_ptr< SbiExprNode > pExpr
Definition: expr.hxx:171
bool IsLvalue() const
Definition: expr.hxx:206
std::unique_ptr< SbiExprNode > Exp()
Definition: exprtree.cxx:618
std::unique_ptr< SbiExprNode > AddSub()
Definition: exprtree.cxx:679
OUString aArgName
Definition: expr.hxx:169
bool IsVariable() const
Definition: expr.hxx:205
bool bBracket
Definition: expr.hxx:177
sal_uInt16 nParenLevel
Definition: expr.hxx:178
void SetBased()
Definition: expr.hxx:200
bool bByVal
Definition: expr.hxx:176
std::unique_ptr< SbiExprNode > MulDiv()
Definition: exprtree.cxx:632
std::unique_ptr< SbiExprNode > Term(const KeywordSymbolInfo *pKeywordSymbolInfo=nullptr)
Definition: exprtree.cxx:154
SbiParser * pParser
Definition: expr.hxx:170
const OUString & GetString() const
Definition: expr.hxx:208
SbxDataType GetType() const
Definition: expr.hxx:211
void SetByVal()
Definition: expr.hxx:202
std::unique_ptr< SbiExprNode > ObjTerm(SbiSymDef &)
Definition: exprtree.cxx:381
SbiExpression(SbiParser *, SbiExprType=SbSTDEXPR, SbiExprMode eMode=EXPRMODE_STANDARD, const KeywordSymbolInfo *pKeywordSymbolInfo=nullptr)
Definition: exprtree.cxx:28
std::unique_ptr< SbiExprNode > Mod()
Definition: exprtree.cxx:665
RecursiveMode
Definition: expr.hxx:82
@ FORCE_CALL
Definition: expr.hxx:84
@ UNDEFINED
Definition: expr.hxx:83
@ PREVENT_CALL
Definition: expr.hxx:85
std::unique_ptr< SbiExprList > SbiExprListPtr
Definition: expr.hxx:34
std::vector< SbiExprListPtr > SbiExprListVector
Definition: expr.hxx:38
SbiExprType
Definition: expr.hxx:53
@ SbSTDEXPR
Definition: expr.hxx:54
@ SbOPERAND
Definition: expr.hxx:57
@ SbLVALUE
Definition: expr.hxx:55
@ SbSYMBOL
Definition: expr.hxx:56
SbiNodeType
Definition: expr.hxx:71
@ SbxTYPEOF
Definition: expr.hxx:75
@ SbxSTRVAL
Definition: expr.hxx:73
@ SbxNODE
Definition: expr.hxx:76
@ SbxNEW
Definition: expr.hxx:77
@ SbxDUMMY
Definition: expr.hxx:78
@ SbxVARVAL
Definition: expr.hxx:74
@ SbxNUMVAL
Definition: expr.hxx:72
SbiExprMode
Definition: expr.hxx:60
@ EXPRMODE_LPAREN_PENDING
Definition: expr.hxx:63
@ EXPRMODE_EMPTY_PAREN
Definition: expr.hxx:68
@ EXPRMODE_LPAREN_NOT_NEEDED
Definition: expr.hxx:64
@ EXPRMODE_ARRAY_OR_OBJECT
Definition: expr.hxx:65
@ EXPRMODE_STANDARD
Definition: expr.hxx:61
@ EXPRMODE_STANDALONE
Definition: expr.hxx:62
void * p
SbiOpcode
Definition: opcodes.hxx:25
SbxDataType
Definition: sbxdef.hxx:37
OUString m_aKeywordSymbol
Definition: expr.hxx:49
SbxDataType m_eSbxDataType
Definition: expr.hxx:50
Definition: expr.hxx:40
SbiExprListVector * pvMorePar
Definition: expr.hxx:44
SbiExprNode * pNext
Definition: expr.hxx:41
SbiSymDef * pDef
Definition: expr.hxx:42
SbiExprList * pPar
Definition: expr.hxx:43
SbiToken
Definition: token.hxx:30