4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
9 * This is the expression parsing part of parsing C.
26 #include "expression.h"
29 static int match_oplist(int op
, ...)
35 int nextop
= va_arg(args
, int);
43 static struct token
*comma_expression(struct token
*, struct expression
**);
45 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
47 token
= expect(token
, '(', where
);
48 if (match_op(token
, '{')) {
49 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
50 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
54 token
= compound_statement(token
->next
, stmt
);
56 token
= expect(token
, '}', "at end of statement expression");
58 token
= parse_expression(token
, expr
);
59 return expect(token
, ')', where
);
62 static struct token
*string_expression(struct token
*token
, struct expression
*expr
)
64 struct string
*string
= token
->string
;
65 struct token
*next
= token
->next
;
67 if (token_type(next
) == TOKEN_STRING
) {
68 int totlen
= string
->length
-1;
72 totlen
+= next
->string
->length
-1;
74 } while (token_type(next
) == TOKEN_STRING
);
76 if (totlen
> MAX_STRING
) {
77 warn(token
->pos
, "trying to concatenate %d-character string (%d bytes max)", totlen
, MAX_STRING
);
81 string
= __alloc_string(totlen
+1);
82 string
->length
= totlen
+1;
86 struct string
*s
= next
->string
;
87 int len
= s
->length
-1;
94 memcpy(data
, s
->data
, len
);
96 } while (token_type(next
) == TOKEN_STRING
);
99 expr
->string
= string
;
103 static void get_fp_value(struct expression
*expr
, struct token
*token
)
105 static int fp_warned
;
107 expr
->ctype
= &double_ctype
;
110 warn(token
->pos
, "FP values not yet implemented");
116 #define ULLONG_MAX (~0ULL)
119 static void get_number_value(struct expression
*expr
, struct token
*token
)
121 const char *str
= token
->number
;
122 unsigned long long value
;
124 unsigned long modifiers
= 0;
125 int overflow
= 0, do_warn
= 0;
126 int try_unsigned
= 1;
130 value
= strtoull(str
, &end
, 0);
133 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
140 } else if (c
== 'u' || c
== 'U') {
141 added
= MOD_UNSIGNED
;
142 } else if (c
== 'l' || c
== 'L') {
145 added
|= MOD_LONGLONG
;
150 if (modifiers
& added
)
156 /* OK, it's a valid integer */
157 /* decimals can be unsigned only if directly specified as such */
158 if (str
[0] != '0' && !(modifiers
& MOD_UNSIGNED
))
160 if (!(modifiers
& MOD_LONG
)) {
161 bits
= bits_in_int
- 1;
162 if (!(value
& (~1ULL << bits
))) {
163 if (!(value
& (1ULL << bits
))) {
165 } else if (try_unsigned
) {
166 modifiers
|= MOD_UNSIGNED
;
170 modifiers
|= MOD_LONG
;
173 if (!(modifiers
& MOD_LONGLONG
)) {
174 bits
= bits_in_long
- 1;
175 if (!(value
& (~1ULL << bits
))) {
176 if (!(value
& (1ULL << bits
))) {
178 } else if (try_unsigned
) {
179 modifiers
|= MOD_UNSIGNED
;
184 modifiers
|= MOD_LONGLONG
;
187 bits
= bits_in_longlong
- 1;
188 if (value
& (~1ULL << bits
))
190 if (!(value
& (1ULL << bits
)))
193 warn(expr
->pos
, "decimal constant %s is too big for long long",
195 modifiers
|= MOD_UNSIGNED
;
198 warn(expr
->pos
, "constant %s is so big it is%s%s%s",
200 (modifiers
& MOD_UNSIGNED
) ? " unsigned":"",
201 (modifiers
& MOD_LONG
) ? " long":"",
202 (modifiers
& MOD_LONGLONG
) ? " long":"");
205 "decimal constant %s is between LONG_MAX and ULONG_MAX."
206 " For C99 that means long long, C90 compilers are very "
207 "likely to produce unsigned long (and a warning) here",
209 expr
->type
= EXPR_VALUE
;
210 expr
->ctype
= ctype_integer(modifiers
);
214 error(expr
->pos
, "constant %s is too big even for unsigned long long",
217 get_fp_value(expr
, token
);
220 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
222 struct expression
*expr
= NULL
;
224 switch (token_type(token
)) {
226 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
227 expr
->ctype
= &int_ctype
;
228 expr
->value
= (unsigned char) token
->character
;
233 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
234 get_number_value(expr
, token
);
239 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
240 struct token
*next
= token
->next
;
242 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
245 * We support types as real first-class citizens, with type
248 * if (typeof(a) == int) ..
250 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
251 warn(token
->pos
, "typename in expression");
254 expr
->symbol_name
= token
->ident
;
261 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
262 token
= string_expression(token
, expr
);
267 if (token
->special
== '(') {
268 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
270 token
= parens_expression(token
, &expr
->unop
, "in expression");
273 if (token
->special
== '[' && lookup_type(token
->next
)) {
274 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
275 token
= typename(token
->next
, &expr
->symbol
);
276 token
= expect(token
, ']', "in type expression");
287 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
289 while (!match_op(token
, ')')) {
290 struct expression
*expr
= NULL
;
291 token
= assignment_expression(token
, &expr
);
294 add_expression(list
, expr
);
295 if (!match_op(token
, ','))
303 * extend to deal with the ambiguous C grammar for parsing
304 * a cast expressions followed by an initializer.
306 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
308 struct expression
*expr
= cast_init_expr
;
311 token
= primary_expression(token
, &expr
);
313 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
314 switch (token
->special
) {
315 case '[': { /* Array dereference */
316 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
317 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
324 token
= parse_expression(token
->next
, &add
->right
);
325 token
= expect(token
, ']', "at end of array dereference");
329 case SPECIAL_INCREMENT
: /* Post-increment */
330 case SPECIAL_DECREMENT
: { /* Post-decrement */
331 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
332 post
->op
= token
->special
;
338 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
339 /* "x->y" is just shorthand for "(*x).y" */
340 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
346 case '.': { /* Structure member dereference */
347 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
351 if (token_type(token
) != TOKEN_IDENT
) {
352 warn(token
->pos
, "Expected member name");
355 deref
->member
= token
->ident
;
361 case '(': { /* Function call */
362 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
365 token
= expression_list(token
->next
, &call
->args
);
366 token
= expect(token
, ')', "in function call");
380 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
381 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
383 if (token_type(token
) == TOKEN_IDENT
) {
384 if (token
->ident
== &sizeof_ident
) {
385 struct expression
*sizeof_ex
386 = alloc_expression(token
->pos
, EXPR_SIZEOF
);
388 tree
= &sizeof_ex
->unop
;
390 if (!match_op(token
, '(') || !lookup_type(token
->next
))
391 return unary_expression(token
, &sizeof_ex
->cast_expression
);
392 token
= typename(token
->next
, &sizeof_ex
->cast_type
);
393 return expect(token
, ')', "at end of sizeof type-name");
394 } else if (token
->ident
== &__alignof___ident
) {
395 struct expression
*alignof_ex
396 = alloc_expression(token
->pos
, EXPR_ALIGNOF
);
398 tree
= &alignof_ex
->unop
;
400 if (!match_op(token
, '(') || !lookup_type(token
->next
))
401 return unary_expression(token
, &alignof_ex
->cast_expression
);
402 token
= typename(token
->next
, &alignof_ex
->cast_type
);
403 return expect(token
, ')', "at end of alignof type-name");
407 if (token_type(token
) == TOKEN_SPECIAL
) {
408 if (match_oplist(token
->special
,
409 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
410 '&', '*', '+', '-', '~', '!', 0)) {
411 struct expression
*unop
;
412 struct expression
*unary
;
415 next
= cast_expression(token
->next
, &unop
);
417 warn(token
->pos
, "Syntax error in unary expression");
420 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
421 unary
->op
= token
->special
;
427 /* Gcc extension: &&label gives the address of a label */
428 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
429 token_type(token
->next
) == TOKEN_IDENT
) {
430 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
431 label
->label_symbol
= label_symbol(token
->next
);
433 return token
->next
->next
;
438 return postfix_expression(token
, tree
, NULL
);
442 * Ambiguity: a '(' can be either a cast-expression or
443 * a primary-expression depending on whether it is followed
446 * additional ambiguity: a "cast expression" followed by
447 * an initializer is really a postfix-expression.
449 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
451 if (match_op(token
, '(')) {
452 struct token
*next
= token
->next
;
453 if (lookup_type(next
)) {
454 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
457 token
= typename(next
, &sym
);
458 cast
->cast_type
= sym
;
459 token
= expect(token
, ')', "at end of cast operator");
460 if (match_op(token
, '{')) {
461 token
= initializer(&cast
->cast_expression
, token
);
462 return postfix_expression(token
, tree
, cast
);
465 token
= cast_expression(token
, &cast
->cast_expression
);
469 return unary_expression(token
, tree
);
473 * Generic left-to-right binop parsing
475 * This _really_ needs to be inlined, because that makes the inner
476 * function call statically deterministic rather than a totally
477 * unpredictable indirect call. But gcc-3 is so "clever" that it
478 * doesn't do so by default even when you tell it to inline it.
480 * Making it a macro avoids the inlining problem, and also means
481 * that we can pass in the op-comparison as an expression rather
482 * than create a data structure for it.
485 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
486 struct expression *left = NULL; \
487 struct token * next = inner(token, &left); \
490 while (token_type(next) == TOKEN_SPECIAL) { \
491 struct expression *top, *right = NULL; \
492 int op = next->special; \
496 top = alloc_expression(next->pos, type); \
497 next = inner(next->next, &right); \
499 warn(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
504 top->right = right; \
513 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
516 token
, tree
, EXPR_BINOP
, cast_expression
,
517 (op
== '*') || (op
== '/') || (op
== '%')
521 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
524 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
525 (op
== '+') || (op
== '-')
529 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
532 token
, tree
, EXPR_BINOP
, additive_expression
,
533 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
537 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
540 token
, tree
, EXPR_COMPARE
, shift_expression
,
541 (op
== '<') || (op
== '>') ||
542 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
546 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
549 token
, tree
, EXPR_COMPARE
, relational_expression
,
550 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
554 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
557 token
, tree
, EXPR_BINOP
, equality_expression
,
562 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
565 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
570 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
573 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
578 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
581 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
582 (op
== SPECIAL_LOGICAL_AND
)
586 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
589 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
590 (op
== SPECIAL_LOGICAL_OR
)
594 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
596 token
= logical_or_expression(token
, tree
);
597 if (match_op(token
, '?')) {
598 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
599 expr
->op
= token
->special
;
602 token
= parse_expression(token
->next
, &expr
->cond_true
);
603 token
= expect(token
, ':', "in conditional expression");
604 token
= conditional_expression(token
, &expr
->cond_false
);
609 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
611 token
= conditional_expression(token
, tree
);
612 if (token_type(token
) == TOKEN_SPECIAL
) {
613 static const int assignments
[] = {
615 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
616 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
617 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
618 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
619 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
620 int i
, op
= token
->special
;
621 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
622 if (assignments
[i
] == op
) {
623 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
627 return assignment_expression(token
->next
, &expr
->right
);
633 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
636 token
, tree
, EXPR_COMMA
, assignment_expression
,
641 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
643 return comma_expression(token
,tree
);