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
, ...)
36 int nextop
= va_arg(args
, int);
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 token
= typename(token
, &sym
);
122 sparse_error(token
->pos
,
123 "type expression should not include identifier "
124 "\"%s\"", sym
->ident
->name
);
125 (*tree
)->symbol
= sym
;
129 static struct token
*builtin_types_compatible_p_expr(struct token
*token
,
130 struct expression
**tree
)
132 struct expression
*expr
= alloc_expression(
133 token
->pos
, EXPR_COMPARE
);
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
*string_expression(struct token
*token
, struct expression
*expr
)
157 struct string
*string
= token
->string
;
158 struct token
*next
= token
->next
;
160 convert_function(token
);
162 if (token_type(next
) == TOKEN_STRING
) {
163 int totlen
= string
->length
-1;
167 totlen
+= next
->string
->length
-1;
169 } while (token_type(next
) == TOKEN_STRING
);
171 if (totlen
> MAX_STRING
) {
172 warning(token
->pos
, "trying to concatenate %d-character string (%d bytes max)", totlen
, MAX_STRING
);
176 string
= __alloc_string(totlen
+1);
177 string
->length
= totlen
+1;
181 struct string
*s
= next
->string
;
182 int len
= s
->length
-1;
189 memcpy(data
, s
->data
, len
);
191 } while (token_type(next
) == TOKEN_STRING
);
194 expr
->string
= string
;
199 #define ULLONG_MAX (~0ULL)
202 static void get_number_value(struct expression
*expr
, struct token
*token
)
204 const char *str
= token
->number
;
205 unsigned long long value
;
207 unsigned long modifiers
= 0;
208 int overflow
= 0, do_warn
= 0;
209 int try_unsigned
= 1;
213 value
= strtoull(str
, &end
, 0);
216 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
223 } else if (c
== 'u' || c
== 'U') {
224 added
= MOD_UNSIGNED
;
225 } else if (c
== 'l' || c
== 'L') {
228 added
|= MOD_LONGLONG
;
233 if (modifiers
& added
)
239 /* OK, it's a valid integer */
240 /* decimals can be unsigned only if directly specified as such */
241 if (str
[0] != '0' && !(modifiers
& MOD_UNSIGNED
))
243 if (!(modifiers
& MOD_LONG
)) {
244 bits
= bits_in_int
- 1;
245 if (!(value
& (~1ULL << bits
))) {
246 if (!(value
& (1ULL << bits
))) {
248 } else if (try_unsigned
) {
249 modifiers
|= MOD_UNSIGNED
;
253 modifiers
|= MOD_LONG
;
256 if (!(modifiers
& MOD_LONGLONG
)) {
257 bits
= bits_in_long
- 1;
258 if (!(value
& (~1ULL << bits
))) {
259 if (!(value
& (1ULL << bits
))) {
261 } else if (try_unsigned
) {
262 modifiers
|= MOD_UNSIGNED
;
267 modifiers
|= MOD_LONGLONG
;
270 bits
= bits_in_longlong
- 1;
271 if (value
& (~1ULL << bits
))
273 if (!(value
& (1ULL << bits
)))
276 warning(expr
->pos
, "decimal constant %s is too big for long long",
278 modifiers
|= MOD_UNSIGNED
;
281 warning(expr
->pos
, "constant %s is so big it is%s%s%s",
283 (modifiers
& MOD_UNSIGNED
) ? " unsigned":"",
284 (modifiers
& MOD_LONG
) ? " long":"",
285 (modifiers
& MOD_LONGLONG
) ? " long":"");
288 "decimal constant %s is between LONG_MAX and ULONG_MAX."
289 " For C99 that means long long, C90 compilers are very "
290 "likely to produce unsigned long (and a warning) here",
292 expr
->type
= EXPR_VALUE
;
293 expr
->ctype
= ctype_integer(modifiers
);
297 error_die(expr
->pos
, "constant %s is too big even for unsigned long long",
301 expr
->fvalue
= string_to_ld(str
, &end
);
308 if (*end
== 'f' || *end
== 'F')
309 expr
->ctype
= &float_ctype
;
310 else if (*end
== 'l' || *end
== 'L')
311 expr
->ctype
= &ldouble_ctype
;
313 expr
->ctype
= &double_ctype
;
317 expr
->type
= EXPR_FVALUE
;
321 error_die(expr
->pos
, "constant %s is not a valid number", show_token(token
));
324 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
326 struct expression
*expr
= NULL
;
328 switch (token_type(token
)) {
330 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
331 expr
->ctype
= &int_ctype
;
332 expr
->value
= (unsigned char) token
->character
;
337 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
338 get_number_value(expr
, token
);
342 case TOKEN_ZERO_IDENT
: {
343 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
344 expr
->ctype
= &int_ctype
;
345 expr
->symbol
= &zero_int
;
346 expr
->symbol_name
= token
->ident
;
352 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
353 struct token
*next
= token
->next
;
356 if (convert_function(token
))
358 if (token
->ident
== &__builtin_types_compatible_p_ident
) {
359 token
= builtin_types_compatible_p_expr(token
, &expr
);
364 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
367 * We support types as real first-class citizens, with type
370 * if (typeof(a) == int) ..
372 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
373 sparse_error(token
->pos
, "typename in expression");
376 expr
->symbol_name
= token
->ident
;
384 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
385 token
= string_expression(token
, expr
);
390 if (token
->special
== '(') {
391 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
393 token
= parens_expression(token
, &expr
->unop
, "in expression");
396 if (token
->special
== '[' && lookup_type(token
->next
)) {
397 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
398 token
= typename(token
->next
, &expr
->symbol
);
399 token
= expect(token
, ']', "in type expression");
410 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
412 while (!match_op(token
, ')')) {
413 struct expression
*expr
= NULL
;
414 token
= assignment_expression(token
, &expr
);
417 add_expression(list
, expr
);
418 if (!match_op(token
, ','))
426 * extend to deal with the ambiguous C grammar for parsing
427 * a cast expressions followed by an initializer.
429 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
431 struct expression
*expr
= cast_init_expr
;
434 token
= primary_expression(token
, &expr
);
436 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
437 switch (token
->special
) {
438 case '[': { /* Array dereference */
439 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
440 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
447 token
= parse_expression(token
->next
, &add
->right
);
448 token
= expect(token
, ']', "at end of array dereference");
452 case SPECIAL_INCREMENT
: /* Post-increment */
453 case SPECIAL_DECREMENT
: { /* Post-decrement */
454 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
455 post
->op
= token
->special
;
461 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
462 /* "x->y" is just shorthand for "(*x).y" */
463 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
469 case '.': { /* Structure member dereference */
470 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
474 if (token_type(token
) != TOKEN_IDENT
) {
475 sparse_error(token
->pos
, "Expected member name");
478 deref
->member
= token
->ident
;
484 case '(': { /* Function call */
485 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
488 token
= expression_list(token
->next
, &call
->args
);
489 token
= expect(token
, ')', "in function call");
503 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
504 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
);
506 static struct token
*type_info_expression(struct token
*token
,
507 struct expression
**tree
, int type
)
509 struct expression
*expr
= alloc_expression(token
->pos
, type
);
513 if (!match_op(token
, '(') || !lookup_type(token
->next
))
514 return unary_expression(token
, &expr
->cast_expression
);
515 token
= typename(token
->next
, &expr
->cast_type
);
517 if (!match_op(token
, ')')) {
518 static const char * error
[] = {
519 [EXPR_SIZEOF
] = "at end of sizeof",
520 [EXPR_ALIGNOF
] = "at end of __alignof__",
521 [EXPR_PTRSIZEOF
] = "at end of __sizeof_ptr__"
523 return expect(token
, ')', error
[type
]);
528 * C99 ambiguity: the typename might have been the beginning
529 * of a typed initializer expression..
531 if (match_op(token
, '{'))
532 token
= initializer(&expr
->cast_expression
, token
);
536 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
538 if (token_type(token
) == TOKEN_IDENT
) {
539 struct ident
*ident
= token
->ident
;
540 if (ident
->reserved
) {
541 static const struct {
544 } type_information
[] = {
545 { &sizeof_ident
, EXPR_SIZEOF
},
546 { &__alignof___ident
, EXPR_ALIGNOF
},
547 { &__sizeof_ptr___ident
, EXPR_PTRSIZEOF
},
550 for (i
= 0; i
< 3; i
++) {
551 if (ident
== type_information
[i
].id
)
552 return type_info_expression(token
, tree
, type_information
[i
].type
);
557 if (token_type(token
) == TOKEN_SPECIAL
) {
558 if (match_oplist(token
->special
,
559 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
560 '&', '*', '+', '-', '~', '!', 0)) {
561 struct expression
*unop
;
562 struct expression
*unary
;
565 next
= cast_expression(token
->next
, &unop
);
567 sparse_error(token
->pos
, "Syntax error in unary expression");
570 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
571 unary
->op
= token
->special
;
577 /* Gcc extension: &&label gives the address of a label */
578 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
579 token_type(token
->next
) == TOKEN_IDENT
) {
580 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
581 struct symbol
*sym
= label_symbol(token
->next
);
582 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
)) {
583 sym
->ctype
.modifiers
|= MOD_ADDRESSABLE
;
584 add_symbol(&function_computed_target_list
, sym
);
586 label
->label_symbol
= sym
;
588 return token
->next
->next
;
593 return postfix_expression(token
, tree
, NULL
);
597 * Ambiguity: a '(' can be either a cast-expression or
598 * a primary-expression depending on whether it is followed
601 * additional ambiguity: a "cast expression" followed by
602 * an initializer is really a postfix-expression.
604 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
606 if (match_op(token
, '(')) {
607 struct token
*next
= token
->next
;
608 if (lookup_type(next
)) {
609 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
612 token
= typename(next
, &sym
);
613 cast
->cast_type
= sym
;
614 token
= expect(token
, ')', "at end of cast operator");
615 if (match_op(token
, '{')) {
616 token
= initializer(&cast
->cast_expression
, token
);
617 return postfix_expression(token
, tree
, cast
);
620 token
= cast_expression(token
, &cast
->cast_expression
);
624 return unary_expression(token
, tree
);
628 * Generic left-to-right binop parsing
630 * This _really_ needs to be inlined, because that makes the inner
631 * function call statically deterministic rather than a totally
632 * unpredictable indirect call. But gcc-3 is so "clever" that it
633 * doesn't do so by default even when you tell it to inline it.
635 * Making it a macro avoids the inlining problem, and also means
636 * that we can pass in the op-comparison as an expression rather
637 * than create a data structure for it.
640 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
641 struct expression *left = NULL; \
642 struct token * next = inner(token, &left); \
645 while (token_type(next) == TOKEN_SPECIAL) { \
646 struct expression *top, *right = NULL; \
647 int op = next->special; \
651 top = alloc_expression(next->pos, type); \
652 next = inner(next->next, &right); \
654 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
659 top->right = right; \
668 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
671 token
, tree
, EXPR_BINOP
, cast_expression
,
672 (op
== '*') || (op
== '/') || (op
== '%')
676 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
679 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
680 (op
== '+') || (op
== '-')
684 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
687 token
, tree
, EXPR_BINOP
, additive_expression
,
688 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
692 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
695 token
, tree
, EXPR_COMPARE
, shift_expression
,
696 (op
== '<') || (op
== '>') ||
697 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
701 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
704 token
, tree
, EXPR_COMPARE
, relational_expression
,
705 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
709 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
712 token
, tree
, EXPR_BINOP
, equality_expression
,
717 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
720 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
725 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
728 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
733 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
736 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
737 (op
== SPECIAL_LOGICAL_AND
)
741 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
744 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
745 (op
== SPECIAL_LOGICAL_OR
)
749 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
751 token
= logical_or_expression(token
, tree
);
752 if (*tree
&& match_op(token
, '?')) {
753 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
754 expr
->op
= token
->special
;
757 token
= parse_expression(token
->next
, &expr
->cond_true
);
758 token
= expect(token
, ':', "in conditional expression");
759 token
= conditional_expression(token
, &expr
->cond_false
);
764 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
766 token
= conditional_expression(token
, tree
);
767 if (*tree
&& token_type(token
) == TOKEN_SPECIAL
) {
768 static const int assignments
[] = {
770 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
771 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
772 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
773 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
774 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
775 int i
, op
= token
->special
;
776 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
777 if (assignments
[i
] == op
) {
778 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
782 return assignment_expression(token
->next
, &expr
->right
);
788 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
791 token
, tree
, EXPR_COMMA
, assignment_expression
,
796 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
798 return comma_expression(token
,tree
);