xref: /aoo4110/main/idlc/inc/idlc/astexpression.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _IDLC_ASTEXPRESSION_HXX_
24 #define _IDLC_ASTEXPRESSION_HXX_
25 
26 #include <idlc/idlc.hxx>
27 
28 // Enum to define all the different operators to combine expressions
29 enum ExprComb
30 {
31 	EC_add,		// '+'
32 	EC_minus,	// '-'
33 	EC_mul,		// '*'
34 	EC_div,		// '/'
35 	EC_mod,		// '%'
36 	EC_or,		// '|'
37 	EC_xor,		// '^'
38 	EC_and,		// '&'
39 	EC_left,	// '<<'
40 	EC_right,	// '>>'
41 	EC_u_plus,	// unary '+'
42 	EC_u_minus,	// unary '-'
43 	EC_bit_neg,	// '~'
44 	EC_none,	// No operator (missing)
45 	EC_symbol	// a symbol (function or constant name)
46 };
47 
48 // Enum to define the different kinds of evaluation possible
49 enum EvalKind
50 {
51 	EK_const,			// Must evaluate to constant
52 	EK_positive_int		// Must evaluate to positive integer
53 };
54 
55 // Enum to define expression type
56 enum ExprType
57 {
58 	ET_short,		// Expression value is short
59 	ET_ushort,		// Expression value is unsigned short
60 	ET_long,		// Expression value is long
61 	ET_ulong,		// Expression value is unsigned long
62 	ET_hyper,		// Expression value is hyper (64 bit)
63 	ET_uhyper,		// Expression value is unsigned hyper
64 	ET_float,		// Expression value is 32-bit float
65 	ET_double,		// Expression value is 64-bit float
66 	ET_char,		// Expression value is char
67 	ET_byte,		// Expression value is byte
68 	ET_boolean,		// Expression value is boolean
69 	ET_string,		// Expression value is char *
70 	ET_any,			// Expression value is any of above
71 	ET_void,		// Expression value is void (absent)
72 	ET_type,		// Expression value is type
73 	ET_none 		// Expression value is missing
74 };
75 
76 // Structure to describe value of constant expression and its type
77 struct AstExprValue
78 {
79 	union
80 	{
81 		sal_uInt8		byval;		// Contains byte expression value
82 		sal_Int16		sval;		// Contains short expression value
83 		sal_uInt16		usval;		// Contains unsigned short expr value
84 		sal_Int32		lval;		// Contains long expression value
85 		sal_uInt32		ulval;		// Contains unsigned long expr value
86 		sal_Int64		hval;		// Contains hyper expression value
87 		sal_uInt64		uhval;		// Contains unsigned hyper expr value
88 		sal_Bool		bval;		// Contains boolean expression value
89 		float			fval;		// Contains 32-bit float expr value
90 		double			dval;		// Contains 64-bit float expr value
91 		sal_uInt32		eval;		// Contains enumeration value
92 	} u;
93 	ExprType et;
94 };
95 
96 const sal_Char* SAL_CALL exprTypeToString(ExprType t);
97 
98 class AstExpression
99 {
100 public:
101 	// Constructor(s)
102 	AstExpression(ExprComb c, AstExpression *pExpr1, AstExpression *pExpr2);
103 
104 	AstExpression(sal_Int32			l);
105 	AstExpression(sal_Int32			l, ExprType et);
106 	AstExpression(sal_Int64			h);
107 	AstExpression(sal_uInt64		uh);
108 	AstExpression(double			d);
109 	AstExpression(::rtl::OString* scopedName);
110 
111 	virtual ~AstExpression();
112 
113 	// Data Accessors
getScope()114 	AstScope* getScope()
115 		{ return m_pScope; }
setScope(AstScope * pScope)116 	void setScope(AstScope* pScope)
117 		{ m_pScope = pScope; }
getLine()118 	sal_Int32 getLine()
119 		{ return m_lineNo; }
setLine(sal_Int32 l)120 	void setLine(sal_Int32 l)
121 		{ m_lineNo = l; }
getFileName()122 	const ::rtl::OString& getFileName()
123 		{ return m_fileName; }
setFileName(const::rtl::OString & fileName)124 	void setFileName(const ::rtl::OString& fileName)
125 		{ m_fileName = fileName; }
getCombOperator()126 	ExprComb getCombOperator()
127 		{ return m_combOperator; }
setCombOperator(ExprComb new_ec)128 	void setCombOperator(ExprComb new_ec)
129 		{ m_combOperator = new_ec; }
getExprValue()130 	AstExprValue* getExprValue()
131 		{ return m_exprValue; }
setExprValue(AstExprValue * pEv)132 	void setExprValue(AstExprValue *pEv)
133 		{ m_exprValue = pEv; }
getExpr1()134 	AstExpression* getExpr1()
135 		{ return m_subExpr1; }
setExpr1(AstExpression * pExpr)136 	void setExpr1(AstExpression *pExpr)
137 		{ m_subExpr1 = pExpr; }
getExpr2()138 	AstExpression* getExpr2()
139 		{ return m_subExpr2; }
setExpr2(AstExpression * pExpr)140 	void setExpr2(AstExpression *pExpr)
141 		{ m_subExpr2 = pExpr; }
getSymbolicName()142 	::rtl::OString* getSymbolicName()
143 		{ return m_pSymbolicName; }
setSymbolicName(::rtl::OString * pSymbolicName)144 	void setSymbolicName(::rtl::OString* pSymbolicName)
145 		{ m_pSymbolicName = pSymbolicName; }
146 
147 	// Evaluation and value coercion
148 	AstExprValue* coerce(ExprType type, sal_Bool bAssign=sal_True);
149 
150 	// Evaluate then store value inside this AstExpression
151 	void evaluate(EvalKind ek);
152 
153 	// Compare to AstExpressions
154 	sal_Bool operator==(AstExpression *pExpr);
155 	sal_Bool compare(AstExpression *pExpr);
156 
157 	::rtl::OString toString();
dump()158 	void dump() {}
159 private:
160 	// Fill out the lineno, filename and definition scope details
161 	void	fillDefinitionDetails();
162 	// Internal evaluation
163 	AstExprValue* eval_internal(EvalKind ek);
164 	// Evaluate different sets of operators
165 	AstExprValue* eval_bin_op(EvalKind ek);
166 	AstExprValue* eval_bit_op(EvalKind ek);
167 	AstExprValue* eval_un_op(EvalKind ek);
168 	AstExprValue* eval_symbol(EvalKind ek);
169 
170 	AstScope*		m_pScope;		// scope defined in
171 	sal_Int32		m_lineNo;		// line number defined in
172 	::rtl::OString	m_fileName;		// fileName	defined in
173 
174 	ExprComb		m_combOperator;
175 	AstExpression*	m_subExpr1;
176 	AstExpression*	m_subExpr2;
177 	AstExprValue*	m_exprValue;
178 	::rtl::OString*	m_pSymbolicName;
179 };
180 
181 #endif // _IDLC_ASTEXPRESSION_HXX_
182 
183