1/* Parser for C and Objective-C. 2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 4 5 Parser actions based on the old Bison parser; structure somewhat 6 influenced by and fragments based on the C++ parser. 7 8This file is part of GCC. 9 10GCC is free software; you can redistribute it and/or modify it under 11the terms of the GNU General Public License as published by the Free 12Software Foundation; either version 2, or (at your option) any later 13version. 14 15GCC is distributed in the hope that it will be useful, but WITHOUT ANY 16WARRANTY; without even the implied warranty of MERCHANTABILITY or 17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 18for more details. 19 20You should have received a copy of the GNU General Public License 21along with GCC; see the file COPYING. If not, write to the Free 22Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 2302110-1301, USA. */ 24 25/* TODO: 26 27 Make sure all relevant comments, and all relevant code from all 28 actions, brought over from old parser. Verify exact correspondence 29 of syntax accepted. 30 31 Add testcases covering every input symbol in every state in old and 32 new parsers. 33 34 Include full syntax for GNU C, including erroneous cases accepted 35 with error messages, in syntax productions in comments. 36 37 Make more diagnostics in the front end generally take an explicit 38 location rather than implicitly using input_location. */ 39 40#include "config.h" 41#include "system.h" 42#include "coretypes.h" 43#include "tm.h" 44#include "tree.h" 45#include "rtl.h" 46#include "langhooks.h" 47#include "input.h" 48#include "cpplib.h" 49#include "timevar.h" 50#include "c-pragma.h" 51#include "c-tree.h" 52#include "flags.h" 53#include "output.h" 54#include "toplev.h" 55#include "ggc.h" 56#include "c-common.h" 57#include "vec.h" 58#include "target.h" 59#include "cgraph.h" 60 61 62/* Miscellaneous data and functions needed for the parser. */ 63 64int yydebug; 65 66/* Objective-C specific parser/lexer information. */ 67 68static int objc_pq_context = 0; 69 70/* The following flag is needed to contextualize Objective-C lexical 71 analysis. In some cases (e.g., 'int NSObject;'), it is undesirable 72 to bind an identifier to an Objective-C class, even if a class with 73 that name exists. */ 74static int objc_need_raw_identifier = 0; 75#define OBJC_NEED_RAW_IDENTIFIER(VAL) \ 76 do { \ 77 if (c_dialect_objc ()) \ 78 objc_need_raw_identifier = VAL; \ 79 } while (0) 80 81/* The reserved keyword table. */ 82struct resword 83{ 84 const char *word; 85 ENUM_BITFIELD(rid) rid : 16; 86 unsigned int disable : 16; 87}; 88 89/* Disable mask. Keywords are disabled if (reswords[i].disable & 90 mask) is _true_. */ 91#define D_C89 0x01 /* not in C89 */ 92#define D_EXT 0x02 /* GCC extension */ 93#define D_EXT89 0x04 /* GCC extension incorporated in C99 */ 94#define D_OBJC 0x08 /* Objective C only */ 95 96static const struct resword reswords[] = 97{ 98 { "_Bool", RID_BOOL, 0 }, 99 { "_Complex", RID_COMPLEX, 0 }, 100 { "_Decimal32", RID_DFLOAT32, D_EXT }, 101 { "_Decimal64", RID_DFLOAT64, D_EXT }, 102 { "_Decimal128", RID_DFLOAT128, D_EXT }, 103 { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, 104 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, 105 { "__alignof", RID_ALIGNOF, 0 }, 106 { "__alignof__", RID_ALIGNOF, 0 }, 107 { "__asm", RID_ASM, 0 }, 108 { "__asm__", RID_ASM, 0 }, 109 { "__attribute", RID_ATTRIBUTE, 0 }, 110 { "__attribute__", RID_ATTRIBUTE, 0 }, 111 { "__builtin_choose_expr", RID_CHOOSE_EXPR, 0 }, 112 { "__builtin_offsetof", RID_OFFSETOF, 0 }, 113 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, 0 }, 114 { "__builtin_va_arg", RID_VA_ARG, 0 }, 115 { "__complex", RID_COMPLEX, 0 }, 116 { "__complex__", RID_COMPLEX, 0 }, 117 { "__const", RID_CONST, 0 }, 118 { "__const__", RID_CONST, 0 }, 119 { "__extension__", RID_EXTENSION, 0 }, 120 { "__func__", RID_C99_FUNCTION_NAME, 0 }, 121 { "__imag", RID_IMAGPART, 0 }, 122 { "__imag__", RID_IMAGPART, 0 }, 123 { "__inline", RID_INLINE, 0 }, 124 { "__inline__", RID_INLINE, 0 }, 125 { "__label__", RID_LABEL, 0 }, 126 { "__real", RID_REALPART, 0 }, 127 { "__real__", RID_REALPART, 0 }, 128 { "__restrict", RID_RESTRICT, 0 }, 129 { "__restrict__", RID_RESTRICT, 0 }, 130 { "__signed", RID_SIGNED, 0 }, 131 { "__signed__", RID_SIGNED, 0 }, 132 { "__thread", RID_THREAD, 0 }, 133 { "__typeof", RID_TYPEOF, 0 }, 134 { "__typeof__", RID_TYPEOF, 0 }, 135 { "__volatile", RID_VOLATILE, 0 }, 136 { "__volatile__", RID_VOLATILE, 0 }, 137 { "asm", RID_ASM, D_EXT }, 138 { "auto", RID_AUTO, 0 }, 139 { "break", RID_BREAK, 0 }, 140 { "case", RID_CASE, 0 }, 141 { "char", RID_CHAR, 0 }, 142 { "const", RID_CONST, 0 }, 143 { "continue", RID_CONTINUE, 0 }, 144 { "default", RID_DEFAULT, 0 }, 145 { "do", RID_DO, 0 }, 146 { "double", RID_DOUBLE, 0 }, 147 { "else", RID_ELSE, 0 }, 148 { "enum", RID_ENUM, 0 }, 149 { "extern", RID_EXTERN, 0 }, 150 { "float", RID_FLOAT, 0 }, 151 { "for", RID_FOR, 0 }, 152 { "goto", RID_GOTO, 0 }, 153 { "if", RID_IF, 0 }, 154 { "inline", RID_INLINE, D_EXT89 }, 155 { "int", RID_INT, 0 }, 156 { "long", RID_LONG, 0 }, 157 { "register", RID_REGISTER, 0 }, 158 { "restrict", RID_RESTRICT, D_C89 }, 159 { "return", RID_RETURN, 0 }, 160 { "short", RID_SHORT, 0 }, 161 { "signed", RID_SIGNED, 0 }, 162 { "sizeof", RID_SIZEOF, 0 }, 163 { "static", RID_STATIC, 0 }, 164 { "struct", RID_STRUCT, 0 }, 165 { "switch", RID_SWITCH, 0 }, 166 { "typedef", RID_TYPEDEF, 0 }, 167 { "typeof", RID_TYPEOF, D_EXT }, 168 { "union", RID_UNION, 0 }, 169 { "unsigned", RID_UNSIGNED, 0 }, 170 { "void", RID_VOID, 0 }, 171 { "volatile", RID_VOLATILE, 0 }, 172 { "while", RID_WHILE, 0 }, 173 /* These Objective-C keywords are recognized only immediately after 174 an '@'. */ 175 { "class", RID_AT_CLASS, D_OBJC }, 176 { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, 177 { "defs", RID_AT_DEFS, D_OBJC }, 178 { "encode", RID_AT_ENCODE, D_OBJC }, 179 { "end", RID_AT_END, D_OBJC }, 180 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, 181 { "interface", RID_AT_INTERFACE, D_OBJC }, 182 { "private", RID_AT_PRIVATE, D_OBJC }, 183 { "protected", RID_AT_PROTECTED, D_OBJC }, 184 { "protocol", RID_AT_PROTOCOL, D_OBJC }, 185 { "public", RID_AT_PUBLIC, D_OBJC }, 186 { "selector", RID_AT_SELECTOR, D_OBJC }, 187 { "throw", RID_AT_THROW, D_OBJC }, 188 { "try", RID_AT_TRY, D_OBJC }, 189 { "catch", RID_AT_CATCH, D_OBJC }, 190 { "finally", RID_AT_FINALLY, D_OBJC }, 191 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC }, 192 /* These are recognized only in protocol-qualifier context 193 (see above) */ 194 { "bycopy", RID_BYCOPY, D_OBJC }, 195 { "byref", RID_BYREF, D_OBJC }, 196 { "in", RID_IN, D_OBJC }, 197 { "inout", RID_INOUT, D_OBJC }, 198 { "oneway", RID_ONEWAY, D_OBJC }, 199 { "out", RID_OUT, D_OBJC }, 200}; 201#define N_reswords (sizeof reswords / sizeof (struct resword)) 202 203/* All OpenMP clauses. OpenMP 2.5. */ 204typedef enum pragma_omp_clause { 205 PRAGMA_OMP_CLAUSE_NONE = 0, 206 207 PRAGMA_OMP_CLAUSE_COPYIN, 208 PRAGMA_OMP_CLAUSE_COPYPRIVATE, 209 PRAGMA_OMP_CLAUSE_DEFAULT, 210 PRAGMA_OMP_CLAUSE_FIRSTPRIVATE, 211 PRAGMA_OMP_CLAUSE_IF, 212 PRAGMA_OMP_CLAUSE_LASTPRIVATE, 213 PRAGMA_OMP_CLAUSE_NOWAIT, 214 PRAGMA_OMP_CLAUSE_NUM_THREADS, 215 PRAGMA_OMP_CLAUSE_ORDERED, 216 PRAGMA_OMP_CLAUSE_PRIVATE, 217 PRAGMA_OMP_CLAUSE_REDUCTION, 218 PRAGMA_OMP_CLAUSE_SCHEDULE, 219 PRAGMA_OMP_CLAUSE_SHARED 220} pragma_omp_clause; 221 222 223/* Initialization routine for this file. */ 224 225void 226c_parse_init (void) 227{ 228 /* The only initialization required is of the reserved word 229 identifiers. */ 230 unsigned int i; 231 tree id; 232 int mask = (flag_isoc99 ? 0 : D_C89) 233 | (flag_no_asm ? (flag_isoc99 ? D_EXT : D_EXT|D_EXT89) : 0); 234 235 if (!c_dialect_objc ()) 236 mask |= D_OBJC; 237 238 ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX); 239 for (i = 0; i < N_reswords; i++) 240 { 241 /* If a keyword is disabled, do not enter it into the table 242 and so create a canonical spelling that isn't a keyword. */ 243 if (reswords[i].disable & mask) 244 continue; 245 246 id = get_identifier (reswords[i].word); 247 C_RID_CODE (id) = reswords[i].rid; 248 C_IS_RESERVED_WORD (id) = 1; 249 ridpointers [(int) reswords[i].rid] = id; 250 } 251} 252 253/* The C lexer intermediates between the lexer in cpplib and c-lex.c 254 and the C parser. Unlike the C++ lexer, the parser structure 255 stores the lexer information instead of using a separate structure. 256 Identifiers are separated into ordinary identifiers, type names, 257 keywords and some other Objective-C types of identifiers, and some 258 look-ahead is maintained. 259 260 ??? It might be a good idea to lex the whole file up front (as for 261 C++). It would then be possible to share more of the C and C++ 262 lexer code, if desired. */ 263 264/* The following local token type is used. */ 265 266/* A keyword. */ 267#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1)) 268 269/* More information about the type of a CPP_NAME token. */ 270typedef enum c_id_kind { 271 /* An ordinary identifier. */ 272 C_ID_ID, 273 /* An identifier declared as a typedef name. */ 274 C_ID_TYPENAME, 275 /* An identifier declared as an Objective-C class name. */ 276 C_ID_CLASSNAME, 277 /* Not an identifier. */ 278 C_ID_NONE 279} c_id_kind; 280 281/* A single C token after string literal concatenation and conversion 282 of preprocessing tokens to tokens. */ 283typedef struct c_token GTY (()) 284{ 285 /* The kind of token. */ 286 ENUM_BITFIELD (cpp_ttype) type : 8; 287 /* If this token is a CPP_NAME, this value indicates whether also 288 declared as some kind of type. Otherwise, it is C_ID_NONE. */ 289 ENUM_BITFIELD (c_id_kind) id_kind : 8; 290 /* If this token is a keyword, this value indicates which keyword. 291 Otherwise, this value is RID_MAX. */ 292 ENUM_BITFIELD (rid) keyword : 8; 293 /* If this token is a CPP_PRAGMA, this indicates the pragma that 294 was seen. Otherwise it is PRAGMA_NONE. */ 295 ENUM_BITFIELD (pragma_kind) pragma_kind : 7; 296 /* True if this token is from a system header. */ 297 BOOL_BITFIELD in_system_header : 1; 298 /* The value associated with this token, if any. */ 299 tree value; 300 /* The location at which this token was found. */ 301 location_t location; 302} c_token; 303 304/* A parser structure recording information about the state and 305 context of parsing. Includes lexer information with up to two 306 tokens of look-ahead; more are not needed for C. */ 307typedef struct c_parser GTY(()) 308{ 309 /* The look-ahead tokens. */ 310 c_token tokens[2]; 311 /* How many look-ahead tokens are available (0, 1 or 2). */ 312 short tokens_avail; 313 /* True if a syntax error is being recovered from; false otherwise. 314 c_parser_error sets this flag. It should clear this flag when 315 enough tokens have been consumed to recover from the error. */ 316 BOOL_BITFIELD error : 1; 317 /* True if we're processing a pragma, and shouldn't automatically 318 consume CPP_PRAGMA_EOL. */ 319 BOOL_BITFIELD in_pragma : 1; 320} c_parser; 321 322 323/* The actual parser and external interface. ??? Does this need to be 324 garbage-collected? */ 325 326static GTY (()) c_parser *the_parser; 327 328 329/* Read in and lex a single token, storing it in *TOKEN. */ 330 331static void 332c_lex_one_token (c_token *token) 333{ 334 timevar_push (TV_LEX); 335 336 token->type = c_lex_with_flags (&token->value, &token->location, NULL); 337 token->id_kind = C_ID_NONE; 338 token->keyword = RID_MAX; 339 token->pragma_kind = PRAGMA_NONE; 340 token->in_system_header = in_system_header; 341 342 switch (token->type) 343 { 344 case CPP_NAME: 345 { 346 tree decl; 347 348 int objc_force_identifier = objc_need_raw_identifier; 349 OBJC_NEED_RAW_IDENTIFIER (0); 350 351 if (C_IS_RESERVED_WORD (token->value)) 352 { 353 enum rid rid_code = C_RID_CODE (token->value); 354 355 if (c_dialect_objc ()) 356 { 357 if (!OBJC_IS_AT_KEYWORD (rid_code) 358 && (!OBJC_IS_PQ_KEYWORD (rid_code) || objc_pq_context)) 359 { 360 /* Return the canonical spelling for this keyword. */ 361 token->value = ridpointers[(int) rid_code]; 362 token->type = CPP_KEYWORD; 363 token->keyword = rid_code; 364 break; 365 } 366 } 367 else 368 { 369 /* Return the canonical spelling for this keyword. */ 370 token->value = ridpointers[(int) rid_code]; 371 token->type = CPP_KEYWORD; 372 token->keyword = rid_code; 373 break; 374 } 375 } 376 377 decl = lookup_name (token->value); 378 if (decl) 379 { 380 if (TREE_CODE (decl) == TYPE_DECL) 381 { 382 token->id_kind = C_ID_TYPENAME; 383 break; 384 } 385 } 386 else if (c_dialect_objc ()) 387 { 388 tree objc_interface_decl = objc_is_class_name (token->value); 389 /* Objective-C class names are in the same namespace as 390 variables and typedefs, and hence are shadowed by local 391 declarations. */ 392 if (objc_interface_decl 393 && (global_bindings_p () 394 || (!objc_force_identifier && !decl))) 395 { 396 token->value = objc_interface_decl; 397 token->id_kind = C_ID_CLASSNAME; 398 break; 399 } 400 } 401 token->id_kind = C_ID_ID; 402 } 403 break; 404 case CPP_AT_NAME: 405 /* This only happens in Objective-C; it must be a keyword. */ 406 token->type = CPP_KEYWORD; 407 token->keyword = C_RID_CODE (token->value); 408 break; 409 case CPP_COLON: 410 case CPP_COMMA: 411 case CPP_CLOSE_PAREN: 412 case CPP_SEMICOLON: 413 /* These tokens may affect the interpretation of any identifiers 414 following, if doing Objective-C. */ 415 OBJC_NEED_RAW_IDENTIFIER (0); 416 break; 417 case CPP_PRAGMA: 418 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */ 419 token->pragma_kind = TREE_INT_CST_LOW (token->value); 420 token->value = NULL; 421 break; 422 default: 423 break; 424 } 425 timevar_pop (TV_LEX); 426} 427 428/* Return a pointer to the next token from PARSER, reading it in if 429 necessary. */ 430 431static inline c_token * 432c_parser_peek_token (c_parser *parser) 433{ 434 if (parser->tokens_avail == 0) 435 { 436 c_lex_one_token (&parser->tokens[0]); 437 parser->tokens_avail = 1; 438 } 439 return &parser->tokens[0]; 440} 441 442/* Return true if the next token from PARSER has the indicated 443 TYPE. */ 444 445static inline bool 446c_parser_next_token_is (c_parser *parser, enum cpp_ttype type) 447{ 448 return c_parser_peek_token (parser)->type == type; 449} 450 451/* Return true if the next token from PARSER does not have the 452 indicated TYPE. */ 453 454static inline bool 455c_parser_next_token_is_not (c_parser *parser, enum cpp_ttype type) 456{ 457 return !c_parser_next_token_is (parser, type); 458} 459 460/* Return true if the next token from PARSER is the indicated 461 KEYWORD. */ 462 463static inline bool 464c_parser_next_token_is_keyword (c_parser *parser, enum rid keyword) 465{ 466 c_token *token; 467 468 /* Peek at the next token. */ 469 token = c_parser_peek_token (parser); 470 /* Check to see if it is the indicated keyword. */ 471 return token->keyword == keyword; 472} 473 474/* Return true if TOKEN can start a type name, 475 false otherwise. */ 476static bool 477c_token_starts_typename (c_token *token) 478{ 479 switch (token->type) 480 { 481 case CPP_NAME: 482 switch (token->id_kind) 483 { 484 case C_ID_ID: 485 return false; 486 case C_ID_TYPENAME: 487 return true; 488 case C_ID_CLASSNAME: 489 gcc_assert (c_dialect_objc ()); 490 return true; 491 default: 492 gcc_unreachable (); 493 } 494 case CPP_KEYWORD: 495 switch (token->keyword) 496 { 497 case RID_UNSIGNED: 498 case RID_LONG: 499 case RID_SHORT: 500 case RID_SIGNED: 501 case RID_COMPLEX: 502 case RID_INT: 503 case RID_CHAR: 504 case RID_FLOAT: 505 case RID_DOUBLE: 506 case RID_VOID: 507 case RID_DFLOAT32: 508 case RID_DFLOAT64: 509 case RID_DFLOAT128: 510 case RID_BOOL: 511 case RID_ENUM: 512 case RID_STRUCT: 513 case RID_UNION: 514 case RID_TYPEOF: 515 case RID_CONST: 516 case RID_VOLATILE: 517 case RID_RESTRICT: 518 case RID_ATTRIBUTE: 519 return true; 520 default: 521 return false; 522 } 523 case CPP_LESS: 524 if (c_dialect_objc ()) 525 return true; 526 return false; 527 default: 528 return false; 529 } 530} 531 532/* Return true if the next token from PARSER can start a type name, 533 false otherwise. */ 534static inline bool 535c_parser_next_token_starts_typename (c_parser *parser) 536{ 537 c_token *token = c_parser_peek_token (parser); 538 return c_token_starts_typename (token); 539} 540 541/* Return true if TOKEN can start declaration specifiers, false 542 otherwise. */ 543static bool 544c_token_starts_declspecs (c_token *token) 545{ 546 switch (token->type) 547 { 548 case CPP_NAME: 549 switch (token->id_kind) 550 { 551 case C_ID_ID: 552 return false; 553 case C_ID_TYPENAME: 554 return true; 555 case C_ID_CLASSNAME: 556 gcc_assert (c_dialect_objc ()); 557 return true; 558 default: 559 gcc_unreachable (); 560 } 561 case CPP_KEYWORD: 562 switch (token->keyword) 563 { 564 case RID_STATIC: 565 case RID_EXTERN: 566 case RID_REGISTER: 567 case RID_TYPEDEF: 568 case RID_INLINE: 569 case RID_AUTO: 570 case RID_THREAD: 571 case RID_UNSIGNED: 572 case RID_LONG: 573 case RID_SHORT: 574 case RID_SIGNED: 575 case RID_COMPLEX: 576 case RID_INT: 577 case RID_CHAR: 578 case RID_FLOAT: 579 case RID_DOUBLE: 580 case RID_VOID: 581 case RID_DFLOAT32: 582 case RID_DFLOAT64: 583 case RID_DFLOAT128: 584 case RID_BOOL: 585 case RID_ENUM: 586 case RID_STRUCT: 587 case RID_UNION: 588 case RID_TYPEOF: 589 case RID_CONST: 590 case RID_VOLATILE: 591 case RID_RESTRICT: 592 case RID_ATTRIBUTE: 593 return true; 594 default: 595 return false; 596 } 597 case CPP_LESS: 598 if (c_dialect_objc ()) 599 return true; 600 return false; 601 default: 602 return false; 603 } 604} 605 606/* Return true if the next token from PARSER can start declaration 607 specifiers, false otherwise. */ 608static inline bool 609c_parser_next_token_starts_declspecs (c_parser *parser) 610{ 611 c_token *token = c_parser_peek_token (parser); 612 return c_token_starts_declspecs (token); 613} 614 615/* Return a pointer to the next-but-one token from PARSER, reading it 616 in if necessary. The next token is already read in. */ 617 618static c_token * 619c_parser_peek_2nd_token (c_parser *parser) 620{ 621 if (parser->tokens_avail >= 2) 622 return &parser->tokens[1]; 623 gcc_assert (parser->tokens_avail == 1); 624 gcc_assert (parser->tokens[0].type != CPP_EOF); 625 gcc_assert (parser->tokens[0].type != CPP_PRAGMA_EOL); 626 c_lex_one_token (&parser->tokens[1]); 627 parser->tokens_avail = 2; 628 return &parser->tokens[1]; 629} 630 631/* Consume the next token from PARSER. */ 632 633static void 634c_parser_consume_token (c_parser *parser) 635{ 636 gcc_assert (parser->tokens_avail >= 1); 637 gcc_assert (parser->tokens[0].type != CPP_EOF); 638 gcc_assert (!parser->in_pragma || parser->tokens[0].type != CPP_PRAGMA_EOL); 639 gcc_assert (parser->error || parser->tokens[0].type != CPP_PRAGMA); 640 if (parser->tokens_avail == 2) 641 parser->tokens[0] = parser->tokens[1]; 642 parser->tokens_avail--; 643} 644 645/* Expect the current token to be a #pragma. Consume it and remember 646 that we've begun parsing a pragma. */ 647 648static void 649c_parser_consume_pragma (c_parser *parser) 650{ 651 gcc_assert (!parser->in_pragma); 652 gcc_assert (parser->tokens_avail >= 1); 653 gcc_assert (parser->tokens[0].type == CPP_PRAGMA); 654 if (parser->tokens_avail == 2) 655 parser->tokens[0] = parser->tokens[1]; 656 parser->tokens_avail--; 657 parser->in_pragma = true; 658} 659 660/* Update the globals input_location and in_system_header from 661 TOKEN. */ 662static inline void 663c_parser_set_source_position_from_token (c_token *token) 664{ 665 if (token->type != CPP_EOF) 666 { 667 input_location = token->location; 668 in_system_header = token->in_system_header; 669 } 670} 671 672/* Issue a diagnostic of the form 673 FILE:LINE: MESSAGE before TOKEN 674 where TOKEN is the next token in the input stream of PARSER. 675 MESSAGE (specified by the caller) is usually of the form "expected 676 OTHER-TOKEN". 677 678 Do not issue a diagnostic if still recovering from an error. 679 680 ??? This is taken from the C++ parser, but building up messages in 681 this way is not i18n-friendly and some other approach should be 682 used. */ 683 684static void 685c_parser_error (c_parser *parser, const char *gmsgid) 686{ 687 c_token *token = c_parser_peek_token (parser); 688 if (parser->error) 689 return; 690 parser->error = true; 691 if (!gmsgid) 692 return; 693 /* This diagnostic makes more sense if it is tagged to the line of 694 the token we just peeked at. */ 695 c_parser_set_source_position_from_token (token); 696 c_parse_error (gmsgid, 697 /* Because c_parse_error does not understand 698 CPP_KEYWORD, keywords are treated like 699 identifiers. */ 700 (token->type == CPP_KEYWORD ? CPP_NAME : token->type), 701 token->value); 702} 703 704/* If the next token is of the indicated TYPE, consume it. Otherwise, 705 issue the error MSGID. If MSGID is NULL then a message has already 706 been produced and no message will be produced this time. Returns 707 true if found, false otherwise. */ 708 709static bool 710c_parser_require (c_parser *parser, 711 enum cpp_ttype type, 712 const char *msgid) 713{ 714 if (c_parser_next_token_is (parser, type)) 715 { 716 c_parser_consume_token (parser); 717 return true; 718 } 719 else 720 { 721 c_parser_error (parser, msgid); 722 return false; 723 } 724} 725 726/* If the next token is the indicated keyword, consume it. Otherwise, 727 issue the error MSGID. Returns true if found, false otherwise. */ 728 729static bool 730c_parser_require_keyword (c_parser *parser, 731 enum rid keyword, 732 const char *msgid) 733{ 734 if (c_parser_next_token_is_keyword (parser, keyword)) 735 { 736 c_parser_consume_token (parser); 737 return true; 738 } 739 else 740 { 741 c_parser_error (parser, msgid); 742 return false; 743 } 744} 745 746/* Like c_parser_require, except that tokens will be skipped until the 747 desired token is found. An error message is still produced if the 748 next token is not as expected. If MSGID is NULL then a message has 749 already been produced and no message will be produced this 750 time. */ 751 752static void 753c_parser_skip_until_found (c_parser *parser, 754 enum cpp_ttype type, 755 const char *msgid) 756{ 757 unsigned nesting_depth = 0; 758 759 if (c_parser_require (parser, type, msgid)) 760 return; 761 762 /* Skip tokens until the desired token is found. */ 763 while (true) 764 { 765 /* Peek at the next token. */ 766 c_token *token = c_parser_peek_token (parser); 767 /* If we've reached the token we want, consume it and stop. */ 768 if (token->type == type && !nesting_depth) 769 { 770 c_parser_consume_token (parser); 771 break; 772 } 773 774 /* If we've run out of tokens, stop. */ 775 if (token->type == CPP_EOF) 776 return; 777 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma) 778 return; 779 if (token->type == CPP_OPEN_BRACE 780 || token->type == CPP_OPEN_PAREN 781 || token->type == CPP_OPEN_SQUARE) 782 ++nesting_depth; 783 else if (token->type == CPP_CLOSE_BRACE 784 || token->type == CPP_CLOSE_PAREN 785 || token->type == CPP_CLOSE_SQUARE) 786 { 787 if (nesting_depth-- == 0) 788 break; 789 } 790 /* Consume this token. */ 791 c_parser_consume_token (parser); 792 } 793 parser->error = false; 794} 795 796/* Skip tokens until the end of a parameter is found, but do not 797 consume the comma, semicolon or closing delimiter. */ 798 799static void 800c_parser_skip_to_end_of_parameter (c_parser *parser) 801{ 802 unsigned nesting_depth = 0; 803 804 while (true) 805 { 806 c_token *token = c_parser_peek_token (parser); 807 if ((token->type == CPP_COMMA || token->type == CPP_SEMICOLON) 808 && !nesting_depth) 809 break; 810 /* If we've run out of tokens, stop. */ 811 if (token->type == CPP_EOF) 812 return; 813 if (token->type == CPP_PRAGMA_EOL && parser->in_pragma) 814 return; 815 if (token->type == CPP_OPEN_BRACE 816 || token->type == CPP_OPEN_PAREN 817 || token->type == CPP_OPEN_SQUARE) 818 ++nesting_depth; 819 else if (token->type == CPP_CLOSE_BRACE 820 || token->type == CPP_CLOSE_PAREN 821 || token->type == CPP_CLOSE_SQUARE) 822 { 823 if (nesting_depth-- == 0) 824 break; 825 } 826 /* Consume this token. */ 827 c_parser_consume_token (parser); 828 } 829 parser->error = false; 830} 831 832/* Expect to be at the end of the pragma directive and consume an 833 end of line marker. */ 834 835static void 836c_parser_skip_to_pragma_eol (c_parser *parser) 837{ 838 gcc_assert (parser->in_pragma); 839 parser->in_pragma = false; 840 841 if (!c_parser_require (parser, CPP_PRAGMA_EOL, "expected end of line")) 842 while (true) 843 { 844 c_token *token = c_parser_peek_token (parser); 845 if (token->type == CPP_EOF) 846 break; 847 if (token->type == CPP_PRAGMA_EOL) 848 { 849 c_parser_consume_token (parser); 850 break; 851 } 852 c_parser_consume_token (parser); 853 } 854 855 parser->error = false; 856} 857 858/* Skip tokens until we have consumed an entire block, or until we 859 have consumed a non-nested ';'. */ 860 861static void 862c_parser_skip_to_end_of_block_or_statement (c_parser *parser) 863{ 864 unsigned nesting_depth = 0; 865 bool save_error = parser->error; 866 867 while (true) 868 { 869 c_token *token; 870 871 /* Peek at the next token. */ 872 token = c_parser_peek_token (parser); 873 874 switch (token->type) 875 { 876 case CPP_EOF: 877 return; 878 879 case CPP_PRAGMA_EOL: 880 if (parser->in_pragma) 881 return; 882 break; 883 884 case CPP_SEMICOLON: 885 /* If the next token is a ';', we have reached the 886 end of the statement. */ 887 if (!nesting_depth) 888 { 889 /* Consume the ';'. */ 890 c_parser_consume_token (parser); 891 goto finished; 892 } 893 break; 894 895 case CPP_CLOSE_BRACE: 896 /* If the next token is a non-nested '}', then we have 897 reached the end of the current block. */ 898 if (nesting_depth == 0 || --nesting_depth == 0) 899 { 900 c_parser_consume_token (parser); 901 goto finished; 902 } 903 break; 904 905 case CPP_OPEN_BRACE: 906 /* If it the next token is a '{', then we are entering a new 907 block. Consume the entire block. */ 908 ++nesting_depth; 909 break; 910 911 case CPP_PRAGMA: 912 /* If we see a pragma, consume the whole thing at once. We 913 have some safeguards against consuming pragmas willy-nilly. 914 Normally, we'd expect to be here with parser->error set, 915 which disables these safeguards. But it's possible to get 916 here for secondary error recovery, after parser->error has 917 been cleared. */ 918 c_parser_consume_pragma (parser); 919 c_parser_skip_to_pragma_eol (parser); 920 parser->error = save_error; 921 continue; 922 923 default: 924 break; 925 } 926 927 c_parser_consume_token (parser); 928 } 929 930 finished: 931 parser->error = false; 932} 933 934/* Save the warning flags which are controlled by __extension__. */ 935 936static inline int 937disable_extension_diagnostics (void) 938{ 939 int ret = (pedantic 940 | (warn_pointer_arith << 1) 941 | (warn_traditional << 2) 942 | (flag_iso << 3)); 943 pedantic = 0; 944 warn_pointer_arith = 0; 945 warn_traditional = 0; 946 flag_iso = 0; 947 return ret; 948} 949 950/* Restore the warning flags which are controlled by __extension__. 951 FLAGS is the return value from disable_extension_diagnostics. */ 952 953static inline void 954restore_extension_diagnostics (int flags) 955{ 956 pedantic = flags & 1; 957 warn_pointer_arith = (flags >> 1) & 1; 958 warn_traditional = (flags >> 2) & 1; 959 flag_iso = (flags >> 3) & 1; 960} 961 962/* Possibly kinds of declarator to parse. */ 963typedef enum c_dtr_syn { 964 /* A normal declarator with an identifier. */ 965 C_DTR_NORMAL, 966 /* An abstract declarator (maybe empty). */ 967 C_DTR_ABSTRACT, 968 /* A parameter declarator: may be either, but after a type name does 969 not redeclare a typedef name as an identifier if it can 970 alternatively be interpreted as a typedef name; see DR#009, 971 applied in C90 TC1, omitted from C99 and reapplied in C99 TC2 972 following DR#249. For example, given a typedef T, "int T" and 973 "int *T" are valid parameter declarations redeclaring T, while 974 "int (T)" and "int * (T)" and "int (T[])" and "int (T (int))" are 975 abstract declarators rather than involving redundant parentheses; 976 the same applies with attributes inside the parentheses before 977 "T". */ 978 C_DTR_PARM 979} c_dtr_syn; 980 981static void c_parser_external_declaration (c_parser *); 982static void c_parser_asm_definition (c_parser *); 983static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool, bool); 984static void c_parser_declspecs (c_parser *, struct c_declspecs *, bool, bool, 985 bool); 986static struct c_typespec c_parser_enum_specifier (c_parser *); 987static struct c_typespec c_parser_struct_or_union_specifier (c_parser *); 988static tree c_parser_struct_declaration (c_parser *); 989static struct c_typespec c_parser_typeof_specifier (c_parser *); 990static struct c_declarator *c_parser_declarator (c_parser *, bool, c_dtr_syn, 991 bool *); 992static struct c_declarator *c_parser_direct_declarator (c_parser *, bool, 993 c_dtr_syn, bool *); 994static struct c_declarator *c_parser_direct_declarator_inner (c_parser *, 995 bool, 996 struct c_declarator *); 997static struct c_arg_info *c_parser_parms_declarator (c_parser *, bool, tree); 998static struct c_arg_info *c_parser_parms_list_declarator (c_parser *, tree); 999static struct c_parm *c_parser_parameter_declaration (c_parser *, tree); 1000static tree c_parser_simple_asm_expr (c_parser *); 1001static tree c_parser_attributes (c_parser *); 1002static struct c_type_name *c_parser_type_name (c_parser *); 1003static struct c_expr c_parser_initializer (c_parser *); 1004static struct c_expr c_parser_braced_init (c_parser *, tree, bool); 1005static void c_parser_initelt (c_parser *); 1006static void c_parser_initval (c_parser *, struct c_expr *); 1007static tree c_parser_compound_statement (c_parser *); 1008static void c_parser_compound_statement_nostart (c_parser *); 1009static void c_parser_label (c_parser *); 1010static void c_parser_statement (c_parser *); 1011static void c_parser_statement_after_labels (c_parser *); 1012static void c_parser_if_statement (c_parser *); 1013static void c_parser_switch_statement (c_parser *); 1014static void c_parser_while_statement (c_parser *); 1015static void c_parser_do_statement (c_parser *); 1016static void c_parser_for_statement (c_parser *); 1017static tree c_parser_asm_statement (c_parser *); 1018static tree c_parser_asm_operands (c_parser *, bool); 1019static tree c_parser_asm_clobbers (c_parser *); 1020static struct c_expr c_parser_expr_no_commas (c_parser *, struct c_expr *); 1021static struct c_expr c_parser_conditional_expression (c_parser *, 1022 struct c_expr *); 1023static struct c_expr c_parser_binary_expression (c_parser *, struct c_expr *); 1024static struct c_expr c_parser_cast_expression (c_parser *, struct c_expr *); 1025static struct c_expr c_parser_unary_expression (c_parser *); 1026static struct c_expr c_parser_sizeof_expression (c_parser *); 1027static struct c_expr c_parser_alignof_expression (c_parser *); 1028static struct c_expr c_parser_postfix_expression (c_parser *); 1029static struct c_expr c_parser_postfix_expression_after_paren_type (c_parser *, 1030 struct c_type_name *); 1031static struct c_expr c_parser_postfix_expression_after_primary (c_parser *, 1032 struct c_expr); 1033static struct c_expr c_parser_expression (c_parser *); 1034static struct c_expr c_parser_expression_conv (c_parser *); 1035static tree c_parser_expr_list (c_parser *, bool); 1036static void c_parser_omp_construct (c_parser *); 1037static void c_parser_omp_threadprivate (c_parser *); 1038static void c_parser_omp_barrier (c_parser *); 1039static void c_parser_omp_flush (c_parser *); 1040 1041enum pragma_context { pragma_external, pragma_stmt, pragma_compound }; 1042static bool c_parser_pragma (c_parser *, enum pragma_context); 1043 1044/* These Objective-C parser functions are only ever called when 1045 compiling Objective-C. */ 1046static void c_parser_objc_class_definition (c_parser *); 1047static void c_parser_objc_class_instance_variables (c_parser *); 1048static void c_parser_objc_class_declaration (c_parser *); 1049static void c_parser_objc_alias_declaration (c_parser *); 1050static void c_parser_objc_protocol_definition (c_parser *); 1051static enum tree_code c_parser_objc_method_type (c_parser *); 1052static void c_parser_objc_method_definition (c_parser *); 1053static void c_parser_objc_methodprotolist (c_parser *); 1054static void c_parser_objc_methodproto (c_parser *); 1055static tree c_parser_objc_method_decl (c_parser *); 1056static tree c_parser_objc_type_name (c_parser *); 1057static tree c_parser_objc_protocol_refs (c_parser *); 1058static void c_parser_objc_try_catch_statement (c_parser *); 1059static void c_parser_objc_synchronized_statement (c_parser *); 1060static tree c_parser_objc_selector (c_parser *); 1061static tree c_parser_objc_selector_arg (c_parser *); 1062static tree c_parser_objc_receiver (c_parser *); 1063static tree c_parser_objc_message_args (c_parser *); 1064static tree c_parser_objc_keywordexpr (c_parser *); 1065 1066/* Parse a translation unit (C90 6.7, C99 6.9). 1067 1068 translation-unit: 1069 external-declarations 1070 1071 external-declarations: 1072 external-declaration 1073 external-declarations external-declaration 1074 1075 GNU extensions: 1076 1077 translation-unit: 1078 empty 1079*/ 1080 1081static void 1082c_parser_translation_unit (c_parser *parser) 1083{ 1084 if (c_parser_next_token_is (parser, CPP_EOF)) 1085 { 1086 if (pedantic) 1087 pedwarn ("ISO C forbids an empty source file"); 1088 } 1089 else 1090 { 1091 void *obstack_position = obstack_alloc (&parser_obstack, 0); 1092 do 1093 { 1094 ggc_collect (); 1095 c_parser_external_declaration (parser); 1096 obstack_free (&parser_obstack, obstack_position); 1097 } 1098 while (c_parser_next_token_is_not (parser, CPP_EOF)); 1099 } 1100} 1101 1102/* Parse an external declaration (C90 6.7, C99 6.9). 1103 1104 external-declaration: 1105 function-definition 1106 declaration 1107 1108 GNU extensions: 1109 1110 external-declaration: 1111 asm-definition 1112 ; 1113 __extension__ external-declaration 1114 1115 Objective-C: 1116 1117 external-declaration: 1118 objc-class-definition 1119 objc-class-declaration 1120 objc-alias-declaration 1121 objc-protocol-definition 1122 objc-method-definition 1123 @end 1124*/ 1125 1126static void 1127c_parser_external_declaration (c_parser *parser) 1128{ 1129 int ext; 1130 switch (c_parser_peek_token (parser)->type) 1131 { 1132 case CPP_KEYWORD: 1133 switch (c_parser_peek_token (parser)->keyword) 1134 { 1135 case RID_EXTENSION: 1136 ext = disable_extension_diagnostics (); 1137 c_parser_consume_token (parser); 1138 c_parser_external_declaration (parser); 1139 restore_extension_diagnostics (ext); 1140 break; 1141 case RID_ASM: 1142 c_parser_asm_definition (parser); 1143 break; 1144 case RID_AT_INTERFACE: 1145 case RID_AT_IMPLEMENTATION: 1146 gcc_assert (c_dialect_objc ()); 1147 c_parser_objc_class_definition (parser); 1148 break; 1149 case RID_AT_CLASS: 1150 gcc_assert (c_dialect_objc ()); 1151 c_parser_objc_class_declaration (parser); 1152 break; 1153 case RID_AT_ALIAS: 1154 gcc_assert (c_dialect_objc ()); 1155 c_parser_objc_alias_declaration (parser); 1156 break; 1157 case RID_AT_PROTOCOL: 1158 gcc_assert (c_dialect_objc ()); 1159 c_parser_objc_protocol_definition (parser); 1160 break; 1161 case RID_AT_END: 1162 gcc_assert (c_dialect_objc ()); 1163 c_parser_consume_token (parser); 1164 objc_finish_implementation (); 1165 break; 1166 default: 1167 goto decl_or_fndef; 1168 } 1169 break; 1170 case CPP_SEMICOLON: 1171 if (pedantic) 1172 pedwarn ("ISO C does not allow extra %<;%> outside of a function"); 1173 c_parser_consume_token (parser); 1174 break; 1175 case CPP_PRAGMA: 1176 c_parser_pragma (parser, pragma_external); 1177 break; 1178 case CPP_PLUS: 1179 case CPP_MINUS: 1180 if (c_dialect_objc ()) 1181 { 1182 c_parser_objc_method_definition (parser); 1183 break; 1184 } 1185 /* Else fall through, and yield a syntax error trying to parse 1186 as a declaration or function definition. */ 1187 default: 1188 decl_or_fndef: 1189 /* A declaration or a function definition. We can only tell 1190 which after parsing the declaration specifiers, if any, and 1191 the first declarator. */ 1192 c_parser_declaration_or_fndef (parser, true, true, false, true); 1193 break; 1194 } 1195} 1196 1197 1198/* Parse a declaration or function definition (C90 6.5, 6.7.1, C99 1199 6.7, 6.9.1). If FNDEF_OK is true, a function definition is 1200 accepted; otherwise (old-style parameter declarations) only other 1201 declarations are accepted. If NESTED is true, we are inside a 1202 function or parsing old-style parameter declarations; any functions 1203 encountered are nested functions and declaration specifiers are 1204 required; otherwise we are at top level and functions are normal 1205 functions and declaration specifiers may be optional. If EMPTY_OK 1206 is true, empty declarations are OK (subject to all other 1207 constraints); otherwise (old-style parameter declarations) they are 1208 diagnosed. If START_ATTR_OK is true, the declaration specifiers 1209 may start with attributes; otherwise they may not. 1210 1211 declaration: 1212 declaration-specifiers init-declarator-list[opt] ; 1213 1214 function-definition: 1215 declaration-specifiers[opt] declarator declaration-list[opt] 1216 compound-statement 1217 1218 declaration-list: 1219 declaration 1220 declaration-list declaration 1221 1222 init-declarator-list: 1223 init-declarator 1224 init-declarator-list , init-declarator 1225 1226 init-declarator: 1227 declarator simple-asm-expr[opt] attributes[opt] 1228 declarator simple-asm-expr[opt] attributes[opt] = initializer 1229 1230 GNU extensions: 1231 1232 nested-function-definition: 1233 declaration-specifiers declarator declaration-list[opt] 1234 compound-statement 1235 1236 The simple-asm-expr and attributes are GNU extensions. 1237 1238 This function does not handle __extension__; that is handled in its 1239 callers. ??? Following the old parser, __extension__ may start 1240 external declarations, declarations in functions and declarations 1241 at the start of "for" loops, but not old-style parameter 1242 declarations. 1243 1244 C99 requires declaration specifiers in a function definition; the 1245 absence is diagnosed through the diagnosis of implicit int. In GNU 1246 C we also allow but diagnose declarations without declaration 1247 specifiers, but only at top level (elsewhere they conflict with 1248 other syntax). 1249 1250 OpenMP: 1251 1252 declaration: 1253 threadprivate-directive */ 1254 1255static void 1256c_parser_declaration_or_fndef (c_parser *parser, bool fndef_ok, bool empty_ok, 1257 bool nested, bool start_attr_ok) 1258{ 1259 struct c_declspecs *specs; 1260 tree prefix_attrs; 1261 tree all_prefix_attrs; 1262 bool diagnosed_no_specs = false; 1263 1264 specs = build_null_declspecs (); 1265 c_parser_declspecs (parser, specs, true, true, start_attr_ok); 1266 if (parser->error) 1267 { 1268 c_parser_skip_to_end_of_block_or_statement (parser); 1269 return; 1270 } 1271 if (nested && !specs->declspecs_seen_p) 1272 { 1273 c_parser_error (parser, "expected declaration specifiers"); 1274 c_parser_skip_to_end_of_block_or_statement (parser); 1275 return; 1276 } 1277 finish_declspecs (specs); 1278 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 1279 { 1280 if (empty_ok) 1281 shadow_tag (specs); 1282 else 1283 { 1284 shadow_tag_warned (specs, 1); 1285 pedwarn ("empty declaration"); 1286 } 1287 c_parser_consume_token (parser); 1288 return; 1289 } 1290 pending_xref_error (); 1291 prefix_attrs = specs->attrs; 1292 all_prefix_attrs = prefix_attrs; 1293 specs->attrs = NULL_TREE; 1294 while (true) 1295 { 1296 struct c_declarator *declarator; 1297 bool dummy = false; 1298 tree fnbody; 1299 /* Declaring either one or more declarators (in which case we 1300 should diagnose if there were no declaration specifiers) or a 1301 function definition (in which case the diagnostic for 1302 implicit int suffices). */ 1303 declarator = c_parser_declarator (parser, specs->type_seen_p, 1304 C_DTR_NORMAL, &dummy); 1305 if (declarator == NULL) 1306 { 1307 c_parser_skip_to_end_of_block_or_statement (parser); 1308 return; 1309 } 1310 if (c_parser_next_token_is (parser, CPP_EQ) 1311 || c_parser_next_token_is (parser, CPP_COMMA) 1312 || c_parser_next_token_is (parser, CPP_SEMICOLON) 1313 || c_parser_next_token_is_keyword (parser, RID_ASM) 1314 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) 1315 { 1316 tree asm_name = NULL_TREE; 1317 tree postfix_attrs = NULL_TREE; 1318 if (!diagnosed_no_specs && !specs->declspecs_seen_p) 1319 { 1320 diagnosed_no_specs = true; 1321 pedwarn ("data definition has no type or storage class"); 1322 } 1323 /* Having seen a data definition, there cannot now be a 1324 function definition. */ 1325 fndef_ok = false; 1326 if (c_parser_next_token_is_keyword (parser, RID_ASM)) 1327 asm_name = c_parser_simple_asm_expr (parser); 1328 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) 1329 postfix_attrs = c_parser_attributes (parser); 1330 if (c_parser_next_token_is (parser, CPP_EQ)) 1331 { 1332 tree d; 1333 struct c_expr init; 1334 c_parser_consume_token (parser); 1335 /* The declaration of the variable is in effect while 1336 its initializer is parsed. */ 1337 d = start_decl (declarator, specs, true, 1338 chainon (postfix_attrs, all_prefix_attrs)); 1339 if (!d) 1340 d = error_mark_node; 1341 start_init (d, asm_name, global_bindings_p ()); 1342 init = c_parser_initializer (parser); 1343 finish_init (); 1344 if (d != error_mark_node) 1345 { 1346 maybe_warn_string_init (TREE_TYPE (d), init); 1347 finish_decl (d, init.value, asm_name); 1348 } 1349 } 1350 else 1351 { 1352 tree d = start_decl (declarator, specs, false, 1353 chainon (postfix_attrs, 1354 all_prefix_attrs)); 1355 if (d) 1356 finish_decl (d, NULL_TREE, asm_name); 1357 } 1358 if (c_parser_next_token_is (parser, CPP_COMMA)) 1359 { 1360 c_parser_consume_token (parser); 1361 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) 1362 all_prefix_attrs = chainon (c_parser_attributes (parser), 1363 prefix_attrs); 1364 else 1365 all_prefix_attrs = prefix_attrs; 1366 continue; 1367 } 1368 else if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 1369 { 1370 c_parser_consume_token (parser); 1371 return; 1372 } 1373 else 1374 { 1375 c_parser_error (parser, "expected %<,%> or %<;%>"); 1376 c_parser_skip_to_end_of_block_or_statement (parser); 1377 return; 1378 } 1379 } 1380 else if (!fndef_ok) 1381 { 1382 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, " 1383 "%<asm%> or %<__attribute__%>"); 1384 c_parser_skip_to_end_of_block_or_statement (parser); 1385 return; 1386 } 1387 /* Function definition (nested or otherwise). */ 1388 if (nested) 1389 { 1390 if (pedantic) 1391 pedwarn ("ISO C forbids nested functions"); 1392 push_function_context (); 1393 } 1394 if (!start_function (specs, declarator, all_prefix_attrs)) 1395 { 1396 /* This can appear in many cases looking nothing like a 1397 function definition, so we don't give a more specific 1398 error suggesting there was one. */ 1399 c_parser_error (parser, "expected %<=%>, %<,%>, %<;%>, %<asm%> " 1400 "or %<__attribute__%>"); 1401 if (nested) 1402 pop_function_context (); 1403 break; 1404 } 1405 /* Parse old-style parameter declarations. ??? Attributes are 1406 not allowed to start declaration specifiers here because of a 1407 syntax conflict between a function declaration with attribute 1408 suffix and a function definition with an attribute prefix on 1409 first old-style parameter declaration. Following the old 1410 parser, they are not accepted on subsequent old-style 1411 parameter declarations either. However, there is no 1412 ambiguity after the first declaration, nor indeed on the 1413 first as long as we don't allow postfix attributes after a 1414 declarator with a nonempty identifier list in a definition; 1415 and postfix attributes have never been accepted here in 1416 function definitions either. */ 1417 while (c_parser_next_token_is_not (parser, CPP_EOF) 1418 && c_parser_next_token_is_not (parser, CPP_OPEN_BRACE)) 1419 c_parser_declaration_or_fndef (parser, false, false, true, false); 1420 DECL_SOURCE_LOCATION (current_function_decl) 1421 = c_parser_peek_token (parser)->location; 1422 store_parm_decls (); 1423 fnbody = c_parser_compound_statement (parser); 1424 if (nested) 1425 { 1426 tree decl = current_function_decl; 1427 add_stmt (fnbody); 1428 finish_function (); 1429 pop_function_context (); 1430 add_stmt (build_stmt (DECL_EXPR, decl)); 1431 } 1432 else 1433 { 1434 add_stmt (fnbody); 1435 finish_function (); 1436 } 1437 break; 1438 } 1439} 1440 1441/* Parse an asm-definition (asm() outside a function body). This is a 1442 GNU extension. 1443 1444 asm-definition: 1445 simple-asm-expr ; 1446*/ 1447 1448static void 1449c_parser_asm_definition (c_parser *parser) 1450{ 1451 tree asm_str = c_parser_simple_asm_expr (parser); 1452 if (asm_str) 1453 cgraph_add_asm_node (asm_str); 1454 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 1455} 1456 1457/* Parse some declaration specifiers (possibly none) (C90 6.5, C99 1458 6.7), adding them to SPECS (which may already include some). 1459 Storage class specifiers are accepted iff SCSPEC_OK; type 1460 specifiers are accepted iff TYPESPEC_OK; attributes are accepted at 1461 the start iff START_ATTR_OK. 1462 1463 declaration-specifiers: 1464 storage-class-specifier declaration-specifiers[opt] 1465 type-specifier declaration-specifiers[opt] 1466 type-qualifier declaration-specifiers[opt] 1467 function-specifier declaration-specifiers[opt] 1468 1469 Function specifiers (inline) are from C99, and are currently 1470 handled as storage class specifiers, as is __thread. 1471 1472 C90 6.5.1, C99 6.7.1: 1473 storage-class-specifier: 1474 typedef 1475 extern 1476 static 1477 auto 1478 register 1479 1480 C99 6.7.4: 1481 function-specifier: 1482 inline 1483 1484 C90 6.5.2, C99 6.7.2: 1485 type-specifier: 1486 void 1487 char 1488 short 1489 int 1490 long 1491 float 1492 double 1493 signed 1494 unsigned 1495 _Bool 1496 _Complex 1497 [_Imaginary removed in C99 TC2] 1498 struct-or-union-specifier 1499 enum-specifier 1500 typedef-name 1501 1502 (_Bool and _Complex are new in C99.) 1503 1504 C90 6.5.3, C99 6.7.3: 1505 1506 type-qualifier: 1507 const 1508 restrict 1509 volatile 1510 1511 (restrict is new in C99.) 1512 1513 GNU extensions: 1514 1515 declaration-specifiers: 1516 attributes declaration-specifiers[opt] 1517 1518 storage-class-specifier: 1519 __thread 1520 1521 type-specifier: 1522 typeof-specifier 1523 _Decimal32 1524 _Decimal64 1525 _Decimal128 1526 1527 Objective-C: 1528 1529 type-specifier: 1530 class-name objc-protocol-refs[opt] 1531 typedef-name objc-protocol-refs 1532 objc-protocol-refs 1533*/ 1534 1535static void 1536c_parser_declspecs (c_parser *parser, struct c_declspecs *specs, 1537 bool scspec_ok, bool typespec_ok, bool start_attr_ok) 1538{ 1539 bool attrs_ok = start_attr_ok; 1540 bool seen_type = specs->type_seen_p; 1541 while (c_parser_next_token_is (parser, CPP_NAME) 1542 || c_parser_next_token_is (parser, CPP_KEYWORD) 1543 || (c_dialect_objc () && c_parser_next_token_is (parser, CPP_LESS))) 1544 { 1545 struct c_typespec t; 1546 tree attrs; 1547 if (c_parser_next_token_is (parser, CPP_NAME)) 1548 { 1549 tree value = c_parser_peek_token (parser)->value; 1550 c_id_kind kind = c_parser_peek_token (parser)->id_kind; 1551 /* This finishes the specifiers unless a type name is OK, it 1552 is declared as a type name and a type name hasn't yet 1553 been seen. */ 1554 if (!typespec_ok || seen_type 1555 || (kind != C_ID_TYPENAME && kind != C_ID_CLASSNAME)) 1556 break; 1557 c_parser_consume_token (parser); 1558 seen_type = true; 1559 attrs_ok = true; 1560 if (kind == C_ID_TYPENAME 1561 && (!c_dialect_objc () 1562 || c_parser_next_token_is_not (parser, CPP_LESS))) 1563 { 1564 t.kind = ctsk_typedef; 1565 /* For a typedef name, record the meaning, not the name. 1566 In case of 'foo foo, bar;'. */ 1567 t.spec = lookup_name (value); 1568 } 1569 else 1570 { 1571 tree proto = NULL_TREE; 1572 gcc_assert (c_dialect_objc ()); 1573 t.kind = ctsk_objc; 1574 if (c_parser_next_token_is (parser, CPP_LESS)) 1575 proto = c_parser_objc_protocol_refs (parser); 1576 t.spec = objc_get_protocol_qualified_type (value, proto); 1577 } 1578 declspecs_add_type (specs, t); 1579 continue; 1580 } 1581 if (c_parser_next_token_is (parser, CPP_LESS)) 1582 { 1583 /* Make "<SomeProtocol>" equivalent to "id <SomeProtocol>" - 1584 nisse@lysator.liu.se. */ 1585 tree proto; 1586 gcc_assert (c_dialect_objc ()); 1587 if (!typespec_ok || seen_type) 1588 break; 1589 proto = c_parser_objc_protocol_refs (parser); 1590 t.kind = ctsk_objc; 1591 t.spec = objc_get_protocol_qualified_type (NULL_TREE, proto); 1592 declspecs_add_type (specs, t); 1593 continue; 1594 } 1595 gcc_assert (c_parser_next_token_is (parser, CPP_KEYWORD)); 1596 switch (c_parser_peek_token (parser)->keyword) 1597 { 1598 case RID_STATIC: 1599 case RID_EXTERN: 1600 case RID_REGISTER: 1601 case RID_TYPEDEF: 1602 case RID_INLINE: 1603 case RID_AUTO: 1604 case RID_THREAD: 1605 if (!scspec_ok) 1606 goto out; 1607 attrs_ok = true; 1608 /* TODO: Distinguish between function specifiers (inline) 1609 and storage class specifiers, either here or in 1610 declspecs_add_scspec. */ 1611 declspecs_add_scspec (specs, c_parser_peek_token (parser)->value); 1612 c_parser_consume_token (parser); 1613 break; 1614 case RID_UNSIGNED: 1615 case RID_LONG: 1616 case RID_SHORT: 1617 case RID_SIGNED: 1618 case RID_COMPLEX: 1619 case RID_INT: 1620 case RID_CHAR: 1621 case RID_FLOAT: 1622 case RID_DOUBLE: 1623 case RID_VOID: 1624 case RID_DFLOAT32: 1625 case RID_DFLOAT64: 1626 case RID_DFLOAT128: 1627 case RID_BOOL: 1628 if (!typespec_ok) 1629 goto out; 1630 attrs_ok = true; 1631 seen_type = true; 1632 OBJC_NEED_RAW_IDENTIFIER (1); 1633 t.kind = ctsk_resword; 1634 t.spec = c_parser_peek_token (parser)->value; 1635 declspecs_add_type (specs, t); 1636 c_parser_consume_token (parser); 1637 break; 1638 case RID_ENUM: 1639 if (!typespec_ok) 1640 goto out; 1641 attrs_ok = true; 1642 seen_type = true; 1643 t = c_parser_enum_specifier (parser); 1644 declspecs_add_type (specs, t); 1645 break; 1646 case RID_STRUCT: 1647 case RID_UNION: 1648 if (!typespec_ok) 1649 goto out; 1650 attrs_ok = true; 1651 seen_type = true; 1652 t = c_parser_struct_or_union_specifier (parser); 1653 declspecs_add_type (specs, t); 1654 break; 1655 case RID_TYPEOF: 1656 /* ??? The old parser rejected typeof after other type 1657 specifiers, but is a syntax error the best way of 1658 handling this? */ 1659 if (!typespec_ok || seen_type) 1660 goto out; 1661 attrs_ok = true; 1662 seen_type = true; 1663 t = c_parser_typeof_specifier (parser); 1664 declspecs_add_type (specs, t); 1665 break; 1666 case RID_CONST: 1667 case RID_VOLATILE: 1668 case RID_RESTRICT: 1669 attrs_ok = true; 1670 declspecs_add_qual (specs, c_parser_peek_token (parser)->value); 1671 c_parser_consume_token (parser); 1672 break; 1673 case RID_ATTRIBUTE: 1674 if (!attrs_ok) 1675 goto out; 1676 attrs = c_parser_attributes (parser); 1677 declspecs_add_attrs (specs, attrs); 1678 break; 1679 default: 1680 goto out; 1681 } 1682 } 1683 out: ; 1684} 1685 1686/* Parse an enum specifier (C90 6.5.2.2, C99 6.7.2.2). 1687 1688 enum-specifier: 1689 enum attributes[opt] identifier[opt] { enumerator-list } attributes[opt] 1690 enum attributes[opt] identifier[opt] { enumerator-list , } attributes[opt] 1691 enum attributes[opt] identifier 1692 1693 The form with trailing comma is new in C99. The forms with 1694 attributes are GNU extensions. In GNU C, we accept any expression 1695 without commas in the syntax (assignment expressions, not just 1696 conditional expressions); assignment expressions will be diagnosed 1697 as non-constant. 1698 1699 enumerator-list: 1700 enumerator 1701 enumerator-list , enumerator 1702 1703 enumerator: 1704 enumeration-constant 1705 enumeration-constant = constant-expression 1706*/ 1707 1708static struct c_typespec 1709c_parser_enum_specifier (c_parser *parser) 1710{ 1711 struct c_typespec ret; 1712 tree attrs; 1713 tree ident = NULL_TREE; 1714 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ENUM)); 1715 c_parser_consume_token (parser); 1716 attrs = c_parser_attributes (parser); 1717 if (c_parser_next_token_is (parser, CPP_NAME)) 1718 { 1719 ident = c_parser_peek_token (parser)->value; 1720 c_parser_consume_token (parser); 1721 } 1722 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 1723 { 1724 /* Parse an enum definition. */ 1725 tree type = start_enum (ident); 1726 tree postfix_attrs; 1727 /* We chain the enumerators in reverse order, then put them in 1728 forward order at the end. */ 1729 tree values = NULL_TREE; 1730 c_parser_consume_token (parser); 1731 while (true) 1732 { 1733 tree enum_id; 1734 tree enum_value; 1735 tree enum_decl; 1736 bool seen_comma; 1737 if (c_parser_next_token_is_not (parser, CPP_NAME)) 1738 { 1739 c_parser_error (parser, "expected identifier"); 1740 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); 1741 values = error_mark_node; 1742 break; 1743 } 1744 enum_id = c_parser_peek_token (parser)->value; 1745 c_parser_consume_token (parser); 1746 if (c_parser_next_token_is (parser, CPP_EQ)) 1747 { 1748 c_parser_consume_token (parser); 1749 enum_value = c_parser_expr_no_commas (parser, NULL).value; 1750 } 1751 else 1752 enum_value = NULL_TREE; 1753 enum_decl = build_enumerator (enum_id, enum_value); 1754 TREE_CHAIN (enum_decl) = values; 1755 values = enum_decl; 1756 seen_comma = false; 1757 if (c_parser_next_token_is (parser, CPP_COMMA)) 1758 { 1759 seen_comma = true; 1760 c_parser_consume_token (parser); 1761 } 1762 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 1763 { 1764 if (seen_comma && pedantic && !flag_isoc99) 1765 pedwarn ("comma at end of enumerator list"); 1766 c_parser_consume_token (parser); 1767 break; 1768 } 1769 if (!seen_comma) 1770 { 1771 c_parser_error (parser, "expected %<,%> or %<}%>"); 1772 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); 1773 values = error_mark_node; 1774 break; 1775 } 1776 } 1777 postfix_attrs = c_parser_attributes (parser); 1778 ret.spec = finish_enum (type, nreverse (values), 1779 chainon (attrs, postfix_attrs)); 1780 ret.kind = ctsk_tagdef; 1781 return ret; 1782 } 1783 else if (!ident) 1784 { 1785 c_parser_error (parser, "expected %<{%>"); 1786 ret.spec = error_mark_node; 1787 ret.kind = ctsk_tagref; 1788 return ret; 1789 } 1790 ret = parser_xref_tag (ENUMERAL_TYPE, ident); 1791 /* In ISO C, enumerated types can be referred to only if already 1792 defined. */ 1793 if (pedantic && !COMPLETE_TYPE_P (ret.spec)) 1794 pedwarn ("ISO C forbids forward references to %<enum%> types"); 1795 return ret; 1796} 1797 1798/* Parse a struct or union specifier (C90 6.5.2.1, C99 6.7.2.1). 1799 1800 struct-or-union-specifier: 1801 struct-or-union attributes[opt] identifier[opt] 1802 { struct-contents } attributes[opt] 1803 struct-or-union attributes[opt] identifier 1804 1805 struct-contents: 1806 struct-declaration-list 1807 1808 struct-declaration-list: 1809 struct-declaration ; 1810 struct-declaration-list struct-declaration ; 1811 1812 GNU extensions: 1813 1814 struct-contents: 1815 empty 1816 struct-declaration 1817 struct-declaration-list struct-declaration 1818 1819 struct-declaration-list: 1820 struct-declaration-list ; 1821 ; 1822 1823 (Note that in the syntax here, unlike that in ISO C, the semicolons 1824 are included here rather than in struct-declaration, in order to 1825 describe the syntax with extra semicolons and missing semicolon at 1826 end.) 1827 1828 Objective-C: 1829 1830 struct-declaration-list: 1831 @defs ( class-name ) 1832 1833 (Note this does not include a trailing semicolon, but can be 1834 followed by further declarations, and gets a pedwarn-if-pedantic 1835 when followed by a semicolon.) */ 1836 1837static struct c_typespec 1838c_parser_struct_or_union_specifier (c_parser *parser) 1839{ 1840 struct c_typespec ret; 1841 tree attrs; 1842 tree ident = NULL_TREE; 1843 enum tree_code code; 1844 switch (c_parser_peek_token (parser)->keyword) 1845 { 1846 case RID_STRUCT: 1847 code = RECORD_TYPE; 1848 break; 1849 case RID_UNION: 1850 code = UNION_TYPE; 1851 break; 1852 default: 1853 gcc_unreachable (); 1854 } 1855 c_parser_consume_token (parser); 1856 attrs = c_parser_attributes (parser); 1857 if (c_parser_next_token_is (parser, CPP_NAME)) 1858 { 1859 ident = c_parser_peek_token (parser)->value; 1860 c_parser_consume_token (parser); 1861 } 1862 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 1863 { 1864 /* Parse a struct or union definition. Start the scope of the 1865 tag before parsing components. */ 1866 tree type = start_struct (code, ident); 1867 tree postfix_attrs; 1868 /* We chain the components in reverse order, then put them in 1869 forward order at the end. Each struct-declaration may 1870 declare multiple components (comma-separated), so we must use 1871 chainon to join them, although when parsing each 1872 struct-declaration we can use TREE_CHAIN directly. 1873 1874 The theory behind all this is that there will be more 1875 semicolon separated fields than comma separated fields, and 1876 so we'll be minimizing the number of node traversals required 1877 by chainon. */ 1878 tree contents = NULL_TREE; 1879 c_parser_consume_token (parser); 1880 /* Handle the Objective-C @defs construct, 1881 e.g. foo(sizeof(struct{ @defs(ClassName) }));. */ 1882 if (c_parser_next_token_is_keyword (parser, RID_AT_DEFS)) 1883 { 1884 tree name; 1885 gcc_assert (c_dialect_objc ()); 1886 c_parser_consume_token (parser); 1887 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 1888 goto end_at_defs; 1889 if (c_parser_next_token_is (parser, CPP_NAME) 1890 && c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME) 1891 { 1892 name = c_parser_peek_token (parser)->value; 1893 c_parser_consume_token (parser); 1894 } 1895 else 1896 { 1897 c_parser_error (parser, "expected class name"); 1898 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 1899 goto end_at_defs; 1900 } 1901 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 1902 "expected %<)%>"); 1903 contents = nreverse (objc_get_class_ivars (name)); 1904 } 1905 end_at_defs: 1906 /* Parse the struct-declarations and semicolons. Problems with 1907 semicolons are diagnosed here; empty structures are diagnosed 1908 elsewhere. */ 1909 while (true) 1910 { 1911 tree decls; 1912 /* Parse any stray semicolon. */ 1913 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 1914 { 1915 if (pedantic) 1916 pedwarn ("extra semicolon in struct or union specified"); 1917 c_parser_consume_token (parser); 1918 continue; 1919 } 1920 /* Stop if at the end of the struct or union contents. */ 1921 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 1922 { 1923 c_parser_consume_token (parser); 1924 break; 1925 } 1926 /* Accept #pragmas at struct scope. */ 1927 if (c_parser_next_token_is (parser, CPP_PRAGMA)) 1928 { 1929 c_parser_pragma (parser, pragma_external); 1930 continue; 1931 } 1932 /* Parse some comma-separated declarations, but not the 1933 trailing semicolon if any. */ 1934 decls = c_parser_struct_declaration (parser); 1935 contents = chainon (decls, contents); 1936 /* If no semicolon follows, either we have a parse error or 1937 are at the end of the struct or union and should 1938 pedwarn. */ 1939 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 1940 c_parser_consume_token (parser); 1941 else 1942 { 1943 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 1944 pedwarn ("no semicolon at end of struct or union"); 1945 else 1946 { 1947 c_parser_error (parser, "expected %<;%>"); 1948 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); 1949 break; 1950 } 1951 } 1952 } 1953 postfix_attrs = c_parser_attributes (parser); 1954 ret.spec = finish_struct (type, nreverse (contents), 1955 chainon (attrs, postfix_attrs)); 1956 ret.kind = ctsk_tagdef; 1957 return ret; 1958 } 1959 else if (!ident) 1960 { 1961 c_parser_error (parser, "expected %<{%>"); 1962 ret.spec = error_mark_node; 1963 ret.kind = ctsk_tagref; 1964 return ret; 1965 } 1966 ret = parser_xref_tag (code, ident); 1967 return ret; 1968} 1969 1970/* Parse a struct-declaration (C90 6.5.2.1, C99 6.7.2.1), *without* 1971 the trailing semicolon. 1972 1973 struct-declaration: 1974 specifier-qualifier-list struct-declarator-list 1975 1976 specifier-qualifier-list: 1977 type-specifier specifier-qualifier-list[opt] 1978 type-qualifier specifier-qualifier-list[opt] 1979 attributes specifier-qualifier-list[opt] 1980 1981 struct-declarator-list: 1982 struct-declarator 1983 struct-declarator-list , attributes[opt] struct-declarator 1984 1985 struct-declarator: 1986 declarator attributes[opt] 1987 declarator[opt] : constant-expression attributes[opt] 1988 1989 GNU extensions: 1990 1991 struct-declaration: 1992 __extension__ struct-declaration 1993 specifier-qualifier-list 1994 1995 Unlike the ISO C syntax, semicolons are handled elsewhere. The use 1996 of attributes where shown is a GNU extension. In GNU C, we accept 1997 any expression without commas in the syntax (assignment 1998 expressions, not just conditional expressions); assignment 1999 expressions will be diagnosed as non-constant. */ 2000 2001static tree 2002c_parser_struct_declaration (c_parser *parser) 2003{ 2004 struct c_declspecs *specs; 2005 tree prefix_attrs; 2006 tree all_prefix_attrs; 2007 tree decls; 2008 if (c_parser_next_token_is_keyword (parser, RID_EXTENSION)) 2009 { 2010 int ext; 2011 tree decl; 2012 ext = disable_extension_diagnostics (); 2013 c_parser_consume_token (parser); 2014 decl = c_parser_struct_declaration (parser); 2015 restore_extension_diagnostics (ext); 2016 return decl; 2017 } 2018 specs = build_null_declspecs (); 2019 c_parser_declspecs (parser, specs, false, true, true); 2020 if (parser->error) 2021 return NULL_TREE; 2022 if (!specs->declspecs_seen_p) 2023 { 2024 c_parser_error (parser, "expected specifier-qualifier-list"); 2025 return NULL_TREE; 2026 } 2027 finish_declspecs (specs); 2028 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 2029 { 2030 tree ret; 2031 if (!specs->type_seen_p) 2032 { 2033 if (pedantic) 2034 pedwarn ("ISO C forbids member declarations with no members"); 2035 shadow_tag_warned (specs, pedantic); 2036 ret = NULL_TREE; 2037 } 2038 else 2039 { 2040 /* Support for unnamed structs or unions as members of 2041 structs or unions (which is [a] useful and [b] supports 2042 MS P-SDK). */ 2043 ret = grokfield (build_id_declarator (NULL_TREE), specs, NULL_TREE); 2044 } 2045 return ret; 2046 } 2047 pending_xref_error (); 2048 prefix_attrs = specs->attrs; 2049 all_prefix_attrs = prefix_attrs; 2050 specs->attrs = NULL_TREE; 2051 decls = NULL_TREE; 2052 while (true) 2053 { 2054 /* Declaring one or more declarators or un-named bit-fields. */ 2055 struct c_declarator *declarator; 2056 bool dummy = false; 2057 if (c_parser_next_token_is (parser, CPP_COLON)) 2058 declarator = build_id_declarator (NULL_TREE); 2059 else 2060 declarator = c_parser_declarator (parser, specs->type_seen_p, 2061 C_DTR_NORMAL, &dummy); 2062 if (declarator == NULL) 2063 { 2064 c_parser_skip_to_end_of_block_or_statement (parser); 2065 break; 2066 } 2067 if (c_parser_next_token_is (parser, CPP_COLON) 2068 || c_parser_next_token_is (parser, CPP_COMMA) 2069 || c_parser_next_token_is (parser, CPP_SEMICOLON) 2070 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE) 2071 || c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) 2072 { 2073 tree postfix_attrs = NULL_TREE; 2074 tree width = NULL_TREE; 2075 tree d; 2076 if (c_parser_next_token_is (parser, CPP_COLON)) 2077 { 2078 c_parser_consume_token (parser); 2079 width = c_parser_expr_no_commas (parser, NULL).value; 2080 } 2081 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) 2082 postfix_attrs = c_parser_attributes (parser); 2083 d = grokfield (declarator, specs, width); 2084 decl_attributes (&d, chainon (postfix_attrs, 2085 all_prefix_attrs), 0); 2086 TREE_CHAIN (d) = decls; 2087 decls = d; 2088 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) 2089 all_prefix_attrs = chainon (c_parser_attributes (parser), 2090 prefix_attrs); 2091 else 2092 all_prefix_attrs = prefix_attrs; 2093 if (c_parser_next_token_is (parser, CPP_COMMA)) 2094 c_parser_consume_token (parser); 2095 else if (c_parser_next_token_is (parser, CPP_SEMICOLON) 2096 || c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 2097 { 2098 /* Semicolon consumed in caller. */ 2099 break; 2100 } 2101 else 2102 { 2103 c_parser_error (parser, "expected %<,%>, %<;%> or %<}%>"); 2104 break; 2105 } 2106 } 2107 else 2108 { 2109 c_parser_error (parser, 2110 "expected %<:%>, %<,%>, %<;%>, %<}%> or " 2111 "%<__attribute__%>"); 2112 break; 2113 } 2114 } 2115 return decls; 2116} 2117 2118/* Parse a typeof specifier (a GNU extension). 2119 2120 typeof-specifier: 2121 typeof ( expression ) 2122 typeof ( type-name ) 2123*/ 2124 2125static struct c_typespec 2126c_parser_typeof_specifier (c_parser *parser) 2127{ 2128 struct c_typespec ret; 2129 ret.kind = ctsk_typeof; 2130 ret.spec = error_mark_node; 2131 gcc_assert (c_parser_next_token_is_keyword (parser, RID_TYPEOF)); 2132 c_parser_consume_token (parser); 2133 skip_evaluation++; 2134 in_typeof++; 2135 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 2136 { 2137 skip_evaluation--; 2138 in_typeof--; 2139 return ret; 2140 } 2141 if (c_parser_next_token_starts_typename (parser)) 2142 { 2143 struct c_type_name *type = c_parser_type_name (parser); 2144 skip_evaluation--; 2145 in_typeof--; 2146 if (type != NULL) 2147 { 2148 ret.spec = groktypename (type); 2149 pop_maybe_used (variably_modified_type_p (ret.spec, NULL_TREE)); 2150 } 2151 } 2152 else 2153 { 2154 bool was_vm; 2155 struct c_expr expr = c_parser_expression (parser); 2156 skip_evaluation--; 2157 in_typeof--; 2158 if (TREE_CODE (expr.value) == COMPONENT_REF 2159 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) 2160 error ("%<typeof%> applied to a bit-field"); 2161 ret.spec = TREE_TYPE (expr.value); 2162 was_vm = variably_modified_type_p (ret.spec, NULL_TREE); 2163 /* This should be returned with the type so that when the type 2164 is evaluated, this can be evaluated. For now, we avoid 2165 evaluation when the context might. */ 2166 if (!skip_evaluation && was_vm) 2167 { 2168 tree e = expr.value; 2169 2170 /* If the expression is not of a type to which we cannot assign a line 2171 number, wrap the thing in a no-op NOP_EXPR. */ 2172 if (DECL_P (e) || CONSTANT_CLASS_P (e)) 2173 e = build1 (NOP_EXPR, void_type_node, e); 2174 2175 if (EXPR_P (e)) 2176 SET_EXPR_LOCATION (e, input_location); 2177 2178 add_stmt (e); 2179 } 2180 pop_maybe_used (was_vm); 2181 } 2182 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 2183 return ret; 2184} 2185 2186/* Parse a declarator, possibly an abstract declarator (C90 6.5.4, 2187 6.5.5, C99 6.7.5, 6.7.6). If TYPE_SEEN_P then a typedef name may 2188 be redeclared; otherwise it may not. KIND indicates which kind of 2189 declarator is wanted. Returns a valid declarator except in the 2190 case of a syntax error in which case NULL is returned. *SEEN_ID is 2191 set to true if an identifier being declared is seen; this is used 2192 to diagnose bad forms of abstract array declarators and to 2193 determine whether an identifier list is syntactically permitted. 2194 2195 declarator: 2196 pointer[opt] direct-declarator 2197 2198 direct-declarator: 2199 identifier 2200 ( attributes[opt] declarator ) 2201 direct-declarator array-declarator 2202 direct-declarator ( parameter-type-list ) 2203 direct-declarator ( identifier-list[opt] ) 2204 2205 pointer: 2206 * type-qualifier-list[opt] 2207 * type-qualifier-list[opt] pointer 2208 2209 type-qualifier-list: 2210 type-qualifier 2211 attributes 2212 type-qualifier-list type-qualifier 2213 type-qualifier-list attributes 2214 2215 parameter-type-list: 2216 parameter-list 2217 parameter-list , ... 2218 2219 parameter-list: 2220 parameter-declaration 2221 parameter-list , parameter-declaration 2222 2223 parameter-declaration: 2224 declaration-specifiers declarator attributes[opt] 2225 declaration-specifiers abstract-declarator[opt] attributes[opt] 2226 2227 identifier-list: 2228 identifier 2229 identifier-list , identifier 2230 2231 abstract-declarator: 2232 pointer 2233 pointer[opt] direct-abstract-declarator 2234 2235 direct-abstract-declarator: 2236 ( attributes[opt] abstract-declarator ) 2237 direct-abstract-declarator[opt] array-declarator 2238 direct-abstract-declarator[opt] ( parameter-type-list[opt] ) 2239 2240 GNU extensions: 2241 2242 direct-declarator: 2243 direct-declarator ( parameter-forward-declarations 2244 parameter-type-list[opt] ) 2245 2246 direct-abstract-declarator: 2247 direct-abstract-declarator[opt] ( parameter-forward-declarations 2248 parameter-type-list[opt] ) 2249 2250 parameter-forward-declarations: 2251 parameter-list ; 2252 parameter-forward-declarations parameter-list ; 2253 2254 The uses of attributes shown above are GNU extensions. 2255 2256 Some forms of array declarator are not included in C99 in the 2257 syntax for abstract declarators; these are disallowed elsewhere. 2258 This may be a defect (DR#289). 2259 2260 This function also accepts an omitted abstract declarator as being 2261 an abstract declarator, although not part of the formal syntax. */ 2262 2263static struct c_declarator * 2264c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind, 2265 bool *seen_id) 2266{ 2267 /* Parse any initial pointer part. */ 2268 if (c_parser_next_token_is (parser, CPP_MULT)) 2269 { 2270 struct c_declspecs *quals_attrs = build_null_declspecs (); 2271 struct c_declarator *inner; 2272 c_parser_consume_token (parser); 2273 c_parser_declspecs (parser, quals_attrs, false, false, true); 2274 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id); 2275 if (inner == NULL) 2276 return NULL; 2277 else 2278 return make_pointer_declarator (quals_attrs, inner); 2279 } 2280 /* Now we have a direct declarator, direct abstract declarator or 2281 nothing (which counts as a direct abstract declarator here). */ 2282 return c_parser_direct_declarator (parser, type_seen_p, kind, seen_id); 2283} 2284 2285/* Parse a direct declarator or direct abstract declarator; arguments 2286 as c_parser_declarator. */ 2287 2288static struct c_declarator * 2289c_parser_direct_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind, 2290 bool *seen_id) 2291{ 2292 /* The direct declarator must start with an identifier (possibly 2293 omitted) or a parenthesized declarator (possibly abstract). In 2294 an ordinary declarator, initial parentheses must start a 2295 parenthesized declarator. In an abstract declarator or parameter 2296 declarator, they could start a parenthesized declarator or a 2297 parameter list. To tell which, the open parenthesis and any 2298 following attributes must be read. If a declaration specifier 2299 follows, then it is a parameter list; if the specifier is a 2300 typedef name, there might be an ambiguity about redeclaring it, 2301 which is resolved in the direction of treating it as a typedef 2302 name. If a close parenthesis follows, it is also an empty 2303 parameter list, as the syntax does not permit empty abstract 2304 declarators. Otherwise, it is a parenthesized declarator (in 2305 which case the analysis may be repeated inside it, recursively). 2306 2307 ??? There is an ambiguity in a parameter declaration "int 2308 (__attribute__((foo)) x)", where x is not a typedef name: it 2309 could be an abstract declarator for a function, or declare x with 2310 parentheses. The proper resolution of this ambiguity needs 2311 documenting. At present we follow an accident of the old 2312 parser's implementation, whereby the first parameter must have 2313 some declaration specifiers other than just attributes. Thus as 2314 a parameter declaration it is treated as a parenthesized 2315 parameter named x, and as an abstract declarator it is 2316 rejected. 2317 2318 ??? Also following the old parser, attributes inside an empty 2319 parameter list are ignored, making it a list not yielding a 2320 prototype, rather than giving an error or making it have one 2321 parameter with implicit type int. 2322 2323 ??? Also following the old parser, typedef names may be 2324 redeclared in declarators, but not Objective-C class names. */ 2325 2326 if (kind != C_DTR_ABSTRACT 2327 && c_parser_next_token_is (parser, CPP_NAME) 2328 && ((type_seen_p 2329 && c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME) 2330 || c_parser_peek_token (parser)->id_kind == C_ID_ID)) 2331 { 2332 struct c_declarator *inner 2333 = build_id_declarator (c_parser_peek_token (parser)->value); 2334 *seen_id = true; 2335 inner->id_loc = c_parser_peek_token (parser)->location; 2336 c_parser_consume_token (parser); 2337 return c_parser_direct_declarator_inner (parser, *seen_id, inner); 2338 } 2339 2340 if (kind != C_DTR_NORMAL 2341 && c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) 2342 { 2343 struct c_declarator *inner = build_id_declarator (NULL_TREE); 2344 return c_parser_direct_declarator_inner (parser, *seen_id, inner); 2345 } 2346 2347 /* Either we are at the end of an abstract declarator, or we have 2348 parentheses. */ 2349 2350 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 2351 { 2352 tree attrs; 2353 struct c_declarator *inner; 2354 c_parser_consume_token (parser); 2355 attrs = c_parser_attributes (parser); 2356 if (kind != C_DTR_NORMAL 2357 && (c_parser_next_token_starts_declspecs (parser) 2358 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN))) 2359 { 2360 struct c_arg_info *args 2361 = c_parser_parms_declarator (parser, kind == C_DTR_NORMAL, 2362 attrs); 2363 if (args == NULL) 2364 return NULL; 2365 else 2366 { 2367 inner 2368 = build_function_declarator (args, 2369 build_id_declarator (NULL_TREE)); 2370 return c_parser_direct_declarator_inner (parser, *seen_id, 2371 inner); 2372 } 2373 } 2374 /* A parenthesized declarator. */ 2375 inner = c_parser_declarator (parser, type_seen_p, kind, seen_id); 2376 if (inner != NULL && attrs != NULL) 2377 inner = build_attrs_declarator (attrs, inner); 2378 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2379 { 2380 c_parser_consume_token (parser); 2381 if (inner == NULL) 2382 return NULL; 2383 else 2384 return c_parser_direct_declarator_inner (parser, *seen_id, inner); 2385 } 2386 else 2387 { 2388 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 2389 "expected %<)%>"); 2390 return NULL; 2391 } 2392 } 2393 else 2394 { 2395 if (kind == C_DTR_NORMAL) 2396 { 2397 c_parser_error (parser, "expected identifier or %<(%>"); 2398 return NULL; 2399 } 2400 else 2401 return build_id_declarator (NULL_TREE); 2402 } 2403} 2404 2405/* Parse part of a direct declarator or direct abstract declarator, 2406 given that some (in INNER) has already been parsed; ID_PRESENT is 2407 true if an identifier is present, false for an abstract 2408 declarator. */ 2409 2410static struct c_declarator * 2411c_parser_direct_declarator_inner (c_parser *parser, bool id_present, 2412 struct c_declarator *inner) 2413{ 2414 /* Parse a sequence of array declarators and parameter lists. */ 2415 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) 2416 { 2417 struct c_declarator *declarator; 2418 struct c_declspecs *quals_attrs = build_null_declspecs (); 2419 bool static_seen; 2420 bool star_seen; 2421 tree dimen; 2422 c_parser_consume_token (parser); 2423 c_parser_declspecs (parser, quals_attrs, false, false, true); 2424 static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC); 2425 if (static_seen) 2426 c_parser_consume_token (parser); 2427 if (static_seen && !quals_attrs->declspecs_seen_p) 2428 c_parser_declspecs (parser, quals_attrs, false, false, true); 2429 if (!quals_attrs->declspecs_seen_p) 2430 quals_attrs = NULL; 2431 /* If "static" is present, there must be an array dimension. 2432 Otherwise, there may be a dimension, "*", or no 2433 dimension. */ 2434 if (static_seen) 2435 { 2436 star_seen = false; 2437 dimen = c_parser_expr_no_commas (parser, NULL).value; 2438 } 2439 else 2440 { 2441 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) 2442 { 2443 dimen = NULL_TREE; 2444 star_seen = false; 2445 } 2446 else if (c_parser_next_token_is (parser, CPP_MULT)) 2447 { 2448 if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE) 2449 { 2450 dimen = NULL_TREE; 2451 star_seen = true; 2452 c_parser_consume_token (parser); 2453 } 2454 else 2455 { 2456 star_seen = false; 2457 dimen = c_parser_expr_no_commas (parser, NULL).value; 2458 } 2459 } 2460 else 2461 { 2462 star_seen = false; 2463 dimen = c_parser_expr_no_commas (parser, NULL).value; 2464 } 2465 } 2466 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) 2467 c_parser_consume_token (parser); 2468 else 2469 { 2470 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 2471 "expected %<]%>"); 2472 return NULL; 2473 } 2474 declarator = build_array_declarator (dimen, quals_attrs, static_seen, 2475 star_seen); 2476 if (declarator == NULL) 2477 return NULL; 2478 inner = set_array_declarator_inner (declarator, inner, !id_present); 2479 return c_parser_direct_declarator_inner (parser, id_present, inner); 2480 } 2481 else if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 2482 { 2483 tree attrs; 2484 struct c_arg_info *args; 2485 c_parser_consume_token (parser); 2486 attrs = c_parser_attributes (parser); 2487 args = c_parser_parms_declarator (parser, id_present, attrs); 2488 if (args == NULL) 2489 return NULL; 2490 else 2491 { 2492 inner = build_function_declarator (args, inner); 2493 return c_parser_direct_declarator_inner (parser, id_present, inner); 2494 } 2495 } 2496 return inner; 2497} 2498 2499/* Parse a parameter list or identifier list, including the closing 2500 parenthesis but not the opening one. ATTRS are the attributes at 2501 the start of the list. ID_LIST_OK is true if an identifier list is 2502 acceptable; such a list must not have attributes at the start. */ 2503 2504static struct c_arg_info * 2505c_parser_parms_declarator (c_parser *parser, bool id_list_ok, tree attrs) 2506{ 2507 push_scope (); 2508 declare_parm_level (); 2509 /* If the list starts with an identifier, it is an identifier list. 2510 Otherwise, it is either a prototype list or an empty list. */ 2511 if (id_list_ok 2512 && !attrs 2513 && c_parser_next_token_is (parser, CPP_NAME) 2514 && c_parser_peek_token (parser)->id_kind == C_ID_ID) 2515 { 2516 tree list = NULL_TREE, *nextp = &list; 2517 while (c_parser_next_token_is (parser, CPP_NAME) 2518 && c_parser_peek_token (parser)->id_kind == C_ID_ID) 2519 { 2520 *nextp = build_tree_list (NULL_TREE, 2521 c_parser_peek_token (parser)->value); 2522 nextp = & TREE_CHAIN (*nextp); 2523 c_parser_consume_token (parser); 2524 if (c_parser_next_token_is_not (parser, CPP_COMMA)) 2525 break; 2526 c_parser_consume_token (parser); 2527 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2528 { 2529 c_parser_error (parser, "expected identifier"); 2530 break; 2531 } 2532 } 2533 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2534 { 2535 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); 2536 ret->parms = 0; 2537 ret->tags = 0; 2538 ret->types = list; 2539 ret->others = 0; 2540 ret->pending_sizes = 0; 2541 ret->had_vla_unspec = 0; 2542 c_parser_consume_token (parser); 2543 pop_scope (); 2544 return ret; 2545 } 2546 else 2547 { 2548 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 2549 "expected %<)%>"); 2550 pop_scope (); 2551 return NULL; 2552 } 2553 } 2554 else 2555 { 2556 struct c_arg_info *ret = c_parser_parms_list_declarator (parser, attrs); 2557 pop_scope (); 2558 return ret; 2559 } 2560} 2561 2562/* Parse a parameter list (possibly empty), including the closing 2563 parenthesis but not the opening one. ATTRS are the attributes at 2564 the start of the list. */ 2565 2566static struct c_arg_info * 2567c_parser_parms_list_declarator (c_parser *parser, tree attrs) 2568{ 2569 bool good_parm = false; 2570 /* ??? Following the old parser, forward parameter declarations may 2571 use abstract declarators, and if no real parameter declarations 2572 follow the forward declarations then this is not diagnosed. Also 2573 note as above that attributes are ignored as the only contents of 2574 the parentheses, or as the only contents after forward 2575 declarations. */ 2576 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2577 { 2578 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); 2579 ret->parms = 0; 2580 ret->tags = 0; 2581 ret->types = 0; 2582 ret->others = 0; 2583 ret->pending_sizes = 0; 2584 ret->had_vla_unspec = 0; 2585 c_parser_consume_token (parser); 2586 return ret; 2587 } 2588 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) 2589 { 2590 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info); 2591 ret->parms = 0; 2592 ret->tags = 0; 2593 ret->others = 0; 2594 ret->pending_sizes = 0; 2595 ret->had_vla_unspec = 0; 2596 /* Suppress -Wold-style-definition for this case. */ 2597 ret->types = error_mark_node; 2598 error ("ISO C requires a named argument before %<...%>"); 2599 c_parser_consume_token (parser); 2600 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2601 { 2602 c_parser_consume_token (parser); 2603 return ret; 2604 } 2605 else 2606 { 2607 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 2608 "expected %<)%>"); 2609 return NULL; 2610 } 2611 } 2612 /* Nonempty list of parameters, either terminated with semicolon 2613 (forward declarations; recurse) or with close parenthesis (normal 2614 function) or with ", ... )" (variadic function). */ 2615 while (true) 2616 { 2617 /* Parse a parameter. */ 2618 struct c_parm *parm = c_parser_parameter_declaration (parser, attrs); 2619 attrs = NULL_TREE; 2620 if (parm != NULL) 2621 { 2622 good_parm = true; 2623 push_parm_decl (parm); 2624 } 2625 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 2626 { 2627 tree new_attrs; 2628 c_parser_consume_token (parser); 2629 mark_forward_parm_decls (); 2630 new_attrs = c_parser_attributes (parser); 2631 return c_parser_parms_list_declarator (parser, new_attrs); 2632 } 2633 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2634 { 2635 c_parser_consume_token (parser); 2636 if (good_parm) 2637 return get_parm_info (false); 2638 else 2639 { 2640 struct c_arg_info *ret 2641 = XOBNEW (&parser_obstack, struct c_arg_info); 2642 ret->parms = 0; 2643 ret->tags = 0; 2644 ret->types = 0; 2645 ret->others = 0; 2646 ret->pending_sizes = 0; 2647 ret->had_vla_unspec = 0; 2648 return ret; 2649 } 2650 } 2651 if (!c_parser_require (parser, CPP_COMMA, 2652 "expected %<;%>, %<,%> or %<)%>")) 2653 { 2654 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 2655 return NULL; 2656 } 2657 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) 2658 { 2659 c_parser_consume_token (parser); 2660 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2661 { 2662 c_parser_consume_token (parser); 2663 if (good_parm) 2664 return get_parm_info (true); 2665 else 2666 { 2667 struct c_arg_info *ret 2668 = XOBNEW (&parser_obstack, struct c_arg_info); 2669 ret->parms = 0; 2670 ret->tags = 0; 2671 ret->types = 0; 2672 ret->others = 0; 2673 ret->pending_sizes = 0; 2674 ret->had_vla_unspec = 0; 2675 return ret; 2676 } 2677 } 2678 else 2679 { 2680 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 2681 "expected %<)%>"); 2682 return NULL; 2683 } 2684 } 2685 } 2686} 2687 2688/* Parse a parameter declaration. ATTRS are the attributes at the 2689 start of the declaration if it is the first parameter. */ 2690 2691static struct c_parm * 2692c_parser_parameter_declaration (c_parser *parser, tree attrs) 2693{ 2694 struct c_declspecs *specs; 2695 struct c_declarator *declarator; 2696 tree prefix_attrs; 2697 tree postfix_attrs = NULL_TREE; 2698 bool dummy = false; 2699 if (!c_parser_next_token_starts_declspecs (parser)) 2700 { 2701 /* ??? In some Objective-C cases '...' isn't applicable so there 2702 should be a different message. */ 2703 c_parser_error (parser, 2704 "expected declaration specifiers or %<...%>"); 2705 c_parser_skip_to_end_of_parameter (parser); 2706 return NULL; 2707 } 2708 specs = build_null_declspecs (); 2709 if (attrs) 2710 { 2711 declspecs_add_attrs (specs, attrs); 2712 attrs = NULL_TREE; 2713 } 2714 c_parser_declspecs (parser, specs, true, true, true); 2715 finish_declspecs (specs); 2716 pending_xref_error (); 2717 prefix_attrs = specs->attrs; 2718 specs->attrs = NULL_TREE; 2719 declarator = c_parser_declarator (parser, specs->type_seen_p, 2720 C_DTR_PARM, &dummy); 2721 if (declarator == NULL) 2722 { 2723 c_parser_skip_until_found (parser, CPP_COMMA, NULL); 2724 return NULL; 2725 } 2726 if (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) 2727 postfix_attrs = c_parser_attributes (parser); 2728 return build_c_parm (specs, chainon (postfix_attrs, prefix_attrs), 2729 declarator); 2730} 2731 2732/* Parse a string literal in an asm expression. It should not be 2733 translated, and wide string literals are an error although 2734 permitted by the syntax. This is a GNU extension. 2735 2736 asm-string-literal: 2737 string-literal 2738 2739 ??? At present, following the old parser, the caller needs to have 2740 set c_lex_string_translate to 0. It would be better to follow the 2741 C++ parser rather than using the c_lex_string_translate kludge. */ 2742 2743static tree 2744c_parser_asm_string_literal (c_parser *parser) 2745{ 2746 tree str; 2747 if (c_parser_next_token_is (parser, CPP_STRING)) 2748 { 2749 str = c_parser_peek_token (parser)->value; 2750 c_parser_consume_token (parser); 2751 } 2752 else if (c_parser_next_token_is (parser, CPP_WSTRING)) 2753 { 2754 error ("wide string literal in %<asm%>"); 2755 str = build_string (1, ""); 2756 c_parser_consume_token (parser); 2757 } 2758 else 2759 { 2760 c_parser_error (parser, "expected string literal"); 2761 str = NULL_TREE; 2762 } 2763 return str; 2764} 2765 2766/* Parse a simple asm expression. This is used in restricted 2767 contexts, where a full expression with inputs and outputs does not 2768 make sense. This is a GNU extension. 2769 2770 simple-asm-expr: 2771 asm ( asm-string-literal ) 2772*/ 2773 2774static tree 2775c_parser_simple_asm_expr (c_parser *parser) 2776{ 2777 tree str; 2778 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM)); 2779 /* ??? Follow the C++ parser rather than using the 2780 c_lex_string_translate kludge. */ 2781 c_lex_string_translate = 0; 2782 c_parser_consume_token (parser); 2783 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 2784 { 2785 c_lex_string_translate = 1; 2786 return NULL_TREE; 2787 } 2788 str = c_parser_asm_string_literal (parser); 2789 c_lex_string_translate = 1; 2790 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) 2791 { 2792 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 2793 return NULL_TREE; 2794 } 2795 return str; 2796} 2797 2798/* Parse (possibly empty) attributes. This is a GNU extension. 2799 2800 attributes: 2801 empty 2802 attributes attribute 2803 2804 attribute: 2805 __attribute__ ( ( attribute-list ) ) 2806 2807 attribute-list: 2808 attrib 2809 attribute_list , attrib 2810 2811 attrib: 2812 empty 2813 any-word 2814 any-word ( identifier ) 2815 any-word ( identifier , nonempty-expr-list ) 2816 any-word ( expr-list ) 2817 2818 where the "identifier" must not be declared as a type, and 2819 "any-word" may be any identifier (including one declared as a 2820 type), a reserved word storage class specifier, type specifier or 2821 type qualifier. ??? This still leaves out most reserved keywords 2822 (following the old parser), shouldn't we include them, and why not 2823 allow identifiers declared as types to start the arguments? */ 2824 2825static tree 2826c_parser_attributes (c_parser *parser) 2827{ 2828 tree attrs = NULL_TREE; 2829 while (c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE)) 2830 { 2831 /* ??? Follow the C++ parser rather than using the 2832 c_lex_string_translate kludge. */ 2833 c_lex_string_translate = 0; 2834 c_parser_consume_token (parser); 2835 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 2836 { 2837 c_lex_string_translate = 1; 2838 return attrs; 2839 } 2840 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 2841 { 2842 c_lex_string_translate = 1; 2843 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 2844 return attrs; 2845 } 2846 /* Parse the attribute list. */ 2847 while (c_parser_next_token_is (parser, CPP_COMMA) 2848 || c_parser_next_token_is (parser, CPP_NAME) 2849 || c_parser_next_token_is (parser, CPP_KEYWORD)) 2850 { 2851 tree attr, attr_name, attr_args; 2852 if (c_parser_next_token_is (parser, CPP_COMMA)) 2853 { 2854 c_parser_consume_token (parser); 2855 continue; 2856 } 2857 if (c_parser_next_token_is (parser, CPP_KEYWORD)) 2858 { 2859 /* ??? See comment above about what keywords are 2860 accepted here. */ 2861 bool ok; 2862 switch (c_parser_peek_token (parser)->keyword) 2863 { 2864 case RID_STATIC: 2865 case RID_UNSIGNED: 2866 case RID_LONG: 2867 case RID_CONST: 2868 case RID_EXTERN: 2869 case RID_REGISTER: 2870 case RID_TYPEDEF: 2871 case RID_SHORT: 2872 case RID_INLINE: 2873 case RID_VOLATILE: 2874 case RID_SIGNED: 2875 case RID_AUTO: 2876 case RID_RESTRICT: 2877 case RID_COMPLEX: 2878 case RID_THREAD: 2879 case RID_INT: 2880 case RID_CHAR: 2881 case RID_FLOAT: 2882 case RID_DOUBLE: 2883 case RID_VOID: 2884 case RID_DFLOAT32: 2885 case RID_DFLOAT64: 2886 case RID_DFLOAT128: 2887 case RID_BOOL: 2888 ok = true; 2889 break; 2890 default: 2891 ok = false; 2892 break; 2893 } 2894 if (!ok) 2895 break; 2896 } 2897 attr_name = c_parser_peek_token (parser)->value; 2898 c_parser_consume_token (parser); 2899 if (c_parser_next_token_is_not (parser, CPP_OPEN_PAREN)) 2900 { 2901 attr = build_tree_list (attr_name, NULL_TREE); 2902 attrs = chainon (attrs, attr); 2903 continue; 2904 } 2905 c_parser_consume_token (parser); 2906 /* Parse the attribute contents. If they start with an 2907 identifier which is followed by a comma or close 2908 parenthesis, then the arguments start with that 2909 identifier; otherwise they are an expression list. */ 2910 if (c_parser_next_token_is (parser, CPP_NAME) 2911 && c_parser_peek_token (parser)->id_kind == C_ID_ID 2912 && ((c_parser_peek_2nd_token (parser)->type == CPP_COMMA) 2913 || (c_parser_peek_2nd_token (parser)->type 2914 == CPP_CLOSE_PAREN))) 2915 { 2916 tree arg1 = c_parser_peek_token (parser)->value; 2917 c_parser_consume_token (parser); 2918 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2919 attr_args = build_tree_list (NULL_TREE, arg1); 2920 else 2921 { 2922 c_parser_consume_token (parser); 2923 attr_args = tree_cons (NULL_TREE, arg1, 2924 c_parser_expr_list (parser, false)); 2925 } 2926 } 2927 else 2928 { 2929 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2930 attr_args = NULL_TREE; 2931 else 2932 attr_args = c_parser_expr_list (parser, false); 2933 } 2934 attr = build_tree_list (attr_name, attr_args); 2935 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2936 c_parser_consume_token (parser); 2937 else 2938 { 2939 c_lex_string_translate = 1; 2940 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 2941 "expected %<)%>"); 2942 return attrs; 2943 } 2944 attrs = chainon (attrs, attr); 2945 } 2946 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2947 c_parser_consume_token (parser); 2948 else 2949 { 2950 c_lex_string_translate = 1; 2951 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 2952 "expected %<)%>"); 2953 return attrs; 2954 } 2955 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 2956 c_parser_consume_token (parser); 2957 else 2958 { 2959 c_lex_string_translate = 1; 2960 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 2961 "expected %<)%>"); 2962 return attrs; 2963 } 2964 c_lex_string_translate = 1; 2965 } 2966 return attrs; 2967} 2968 2969/* Parse a type name (C90 6.5.5, C99 6.7.6). 2970 2971 type-name: 2972 specifier-qualifier-list abstract-declarator[opt] 2973*/ 2974 2975static struct c_type_name * 2976c_parser_type_name (c_parser *parser) 2977{ 2978 struct c_declspecs *specs = build_null_declspecs (); 2979 struct c_declarator *declarator; 2980 struct c_type_name *ret; 2981 bool dummy = false; 2982 c_parser_declspecs (parser, specs, false, true, true); 2983 if (!specs->declspecs_seen_p) 2984 { 2985 c_parser_error (parser, "expected specifier-qualifier-list"); 2986 return NULL; 2987 } 2988 pending_xref_error (); 2989 finish_declspecs (specs); 2990 declarator = c_parser_declarator (parser, specs->type_seen_p, 2991 C_DTR_ABSTRACT, &dummy); 2992 if (declarator == NULL) 2993 return NULL; 2994 ret = XOBNEW (&parser_obstack, struct c_type_name); 2995 ret->specs = specs; 2996 ret->declarator = declarator; 2997 return ret; 2998} 2999 3000/* Parse an initializer (C90 6.5.7, C99 6.7.8). 3001 3002 initializer: 3003 assignment-expression 3004 { initializer-list } 3005 { initializer-list , } 3006 3007 initializer-list: 3008 designation[opt] initializer 3009 initializer-list , designation[opt] initializer 3010 3011 designation: 3012 designator-list = 3013 3014 designator-list: 3015 designator 3016 designator-list designator 3017 3018 designator: 3019 array-designator 3020 . identifier 3021 3022 array-designator: 3023 [ constant-expression ] 3024 3025 GNU extensions: 3026 3027 initializer: 3028 { } 3029 3030 designation: 3031 array-designator 3032 identifier : 3033 3034 array-designator: 3035 [ constant-expression ... constant-expression ] 3036 3037 Any expression without commas is accepted in the syntax for the 3038 constant-expressions, with non-constant expressions rejected later. 3039 3040 This function is only used for top-level initializers; for nested 3041 ones, see c_parser_initval. */ 3042 3043static struct c_expr 3044c_parser_initializer (c_parser *parser) 3045{ 3046 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 3047 return c_parser_braced_init (parser, NULL_TREE, false); 3048 else 3049 { 3050 struct c_expr ret; 3051 ret = c_parser_expr_no_commas (parser, NULL); 3052 if (TREE_CODE (ret.value) != STRING_CST 3053 && TREE_CODE (ret.value) != COMPOUND_LITERAL_EXPR) 3054 ret = default_function_array_conversion (ret); 3055 return ret; 3056 } 3057} 3058 3059/* Parse a braced initializer list. TYPE is the type specified for a 3060 compound literal, and NULL_TREE for other initializers and for 3061 nested braced lists. NESTED_P is true for nested braced lists, 3062 false for the list of a compound literal or the list that is the 3063 top-level initializer in a declaration. */ 3064 3065static struct c_expr 3066c_parser_braced_init (c_parser *parser, tree type, bool nested_p) 3067{ 3068 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE)); 3069 c_parser_consume_token (parser); 3070 if (nested_p) 3071 push_init_level (0); 3072 else 3073 really_start_incremental_init (type); 3074 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 3075 { 3076 if (pedantic) 3077 pedwarn ("ISO C forbids empty initializer braces"); 3078 } 3079 else 3080 { 3081 /* Parse a non-empty initializer list, possibly with a trailing 3082 comma. */ 3083 while (true) 3084 { 3085 c_parser_initelt (parser); 3086 if (parser->error) 3087 break; 3088 if (c_parser_next_token_is (parser, CPP_COMMA)) 3089 c_parser_consume_token (parser); 3090 else 3091 break; 3092 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 3093 break; 3094 } 3095 } 3096 if (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE)) 3097 { 3098 struct c_expr ret; 3099 ret.value = error_mark_node; 3100 ret.original_code = ERROR_MARK; 3101 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, "expected %<}%>"); 3102 return ret; 3103 } 3104 c_parser_consume_token (parser); 3105 return pop_init_level (0); 3106} 3107 3108/* Parse a nested initializer, including designators. */ 3109 3110static void 3111c_parser_initelt (c_parser *parser) 3112{ 3113 /* Parse any designator or designator list. A single array 3114 designator may have the subsequent "=" omitted in GNU C, but a 3115 longer list or a structure member designator may not. */ 3116 if (c_parser_next_token_is (parser, CPP_NAME) 3117 && c_parser_peek_2nd_token (parser)->type == CPP_COLON) 3118 { 3119 /* Old-style structure member designator. */ 3120 set_init_label (c_parser_peek_token (parser)->value); 3121 if (pedantic) 3122 pedwarn ("obsolete use of designated initializer with %<:%>"); 3123 c_parser_consume_token (parser); 3124 c_parser_consume_token (parser); 3125 } 3126 else 3127 { 3128 /* des_seen is 0 if there have been no designators, 1 if there 3129 has been a single array designator and 2 otherwise. */ 3130 int des_seen = 0; 3131 while (c_parser_next_token_is (parser, CPP_OPEN_SQUARE) 3132 || c_parser_next_token_is (parser, CPP_DOT)) 3133 { 3134 int des_prev = des_seen; 3135 if (des_seen < 2) 3136 des_seen++; 3137 if (c_parser_next_token_is (parser, CPP_DOT)) 3138 { 3139 des_seen = 2; 3140 c_parser_consume_token (parser); 3141 if (c_parser_next_token_is (parser, CPP_NAME)) 3142 { 3143 set_init_label (c_parser_peek_token (parser)->value); 3144 c_parser_consume_token (parser); 3145 } 3146 else 3147 { 3148 struct c_expr init; 3149 init.value = error_mark_node; 3150 init.original_code = ERROR_MARK; 3151 c_parser_error (parser, "expected identifier"); 3152 c_parser_skip_until_found (parser, CPP_COMMA, NULL); 3153 process_init_element (init); 3154 return; 3155 } 3156 } 3157 else 3158 { 3159 tree first, second; 3160 /* ??? Following the old parser, [ objc-receiver 3161 objc-message-args ] is accepted as an initializer, 3162 being distinguished from a designator by what follows 3163 the first assignment expression inside the square 3164 brackets, but after a first array designator a 3165 subsequent square bracket is for Objective-C taken to 3166 start an expression, using the obsolete form of 3167 designated initializer without '=', rather than 3168 possibly being a second level of designation: in LALR 3169 terms, the '[' is shifted rather than reducing 3170 designator to designator-list. */ 3171 if (des_prev == 1 && c_dialect_objc ()) 3172 { 3173 des_seen = des_prev; 3174 break; 3175 } 3176 if (des_prev == 0 && c_dialect_objc ()) 3177 { 3178 /* This might be an array designator or an 3179 Objective-C message expression. If the former, 3180 continue parsing here; if the latter, parse the 3181 remainder of the initializer given the starting 3182 primary-expression. ??? It might make sense to 3183 distinguish when des_prev == 1 as well; see 3184 previous comment. */ 3185 tree rec, args; 3186 struct c_expr mexpr; 3187 c_parser_consume_token (parser); 3188 if (c_parser_peek_token (parser)->type == CPP_NAME 3189 && ((c_parser_peek_token (parser)->id_kind 3190 == C_ID_TYPENAME) 3191 || (c_parser_peek_token (parser)->id_kind 3192 == C_ID_CLASSNAME))) 3193 { 3194 /* Type name receiver. */ 3195 tree id = c_parser_peek_token (parser)->value; 3196 c_parser_consume_token (parser); 3197 rec = objc_get_class_reference (id); 3198 goto parse_message_args; 3199 } 3200 first = c_parser_expr_no_commas (parser, NULL).value; 3201 if (c_parser_next_token_is (parser, CPP_ELLIPSIS) 3202 || c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) 3203 goto array_desig_after_first; 3204 /* Expression receiver. So far only one part 3205 without commas has been parsed; there might be 3206 more of the expression. */ 3207 rec = first; 3208 while (c_parser_next_token_is (parser, CPP_COMMA)) 3209 { 3210 struct c_expr next; 3211 c_parser_consume_token (parser); 3212 next = c_parser_expr_no_commas (parser, NULL); 3213 next = default_function_array_conversion (next); 3214 rec = build_compound_expr (rec, next.value); 3215 } 3216 parse_message_args: 3217 /* Now parse the objc-message-args. */ 3218 args = c_parser_objc_message_args (parser); 3219 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 3220 "expected %<]%>"); 3221 mexpr.value 3222 = objc_build_message_expr (build_tree_list (rec, args)); 3223 mexpr.original_code = ERROR_MARK; 3224 /* Now parse and process the remainder of the 3225 initializer, starting with this message 3226 expression as a primary-expression. */ 3227 c_parser_initval (parser, &mexpr); 3228 return; 3229 } 3230 c_parser_consume_token (parser); 3231 first = c_parser_expr_no_commas (parser, NULL).value; 3232 array_desig_after_first: 3233 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) 3234 { 3235 c_parser_consume_token (parser); 3236 second = c_parser_expr_no_commas (parser, NULL).value; 3237 } 3238 else 3239 second = NULL_TREE; 3240 if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE)) 3241 { 3242 c_parser_consume_token (parser); 3243 set_init_index (first, second); 3244 if (pedantic && second) 3245 pedwarn ("ISO C forbids specifying range of " 3246 "elements to initialize"); 3247 } 3248 else 3249 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 3250 "expected %<]%>"); 3251 } 3252 } 3253 if (des_seen >= 1) 3254 { 3255 if (c_parser_next_token_is (parser, CPP_EQ)) 3256 { 3257 if (pedantic && !flag_isoc99) 3258 pedwarn ("ISO C90 forbids specifying subobject to initialize"); 3259 c_parser_consume_token (parser); 3260 } 3261 else 3262 { 3263 if (des_seen == 1) 3264 { 3265 if (pedantic) 3266 pedwarn ("obsolete use of designated initializer " 3267 "without %<=%>"); 3268 } 3269 else 3270 { 3271 struct c_expr init; 3272 init.value = error_mark_node; 3273 init.original_code = ERROR_MARK; 3274 c_parser_error (parser, "expected %<=%>"); 3275 c_parser_skip_until_found (parser, CPP_COMMA, NULL); 3276 process_init_element (init); 3277 return; 3278 } 3279 } 3280 } 3281 } 3282 c_parser_initval (parser, NULL); 3283} 3284 3285/* Parse a nested initializer; as c_parser_initializer but parses 3286 initializers within braced lists, after any designators have been 3287 applied. If AFTER is not NULL then it is an Objective-C message 3288 expression which is the primary-expression starting the 3289 initializer. */ 3290 3291static void 3292c_parser_initval (c_parser *parser, struct c_expr *after) 3293{ 3294 struct c_expr init; 3295 gcc_assert (!after || c_dialect_objc ()); 3296 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE) && !after) 3297 init = c_parser_braced_init (parser, NULL_TREE, true); 3298 else 3299 { 3300 init = c_parser_expr_no_commas (parser, after); 3301 if (init.value != NULL_TREE 3302 && TREE_CODE (init.value) != STRING_CST 3303 && TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR) 3304 init = default_function_array_conversion (init); 3305 } 3306 process_init_element (init); 3307} 3308 3309/* Parse a compound statement (possibly a function body) (C90 6.6.2, 3310 C99 6.8.2). 3311 3312 compound-statement: 3313 { block-item-list[opt] } 3314 { label-declarations block-item-list } 3315 3316 block-item-list: 3317 block-item 3318 block-item-list block-item 3319 3320 block-item: 3321 nested-declaration 3322 statement 3323 3324 nested-declaration: 3325 declaration 3326 3327 GNU extensions: 3328 3329 compound-statement: 3330 { label-declarations block-item-list } 3331 3332 nested-declaration: 3333 __extension__ nested-declaration 3334 nested-function-definition 3335 3336 label-declarations: 3337 label-declaration 3338 label-declarations label-declaration 3339 3340 label-declaration: 3341 __label__ identifier-list ; 3342 3343 Allowing the mixing of declarations and code is new in C99. The 3344 GNU syntax also permits (not shown above) labels at the end of 3345 compound statements, which yield an error. We don't allow labels 3346 on declarations; this might seem like a natural extension, but 3347 there would be a conflict between attributes on the label and 3348 prefix attributes on the declaration. ??? The syntax follows the 3349 old parser in requiring something after label declarations. 3350 Although they are erroneous if the labels declared aren't defined, 3351 is it useful for the syntax to be this way? 3352 3353 OpenMP: 3354 3355 block-item: 3356 openmp-directive 3357 3358 openmp-directive: 3359 barrier-directive 3360 flush-directive */ 3361 3362static tree 3363c_parser_compound_statement (c_parser *parser) 3364{ 3365 tree stmt; 3366 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) 3367 return error_mark_node; 3368 stmt = c_begin_compound_stmt (true); 3369 c_parser_compound_statement_nostart (parser); 3370 return c_end_compound_stmt (stmt, true); 3371} 3372 3373/* Parse a compound statement except for the opening brace. This is 3374 used for parsing both compound statements and statement expressions 3375 (which follow different paths to handling the opening). */ 3376 3377static void 3378c_parser_compound_statement_nostart (c_parser *parser) 3379{ 3380 bool last_stmt = false; 3381 bool last_label = false; 3382 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 3383 { 3384 c_parser_consume_token (parser); 3385 return; 3386 } 3387 if (c_parser_next_token_is_keyword (parser, RID_LABEL)) 3388 { 3389 /* Read zero or more forward-declarations for labels that nested 3390 functions can jump to. */ 3391 while (c_parser_next_token_is_keyword (parser, RID_LABEL)) 3392 { 3393 c_parser_consume_token (parser); 3394 /* Any identifiers, including those declared as type names, 3395 are OK here. */ 3396 while (true) 3397 { 3398 tree label; 3399 if (c_parser_next_token_is_not (parser, CPP_NAME)) 3400 { 3401 c_parser_error (parser, "expected identifier"); 3402 break; 3403 } 3404 label 3405 = declare_label (c_parser_peek_token (parser)->value); 3406 C_DECLARED_LABEL_FLAG (label) = 1; 3407 add_stmt (build_stmt (DECL_EXPR, label)); 3408 c_parser_consume_token (parser); 3409 if (c_parser_next_token_is (parser, CPP_COMMA)) 3410 c_parser_consume_token (parser); 3411 else 3412 break; 3413 } 3414 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 3415 } 3416 /* ??? Locating this diagnostic on the token after the 3417 declarations end follows the old parser, but it might be 3418 better to locate it where the declarations start instead. */ 3419 if (pedantic) 3420 pedwarn ("ISO C forbids label declarations"); 3421 } 3422 /* We must now have at least one statement, label or declaration. */ 3423 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 3424 { 3425 c_parser_error (parser, "expected declaration or statement"); 3426 c_parser_consume_token (parser); 3427 return; 3428 } 3429 while (c_parser_next_token_is_not (parser, CPP_CLOSE_BRACE)) 3430 { 3431 location_t loc = c_parser_peek_token (parser)->location; 3432 if (c_parser_next_token_is_keyword (parser, RID_CASE) 3433 || c_parser_next_token_is_keyword (parser, RID_DEFAULT) 3434 || (c_parser_next_token_is (parser, CPP_NAME) 3435 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) 3436 { 3437 last_label = true; 3438 last_stmt = false; 3439 c_parser_label (parser); 3440 } 3441 else if (!last_label 3442 && c_parser_next_token_starts_declspecs (parser)) 3443 { 3444 last_label = false; 3445 c_parser_declaration_or_fndef (parser, true, true, true, true); 3446 if (last_stmt 3447 && ((pedantic && !flag_isoc99) 3448 || warn_declaration_after_statement)) 3449 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code", 3450 &loc); 3451 last_stmt = false; 3452 } 3453 else if (!last_label 3454 && c_parser_next_token_is_keyword (parser, RID_EXTENSION)) 3455 { 3456 /* __extension__ can start a declaration, but is also an 3457 unary operator that can start an expression. Consume all 3458 but the last of a possible series of __extension__ to 3459 determine which. */ 3460 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD 3461 && (c_parser_peek_2nd_token (parser)->keyword 3462 == RID_EXTENSION)) 3463 c_parser_consume_token (parser); 3464 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser))) 3465 { 3466 int ext; 3467 ext = disable_extension_diagnostics (); 3468 c_parser_consume_token (parser); 3469 last_label = false; 3470 c_parser_declaration_or_fndef (parser, true, true, true, true); 3471 /* Following the old parser, __extension__ does not 3472 disable this diagnostic. */ 3473 restore_extension_diagnostics (ext); 3474 if (last_stmt 3475 && ((pedantic && !flag_isoc99) 3476 || warn_declaration_after_statement)) 3477 pedwarn_c90 ("%HISO C90 forbids mixed declarations and code", 3478 &loc); 3479 last_stmt = false; 3480 } 3481 else 3482 goto statement; 3483 } 3484 else if (c_parser_next_token_is (parser, CPP_PRAGMA)) 3485 { 3486 /* External pragmas, and some omp pragmas, are not associated 3487 with regular c code, and so are not to be considered statements 3488 syntactically. This ensures that the user doesn't put them 3489 places that would turn into syntax errors if the directive 3490 were ignored. */ 3491 if (c_parser_pragma (parser, pragma_compound)) 3492 last_label = false, last_stmt = true; 3493 } 3494 else if (c_parser_next_token_is (parser, CPP_EOF)) 3495 { 3496 c_parser_error (parser, "expected declaration or statement"); 3497 return; 3498 } 3499 else 3500 { 3501 statement: 3502 last_label = false; 3503 last_stmt = true; 3504 c_parser_statement_after_labels (parser); 3505 } 3506 3507 parser->error = false; 3508 } 3509 if (last_label) 3510 error ("label at end of compound statement"); 3511 c_parser_consume_token (parser); 3512} 3513 3514/* Parse a label (C90 6.6.1, C99 6.8.1). 3515 3516 label: 3517 identifier : attributes[opt] 3518 case constant-expression : 3519 default : 3520 3521 GNU extensions: 3522 3523 label: 3524 case constant-expression ... constant-expression : 3525 3526 The use of attributes on labels is a GNU extension. The syntax in 3527 GNU C accepts any expressions without commas, non-constant 3528 expressions being rejected later. */ 3529 3530static void 3531c_parser_label (c_parser *parser) 3532{ 3533 location_t loc1 = c_parser_peek_token (parser)->location; 3534 tree label = NULL_TREE; 3535 if (c_parser_next_token_is_keyword (parser, RID_CASE)) 3536 { 3537 tree exp1, exp2; 3538 c_parser_consume_token (parser); 3539 exp1 = c_parser_expr_no_commas (parser, NULL).value; 3540 if (c_parser_next_token_is (parser, CPP_COLON)) 3541 { 3542 c_parser_consume_token (parser); 3543 label = do_case (exp1, NULL_TREE); 3544 } 3545 else if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) 3546 { 3547 c_parser_consume_token (parser); 3548 exp2 = c_parser_expr_no_commas (parser, NULL).value; 3549 if (c_parser_require (parser, CPP_COLON, "expected %<:%>")) 3550 label = do_case (exp1, exp2); 3551 } 3552 else 3553 c_parser_error (parser, "expected %<:%> or %<...%>"); 3554 } 3555 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)) 3556 { 3557 c_parser_consume_token (parser); 3558 if (c_parser_require (parser, CPP_COLON, "expected %<:%>")) 3559 label = do_case (NULL_TREE, NULL_TREE); 3560 } 3561 else 3562 { 3563 tree name = c_parser_peek_token (parser)->value; 3564 tree tlab; 3565 location_t loc2; 3566 tree attrs; 3567 gcc_assert (c_parser_next_token_is (parser, CPP_NAME)); 3568 c_parser_consume_token (parser); 3569 gcc_assert (c_parser_next_token_is (parser, CPP_COLON)); 3570 loc2 = c_parser_peek_token (parser)->location; 3571 c_parser_consume_token (parser); 3572 attrs = c_parser_attributes (parser); 3573 tlab = define_label (loc2, name); 3574 if (tlab) 3575 { 3576 decl_attributes (&tlab, attrs, 0); 3577 label = add_stmt (build_stmt (LABEL_EXPR, tlab)); 3578 } 3579 } 3580 if (label) 3581 SET_EXPR_LOCATION (label, loc1); 3582} 3583 3584/* Parse a statement (C90 6.6, C99 6.8). 3585 3586 statement: 3587 labeled-statement 3588 compound-statement 3589 expression-statement 3590 selection-statement 3591 iteration-statement 3592 jump-statement 3593 3594 labeled-statement: 3595 label statement 3596 3597 expression-statement: 3598 expression[opt] ; 3599 3600 selection-statement: 3601 if-statement 3602 switch-statement 3603 3604 iteration-statement: 3605 while-statement 3606 do-statement 3607 for-statement 3608 3609 jump-statement: 3610 goto identifier ; 3611 continue ; 3612 break ; 3613 return expression[opt] ; 3614 3615 GNU extensions: 3616 3617 statement: 3618 asm-statement 3619 3620 jump-statement: 3621 goto * expression ; 3622 3623 Objective-C: 3624 3625 statement: 3626 objc-throw-statement 3627 objc-try-catch-statement 3628 objc-synchronized-statement 3629 3630 objc-throw-statement: 3631 @throw expression ; 3632 @throw ; 3633 3634 OpenMP: 3635 3636 statement: 3637 openmp-construct 3638 3639 openmp-construct: 3640 parallel-construct 3641 for-construct 3642 sections-construct 3643 single-construct 3644 parallel-for-construct 3645 parallel-sections-construct 3646 master-construct 3647 critical-construct 3648 atomic-construct 3649 ordered-construct 3650 3651 parallel-construct: 3652 parallel-directive structured-block 3653 3654 for-construct: 3655 for-directive iteration-statement 3656 3657 sections-construct: 3658 sections-directive section-scope 3659 3660 single-construct: 3661 single-directive structured-block 3662 3663 parallel-for-construct: 3664 parallel-for-directive iteration-statement 3665 3666 parallel-sections-construct: 3667 parallel-sections-directive section-scope 3668 3669 master-construct: 3670 master-directive structured-block 3671 3672 critical-construct: 3673 critical-directive structured-block 3674 3675 atomic-construct: 3676 atomic-directive expression-statement 3677 3678 ordered-construct: 3679 ordered-directive structured-block */ 3680 3681static void 3682c_parser_statement (c_parser *parser) 3683{ 3684 while (c_parser_next_token_is_keyword (parser, RID_CASE) 3685 || c_parser_next_token_is_keyword (parser, RID_DEFAULT) 3686 || (c_parser_next_token_is (parser, CPP_NAME) 3687 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) 3688 c_parser_label (parser); 3689 c_parser_statement_after_labels (parser); 3690} 3691 3692/* Parse a statement, other than a labeled statement. */ 3693 3694static void 3695c_parser_statement_after_labels (c_parser *parser) 3696{ 3697 location_t loc = c_parser_peek_token (parser)->location; 3698 tree stmt = NULL_TREE; 3699 switch (c_parser_peek_token (parser)->type) 3700 { 3701 case CPP_OPEN_BRACE: 3702 add_stmt (c_parser_compound_statement (parser)); 3703 break; 3704 case CPP_KEYWORD: 3705 switch (c_parser_peek_token (parser)->keyword) 3706 { 3707 case RID_IF: 3708 c_parser_if_statement (parser); 3709 break; 3710 case RID_SWITCH: 3711 c_parser_switch_statement (parser); 3712 break; 3713 case RID_WHILE: 3714 c_parser_while_statement (parser); 3715 break; 3716 case RID_DO: 3717 c_parser_do_statement (parser); 3718 break; 3719 case RID_FOR: 3720 c_parser_for_statement (parser); 3721 break; 3722 case RID_GOTO: 3723 c_parser_consume_token (parser); 3724 if (c_parser_next_token_is (parser, CPP_NAME)) 3725 { 3726 stmt = c_finish_goto_label (c_parser_peek_token (parser)->value); 3727 c_parser_consume_token (parser); 3728 } 3729 else if (c_parser_next_token_is (parser, CPP_MULT)) 3730 { 3731 c_parser_consume_token (parser); 3732 stmt = c_finish_goto_ptr (c_parser_expression (parser).value); 3733 } 3734 else 3735 c_parser_error (parser, "expected identifier or %<*%>"); 3736 goto expect_semicolon; 3737 case RID_CONTINUE: 3738 c_parser_consume_token (parser); 3739 stmt = c_finish_bc_stmt (&c_cont_label, false); 3740 goto expect_semicolon; 3741 case RID_BREAK: 3742 c_parser_consume_token (parser); 3743 stmt = c_finish_bc_stmt (&c_break_label, true); 3744 goto expect_semicolon; 3745 case RID_RETURN: 3746 c_parser_consume_token (parser); 3747 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 3748 { 3749 stmt = c_finish_return (NULL_TREE); 3750 c_parser_consume_token (parser); 3751 } 3752 else 3753 { 3754 stmt = c_finish_return (c_parser_expression_conv (parser).value); 3755 goto expect_semicolon; 3756 } 3757 break; 3758 case RID_ASM: 3759 stmt = c_parser_asm_statement (parser); 3760 break; 3761 case RID_AT_THROW: 3762 gcc_assert (c_dialect_objc ()); 3763 c_parser_consume_token (parser); 3764 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 3765 { 3766 stmt = objc_build_throw_stmt (NULL_TREE); 3767 c_parser_consume_token (parser); 3768 } 3769 else 3770 { 3771 stmt 3772 = objc_build_throw_stmt (c_parser_expression (parser).value); 3773 goto expect_semicolon; 3774 } 3775 break; 3776 case RID_AT_TRY: 3777 gcc_assert (c_dialect_objc ()); 3778 c_parser_objc_try_catch_statement (parser); 3779 break; 3780 case RID_AT_SYNCHRONIZED: 3781 gcc_assert (c_dialect_objc ()); 3782 c_parser_objc_synchronized_statement (parser); 3783 break; 3784 default: 3785 goto expr_stmt; 3786 } 3787 break; 3788 case CPP_SEMICOLON: 3789 c_parser_consume_token (parser); 3790 break; 3791 case CPP_CLOSE_PAREN: 3792 case CPP_CLOSE_SQUARE: 3793 /* Avoid infinite loop in error recovery: 3794 c_parser_skip_until_found stops at a closing nesting 3795 delimiter without consuming it, but here we need to consume 3796 it to proceed further. */ 3797 c_parser_error (parser, "expected statement"); 3798 c_parser_consume_token (parser); 3799 break; 3800 case CPP_PRAGMA: 3801 c_parser_pragma (parser, pragma_stmt); 3802 break; 3803 default: 3804 expr_stmt: 3805 stmt = c_finish_expr_stmt (c_parser_expression_conv (parser).value); 3806 expect_semicolon: 3807 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 3808 break; 3809 } 3810 /* Two cases cannot and do not have line numbers associated: If stmt 3811 is degenerate, such as "2;", then stmt is an INTEGER_CST, which 3812 cannot hold line numbers. But that's OK because the statement 3813 will either be changed to a MODIFY_EXPR during gimplification of 3814 the statement expr, or discarded. If stmt was compound, but 3815 without new variables, we will have skipped the creation of a 3816 BIND and will have a bare STATEMENT_LIST. But that's OK because 3817 (recursively) all of the component statements should already have 3818 line numbers assigned. ??? Can we discard no-op statements 3819 earlier? */ 3820 if (stmt && EXPR_P (stmt)) 3821 SET_EXPR_LOCATION (stmt, loc); 3822} 3823 3824/* Parse a parenthesized condition from an if, do or while statement. 3825 3826 condition: 3827 ( expression ) 3828*/ 3829static tree 3830c_parser_paren_condition (c_parser *parser) 3831{ 3832 location_t loc; 3833 tree cond; 3834 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 3835 return error_mark_node; 3836 loc = c_parser_peek_token (parser)->location; 3837 cond = c_objc_common_truthvalue_conversion 3838 (c_parser_expression_conv (parser).value); 3839 if (EXPR_P (cond)) 3840 SET_EXPR_LOCATION (cond, loc); 3841 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 3842 return cond; 3843} 3844 3845/* Parse a statement which is a block in C99. */ 3846 3847static tree 3848c_parser_c99_block_statement (c_parser *parser) 3849{ 3850 tree block = c_begin_compound_stmt (flag_isoc99); 3851 c_parser_statement (parser); 3852 return c_end_compound_stmt (block, flag_isoc99); 3853} 3854 3855/* Parse the body of an if statement or the else half thereof. This 3856 is just parsing a statement but (a) it is a block in C99, (b) we 3857 track whether the body is an if statement for the sake of 3858 -Wparentheses warnings, (c) we handle an empty body specially for 3859 the sake of -Wextra warnings. */ 3860 3861static tree 3862c_parser_if_body (c_parser *parser, bool *if_p) 3863{ 3864 tree block = c_begin_compound_stmt (flag_isoc99); 3865 while (c_parser_next_token_is_keyword (parser, RID_CASE) 3866 || c_parser_next_token_is_keyword (parser, RID_DEFAULT) 3867 || (c_parser_next_token_is (parser, CPP_NAME) 3868 && c_parser_peek_2nd_token (parser)->type == CPP_COLON)) 3869 c_parser_label (parser); 3870 *if_p = c_parser_next_token_is_keyword (parser, RID_IF); 3871 if (extra_warnings && c_parser_next_token_is (parser, CPP_SEMICOLON)) 3872 add_stmt (build_empty_stmt ()); 3873 c_parser_statement_after_labels (parser); 3874 return c_end_compound_stmt (block, flag_isoc99); 3875} 3876 3877/* Parse an if statement (C90 6.6.4, C99 6.8.4). 3878 3879 if-statement: 3880 if ( expression ) statement 3881 if ( expression ) statement else statement 3882*/ 3883 3884static void 3885c_parser_if_statement (c_parser *parser) 3886{ 3887 tree block; 3888 location_t loc; 3889 tree cond; 3890 bool first_if = false, second_if = false; 3891 tree first_body, second_body; 3892 gcc_assert (c_parser_next_token_is_keyword (parser, RID_IF)); 3893 c_parser_consume_token (parser); 3894 block = c_begin_compound_stmt (flag_isoc99); 3895 loc = c_parser_peek_token (parser)->location; 3896 cond = c_parser_paren_condition (parser); 3897 first_body = c_parser_if_body (parser, &first_if); 3898 if (c_parser_next_token_is_keyword (parser, RID_ELSE)) 3899 { 3900 c_parser_consume_token (parser); 3901 second_body = c_parser_if_body (parser, &second_if); 3902 } 3903 else 3904 second_body = NULL_TREE; 3905 c_finish_if_stmt (loc, cond, first_body, second_body, first_if); 3906 add_stmt (c_end_compound_stmt (block, flag_isoc99)); 3907} 3908 3909/* Parse a switch statement (C90 6.6.4, C99 6.8.4). 3910 3911 switch-statement: 3912 switch (expression) statement 3913*/ 3914 3915static void 3916c_parser_switch_statement (c_parser *parser) 3917{ 3918 tree block, expr, body, save_break; 3919 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SWITCH)); 3920 c_parser_consume_token (parser); 3921 block = c_begin_compound_stmt (flag_isoc99); 3922 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 3923 { 3924 expr = c_parser_expression (parser).value; 3925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 3926 } 3927 else 3928 expr = error_mark_node; 3929 c_start_case (expr); 3930 save_break = c_break_label; 3931 c_break_label = NULL_TREE; 3932 body = c_parser_c99_block_statement (parser); 3933 c_finish_case (body); 3934 if (c_break_label) 3935 add_stmt (build1 (LABEL_EXPR, void_type_node, c_break_label)); 3936 c_break_label = save_break; 3937 add_stmt (c_end_compound_stmt (block, flag_isoc99)); 3938} 3939 3940/* Parse a while statement (C90 6.6.5, C99 6.8.5). 3941 3942 while-statement: 3943 APPLE LOCAL begin for-fsf-4_4 3274130 5295549 3944 while attributes (expression) statement 3945 3946 The use of attributes is a GNU extension. 3947 APPLE LOCAL end for-fsf-4_4 3274130 5295549 3948*/ 3949 3950static void 3951c_parser_while_statement (c_parser *parser) 3952{ 3953/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 3954 tree block, cond, body, save_break, save_cont, attrs; 3955/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 3956 location_t loc; 3957 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE)); 3958 c_parser_consume_token (parser); 3959/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 3960 attrs = c_parser_attributes (parser); 3961/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 3962 block = c_begin_compound_stmt (flag_isoc99); 3963 loc = c_parser_peek_token (parser)->location; 3964 cond = c_parser_paren_condition (parser); 3965 save_break = c_break_label; 3966 c_break_label = NULL_TREE; 3967 save_cont = c_cont_label; 3968 c_cont_label = NULL_TREE; 3969 body = c_parser_c99_block_statement (parser); 3970/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 3971 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, attrs, 3972 true); 3973/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 3974 add_stmt (c_end_compound_stmt (block, flag_isoc99)); 3975 c_break_label = save_break; 3976 c_cont_label = save_cont; 3977} 3978 3979/* Parse a do statement (C90 6.6.5, C99 6.8.5). 3980 3981 do-statement: 3982 APPLE LOCAL begin for-fsf-4_4 3274130 5295549 3983 do attributes statement while ( expression ) ; 3984 3985 The use of attributes is a GNU extension. 3986 APPLE LOCAL end for-fsf-4_4 3274130 5295549 3987*/ 3988 3989static void 3990c_parser_do_statement (c_parser *parser) 3991{ 3992/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 3993 tree block, cond, body, save_break, save_cont, new_break, new_cont, attrs; 3994/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 3995 location_t loc; 3996 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO)); 3997 c_parser_consume_token (parser); 3998/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 3999 attrs = c_parser_attributes (parser); 4000/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 4001 block = c_begin_compound_stmt (flag_isoc99); 4002 loc = c_parser_peek_token (parser)->location; 4003 save_break = c_break_label; 4004 c_break_label = NULL_TREE; 4005 save_cont = c_cont_label; 4006 c_cont_label = NULL_TREE; 4007 body = c_parser_c99_block_statement (parser); 4008 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>"); 4009 new_break = c_break_label; 4010 c_break_label = save_break; 4011 new_cont = c_cont_label; 4012 c_cont_label = save_cont; 4013 cond = c_parser_paren_condition (parser); 4014 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>")) 4015 c_parser_skip_to_end_of_block_or_statement (parser); 4016/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 4017 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, attrs, false); 4018/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 4019 add_stmt (c_end_compound_stmt (block, flag_isoc99)); 4020} 4021 4022/* Parse a for statement (C90 6.6.5, C99 6.8.5). 4023 4024 for-statement: 4025 APPLE LOCAL begin for-fsf-4_4 3274130 5295549 4026 for attributes ( expression[opt] ; expression[opt] ; expression[opt] ) \ 4027 statement 4028 for attributes ( nested-declaration expression[opt] ; expression[opt] ) \ 4029 statement 4030 4031 The form with a declaration is new in C99. 4032 4033 The use of attributes is a GNU extension. 4034 4035 APPLE LOCAL end for-fsf-4_4 3274130 5295549 4036 ??? In accordance with the old parser, the declaration may be a 4037 nested function, which is then rejected in check_for_loop_decls, 4038 but does it make any sense for this to be included in the grammar? 4039 Note in particular that the nested function does not include a 4040 trailing ';', whereas the "declaration" production includes one. 4041 Also, can we reject bad declarations earlier and cheaper than 4042 check_for_loop_decls? */ 4043 4044static void 4045c_parser_for_statement (c_parser *parser) 4046{ 4047/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 4048 tree block, cond, incr, save_break, save_cont, body, attrs; 4049/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 4050 location_t loc; 4051 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR)); 4052 loc = c_parser_peek_token (parser)->location; 4053 c_parser_consume_token (parser); 4054/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 4055 attrs = c_parser_attributes (parser); 4056/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 4057 block = c_begin_compound_stmt (flag_isoc99); 4058 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 4059 { 4060 /* Parse the initialization declaration or expression. */ 4061 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 4062 { 4063 c_parser_consume_token (parser); 4064 c_finish_expr_stmt (NULL_TREE); 4065 } 4066 else if (c_parser_next_token_starts_declspecs (parser)) 4067 { 4068 c_parser_declaration_or_fndef (parser, true, true, true, true); 4069 check_for_loop_decls (); 4070 } 4071 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION)) 4072 { 4073 /* __extension__ can start a declaration, but is also an 4074 unary operator that can start an expression. Consume all 4075 but the last of a possible series of __extension__ to 4076 determine which. */ 4077 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD 4078 && (c_parser_peek_2nd_token (parser)->keyword 4079 == RID_EXTENSION)) 4080 c_parser_consume_token (parser); 4081 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser))) 4082 { 4083 int ext; 4084 ext = disable_extension_diagnostics (); 4085 c_parser_consume_token (parser); 4086 c_parser_declaration_or_fndef (parser, true, true, true, true); 4087 restore_extension_diagnostics (ext); 4088 check_for_loop_decls (); 4089 } 4090 else 4091 goto init_expr; 4092 } 4093 else 4094 { 4095 init_expr: 4096 c_finish_expr_stmt (c_parser_expression (parser).value); 4097 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 4098 } 4099 /* Parse the loop condition. */ 4100 loc = c_parser_peek_token (parser)->location; 4101 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 4102 { 4103 c_parser_consume_token (parser); 4104 cond = NULL_TREE; 4105 } 4106 else 4107 { 4108 tree ocond = c_parser_expression_conv (parser).value; 4109 cond = c_objc_common_truthvalue_conversion (ocond); 4110 if (EXPR_P (cond)) 4111 SET_EXPR_LOCATION (cond, loc); 4112 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 4113 } 4114 /* Parse the increment expression. */ 4115 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4116 incr = c_process_expr_stmt (NULL_TREE); 4117 else 4118 incr = c_process_expr_stmt (c_parser_expression (parser).value); 4119 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 4120 } 4121 else 4122 { 4123 cond = error_mark_node; 4124 incr = error_mark_node; 4125 } 4126 save_break = c_break_label; 4127 c_break_label = NULL_TREE; 4128 save_cont = c_cont_label; 4129 c_cont_label = NULL_TREE; 4130 body = c_parser_c99_block_statement (parser); 4131/* APPLE LOCAL begin for-fsf-4_4 3274130 5295549 */ \ 4132 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, attrs, 4133 true); 4134/* APPLE LOCAL end for-fsf-4_4 3274130 5295549 */ \ 4135 add_stmt (c_end_compound_stmt (block, flag_isoc99)); 4136 c_break_label = save_break; 4137 c_cont_label = save_cont; 4138} 4139 4140/* Parse an asm statement, a GNU extension. This is a full-blown asm 4141 statement with inputs, outputs, clobbers, and volatile tag 4142 allowed. 4143 4144 asm-statement: 4145 asm type-qualifier[opt] ( asm-argument ) ; 4146 4147 asm-argument: 4148 asm-string-literal 4149 asm-string-literal : asm-operands[opt] 4150 asm-string-literal : asm-operands[opt] : asm-operands[opt] 4151 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers 4152 4153 Qualifiers other than volatile are accepted in the syntax but 4154 warned for. */ 4155 4156static tree 4157c_parser_asm_statement (c_parser *parser) 4158{ 4159 tree quals, str, outputs, inputs, clobbers, ret; 4160 bool simple; 4161 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM)); 4162 c_parser_consume_token (parser); 4163 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE)) 4164 { 4165 quals = c_parser_peek_token (parser)->value; 4166 c_parser_consume_token (parser); 4167 } 4168 else if (c_parser_next_token_is_keyword (parser, RID_CONST) 4169 || c_parser_next_token_is_keyword (parser, RID_RESTRICT)) 4170 { 4171 warning (0, "%E qualifier ignored on asm", 4172 c_parser_peek_token (parser)->value); 4173 quals = NULL_TREE; 4174 c_parser_consume_token (parser); 4175 } 4176 else 4177 quals = NULL_TREE; 4178 /* ??? Follow the C++ parser rather than using the 4179 c_lex_string_translate kludge. */ 4180 c_lex_string_translate = 0; 4181 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 4182 { 4183 c_lex_string_translate = 1; 4184 return NULL_TREE; 4185 } 4186 str = c_parser_asm_string_literal (parser); 4187 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4188 { 4189 simple = true; 4190 outputs = NULL_TREE; 4191 inputs = NULL_TREE; 4192 clobbers = NULL_TREE; 4193 goto done_asm; 4194 } 4195 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) 4196 { 4197 c_lex_string_translate = 1; 4198 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4199 return NULL_TREE; 4200 } 4201 simple = false; 4202 /* Parse outputs. */ 4203 if (c_parser_next_token_is (parser, CPP_COLON) 4204 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4205 outputs = NULL_TREE; 4206 else 4207 outputs = c_parser_asm_operands (parser, false); 4208 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4209 { 4210 inputs = NULL_TREE; 4211 clobbers = NULL_TREE; 4212 goto done_asm; 4213 } 4214 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) 4215 { 4216 c_lex_string_translate = 1; 4217 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4218 return NULL_TREE; 4219 } 4220 /* Parse inputs. */ 4221 if (c_parser_next_token_is (parser, CPP_COLON) 4222 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4223 inputs = NULL_TREE; 4224 else 4225 inputs = c_parser_asm_operands (parser, true); 4226 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4227 { 4228 clobbers = NULL_TREE; 4229 goto done_asm; 4230 } 4231 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) 4232 { 4233 c_lex_string_translate = 1; 4234 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4235 return NULL_TREE; 4236 } 4237 /* Parse clobbers. */ 4238 clobbers = c_parser_asm_clobbers (parser); 4239 done_asm: 4240 c_lex_string_translate = 1; 4241 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) 4242 { 4243 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4244 return NULL_TREE; 4245 } 4246 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>")) 4247 c_parser_skip_to_end_of_block_or_statement (parser); 4248 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs, 4249 clobbers, simple)); 4250 return ret; 4251} 4252 4253/* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but 4254 not outputs), apply the default conversion of functions and arrays 4255 to pointers. 4256 4257 asm-operands: 4258 asm-operand 4259 asm-operands , asm-operand 4260 4261 asm-operand: 4262 asm-string-literal ( expression ) 4263 [ identifier ] asm-string-literal ( expression ) 4264*/ 4265 4266static tree 4267c_parser_asm_operands (c_parser *parser, bool convert_p) 4268{ 4269 tree list = NULL_TREE; 4270 while (true) 4271 { 4272 tree name, str; 4273 struct c_expr expr; 4274 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) 4275 { 4276 c_parser_consume_token (parser); 4277 if (c_parser_next_token_is (parser, CPP_NAME)) 4278 { 4279 tree id = c_parser_peek_token (parser)->value; 4280 c_parser_consume_token (parser); 4281 name = build_string (IDENTIFIER_LENGTH (id), 4282 IDENTIFIER_POINTER (id)); 4283 } 4284 else 4285 { 4286 c_parser_error (parser, "expected identifier"); 4287 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL); 4288 return NULL_TREE; 4289 } 4290 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 4291 "expected %<]%>"); 4292 } 4293 else 4294 name = NULL_TREE; 4295 str = c_parser_asm_string_literal (parser); 4296 if (str == NULL_TREE) 4297 return NULL_TREE; 4298 c_lex_string_translate = 1; 4299 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 4300 { 4301 c_lex_string_translate = 0; 4302 return NULL_TREE; 4303 } 4304 expr = c_parser_expression (parser); 4305 if (convert_p) 4306 expr = default_function_array_conversion (expr); 4307 c_lex_string_translate = 0; 4308 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) 4309 { 4310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4311 return NULL_TREE; 4312 } 4313 list = chainon (list, build_tree_list (build_tree_list (name, str), 4314 expr.value)); 4315 if (c_parser_next_token_is (parser, CPP_COMMA)) 4316 c_parser_consume_token (parser); 4317 else 4318 break; 4319 } 4320 return list; 4321} 4322 4323/* Parse asm clobbers, a GNU extension. 4324 4325 asm-clobbers: 4326 asm-string-literal 4327 asm-clobbers , asm-string-literal 4328*/ 4329 4330static tree 4331c_parser_asm_clobbers (c_parser *parser) 4332{ 4333 tree list = NULL_TREE; 4334 while (true) 4335 { 4336 tree str = c_parser_asm_string_literal (parser); 4337 if (str) 4338 list = tree_cons (NULL_TREE, str, list); 4339 else 4340 return NULL_TREE; 4341 if (c_parser_next_token_is (parser, CPP_COMMA)) 4342 c_parser_consume_token (parser); 4343 else 4344 break; 4345 } 4346 return list; 4347} 4348 4349/* Parse an expression other than a compound expression; that is, an 4350 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not 4351 NULL then it is an Objective-C message expression which is the 4352 primary-expression starting the expression as an initializer. 4353 4354 assignment-expression: 4355 conditional-expression 4356 unary-expression assignment-operator assignment-expression 4357 4358 assignment-operator: one of 4359 = *= /= %= += -= <<= >>= &= ^= |= 4360 4361 In GNU C we accept any conditional expression on the LHS and 4362 diagnose the invalid lvalue rather than producing a syntax 4363 error. */ 4364 4365static struct c_expr 4366c_parser_expr_no_commas (c_parser *parser, struct c_expr *after) 4367{ 4368 struct c_expr lhs, rhs, ret; 4369 enum tree_code code; 4370 gcc_assert (!after || c_dialect_objc ()); 4371 lhs = c_parser_conditional_expression (parser, after); 4372 switch (c_parser_peek_token (parser)->type) 4373 { 4374 case CPP_EQ: 4375 code = NOP_EXPR; 4376 break; 4377 case CPP_MULT_EQ: 4378 code = MULT_EXPR; 4379 break; 4380 case CPP_DIV_EQ: 4381 code = TRUNC_DIV_EXPR; 4382 break; 4383 case CPP_MOD_EQ: 4384 code = TRUNC_MOD_EXPR; 4385 break; 4386 case CPP_PLUS_EQ: 4387 code = PLUS_EXPR; 4388 break; 4389 case CPP_MINUS_EQ: 4390 code = MINUS_EXPR; 4391 break; 4392 case CPP_LSHIFT_EQ: 4393 code = LSHIFT_EXPR; 4394 break; 4395 case CPP_RSHIFT_EQ: 4396 code = RSHIFT_EXPR; 4397 break; 4398 case CPP_AND_EQ: 4399 code = BIT_AND_EXPR; 4400 break; 4401 case CPP_XOR_EQ: 4402 code = BIT_XOR_EXPR; 4403 break; 4404 case CPP_OR_EQ: 4405 code = BIT_IOR_EXPR; 4406 break; 4407 default: 4408 return lhs; 4409 } 4410 c_parser_consume_token (parser); 4411 rhs = c_parser_expr_no_commas (parser, NULL); 4412 rhs = default_function_array_conversion (rhs); 4413 ret.value = build_modify_expr (lhs.value, code, rhs.value); 4414 if (code == NOP_EXPR) 4415 ret.original_code = MODIFY_EXPR; 4416 else 4417 { 4418 TREE_NO_WARNING (ret.value) = 1; 4419 ret.original_code = ERROR_MARK; 4420 } 4421 return ret; 4422} 4423 4424/* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER 4425 is not NULL then it is an Objective-C message expression which is 4426 the primary-expression starting the expression as an initializer. 4427 4428 conditional-expression: 4429 logical-OR-expression 4430 logical-OR-expression ? expression : conditional-expression 4431 4432 GNU extensions: 4433 4434 conditional-expression: 4435 logical-OR-expression ? : conditional-expression 4436*/ 4437 4438static struct c_expr 4439c_parser_conditional_expression (c_parser *parser, struct c_expr *after) 4440{ 4441 struct c_expr cond, exp1, exp2, ret; 4442 gcc_assert (!after || c_dialect_objc ()); 4443 cond = c_parser_binary_expression (parser, after); 4444 if (c_parser_next_token_is_not (parser, CPP_QUERY)) 4445 return cond; 4446 cond = default_function_array_conversion (cond); 4447 c_parser_consume_token (parser); 4448 if (c_parser_next_token_is (parser, CPP_COLON)) 4449 { 4450 if (pedantic) 4451 pedwarn ("ISO C forbids omitting the middle term of a ?: expression"); 4452 /* Make sure first operand is calculated only once. */ 4453 exp1.value = save_expr (default_conversion (cond.value)); 4454 cond.value = c_objc_common_truthvalue_conversion (exp1.value); 4455 skip_evaluation += cond.value == truthvalue_true_node; 4456 } 4457 else 4458 { 4459 cond.value 4460 = c_objc_common_truthvalue_conversion 4461 (default_conversion (cond.value)); 4462 skip_evaluation += cond.value == truthvalue_false_node; 4463 exp1 = c_parser_expression_conv (parser); 4464 skip_evaluation += ((cond.value == truthvalue_true_node) 4465 - (cond.value == truthvalue_false_node)); 4466 } 4467 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) 4468 { 4469 skip_evaluation -= cond.value == truthvalue_true_node; 4470 ret.value = error_mark_node; 4471 ret.original_code = ERROR_MARK; 4472 return ret; 4473 } 4474 exp2 = c_parser_conditional_expression (parser, NULL); 4475 exp2 = default_function_array_conversion (exp2); 4476 skip_evaluation -= cond.value == truthvalue_true_node; 4477 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value); 4478 ret.original_code = ERROR_MARK; 4479 return ret; 4480} 4481 4482/* Parse a binary expression; that is, a logical-OR-expression (C90 4483 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is 4484 an Objective-C message expression which is the primary-expression 4485 starting the expression as an initializer. 4486 4487 multiplicative-expression: 4488 cast-expression 4489 multiplicative-expression * cast-expression 4490 multiplicative-expression / cast-expression 4491 multiplicative-expression % cast-expression 4492 4493 additive-expression: 4494 multiplicative-expression 4495 additive-expression + multiplicative-expression 4496 additive-expression - multiplicative-expression 4497 4498 shift-expression: 4499 additive-expression 4500 shift-expression << additive-expression 4501 shift-expression >> additive-expression 4502 4503 relational-expression: 4504 shift-expression 4505 relational-expression < shift-expression 4506 relational-expression > shift-expression 4507 relational-expression <= shift-expression 4508 relational-expression >= shift-expression 4509 4510 equality-expression: 4511 relational-expression 4512 equality-expression == relational-expression 4513 equality-expression != relational-expression 4514 4515 AND-expression: 4516 equality-expression 4517 AND-expression & equality-expression 4518 4519 exclusive-OR-expression: 4520 AND-expression 4521 exclusive-OR-expression ^ AND-expression 4522 4523 inclusive-OR-expression: 4524 exclusive-OR-expression 4525 inclusive-OR-expression | exclusive-OR-expression 4526 4527 logical-AND-expression: 4528 inclusive-OR-expression 4529 logical-AND-expression && inclusive-OR-expression 4530 4531 logical-OR-expression: 4532 logical-AND-expression 4533 logical-OR-expression || logical-AND-expression 4534*/ 4535 4536static struct c_expr 4537c_parser_binary_expression (c_parser *parser, struct c_expr *after) 4538{ 4539 /* A binary expression is parsed using operator-precedence parsing, 4540 with the operands being cast expressions. All the binary 4541 operators are left-associative. Thus a binary expression is of 4542 form: 4543 4544 E0 op1 E1 op2 E2 ... 4545 4546 which we represent on a stack. On the stack, the precedence 4547 levels are strictly increasing. When a new operator is 4548 encountered of higher precedence than that at the top of the 4549 stack, it is pushed; its LHS is the top expression, and its RHS 4550 is everything parsed until it is popped. When a new operator is 4551 encountered with precedence less than or equal to that at the top 4552 of the stack, triples E[i-1] op[i] E[i] are popped and replaced 4553 by the result of the operation until the operator at the top of 4554 the stack has lower precedence than the new operator or there is 4555 only one element on the stack; then the top expression is the LHS 4556 of the new operator. In the case of logical AND and OR 4557 expressions, we also need to adjust skip_evaluation as 4558 appropriate when the operators are pushed and popped. */ 4559 4560 /* The precedence levels, where 0 is a dummy lowest level used for 4561 the bottom of the stack. */ 4562 enum prec { 4563 PREC_NONE, 4564 PREC_LOGOR, 4565 PREC_LOGAND, 4566 PREC_BITOR, 4567 PREC_BITXOR, 4568 PREC_BITAND, 4569 PREC_EQ, 4570 PREC_REL, 4571 PREC_SHIFT, 4572 PREC_ADD, 4573 PREC_MULT, 4574 NUM_PRECS 4575 }; 4576 struct { 4577 /* The expression at this stack level. */ 4578 struct c_expr expr; 4579 /* The precedence of the operator on its left, PREC_NONE at the 4580 bottom of the stack. */ 4581 enum prec prec; 4582 /* The operation on its left. */ 4583 enum tree_code op; 4584 } stack[NUM_PRECS]; 4585 int sp; 4586#define POP \ 4587 do { \ 4588 switch (stack[sp].op) \ 4589 { \ 4590 case TRUTH_ANDIF_EXPR: \ 4591 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \ 4592 break; \ 4593 case TRUTH_ORIF_EXPR: \ 4594 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \ 4595 break; \ 4596 default: \ 4597 break; \ 4598 } \ 4599 stack[sp - 1].expr \ 4600 = default_function_array_conversion (stack[sp - 1].expr); \ 4601 stack[sp].expr \ 4602 = default_function_array_conversion (stack[sp].expr); \ 4603 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \ 4604 stack[sp - 1].expr, \ 4605 stack[sp].expr); \ 4606 sp--; \ 4607 } while (0) 4608 gcc_assert (!after || c_dialect_objc ()); 4609 stack[0].expr = c_parser_cast_expression (parser, after); 4610 stack[0].prec = PREC_NONE; 4611 sp = 0; 4612 while (true) 4613 { 4614 enum prec oprec; 4615 enum tree_code ocode; 4616 if (parser->error) 4617 goto out; 4618 switch (c_parser_peek_token (parser)->type) 4619 { 4620 case CPP_MULT: 4621 oprec = PREC_MULT; 4622 ocode = MULT_EXPR; 4623 break; 4624 case CPP_DIV: 4625 oprec = PREC_MULT; 4626 ocode = TRUNC_DIV_EXPR; 4627 break; 4628 case CPP_MOD: 4629 oprec = PREC_MULT; 4630 ocode = TRUNC_MOD_EXPR; 4631 break; 4632 case CPP_PLUS: 4633 oprec = PREC_ADD; 4634 ocode = PLUS_EXPR; 4635 break; 4636 case CPP_MINUS: 4637 oprec = PREC_ADD; 4638 ocode = MINUS_EXPR; 4639 break; 4640 case CPP_LSHIFT: 4641 oprec = PREC_SHIFT; 4642 ocode = LSHIFT_EXPR; 4643 break; 4644 case CPP_RSHIFT: 4645 oprec = PREC_SHIFT; 4646 ocode = RSHIFT_EXPR; 4647 break; 4648 case CPP_LESS: 4649 oprec = PREC_REL; 4650 ocode = LT_EXPR; 4651 break; 4652 case CPP_GREATER: 4653 oprec = PREC_REL; 4654 ocode = GT_EXPR; 4655 break; 4656 case CPP_LESS_EQ: 4657 oprec = PREC_REL; 4658 ocode = LE_EXPR; 4659 break; 4660 case CPP_GREATER_EQ: 4661 oprec = PREC_REL; 4662 ocode = GE_EXPR; 4663 break; 4664 case CPP_EQ_EQ: 4665 oprec = PREC_EQ; 4666 ocode = EQ_EXPR; 4667 break; 4668 case CPP_NOT_EQ: 4669 oprec = PREC_EQ; 4670 ocode = NE_EXPR; 4671 break; 4672 case CPP_AND: 4673 oprec = PREC_BITAND; 4674 ocode = BIT_AND_EXPR; 4675 break; 4676 case CPP_XOR: 4677 oprec = PREC_BITXOR; 4678 ocode = BIT_XOR_EXPR; 4679 break; 4680 case CPP_OR: 4681 oprec = PREC_BITOR; 4682 ocode = BIT_IOR_EXPR; 4683 break; 4684 case CPP_AND_AND: 4685 oprec = PREC_LOGAND; 4686 ocode = TRUTH_ANDIF_EXPR; 4687 break; 4688 case CPP_OR_OR: 4689 oprec = PREC_LOGOR; 4690 ocode = TRUTH_ORIF_EXPR; 4691 break; 4692 default: 4693 /* Not a binary operator, so end of the binary 4694 expression. */ 4695 goto out; 4696 } 4697 c_parser_consume_token (parser); 4698 while (oprec <= stack[sp].prec) 4699 POP; 4700 switch (ocode) 4701 { 4702 case TRUTH_ANDIF_EXPR: 4703 stack[sp].expr 4704 = default_function_array_conversion (stack[sp].expr); 4705 stack[sp].expr.value = c_objc_common_truthvalue_conversion 4706 (default_conversion (stack[sp].expr.value)); 4707 skip_evaluation += stack[sp].expr.value == truthvalue_false_node; 4708 break; 4709 case TRUTH_ORIF_EXPR: 4710 stack[sp].expr 4711 = default_function_array_conversion (stack[sp].expr); 4712 stack[sp].expr.value = c_objc_common_truthvalue_conversion 4713 (default_conversion (stack[sp].expr.value)); 4714 skip_evaluation += stack[sp].expr.value == truthvalue_true_node; 4715 break; 4716 default: 4717 break; 4718 } 4719 sp++; 4720 stack[sp].expr = c_parser_cast_expression (parser, NULL); 4721 stack[sp].prec = oprec; 4722 stack[sp].op = ocode; 4723 } 4724 out: 4725 while (sp > 0) 4726 POP; 4727 return stack[0].expr; 4728#undef POP 4729} 4730 4731/* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not 4732 NULL then it is an Objective-C message expression which is the 4733 primary-expression starting the expression as an initializer. 4734 4735 cast-expression: 4736 unary-expression 4737 ( type-name ) unary-expression 4738*/ 4739 4740static struct c_expr 4741c_parser_cast_expression (c_parser *parser, struct c_expr *after) 4742{ 4743 gcc_assert (!after || c_dialect_objc ()); 4744 if (after) 4745 return c_parser_postfix_expression_after_primary (parser, *after); 4746 /* If the expression begins with a parenthesized type name, it may 4747 be either a cast or a compound literal; we need to see whether 4748 the next character is '{' to tell the difference. If not, it is 4749 an unary expression. */ 4750 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) 4751 && c_token_starts_typename (c_parser_peek_2nd_token (parser))) 4752 { 4753 struct c_type_name *type_name; 4754 struct c_expr ret; 4755 struct c_expr expr; 4756 c_parser_consume_token (parser); 4757 type_name = c_parser_type_name (parser); 4758 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 4759 if (type_name == NULL) 4760 { 4761 ret.value = error_mark_node; 4762 ret.original_code = ERROR_MARK; 4763 return ret; 4764 } 4765 4766 /* Save casted types in the function's used types hash table. */ 4767 used_types_insert (type_name->specs->type); 4768 4769 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 4770 return c_parser_postfix_expression_after_paren_type (parser, 4771 type_name); 4772 expr = c_parser_cast_expression (parser, NULL); 4773 expr = default_function_array_conversion (expr); 4774 ret.value = c_cast_expr (type_name, expr.value); 4775 ret.original_code = ERROR_MARK; 4776 return ret; 4777 } 4778 else 4779 return c_parser_unary_expression (parser); 4780} 4781 4782/* Parse an unary expression (C90 6.3.3, C99 6.5.3). 4783 4784 unary-expression: 4785 postfix-expression 4786 ++ unary-expression 4787 -- unary-expression 4788 unary-operator cast-expression 4789 sizeof unary-expression 4790 sizeof ( type-name ) 4791 4792 unary-operator: one of 4793 & * + - ~ ! 4794 4795 GNU extensions: 4796 4797 unary-expression: 4798 __alignof__ unary-expression 4799 __alignof__ ( type-name ) 4800 && identifier 4801 4802 unary-operator: one of 4803 __extension__ __real__ __imag__ 4804 4805 In addition, the GNU syntax treats ++ and -- as unary operators, so 4806 they may be applied to cast expressions with errors for non-lvalues 4807 given later. */ 4808 4809static struct c_expr 4810c_parser_unary_expression (c_parser *parser) 4811{ 4812 int ext; 4813 struct c_expr ret, op; 4814 switch (c_parser_peek_token (parser)->type) 4815 { 4816 case CPP_PLUS_PLUS: 4817 c_parser_consume_token (parser); 4818 op = c_parser_cast_expression (parser, NULL); 4819 op = default_function_array_conversion (op); 4820 return parser_build_unary_op (PREINCREMENT_EXPR, op); 4821 case CPP_MINUS_MINUS: 4822 c_parser_consume_token (parser); 4823 op = c_parser_cast_expression (parser, NULL); 4824 op = default_function_array_conversion (op); 4825 return parser_build_unary_op (PREDECREMENT_EXPR, op); 4826 case CPP_AND: 4827 c_parser_consume_token (parser); 4828 return parser_build_unary_op (ADDR_EXPR, 4829 c_parser_cast_expression (parser, NULL)); 4830 case CPP_MULT: 4831 c_parser_consume_token (parser); 4832 op = c_parser_cast_expression (parser, NULL); 4833 op = default_function_array_conversion (op); 4834 ret.value = build_indirect_ref (op.value, "unary *"); 4835 ret.original_code = ERROR_MARK; 4836 return ret; 4837 case CPP_PLUS: 4838 c_parser_consume_token (parser); 4839 if (!c_dialect_objc () && !in_system_header) 4840 warning (OPT_Wtraditional, 4841 "traditional C rejects the unary plus operator"); 4842 op = c_parser_cast_expression (parser, NULL); 4843 op = default_function_array_conversion (op); 4844 return parser_build_unary_op (CONVERT_EXPR, op); 4845 case CPP_MINUS: 4846 c_parser_consume_token (parser); 4847 op = c_parser_cast_expression (parser, NULL); 4848 op = default_function_array_conversion (op); 4849 return parser_build_unary_op (NEGATE_EXPR, op); 4850 case CPP_COMPL: 4851 c_parser_consume_token (parser); 4852 op = c_parser_cast_expression (parser, NULL); 4853 op = default_function_array_conversion (op); 4854 return parser_build_unary_op (BIT_NOT_EXPR, op); 4855 case CPP_NOT: 4856 c_parser_consume_token (parser); 4857 op = c_parser_cast_expression (parser, NULL); 4858 op = default_function_array_conversion (op); 4859 return parser_build_unary_op (TRUTH_NOT_EXPR, op); 4860 case CPP_AND_AND: 4861 /* Refer to the address of a label as a pointer. */ 4862 c_parser_consume_token (parser); 4863 if (c_parser_next_token_is (parser, CPP_NAME)) 4864 { 4865 ret.value = finish_label_address_expr 4866 (c_parser_peek_token (parser)->value); 4867 c_parser_consume_token (parser); 4868 } 4869 else 4870 { 4871 c_parser_error (parser, "expected identifier"); 4872 ret.value = error_mark_node; 4873 } 4874 ret.original_code = ERROR_MARK; 4875 return ret; 4876 case CPP_KEYWORD: 4877 switch (c_parser_peek_token (parser)->keyword) 4878 { 4879 case RID_SIZEOF: 4880 return c_parser_sizeof_expression (parser); 4881 case RID_ALIGNOF: 4882 return c_parser_alignof_expression (parser); 4883 case RID_EXTENSION: 4884 c_parser_consume_token (parser); 4885 ext = disable_extension_diagnostics (); 4886 ret = c_parser_cast_expression (parser, NULL); 4887 restore_extension_diagnostics (ext); 4888 return ret; 4889 case RID_REALPART: 4890 c_parser_consume_token (parser); 4891 op = c_parser_cast_expression (parser, NULL); 4892 op = default_function_array_conversion (op); 4893 return parser_build_unary_op (REALPART_EXPR, op); 4894 case RID_IMAGPART: 4895 c_parser_consume_token (parser); 4896 op = c_parser_cast_expression (parser, NULL); 4897 op = default_function_array_conversion (op); 4898 return parser_build_unary_op (IMAGPART_EXPR, op); 4899 default: 4900 return c_parser_postfix_expression (parser); 4901 } 4902 default: 4903 return c_parser_postfix_expression (parser); 4904 } 4905} 4906 4907/* Parse a sizeof expression. */ 4908 4909static struct c_expr 4910c_parser_sizeof_expression (c_parser *parser) 4911{ 4912 struct c_expr expr; 4913 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF)); 4914 c_parser_consume_token (parser); 4915 skip_evaluation++; 4916 in_sizeof++; 4917 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) 4918 && c_token_starts_typename (c_parser_peek_2nd_token (parser))) 4919 { 4920 /* Either sizeof ( type-name ) or sizeof unary-expression 4921 starting with a compound literal. */ 4922 struct c_type_name *type_name; 4923 c_parser_consume_token (parser); 4924 type_name = c_parser_type_name (parser); 4925 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 4926 if (type_name == NULL) 4927 { 4928 struct c_expr ret; 4929 skip_evaluation--; 4930 in_sizeof--; 4931 ret.value = error_mark_node; 4932 ret.original_code = ERROR_MARK; 4933 return ret; 4934 } 4935 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 4936 { 4937 expr = c_parser_postfix_expression_after_paren_type (parser, 4938 type_name); 4939 goto sizeof_expr; 4940 } 4941 /* sizeof ( type-name ). */ 4942 skip_evaluation--; 4943 in_sizeof--; 4944 if (type_name->declarator->kind == cdk_array 4945 && type_name->declarator->u.array.vla_unspec_p) 4946 { 4947 /* C99 6.7.5.2p4 */ 4948 error ("%<[*]%> not allowed in other than a declaration"); 4949 } 4950 return c_expr_sizeof_type (type_name); 4951 } 4952 else 4953 { 4954 expr = c_parser_unary_expression (parser); 4955 sizeof_expr: 4956 skip_evaluation--; 4957 in_sizeof--; 4958 if (TREE_CODE (expr.value) == COMPONENT_REF 4959 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) 4960 error ("%<sizeof%> applied to a bit-field"); 4961 return c_expr_sizeof_expr (expr); 4962 } 4963} 4964 4965/* Parse an alignof expression. */ 4966 4967static struct c_expr 4968c_parser_alignof_expression (c_parser *parser) 4969{ 4970 struct c_expr expr; 4971 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF)); 4972 c_parser_consume_token (parser); 4973 skip_evaluation++; 4974 in_alignof++; 4975 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) 4976 && c_token_starts_typename (c_parser_peek_2nd_token (parser))) 4977 { 4978 /* Either __alignof__ ( type-name ) or __alignof__ 4979 unary-expression starting with a compound literal. */ 4980 struct c_type_name *type_name; 4981 struct c_expr ret; 4982 c_parser_consume_token (parser); 4983 type_name = c_parser_type_name (parser); 4984 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 4985 if (type_name == NULL) 4986 { 4987 struct c_expr ret; 4988 skip_evaluation--; 4989 in_alignof--; 4990 ret.value = error_mark_node; 4991 ret.original_code = ERROR_MARK; 4992 return ret; 4993 } 4994 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 4995 { 4996 expr = c_parser_postfix_expression_after_paren_type (parser, 4997 type_name); 4998 goto alignof_expr; 4999 } 5000 /* alignof ( type-name ). */ 5001 skip_evaluation--; 5002 in_alignof--; 5003 ret.value = c_alignof (groktypename (type_name)); 5004 ret.original_code = ERROR_MARK; 5005 return ret; 5006 } 5007 else 5008 { 5009 struct c_expr ret; 5010 expr = c_parser_unary_expression (parser); 5011 alignof_expr: 5012 skip_evaluation--; 5013 in_alignof--; 5014 ret.value = c_alignof_expr (expr.value); 5015 ret.original_code = ERROR_MARK; 5016 return ret; 5017 } 5018} 5019 5020/* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2). 5021 5022 postfix-expression: 5023 primary-expression 5024 postfix-expression [ expression ] 5025 postfix-expression ( argument-expression-list[opt] ) 5026 postfix-expression . identifier 5027 postfix-expression -> identifier 5028 postfix-expression ++ 5029 postfix-expression -- 5030 ( type-name ) { initializer-list } 5031 ( type-name ) { initializer-list , } 5032 5033 argument-expression-list: 5034 argument-expression 5035 argument-expression-list , argument-expression 5036 5037 primary-expression: 5038 identifier 5039 constant 5040 string-literal 5041 ( expression ) 5042 5043 GNU extensions: 5044 5045 primary-expression: 5046 __func__ 5047 (treated as a keyword in GNU C) 5048 __FUNCTION__ 5049 __PRETTY_FUNCTION__ 5050 ( compound-statement ) 5051 __builtin_va_arg ( assignment-expression , type-name ) 5052 __builtin_offsetof ( type-name , offsetof-member-designator ) 5053 __builtin_choose_expr ( assignment-expression , 5054 assignment-expression , 5055 assignment-expression ) 5056 __builtin_types_compatible_p ( type-name , type-name ) 5057 5058 offsetof-member-designator: 5059 identifier 5060 offsetof-member-designator . identifier 5061 offsetof-member-designator [ expression ] 5062 5063 Objective-C: 5064 5065 primary-expression: 5066 [ objc-receiver objc-message-args ] 5067 @selector ( objc-selector-arg ) 5068 @protocol ( identifier ) 5069 @encode ( type-name ) 5070 objc-string-literal 5071*/ 5072 5073static struct c_expr 5074c_parser_postfix_expression (c_parser *parser) 5075{ 5076 struct c_expr expr, e1, e2, e3; 5077 struct c_type_name *t1, *t2; 5078 switch (c_parser_peek_token (parser)->type) 5079 { 5080 case CPP_NUMBER: 5081 case CPP_CHAR: 5082 case CPP_WCHAR: 5083 expr.value = c_parser_peek_token (parser)->value; 5084 expr.original_code = ERROR_MARK; 5085 c_parser_consume_token (parser); 5086 break; 5087 case CPP_STRING: 5088 case CPP_WSTRING: 5089 expr.value = c_parser_peek_token (parser)->value; 5090 expr.original_code = STRING_CST; 5091 c_parser_consume_token (parser); 5092 break; 5093 case CPP_OBJC_STRING: 5094 gcc_assert (c_dialect_objc ()); 5095 expr.value 5096 = objc_build_string_object (c_parser_peek_token (parser)->value); 5097 expr.original_code = ERROR_MARK; 5098 c_parser_consume_token (parser); 5099 break; 5100 case CPP_NAME: 5101 if (c_parser_peek_token (parser)->id_kind != C_ID_ID) 5102 { 5103 c_parser_error (parser, "expected expression"); 5104 expr.value = error_mark_node; 5105 expr.original_code = ERROR_MARK; 5106 break; 5107 } 5108 { 5109 tree id = c_parser_peek_token (parser)->value; 5110 location_t loc = c_parser_peek_token (parser)->location; 5111 c_parser_consume_token (parser); 5112 expr.value = build_external_ref (id, 5113 (c_parser_peek_token (parser)->type 5114 == CPP_OPEN_PAREN), loc); 5115 expr.original_code = ERROR_MARK; 5116 } 5117 break; 5118 case CPP_OPEN_PAREN: 5119 /* A parenthesized expression, statement expression or compound 5120 literal. */ 5121 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE) 5122 { 5123 /* A statement expression. */ 5124 tree stmt; 5125 c_parser_consume_token (parser); 5126 c_parser_consume_token (parser); 5127 if (cur_stmt_list == NULL) 5128 { 5129 error ("braced-group within expression allowed " 5130 "only inside a function"); 5131 parser->error = true; 5132 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); 5133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5134 expr.value = error_mark_node; 5135 expr.original_code = ERROR_MARK; 5136 break; 5137 } 5138 stmt = c_begin_stmt_expr (); 5139 c_parser_compound_statement_nostart (parser); 5140 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5141 "expected %<)%>"); 5142 if (pedantic) 5143 pedwarn ("ISO C forbids braced-groups within expressions"); 5144 expr.value = c_finish_stmt_expr (stmt); 5145 expr.original_code = ERROR_MARK; 5146 } 5147 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser))) 5148 { 5149 /* A compound literal. ??? Can we actually get here rather 5150 than going directly to 5151 c_parser_postfix_expression_after_paren_type from 5152 elsewhere? */ 5153 struct c_type_name *type_name; 5154 c_parser_consume_token (parser); 5155 type_name = c_parser_type_name (parser); 5156 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5157 "expected %<)%>"); 5158 if (type_name == NULL) 5159 { 5160 expr.value = error_mark_node; 5161 expr.original_code = ERROR_MARK; 5162 } 5163 else 5164 expr = c_parser_postfix_expression_after_paren_type (parser, 5165 type_name); 5166 } 5167 else 5168 { 5169 /* A parenthesized expression. */ 5170 c_parser_consume_token (parser); 5171 expr = c_parser_expression (parser); 5172 if (TREE_CODE (expr.value) == MODIFY_EXPR) 5173 TREE_NO_WARNING (expr.value) = 1; 5174 expr.original_code = ERROR_MARK; 5175 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5176 "expected %<)%>"); 5177 } 5178 break; 5179 case CPP_KEYWORD: 5180 switch (c_parser_peek_token (parser)->keyword) 5181 { 5182 case RID_FUNCTION_NAME: 5183 case RID_PRETTY_FUNCTION_NAME: 5184 case RID_C99_FUNCTION_NAME: 5185 expr.value = fname_decl (c_parser_peek_token (parser)->keyword, 5186 c_parser_peek_token (parser)->value); 5187 expr.original_code = ERROR_MARK; 5188 c_parser_consume_token (parser); 5189 break; 5190 case RID_VA_ARG: 5191 c_parser_consume_token (parser); 5192 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5193 { 5194 expr.value = error_mark_node; 5195 expr.original_code = ERROR_MARK; 5196 break; 5197 } 5198 e1 = c_parser_expr_no_commas (parser, NULL); 5199 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5200 { 5201 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5202 expr.value = error_mark_node; 5203 expr.original_code = ERROR_MARK; 5204 break; 5205 } 5206 t1 = c_parser_type_name (parser); 5207 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5208 "expected %<)%>"); 5209 if (t1 == NULL) 5210 { 5211 expr.value = error_mark_node; 5212 expr.original_code = ERROR_MARK; 5213 } 5214 else 5215 { 5216 expr.value = build_va_arg (e1.value, groktypename (t1)); 5217 expr.original_code = ERROR_MARK; 5218 } 5219 break; 5220 case RID_OFFSETOF: 5221 c_parser_consume_token (parser); 5222 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5223 { 5224 expr.value = error_mark_node; 5225 expr.original_code = ERROR_MARK; 5226 break; 5227 } 5228 t1 = c_parser_type_name (parser); 5229 if (t1 == NULL) 5230 { 5231 expr.value = error_mark_node; 5232 expr.original_code = ERROR_MARK; 5233 break; 5234 } 5235 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5236 { 5237 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5238 expr.value = error_mark_node; 5239 expr.original_code = ERROR_MARK; 5240 break; 5241 } 5242 { 5243 tree type = groktypename (t1); 5244 tree offsetof_ref; 5245 if (type == error_mark_node) 5246 offsetof_ref = error_mark_node; 5247 else 5248 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node); 5249 /* Parse the second argument to __builtin_offsetof. We 5250 must have one identifier, and beyond that we want to 5251 accept sub structure and sub array references. */ 5252 if (c_parser_next_token_is (parser, CPP_NAME)) 5253 { 5254 offsetof_ref = build_component_ref 5255 (offsetof_ref, c_parser_peek_token (parser)->value); 5256 c_parser_consume_token (parser); 5257 while (c_parser_next_token_is (parser, CPP_DOT) 5258 || c_parser_next_token_is (parser, 5259 CPP_OPEN_SQUARE)) 5260 { 5261 if (c_parser_next_token_is (parser, CPP_DOT)) 5262 { 5263 c_parser_consume_token (parser); 5264 if (c_parser_next_token_is_not (parser, 5265 CPP_NAME)) 5266 { 5267 c_parser_error (parser, "expected identifier"); 5268 break; 5269 } 5270 offsetof_ref = build_component_ref 5271 (offsetof_ref, 5272 c_parser_peek_token (parser)->value); 5273 c_parser_consume_token (parser); 5274 } 5275 else 5276 { 5277 tree idx; 5278 c_parser_consume_token (parser); 5279 idx = c_parser_expression (parser).value; 5280 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 5281 "expected %<]%>"); 5282 offsetof_ref = build_array_ref (offsetof_ref, idx); 5283 } 5284 } 5285 } 5286 else 5287 c_parser_error (parser, "expected identifier"); 5288 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5289 "expected %<)%>"); 5290 expr.value = fold_offsetof (offsetof_ref, NULL_TREE); 5291 expr.original_code = ERROR_MARK; 5292 } 5293 break; 5294 case RID_CHOOSE_EXPR: 5295 c_parser_consume_token (parser); 5296 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5297 { 5298 expr.value = error_mark_node; 5299 expr.original_code = ERROR_MARK; 5300 break; 5301 } 5302 e1 = c_parser_expr_no_commas (parser, NULL); 5303 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5304 { 5305 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5306 expr.value = error_mark_node; 5307 expr.original_code = ERROR_MARK; 5308 break; 5309 } 5310 e2 = c_parser_expr_no_commas (parser, NULL); 5311 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5312 { 5313 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5314 expr.value = error_mark_node; 5315 expr.original_code = ERROR_MARK; 5316 break; 5317 } 5318 e3 = c_parser_expr_no_commas (parser, NULL); 5319 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5320 "expected %<)%>"); 5321 { 5322 tree c; 5323 5324 c = fold (e1.value); 5325 if (TREE_CODE (c) != INTEGER_CST) 5326 error ("first argument to %<__builtin_choose_expr%> not" 5327 " a constant"); 5328 expr = integer_zerop (c) ? e3 : e2; 5329 } 5330 break; 5331 case RID_TYPES_COMPATIBLE_P: 5332 c_parser_consume_token (parser); 5333 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5334 { 5335 expr.value = error_mark_node; 5336 expr.original_code = ERROR_MARK; 5337 break; 5338 } 5339 t1 = c_parser_type_name (parser); 5340 if (t1 == NULL) 5341 { 5342 expr.value = error_mark_node; 5343 expr.original_code = ERROR_MARK; 5344 break; 5345 } 5346 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5347 { 5348 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5349 expr.value = error_mark_node; 5350 expr.original_code = ERROR_MARK; 5351 break; 5352 } 5353 t2 = c_parser_type_name (parser); 5354 if (t2 == NULL) 5355 { 5356 expr.value = error_mark_node; 5357 expr.original_code = ERROR_MARK; 5358 break; 5359 } 5360 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5361 "expected %<)%>"); 5362 { 5363 tree e1, e2; 5364 5365 e1 = TYPE_MAIN_VARIANT (groktypename (t1)); 5366 e2 = TYPE_MAIN_VARIANT (groktypename (t2)); 5367 5368 expr.value = comptypes (e1, e2) 5369 ? build_int_cst (NULL_TREE, 1) 5370 : build_int_cst (NULL_TREE, 0); 5371 expr.original_code = ERROR_MARK; 5372 } 5373 break; 5374 case RID_AT_SELECTOR: 5375 gcc_assert (c_dialect_objc ()); 5376 c_parser_consume_token (parser); 5377 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5378 { 5379 expr.value = error_mark_node; 5380 expr.original_code = ERROR_MARK; 5381 break; 5382 } 5383 { 5384 tree sel = c_parser_objc_selector_arg (parser); 5385 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5386 "expected %<)%>"); 5387 expr.value = objc_build_selector_expr (sel); 5388 expr.original_code = ERROR_MARK; 5389 } 5390 break; 5391 case RID_AT_PROTOCOL: 5392 gcc_assert (c_dialect_objc ()); 5393 c_parser_consume_token (parser); 5394 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5395 { 5396 expr.value = error_mark_node; 5397 expr.original_code = ERROR_MARK; 5398 break; 5399 } 5400 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5401 { 5402 c_parser_error (parser, "expected identifier"); 5403 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5404 expr.value = error_mark_node; 5405 expr.original_code = ERROR_MARK; 5406 break; 5407 } 5408 { 5409 tree id = c_parser_peek_token (parser)->value; 5410 c_parser_consume_token (parser); 5411 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5412 "expected %<)%>"); 5413 expr.value = objc_build_protocol_expr (id); 5414 expr.original_code = ERROR_MARK; 5415 } 5416 break; 5417 case RID_AT_ENCODE: 5418 /* Extension to support C-structures in the archiver. */ 5419 gcc_assert (c_dialect_objc ()); 5420 c_parser_consume_token (parser); 5421 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5422 { 5423 expr.value = error_mark_node; 5424 expr.original_code = ERROR_MARK; 5425 break; 5426 } 5427 t1 = c_parser_type_name (parser); 5428 if (t1 == NULL) 5429 { 5430 expr.value = error_mark_node; 5431 expr.original_code = ERROR_MARK; 5432 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5433 break; 5434 } 5435 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5436 "expected %<)%>"); 5437 { 5438 tree type = groktypename (t1); 5439 expr.value = objc_build_encode_expr (type); 5440 expr.original_code = ERROR_MARK; 5441 } 5442 break; 5443 default: 5444 c_parser_error (parser, "expected expression"); 5445 expr.value = error_mark_node; 5446 expr.original_code = ERROR_MARK; 5447 break; 5448 } 5449 break; 5450 case CPP_OPEN_SQUARE: 5451 if (c_dialect_objc ()) 5452 { 5453 tree receiver, args; 5454 c_parser_consume_token (parser); 5455 receiver = c_parser_objc_receiver (parser); 5456 args = c_parser_objc_message_args (parser); 5457 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 5458 "expected %<]%>"); 5459 expr.value = objc_build_message_expr (build_tree_list (receiver, 5460 args)); 5461 expr.original_code = ERROR_MARK; 5462 break; 5463 } 5464 /* Else fall through to report error. */ 5465 default: 5466 c_parser_error (parser, "expected expression"); 5467 expr.value = error_mark_node; 5468 expr.original_code = ERROR_MARK; 5469 break; 5470 } 5471 return c_parser_postfix_expression_after_primary (parser, expr); 5472} 5473 5474/* Parse a postfix expression after a parenthesized type name: the 5475 brace-enclosed initializer of a compound literal, possibly followed 5476 by some postfix operators. This is separate because it is not 5477 possible to tell until after the type name whether a cast 5478 expression has a cast or a compound literal, or whether the operand 5479 of sizeof is a parenthesized type name or starts with a compound 5480 literal. */ 5481 5482static struct c_expr 5483c_parser_postfix_expression_after_paren_type (c_parser *parser, 5484 struct c_type_name *type_name) 5485{ 5486 tree type; 5487 struct c_expr init; 5488 struct c_expr expr; 5489 start_init (NULL_TREE, NULL, 0); 5490 type = groktypename (type_name); 5491 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type)) 5492 { 5493 error ("compound literal has variable size"); 5494 type = error_mark_node; 5495 } 5496 init = c_parser_braced_init (parser, type, false); 5497 finish_init (); 5498 maybe_warn_string_init (type, init); 5499 5500 if (pedantic && !flag_isoc99) 5501 pedwarn ("ISO C90 forbids compound literals"); 5502 expr.value = build_compound_literal (type, init.value); 5503 expr.original_code = ERROR_MARK; 5504 return c_parser_postfix_expression_after_primary (parser, expr); 5505} 5506 5507/* Parse a postfix expression after the initial primary or compound 5508 literal; that is, parse a series of postfix operators. */ 5509 5510static struct c_expr 5511c_parser_postfix_expression_after_primary (c_parser *parser, 5512 struct c_expr expr) 5513{ 5514 tree ident, idx, exprlist; 5515 while (true) 5516 { 5517 switch (c_parser_peek_token (parser)->type) 5518 { 5519 case CPP_OPEN_SQUARE: 5520 /* Array reference. */ 5521 c_parser_consume_token (parser); 5522 idx = c_parser_expression (parser).value; 5523 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 5524 "expected %<]%>"); 5525 expr.value = build_array_ref (expr.value, idx); 5526 expr.original_code = ERROR_MARK; 5527 break; 5528 case CPP_OPEN_PAREN: 5529 /* Function call. */ 5530 c_parser_consume_token (parser); 5531 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 5532 exprlist = NULL_TREE; 5533 else 5534 exprlist = c_parser_expr_list (parser, true); 5535 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5536 "expected %<)%>"); 5537 expr.value = build_function_call (expr.value, exprlist); 5538 expr.original_code = ERROR_MARK; 5539 break; 5540 case CPP_DOT: 5541 /* Structure element reference. */ 5542 c_parser_consume_token (parser); 5543 expr = default_function_array_conversion (expr); 5544 if (c_parser_next_token_is (parser, CPP_NAME)) 5545 ident = c_parser_peek_token (parser)->value; 5546 else 5547 { 5548 c_parser_error (parser, "expected identifier"); 5549 expr.value = error_mark_node; 5550 expr.original_code = ERROR_MARK; 5551 return expr; 5552 } 5553 c_parser_consume_token (parser); 5554 expr.value = build_component_ref (expr.value, ident); 5555 expr.original_code = ERROR_MARK; 5556 break; 5557 case CPP_DEREF: 5558 /* Structure element reference. */ 5559 c_parser_consume_token (parser); 5560 expr = default_function_array_conversion (expr); 5561 if (c_parser_next_token_is (parser, CPP_NAME)) 5562 ident = c_parser_peek_token (parser)->value; 5563 else 5564 { 5565 c_parser_error (parser, "expected identifier"); 5566 expr.value = error_mark_node; 5567 expr.original_code = ERROR_MARK; 5568 return expr; 5569 } 5570 c_parser_consume_token (parser); 5571 expr.value = build_component_ref (build_indirect_ref (expr.value, 5572 "->"), ident); 5573 expr.original_code = ERROR_MARK; 5574 break; 5575 case CPP_PLUS_PLUS: 5576 /* Postincrement. */ 5577 c_parser_consume_token (parser); 5578 expr = default_function_array_conversion (expr); 5579 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0); 5580 expr.original_code = ERROR_MARK; 5581 break; 5582 case CPP_MINUS_MINUS: 5583 /* Postdecrement. */ 5584 c_parser_consume_token (parser); 5585 expr = default_function_array_conversion (expr); 5586 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0); 5587 expr.original_code = ERROR_MARK; 5588 break; 5589 default: 5590 return expr; 5591 } 5592 } 5593} 5594 5595/* Parse an expression (C90 6.3.17, C99 6.5.17). 5596 5597 expression: 5598 assignment-expression 5599 expression , assignment-expression 5600*/ 5601 5602static struct c_expr 5603c_parser_expression (c_parser *parser) 5604{ 5605 struct c_expr expr; 5606 expr = c_parser_expr_no_commas (parser, NULL); 5607 while (c_parser_next_token_is (parser, CPP_COMMA)) 5608 { 5609 struct c_expr next; 5610 c_parser_consume_token (parser); 5611 next = c_parser_expr_no_commas (parser, NULL); 5612 next = default_function_array_conversion (next); 5613 expr.value = build_compound_expr (expr.value, next.value); 5614 expr.original_code = COMPOUND_EXPR; 5615 } 5616 return expr; 5617} 5618 5619/* Parse an expression and convert functions or arrays to 5620 pointers. */ 5621 5622static struct c_expr 5623c_parser_expression_conv (c_parser *parser) 5624{ 5625 struct c_expr expr; 5626 expr = c_parser_expression (parser); 5627 expr = default_function_array_conversion (expr); 5628 return expr; 5629} 5630 5631/* Parse a non-empty list of expressions. If CONVERT_P, convert 5632 functions and arrays to pointers. 5633 5634 nonempty-expr-list: 5635 assignment-expression 5636 nonempty-expr-list , assignment-expression 5637*/ 5638 5639static tree 5640c_parser_expr_list (c_parser *parser, bool convert_p) 5641{ 5642 struct c_expr expr; 5643 tree ret, cur; 5644 expr = c_parser_expr_no_commas (parser, NULL); 5645 if (convert_p) 5646 expr = default_function_array_conversion (expr); 5647 ret = cur = build_tree_list (NULL_TREE, expr.value); 5648 while (c_parser_next_token_is (parser, CPP_COMMA)) 5649 { 5650 c_parser_consume_token (parser); 5651 expr = c_parser_expr_no_commas (parser, NULL); 5652 if (convert_p) 5653 expr = default_function_array_conversion (expr); 5654 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value); 5655 } 5656 return ret; 5657} 5658 5659 5660/* Parse Objective-C-specific constructs. */ 5661 5662/* Parse an objc-class-definition. 5663 5664 objc-class-definition: 5665 @interface identifier objc-superclass[opt] objc-protocol-refs[opt] 5666 objc-class-instance-variables[opt] objc-methodprotolist @end 5667 @implementation identifier objc-superclass[opt] 5668 objc-class-instance-variables[opt] 5669 @interface identifier ( identifier ) objc-protocol-refs[opt] 5670 objc-methodprotolist @end 5671 @implementation identifier ( identifier ) 5672 5673 objc-superclass: 5674 : identifier 5675 5676 "@interface identifier (" must start "@interface identifier ( 5677 identifier ) ...": objc-methodprotolist in the first production may 5678 not start with a parenthesized identifier as a declarator of a data 5679 definition with no declaration specifiers if the objc-superclass, 5680 objc-protocol-refs and objc-class-instance-variables are omitted. */ 5681 5682static void 5683c_parser_objc_class_definition (c_parser *parser) 5684{ 5685 bool iface_p; 5686 tree id1; 5687 tree superclass; 5688 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE)) 5689 iface_p = true; 5690 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION)) 5691 iface_p = false; 5692 else 5693 gcc_unreachable (); 5694 c_parser_consume_token (parser); 5695 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5696 { 5697 c_parser_error (parser, "expected identifier"); 5698 return; 5699 } 5700 id1 = c_parser_peek_token (parser)->value; 5701 c_parser_consume_token (parser); 5702 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 5703 { 5704 tree id2; 5705 tree proto = NULL_TREE; 5706 c_parser_consume_token (parser); 5707 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5708 { 5709 c_parser_error (parser, "expected identifier"); 5710 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5711 return; 5712 } 5713 id2 = c_parser_peek_token (parser)->value; 5714 c_parser_consume_token (parser); 5715 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 5716 if (!iface_p) 5717 { 5718 objc_start_category_implementation (id1, id2); 5719 return; 5720 } 5721 if (c_parser_next_token_is (parser, CPP_LESS)) 5722 proto = c_parser_objc_protocol_refs (parser); 5723 objc_start_category_interface (id1, id2, proto); 5724 c_parser_objc_methodprotolist (parser); 5725 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); 5726 objc_finish_interface (); 5727 return; 5728 } 5729 if (c_parser_next_token_is (parser, CPP_COLON)) 5730 { 5731 c_parser_consume_token (parser); 5732 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5733 { 5734 c_parser_error (parser, "expected identifier"); 5735 return; 5736 } 5737 superclass = c_parser_peek_token (parser)->value; 5738 c_parser_consume_token (parser); 5739 } 5740 else 5741 superclass = NULL_TREE; 5742 if (iface_p) 5743 { 5744 tree proto = NULL_TREE; 5745 if (c_parser_next_token_is (parser, CPP_LESS)) 5746 proto = c_parser_objc_protocol_refs (parser); 5747 objc_start_class_interface (id1, superclass, proto); 5748 } 5749 else 5750 objc_start_class_implementation (id1, superclass); 5751 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 5752 c_parser_objc_class_instance_variables (parser); 5753 if (iface_p) 5754 { 5755 objc_continue_interface (); 5756 c_parser_objc_methodprotolist (parser); 5757 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); 5758 objc_finish_interface (); 5759 } 5760 else 5761 { 5762 objc_continue_implementation (); 5763 return; 5764 } 5765} 5766 5767/* Parse objc-class-instance-variables. 5768 5769 objc-class-instance-variables: 5770 { objc-instance-variable-decl-list[opt] } 5771 5772 objc-instance-variable-decl-list: 5773 objc-visibility-spec 5774 objc-instance-variable-decl ; 5775 ; 5776 objc-instance-variable-decl-list objc-visibility-spec 5777 objc-instance-variable-decl-list objc-instance-variable-decl ; 5778 objc-instance-variable-decl-list ; 5779 5780 objc-visibility-spec: 5781 @private 5782 @protected 5783 @public 5784 5785 objc-instance-variable-decl: 5786 struct-declaration 5787*/ 5788 5789static void 5790c_parser_objc_class_instance_variables (c_parser *parser) 5791{ 5792 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE)); 5793 c_parser_consume_token (parser); 5794 while (c_parser_next_token_is_not (parser, CPP_EOF)) 5795 { 5796 tree decls; 5797 /* Parse any stray semicolon. */ 5798 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 5799 { 5800 if (pedantic) 5801 pedwarn ("extra semicolon in struct or union specified"); 5802 c_parser_consume_token (parser); 5803 continue; 5804 } 5805 /* Stop if at the end of the instance variables. */ 5806 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 5807 { 5808 c_parser_consume_token (parser); 5809 break; 5810 } 5811 /* Parse any objc-visibility-spec. */ 5812 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE)) 5813 { 5814 c_parser_consume_token (parser); 5815 objc_set_visibility (2); 5816 continue; 5817 } 5818 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED)) 5819 { 5820 c_parser_consume_token (parser); 5821 objc_set_visibility (0); 5822 continue; 5823 } 5824 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC)) 5825 { 5826 c_parser_consume_token (parser); 5827 objc_set_visibility (1); 5828 continue; 5829 } 5830 else if (c_parser_next_token_is (parser, CPP_PRAGMA)) 5831 { 5832 c_parser_pragma (parser, pragma_external); 5833 continue; 5834 } 5835 5836 /* Parse some comma-separated declarations. */ 5837 decls = c_parser_struct_declaration (parser); 5838 { 5839 /* Comma-separated instance variables are chained together in 5840 reverse order; add them one by one. */ 5841 tree ivar = nreverse (decls); 5842 for (; ivar; ivar = TREE_CHAIN (ivar)) 5843 objc_add_instance_variable (copy_node (ivar)); 5844 } 5845 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 5846 } 5847} 5848 5849/* Parse an objc-class-declaration. 5850 5851 objc-class-declaration: 5852 @class identifier-list ; 5853*/ 5854 5855static void 5856c_parser_objc_class_declaration (c_parser *parser) 5857{ 5858 tree list = NULL_TREE; 5859 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS)); 5860 c_parser_consume_token (parser); 5861 /* Any identifiers, including those declared as type names, are OK 5862 here. */ 5863 while (true) 5864 { 5865 tree id; 5866 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5867 { 5868 c_parser_error (parser, "expected identifier"); 5869 break; 5870 } 5871 id = c_parser_peek_token (parser)->value; 5872 list = chainon (list, build_tree_list (NULL_TREE, id)); 5873 c_parser_consume_token (parser); 5874 if (c_parser_next_token_is (parser, CPP_COMMA)) 5875 c_parser_consume_token (parser); 5876 else 5877 break; 5878 } 5879 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 5880 objc_declare_class (list); 5881} 5882 5883/* Parse an objc-alias-declaration. 5884 5885 objc-alias-declaration: 5886 @compatibility_alias identifier identifier ; 5887*/ 5888 5889static void 5890c_parser_objc_alias_declaration (c_parser *parser) 5891{ 5892 tree id1, id2; 5893 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS)); 5894 c_parser_consume_token (parser); 5895 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5896 { 5897 c_parser_error (parser, "expected identifier"); 5898 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL); 5899 return; 5900 } 5901 id1 = c_parser_peek_token (parser)->value; 5902 c_parser_consume_token (parser); 5903 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5904 { 5905 c_parser_error (parser, "expected identifier"); 5906 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL); 5907 return; 5908 } 5909 id2 = c_parser_peek_token (parser)->value; 5910 c_parser_consume_token (parser); 5911 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 5912 objc_declare_alias (id1, id2); 5913} 5914 5915/* Parse an objc-protocol-definition. 5916 5917 objc-protocol-definition: 5918 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end 5919 @protocol identifier-list ; 5920 5921 "@protocol identifier ;" should be resolved as "@protocol 5922 identifier-list ;": objc-methodprotolist may not start with a 5923 semicolon in the first alternative if objc-protocol-refs are 5924 omitted. */ 5925 5926static void 5927c_parser_objc_protocol_definition (c_parser *parser) 5928{ 5929 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL)); 5930 c_parser_consume_token (parser); 5931 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5932 { 5933 c_parser_error (parser, "expected identifier"); 5934 return; 5935 } 5936 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA 5937 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON) 5938 { 5939 tree list = NULL_TREE; 5940 /* Any identifiers, including those declared as type names, are 5941 OK here. */ 5942 while (true) 5943 { 5944 tree id; 5945 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5946 { 5947 c_parser_error (parser, "expected identifier"); 5948 break; 5949 } 5950 id = c_parser_peek_token (parser)->value; 5951 list = chainon (list, build_tree_list (NULL_TREE, id)); 5952 c_parser_consume_token (parser); 5953 if (c_parser_next_token_is (parser, CPP_COMMA)) 5954 c_parser_consume_token (parser); 5955 else 5956 break; 5957 } 5958 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 5959 objc_declare_protocols (list); 5960 } 5961 else 5962 { 5963 tree id = c_parser_peek_token (parser)->value; 5964 tree proto = NULL_TREE; 5965 c_parser_consume_token (parser); 5966 if (c_parser_next_token_is (parser, CPP_LESS)) 5967 proto = c_parser_objc_protocol_refs (parser); 5968 objc_pq_context = 1; 5969 objc_start_protocol (id, proto); 5970 c_parser_objc_methodprotolist (parser); 5971 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); 5972 objc_pq_context = 0; 5973 objc_finish_interface (); 5974 } 5975} 5976 5977/* Parse an objc-method-type. 5978 5979 objc-method-type: 5980 + 5981 - 5982*/ 5983 5984static enum tree_code 5985c_parser_objc_method_type (c_parser *parser) 5986{ 5987 switch (c_parser_peek_token (parser)->type) 5988 { 5989 case CPP_PLUS: 5990 c_parser_consume_token (parser); 5991 return PLUS_EXPR; 5992 case CPP_MINUS: 5993 c_parser_consume_token (parser); 5994 return MINUS_EXPR; 5995 default: 5996 gcc_unreachable (); 5997 } 5998} 5999 6000/* Parse an objc-method-definition. 6001 6002 objc-method-definition: 6003 objc-method-type objc-method-decl ;[opt] compound-statement 6004*/ 6005 6006static void 6007c_parser_objc_method_definition (c_parser *parser) 6008{ 6009 enum tree_code type = c_parser_objc_method_type (parser); 6010 tree decl; 6011 objc_set_method_type (type); 6012 objc_pq_context = 1; 6013 decl = c_parser_objc_method_decl (parser); 6014 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 6015 { 6016 c_parser_consume_token (parser); 6017 if (pedantic) 6018 pedwarn ("extra semicolon in method definition specified"); 6019 } 6020 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 6021 { 6022 c_parser_error (parser, "expected %<{%>"); 6023 return; 6024 } 6025 objc_pq_context = 0; 6026 objc_start_method_definition (decl); 6027 add_stmt (c_parser_compound_statement (parser)); 6028 objc_finish_method_definition (current_function_decl); 6029} 6030 6031/* Parse an objc-methodprotolist. 6032 6033 objc-methodprotolist: 6034 empty 6035 objc-methodprotolist objc-methodproto 6036 objc-methodprotolist declaration 6037 objc-methodprotolist ; 6038 6039 The declaration is a data definition, which may be missing 6040 declaration specifiers under the same rules and diagnostics as 6041 other data definitions outside functions, and the stray semicolon 6042 is diagnosed the same way as a stray semicolon outside a 6043 function. */ 6044 6045static void 6046c_parser_objc_methodprotolist (c_parser *parser) 6047{ 6048 while (true) 6049 { 6050 /* The list is terminated by @end. */ 6051 switch (c_parser_peek_token (parser)->type) 6052 { 6053 case CPP_SEMICOLON: 6054 if (pedantic) 6055 pedwarn ("ISO C does not allow extra %<;%> outside of a function"); 6056 c_parser_consume_token (parser); 6057 break; 6058 case CPP_PLUS: 6059 case CPP_MINUS: 6060 c_parser_objc_methodproto (parser); 6061 break; 6062 case CPP_PRAGMA: 6063 c_parser_pragma (parser, pragma_external); 6064 break; 6065 case CPP_EOF: 6066 return; 6067 default: 6068 if (c_parser_next_token_is_keyword (parser, RID_AT_END)) 6069 return; 6070 c_parser_declaration_or_fndef (parser, false, true, false, true); 6071 break; 6072 } 6073 } 6074} 6075 6076/* Parse an objc-methodproto. 6077 6078 objc-methodproto: 6079 objc-method-type objc-method-decl ; 6080*/ 6081 6082static void 6083c_parser_objc_methodproto (c_parser *parser) 6084{ 6085 enum tree_code type = c_parser_objc_method_type (parser); 6086 tree decl; 6087 objc_set_method_type (type); 6088 /* Remember protocol qualifiers in prototypes. */ 6089 objc_pq_context = 1; 6090 decl = c_parser_objc_method_decl (parser); 6091 /* Forget protocol qualifiers here. */ 6092 objc_pq_context = 0; 6093 objc_add_method_declaration (decl); 6094 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 6095} 6096 6097/* Parse an objc-method-decl. 6098 6099 objc-method-decl: 6100 ( objc-type-name ) objc-selector 6101 objc-selector 6102 ( objc-type-name ) objc-keyword-selector objc-optparmlist 6103 objc-keyword-selector objc-optparmlist 6104 6105 objc-keyword-selector: 6106 objc-keyword-decl 6107 objc-keyword-selector objc-keyword-decl 6108 6109 objc-keyword-decl: 6110 objc-selector : ( objc-type-name ) identifier 6111 objc-selector : identifier 6112 : ( objc-type-name ) identifier 6113 : identifier 6114 6115 objc-optparmlist: 6116 objc-optparms objc-optellipsis 6117 6118 objc-optparms: 6119 empty 6120 objc-opt-parms , parameter-declaration 6121 6122 objc-optellipsis: 6123 empty 6124 , ... 6125*/ 6126 6127static tree 6128c_parser_objc_method_decl (c_parser *parser) 6129{ 6130 tree type = NULL_TREE; 6131 tree sel; 6132 tree parms = NULL_TREE; 6133 bool ellipsis = false; 6134 6135 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 6136 { 6137 c_parser_consume_token (parser); 6138 type = c_parser_objc_type_name (parser); 6139 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6140 } 6141 sel = c_parser_objc_selector (parser); 6142 /* If there is no selector, or a colon follows, we have an 6143 objc-keyword-selector. If there is a selector, and a colon does 6144 not follow, that selector ends the objc-method-decl. */ 6145 if (!sel || c_parser_next_token_is (parser, CPP_COLON)) 6146 { 6147 tree tsel = sel; 6148 tree list = NULL_TREE; 6149 while (true) 6150 { 6151 tree atype = NULL_TREE, id, keyworddecl; 6152 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) 6153 break; 6154 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 6155 { 6156 c_parser_consume_token (parser); 6157 atype = c_parser_objc_type_name (parser); 6158 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 6159 "expected %<)%>"); 6160 } 6161 if (c_parser_next_token_is_not (parser, CPP_NAME)) 6162 { 6163 c_parser_error (parser, "expected identifier"); 6164 return error_mark_node; 6165 } 6166 id = c_parser_peek_token (parser)->value; 6167 c_parser_consume_token (parser); 6168 keyworddecl = objc_build_keyword_decl (tsel, atype, id); 6169 list = chainon (list, keyworddecl); 6170 tsel = c_parser_objc_selector (parser); 6171 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON)) 6172 break; 6173 } 6174 /* Parse the optional parameter list. Optional Objective-C 6175 method parameters follow the C syntax, and may include '...' 6176 to denote a variable number of arguments. */ 6177 parms = make_node (TREE_LIST); 6178 while (c_parser_next_token_is (parser, CPP_COMMA)) 6179 { 6180 struct c_parm *parm; 6181 c_parser_consume_token (parser); 6182 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) 6183 { 6184 ellipsis = true; 6185 c_parser_consume_token (parser); 6186 break; 6187 } 6188 parm = c_parser_parameter_declaration (parser, NULL_TREE); 6189 if (parm == NULL) 6190 break; 6191 parms = chainon (parms, 6192 build_tree_list (NULL_TREE, grokparm (parm))); 6193 } 6194 sel = list; 6195 } 6196 return objc_build_method_signature (type, sel, parms, ellipsis); 6197} 6198 6199/* Parse an objc-type-name. 6200 6201 objc-type-name: 6202 objc-type-qualifiers[opt] type-name 6203 objc-type-qualifiers[opt] 6204 6205 objc-type-qualifiers: 6206 objc-type-qualifier 6207 objc-type-qualifiers objc-type-qualifier 6208 6209 objc-type-qualifier: one of 6210 in out inout bycopy byref oneway 6211*/ 6212 6213static tree 6214c_parser_objc_type_name (c_parser *parser) 6215{ 6216 tree quals = NULL_TREE; 6217 struct c_type_name *typename = NULL; 6218 tree type = NULL_TREE; 6219 while (true) 6220 { 6221 c_token *token = c_parser_peek_token (parser); 6222 if (token->type == CPP_KEYWORD 6223 && (token->keyword == RID_IN 6224 || token->keyword == RID_OUT 6225 || token->keyword == RID_INOUT 6226 || token->keyword == RID_BYCOPY 6227 || token->keyword == RID_BYREF 6228 || token->keyword == RID_ONEWAY)) 6229 { 6230 quals = chainon (quals, build_tree_list (NULL_TREE, token->value)); 6231 c_parser_consume_token (parser); 6232 } 6233 else 6234 break; 6235 } 6236 if (c_parser_next_token_starts_typename (parser)) 6237 typename = c_parser_type_name (parser); 6238 if (typename) 6239 type = groktypename (typename); 6240 return build_tree_list (quals, type); 6241} 6242 6243/* Parse objc-protocol-refs. 6244 6245 objc-protocol-refs: 6246 < identifier-list > 6247*/ 6248 6249static tree 6250c_parser_objc_protocol_refs (c_parser *parser) 6251{ 6252 tree list = NULL_TREE; 6253 gcc_assert (c_parser_next_token_is (parser, CPP_LESS)); 6254 c_parser_consume_token (parser); 6255 /* Any identifiers, including those declared as type names, are OK 6256 here. */ 6257 while (true) 6258 { 6259 tree id; 6260 if (c_parser_next_token_is_not (parser, CPP_NAME)) 6261 { 6262 c_parser_error (parser, "expected identifier"); 6263 break; 6264 } 6265 id = c_parser_peek_token (parser)->value; 6266 list = chainon (list, build_tree_list (NULL_TREE, id)); 6267 c_parser_consume_token (parser); 6268 if (c_parser_next_token_is (parser, CPP_COMMA)) 6269 c_parser_consume_token (parser); 6270 else 6271 break; 6272 } 6273 c_parser_require (parser, CPP_GREATER, "expected %<>%>"); 6274 return list; 6275} 6276 6277/* Parse an objc-try-catch-statement. 6278 6279 objc-try-catch-statement: 6280 @try compound-statement objc-catch-list[opt] 6281 @try compound-statement objc-catch-list[opt] @finally compound-statement 6282 6283 objc-catch-list: 6284 @catch ( parameter-declaration ) compound-statement 6285 objc-catch-list @catch ( parameter-declaration ) compound-statement 6286*/ 6287 6288static void 6289c_parser_objc_try_catch_statement (c_parser *parser) 6290{ 6291 location_t loc; 6292 tree stmt; 6293 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY)); 6294 c_parser_consume_token (parser); 6295 loc = c_parser_peek_token (parser)->location; 6296 stmt = c_parser_compound_statement (parser); 6297 objc_begin_try_stmt (loc, stmt); 6298 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH)) 6299 { 6300 struct c_parm *parm; 6301 c_parser_consume_token (parser); 6302 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6303 break; 6304 parm = c_parser_parameter_declaration (parser, NULL_TREE); 6305 if (parm == NULL) 6306 { 6307 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 6308 break; 6309 } 6310 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6311 objc_begin_catch_clause (grokparm (parm)); 6312 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) 6313 c_parser_compound_statement_nostart (parser); 6314 objc_finish_catch_clause (); 6315 } 6316 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY)) 6317 { 6318 location_t finloc; 6319 tree finstmt; 6320 c_parser_consume_token (parser); 6321 finloc = c_parser_peek_token (parser)->location; 6322 finstmt = c_parser_compound_statement (parser); 6323 objc_build_finally_clause (finloc, finstmt); 6324 } 6325 objc_finish_try_stmt (); 6326} 6327 6328/* Parse an objc-synchronized-statement. 6329 6330 objc-synchronized-statement: 6331 @synchronized ( expression ) compound-statement 6332*/ 6333 6334static void 6335c_parser_objc_synchronized_statement (c_parser *parser) 6336{ 6337 location_t loc; 6338 tree expr, stmt; 6339 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED)); 6340 c_parser_consume_token (parser); 6341 loc = c_parser_peek_token (parser)->location; 6342 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6343 { 6344 expr = c_parser_expression (parser).value; 6345 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6346 } 6347 else 6348 expr = error_mark_node; 6349 stmt = c_parser_compound_statement (parser); 6350 objc_build_synchronized (loc, expr, stmt); 6351} 6352 6353/* Parse an objc-selector; return NULL_TREE without an error if the 6354 next token is not an objc-selector. 6355 6356 objc-selector: 6357 identifier 6358 one of 6359 enum struct union if else while do for switch case default 6360 break continue return goto asm sizeof typeof __alignof 6361 unsigned long const short volatile signed restrict _Complex 6362 in out inout bycopy byref oneway int char float double void _Bool 6363 6364 ??? Why this selection of keywords but not, for example, storage 6365 class specifiers? */ 6366 6367static tree 6368c_parser_objc_selector (c_parser *parser) 6369{ 6370 c_token *token = c_parser_peek_token (parser); 6371 tree value = token->value; 6372 if (token->type == CPP_NAME) 6373 { 6374 c_parser_consume_token (parser); 6375 return value; 6376 } 6377 if (token->type != CPP_KEYWORD) 6378 return NULL_TREE; 6379 switch (token->keyword) 6380 { 6381 case RID_ENUM: 6382 case RID_STRUCT: 6383 case RID_UNION: 6384 case RID_IF: 6385 case RID_ELSE: 6386 case RID_WHILE: 6387 case RID_DO: 6388 case RID_FOR: 6389 case RID_SWITCH: 6390 case RID_CASE: 6391 case RID_DEFAULT: 6392 case RID_BREAK: 6393 case RID_CONTINUE: 6394 case RID_RETURN: 6395 case RID_GOTO: 6396 case RID_ASM: 6397 case RID_SIZEOF: 6398 case RID_TYPEOF: 6399 case RID_ALIGNOF: 6400 case RID_UNSIGNED: 6401 case RID_LONG: 6402 case RID_CONST: 6403 case RID_SHORT: 6404 case RID_VOLATILE: 6405 case RID_SIGNED: 6406 case RID_RESTRICT: 6407 case RID_COMPLEX: 6408 case RID_IN: 6409 case RID_OUT: 6410 case RID_INOUT: 6411 case RID_BYCOPY: 6412 case RID_BYREF: 6413 case RID_ONEWAY: 6414 case RID_INT: 6415 case RID_CHAR: 6416 case RID_FLOAT: 6417 case RID_DOUBLE: 6418 case RID_VOID: 6419 case RID_BOOL: 6420 c_parser_consume_token (parser); 6421 return value; 6422 default: 6423 return NULL_TREE; 6424 } 6425} 6426 6427/* Parse an objc-selector-arg. 6428 6429 objc-selector-arg: 6430 objc-selector 6431 objc-keywordname-list 6432 6433 objc-keywordname-list: 6434 objc-keywordname 6435 objc-keywordname-list objc-keywordname 6436 6437 objc-keywordname: 6438 objc-selector : 6439 : 6440*/ 6441 6442static tree 6443c_parser_objc_selector_arg (c_parser *parser) 6444{ 6445 tree sel = c_parser_objc_selector (parser); 6446 tree list = NULL_TREE; 6447 if (sel && c_parser_next_token_is_not (parser, CPP_COLON)) 6448 return sel; 6449 while (true) 6450 { 6451 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) 6452 return list; 6453 list = chainon (list, build_tree_list (sel, NULL_TREE)); 6454 sel = c_parser_objc_selector (parser); 6455 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON)) 6456 break; 6457 } 6458 return list; 6459} 6460 6461/* Parse an objc-receiver. 6462 6463 objc-receiver: 6464 expression 6465 class-name 6466 type-name 6467*/ 6468 6469static tree 6470c_parser_objc_receiver (c_parser *parser) 6471{ 6472 if (c_parser_peek_token (parser)->type == CPP_NAME 6473 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME 6474 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)) 6475 { 6476 tree id = c_parser_peek_token (parser)->value; 6477 c_parser_consume_token (parser); 6478 return objc_get_class_reference (id); 6479 } 6480 return c_parser_expression (parser).value; 6481} 6482 6483/* Parse objc-message-args. 6484 6485 objc-message-args: 6486 objc-selector 6487 objc-keywordarg-list 6488 6489 objc-keywordarg-list: 6490 objc-keywordarg 6491 objc-keywordarg-list objc-keywordarg 6492 6493 objc-keywordarg: 6494 objc-selector : objc-keywordexpr 6495 : objc-keywordexpr 6496*/ 6497 6498static tree 6499c_parser_objc_message_args (c_parser *parser) 6500{ 6501 tree sel = c_parser_objc_selector (parser); 6502 tree list = NULL_TREE; 6503 if (sel && c_parser_next_token_is_not (parser, CPP_COLON)) 6504 return sel; 6505 while (true) 6506 { 6507 tree keywordexpr; 6508 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) 6509 return list; 6510 keywordexpr = c_parser_objc_keywordexpr (parser); 6511 list = chainon (list, build_tree_list (sel, keywordexpr)); 6512 sel = c_parser_objc_selector (parser); 6513 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON)) 6514 break; 6515 } 6516 return list; 6517} 6518 6519/* Parse an objc-keywordexpr. 6520 6521 objc-keywordexpr: 6522 nonempty-expr-list 6523*/ 6524 6525static tree 6526c_parser_objc_keywordexpr (c_parser *parser) 6527{ 6528 tree list = c_parser_expr_list (parser, true); 6529 if (TREE_CHAIN (list) == NULL_TREE) 6530 { 6531 /* Just return the expression, remove a level of 6532 indirection. */ 6533 return TREE_VALUE (list); 6534 } 6535 else 6536 { 6537 /* We have a comma expression, we will collapse later. */ 6538 return list; 6539 } 6540} 6541 6542 6543/* Handle pragmas. Some OpenMP pragmas are associated with, and therefore 6544 should be considered, statements. ALLOW_STMT is true if we're within 6545 the context of a function and such pragmas are to be allowed. Returns 6546 true if we actually parsed such a pragma. */ 6547 6548static bool 6549c_parser_pragma (c_parser *parser, enum pragma_context context) 6550{ 6551 unsigned int id; 6552 6553 id = c_parser_peek_token (parser)->pragma_kind; 6554 gcc_assert (id != PRAGMA_NONE); 6555 6556 switch (id) 6557 { 6558 case PRAGMA_OMP_BARRIER: 6559 if (context != pragma_compound) 6560 { 6561 if (context == pragma_stmt) 6562 c_parser_error (parser, "%<#pragma omp barrier%> may only be " 6563 "used in compound statements"); 6564 goto bad_stmt; 6565 } 6566 c_parser_omp_barrier (parser); 6567 return false; 6568 6569 case PRAGMA_OMP_FLUSH: 6570 if (context != pragma_compound) 6571 { 6572 if (context == pragma_stmt) 6573 c_parser_error (parser, "%<#pragma omp flush%> may only be " 6574 "used in compound statements"); 6575 goto bad_stmt; 6576 } 6577 c_parser_omp_flush (parser); 6578 return false; 6579 6580 case PRAGMA_OMP_THREADPRIVATE: 6581 c_parser_omp_threadprivate (parser); 6582 return false; 6583 6584 case PRAGMA_OMP_SECTION: 6585 error ("%<#pragma omp section%> may only be used in " 6586 "%<#pragma omp sections%> construct"); 6587 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); 6588 return false; 6589 6590 case PRAGMA_GCC_PCH_PREPROCESS: 6591 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first"); 6592 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); 6593 return false; 6594 6595 default: 6596 if (id < PRAGMA_FIRST_EXTERNAL) 6597 { 6598 if (context == pragma_external) 6599 { 6600 bad_stmt: 6601 c_parser_error (parser, "expected declaration specifiers"); 6602 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); 6603 return false; 6604 } 6605 c_parser_omp_construct (parser); 6606 return true; 6607 } 6608 break; 6609 } 6610 6611 c_parser_consume_pragma (parser); 6612 c_invoke_pragma_handler (id); 6613 6614 /* Skip to EOL, but suppress any error message. Those will have been 6615 generated by the handler routine through calling error, as opposed 6616 to calling c_parser_error. */ 6617 parser->error = true; 6618 c_parser_skip_to_pragma_eol (parser); 6619 6620 return false; 6621} 6622 6623/* The interface the pragma parsers have to the lexer. */ 6624 6625enum cpp_ttype 6626pragma_lex (tree *value) 6627{ 6628 c_token *tok = c_parser_peek_token (the_parser); 6629 enum cpp_ttype ret = tok->type; 6630 6631 *value = tok->value; 6632 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF) 6633 ret = CPP_EOF; 6634 else 6635 { 6636 if (ret == CPP_KEYWORD) 6637 ret = CPP_NAME; 6638 c_parser_consume_token (the_parser); 6639 } 6640 6641 return ret; 6642} 6643 6644static void 6645c_parser_pragma_pch_preprocess (c_parser *parser) 6646{ 6647 tree name = NULL; 6648 6649 c_parser_consume_pragma (parser); 6650 if (c_parser_next_token_is (parser, CPP_STRING)) 6651 { 6652 name = c_parser_peek_token (parser)->value; 6653 c_parser_consume_token (parser); 6654 } 6655 else 6656 c_parser_error (parser, "expected string literal"); 6657 c_parser_skip_to_pragma_eol (parser); 6658 6659 if (name) 6660 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name)); 6661} 6662 6663/* OpenMP 2.5 parsing routines. */ 6664 6665/* Returns name of the next clause. 6666 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and 6667 the token is not consumed. Otherwise appropriate pragma_omp_clause is 6668 returned and the token is consumed. */ 6669 6670static pragma_omp_clause 6671c_parser_omp_clause_name (c_parser *parser) 6672{ 6673 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE; 6674 6675 if (c_parser_next_token_is_keyword (parser, RID_IF)) 6676 result = PRAGMA_OMP_CLAUSE_IF; 6677 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)) 6678 result = PRAGMA_OMP_CLAUSE_DEFAULT; 6679 else if (c_parser_next_token_is (parser, CPP_NAME)) 6680 { 6681 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); 6682 6683 switch (p[0]) 6684 { 6685 case 'c': 6686 if (!strcmp ("copyin", p)) 6687 result = PRAGMA_OMP_CLAUSE_COPYIN; 6688 else if (!strcmp ("copyprivate", p)) 6689 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE; 6690 break; 6691 case 'f': 6692 if (!strcmp ("firstprivate", p)) 6693 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE; 6694 break; 6695 case 'l': 6696 if (!strcmp ("lastprivate", p)) 6697 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE; 6698 break; 6699 case 'n': 6700 if (!strcmp ("nowait", p)) 6701 result = PRAGMA_OMP_CLAUSE_NOWAIT; 6702 else if (!strcmp ("num_threads", p)) 6703 result = PRAGMA_OMP_CLAUSE_NUM_THREADS; 6704 break; 6705 case 'o': 6706 if (!strcmp ("ordered", p)) 6707 result = PRAGMA_OMP_CLAUSE_ORDERED; 6708 break; 6709 case 'p': 6710 if (!strcmp ("private", p)) 6711 result = PRAGMA_OMP_CLAUSE_PRIVATE; 6712 break; 6713 case 'r': 6714 if (!strcmp ("reduction", p)) 6715 result = PRAGMA_OMP_CLAUSE_REDUCTION; 6716 break; 6717 case 's': 6718 if (!strcmp ("schedule", p)) 6719 result = PRAGMA_OMP_CLAUSE_SCHEDULE; 6720 else if (!strcmp ("shared", p)) 6721 result = PRAGMA_OMP_CLAUSE_SHARED; 6722 break; 6723 } 6724 } 6725 6726 if (result != PRAGMA_OMP_CLAUSE_NONE) 6727 c_parser_consume_token (parser); 6728 6729 return result; 6730} 6731 6732/* Validate that a clause of the given type does not already exist. */ 6733 6734static void 6735check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name) 6736{ 6737 tree c; 6738 6739 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) 6740 if (OMP_CLAUSE_CODE (c) == code) 6741 { 6742 error ("too many %qs clauses", name); 6743 break; 6744 } 6745} 6746 6747/* OpenMP 2.5: 6748 variable-list: 6749 identifier 6750 variable-list , identifier 6751 6752 If KIND is nonzero, create the appropriate node and install the decl 6753 in OMP_CLAUSE_DECL and add the node to the head of the list. 6754 6755 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE; 6756 return the list created. */ 6757 6758static tree 6759c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind, 6760 tree list) 6761{ 6762 if (c_parser_next_token_is_not (parser, CPP_NAME) 6763 || c_parser_peek_token (parser)->id_kind != C_ID_ID) 6764 c_parser_error (parser, "expected identifier"); 6765 6766 while (c_parser_next_token_is (parser, CPP_NAME) 6767 && c_parser_peek_token (parser)->id_kind == C_ID_ID) 6768 { 6769 tree t = lookup_name (c_parser_peek_token (parser)->value); 6770 6771 if (t == NULL_TREE) 6772 undeclared_variable (c_parser_peek_token (parser)->value, 6773 c_parser_peek_token (parser)->location); 6774 else if (t == error_mark_node) 6775 ; 6776 else if (kind != 0) 6777 { 6778 tree u = build_omp_clause (kind); 6779 OMP_CLAUSE_DECL (u) = t; 6780 OMP_CLAUSE_CHAIN (u) = list; 6781 list = u; 6782 } 6783 else 6784 list = tree_cons (t, NULL_TREE, list); 6785 6786 c_parser_consume_token (parser); 6787 6788 if (c_parser_next_token_is_not (parser, CPP_COMMA)) 6789 break; 6790 6791 c_parser_consume_token (parser); 6792 } 6793 6794 return list; 6795} 6796 6797/* Similarly, but expect leading and trailing parenthesis. This is a very 6798 common case for omp clauses. */ 6799 6800static tree 6801c_parser_omp_var_list_parens (c_parser *parser, enum tree_code kind, tree list) 6802{ 6803 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6804 { 6805 list = c_parser_omp_variable_list (parser, kind, list); 6806 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6807 } 6808 return list; 6809} 6810 6811/* OpenMP 2.5: 6812 copyin ( variable-list ) */ 6813 6814static tree 6815c_parser_omp_clause_copyin (c_parser *parser, tree list) 6816{ 6817 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list); 6818} 6819 6820/* OpenMP 2.5: 6821 copyprivate ( variable-list ) */ 6822 6823static tree 6824c_parser_omp_clause_copyprivate (c_parser *parser, tree list) 6825{ 6826 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list); 6827} 6828 6829/* OpenMP 2.5: 6830 default ( shared | none ) */ 6831 6832static tree 6833c_parser_omp_clause_default (c_parser *parser, tree list) 6834{ 6835 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED; 6836 tree c; 6837 6838 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6839 return list; 6840 if (c_parser_next_token_is (parser, CPP_NAME)) 6841 { 6842 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); 6843 6844 switch (p[0]) 6845 { 6846 case 'n': 6847 if (strcmp ("none", p) != 0) 6848 goto invalid_kind; 6849 kind = OMP_CLAUSE_DEFAULT_NONE; 6850 break; 6851 6852 case 's': 6853 if (strcmp ("shared", p) != 0) 6854 goto invalid_kind; 6855 kind = OMP_CLAUSE_DEFAULT_SHARED; 6856 break; 6857 6858 default: 6859 goto invalid_kind; 6860 } 6861 6862 c_parser_consume_token (parser); 6863 } 6864 else 6865 { 6866 invalid_kind: 6867 c_parser_error (parser, "expected %<none%> or %<shared%>"); 6868 } 6869 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6870 6871 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED) 6872 return list; 6873 6874 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default"); 6875 c = build_omp_clause (OMP_CLAUSE_DEFAULT); 6876 OMP_CLAUSE_CHAIN (c) = list; 6877 OMP_CLAUSE_DEFAULT_KIND (c) = kind; 6878 6879 return c; 6880} 6881 6882/* OpenMP 2.5: 6883 firstprivate ( variable-list ) */ 6884 6885static tree 6886c_parser_omp_clause_firstprivate (c_parser *parser, tree list) 6887{ 6888 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list); 6889} 6890 6891/* OpenMP 2.5: 6892 if ( expression ) */ 6893 6894static tree 6895c_parser_omp_clause_if (c_parser *parser, tree list) 6896{ 6897 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 6898 { 6899 tree t = c_parser_paren_condition (parser); 6900 tree c; 6901 6902 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if"); 6903 6904 c = build_omp_clause (OMP_CLAUSE_IF); 6905 OMP_CLAUSE_IF_EXPR (c) = t; 6906 OMP_CLAUSE_CHAIN (c) = list; 6907 list = c; 6908 } 6909 else 6910 c_parser_error (parser, "expected %<(%>"); 6911 6912 return list; 6913} 6914 6915/* OpenMP 2.5: 6916 lastprivate ( variable-list ) */ 6917 6918static tree 6919c_parser_omp_clause_lastprivate (c_parser *parser, tree list) 6920{ 6921 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list); 6922} 6923 6924/* OpenMP 2.5: 6925 nowait */ 6926 6927static tree 6928c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list) 6929{ 6930 tree c; 6931 6932 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait"); 6933 6934 c = build_omp_clause (OMP_CLAUSE_NOWAIT); 6935 OMP_CLAUSE_CHAIN (c) = list; 6936 return c; 6937} 6938 6939/* OpenMP 2.5: 6940 num_threads ( expression ) */ 6941 6942static tree 6943c_parser_omp_clause_num_threads (c_parser *parser, tree list) 6944{ 6945 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6946 { 6947 tree c, t = c_parser_expression (parser).value; 6948 6949 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6950 6951 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))) 6952 { 6953 c_parser_error (parser, "expected integer expression"); 6954 return list; 6955 } 6956 6957 /* Attempt to statically determine when the number isn't positive. */ 6958 c = fold_build2 (LE_EXPR, boolean_type_node, t, 6959 build_int_cst (TREE_TYPE (t), 0)); 6960 if (c == boolean_true_node) 6961 { 6962 warning (0, "%<num_threads%> value must be positive"); 6963 t = integer_one_node; 6964 } 6965 6966 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads"); 6967 6968 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS); 6969 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t; 6970 OMP_CLAUSE_CHAIN (c) = list; 6971 list = c; 6972 } 6973 6974 return list; 6975} 6976 6977/* OpenMP 2.5: 6978 ordered */ 6979 6980static tree 6981c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list) 6982{ 6983 tree c; 6984 6985 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered"); 6986 6987 c = build_omp_clause (OMP_CLAUSE_ORDERED); 6988 OMP_CLAUSE_CHAIN (c) = list; 6989 return c; 6990} 6991 6992/* OpenMP 2.5: 6993 private ( variable-list ) */ 6994 6995static tree 6996c_parser_omp_clause_private (c_parser *parser, tree list) 6997{ 6998 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list); 6999} 7000 7001/* OpenMP 2.5: 7002 reduction ( reduction-operator : variable-list ) 7003 7004 reduction-operator: 7005 One of: + * - & ^ | && || */ 7006 7007static tree 7008c_parser_omp_clause_reduction (c_parser *parser, tree list) 7009{ 7010 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 7011 { 7012 enum tree_code code; 7013 7014 switch (c_parser_peek_token (parser)->type) 7015 { 7016 case CPP_PLUS: 7017 code = PLUS_EXPR; 7018 break; 7019 case CPP_MULT: 7020 code = MULT_EXPR; 7021 break; 7022 case CPP_MINUS: 7023 code = MINUS_EXPR; 7024 break; 7025 case CPP_AND: 7026 code = BIT_AND_EXPR; 7027 break; 7028 case CPP_XOR: 7029 code = BIT_XOR_EXPR; 7030 break; 7031 case CPP_OR: 7032 code = BIT_IOR_EXPR; 7033 break; 7034 case CPP_AND_AND: 7035 code = TRUTH_ANDIF_EXPR; 7036 break; 7037 case CPP_OR_OR: 7038 code = TRUTH_ORIF_EXPR; 7039 break; 7040 default: 7041 c_parser_error (parser, 7042 "expected %<+%>, %<*%>, %<-%>, %<&%>, " 7043 "%<^%>, %<|%>, %<&&%>, or %<||%>"); 7044 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0); 7045 return list; 7046 } 7047 c_parser_consume_token (parser); 7048 if (c_parser_require (parser, CPP_COLON, "expected %<:%>")) 7049 { 7050 tree nl, c; 7051 7052 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list); 7053 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c)) 7054 OMP_CLAUSE_REDUCTION_CODE (c) = code; 7055 7056 list = nl; 7057 } 7058 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7059 } 7060 return list; 7061} 7062 7063/* OpenMP 2.5: 7064 schedule ( schedule-kind ) 7065 schedule ( schedule-kind , expression ) 7066 7067 schedule-kind: 7068 static | dynamic | guided | runtime 7069*/ 7070 7071static tree 7072c_parser_omp_clause_schedule (c_parser *parser, tree list) 7073{ 7074 tree c, t; 7075 7076 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 7077 return list; 7078 7079 c = build_omp_clause (OMP_CLAUSE_SCHEDULE); 7080 7081 if (c_parser_next_token_is (parser, CPP_NAME)) 7082 { 7083 tree kind = c_parser_peek_token (parser)->value; 7084 const char *p = IDENTIFIER_POINTER (kind); 7085 7086 switch (p[0]) 7087 { 7088 case 'd': 7089 if (strcmp ("dynamic", p) != 0) 7090 goto invalid_kind; 7091 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC; 7092 break; 7093 7094 case 'g': 7095 if (strcmp ("guided", p) != 0) 7096 goto invalid_kind; 7097 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED; 7098 break; 7099 7100 case 'r': 7101 if (strcmp ("runtime", p) != 0) 7102 goto invalid_kind; 7103 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME; 7104 break; 7105 7106 default: 7107 goto invalid_kind; 7108 } 7109 } 7110 else if (c_parser_next_token_is_keyword (parser, RID_STATIC)) 7111 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC; 7112 else 7113 goto invalid_kind; 7114 7115 c_parser_consume_token (parser); 7116 if (c_parser_next_token_is (parser, CPP_COMMA)) 7117 { 7118 c_parser_consume_token (parser); 7119 7120 t = c_parser_expr_no_commas (parser, NULL).value; 7121 7122 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME) 7123 error ("schedule %<runtime%> does not take " 7124 "a %<chunk_size%> parameter"); 7125 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE) 7126 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t; 7127 else 7128 c_parser_error (parser, "expected integer expression"); 7129 7130 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7131 } 7132 else 7133 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 7134 "expected %<,%> or %<)%>"); 7135 7136 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule"); 7137 OMP_CLAUSE_CHAIN (c) = list; 7138 return c; 7139 7140 invalid_kind: 7141 c_parser_error (parser, "invalid schedule kind"); 7142 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0); 7143 return list; 7144} 7145 7146/* OpenMP 2.5: 7147 shared ( variable-list ) */ 7148 7149static tree 7150c_parser_omp_clause_shared (c_parser *parser, tree list) 7151{ 7152 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list); 7153} 7154 7155/* Parse all OpenMP clauses. The set clauses allowed by the directive 7156 is a bitmask in MASK. Return the list of clauses found; the result 7157 of clause default goes in *pdefault. */ 7158 7159static tree 7160c_parser_omp_all_clauses (c_parser *parser, unsigned int mask, 7161 const char *where) 7162{ 7163 tree clauses = NULL; 7164 7165 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL)) 7166 { 7167 const pragma_omp_clause c_kind = c_parser_omp_clause_name (parser); 7168 const char *c_name; 7169 tree prev = clauses; 7170 7171 switch (c_kind) 7172 { 7173 case PRAGMA_OMP_CLAUSE_COPYIN: 7174 clauses = c_parser_omp_clause_copyin (parser, clauses); 7175 c_name = "copyin"; 7176 break; 7177 case PRAGMA_OMP_CLAUSE_COPYPRIVATE: 7178 clauses = c_parser_omp_clause_copyprivate (parser, clauses); 7179 c_name = "copyprivate"; 7180 break; 7181 case PRAGMA_OMP_CLAUSE_DEFAULT: 7182 clauses = c_parser_omp_clause_default (parser, clauses); 7183 c_name = "default"; 7184 break; 7185 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE: 7186 clauses = c_parser_omp_clause_firstprivate (parser, clauses); 7187 c_name = "firstprivate"; 7188 break; 7189 case PRAGMA_OMP_CLAUSE_IF: 7190 clauses = c_parser_omp_clause_if (parser, clauses); 7191 c_name = "if"; 7192 break; 7193 case PRAGMA_OMP_CLAUSE_LASTPRIVATE: 7194 clauses = c_parser_omp_clause_lastprivate (parser, clauses); 7195 c_name = "lastprivate"; 7196 break; 7197 case PRAGMA_OMP_CLAUSE_NOWAIT: 7198 clauses = c_parser_omp_clause_nowait (parser, clauses); 7199 c_name = "nowait"; 7200 break; 7201 case PRAGMA_OMP_CLAUSE_NUM_THREADS: 7202 clauses = c_parser_omp_clause_num_threads (parser, clauses); 7203 c_name = "num_threads"; 7204 break; 7205 case PRAGMA_OMP_CLAUSE_ORDERED: 7206 clauses = c_parser_omp_clause_ordered (parser, clauses); 7207 c_name = "ordered"; 7208 break; 7209 case PRAGMA_OMP_CLAUSE_PRIVATE: 7210 clauses = c_parser_omp_clause_private (parser, clauses); 7211 c_name = "private"; 7212 break; 7213 case PRAGMA_OMP_CLAUSE_REDUCTION: 7214 clauses = c_parser_omp_clause_reduction (parser, clauses); 7215 c_name = "reduction"; 7216 break; 7217 case PRAGMA_OMP_CLAUSE_SCHEDULE: 7218 clauses = c_parser_omp_clause_schedule (parser, clauses); 7219 c_name = "schedule"; 7220 break; 7221 case PRAGMA_OMP_CLAUSE_SHARED: 7222 clauses = c_parser_omp_clause_shared (parser, clauses); 7223 c_name = "shared"; 7224 break; 7225 default: 7226 c_parser_error (parser, "expected %<#pragma omp%> clause"); 7227 goto saw_error; 7228 } 7229 7230 if (((mask >> c_kind) & 1) == 0 && !parser->error) 7231 { 7232 /* Remove the invalid clause(s) from the list to avoid 7233 confusing the rest of the compiler. */ 7234 clauses = prev; 7235 error ("%qs is not valid for %qs", c_name, where); 7236 } 7237 } 7238 7239 saw_error: 7240 c_parser_skip_to_pragma_eol (parser); 7241 7242 return c_finish_omp_clauses (clauses); 7243} 7244 7245/* OpenMP 2.5: 7246 structured-block: 7247 statement 7248 7249 In practice, we're also interested in adding the statement to an 7250 outer node. So it is convenient if we work around the fact that 7251 c_parser_statement calls add_stmt. */ 7252 7253static tree 7254c_parser_omp_structured_block (c_parser *parser) 7255{ 7256 tree stmt = push_stmt_list (); 7257 c_parser_statement (parser); 7258 return pop_stmt_list (stmt); 7259} 7260 7261/* OpenMP 2.5: 7262 # pragma omp atomic new-line 7263 expression-stmt 7264 7265 expression-stmt: 7266 x binop= expr | x++ | ++x | x-- | --x 7267 binop: 7268 +, *, -, /, &, ^, |, <<, >> 7269 7270 where x is an lvalue expression with scalar type. */ 7271 7272static void 7273c_parser_omp_atomic (c_parser *parser) 7274{ 7275 tree lhs, rhs; 7276 tree stmt; 7277 enum tree_code code; 7278 7279 c_parser_skip_to_pragma_eol (parser); 7280 7281 lhs = c_parser_unary_expression (parser).value; 7282 switch (TREE_CODE (lhs)) 7283 { 7284 case ERROR_MARK: 7285 saw_error: 7286 c_parser_skip_to_end_of_block_or_statement (parser); 7287 return; 7288 7289 case PREINCREMENT_EXPR: 7290 case POSTINCREMENT_EXPR: 7291 lhs = TREE_OPERAND (lhs, 0); 7292 code = PLUS_EXPR; 7293 rhs = integer_one_node; 7294 break; 7295 7296 case PREDECREMENT_EXPR: 7297 case POSTDECREMENT_EXPR: 7298 lhs = TREE_OPERAND (lhs, 0); 7299 code = MINUS_EXPR; 7300 rhs = integer_one_node; 7301 break; 7302 7303 default: 7304 switch (c_parser_peek_token (parser)->type) 7305 { 7306 case CPP_MULT_EQ: 7307 code = MULT_EXPR; 7308 break; 7309 case CPP_DIV_EQ: 7310 code = TRUNC_DIV_EXPR; 7311 break; 7312 case CPP_PLUS_EQ: 7313 code = PLUS_EXPR; 7314 break; 7315 case CPP_MINUS_EQ: 7316 code = MINUS_EXPR; 7317 break; 7318 case CPP_LSHIFT_EQ: 7319 code = LSHIFT_EXPR; 7320 break; 7321 case CPP_RSHIFT_EQ: 7322 code = RSHIFT_EXPR; 7323 break; 7324 case CPP_AND_EQ: 7325 code = BIT_AND_EXPR; 7326 break; 7327 case CPP_OR_EQ: 7328 code = BIT_IOR_EXPR; 7329 break; 7330 case CPP_XOR_EQ: 7331 code = BIT_XOR_EXPR; 7332 break; 7333 default: 7334 c_parser_error (parser, 7335 "invalid operator for %<#pragma omp atomic%>"); 7336 goto saw_error; 7337 } 7338 7339 c_parser_consume_token (parser); 7340 rhs = c_parser_expression (parser).value; 7341 break; 7342 } 7343 stmt = c_finish_omp_atomic (code, lhs, rhs); 7344 if (stmt != error_mark_node) 7345 add_stmt (stmt); 7346 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 7347} 7348 7349 7350/* OpenMP 2.5: 7351 # pragma omp barrier new-line 7352*/ 7353 7354static void 7355c_parser_omp_barrier (c_parser *parser) 7356{ 7357 c_parser_consume_pragma (parser); 7358 c_parser_skip_to_pragma_eol (parser); 7359 7360 c_finish_omp_barrier (); 7361} 7362 7363/* OpenMP 2.5: 7364 # pragma omp critical [(name)] new-line 7365 structured-block 7366*/ 7367 7368static tree 7369c_parser_omp_critical (c_parser *parser) 7370{ 7371 tree stmt, name = NULL; 7372 7373 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 7374 { 7375 c_parser_consume_token (parser); 7376 if (c_parser_next_token_is (parser, CPP_NAME)) 7377 { 7378 name = c_parser_peek_token (parser)->value; 7379 c_parser_consume_token (parser); 7380 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7381 } 7382 else 7383 c_parser_error (parser, "expected identifier"); 7384 } 7385 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL)) 7386 c_parser_error (parser, "expected %<(%> or end of line"); 7387 c_parser_skip_to_pragma_eol (parser); 7388 7389 stmt = c_parser_omp_structured_block (parser); 7390 return c_finish_omp_critical (stmt, name); 7391} 7392 7393/* OpenMP 2.5: 7394 # pragma omp flush flush-vars[opt] new-line 7395 7396 flush-vars: 7397 ( variable-list ) */ 7398 7399static void 7400c_parser_omp_flush (c_parser *parser) 7401{ 7402 c_parser_consume_pragma (parser); 7403 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 7404 c_parser_omp_var_list_parens (parser, 0, NULL); 7405 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL)) 7406 c_parser_error (parser, "expected %<(%> or end of line"); 7407 c_parser_skip_to_pragma_eol (parser); 7408 7409 c_finish_omp_flush (); 7410} 7411 7412/* Parse the restricted form of the for statment allowed by OpenMP. 7413 The real trick here is to determine the loop control variable early 7414 so that we can push a new decl if necessary to make it private. */ 7415 7416static tree 7417c_parser_omp_for_loop (c_parser *parser) 7418{ 7419 tree decl, cond, incr, save_break, save_cont, body, init; 7420 location_t loc; 7421 7422 if (!c_parser_next_token_is_keyword (parser, RID_FOR)) 7423 { 7424 c_parser_error (parser, "for statement expected"); 7425 return NULL; 7426 } 7427 loc = c_parser_peek_token (parser)->location; 7428 c_parser_consume_token (parser); 7429 7430 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 7431 return NULL; 7432 7433 /* Parse the initialization declaration or expression. */ 7434 if (c_parser_next_token_starts_declspecs (parser)) 7435 { 7436 c_parser_declaration_or_fndef (parser, true, true, true, true); 7437 decl = check_for_loop_decls (); 7438 if (decl == NULL) 7439 goto error_init; 7440 init = decl; 7441 } 7442 else if (c_parser_next_token_is (parser, CPP_NAME) 7443 && c_parser_peek_2nd_token (parser)->type == CPP_EQ) 7444 { 7445 decl = c_parser_postfix_expression (parser).value; 7446 7447 c_parser_require (parser, CPP_EQ, "expected %<=%>"); 7448 7449 init = c_parser_expr_no_commas (parser, NULL).value; 7450 init = build_modify_expr (decl, NOP_EXPR, init); 7451 init = c_process_expr_stmt (init); 7452 7453 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 7454 } 7455 else 7456 goto error_init; 7457 7458 /* Parse the loop condition. */ 7459 cond = NULL_TREE; 7460 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON)) 7461 { 7462 cond = c_parser_expression_conv (parser).value; 7463 cond = c_objc_common_truthvalue_conversion (cond); 7464 if (EXPR_P (cond)) 7465 SET_EXPR_LOCATION (cond, input_location); 7466 } 7467 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 7468 7469 /* Parse the increment expression. */ 7470 incr = NULL_TREE; 7471 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN)) 7472 incr = c_process_expr_stmt (c_parser_expression (parser).value); 7473 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7474 7475 parse_body: 7476 save_break = c_break_label; 7477 c_break_label = size_one_node; 7478 save_cont = c_cont_label; 7479 c_cont_label = NULL_TREE; 7480 body = push_stmt_list (); 7481 7482 add_stmt (c_parser_c99_block_statement (parser)); 7483 if (c_cont_label) 7484 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label)); 7485 7486 body = pop_stmt_list (body); 7487 c_break_label = save_break; 7488 c_cont_label = save_cont; 7489 7490 /* Only bother calling c_finish_omp_for if we havn't already generated 7491 an error from the initialization parsing. */ 7492 if (decl != NULL && decl != error_mark_node && init != error_mark_node) 7493 return c_finish_omp_for (loc, decl, init, cond, incr, body, NULL); 7494 return NULL; 7495 7496 error_init: 7497 c_parser_error (parser, "expected iteration declaration or initialization"); 7498 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7499 decl = init = cond = incr = NULL_TREE; 7500 goto parse_body; 7501} 7502 7503/* OpenMP 2.5: 7504 #pragma omp for for-clause[optseq] new-line 7505 for-loop 7506*/ 7507 7508#define OMP_FOR_CLAUSE_MASK \ 7509 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 7510 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 7511 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ 7512 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 7513 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \ 7514 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \ 7515 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 7516 7517static tree 7518c_parser_omp_for (c_parser *parser) 7519{ 7520 tree block, clauses, ret; 7521 7522 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK, 7523 "#pragma omp for"); 7524 7525 block = c_begin_compound_stmt (true); 7526 ret = c_parser_omp_for_loop (parser); 7527 if (ret) 7528 OMP_FOR_CLAUSES (ret) = clauses; 7529 block = c_end_compound_stmt (block, true); 7530 add_stmt (block); 7531 7532 return ret; 7533} 7534 7535/* OpenMP 2.5: 7536 # pragma omp master new-line 7537 structured-block 7538*/ 7539 7540static tree 7541c_parser_omp_master (c_parser *parser) 7542{ 7543 c_parser_skip_to_pragma_eol (parser); 7544 return c_finish_omp_master (c_parser_omp_structured_block (parser)); 7545} 7546 7547/* OpenMP 2.5: 7548 # pragma omp ordered new-line 7549 structured-block 7550*/ 7551 7552static tree 7553c_parser_omp_ordered (c_parser *parser) 7554{ 7555 c_parser_skip_to_pragma_eol (parser); 7556 return c_finish_omp_ordered (c_parser_omp_structured_block (parser)); 7557} 7558 7559/* OpenMP 2.5: 7560 7561 section-scope: 7562 { section-sequence } 7563 7564 section-sequence: 7565 section-directive[opt] structured-block 7566 section-sequence section-directive structured-block */ 7567 7568static tree 7569c_parser_omp_sections_scope (c_parser *parser) 7570{ 7571 tree stmt, substmt; 7572 bool error_suppress = false; 7573 location_t loc; 7574 7575 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) 7576 { 7577 /* Avoid skipping until the end of the block. */ 7578 parser->error = false; 7579 return NULL_TREE; 7580 } 7581 7582 stmt = push_stmt_list (); 7583 7584 loc = c_parser_peek_token (parser)->location; 7585 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION) 7586 { 7587 substmt = push_stmt_list (); 7588 7589 while (1) 7590 { 7591 c_parser_statement (parser); 7592 7593 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION) 7594 break; 7595 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 7596 break; 7597 if (c_parser_next_token_is (parser, CPP_EOF)) 7598 break; 7599 } 7600 7601 substmt = pop_stmt_list (substmt); 7602 substmt = build1 (OMP_SECTION, void_type_node, substmt); 7603 SET_EXPR_LOCATION (substmt, loc); 7604 add_stmt (substmt); 7605 } 7606 7607 while (1) 7608 { 7609 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 7610 break; 7611 if (c_parser_next_token_is (parser, CPP_EOF)) 7612 break; 7613 7614 loc = c_parser_peek_token (parser)->location; 7615 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION) 7616 { 7617 c_parser_consume_pragma (parser); 7618 c_parser_skip_to_pragma_eol (parser); 7619 error_suppress = false; 7620 } 7621 else if (!error_suppress) 7622 { 7623 error ("expected %<#pragma omp section%> or %<}%>"); 7624 error_suppress = true; 7625 } 7626 7627 substmt = c_parser_omp_structured_block (parser); 7628 substmt = build1 (OMP_SECTION, void_type_node, substmt); 7629 SET_EXPR_LOCATION (substmt, loc); 7630 add_stmt (substmt); 7631 } 7632 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, 7633 "expected %<#pragma omp section%> or %<}%>"); 7634 7635 substmt = pop_stmt_list (stmt); 7636 7637 stmt = make_node (OMP_SECTIONS); 7638 TREE_TYPE (stmt) = void_type_node; 7639 OMP_SECTIONS_BODY (stmt) = substmt; 7640 7641 return add_stmt (stmt); 7642} 7643 7644/* OpenMP 2.5: 7645 # pragma omp sections sections-clause[optseq] newline 7646 sections-scope 7647*/ 7648 7649#define OMP_SECTIONS_CLAUSE_MASK \ 7650 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 7651 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 7652 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ 7653 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 7654 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 7655 7656static tree 7657c_parser_omp_sections (c_parser *parser) 7658{ 7659 tree block, clauses, ret; 7660 7661 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK, 7662 "#pragma omp sections"); 7663 7664 block = c_begin_compound_stmt (true); 7665 ret = c_parser_omp_sections_scope (parser); 7666 if (ret) 7667 OMP_SECTIONS_CLAUSES (ret) = clauses; 7668 block = c_end_compound_stmt (block, true); 7669 add_stmt (block); 7670 7671 return ret; 7672} 7673 7674/* OpenMP 2.5: 7675 # pragma parallel parallel-clause new-line 7676 # pragma parallel for parallel-for-clause new-line 7677 # pragma parallel sections parallel-sections-clause new-line 7678*/ 7679 7680#define OMP_PARALLEL_CLAUSE_MASK \ 7681 ( (1u << PRAGMA_OMP_CLAUSE_IF) \ 7682 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 7683 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 7684 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ 7685 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \ 7686 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \ 7687 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 7688 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS)) 7689 7690static tree 7691c_parser_omp_parallel (c_parser *parser) 7692{ 7693 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL; 7694 const char *p_name = "#pragma omp parallel"; 7695 tree stmt, clauses, par_clause, ws_clause, block; 7696 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK; 7697 7698 if (c_parser_next_token_is_keyword (parser, RID_FOR)) 7699 { 7700 c_parser_consume_token (parser); 7701 p_kind = PRAGMA_OMP_PARALLEL_FOR; 7702 p_name = "#pragma omp parallel for"; 7703 mask |= OMP_FOR_CLAUSE_MASK; 7704 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); 7705 } 7706 else if (c_parser_next_token_is (parser, CPP_NAME)) 7707 { 7708 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); 7709 if (strcmp (p, "sections") == 0) 7710 { 7711 c_parser_consume_token (parser); 7712 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS; 7713 p_name = "#pragma omp parallel sections"; 7714 mask |= OMP_SECTIONS_CLAUSE_MASK; 7715 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); 7716 } 7717 } 7718 7719 clauses = c_parser_omp_all_clauses (parser, mask, p_name); 7720 7721 switch (p_kind) 7722 { 7723 case PRAGMA_OMP_PARALLEL: 7724 block = c_begin_omp_parallel (); 7725 c_parser_statement (parser); 7726 stmt = c_finish_omp_parallel (clauses, block); 7727 break; 7728 7729 case PRAGMA_OMP_PARALLEL_FOR: 7730 block = c_begin_omp_parallel (); 7731 c_split_parallel_clauses (clauses, &par_clause, &ws_clause); 7732 stmt = c_parser_omp_for_loop (parser); 7733 if (stmt) 7734 OMP_FOR_CLAUSES (stmt) = ws_clause; 7735 stmt = c_finish_omp_parallel (par_clause, block); 7736 OMP_PARALLEL_COMBINED (stmt) = 1; 7737 break; 7738 7739 case PRAGMA_OMP_PARALLEL_SECTIONS: 7740 block = c_begin_omp_parallel (); 7741 c_split_parallel_clauses (clauses, &par_clause, &ws_clause); 7742 stmt = c_parser_omp_sections_scope (parser); 7743 if (stmt) 7744 OMP_SECTIONS_CLAUSES (stmt) = ws_clause; 7745 stmt = c_finish_omp_parallel (par_clause, block); 7746 OMP_PARALLEL_COMBINED (stmt) = 1; 7747 break; 7748 7749 default: 7750 gcc_unreachable (); 7751 } 7752 7753 return stmt; 7754} 7755 7756/* OpenMP 2.5: 7757 # pragma omp single single-clause[optseq] new-line 7758 structured-block 7759*/ 7760 7761#define OMP_SINGLE_CLAUSE_MASK \ 7762 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 7763 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 7764 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \ 7765 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 7766 7767static tree 7768c_parser_omp_single (c_parser *parser) 7769{ 7770 tree stmt = make_node (OMP_SINGLE); 7771 TREE_TYPE (stmt) = void_type_node; 7772 7773 OMP_SINGLE_CLAUSES (stmt) 7774 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK, 7775 "#pragma omp single"); 7776 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser); 7777 7778 return add_stmt (stmt); 7779} 7780 7781 7782/* Main entry point to parsing most OpenMP pragmas. */ 7783 7784static void 7785c_parser_omp_construct (c_parser *parser) 7786{ 7787 enum pragma_kind p_kind; 7788 location_t loc; 7789 tree stmt; 7790 7791 loc = c_parser_peek_token (parser)->location; 7792 p_kind = c_parser_peek_token (parser)->pragma_kind; 7793 c_parser_consume_pragma (parser); 7794 7795 /* For all constructs below except #pragma omp atomic 7796 MUST_NOT_THROW catch handlers are needed when exceptions 7797 are enabled. */ 7798 if (p_kind != PRAGMA_OMP_ATOMIC) 7799 c_maybe_initialize_eh (); 7800 7801 switch (p_kind) 7802 { 7803 case PRAGMA_OMP_ATOMIC: 7804 c_parser_omp_atomic (parser); 7805 return; 7806 case PRAGMA_OMP_CRITICAL: 7807 stmt = c_parser_omp_critical (parser); 7808 break; 7809 case PRAGMA_OMP_FOR: 7810 stmt = c_parser_omp_for (parser); 7811 break; 7812 case PRAGMA_OMP_MASTER: 7813 stmt = c_parser_omp_master (parser); 7814 break; 7815 case PRAGMA_OMP_ORDERED: 7816 stmt = c_parser_omp_ordered (parser); 7817 break; 7818 case PRAGMA_OMP_PARALLEL: 7819 stmt = c_parser_omp_parallel (parser); 7820 break; 7821 case PRAGMA_OMP_SECTIONS: 7822 stmt = c_parser_omp_sections (parser); 7823 break; 7824 case PRAGMA_OMP_SINGLE: 7825 stmt = c_parser_omp_single (parser); 7826 break; 7827 default: 7828 gcc_unreachable (); 7829 } 7830 7831 if (stmt) 7832 SET_EXPR_LOCATION (stmt, loc); 7833} 7834 7835 7836/* OpenMP 2.5: 7837 # pragma omp threadprivate (variable-list) */ 7838 7839static void 7840c_parser_omp_threadprivate (c_parser *parser) 7841{ 7842 tree vars, t; 7843 7844 c_parser_consume_pragma (parser); 7845 vars = c_parser_omp_var_list_parens (parser, 0, NULL); 7846 7847 if (!targetm.have_tls) 7848 sorry ("threadprivate variables not supported in this target"); 7849 7850 /* Mark every variable in VARS to be assigned thread local storage. */ 7851 for (t = vars; t; t = TREE_CHAIN (t)) 7852 { 7853 tree v = TREE_PURPOSE (t); 7854 7855 /* If V had already been marked threadprivate, it doesn't matter 7856 whether it had been used prior to this point. */ 7857 if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v)) 7858 error ("%qE declared %<threadprivate%> after first use", v); 7859 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v)) 7860 error ("automatic variable %qE cannot be %<threadprivate%>", v); 7861 else if (! COMPLETE_TYPE_P (TREE_TYPE (v))) 7862 error ("%<threadprivate%> %qE has incomplete type", v); 7863 else 7864 { 7865 if (! DECL_THREAD_LOCAL_P (v)) 7866 { 7867 DECL_TLS_MODEL (v) = decl_default_tls_model (v); 7868 /* If rtl has been already set for this var, call 7869 make_decl_rtl once again, so that encode_section_info 7870 has a chance to look at the new decl flags. */ 7871 if (DECL_RTL_SET_P (v)) 7872 make_decl_rtl (v); 7873 } 7874 C_DECL_THREADPRIVATE_P (v) = 1; 7875 } 7876 } 7877 7878 c_parser_skip_to_pragma_eol (parser); 7879} 7880 7881 7882/* Parse a single source file. */ 7883 7884void 7885c_parse_file (void) 7886{ 7887 /* Use local storage to begin. If the first token is a pragma, parse it. 7888 If it is #pragma GCC pch_preprocess, then this will load a PCH file 7889 which will cause garbage collection. */ 7890 c_parser tparser; 7891 7892 memset (&tparser, 0, sizeof tparser); 7893 the_parser = &tparser; 7894 7895 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS) 7896 c_parser_pragma_pch_preprocess (&tparser); 7897 7898 the_parser = GGC_NEW (c_parser); 7899 *the_parser = tparser; 7900 7901 c_parser_translation_unit (the_parser); 7902 the_parser = NULL; 7903} 7904 7905#include "gt-c-parser.h" 7906