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
);
304 case TOKEN_ZERO_IDENT
: {
305 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
306 expr
->ctype
= &int_ctype
;
307 expr
->symbol
= &zero_int
;
308 expr
->symbol_name
= token
->ident
;
314 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
315 struct token
*next
= token
->next
;
317 if (!sym
&& convert_function(token
))
320 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
323 * We support types as real first-class citizens, with type
326 * if (typeof(a) == int) ..
328 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
329 error(token
->pos
, "typename in expression");
332 expr
->symbol_name
= token
->ident
;
340 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
341 token
= string_expression(token
, expr
);
346 if (token
->special
== '(') {
347 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
349 token
= parens_expression(token
, &expr
->unop
, "in expression");
352 if (token
->special
== '[' && lookup_type(token
->next
)) {
353 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
354 token
= typename(token
->next
, &expr
->symbol
);
355 token
= expect(token
, ']', "in type expression");
366 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
368 while (!match_op(token
, ')')) {
369 struct expression
*expr
= NULL
;
370 token
= assignment_expression(token
, &expr
);
373 add_expression(list
, expr
);
374 if (!match_op(token
, ','))
382 * extend to deal with the ambiguous C grammar for parsing
383 * a cast expressions followed by an initializer.
385 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
387 struct expression
*expr
= cast_init_expr
;
390 token
= primary_expression(token
, &expr
);
392 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
393 switch (token
->special
) {
394 case '[': { /* Array dereference */
395 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
396 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
403 token
= parse_expression(token
->next
, &add
->right
);
404 token
= expect(token
, ']', "at end of array dereference");
408 case SPECIAL_INCREMENT
: /* Post-increment */
409 case SPECIAL_DECREMENT
: { /* Post-decrement */
410 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
411 post
->op
= token
->special
;
417 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
418 /* "x->y" is just shorthand for "(*x).y" */
419 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
425 case '.': { /* Structure member dereference */
426 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
430 if (token_type(token
) != TOKEN_IDENT
) {
431 error(token
->pos
, "Expected member name");
434 deref
->member
= token
->ident
;
440 case '(': { /* Function call */
441 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
444 token
= expression_list(token
->next
, &call
->args
);
445 token
= expect(token
, ')', "in function call");
459 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
460 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
);
462 static struct token
*type_info_expression(struct token
*token
,
463 struct expression
**tree
, int type
)
465 struct expression
*expr
= alloc_expression(token
->pos
, type
);
469 if (!match_op(token
, '(') || !lookup_type(token
->next
))
470 return unary_expression(token
, &expr
->cast_expression
);
471 token
= typename(token
->next
, &expr
->cast_type
);
473 if (!match_op(token
, ')')) {
474 static const char * error
[] = {
475 [EXPR_SIZEOF
] = "at end of sizeof",
476 [EXPR_ALIGNOF
] = "at end of __alignof__",
477 [EXPR_PTRSIZEOF
] = "at end of __sizeof_ptr__"
479 return expect(token
, ')', error
[type
]);
484 * C99 ambiguity: the typename might have been the beginning
485 * of a typed initializer expression..
487 if (match_op(token
, '{'))
488 token
= initializer(&expr
->cast_expression
, token
);
492 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
494 if (token_type(token
) == TOKEN_IDENT
) {
495 struct ident
*ident
= token
->ident
;
496 if (ident
->reserved
) {
497 static const struct {
500 } type_information
[] = {
501 { &sizeof_ident
, EXPR_SIZEOF
},
502 { &__alignof___ident
, EXPR_ALIGNOF
},
503 { &__sizeof_ptr___ident
, EXPR_PTRSIZEOF
},
506 for (i
= 0; i
< 3; i
++) {
507 if (ident
== type_information
[i
].id
)
508 return type_info_expression(token
, tree
, type_information
[i
].type
);
513 if (token_type(token
) == TOKEN_SPECIAL
) {
514 if (match_oplist(token
->special
,
515 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
516 '&', '*', '+', '-', '~', '!', 0)) {
517 struct expression
*unop
;
518 struct expression
*unary
;
521 next
= cast_expression(token
->next
, &unop
);
523 error(token
->pos
, "Syntax error in unary expression");
526 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
527 unary
->op
= token
->special
;
533 /* Gcc extension: &&label gives the address of a label */
534 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
535 token_type(token
->next
) == TOKEN_IDENT
) {
536 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
537 struct symbol
*sym
= label_symbol(token
->next
);
538 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
)) {
539 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
540 add_symbol(&function_computed_target_list
, sym
);
542 label
->label_symbol
= sym
;
544 return token
->next
->next
;
549 return postfix_expression(token
, tree
, NULL
);
553 * Ambiguity: a '(' can be either a cast-expression or
554 * a primary-expression depending on whether it is followed
557 * additional ambiguity: a "cast expression" followed by
558 * an initializer is really a postfix-expression.
560 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
562 if (match_op(token
, '(')) {
563 struct token
*next
= token
->next
;
564 if (lookup_type(next
)) {
565 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
568 token
= typename(next
, &sym
);
569 cast
->cast_type
= sym
;
570 token
= expect(token
, ')', "at end of cast operator");
571 if (match_op(token
, '{')) {
572 token
= initializer(&cast
->cast_expression
, token
);
573 return postfix_expression(token
, tree
, cast
);
576 token
= cast_expression(token
, &cast
->cast_expression
);
580 return unary_expression(token
, tree
);
584 * Generic left-to-right binop parsing
586 * This _really_ needs to be inlined, because that makes the inner
587 * function call statically deterministic rather than a totally
588 * unpredictable indirect call. But gcc-3 is so "clever" that it
589 * doesn't do so by default even when you tell it to inline it.
591 * Making it a macro avoids the inlining problem, and also means
592 * that we can pass in the op-comparison as an expression rather
593 * than create a data structure for it.
596 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
597 struct expression *left = NULL; \
598 struct token * next = inner(token, &left); \
601 while (token_type(next) == TOKEN_SPECIAL) { \
602 struct expression *top, *right = NULL; \
603 int op = next->special; \
607 top = alloc_expression(next->pos, type); \
608 next = inner(next->next, &right); \
610 error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
615 top->right = right; \
624 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
627 token
, tree
, EXPR_BINOP
, cast_expression
,
628 (op
== '*') || (op
== '/') || (op
== '%')
632 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
635 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
636 (op
== '+') || (op
== '-')
640 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
643 token
, tree
, EXPR_BINOP
, additive_expression
,
644 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
648 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
651 token
, tree
, EXPR_COMPARE
, shift_expression
,
652 (op
== '<') || (op
== '>') ||
653 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
657 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
660 token
, tree
, EXPR_COMPARE
, relational_expression
,
661 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
665 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
668 token
, tree
, EXPR_BINOP
, equality_expression
,
673 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
676 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
681 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
684 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
689 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
692 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
693 (op
== SPECIAL_LOGICAL_AND
)
697 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
700 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
701 (op
== SPECIAL_LOGICAL_OR
)
705 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
707 token
= logical_or_expression(token
, tree
);
708 if (*tree
&& match_op(token
, '?')) {
709 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
710 expr
->op
= token
->special
;
713 token
= parse_expression(token
->next
, &expr
->cond_true
);
714 token
= expect(token
, ':', "in conditional expression");
715 token
= conditional_expression(token
, &expr
->cond_false
);
720 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
722 token
= conditional_expression(token
, tree
);
723 if (*tree
&& token_type(token
) == TOKEN_SPECIAL
) {
724 static const int assignments
[] = {
726 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
727 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
728 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
729 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
730 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
731 int i
, op
= token
->special
;
732 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
733 if (assignments
[i
] == op
) {
734 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
738 return assignment_expression(token
->next
, &expr
->right
);
744 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
747 token
, tree
, EXPR_COMMA
, assignment_expression
,
752 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
754 return comma_expression(token
,tree
);