Debian Codegraph @

basic/source/inc/expr.hxx

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