4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * This is the expression parsing part of parsing C.
43 #include "expression.h"
47 static int match_oplist(int op
, ...)
54 nextop
= va_arg(args
, int);
55 } while (nextop
!= 0 && nextop
!= op
);
61 static struct token
*comma_expression(struct token
*, struct expression
**);
63 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
67 token
= expect(token
, '(', where
);
69 if (match_op(token
, '{')) {
70 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
71 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
74 start_symbol_scope(e
->pos
);
75 token
= compound_statement(token
->next
, stmt
);
77 token
= expect(token
, '}', "at end of statement expression");
79 token
= parse_expression(token
, expr
);
82 sparse_error(token
->pos
, "an expression is expected before ')'");
83 return expect(token
, ')', where
);
87 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
90 static struct symbol
*handle_func(struct token
*token
)
92 struct ident
*ident
= token
->ident
;
93 struct symbol
*decl
, *array
;
94 struct string
*string
;
97 if (ident
!= &__func___ident
&&
98 ident
!= &__FUNCTION___ident
&&
99 ident
!= &__PRETTY_FUNCTION___ident
)
102 if (!current_fn
|| !current_fn
->ident
)
105 /* OK, it's one of ours */
106 array
= alloc_symbol(token
->pos
, SYM_ARRAY
);
107 array
->ctype
.base_type
= &char_ctype
;
108 array
->ctype
.alignment
= 1;
109 array
->endpos
= token
->pos
;
110 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
111 decl
->ctype
.base_type
= array
;
112 decl
->ctype
.alignment
= 1;
113 decl
->ctype
.modifiers
= MOD_STATIC
;
114 decl
->endpos
= token
->pos
;
116 /* function-scope, but in NS_SYMBOL */
117 bind_symbol(decl
, ident
, NS_LABEL
);
118 decl
->namespace = NS_SYMBOL
;
120 len
= current_fn
->ident
->len
;
121 string
= __alloc_string(len
+ 1);
122 memcpy(string
->data
, current_fn
->ident
->name
, len
);
123 string
->data
[len
] = 0;
124 string
->length
= len
+ 1;
126 decl
->initializer
= alloc_expression(token
->pos
, EXPR_STRING
);
127 decl
->initializer
->string
= string
;
128 decl
->initializer
->ctype
= decl
;
129 decl
->array_size
= alloc_const_expression(token
->pos
, len
+ 1);
130 array
->array_size
= decl
->array_size
;
131 decl
->bit_size
= array
->bit_size
= bytes_to_bits(len
+ 1);
136 static struct token
*parse_type(struct token
*token
, struct expression
**tree
)
139 *tree
= alloc_expression(token
->pos
, EXPR_TYPE
);
140 token
= typename(token
, &sym
, NULL
);
142 sparse_error(token
->pos
,
143 "type expression should not include identifier "
144 "\"%s\"", sym
->ident
->name
);
145 (*tree
)->symbol
= sym
;
149 static struct token
*builtin_types_compatible_p_expr(struct token
*token
,
150 struct expression
**tree
)
152 struct expression
*expr
= alloc_expression(
153 token
->pos
, EXPR_COMPARE
);
154 expr
->op
= SPECIAL_EQUAL
;
156 if (!match_op(token
, '('))
157 return expect(token
, '(',
158 "after __builtin_types_compatible_p");
160 token
= parse_type(token
, &expr
->left
);
161 if (!match_op(token
, ','))
162 return expect(token
, ',',
163 "in __builtin_types_compatible_p");
165 token
= parse_type(token
, &expr
->right
);
166 if (!match_op(token
, ')'))
167 return expect(token
, ')',
168 "at end of __builtin_types_compatible_p");
175 static struct token
*builtin_offsetof_expr(struct token
*token
,
176 struct expression
**tree
)
178 struct expression
*expr
= NULL
;
179 struct expression
**p
= &expr
;
184 if (!match_op(token
, '('))
185 return expect(token
, '(', "after __builtin_offset");
188 token
= typename(token
, &sym
, NULL
);
190 sparse_error(token
->pos
,
191 "type expression should not include identifier "
192 "\"%s\"", sym
->ident
->name
);
194 if (!match_op(token
, ','))
195 return expect(token
, ',', "in __builtin_offset");
198 struct expression
*e
;
204 return expect(token
, ')', "at end of __builtin_offset");
205 case SPECIAL_DEREFERENCE
:
206 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
213 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
215 if (token_type(token
) != TOKEN_IDENT
) {
216 sparse_error(token
->pos
, "Expected member name");
219 e
->ident
= token
->ident
;
224 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
226 token
= parse_expression(token
, &e
->index
);
227 token
= expect(token
, ']',
228 "at end of array dereference");
234 op
= token_type(token
) == TOKEN_SPECIAL
? token
->special
: 0;
239 #define ULLONG_MAX (~0ULL)
242 static unsigned long long parse_num(const char *nptr
, char **end
)
244 if (nptr
[0] == '0' && tolower((unsigned char)nptr
[1]) == 'b')
245 return strtoull(&nptr
[2], end
, 2);
246 return strtoull(nptr
, end
, 0);
249 static void get_number_value(struct expression
*expr
, struct token
*token
)
251 const char *str
= token
->number
;
252 unsigned long long value
;
254 int size
= 0, want_unsigned
= 0;
255 int overflow
= 0, do_warn
= 0;
256 int try_unsigned
= 1;
260 value
= parse_num(str
, &end
);
263 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
269 } else if (c
== 'u' || c
== 'U') {
273 } else if (c
== 'l' || c
== 'L') {
286 /* OK, it's a valid integer */
287 /* decimals can be unsigned only if directly specified as such */
288 if (str
[0] != '0' && !want_unsigned
)
291 bits
= bits_in_int
- 1;
292 if (!(value
& (~1ULL << bits
))) {
293 if (!(value
& (1ULL << bits
))) {
295 } else if (try_unsigned
) {
304 bits
= bits_in_long
- 1;
305 if (!(value
& (~1ULL << bits
))) {
306 if (!(value
& (1ULL << bits
))) {
308 } else if (try_unsigned
) {
317 bits
= bits_in_longlong
- 1;
318 if (value
& (~1ULL << bits
))
320 if (!(value
& (1ULL << bits
)))
323 warning(expr
->pos
, "decimal constant %s is too big for long long",
327 if (do_warn
&& Wconstant_suffix
)
328 warning(expr
->pos
, "constant %s is so big it is%s%s%s",
330 want_unsigned
? " unsigned":"",
331 size
> 0 ? " long":"",
332 size
> 1 ? " long":"");
335 "decimal constant %s is between LONG_MAX and ULONG_MAX."
336 " For C99 that means long long, C90 compilers are very "
337 "likely to produce unsigned long (and a warning) here",
339 expr
->type
= EXPR_VALUE
;
340 expr
->flags
= CEF_SET_INT
;
341 expr
->ctype
= ctype_integer(size
, want_unsigned
);
345 error_die(expr
->pos
, "constant %s is too big even for unsigned long long",
349 expr
->fvalue
= string_to_ld(str
, &end
);
356 if (*end
== 'f' || *end
== 'F')
357 expr
->ctype
= &float_ctype
;
358 else if (*end
== 'l' || *end
== 'L')
359 expr
->ctype
= &ldouble_ctype
;
361 expr
->ctype
= &double_ctype
;
365 expr
->flags
= CEF_SET_FLOAT
;
366 expr
->type
= EXPR_FVALUE
;
370 error_die(expr
->pos
, "constant %s is not a valid number", show_token(token
));
373 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
375 struct expression
*expr
= NULL
;
377 switch (token_type(token
)) {
378 case TOKEN_CHAR
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
379 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
380 expr
->flags
= CEF_SET_CHAR
;
381 expr
->ctype
= token_type(token
) < TOKEN_WIDE_CHAR
? &int_ctype
: &long_ctype
;
382 get_char_constant(token
, &expr
->value
);
387 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
388 get_number_value(expr
, token
); /* will see if it's an integer */
392 case TOKEN_ZERO_IDENT
: {
393 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
394 expr
->flags
= CEF_SET_INT
;
395 expr
->ctype
= &int_ctype
;
396 expr
->symbol
= &zero_int
;
397 expr
->symbol_name
= token
->ident
;
403 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
404 struct token
*next
= token
->next
;
407 sym
= handle_func(token
);
408 if (token
->ident
== &__builtin_types_compatible_p_ident
) {
409 token
= builtin_types_compatible_p_expr(token
, &expr
);
412 if (token
->ident
== &__builtin_offsetof_ident
) {
413 token
= builtin_offsetof_expr(token
, &expr
);
416 } else if (sym
->enum_member
) {
417 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
418 *expr
= *sym
->initializer
;
419 /* we want the right position reported, thus the copy */
420 expr
->pos
= token
->pos
;
421 expr
->flags
= CEF_SET_ENUM
;
426 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
429 * We support types as real first-class citizens, with type
432 * if (typeof(a) == int) ..
434 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
435 sparse_error(token
->pos
, "typename in expression");
438 expr
->symbol_name
= token
->ident
;
442 * A pointer to an lvalue designating a static storage
443 * duration object is an address constant [6.6(9)].
445 if (sym
&& (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_STATIC
)))
446 expr
->flags
= CEF_ADDR
;
453 case TOKEN_WIDE_STRING
:
454 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
455 token
= get_string_constant(token
, expr
);
459 if (token
->special
== '(') {
460 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
462 token
= parens_expression(token
, &expr
->unop
, "in expression");
465 if (token
->special
== '[' && lookup_type(token
->next
)) {
466 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
467 token
= typename(token
->next
, &expr
->symbol
, NULL
);
468 token
= expect(token
, ']', "in type expression");
479 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
481 while (!match_op(token
, ')')) {
482 struct expression
*expr
= NULL
;
483 token
= assignment_expression(token
, &expr
);
486 add_expression(list
, expr
);
487 if (!match_op(token
, ','))
495 * extend to deal with the ambiguous C grammar for parsing
496 * a cast expressions followed by an initializer.
498 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
500 struct expression
*expr
= cast_init_expr
;
503 token
= primary_expression(token
, &expr
);
505 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
506 switch (token
->special
) {
507 case '[': { /* Array dereference */
508 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
509 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
516 token
= parse_expression(token
->next
, &add
->right
);
517 token
= expect(token
, ']', "at end of array dereference");
521 case SPECIAL_INCREMENT
: /* Post-increment */
522 case SPECIAL_DECREMENT
: { /* Post-decrement */
523 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
524 post
->op
= token
->special
;
530 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
531 /* "x->y" is just shorthand for "(*x).y" */
532 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
538 case '.': { /* Structure member dereference */
539 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
543 if (token_type(token
) != TOKEN_IDENT
) {
544 sparse_error(token
->pos
, "Expected member name");
547 deref
->member
= token
->ident
;
548 deref
->member_offset
= -1;
554 case '(': { /* Function call */
555 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
558 token
= expression_list(token
->next
, &call
->args
);
559 token
= expect(token
, ')', "in function call");
573 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
574 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
);
576 static struct token
*type_info_expression(struct token
*token
,
577 struct expression
**tree
, int type
)
579 struct expression
*expr
= alloc_expression(token
->pos
, type
);
583 expr
->flags
= CEF_SET_ICE
; /* XXX: VLA support will need that changed */
585 if (!match_op(token
, '(') || !lookup_type(token
->next
))
586 return unary_expression(token
, &expr
->cast_expression
);
588 token
= typename(token
->next
, &expr
->cast_type
, NULL
);
590 if (!match_op(token
, ')')) {
591 static const char * error
[] = {
592 [EXPR_SIZEOF
] = "at end of sizeof",
593 [EXPR_ALIGNOF
] = "at end of __alignof__",
594 [EXPR_PTRSIZEOF
] = "at end of __sizeof_ptr__"
596 return expect(token
, ')', error
[type
]);
601 * C99 ambiguity: the typename might have been the beginning
602 * of a typed initializer expression..
604 if (match_op(token
, '{')) {
605 struct expression
*cast
= alloc_expression(p
->pos
, EXPR_CAST
);
606 cast
->cast_type
= expr
->cast_type
;
607 expr
->cast_type
= NULL
;
608 expr
->cast_expression
= cast
;
609 token
= initializer(&cast
->cast_expression
, token
);
610 token
= postfix_expression(token
, &expr
->cast_expression
, cast
);
615 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
617 if (token_type(token
) == TOKEN_IDENT
) {
618 struct ident
*ident
= token
->ident
;
619 if (ident
->reserved
) {
620 static const struct {
623 } type_information
[] = {
624 { &sizeof_ident
, EXPR_SIZEOF
},
625 { &__alignof___ident
, EXPR_ALIGNOF
},
626 { &__alignof_ident
, EXPR_ALIGNOF
},
627 { &_Alignof_ident
, EXPR_ALIGNOF
},
628 { &__sizeof_ptr___ident
, EXPR_PTRSIZEOF
},
631 for (i
= 0; i
< ARRAY_SIZE(type_information
); i
++) {
632 if (ident
== type_information
[i
].id
)
633 return type_info_expression(token
, tree
, type_information
[i
].type
);
638 if (token_type(token
) == TOKEN_SPECIAL
) {
639 if (match_oplist(token
->special
,
640 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
642 struct expression
*unop
;
643 struct expression
*unary
;
646 next
= cast_expression(token
->next
, &unop
);
648 sparse_error(token
->pos
, "Syntax error in unary expression");
652 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
653 unary
->op
= token
->special
;
658 /* possibly constant ones */
659 if (match_oplist(token
->special
, '+', '-', '~', '!', 0)) {
660 struct expression
*unop
;
661 struct expression
*unary
;
664 next
= cast_expression(token
->next
, &unop
);
666 sparse_error(token
->pos
, "Syntax error in unary expression");
670 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
671 unary
->op
= token
->special
;
676 /* Gcc extension: &&label gives the address of a label */
677 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
678 token_type(token
->next
) == TOKEN_IDENT
) {
679 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
680 struct symbol
*sym
= label_symbol(token
->next
);
681 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
)) {
682 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
683 add_symbol(&function_computed_target_list
, sym
);
685 label
->flags
= CEF_ADDR
;
686 label
->label_symbol
= sym
;
688 return token
->next
->next
;
693 return postfix_expression(token
, tree
, NULL
);
697 * Ambiguity: a '(' can be either a cast-expression or
698 * a primary-expression depending on whether it is followed
701 * additional ambiguity: a "cast expression" followed by
702 * an initializer is really a postfix-expression.
704 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
706 if (match_op(token
, '(')) {
707 struct token
*next
= token
->next
;
708 if (lookup_type(next
)) {
709 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
710 struct expression
*v
;
714 token
= typename(next
, &sym
, &is_force
);
715 cast
->cast_type
= sym
;
716 token
= expect(token
, ')', "at end of cast operator");
717 if (match_op(token
, '{')) {
718 if (toplevel(block_scope
))
719 sym
->ctype
.modifiers
|= MOD_TOPLEVEL
;
722 "[force] in compound literal");
723 token
= initializer(&cast
->cast_expression
, token
);
724 return postfix_expression(token
, tree
, cast
);
728 cast
->type
= EXPR_FORCE_CAST
;
729 token
= cast_expression(token
, &v
);
732 cast
->cast_expression
= v
;
736 return unary_expression(token
, tree
);
740 * Generic left-to-right binop parsing
742 * This _really_ needs to be inlined, because that makes the inner
743 * function call statically deterministic rather than a totally
744 * unpredictable indirect call. But gcc-3 is so "clever" that it
745 * doesn't do so by default even when you tell it to inline it.
747 * Making it a macro avoids the inlining problem, and also means
748 * that we can pass in the op-comparison as an expression rather
749 * than create a data structure for it.
752 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
753 struct expression *left = NULL; \
754 struct token * next = inner(__token, &left); \
757 while (token_type(next) == TOKEN_SPECIAL) { \
758 struct expression *top, *right = NULL; \
759 int op = next->special; \
763 top = alloc_expression(next->pos, type); \
764 next = inner(next->next, &right); \
766 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
771 top->right = right; \
779 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
782 token
, tree
, EXPR_BINOP
, cast_expression
,
783 (op
== '*') || (op
== '/') || (op
== '%')
787 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
790 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
791 (op
== '+') || (op
== '-')
795 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
798 token
, tree
, EXPR_BINOP
, additive_expression
,
799 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
803 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
806 token
, tree
, EXPR_COMPARE
, shift_expression
,
807 (op
== '<') || (op
== '>') ||
808 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
812 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
815 token
, tree
, EXPR_COMPARE
, relational_expression
,
816 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
820 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
823 token
, tree
, EXPR_BINOP
, equality_expression
,
828 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
831 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
836 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
839 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
844 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
847 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
848 (op
== SPECIAL_LOGICAL_AND
)
852 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
855 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
856 (op
== SPECIAL_LOGICAL_OR
)
860 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
862 token
= logical_or_expression(token
, tree
);
863 if (*tree
&& match_op(token
, '?')) {
864 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
865 expr
->op
= token
->special
;
868 token
= parse_expression(token
->next
, &expr
->cond_true
);
869 token
= expect(token
, ':', "in conditional expression");
870 token
= conditional_expression(token
, &expr
->cond_false
);
875 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
877 token
= conditional_expression(token
, tree
);
878 if (*tree
&& token_type(token
) == TOKEN_SPECIAL
) {
879 static const int assignments
[] = {
881 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
882 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
883 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
884 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
885 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
886 int i
, op
= token
->special
;
887 for (i
= 0; i
< ARRAY_SIZE(assignments
); i
++)
888 if (assignments
[i
] == op
) {
889 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
893 return assignment_expression(token
->next
, &expr
->right
);
899 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
902 token
, tree
, EXPR_COMMA
, assignment_expression
,
907 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
909 return comma_expression(token
,tree
);