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.
11 #define _ISOC99_SOURCE
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
);
63 static struct token
*string_expression(struct token
*token
, struct expression
*expr
)
65 struct string
*string
= token
->string
;
66 struct token
*next
= token
->next
;
68 if (token_type(next
) == TOKEN_STRING
) {
69 int totlen
= string
->length
-1;
73 totlen
+= next
->string
->length
-1;
75 } while (token_type(next
) == TOKEN_STRING
);
77 if (totlen
> MAX_STRING
) {
78 warn(token
->pos
, "trying to concatenate %d-character string (%d bytes max)", totlen
, MAX_STRING
);
82 string
= __alloc_string(totlen
+1);
83 string
->length
= totlen
+1;
87 struct string
*s
= next
->string
;
88 int len
= s
->length
-1;
95 memcpy(data
, s
->data
, len
);
97 } while (token_type(next
) == TOKEN_STRING
);
100 expr
->string
= string
;
105 #define ULLONG_MAX (~0ULL)
108 static void get_number_value(struct expression
*expr
, struct token
*token
)
110 const char *str
= token
->number
;
111 unsigned long long value
;
113 unsigned long modifiers
= 0;
114 int overflow
= 0, do_warn
= 0;
115 int try_unsigned
= 1;
119 value
= strtoull(str
, &end
, 0);
122 if (value
== ULLONG_MAX
&& errno
== ERANGE
)
129 } else if (c
== 'u' || c
== 'U') {
130 added
= MOD_UNSIGNED
;
131 } else if (c
== 'l' || c
== 'L') {
134 added
|= MOD_LONGLONG
;
139 if (modifiers
& added
)
145 /* OK, it's a valid integer */
146 /* decimals can be unsigned only if directly specified as such */
147 if (str
[0] != '0' && !(modifiers
& MOD_UNSIGNED
))
149 if (!(modifiers
& MOD_LONG
)) {
150 bits
= bits_in_int
- 1;
151 if (!(value
& (~1ULL << bits
))) {
152 if (!(value
& (1ULL << bits
))) {
154 } else if (try_unsigned
) {
155 modifiers
|= MOD_UNSIGNED
;
159 modifiers
|= MOD_LONG
;
162 if (!(modifiers
& MOD_LONGLONG
)) {
163 bits
= bits_in_long
- 1;
164 if (!(value
& (~1ULL << bits
))) {
165 if (!(value
& (1ULL << bits
))) {
167 } else if (try_unsigned
) {
168 modifiers
|= MOD_UNSIGNED
;
173 modifiers
|= MOD_LONGLONG
;
176 bits
= bits_in_longlong
- 1;
177 if (value
& (~1ULL << bits
))
179 if (!(value
& (1ULL << bits
)))
182 warn(expr
->pos
, "decimal constant %s is too big for long long",
184 modifiers
|= MOD_UNSIGNED
;
187 warn(expr
->pos
, "constant %s is so big it is%s%s%s",
189 (modifiers
& MOD_UNSIGNED
) ? " unsigned":"",
190 (modifiers
& MOD_LONG
) ? " long":"",
191 (modifiers
& MOD_LONGLONG
) ? " long":"");
194 "decimal constant %s is between LONG_MAX and ULONG_MAX."
195 " For C99 that means long long, C90 compilers are very "
196 "likely to produce unsigned long (and a warning) here",
198 expr
->type
= EXPR_VALUE
;
199 expr
->ctype
= ctype_integer(modifiers
);
203 error(expr
->pos
, "constant %s is too big even for unsigned long long",
207 expr
->fvalue
= strtold(str
, &end
);
214 if (*end
== 'f' || *end
== 'F')
215 expr
->ctype
= &float_ctype
;
216 else if (*end
== 'l' || *end
== 'L')
217 expr
->ctype
= &ldouble_ctype
;
219 expr
->ctype
= &double_ctype
;
223 expr
->type
= EXPR_FVALUE
;
227 error(expr
->pos
, "constant %s is not a valid number", show_token(token
));
230 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
232 struct expression
*expr
= NULL
;
234 switch (token_type(token
)) {
236 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
237 expr
->ctype
= &int_ctype
;
238 expr
->value
= (unsigned char) token
->character
;
243 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
244 get_number_value(expr
, token
);
249 struct symbol
*sym
= lookup_symbol(token
->ident
, NS_SYMBOL
| NS_TYPEDEF
);
250 struct token
*next
= token
->next
;
252 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
255 * We support types as real first-class citizens, with type
258 * if (typeof(a) == int) ..
260 if (sym
&& sym
->namespace == NS_TYPEDEF
) {
261 warn(token
->pos
, "typename in expression");
264 expr
->symbol_name
= token
->ident
;
271 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
272 token
= string_expression(token
, expr
);
277 if (token
->special
== '(') {
278 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
280 token
= parens_expression(token
, &expr
->unop
, "in expression");
283 if (token
->special
== '[' && lookup_type(token
->next
)) {
284 expr
= alloc_expression(token
->pos
, EXPR_TYPE
);
285 token
= typename(token
->next
, &expr
->symbol
);
286 token
= expect(token
, ']', "in type expression");
297 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
299 while (!match_op(token
, ')')) {
300 struct expression
*expr
= NULL
;
301 token
= assignment_expression(token
, &expr
);
304 add_expression(list
, expr
);
305 if (!match_op(token
, ','))
313 * extend to deal with the ambiguous C grammar for parsing
314 * a cast expressions followed by an initializer.
316 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
318 struct expression
*expr
= cast_init_expr
;
321 token
= primary_expression(token
, &expr
);
323 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
324 switch (token
->special
) {
325 case '[': { /* Array dereference */
326 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
327 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
334 token
= parse_expression(token
->next
, &add
->right
);
335 token
= expect(token
, ']', "at end of array dereference");
339 case SPECIAL_INCREMENT
: /* Post-increment */
340 case SPECIAL_DECREMENT
: { /* Post-decrement */
341 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
342 post
->op
= token
->special
;
348 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
349 /* "x->y" is just shorthand for "(*x).y" */
350 struct expression
*inner
= alloc_expression(token
->pos
, EXPR_PREOP
);
356 case '.': { /* Structure member dereference */
357 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
361 if (token_type(token
) != TOKEN_IDENT
) {
362 warn(token
->pos
, "Expected member name");
365 deref
->member
= token
->ident
;
371 case '(': { /* Function call */
372 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
375 token
= expression_list(token
->next
, &call
->args
);
376 token
= expect(token
, ')', "in function call");
390 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
391 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
393 if (token_type(token
) == TOKEN_IDENT
) {
394 if (token
->ident
== &sizeof_ident
) {
395 struct expression
*sizeof_ex
396 = alloc_expression(token
->pos
, EXPR_SIZEOF
);
398 tree
= &sizeof_ex
->unop
;
400 if (!match_op(token
, '(') || !lookup_type(token
->next
))
401 return unary_expression(token
, &sizeof_ex
->cast_expression
);
402 token
= typename(token
->next
, &sizeof_ex
->cast_type
);
403 return expect(token
, ')', "at end of sizeof type-name");
404 } else if (token
->ident
== &__alignof___ident
) {
405 struct expression
*alignof_ex
406 = alloc_expression(token
->pos
, EXPR_ALIGNOF
);
408 tree
= &alignof_ex
->unop
;
410 if (!match_op(token
, '(') || !lookup_type(token
->next
))
411 return unary_expression(token
, &alignof_ex
->cast_expression
);
412 token
= typename(token
->next
, &alignof_ex
->cast_type
);
413 return expect(token
, ')', "at end of alignof type-name");
417 if (token_type(token
) == TOKEN_SPECIAL
) {
418 if (match_oplist(token
->special
,
419 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
420 '&', '*', '+', '-', '~', '!', 0)) {
421 struct expression
*unop
;
422 struct expression
*unary
;
425 next
= cast_expression(token
->next
, &unop
);
427 warn(token
->pos
, "Syntax error in unary expression");
430 unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
431 unary
->op
= token
->special
;
437 /* Gcc extension: &&label gives the address of a label */
438 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
439 token_type(token
->next
) == TOKEN_IDENT
) {
440 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
441 label
->label_symbol
= label_symbol(token
->next
);
443 return token
->next
->next
;
448 return postfix_expression(token
, tree
, NULL
);
452 * Ambiguity: a '(' can be either a cast-expression or
453 * a primary-expression depending on whether it is followed
456 * additional ambiguity: a "cast expression" followed by
457 * an initializer is really a postfix-expression.
459 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
461 if (match_op(token
, '(')) {
462 struct token
*next
= token
->next
;
463 if (lookup_type(next
)) {
464 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
467 token
= typename(next
, &sym
);
468 cast
->cast_type
= sym
;
469 token
= expect(token
, ')', "at end of cast operator");
470 if (match_op(token
, '{')) {
471 token
= initializer(&cast
->cast_expression
, token
);
472 return postfix_expression(token
, tree
, cast
);
475 token
= cast_expression(token
, &cast
->cast_expression
);
479 return unary_expression(token
, tree
);
483 * Generic left-to-right binop parsing
485 * This _really_ needs to be inlined, because that makes the inner
486 * function call statically deterministic rather than a totally
487 * unpredictable indirect call. But gcc-3 is so "clever" that it
488 * doesn't do so by default even when you tell it to inline it.
490 * Making it a macro avoids the inlining problem, and also means
491 * that we can pass in the op-comparison as an expression rather
492 * than create a data structure for it.
495 #define LR_BINOP_EXPRESSION(token, tree, type, inner, compare) \
496 struct expression *left = NULL; \
497 struct token * next = inner(token, &left); \
500 while (token_type(next) == TOKEN_SPECIAL) { \
501 struct expression *top, *right = NULL; \
502 int op = next->special; \
506 top = alloc_expression(next->pos, type); \
507 next = inner(next->next, &right); \
509 warn(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
514 top->right = right; \
523 static struct token *multiplicative_expression(struct token *token, struct expression **tree)
526 token
, tree
, EXPR_BINOP
, cast_expression
,
527 (op
== '*') || (op
== '/') || (op
== '%')
531 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
534 token
, tree
, EXPR_BINOP
, multiplicative_expression
,
535 (op
== '+') || (op
== '-')
539 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
542 token
, tree
, EXPR_BINOP
, additive_expression
,
543 (op
== SPECIAL_LEFTSHIFT
) || (op
== SPECIAL_RIGHTSHIFT
)
547 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
550 token
, tree
, EXPR_COMPARE
, shift_expression
,
551 (op
== '<') || (op
== '>') ||
552 (op
== SPECIAL_LTE
) || (op
== SPECIAL_GTE
)
556 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
559 token
, tree
, EXPR_COMPARE
, relational_expression
,
560 (op
== SPECIAL_EQUAL
) || (op
== SPECIAL_NOTEQUAL
)
564 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
567 token
, tree
, EXPR_BINOP
, equality_expression
,
572 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
575 token
, tree
, EXPR_BINOP
, bitwise_and_expression
,
580 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
583 token
, tree
, EXPR_BINOP
, bitwise_xor_expression
,
588 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
591 token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
,
592 (op
== SPECIAL_LOGICAL_AND
)
596 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
599 token
, tree
, EXPR_LOGICAL
, logical_and_expression
,
600 (op
== SPECIAL_LOGICAL_OR
)
604 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
606 token
= logical_or_expression(token
, tree
);
607 if (match_op(token
, '?')) {
608 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
609 expr
->op
= token
->special
;
612 token
= parse_expression(token
->next
, &expr
->cond_true
);
613 token
= expect(token
, ':', "in conditional expression");
614 token
= conditional_expression(token
, &expr
->cond_false
);
619 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
621 token
= conditional_expression(token
, tree
);
622 if (token_type(token
) == TOKEN_SPECIAL
) {
623 static const int assignments
[] = {
625 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
626 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
627 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
628 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
629 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
630 int i
, op
= token
->special
;
631 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
632 if (assignments
[i
] == op
) {
633 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
637 return assignment_expression(token
->next
, &expr
->right
);
643 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
646 token
, tree
, EXPR_COMMA
, assignment_expression
,
651 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
653 return comma_expression(token
,tree
);