1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #ifndef _IDLC_ASTEXPRESSION_HXX_ 28 #define _IDLC_ASTEXPRESSION_HXX_ 29 30 #include <idlc/idlc.hxx> 31 32 // Enum to define all the different operators to combine expressions 33 enum ExprComb 34 { 35 EC_add, // '+' 36 EC_minus, // '-' 37 EC_mul, // '*' 38 EC_div, // '/' 39 EC_mod, // '%' 40 EC_or, // '|' 41 EC_xor, // '^' 42 EC_and, // '&' 43 EC_left, // '<<' 44 EC_right, // '>>' 45 EC_u_plus, // unary '+' 46 EC_u_minus, // unary '-' 47 EC_bit_neg, // '~' 48 EC_none, // No operator (missing) 49 EC_symbol // a symbol (function or constant name) 50 }; 51 52 // Enum to define the different kinds of evaluation possible 53 enum EvalKind 54 { 55 EK_const, // Must evaluate to constant 56 EK_positive_int // Must evaluate to positive integer 57 }; 58 59 // Enum to define expression type 60 enum ExprType 61 { 62 ET_short, // Expression value is short 63 ET_ushort, // Expression value is unsigned short 64 ET_long, // Expression value is long 65 ET_ulong, // Expression value is unsigned long 66 ET_hyper, // Expression value is hyper (64 bit) 67 ET_uhyper, // Expression value is unsigned hyper 68 ET_float, // Expression value is 32-bit float 69 ET_double, // Expression value is 64-bit float 70 ET_char, // Expression value is char 71 ET_byte, // Expression value is byte 72 ET_boolean, // Expression value is boolean 73 ET_string, // Expression value is char * 74 ET_any, // Expression value is any of above 75 ET_void, // Expression value is void (absent) 76 ET_type, // Expression value is type 77 ET_none // Expression value is missing 78 }; 79 80 // Structure to describe value of constant expression and its type 81 struct AstExprValue 82 { 83 union 84 { 85 sal_uInt8 byval; // Contains byte expression value 86 sal_Int16 sval; // Contains short expression value 87 sal_uInt16 usval; // Contains unsigned short expr value 88 sal_Int32 lval; // Contains long expression value 89 sal_uInt32 ulval; // Contains unsigned long expr value 90 sal_Int64 hval; // Contains hyper expression value 91 sal_uInt64 uhval; // Contains unsigned hyper expr value 92 sal_Bool bval; // Contains boolean expression value 93 float fval; // Contains 32-bit float expr value 94 double dval; // Contains 64-bit float expr value 95 sal_uInt32 eval; // Contains enumeration value 96 } u; 97 ExprType et; 98 }; 99 100 const sal_Char* SAL_CALL exprTypeToString(ExprType t); 101 102 class AstExpression 103 { 104 public: 105 // Constructor(s) 106 AstExpression(ExprComb c, AstExpression *pExpr1, AstExpression *pExpr2); 107 108 AstExpression(sal_Int32 l); 109 AstExpression(sal_Int32 l, ExprType et); 110 AstExpression(sal_Int64 h); 111 AstExpression(sal_uInt64 uh); 112 AstExpression(double d); 113 AstExpression(::rtl::OString* scopedName); 114 115 virtual ~AstExpression(); 116 117 // Data Accessors 118 AstScope* getScope() 119 { return m_pScope; } 120 void setScope(AstScope* pScope) 121 { m_pScope = pScope; } 122 sal_Int32 getLine() 123 { return m_lineNo; } 124 void setLine(sal_Int32 l) 125 { m_lineNo = l; } 126 const ::rtl::OString& getFileName() 127 { return m_fileName; } 128 void setFileName(const ::rtl::OString& fileName) 129 { m_fileName = fileName; } 130 ExprComb getCombOperator() 131 { return m_combOperator; } 132 void setCombOperator(ExprComb new_ec) 133 { m_combOperator = new_ec; } 134 AstExprValue* getExprValue() 135 { return m_exprValue; } 136 void setExprValue(AstExprValue *pEv) 137 { m_exprValue = pEv; } 138 AstExpression* getExpr1() 139 { return m_subExpr1; } 140 void setExpr1(AstExpression *pExpr) 141 { m_subExpr1 = pExpr; } 142 AstExpression* getExpr2() 143 { return m_subExpr2; } 144 void setExpr2(AstExpression *pExpr) 145 { m_subExpr2 = pExpr; } 146 ::rtl::OString* getSymbolicName() 147 { return m_pSymbolicName; } 148 void setSymbolicName(::rtl::OString* pSymbolicName) 149 { m_pSymbolicName = pSymbolicName; } 150 151 // Evaluation and value coercion 152 AstExprValue* coerce(ExprType type, sal_Bool bAssign=sal_True); 153 154 // Evaluate then store value inside this AstExpression 155 void evaluate(EvalKind ek); 156 157 // Compare to AstExpressions 158 sal_Bool operator==(AstExpression *pExpr); 159 sal_Bool compare(AstExpression *pExpr); 160 161 ::rtl::OString toString(); 162 void dump() {} 163 private: 164 // Fill out the lineno, filename and definition scope details 165 void fillDefinitionDetails(); 166 // Internal evaluation 167 AstExprValue* eval_internal(EvalKind ek); 168 // Evaluate different sets of operators 169 AstExprValue* eval_bin_op(EvalKind ek); 170 AstExprValue* eval_bit_op(EvalKind ek); 171 AstExprValue* eval_un_op(EvalKind ek); 172 AstExprValue* eval_symbol(EvalKind ek); 173 174 AstScope* m_pScope; // scope defined in 175 sal_Int32 m_lineNo; // line number defined in 176 ::rtl::OString m_fileName; // fileName defined in 177 178 ExprComb m_combOperator; 179 AstExpression* m_subExpr1; 180 AstExpression* m_subExpr2; 181 AstExprValue* m_exprValue; 182 ::rtl::OString* m_pSymbolicName; 183 }; 184 185 #endif // _IDLC_ASTEXPRESSION_HXX_ 186 187