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 .
24 #include "opcodes.hxx"
37 typedef std::unique_ptr
<SbiExprList
> SbiExprListPtr
;
38 typedef std::vector
<SbiExprListPtr
> 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
;
53 enum SbiExprType
{ // expression types:
54 SbSTDEXPR
, // normal expression
55 SbLVALUE
, // any lValue
56 SbSYMBOL
, // any composite symbol
57 SbOPERAND
// variable/function
60 enum SbiExprMode
{ // Expression context:
61 EXPRMODE_STANDARD
, // default
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(...)[(...)] = ?
68 EXPRMODE_EMPTY_PAREN
// It turned out that the paren don't contain anything: a()
72 SbxNUMVAL
, // nVal = value
73 SbxSTRVAL
, // aStrVal = value, before #i59791/#i45570: nStringId = value
74 SbxVARVAL
, // aVar = value
75 SbxTYPEOF
, // TypeOf ObjExpr Is Type
77 SbxNEW
, // new <type> expression
88 class SbiExprNode final
{ // operators (and operands)
89 friend class SbiExpression
;
90 friend class SbiConstExpression
;
92 sal_uInt16 nTypeStrId
; // pooled String-ID, #i59791/#i45570 Now only for TypeOf
93 double nVal
; // numeric value
94 SbVar aVar
; // or variable
96 OUString aStrVal
; // #i59791/#i45570 Store string directly
97 std::unique_ptr
<SbiExprNode
> pLeft
; // left branch
98 std::unique_ptr
<SbiExprNode
> pRight
; // right branch (NULL for unary ops)
99 SbiExprNode
* pWithParent
; // node, whose member is "this per with"
100 SbiNodeType eNodeType
;
103 bool bError
; // true: error
104 void FoldConstants(SbiParser
*);
105 void FoldConstantsBinaryNode(SbiParser
*);
106 void FoldConstantsUnaryNode(SbiParser
*);
107 void CollectBits(); // converting numbers to strings
108 bool IsOperand() const
109 { return eNodeType
!= SbxNODE
&& eNodeType
!= SbxTYPEOF
&& eNodeType
!= SbxNEW
; }
110 bool IsNumber() const;
111 bool IsLvalue() const; // true, if usable as Lvalue
112 void GenElement( SbiCodeGen
&, SbiOpcode
);
116 SbiExprNode( double, SbxDataType
);
117 SbiExprNode( const OUString
& );
118 SbiExprNode( const SbiSymDef
&, SbxDataType
, SbiExprListPtr
= nullptr );
119 SbiExprNode( std::unique_ptr
<SbiExprNode
>, SbiToken
, std::unique_ptr
<SbiExprNode
> );
120 SbiExprNode( std::unique_ptr
<SbiExprNode
>, sal_uInt16
); // #120061 TypeOf
121 SbiExprNode( sal_uInt16
); // new <type>
124 bool IsValid() const { return !bError
; }
125 bool IsConstant() const // true: constant operand
126 { return eNodeType
== SbxSTRVAL
|| eNodeType
== SbxNUMVAL
; }
127 void ConvertToIntConstIfPossible();
128 bool IsVariable() const;
130 void SetWithParent( SbiExprNode
* p
) { pWithParent
= p
; }
132 SbxDataType
GetType() const { return eType
; }
133 void SetType( SbxDataType eTp
) { eType
= eTp
; }
134 SbiNodeType
GetNodeType() const { return eNodeType
; }
136 SbiSymDef
* GetRealVar(); // last variable in x.y.z
137 SbiExprNode
* GetRealNode(); // last node in x.y.z
138 const OUString
& GetString() const { return aStrVal
; }
139 short GetNumber() const { return static_cast<short>(nVal
); }
140 SbiExprList
* GetParameters() { return aVar
.pPar
; }
142 void Optimize(SbiParser
*); // tree matching
144 void Gen( SbiCodeGen
& rGen
, RecursiveMode eRecMode
= UNDEFINED
); // giving out a node
147 class SbiExpression
{
148 friend class SbiExprList
;
152 std::unique_ptr
<SbiExprNode
> pExpr
; // expression tree
153 SbiExprType eCurExpr
; // type of expression
154 SbiExprMode m_eMode
; // expression context
155 bool bBased
= false; // true: easy DIM-part (+BASE)
157 bool bByVal
= false; // true: ByVal-Parameter
158 bool bBracket
= false; // true: Parameter list with brackets
159 sal_uInt16 nParenLevel
= 0;
160 std::unique_ptr
<SbiExprNode
> Term( const KeywordSymbolInfo
* pKeywordSymbolInfo
= nullptr );
161 std::unique_ptr
<SbiExprNode
> ObjTerm( SbiSymDef
& );
162 std::unique_ptr
<SbiExprNode
> Operand( bool bUsedForTypeOf
= false );
163 std::unique_ptr
<SbiExprNode
> Unary();
164 std::unique_ptr
<SbiExprNode
> Exp();
165 std::unique_ptr
<SbiExprNode
> MulDiv();
166 std::unique_ptr
<SbiExprNode
> IntDiv();
167 std::unique_ptr
<SbiExprNode
> Mod();
168 std::unique_ptr
<SbiExprNode
> AddSub();
169 std::unique_ptr
<SbiExprNode
> Cat();
170 std::unique_ptr
<SbiExprNode
> Like();
171 std::unique_ptr
<SbiExprNode
> VBA_Not();
172 std::unique_ptr
<SbiExprNode
> Comp();
173 std::unique_ptr
<SbiExprNode
> Boolean();
175 SbiExpression( SbiParser
*, SbiExprType
= SbSTDEXPR
,
176 SbiExprMode eMode
= EXPRMODE_STANDARD
, const KeywordSymbolInfo
* pKeywordSymbolInfo
= nullptr ); // parsing Ctor
177 SbiExpression( SbiParser
*, double, SbxDataType
);
178 SbiExpression( SbiParser
*, const SbiSymDef
&, SbiExprListPtr
= nullptr );
180 OUString
& GetName() { return aArgName
; }
181 void SetBased() { bBased
= true; }
182 bool IsBased() const { return bBased
; }
183 void SetByVal() { bByVal
= true; }
184 bool IsBracket() const { return bBracket
; }
185 bool IsValid() const { return pExpr
->IsValid(); }
186 bool IsVariable() const { return pExpr
->IsVariable(); }
187 bool IsLvalue() const { return pExpr
->IsLvalue(); }
188 void ConvertToIntConstIfPossible() { pExpr
->ConvertToIntConstIfPossible(); }
189 const OUString
& GetString() const { return pExpr
->GetString(); }
190 SbiSymDef
* GetRealVar() { return pExpr
->GetRealVar(); }
191 SbiExprNode
* GetExprNode() { return pExpr
.get(); }
192 SbxDataType
GetType() const { return pExpr
->GetType(); }
193 void Gen( RecursiveMode eRecMode
= UNDEFINED
);
196 class SbiConstExpression
: public SbiExpression
{
200 public: // numeric constant
201 SbiConstExpression( SbiParser
* );
202 SbxDataType
GetType() const { return eType
; }
203 const OUString
& GetString() const { return aVal
; }
204 double GetValue() const { return nVal
; }
205 short GetShortValue();
208 class SbiExprList final
{ // class for parameters and dims
209 std::vector
<std::unique_ptr
<SbiExpression
>> aData
;
216 static SbiExprListPtr
ParseParameters(SbiParser
*, bool bStandaloneExpression
= false, bool bPar
= true);
217 static SbiExprListPtr
ParseDimList( SbiParser
* );
218 bool IsBracket() const { return bBracket
; }
219 bool IsValid() const { return !bError
; }
220 short GetSize() const { return aData
.size(); }
221 short GetDims() const { return nDim
; }
222 SbiExpression
* Get( size_t );
223 void Gen( SbiCodeGen
& rGen
); // code generation
224 void addExpression( std::unique_ptr
<SbiExpression
>&& pExpr
);
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */