4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
9 * This is the expression parsing part of parsing C.
28 #include "expression.h"
31 static int match_oplist(int op
, ...)
37 int nextop
= va_arg(args
, int);
45 static struct token
*comma_expression(struct token
*, struct expression
**);
47 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
49 token
= expect(token
, '(', where
);
50 if (match_op(token
, '{')) {
51 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
52 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
56 token
= compound_statement(token
->next
, stmt
);
58 token
= expect(token
, '}', "at end of statement expression");
60 token
= parse_expression(token
, expr
);
61 return expect(token
, ')', where
);
65 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
68 static int convert_one_fn_token(struct token
*token
)
70 struct symbol
*sym
= current_fn
;
73 struct ident
*ident
= sym
->ident
;
76 struct string
*string
;
78 string
= __alloc_string(len
+1);
79 memcpy(string
->data
, ident
->name
, len
);
80 string
->data
[len
] = 0;
81 string
->length
= len
+1;
82 token_type(token
) = TOKEN_STRING
;
83 token
->string
= string
;
90 static int convert_function(struct token
*next
)
94 struct token
*token
= next
;
96 switch (token_type(token
)) {
100 if (token
->ident
== &__func___ident
||
101 token
->ident
== &__FUNCTION___ident
||
102 token
->ident
== &__PRETTY_FUNCTION___ident
) {
103 if (!convert_one_fn_token(token
))
117 static struct token
*string_expression(struct token
*token
, struct expression
*expr
)
119 struct string
*string
= token
->string
;
120 struct token
*next
= token
->next
;
122 convert_function(token
);
124 if (token_type(next
) == TOKEN_STRING
) {
125 int totlen
= string
->length
-1;
129 totlen
+= next
->string
->length
-1;
131 } while (token_type(next
) == TOKEN_STRING
);
133 if (totlen
> MAX_STRING
) {
134 warning(token
->pos
, "trying to concatenate %d-character string (%d bytes max)", totlen
, MAX_STRING
);
138 string
= __alloc_string(totlen
+1);
139 string
->length
= totlen
+1;
143 struct string
*s
= next
->string
;
144 int len
= s
->length
-1;
151 memcpy(data
, s
->data
, len
);
153 } while (token_type(next
) == TOKEN_STRING
);
156 expr
->string
= string
;
161 #define ULLONG_MAX (~0ULL)
164 static void get_number_value(struct expression
*expr
, struct token
*token
)
166 const char *str
= token
->number
;
167 unsigned long long value
;
169 unsigned long modifiers
= 0;
170 int overflow
= 0, do_warn
= 0;
171 int try_unsigned
= 1;
175 value
= strtoull(str
, &end
, 0);
178 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
185 } else if (c
== 'u' || c
== 'U') {
186 added
= MOD_UNSIGNED
;
187 } else if (c
== 'l' || c
== 'L') {
190 added
|= MOD_LONGLONG
;
195 if (modifiers
& added
)
201 /* OK, it's a valid integer */
202 /* decimals can be unsigned only if directly specified as such */
203 if (str
[0] != '0' && !(modifiers
& MOD_UNSIGNED
))
205 if (!(modifiers
& MOD_LONG
)) {
206 bits
= bits_in_int
- 1;
207 if (!(value
& (~1ULL << bits
))) {
208 if (!(value
& (1ULL << bits
))) {
210 } else if (try_unsigned
) {
211 modifiers
|= MOD_UNSIGNED
;
215 modifiers
|= MOD_LONG
;
218 if (!(modifiers
& MOD_LONGLONG
)) {
219 bits
= bits_in_long
- 1;
220 if (!(value
& (~1ULL << bits
))) {
221 if (!(value
& (1ULL << bits
))) {
223 } else if (try_unsigned
) {
224 modifiers
|= MOD_UNSIGNED
;
229 modifiers
|= MOD_LONGLONG
;
232 bits
= bits_in_longlong
- 1;
233 if (value
& (~1ULL << bits
))
235 if (!(value
& (1ULL << bits
)))
238 warning(expr
->pos
, "decimal constant %s is too big for long long",
240 modifiers
|= MOD_UNSIGNED
;
243 warning(expr
->pos
, "constant %s is so big it is%s%s%s",
245 (modifiers
& MOD_UNSIGNED
) ? " unsigned":"",
246 (modifiers
& MOD_LONG
) ? " long":"",
247 (modifiers
& MOD_LONGLONG
) ? " long":"");
250 "decimal constant %s is between LONG_MAX and ULONG_MAX."
251 " For C99 that means long long, C90 compilers are very "
252 "likely to produce unsigned long (and a warning) here",
254 expr
->type
= EXPR_VALUE
;
255 expr
->ctype
= ctype_integer(modifiers
);
259 error_die(expr
->pos
, "constant %s is too big even for unsigned long long",
263 expr
->fvalue
= string_to_ld(str
, &end
);
270 if (*end
== 'f' || *end
== 'F')
271 expr
->ctype
= &float_ctype
;
272 else if (*end
== 'l' || *end
== 'L')
273 expr
->ctype
= &ldouble_ctype
;
275 expr
->ctype
= &double_ctype
;
279 expr
->type
= EXPR_FVALUE
;
283 error_die(expr
->pos
, "constant %s is not a valid number", show_token(token
));
286 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
288 struct expression
*expr
= NULL
;
290 switch (token_type(token
)) {
292 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
293 expr
->ctype
= &int_ctype
;
294 expr
->value
= (unsigned char) token
->character
;
299 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
300 get_number_value(expr
, token
);
305 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
306 struct token
*next
= token
->next
;
308 if (!sym
&& convert_function(token
))
311 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
314 * We support types as real first-class citizens, with type
317 * if (typeof(a) == int) ..
319 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
320 warning(token
->pos
, "typename in expression");
323 expr
->symbol_name
= token
->ident
;
331 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
332 token
= string_expression(token
, expr
);
337 if (token
->special
== '(') {
338 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
340 token
= parens_expression(token
, &expr
->unop
, "in expression");
343 if (token
->special
== '[' && lookup_type(token
->next
)) {
344 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
345 token
= typename(token
->next
, &expr
->symbol
);
346 token
= expect(token
, ']', "in type expression");
357 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
359 while (!match_op(token
, ')')) {
360 struct expression
*expr
= NULL
;
361 token
= assignment_expression(token
, &expr
);
364 add_expression(list
, expr
);
365 if (!match_op(token
, ','))
373 * extend to deal with the ambiguous C grammar for parsing
374 * a cast expressions followed by an initializer.
376 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
378 struct expression
*expr
= cast_init_expr
;
381 token
= primary_expression(token
, &expr
);
383 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
384 switch (token
->special
) {
385 case '[': { /* Array dereference */
386 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
387 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
394 token
= parse_expression(token
->next
, &add
->right
);
395 token
= expect(token
, ']', "at end of array dereference");
399 case SPECIAL_INCREMENT
: /* Post-increment */
400 case SPECIAL_DECREMENT
: { /* Post-decrement */
401 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
402 post
->op
= token
->special
;
408 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
409 /* "x->y" is just shorthand for "(*x).y" */
410 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
416 case '.': { /* Structure member dereference */
417 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
421 if (token_type(token
) != TOKEN_IDENT
) {
422 warning(token
->pos
, "Expected member name");
425 deref
->member
= token
->ident
;
431 case '(': { /* Function call */
432 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
435 token
= expression_list(token
->next
, &call
->args
);
436 token
= expect(token
, ')', "in function call");
450 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
451 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
);
453 static struct token
*type_info_expression(struct token
*token
,
454 struct expression
**tree
, int type
)
456 struct expression
*expr
= alloc_expression(token
->pos
, type
);
460 if (!match_op(token
, '(') || !lookup_type(token
->next
))
461 return unary_expression(token
, &expr
->cast_expression
);
462 token
= typename(token
->next
, &expr
->cast_type
);
464 if (!match_op(token
, ')')) {
465 static const char * error
[] = {
466 [EXPR_SIZEOF
] = "at end of sizeof",
467 [EXPR_ALIGNOF
] = "at end of __alignof__",
468 [EXPR_PTRSIZEOF
] = "at end of __sizeof_ptr__"
470 return expect(token
, ')', error
[type
]);
475 * C99 ambiguity: the typename might have been the beginning
476 * of a typed initializer expression..
478 if (match_op(token
, '{'))
479 token
= initializer(&expr
->cast_expression
, token
);
483 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
485 if (token_type(token
) == TOKEN_IDENT
) {
486 struct ident
*ident
= token
->ident
;
487 if (ident
->reserved
) {
488 static const struct {
491 } type_information
[] = {
492 { &sizeof_ident
, EXPR_SIZEOF
},
493 { &__alignof___ident
, EXPR_ALIGNOF
},
494 { &__sizeof_ptr___ident
, EXPR_PTRSIZEOF
},
497 for (i
= 0; i
< 3; i
++) {
498 if (ident
== type_information
[i
].id
)
499 return type_info_expression(token
, tree
, type_information
[i
].type
);
504 if (token_type(token
) == TOKEN_SPECIAL
) {
505 if (match_oplist(token
->special
,
506 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
507 '&', '*', '+', '-', '~', '!', 0)) {
508 struct expression
*unop
;
509 struct expression
*unary
;
512 next
= cast_expression(token
->next
, &unop
);
514 warning(token
->pos
, "Syntax error in unary expression");
517 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
518 unary
->op
= token
->special
;
524 /* Gcc extension: &&label gives the address of a label */
525 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
526 token_type(token
->next
) == TOKEN_IDENT
) {
527 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
528 struct symbol
*sym
= label_symbol(token
->next
);
529 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
)) {
530 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
531 add_symbol(&function_computed_target_list
, sym
);
533 label
->label_symbol
= sym
;
535 return token
->next
->next
;
540 return postfix_expression(token
, tree
, NULL
);
544 * Ambiguity: a '(' can be either a cast-expression or
545 * a primary-expression depending on whether it is followed
548 * additional ambiguity: a "cast expression" followed by
549 * an initializer is really a postfix-expression.
551 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
553 if (match_op(token
, '(')) {
554 struct token
*next
= token
->next
;
555 if (lookup_type(next
)) {
556 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
559 token
= typename(next
, &sym
);
560 cast
->cast_type
= sym
;
561 token
= expect(token
, ')', "at end of cast operator");
562 if (match_op(token
, '{')) {
563 token
= initializer(&cast
->cast_expression
, token
);
564 return postfix_expression(token
, tree
, cast
);
567 token
= cast_expression(token
, &cast
->cast_expression
);
571 return unary_expression(token
, tree
);
575 * Generic left-to-right binop parsing
577 * This _really_ needs to be inlined, because that makes the inner
578 * function call statically deterministic rather than a totally
579 * unpredictable indirect call. But gcc-3 is so "clever" that it
580 * doesn't do so by default even when you tell it to inline it.
582 * Making it a macro avoids the inlining problem, and also means
583 * that we can pass in the op-comparison as an expression rather
584 * than create a data structure for it.
587 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
588 struct expression *left = NULL; \
589 struct token * next = inner(token, &left); \
592 while (token_type(next) == TOKEN_SPECIAL) { \
593 struct expression *top, *right = NULL; \
594 int op = next->special; \
598 top = alloc_expression(next->pos, type); \
599 next = inner(next->next, &right); \
601 warning(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
606 top->right = right; \
615 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
618 token
, tree
, EXPR_BINOP
, cast_expression
,
619 (op
== '*') || (op
== '/') || (op
== '%')
623 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
626 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
627 (op
== '+') || (op
== '-')
631 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
634 token
, tree
, EXPR_BINOP
, additive_expression
,
635 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
639 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
642 token
, tree
, EXPR_COMPARE
, shift_expression
,
643 (op
== '<') || (op
== '>') ||
644 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
648 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
651 token
, tree
, EXPR_COMPARE
, relational_expression
,
652 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
656 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
659 token
, tree
, EXPR_BINOP
, equality_expression
,
664 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
667 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
672 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
675 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
680 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
683 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
684 (op
== SPECIAL_LOGICAL_AND
)
688 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
691 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
692 (op
== SPECIAL_LOGICAL_OR
)
696 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
698 token
= logical_or_expression(token
, tree
);
699 if (*tree
&& match_op(token
, '?')) {
700 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
701 expr
->op
= token
->special
;
704 token
= parse_expression(token
->next
, &expr
->cond_true
);
705 token
= expect(token
, ':', "in conditional expression");
706 token
= conditional_expression(token
, &expr
->cond_false
);
711 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
713 token
= conditional_expression(token
, tree
);
714 if (*tree
&& token_type(token
) == TOKEN_SPECIAL
) {
715 static const int assignments
[] = {
717 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
718 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
719 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
720 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
721 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
722 int i
, op
= token
->special
;
723 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
724 if (assignments
[i
] == op
) {
725 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
729 return assignment_expression(token
->next
, &expr
->right
);
735 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
738 token
, tree
, EXPR_COMMA
, assignment_expression
,
743 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
745 return comma_expression(token
,tree
);