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.
27 #include "expression.h"
31 static int match_oplist(int op
, ...)
38 nextop
= va_arg(args
, int);
39 } while (nextop
!= 0 && nextop
!= op
);
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
);
55 start_symbol_scope(e
->pos
);
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 struct symbol
*handle_func(struct token
*token
)
70 struct ident
*ident
= token
->ident
;
71 struct symbol
*decl
, *array
;
72 struct string
*string
;
75 if (ident
!= &__func___ident
&&
76 ident
!= &__FUNCTION___ident
&&
77 ident
!= &__PRETTY_FUNCTION___ident
)
80 if (!current_fn
|| !current_fn
->ident
)
83 /* OK, it's one of ours */
84 array
= alloc_symbol(token
->pos
, SYM_ARRAY
);
85 array
->ctype
.base_type
= &char_ctype
;
86 array
->ctype
.alignment
= 1;
87 array
->endpos
= token
->pos
;
88 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
89 decl
->ctype
.base_type
= array
;
90 decl
->ctype
.alignment
= 1;
91 decl
->ctype
.modifiers
= MOD_STATIC
;
92 decl
->endpos
= token
->pos
;
94 /* function-scope, but in NS_SYMBOL */
95 bind_symbol(decl
, ident
, NS_LABEL
);
96 decl
->namespace = NS_SYMBOL
;
98 len
= current_fn
->ident
->len
;
99 string
= __alloc_string(len
+ 1);
100 memcpy(string
->data
, current_fn
->ident
->name
, len
);
101 string
->data
[len
] = 0;
102 string
->length
= len
+ 1;
104 decl
->initializer
= alloc_expression(token
->pos
, EXPR_STRING
);
105 decl
->initializer
->string
= string
;
106 decl
->initializer
->ctype
= decl
;
107 decl
->array_size
= alloc_const_expression(token
->pos
, len
+ 1);
108 array
->array_size
= decl
->array_size
;
109 decl
->bit_size
= array
->bit_size
= bytes_to_bits(len
+ 1);
114 static struct token
*parse_type(struct token
*token
, struct expression
**tree
)
117 *tree
= alloc_expression(token
->pos
, EXPR_TYPE
);
118 (*tree
)->flags
= Int_const_expr
; /* sic */
119 token
= typename(token
, &sym
, NULL
);
121 sparse_error(token
->pos
,
122 "type expression should not include identifier "
123 "\"%s\"", sym
->ident
->name
);
124 (*tree
)->symbol
= sym
;
128 static struct token
*builtin_types_compatible_p_expr(struct token
*token
,
129 struct expression
**tree
)
131 struct expression
*expr
= alloc_expression(
132 token
->pos
, EXPR_COMPARE
);
133 expr
->flags
= Int_const_expr
;
134 expr
->op
= SPECIAL_EQUAL
;
136 if (!match_op(token
, '('))
137 return expect(token
, '(',
138 "after __builtin_types_compatible_p");
140 token
= parse_type(token
, &expr
->left
);
141 if (!match_op(token
, ','))
142 return expect(token
, ',',
143 "in __builtin_types_compatible_p");
145 token
= parse_type(token
, &expr
->right
);
146 if (!match_op(token
, ')'))
147 return expect(token
, ')',
148 "at end of __builtin_types_compatible_p");
155 static struct token
*builtin_offsetof_expr(struct token
*token
,
156 struct expression
**tree
)
158 struct expression
*expr
= NULL
;
159 struct expression
**p
= &expr
;
164 if (!match_op(token
, '('))
165 return expect(token
, '(', "after __builtin_offset");
168 token
= typename(token
, &sym
, NULL
);
170 sparse_error(token
->pos
,
171 "type expression should not include identifier "
172 "\"%s\"", sym
->ident
->name
);
174 if (!match_op(token
, ','))
175 return expect(token
, ',', "in __builtin_offset");
178 struct expression
*e
;
184 return expect(token
, ')', "at end of __builtin_offset");
185 case SPECIAL_DEREFERENCE
:
186 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
187 e
->flags
= Int_const_expr
;
194 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
195 e
->flags
= Int_const_expr
;
197 if (token_type(token
) != TOKEN_IDENT
) {
198 sparse_error(token
->pos
, "Expected member name");
201 e
->ident
= token
->ident
;
206 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
207 e
->flags
= Int_const_expr
;
209 token
= parse_expression(token
, &e
->index
);
210 token
= expect(token
, ']',
211 "at end of array dereference");
217 op
= token_type(token
) == TOKEN_SPECIAL
? token
->special
: 0;
222 #define ULLONG_MAX (~0ULL)
225 static unsigned long long parse_num(const char *nptr
, char **end
)
227 if (nptr
[0] == '0' && tolower(nptr
[1]) == 'b')
228 return strtoull(&nptr
[2], end
, 2);
229 return strtoull(nptr
, end
, 0);
232 static void get_number_value(struct expression
*expr
, struct token
*token
)
234 const char *str
= token
->number
;
235 unsigned long long value
;
237 int size
= 0, want_unsigned
= 0;
238 int overflow
= 0, do_warn
= 0;
239 int try_unsigned
= 1;
243 value
= parse_num(str
, &end
);
246 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
252 } else if (c
== 'u' || c
== 'U') {
256 } else if (c
== 'l' || c
== 'L') {
269 /* OK, it's a valid integer */
270 /* decimals can be unsigned only if directly specified as such */
271 if (str
[0] != '0' && !want_unsigned
)
274 bits
= bits_in_int
- 1;
275 if (!(value
& (~1ULL << bits
))) {
276 if (!(value
& (1ULL << bits
))) {
278 } else if (try_unsigned
) {
287 bits
= bits_in_long
- 1;
288 if (!(value
& (~1ULL << bits
))) {
289 if (!(value
& (1ULL << bits
))) {
291 } else if (try_unsigned
) {
300 bits
= bits_in_longlong
- 1;
301 if (value
& (~1ULL << bits
))
303 if (!(value
& (1ULL << bits
)))
306 warning(expr
->pos
, "decimal constant %s is too big for long long",
311 warning(expr
->pos
, "constant %s is so big it is%s%s%s",
313 want_unsigned
? " unsigned":"",
314 size
> 0 ? " long":"",
315 size
> 1 ? " long":"");
318 "decimal constant %s is between LONG_MAX and ULONG_MAX."
319 " For C99 that means long long, C90 compilers are very "
320 "likely to produce unsigned long (and a warning) here",
322 expr
->type
= EXPR_VALUE
;
323 expr
->flags
= Int_const_expr
;
324 expr
->ctype
= ctype_integer(size
, want_unsigned
);
328 error_die(expr
->pos
, "constant %s is too big even for unsigned long long",
332 expr
->fvalue
= string_to_ld(str
, &end
);
339 if (*end
== 'f' || *end
== 'F')
340 expr
->ctype
= &float_ctype
;
341 else if (*end
== 'l' || *end
== 'L')
342 expr
->ctype
= &ldouble_ctype
;
344 expr
->ctype
= &double_ctype
;
348 expr
->flags
= Float_literal
;
349 expr
->type
= EXPR_FVALUE
;
353 error_die(expr
->pos
, "constant %s is not a valid number", show_token(token
));
356 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
358 struct expression
*expr
= NULL
;
360 switch (token_type(token
)) {
361 case TOKEN_CHAR
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
362 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
363 expr
->flags
= Int_const_expr
;
364 expr
->ctype
= token_type(token
) < TOKEN_WIDE_CHAR
? &int_ctype
: &long_ctype
;
365 get_char_constant(token
, &expr
->value
);
370 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
371 get_number_value(expr
, token
); /* will see if it's an integer */
375 case TOKEN_ZERO_IDENT
: {
376 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
377 expr
->flags
= Int_const_expr
;
378 expr
->ctype
= &int_ctype
;
379 expr
->symbol
= &zero_int
;
380 expr
->symbol_name
= token
->ident
;
386 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
387 struct token
*next
= token
->next
;
390 sym
= handle_func(token
);
391 if (token
->ident
== &__builtin_types_compatible_p_ident
) {
392 token
= builtin_types_compatible_p_expr(token
, &expr
);
395 if (token
->ident
== &__builtin_offsetof_ident
) {
396 token
= builtin_offsetof_expr(token
, &expr
);
399 } else if (sym
->enum_member
) {
400 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
401 *expr
= *sym
->initializer
;
402 /* we want the right position reported, thus the copy */
403 expr
->pos
= token
->pos
;
404 expr
->flags
= Int_const_expr
;
409 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
412 * We support types as real first-class citizens, with type
415 * if (typeof(a) == int) ..
417 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
418 sparse_error(token
->pos
, "typename in expression");
421 expr
->symbol_name
= token
->ident
;
428 case TOKEN_WIDE_STRING
:
429 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
430 token
= get_string_constant(token
, expr
);
434 if (token
->special
== '(') {
435 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
437 token
= parens_expression(token
, &expr
->unop
, "in expression");
439 expr
->flags
= expr
->unop
->flags
;
442 if (token
->special
== '[' && lookup_type(token
->next
)) {
443 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
444 expr
->flags
= Int_const_expr
; /* sic */
445 token
= typename(token
->next
, &expr
->symbol
, NULL
);
446 token
= expect(token
, ']', "in type expression");
457 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
459 while (!match_op(token
, ')')) {
460 struct expression
*expr
= NULL
;
461 token
= assignment_expression(token
, &expr
);
464 add_expression(list
, expr
);
465 if (!match_op(token
, ','))
473 * extend to deal with the ambiguous C grammar for parsing
474 * a cast expressions followed by an initializer.
476 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
478 struct expression
*expr
= cast_init_expr
;
481 token
= primary_expression(token
, &expr
);
483 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
484 switch (token
->special
) {
485 case '[': { /* Array dereference */
486 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
487 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
494 token
= parse_expression(token
->next
, &add
->right
);
495 token
= expect(token
, ']', "at end of array dereference");
499 case SPECIAL_INCREMENT
: /* Post-increment */
500 case SPECIAL_DECREMENT
: { /* Post-decrement */
501 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
502 post
->op
= token
->special
;
508 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
509 /* "x->y" is just shorthand for "(*x).y" */
510 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
516 case '.': { /* Structure member dereference */
517 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
521 if (token_type(token
) != TOKEN_IDENT
) {
522 sparse_error(token
->pos
, "Expected member name");
525 deref
->member
= token
->ident
;
531 case '(': { /* Function call */
532 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
535 token
= expression_list(token
->next
, &call
->args
);
536 token
= expect(token
, ')', "in function call");
550 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
551 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
);
553 static struct token
*type_info_expression(struct token
*token
,
554 struct expression
**tree
, int type
)
556 struct expression
*expr
= alloc_expression(token
->pos
, type
);
560 expr
->flags
= Int_const_expr
; /* XXX: VLA support will need that changed */
562 if (!match_op(token
, '(') || !lookup_type(token
->next
))
563 return unary_expression(token
, &expr
->cast_expression
);
565 token
= typename(token
->next
, &expr
->cast_type
, NULL
);
567 if (!match_op(token
, ')')) {
568 static const char * error
[] = {
569 [EXPR_SIZEOF
] = "at end of sizeof",
570 [EXPR_ALIGNOF
] = "at end of __alignof__",
571 [EXPR_PTRSIZEOF
] = "at end of __sizeof_ptr__"
573 return expect(token
, ')', error
[type
]);
578 * C99 ambiguity: the typename might have been the beginning
579 * of a typed initializer expression..
581 if (match_op(token
, '{')) {
582 struct expression
*cast
= alloc_expression(p
->pos
, EXPR_CAST
);
583 cast
->cast_type
= expr
->cast_type
;
584 expr
->cast_type
= NULL
;
585 expr
->cast_expression
= cast
;
586 token
= initializer(&cast
->cast_expression
, token
);
587 token
= postfix_expression(token
, &expr
->cast_expression
, cast
);
592 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
594 if (token_type(token
) == TOKEN_IDENT
) {
595 struct ident
*ident
= token
->ident
;
596 if (ident
->reserved
) {
597 static const struct {
600 } type_information
[] = {
601 { &sizeof_ident
, EXPR_SIZEOF
},
602 { &__alignof___ident
, EXPR_ALIGNOF
},
603 { &__alignof_ident
, EXPR_ALIGNOF
},
604 { &__sizeof_ptr___ident
, EXPR_PTRSIZEOF
},
607 for (i
= 0; i
< ARRAY_SIZE(type_information
); i
++) {
608 if (ident
== type_information
[i
].id
)
609 return type_info_expression(token
, tree
, type_information
[i
].type
);
614 if (token_type(token
) == TOKEN_SPECIAL
) {
615 if (match_oplist(token
->special
,
616 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
618 struct expression
*unop
;
619 struct expression
*unary
;
622 next
= cast_expression(token
->next
, &unop
);
624 sparse_error(token
->pos
, "Syntax error in unary expression");
628 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
629 unary
->op
= token
->special
;
634 /* possibly constant ones */
635 if (match_oplist(token
->special
, '+', '-', '~', '!', 0)) {
636 struct expression
*unop
;
637 struct expression
*unary
;
640 next
= cast_expression(token
->next
, &unop
);
642 sparse_error(token
->pos
, "Syntax error in unary expression");
646 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
647 unary
->op
= token
->special
;
649 unary
->flags
= unop
->flags
& Int_const_expr
;
653 /* Gcc extension: &&label gives the address of a label */
654 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
655 token_type(token
->next
) == TOKEN_IDENT
) {
656 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
657 struct symbol
*sym
= label_symbol(token
->next
);
658 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
)) {
659 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
660 add_symbol(&function_computed_target_list
, sym
);
662 label
->label_symbol
= sym
;
664 return token
->next
->next
;
669 return postfix_expression(token
, tree
, NULL
);
673 * Ambiguity: a '(' can be either a cast-expression or
674 * a primary-expression depending on whether it is followed
677 * additional ambiguity: a "cast expression" followed by
678 * an initializer is really a postfix-expression.
680 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
682 if (match_op(token
, '(')) {
683 struct token
*next
= token
->next
;
684 if (lookup_type(next
)) {
685 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
686 struct expression
*v
;
690 token
= typename(next
, &sym
, &is_force
);
691 cast
->cast_type
= sym
;
692 token
= expect(token
, ')', "at end of cast operator");
693 if (match_op(token
, '{')) {
696 "[force] in compound literal");
697 token
= initializer(&cast
->cast_expression
, token
);
698 return postfix_expression(token
, tree
, cast
);
702 cast
->type
= EXPR_FORCE_CAST
;
703 token
= cast_expression(token
, &v
);
706 cast
->cast_expression
= v
;
707 if (v
->flags
& Int_const_expr
)
708 cast
->flags
= Int_const_expr
;
709 else if (v
->flags
& Float_literal
) /* and _not_ int */
710 cast
->flags
= Int_const_expr
| Float_literal
;
714 return unary_expression(token
, tree
);
718 * Generic left-to-right binop parsing
720 * This _really_ needs to be inlined, because that makes the inner
721 * function call statically deterministic rather than a totally
722 * unpredictable indirect call. But gcc-3 is so "clever" that it
723 * doesn't do so by default even when you tell it to inline it.
725 * Making it a macro avoids the inlining problem, and also means
726 * that we can pass in the op-comparison as an expression rather
727 * than create a data structure for it.
730 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
731 struct expression *left = NULL; \
732 struct token * next = inner(__token, &left); \
735 while (token_type(next) == TOKEN_SPECIAL) { \
736 struct expression *top, *right = NULL; \
737 int op = next->special; \
741 top = alloc_expression(next->pos, type); \
742 next = inner(next->next, &right); \
744 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
747 top->flags = left->flags & right->flags \
751 top->right = right; \
759 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
762 token
, tree
, EXPR_BINOP
, cast_expression
,
763 (op
== '*') || (op
== '/') || (op
== '%')
767 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
770 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
771 (op
== '+') || (op
== '-')
775 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
778 token
, tree
, EXPR_BINOP
, additive_expression
,
779 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
783 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
786 token
, tree
, EXPR_COMPARE
, shift_expression
,
787 (op
== '<') || (op
== '>') ||
788 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
792 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
795 token
, tree
, EXPR_COMPARE
, relational_expression
,
796 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
800 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
803 token
, tree
, EXPR_BINOP
, equality_expression
,
808 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
811 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
816 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
819 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
824 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
827 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
828 (op
== SPECIAL_LOGICAL_AND
)
832 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
835 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
836 (op
== SPECIAL_LOGICAL_OR
)
840 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
842 token
= logical_or_expression(token
, tree
);
843 if (*tree
&& match_op(token
, '?')) {
844 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
845 expr
->op
= token
->special
;
848 token
= parse_expression(token
->next
, &expr
->cond_true
);
849 token
= expect(token
, ':', "in conditional expression");
850 token
= conditional_expression(token
, &expr
->cond_false
);
851 if (expr
->left
&& expr
->cond_false
) {
852 int is_const
= expr
->left
->flags
&
853 expr
->cond_false
->flags
&
856 is_const
&= expr
->cond_true
->flags
;
857 expr
->flags
= is_const
;
863 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
865 token
= conditional_expression(token
, tree
);
866 if (*tree
&& token_type(token
) == TOKEN_SPECIAL
) {
867 static const int assignments
[] = {
869 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
870 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
871 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
872 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
873 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
874 int i
, op
= token
->special
;
875 for (i
= 0; i
< ARRAY_SIZE(assignments
); i
++)
876 if (assignments
[i
] == op
) {
877 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
881 return assignment_expression(token
->next
, &expr
->right
);
887 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
890 token
, tree
, EXPR_COMMA
, assignment_expression
,
895 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
897 return comma_expression(token
,tree
);