4 * Copyright (C) 2003 Transmeta Corp, all rights reserved.
6 * This is the expression parsing part of parsing C.
21 #include "expression.h"
24 static int match_oplist(int op
, ...)
30 int nextop
= va_arg(args
, int);
38 static struct token
*comma_expression(struct token
*, struct expression
**);
40 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
42 token
= expect(token
, '(', where
);
43 if (match_op(token
, '{')) {
44 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
45 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
49 token
= compound_statement(token
->next
, stmt
);
51 token
= expect(token
, '}', "at end of statement expression");
53 token
= parse_expression(token
, expr
);
54 return expect(token
, ')', where
);
57 static struct token
*string_expression(struct token
*token
, struct expression
*expr
)
59 struct string
*string
= token
->string
;
60 struct token
*next
= token
->next
;
62 if (token_type(next
) == TOKEN_STRING
) {
63 int totlen
= string
->length
;
67 totlen
+= next
->string
->length
-1;
69 } while (token_type(next
) == TOKEN_STRING
);
71 string
= __alloc_string(totlen
);
72 string
->length
= totlen
;
76 struct string
*s
= next
->string
;
80 memcpy(data
, s
->data
, len
);
82 } while (token_type(next
) == TOKEN_STRING
);
84 expr
->string
= string
;
88 static void get_int_value(struct expression
*expr
, const char *str
)
90 unsigned long long value
= 0;
91 unsigned int base
= 10, digit
, bits
;
92 unsigned long modifiers
, extramod
;
96 base
= 18; // the -= 2 for the octal case will
97 str
++; // skip the 'x'
100 str
++; // skip the 'o' or 'x/X'
101 base
-= 2; // the fall-through will make this 8
103 while ((digit
= hexval(*str
)) < base
) {
104 value
= value
* base
+ digit
;
110 if (c
== 'u' || c
== 'U') {
111 modifiers
|= MOD_UNSIGNED
;
114 if (c
== 'l' || c
== 'L') {
115 if (modifiers
& MOD_LONG
)
116 modifiers
|= MOD_LONGLONG
;
117 modifiers
|= MOD_LONG
;
123 bits
= BITS_IN_LONGLONG
;
125 if (!(modifiers
& MOD_LONGLONG
)) {
126 if (value
& (~0ULL << BITS_IN_LONG
)) {
127 extramod
= MOD_LONGLONG
| MOD_LONG
;
130 if (!(modifiers
& MOD_LONG
)) {
131 if (value
& (~0ULL << BITS_IN_INT
)) {
138 if (!(modifiers
& MOD_UNSIGNED
)) {
139 if (value
& (1ULL << (bits
-1))) {
140 extramod
|= MOD_UNSIGNED
;
145 * Special case: "int" gets promoted directly to "long"
146 * for normal decimal numbers..
148 modifiers
|= extramod
;
149 if (base
== 10 && modifiers
== MOD_UNSIGNED
) {
150 modifiers
= MOD_LONG
;
151 if (BITS_IN_LONG
== BITS_IN_INT
)
152 modifiers
= MOD_LONG
| MOD_UNSIGNED
;
154 warn(expr
->pos
, "value is so big it is%s%s%s",
155 (modifiers
& MOD_UNSIGNED
) ? " unsigned":"",
156 (modifiers
& MOD_LONG
) ? " long":"",
157 (modifiers
& MOD_LONGLONG
) ? " long":"");
160 expr
->type
= EXPR_VALUE
;
161 expr
->ctype
= ctype_integer(modifiers
);
165 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
167 struct expression
*expr
= NULL
;
169 switch (token_type(token
)) {
171 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
172 expr
->ctype
= &double_ctype
;
174 warn(token
->pos
, "FP values not yet implemented");
179 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
180 expr
->ctype
= &int_ctype
;
181 expr
->value
= (unsigned char) token
->character
;
186 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
187 get_int_value(expr
, token
->integer
);
192 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
193 expr
->symbol_name
= token
->ident
;
194 expr
->symbol
= lookup_symbol(token
->ident
, NS_SYMBOL
);
200 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
201 token
= string_expression(token
, expr
);
206 if (token
->special
== '(') {
207 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
209 token
= parens_expression(token
, &expr
->unop
, "in expression");
219 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
221 while (!match_op(token
, ')')) {
222 struct expression
*expr
= NULL
;
223 token
= assignment_expression(token
, &expr
);
226 add_expression(list
, expr
);
227 if (!match_op(token
, ','))
234 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
)
236 struct expression
*expr
= NULL
;
238 token
= primary_expression(token
, &expr
);
239 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
240 switch (token
->special
) {
241 case '[': { /* Array dereference */
242 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
243 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
250 token
= parse_expression(token
->next
, &add
->right
);
251 token
= expect(token
, ']', "at end of array dereference");
255 case SPECIAL_INCREMENT
: /* Post-increment */
256 case SPECIAL_DECREMENT
: { /* Post-decrement */
257 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
258 post
->op
= token
->special
;
264 case '.': /* Structure member dereference */
265 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
266 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
267 deref
->op
= token
->special
;
270 if (token_type(token
) != TOKEN_IDENT
) {
271 warn(token
->pos
, "Expected member name");
274 deref
->member
= token
->ident
;
280 case '(': { /* Function call */
281 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
284 token
= expression_list(token
->next
, &call
->args
);
285 token
= expect(token
, ')', "in function call");
299 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
300 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
302 if (token_type(token
) == TOKEN_IDENT
&&
303 (token
->ident
== &sizeof_ident
||
304 token
->ident
== &__alignof___ident
)) {
305 struct expression
*sizeof_ex
= alloc_expression(token
->pos
, EXPR_SIZEOF
);
307 tree
= &sizeof_ex
->unop
;
309 if (!match_op(token
, '(') || !lookup_type(token
->next
))
310 return unary_expression(token
, &sizeof_ex
->cast_expression
);
311 token
= typename(token
->next
, &sizeof_ex
->cast_type
);
312 return expect(token
, ')', "at end of sizeof type-name");
315 if (token_type(token
) == TOKEN_SPECIAL
) {
316 if (match_oplist(token
->special
,
317 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
318 '&', '*', '+', '-', '~', '!', 0)) {
319 struct expression
*unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
320 unary
->op
= token
->special
;
322 return cast_expression(token
->next
, &unary
->unop
);
326 return postfix_expression(token
, tree
);
330 * Ambiguity: a '(' can be either a cast-expression or
331 * a primary-expression depending on whether it is followed
334 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
336 if (match_op(token
, '(')) {
337 struct token
*next
= token
->next
;
338 if (lookup_type(next
)) {
339 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
342 token
= typename(next
, &sym
);
343 cast
->cast_type
= sym
->ctype
.base_type
;
344 token
= expect(token
, ')', "at end of cast operator");
346 if (match_op(token
, '{'))
347 return initializer(&cast
->cast_expression
, token
);
348 token
= cast_expression(token
, &cast
->cast_expression
);
352 return unary_expression(token
, tree
);
355 /* Generic left-to-right binop parsing */
356 static struct token
*lr_binop_expression(struct token
*token
, struct expression
**tree
,
357 enum expression_type type
, struct token
*(*inner
)(struct token
*, struct expression
**), ...)
359 struct expression
*left
= NULL
;
360 struct token
* next
= inner(token
, &left
);
363 while (token_type(next
) == TOKEN_SPECIAL
) {
364 struct expression
*top
, *right
= NULL
;
365 int op
= next
->special
;
368 va_start(args
, inner
);
370 int nextop
= va_arg(args
, int);
377 top
= alloc_expression(next
->pos
, type
);
378 next
= inner(next
->next
, &right
);
380 warn(next
->pos
, "No right hand side of '%s'-expression", show_special(op
));
394 static struct token
*multiplicative_expression(struct token
*token
, struct expression
**tree
)
396 return lr_binop_expression(token
, tree
, EXPR_BINOP
, cast_expression
, '*', '/', '%', 0);
399 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
401 return lr_binop_expression(token
, tree
, EXPR_BINOP
, multiplicative_expression
, '+', '-', 0);
404 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
406 return lr_binop_expression(token
, tree
, EXPR_BINOP
, additive_expression
, SPECIAL_LEFTSHIFT
, SPECIAL_RIGHTSHIFT
, 0);
409 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
411 return lr_binop_expression(token
, tree
, EXPR_COMPARE
, shift_expression
, '<', '>', SPECIAL_LTE
, SPECIAL_GTE
, 0);
414 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
416 return lr_binop_expression(token
, tree
, EXPR_COMPARE
, relational_expression
, SPECIAL_EQUAL
, SPECIAL_NOTEQUAL
, 0);
419 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
421 return lr_binop_expression(token
, tree
, EXPR_BINOP
, equality_expression
, '&', 0);
424 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
426 return lr_binop_expression(token
, tree
, EXPR_BINOP
, bitwise_and_expression
, '^', 0);
429 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
431 return lr_binop_expression(token
, tree
, EXPR_BINOP
, bitwise_xor_expression
, '|', 0);
434 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
436 return lr_binop_expression(token
, tree
, EXPR_BINOP
, bitwise_or_expression
, SPECIAL_LOGICAL_AND
, 0);
439 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
441 return lr_binop_expression(token
, tree
, EXPR_BINOP
, logical_and_expression
, SPECIAL_LOGICAL_OR
, 0);
444 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
446 token
= logical_or_expression(token
, tree
);
447 if (match_op(token
, '?')) {
448 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
449 expr
->op
= token
->special
;
452 token
= parse_expression(token
->next
, &expr
->cond_true
);
453 token
= expect(token
, ':', "in conditional expression");
454 token
= conditional_expression(token
, &expr
->cond_false
);
459 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
461 token
= conditional_expression(token
, tree
);
462 if (token_type(token
) == TOKEN_SPECIAL
) {
463 static const int assignments
[] = {
465 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
466 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
467 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
468 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
469 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
470 int i
, op
= token
->special
;
471 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
472 if (assignments
[i
] == op
) {
473 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
477 return assignment_expression(token
->next
, &expr
->right
);
483 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
485 return lr_binop_expression(token
, tree
, EXPR_COMMA
, assignment_expression
, ',', 0);
488 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
490 return comma_expression(token
,tree
);