1/* 2 * sparse/expression.c 3 * 4 * Copyright (C) 2003 Transmeta Corp. 5 * 2003-2004 Linus Torvalds 6 * 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: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 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 23 * THE SOFTWARE. 24 * 25 * This is the expression parsing part of parsing C. 26 */ 27#include <stdarg.h> 28#include <stdlib.h> 29#include <stdio.h> 30#include <string.h> 31#include <ctype.h> 32#include <unistd.h> 33#include <fcntl.h> 34#include <errno.h> 35#include <limits.h> 36 37#include "lib.h" 38#include "allocate.h" 39#include "token.h" 40#include "parse.h" 41#include "symbol.h" 42#include "scope.h" 43#include "expression.h" 44#include "target.h" 45#include "char.h" 46 47ALLOCATOR(type_expression, "type-expr-maps"); 48 49static int match_oplist(int op, ...) 50{ 51 va_list args; 52 int nextop; 53 54 va_start(args, op); 55 do { 56 nextop = va_arg(args, int); 57 } while (nextop != 0 && nextop != op); 58 va_end(args); 59 60 return nextop != 0; 61} 62 63static struct token *comma_expression(struct token *, struct expression **); 64 65struct token *parens_expression(struct token *token, struct expression **expr, const char *where) 66{ 67 struct token *p; 68 69 token = expect(token, '(', where); 70 p = token; 71 if (match_op(token, '{')) { 72 struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT); 73 struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND); 74 *expr = e; 75 e->statement = stmt; 76 start_label_scope(); 77 token = compound_statement(token->next, stmt); 78 end_label_scope(); 79 token = expect(token, '}', "at end of statement expression"); 80 } else 81 token = parse_expression(token, expr); 82 83 if (token == p) 84 sparse_error(token->pos, "an expression is expected before ')'"); 85 return expect(token, ')', where); 86} 87 88struct token *string_expression(struct token *token, struct expression **expr, const char *where) 89{ 90 struct token *next = primary_expression(token, expr); 91 92 if (!*expr || (*expr)->type != EXPR_STRING) { 93 sparse_error(token->pos, "string literal expected for %s", where); 94 *expr = NULL; 95 } 96 return next; 97} 98 99/* 100 * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token 101 * conversion 102 */ 103static struct symbol *handle_func(struct token *token) 104{ 105 struct ident *ident = token->ident; 106 struct symbol *decl, *array; 107 struct string *string; 108 int len; 109 110 if (ident != &__func___ident && 111 ident != &__FUNCTION___ident && 112 ident != &__PRETTY_FUNCTION___ident) 113 return NULL; 114 115 if (!current_fn || !current_fn->ident) 116 return NULL; 117 118 /* OK, it's one of ours */ 119 array = alloc_symbol(token->pos, SYM_ARRAY); 120 array->ctype.base_type = &char_ctype; 121 array->ctype.alignment = 1; 122 array->endpos = token->pos; 123 decl = alloc_symbol(token->pos, SYM_NODE); 124 decl->ctype.base_type = array; 125 decl->ctype.alignment = 1; 126 decl->ctype.modifiers = MOD_STATIC; 127 decl->endpos = token->pos; 128 129 /* NS_SYMBOL but in function-scope */ 130 bind_symbol_with_scope(decl, ident, NS_SYMBOL, function_scope); 131 132 len = current_fn->ident->len; 133 string = __alloc_string(len + 1); 134 memcpy(string->data, current_fn->ident->name, len); 135 string->data[len] = 0; 136 string->length = len + 1; 137 138 decl->initializer = alloc_expression(token->pos, EXPR_STRING); 139 decl->initializer->string = string; 140 decl->initializer->ctype = decl; 141 decl->array_size = alloc_const_expression(token->pos, len + 1); 142 array->array_size = decl->array_size; 143 decl->bit_size = array->bit_size = bytes_to_bits(len + 1); 144 145 return decl; 146} 147 148static struct token *parse_type(struct token *token, struct expression **tree) 149{ 150 struct symbol *sym; 151 *tree = alloc_expression(token->pos, EXPR_TYPE); 152 token = typename(token, &sym, NULL); 153 if (sym->ident) 154 sparse_error(token->pos, 155 "type expression should not include identifier " 156 "\"%s\"", sym->ident->name); 157 (*tree)->symbol = sym; 158 return token; 159} 160 161static struct token *builtin_types_compatible_p_expr(struct token *token, 162 struct expression **tree) 163{ 164 struct expression *expr = alloc_expression( 165 token->pos, EXPR_COMPARE); 166 expr->op = SPECIAL_EQUAL; 167 token = token->next; 168 if (!match_op(token, '(')) 169 return expect(token, '(', 170 "after __builtin_types_compatible_p"); 171 token = token->next; 172 token = parse_type(token, &expr->left); 173 if (!match_op(token, ',')) 174 return expect(token, ',', 175 "in __builtin_types_compatible_p"); 176 token = token->next; 177 token = parse_type(token, &expr->right); 178 if (!match_op(token, ')')) 179 return expect(token, ')', 180 "at end of __builtin_types_compatible_p"); 181 token = token->next; 182 183 *tree = expr; 184 return token; 185} 186 187static struct token *builtin_offsetof_expr(struct token *token, 188 struct expression **tree) 189{ 190 struct expression *expr = NULL; 191 struct expression **p = &expr; 192 struct symbol *sym; 193 int op = '.'; 194 195 token = token->next; 196 if (!match_op(token, '(')) 197 return expect(token, '(', "after __builtin_offset"); 198 199 token = token->next; 200 token = typename(token, &sym, NULL); 201 if (sym->ident) 202 sparse_error(token->pos, 203 "type expression should not include identifier " 204 "\"%s\"", sym->ident->name); 205 206 if (!match_op(token, ',')) 207 return expect(token, ',', "in __builtin_offset"); 208 209 while (1) { 210 struct expression *e; 211 switch (op) { 212 case ')': 213 expr->in = sym; 214 *tree = expr; 215 default: 216 return expect(token, ')', "at end of __builtin_offset"); 217 case SPECIAL_DEREFERENCE: 218 e = alloc_expression(token->pos, EXPR_OFFSETOF); 219 e->op = '['; 220 *p = e; 221 p = &e->down; 222 /* fall through */ 223 case '.': 224 token = token->next; 225 e = alloc_expression(token->pos, EXPR_OFFSETOF); 226 e->op = '.'; 227 if (token_type(token) != TOKEN_IDENT) { 228 sparse_error(token->pos, "Expected member name"); 229 return token; 230 } 231 e->ident = token->ident; 232 token = token->next; 233 break; 234 case '[': 235 token = token->next; 236 e = alloc_expression(token->pos, EXPR_OFFSETOF); 237 e->op = '['; 238 token = parse_expression(token, &e->index); 239 token = expect(token, ']', 240 "at end of array dereference"); 241 if (!e->index) 242 return token; 243 } 244 *p = e; 245 p = &e->down; 246 op = token_type(token) == TOKEN_SPECIAL ? token->special : 0; 247 } 248} 249 250#ifndef ULLONG_MAX 251#define ULLONG_MAX (~0ULL) 252#endif 253 254static unsigned long long parse_num(const char *nptr, char **end) 255{ 256 if (nptr[0] == '0' && tolower((unsigned char)nptr[1]) == 'b') 257 return strtoull(&nptr[2], end, 2); 258 return strtoull(nptr, end, 0); 259} 260 261static void get_number_value(struct expression *expr, struct token *token) 262{ 263 const char *str = token->number; 264 unsigned long long value; 265 char *end; 266 int size = 0, want_unsigned = 0; 267 int overflow = 0, do_warn = 0; 268 int try_unsigned = 1; 269 int bits; 270 271 errno = 0; 272 value = parse_num(str, &end); 273 if (end == str) 274 goto Float; 275 if (value == ULLONG_MAX && errno == ERANGE) 276 overflow = 1; 277 while (1) { 278 char c = *end++; 279 if (!c) { 280 break; 281 } else if (c == 'u' || c == 'U') { 282 if (want_unsigned) 283 goto Enoint; 284 want_unsigned = 1; 285 } else if (c == 'l' || c == 'L') { 286 if (size) 287 goto Enoint; 288 size = 1; 289 if (*end == c) { 290 size = 2; 291 end++; 292 } 293 } else 294 goto Float; 295 } 296 if (overflow) 297 goto Eoverflow; 298 /* OK, it's a valid integer */ 299 /* decimals can be unsigned only if directly specified as such */ 300 if (str[0] != '0' && !want_unsigned) 301 try_unsigned = 0; 302 if (!size) { 303 bits = bits_in_int - 1; 304 if (!(value & (~1ULL << bits))) { 305 if (!(value & (1ULL << bits))) { 306 goto got_it; 307 } else if (try_unsigned) { 308 want_unsigned = 1; 309 goto got_it; 310 } 311 } 312 size = 1; 313 do_warn = 1; 314 } 315 if (size < 2) { 316 bits = bits_in_long - 1; 317 if (!(value & (~1ULL << bits))) { 318 if (!(value & (1ULL << bits))) { 319 goto got_it; 320 } else if (try_unsigned) { 321 want_unsigned = 1; 322 goto got_it; 323 } 324 do_warn |= 2; 325 } 326 size = 2; 327 do_warn |= 1; 328 } 329 bits = bits_in_longlong - 1; 330 if (value & (~1ULL << bits)) 331 goto Eoverflow; 332 if (!(value & (1ULL << bits))) 333 goto got_it; 334 if (!try_unsigned) 335 warning(expr->pos, "decimal constant %s is too big for long long", 336 show_token(token)); 337 want_unsigned = 1; 338got_it: 339 if (do_warn && Wconstant_suffix) 340 warning(expr->pos, "constant %s is so big it is%s%s%s", 341 show_token(token), 342 want_unsigned ? " unsigned":"", 343 size > 0 ? " long":"", 344 size > 1 ? " long":""); 345 if (do_warn & 2) 346 warning(expr->pos, 347 "decimal constant %s is between LONG_MAX and ULONG_MAX." 348 " For C99 that means long long, C90 compilers are very " 349 "likely to produce unsigned long (and a warning) here", 350 show_token(token)); 351 expr->type = EXPR_VALUE; 352 expr->flags = CEF_SET_INT; 353 expr->ctype = ctype_integer(size, want_unsigned); 354 expr->value = value; 355 return; 356Eoverflow: 357 error_die(expr->pos, "constant %s is too big even for unsigned long long", 358 show_token(token)); 359 return; 360Float: 361 expr->fvalue = string_to_ld(str, &end); 362 if (str == end) 363 goto Enoint; 364 365 if (*end && end[1]) 366 goto Enoint; 367 368 if (*end == 'f' || *end == 'F') 369 expr->ctype = &float_ctype; 370 else if (*end == 'l' || *end == 'L') 371 expr->ctype = &ldouble_ctype; 372 else if (!*end) 373 expr->ctype = &double_ctype; 374 else 375 goto Enoint; 376 377 expr->flags = CEF_SET_FLOAT; 378 expr->type = EXPR_FVALUE; 379 return; 380 381Enoint: 382 sparse_error(expr->pos, "constant %s is not a valid number", show_token(token)); 383 expr->type = EXPR_VALUE; 384 expr->value = 0; 385 expr->ctype = &int_ctype; 386} 387 388static struct token *generic_selection(struct token *token, struct expression **tree) 389{ 390 struct expression *expr = alloc_expression(token->pos, EXPR_GENERIC); 391 struct type_expression **last = &expr->map; 392 393 token = expect(token, '(', "after '_Generic'"); 394 token = assignment_expression(token, &expr->control); 395 if (!match_op(token, ',')) { 396 goto end; 397 } 398 while (match_op(token, ',')) { 399 token = token->next; 400 if (lookup_type(token)) { 401 struct type_expression *map = __alloc_type_expression(0); 402 token = typename(token, &map->type, NULL); 403 token = expect(token, ':', "after typename"); 404 token = assignment_expression(token, &map->expr); 405 *last = map; 406 last = &map->next; 407 } else if (match_ident(token, &default_ident)) { 408 if (expr->def) { 409 warning(token->pos, "multiple default in generic expression"); 410 info(expr->def->pos, "note: previous was here"); 411 } 412 token = token->next; 413 token = expect(token, ':', "after typename"); 414 token = assignment_expression(token, &expr->def); 415 } 416 } 417end: 418 *tree = expr; 419 return expect(token, ')', "after expression"); 420} 421 422struct token *primary_expression(struct token *token, struct expression **tree) 423{ 424 struct expression *expr = NULL; 425 426 switch (token_type(token)) { 427 case TOKEN_CHAR ... TOKEN_WIDE_CHAR_EMBEDDED_3: 428 expr = alloc_expression(token->pos, EXPR_VALUE); 429 expr->flags = CEF_SET_CHAR; 430 expr->ctype = token_type(token) < TOKEN_WIDE_CHAR ? &int_ctype : &long_ctype; 431 get_char_constant(token, &expr->value); 432 token = token->next; 433 break; 434 435 case TOKEN_NUMBER: 436 expr = alloc_expression(token->pos, EXPR_VALUE); 437 get_number_value(expr, token); /* will see if it's an integer */ 438 token = token->next; 439 break; 440 441 case TOKEN_ZERO_IDENT: { 442 expr = alloc_expression(token->pos, EXPR_SYMBOL); 443 expr->flags = CEF_SET_INT; 444 expr->ctype = &int_ctype; 445 expr->symbol = &zero_int; 446 expr->symbol_name = token->ident; 447 token = token->next; 448 break; 449 } 450 451 case TOKEN_IDENT: { 452 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF); 453 struct token *next = token->next; 454 455 if (!sym) { 456 sym = handle_func(token); 457 if (token->ident == &__builtin_types_compatible_p_ident) { 458 token = builtin_types_compatible_p_expr(token, &expr); 459 break; 460 } 461 if (token->ident == &__builtin_offsetof_ident) { 462 token = builtin_offsetof_expr(token, &expr); 463 break; 464 } 465 if (token->ident == &_Generic_ident) { 466 token = generic_selection(token->next, &expr); 467 break; 468 } 469 } else if (sym->enum_member) { 470 expr = alloc_expression(token->pos, EXPR_VALUE); 471 *expr = *sym->initializer; 472 /* we want the right position reported, thus the copy */ 473 expr->pos = token->pos; 474 expr->flags = CEF_SET_ENUM; 475 token = next; 476 break; 477 } 478 479 expr = alloc_expression(token->pos, EXPR_SYMBOL); 480 481 /* 482 * We support types as real first-class citizens, with type 483 * comparisons etc: 484 * 485 * if (typeof(a) == int) .. 486 */ 487 if (sym && sym->namespace == NS_TYPEDEF) { 488 sparse_error(token->pos, "typename in expression"); 489 sym = NULL; 490 } 491 expr->symbol_name = token->ident; 492 expr->symbol = sym; 493 494 /* 495 * A pointer to an lvalue designating a static storage 496 * duration object is an address constant [6.6(9)]. 497 */ 498 if (sym && (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_STATIC))) 499 expr->flags = CEF_ADDR; 500 501 token = next; 502 break; 503 } 504 505 case TOKEN_STRING: 506 case TOKEN_WIDE_STRING: 507 expr = alloc_expression(token->pos, EXPR_STRING); 508 token = get_string_constant(token, expr); 509 break; 510 511 case TOKEN_SPECIAL: 512 if (token->special == '(') { 513 expr = alloc_expression(token->pos, EXPR_PREOP); 514 expr->op = '('; 515 token = parens_expression(token, &expr->unop, "in expression"); 516 break; 517 } 518 if (token->special == '[' && lookup_type(token->next)) { 519 expr = alloc_expression(token->pos, EXPR_TYPE); 520 token = typename(token->next, &expr->symbol, NULL); 521 token = expect(token, ']', "in type expression"); 522 break; 523 } 524 525 default: 526 ; 527 } 528 *tree = expr; 529 return token; 530} 531 532static struct token *expression_list(struct token *token, struct expression_list **list) 533{ 534 while (!match_op(token, ')')) { 535 struct expression *expr = NULL; 536 token = assignment_expression(token, &expr); 537 if (!expr) 538 break; 539 add_expression(list, expr); 540 if (!match_op(token, ',')) 541 break; 542 token = token->next; 543 } 544 return token; 545} 546 547/* 548 * extend to deal with the ambiguous C grammar for parsing 549 * a cast expressions followed by an initializer. 550 */ 551static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr) 552{ 553 struct expression *expr = cast_init_expr; 554 555 if (!expr) 556 token = primary_expression(token, &expr); 557 558 while (expr && token_type(token) == TOKEN_SPECIAL) { 559 switch (token->special) { 560 case '[': { /* Array dereference */ 561 struct expression *deref = alloc_expression(token->pos, EXPR_PREOP); 562 struct expression *add = alloc_expression(token->pos, EXPR_BINOP); 563 564 deref->op = '*'; 565 deref->unop = add; 566 567 add->op = '+'; 568 add->left = expr; 569 token = parse_expression(token->next, &add->right); 570 token = expect(token, ']', "at end of array dereference"); 571 expr = deref; 572 continue; 573 } 574 case SPECIAL_INCREMENT: /* Post-increment */ 575 case SPECIAL_DECREMENT: { /* Post-decrement */ 576 struct expression *post = alloc_expression(token->pos, EXPR_POSTOP); 577 post->op = token->special; 578 post->unop = expr; 579 expr = post; 580 token = token->next; 581 continue; 582 } 583 case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */ 584 /* "x->y" is just shorthand for "(*x).y" */ 585 struct expression *inner = alloc_expression(token->pos, EXPR_PREOP); 586 inner->op = '*'; 587 inner->unop = expr; 588 expr = inner; 589 } 590 /* Fall through!! */ 591 case '.': { /* Structure member dereference */ 592 struct expression *deref = alloc_expression(token->pos, EXPR_DEREF); 593 deref->op = '.'; 594 deref->deref = expr; 595 token = token->next; 596 if (token_type(token) != TOKEN_IDENT) { 597 sparse_error(token->pos, "Expected member name"); 598 break; 599 } 600 deref->member = token->ident; 601 token = token->next; 602 expr = deref; 603 continue; 604 } 605 606 case '(': { /* Function call */ 607 struct expression *call = alloc_expression(token->pos, EXPR_CALL); 608 call->op = '('; 609 call->fn = expr; 610 token = expression_list(token->next, &call->args); 611 token = expect(token, ')', "in function call"); 612 expr = call; 613 continue; 614 } 615 616 default: 617 break; 618 } 619 break; 620 } 621 *tree = expr; 622 return token; 623} 624 625static struct token *cast_expression(struct token *token, struct expression **tree); 626static struct token *unary_expression(struct token *token, struct expression **tree); 627 628static struct token *type_info_expression(struct token *token, 629 struct expression **tree, int type) 630{ 631 struct expression *expr = alloc_expression(token->pos, type); 632 struct token *p; 633 634 *tree = expr; 635 expr->flags = CEF_SET_ICE; /* XXX: VLA support will need that changed */ 636 token = token->next; 637 if (!match_op(token, '(') || !lookup_type(token->next)) 638 return unary_expression(token, &expr->cast_expression); 639 p = token; 640 token = typename(token->next, &expr->cast_type, NULL); 641 642 if (!match_op(token, ')')) { 643 static const char * error[] = { 644 [EXPR_SIZEOF] = "at end of sizeof", 645 [EXPR_ALIGNOF] = "at end of __alignof__", 646 [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__" 647 }; 648 return expect(token, ')', error[type]); 649 } 650 651 token = token->next; 652 /* 653 * C99 ambiguity: the typename might have been the beginning 654 * of a typed initializer expression.. 655 */ 656 if (match_op(token, '{')) { 657 struct expression *cast = alloc_expression(p->pos, EXPR_CAST); 658 cast->cast_type = expr->cast_type; 659 expr->cast_type = NULL; 660 expr->cast_expression = cast; 661 token = initializer(&cast->cast_expression, token); 662 token = postfix_expression(token, &expr->cast_expression, cast); 663 } 664 return token; 665} 666 667static struct token *unary_expression(struct token *token, struct expression **tree) 668{ 669 if (token_type(token) == TOKEN_IDENT) { 670 struct ident *ident = token->ident; 671 if (ident->reserved) { 672 static const struct { 673 struct ident *id; 674 int type; 675 } type_information[] = { 676 { &sizeof_ident, EXPR_SIZEOF }, 677 { &__alignof___ident, EXPR_ALIGNOF }, 678 { &__alignof_ident, EXPR_ALIGNOF }, 679 { &_Alignof_ident, EXPR_ALIGNOF }, 680 { &__sizeof_ptr___ident, EXPR_PTRSIZEOF }, 681 }; 682 int i; 683 for (i = 0; i < ARRAY_SIZE(type_information); i++) { 684 if (ident == type_information[i].id) 685 return type_info_expression(token, tree, type_information[i].type); 686 } 687 } 688 } 689 690 if (token_type(token) == TOKEN_SPECIAL) { 691 if (match_oplist(token->special, 692 SPECIAL_INCREMENT, SPECIAL_DECREMENT, 693 '&', '*', 0)) { 694 struct expression *unop; 695 struct expression *unary; 696 struct token *next; 697 698 next = cast_expression(token->next, &unop); 699 if (!unop) { 700 sparse_error(token->pos, "Syntax error in unary expression"); 701 *tree = NULL; 702 return next; 703 } 704 unary = alloc_expression(token->pos, EXPR_PREOP); 705 unary->op = token->special; 706 unary->unop = unop; 707 *tree = unary; 708 return next; 709 } 710 /* possibly constant ones */ 711 if (match_oplist(token->special, '+', '-', '~', '!', 0)) { 712 struct expression *unop; 713 struct expression *unary; 714 struct token *next; 715 716 next = cast_expression(token->next, &unop); 717 if (!unop) { 718 sparse_error(token->pos, "Syntax error in unary expression"); 719 *tree = NULL; 720 return next; 721 } 722 unary = alloc_expression(token->pos, EXPR_PREOP); 723 unary->op = token->special; 724 unary->unop = unop; 725 *tree = unary; 726 return next; 727 } 728 /* Gcc extension: &&label gives the address of a label */ 729 if (match_op(token, SPECIAL_LOGICAL_AND) && 730 token_type(token->next) == TOKEN_IDENT) { 731 struct expression *label = alloc_expression(token->pos, EXPR_LABEL); 732 struct symbol *sym = label_symbol(token->next, 1); 733 if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) { 734 sym->ctype.modifiers |= MOD_ADDRESSABLE; 735 add_symbol(&function_computed_target_list, sym); 736 } 737 check_label_usage(sym, token->pos); 738 label->flags = CEF_ADDR; 739 label->label_symbol = sym; 740 *tree = label; 741 return token->next->next; 742 } 743 744 } 745 746 return postfix_expression(token, tree, NULL); 747} 748 749/* 750 * Ambiguity: a '(' can be either a cast-expression or 751 * a primary-expression depending on whether it is followed 752 * by a type or not. 753 * 754 * additional ambiguity: a "cast expression" followed by 755 * an initializer is really a postfix-expression. 756 */ 757static struct token *cast_expression(struct token *token, struct expression **tree) 758{ 759 if (match_op(token, '(')) { 760 struct token *next = token->next; 761 if (lookup_type(next)) { 762 struct expression *cast = alloc_expression(next->pos, EXPR_CAST); 763 struct expression *v; 764 struct symbol *sym; 765 int is_force; 766 767 token = typename(next, &sym, &is_force); 768 cast->cast_type = sym; 769 token = expect(token, ')', "at end of cast operator"); 770 if (match_op(token, '{')) { 771 if (toplevel(block_scope)) 772 sym->ctype.modifiers |= MOD_TOPLEVEL; 773 if (is_force) 774 warning(sym->pos, 775 "[force] in compound literal"); 776 token = initializer(&cast->cast_expression, token); 777 return postfix_expression(token, tree, cast); 778 } 779 *tree = cast; 780 if (is_force) 781 cast->type = EXPR_FORCE_CAST; 782 token = cast_expression(token, &v); 783 if (!v) 784 return token; 785 cast->cast_expression = v; 786 return token; 787 } 788 } 789 return unary_expression(token, tree); 790} 791 792/* 793 * Generic left-to-right binop parsing 794 * 795 * This _really_ needs to be inlined, because that makes the inner 796 * function call statically deterministic rather than a totally 797 * unpredictable indirect call. But gcc-3 is so "clever" that it 798 * doesn't do so by default even when you tell it to inline it. 799 * 800 * Making it a macro avoids the inlining problem, and also means 801 * that we can pass in the op-comparison as an expression rather 802 * than create a data structure for it. 803 */ 804 805#define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \ 806 struct expression *left = NULL; \ 807 struct token * next = inner(__token, &left); \ 808 \ 809 if (left) { \ 810 while (token_type(next) == TOKEN_SPECIAL) { \ 811 struct expression *top, *right = NULL; \ 812 int op = next->special; \ 813 \ 814 if (!(compare)) \ 815 goto out; \ 816 top = alloc_expression(next->pos, type); \ 817 next = inner(next->next, &right); \ 818 if (!right) { \ 819 sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \ 820 break; \ 821 } \ 822 top->op = op; \ 823 top->left = left; \ 824 top->right = right; \ 825 left = top; \ 826 } \ 827 } \ 828out: \ 829 *tree = left; \ 830 return next; \ 831 832static struct token *multiplicative_expression(struct token *token, struct expression **tree) 833{ 834 LR_BINOP_EXPRESSION( 835 token, tree, EXPR_BINOP, cast_expression, 836 (op == '*') || (op == '/') || (op == '%') 837 ); 838} 839 840static struct token *additive_expression(struct token *token, struct expression **tree) 841{ 842 LR_BINOP_EXPRESSION( 843 token, tree, EXPR_BINOP, multiplicative_expression, 844 (op == '+') || (op == '-') 845 ); 846} 847 848static struct token *shift_expression(struct token *token, struct expression **tree) 849{ 850 LR_BINOP_EXPRESSION( 851 token, tree, EXPR_BINOP, additive_expression, 852 (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT) 853 ); 854} 855 856static struct token *relational_expression(struct token *token, struct expression **tree) 857{ 858 LR_BINOP_EXPRESSION( 859 token, tree, EXPR_COMPARE, shift_expression, 860 (op == '<') || (op == '>') || 861 (op == SPECIAL_LTE) || (op == SPECIAL_GTE) 862 ); 863} 864 865static struct token *equality_expression(struct token *token, struct expression **tree) 866{ 867 LR_BINOP_EXPRESSION( 868 token, tree, EXPR_COMPARE, relational_expression, 869 (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL) 870 ); 871} 872 873static struct token *bitwise_and_expression(struct token *token, struct expression **tree) 874{ 875 LR_BINOP_EXPRESSION( 876 token, tree, EXPR_BINOP, equality_expression, 877 (op == '&') 878 ); 879} 880 881static struct token *bitwise_xor_expression(struct token *token, struct expression **tree) 882{ 883 LR_BINOP_EXPRESSION( 884 token, tree, EXPR_BINOP, bitwise_and_expression, 885 (op == '^') 886 ); 887} 888 889static struct token *bitwise_or_expression(struct token *token, struct expression **tree) 890{ 891 LR_BINOP_EXPRESSION( 892 token, tree, EXPR_BINOP, bitwise_xor_expression, 893 (op == '|') 894 ); 895} 896 897static struct token *logical_and_expression(struct token *token, struct expression **tree) 898{ 899 LR_BINOP_EXPRESSION( 900 token, tree, EXPR_LOGICAL, bitwise_or_expression, 901 (op == SPECIAL_LOGICAL_AND) 902 ); 903} 904 905static struct token *logical_or_expression(struct token *token, struct expression **tree) 906{ 907 LR_BINOP_EXPRESSION( 908 token, tree, EXPR_LOGICAL, logical_and_expression, 909 (op == SPECIAL_LOGICAL_OR) 910 ); 911} 912 913struct token *conditional_expression(struct token *token, struct expression **tree) 914{ 915 token = logical_or_expression(token, tree); 916 if (*tree && match_op(token, '?')) { 917 struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL); 918 expr->op = token->special; 919 expr->conditional = *tree; 920 *tree = expr; 921 token = parse_expression(token->next, &expr->cond_true); 922 token = expect(token, ':', "in conditional expression"); 923 token = conditional_expression(token, &expr->cond_false); 924 } 925 return token; 926} 927 928struct token *assignment_expression(struct token *token, struct expression **tree) 929{ 930 token = conditional_expression(token, tree); 931 if (*tree && token_type(token) == TOKEN_SPECIAL) { 932 static const int assignments[] = { 933 '=', 934 SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN, 935 SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN, 936 SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN, 937 SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN, 938 SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN }; 939 int i, op = token->special; 940 for (i = 0; i < ARRAY_SIZE(assignments); i++) 941 if (assignments[i] == op) { 942 struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT); 943 struct token *next = token->next; 944 expr->left = *tree; 945 expr->op = op; 946 *tree = expr; 947 token = assignment_expression(next, &expr->right); 948 if (token == next) 949 expression_error(expr, "expression expected before '%s'", show_token(token)); 950 return token; 951 } 952 } 953 return token; 954} 955 956static struct token *comma_expression(struct token *token, struct expression **tree) 957{ 958 LR_BINOP_EXPRESSION( 959 token, tree, EXPR_COMMA, assignment_expression, 960 (op == ',') 961 ); 962} 963 964struct token *parse_expression(struct token *token, struct expression **tree) 965{ 966 return comma_expression(token,tree); 967} 968 969 970