1/* 2 * Stupid C parser, version 1e-6. 3 * 4 * Let's see how hard this is to do. 5 * 6 * Copyright (C) 2003 Transmeta Corp. 7 * 2003-2004 Linus Torvalds 8 * Copyright (C) 2004 Christopher Li 9 * 10 * Permission is hereby granted, free of charge, to any person obtaining a copy 11 * of this software and associated documentation files (the "Software"), to deal 12 * in the Software without restriction, including without limitation the rights 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 * copies of the Software, and to permit persons to whom the Software is 15 * furnished to do so, subject to the following conditions: 16 * 17 * The above copyright notice and this permission notice shall be included in 18 * all copies or substantial portions of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 * THE SOFTWARE. 27 */ 28 29#include <stdarg.h> 30#include <stdlib.h> 31#include <stdio.h> 32#include <string.h> 33#include <ctype.h> 34#include <unistd.h> 35#include <fcntl.h> 36#include <limits.h> 37 38#include "lib.h" 39#include "allocate.h" 40#include "token.h" 41#include "parse.h" 42#include "symbol.h" 43#include "scope.h" 44#include "expression.h" 45#include "target.h" 46 47static struct symbol_list **function_symbol_list; 48struct symbol_list *function_computed_target_list; 49struct statement_list *function_computed_goto_list; 50 51static struct token *statement(struct token *token, struct statement **tree); 52static struct token *handle_attributes(struct token *token, struct decl_state *ctx); 53 54typedef struct token *declarator_t(struct token *, struct symbol *, struct decl_state *); 55static declarator_t 56 struct_specifier, union_specifier, enum_specifier, 57 attribute_specifier, typeof_specifier, 58 storage_specifier, thread_specifier; 59static declarator_t generic_qualifier; 60static declarator_t autotype_specifier; 61 62static struct token *parse_if_statement(struct token *token, struct statement *stmt); 63static struct token *parse_return_statement(struct token *token, struct statement *stmt); 64static struct token *parse_loop_iterator(struct token *token, struct statement *stmt); 65static struct token *parse_default_statement(struct token *token, struct statement *stmt); 66static struct token *parse_case_statement(struct token *token, struct statement *stmt); 67static struct token *parse_switch_statement(struct token *token, struct statement *stmt); 68static struct token *parse_for_statement(struct token *token, struct statement *stmt); 69static struct token *parse_while_statement(struct token *token, struct statement *stmt); 70static struct token *parse_do_statement(struct token *token, struct statement *stmt); 71static struct token *parse_goto_statement(struct token *token, struct statement *stmt); 72static struct token *parse_context_statement(struct token *token, struct statement *stmt); 73static struct token *parse_range_statement(struct token *token, struct statement *stmt); 74static struct token *parse_asm_statement(struct token *token, struct statement *stmt); 75static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list); 76static struct token *parse_static_assert(struct token *token, struct symbol_list **unused); 77 78typedef struct token *attr_t(struct token *, struct symbol *, 79 struct decl_state *); 80 81static attr_t 82 attribute_packed, attribute_aligned, attribute_modifier, 83 attribute_function, 84 attribute_bitwise, 85 attribute_address_space, attribute_context, 86 attribute_designated_init, 87 attribute_transparent_union, ignore_attribute, 88 attribute_mode, attribute_force; 89 90typedef struct symbol *to_mode_t(struct symbol *); 91 92static to_mode_t 93 to_QI_mode, to_HI_mode, to_SI_mode, to_DI_mode, to_TI_mode; 94static to_mode_t to_pointer_mode, to_word_mode; 95 96enum { 97 Set_T = 1, 98 Set_S = 2, 99 Set_Char = 4, 100 Set_Int = 8, 101 Set_Double = 16, 102 Set_Float = 32, 103 Set_Signed = 64, 104 Set_Unsigned = 128, 105 Set_Short = 256, 106 Set_Long = 512, 107 Set_Vlong = 1024, 108 Set_Int128 = 2048, 109 Set_Any = Set_T | Set_Short | Set_Long | Set_Signed | Set_Unsigned 110}; 111 112enum { 113 CInt = 0, CSInt, CUInt, CReal, 114}; 115 116static void asm_modifier(struct token *token, unsigned long *mods, unsigned long mod) 117{ 118 if (*mods & mod) 119 warning(token->pos, "duplicated asm modifier"); 120 *mods |= mod; 121} 122 123static struct symbol_op typedef_op = { 124 .type = KW_MODIFIER, 125 .declarator = storage_specifier, 126}; 127 128static struct symbol_op inline_op = { 129 .type = KW_MODIFIER, 130 .declarator = generic_qualifier, 131 .asm_modifier = asm_modifier, 132}; 133 134static struct symbol_op noreturn_op = { 135 .type = KW_MODIFIER, 136 .declarator = generic_qualifier, 137}; 138 139static declarator_t alignas_specifier; 140static struct symbol_op alignas_op = { 141 .type = KW_MODIFIER, 142 .declarator = alignas_specifier, 143}; 144 145static struct symbol_op auto_op = { 146 .type = KW_MODIFIER, 147 .declarator = storage_specifier, 148}; 149 150static struct symbol_op register_op = { 151 .type = KW_MODIFIER, 152 .declarator = storage_specifier, 153}; 154 155static struct symbol_op static_op = { 156 .type = KW_MODIFIER|KW_STATIC, 157 .declarator = storage_specifier, 158}; 159 160static struct symbol_op extern_op = { 161 .type = KW_MODIFIER, 162 .declarator = storage_specifier, 163}; 164 165static struct symbol_op thread_op = { 166 .type = KW_MODIFIER, 167 .declarator = thread_specifier, 168}; 169 170static struct symbol_op const_op = { 171 .type = KW_QUALIFIER, 172 .declarator = generic_qualifier, 173}; 174 175static struct symbol_op volatile_op = { 176 .type = KW_QUALIFIER, 177 .declarator = generic_qualifier, 178 .asm_modifier = asm_modifier, 179}; 180 181static struct symbol_op restrict_op = { 182 .type = KW_QUALIFIER, 183 .declarator = generic_qualifier, 184}; 185 186static struct symbol_op atomic_op = { 187 .type = KW_QUALIFIER, 188 .declarator = generic_qualifier, 189}; 190 191static struct symbol_op typeof_op = { 192 .type = KW_SPECIFIER, 193 .declarator = typeof_specifier, 194 .test = Set_Any, 195 .set = Set_S|Set_T, 196}; 197 198static struct symbol_op autotype_op = { 199 .type = KW_SPECIFIER, 200 .declarator = autotype_specifier, 201 .test = Set_Any, 202 .set = Set_S|Set_T, 203}; 204 205static struct symbol_op attribute_op = { 206 .type = KW_ATTRIBUTE, 207 .declarator = attribute_specifier, 208}; 209 210static struct symbol_op struct_op = { 211 .type = KW_SPECIFIER, 212 .declarator = struct_specifier, 213 .test = Set_Any, 214 .set = Set_S|Set_T, 215}; 216 217static struct symbol_op union_op = { 218 .type = KW_SPECIFIER, 219 .declarator = union_specifier, 220 .test = Set_Any, 221 .set = Set_S|Set_T, 222}; 223 224static struct symbol_op enum_op = { 225 .type = KW_SPECIFIER, 226 .declarator = enum_specifier, 227 .test = Set_Any, 228 .set = Set_S|Set_T, 229}; 230 231static struct symbol_op spec_op = { 232 .type = KW_SPECIFIER | KW_EXACT, 233 .test = Set_Any, 234 .set = Set_S|Set_T, 235}; 236 237static struct symbol_op char_op = { 238 .type = KW_SPECIFIER, 239 .test = Set_T|Set_Long|Set_Short, 240 .set = Set_T|Set_Char, 241 .class = CInt, 242}; 243 244static struct symbol_op int_op = { 245 .type = KW_SPECIFIER, 246 .test = Set_T, 247 .set = Set_T|Set_Int, 248}; 249 250static struct symbol_op double_op = { 251 .type = KW_SPECIFIER, 252 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Vlong, 253 .set = Set_T|Set_Double, 254 .class = CReal, 255}; 256 257static struct symbol_op float_op = { 258 .type = KW_SPECIFIER, 259 .test = Set_T|Set_Signed|Set_Unsigned|Set_Short|Set_Long, 260 .set = Set_T|Set_Float, 261 .class = CReal, 262}; 263 264static struct symbol_op short_op = { 265 .type = KW_SPECIFIER, 266 .test = Set_S|Set_Char|Set_Float|Set_Double|Set_Long|Set_Short, 267 .set = Set_Short, 268}; 269 270static struct symbol_op signed_op = { 271 .type = KW_SPECIFIER, 272 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned, 273 .set = Set_Signed, 274 .class = CSInt, 275}; 276 277static struct symbol_op unsigned_op = { 278 .type = KW_SPECIFIER, 279 .test = Set_S|Set_Float|Set_Double|Set_Signed|Set_Unsigned, 280 .set = Set_Unsigned, 281 .class = CUInt, 282}; 283 284static struct symbol_op long_op = { 285 .type = KW_SPECIFIER, 286 .test = Set_S|Set_Char|Set_Float|Set_Short|Set_Vlong, 287 .set = Set_Long, 288}; 289 290static struct symbol_op int128_op = { 291 .type = KW_SPECIFIER, 292 .test = Set_S|Set_T|Set_Char|Set_Short|Set_Int|Set_Float|Set_Double|Set_Long|Set_Vlong|Set_Int128, 293 .set = Set_T|Set_Int128|Set_Vlong, 294 .class = CInt, 295}; 296 297static struct symbol_op if_op = { 298 .statement = parse_if_statement, 299}; 300 301static struct symbol_op return_op = { 302 .statement = parse_return_statement, 303}; 304 305static struct symbol_op loop_iter_op = { 306 .statement = parse_loop_iterator, 307}; 308 309static struct symbol_op default_op = { 310 .statement = parse_default_statement, 311}; 312 313static struct symbol_op case_op = { 314 .statement = parse_case_statement, 315}; 316 317static struct symbol_op switch_op = { 318 .statement = parse_switch_statement, 319}; 320 321static struct symbol_op for_op = { 322 .statement = parse_for_statement, 323}; 324 325static struct symbol_op while_op = { 326 .statement = parse_while_statement, 327}; 328 329static struct symbol_op do_op = { 330 .statement = parse_do_statement, 331}; 332 333static struct symbol_op goto_op = { 334 .statement = parse_goto_statement, 335}; 336 337static struct symbol_op __context___op = { 338 .statement = parse_context_statement, 339 .attribute = attribute_context, 340}; 341 342static struct symbol_op range_op = { 343 .statement = parse_range_statement, 344}; 345 346static struct symbol_op asm_op = { 347 .type = KW_ASM, 348 .statement = parse_asm_statement, 349 .toplevel = toplevel_asm_declaration, 350}; 351 352static struct symbol_op static_assert_op = { 353 .toplevel = parse_static_assert, 354}; 355 356static struct symbol_op packed_op = { 357 .attribute = attribute_packed, 358}; 359 360static struct symbol_op aligned_op = { 361 .attribute = attribute_aligned, 362}; 363 364static struct symbol_op attr_mod_op = { 365 .attribute = attribute_modifier, 366}; 367 368static struct symbol_op attr_fun_op = { 369 .attribute = attribute_function, 370}; 371 372static struct symbol_op attr_bitwise_op = { 373 .attribute = attribute_bitwise, 374}; 375 376static struct symbol_op attr_force_op = { 377 .attribute = attribute_force, 378}; 379 380static struct symbol_op address_space_op = { 381 .attribute = attribute_address_space, 382}; 383 384static struct symbol_op mode_op = { 385 .attribute = attribute_mode, 386}; 387 388static struct symbol_op context_op = { 389 .attribute = attribute_context, 390}; 391 392static struct symbol_op designated_init_op = { 393 .attribute = attribute_designated_init, 394}; 395 396static struct symbol_op transparent_union_op = { 397 .attribute = attribute_transparent_union, 398}; 399 400static struct symbol_op ignore_attr_op = { 401 .attribute = ignore_attribute, 402}; 403 404static struct symbol_op mode_QI_op = { 405 .type = KW_MODE, 406 .to_mode = to_QI_mode 407}; 408 409static struct symbol_op mode_HI_op = { 410 .type = KW_MODE, 411 .to_mode = to_HI_mode 412}; 413 414static struct symbol_op mode_SI_op = { 415 .type = KW_MODE, 416 .to_mode = to_SI_mode 417}; 418 419static struct symbol_op mode_DI_op = { 420 .type = KW_MODE, 421 .to_mode = to_DI_mode 422}; 423 424static struct symbol_op mode_TI_op = { 425 .type = KW_MODE, 426 .to_mode = to_TI_mode 427}; 428 429static struct symbol_op mode_pointer_op = { 430 .type = KW_MODE, 431 .to_mode = to_pointer_mode 432}; 433 434static struct symbol_op mode_word_op = { 435 .type = KW_MODE, 436 .to_mode = to_word_mode 437}; 438 439/* 440 * Define the keyword and their effects. 441 * The entries in the 'typedef' and put in NS_TYPEDEF and 442 * are automatically set as reserved keyword while the ones 443 * in the 'keyword' table are just put in NS_KEYWORD. 444 * 445 * The entries are added via the 3 macros: 446 * N() for entries with "name" only, 447 * D() for entries with "name" & "__name__", 448 * A() for entries with "name", "__name" & "__name__", 449 * U() for entries with "__name" & "__name__". 450 */ 451static struct init_keyword { 452 const char *name; 453 struct symbol_op *op; 454 struct symbol *type; 455 unsigned long mods; 456} typedefs[] = { 457#define N(I, O,...) { I, O,##__VA_ARGS__ } 458#define D(I, O,...) N(I,O,##__VA_ARGS__ ), \ 459 N("__" I "__",O,##__VA_ARGS__) 460#define A(I, O,...) N(I,O,##__VA_ARGS__ ), \ 461 N("__" I,O,##__VA_ARGS__), \ 462 N("__" I "__",O,##__VA_ARGS__) 463#define U(I, O,...) N("__" I,O,##__VA_ARGS__), \ 464 N("__" I "__",O,##__VA_ARGS__) 465 /* Storage classes */ 466 N("auto", &auto_op, .mods = MOD_AUTO), 467 N("register", ®ister_op, .mods = MOD_REGISTER), 468 N("static", &static_op, .mods = MOD_STATIC), 469 N("extern", &extern_op, .mods = MOD_EXTERN), 470 N("__thread", &thread_op), 471 N("_Thread_local", &thread_op), 472 473 A("inline", &inline_op, .mods = MOD_INLINE), 474 475 /* Typedef ... */ 476 N("typedef", &typedef_op, .mods = MOD_USERTYPE), 477 A("typeof", &typeof_op), 478 N("__auto_type", &autotype_op), 479 480 /* Type qualifiers */ 481 A("const", &const_op, .mods = MOD_CONST), 482 A("volatile", &volatile_op, .mods = MOD_VOLATILE), 483 A("restrict", &restrict_op, .mods = MOD_RESTRICT), 484 485 N("_Atomic", &atomic_op, .mods = MOD_ATOMIC), 486 N("_Noreturn", &noreturn_op, .mods = MOD_NORETURN), 487 N("_Alignas", &alignas_op), 488 489 U("attribute", &attribute_op), 490 491 /* Type specifiers */ 492 N("struct", &struct_op), 493 N("union", &union_op), 494 N("enum", &enum_op), 495 496 N("void", &spec_op, .type = &void_ctype), 497 N("char", &char_op), 498 N("short", &short_op), 499 N("int", &int_op), 500 N("long", &long_op), 501 N("float", &float_op), 502 N("double", &double_op), 503 A("signed", &signed_op), 504 N("unsigned", &unsigned_op), 505 N("__int128", &int128_op), 506 N("_Bool", &spec_op, .type = &bool_ctype), 507 508 /* Predeclared types */ 509 N("__builtin_va_list", &spec_op, .type = &ptr_ctype), 510 N("__builtin_ms_va_list",&spec_op, .type = &ptr_ctype), 511 N("__int128_t", &spec_op, .type = &sint128_ctype), 512 N("__uint128_t", &spec_op, .type = &uint128_ctype), 513 N("_Float32", &spec_op, .type = &float32_ctype), 514 N("_Float32x", &spec_op, .type = &float32x_ctype), 515 N("_Float64", &spec_op, .type = &float64_ctype), 516 N("_Float64x", &spec_op, .type = &float64x_ctype), 517 N("_Float128", &spec_op, .type = &float128_ctype), 518}, keywords[] = { 519 /* Statements */ 520 N("if", &if_op), 521 N("return", &return_op), 522 N("break", &loop_iter_op), 523 N("continue", &loop_iter_op), 524 N("default", &default_op), 525 N("case", &case_op), 526 N("switch", &switch_op), 527 N("for", &for_op), 528 N("while", &while_op), 529 N("do", &do_op), 530 N("goto", &goto_op), 531 A("asm", &asm_op), 532 N("context", &context_op), 533 N("__context__", &__context___op), 534 N("__range__", &range_op), 535 N("_Static_assert", &static_assert_op), 536 537 /* Attributes */ 538 D("packed", &packed_op), 539 D("aligned", &aligned_op), 540 D("nocast", &attr_mod_op, .mods = MOD_NOCAST), 541 D("noderef", &attr_mod_op, .mods = MOD_NODEREF), 542 D("safe", &attr_mod_op, .mods = MOD_SAFE), 543 D("unused", &attr_mod_op, .mods = MOD_UNUSED), 544 D("externally_visible", &attr_mod_op, .mods = MOD_EXT_VISIBLE), 545 D("force", &attr_force_op), 546 D("bitwise", &attr_bitwise_op, .mods = MOD_BITWISE), 547 D("address_space", &address_space_op), 548 D("designated_init", &designated_init_op), 549 D("transparent_union", &transparent_union_op), 550 D("noreturn", &attr_fun_op, .mods = MOD_NORETURN), 551 D("pure", &attr_fun_op, .mods = MOD_PURE), 552 A("const", &attr_fun_op, .mods = MOD_PURE), 553 D("gnu_inline", &attr_fun_op, .mods = MOD_GNU_INLINE), 554 555 /* Modes */ 556 D("mode", &mode_op), 557 D("QI", &mode_QI_op), 558 D("HI", &mode_HI_op), 559 D("SI", &mode_SI_op), 560 D("DI", &mode_DI_op), 561 D("TI", &mode_TI_op), 562 D("byte", &mode_QI_op), 563 D("pointer", &mode_pointer_op), 564 D("word", &mode_word_op), 565}; 566 567 568static const char *ignored_attributes[] = { 569 570#define GCC_ATTR(x) \ 571 STRINGIFY(x), \ 572 STRINGIFY(__##x##__), 573 574#include "gcc-attr-list.h" 575 576#undef GCC_ATTR 577 578 "bounded", 579 "__bounded__", 580 "__noclone", 581 "__nonnull", 582 "__nothrow", 583}; 584 585 586static void init_keyword(int stream, struct init_keyword *kw, enum namespace ns) 587{ 588 struct symbol *sym = create_symbol(stream, kw->name, SYM_KEYWORD, ns); 589 sym->ident->keyword = 1; 590 sym->ident->reserved |= (ns == NS_TYPEDEF); 591 sym->ctype.modifiers = kw->mods; 592 sym->ctype.base_type = kw->type; 593 sym->op = kw->op; 594} 595 596void init_parser(int stream) 597{ 598 int i; 599 600 for (i = 0; i < ARRAY_SIZE(typedefs); i++) 601 init_keyword(stream, &typedefs[i], NS_TYPEDEF); 602 for (i = 0; i < ARRAY_SIZE(keywords); i++) 603 init_keyword(stream, &keywords[i], NS_KEYWORD); 604 605 for (i = 0; i < ARRAY_SIZE(ignored_attributes); i++) { 606 const char * name = ignored_attributes[i]; 607 struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD, 608 NS_KEYWORD); 609 if (!sym->op) { 610 sym->ident->keyword = 1; 611 sym->op = &ignore_attr_op; 612 } 613 } 614} 615 616 617static struct token *skip_to(struct token *token, int op) 618{ 619 while (!match_op(token, op) && !eof_token(token)) 620 token = token->next; 621 return token; 622} 623 624static struct token bad_token = { .pos.type = TOKEN_BAD }; 625struct token *expect(struct token *token, int op, const char *where) 626{ 627 if (!match_op(token, op)) { 628 if (token != &bad_token) { 629 bad_token.next = token; 630 sparse_error(token->pos, "Expected %s %s", show_special(op), where); 631 sparse_error(token->pos, "got %s", show_token(token)); 632 } 633 if (op == ';') 634 return skip_to(token, op); 635 return &bad_token; 636 } 637 return token->next; 638} 639 640/// 641// issue an error message on new parsing errors 642// @token: the current token 643// @errmsg: the error message 644// If the current token is from a previous error, an error message 645// has already been issued, so nothing more is done. 646// Otherwise, @errmsg is displayed followed by the current token. 647static void unexpected(struct token *token, const char *errmsg) 648{ 649 if (token == &bad_token) 650 return; 651 sparse_error(token->pos, "%s", errmsg); 652 sparse_error(token->pos, "got %s", show_token(token)); 653} 654 655// Add a symbol to the list of function-local symbols 656static void fn_local_symbol(struct symbol *sym) 657{ 658 if (function_symbol_list) 659 add_symbol(function_symbol_list, sym); 660} 661 662struct statement *alloc_statement(struct position pos, int type) 663{ 664 struct statement *stmt = __alloc_statement(0); 665 stmt->type = type; 666 stmt->pos = pos; 667 return stmt; 668} 669 670static struct token *struct_declaration_list(struct token *token, struct symbol_list **list); 671 672static void apply_ctype(struct position pos, struct ctype *dst, struct ctype *src); 673 674static void apply_modifiers(struct position pos, struct decl_state *ctx) 675{ 676 struct symbol *ctype; 677 if (!ctx->mode) 678 return; 679 ctype = ctx->mode->to_mode(ctx->ctype.base_type); 680 if (!ctype) 681 sparse_error(pos, "don't know how to apply mode to %s", 682 show_typename(ctx->ctype.base_type)); 683 else 684 ctx->ctype.base_type = ctype; 685 686} 687 688static struct symbol * alloc_indirect_symbol(struct position pos, struct ctype *ctype, int type) 689{ 690 struct symbol *sym = alloc_symbol(pos, type); 691 692 sym->ctype.base_type = ctype->base_type; 693 sym->ctype.modifiers = ctype->modifiers; 694 695 ctype->base_type = sym; 696 ctype->modifiers = 0; 697 return sym; 698} 699 700/* 701 * NOTE! NS_LABEL is not just a different namespace, 702 * it also ends up using function scope instead of the 703 * regular symbol scope. 704 */ 705struct symbol *label_symbol(struct token *token, int used) 706{ 707 struct symbol *sym = lookup_symbol(token->ident, NS_LABEL); 708 if (!sym) { 709 sym = alloc_symbol(token->pos, SYM_LABEL); 710 bind_symbol(sym, token->ident, NS_LABEL); 711 if (used) 712 sym->used = 1; 713 fn_local_symbol(sym); 714 } 715 return sym; 716} 717 718static struct token *struct_union_enum_specifier(enum type type, 719 struct token *token, struct decl_state *ctx, 720 struct token *(*parse)(struct token *, struct symbol *)) 721{ 722 struct decl_state attr = { }; 723 struct symbol *sym; 724 struct position *repos; 725 726 token = handle_attributes(token, &attr); 727 if (token_type(token) == TOKEN_IDENT) { 728 sym = lookup_symbol(token->ident, NS_STRUCT); 729 if (!sym || 730 (is_outer_scope(sym->scope) && 731 (match_op(token->next,';') || match_op(token->next,'{')))) { 732 // Either a new symbol, or else an out-of-scope 733 // symbol being redefined. 734 sym = alloc_symbol(token->pos, type); 735 bind_symbol(sym, token->ident, NS_STRUCT); 736 } 737 if (sym->type != type) 738 error_die(token->pos, "invalid tag applied to %s", show_typename (sym)); 739 ctx->ctype.base_type = sym; 740 repos = &token->pos; 741 token = token->next; 742 if (!match_op(token, '{')) 743 return token; 744 745 // The following test is actually wrong for empty 746 // structs, but (1) they are not C99, (2) gcc does 747 // the same thing, and (3) it's easier. 748 if (sym->symbol_list) 749 error_die(token->pos, "redefinition of %s", show_typename (sym)); 750 sym->pos = *repos; 751 752 // Mark the structure as needing re-examination 753 sym->examined = 0; 754 } else if (match_op(token, '{')) { 755 // private struct/union/enum type 756 sym = alloc_symbol(token->pos, type); 757 set_current_scope(sym); // used by dissect 758 ctx->ctype.base_type = sym; 759 } else { 760 sparse_error(token->pos, "expected declaration"); 761 ctx->ctype.base_type = &bad_ctype; 762 return token; 763 } 764 765 token = parse(token->next, sym); 766 token = expect(token, '}', "at end of specifier"); 767 attr.ctype.base_type = sym; 768 token = handle_attributes(token, &attr); 769 apply_ctype(token->pos, &sym->ctype, &attr.ctype); 770 sym->packed = attr.packed; 771 772 sym->endpos = token->pos; 773 774 return token; 775} 776 777static struct token *parse_struct_declaration(struct token *token, struct symbol *sym) 778{ 779 struct symbol *field, *last = NULL; 780 struct token *res; 781 res = struct_declaration_list(token, &sym->symbol_list); 782 FOR_EACH_PTR(sym->symbol_list, field) { 783 if (!field->ident) { 784 struct symbol *base = field->ctype.base_type; 785 if (base && base->type == SYM_BITFIELD) 786 continue; 787 } 788 if (last) 789 last->next_subobject = field; 790 last = field; 791 } END_FOR_EACH_PTR(field); 792 return res; 793} 794 795static struct token *parse_union_declaration(struct token *token, struct symbol *sym) 796{ 797 return struct_declaration_list(token, &sym->symbol_list); 798} 799 800static struct token *struct_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 801{ 802 return struct_union_enum_specifier(SYM_STRUCT, token, ctx, parse_struct_declaration); 803} 804 805static struct token *union_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 806{ 807 return struct_union_enum_specifier(SYM_UNION, token, ctx, parse_union_declaration); 808} 809 810/// 811// safe right shift 812// 813// This allow to use a shift amount as big (or bigger) 814// than the width of the value to be shifted, in which case 815// the result is, of course, 0. 816static unsigned long long rshift(unsigned long long val, unsigned int n) 817{ 818 if (n >= (sizeof(val) * 8)) 819 return 0; 820 return val >> n; 821} 822 823struct range { 824 long long neg; 825 unsigned long long pos; 826}; 827 828static void update_range(struct range *range, unsigned long long uval, struct symbol *vtype) 829{ 830 long long sval = uval; 831 832 if (is_signed_type(vtype) && (sval < 0)) { 833 if (sval < range->neg) 834 range->neg = sval; 835 } else { 836 if (uval > range->pos) 837 range->pos = uval; 838 } 839} 840 841static int type_is_ok(struct symbol *type, struct range range) 842{ 843 int shift = type->bit_size; 844 int is_unsigned = type->ctype.modifiers & MOD_UNSIGNED; 845 846 if (!is_unsigned) 847 shift--; 848 if (rshift(range.pos, shift)) 849 return 0; 850 if (range.neg == 0) 851 return 1; 852 if (is_unsigned) 853 return 0; 854 if (rshift(~range.neg, shift)) 855 return 0; 856 return 1; 857} 858 859static struct range type_range(struct symbol *type) 860{ 861 struct range range; 862 unsigned int size = type->bit_size; 863 unsigned long long max; 864 long long min; 865 866 if (is_signed_type(type)) { 867 min = sign_bit(size); 868 max = min - 1; 869 } else { 870 min = 0; 871 max = bits_mask(size); 872 } 873 874 range.pos = max; 875 range.neg = min; 876 return range; 877} 878 879static int val_in_range(struct range *range, long long sval, struct symbol *vtype) 880{ 881 unsigned long long uval = sval; 882 883 if (is_signed_type(vtype) && (sval < 0)) 884 return range->neg <= sval; 885 else 886 return uval <= range->pos; 887} 888 889static void cast_enum_list(struct symbol_list *list, struct symbol *base_type) 890{ 891 struct range irange = type_range(&int_ctype); 892 struct symbol *sym; 893 894 FOR_EACH_PTR(list, sym) { 895 struct expression *expr = sym->initializer; 896 struct symbol *ctype; 897 long long val; 898 if (expr->type != EXPR_VALUE) 899 continue; 900 ctype = expr->ctype; 901 val = get_expression_value(expr); 902 if (is_int_type(ctype) && val_in_range(&irange, val, ctype)) { 903 expr->ctype = &int_ctype; 904 continue; 905 } 906 cast_value(expr, base_type, expr, ctype); 907 expr->ctype = base_type; 908 } END_FOR_EACH_PTR(sym); 909} 910 911static struct token *parse_enum_declaration(struct token *token, struct symbol *parent) 912{ 913 unsigned long long lastval = 0; 914 struct symbol *ctype = NULL, *base_type = NULL; 915 struct range range = { }; 916 int mix_bitwise = 0; 917 918 parent->examined = 1; 919 parent->ctype.base_type = &int_ctype; 920 while (token_type(token) == TOKEN_IDENT) { 921 struct expression *expr = NULL; 922 struct token *next = token->next; 923 struct decl_state ctx = { }; 924 struct symbol *sym; 925 926 // FIXME: only 'deprecated' should be accepted 927 next = handle_attributes(next, &ctx); 928 929 if (match_op(next, '=')) { 930 next = constant_expression(next->next, &expr); 931 lastval = get_expression_value(expr); 932 ctype = &void_ctype; 933 if (expr && expr->ctype) 934 ctype = expr->ctype; 935 } else if (!ctype) { 936 ctype = &int_ctype; 937 } else if (is_int_type(ctype)) { 938 lastval++; 939 } else { 940 error_die(token->pos, "can't increment the last enum member"); 941 } 942 943 if (!expr) { 944 expr = alloc_expression(token->pos, EXPR_VALUE); 945 expr->value = lastval; 946 expr->ctype = ctype; 947 } 948 949 sym = alloc_symbol(token->pos, SYM_NODE); 950 bind_symbol(sym, token->ident, NS_SYMBOL); 951 sym->ctype.modifiers &= ~MOD_ADDRESSABLE; 952 sym->initializer = expr; 953 sym->enum_member = 1; 954 sym->ctype.base_type = parent; 955 add_ptr_list(&parent->symbol_list, sym); 956 957 if (base_type != &bad_ctype) { 958 if (ctype->type == SYM_NODE) 959 ctype = ctype->ctype.base_type; 960 if (ctype->type == SYM_ENUM) { 961 if (ctype == parent) 962 ctype = base_type; 963 else 964 ctype = ctype->ctype.base_type; 965 } 966 /* 967 * base_type rules: 968 * - if all enums are of the same type, then 969 * the base_type is that type (two first 970 * cases) 971 * - if enums are of different types, they 972 * all have to be integer types, and the 973 * base type is at least "int_ctype". 974 * - otherwise the base_type is "bad_ctype". 975 */ 976 if (!base_type || ctype == &bad_ctype) { 977 base_type = ctype; 978 } else if (ctype == base_type) { 979 /* nothing */ 980 } else if (is_int_type(base_type) && is_int_type(ctype)) { 981 base_type = &int_ctype; 982 } else if (is_restricted_type(base_type) != is_restricted_type(ctype)) { 983 if (!mix_bitwise++) { 984 warning(expr->pos, "mixed bitwiseness"); 985 } 986 } else if (is_restricted_type(base_type) && base_type != ctype) { 987 sparse_error(expr->pos, "incompatible restricted type"); 988 info(expr->pos, " expected: %s", show_typename(base_type)); 989 info(expr->pos, " got: %s", show_typename(ctype)); 990 base_type = &bad_ctype; 991 } else if (base_type != &bad_ctype) { 992 sparse_error(token->pos, "bad enum definition"); 993 base_type = &bad_ctype; 994 } 995 parent->ctype.base_type = base_type; 996 } 997 if (is_int_type(base_type)) { 998 update_range(&range, lastval, ctype); 999 } 1000 token = next; 1001 1002 sym->endpos = token->pos; 1003 1004 if (!match_op(token, ',')) 1005 break; 1006 token = token->next; 1007 } 1008 if (!base_type) { 1009 sparse_error(token->pos, "empty enum definition"); 1010 base_type = &bad_ctype; 1011 } 1012 else if (!is_int_type(base_type)) 1013 ; 1014 else if (type_is_ok(&uint_ctype, range)) 1015 base_type = &uint_ctype; 1016 else if (type_is_ok(&int_ctype, range)) 1017 base_type = &int_ctype; 1018 else if (type_is_ok(&ulong_ctype, range)) 1019 base_type = &ulong_ctype; 1020 else if (type_is_ok(&long_ctype, range)) 1021 base_type = &long_ctype; 1022 else if (type_is_ok(&ullong_ctype, range)) 1023 base_type = &ullong_ctype; 1024 else if (type_is_ok(&llong_ctype, range)) 1025 base_type = &llong_ctype; 1026 else 1027 base_type = &bad_ctype; 1028 parent->ctype.base_type = base_type; 1029 parent->ctype.modifiers |= (base_type->ctype.modifiers & MOD_UNSIGNED); 1030 parent->examined = 0; 1031 1032 if (mix_bitwise) 1033 return token; 1034 cast_enum_list(parent->symbol_list, base_type); 1035 1036 return token; 1037} 1038 1039static struct token *enum_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1040{ 1041 struct token *ret = struct_union_enum_specifier(SYM_ENUM, token, ctx, parse_enum_declaration); 1042 struct ctype *ctype = &ctx->ctype.base_type->ctype; 1043 1044 if (!ctype->base_type) 1045 ctype->base_type = &incomplete_ctype; 1046 1047 return ret; 1048} 1049 1050static struct token *typeof_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1051{ 1052 1053 if (!match_op(token, '(')) { 1054 sparse_error(token->pos, "expected '(' after typeof"); 1055 return token; 1056 } 1057 if (lookup_type(token->next)) { 1058 struct symbol *sym; 1059 token = typename(token->next, &sym, NULL); 1060 ctx->ctype.base_type = sym->ctype.base_type; 1061 apply_ctype(token->pos, &ctx->ctype, &sym->ctype); 1062 } else { 1063 struct symbol *typeof_sym = alloc_symbol(token->pos, SYM_TYPEOF); 1064 token = parse_expression(token->next, &typeof_sym->initializer); 1065 1066 typeof_sym->endpos = token->pos; 1067 if (!typeof_sym->initializer) { 1068 sparse_error(token->pos, "expected expression after the '(' token"); 1069 typeof_sym = &bad_ctype; 1070 } 1071 ctx->ctype.base_type = typeof_sym; 1072 } 1073 return expect(token, ')', "after typeof"); 1074} 1075 1076static struct token *autotype_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1077{ 1078 ctx->ctype.base_type = &autotype_ctype; 1079 ctx->autotype = 1; 1080 return token; 1081} 1082 1083static struct token *ignore_attribute(struct token *token, struct symbol *attr, struct decl_state *ctx) 1084{ 1085 struct expression *expr = NULL; 1086 if (match_op(token, '(')) 1087 token = parens_expression(token, &expr, "in attribute"); 1088 return token; 1089} 1090 1091static struct token *attribute_packed(struct token *token, struct symbol *attr, struct decl_state *ctx) 1092{ 1093 if (!ctx->ctype.alignment) { 1094 ctx->ctype.alignment = 1; 1095 ctx->packed = 1; 1096 } 1097 return token; 1098} 1099 1100static struct token *attribute_aligned(struct token *token, struct symbol *attr, struct decl_state *ctx) 1101{ 1102 int alignment = max_alignment; 1103 struct expression *expr = NULL; 1104 1105 if (match_op(token, '(')) { 1106 token = parens_expression(token, &expr, "in attribute"); 1107 if (expr) 1108 alignment = const_expression_value(expr); 1109 } 1110 if (alignment & (alignment-1)) { 1111 warning(token->pos, "I don't like non-power-of-2 alignments"); 1112 return token; 1113 } else if (alignment > ctx->ctype.alignment) 1114 ctx->ctype.alignment = alignment; 1115 return token; 1116} 1117 1118static void apply_mod(struct position *pos, unsigned long *mods, unsigned long mod) 1119{ 1120 if (*mods & mod & ~MOD_DUP_OK) 1121 warning(*pos, "duplicate %s", modifier_name(mod)); 1122 *mods |= mod; 1123} 1124 1125static void apply_qualifier(struct position *pos, struct ctype *ctx, unsigned long qual) 1126{ 1127 apply_mod(pos, &ctx->modifiers, qual); 1128} 1129 1130static struct token *attribute_modifier(struct token *token, struct symbol *attr, struct decl_state *ctx) 1131{ 1132 apply_mod(&token->pos, &ctx->ctype.modifiers, attr->ctype.modifiers); 1133 return token; 1134} 1135 1136static struct token *attribute_function(struct token *token, struct symbol *attr, struct decl_state *ctx) 1137{ 1138 apply_mod(&token->pos, &ctx->f_modifiers, attr->ctype.modifiers); 1139 return token; 1140} 1141 1142static struct token *attribute_bitwise(struct token *token, struct symbol *attr, struct decl_state *ctx) 1143{ 1144 if (Wbitwise) 1145 attribute_modifier(token, attr, ctx); 1146 return token; 1147} 1148 1149static struct ident *numerical_address_space(int asn) 1150{ 1151 char buff[32]; 1152 1153 if (!asn) 1154 return NULL; 1155 sprintf(buff, "<asn:%d>", asn); 1156 return built_in_ident(buff); 1157} 1158 1159static struct token *attribute_address_space(struct token *token, struct symbol *attr, struct decl_state *ctx) 1160{ 1161 struct expression *expr = NULL; 1162 struct ident *as = NULL; 1163 struct token *next; 1164 1165 token = expect(token, '(', "after address_space attribute"); 1166 switch (token_type(token)) { 1167 case TOKEN_NUMBER: 1168 next = primary_expression(token, &expr); 1169 if (expr->type != EXPR_VALUE) 1170 goto invalid; 1171 as = numerical_address_space(expr->value); 1172 break; 1173 case TOKEN_IDENT: 1174 next = token->next; 1175 as = token->ident; 1176 break; 1177 default: 1178 next = token->next; 1179 invalid: 1180 as = NULL; 1181 warning(token->pos, "invalid address space name"); 1182 } 1183 1184 if (Waddress_space && as) { 1185 if (ctx->ctype.as) 1186 sparse_error(token->pos, 1187 "multiple address spaces given: %s & %s", 1188 show_as(ctx->ctype.as), show_as(as)); 1189 ctx->ctype.as = as; 1190 } 1191 token = expect(next, ')', "after address_space attribute"); 1192 return token; 1193} 1194 1195static struct symbol *to_QI_mode(struct symbol *ctype) 1196{ 1197 if (ctype->ctype.base_type != &int_type) 1198 return NULL; 1199 if (ctype == &char_ctype) 1200 return ctype; 1201 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uchar_ctype 1202 : &schar_ctype; 1203} 1204 1205static struct symbol *to_HI_mode(struct symbol *ctype) 1206{ 1207 if (ctype->ctype.base_type != &int_type) 1208 return NULL; 1209 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ushort_ctype 1210 : &sshort_ctype; 1211} 1212 1213static struct symbol *to_SI_mode(struct symbol *ctype) 1214{ 1215 if (ctype->ctype.base_type != &int_type) 1216 return NULL; 1217 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint_ctype 1218 : &sint_ctype; 1219} 1220 1221static struct symbol *to_DI_mode(struct symbol *ctype) 1222{ 1223 if (ctype->ctype.base_type != &int_type) 1224 return NULL; 1225 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ullong_ctype 1226 : &sllong_ctype; 1227} 1228 1229static struct symbol *to_TI_mode(struct symbol *ctype) 1230{ 1231 if (ctype->ctype.base_type != &int_type) 1232 return NULL; 1233 return ctype->ctype.modifiers & MOD_UNSIGNED ? &uint128_ctype 1234 : &sint128_ctype; 1235} 1236 1237static struct symbol *to_pointer_mode(struct symbol *ctype) 1238{ 1239 if (ctype->ctype.base_type != &int_type) 1240 return NULL; 1241 return ctype->ctype.modifiers & MOD_UNSIGNED ? uintptr_ctype 1242 : intptr_ctype; 1243} 1244 1245static struct symbol *to_word_mode(struct symbol *ctype) 1246{ 1247 if (ctype->ctype.base_type != &int_type) 1248 return NULL; 1249 return ctype->ctype.modifiers & MOD_UNSIGNED ? &ulong_ctype 1250 : &slong_ctype; 1251} 1252 1253static struct token *attribute_mode(struct token *token, struct symbol *attr, struct decl_state *ctx) 1254{ 1255 token = expect(token, '(', "after mode attribute"); 1256 if (token_type(token) == TOKEN_IDENT) { 1257 struct symbol *mode = lookup_keyword(token->ident, NS_KEYWORD); 1258 if (mode && mode->op->type & KW_MODE) 1259 ctx->mode = mode->op; 1260 else 1261 sparse_error(token->pos, "unknown mode attribute %s", show_ident(token->ident)); 1262 token = token->next; 1263 } else 1264 sparse_error(token->pos, "expect attribute mode symbol\n"); 1265 token = expect(token, ')', "after mode attribute"); 1266 return token; 1267} 1268 1269static struct token *attribute_context(struct token *token, struct symbol *attr, struct decl_state *ctx) 1270{ 1271 struct context *context = alloc_context(); 1272 struct expression *args[3]; 1273 int idx = 0; 1274 1275 token = expect(token, '(', "after context attribute"); 1276 token = conditional_expression(token, &args[0]); 1277 token = expect(token, ',', "after context 1st argument"); 1278 token = conditional_expression(token, &args[1]); 1279 if (match_op(token, ',')) { 1280 token = token->next; 1281 token = conditional_expression(token, &args[2]); 1282 token = expect(token, ')', "after context 3rd argument"); 1283 context->context = args[0]; 1284 idx++; 1285 } else { 1286 token = expect(token, ')', "after context 2nd argument"); 1287 } 1288 context->in = get_expression_value(args[idx++]); 1289 context->out = get_expression_value(args[idx++]); 1290 add_ptr_list(&ctx->ctype.contexts, context); 1291 return token; 1292} 1293 1294static struct token *attribute_designated_init(struct token *token, struct symbol *attr, struct decl_state *ctx) 1295{ 1296 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_STRUCT) 1297 ctx->ctype.base_type->designated_init = 1; 1298 else 1299 warning(token->pos, "attribute designated_init applied to non-structure type"); 1300 return token; 1301} 1302 1303static struct token *attribute_transparent_union(struct token *token, struct symbol *attr, struct decl_state *ctx) 1304{ 1305 if (Wtransparent_union) 1306 warning(token->pos, "attribute __transparent_union__"); 1307 1308 if (ctx->ctype.base_type && ctx->ctype.base_type->type == SYM_UNION) 1309 ctx->ctype.base_type->transparent_union = 1; 1310 else 1311 warning(token->pos, "attribute __transparent_union__ applied to non-union type"); 1312 return token; 1313} 1314 1315static struct token *recover_unknown_attribute(struct token *token) 1316{ 1317 struct expression *expr = NULL; 1318 1319 if (Wunknown_attribute) 1320 warning(token->pos, "unknown attribute '%s'", show_ident(token->ident)); 1321 token = token->next; 1322 if (match_op(token, '(')) 1323 token = parens_expression(token, &expr, "in attribute"); 1324 return token; 1325} 1326 1327static struct token *attribute_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1328{ 1329 token = expect(token, '(', "after attribute"); 1330 token = expect(token, '(', "after attribute"); 1331 1332 while (token_type(token) == TOKEN_IDENT) { 1333 struct symbol *attr = lookup_keyword(token->ident, NS_KEYWORD); 1334 if (attr && attr->op->attribute) 1335 token = attr->op->attribute(token->next, attr, ctx); 1336 else 1337 token = recover_unknown_attribute(token); 1338 1339 if (!match_op(token, ',')) 1340 break; 1341 token = token->next; 1342 } 1343 1344 token = expect(token, ')', "after attribute"); 1345 token = expect(token, ')', "after attribute"); 1346 return token; 1347} 1348 1349static unsigned long decl_modifiers(struct decl_state *ctx) 1350{ 1351 unsigned long mods = ctx->ctype.modifiers & MOD_DECLARE; 1352 ctx->ctype.modifiers &= ~MOD_DECLARE; 1353 return ctx->storage_class | mods; 1354} 1355 1356static struct token *storage_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx) 1357{ 1358 int is_tls = ctx->ctype.modifiers & MOD_TLS; 1359 unsigned long class = sym->ctype.modifiers; 1360 const char *storage = modifier_name(class); 1361 1362 /* __thread can be used alone, or with extern or static */ 1363 if (is_tls && (class & ~(MOD_STATIC|MOD_EXTERN))) 1364 sparse_error(next->pos, "__thread cannot be used with '%s'", storage); 1365 else if (!ctx->storage_class) 1366 ctx->storage_class = class; 1367 else if (ctx->storage_class == class) 1368 sparse_error(next->pos, "duplicate %s", storage); 1369 else 1370 sparse_error(next->pos, "multiple storage classes"); 1371 return next; 1372} 1373 1374static struct token *thread_specifier(struct token *next, struct symbol *sym, struct decl_state *ctx) 1375{ 1376 /* This GCC extension can be used alone, or with extern or static */ 1377 if (!(ctx->storage_class & ~(MOD_STATIC|MOD_EXTERN))) { 1378 apply_qualifier(&next->pos, &ctx->ctype, MOD_TLS); 1379 } else { 1380 sparse_error(next->pos, "__thread cannot be used with '%s'", 1381 modifier_name(ctx->storage_class)); 1382 } 1383 1384 return next; 1385} 1386 1387static struct token *attribute_force(struct token *token, struct symbol *attr, struct decl_state *ctx) 1388{ 1389 ctx->forced = 1; 1390 return token; 1391} 1392 1393static struct token *alignas_specifier(struct token *token, struct symbol *sym, struct decl_state *ctx) 1394{ 1395 int alignment = 0; 1396 1397 if (!match_op(token, '(')) { 1398 sparse_error(token->pos, "expected '(' after _Alignas"); 1399 return token; 1400 } 1401 if (lookup_type(token->next)) { 1402 struct symbol *sym = NULL; 1403 token = typename(token->next, &sym, NULL); 1404 sym = examine_symbol_type(sym); 1405 alignment = sym->ctype.alignment; 1406 token = expect(token, ')', "after _Alignas(..."); 1407 } else { 1408 struct expression *expr = NULL; 1409 token = parens_expression(token, &expr, "after _Alignas"); 1410 if (!expr) 1411 return token; 1412 alignment = const_expression_value(expr); 1413 } 1414 1415 if (alignment < 0) { 1416 warning(token->pos, "non-positive alignment"); 1417 return token; 1418 } 1419 if (alignment & (alignment-1)) { 1420 warning(token->pos, "non-power-of-2 alignment"); 1421 return token; 1422 } 1423 if (alignment > ctx->ctype.alignment) 1424 ctx->ctype.alignment = alignment; 1425 return token; 1426} 1427 1428static struct token *generic_qualifier(struct token *next, struct symbol *sym, struct decl_state *ctx) 1429{ 1430 apply_qualifier(&next->pos, &ctx->ctype, sym->ctype.modifiers); 1431 return next; 1432} 1433 1434static void apply_ctype(struct position pos, struct ctype *dst, struct ctype *src) 1435{ 1436 unsigned long mod = src->modifiers; 1437 1438 if (mod) 1439 apply_qualifier(&pos, dst, mod); 1440 1441 /* Context */ 1442 concat_ptr_list((struct ptr_list *)src->contexts, 1443 (struct ptr_list **)&dst->contexts); 1444 1445 /* Alignment */ 1446 if (src->alignment > dst->alignment) 1447 dst->alignment = src->alignment; 1448 1449 /* Address space */ 1450 if (src->as) 1451 dst->as = src->as; 1452} 1453 1454static void specifier_conflict(struct position pos, int what, struct ident *new) 1455{ 1456 const char *old; 1457 if (what & (Set_S | Set_T)) 1458 goto Catch_all; 1459 if (what & Set_Char) 1460 old = "char"; 1461 else if (what & Set_Double) 1462 old = "double"; 1463 else if (what & Set_Float) 1464 old = "float"; 1465 else if (what & Set_Signed) 1466 old = "signed"; 1467 else if (what & Set_Unsigned) 1468 old = "unsigned"; 1469 else if (what & Set_Short) 1470 old = "short"; 1471 else if (what & Set_Long) 1472 old = "long"; 1473 else 1474 old = "long long"; 1475 sparse_error(pos, "impossible combination of type specifiers: %s %s", 1476 old, show_ident(new)); 1477 return; 1478 1479Catch_all: 1480 sparse_error(pos, "two or more data types in declaration specifiers"); 1481} 1482 1483static struct symbol * const int_types[] = 1484 {&char_ctype, &short_ctype, &int_ctype, &long_ctype, &llong_ctype, &int128_ctype}; 1485static struct symbol * const signed_types[] = 1486 {&schar_ctype, &sshort_ctype, &sint_ctype, &slong_ctype, &sllong_ctype, 1487 &sint128_ctype}; 1488static struct symbol * const unsigned_types[] = 1489 {&uchar_ctype, &ushort_ctype, &uint_ctype, &ulong_ctype, &ullong_ctype, 1490 &uint128_ctype}; 1491static struct symbol * const real_types[] = 1492 {&float_ctype, &double_ctype, &ldouble_ctype}; 1493static struct symbol * const * const types[] = { 1494 [CInt] = int_types + 2, 1495 [CSInt] = signed_types + 2, 1496 [CUInt] = unsigned_types + 2, 1497 [CReal] = real_types + 1, 1498}; 1499 1500struct symbol *ctype_integer(int size, int want_unsigned) 1501{ 1502 return types[want_unsigned ? CUInt : CInt][size]; 1503} 1504 1505static struct token *handle_qualifiers(struct token *t, struct decl_state *ctx) 1506{ 1507 while (token_type(t) == TOKEN_IDENT) { 1508 struct symbol *s = lookup_keyword(t->ident, NS_TYPEDEF); 1509 if (!s) 1510 break; 1511 if (!(s->op->type & (KW_ATTRIBUTE | KW_QUALIFIER))) 1512 break; 1513 t = t->next; 1514 if (s->op->declarator) 1515 t = s->op->declarator(t, s, ctx); 1516 } 1517 return t; 1518} 1519 1520static struct token *declaration_specifiers(struct token *token, struct decl_state *ctx) 1521{ 1522 int seen = 0; 1523 int class = CInt; 1524 int rank = 0; 1525 1526 while (token_type(token) == TOKEN_IDENT) { 1527 struct symbol *s = lookup_symbol(token->ident, 1528 NS_TYPEDEF | NS_SYMBOL); 1529 if (!s || !(s->namespace & NS_TYPEDEF)) 1530 break; 1531 if (s->type != SYM_KEYWORD) { 1532 if (seen & Set_Any) 1533 break; 1534 seen |= Set_S | Set_T; 1535 ctx->ctype.base_type = s->ctype.base_type; 1536 apply_ctype(token->pos, &ctx->ctype, &s->ctype); 1537 token = token->next; 1538 continue; 1539 } 1540 if (s->op->type & KW_SPECIFIER) { 1541 if (seen & s->op->test) { 1542 specifier_conflict(token->pos, 1543 seen & s->op->test, 1544 token->ident); 1545 break; 1546 } 1547 seen |= s->op->set; 1548 class += s->op->class; 1549 if (s->op->set & Set_Int128) 1550 rank = 3; 1551 else if (s->op->set & Set_Char) 1552 rank = -2; 1553 if (s->op->set & (Set_Short|Set_Float)) { 1554 rank = -1; 1555 } else if (s->op->set & Set_Long && rank++) { 1556 if (class == CReal) { 1557 specifier_conflict(token->pos, 1558 Set_Vlong, 1559 &double_ident); 1560 break; 1561 } 1562 seen |= Set_Vlong; 1563 } 1564 } 1565 token = token->next; 1566 if (s->op->declarator) // Note: this eats attributes 1567 token = s->op->declarator(token, s, ctx); 1568 if (s->op->type & KW_EXACT) { 1569 ctx->ctype.base_type = s->ctype.base_type; 1570 ctx->ctype.modifiers |= s->ctype.modifiers; 1571 } 1572 } 1573 1574 if (!(seen & Set_S)) { /* not set explicitly? */ 1575 struct symbol *base = &incomplete_ctype; 1576 if (seen & Set_Any) 1577 base = types[class][rank]; 1578 ctx->ctype.base_type = base; 1579 } 1580 1581 if (ctx->ctype.modifiers & MOD_BITWISE) { 1582 struct symbol *type; 1583 ctx->ctype.modifiers &= ~MOD_BITWISE; 1584 if (!is_int_type(ctx->ctype.base_type)) { 1585 sparse_error(token->pos, "invalid modifier"); 1586 return token; 1587 } 1588 type = alloc_symbol(token->pos, SYM_BASETYPE); 1589 *type = *ctx->ctype.base_type; 1590 type->ctype.modifiers &= ~MOD_SPECIFIER; 1591 type->ctype.base_type = ctx->ctype.base_type; 1592 type->type = SYM_RESTRICT; 1593 ctx->ctype.base_type = type; 1594 create_fouled(type); 1595 } 1596 return token; 1597} 1598 1599static struct token *abstract_array_declarator(struct token *token, struct symbol *sym) 1600{ 1601 struct expression *expr = NULL; 1602 int has_static = 0; 1603 1604 while (token_type(token) == TOKEN_IDENT) { 1605 struct symbol *sym = lookup_keyword(token->ident, NS_TYPEDEF); 1606 if (!sym || !(sym->op->type & (KW_STATIC|KW_QUALIFIER))) 1607 break; 1608 if (has_static && (sym->op->type & KW_STATIC)) 1609 sparse_error(token->pos, "duplicate array static declarator"); 1610 has_static |= (sym->op->type & KW_STATIC); 1611 token = token->next; 1612 } 1613 if (match_op(token, '*') && match_op(token->next, ']')) { 1614 // FIXME: '[*]' is treated like '[]' 1615 token = token->next; 1616 } else { 1617 token = assignment_expression(token, &expr); 1618 } 1619 sym->array_size = expr; 1620 return token; 1621} 1622 1623static struct token *parameter_type_list(struct token *, struct symbol *); 1624static struct token *identifier_list(struct token *, struct symbol *); 1625static struct token *declarator(struct token *token, struct decl_state *ctx); 1626 1627static struct token *handle_asm_name(struct token *token, struct decl_state *ctx) 1628{ 1629 struct expression *expr; 1630 struct symbol *keyword; 1631 1632 if (token_type(token) != TOKEN_IDENT) 1633 return token; 1634 keyword = lookup_keyword(token->ident, NS_KEYWORD); 1635 if (!keyword) 1636 return token; 1637 if (!(keyword->op->type & KW_ASM)) 1638 return token; 1639 1640 token = token->next; 1641 token = expect(token, '(', "after asm"); 1642 token = string_expression(token, &expr, "asm name"); 1643 token = expect(token, ')', "after asm"); 1644 return token; 1645} 1646 1647/// 1648// test if @token is '__attribute__' (or one of its variant) 1649static bool match_attribute(struct token *token) 1650{ 1651 struct symbol *sym; 1652 1653 if (token_type(token) != TOKEN_IDENT) 1654 return false; 1655 sym = lookup_keyword(token->ident, NS_TYPEDEF); 1656 if (!sym || !sym->op) 1657 return false; 1658 return sym->op->type & KW_ATTRIBUTE; 1659} 1660 1661static struct token *skip_attribute(struct token *token) 1662{ 1663 token = token->next; 1664 if (match_op(token, '(')) { 1665 int depth = 1; 1666 token = token->next; 1667 while (depth && !eof_token(token)) { 1668 if (token_type(token) == TOKEN_SPECIAL) { 1669 if (token->special == '(') 1670 depth++; 1671 else if (token->special == ')') 1672 depth--; 1673 } 1674 token = token->next; 1675 } 1676 } 1677 return token; 1678} 1679 1680static struct token *skip_attributes(struct token *token) 1681{ 1682 while (match_attribute(token)) { 1683 token = expect(token->next, '(', "after attribute"); 1684 token = expect(token, '(', "after attribute"); 1685 while (token_type(token) == TOKEN_IDENT) { 1686 token = skip_attribute(token); 1687 if (!match_op(token, ',')) 1688 break; 1689 token = token->next; 1690 } 1691 token = expect(token, ')', "after attribute"); 1692 token = expect(token, ')', "after attribute"); 1693 } 1694 return token; 1695} 1696 1697static struct token *handle_attributes(struct token *token, struct decl_state *ctx) 1698{ 1699 while (match_attribute(token)) 1700 token = attribute_specifier(token->next, NULL, ctx); 1701 return token; 1702} 1703 1704static int is_nested(struct token *token, struct token **p, 1705 int prefer_abstract) 1706{ 1707 /* 1708 * This can be either a parameter list or a grouping. 1709 * For the direct (non-abstract) case, we know if must be 1710 * a parameter list if we already saw the identifier. 1711 * For the abstract case, we know if must be a parameter 1712 * list if it is empty or starts with a type. 1713 */ 1714 struct token *next = token->next; 1715 1716 *p = next = skip_attributes(next); 1717 1718 if (token_type(next) == TOKEN_IDENT) { 1719 if (lookup_type(next)) 1720 return !prefer_abstract; 1721 return 1; 1722 } 1723 1724 if (match_op(next, ')') || match_op(next, SPECIAL_ELLIPSIS)) 1725 return 0; 1726 1727 return 1; 1728} 1729 1730enum kind { 1731 Empty, K_R, Proto, Bad_Func, 1732}; 1733 1734static enum kind which_func(struct token *token, 1735 struct ident **n, 1736 int prefer_abstract) 1737{ 1738 struct token *next = token->next; 1739 1740 if (token_type(next) == TOKEN_IDENT) { 1741 if (lookup_type(next)) 1742 return Proto; 1743 /* identifier list not in definition; complain */ 1744 if (prefer_abstract) 1745 warning(token->pos, 1746 "identifier list not in definition"); 1747 return K_R; 1748 } 1749 1750 if (token_type(next) != TOKEN_SPECIAL) 1751 return Bad_Func; 1752 1753 if (next->special == ')') { 1754 /* don't complain about those */ 1755 if (!n || match_op(next->next, ';') || match_op(next->next, ',')) 1756 return Empty; 1757 if (Wstrict_prototypes) 1758 warning(next->pos, 1759 "non-ANSI function declaration of function '%s'", 1760 show_ident(*n)); 1761 return Empty; 1762 } 1763 1764 if (next->special == SPECIAL_ELLIPSIS) { 1765 warning(next->pos, 1766 "variadic functions must have one named argument"); 1767 return Proto; 1768 } 1769 1770 return Bad_Func; 1771} 1772 1773static struct token *direct_declarator(struct token *token, struct decl_state *ctx) 1774{ 1775 struct ctype *ctype = &ctx->ctype; 1776 struct token *next; 1777 struct ident **p = ctx->ident; 1778 1779 if (ctx->ident && token_type(token) == TOKEN_IDENT) { 1780 *ctx->ident = token->ident; 1781 token = token->next; 1782 } else if (match_op(token, '(') && 1783 is_nested(token, &next, ctx->prefer_abstract)) { 1784 struct symbol *base_type = ctype->base_type; 1785 if (token->next != next) 1786 next = handle_attributes(token->next, ctx); 1787 token = declarator(next, ctx); 1788 token = expect(token, ')', "in nested declarator"); 1789 while (ctype->base_type != base_type) 1790 ctype = &ctype->base_type->ctype; 1791 p = NULL; 1792 } 1793 1794 if (match_op(token, '(')) { 1795 enum kind kind = which_func(token, p, ctx->prefer_abstract); 1796 struct symbol *fn; 1797 fn = alloc_indirect_symbol(token->pos, ctype, SYM_FN); 1798 ctype->modifiers |= ctx->f_modifiers; 1799 token = token->next; 1800 if (kind == K_R) 1801 token = identifier_list(token, fn); 1802 else if (kind == Proto) 1803 token = parameter_type_list(token, fn); 1804 token = expect(token, ')', "in function declarator"); 1805 fn->endpos = token->pos; 1806 return token; 1807 } 1808 1809 while (match_op(token, '[')) { 1810 struct symbol *array; 1811 array = alloc_indirect_symbol(token->pos, ctype, SYM_ARRAY); 1812 token = abstract_array_declarator(token->next, array); 1813 token = expect(token, ']', "in abstract_array_declarator"); 1814 array->endpos = token->pos; 1815 ctype = &array->ctype; 1816 } 1817 return token; 1818} 1819 1820static struct token *pointer(struct token *token, struct decl_state *ctx) 1821{ 1822 while (match_op(token,'*')) { 1823 struct symbol *ptr = alloc_symbol(token->pos, SYM_PTR); 1824 ptr->ctype.modifiers = ctx->ctype.modifiers; 1825 ptr->ctype.base_type = ctx->ctype.base_type; 1826 ptr->ctype.as = ctx->ctype.as; 1827 ptr->ctype.contexts = ctx->ctype.contexts; 1828 ctx->ctype.modifiers = 0; 1829 ctx->ctype.base_type = ptr; 1830 ctx->ctype.as = NULL; 1831 ctx->ctype.contexts = NULL; 1832 ctx->ctype.alignment = 0; 1833 1834 token = handle_qualifiers(token->next, ctx); 1835 ctx->ctype.base_type->endpos = token->pos; 1836 } 1837 return token; 1838} 1839 1840static struct token *declarator(struct token *token, struct decl_state *ctx) 1841{ 1842 token = pointer(token, ctx); 1843 return direct_declarator(token, ctx); 1844} 1845 1846static struct token *handle_bitfield(struct token *token, struct decl_state *ctx) 1847{ 1848 struct ctype *ctype = &ctx->ctype; 1849 struct expression *expr; 1850 struct symbol *bitfield; 1851 long long width; 1852 1853 if (ctype->base_type != &int_type && !is_int_type(ctype->base_type)) { 1854 sparse_error(token->pos, "invalid bitfield specifier for type %s.", 1855 show_typename(ctype->base_type)); 1856 // Parse this to recover gracefully. 1857 return conditional_expression(token->next, &expr); 1858 } 1859 1860 bitfield = alloc_indirect_symbol(token->pos, ctype, SYM_BITFIELD); 1861 token = conditional_expression(token->next, &expr); 1862 width = const_expression_value(expr); 1863 bitfield->bit_size = width; 1864 1865 if (width < 0 || width > INT_MAX || (*ctx->ident && width == 0)) { 1866 sparse_error(token->pos, "bitfield '%s' has invalid width (%lld)", 1867 show_ident(*ctx->ident), width); 1868 width = -1; 1869 } else if (*ctx->ident) { 1870 struct symbol *base_type = bitfield->ctype.base_type; 1871 struct symbol *bitfield_type = base_type == &int_type ? bitfield : base_type; 1872 int is_signed = !(bitfield_type->ctype.modifiers & MOD_UNSIGNED); 1873 if (Wone_bit_signed_bitfield && width == 1 && is_signed) { 1874 // Valid values are either {-1;0} or {0}, depending on integer 1875 // representation. The latter makes for very efficient code... 1876 sparse_error(token->pos, "dubious one-bit signed bitfield"); 1877 } 1878 if (Wdefault_bitfield_sign && 1879 bitfield_type->type != SYM_ENUM && 1880 !(bitfield_type->ctype.modifiers & MOD_EXPLICITLY_SIGNED) && 1881 is_signed) { 1882 // The sign of bitfields is unspecified by default. 1883 warning(token->pos, "dubious bitfield without explicit `signed' or `unsigned'"); 1884 } 1885 } 1886 bitfield->bit_size = width; 1887 bitfield->endpos = token->pos; 1888 bitfield->ident = *ctx->ident; 1889 return token; 1890} 1891 1892static struct token *declaration_list(struct token *token, struct symbol_list **list) 1893{ 1894 struct decl_state ctx = {.prefer_abstract = 0}; 1895 struct ctype saved; 1896 unsigned long mod; 1897 1898 token = declaration_specifiers(token, &ctx); 1899 mod = decl_modifiers(&ctx); 1900 saved = ctx.ctype; 1901 for (;;) { 1902 struct symbol *decl = alloc_symbol(token->pos, SYM_NODE); 1903 ctx.ident = &decl->ident; 1904 1905 token = declarator(token, &ctx); 1906 if (match_op(token, ':')) 1907 token = handle_bitfield(token, &ctx); 1908 1909 token = handle_attributes(token, &ctx); 1910 apply_modifiers(token->pos, &ctx); 1911 1912 decl->ctype = ctx.ctype; 1913 decl->ctype.modifiers |= mod; 1914 decl->endpos = token->pos; 1915 add_symbol(list, decl); 1916 if (!match_op(token, ',')) 1917 break; 1918 token = token->next; 1919 ctx.ctype = saved; 1920 } 1921 return token; 1922} 1923 1924static struct token *struct_declaration_list(struct token *token, struct symbol_list **list) 1925{ 1926 while (!match_op(token, '}')) { 1927 if (match_ident(token, &_Static_assert_ident)) { 1928 token = parse_static_assert(token, NULL); 1929 continue; 1930 } 1931 if (!match_op(token, ';')) 1932 token = declaration_list(token, list); 1933 if (!match_op(token, ';')) { 1934 sparse_error(token->pos, "expected ; at end of declaration"); 1935 break; 1936 } 1937 token = token->next; 1938 } 1939 return token; 1940} 1941 1942static struct token *parameter_declaration(struct token *token, struct symbol *sym) 1943{ 1944 struct decl_state ctx = {.prefer_abstract = 1}; 1945 1946 token = declaration_specifiers(token, &ctx); 1947 ctx.ident = &sym->ident; 1948 token = declarator(token, &ctx); 1949 token = handle_attributes(token, &ctx); 1950 apply_modifiers(token->pos, &ctx); 1951 sym->ctype = ctx.ctype; 1952 sym->ctype.modifiers |= decl_modifiers(&ctx); 1953 sym->endpos = token->pos; 1954 sym->forced_arg = ctx.forced; 1955 return token; 1956} 1957 1958struct token *typename(struct token *token, struct symbol **p, int *forced) 1959{ 1960 struct decl_state ctx = {.prefer_abstract = 1}; 1961 unsigned long class; 1962 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE); 1963 *p = sym; 1964 token = declaration_specifiers(token, &ctx); 1965 token = declarator(token, &ctx); 1966 apply_modifiers(token->pos, &ctx); 1967 sym->ctype = ctx.ctype; 1968 sym->endpos = token->pos; 1969 class = ctx.storage_class; 1970 if (forced) 1971 *forced = ctx.forced; 1972 if (class) 1973 warning(sym->pos, "storage class in typename (%s%s)", 1974 modifier_string(class), show_typename(sym)); 1975 return token; 1976} 1977 1978static struct token *expression_statement(struct token *token, struct expression **tree) 1979{ 1980 token = parse_expression(token, tree); 1981 return expect(token, ';', "at end of statement"); 1982} 1983 1984static struct token *parse_asm_operands(struct token *token, struct statement *stmt, 1985 struct asm_operand_list **inout) 1986{ 1987 /* Allow empty operands */ 1988 if (match_op(token->next, ':') || match_op(token->next, ')')) 1989 return token->next; 1990 do { 1991 struct asm_operand *op = __alloc_asm_operand(0); 1992 if (match_op(token->next, '[') && 1993 token_type(token->next->next) == TOKEN_IDENT && 1994 match_op(token->next->next->next, ']')) { 1995 op->name = token->next->next->ident; 1996 token = token->next->next->next; 1997 } 1998 token = token->next; 1999 token = string_expression(token, &op->constraint, "asm constraint"); 2000 token = parens_expression(token, &op->expr, "in asm parameter"); 2001 add_ptr_list(inout, op); 2002 } while (match_op(token, ',')); 2003 return token; 2004} 2005 2006static struct token *parse_asm_clobbers(struct token *token, struct statement *stmt, 2007 struct expression_list **clobbers) 2008{ 2009 struct expression *expr; 2010 2011 do { 2012 token = primary_expression(token->next, &expr); 2013 if (expr) 2014 add_expression(clobbers, expr); 2015 } while (match_op(token, ',')); 2016 return token; 2017} 2018 2019static struct token *parse_asm_labels(struct token *token, struct statement *stmt, 2020 struct symbol_list **labels) 2021{ 2022 struct symbol *label; 2023 2024 do { 2025 token = token->next; /* skip ':' and ',' */ 2026 if (token_type(token) != TOKEN_IDENT) 2027 return token; 2028 label = label_symbol(token, 1); 2029 add_symbol(labels, label); 2030 token = token->next; 2031 } while (match_op(token, ',')); 2032 return token; 2033} 2034 2035static struct token *parse_asm_statement(struct token *token, struct statement *stmt) 2036{ 2037 unsigned long mods = 0; 2038 2039 token = token->next; 2040 stmt->type = STMT_ASM; 2041 while (token_type(token) == TOKEN_IDENT) { 2042 struct symbol *s = lookup_keyword(token->ident, NS_TYPEDEF); 2043 if (s && s->op->asm_modifier) 2044 s->op->asm_modifier(token, &mods, s->ctype.modifiers); 2045 else if (token->ident == &goto_ident) 2046 asm_modifier(token, &mods, MOD_ASM_GOTO); 2047 token = token->next; 2048 } 2049 token = expect(token, '(', "after asm"); 2050 token = string_expression(token, &stmt->asm_string, "inline asm"); 2051 if (match_op(token, ':')) 2052 token = parse_asm_operands(token, stmt, &stmt->asm_outputs); 2053 if (match_op(token, ':')) 2054 token = parse_asm_operands(token, stmt, &stmt->asm_inputs); 2055 if (match_op(token, ':')) 2056 token = parse_asm_clobbers(token, stmt, &stmt->asm_clobbers); 2057 if (match_op(token, ':') && (mods & MOD_ASM_GOTO)) 2058 token = parse_asm_labels(token, stmt, &stmt->asm_labels); 2059 token = expect(token, ')', "after asm"); 2060 return expect(token, ';', "at end of asm-statement"); 2061} 2062 2063static struct token *parse_static_assert(struct token *token, struct symbol_list **unused) 2064{ 2065 struct expression *cond = NULL, *message = NULL; 2066 2067 token = expect(token->next, '(', "after _Static_assert"); 2068 token = constant_expression(token, &cond); 2069 if (!cond) 2070 sparse_error(token->pos, "Expected constant expression"); 2071 if (match_op(token, ',')) { 2072 token = token->next; 2073 token = string_expression(token, &message, "_Static_assert()"); 2074 if (!message) 2075 cond = NULL; 2076 } 2077 token = expect(token, ')', "after diagnostic message in _Static_assert"); 2078 token = expect(token, ';', "after _Static_assert()"); 2079 2080 if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE) { 2081 const char *sep = "", *msg = ""; 2082 2083 if (message) { 2084 sep = ": "; 2085 msg = show_string(message->string); 2086 } 2087 sparse_error(cond->pos, "static assertion failed%s%s", sep, msg); 2088 } 2089 2090 return token; 2091} 2092 2093/* Make a statement out of an expression */ 2094static struct statement *make_statement(struct expression *expr) 2095{ 2096 struct statement *stmt; 2097 2098 if (!expr) 2099 return NULL; 2100 stmt = alloc_statement(expr->pos, STMT_EXPRESSION); 2101 stmt->expression = expr; 2102 return stmt; 2103} 2104 2105/* 2106 * All iterators have two symbols associated with them: 2107 * the "continue" and "break" symbols, which are targets 2108 * for continue and break statements respectively. 2109 * 2110 * They are in a special name-space, but they follow 2111 * all the normal visibility rules, so nested iterators 2112 * automatically work right. 2113 */ 2114static void start_iterator(struct statement *stmt) 2115{ 2116 struct symbol *cont, *brk; 2117 2118 start_block_scope(); 2119 cont = alloc_symbol(stmt->pos, SYM_NODE); 2120 bind_symbol(cont, &continue_ident, NS_ITERATOR); 2121 brk = alloc_symbol(stmt->pos, SYM_NODE); 2122 bind_symbol(brk, &break_ident, NS_ITERATOR); 2123 2124 stmt->type = STMT_ITERATOR; 2125 stmt->iterator_break = brk; 2126 stmt->iterator_continue = cont; 2127 fn_local_symbol(brk); 2128 fn_local_symbol(cont); 2129} 2130 2131static void end_iterator(struct statement *stmt) 2132{ 2133 end_block_scope(); 2134} 2135 2136static struct statement *start_function(struct symbol *sym) 2137{ 2138 struct symbol *ret; 2139 struct statement *stmt = alloc_statement(sym->pos, STMT_COMPOUND); 2140 2141 start_function_scope(); 2142 ret = alloc_symbol(sym->pos, SYM_NODE); 2143 ret->ctype = sym->ctype.base_type->ctype; 2144 ret->ctype.modifiers &= ~(MOD_STORAGE | MOD_QUALIFIER | MOD_TLS | MOD_ACCESS | MOD_NOCAST | MOD_NODEREF); 2145 ret->ctype.modifiers |= (MOD_AUTO | MOD_REGISTER); 2146 bind_symbol(ret, &return_ident, NS_ITERATOR); 2147 stmt->ret = ret; 2148 fn_local_symbol(ret); 2149 2150 // Currently parsed symbol for __func__/__FUNCTION__/__PRETTY_FUNCTION__ 2151 current_fn = sym; 2152 2153 return stmt; 2154} 2155 2156static void end_function(struct symbol *sym) 2157{ 2158 current_fn = NULL; 2159 end_function_scope(); 2160} 2161 2162/* 2163 * A "switch()" statement, like an iterator, has a 2164 * the "break" symbol associated with it. It works 2165 * exactly like the iterator break - it's the target 2166 * for any break-statements in scope, and means that 2167 * "break" handling doesn't even need to know whether 2168 * it's breaking out of an iterator or a switch. 2169 * 2170 * In addition, the "case" symbol is a marker for the 2171 * case/default statements to find the switch statement 2172 * that they are associated with. 2173 */ 2174static void start_switch(struct statement *stmt) 2175{ 2176 struct symbol *brk, *switch_case; 2177 2178 start_block_scope(); 2179 brk = alloc_symbol(stmt->pos, SYM_NODE); 2180 bind_symbol(brk, &break_ident, NS_ITERATOR); 2181 2182 switch_case = alloc_symbol(stmt->pos, SYM_NODE); 2183 bind_symbol(switch_case, &case_ident, NS_ITERATOR); 2184 switch_case->stmt = stmt; 2185 2186 stmt->type = STMT_SWITCH; 2187 stmt->switch_break = brk; 2188 stmt->switch_case = switch_case; 2189 2190 fn_local_symbol(brk); 2191 fn_local_symbol(switch_case); 2192} 2193 2194static void end_switch(struct statement *stmt) 2195{ 2196 if (!stmt->switch_case->symbol_list) 2197 warning(stmt->pos, "switch with no cases"); 2198 end_block_scope(); 2199} 2200 2201static void add_case_statement(struct statement *stmt) 2202{ 2203 struct symbol *target = lookup_symbol(&case_ident, NS_ITERATOR); 2204 struct symbol *sym; 2205 2206 if (!target) { 2207 sparse_error(stmt->pos, "not in switch scope"); 2208 stmt->type = STMT_NONE; 2209 return; 2210 } 2211 sym = alloc_symbol(stmt->pos, SYM_NODE); 2212 add_symbol(&target->symbol_list, sym); 2213 sym->stmt = stmt; 2214 stmt->case_label = sym; 2215 fn_local_symbol(sym); 2216} 2217 2218static struct token *parse_return_statement(struct token *token, struct statement *stmt) 2219{ 2220 struct symbol *target = lookup_symbol(&return_ident, NS_ITERATOR); 2221 2222 if (!target) 2223 error_die(token->pos, "internal error: return without a function target"); 2224 stmt->type = STMT_RETURN; 2225 stmt->ret_target = target; 2226 return expression_statement(token->next, &stmt->ret_value); 2227} 2228 2229static void validate_for_loop_decl(struct symbol *sym) 2230{ 2231 unsigned long storage = sym->ctype.modifiers & MOD_STORAGE; 2232 2233 if (storage & ~(MOD_AUTO | MOD_REGISTER)) { 2234 const char *name = show_ident(sym->ident); 2235 sparse_error(sym->pos, "non-local var '%s' in for-loop initializer", name); 2236 sym->ctype.modifiers &= ~MOD_STORAGE; 2237 } 2238} 2239 2240static struct token *parse_for_statement(struct token *token, struct statement *stmt) 2241{ 2242 struct symbol_list *syms; 2243 struct expression *e1, *e2, *e3; 2244 struct statement *iterator; 2245 2246 start_iterator(stmt); 2247 token = expect(token->next, '(', "after 'for'"); 2248 2249 syms = NULL; 2250 e1 = NULL; 2251 /* C99 variable declaration? */ 2252 if (lookup_type(token)) { 2253 token = external_declaration(token, &syms, validate_for_loop_decl); 2254 } else { 2255 token = parse_expression(token, &e1); 2256 token = expect(token, ';', "in 'for'"); 2257 } 2258 token = parse_expression(token, &e2); 2259 token = expect(token, ';', "in 'for'"); 2260 token = parse_expression(token, &e3); 2261 token = expect(token, ')', "in 'for'"); 2262 token = statement(token, &iterator); 2263 2264 stmt->iterator_syms = syms; 2265 stmt->iterator_pre_statement = make_statement(e1); 2266 stmt->iterator_pre_condition = e2; 2267 stmt->iterator_post_statement = make_statement(e3); 2268 stmt->iterator_post_condition = NULL; 2269 stmt->iterator_statement = iterator; 2270 end_iterator(stmt); 2271 2272 return token; 2273} 2274 2275static struct token *parse_while_statement(struct token *token, struct statement *stmt) 2276{ 2277 struct expression *expr; 2278 struct statement *iterator; 2279 2280 start_iterator(stmt); 2281 token = parens_expression(token->next, &expr, "after 'while'"); 2282 token = statement(token, &iterator); 2283 2284 stmt->iterator_pre_condition = expr; 2285 stmt->iterator_post_condition = NULL; 2286 stmt->iterator_statement = iterator; 2287 end_iterator(stmt); 2288 2289 return token; 2290} 2291 2292static struct token *parse_do_statement(struct token *token, struct statement *stmt) 2293{ 2294 struct expression *expr; 2295 struct statement *iterator; 2296 2297 start_iterator(stmt); 2298 token = statement(token->next, &iterator); 2299 if (token_type(token) == TOKEN_IDENT && token->ident == &while_ident) 2300 token = token->next; 2301 else 2302 sparse_error(token->pos, "expected 'while' after 'do'"); 2303 token = parens_expression(token, &expr, "after 'do-while'"); 2304 2305 stmt->iterator_post_condition = expr; 2306 stmt->iterator_statement = iterator; 2307 end_iterator(stmt); 2308 2309 if (iterator && iterator->type != STMT_COMPOUND && Wdo_while) 2310 warning(iterator->pos, "do-while statement is not a compound statement"); 2311 2312 return expect(token, ';', "after statement"); 2313} 2314 2315static struct token *parse_if_statement(struct token *token, struct statement *stmt) 2316{ 2317 stmt->type = STMT_IF; 2318 token = parens_expression(token->next, &stmt->if_conditional, "after if"); 2319 token = statement(token, &stmt->if_true); 2320 if (token_type(token) != TOKEN_IDENT) 2321 return token; 2322 if (token->ident != &else_ident) 2323 return token; 2324 return statement(token->next, &stmt->if_false); 2325} 2326 2327static inline struct token *case_statement(struct token *token, struct statement *stmt) 2328{ 2329 stmt->type = STMT_CASE; 2330 token = expect(token, ':', "after default/case"); 2331 add_case_statement(stmt); 2332 return statement(token, &stmt->case_statement); 2333} 2334 2335static struct token *parse_case_statement(struct token *token, struct statement *stmt) 2336{ 2337 token = parse_expression(token->next, &stmt->case_expression); 2338 if (match_op(token, SPECIAL_ELLIPSIS)) 2339 token = parse_expression(token->next, &stmt->case_to); 2340 return case_statement(token, stmt); 2341} 2342 2343static struct token *parse_default_statement(struct token *token, struct statement *stmt) 2344{ 2345 return case_statement(token->next, stmt); 2346} 2347 2348static struct token *parse_loop_iterator(struct token *token, struct statement *stmt) 2349{ 2350 struct symbol *target = lookup_symbol(token->ident, NS_ITERATOR); 2351 stmt->type = STMT_GOTO; 2352 stmt->goto_label = target; 2353 if (!target) 2354 sparse_error(stmt->pos, "break/continue not in iterator scope"); 2355 return expect(token->next, ';', "at end of statement"); 2356} 2357 2358static struct token *parse_switch_statement(struct token *token, struct statement *stmt) 2359{ 2360 stmt->type = STMT_SWITCH; 2361 start_switch(stmt); 2362 token = parens_expression(token->next, &stmt->switch_expression, "after 'switch'"); 2363 token = statement(token, &stmt->switch_statement); 2364 end_switch(stmt); 2365 return token; 2366} 2367 2368static void warn_label_usage(struct position def, struct position use, struct ident *ident) 2369{ 2370 const char *id = show_ident(ident); 2371 sparse_error(use, "label '%s' used outside statement expression", id); 2372 info(def, " label '%s' defined here", id); 2373 current_fn->bogus_linear = 1; 2374} 2375 2376void check_label_usage(struct symbol *label, struct position use_pos) 2377{ 2378 struct statement *def = label->stmt; 2379 2380 if (def) { 2381 if (!is_in_scope(def->label_scope, label_scope)) 2382 warn_label_usage(def->pos, use_pos, label->ident); 2383 } else if (!label->label_scope) { 2384 label->label_scope = label_scope; 2385 label->label_pos = use_pos; 2386 } 2387} 2388 2389static struct token *parse_goto_statement(struct token *token, struct statement *stmt) 2390{ 2391 stmt->type = STMT_GOTO; 2392 token = token->next; 2393 if (match_op(token, '*')) { 2394 token = parse_expression(token->next, &stmt->goto_expression); 2395 add_statement(&function_computed_goto_list, stmt); 2396 } else if (token_type(token) == TOKEN_IDENT) { 2397 struct symbol *label = label_symbol(token, 1); 2398 stmt->goto_label = label; 2399 check_label_usage(label, stmt->pos); 2400 token = token->next; 2401 } else { 2402 sparse_error(token->pos, "Expected identifier or goto expression"); 2403 } 2404 return expect(token, ';', "at end of statement"); 2405} 2406 2407static struct token *parse_context_statement(struct token *token, struct statement *stmt) 2408{ 2409 stmt->type = STMT_CONTEXT; 2410 token = token->next; 2411 token = expect(token, '(', "after __context__ statement"); 2412 token = assignment_expression(token, &stmt->expression); 2413 if (!stmt->expression) 2414 unexpected(token, "expression expected after '('"); 2415 if (match_op(token, ',')) { 2416 token = token->next; 2417 stmt->context = stmt->expression; 2418 token = assignment_expression(token, &stmt->expression); 2419 if (!stmt->expression) 2420 unexpected(token, "expression expected after ','"); 2421 } 2422 token = expect(token, ')', "at end of __context__ statement"); 2423 return expect(token, ';', "at end of statement"); 2424} 2425 2426static struct token *parse_range_statement(struct token *token, struct statement *stmt) 2427{ 2428 stmt->type = STMT_RANGE; 2429 token = token->next; 2430 token = expect(token, '(', "after __range__ statement"); 2431 token = assignment_expression(token, &stmt->range_expression); 2432 token = expect(token, ',', "after range expression"); 2433 token = assignment_expression(token, &stmt->range_low); 2434 token = expect(token, ',', "after low range"); 2435 token = assignment_expression(token, &stmt->range_high); 2436 token = expect(token, ')', "after range statement"); 2437 return expect(token, ';', "after range statement"); 2438} 2439 2440static struct token *handle_label_attributes(struct token *token, struct symbol *label) 2441{ 2442 struct decl_state ctx = { }; 2443 2444 token = handle_attributes(token, &ctx); 2445 label->label_modifiers = ctx.ctype.modifiers; 2446 return token; 2447} 2448 2449static struct token *statement(struct token *token, struct statement **tree) 2450{ 2451 struct statement *stmt = alloc_statement(token->pos, STMT_NONE); 2452 2453 *tree = stmt; 2454 if (token_type(token) == TOKEN_IDENT) { 2455 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD); 2456 if (s && s->op->statement) 2457 return s->op->statement(token, stmt); 2458 2459 if (match_op(token->next, ':')) { 2460 struct symbol *s = label_symbol(token, 0); 2461 token = handle_label_attributes(token->next->next, s); 2462 if (s->stmt) { 2463 sparse_error(stmt->pos, "label '%s' redefined", show_ident(s->ident)); 2464 // skip the label to avoid multiple definitions 2465 return statement(token, tree); 2466 } 2467 stmt->type = STMT_LABEL; 2468 stmt->label_identifier = s; 2469 stmt->label_scope = label_scope; 2470 if (s->label_scope) { 2471 if (!is_in_scope(label_scope, s->label_scope)) 2472 warn_label_usage(stmt->pos, s->label_pos, s->ident); 2473 } 2474 s->stmt = stmt; 2475 if (match_op(token, '}')) { 2476 warning(token->pos, "statement expected after label"); 2477 stmt->label_statement = alloc_statement(token->pos, STMT_NONE); 2478 return token; 2479 } 2480 return statement(token, &stmt->label_statement); 2481 } 2482 } 2483 2484 if (match_op(token, '{')) { 2485 token = compound_statement(token->next, stmt); 2486 return expect(token, '}', "at end of compound statement"); 2487 } 2488 2489 stmt->type = STMT_EXPRESSION; 2490 return expression_statement(token, &stmt->expression); 2491} 2492 2493/* gcc extension - __label__ ident-list; in the beginning of compound stmt */ 2494static struct token *label_statement(struct token *token) 2495{ 2496 while (token_type(token) == TOKEN_IDENT) { 2497 struct symbol *sym = alloc_symbol(token->pos, SYM_LABEL); 2498 /* it's block-scope, but we want label namespace */ 2499 bind_symbol_with_scope(sym, token->ident, NS_LABEL, block_scope); 2500 fn_local_symbol(sym); 2501 token = token->next; 2502 if (!match_op(token, ',')) 2503 break; 2504 token = token->next; 2505 } 2506 return expect(token, ';', "at end of label declaration"); 2507} 2508 2509static struct token * statement_list(struct token *token, struct statement_list **list) 2510{ 2511 int seen_statement = 0; 2512 while (token_type(token) == TOKEN_IDENT && 2513 token->ident == &__label___ident) 2514 token = label_statement(token->next); 2515 for (;;) { 2516 struct statement * stmt; 2517 if (eof_token(token)) 2518 break; 2519 if (match_op(token, '}')) 2520 break; 2521 if (match_ident(token, &_Static_assert_ident)) { 2522 token = parse_static_assert(token, NULL); 2523 continue; 2524 } 2525 if (lookup_type(token)) { 2526 if (seen_statement) { 2527 warning(token->pos, "mixing declarations and code"); 2528 seen_statement = 0; 2529 } 2530 stmt = alloc_statement(token->pos, STMT_DECLARATION); 2531 token = external_declaration(token, &stmt->declaration, NULL); 2532 } else { 2533 seen_statement = Wdeclarationafterstatement; 2534 token = statement(token, &stmt); 2535 } 2536 add_statement(list, stmt); 2537 } 2538 return token; 2539} 2540 2541static struct token *identifier_list(struct token *token, struct symbol *fn) 2542{ 2543 struct symbol_list **list = &fn->arguments; 2544 for (;;) { 2545 struct symbol *sym = alloc_symbol(token->pos, SYM_NODE); 2546 sym->ident = token->ident; 2547 token = token->next; 2548 sym->endpos = token->pos; 2549 sym->ctype.base_type = &incomplete_ctype; 2550 add_symbol(list, sym); 2551 if (!match_op(token, ',') || 2552 token_type(token->next) != TOKEN_IDENT || 2553 lookup_type(token->next)) 2554 break; 2555 token = token->next; 2556 } 2557 return token; 2558} 2559 2560static struct token *parameter_type_list(struct token *token, struct symbol *fn) 2561{ 2562 struct symbol_list **list = &fn->arguments; 2563 2564 for (;;) { 2565 struct symbol *sym; 2566 2567 if (match_op(token, SPECIAL_ELLIPSIS)) { 2568 fn->variadic = 1; 2569 token = token->next; 2570 break; 2571 } 2572 2573 sym = alloc_symbol(token->pos, SYM_NODE); 2574 token = parameter_declaration(token, sym); 2575 if (sym->ctype.base_type == &void_ctype) { 2576 /* Special case: (void) */ 2577 if (!*list && !sym->ident) 2578 break; 2579 warning(token->pos, "void parameter"); 2580 } 2581 add_symbol(list, sym); 2582 if (!match_op(token, ',')) 2583 break; 2584 token = token->next; 2585 } 2586 return token; 2587} 2588 2589struct token *compound_statement(struct token *token, struct statement *stmt) 2590{ 2591 stmt->type = STMT_COMPOUND; 2592 start_block_scope(); 2593 token = statement_list(token, &stmt->stmts); 2594 end_block_scope(); 2595 return token; 2596} 2597 2598static struct expression *identifier_expression(struct token *token) 2599{ 2600 struct expression *expr = alloc_expression(token->pos, EXPR_IDENTIFIER); 2601 expr->expr_ident = token->ident; 2602 return expr; 2603} 2604 2605static struct expression *index_expression(struct expression *from, struct expression *to) 2606{ 2607 int idx_from, idx_to; 2608 struct expression *expr = alloc_expression(from->pos, EXPR_INDEX); 2609 2610 idx_from = const_expression_value(from); 2611 idx_to = idx_from; 2612 if (to) { 2613 idx_to = const_expression_value(to); 2614 if (idx_to < idx_from || idx_from < 0) 2615 warning(from->pos, "nonsense array initializer index range"); 2616 } 2617 expr->idx_from = idx_from; 2618 expr->idx_to = idx_to; 2619 return expr; 2620} 2621 2622static struct token *single_initializer(struct expression **ep, struct token *token) 2623{ 2624 int expect_equal = 0; 2625 struct token *next = token->next; 2626 struct expression **tail = ep; 2627 int nested; 2628 2629 *ep = NULL; 2630 2631 if ((token_type(token) == TOKEN_IDENT) && match_op(next, ':')) { 2632 struct expression *expr = identifier_expression(token); 2633 if (Wold_initializer) 2634 warning(token->pos, "obsolete struct initializer, use C99 syntax"); 2635 token = initializer(&expr->ident_expression, next->next); 2636 if (expr->ident_expression) 2637 *ep = expr; 2638 return token; 2639 } 2640 2641 for (tail = ep, nested = 0; ; nested++, next = token->next) { 2642 if (match_op(token, '.') && (token_type(next) == TOKEN_IDENT)) { 2643 struct expression *expr = identifier_expression(next); 2644 *tail = expr; 2645 tail = &expr->ident_expression; 2646 expect_equal = 1; 2647 token = next->next; 2648 } else if (match_op(token, '[')) { 2649 struct expression *from = NULL, *to = NULL, *expr; 2650 token = constant_expression(token->next, &from); 2651 if (!from) { 2652 sparse_error(token->pos, "Expected constant expression"); 2653 break; 2654 } 2655 if (match_op(token, SPECIAL_ELLIPSIS)) 2656 token = constant_expression(token->next, &to); 2657 expr = index_expression(from, to); 2658 *tail = expr; 2659 tail = &expr->idx_expression; 2660 token = expect(token, ']', "at end of initializer index"); 2661 if (nested) 2662 expect_equal = 1; 2663 } else { 2664 break; 2665 } 2666 } 2667 if (nested && !expect_equal) { 2668 if (!match_op(token, '=')) 2669 warning(token->pos, "obsolete array initializer, use C99 syntax"); 2670 else 2671 expect_equal = 1; 2672 } 2673 if (expect_equal) 2674 token = expect(token, '=', "at end of initializer index"); 2675 2676 token = initializer(tail, token); 2677 if (!*tail) 2678 *ep = NULL; 2679 return token; 2680} 2681 2682static struct token *initializer_list(struct expression_list **list, struct token *token) 2683{ 2684 struct expression *expr; 2685 2686 for (;;) { 2687 token = single_initializer(&expr, token); 2688 if (!expr) 2689 break; 2690 add_expression(list, expr); 2691 if (!match_op(token, ',')) 2692 break; 2693 token = token->next; 2694 } 2695 return token; 2696} 2697 2698struct token *initializer(struct expression **tree, struct token *token) 2699{ 2700 if (match_op(token, '{')) { 2701 struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER); 2702 *tree = expr; 2703 if (!Wuniversal_initializer) { 2704 struct token *next = token->next; 2705 // '{ 0 }' is equivalent to '{ }' except for some 2706 // warnings, like using 0 to initialize a null-pointer. 2707 if (match_token_zero(next)) { 2708 if (match_op(next->next, '}')) 2709 expr->zero_init = 1; 2710 } 2711 } 2712 2713 token = initializer_list(&expr->expr_list, token->next); 2714 return expect(token, '}', "at end of initializer"); 2715 } 2716 return assignment_expression(token, tree); 2717} 2718 2719static void declare_argument(struct symbol *sym, struct symbol *fn) 2720{ 2721 if (!sym->ident) { 2722 sparse_error(sym->pos, "no identifier for function argument"); 2723 return; 2724 } 2725 if (sym->ctype.base_type == &incomplete_ctype) { 2726 sym->ctype.base_type = &int_ctype; 2727 2728 if (Wimplicit_int) { 2729 sparse_error(sym->pos, "missing type declaration for parameter '%s'", 2730 show_ident(sym->ident)); 2731 } 2732 } 2733 bind_symbol(sym, sym->ident, NS_SYMBOL); 2734} 2735 2736static struct token *parse_function_body(struct token *token, struct symbol *decl, 2737 struct symbol_list **list) 2738{ 2739 struct symbol_list **old_symbol_list; 2740 struct symbol *base_type = decl->ctype.base_type; 2741 struct statement *stmt, **p; 2742 struct symbol *prev; 2743 struct symbol *arg; 2744 2745 old_symbol_list = function_symbol_list; 2746 if (decl->ctype.modifiers & MOD_INLINE) { 2747 function_symbol_list = &decl->inline_symbol_list; 2748 p = &base_type->inline_stmt; 2749 } else { 2750 function_symbol_list = &decl->symbol_list; 2751 p = &base_type->stmt; 2752 } 2753 function_computed_target_list = NULL; 2754 function_computed_goto_list = NULL; 2755 2756 if ((decl->ctype.modifiers & (MOD_EXTERN|MOD_INLINE)) == MOD_EXTERN) { 2757 if (Wexternal_function_has_definition) 2758 warning(decl->pos, "function '%s' with external linkage has definition", show_ident(decl->ident)); 2759 } 2760 if (!(decl->ctype.modifiers & MOD_STATIC)) 2761 decl->ctype.modifiers |= MOD_EXTERN; 2762 2763 stmt = start_function(decl); 2764 *p = stmt; 2765 2766 FOR_EACH_PTR (base_type->arguments, arg) { 2767 declare_argument(arg, base_type); 2768 } END_FOR_EACH_PTR(arg); 2769 2770 token = statement_list(token->next, &stmt->stmts); 2771 end_function(decl); 2772 2773 if (!(decl->ctype.modifiers & MOD_INLINE)) 2774 add_symbol(list, decl); 2775 check_declaration(decl); 2776 decl->definition = decl; 2777 prev = decl->same_symbol; 2778 if (prev && prev->definition) { 2779 warning(decl->pos, "multiple definitions for function '%s'", 2780 show_ident(decl->ident)); 2781 info(prev->definition->pos, " the previous one is here"); 2782 } else { 2783 while (prev) { 2784 rebind_scope(prev, decl->scope); 2785 prev->definition = decl; 2786 prev = prev->same_symbol; 2787 } 2788 } 2789 function_symbol_list = old_symbol_list; 2790 if (function_computed_goto_list) { 2791 if (!function_computed_target_list) 2792 warning(decl->pos, "function '%s' has computed goto but no targets?", show_ident(decl->ident)); 2793 else { 2794 FOR_EACH_PTR(function_computed_goto_list, stmt) { 2795 stmt->target_list = function_computed_target_list; 2796 } END_FOR_EACH_PTR(stmt); 2797 } 2798 } 2799 return expect(token, '}', "at end of function"); 2800} 2801 2802static void promote_k_r_types(struct symbol *arg) 2803{ 2804 struct symbol *base = arg->ctype.base_type; 2805 if (base && base->ctype.base_type == &int_type && base->rank < 0) { 2806 arg->ctype.base_type = &int_ctype; 2807 } 2808} 2809 2810static void apply_k_r_types(struct symbol_list *argtypes, struct symbol *fn) 2811{ 2812 struct symbol_list *real_args = fn->ctype.base_type->arguments; 2813 struct symbol *arg; 2814 2815 FOR_EACH_PTR(real_args, arg) { 2816 struct symbol *type; 2817 2818 /* This is quadratic in the number of arguments. We _really_ don't care */ 2819 FOR_EACH_PTR(argtypes, type) { 2820 if (type->ident == arg->ident) 2821 goto match; 2822 } END_FOR_EACH_PTR(type); 2823 if (Wimplicit_int) { 2824 warning(arg->pos, "missing type declaration for parameter '%s'", 2825 show_ident(arg->ident)); 2826 } 2827 type = alloc_symbol(arg->pos, SYM_NODE); 2828 type->ident = arg->ident; 2829 type->ctype.base_type = &int_ctype; 2830match: 2831 type->used = 1; 2832 /* "char" and "short" promote to "int" */ 2833 promote_k_r_types(type); 2834 2835 arg->ctype = type->ctype; 2836 } END_FOR_EACH_PTR(arg); 2837 2838 FOR_EACH_PTR(argtypes, arg) { 2839 if (!arg->used) 2840 warning(arg->pos, "nonsensical parameter declaration '%s'", show_ident(arg->ident)); 2841 } END_FOR_EACH_PTR(arg); 2842 2843} 2844 2845static struct token *parse_k_r_arguments(struct token *token, struct symbol *decl, 2846 struct symbol_list **list) 2847{ 2848 struct symbol_list *args = NULL; 2849 2850 if (Wold_style_definition) 2851 warning(token->pos, "non-ANSI definition of function '%s'", show_ident(decl->ident)); 2852 2853 do { 2854 token = declaration_list(token, &args); 2855 if (!match_op(token, ';')) { 2856 sparse_error(token->pos, "expected ';' at end of parameter declaration"); 2857 break; 2858 } 2859 token = token->next; 2860 } while (lookup_type(token)); 2861 2862 apply_k_r_types(args, decl); 2863 2864 if (!match_op(token, '{')) { 2865 sparse_error(token->pos, "expected function body"); 2866 return token; 2867 } 2868 return parse_function_body(token, decl, list); 2869} 2870 2871static struct token *toplevel_asm_declaration(struct token *token, struct symbol_list **list) 2872{ 2873 struct symbol *anon = alloc_symbol(token->pos, SYM_NODE); 2874 struct symbol *fn = alloc_symbol(token->pos, SYM_FN); 2875 struct statement *stmt; 2876 2877 anon->ctype.base_type = fn; 2878 stmt = alloc_statement(token->pos, STMT_NONE); 2879 fn->stmt = stmt; 2880 2881 token = parse_asm_statement(token, stmt); 2882 2883 // FIXME: add_symbol(list, anon); 2884 return token; 2885} 2886 2887struct token *external_declaration(struct token *token, struct symbol_list **list, 2888 validate_decl_t validate_decl) 2889{ 2890 struct ident *ident = NULL; 2891 struct symbol *decl; 2892 struct decl_state ctx = { .ident = &ident }; 2893 struct ctype saved; 2894 struct symbol *base_type; 2895 unsigned long mod; 2896 int is_typedef; 2897 2898 /* Top-level inline asm or static assertion? */ 2899 if (token_type(token) == TOKEN_IDENT) { 2900 struct symbol *s = lookup_keyword(token->ident, NS_KEYWORD); 2901 if (s && s->op->toplevel) 2902 return s->op->toplevel(token, list); 2903 } 2904 2905 /* Parse declaration-specifiers, if any */ 2906 token = declaration_specifiers(token, &ctx); 2907 mod = decl_modifiers(&ctx); 2908 decl = alloc_symbol(token->pos, SYM_NODE); 2909 /* Just a type declaration? */ 2910 if (match_op(token, ';')) { 2911 apply_modifiers(token->pos, &ctx); 2912 return token->next; 2913 } 2914 2915 saved = ctx.ctype; 2916 token = declarator(token, &ctx); 2917 token = handle_asm_name(token, &ctx); 2918 token = handle_attributes(token, &ctx); 2919 apply_modifiers(token->pos, &ctx); 2920 2921 decl->ctype = ctx.ctype; 2922 decl->ctype.modifiers |= mod; 2923 decl->endpos = token->pos; 2924 2925 /* Just a type declaration? */ 2926 if (!ident) { 2927 warning(token->pos, "missing identifier in declaration"); 2928 return expect(token, ';', "at the end of type declaration"); 2929 } 2930 2931 /* type define declaration? */ 2932 is_typedef = ctx.storage_class == MOD_USERTYPE; 2933 2934 /* Typedefs don't have meaningful storage */ 2935 if (is_typedef) 2936 decl->ctype.modifiers |= MOD_USERTYPE; 2937 2938 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL); 2939 2940 base_type = decl->ctype.base_type; 2941 2942 if (is_typedef) { 2943 if (base_type && !base_type->ident) { 2944 switch (base_type->type) { 2945 case SYM_STRUCT: 2946 case SYM_UNION: 2947 case SYM_ENUM: 2948 case SYM_RESTRICT: 2949 base_type->ident = ident; 2950 break; 2951 default: 2952 break; 2953 } 2954 } 2955 } else if (base_type && base_type->type == SYM_FN) { 2956 if (base_type->ctype.base_type == &autotype_ctype) { 2957 sparse_error(decl->pos, "'%s()' has __auto_type return type", 2958 show_ident(decl->ident)); 2959 base_type->ctype.base_type = &int_ctype; 2960 } 2961 if (base_type->ctype.base_type == &incomplete_ctype) { 2962 warning(decl->pos, "'%s()' has implicit return type", 2963 show_ident(decl->ident)); 2964 base_type->ctype.base_type = &int_ctype; 2965 } 2966 /* apply attributes placed after the declarator */ 2967 decl->ctype.modifiers |= ctx.f_modifiers; 2968 2969 /* K&R argument declaration? */ 2970 if (lookup_type(token)) 2971 return parse_k_r_arguments(token, decl, list); 2972 if (match_op(token, '{')) 2973 return parse_function_body(token, decl, list); 2974 2975 if (!(decl->ctype.modifiers & MOD_STATIC)) 2976 decl->ctype.modifiers |= MOD_EXTERN; 2977 } else if (base_type == &void_ctype && !(decl->ctype.modifiers & MOD_EXTERN)) { 2978 sparse_error(token->pos, "void declaration"); 2979 } 2980 if (base_type == &incomplete_ctype) { 2981 warning(decl->pos, "'%s' has implicit type", show_ident(decl->ident)); 2982 decl->ctype.base_type = &int_ctype;; 2983 } 2984 2985 for (;;) { 2986 if (!is_typedef && match_op(token, '=')) { 2987 struct token *next = token->next; 2988 token = initializer(&decl->initializer, next); 2989 if (token == next) 2990 sparse_error(token->pos, "expression expected before '%s'", show_token(token)); 2991 } 2992 if (!is_typedef) { 2993 if (validate_decl) 2994 validate_decl(decl); 2995 2996 if (decl->initializer && decl->ctype.modifiers & MOD_EXTERN) { 2997 warning(decl->pos, "symbol with external linkage has initializer"); 2998 decl->ctype.modifiers &= ~MOD_EXTERN; 2999 } 3000 3001 if (!(decl->ctype.modifiers & (MOD_EXTERN | MOD_INLINE))) { 3002 add_symbol(list, decl); 3003 fn_local_symbol(decl); 3004 } 3005 } 3006 check_declaration(decl); 3007 if (decl->same_symbol) { 3008 decl->definition = decl->same_symbol->definition; 3009 decl->op = decl->same_symbol->op; 3010 if (is_typedef) { 3011 // TODO: handle -std=c89 --pedantic 3012 check_duplicates(decl); 3013 } 3014 } 3015 3016 if (ctx.autotype) { 3017 const char *msg = NULL; 3018 if (decl->ctype.base_type != &autotype_ctype) 3019 msg = "on non-identifier"; 3020 else if (match_op(token, ',')) 3021 msg = "on declaration list"; 3022 else if (!decl->initializer) 3023 msg = "without initializer"; 3024 else if (decl->initializer->type == EXPR_SYMBOL && 3025 decl->initializer->symbol == decl) 3026 msg = "on self-init var"; 3027 if (msg) { 3028 sparse_error(decl->pos, "__auto_type %s", msg); 3029 decl->ctype.base_type = &bad_ctype; 3030 } 3031 } 3032 3033 if (!match_op(token, ',')) 3034 break; 3035 3036 token = token->next; 3037 ident = NULL; 3038 decl = alloc_symbol(token->pos, SYM_NODE); 3039 ctx.ctype = saved; 3040 token = handle_attributes(token, &ctx); 3041 token = declarator(token, &ctx); 3042 token = handle_asm_name(token, &ctx); 3043 token = handle_attributes(token, &ctx); 3044 apply_modifiers(token->pos, &ctx); 3045 decl->ctype = ctx.ctype; 3046 decl->ctype.modifiers |= mod; 3047 decl->endpos = token->pos; 3048 if (!ident) { 3049 sparse_error(token->pos, "expected identifier name in type definition"); 3050 return token; 3051 } 3052 3053 if (is_typedef) 3054 decl->ctype.modifiers |= MOD_USERTYPE; 3055 3056 bind_symbol(decl, ident, is_typedef ? NS_TYPEDEF: NS_SYMBOL); 3057 3058 /* Function declarations are automatically extern unless specifically static */ 3059 base_type = decl->ctype.base_type; 3060 if (!is_typedef && base_type && base_type->type == SYM_FN) { 3061 if (!(decl->ctype.modifiers & MOD_STATIC)) 3062 decl->ctype.modifiers |= MOD_EXTERN; 3063 } 3064 } 3065 return expect(token, ';', "at end of declaration"); 3066} 3067