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
71 * Flags for tracking the promotion of constness related attributes
72 * from subexpressions to their parents.
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.
79 * Use the CEF_*_SET_MASK and CEF_*_CLEAR_MASK
80 * helper macros defined below to set or clear one of these flags.
85 * A constant in the sense of [6.4.4]:
86 * - Integer constant [6.4.4.1]
87 * - Floating point constant [6.4.4.2]
88 * - Enumeration constant [6.4.4.3]
89 * - Character constant [6.4.4.4]
97 * A constant expression in the sense of [6.6]:
98 * - integer constant expression [6.6(6)]
99 * - arithmetic constant expression [6.6(8)]
100 * - address constant [6.6(9)]
106 /* integer constant expression => arithmetic constant expression */
107 CEF_SET_ICE
= (CEF_ICE
| CEF_ACE
),
109 /* integer constant => integer constant expression */
110 CEF_SET_INT
= (CEF_INT
| CEF_SET_ICE
),
112 /* floating point constant => arithmetic constant expression */
113 CEF_SET_FLOAT
= (CEF_FLOAT
| CEF_ACE
),
115 /* enumeration constant => integer constant expression */
116 CEF_SET_ENUM
= (CEF_ENUM
| CEF_SET_ICE
),
118 /* character constant => integer constant expression */
119 CEF_SET_CHAR
= (CEF_CHAR
| CEF_SET_ICE
),
122 * Remove any "Constant" [6.4.4] flag, but retain the "constant
123 * expression" [6.6] flags.
125 CEF_CONST_MASK
= (CEF_INT
| CEF_FLOAT
| CEF_CHAR
),
128 * not an integer constant expression => neither of integer,
129 * enumeration and character constant
131 CEF_CLR_ICE
= (CEF_ICE
| CEF_INT
| CEF_ENUM
| CEF_CHAR
),
136 }; /* for expr->taint */
139 enum expression_type type
:8;
143 struct symbol
*ctype
;
147 unsigned long long value
;
157 struct string
*string
;
160 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
162 struct expression
*unop
;
163 unsigned long op_value
;
166 // EXPR_SYMBOL, EXPR_TYPE
167 struct /* symbol_arg */ {
168 struct symbol
*symbol
;
169 struct ident
*symbol_name
;
173 struct statement
*statement
;
175 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
176 struct /* binop_arg */ {
177 struct expression
*left
, *right
;
180 struct /* deref_arg */ {
181 struct expression
*deref
;
182 struct ident
*member
;
186 struct expression
*base
;
187 unsigned r_bitpos
, r_nrbits
;
189 // EXPR_CAST and EXPR_SIZEOF
190 struct /* cast_arg */ {
191 struct symbol
*cast_type
;
192 struct expression
*cast_expression
;
196 struct /* conditional_expr */ {
197 struct expression
*conditional
, *cond_true
, *cond_false
;
200 struct /* call_expr */ {
201 struct expression
*fn
;
202 struct expression_list
*args
;
205 struct /* label_expr */ {
206 struct symbol
*label_symbol
;
209 struct expression_list
*expr_list
;
211 struct /* ident_expr */ {
213 struct ident
*expr_ident
;
214 struct symbol
*field
;
215 struct expression
*ident_expression
;
218 struct /* index_expr */ {
219 unsigned int idx_from
, idx_to
;
220 struct expression
*idx_expression
;
223 struct /* initpos_expr */ {
224 unsigned int init_offset
, init_nr
;
225 struct expression
*init_expr
;
230 struct expression
*down
;
233 struct expression
*index
;
239 /* Constant expression values */
240 int is_zero_constant(struct expression
*);
241 int expr_truth_value(struct expression
*expr
);
242 long long get_expression_value(struct expression
*);
243 long long const_expression_value(struct expression
*);
244 long long get_expression_value_silent(struct expression
*expr
);
246 /* Expression parsing */
247 struct token
*parse_expression(struct token
*token
, struct expression
**tree
);
248 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
);
249 struct token
*primary_expression(struct token
*token
, struct expression
**tree
);
250 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
);
251 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
);
253 extern void evaluate_symbol_list(struct symbol_list
*list
);
254 extern struct symbol
*evaluate_statement(struct statement
*stmt
);
255 extern struct symbol
*evaluate_expression(struct expression
*);
257 extern int expand_symbol(struct symbol
*);
259 static inline struct expression
*alloc_expression(struct position pos
, int type
)
261 struct expression
*expr
= __alloc_expression(0);
264 expr
->flags
= CEF_NONE
;
268 static inline struct expression
*alloc_const_expression(struct position pos
, int value
)
270 struct expression
*expr
= __alloc_expression(0);
271 expr
->type
= EXPR_VALUE
;
274 expr
->ctype
= &int_ctype
;
275 expr
->flags
= CEF_SET_INT
;
279 /* Type name parsing */
280 struct token
*typename(struct token
*, struct symbol
**, int *);
282 static inline int lookup_type(struct token
*token
)
284 if (token
->pos
.type
== TOKEN_IDENT
) {
285 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
286 return sym
&& (sym
->namespace & NS_TYPEDEF
);
291 /* Statement parsing */
292 struct statement
*alloc_statement(struct position pos
, int type
);
293 struct token
*initializer(struct expression
**tree
, struct token
*token
);
294 struct token
*compound_statement(struct token
*, struct statement
*);
296 /* The preprocessor calls this 'constant_expression()' */
297 #define constant_expression(token,tree) conditional_expression(token, tree)
299 /* Cast folding of constant values.. */
300 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
301 struct expression
*old
, struct symbol
*oldtype
);