47 EXPR_CAST,
48 EXPR_FORCE_CAST,
49 EXPR_IMPLIED_CAST,
50 EXPR_SIZEOF,
51 EXPR_ALIGNOF,
52 EXPR_PTRSIZEOF,
53 EXPR_CONDITIONAL,
54 EXPR_SELECT, // a "safe" conditional expression
55 EXPR_STATEMENT,
56 EXPR_CALL,
57 EXPR_COMMA,
58 EXPR_COMPARE,
59 EXPR_LABEL,
60 EXPR_INITIALIZER, // initializer list
61 EXPR_IDENTIFIER, // identifier in initializer
62 EXPR_INDEX, // index in initializer
63 EXPR_POS, // position in initializer
64 EXPR_FVALUE,
65 EXPR_SLICE,
66 EXPR_OFFSETOF,
67 };
68
69
70 /*
71 * Flags for tracking the promotion of constness related attributes
72 * from subexpressions to their parents.
73 *
74 * The flags are not independent as one might imply another.
75 * The implications are as follows:
76 * - CEF_INT, CEF_ENUM and
77 * CEF_CHAR imply CEF_ICE.
78 *
79 * Use the CEF_*_SET_MASK and CEF_*_CLEAR_MASK
80 * helper macros defined below to set or clear one of these flags.
81 */
82 enum constexpr_flag {
83 CEF_NONE = 0,
84 /*
85 * A constant in the sense of [6.4.4]:
86 * - Integer constant [6.4.4.1]
177 };
178
179 // EXPR_STATEMENT
180 struct statement *statement;
181
182 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
183 struct /* binop_arg */ {
184 struct expression *left, *right;
185 };
186 // EXPR_DEREF
187 struct /* deref_arg */ {
188 struct expression *deref;
189 struct ident *member;
190 int member_offset;
191 };
192 // EXPR_SLICE
193 struct /* slice */ {
194 struct expression *base;
195 unsigned r_bitpos, r_nrbits;
196 };
197 // EXPR_CAST and EXPR_SIZEOF
198 struct /* cast_arg */ {
199 struct symbol *cast_type;
200 struct expression *cast_expression;
201 };
202 // EXPR_CONDITIONAL
203 // EXPR_SELECT
204 struct /* conditional_expr */ {
205 struct expression *conditional, *cond_true, *cond_false;
206 };
207 // EXPR_CALL
208 struct /* call_expr */ {
209 struct expression *fn;
210 struct expression_list *args;
211 };
212 // EXPR_LABEL
213 struct /* label_expr */ {
214 struct symbol *label_symbol;
215 };
216 // EXPR_INITIALIZER
217 struct expression_list *expr_list;
224 };
225 // EXPR_INDEX
226 struct /* index_expr */ {
227 unsigned int idx_from, idx_to;
228 struct expression *idx_expression;
229 };
230 // EXPR_POS
231 struct /* initpos_expr */ {
232 unsigned int init_offset, init_nr;
233 struct expression *init_expr;
234 };
235 // EXPR_OFFSETOF
236 struct {
237 struct symbol *in;
238 struct expression *down;
239 union {
240 struct ident *ident;
241 struct expression *index;
242 };
243 };
244 };
245 };
246
247 /* Constant expression values */
248 int is_zero_constant(struct expression *);
249 int expr_truth_value(struct expression *expr);
250 long long get_expression_value(struct expression *);
251 long long const_expression_value(struct expression *);
252 long long get_expression_value_silent(struct expression *expr);
253
254 /* Expression parsing */
255 struct token *parse_expression(struct token *token, struct expression **tree);
256 struct token *conditional_expression(struct token *token, struct expression **tree);
257 struct token *primary_expression(struct token *token, struct expression **tree);
258 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
259 struct token *assignment_expression(struct token *token, struct expression **tree);
260
261 extern void evaluate_symbol_list(struct symbol_list *list);
262 extern struct symbol *evaluate_statement(struct statement *stmt);
263 extern struct symbol *evaluate_expression(struct expression *);
264 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset);
265
266 extern int expand_symbol(struct symbol *);
267
268 static inline struct expression *alloc_expression(struct position pos, int type)
269 {
|
47 EXPR_CAST,
48 EXPR_FORCE_CAST,
49 EXPR_IMPLIED_CAST,
50 EXPR_SIZEOF,
51 EXPR_ALIGNOF,
52 EXPR_PTRSIZEOF,
53 EXPR_CONDITIONAL,
54 EXPR_SELECT, // a "safe" conditional expression
55 EXPR_STATEMENT,
56 EXPR_CALL,
57 EXPR_COMMA,
58 EXPR_COMPARE,
59 EXPR_LABEL,
60 EXPR_INITIALIZER, // initializer list
61 EXPR_IDENTIFIER, // identifier in initializer
62 EXPR_INDEX, // index in initializer
63 EXPR_POS, // position in initializer
64 EXPR_FVALUE,
65 EXPR_SLICE,
66 EXPR_OFFSETOF,
67 EXPR_ASM_OPERAND,
68 };
69
70
71 /*
72 * Flags for tracking the promotion of constness related attributes
73 * from subexpressions to their parents.
74 *
75 * The flags are not independent as one might imply another.
76 * The implications are as follows:
77 * - CEF_INT, CEF_ENUM and
78 * CEF_CHAR imply CEF_ICE.
79 *
80 * Use the CEF_*_SET_MASK and CEF_*_CLEAR_MASK
81 * helper macros defined below to set or clear one of these flags.
82 */
83 enum constexpr_flag {
84 CEF_NONE = 0,
85 /*
86 * A constant in the sense of [6.4.4]:
87 * - Integer constant [6.4.4.1]
178 };
179
180 // EXPR_STATEMENT
181 struct statement *statement;
182
183 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
184 struct /* binop_arg */ {
185 struct expression *left, *right;
186 };
187 // EXPR_DEREF
188 struct /* deref_arg */ {
189 struct expression *deref;
190 struct ident *member;
191 int member_offset;
192 };
193 // EXPR_SLICE
194 struct /* slice */ {
195 struct expression *base;
196 unsigned r_bitpos, r_nrbits;
197 };
198 // EXPR_CAST, EXPR_FORCE_CAST, EXPR_IMPLIED_CAST,
199 // EXPR_SIZEOF, EXPR_ALIGNOF and EXPR_PTRSIZEOF
200 struct /* cast_arg */ {
201 struct symbol *cast_type;
202 struct expression *cast_expression;
203 };
204 // EXPR_CONDITIONAL
205 // EXPR_SELECT
206 struct /* conditional_expr */ {
207 struct expression *conditional, *cond_true, *cond_false;
208 };
209 // EXPR_CALL
210 struct /* call_expr */ {
211 struct expression *fn;
212 struct expression_list *args;
213 };
214 // EXPR_LABEL
215 struct /* label_expr */ {
216 struct symbol *label_symbol;
217 };
218 // EXPR_INITIALIZER
219 struct expression_list *expr_list;
226 };
227 // EXPR_INDEX
228 struct /* index_expr */ {
229 unsigned int idx_from, idx_to;
230 struct expression *idx_expression;
231 };
232 // EXPR_POS
233 struct /* initpos_expr */ {
234 unsigned int init_offset, init_nr;
235 struct expression *init_expr;
236 };
237 // EXPR_OFFSETOF
238 struct {
239 struct symbol *in;
240 struct expression *down;
241 union {
242 struct ident *ident;
243 struct expression *index;
244 };
245 };
246 // EXPR_ASM_OPERAND
247 struct {
248 struct ident *name;
249 struct expression *constraint;
250 struct expression *expr;
251 };
252 };
253 };
254
255 ///
256 // Constant expression values
257 // --------------------------
258
259 ///
260 // test if an expression evaluates to the constant ``0``.
261 // @return: ``1`` if @expr evaluate to ``0``,
262 // ``0`` otherwise.
263 int is_zero_constant(struct expression *expr);
264
265 ///
266 // test the compile time truth value of an expression
267 // @return:
268 // * ``-1`` if @expr is not constant,
269 // * ``0`` or ``1`` depending on the truth value of @expr.
270 int expr_truth_value(struct expression *expr);
271
272 long long get_expression_value(struct expression *);
273 long long const_expression_value(struct expression *);
274 long long get_expression_value_silent(struct expression *expr);
275
276 /* Expression parsing */
277 struct token *parse_expression(struct token *token, struct expression **tree);
278 struct token *conditional_expression(struct token *token, struct expression **tree);
279 struct token *primary_expression(struct token *token, struct expression **tree);
280 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
281 struct token *assignment_expression(struct token *token, struct expression **tree);
282
283 extern void evaluate_symbol_list(struct symbol_list *list);
284 extern struct symbol *evaluate_statement(struct statement *stmt);
285 extern struct symbol *evaluate_expression(struct expression *);
286 struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset);
287
288 extern int expand_symbol(struct symbol *);
289
290 static inline struct expression *alloc_expression(struct position pos, int type)
291 {
|