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
)
65 token
= expect(token
, '(', where
);
66 if (match_op(token
, '{')) {
67 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
68 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
71 start_symbol_scope(e
->pos
);
72 token
= compound_statement(token
->next
, stmt
);
74 token
= expect(token
, '}', "at end of statement expression");
76 token
= parse_expression(token
, expr
);
77 return expect(token
, ')', where
);
81 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
84 static struct symbol
*handle_func(struct token
*token
)
86 struct ident
*ident
= token
->ident
;
87 struct symbol
*decl
, *array
;
88 struct string
*string
;
91 if (ident
!= &__func___ident
&&
92 ident
!= &__FUNCTION___ident
&&
93 ident
!= &__PRETTY_FUNCTION___ident
)
96 if (!current_fn
|| !current_fn
->ident
)
99 /* OK, it's one of ours */
100 array
= alloc_symbol(token
->pos
, SYM_ARRAY
);
101 array
->ctype
.base_type
= &char_ctype
;
102 array
->ctype
.alignment
= 1;
103 array
->endpos
= token
->pos
;
104 decl
= alloc_symbol(token
->pos
, SYM_NODE
);
105 decl
->ctype
.base_type
= array
;
106 decl
->ctype
.alignment
= 1;
107 decl
->ctype
.modifiers
= MOD_STATIC
;
108 decl
->endpos
= token
->pos
;
110 /* function-scope, but in NS_SYMBOL */
111 bind_symbol(decl
, ident
, NS_LABEL
);
112 decl
->namespace = NS_SYMBOL
;
114 len
= current_fn
->ident
->len
;
115 string
= __alloc_string(len
+ 1);
116 memcpy(string
->data
, current_fn
->ident
->name
, len
);
117 string
->data
[len
] = 0;
118 string
->length
= len
+ 1;
120 decl
->initializer
= alloc_expression(token
->pos
, EXPR_STRING
);
121 decl
->initializer
->string
= string
;
122 decl
->initializer
->ctype
= decl
;
123 decl
->array_size
= alloc_const_expression(token
->pos
, len
+ 1);
124 array
->array_size
= decl
->array_size
;
125 decl
->bit_size
= array
->bit_size
= bytes_to_bits(len
+ 1);
130 static struct token
*parse_type(struct token
*token
, struct expression
**tree
)
133 *tree
= alloc_expression(token
->pos
, EXPR_TYPE
);
134 token
= typename(token
, &sym
, NULL
);
136 sparse_error(token
->pos
,
137 "type expression should not include identifier "
138 "\"%s\"", sym
->ident
->name
);
139 (*tree
)->symbol
= sym
;
143 static struct token
*builtin_types_compatible_p_expr(struct token
*token
,
144 struct expression
**tree
)
146 struct expression
*expr
= alloc_expression(
147 token
->pos
, EXPR_COMPARE
);
148 expr
->op
= SPECIAL_EQUAL
;
150 if (!match_op(token
, '('))
151 return expect(token
, '(',
152 "after __builtin_types_compatible_p");
154 token
= parse_type(token
, &expr
->left
);
155 if (!match_op(token
, ','))
156 return expect(token
, ',',
157 "in __builtin_types_compatible_p");
159 token
= parse_type(token
, &expr
->right
);
160 if (!match_op(token
, ')'))
161 return expect(token
, ')',
162 "at end of __builtin_types_compatible_p");
169 static struct token
*builtin_offsetof_expr(struct token
*token
,
170 struct expression
**tree
)
172 struct expression
*expr
= NULL
;
173 struct expression
**p
= &expr
;
178 if (!match_op(token
, '('))
179 return expect(token
, '(', "after __builtin_offset");
182 token
= typename(token
, &sym
, NULL
);
184 sparse_error(token
->pos
,
185 "type expression should not include identifier "
186 "\"%s\"", sym
->ident
->name
);
188 if (!match_op(token
, ','))
189 return expect(token
, ',', "in __builtin_offset");
192 struct expression
*e
;
198 return expect(token
, ')', "at end of __builtin_offset");
199 case SPECIAL_DEREFERENCE
:
200 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
207 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
209 if (token_type(token
) != TOKEN_IDENT
) {
210 sparse_error(token
->pos
, "Expected member name");
213 e
->ident
= token
->ident
;
218 e
= alloc_expression(token
->pos
, EXPR_OFFSETOF
);
220 token
= parse_expression(token
, &e
->index
);
221 token
= expect(token
, ']',
222 "at end of array dereference");
228 op
= token_type(token
) == TOKEN_SPECIAL
? token
->special
: 0;
233 #define ULLONG_MAX (~0ULL)
236 static unsigned long long parse_num(const char *nptr
, char **end
)
238 if (nptr
[0] == '0' && tolower((unsigned char)nptr
[1]) == 'b')
239 return strtoull(&nptr
[2], end
, 2);
240 return strtoull(nptr
, end
, 0);
243 static void get_number_value(struct expression
*expr
, struct token
*token
)
245 const char *str
= token
->number
;
246 unsigned long long value
;
248 int size
= 0, want_unsigned
= 0;
249 int overflow
= 0, do_warn
= 0;
250 int try_unsigned
= 1;
254 value
= parse_num(str
, &end
);
257 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
263 } else if (c
== 'u' || c
== 'U') {
267 } else if (c
== 'l' || c
== 'L') {
280 /* OK, it's a valid integer */
281 /* decimals can be unsigned only if directly specified as such */
282 if (str
[0] != '0' && !want_unsigned
)
285 bits
= bits_in_int
- 1;
286 if (!(value
& (~1ULL << bits
))) {
287 if (!(value
& (1ULL << bits
))) {
289 } else if (try_unsigned
) {
298 bits
= bits_in_long
- 1;
299 if (!(value
& (~1ULL << bits
))) {
300 if (!(value
& (1ULL << bits
))) {
302 } else if (try_unsigned
) {
311 bits
= bits_in_longlong
- 1;
312 if (value
& (~1ULL << bits
))
314 if (!(value
& (1ULL << bits
)))
317 warning(expr
->pos
, "decimal constant %s is too big for long long",
322 warning(expr
->pos
, "constant %s is so big it is%s%s%s",
324 want_unsigned
? " unsigned":"",
325 size
> 0 ? " long":"",
326 size
> 1 ? " long":"");
329 "decimal constant %s is between LONG_MAX and ULONG_MAX."
330 " For C99 that means long long, C90 compilers are very "
331 "likely to produce unsigned long (and a warning) here",
333 expr
->type
= EXPR_VALUE
;
334 expr
->flags
= CEF_SET_INT
;
335 expr
->ctype
= ctype_integer(size
, want_unsigned
);
339 error_die(expr
->pos
, "constant %s is too big even for unsigned long long",
343 expr
->fvalue
= string_to_ld(str
, &end
);
350 if (*end
== 'f' || *end
== 'F')
351 expr
->ctype
= &float_ctype
;
352 else if (*end
== 'l' || *end
== 'L')
353 expr
->ctype
= &ldouble_ctype
;
355 expr
->ctype
= &double_ctype
;
359 expr
->flags
= CEF_SET_FLOAT
;
360 expr
->type
= EXPR_FVALUE
;
364 error_die(expr
->pos
, "constant %s is not a valid number", show_token(token
));
367 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
369 struct expression
*expr
= NULL
;
371 switch (token_type(token
)) {
372 case TOKEN_CHAR
... TOKEN_WIDE_CHAR_EMBEDDED_3
:
373 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
374 expr
->flags
= CEF_SET_CHAR
;
375 expr
->ctype
= token_type(token
) < TOKEN_WIDE_CHAR
? &int_ctype
: &long_ctype
;
376 get_char_constant(token
, &expr
->value
);
381 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
382 get_number_value(expr
, token
); /* will see if it's an integer */
386 case TOKEN_ZERO_IDENT
: {
387 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
388 expr
->flags
= CEF_SET_INT
;
389 expr
->ctype
= &int_ctype
;
390 expr
->symbol
= &zero_int
;
391 expr
->symbol_name
= token
->ident
;
397 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
398 struct token
*next
= token
->next
;
401 sym
= handle_func(token
);
402 if (token
->ident
== &__builtin_types_compatible_p_ident
) {
403 token
= builtin_types_compatible_p_expr(token
, &expr
);
406 if (token
->ident
== &__builtin_offsetof_ident
) {
407 token
= builtin_offsetof_expr(token
, &expr
);
410 } else if (sym
->enum_member
) {
411 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
412 *expr
= *sym
->initializer
;
413 /* we want the right position reported, thus the copy */
414 expr
->pos
= token
->pos
;
415 expr
->flags
= CEF_SET_ENUM
;
420 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
423 * We support types as real first-class citizens, with type
426 * if (typeof(a) == int) ..
428 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
429 sparse_error(token
->pos
, "typename in expression");
432 expr
->symbol_name
= token
->ident
;
436 * A pointer to an lvalue designating a static storage
437 * duration object is an address constant [6.6(9)].
439 if (sym
&& (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_STATIC
)))
440 expr
->flags
= CEF_ADDR
;
447 case TOKEN_WIDE_STRING
:
448 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
449 token
= get_string_constant(token
, expr
);
453 if (token
->special
== '(') {
454 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
456 token
= parens_expression(token
, &expr
->unop
, "in expression");
459 if (token
->special
== '[' && lookup_type(token
->next
)) {
460 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
461 token
= typename(token
->next
, &expr
->symbol
, NULL
);
462 token
= expect(token
, ']', "in type expression");
473 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
475 while (!match_op(token
, ')')) {
476 struct expression
*expr
= NULL
;
477 token
= assignment_expression(token
, &expr
);
480 add_expression(list
, expr
);
481 if (!match_op(token
, ','))
489 * extend to deal with the ambiguous C grammar for parsing
490 * a cast expressions followed by an initializer.
492 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
494 struct expression
*expr
= cast_init_expr
;
497 token
= primary_expression(token
, &expr
);
499 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
500 switch (token
->special
) {
501 case '[': { /* Array dereference */
502 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
503 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
510 token
= parse_expression(token
->next
, &add
->right
);
511 token
= expect(token
, ']', "at end of array dereference");
515 case SPECIAL_INCREMENT
: /* Post-increment */
516 case SPECIAL_DECREMENT
: { /* Post-decrement */
517 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
518 post
->op
= token
->special
;
524 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
525 /* "x->y" is just shorthand for "(*x).y" */
526 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
532 case '.': { /* Structure member dereference */
533 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
537 if (token_type(token
) != TOKEN_IDENT
) {
538 sparse_error(token
->pos
, "Expected member name");
541 deref
->member
= token
->ident
;
542 deref
->member_offset
= -1;
548 case '(': { /* Function call */
549 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
552 token
= expression_list(token
->next
, &call
->args
);
553 token
= expect(token
, ')', "in function call");
567 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
568 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
);
570 static struct token
*type_info_expression(struct token
*token
,
571 struct expression
**tree
, int type
)
573 struct expression
*expr
= alloc_expression(token
->pos
, type
);
577 expr
->flags
= CEF_SET_ICE
; /* XXX: VLA support will need that changed */
579 if (!match_op(token
, '(') || !lookup_type(token
->next
))
580 return unary_expression(token
, &expr
->cast_expression
);
582 token
= typename(token
->next
, &expr
->cast_type
, NULL
);
584 if (!match_op(token
, ')')) {
585 static const char * error
[] = {
586 [EXPR_SIZEOF
] = "at end of sizeof",
587 [EXPR_ALIGNOF
] = "at end of __alignof__",
588 [EXPR_PTRSIZEOF
] = "at end of __sizeof_ptr__"
590 return expect(token
, ')', error
[type
]);
595 * C99 ambiguity: the typename might have been the beginning
596 * of a typed initializer expression..
598 if (match_op(token
, '{')) {
599 struct expression
*cast
= alloc_expression(p
->pos
, EXPR_CAST
);
600 cast
->cast_type
= expr
->cast_type
;
601 expr
->cast_type
= NULL
;
602 expr
->cast_expression
= cast
;
603 token
= initializer(&cast
->cast_expression
, token
);
604 token
= postfix_expression(token
, &expr
->cast_expression
, cast
);
609 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
611 if (token_type(token
) == TOKEN_IDENT
) {
612 struct ident
*ident
= token
->ident
;
613 if (ident
->reserved
) {
614 static const struct {
617 } type_information
[] = {
618 { &sizeof_ident
, EXPR_SIZEOF
},
619 { &__alignof___ident
, EXPR_ALIGNOF
},
620 { &__alignof_ident
, EXPR_ALIGNOF
},
621 { &_Alignof_ident
, EXPR_ALIGNOF
},
622 { &__sizeof_ptr___ident
, EXPR_PTRSIZEOF
},
625 for (i
= 0; i
< ARRAY_SIZE(type_information
); i
++) {
626 if (ident
== type_information
[i
].id
)
627 return type_info_expression(token
, tree
, type_information
[i
].type
);
632 if (token_type(token
) == TOKEN_SPECIAL
) {
633 if (match_oplist(token
->special
,
634 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
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
;
652 /* possibly constant ones */
653 if (match_oplist(token
->special
, '+', '-', '~', '!', 0)) {
654 struct expression
*unop
;
655 struct expression
*unary
;
658 next
= cast_expression(token
->next
, &unop
);
660 sparse_error(token
->pos
, "Syntax error in unary expression");
664 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
665 unary
->op
= token
->special
;
670 /* Gcc extension: &&label gives the address of a label */
671 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
672 token_type(token
->next
) == TOKEN_IDENT
) {
673 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
674 struct symbol
*sym
= label_symbol(token
->next
);
675 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
)) {
676 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
677 add_symbol(&function_computed_target_list
, sym
);
679 label
->flags
= CEF_ADDR
;
680 label
->label_symbol
= sym
;
682 return token
->next
->next
;
687 return postfix_expression(token
, tree
, NULL
);
691 * Ambiguity: a '(' can be either a cast-expression or
692 * a primary-expression depending on whether it is followed
695 * additional ambiguity: a "cast expression" followed by
696 * an initializer is really a postfix-expression.
698 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
700 if (match_op(token
, '(')) {
701 struct token
*next
= token
->next
;
702 if (lookup_type(next
)) {
703 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
704 struct expression
*v
;
708 token
= typename(next
, &sym
, &is_force
);
709 cast
->cast_type
= sym
;
710 token
= expect(token
, ')', "at end of cast operator");
711 if (match_op(token
, '{')) {
712 if (toplevel(block_scope
))
713 sym
->ctype
.modifiers
|= MOD_TOPLEVEL
;
716 "[force] in compound literal");
717 token
= initializer(&cast
->cast_expression
, token
);
718 return postfix_expression(token
, tree
, cast
);
722 cast
->type
= EXPR_FORCE_CAST
;
723 token
= cast_expression(token
, &v
);
726 cast
->cast_expression
= v
;
730 return unary_expression(token
, tree
);
734 * Generic left-to-right binop parsing
736 * This _really_ needs to be inlined, because that makes the inner
737 * function call statically deterministic rather than a totally
738 * unpredictable indirect call. But gcc-3 is so "clever" that it
739 * doesn't do so by default even when you tell it to inline it.
741 * Making it a macro avoids the inlining problem, and also means
742 * that we can pass in the op-comparison as an expression rather
743 * than create a data structure for it.
746 #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
747 struct expression *left = NULL; \
748 struct token * next = inner(__token, &left); \
751 while (token_type(next) == TOKEN_SPECIAL) { \
752 struct expression *top, *right = NULL; \
753 int op = next->special; \
757 top = alloc_expression(next->pos, type); \
758 next = inner(next->next, &right); \
760 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
765 top->right = right; \
773 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
776 token
, tree
, EXPR_BINOP
, cast_expression
,
777 (op
== '*') || (op
== '/') || (op
== '%')
781 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
784 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
785 (op
== '+') || (op
== '-')
789 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
792 token
, tree
, EXPR_BINOP
, additive_expression
,
793 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
797 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
800 token
, tree
, EXPR_COMPARE
, shift_expression
,
801 (op
== '<') || (op
== '>') ||
802 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
806 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
809 token
, tree
, EXPR_COMPARE
, relational_expression
,
810 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
814 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
817 token
, tree
, EXPR_BINOP
, equality_expression
,
822 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
825 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
830 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
833 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
838 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
841 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
842 (op
== SPECIAL_LOGICAL_AND
)
846 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
849 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
850 (op
== SPECIAL_LOGICAL_OR
)
854 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
856 token
= logical_or_expression(token
, tree
);
857 if (*tree
&& match_op(token
, '?')) {
858 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
859 expr
->op
= token
->special
;
862 token
= parse_expression(token
->next
, &expr
->cond_true
);
863 token
= expect(token
, ':', "in conditional expression");
864 token
= conditional_expression(token
, &expr
->cond_false
);
869 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
871 token
= conditional_expression(token
, tree
);
872 if (*tree
&& token_type(token
) == TOKEN_SPECIAL
) {
873 static const int assignments
[] = {
875 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
876 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
877 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
878 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
879 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
880 int i
, op
= token
->special
;
881 for (i
= 0; i
< ARRAY_SIZE(assignments
); i
++)
882 if (assignments
[i
] == op
) {
883 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
887 return assignment_expression(token
->next
, &expr
->right
);
893 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
896 token
, tree
, EXPR_COMMA
, assignment_expression
,
901 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
903 return comma_expression(token
,tree
);