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.
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
);
63 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
66 static int convert_one_fn_token(struct token
*token
)
68 struct symbol
*sym
= current_fn
;
71 struct ident
*ident
= sym
->ident
;
74 struct string
*string
;
76 string
= __alloc_string(len
+1);
77 memcpy(string
->data
, ident
->name
, len
);
78 string
->data
[len
] = 0;
79 string
->length
= len
+1;
80 token_type(token
) = TOKEN_STRING
;
81 token
->string
= string
;
88 static int convert_function(struct token
*next
)
92 struct token
*token
= next
;
94 switch (token_type(token
)) {
98 if (token
->ident
== &__func___ident
||
99 token
->ident
== &__FUNCTION___ident
||
100 token
->ident
== &__PRETTY_FUNCTION___ident
) {
101 if (!convert_one_fn_token(token
))
115 static struct token
*string_expression(struct token
*token
, struct expression
*expr
)
117 struct string
*string
= token
->string
;
118 struct token
*next
= token
->next
;
120 convert_function(token
);
122 if (token_type(next
) == TOKEN_STRING
) {
123 int totlen
= string
->length
-1;
127 totlen
+= next
->string
->length
-1;
129 } while (token_type(next
) == TOKEN_STRING
);
131 if (totlen
> MAX_STRING
) {
132 warning(token
->pos
, "trying to concatenate %d-character string (%d bytes max)", totlen
, MAX_STRING
);
136 string
= __alloc_string(totlen
+1);
137 string
->length
= totlen
+1;
141 struct string
*s
= next
->string
;
142 int len
= s
->length
-1;
149 memcpy(data
, s
->data
, len
);
151 } while (token_type(next
) == TOKEN_STRING
);
154 expr
->string
= string
;
159 #define ULLONG_MAX (~0ULL)
162 static void get_number_value(struct expression
*expr
, struct token
*token
)
164 const char *str
= token
->number
;
165 unsigned long long value
;
167 unsigned long modifiers
= 0;
168 int overflow
= 0, do_warn
= 0;
169 int try_unsigned
= 1;
173 value
= strtoull(str
, &end
, 0);
176 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
183 } else if (c
== 'u' || c
== 'U') {
184 added
= MOD_UNSIGNED
;
185 } else if (c
== 'l' || c
== 'L') {
188 added
|= MOD_LONGLONG
;
193 if (modifiers
& added
)
199 /* OK, it's a valid integer */
200 /* decimals can be unsigned only if directly specified as such */
201 if (str
[0] != '0' && !(modifiers
& MOD_UNSIGNED
))
203 if (!(modifiers
& MOD_LONG
)) {
204 bits
= bits_in_int
- 1;
205 if (!(value
& (~1ULL << bits
))) {
206 if (!(value
& (1ULL << bits
))) {
208 } else if (try_unsigned
) {
209 modifiers
|= MOD_UNSIGNED
;
213 modifiers
|= MOD_LONG
;
216 if (!(modifiers
& MOD_LONGLONG
)) {
217 bits
= bits_in_long
- 1;
218 if (!(value
& (~1ULL << bits
))) {
219 if (!(value
& (1ULL << bits
))) {
221 } else if (try_unsigned
) {
222 modifiers
|= MOD_UNSIGNED
;
227 modifiers
|= MOD_LONGLONG
;
230 bits
= bits_in_longlong
- 1;
231 if (value
& (~1ULL << bits
))
233 if (!(value
& (1ULL << bits
)))
236 warning(expr
->pos
, "decimal constant %s is too big for long long",
238 modifiers
|= MOD_UNSIGNED
;
241 warning(expr
->pos
, "constant %s is so big it is%s%s%s",
243 (modifiers
& MOD_UNSIGNED
) ? " unsigned":"",
244 (modifiers
& MOD_LONG
) ? " long":"",
245 (modifiers
& MOD_LONGLONG
) ? " long":"");
248 "decimal constant %s is between LONG_MAX and ULONG_MAX."
249 " For C99 that means long long, C90 compilers are very "
250 "likely to produce unsigned long (and a warning) here",
252 expr
->type
= EXPR_VALUE
;
253 expr
->ctype
= ctype_integer(modifiers
);
257 error_die(expr
->pos
, "constant %s is too big even for unsigned long long",
261 expr
->fvalue
= string_to_ld(str
, &end
);
268 if (*end
== 'f' || *end
== 'F')
269 expr
->ctype
= &float_ctype
;
270 else if (*end
== 'l' || *end
== 'L')
271 expr
->ctype
= &ldouble_ctype
;
273 expr
->ctype
= &double_ctype
;
277 expr
->type
= EXPR_FVALUE
;
281 error_die(expr
->pos
, "constant %s is not a valid number", show_token(token
));
284 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
286 struct expression
*expr
= NULL
;
288 switch (token_type(token
)) {
290 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
291 expr
->ctype
= &int_ctype
;
292 expr
->value
= (unsigned char) token
->character
;
297 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
298 get_number_value(expr
, token
);
303 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
304 struct token
*next
= token
->next
;
306 if (!sym
&& convert_function(token
))
309 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
312 * We support types as real first-class citizens, with type
315 * if (typeof(a) == int) ..
317 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
318 warning(token
->pos
, "typename in expression");
321 expr
->symbol_name
= token
->ident
;
329 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
330 token
= string_expression(token
, expr
);
335 if (token
->special
== '(') {
336 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
338 token
= parens_expression(token
, &expr
->unop
, "in expression");
341 if (token
->special
== '[' && lookup_type(token
->next
)) {
342 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
343 token
= typename(token
->next
, &expr
->symbol
);
344 token
= expect(token
, ']', "in type expression");
355 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
357 while (!match_op(token
, ')')) {
358 struct expression
*expr
= NULL
;
359 token
= assignment_expression(token
, &expr
);
362 add_expression(list
, expr
);
363 if (!match_op(token
, ','))
371 * extend to deal with the ambiguous C grammar for parsing
372 * a cast expressions followed by an initializer.
374 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
376 struct expression
*expr
= cast_init_expr
;
379 token
= primary_expression(token
, &expr
);
381 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
382 switch (token
->special
) {
383 case '[': { /* Array dereference */
384 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
385 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
392 token
= parse_expression(token
->next
, &add
->right
);
393 token
= expect(token
, ']', "at end of array dereference");
397 case SPECIAL_INCREMENT
: /* Post-increment */
398 case SPECIAL_DECREMENT
: { /* Post-decrement */
399 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
400 post
->op
= token
->special
;
406 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
407 /* "x->y" is just shorthand for "(*x).y" */
408 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
414 case '.': { /* Structure member dereference */
415 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
419 if (token_type(token
) != TOKEN_IDENT
) {
420 warning(token
->pos
, "Expected member name");
423 deref
->member
= token
->ident
;
429 case '(': { /* Function call */
430 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
433 token
= expression_list(token
->next
, &call
->args
);
434 token
= expect(token
, ')', "in function call");
448 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
449 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
);
451 static struct token
*type_info_expression(struct token
*token
,
452 struct expression
**tree
, int type
)
454 struct expression
*expr
= alloc_expression(token
->pos
, type
);
458 if (!match_op(token
, '(') || !lookup_type(token
->next
))
459 return unary_expression(token
, &expr
->cast_expression
);
460 token
= typename(token
->next
, &expr
->cast_type
);
462 if (!match_op(token
, ')')) {
463 static const char * error
[] = {
464 [EXPR_SIZEOF
] = "at end of sizeof",
465 [EXPR_ALIGNOF
] = "at end of __alignof__",
466 [EXPR_PTRSIZEOF
] = "at end of __sizeof_ptr__"
468 return expect(token
, ')', error
[type
]);
473 * C99 ambiguity: the typename might have been the beginning
474 * of a typed initializer expression..
476 if (match_op(token
, '{'))
477 token
= initializer(&expr
->cast_expression
, token
);
481 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
483 if (token_type(token
) == TOKEN_IDENT
) {
484 struct ident
*ident
= token
->ident
;
485 if (ident
->reserved
) {
486 static const struct {
489 } type_information
[] = {
490 { &sizeof_ident
, EXPR_SIZEOF
},
491 { &__alignof___ident
, EXPR_ALIGNOF
},
492 { &__sizeof_ptr___ident
, EXPR_PTRSIZEOF
},
495 for (i
= 0; i
< 3; i
++) {
496 if (ident
== type_information
[i
].id
)
497 return type_info_expression(token
, tree
, type_information
[i
].type
);
502 if (token_type(token
) == TOKEN_SPECIAL
) {
503 if (match_oplist(token
->special
,
504 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
505 '&', '*', '+', '-', '~', '!', 0)) {
506 struct expression
*unop
;
507 struct expression
*unary
;
510 next
= cast_expression(token
->next
, &unop
);
512 warning(token
->pos
, "Syntax error in unary expression");
515 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
516 unary
->op
= token
->special
;
522 /* Gcc extension: &&label gives the address of a label */
523 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
524 token_type(token
->next
) == TOKEN_IDENT
) {
525 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
526 struct symbol
*sym
= label_symbol(token
->next
);
527 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
)) {
528 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
529 add_symbol(&function_computed_target_list
, sym
);
531 label
->label_symbol
= sym
;
533 return token
->next
->next
;
538 return postfix_expression(token
, tree
, NULL
);
542 * Ambiguity: a '(' can be either a cast-expression or
543 * a primary-expression depending on whether it is followed
546 * additional ambiguity: a "cast expression" followed by
547 * an initializer is really a postfix-expression.
549 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
551 if (match_op(token
, '(')) {
552 struct token
*next
= token
->next
;
553 if (lookup_type(next
)) {
554 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
557 token
= typename(next
, &sym
);
558 cast
->cast_type
= sym
;
559 token
= expect(token
, ')', "at end of cast operator");
560 if (match_op(token
, '{')) {
561 token
= initializer(&cast
->cast_expression
, token
);
562 return postfix_expression(token
, tree
, cast
);
565 token
= cast_expression(token
, &cast
->cast_expression
);
569 return unary_expression(token
, tree
);
573 * Generic left-to-right binop parsing
575 * This _really_ needs to be inlined, because that makes the inner
576 * function call statically deterministic rather than a totally
577 * unpredictable indirect call. But gcc-3 is so "clever" that it
578 * doesn't do so by default even when you tell it to inline it.
580 * Making it a macro avoids the inlining problem, and also means
581 * that we can pass in the op-comparison as an expression rather
582 * than create a data structure for it.
585 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
586 struct expression *left = NULL; \
587 struct token * next = inner(token, &left); \
590 while (token_type(next) == TOKEN_SPECIAL) { \
591 struct expression *top, *right = NULL; \
592 int op = next->special; \
596 top = alloc_expression(next->pos, type); \
597 next = inner(next->next, &right); \
599 warning(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
604 top->right = right; \
613 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
616 token
, tree
, EXPR_BINOP
, cast_expression
,
617 (op
== '*') || (op
== '/') || (op
== '%')
621 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
624 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
625 (op
== '+') || (op
== '-')
629 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
632 token
, tree
, EXPR_BINOP
, additive_expression
,
633 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
637 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
640 token
, tree
, EXPR_COMPARE
, shift_expression
,
641 (op
== '<') || (op
== '>') ||
642 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
646 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
649 token
, tree
, EXPR_COMPARE
, relational_expression
,
650 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
654 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
657 token
, tree
, EXPR_BINOP
, equality_expression
,
662 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
665 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
670 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
673 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
678 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
681 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
682 (op
== SPECIAL_LOGICAL_AND
)
686 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
689 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
690 (op
== SPECIAL_LOGICAL_OR
)
694 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
696 token
= logical_or_expression(token
, tree
);
697 if (match_op(token
, '?')) {
698 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
699 expr
->op
= token
->special
;
702 token
= parse_expression(token
->next
, &expr
->cond_true
);
703 token
= expect(token
, ':', "in conditional expression");
704 token
= conditional_expression(token
, &expr
->cond_false
);
709 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
711 token
= conditional_expression(token
, tree
);
712 if (token_type(token
) == TOKEN_SPECIAL
) {
713 static const int assignments
[] = {
715 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
716 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
717 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
718 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
719 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
720 int i
, op
= token
->special
;
721 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
722 if (assignments
[i
] == op
) {
723 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
727 return assignment_expression(token
->next
, &expr
->right
);
733 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
736 token
, tree
, EXPR_COMMA
, assignment_expression
,
741 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
743 return comma_expression(token
,tree
);