1 /******************************************************************************
3 * $Id: constexp.y,v 1.6 2001/03/19 19:27:40 root Exp $
6 * Copyright (C) 1997-2012 by Dimitri van Heesch.
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation under the terms of the GNU General Public License is hereby
10 * granted. No representations are made about the suitability of this software
11 * for any purpose. It is provided "as is" without express or implied warranty.
12 * See the GNU General Public License for more details.
14 * Documents produced by Doxygen are derivative works derived from the
15 * input used in their production; they are not affected by this license.
29 #define YYSTYPE CPPValue
34 int cppExpYYerror(const char *s)
36 warn(g_constExpFileName,g_constExpLineNr,
37 "warning: preprocessing issue while doing constant expression evaluation: %s",s);
45 %token TOK_QUESTIONMARK
55 %token TOK_GREATERTHAN
56 %token TOK_LESSTHANOREQUALTO
57 %token TOK_GREATERTHANOREQUALTO
71 %token TOK_HEXADECIMALINT
77 start: constant_expression
78 { g_resultValue = $1; return 0; }
81 constant_expression: logical_or_expression
83 | logical_or_expression
84 TOK_QUESTIONMARK logical_or_expression
85 TOK_COLON logical_or_expression
87 bool c = ($1.isInt() ? ((long)$1 != 0) : ((double)$1 != 0.0));
92 logical_or_expression: logical_and_expression
94 | logical_or_expression TOK_OR logical_and_expression
96 $$ = CPPValue( (long)((long)$1 || (long)$3) );
100 logical_and_expression: inclusive_or_expression
102 | logical_and_expression TOK_AND inclusive_or_expression
104 $$ = CPPValue( (long)((long)$1 && (long)$3) );
108 inclusive_or_expression: exclusive_or_expression
110 | inclusive_or_expression TOK_BITWISEOR
111 exclusive_or_expression
113 $$ = CPPValue( (long)$1 | (long)$3 );
117 exclusive_or_expression: and_expression
119 | exclusive_or_expression TOK_BITWISEXOR and_expression
121 $$ = CPPValue( (long)$1 ^ (long)$3 );
125 and_expression: equality_expression
127 | and_expression TOK_AMPERSAND equality_expression
129 $$ = CPPValue( (long)$1 & (long)$3 );
133 equality_expression: relational_expression
135 | equality_expression TOK_EQUAL relational_expression
137 $$ = CPPValue( (long)((double)$1 == (double)$3) );
139 | equality_expression TOK_NOTEQUAL relational_expression
141 $$ = CPPValue( (long)((double)$1 != (double)$3) );
145 relational_expression: shift_expression
147 | relational_expression TOK_LESSTHAN shift_expression
149 $$ = CPPValue( (long)((double)$1 < (double)$3) );
151 | relational_expression TOK_GREATERTHAN shift_expression
153 $$ = CPPValue( (long)((double)$1 > (double)$3) );
155 | relational_expression TOK_LESSTHANOREQUALTO
158 $$ = CPPValue( (long)((double)$1 <= (double)$3) );
160 | relational_expression TOK_GREATERTHANOREQUALTO
163 $$ = CPPValue( (long)((double)$1 >= (double)$3) );
167 shift_expression: additive_expression
169 | shift_expression TOK_SHIFTLEFT additive_expression
171 $$ = CPPValue( (long)$1 << (long)$3 );
173 | shift_expression TOK_SHIFTRIGHT additive_expression
175 $$ = CPPValue( (long)$1 >> (long)$3 );
179 additive_expression: multiplicative_expression
181 | additive_expression TOK_PLUS multiplicative_expression
183 if (!$1.isInt() || !$3.isInt())
185 $$ = CPPValue( (double)$1 + (double)$3 );
189 $$ = CPPValue( (long)$1 + (long)$3 );
192 | additive_expression TOK_MINUS multiplicative_expression
194 if (!$1.isInt() || !$3.isInt())
196 $$ = CPPValue( (double)$1 - (double)$3 );
200 $$ = CPPValue( (long)$1 - (long)$3 );
205 multiplicative_expression: unary_expression
207 | multiplicative_expression TOK_STAR unary_expression
209 if (!$1.isInt() || !$3.isInt())
211 $$ = CPPValue( (double)$1 * (double)$3 );
215 $$ = CPPValue( (long)$1 * (long)$3 );
218 | multiplicative_expression TOK_DIVIDE unary_expression
220 if (!$1.isInt() || !$3.isInt())
222 $$ = CPPValue( (double)$1 / (double)$3 );
227 if (value==0) value=1;
228 $$ = CPPValue( (long)$1 / value );
231 | multiplicative_expression TOK_MOD unary_expression
234 if (value==0) value=1;
235 $$ = CPPValue( (long)$1 % value );
239 unary_expression: primary_expression
241 | TOK_PLUS unary_expression
243 | TOK_MINUS unary_expression
246 $$ = CPPValue(-(long)$2);
248 $$ = CPPValue(-(double)$2);
250 | TOK_TILDE unary_expression
252 $$ = CPPValue(~(long)$2);
254 | TOK_NOT unary_expression
256 $$ = CPPValue((long)!(long)$2);
260 primary_expression: constant
262 | TOK_LPAREN constant_expression TOK_RPAREN
266 constant: TOK_OCTALINT
267 { $$ = parseOctal(); }
269 { $$ = parseDecimal(); }
271 { $$ = parseHexadecimal(); }
273 { $$ = parseCharacter(); }
275 { $$ = parseFloat(); }