4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
9 * This is the expression parsing part of parsing C.
26 #include "expression.h"
29 static int match_oplist(int op
, ...)
35 int nextop
= va_arg(args
, int);
43 static struct token
*comma_expression(struct token
*, struct expression
**);
45 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
47 token
= expect(token
, '(', where
);
48 if (match_op(token
, '{')) {
49 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
50 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
54 token
= compound_statement(token
->next
, stmt
);
56 token
= expect(token
, '}', "at end of statement expression");
58 token
= parse_expression(token
, expr
);
59 return expect(token
, ')', where
);
62 static struct token
*string_expression(struct token
*token
, struct expression
*expr
)
64 struct string
*string
= token
->string
;
65 struct token
*next
= token
->next
;
67 if (token_type(next
) == TOKEN_STRING
) {
68 int totlen
= string
->length
;
72 totlen
+= next
->string
->length
-1;
74 } while (token_type(next
) == TOKEN_STRING
);
76 string
= __alloc_string(totlen
);
77 string
->length
= totlen
;
81 struct string
*s
= next
->string
;
85 memcpy(data
, s
->data
, len
);
87 } while (token_type(next
) == TOKEN_STRING
);
89 expr
->string
= string
;
93 static void get_fp_value(struct expression
*expr
, struct token
*token
)
97 expr
->ctype
= &double_ctype
;
100 warn(token
->pos
, "FP values not yet implemented");
106 #define ULLONG_MAX (~0ULL)
109 static void get_number_value(struct expression
*expr
, struct token
*token
)
111 const char *str
= token
->number
;
112 unsigned long long value
;
114 unsigned long modifiers
= 0;
115 int overflow
= 0, do_warn
= 0;
116 int try_unsigned
= 1;
120 value
= strtoull(str
, &end
, 0);
123 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
130 } else if (c
== 'u' || c
== 'U') {
131 added
= MOD_UNSIGNED
;
132 } else if (c
== 'l' || c
== 'L') {
135 added
|= MOD_LONGLONG
;
140 if (modifiers
& added
)
146 /* OK, it's a valid integer */
147 /* decimals can be unsigned only if directly specified as such */
148 if (str
[0] != '0' && !(modifiers
& MOD_UNSIGNED
))
150 if (!(modifiers
& MOD_LONG
)) {
151 bits
= bits_in_int
- 1;
152 if (!(value
& (~1ULL << bits
))) {
153 if (!(value
& (1ULL << bits
))) {
155 } else if (try_unsigned
) {
156 modifiers
|= MOD_UNSIGNED
;
160 modifiers
|= MOD_LONG
;
163 if (!(modifiers
& MOD_LONGLONG
)) {
164 bits
= bits_in_long
- 1;
165 if (!(value
& (~1ULL << bits
))) {
166 if (!(value
& (1ULL << bits
))) {
168 } else if (try_unsigned
) {
169 modifiers
|= MOD_UNSIGNED
;
174 modifiers
|= MOD_LONGLONG
;
177 bits
= bits_in_longlong
- 1;
178 if (value
& (~1ULL << bits
))
180 if (!(value
& (1ULL << bits
)))
183 warn(expr
->pos
, "decimal constant %s is too big for long long",
185 modifiers
|= MOD_UNSIGNED
;
188 warn(expr
->pos
, "constant %s is so big it is%s%s%s",
190 (modifiers
& MOD_UNSIGNED
) ? " unsigned":"",
191 (modifiers
& MOD_LONG
) ? " long":"",
192 (modifiers
& MOD_LONGLONG
) ? " long":"");
195 "decimal constant %s is between LONG_MAX and ULONG_MAX."
196 " For C99 that means long long, C90 compilers are very "
197 "likely to produce unsigned long (and a warning) here",
199 expr
->type
= EXPR_VALUE
;
200 expr
->ctype
= ctype_integer(modifiers
);
204 error(expr
->pos
, "constant %s is too big even for unsigned long long",
207 get_fp_value(expr
, token
);
210 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
212 struct expression
*expr
= NULL
;
214 switch (token_type(token
)) {
216 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
217 expr
->ctype
= &int_ctype
;
218 expr
->value
= (unsigned char) token
->character
;
223 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
224 get_number_value(expr
, token
);
229 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
230 struct token
*next
= token
->next
;
232 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
235 * We support types as real first-class citizens, with type
238 * if (typeof(a) == int) ..
240 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
241 warn(token
->pos
, "typename in expression");
244 expr
->symbol_name
= token
->ident
;
251 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
252 token
= string_expression(token
, expr
);
257 if (token
->special
== '(') {
258 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
260 token
= parens_expression(token
, &expr
->unop
, "in expression");
263 if (token
->special
== '[' && lookup_type(token
->next
)) {
264 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
265 token
= typename(token
->next
, &expr
->symbol
);
266 token
= expect(token
, ']', "in type expression");
277 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
279 while (!match_op(token
, ')')) {
280 struct expression
*expr
= NULL
;
281 token
= assignment_expression(token
, &expr
);
284 add_expression(list
, expr
);
285 if (!match_op(token
, ','))
293 * extend to deal with the ambiguous C grammar for parsing
294 * a cast expressions followed by an initializer.
296 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
298 struct expression
*expr
= cast_init_expr
;
301 token
= primary_expression(token
, &expr
);
303 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
304 switch (token
->special
) {
305 case '[': { /* Array dereference */
306 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
307 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
314 token
= parse_expression(token
->next
, &add
->right
);
315 token
= expect(token
, ']', "at end of array dereference");
319 case SPECIAL_INCREMENT
: /* Post-increment */
320 case SPECIAL_DECREMENT
: { /* Post-decrement */
321 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
322 post
->op
= token
->special
;
328 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
329 /* "x->y" is just shorthand for "(*x).y" */
330 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
336 case '.': { /* Structure member dereference */
337 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
341 if (token_type(token
) != TOKEN_IDENT
) {
342 warn(token
->pos
, "Expected member name");
345 deref
->member
= token
->ident
;
351 case '(': { /* Function call */
352 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
355 token
= expression_list(token
->next
, &call
->args
);
356 token
= expect(token
, ')', "in function call");
370 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
371 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
373 if (token_type(token
) == TOKEN_IDENT
&&
374 (token
->ident
== &sizeof_ident
||
375 token
->ident
== &__alignof___ident
)) {
376 struct expression
*sizeof_ex
= alloc_expression(token
->pos
, EXPR_SIZEOF
);
378 tree
= &sizeof_ex
->unop
;
380 if (!match_op(token
, '(') || !lookup_type(token
->next
))
381 return unary_expression(token
, &sizeof_ex
->cast_expression
);
382 token
= typename(token
->next
, &sizeof_ex
->cast_type
);
383 return expect(token
, ')', "at end of sizeof type-name");
386 if (token_type(token
) == TOKEN_SPECIAL
) {
387 if (match_oplist(token
->special
,
388 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
389 '&', '*', '+', '-', '~', '!', 0)) {
390 struct expression
*unop
;
391 struct expression
*unary
;
394 next
= cast_expression(token
->next
, &unop
);
396 warn(token
->pos
, "Syntax error in unary expression");
399 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
400 unary
->op
= token
->special
;
406 /* Gcc extension: &&label gives the address of a label */
407 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
408 token_type(token
->next
) == TOKEN_IDENT
) {
409 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
410 label
->label_symbol
= label_symbol(token
->next
);
412 return token
->next
->next
;
417 return postfix_expression(token
, tree
, NULL
);
421 * Ambiguity: a '(' can be either a cast-expression or
422 * a primary-expression depending on whether it is followed
425 * additional ambiguity: a "cast expression" followed by
426 * an initializer is really a postfix-expression.
428 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
430 if (match_op(token
, '(')) {
431 struct token
*next
= token
->next
;
432 if (lookup_type(next
)) {
433 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
436 token
= typename(next
, &sym
);
437 cast
->cast_type
= sym
;
438 token
= expect(token
, ')', "at end of cast operator");
439 if (match_op(token
, '{')) {
440 token
= initializer(&cast
->cast_expression
, token
);
441 return postfix_expression(token
, tree
, cast
);
444 token
= cast_expression(token
, &cast
->cast_expression
);
448 return unary_expression(token
, tree
);
452 * Generic left-to-right binop parsing
454 * This _really_ needs to be inlined, because that makes the inner
455 * function call statically deterministic rather than a totally
456 * unpredictable indirect call. But gcc-3 is so "clever" that it
457 * doesn't do so by default even when you tell it to inline it.
459 * Making it a macro avoids the inlining problem, and also means
460 * that we can pass in the op-comparison as an expression rather
461 * than create a data structure for it.
464 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
465 struct expression *left = NULL; \
466 struct token * next = inner(token, &left); \
469 while (token_type(next) == TOKEN_SPECIAL) { \
470 struct expression *top, *right = NULL; \
471 int op = next->special; \
475 top = alloc_expression(next->pos, type); \
476 next = inner(next->next, &right); \
478 warn(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
483 top->right = right; \
492 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
495 token
, tree
, EXPR_BINOP
, cast_expression
,
496 (op
== '*') || (op
== '/') || (op
== '%')
500 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
503 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
504 (op
== '+') || (op
== '-')
508 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
511 token
, tree
, EXPR_BINOP
, additive_expression
,
512 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
516 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
519 token
, tree
, EXPR_COMPARE
, shift_expression
,
520 (op
== '<') || (op
== '>') ||
521 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
525 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
528 token
, tree
, EXPR_COMPARE
, relational_expression
,
529 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
533 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
536 token
, tree
, EXPR_BINOP
, equality_expression
,
541 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
544 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
549 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
552 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
557 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
560 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
561 (op
== SPECIAL_LOGICAL_AND
)
565 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
568 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
569 (op
== SPECIAL_LOGICAL_OR
)
573 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
575 token
= logical_or_expression(token
, tree
);
576 if (match_op(token
, '?')) {
577 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
578 expr
->op
= token
->special
;
581 token
= parse_expression(token
->next
, &expr
->cond_true
);
582 token
= expect(token
, ':', "in conditional expression");
583 token
= conditional_expression(token
, &expr
->cond_false
);
588 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
590 token
= conditional_expression(token
, tree
);
591 if (token_type(token
) == TOKEN_SPECIAL
) {
592 static const int assignments
[] = {
594 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
595 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
596 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
597 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
598 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
599 int i
, op
= token
->special
;
600 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
601 if (assignments
[i
] == op
) {
602 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
606 return assignment_expression(token
->next
, &expr
->right
);
612 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
615 token
, tree
, EXPR_COMMA
, assignment_expression
,
620 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
622 return comma_expression(token
,tree
);