6 * Copyright (C) 2003 Transmeta Corp.
9 * Licensed under the Open Software License version 1.1
11 * Declarations and helper functions for expression parsing.
16 struct expression_list
;
18 enum expression_type
{
35 EXPR_SELECT
, // a "safe" conditional expression
41 EXPR_INITIALIZER
, // initializer list
42 EXPR_IDENTIFIER
, // identifier in initializer
43 EXPR_INDEX
, // index in initializer
44 EXPR_POS
, // position in initializer
50 enum expression_type type
;
56 unsigned long long value
;
62 struct string
*string
;
64 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
66 struct expression
*unop
;
67 unsigned long op_value
;
70 // EXPR_SYMBOL, EXPR_TYPE
71 struct /* symbol_arg */ {
72 struct symbol
*symbol
;
73 struct ident
*symbol_name
;
77 struct statement
*statement
;
79 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
80 struct /* binop_arg */ {
81 struct expression
*left
, *right
;
84 struct /* deref_arg */ {
85 struct expression
*deref
;
90 struct expression
*base
;
91 unsigned r_bitpos
, r_nrbits
;
93 // EXPR_CAST and EXPR_SIZEOF
94 struct /* cast_arg */ {
95 struct symbol
*cast_type
;
96 struct expression
*cast_expression
;
100 struct /* conditional_expr */ {
101 struct expression
*conditional
, *cond_true
, *cond_false
;
104 struct /* call_expr */ {
105 struct expression
*fn
;
106 struct expression_list
*args
;
109 struct /* label_expr */ {
110 struct symbol
*label_symbol
;
113 struct expression_list
*expr_list
;
115 struct /* ident_expr */ {
116 struct ident
*expr_ident
;
117 struct symbol
*field
;
118 struct expression
*ident_expression
;
121 struct /* index_expr */ {
122 unsigned int idx_from
, idx_to
;
123 struct expression
*idx_expression
;
126 struct /* initpos_expr */ {
127 unsigned int init_offset
, init_nr
;
128 struct expression
*init_expr
;
133 /* Constant expression values */
134 long long get_expression_value(struct expression
*);
136 /* Expression parsing */
137 struct token
*parse_expression(struct token
*token
, struct expression
**tree
);
138 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
);
139 struct token
*primary_expression(struct token
*token
, struct expression
**tree
);
140 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
);
141 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
);
143 extern void evaluate_symbol_list(struct symbol_list
*list
);
144 extern struct symbol
*evaluate_statement(struct statement
*stmt
);
145 extern struct symbol
*evaluate_expression(struct expression
*);
147 extern int expand_symbol(struct symbol
*);
149 static inline struct expression
*alloc_expression(struct position pos
, int type
)
151 struct expression
*expr
= __alloc_expression(0);
157 static inline struct expression
*alloc_const_expression(struct position pos
, int value
)
159 struct expression
*expr
= __alloc_expression(0);
160 expr
->type
= EXPR_VALUE
;
163 expr
->ctype
= &int_ctype
;
167 /* Type name parsing */
168 struct token
*typename(struct token
*, struct symbol
**);
170 static inline int lookup_type(struct token
*token
)
172 if (token
->pos
.type
== TOKEN_IDENT
) {
173 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
174 return sym
&& sym
->namespace == NS_TYPEDEF
;
179 /* Statement parsing */
180 struct statement
*alloc_statement(struct position pos
, int type
);
181 struct token
*initializer(struct expression
**tree
, struct token
*token
);
182 struct token
*compound_statement(struct token
*, struct statement
*);
184 /* The preprocessor calls this 'constant_expression()' */
185 #define constant_expression(token,tree) conditional_expression(token, tree)
187 /* Cast folding of constant values.. */
188 void cast_value(struct expression
*expr
, struct symbol
*newtype
,
189 struct expression
*old
, struct symbol
*oldtype
);