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 }; /* for expr->flags */
76 }; /* for expr->flags */
80 }; /* for expr->taint */
83 struct expression
*parent
;
84 enum expression_type type
:8;
86 unsigned smatch_flags
:16;
93 unsigned long long value
;
103 struct string
*string
;
106 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
108 struct expression
*unop
;
109 unsigned long op_value
;
112 // EXPR_SYMBOL, EXPR_TYPE
113 struct /* symbol_arg */ {
114 struct symbol
*symbol
;
115 struct ident
*symbol_name
;
119 struct statement
*statement
;
121 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
122 struct /* binop_arg */ {
123 struct expression
*left
, *right
;
126 struct /* deref_arg */ {
127 struct expression
*deref
;
128 struct ident
*member
;
133 struct expression
*base
;
134 unsigned r_bitpos
, r_nrbits
;
136 // EXPR_CAST and EXPR_SIZEOF
137 struct /* cast_arg */ {
138 struct symbol
*cast_type
;
139 struct expression
*cast_expression
;
143 struct /* conditional_expr */ {
144 struct expression
*conditional
, *cond_true
, *cond_false
;
147 struct /* call_expr */ {
148 struct expression
*fn
;
149 struct expression_list
*args
;
152 struct /* label_expr */ {
153 struct symbol
*label_symbol
;
156 struct expression_list
*expr_list
;
158 struct /* ident_expr */ {
160 struct ident
*expr_ident
;
161 struct symbol
*field
;
162 struct expression
*ident_expression
;
165 struct /* index_expr */ {
166 unsigned int idx_from
, idx_to
;
167 struct expression
*idx_expression
;
170 struct /* initpos_expr */ {
171 unsigned int init_offset
, init_nr
;
172 struct expression
*init_expr
;
177 struct expression
*down
;
180 struct expression
*index
;
186 /* Constant expression values */
187 int is_zero_constant(struct expression
*);
188 long long get_expression_value(struct expression
*);
189 long long const_expression_value(struct expression
*);
190 long long get_expression_value_silent(struct expression
*expr
);
192 /* Expression parsing */
193 struct token
*parse_expression(struct token
*token
, struct expression
**tree
);
194 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
);
195 struct token
*primary_expression(struct token
*token
, struct expression
**tree
);
196 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
);
197 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
);
199 extern void evaluate_symbol_list(struct symbol_list
*list
);
200 extern struct symbol
*evaluate_statement(struct statement
*stmt
);
201 extern struct symbol
*evaluate_expression(struct expression
*);
203 extern int expand_symbol(struct symbol
*);
205 static inline struct expression
*alloc_expression(struct position pos
, int type
)
207 struct expression
*expr
= __alloc_expression(0);
213 static inline struct expression
*alloc_const_expression(struct position pos
, int value
)
215 struct expression
*expr
= __alloc_expression(0);
216 expr
->type
= EXPR_VALUE
;
219 expr
->ctype
= &int_ctype
;
223 /* Type name parsing */
224 struct token
*typename(struct token
*, struct symbol
**, int *);
226 static inline int lookup_type(struct token
*token
)
228 if (token
->pos
.type
== TOKEN_IDENT
) {
229 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
230 return sym
&& (sym
->namespace & NS_TYPEDEF
);
235 /* Statement parsing */
236 struct statement
*alloc_statement(struct position pos
, int type
);
237 struct token
*initializer(struct expression
**tree
, struct token
*token
);
238 struct token
*compound_statement(struct token
*, struct statement
*);
240 /* The preprocessor calls this 'constant_expression()' */
241 #define constant_expression(token,tree) conditional_expression(token, tree)
243 /* Cast folding of constant values.. */
244 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
245 struct expression
*old
, struct symbol
*oldtype
);