6 * Copyright (C) 2003 Transmeta Corp.
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * Declarations and helper functions for expression parsing.
34 struct expression_list
;
36 enum expression_type
{
54 EXPR_SELECT
, // a "safe" conditional expression
60 EXPR_INITIALIZER
, // initializer list
61 EXPR_IDENTIFIER
, // identifier in initializer
62 EXPR_INDEX
, // index in initializer
63 EXPR_POS
, // position in initializer
72 * Flags for tracking the promotion of constness related attributes
73 * from subexpressions to their parents.
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.
80 * Use the CEF_*_SET_MASK and CEF_*_CLEAR_MASK
81 * helper macros defined below to set or clear one of these flags.
86 * A constant in the sense of [6.4.4]:
87 * - Integer constant [6.4.4.1]
88 * - Floating point constant [6.4.4.2]
89 * - Enumeration constant [6.4.4.3]
90 * - Character constant [6.4.4.4]
98 * A constant expression in the sense of [6.6]:
99 * - integer constant expression [6.6(6)]
100 * - arithmetic constant expression [6.6(8)]
101 * - address constant [6.6(9)]
107 /* integer constant expression => arithmetic constant expression */
108 CEF_SET_ICE
= (CEF_ICE
| CEF_ACE
),
110 /* integer constant => integer constant expression */
111 CEF_SET_INT
= (CEF_INT
| CEF_SET_ICE
),
113 /* floating point constant => arithmetic constant expression */
114 CEF_SET_FLOAT
= (CEF_FLOAT
| CEF_ACE
),
116 /* enumeration constant => integer constant expression */
117 CEF_SET_ENUM
= (CEF_ENUM
| CEF_SET_ICE
),
119 /* character constant => integer constant expression */
120 CEF_SET_CHAR
= (CEF_CHAR
| CEF_SET_ICE
),
123 * Remove any "Constant" [6.4.4] flag, but retain the "constant
124 * expression" [6.6] flags.
126 CEF_CONST_MASK
= (CEF_INT
| CEF_FLOAT
| CEF_CHAR
),
129 * not an integer constant expression => neither of integer,
130 * enumeration and character constant
132 CEF_CLR_ICE
= (CEF_ICE
| CEF_INT
| CEF_ENUM
| CEF_CHAR
),
139 }; /* for expr->smatch_flags */
143 }; /* for expr->taint */
147 struct expression
*constraint
;
148 struct expression
*expr
;
149 unsigned int is_assign
:1;
150 unsigned int is_modify
:1;
151 unsigned int is_earlyclobber
:1;
152 unsigned int is_commutative
:1;
153 unsigned int is_register
:1;
154 unsigned int is_memory
:1;
157 struct type_expression
{
159 struct expression
*expr
;
160 struct type_expression
*next
;
163 DECLARE_ALLOCATOR(type_expression
);
169 unsigned long long uvalue
;
177 enum expression_type type
:8;
179 unsigned smatch_flags
:16;
180 unsigned zero_init
:1;
183 struct symbol
*ctype
;
184 unsigned long parent
;
188 unsigned long long value
;
198 struct string
*string
;
201 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
203 struct expression
*unop
;
204 unsigned long op_value
;
207 // EXPR_SYMBOL, EXPR_TYPE
208 struct /* symbol_arg */ {
209 struct symbol
*symbol
;
210 struct ident
*symbol_name
;
214 struct statement
*statement
;
216 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
217 struct /* binop_arg */ {
218 struct expression
*left
, *right
;
222 struct /* deref_arg */ {
223 struct expression
*deref
;
224 struct ident
*member
;
229 struct expression
*base
;
230 unsigned r_bitpos
, r_nrbits
;
232 // EXPR_CAST, EXPR_FORCE_CAST, EXPR_IMPLIED_CAST,
233 // EXPR_SIZEOF, EXPR_ALIGNOF and EXPR_PTRSIZEOF
234 struct /* cast_arg */ {
235 struct symbol
*cast_type
;
236 struct expression
*cast_expression
;
240 struct /* conditional_expr */ {
241 struct expression
*conditional
, *cond_true
, *cond_false
;
244 struct /* call_expr */ {
245 struct expression
*fn
;
246 struct expression_list
*args
;
249 struct /* label_expr */ {
250 struct symbol
*label_symbol
;
253 struct expression_list
*expr_list
;
255 struct /* ident_expr */ {
257 struct ident
*expr_ident
;
258 struct symbol
*field
;
259 struct expression
*ident_expression
;
262 struct /* index_expr */ {
263 unsigned int idx_from
, idx_to
;
264 struct expression
*idx_expression
;
267 struct /* initpos_expr */ {
268 unsigned int init_offset
, init_nr
;
269 struct expression
*init_expr
;
274 struct expression
*down
;
277 struct expression
*index
;
282 struct expression
*control
;
283 struct expression
*def
;
284 struct type_expression
*map
;
290 // Constant expression values
291 // --------------------------
294 // test if an expression evaluates to the constant ``0``.
295 // @return: ``1`` if @expr evaluate to ``0``,
297 int is_zero_constant(struct expression
*expr
);
300 // test the compile time truth value of an expression
302 // * ``-1`` if @expr is not constant,
303 // * ``0`` or ``1`` depending on the truth value of @expr.
304 int expr_truth_value(struct expression
*expr
);
306 long long get_expression_value(struct expression
*);
307 long long const_expression_value(struct expression
*);
308 long long get_expression_value_silent(struct expression
*expr
);
310 /* Expression parsing */
311 struct token
*parse_expression(struct token
*token
, struct expression
**tree
);
312 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
);
313 struct token
*primary_expression(struct token
*token
, struct expression
**tree
);
314 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
);
315 struct token
*string_expression(struct token
*token
, struct expression
**expr
, const char *where
);
316 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
);
318 extern void evaluate_symbol_list(struct symbol_list
*list
);
319 extern struct symbol
*evaluate_statement(struct statement
*stmt
);
320 extern struct symbol
*evaluate_expression(struct expression
*);
321 struct symbol
*find_identifier(struct ident
*ident
, struct symbol_list
*_list
, int *offset
);
323 extern int expand_symbol(struct symbol
*);
325 static inline struct expression
*alloc_expression(struct position pos
, int type
)
327 struct expression
*expr
= __alloc_expression(0);
330 expr
->flags
= CEF_NONE
;
334 static inline struct expression
*alloc_const_expression(struct position pos
, int value
)
336 struct expression
*expr
= __alloc_expression(0);
337 expr
->type
= EXPR_VALUE
;
340 expr
->ctype
= &int_ctype
;
341 expr
->flags
= CEF_SET_INT
;
345 /* Type name parsing */
346 struct token
*typename(struct token
*, struct symbol
**, int *);
348 static inline int lookup_type(struct token
*token
)
350 if (token
->pos
.type
== TOKEN_IDENT
) {
351 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
352 return sym
&& (sym
->namespace & NS_TYPEDEF
);
357 /* Statement parsing */
358 struct statement
*alloc_statement(struct position pos
, int type
);
359 struct token
*initializer(struct expression
**tree
, struct token
*token
);
360 struct token
*compound_statement(struct token
*, struct statement
*);
362 /* The preprocessor calls this 'constant_expression()' */
363 #define constant_expression(token,tree) conditional_expression(token, tree)
365 /* Cast folding of constant values.. */
366 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
367 struct expression
*old
, struct symbol
*oldtype
);