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"
30 static int match_oplist(int op
, ...)
37 nextop
= va_arg(args
, int);
38 } while (nextop
!= 0 && nextop
!= op
);
44 static struct token
*comma_expression(struct token
*, struct expression
**);
46 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
48 token
= expect(token
, '(', where
);
49 if (match_op(token
, '{')) {
50 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
51 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
55 token
= compound_statement(token
->next
, stmt
);
57 token
= expect(token
, '}', "at end of statement expression");
59 token
= parse_expression(token
, expr
);
60 return expect(token
, ')', where
);
64 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
67 static int convert_one_fn_token(struct token
*token
)
69 struct symbol
*sym
= current_fn
;
72 struct ident
*ident
= sym
->ident
;
75 struct string
*string
;
77 string
= __alloc_string(len
+1);
78 memcpy(string
->data
, ident
->name
, len
);
79 string
->data
[len
] = 0;
80 string
->length
= len
+1;
81 token_type(token
) = TOKEN_STRING
;
82 token
->string
= string
;
89 static int convert_function(struct token
*next
)
93 struct token
*token
= next
;
95 switch (token_type(token
)) {
99 if (token
->ident
== &__func___ident
||
100 token
->ident
== &__FUNCTION___ident
||
101 token
->ident
== &__PRETTY_FUNCTION___ident
) {
102 if (!convert_one_fn_token(token
))
116 static struct token
*parse_type(struct token
*token
, struct expression
**tree
)
119 *tree
= alloc_expression(token
->pos
, EXPR_TYPE
);
120 (*tree
)->flags
= Int_const_expr
; /* sic */
121 token
= typename(token
, &sym
, NULL
);
123 sparse_error(token
->pos
,
124 "type expression should not include identifier "
125 "\"%s\"", sym
->ident
->name
);
126 (*tree
)->symbol
= sym
;
130 static struct token
*builtin_types_compatible_p_expr(struct token
*token
,
131 struct expression
**tree
)
133 struct expression
*expr
= alloc_expression(
134 token
->pos
, EXPR_COMPARE
);
135 expr
->flags
= Int_const_expr
;
136 expr
->op
= SPECIAL_EQUAL
;
138 if (!match_op(token
, '('))
139 return expect(token
, '(',
140 "after __builtin_types_compatible_p");
142 token
= parse_type(token
, &expr
->left
);
143 if (!match_op(token
, ','))
144 return expect(token
, ',',
145 "in __builtin_types_compatible_p");
147 token
= parse_type(token
, &expr
->right
);
148 if (!match_op(token
, ')'))
149 return expect(token
, ')',
150 "at end of __builtin_types_compatible_p");
157 static struct token
*builtin_offsetof_expr(struct token
*token
,
158 struct expression
**tree
)
160 struct expression
*expr
= NULL
;
161 struct expression
**p
= &expr
;
166 if (!match_op(token
, '('))
167 return expect(token
, '(', "after __builtin_offset");
170 token
= typename(token
, &sym
, NULL
);
172 sparse_error(token
->pos
,
173 "type expression should not include identifier "
174 "\"%s\"", sym
->ident
->name
);
176 if (!match_op(token
, ','))
177 return expect(token
, ',', "in __builtin_offset");
180 struct expression
*e
;
186 return expect(token
, ')', "at end of __builtin_offset");
187 case SPECIAL_DEREFERENCE
:
188 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
189 e
->flags
= Int_const_expr
;
196 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
197 e
->flags
= Int_const_expr
;
199 if (token_type(token
) != TOKEN_IDENT
) {
200 sparse_error(token
->pos
, "Expected member name");
203 e
->ident
= token
->ident
;
208 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
209 e
->flags
= Int_const_expr
;
211 token
= parse_expression(token
, &e
->index
);
212 token
= expect(token
, ']',
213 "at end of array dereference");
219 op
= token_type(token
) == TOKEN_SPECIAL
? token
->special
: 0;
223 static struct token
*string_expression(struct token
*token
, struct expression
*expr
)
225 struct string
*string
= token
->string
;
226 struct token
*next
= token
->next
;
228 convert_function(token
);
230 if (token_type(next
) == TOKEN_STRING
) {
231 int totlen
= string
->length
-1;
235 totlen
+= next
->string
->length
-1;
237 } while (token_type(next
) == TOKEN_STRING
);
239 if (totlen
> MAX_STRING
) {
240 warning(token
->pos
, "trying to concatenate %d-character string (%d bytes max)", totlen
, MAX_STRING
);
244 string
= __alloc_string(totlen
+1);
245 string
->length
= totlen
+1;
249 struct string
*s
= next
->string
;
250 int len
= s
->length
-1;
257 memcpy(data
, s
->data
, len
);
259 } while (token_type(next
) == TOKEN_STRING
);
262 expr
->string
= string
;
267 #define ULLONG_MAX (~0ULL)
270 static void get_number_value(struct expression
*expr
, struct token
*token
)
272 const char *str
= token
->number
;
273 unsigned long long value
;
275 int size
= 0, want_unsigned
= 0;
276 int overflow
= 0, do_warn
= 0;
277 int try_unsigned
= 1;
281 value
= strtoull(str
, &end
, 0);
284 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
290 } else if (c
== 'u' || c
== 'U') {
294 } else if (c
== 'l' || c
== 'L') {
307 /* OK, it's a valid integer */
308 /* decimals can be unsigned only if directly specified as such */
309 if (str
[0] != '0' && !want_unsigned
)
312 bits
= bits_in_int
- 1;
313 if (!(value
& (~1ULL << bits
))) {
314 if (!(value
& (1ULL << bits
))) {
316 } else if (try_unsigned
) {
325 bits
= bits_in_long
- 1;
326 if (!(value
& (~1ULL << bits
))) {
327 if (!(value
& (1ULL << bits
))) {
329 } else if (try_unsigned
) {
338 bits
= bits_in_longlong
- 1;
339 if (value
& (~1ULL << bits
))
341 if (!(value
& (1ULL << bits
)))
344 warning(expr
->pos
, "decimal constant %s is too big for long long",
349 warning(expr
->pos
, "constant %s is so big it is%s%s%s",
351 want_unsigned
? " unsigned":"",
352 size
> 0 ? " long":"",
353 size
> 1 ? " long":"");
356 "decimal constant %s is between LONG_MAX and ULONG_MAX."
357 " For C99 that means long long, C90 compilers are very "
358 "likely to produce unsigned long (and a warning) here",
360 expr
->type
= EXPR_VALUE
;
361 expr
->flags
= Int_const_expr
;
362 expr
->ctype
= ctype_integer(size
, want_unsigned
);
366 error_die(expr
->pos
, "constant %s is too big even for unsigned long long",
370 expr
->fvalue
= string_to_ld(str
, &end
);
377 if (*end
== 'f' || *end
== 'F')
378 expr
->ctype
= &float_ctype
;
379 else if (*end
== 'l' || *end
== 'L')
380 expr
->ctype
= &ldouble_ctype
;
382 expr
->ctype
= &double_ctype
;
386 expr
->flags
= Float_literal
;
387 expr
->type
= EXPR_FVALUE
;
391 error_die(expr
->pos
, "constant %s is not a valid number", show_token(token
));
394 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
396 struct expression
*expr
= NULL
;
398 switch (token_type(token
)) {
400 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
401 expr
->flags
= Int_const_expr
;
402 expr
->ctype
= &int_ctype
;
403 expr
->value
= (unsigned char) token
->character
;
408 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
409 get_number_value(expr
, token
); /* will see if it's an integer */
413 case TOKEN_ZERO_IDENT
: {
414 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
415 expr
->flags
= Int_const_expr
;
416 expr
->ctype
= &int_ctype
;
417 expr
->symbol
= &zero_int
;
418 expr
->symbol_name
= token
->ident
;
424 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
425 struct token
*next
= token
->next
;
428 if (convert_function(token
))
430 if (token
->ident
== &__builtin_types_compatible_p_ident
) {
431 token
= builtin_types_compatible_p_expr(token
, &expr
);
434 if (token
->ident
== &__builtin_offsetof_ident
) {
435 token
= builtin_offsetof_expr(token
, &expr
);
438 } else if (sym
->enum_member
) {
439 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
440 *expr
= *sym
->initializer
;
441 /* we want the right position reported, thus the copy */
442 expr
->pos
= token
->pos
;
443 expr
->flags
= Int_const_expr
;
448 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
451 * We support types as real first-class citizens, with type
454 * if (typeof(a) == int) ..
456 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
457 sparse_error(token
->pos
, "typename in expression");
460 expr
->symbol_name
= token
->ident
;
468 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
469 token
= string_expression(token
, expr
);
474 if (token
->special
== '(') {
475 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
477 token
= parens_expression(token
, &expr
->unop
, "in expression");
479 expr
->flags
= expr
->unop
->flags
;
482 if (token
->special
== '[' && lookup_type(token
->next
)) {
483 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
484 expr
->flags
= Int_const_expr
; /* sic */
485 token
= typename(token
->next
, &expr
->symbol
, NULL
);
486 token
= expect(token
, ']', "in type expression");
497 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
499 while (!match_op(token
, ')')) {
500 struct expression
*expr
= NULL
;
501 token
= assignment_expression(token
, &expr
);
504 add_expression(list
, expr
);
505 if (!match_op(token
, ','))
513 * extend to deal with the ambiguous C grammar for parsing
514 * a cast expressions followed by an initializer.
516 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
518 struct expression
*expr
= cast_init_expr
;
521 token
= primary_expression(token
, &expr
);
523 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
524 switch (token
->special
) {
525 case '[': { /* Array dereference */
526 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
527 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
534 token
= parse_expression(token
->next
, &add
->right
);
535 token
= expect(token
, ']', "at end of array dereference");
539 case SPECIAL_INCREMENT
: /* Post-increment */
540 case SPECIAL_DECREMENT
: { /* Post-decrement */
541 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
542 post
->op
= token
->special
;
548 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
549 /* "x->y" is just shorthand for "(*x).y" */
550 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
556 case '.': { /* Structure member dereference */
557 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
561 if (token_type(token
) != TOKEN_IDENT
) {
562 sparse_error(token
->pos
, "Expected member name");
565 deref
->member
= token
->ident
;
571 case '(': { /* Function call */
572 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
575 token
= expression_list(token
->next
, &call
->args
);
576 token
= expect(token
, ')', "in function call");
590 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
591 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
);
593 static struct token
*type_info_expression(struct token
*token
,
594 struct expression
**tree
, int type
)
596 struct expression
*expr
= alloc_expression(token
->pos
, type
);
600 expr
->flags
= Int_const_expr
; /* XXX: VLA support will need that changed */
602 if (!match_op(token
, '(') || !lookup_type(token
->next
))
603 return unary_expression(token
, &expr
->cast_expression
);
605 token
= typename(token
->next
, &expr
->cast_type
, NULL
);
607 if (!match_op(token
, ')')) {
608 static const char * error
[] = {
609 [EXPR_SIZEOF
] = "at end of sizeof",
610 [EXPR_ALIGNOF
] = "at end of __alignof__",
611 [EXPR_PTRSIZEOF
] = "at end of __sizeof_ptr__"
613 return expect(token
, ')', error
[type
]);
618 * C99 ambiguity: the typename might have been the beginning
619 * of a typed initializer expression..
621 if (match_op(token
, '{')) {
622 struct expression
*cast
= alloc_expression(p
->pos
, EXPR_CAST
);
623 cast
->cast_type
= expr
->cast_type
;
624 expr
->cast_type
= NULL
;
625 expr
->cast_expression
= cast
;
626 token
= initializer(&cast
->cast_expression
, token
);
627 token
= postfix_expression(token
, &expr
->cast_expression
, cast
);
632 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
634 if (token_type(token
) == TOKEN_IDENT
) {
635 struct ident
*ident
= token
->ident
;
636 if (ident
->reserved
) {
637 static const struct {
640 } type_information
[] = {
641 { &sizeof_ident
, EXPR_SIZEOF
},
642 { &__alignof___ident
, EXPR_ALIGNOF
},
643 { &__alignof_ident
, EXPR_ALIGNOF
},
644 { &__sizeof_ptr___ident
, EXPR_PTRSIZEOF
},
647 for (i
= 0; i
< 3; i
++) {
648 if (ident
== type_information
[i
].id
)
649 return type_info_expression(token
, tree
, type_information
[i
].type
);
654 if (token_type(token
) == TOKEN_SPECIAL
) {
655 if (match_oplist(token
->special
,
656 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
658 struct expression
*unop
;
659 struct expression
*unary
;
662 next
= cast_expression(token
->next
, &unop
);
664 sparse_error(token
->pos
, "Syntax error in unary expression");
668 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
669 unary
->op
= token
->special
;
674 /* possibly constant ones */
675 if (match_oplist(token
->special
, '+', '-', '~', '!', 0)) {
676 struct expression
*unop
;
677 struct expression
*unary
;
680 next
= cast_expression(token
->next
, &unop
);
682 sparse_error(token
->pos
, "Syntax error in unary expression");
686 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
687 unary
->op
= token
->special
;
689 unary
->flags
= unop
->flags
& Int_const_expr
;
693 /* Gcc extension: &&label gives the address of a label */
694 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
695 token_type(token
->next
) == TOKEN_IDENT
) {
696 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
697 struct symbol
*sym
= label_symbol(token
->next
);
698 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
)) {
699 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
700 add_symbol(&function_computed_target_list
, sym
);
702 label
->label_symbol
= sym
;
704 return token
->next
->next
;
709 return postfix_expression(token
, tree
, NULL
);
713 * Ambiguity: a '(' can be either a cast-expression or
714 * a primary-expression depending on whether it is followed
717 * additional ambiguity: a "cast expression" followed by
718 * an initializer is really a postfix-expression.
720 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
722 if (match_op(token
, '(')) {
723 struct token
*next
= token
->next
;
724 if (lookup_type(next
)) {
725 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
726 struct expression
*v
;
730 token
= typename(next
, &sym
, &is_force
);
731 cast
->cast_type
= sym
;
732 token
= expect(token
, ')', "at end of cast operator");
733 if (match_op(token
, '{')) {
736 "[force] in compound literal");
737 token
= initializer(&cast
->cast_expression
, token
);
738 return postfix_expression(token
, tree
, cast
);
742 cast
->type
= EXPR_FORCE_CAST
;
743 token
= cast_expression(token
, &v
);
746 cast
->cast_expression
= v
;
747 if (v
->flags
& Int_const_expr
)
748 cast
->flags
= Int_const_expr
;
749 else if (v
->flags
& Float_literal
) /* and _not_ int */
750 cast
->flags
= Int_const_expr
| Float_literal
;
754 return unary_expression(token
, tree
);
758 * Generic left-to-right binop parsing
760 * This _really_ needs to be inlined, because that makes the inner
761 * function call statically deterministic rather than a totally
762 * unpredictable indirect call. But gcc-3 is so "clever" that it
763 * doesn't do so by default even when you tell it to inline it.
765 * Making it a macro avoids the inlining problem, and also means
766 * that we can pass in the op-comparison as an expression rather
767 * than create a data structure for it.
770 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
771 struct expression *left = NULL; \
772 struct token * next = inner(__token, &left); \
775 while (token_type(next) == TOKEN_SPECIAL) { \
776 struct expression *top, *right = NULL; \
777 int op = next->special; \
781 top = alloc_expression(next->pos, type); \
782 next = inner(next->next, &right); \
784 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
787 top->flags = left->flags & right->flags \
791 top->right = right; \
799 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
802 token
, tree
, EXPR_BINOP
, cast_expression
,
803 (op
== '*') || (op
== '/') || (op
== '%')
807 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
810 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
811 (op
== '+') || (op
== '-')
815 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
818 token
, tree
, EXPR_BINOP
, additive_expression
,
819 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
823 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
826 token
, tree
, EXPR_COMPARE
, shift_expression
,
827 (op
== '<') || (op
== '>') ||
828 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
832 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
835 token
, tree
, EXPR_COMPARE
, relational_expression
,
836 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
840 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
843 token
, tree
, EXPR_BINOP
, equality_expression
,
848 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
851 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
856 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
859 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
864 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
867 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
868 (op
== SPECIAL_LOGICAL_AND
)
872 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
875 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
876 (op
== SPECIAL_LOGICAL_OR
)
880 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
882 token
= logical_or_expression(token
, tree
);
883 if (*tree
&& match_op(token
, '?')) {
884 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
885 expr
->op
= token
->special
;
888 token
= parse_expression(token
->next
, &expr
->cond_true
);
889 token
= expect(token
, ':', "in conditional expression");
890 token
= conditional_expression(token
, &expr
->cond_false
);
891 if (expr
->left
&& expr
->cond_false
) {
892 int is_const
= expr
->left
->flags
&
893 expr
->cond_false
->flags
&
896 is_const
&= expr
->cond_true
->flags
;
897 expr
->flags
= is_const
;
903 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
905 token
= conditional_expression(token
, tree
);
906 if (*tree
&& token_type(token
) == TOKEN_SPECIAL
) {
907 static const int assignments
[] = {
909 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
910 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
911 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
912 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
913 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
914 int i
, op
= token
->special
;
915 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
916 if (assignments
[i
] == op
) {
917 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
921 return assignment_expression(token
->next
, &expr
->right
);
927 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
930 token
, tree
, EXPR_COMMA
, assignment_expression
,
935 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
937 return comma_expression(token
,tree
);