4 * Copyright (C) 2003 Transmeta Corp.
6 * Licensed under the Open Software License version 1.1
8 * This is the expression parsing part of parsing C.
23 #include "expression.h"
26 static int match_oplist(int op
, ...)
32 int nextop
= va_arg(args
, int);
40 static struct token
*comma_expression(struct token
*, struct expression
**);
42 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
44 token
= expect(token
, '(', where
);
45 if (match_op(token
, '{')) {
46 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
47 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
51 token
= compound_statement(token
->next
, stmt
);
53 token
= expect(token
, '}', "at end of statement expression");
55 token
= parse_expression(token
, expr
);
56 return expect(token
, ')', where
);
59 static struct token
*string_expression(struct token
*token
, struct expression
*expr
)
61 struct string
*string
= token
->string
;
62 struct token
*next
= token
->next
;
64 if (token_type(next
) == TOKEN_STRING
) {
65 int totlen
= string
->length
;
69 totlen
+= next
->string
->length
-1;
71 } while (token_type(next
) == TOKEN_STRING
);
73 string
= __alloc_string(totlen
);
74 string
->length
= totlen
;
78 struct string
*s
= next
->string
;
82 memcpy(data
, s
->data
, len
);
84 } while (token_type(next
) == TOKEN_STRING
);
86 expr
->string
= string
;
90 static void get_int_value(struct expression
*expr
, const char *str
)
92 unsigned long long value
= 0;
93 unsigned int base
= 10, digit
, bits
;
94 unsigned long modifiers
, extramod
;
98 base
= 18; // the -= 2 for the octal case will
99 str
++; // skip the 'x'
102 str
++; // skip the 'o' or 'x/X'
103 base
-= 2; // the fall-through will make this 8
105 while ((digit
= hexval(*str
)) < base
) {
106 value
= value
* base
+ digit
;
112 if (c
== 'u' || c
== 'U') {
113 modifiers
|= MOD_UNSIGNED
;
116 if (c
== 'l' || c
== 'L') {
117 if (modifiers
& MOD_LONG
)
118 modifiers
|= MOD_LONGLONG
;
119 modifiers
|= MOD_LONG
;
125 bits
= BITS_IN_LONGLONG
;
127 if (!(modifiers
& MOD_LONGLONG
)) {
128 if (value
& (~1ULL << (BITS_IN_LONG
-1))) {
129 extramod
= MOD_LONGLONG
| MOD_LONG
;
132 if (!(modifiers
& MOD_LONG
)) {
133 if (value
& (~1ULL << (BITS_IN_INT
-1))) {
140 if (!(modifiers
& MOD_UNSIGNED
)) {
141 if (value
& (1ULL << (bits
-1))) {
142 extramod
|= MOD_UNSIGNED
;
147 * Special case: "int" gets promoted directly to "long"
148 * for normal decimal numbers..
150 modifiers
|= extramod
;
151 if (base
== 10 && modifiers
== MOD_UNSIGNED
) {
152 modifiers
= MOD_LONG
;
153 if (BITS_IN_LONG
== BITS_IN_INT
)
154 modifiers
= MOD_LONG
| MOD_UNSIGNED
;
157 /* Hex or octal constants don't complain about missing signedness */
158 if (base
== 10 || extramod
!= MOD_UNSIGNED
)
159 warn(expr
->pos
, "value is so big it is%s%s%s",
160 (modifiers
& MOD_UNSIGNED
) ? " unsigned":"",
161 (modifiers
& MOD_LONG
) ? " long":"",
162 (modifiers
& MOD_LONGLONG
) ? " long":"");
165 expr
->type
= EXPR_VALUE
;
166 expr
->ctype
= ctype_integer(modifiers
);
170 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
172 struct expression
*expr
= NULL
;
174 switch (token_type(token
)) {
176 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
177 expr
->ctype
= &double_ctype
;
179 warn(token
->pos
, "FP values not yet implemented");
184 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
185 expr
->ctype
= &int_ctype
;
186 expr
->value
= (unsigned char) token
->character
;
191 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
192 get_int_value(expr
, token
->integer
);
197 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
198 expr
->symbol_name
= token
->ident
;
199 expr
->symbol
= lookup_symbol(token
->ident
, NS_SYMBOL
);
205 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
206 token
= string_expression(token
, expr
);
211 if (token
->special
== '(') {
212 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
214 token
= parens_expression(token
, &expr
->unop
, "in expression");
224 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
226 while (!match_op(token
, ')')) {
227 struct expression
*expr
= NULL
;
228 token
= assignment_expression(token
, &expr
);
231 add_expression(list
, expr
);
232 if (!match_op(token
, ','))
239 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
)
241 struct expression
*expr
= NULL
;
243 token
= primary_expression(token
, &expr
);
244 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
245 switch (token
->special
) {
246 case '[': { /* Array dereference */
247 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
248 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
255 token
= parse_expression(token
->next
, &add
->right
);
256 token
= expect(token
, ']', "at end of array dereference");
260 case SPECIAL_INCREMENT
: /* Post-increment */
261 case SPECIAL_DECREMENT
: { /* Post-decrement */
262 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
263 post
->op
= token
->special
;
269 case '.': /* Structure member dereference */
270 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
271 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
272 deref
->op
= token
->special
;
275 if (token_type(token
) != TOKEN_IDENT
) {
276 warn(token
->pos
, "Expected member name");
279 deref
->member
= token
->ident
;
285 case '(': { /* Function call */
286 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
289 token
= expression_list(token
->next
, &call
->args
);
290 token
= expect(token
, ')', "in function call");
304 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
305 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
307 if (token_type(token
) == TOKEN_IDENT
&&
308 (token
->ident
== &sizeof_ident
||
309 token
->ident
== &__alignof___ident
)) {
310 struct expression
*sizeof_ex
= alloc_expression(token
->pos
, EXPR_SIZEOF
);
312 tree
= &sizeof_ex
->unop
;
314 if (!match_op(token
, '(') || !lookup_type(token
->next
))
315 return unary_expression(token
, &sizeof_ex
->cast_expression
);
316 token
= typename(token
->next
, &sizeof_ex
->cast_type
);
317 return expect(token
, ')', "at end of sizeof type-name");
320 if (token_type(token
) == TOKEN_SPECIAL
) {
321 if (match_oplist(token
->special
,
322 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
323 '&', '*', '+', '-', '~', '!', 0)) {
324 struct expression
*unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
325 unary
->op
= token
->special
;
327 return cast_expression(token
->next
, &unary
->unop
);
331 return postfix_expression(token
, tree
);
335 * Ambiguity: a '(' can be either a cast-expression or
336 * a primary-expression depending on whether it is followed
339 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
341 if (match_op(token
, '(')) {
342 struct token
*next
= token
->next
;
343 if (lookup_type(next
)) {
344 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
347 token
= typename(next
, &sym
);
348 cast
->cast_type
= sym
->ctype
.base_type
;
349 token
= expect(token
, ')', "at end of cast operator");
351 if (match_op(token
, '{'))
352 return initializer(&cast
->cast_expression
, token
);
353 token
= cast_expression(token
, &cast
->cast_expression
);
357 return unary_expression(token
, tree
);
360 /* Generic left-to-right binop parsing */
361 static struct token
*lr_binop_expression(struct token
*token
, struct expression
**tree
,
362 enum expression_type type
, struct token
*(*inner
)(struct token
*, struct expression
**), ...)
364 struct expression
*left
= NULL
;
365 struct token
* next
= inner(token
, &left
);
368 while (token_type(next
) == TOKEN_SPECIAL
) {
369 struct expression
*top
, *right
= NULL
;
370 int op
= next
->special
;
373 va_start(args
, inner
);
375 int nextop
= va_arg(args
, int);
382 top
= alloc_expression(next
->pos
, type
);
383 next
= inner(next
->next
, &right
);
385 warn(next
->pos
, "No right hand side of '%s'-expression", show_special(op
));
399 static struct token
*multiplicative_expression(struct token
*token
, struct expression
**tree
)
401 return lr_binop_expression(token
, tree
, EXPR_BINOP
, cast_expression
, '*', '/', '%', 0);
404 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
406 return lr_binop_expression(token
, tree
, EXPR_BINOP
, multiplicative_expression
, '+', '-', 0);
409 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
411 return lr_binop_expression(token
, tree
, EXPR_BINOP
, additive_expression
, SPECIAL_LEFTSHIFT
, SPECIAL_RIGHTSHIFT
, 0);
414 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
416 return lr_binop_expression(token
, tree
, EXPR_COMPARE
, shift_expression
, '<', '>', SPECIAL_LTE
, SPECIAL_GTE
, 0);
419 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
421 return lr_binop_expression(token
, tree
, EXPR_COMPARE
, relational_expression
, SPECIAL_EQUAL
, SPECIAL_NOTEQUAL
, 0);
424 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
426 return lr_binop_expression(token
, tree
, EXPR_BINOP
, equality_expression
, '&', 0);
429 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
431 return lr_binop_expression(token
, tree
, EXPR_BINOP
, bitwise_and_expression
, '^', 0);
434 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
436 return lr_binop_expression(token
, tree
, EXPR_BINOP
, bitwise_xor_expression
, '|', 0);
439 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
441 return lr_binop_expression(token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
, SPECIAL_LOGICAL_AND
, 0);
444 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
446 return lr_binop_expression(token
, tree
, EXPR_LOGICAL
, logical_and_expression
, SPECIAL_LOGICAL_OR
, 0);
449 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
451 token
= logical_or_expression(token
, tree
);
452 if (match_op(token
, '?')) {
453 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
454 expr
->op
= token
->special
;
457 token
= parse_expression(token
->next
, &expr
->cond_true
);
458 token
= expect(token
, ':', "in conditional expression");
459 token
= conditional_expression(token
, &expr
->cond_false
);
464 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
466 token
= conditional_expression(token
, tree
);
467 if (token_type(token
) == TOKEN_SPECIAL
) {
468 static const int assignments
[] = {
470 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
471 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
472 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
473 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
474 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
475 int i
, op
= token
->special
;
476 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
477 if (assignments
[i
] == op
) {
478 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
482 return assignment_expression(token
->next
, &expr
->right
);
488 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
490 return lr_binop_expression(token
, tree
, EXPR_COMMA
, assignment_expression
, ',', 0);
493 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
495 return comma_expression(token
,tree
);