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.
24 #include "expression.h"
27 static int match_oplist(int op
, ...)
33 int nextop
= va_arg(args
, int);
41 static struct token
*comma_expression(struct token
*, struct expression
**);
43 struct token
*parens_expression(struct token
*token
, struct expression
**expr
, const char *where
)
45 token
= expect(token
, '(', where
);
46 if (match_op(token
, '{')) {
47 struct expression
*e
= alloc_expression(token
->pos
, EXPR_STATEMENT
);
48 struct statement
*stmt
= alloc_statement(token
->pos
, STMT_COMPOUND
);
52 token
= compound_statement(token
->next
, stmt
);
54 token
= expect(token
, '}', "at end of statement expression");
56 token
= parse_expression(token
, expr
);
57 return expect(token
, ')', where
);
60 static struct token
*string_expression(struct token
*token
, struct expression
*expr
)
62 struct string
*string
= token
->string
;
63 struct token
*next
= token
->next
;
65 if (token_type(next
) == TOKEN_STRING
) {
66 int totlen
= string
->length
;
70 totlen
+= next
->string
->length
-1;
72 } while (token_type(next
) == TOKEN_STRING
);
74 string
= __alloc_string(totlen
);
75 string
->length
= totlen
;
79 struct string
*s
= next
->string
;
83 memcpy(data
, s
->data
, len
);
85 } while (token_type(next
) == TOKEN_STRING
);
87 expr
->string
= string
;
91 static void get_int_value(struct expression
*expr
, const char *str
)
93 unsigned long long value
= 0;
94 unsigned int base
= 10, digit
, bits
;
95 unsigned long modifiers
, extramod
;
99 base
= 18; // the -= 2 for the octal case will
100 str
++; // skip the 'x'
103 str
++; // skip the 'o' or 'x/X'
104 base
-= 2; // the fall-through will make this 8
106 while ((digit
= hexval(*str
)) < base
) {
107 value
= value
* base
+ digit
;
113 if (c
== 'u' || c
== 'U') {
114 modifiers
|= MOD_UNSIGNED
;
117 if (c
== 'l' || c
== 'L') {
118 if (modifiers
& MOD_LONG
)
119 modifiers
|= MOD_LONGLONG
;
120 modifiers
|= MOD_LONG
;
126 bits
= BITS_IN_LONGLONG
;
128 if (!(modifiers
& MOD_LONGLONG
)) {
129 if (value
& (~1ULL << (BITS_IN_LONG
-1))) {
130 extramod
= MOD_LONGLONG
| MOD_LONG
;
133 if (!(modifiers
& MOD_LONG
)) {
134 if (value
& (~1ULL << (BITS_IN_INT
-1))) {
141 if (!(modifiers
& MOD_UNSIGNED
)) {
142 if (value
& (1ULL << (bits
-1))) {
143 extramod
|= MOD_UNSIGNED
;
148 * Special case: "int" gets promoted directly to "long"
149 * for normal decimal numbers..
151 modifiers
|= extramod
;
152 if (base
== 10 && modifiers
== MOD_UNSIGNED
) {
153 modifiers
= MOD_LONG
;
154 if (BITS_IN_LONG
== BITS_IN_INT
)
155 modifiers
= MOD_LONG
| MOD_UNSIGNED
;
158 /* Hex or octal constants don't complain about missing signedness */
159 if (base
== 10 || extramod
!= MOD_UNSIGNED
)
160 warn(expr
->pos
, "value is so big it is%s%s%s",
161 (modifiers
& MOD_UNSIGNED
) ? " unsigned":"",
162 (modifiers
& MOD_LONG
) ? " long":"",
163 (modifiers
& MOD_LONGLONG
) ? " long":"");
166 expr
->type
= EXPR_VALUE
;
167 expr
->ctype
= ctype_integer(modifiers
);
171 struct token
*primary_expression(struct token
*token
, struct expression
**tree
)
173 struct expression
*expr
= NULL
;
175 switch (token_type(token
)) {
176 static int fp_warned
;
178 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
179 expr
->ctype
= &double_ctype
;
182 warn(token
->pos
, "FP values not yet implemented");
189 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
190 expr
->ctype
= &int_ctype
;
191 expr
->value
= (unsigned char) token
->character
;
196 expr
= alloc_expression(token
->pos
, EXPR_VALUE
);
197 get_int_value(expr
, token
->integer
);
202 expr
= alloc_expression(token
->pos
, EXPR_SYMBOL
);
203 expr
->symbol_name
= token
->ident
;
204 expr
->symbol
= lookup_symbol(token
->ident
, NS_SYMBOL
);
210 expr
= alloc_expression(token
->pos
, EXPR_STRING
);
211 token
= string_expression(token
, expr
);
216 if (token
->special
== '(') {
217 expr
= alloc_expression(token
->pos
, EXPR_PREOP
);
219 token
= parens_expression(token
, &expr
->unop
, "in expression");
229 static struct token
*expression_list(struct token
*token
, struct expression_list
**list
)
231 while (!match_op(token
, ')')) {
232 struct expression
*expr
= NULL
;
233 token
= assignment_expression(token
, &expr
);
236 add_expression(list
, expr
);
237 if (!match_op(token
, ','))
245 * extend to deal with the ambiguous C grammar for parsing
246 * a cast expressions followed by an initializer.
248 static struct token
*postfix_expression(struct token
*token
, struct expression
**tree
, struct expression
*cast_init_expr
)
250 struct expression
*expr
= cast_init_expr
;
253 token
= primary_expression(token
, &expr
);
255 while (expr
&& token_type(token
) == TOKEN_SPECIAL
) {
256 switch (token
->special
) {
257 case '[': { /* Array dereference */
258 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_PREOP
);
259 struct expression
*add
= alloc_expression(token
->pos
, EXPR_BINOP
);
266 token
= parse_expression(token
->next
, &add
->right
);
267 token
= expect(token
, ']', "at end of array dereference");
271 case SPECIAL_INCREMENT
: /* Post-increment */
272 case SPECIAL_DECREMENT
: { /* Post-decrement */
273 struct expression
*post
= alloc_expression(token
->pos
, EXPR_POSTOP
);
274 post
->op
= token
->special
;
280 case '.': /* Structure member dereference */
281 case SPECIAL_DEREFERENCE
: { /* Structure pointer member dereference */
282 struct expression
*deref
= alloc_expression(token
->pos
, EXPR_DEREF
);
283 deref
->op
= token
->special
;
286 if (token_type(token
) != TOKEN_IDENT
) {
287 warn(token
->pos
, "Expected member name");
290 deref
->member
= token
->ident
;
296 case '(': { /* Function call */
297 struct expression
*call
= alloc_expression(token
->pos
, EXPR_CALL
);
300 token
= expression_list(token
->next
, &call
->args
);
301 token
= expect(token
, ')', "in function call");
315 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
);
316 static struct token
*unary_expression(struct token
*token
, struct expression
**tree
)
318 if (token_type(token
) == TOKEN_IDENT
&&
319 (token
->ident
== &sizeof_ident
||
320 token
->ident
== &__alignof___ident
)) {
321 struct expression
*sizeof_ex
= alloc_expression(token
->pos
, EXPR_SIZEOF
);
323 tree
= &sizeof_ex
->unop
;
325 if (!match_op(token
, '(') || !lookup_type(token
->next
))
326 return unary_expression(token
, &sizeof_ex
->cast_expression
);
327 token
= typename(token
->next
, &sizeof_ex
->cast_type
);
328 return expect(token
, ')', "at end of sizeof type-name");
331 if (token_type(token
) == TOKEN_SPECIAL
) {
332 if (match_oplist(token
->special
,
333 SPECIAL_INCREMENT
, SPECIAL_DECREMENT
,
334 '&', '*', '+', '-', '~', '!', 0)) {
335 struct expression
*unary
= alloc_expression(token
->pos
, EXPR_PREOP
);
336 unary
->op
= token
->special
;
338 return cast_expression(token
->next
, &unary
->unop
);
341 /* Gcc extension: &&label gives the address of a label */
342 if (match_op(token
, SPECIAL_LOGICAL_AND
) &&
343 token_type(token
->next
) == TOKEN_IDENT
) {
344 struct expression
*label
= alloc_expression(token
->pos
, EXPR_LABEL
);
345 label
->label_symbol
= label_symbol(token
->next
);
347 return token
->next
->next
;
352 return postfix_expression(token
, tree
, NULL
);
356 * Ambiguity: a '(' can be either a cast-expression or
357 * a primary-expression depending on whether it is followed
360 * additional ambiguity: a "cast expression" followed by
361 * an initializer is really a postfix-expression.
363 static struct token
*cast_expression(struct token
*token
, struct expression
**tree
)
365 if (match_op(token
, '(')) {
366 struct token
*next
= token
->next
;
367 if (lookup_type(next
)) {
368 struct expression
*cast
= alloc_expression(next
->pos
, EXPR_CAST
);
371 token
= typename(next
, &sym
);
372 cast
->cast_type
= sym
->ctype
.base_type
;
373 token
= expect(token
, ')', "at end of cast operator");
374 if (match_op(token
, '{')) {
375 token
= initializer(&cast
->cast_expression
, token
);
376 return postfix_expression(token
, tree
, cast
);
379 token
= cast_expression(token
, &cast
->cast_expression
);
383 return unary_expression(token
, tree
);
386 /* Generic left-to-right binop parsing */
387 static struct token
*lr_binop_expression(struct token
*token
, struct expression
**tree
,
388 enum expression_type type
, struct token
*(*inner
)(struct token
*, struct expression
**), ...)
390 struct expression
*left
= NULL
;
391 struct token
* next
= inner(token
, &left
);
394 while (token_type(next
) == TOKEN_SPECIAL
) {
395 struct expression
*top
, *right
= NULL
;
396 int op
= next
->special
;
399 va_start(args
, inner
);
401 int nextop
= va_arg(args
, int);
408 top
= alloc_expression(next
->pos
, type
);
409 next
= inner(next
->next
, &right
);
411 warn(next
->pos
, "No right hand side of '%s'-expression", show_special(op
));
425 static struct token
*multiplicative_expression(struct token
*token
, struct expression
**tree
)
427 return lr_binop_expression(token
, tree
, EXPR_BINOP
, cast_expression
, '*', '/', '%', 0);
430 static struct token
*additive_expression(struct token
*token
, struct expression
**tree
)
432 return lr_binop_expression(token
, tree
, EXPR_BINOP
, multiplicative_expression
, '+', '-', 0);
435 static struct token
*shift_expression(struct token
*token
, struct expression
**tree
)
437 return lr_binop_expression(token
, tree
, EXPR_BINOP
, additive_expression
, SPECIAL_LEFTSHIFT
, SPECIAL_RIGHTSHIFT
, 0);
440 static struct token
*relational_expression(struct token
*token
, struct expression
**tree
)
442 return lr_binop_expression(token
, tree
, EXPR_COMPARE
, shift_expression
, '<', '>', SPECIAL_LTE
, SPECIAL_GTE
, 0);
445 static struct token
*equality_expression(struct token
*token
, struct expression
**tree
)
447 return lr_binop_expression(token
, tree
, EXPR_COMPARE
, relational_expression
, SPECIAL_EQUAL
, SPECIAL_NOTEQUAL
, 0);
450 static struct token
*bitwise_and_expression(struct token
*token
, struct expression
**tree
)
452 return lr_binop_expression(token
, tree
, EXPR_BINOP
, equality_expression
, '&', 0);
455 static struct token
*bitwise_xor_expression(struct token
*token
, struct expression
**tree
)
457 return lr_binop_expression(token
, tree
, EXPR_BINOP
, bitwise_and_expression
, '^', 0);
460 static struct token
*bitwise_or_expression(struct token
*token
, struct expression
**tree
)
462 return lr_binop_expression(token
, tree
, EXPR_BINOP
, bitwise_xor_expression
, '|', 0);
465 static struct token
*logical_and_expression(struct token
*token
, struct expression
**tree
)
467 return lr_binop_expression(token
, tree
, EXPR_LOGICAL
, bitwise_or_expression
, SPECIAL_LOGICAL_AND
, 0);
470 static struct token
*logical_or_expression(struct token
*token
, struct expression
**tree
)
472 return lr_binop_expression(token
, tree
, EXPR_LOGICAL
, logical_and_expression
, SPECIAL_LOGICAL_OR
, 0);
475 struct token
*conditional_expression(struct token
*token
, struct expression
**tree
)
477 token
= logical_or_expression(token
, tree
);
478 if (match_op(token
, '?')) {
479 struct expression
*expr
= alloc_expression(token
->pos
, EXPR_CONDITIONAL
);
480 expr
->op
= token
->special
;
483 token
= parse_expression(token
->next
, &expr
->cond_true
);
484 token
= expect(token
, ':', "in conditional expression");
485 token
= conditional_expression(token
, &expr
->cond_false
);
490 struct token
*assignment_expression(struct token
*token
, struct expression
**tree
)
492 token
= conditional_expression(token
, tree
);
493 if (token_type(token
) == TOKEN_SPECIAL
) {
494 static const int assignments
[] = {
496 SPECIAL_ADD_ASSIGN
, SPECIAL_SUB_ASSIGN
,
497 SPECIAL_MUL_ASSIGN
, SPECIAL_DIV_ASSIGN
,
498 SPECIAL_MOD_ASSIGN
, SPECIAL_SHL_ASSIGN
,
499 SPECIAL_SHR_ASSIGN
, SPECIAL_AND_ASSIGN
,
500 SPECIAL_OR_ASSIGN
, SPECIAL_XOR_ASSIGN
};
501 int i
, op
= token
->special
;
502 for (i
= 0; i
< sizeof(assignments
)/sizeof(int); i
++)
503 if (assignments
[i
] == op
) {
504 struct expression
* expr
= alloc_expression(token
->pos
, EXPR_ASSIGNMENT
);
508 return assignment_expression(token
->next
, &expr
->right
);
514 static struct token
*comma_expression(struct token
*token
, struct expression
**tree
)
516 return lr_binop_expression(token
, tree
, EXPR_COMMA
, assignment_expression
, ',', 0);
519 struct token
*parse_expression(struct token
*token
, struct expression
**tree
)
521 return comma_expression(token
,tree
);