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 while (expression) statement 3944*/ 3945 3946static void 3947c_parser_while_statement (c_parser *parser) 3948{ 3949 tree block, cond, body, save_break, save_cont; 3950 location_t loc; 3951 gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE)); 3952 c_parser_consume_token (parser); 3953 block = c_begin_compound_stmt (flag_isoc99); 3954 loc = c_parser_peek_token (parser)->location; 3955 cond = c_parser_paren_condition (parser); 3956 save_break = c_break_label; 3957 c_break_label = NULL_TREE; 3958 save_cont = c_cont_label; 3959 c_cont_label = NULL_TREE; 3960 body = c_parser_c99_block_statement (parser); 3961 c_finish_loop (loc, cond, NULL, body, c_break_label, c_cont_label, true); 3962 add_stmt (c_end_compound_stmt (block, flag_isoc99)); 3963 c_break_label = save_break; 3964 c_cont_label = save_cont; 3965} 3966 3967/* Parse a do statement (C90 6.6.5, C99 6.8.5). 3968 3969 do-statement: 3970 do statement while ( expression ) ; 3971*/ 3972 3973static void 3974c_parser_do_statement (c_parser *parser) 3975{ 3976 tree block, cond, body, save_break, save_cont, new_break, new_cont; 3977 location_t loc; 3978 gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO)); 3979 c_parser_consume_token (parser); 3980 block = c_begin_compound_stmt (flag_isoc99); 3981 loc = c_parser_peek_token (parser)->location; 3982 save_break = c_break_label; 3983 c_break_label = NULL_TREE; 3984 save_cont = c_cont_label; 3985 c_cont_label = NULL_TREE; 3986 body = c_parser_c99_block_statement (parser); 3987 c_parser_require_keyword (parser, RID_WHILE, "expected %<while%>"); 3988 new_break = c_break_label; 3989 c_break_label = save_break; 3990 new_cont = c_cont_label; 3991 c_cont_label = save_cont; 3992 cond = c_parser_paren_condition (parser); 3993 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>")) 3994 c_parser_skip_to_end_of_block_or_statement (parser); 3995 c_finish_loop (loc, cond, NULL, body, new_break, new_cont, false); 3996 add_stmt (c_end_compound_stmt (block, flag_isoc99)); 3997} 3998 3999/* Parse a for statement (C90 6.6.5, C99 6.8.5). 4000 4001 for-statement: 4002 for ( expression[opt] ; expression[opt] ; expression[opt] ) statement 4003 for ( nested-declaration expression[opt] ; expression[opt] ) statement 4004 4005 The form with a declaration is new in C99. 4006 4007 ??? In accordance with the old parser, the declaration may be a 4008 nested function, which is then rejected in check_for_loop_decls, 4009 but does it make any sense for this to be included in the grammar? 4010 Note in particular that the nested function does not include a 4011 trailing ';', whereas the "declaration" production includes one. 4012 Also, can we reject bad declarations earlier and cheaper than 4013 check_for_loop_decls? */ 4014 4015static void 4016c_parser_for_statement (c_parser *parser) 4017{ 4018 tree block, cond, incr, save_break, save_cont, body; 4019 location_t loc; 4020 gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR)); 4021 loc = c_parser_peek_token (parser)->location; 4022 c_parser_consume_token (parser); 4023 block = c_begin_compound_stmt (flag_isoc99); 4024 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 4025 { 4026 /* Parse the initialization declaration or expression. */ 4027 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 4028 { 4029 c_parser_consume_token (parser); 4030 c_finish_expr_stmt (NULL_TREE); 4031 } 4032 else if (c_parser_next_token_starts_declspecs (parser)) 4033 { 4034 c_parser_declaration_or_fndef (parser, true, true, true, true); 4035 check_for_loop_decls (); 4036 } 4037 else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION)) 4038 { 4039 /* __extension__ can start a declaration, but is also an 4040 unary operator that can start an expression. Consume all 4041 but the last of a possible series of __extension__ to 4042 determine which. */ 4043 while (c_parser_peek_2nd_token (parser)->type == CPP_KEYWORD 4044 && (c_parser_peek_2nd_token (parser)->keyword 4045 == RID_EXTENSION)) 4046 c_parser_consume_token (parser); 4047 if (c_token_starts_declspecs (c_parser_peek_2nd_token (parser))) 4048 { 4049 int ext; 4050 ext = disable_extension_diagnostics (); 4051 c_parser_consume_token (parser); 4052 c_parser_declaration_or_fndef (parser, true, true, true, true); 4053 restore_extension_diagnostics (ext); 4054 check_for_loop_decls (); 4055 } 4056 else 4057 goto init_expr; 4058 } 4059 else 4060 { 4061 init_expr: 4062 c_finish_expr_stmt (c_parser_expression (parser).value); 4063 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 4064 } 4065 /* Parse the loop condition. */ 4066 loc = c_parser_peek_token (parser)->location; 4067 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 4068 { 4069 c_parser_consume_token (parser); 4070 cond = NULL_TREE; 4071 } 4072 else 4073 { 4074 tree ocond = c_parser_expression_conv (parser).value; 4075 cond = c_objc_common_truthvalue_conversion (ocond); 4076 if (EXPR_P (cond)) 4077 SET_EXPR_LOCATION (cond, loc); 4078 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 4079 } 4080 /* Parse the increment expression. */ 4081 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4082 incr = c_process_expr_stmt (NULL_TREE); 4083 else 4084 incr = c_process_expr_stmt (c_parser_expression (parser).value); 4085 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 4086 } 4087 else 4088 { 4089 cond = error_mark_node; 4090 incr = error_mark_node; 4091 } 4092 save_break = c_break_label; 4093 c_break_label = NULL_TREE; 4094 save_cont = c_cont_label; 4095 c_cont_label = NULL_TREE; 4096 body = c_parser_c99_block_statement (parser); 4097 c_finish_loop (loc, cond, incr, body, c_break_label, c_cont_label, true); 4098 add_stmt (c_end_compound_stmt (block, flag_isoc99)); 4099 c_break_label = save_break; 4100 c_cont_label = save_cont; 4101} 4102 4103/* Parse an asm statement, a GNU extension. This is a full-blown asm 4104 statement with inputs, outputs, clobbers, and volatile tag 4105 allowed. 4106 4107 asm-statement: 4108 asm type-qualifier[opt] ( asm-argument ) ; 4109 4110 asm-argument: 4111 asm-string-literal 4112 asm-string-literal : asm-operands[opt] 4113 asm-string-literal : asm-operands[opt] : asm-operands[opt] 4114 asm-string-literal : asm-operands[opt] : asm-operands[opt] : asm-clobbers 4115 4116 Qualifiers other than volatile are accepted in the syntax but 4117 warned for. */ 4118 4119static tree 4120c_parser_asm_statement (c_parser *parser) 4121{ 4122 tree quals, str, outputs, inputs, clobbers, ret; 4123 bool simple; 4124 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ASM)); 4125 c_parser_consume_token (parser); 4126 if (c_parser_next_token_is_keyword (parser, RID_VOLATILE)) 4127 { 4128 quals = c_parser_peek_token (parser)->value; 4129 c_parser_consume_token (parser); 4130 } 4131 else if (c_parser_next_token_is_keyword (parser, RID_CONST) 4132 || c_parser_next_token_is_keyword (parser, RID_RESTRICT)) 4133 { 4134 warning (0, "%E qualifier ignored on asm", 4135 c_parser_peek_token (parser)->value); 4136 quals = NULL_TREE; 4137 c_parser_consume_token (parser); 4138 } 4139 else 4140 quals = NULL_TREE; 4141 /* ??? Follow the C++ parser rather than using the 4142 c_lex_string_translate kludge. */ 4143 c_lex_string_translate = 0; 4144 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 4145 { 4146 c_lex_string_translate = 1; 4147 return NULL_TREE; 4148 } 4149 str = c_parser_asm_string_literal (parser); 4150 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4151 { 4152 simple = true; 4153 outputs = NULL_TREE; 4154 inputs = NULL_TREE; 4155 clobbers = NULL_TREE; 4156 goto done_asm; 4157 } 4158 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) 4159 { 4160 c_lex_string_translate = 1; 4161 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4162 return NULL_TREE; 4163 } 4164 simple = false; 4165 /* Parse outputs. */ 4166 if (c_parser_next_token_is (parser, CPP_COLON) 4167 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4168 outputs = NULL_TREE; 4169 else 4170 outputs = c_parser_asm_operands (parser, false); 4171 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4172 { 4173 inputs = NULL_TREE; 4174 clobbers = NULL_TREE; 4175 goto done_asm; 4176 } 4177 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) 4178 { 4179 c_lex_string_translate = 1; 4180 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4181 return NULL_TREE; 4182 } 4183 /* Parse inputs. */ 4184 if (c_parser_next_token_is (parser, CPP_COLON) 4185 || c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4186 inputs = NULL_TREE; 4187 else 4188 inputs = c_parser_asm_operands (parser, true); 4189 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 4190 { 4191 clobbers = NULL_TREE; 4192 goto done_asm; 4193 } 4194 if (!c_parser_require (parser, CPP_COLON, "expected %<:%> or %<)%>")) 4195 { 4196 c_lex_string_translate = 1; 4197 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4198 return NULL_TREE; 4199 } 4200 /* Parse clobbers. */ 4201 clobbers = c_parser_asm_clobbers (parser); 4202 done_asm: 4203 c_lex_string_translate = 1; 4204 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) 4205 { 4206 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4207 return NULL_TREE; 4208 } 4209 if (!c_parser_require (parser, CPP_SEMICOLON, "expected %<;%>")) 4210 c_parser_skip_to_end_of_block_or_statement (parser); 4211 ret = build_asm_stmt (quals, build_asm_expr (str, outputs, inputs, 4212 clobbers, simple)); 4213 return ret; 4214} 4215 4216/* Parse asm operands, a GNU extension. If CONVERT_P (for inputs but 4217 not outputs), apply the default conversion of functions and arrays 4218 to pointers. 4219 4220 asm-operands: 4221 asm-operand 4222 asm-operands , asm-operand 4223 4224 asm-operand: 4225 asm-string-literal ( expression ) 4226 [ identifier ] asm-string-literal ( expression ) 4227*/ 4228 4229static tree 4230c_parser_asm_operands (c_parser *parser, bool convert_p) 4231{ 4232 tree list = NULL_TREE; 4233 while (true) 4234 { 4235 tree name, str; 4236 struct c_expr expr; 4237 if (c_parser_next_token_is (parser, CPP_OPEN_SQUARE)) 4238 { 4239 c_parser_consume_token (parser); 4240 if (c_parser_next_token_is (parser, CPP_NAME)) 4241 { 4242 tree id = c_parser_peek_token (parser)->value; 4243 c_parser_consume_token (parser); 4244 name = build_string (IDENTIFIER_LENGTH (id), 4245 IDENTIFIER_POINTER (id)); 4246 } 4247 else 4248 { 4249 c_parser_error (parser, "expected identifier"); 4250 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, NULL); 4251 return NULL_TREE; 4252 } 4253 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 4254 "expected %<]%>"); 4255 } 4256 else 4257 name = NULL_TREE; 4258 str = c_parser_asm_string_literal (parser); 4259 if (str == NULL_TREE) 4260 return NULL_TREE; 4261 c_lex_string_translate = 1; 4262 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 4263 { 4264 c_lex_string_translate = 0; 4265 return NULL_TREE; 4266 } 4267 expr = c_parser_expression (parser); 4268 if (convert_p) 4269 expr = default_function_array_conversion (expr); 4270 c_lex_string_translate = 0; 4271 if (!c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>")) 4272 { 4273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 4274 return NULL_TREE; 4275 } 4276 list = chainon (list, build_tree_list (build_tree_list (name, str), 4277 expr.value)); 4278 if (c_parser_next_token_is (parser, CPP_COMMA)) 4279 c_parser_consume_token (parser); 4280 else 4281 break; 4282 } 4283 return list; 4284} 4285 4286/* Parse asm clobbers, a GNU extension. 4287 4288 asm-clobbers: 4289 asm-string-literal 4290 asm-clobbers , asm-string-literal 4291*/ 4292 4293static tree 4294c_parser_asm_clobbers (c_parser *parser) 4295{ 4296 tree list = NULL_TREE; 4297 while (true) 4298 { 4299 tree str = c_parser_asm_string_literal (parser); 4300 if (str) 4301 list = tree_cons (NULL_TREE, str, list); 4302 else 4303 return NULL_TREE; 4304 if (c_parser_next_token_is (parser, CPP_COMMA)) 4305 c_parser_consume_token (parser); 4306 else 4307 break; 4308 } 4309 return list; 4310} 4311 4312/* Parse an expression other than a compound expression; that is, an 4313 assignment expression (C90 6.3.16, C99 6.5.16). If AFTER is not 4314 NULL then it is an Objective-C message expression which is the 4315 primary-expression starting the expression as an initializer. 4316 4317 assignment-expression: 4318 conditional-expression 4319 unary-expression assignment-operator assignment-expression 4320 4321 assignment-operator: one of 4322 = *= /= %= += -= <<= >>= &= ^= |= 4323 4324 In GNU C we accept any conditional expression on the LHS and 4325 diagnose the invalid lvalue rather than producing a syntax 4326 error. */ 4327 4328static struct c_expr 4329c_parser_expr_no_commas (c_parser *parser, struct c_expr *after) 4330{ 4331 struct c_expr lhs, rhs, ret; 4332 enum tree_code code; 4333 gcc_assert (!after || c_dialect_objc ()); 4334 lhs = c_parser_conditional_expression (parser, after); 4335 switch (c_parser_peek_token (parser)->type) 4336 { 4337 case CPP_EQ: 4338 code = NOP_EXPR; 4339 break; 4340 case CPP_MULT_EQ: 4341 code = MULT_EXPR; 4342 break; 4343 case CPP_DIV_EQ: 4344 code = TRUNC_DIV_EXPR; 4345 break; 4346 case CPP_MOD_EQ: 4347 code = TRUNC_MOD_EXPR; 4348 break; 4349 case CPP_PLUS_EQ: 4350 code = PLUS_EXPR; 4351 break; 4352 case CPP_MINUS_EQ: 4353 code = MINUS_EXPR; 4354 break; 4355 case CPP_LSHIFT_EQ: 4356 code = LSHIFT_EXPR; 4357 break; 4358 case CPP_RSHIFT_EQ: 4359 code = RSHIFT_EXPR; 4360 break; 4361 case CPP_AND_EQ: 4362 code = BIT_AND_EXPR; 4363 break; 4364 case CPP_XOR_EQ: 4365 code = BIT_XOR_EXPR; 4366 break; 4367 case CPP_OR_EQ: 4368 code = BIT_IOR_EXPR; 4369 break; 4370 default: 4371 return lhs; 4372 } 4373 c_parser_consume_token (parser); 4374 rhs = c_parser_expr_no_commas (parser, NULL); 4375 rhs = default_function_array_conversion (rhs); 4376 ret.value = build_modify_expr (lhs.value, code, rhs.value); 4377 if (code == NOP_EXPR) 4378 ret.original_code = MODIFY_EXPR; 4379 else 4380 { 4381 TREE_NO_WARNING (ret.value) = 1; 4382 ret.original_code = ERROR_MARK; 4383 } 4384 return ret; 4385} 4386 4387/* Parse a conditional expression (C90 6.3.15, C99 6.5.15). If AFTER 4388 is not NULL then it is an Objective-C message expression which is 4389 the primary-expression starting the expression as an initializer. 4390 4391 conditional-expression: 4392 logical-OR-expression 4393 logical-OR-expression ? expression : conditional-expression 4394 4395 GNU extensions: 4396 4397 conditional-expression: 4398 logical-OR-expression ? : conditional-expression 4399*/ 4400 4401static struct c_expr 4402c_parser_conditional_expression (c_parser *parser, struct c_expr *after) 4403{ 4404 struct c_expr cond, exp1, exp2, ret; 4405 gcc_assert (!after || c_dialect_objc ()); 4406 cond = c_parser_binary_expression (parser, after); 4407 if (c_parser_next_token_is_not (parser, CPP_QUERY)) 4408 return cond; 4409 cond = default_function_array_conversion (cond); 4410 c_parser_consume_token (parser); 4411 if (c_parser_next_token_is (parser, CPP_COLON)) 4412 { 4413 if (pedantic) 4414 pedwarn ("ISO C forbids omitting the middle term of a ?: expression"); 4415 /* Make sure first operand is calculated only once. */ 4416 exp1.value = save_expr (default_conversion (cond.value)); 4417 cond.value = c_objc_common_truthvalue_conversion (exp1.value); 4418 skip_evaluation += cond.value == truthvalue_true_node; 4419 } 4420 else 4421 { 4422 cond.value 4423 = c_objc_common_truthvalue_conversion 4424 (default_conversion (cond.value)); 4425 skip_evaluation += cond.value == truthvalue_false_node; 4426 exp1 = c_parser_expression_conv (parser); 4427 skip_evaluation += ((cond.value == truthvalue_true_node) 4428 - (cond.value == truthvalue_false_node)); 4429 } 4430 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) 4431 { 4432 skip_evaluation -= cond.value == truthvalue_true_node; 4433 ret.value = error_mark_node; 4434 ret.original_code = ERROR_MARK; 4435 return ret; 4436 } 4437 exp2 = c_parser_conditional_expression (parser, NULL); 4438 exp2 = default_function_array_conversion (exp2); 4439 skip_evaluation -= cond.value == truthvalue_true_node; 4440 ret.value = build_conditional_expr (cond.value, exp1.value, exp2.value); 4441 ret.original_code = ERROR_MARK; 4442 return ret; 4443} 4444 4445/* Parse a binary expression; that is, a logical-OR-expression (C90 4446 6.3.5-6.3.14, C99 6.5.5-6.5.14). If AFTER is not NULL then it is 4447 an Objective-C message expression which is the primary-expression 4448 starting the expression as an initializer. 4449 4450 multiplicative-expression: 4451 cast-expression 4452 multiplicative-expression * cast-expression 4453 multiplicative-expression / cast-expression 4454 multiplicative-expression % cast-expression 4455 4456 additive-expression: 4457 multiplicative-expression 4458 additive-expression + multiplicative-expression 4459 additive-expression - multiplicative-expression 4460 4461 shift-expression: 4462 additive-expression 4463 shift-expression << additive-expression 4464 shift-expression >> additive-expression 4465 4466 relational-expression: 4467 shift-expression 4468 relational-expression < shift-expression 4469 relational-expression > shift-expression 4470 relational-expression <= shift-expression 4471 relational-expression >= shift-expression 4472 4473 equality-expression: 4474 relational-expression 4475 equality-expression == relational-expression 4476 equality-expression != relational-expression 4477 4478 AND-expression: 4479 equality-expression 4480 AND-expression & equality-expression 4481 4482 exclusive-OR-expression: 4483 AND-expression 4484 exclusive-OR-expression ^ AND-expression 4485 4486 inclusive-OR-expression: 4487 exclusive-OR-expression 4488 inclusive-OR-expression | exclusive-OR-expression 4489 4490 logical-AND-expression: 4491 inclusive-OR-expression 4492 logical-AND-expression && inclusive-OR-expression 4493 4494 logical-OR-expression: 4495 logical-AND-expression 4496 logical-OR-expression || logical-AND-expression 4497*/ 4498 4499static struct c_expr 4500c_parser_binary_expression (c_parser *parser, struct c_expr *after) 4501{ 4502 /* A binary expression is parsed using operator-precedence parsing, 4503 with the operands being cast expressions. All the binary 4504 operators are left-associative. Thus a binary expression is of 4505 form: 4506 4507 E0 op1 E1 op2 E2 ... 4508 4509 which we represent on a stack. On the stack, the precedence 4510 levels are strictly increasing. When a new operator is 4511 encountered of higher precedence than that at the top of the 4512 stack, it is pushed; its LHS is the top expression, and its RHS 4513 is everything parsed until it is popped. When a new operator is 4514 encountered with precedence less than or equal to that at the top 4515 of the stack, triples E[i-1] op[i] E[i] are popped and replaced 4516 by the result of the operation until the operator at the top of 4517 the stack has lower precedence than the new operator or there is 4518 only one element on the stack; then the top expression is the LHS 4519 of the new operator. In the case of logical AND and OR 4520 expressions, we also need to adjust skip_evaluation as 4521 appropriate when the operators are pushed and popped. */ 4522 4523 /* The precedence levels, where 0 is a dummy lowest level used for 4524 the bottom of the stack. */ 4525 enum prec { 4526 PREC_NONE, 4527 PREC_LOGOR, 4528 PREC_LOGAND, 4529 PREC_BITOR, 4530 PREC_BITXOR, 4531 PREC_BITAND, 4532 PREC_EQ, 4533 PREC_REL, 4534 PREC_SHIFT, 4535 PREC_ADD, 4536 PREC_MULT, 4537 NUM_PRECS 4538 }; 4539 struct { 4540 /* The expression at this stack level. */ 4541 struct c_expr expr; 4542 /* The precedence of the operator on its left, PREC_NONE at the 4543 bottom of the stack. */ 4544 enum prec prec; 4545 /* The operation on its left. */ 4546 enum tree_code op; 4547 } stack[NUM_PRECS]; 4548 int sp; 4549#define POP \ 4550 do { \ 4551 switch (stack[sp].op) \ 4552 { \ 4553 case TRUTH_ANDIF_EXPR: \ 4554 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_false_node; \ 4555 break; \ 4556 case TRUTH_ORIF_EXPR: \ 4557 skip_evaluation -= stack[sp - 1].expr.value == truthvalue_true_node; \ 4558 break; \ 4559 default: \ 4560 break; \ 4561 } \ 4562 stack[sp - 1].expr \ 4563 = default_function_array_conversion (stack[sp - 1].expr); \ 4564 stack[sp].expr \ 4565 = default_function_array_conversion (stack[sp].expr); \ 4566 stack[sp - 1].expr = parser_build_binary_op (stack[sp].op, \ 4567 stack[sp - 1].expr, \ 4568 stack[sp].expr); \ 4569 sp--; \ 4570 } while (0) 4571 gcc_assert (!after || c_dialect_objc ()); 4572 stack[0].expr = c_parser_cast_expression (parser, after); 4573 stack[0].prec = PREC_NONE; 4574 sp = 0; 4575 while (true) 4576 { 4577 enum prec oprec; 4578 enum tree_code ocode; 4579 if (parser->error) 4580 goto out; 4581 switch (c_parser_peek_token (parser)->type) 4582 { 4583 case CPP_MULT: 4584 oprec = PREC_MULT; 4585 ocode = MULT_EXPR; 4586 break; 4587 case CPP_DIV: 4588 oprec = PREC_MULT; 4589 ocode = TRUNC_DIV_EXPR; 4590 break; 4591 case CPP_MOD: 4592 oprec = PREC_MULT; 4593 ocode = TRUNC_MOD_EXPR; 4594 break; 4595 case CPP_PLUS: 4596 oprec = PREC_ADD; 4597 ocode = PLUS_EXPR; 4598 break; 4599 case CPP_MINUS: 4600 oprec = PREC_ADD; 4601 ocode = MINUS_EXPR; 4602 break; 4603 case CPP_LSHIFT: 4604 oprec = PREC_SHIFT; 4605 ocode = LSHIFT_EXPR; 4606 break; 4607 case CPP_RSHIFT: 4608 oprec = PREC_SHIFT; 4609 ocode = RSHIFT_EXPR; 4610 break; 4611 case CPP_LESS: 4612 oprec = PREC_REL; 4613 ocode = LT_EXPR; 4614 break; 4615 case CPP_GREATER: 4616 oprec = PREC_REL; 4617 ocode = GT_EXPR; 4618 break; 4619 case CPP_LESS_EQ: 4620 oprec = PREC_REL; 4621 ocode = LE_EXPR; 4622 break; 4623 case CPP_GREATER_EQ: 4624 oprec = PREC_REL; 4625 ocode = GE_EXPR; 4626 break; 4627 case CPP_EQ_EQ: 4628 oprec = PREC_EQ; 4629 ocode = EQ_EXPR; 4630 break; 4631 case CPP_NOT_EQ: 4632 oprec = PREC_EQ; 4633 ocode = NE_EXPR; 4634 break; 4635 case CPP_AND: 4636 oprec = PREC_BITAND; 4637 ocode = BIT_AND_EXPR; 4638 break; 4639 case CPP_XOR: 4640 oprec = PREC_BITXOR; 4641 ocode = BIT_XOR_EXPR; 4642 break; 4643 case CPP_OR: 4644 oprec = PREC_BITOR; 4645 ocode = BIT_IOR_EXPR; 4646 break; 4647 case CPP_AND_AND: 4648 oprec = PREC_LOGAND; 4649 ocode = TRUTH_ANDIF_EXPR; 4650 break; 4651 case CPP_OR_OR: 4652 oprec = PREC_LOGOR; 4653 ocode = TRUTH_ORIF_EXPR; 4654 break; 4655 default: 4656 /* Not a binary operator, so end of the binary 4657 expression. */ 4658 goto out; 4659 } 4660 c_parser_consume_token (parser); 4661 while (oprec <= stack[sp].prec) 4662 POP; 4663 switch (ocode) 4664 { 4665 case TRUTH_ANDIF_EXPR: 4666 stack[sp].expr 4667 = default_function_array_conversion (stack[sp].expr); 4668 stack[sp].expr.value = c_objc_common_truthvalue_conversion 4669 (default_conversion (stack[sp].expr.value)); 4670 skip_evaluation += stack[sp].expr.value == truthvalue_false_node; 4671 break; 4672 case TRUTH_ORIF_EXPR: 4673 stack[sp].expr 4674 = default_function_array_conversion (stack[sp].expr); 4675 stack[sp].expr.value = c_objc_common_truthvalue_conversion 4676 (default_conversion (stack[sp].expr.value)); 4677 skip_evaluation += stack[sp].expr.value == truthvalue_true_node; 4678 break; 4679 default: 4680 break; 4681 } 4682 sp++; 4683 stack[sp].expr = c_parser_cast_expression (parser, NULL); 4684 stack[sp].prec = oprec; 4685 stack[sp].op = ocode; 4686 } 4687 out: 4688 while (sp > 0) 4689 POP; 4690 return stack[0].expr; 4691#undef POP 4692} 4693 4694/* Parse a cast expression (C90 6.3.4, C99 6.5.4). If AFTER is not 4695 NULL then it is an Objective-C message expression which is the 4696 primary-expression starting the expression as an initializer. 4697 4698 cast-expression: 4699 unary-expression 4700 ( type-name ) unary-expression 4701*/ 4702 4703static struct c_expr 4704c_parser_cast_expression (c_parser *parser, struct c_expr *after) 4705{ 4706 gcc_assert (!after || c_dialect_objc ()); 4707 if (after) 4708 return c_parser_postfix_expression_after_primary (parser, *after); 4709 /* If the expression begins with a parenthesized type name, it may 4710 be either a cast or a compound literal; we need to see whether 4711 the next character is '{' to tell the difference. If not, it is 4712 an unary expression. */ 4713 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) 4714 && c_token_starts_typename (c_parser_peek_2nd_token (parser))) 4715 { 4716 struct c_type_name *type_name; 4717 struct c_expr ret; 4718 struct c_expr expr; 4719 c_parser_consume_token (parser); 4720 type_name = c_parser_type_name (parser); 4721 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 4722 if (type_name == NULL) 4723 { 4724 ret.value = error_mark_node; 4725 ret.original_code = ERROR_MARK; 4726 return ret; 4727 } 4728 4729 /* Save casted types in the function's used types hash table. */ 4730 used_types_insert (type_name->specs->type); 4731 4732 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 4733 return c_parser_postfix_expression_after_paren_type (parser, 4734 type_name); 4735 expr = c_parser_cast_expression (parser, NULL); 4736 expr = default_function_array_conversion (expr); 4737 ret.value = c_cast_expr (type_name, expr.value); 4738 ret.original_code = ERROR_MARK; 4739 return ret; 4740 } 4741 else 4742 return c_parser_unary_expression (parser); 4743} 4744 4745/* Parse an unary expression (C90 6.3.3, C99 6.5.3). 4746 4747 unary-expression: 4748 postfix-expression 4749 ++ unary-expression 4750 -- unary-expression 4751 unary-operator cast-expression 4752 sizeof unary-expression 4753 sizeof ( type-name ) 4754 4755 unary-operator: one of 4756 & * + - ~ ! 4757 4758 GNU extensions: 4759 4760 unary-expression: 4761 __alignof__ unary-expression 4762 __alignof__ ( type-name ) 4763 && identifier 4764 4765 unary-operator: one of 4766 __extension__ __real__ __imag__ 4767 4768 In addition, the GNU syntax treats ++ and -- as unary operators, so 4769 they may be applied to cast expressions with errors for non-lvalues 4770 given later. */ 4771 4772static struct c_expr 4773c_parser_unary_expression (c_parser *parser) 4774{ 4775 int ext; 4776 struct c_expr ret, op; 4777 switch (c_parser_peek_token (parser)->type) 4778 { 4779 case CPP_PLUS_PLUS: 4780 c_parser_consume_token (parser); 4781 op = c_parser_cast_expression (parser, NULL); 4782 op = default_function_array_conversion (op); 4783 return parser_build_unary_op (PREINCREMENT_EXPR, op); 4784 case CPP_MINUS_MINUS: 4785 c_parser_consume_token (parser); 4786 op = c_parser_cast_expression (parser, NULL); 4787 op = default_function_array_conversion (op); 4788 return parser_build_unary_op (PREDECREMENT_EXPR, op); 4789 case CPP_AND: 4790 c_parser_consume_token (parser); 4791 return parser_build_unary_op (ADDR_EXPR, 4792 c_parser_cast_expression (parser, NULL)); 4793 case CPP_MULT: 4794 c_parser_consume_token (parser); 4795 op = c_parser_cast_expression (parser, NULL); 4796 op = default_function_array_conversion (op); 4797 ret.value = build_indirect_ref (op.value, "unary *"); 4798 ret.original_code = ERROR_MARK; 4799 return ret; 4800 case CPP_PLUS: 4801 c_parser_consume_token (parser); 4802 if (!c_dialect_objc () && !in_system_header) 4803 warning (OPT_Wtraditional, 4804 "traditional C rejects the unary plus operator"); 4805 op = c_parser_cast_expression (parser, NULL); 4806 op = default_function_array_conversion (op); 4807 return parser_build_unary_op (CONVERT_EXPR, op); 4808 case CPP_MINUS: 4809 c_parser_consume_token (parser); 4810 op = c_parser_cast_expression (parser, NULL); 4811 op = default_function_array_conversion (op); 4812 return parser_build_unary_op (NEGATE_EXPR, op); 4813 case CPP_COMPL: 4814 c_parser_consume_token (parser); 4815 op = c_parser_cast_expression (parser, NULL); 4816 op = default_function_array_conversion (op); 4817 return parser_build_unary_op (BIT_NOT_EXPR, op); 4818 case CPP_NOT: 4819 c_parser_consume_token (parser); 4820 op = c_parser_cast_expression (parser, NULL); 4821 op = default_function_array_conversion (op); 4822 return parser_build_unary_op (TRUTH_NOT_EXPR, op); 4823 case CPP_AND_AND: 4824 /* Refer to the address of a label as a pointer. */ 4825 c_parser_consume_token (parser); 4826 if (c_parser_next_token_is (parser, CPP_NAME)) 4827 { 4828 ret.value = finish_label_address_expr 4829 (c_parser_peek_token (parser)->value); 4830 c_parser_consume_token (parser); 4831 } 4832 else 4833 { 4834 c_parser_error (parser, "expected identifier"); 4835 ret.value = error_mark_node; 4836 } 4837 ret.original_code = ERROR_MARK; 4838 return ret; 4839 case CPP_KEYWORD: 4840 switch (c_parser_peek_token (parser)->keyword) 4841 { 4842 case RID_SIZEOF: 4843 return c_parser_sizeof_expression (parser); 4844 case RID_ALIGNOF: 4845 return c_parser_alignof_expression (parser); 4846 case RID_EXTENSION: 4847 c_parser_consume_token (parser); 4848 ext = disable_extension_diagnostics (); 4849 ret = c_parser_cast_expression (parser, NULL); 4850 restore_extension_diagnostics (ext); 4851 return ret; 4852 case RID_REALPART: 4853 c_parser_consume_token (parser); 4854 op = c_parser_cast_expression (parser, NULL); 4855 op = default_function_array_conversion (op); 4856 return parser_build_unary_op (REALPART_EXPR, op); 4857 case RID_IMAGPART: 4858 c_parser_consume_token (parser); 4859 op = c_parser_cast_expression (parser, NULL); 4860 op = default_function_array_conversion (op); 4861 return parser_build_unary_op (IMAGPART_EXPR, op); 4862 default: 4863 return c_parser_postfix_expression (parser); 4864 } 4865 default: 4866 return c_parser_postfix_expression (parser); 4867 } 4868} 4869 4870/* Parse a sizeof expression. */ 4871 4872static struct c_expr 4873c_parser_sizeof_expression (c_parser *parser) 4874{ 4875 struct c_expr expr; 4876 gcc_assert (c_parser_next_token_is_keyword (parser, RID_SIZEOF)); 4877 c_parser_consume_token (parser); 4878 skip_evaluation++; 4879 in_sizeof++; 4880 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) 4881 && c_token_starts_typename (c_parser_peek_2nd_token (parser))) 4882 { 4883 /* Either sizeof ( type-name ) or sizeof unary-expression 4884 starting with a compound literal. */ 4885 struct c_type_name *type_name; 4886 c_parser_consume_token (parser); 4887 type_name = c_parser_type_name (parser); 4888 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 4889 if (type_name == NULL) 4890 { 4891 struct c_expr ret; 4892 skip_evaluation--; 4893 in_sizeof--; 4894 ret.value = error_mark_node; 4895 ret.original_code = ERROR_MARK; 4896 return ret; 4897 } 4898 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 4899 { 4900 expr = c_parser_postfix_expression_after_paren_type (parser, 4901 type_name); 4902 goto sizeof_expr; 4903 } 4904 /* sizeof ( type-name ). */ 4905 skip_evaluation--; 4906 in_sizeof--; 4907 if (type_name->declarator->kind == cdk_array 4908 && type_name->declarator->u.array.vla_unspec_p) 4909 { 4910 /* C99 6.7.5.2p4 */ 4911 error ("%<[*]%> not allowed in other than a declaration"); 4912 } 4913 return c_expr_sizeof_type (type_name); 4914 } 4915 else 4916 { 4917 expr = c_parser_unary_expression (parser); 4918 sizeof_expr: 4919 skip_evaluation--; 4920 in_sizeof--; 4921 if (TREE_CODE (expr.value) == COMPONENT_REF 4922 && DECL_C_BIT_FIELD (TREE_OPERAND (expr.value, 1))) 4923 error ("%<sizeof%> applied to a bit-field"); 4924 return c_expr_sizeof_expr (expr); 4925 } 4926} 4927 4928/* Parse an alignof expression. */ 4929 4930static struct c_expr 4931c_parser_alignof_expression (c_parser *parser) 4932{ 4933 struct c_expr expr; 4934 gcc_assert (c_parser_next_token_is_keyword (parser, RID_ALIGNOF)); 4935 c_parser_consume_token (parser); 4936 skip_evaluation++; 4937 in_alignof++; 4938 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN) 4939 && c_token_starts_typename (c_parser_peek_2nd_token (parser))) 4940 { 4941 /* Either __alignof__ ( type-name ) or __alignof__ 4942 unary-expression starting with a compound literal. */ 4943 struct c_type_name *type_name; 4944 struct c_expr ret; 4945 c_parser_consume_token (parser); 4946 type_name = c_parser_type_name (parser); 4947 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 4948 if (type_name == NULL) 4949 { 4950 struct c_expr ret; 4951 skip_evaluation--; 4952 in_alignof--; 4953 ret.value = error_mark_node; 4954 ret.original_code = ERROR_MARK; 4955 return ret; 4956 } 4957 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 4958 { 4959 expr = c_parser_postfix_expression_after_paren_type (parser, 4960 type_name); 4961 goto alignof_expr; 4962 } 4963 /* alignof ( type-name ). */ 4964 skip_evaluation--; 4965 in_alignof--; 4966 ret.value = c_alignof (groktypename (type_name)); 4967 ret.original_code = ERROR_MARK; 4968 return ret; 4969 } 4970 else 4971 { 4972 struct c_expr ret; 4973 expr = c_parser_unary_expression (parser); 4974 alignof_expr: 4975 skip_evaluation--; 4976 in_alignof--; 4977 ret.value = c_alignof_expr (expr.value); 4978 ret.original_code = ERROR_MARK; 4979 return ret; 4980 } 4981} 4982 4983/* Parse a postfix expression (C90 6.3.1-6.3.2, C99 6.5.1-6.5.2). 4984 4985 postfix-expression: 4986 primary-expression 4987 postfix-expression [ expression ] 4988 postfix-expression ( argument-expression-list[opt] ) 4989 postfix-expression . identifier 4990 postfix-expression -> identifier 4991 postfix-expression ++ 4992 postfix-expression -- 4993 ( type-name ) { initializer-list } 4994 ( type-name ) { initializer-list , } 4995 4996 argument-expression-list: 4997 argument-expression 4998 argument-expression-list , argument-expression 4999 5000 primary-expression: 5001 identifier 5002 constant 5003 string-literal 5004 ( expression ) 5005 5006 GNU extensions: 5007 5008 primary-expression: 5009 __func__ 5010 (treated as a keyword in GNU C) 5011 __FUNCTION__ 5012 __PRETTY_FUNCTION__ 5013 ( compound-statement ) 5014 __builtin_va_arg ( assignment-expression , type-name ) 5015 __builtin_offsetof ( type-name , offsetof-member-designator ) 5016 __builtin_choose_expr ( assignment-expression , 5017 assignment-expression , 5018 assignment-expression ) 5019 __builtin_types_compatible_p ( type-name , type-name ) 5020 5021 offsetof-member-designator: 5022 identifier 5023 offsetof-member-designator . identifier 5024 offsetof-member-designator [ expression ] 5025 5026 Objective-C: 5027 5028 primary-expression: 5029 [ objc-receiver objc-message-args ] 5030 @selector ( objc-selector-arg ) 5031 @protocol ( identifier ) 5032 @encode ( type-name ) 5033 objc-string-literal 5034*/ 5035 5036static struct c_expr 5037c_parser_postfix_expression (c_parser *parser) 5038{ 5039 struct c_expr expr, e1, e2, e3; 5040 struct c_type_name *t1, *t2; 5041 switch (c_parser_peek_token (parser)->type) 5042 { 5043 case CPP_NUMBER: 5044 case CPP_CHAR: 5045 case CPP_WCHAR: 5046 expr.value = c_parser_peek_token (parser)->value; 5047 expr.original_code = ERROR_MARK; 5048 c_parser_consume_token (parser); 5049 break; 5050 case CPP_STRING: 5051 case CPP_WSTRING: 5052 expr.value = c_parser_peek_token (parser)->value; 5053 expr.original_code = STRING_CST; 5054 c_parser_consume_token (parser); 5055 break; 5056 case CPP_OBJC_STRING: 5057 gcc_assert (c_dialect_objc ()); 5058 expr.value 5059 = objc_build_string_object (c_parser_peek_token (parser)->value); 5060 expr.original_code = ERROR_MARK; 5061 c_parser_consume_token (parser); 5062 break; 5063 case CPP_NAME: 5064 if (c_parser_peek_token (parser)->id_kind != C_ID_ID) 5065 { 5066 c_parser_error (parser, "expected expression"); 5067 expr.value = error_mark_node; 5068 expr.original_code = ERROR_MARK; 5069 break; 5070 } 5071 { 5072 tree id = c_parser_peek_token (parser)->value; 5073 location_t loc = c_parser_peek_token (parser)->location; 5074 c_parser_consume_token (parser); 5075 expr.value = build_external_ref (id, 5076 (c_parser_peek_token (parser)->type 5077 == CPP_OPEN_PAREN), loc); 5078 expr.original_code = ERROR_MARK; 5079 } 5080 break; 5081 case CPP_OPEN_PAREN: 5082 /* A parenthesized expression, statement expression or compound 5083 literal. */ 5084 if (c_parser_peek_2nd_token (parser)->type == CPP_OPEN_BRACE) 5085 { 5086 /* A statement expression. */ 5087 tree stmt; 5088 c_parser_consume_token (parser); 5089 c_parser_consume_token (parser); 5090 if (cur_stmt_list == NULL) 5091 { 5092 error ("braced-group within expression allowed " 5093 "only inside a function"); 5094 parser->error = true; 5095 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, NULL); 5096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5097 expr.value = error_mark_node; 5098 expr.original_code = ERROR_MARK; 5099 break; 5100 } 5101 stmt = c_begin_stmt_expr (); 5102 c_parser_compound_statement_nostart (parser); 5103 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5104 "expected %<)%>"); 5105 if (pedantic) 5106 pedwarn ("ISO C forbids braced-groups within expressions"); 5107 expr.value = c_finish_stmt_expr (stmt); 5108 expr.original_code = ERROR_MARK; 5109 } 5110 else if (c_token_starts_typename (c_parser_peek_2nd_token (parser))) 5111 { 5112 /* A compound literal. ??? Can we actually get here rather 5113 than going directly to 5114 c_parser_postfix_expression_after_paren_type from 5115 elsewhere? */ 5116 struct c_type_name *type_name; 5117 c_parser_consume_token (parser); 5118 type_name = c_parser_type_name (parser); 5119 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5120 "expected %<)%>"); 5121 if (type_name == NULL) 5122 { 5123 expr.value = error_mark_node; 5124 expr.original_code = ERROR_MARK; 5125 } 5126 else 5127 expr = c_parser_postfix_expression_after_paren_type (parser, 5128 type_name); 5129 } 5130 else 5131 { 5132 /* A parenthesized expression. */ 5133 c_parser_consume_token (parser); 5134 expr = c_parser_expression (parser); 5135 if (TREE_CODE (expr.value) == MODIFY_EXPR) 5136 TREE_NO_WARNING (expr.value) = 1; 5137 expr.original_code = ERROR_MARK; 5138 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5139 "expected %<)%>"); 5140 } 5141 break; 5142 case CPP_KEYWORD: 5143 switch (c_parser_peek_token (parser)->keyword) 5144 { 5145 case RID_FUNCTION_NAME: 5146 case RID_PRETTY_FUNCTION_NAME: 5147 case RID_C99_FUNCTION_NAME: 5148 expr.value = fname_decl (c_parser_peek_token (parser)->keyword, 5149 c_parser_peek_token (parser)->value); 5150 expr.original_code = ERROR_MARK; 5151 c_parser_consume_token (parser); 5152 break; 5153 case RID_VA_ARG: 5154 c_parser_consume_token (parser); 5155 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5156 { 5157 expr.value = error_mark_node; 5158 expr.original_code = ERROR_MARK; 5159 break; 5160 } 5161 e1 = c_parser_expr_no_commas (parser, NULL); 5162 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5163 { 5164 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5165 expr.value = error_mark_node; 5166 expr.original_code = ERROR_MARK; 5167 break; 5168 } 5169 t1 = c_parser_type_name (parser); 5170 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5171 "expected %<)%>"); 5172 if (t1 == NULL) 5173 { 5174 expr.value = error_mark_node; 5175 expr.original_code = ERROR_MARK; 5176 } 5177 else 5178 { 5179 expr.value = build_va_arg (e1.value, groktypename (t1)); 5180 expr.original_code = ERROR_MARK; 5181 } 5182 break; 5183 case RID_OFFSETOF: 5184 c_parser_consume_token (parser); 5185 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5186 { 5187 expr.value = error_mark_node; 5188 expr.original_code = ERROR_MARK; 5189 break; 5190 } 5191 t1 = c_parser_type_name (parser); 5192 if (t1 == NULL) 5193 { 5194 expr.value = error_mark_node; 5195 expr.original_code = ERROR_MARK; 5196 break; 5197 } 5198 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5199 { 5200 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5201 expr.value = error_mark_node; 5202 expr.original_code = ERROR_MARK; 5203 break; 5204 } 5205 { 5206 tree type = groktypename (t1); 5207 tree offsetof_ref; 5208 if (type == error_mark_node) 5209 offsetof_ref = error_mark_node; 5210 else 5211 offsetof_ref = build1 (INDIRECT_REF, type, null_pointer_node); 5212 /* Parse the second argument to __builtin_offsetof. We 5213 must have one identifier, and beyond that we want to 5214 accept sub structure and sub array references. */ 5215 if (c_parser_next_token_is (parser, CPP_NAME)) 5216 { 5217 offsetof_ref = build_component_ref 5218 (offsetof_ref, c_parser_peek_token (parser)->value); 5219 c_parser_consume_token (parser); 5220 while (c_parser_next_token_is (parser, CPP_DOT) 5221 || c_parser_next_token_is (parser, 5222 CPP_OPEN_SQUARE)) 5223 { 5224 if (c_parser_next_token_is (parser, CPP_DOT)) 5225 { 5226 c_parser_consume_token (parser); 5227 if (c_parser_next_token_is_not (parser, 5228 CPP_NAME)) 5229 { 5230 c_parser_error (parser, "expected identifier"); 5231 break; 5232 } 5233 offsetof_ref = build_component_ref 5234 (offsetof_ref, 5235 c_parser_peek_token (parser)->value); 5236 c_parser_consume_token (parser); 5237 } 5238 else 5239 { 5240 tree idx; 5241 c_parser_consume_token (parser); 5242 idx = c_parser_expression (parser).value; 5243 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 5244 "expected %<]%>"); 5245 offsetof_ref = build_array_ref (offsetof_ref, idx); 5246 } 5247 } 5248 } 5249 else 5250 c_parser_error (parser, "expected identifier"); 5251 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5252 "expected %<)%>"); 5253 expr.value = fold_offsetof (offsetof_ref, NULL_TREE); 5254 expr.original_code = ERROR_MARK; 5255 } 5256 break; 5257 case RID_CHOOSE_EXPR: 5258 c_parser_consume_token (parser); 5259 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5260 { 5261 expr.value = error_mark_node; 5262 expr.original_code = ERROR_MARK; 5263 break; 5264 } 5265 e1 = c_parser_expr_no_commas (parser, NULL); 5266 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5267 { 5268 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5269 expr.value = error_mark_node; 5270 expr.original_code = ERROR_MARK; 5271 break; 5272 } 5273 e2 = c_parser_expr_no_commas (parser, NULL); 5274 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5275 { 5276 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5277 expr.value = error_mark_node; 5278 expr.original_code = ERROR_MARK; 5279 break; 5280 } 5281 e3 = c_parser_expr_no_commas (parser, NULL); 5282 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5283 "expected %<)%>"); 5284 { 5285 tree c; 5286 5287 c = fold (e1.value); 5288 if (TREE_CODE (c) != INTEGER_CST) 5289 error ("first argument to %<__builtin_choose_expr%> not" 5290 " a constant"); 5291 expr = integer_zerop (c) ? e3 : e2; 5292 } 5293 break; 5294 case RID_TYPES_COMPATIBLE_P: 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 t1 = c_parser_type_name (parser); 5303 if (t1 == NULL) 5304 { 5305 expr.value = error_mark_node; 5306 expr.original_code = ERROR_MARK; 5307 break; 5308 } 5309 if (!c_parser_require (parser, CPP_COMMA, "expected %<,%>")) 5310 { 5311 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5312 expr.value = error_mark_node; 5313 expr.original_code = ERROR_MARK; 5314 break; 5315 } 5316 t2 = c_parser_type_name (parser); 5317 if (t2 == NULL) 5318 { 5319 expr.value = error_mark_node; 5320 expr.original_code = ERROR_MARK; 5321 break; 5322 } 5323 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5324 "expected %<)%>"); 5325 { 5326 tree e1, e2; 5327 5328 e1 = TYPE_MAIN_VARIANT (groktypename (t1)); 5329 e2 = TYPE_MAIN_VARIANT (groktypename (t2)); 5330 5331 expr.value = comptypes (e1, e2) 5332 ? build_int_cst (NULL_TREE, 1) 5333 : build_int_cst (NULL_TREE, 0); 5334 expr.original_code = ERROR_MARK; 5335 } 5336 break; 5337 case RID_AT_SELECTOR: 5338 gcc_assert (c_dialect_objc ()); 5339 c_parser_consume_token (parser); 5340 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5341 { 5342 expr.value = error_mark_node; 5343 expr.original_code = ERROR_MARK; 5344 break; 5345 } 5346 { 5347 tree sel = c_parser_objc_selector_arg (parser); 5348 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5349 "expected %<)%>"); 5350 expr.value = objc_build_selector_expr (sel); 5351 expr.original_code = ERROR_MARK; 5352 } 5353 break; 5354 case RID_AT_PROTOCOL: 5355 gcc_assert (c_dialect_objc ()); 5356 c_parser_consume_token (parser); 5357 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5358 { 5359 expr.value = error_mark_node; 5360 expr.original_code = ERROR_MARK; 5361 break; 5362 } 5363 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5364 { 5365 c_parser_error (parser, "expected identifier"); 5366 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5367 expr.value = error_mark_node; 5368 expr.original_code = ERROR_MARK; 5369 break; 5370 } 5371 { 5372 tree id = c_parser_peek_token (parser)->value; 5373 c_parser_consume_token (parser); 5374 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5375 "expected %<)%>"); 5376 expr.value = objc_build_protocol_expr (id); 5377 expr.original_code = ERROR_MARK; 5378 } 5379 break; 5380 case RID_AT_ENCODE: 5381 /* Extension to support C-structures in the archiver. */ 5382 gcc_assert (c_dialect_objc ()); 5383 c_parser_consume_token (parser); 5384 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 5385 { 5386 expr.value = error_mark_node; 5387 expr.original_code = ERROR_MARK; 5388 break; 5389 } 5390 t1 = c_parser_type_name (parser); 5391 if (t1 == NULL) 5392 { 5393 expr.value = error_mark_node; 5394 expr.original_code = ERROR_MARK; 5395 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5396 break; 5397 } 5398 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5399 "expected %<)%>"); 5400 { 5401 tree type = groktypename (t1); 5402 expr.value = objc_build_encode_expr (type); 5403 expr.original_code = ERROR_MARK; 5404 } 5405 break; 5406 default: 5407 c_parser_error (parser, "expected expression"); 5408 expr.value = error_mark_node; 5409 expr.original_code = ERROR_MARK; 5410 break; 5411 } 5412 break; 5413 case CPP_OPEN_SQUARE: 5414 if (c_dialect_objc ()) 5415 { 5416 tree receiver, args; 5417 c_parser_consume_token (parser); 5418 receiver = c_parser_objc_receiver (parser); 5419 args = c_parser_objc_message_args (parser); 5420 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 5421 "expected %<]%>"); 5422 expr.value = objc_build_message_expr (build_tree_list (receiver, 5423 args)); 5424 expr.original_code = ERROR_MARK; 5425 break; 5426 } 5427 /* Else fall through to report error. */ 5428 default: 5429 c_parser_error (parser, "expected expression"); 5430 expr.value = error_mark_node; 5431 expr.original_code = ERROR_MARK; 5432 break; 5433 } 5434 return c_parser_postfix_expression_after_primary (parser, expr); 5435} 5436 5437/* Parse a postfix expression after a parenthesized type name: the 5438 brace-enclosed initializer of a compound literal, possibly followed 5439 by some postfix operators. This is separate because it is not 5440 possible to tell until after the type name whether a cast 5441 expression has a cast or a compound literal, or whether the operand 5442 of sizeof is a parenthesized type name or starts with a compound 5443 literal. */ 5444 5445static struct c_expr 5446c_parser_postfix_expression_after_paren_type (c_parser *parser, 5447 struct c_type_name *type_name) 5448{ 5449 tree type; 5450 struct c_expr init; 5451 struct c_expr expr; 5452 start_init (NULL_TREE, NULL, 0); 5453 type = groktypename (type_name); 5454 if (type != error_mark_node && C_TYPE_VARIABLE_SIZE (type)) 5455 { 5456 error ("compound literal has variable size"); 5457 type = error_mark_node; 5458 } 5459 init = c_parser_braced_init (parser, type, false); 5460 finish_init (); 5461 maybe_warn_string_init (type, init); 5462 5463 if (pedantic && !flag_isoc99) 5464 pedwarn ("ISO C90 forbids compound literals"); 5465 expr.value = build_compound_literal (type, init.value); 5466 expr.original_code = ERROR_MARK; 5467 return c_parser_postfix_expression_after_primary (parser, expr); 5468} 5469 5470/* Parse a postfix expression after the initial primary or compound 5471 literal; that is, parse a series of postfix operators. */ 5472 5473static struct c_expr 5474c_parser_postfix_expression_after_primary (c_parser *parser, 5475 struct c_expr expr) 5476{ 5477 tree ident, idx, exprlist; 5478 while (true) 5479 { 5480 switch (c_parser_peek_token (parser)->type) 5481 { 5482 case CPP_OPEN_SQUARE: 5483 /* Array reference. */ 5484 c_parser_consume_token (parser); 5485 idx = c_parser_expression (parser).value; 5486 c_parser_skip_until_found (parser, CPP_CLOSE_SQUARE, 5487 "expected %<]%>"); 5488 expr.value = build_array_ref (expr.value, idx); 5489 expr.original_code = ERROR_MARK; 5490 break; 5491 case CPP_OPEN_PAREN: 5492 /* Function call. */ 5493 c_parser_consume_token (parser); 5494 if (c_parser_next_token_is (parser, CPP_CLOSE_PAREN)) 5495 exprlist = NULL_TREE; 5496 else 5497 exprlist = c_parser_expr_list (parser, true); 5498 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 5499 "expected %<)%>"); 5500 expr.value = build_function_call (expr.value, exprlist); 5501 expr.original_code = ERROR_MARK; 5502 break; 5503 case CPP_DOT: 5504 /* Structure element reference. */ 5505 c_parser_consume_token (parser); 5506 expr = default_function_array_conversion (expr); 5507 if (c_parser_next_token_is (parser, CPP_NAME)) 5508 ident = c_parser_peek_token (parser)->value; 5509 else 5510 { 5511 c_parser_error (parser, "expected identifier"); 5512 expr.value = error_mark_node; 5513 expr.original_code = ERROR_MARK; 5514 return expr; 5515 } 5516 c_parser_consume_token (parser); 5517 expr.value = build_component_ref (expr.value, ident); 5518 expr.original_code = ERROR_MARK; 5519 break; 5520 case CPP_DEREF: 5521 /* Structure element reference. */ 5522 c_parser_consume_token (parser); 5523 expr = default_function_array_conversion (expr); 5524 if (c_parser_next_token_is (parser, CPP_NAME)) 5525 ident = c_parser_peek_token (parser)->value; 5526 else 5527 { 5528 c_parser_error (parser, "expected identifier"); 5529 expr.value = error_mark_node; 5530 expr.original_code = ERROR_MARK; 5531 return expr; 5532 } 5533 c_parser_consume_token (parser); 5534 expr.value = build_component_ref (build_indirect_ref (expr.value, 5535 "->"), ident); 5536 expr.original_code = ERROR_MARK; 5537 break; 5538 case CPP_PLUS_PLUS: 5539 /* Postincrement. */ 5540 c_parser_consume_token (parser); 5541 expr = default_function_array_conversion (expr); 5542 expr.value = build_unary_op (POSTINCREMENT_EXPR, expr.value, 0); 5543 expr.original_code = ERROR_MARK; 5544 break; 5545 case CPP_MINUS_MINUS: 5546 /* Postdecrement. */ 5547 c_parser_consume_token (parser); 5548 expr = default_function_array_conversion (expr); 5549 expr.value = build_unary_op (POSTDECREMENT_EXPR, expr.value, 0); 5550 expr.original_code = ERROR_MARK; 5551 break; 5552 default: 5553 return expr; 5554 } 5555 } 5556} 5557 5558/* Parse an expression (C90 6.3.17, C99 6.5.17). 5559 5560 expression: 5561 assignment-expression 5562 expression , assignment-expression 5563*/ 5564 5565static struct c_expr 5566c_parser_expression (c_parser *parser) 5567{ 5568 struct c_expr expr; 5569 expr = c_parser_expr_no_commas (parser, NULL); 5570 while (c_parser_next_token_is (parser, CPP_COMMA)) 5571 { 5572 struct c_expr next; 5573 c_parser_consume_token (parser); 5574 next = c_parser_expr_no_commas (parser, NULL); 5575 next = default_function_array_conversion (next); 5576 expr.value = build_compound_expr (expr.value, next.value); 5577 expr.original_code = COMPOUND_EXPR; 5578 } 5579 return expr; 5580} 5581 5582/* Parse an expression and convert functions or arrays to 5583 pointers. */ 5584 5585static struct c_expr 5586c_parser_expression_conv (c_parser *parser) 5587{ 5588 struct c_expr expr; 5589 expr = c_parser_expression (parser); 5590 expr = default_function_array_conversion (expr); 5591 return expr; 5592} 5593 5594/* Parse a non-empty list of expressions. If CONVERT_P, convert 5595 functions and arrays to pointers. 5596 5597 nonempty-expr-list: 5598 assignment-expression 5599 nonempty-expr-list , assignment-expression 5600*/ 5601 5602static tree 5603c_parser_expr_list (c_parser *parser, bool convert_p) 5604{ 5605 struct c_expr expr; 5606 tree ret, cur; 5607 expr = c_parser_expr_no_commas (parser, NULL); 5608 if (convert_p) 5609 expr = default_function_array_conversion (expr); 5610 ret = cur = build_tree_list (NULL_TREE, expr.value); 5611 while (c_parser_next_token_is (parser, CPP_COMMA)) 5612 { 5613 c_parser_consume_token (parser); 5614 expr = c_parser_expr_no_commas (parser, NULL); 5615 if (convert_p) 5616 expr = default_function_array_conversion (expr); 5617 cur = TREE_CHAIN (cur) = build_tree_list (NULL_TREE, expr.value); 5618 } 5619 return ret; 5620} 5621 5622 5623/* Parse Objective-C-specific constructs. */ 5624 5625/* Parse an objc-class-definition. 5626 5627 objc-class-definition: 5628 @interface identifier objc-superclass[opt] objc-protocol-refs[opt] 5629 objc-class-instance-variables[opt] objc-methodprotolist @end 5630 @implementation identifier objc-superclass[opt] 5631 objc-class-instance-variables[opt] 5632 @interface identifier ( identifier ) objc-protocol-refs[opt] 5633 objc-methodprotolist @end 5634 @implementation identifier ( identifier ) 5635 5636 objc-superclass: 5637 : identifier 5638 5639 "@interface identifier (" must start "@interface identifier ( 5640 identifier ) ...": objc-methodprotolist in the first production may 5641 not start with a parenthesized identifier as a declarator of a data 5642 definition with no declaration specifiers if the objc-superclass, 5643 objc-protocol-refs and objc-class-instance-variables are omitted. */ 5644 5645static void 5646c_parser_objc_class_definition (c_parser *parser) 5647{ 5648 bool iface_p; 5649 tree id1; 5650 tree superclass; 5651 if (c_parser_next_token_is_keyword (parser, RID_AT_INTERFACE)) 5652 iface_p = true; 5653 else if (c_parser_next_token_is_keyword (parser, RID_AT_IMPLEMENTATION)) 5654 iface_p = false; 5655 else 5656 gcc_unreachable (); 5657 c_parser_consume_token (parser); 5658 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5659 { 5660 c_parser_error (parser, "expected identifier"); 5661 return; 5662 } 5663 id1 = c_parser_peek_token (parser)->value; 5664 c_parser_consume_token (parser); 5665 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 5666 { 5667 tree id2; 5668 tree proto = NULL_TREE; 5669 c_parser_consume_token (parser); 5670 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5671 { 5672 c_parser_error (parser, "expected identifier"); 5673 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 5674 return; 5675 } 5676 id2 = c_parser_peek_token (parser)->value; 5677 c_parser_consume_token (parser); 5678 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 5679 if (!iface_p) 5680 { 5681 objc_start_category_implementation (id1, id2); 5682 return; 5683 } 5684 if (c_parser_next_token_is (parser, CPP_LESS)) 5685 proto = c_parser_objc_protocol_refs (parser); 5686 objc_start_category_interface (id1, id2, proto); 5687 c_parser_objc_methodprotolist (parser); 5688 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); 5689 objc_finish_interface (); 5690 return; 5691 } 5692 if (c_parser_next_token_is (parser, CPP_COLON)) 5693 { 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 superclass = c_parser_peek_token (parser)->value; 5701 c_parser_consume_token (parser); 5702 } 5703 else 5704 superclass = NULL_TREE; 5705 if (iface_p) 5706 { 5707 tree proto = NULL_TREE; 5708 if (c_parser_next_token_is (parser, CPP_LESS)) 5709 proto = c_parser_objc_protocol_refs (parser); 5710 objc_start_class_interface (id1, superclass, proto); 5711 } 5712 else 5713 objc_start_class_implementation (id1, superclass); 5714 if (c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 5715 c_parser_objc_class_instance_variables (parser); 5716 if (iface_p) 5717 { 5718 objc_continue_interface (); 5719 c_parser_objc_methodprotolist (parser); 5720 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); 5721 objc_finish_interface (); 5722 } 5723 else 5724 { 5725 objc_continue_implementation (); 5726 return; 5727 } 5728} 5729 5730/* Parse objc-class-instance-variables. 5731 5732 objc-class-instance-variables: 5733 { objc-instance-variable-decl-list[opt] } 5734 5735 objc-instance-variable-decl-list: 5736 objc-visibility-spec 5737 objc-instance-variable-decl ; 5738 ; 5739 objc-instance-variable-decl-list objc-visibility-spec 5740 objc-instance-variable-decl-list objc-instance-variable-decl ; 5741 objc-instance-variable-decl-list ; 5742 5743 objc-visibility-spec: 5744 @private 5745 @protected 5746 @public 5747 5748 objc-instance-variable-decl: 5749 struct-declaration 5750*/ 5751 5752static void 5753c_parser_objc_class_instance_variables (c_parser *parser) 5754{ 5755 gcc_assert (c_parser_next_token_is (parser, CPP_OPEN_BRACE)); 5756 c_parser_consume_token (parser); 5757 while (c_parser_next_token_is_not (parser, CPP_EOF)) 5758 { 5759 tree decls; 5760 /* Parse any stray semicolon. */ 5761 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 5762 { 5763 if (pedantic) 5764 pedwarn ("extra semicolon in struct or union specified"); 5765 c_parser_consume_token (parser); 5766 continue; 5767 } 5768 /* Stop if at the end of the instance variables. */ 5769 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 5770 { 5771 c_parser_consume_token (parser); 5772 break; 5773 } 5774 /* Parse any objc-visibility-spec. */ 5775 if (c_parser_next_token_is_keyword (parser, RID_AT_PRIVATE)) 5776 { 5777 c_parser_consume_token (parser); 5778 objc_set_visibility (2); 5779 continue; 5780 } 5781 else if (c_parser_next_token_is_keyword (parser, RID_AT_PROTECTED)) 5782 { 5783 c_parser_consume_token (parser); 5784 objc_set_visibility (0); 5785 continue; 5786 } 5787 else if (c_parser_next_token_is_keyword (parser, RID_AT_PUBLIC)) 5788 { 5789 c_parser_consume_token (parser); 5790 objc_set_visibility (1); 5791 continue; 5792 } 5793 else if (c_parser_next_token_is (parser, CPP_PRAGMA)) 5794 { 5795 c_parser_pragma (parser, pragma_external); 5796 continue; 5797 } 5798 5799 /* Parse some comma-separated declarations. */ 5800 decls = c_parser_struct_declaration (parser); 5801 { 5802 /* Comma-separated instance variables are chained together in 5803 reverse order; add them one by one. */ 5804 tree ivar = nreverse (decls); 5805 for (; ivar; ivar = TREE_CHAIN (ivar)) 5806 objc_add_instance_variable (copy_node (ivar)); 5807 } 5808 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 5809 } 5810} 5811 5812/* Parse an objc-class-declaration. 5813 5814 objc-class-declaration: 5815 @class identifier-list ; 5816*/ 5817 5818static void 5819c_parser_objc_class_declaration (c_parser *parser) 5820{ 5821 tree list = NULL_TREE; 5822 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_CLASS)); 5823 c_parser_consume_token (parser); 5824 /* Any identifiers, including those declared as type names, are OK 5825 here. */ 5826 while (true) 5827 { 5828 tree id; 5829 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5830 { 5831 c_parser_error (parser, "expected identifier"); 5832 break; 5833 } 5834 id = c_parser_peek_token (parser)->value; 5835 list = chainon (list, build_tree_list (NULL_TREE, id)); 5836 c_parser_consume_token (parser); 5837 if (c_parser_next_token_is (parser, CPP_COMMA)) 5838 c_parser_consume_token (parser); 5839 else 5840 break; 5841 } 5842 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 5843 objc_declare_class (list); 5844} 5845 5846/* Parse an objc-alias-declaration. 5847 5848 objc-alias-declaration: 5849 @compatibility_alias identifier identifier ; 5850*/ 5851 5852static void 5853c_parser_objc_alias_declaration (c_parser *parser) 5854{ 5855 tree id1, id2; 5856 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_ALIAS)); 5857 c_parser_consume_token (parser); 5858 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5859 { 5860 c_parser_error (parser, "expected identifier"); 5861 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL); 5862 return; 5863 } 5864 id1 = c_parser_peek_token (parser)->value; 5865 c_parser_consume_token (parser); 5866 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5867 { 5868 c_parser_error (parser, "expected identifier"); 5869 c_parser_skip_until_found (parser, CPP_SEMICOLON, NULL); 5870 return; 5871 } 5872 id2 = c_parser_peek_token (parser)->value; 5873 c_parser_consume_token (parser); 5874 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 5875 objc_declare_alias (id1, id2); 5876} 5877 5878/* Parse an objc-protocol-definition. 5879 5880 objc-protocol-definition: 5881 @protocol identifier objc-protocol-refs[opt] objc-methodprotolist @end 5882 @protocol identifier-list ; 5883 5884 "@protocol identifier ;" should be resolved as "@protocol 5885 identifier-list ;": objc-methodprotolist may not start with a 5886 semicolon in the first alternative if objc-protocol-refs are 5887 omitted. */ 5888 5889static void 5890c_parser_objc_protocol_definition (c_parser *parser) 5891{ 5892 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_PROTOCOL)); 5893 c_parser_consume_token (parser); 5894 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5895 { 5896 c_parser_error (parser, "expected identifier"); 5897 return; 5898 } 5899 if (c_parser_peek_2nd_token (parser)->type == CPP_COMMA 5900 || c_parser_peek_2nd_token (parser)->type == CPP_SEMICOLON) 5901 { 5902 tree list = NULL_TREE; 5903 /* Any identifiers, including those declared as type names, are 5904 OK here. */ 5905 while (true) 5906 { 5907 tree id; 5908 if (c_parser_next_token_is_not (parser, CPP_NAME)) 5909 { 5910 c_parser_error (parser, "expected identifier"); 5911 break; 5912 } 5913 id = c_parser_peek_token (parser)->value; 5914 list = chainon (list, build_tree_list (NULL_TREE, id)); 5915 c_parser_consume_token (parser); 5916 if (c_parser_next_token_is (parser, CPP_COMMA)) 5917 c_parser_consume_token (parser); 5918 else 5919 break; 5920 } 5921 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 5922 objc_declare_protocols (list); 5923 } 5924 else 5925 { 5926 tree id = c_parser_peek_token (parser)->value; 5927 tree proto = NULL_TREE; 5928 c_parser_consume_token (parser); 5929 if (c_parser_next_token_is (parser, CPP_LESS)) 5930 proto = c_parser_objc_protocol_refs (parser); 5931 objc_pq_context = 1; 5932 objc_start_protocol (id, proto); 5933 c_parser_objc_methodprotolist (parser); 5934 c_parser_require_keyword (parser, RID_AT_END, "expected %<@end%>"); 5935 objc_pq_context = 0; 5936 objc_finish_interface (); 5937 } 5938} 5939 5940/* Parse an objc-method-type. 5941 5942 objc-method-type: 5943 + 5944 - 5945*/ 5946 5947static enum tree_code 5948c_parser_objc_method_type (c_parser *parser) 5949{ 5950 switch (c_parser_peek_token (parser)->type) 5951 { 5952 case CPP_PLUS: 5953 c_parser_consume_token (parser); 5954 return PLUS_EXPR; 5955 case CPP_MINUS: 5956 c_parser_consume_token (parser); 5957 return MINUS_EXPR; 5958 default: 5959 gcc_unreachable (); 5960 } 5961} 5962 5963/* Parse an objc-method-definition. 5964 5965 objc-method-definition: 5966 objc-method-type objc-method-decl ;[opt] compound-statement 5967*/ 5968 5969static void 5970c_parser_objc_method_definition (c_parser *parser) 5971{ 5972 enum tree_code type = c_parser_objc_method_type (parser); 5973 tree decl; 5974 objc_set_method_type (type); 5975 objc_pq_context = 1; 5976 decl = c_parser_objc_method_decl (parser); 5977 if (c_parser_next_token_is (parser, CPP_SEMICOLON)) 5978 { 5979 c_parser_consume_token (parser); 5980 if (pedantic) 5981 pedwarn ("extra semicolon in method definition specified"); 5982 } 5983 if (!c_parser_next_token_is (parser, CPP_OPEN_BRACE)) 5984 { 5985 c_parser_error (parser, "expected %<{%>"); 5986 return; 5987 } 5988 objc_pq_context = 0; 5989 objc_start_method_definition (decl); 5990 add_stmt (c_parser_compound_statement (parser)); 5991 objc_finish_method_definition (current_function_decl); 5992} 5993 5994/* Parse an objc-methodprotolist. 5995 5996 objc-methodprotolist: 5997 empty 5998 objc-methodprotolist objc-methodproto 5999 objc-methodprotolist declaration 6000 objc-methodprotolist ; 6001 6002 The declaration is a data definition, which may be missing 6003 declaration specifiers under the same rules and diagnostics as 6004 other data definitions outside functions, and the stray semicolon 6005 is diagnosed the same way as a stray semicolon outside a 6006 function. */ 6007 6008static void 6009c_parser_objc_methodprotolist (c_parser *parser) 6010{ 6011 while (true) 6012 { 6013 /* The list is terminated by @end. */ 6014 switch (c_parser_peek_token (parser)->type) 6015 { 6016 case CPP_SEMICOLON: 6017 if (pedantic) 6018 pedwarn ("ISO C does not allow extra %<;%> outside of a function"); 6019 c_parser_consume_token (parser); 6020 break; 6021 case CPP_PLUS: 6022 case CPP_MINUS: 6023 c_parser_objc_methodproto (parser); 6024 break; 6025 case CPP_PRAGMA: 6026 c_parser_pragma (parser, pragma_external); 6027 break; 6028 case CPP_EOF: 6029 return; 6030 default: 6031 if (c_parser_next_token_is_keyword (parser, RID_AT_END)) 6032 return; 6033 c_parser_declaration_or_fndef (parser, false, true, false, true); 6034 break; 6035 } 6036 } 6037} 6038 6039/* Parse an objc-methodproto. 6040 6041 objc-methodproto: 6042 objc-method-type objc-method-decl ; 6043*/ 6044 6045static void 6046c_parser_objc_methodproto (c_parser *parser) 6047{ 6048 enum tree_code type = c_parser_objc_method_type (parser); 6049 tree decl; 6050 objc_set_method_type (type); 6051 /* Remember protocol qualifiers in prototypes. */ 6052 objc_pq_context = 1; 6053 decl = c_parser_objc_method_decl (parser); 6054 /* Forget protocol qualifiers here. */ 6055 objc_pq_context = 0; 6056 objc_add_method_declaration (decl); 6057 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 6058} 6059 6060/* Parse an objc-method-decl. 6061 6062 objc-method-decl: 6063 ( objc-type-name ) objc-selector 6064 objc-selector 6065 ( objc-type-name ) objc-keyword-selector objc-optparmlist 6066 objc-keyword-selector objc-optparmlist 6067 6068 objc-keyword-selector: 6069 objc-keyword-decl 6070 objc-keyword-selector objc-keyword-decl 6071 6072 objc-keyword-decl: 6073 objc-selector : ( objc-type-name ) identifier 6074 objc-selector : identifier 6075 : ( objc-type-name ) identifier 6076 : identifier 6077 6078 objc-optparmlist: 6079 objc-optparms objc-optellipsis 6080 6081 objc-optparms: 6082 empty 6083 objc-opt-parms , parameter-declaration 6084 6085 objc-optellipsis: 6086 empty 6087 , ... 6088*/ 6089 6090static tree 6091c_parser_objc_method_decl (c_parser *parser) 6092{ 6093 tree type = NULL_TREE; 6094 tree sel; 6095 tree parms = NULL_TREE; 6096 bool ellipsis = false; 6097 6098 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 6099 { 6100 c_parser_consume_token (parser); 6101 type = c_parser_objc_type_name (parser); 6102 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6103 } 6104 sel = c_parser_objc_selector (parser); 6105 /* If there is no selector, or a colon follows, we have an 6106 objc-keyword-selector. If there is a selector, and a colon does 6107 not follow, that selector ends the objc-method-decl. */ 6108 if (!sel || c_parser_next_token_is (parser, CPP_COLON)) 6109 { 6110 tree tsel = sel; 6111 tree list = NULL_TREE; 6112 while (true) 6113 { 6114 tree atype = NULL_TREE, id, keyworddecl; 6115 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) 6116 break; 6117 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 6118 { 6119 c_parser_consume_token (parser); 6120 atype = c_parser_objc_type_name (parser); 6121 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 6122 "expected %<)%>"); 6123 } 6124 if (c_parser_next_token_is_not (parser, CPP_NAME)) 6125 { 6126 c_parser_error (parser, "expected identifier"); 6127 return error_mark_node; 6128 } 6129 id = c_parser_peek_token (parser)->value; 6130 c_parser_consume_token (parser); 6131 keyworddecl = objc_build_keyword_decl (tsel, atype, id); 6132 list = chainon (list, keyworddecl); 6133 tsel = c_parser_objc_selector (parser); 6134 if (!tsel && c_parser_next_token_is_not (parser, CPP_COLON)) 6135 break; 6136 } 6137 /* Parse the optional parameter list. Optional Objective-C 6138 method parameters follow the C syntax, and may include '...' 6139 to denote a variable number of arguments. */ 6140 parms = make_node (TREE_LIST); 6141 while (c_parser_next_token_is (parser, CPP_COMMA)) 6142 { 6143 struct c_parm *parm; 6144 c_parser_consume_token (parser); 6145 if (c_parser_next_token_is (parser, CPP_ELLIPSIS)) 6146 { 6147 ellipsis = true; 6148 c_parser_consume_token (parser); 6149 break; 6150 } 6151 parm = c_parser_parameter_declaration (parser, NULL_TREE); 6152 if (parm == NULL) 6153 break; 6154 parms = chainon (parms, 6155 build_tree_list (NULL_TREE, grokparm (parm))); 6156 } 6157 sel = list; 6158 } 6159 return objc_build_method_signature (type, sel, parms, ellipsis); 6160} 6161 6162/* Parse an objc-type-name. 6163 6164 objc-type-name: 6165 objc-type-qualifiers[opt] type-name 6166 objc-type-qualifiers[opt] 6167 6168 objc-type-qualifiers: 6169 objc-type-qualifier 6170 objc-type-qualifiers objc-type-qualifier 6171 6172 objc-type-qualifier: one of 6173 in out inout bycopy byref oneway 6174*/ 6175 6176static tree 6177c_parser_objc_type_name (c_parser *parser) 6178{ 6179 tree quals = NULL_TREE; 6180 struct c_type_name *typename = NULL; 6181 tree type = NULL_TREE; 6182 while (true) 6183 { 6184 c_token *token = c_parser_peek_token (parser); 6185 if (token->type == CPP_KEYWORD 6186 && (token->keyword == RID_IN 6187 || token->keyword == RID_OUT 6188 || token->keyword == RID_INOUT 6189 || token->keyword == RID_BYCOPY 6190 || token->keyword == RID_BYREF 6191 || token->keyword == RID_ONEWAY)) 6192 { 6193 quals = chainon (quals, build_tree_list (NULL_TREE, token->value)); 6194 c_parser_consume_token (parser); 6195 } 6196 else 6197 break; 6198 } 6199 if (c_parser_next_token_starts_typename (parser)) 6200 typename = c_parser_type_name (parser); 6201 if (typename) 6202 type = groktypename (typename); 6203 return build_tree_list (quals, type); 6204} 6205 6206/* Parse objc-protocol-refs. 6207 6208 objc-protocol-refs: 6209 < identifier-list > 6210*/ 6211 6212static tree 6213c_parser_objc_protocol_refs (c_parser *parser) 6214{ 6215 tree list = NULL_TREE; 6216 gcc_assert (c_parser_next_token_is (parser, CPP_LESS)); 6217 c_parser_consume_token (parser); 6218 /* Any identifiers, including those declared as type names, are OK 6219 here. */ 6220 while (true) 6221 { 6222 tree id; 6223 if (c_parser_next_token_is_not (parser, CPP_NAME)) 6224 { 6225 c_parser_error (parser, "expected identifier"); 6226 break; 6227 } 6228 id = c_parser_peek_token (parser)->value; 6229 list = chainon (list, build_tree_list (NULL_TREE, id)); 6230 c_parser_consume_token (parser); 6231 if (c_parser_next_token_is (parser, CPP_COMMA)) 6232 c_parser_consume_token (parser); 6233 else 6234 break; 6235 } 6236 c_parser_require (parser, CPP_GREATER, "expected %<>%>"); 6237 return list; 6238} 6239 6240/* Parse an objc-try-catch-statement. 6241 6242 objc-try-catch-statement: 6243 @try compound-statement objc-catch-list[opt] 6244 @try compound-statement objc-catch-list[opt] @finally compound-statement 6245 6246 objc-catch-list: 6247 @catch ( parameter-declaration ) compound-statement 6248 objc-catch-list @catch ( parameter-declaration ) compound-statement 6249*/ 6250 6251static void 6252c_parser_objc_try_catch_statement (c_parser *parser) 6253{ 6254 location_t loc; 6255 tree stmt; 6256 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_TRY)); 6257 c_parser_consume_token (parser); 6258 loc = c_parser_peek_token (parser)->location; 6259 stmt = c_parser_compound_statement (parser); 6260 objc_begin_try_stmt (loc, stmt); 6261 while (c_parser_next_token_is_keyword (parser, RID_AT_CATCH)) 6262 { 6263 struct c_parm *parm; 6264 c_parser_consume_token (parser); 6265 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6266 break; 6267 parm = c_parser_parameter_declaration (parser, NULL_TREE); 6268 if (parm == NULL) 6269 { 6270 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); 6271 break; 6272 } 6273 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6274 objc_begin_catch_clause (grokparm (parm)); 6275 if (c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) 6276 c_parser_compound_statement_nostart (parser); 6277 objc_finish_catch_clause (); 6278 } 6279 if (c_parser_next_token_is_keyword (parser, RID_AT_FINALLY)) 6280 { 6281 location_t finloc; 6282 tree finstmt; 6283 c_parser_consume_token (parser); 6284 finloc = c_parser_peek_token (parser)->location; 6285 finstmt = c_parser_compound_statement (parser); 6286 objc_build_finally_clause (finloc, finstmt); 6287 } 6288 objc_finish_try_stmt (); 6289} 6290 6291/* Parse an objc-synchronized-statement. 6292 6293 objc-synchronized-statement: 6294 @synchronized ( expression ) compound-statement 6295*/ 6296 6297static void 6298c_parser_objc_synchronized_statement (c_parser *parser) 6299{ 6300 location_t loc; 6301 tree expr, stmt; 6302 gcc_assert (c_parser_next_token_is_keyword (parser, RID_AT_SYNCHRONIZED)); 6303 c_parser_consume_token (parser); 6304 loc = c_parser_peek_token (parser)->location; 6305 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6306 { 6307 expr = c_parser_expression (parser).value; 6308 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6309 } 6310 else 6311 expr = error_mark_node; 6312 stmt = c_parser_compound_statement (parser); 6313 objc_build_synchronized (loc, expr, stmt); 6314} 6315 6316/* Parse an objc-selector; return NULL_TREE without an error if the 6317 next token is not an objc-selector. 6318 6319 objc-selector: 6320 identifier 6321 one of 6322 enum struct union if else while do for switch case default 6323 break continue return goto asm sizeof typeof __alignof 6324 unsigned long const short volatile signed restrict _Complex 6325 in out inout bycopy byref oneway int char float double void _Bool 6326 6327 ??? Why this selection of keywords but not, for example, storage 6328 class specifiers? */ 6329 6330static tree 6331c_parser_objc_selector (c_parser *parser) 6332{ 6333 c_token *token = c_parser_peek_token (parser); 6334 tree value = token->value; 6335 if (token->type == CPP_NAME) 6336 { 6337 c_parser_consume_token (parser); 6338 return value; 6339 } 6340 if (token->type != CPP_KEYWORD) 6341 return NULL_TREE; 6342 switch (token->keyword) 6343 { 6344 case RID_ENUM: 6345 case RID_STRUCT: 6346 case RID_UNION: 6347 case RID_IF: 6348 case RID_ELSE: 6349 case RID_WHILE: 6350 case RID_DO: 6351 case RID_FOR: 6352 case RID_SWITCH: 6353 case RID_CASE: 6354 case RID_DEFAULT: 6355 case RID_BREAK: 6356 case RID_CONTINUE: 6357 case RID_RETURN: 6358 case RID_GOTO: 6359 case RID_ASM: 6360 case RID_SIZEOF: 6361 case RID_TYPEOF: 6362 case RID_ALIGNOF: 6363 case RID_UNSIGNED: 6364 case RID_LONG: 6365 case RID_CONST: 6366 case RID_SHORT: 6367 case RID_VOLATILE: 6368 case RID_SIGNED: 6369 case RID_RESTRICT: 6370 case RID_COMPLEX: 6371 case RID_IN: 6372 case RID_OUT: 6373 case RID_INOUT: 6374 case RID_BYCOPY: 6375 case RID_BYREF: 6376 case RID_ONEWAY: 6377 case RID_INT: 6378 case RID_CHAR: 6379 case RID_FLOAT: 6380 case RID_DOUBLE: 6381 case RID_VOID: 6382 case RID_BOOL: 6383 c_parser_consume_token (parser); 6384 return value; 6385 default: 6386 return NULL_TREE; 6387 } 6388} 6389 6390/* Parse an objc-selector-arg. 6391 6392 objc-selector-arg: 6393 objc-selector 6394 objc-keywordname-list 6395 6396 objc-keywordname-list: 6397 objc-keywordname 6398 objc-keywordname-list objc-keywordname 6399 6400 objc-keywordname: 6401 objc-selector : 6402 : 6403*/ 6404 6405static tree 6406c_parser_objc_selector_arg (c_parser *parser) 6407{ 6408 tree sel = c_parser_objc_selector (parser); 6409 tree list = NULL_TREE; 6410 if (sel && c_parser_next_token_is_not (parser, CPP_COLON)) 6411 return sel; 6412 while (true) 6413 { 6414 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) 6415 return list; 6416 list = chainon (list, build_tree_list (sel, NULL_TREE)); 6417 sel = c_parser_objc_selector (parser); 6418 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON)) 6419 break; 6420 } 6421 return list; 6422} 6423 6424/* Parse an objc-receiver. 6425 6426 objc-receiver: 6427 expression 6428 class-name 6429 type-name 6430*/ 6431 6432static tree 6433c_parser_objc_receiver (c_parser *parser) 6434{ 6435 if (c_parser_peek_token (parser)->type == CPP_NAME 6436 && (c_parser_peek_token (parser)->id_kind == C_ID_TYPENAME 6437 || c_parser_peek_token (parser)->id_kind == C_ID_CLASSNAME)) 6438 { 6439 tree id = c_parser_peek_token (parser)->value; 6440 c_parser_consume_token (parser); 6441 return objc_get_class_reference (id); 6442 } 6443 return c_parser_expression (parser).value; 6444} 6445 6446/* Parse objc-message-args. 6447 6448 objc-message-args: 6449 objc-selector 6450 objc-keywordarg-list 6451 6452 objc-keywordarg-list: 6453 objc-keywordarg 6454 objc-keywordarg-list objc-keywordarg 6455 6456 objc-keywordarg: 6457 objc-selector : objc-keywordexpr 6458 : objc-keywordexpr 6459*/ 6460 6461static tree 6462c_parser_objc_message_args (c_parser *parser) 6463{ 6464 tree sel = c_parser_objc_selector (parser); 6465 tree list = NULL_TREE; 6466 if (sel && c_parser_next_token_is_not (parser, CPP_COLON)) 6467 return sel; 6468 while (true) 6469 { 6470 tree keywordexpr; 6471 if (!c_parser_require (parser, CPP_COLON, "expected %<:%>")) 6472 return list; 6473 keywordexpr = c_parser_objc_keywordexpr (parser); 6474 list = chainon (list, build_tree_list (sel, keywordexpr)); 6475 sel = c_parser_objc_selector (parser); 6476 if (!sel && c_parser_next_token_is_not (parser, CPP_COLON)) 6477 break; 6478 } 6479 return list; 6480} 6481 6482/* Parse an objc-keywordexpr. 6483 6484 objc-keywordexpr: 6485 nonempty-expr-list 6486*/ 6487 6488static tree 6489c_parser_objc_keywordexpr (c_parser *parser) 6490{ 6491 tree list = c_parser_expr_list (parser, true); 6492 if (TREE_CHAIN (list) == NULL_TREE) 6493 { 6494 /* Just return the expression, remove a level of 6495 indirection. */ 6496 return TREE_VALUE (list); 6497 } 6498 else 6499 { 6500 /* We have a comma expression, we will collapse later. */ 6501 return list; 6502 } 6503} 6504 6505 6506/* Handle pragmas. Some OpenMP pragmas are associated with, and therefore 6507 should be considered, statements. ALLOW_STMT is true if we're within 6508 the context of a function and such pragmas are to be allowed. Returns 6509 true if we actually parsed such a pragma. */ 6510 6511static bool 6512c_parser_pragma (c_parser *parser, enum pragma_context context) 6513{ 6514 unsigned int id; 6515 6516 id = c_parser_peek_token (parser)->pragma_kind; 6517 gcc_assert (id != PRAGMA_NONE); 6518 6519 switch (id) 6520 { 6521 case PRAGMA_OMP_BARRIER: 6522 if (context != pragma_compound) 6523 { 6524 if (context == pragma_stmt) 6525 c_parser_error (parser, "%<#pragma omp barrier%> may only be " 6526 "used in compound statements"); 6527 goto bad_stmt; 6528 } 6529 c_parser_omp_barrier (parser); 6530 return false; 6531 6532 case PRAGMA_OMP_FLUSH: 6533 if (context != pragma_compound) 6534 { 6535 if (context == pragma_stmt) 6536 c_parser_error (parser, "%<#pragma omp flush%> may only be " 6537 "used in compound statements"); 6538 goto bad_stmt; 6539 } 6540 c_parser_omp_flush (parser); 6541 return false; 6542 6543 case PRAGMA_OMP_THREADPRIVATE: 6544 c_parser_omp_threadprivate (parser); 6545 return false; 6546 6547 case PRAGMA_OMP_SECTION: 6548 error ("%<#pragma omp section%> may only be used in " 6549 "%<#pragma omp sections%> construct"); 6550 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); 6551 return false; 6552 6553 case PRAGMA_GCC_PCH_PREPROCESS: 6554 c_parser_error (parser, "%<#pragma GCC pch_preprocess%> must be first"); 6555 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); 6556 return false; 6557 6558 default: 6559 if (id < PRAGMA_FIRST_EXTERNAL) 6560 { 6561 if (context == pragma_external) 6562 { 6563 bad_stmt: 6564 c_parser_error (parser, "expected declaration specifiers"); 6565 c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL); 6566 return false; 6567 } 6568 c_parser_omp_construct (parser); 6569 return true; 6570 } 6571 break; 6572 } 6573 6574 c_parser_consume_pragma (parser); 6575 c_invoke_pragma_handler (id); 6576 6577 /* Skip to EOL, but suppress any error message. Those will have been 6578 generated by the handler routine through calling error, as opposed 6579 to calling c_parser_error. */ 6580 parser->error = true; 6581 c_parser_skip_to_pragma_eol (parser); 6582 6583 return false; 6584} 6585 6586/* The interface the pragma parsers have to the lexer. */ 6587 6588enum cpp_ttype 6589pragma_lex (tree *value) 6590{ 6591 c_token *tok = c_parser_peek_token (the_parser); 6592 enum cpp_ttype ret = tok->type; 6593 6594 *value = tok->value; 6595 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF) 6596 ret = CPP_EOF; 6597 else 6598 { 6599 if (ret == CPP_KEYWORD) 6600 ret = CPP_NAME; 6601 c_parser_consume_token (the_parser); 6602 } 6603 6604 return ret; 6605} 6606 6607static void 6608c_parser_pragma_pch_preprocess (c_parser *parser) 6609{ 6610 tree name = NULL; 6611 6612 c_parser_consume_pragma (parser); 6613 if (c_parser_next_token_is (parser, CPP_STRING)) 6614 { 6615 name = c_parser_peek_token (parser)->value; 6616 c_parser_consume_token (parser); 6617 } 6618 else 6619 c_parser_error (parser, "expected string literal"); 6620 c_parser_skip_to_pragma_eol (parser); 6621 6622 if (name) 6623 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name)); 6624} 6625 6626/* OpenMP 2.5 parsing routines. */ 6627 6628/* Returns name of the next clause. 6629 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and 6630 the token is not consumed. Otherwise appropriate pragma_omp_clause is 6631 returned and the token is consumed. */ 6632 6633static pragma_omp_clause 6634c_parser_omp_clause_name (c_parser *parser) 6635{ 6636 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE; 6637 6638 if (c_parser_next_token_is_keyword (parser, RID_IF)) 6639 result = PRAGMA_OMP_CLAUSE_IF; 6640 else if (c_parser_next_token_is_keyword (parser, RID_DEFAULT)) 6641 result = PRAGMA_OMP_CLAUSE_DEFAULT; 6642 else if (c_parser_next_token_is (parser, CPP_NAME)) 6643 { 6644 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); 6645 6646 switch (p[0]) 6647 { 6648 case 'c': 6649 if (!strcmp ("copyin", p)) 6650 result = PRAGMA_OMP_CLAUSE_COPYIN; 6651 else if (!strcmp ("copyprivate", p)) 6652 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE; 6653 break; 6654 case 'f': 6655 if (!strcmp ("firstprivate", p)) 6656 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE; 6657 break; 6658 case 'l': 6659 if (!strcmp ("lastprivate", p)) 6660 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE; 6661 break; 6662 case 'n': 6663 if (!strcmp ("nowait", p)) 6664 result = PRAGMA_OMP_CLAUSE_NOWAIT; 6665 else if (!strcmp ("num_threads", p)) 6666 result = PRAGMA_OMP_CLAUSE_NUM_THREADS; 6667 break; 6668 case 'o': 6669 if (!strcmp ("ordered", p)) 6670 result = PRAGMA_OMP_CLAUSE_ORDERED; 6671 break; 6672 case 'p': 6673 if (!strcmp ("private", p)) 6674 result = PRAGMA_OMP_CLAUSE_PRIVATE; 6675 break; 6676 case 'r': 6677 if (!strcmp ("reduction", p)) 6678 result = PRAGMA_OMP_CLAUSE_REDUCTION; 6679 break; 6680 case 's': 6681 if (!strcmp ("schedule", p)) 6682 result = PRAGMA_OMP_CLAUSE_SCHEDULE; 6683 else if (!strcmp ("shared", p)) 6684 result = PRAGMA_OMP_CLAUSE_SHARED; 6685 break; 6686 } 6687 } 6688 6689 if (result != PRAGMA_OMP_CLAUSE_NONE) 6690 c_parser_consume_token (parser); 6691 6692 return result; 6693} 6694 6695/* Validate that a clause of the given type does not already exist. */ 6696 6697static void 6698check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name) 6699{ 6700 tree c; 6701 6702 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) 6703 if (OMP_CLAUSE_CODE (c) == code) 6704 { 6705 error ("too many %qs clauses", name); 6706 break; 6707 } 6708} 6709 6710/* OpenMP 2.5: 6711 variable-list: 6712 identifier 6713 variable-list , identifier 6714 6715 If KIND is nonzero, create the appropriate node and install the decl 6716 in OMP_CLAUSE_DECL and add the node to the head of the list. 6717 6718 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE; 6719 return the list created. */ 6720 6721static tree 6722c_parser_omp_variable_list (c_parser *parser, enum omp_clause_code kind, 6723 tree list) 6724{ 6725 if (c_parser_next_token_is_not (parser, CPP_NAME) 6726 || c_parser_peek_token (parser)->id_kind != C_ID_ID) 6727 c_parser_error (parser, "expected identifier"); 6728 6729 while (c_parser_next_token_is (parser, CPP_NAME) 6730 && c_parser_peek_token (parser)->id_kind == C_ID_ID) 6731 { 6732 tree t = lookup_name (c_parser_peek_token (parser)->value); 6733 6734 if (t == NULL_TREE) 6735 undeclared_variable (c_parser_peek_token (parser)->value, 6736 c_parser_peek_token (parser)->location); 6737 else if (t == error_mark_node) 6738 ; 6739 else if (kind != 0) 6740 { 6741 tree u = build_omp_clause (kind); 6742 OMP_CLAUSE_DECL (u) = t; 6743 OMP_CLAUSE_CHAIN (u) = list; 6744 list = u; 6745 } 6746 else 6747 list = tree_cons (t, NULL_TREE, list); 6748 6749 c_parser_consume_token (parser); 6750 6751 if (c_parser_next_token_is_not (parser, CPP_COMMA)) 6752 break; 6753 6754 c_parser_consume_token (parser); 6755 } 6756 6757 return list; 6758} 6759 6760/* Similarly, but expect leading and trailing parenthesis. This is a very 6761 common case for omp clauses. */ 6762 6763static tree 6764c_parser_omp_var_list_parens (c_parser *parser, enum tree_code kind, tree list) 6765{ 6766 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6767 { 6768 list = c_parser_omp_variable_list (parser, kind, list); 6769 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6770 } 6771 return list; 6772} 6773 6774/* OpenMP 2.5: 6775 copyin ( variable-list ) */ 6776 6777static tree 6778c_parser_omp_clause_copyin (c_parser *parser, tree list) 6779{ 6780 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYIN, list); 6781} 6782 6783/* OpenMP 2.5: 6784 copyprivate ( variable-list ) */ 6785 6786static tree 6787c_parser_omp_clause_copyprivate (c_parser *parser, tree list) 6788{ 6789 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_COPYPRIVATE, list); 6790} 6791 6792/* OpenMP 2.5: 6793 default ( shared | none ) */ 6794 6795static tree 6796c_parser_omp_clause_default (c_parser *parser, tree list) 6797{ 6798 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED; 6799 tree c; 6800 6801 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6802 return list; 6803 if (c_parser_next_token_is (parser, CPP_NAME)) 6804 { 6805 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); 6806 6807 switch (p[0]) 6808 { 6809 case 'n': 6810 if (strcmp ("none", p) != 0) 6811 goto invalid_kind; 6812 kind = OMP_CLAUSE_DEFAULT_NONE; 6813 break; 6814 6815 case 's': 6816 if (strcmp ("shared", p) != 0) 6817 goto invalid_kind; 6818 kind = OMP_CLAUSE_DEFAULT_SHARED; 6819 break; 6820 6821 default: 6822 goto invalid_kind; 6823 } 6824 6825 c_parser_consume_token (parser); 6826 } 6827 else 6828 { 6829 invalid_kind: 6830 c_parser_error (parser, "expected %<none%> or %<shared%>"); 6831 } 6832 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6833 6834 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED) 6835 return list; 6836 6837 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default"); 6838 c = build_omp_clause (OMP_CLAUSE_DEFAULT); 6839 OMP_CLAUSE_CHAIN (c) = list; 6840 OMP_CLAUSE_DEFAULT_KIND (c) = kind; 6841 6842 return c; 6843} 6844 6845/* OpenMP 2.5: 6846 firstprivate ( variable-list ) */ 6847 6848static tree 6849c_parser_omp_clause_firstprivate (c_parser *parser, tree list) 6850{ 6851 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_FIRSTPRIVATE, list); 6852} 6853 6854/* OpenMP 2.5: 6855 if ( expression ) */ 6856 6857static tree 6858c_parser_omp_clause_if (c_parser *parser, tree list) 6859{ 6860 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 6861 { 6862 tree t = c_parser_paren_condition (parser); 6863 tree c; 6864 6865 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if"); 6866 6867 c = build_omp_clause (OMP_CLAUSE_IF); 6868 OMP_CLAUSE_IF_EXPR (c) = t; 6869 OMP_CLAUSE_CHAIN (c) = list; 6870 list = c; 6871 } 6872 else 6873 c_parser_error (parser, "expected %<(%>"); 6874 6875 return list; 6876} 6877 6878/* OpenMP 2.5: 6879 lastprivate ( variable-list ) */ 6880 6881static tree 6882c_parser_omp_clause_lastprivate (c_parser *parser, tree list) 6883{ 6884 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_LASTPRIVATE, list); 6885} 6886 6887/* OpenMP 2.5: 6888 nowait */ 6889 6890static tree 6891c_parser_omp_clause_nowait (c_parser *parser ATTRIBUTE_UNUSED, tree list) 6892{ 6893 tree c; 6894 6895 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait"); 6896 6897 c = build_omp_clause (OMP_CLAUSE_NOWAIT); 6898 OMP_CLAUSE_CHAIN (c) = list; 6899 return c; 6900} 6901 6902/* OpenMP 2.5: 6903 num_threads ( expression ) */ 6904 6905static tree 6906c_parser_omp_clause_num_threads (c_parser *parser, tree list) 6907{ 6908 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6909 { 6910 tree c, t = c_parser_expression (parser).value; 6911 6912 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 6913 6914 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))) 6915 { 6916 c_parser_error (parser, "expected integer expression"); 6917 return list; 6918 } 6919 6920 /* Attempt to statically determine when the number isn't positive. */ 6921 c = fold_build2 (LE_EXPR, boolean_type_node, t, 6922 build_int_cst (TREE_TYPE (t), 0)); 6923 if (c == boolean_true_node) 6924 { 6925 warning (0, "%<num_threads%> value must be positive"); 6926 t = integer_one_node; 6927 } 6928 6929 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads"); 6930 6931 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS); 6932 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t; 6933 OMP_CLAUSE_CHAIN (c) = list; 6934 list = c; 6935 } 6936 6937 return list; 6938} 6939 6940/* OpenMP 2.5: 6941 ordered */ 6942 6943static tree 6944c_parser_omp_clause_ordered (c_parser *parser ATTRIBUTE_UNUSED, tree list) 6945{ 6946 tree c; 6947 6948 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered"); 6949 6950 c = build_omp_clause (OMP_CLAUSE_ORDERED); 6951 OMP_CLAUSE_CHAIN (c) = list; 6952 return c; 6953} 6954 6955/* OpenMP 2.5: 6956 private ( variable-list ) */ 6957 6958static tree 6959c_parser_omp_clause_private (c_parser *parser, tree list) 6960{ 6961 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_PRIVATE, list); 6962} 6963 6964/* OpenMP 2.5: 6965 reduction ( reduction-operator : variable-list ) 6966 6967 reduction-operator: 6968 One of: + * - & ^ | && || */ 6969 6970static tree 6971c_parser_omp_clause_reduction (c_parser *parser, tree list) 6972{ 6973 if (c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 6974 { 6975 enum tree_code code; 6976 6977 switch (c_parser_peek_token (parser)->type) 6978 { 6979 case CPP_PLUS: 6980 code = PLUS_EXPR; 6981 break; 6982 case CPP_MULT: 6983 code = MULT_EXPR; 6984 break; 6985 case CPP_MINUS: 6986 code = MINUS_EXPR; 6987 break; 6988 case CPP_AND: 6989 code = BIT_AND_EXPR; 6990 break; 6991 case CPP_XOR: 6992 code = BIT_XOR_EXPR; 6993 break; 6994 case CPP_OR: 6995 code = BIT_IOR_EXPR; 6996 break; 6997 case CPP_AND_AND: 6998 code = TRUTH_ANDIF_EXPR; 6999 break; 7000 case CPP_OR_OR: 7001 code = TRUTH_ORIF_EXPR; 7002 break; 7003 default: 7004 c_parser_error (parser, 7005 "expected %<+%>, %<*%>, %<-%>, %<&%>, " 7006 "%<^%>, %<|%>, %<&&%>, or %<||%>"); 7007 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0); 7008 return list; 7009 } 7010 c_parser_consume_token (parser); 7011 if (c_parser_require (parser, CPP_COLON, "expected %<:%>")) 7012 { 7013 tree nl, c; 7014 7015 nl = c_parser_omp_variable_list (parser, OMP_CLAUSE_REDUCTION, list); 7016 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c)) 7017 OMP_CLAUSE_REDUCTION_CODE (c) = code; 7018 7019 list = nl; 7020 } 7021 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7022 } 7023 return list; 7024} 7025 7026/* OpenMP 2.5: 7027 schedule ( schedule-kind ) 7028 schedule ( schedule-kind , expression ) 7029 7030 schedule-kind: 7031 static | dynamic | guided | runtime 7032*/ 7033 7034static tree 7035c_parser_omp_clause_schedule (c_parser *parser, tree list) 7036{ 7037 tree c, t; 7038 7039 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 7040 return list; 7041 7042 c = build_omp_clause (OMP_CLAUSE_SCHEDULE); 7043 7044 if (c_parser_next_token_is (parser, CPP_NAME)) 7045 { 7046 tree kind = c_parser_peek_token (parser)->value; 7047 const char *p = IDENTIFIER_POINTER (kind); 7048 7049 switch (p[0]) 7050 { 7051 case 'd': 7052 if (strcmp ("dynamic", p) != 0) 7053 goto invalid_kind; 7054 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC; 7055 break; 7056 7057 case 'g': 7058 if (strcmp ("guided", p) != 0) 7059 goto invalid_kind; 7060 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED; 7061 break; 7062 7063 case 'r': 7064 if (strcmp ("runtime", p) != 0) 7065 goto invalid_kind; 7066 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME; 7067 break; 7068 7069 default: 7070 goto invalid_kind; 7071 } 7072 } 7073 else if (c_parser_next_token_is_keyword (parser, RID_STATIC)) 7074 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC; 7075 else 7076 goto invalid_kind; 7077 7078 c_parser_consume_token (parser); 7079 if (c_parser_next_token_is (parser, CPP_COMMA)) 7080 { 7081 c_parser_consume_token (parser); 7082 7083 t = c_parser_expr_no_commas (parser, NULL).value; 7084 7085 if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME) 7086 error ("schedule %<runtime%> does not take " 7087 "a %<chunk_size%> parameter"); 7088 else if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE) 7089 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t; 7090 else 7091 c_parser_error (parser, "expected integer expression"); 7092 7093 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7094 } 7095 else 7096 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 7097 "expected %<,%> or %<)%>"); 7098 7099 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule"); 7100 OMP_CLAUSE_CHAIN (c) = list; 7101 return c; 7102 7103 invalid_kind: 7104 c_parser_error (parser, "invalid schedule kind"); 7105 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, 0); 7106 return list; 7107} 7108 7109/* OpenMP 2.5: 7110 shared ( variable-list ) */ 7111 7112static tree 7113c_parser_omp_clause_shared (c_parser *parser, tree list) 7114{ 7115 return c_parser_omp_var_list_parens (parser, OMP_CLAUSE_SHARED, list); 7116} 7117 7118/* Parse all OpenMP clauses. The set clauses allowed by the directive 7119 is a bitmask in MASK. Return the list of clauses found; the result 7120 of clause default goes in *pdefault. */ 7121 7122static tree 7123c_parser_omp_all_clauses (c_parser *parser, unsigned int mask, 7124 const char *where) 7125{ 7126 tree clauses = NULL; 7127 7128 while (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL)) 7129 { 7130 const pragma_omp_clause c_kind = c_parser_omp_clause_name (parser); 7131 const char *c_name; 7132 tree prev = clauses; 7133 7134 switch (c_kind) 7135 { 7136 case PRAGMA_OMP_CLAUSE_COPYIN: 7137 clauses = c_parser_omp_clause_copyin (parser, clauses); 7138 c_name = "copyin"; 7139 break; 7140 case PRAGMA_OMP_CLAUSE_COPYPRIVATE: 7141 clauses = c_parser_omp_clause_copyprivate (parser, clauses); 7142 c_name = "copyprivate"; 7143 break; 7144 case PRAGMA_OMP_CLAUSE_DEFAULT: 7145 clauses = c_parser_omp_clause_default (parser, clauses); 7146 c_name = "default"; 7147 break; 7148 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE: 7149 clauses = c_parser_omp_clause_firstprivate (parser, clauses); 7150 c_name = "firstprivate"; 7151 break; 7152 case PRAGMA_OMP_CLAUSE_IF: 7153 clauses = c_parser_omp_clause_if (parser, clauses); 7154 c_name = "if"; 7155 break; 7156 case PRAGMA_OMP_CLAUSE_LASTPRIVATE: 7157 clauses = c_parser_omp_clause_lastprivate (parser, clauses); 7158 c_name = "lastprivate"; 7159 break; 7160 case PRAGMA_OMP_CLAUSE_NOWAIT: 7161 clauses = c_parser_omp_clause_nowait (parser, clauses); 7162 c_name = "nowait"; 7163 break; 7164 case PRAGMA_OMP_CLAUSE_NUM_THREADS: 7165 clauses = c_parser_omp_clause_num_threads (parser, clauses); 7166 c_name = "num_threads"; 7167 break; 7168 case PRAGMA_OMP_CLAUSE_ORDERED: 7169 clauses = c_parser_omp_clause_ordered (parser, clauses); 7170 c_name = "ordered"; 7171 break; 7172 case PRAGMA_OMP_CLAUSE_PRIVATE: 7173 clauses = c_parser_omp_clause_private (parser, clauses); 7174 c_name = "private"; 7175 break; 7176 case PRAGMA_OMP_CLAUSE_REDUCTION: 7177 clauses = c_parser_omp_clause_reduction (parser, clauses); 7178 c_name = "reduction"; 7179 break; 7180 case PRAGMA_OMP_CLAUSE_SCHEDULE: 7181 clauses = c_parser_omp_clause_schedule (parser, clauses); 7182 c_name = "schedule"; 7183 break; 7184 case PRAGMA_OMP_CLAUSE_SHARED: 7185 clauses = c_parser_omp_clause_shared (parser, clauses); 7186 c_name = "shared"; 7187 break; 7188 default: 7189 c_parser_error (parser, "expected %<#pragma omp%> clause"); 7190 goto saw_error; 7191 } 7192 7193 if (((mask >> c_kind) & 1) == 0 && !parser->error) 7194 { 7195 /* Remove the invalid clause(s) from the list to avoid 7196 confusing the rest of the compiler. */ 7197 clauses = prev; 7198 error ("%qs is not valid for %qs", c_name, where); 7199 } 7200 } 7201 7202 saw_error: 7203 c_parser_skip_to_pragma_eol (parser); 7204 7205 return c_finish_omp_clauses (clauses); 7206} 7207 7208/* OpenMP 2.5: 7209 structured-block: 7210 statement 7211 7212 In practice, we're also interested in adding the statement to an 7213 outer node. So it is convenient if we work around the fact that 7214 c_parser_statement calls add_stmt. */ 7215 7216static tree 7217c_parser_omp_structured_block (c_parser *parser) 7218{ 7219 tree stmt = push_stmt_list (); 7220 c_parser_statement (parser); 7221 return pop_stmt_list (stmt); 7222} 7223 7224/* OpenMP 2.5: 7225 # pragma omp atomic new-line 7226 expression-stmt 7227 7228 expression-stmt: 7229 x binop= expr | x++ | ++x | x-- | --x 7230 binop: 7231 +, *, -, /, &, ^, |, <<, >> 7232 7233 where x is an lvalue expression with scalar type. */ 7234 7235static void 7236c_parser_omp_atomic (c_parser *parser) 7237{ 7238 tree lhs, rhs; 7239 tree stmt; 7240 enum tree_code code; 7241 7242 c_parser_skip_to_pragma_eol (parser); 7243 7244 lhs = c_parser_unary_expression (parser).value; 7245 switch (TREE_CODE (lhs)) 7246 { 7247 case ERROR_MARK: 7248 saw_error: 7249 c_parser_skip_to_end_of_block_or_statement (parser); 7250 return; 7251 7252 case PREINCREMENT_EXPR: 7253 case POSTINCREMENT_EXPR: 7254 lhs = TREE_OPERAND (lhs, 0); 7255 code = PLUS_EXPR; 7256 rhs = integer_one_node; 7257 break; 7258 7259 case PREDECREMENT_EXPR: 7260 case POSTDECREMENT_EXPR: 7261 lhs = TREE_OPERAND (lhs, 0); 7262 code = MINUS_EXPR; 7263 rhs = integer_one_node; 7264 break; 7265 7266 default: 7267 switch (c_parser_peek_token (parser)->type) 7268 { 7269 case CPP_MULT_EQ: 7270 code = MULT_EXPR; 7271 break; 7272 case CPP_DIV_EQ: 7273 code = TRUNC_DIV_EXPR; 7274 break; 7275 case CPP_PLUS_EQ: 7276 code = PLUS_EXPR; 7277 break; 7278 case CPP_MINUS_EQ: 7279 code = MINUS_EXPR; 7280 break; 7281 case CPP_LSHIFT_EQ: 7282 code = LSHIFT_EXPR; 7283 break; 7284 case CPP_RSHIFT_EQ: 7285 code = RSHIFT_EXPR; 7286 break; 7287 case CPP_AND_EQ: 7288 code = BIT_AND_EXPR; 7289 break; 7290 case CPP_OR_EQ: 7291 code = BIT_IOR_EXPR; 7292 break; 7293 case CPP_XOR_EQ: 7294 code = BIT_XOR_EXPR; 7295 break; 7296 default: 7297 c_parser_error (parser, 7298 "invalid operator for %<#pragma omp atomic%>"); 7299 goto saw_error; 7300 } 7301 7302 c_parser_consume_token (parser); 7303 rhs = c_parser_expression (parser).value; 7304 break; 7305 } 7306 stmt = c_finish_omp_atomic (code, lhs, rhs); 7307 if (stmt != error_mark_node) 7308 add_stmt (stmt); 7309 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 7310} 7311 7312 7313/* OpenMP 2.5: 7314 # pragma omp barrier new-line 7315*/ 7316 7317static void 7318c_parser_omp_barrier (c_parser *parser) 7319{ 7320 c_parser_consume_pragma (parser); 7321 c_parser_skip_to_pragma_eol (parser); 7322 7323 c_finish_omp_barrier (); 7324} 7325 7326/* OpenMP 2.5: 7327 # pragma omp critical [(name)] new-line 7328 structured-block 7329*/ 7330 7331static tree 7332c_parser_omp_critical (c_parser *parser) 7333{ 7334 tree stmt, name = NULL; 7335 7336 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 7337 { 7338 c_parser_consume_token (parser); 7339 if (c_parser_next_token_is (parser, CPP_NAME)) 7340 { 7341 name = c_parser_peek_token (parser)->value; 7342 c_parser_consume_token (parser); 7343 c_parser_require (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7344 } 7345 else 7346 c_parser_error (parser, "expected identifier"); 7347 } 7348 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL)) 7349 c_parser_error (parser, "expected %<(%> or end of line"); 7350 c_parser_skip_to_pragma_eol (parser); 7351 7352 stmt = c_parser_omp_structured_block (parser); 7353 return c_finish_omp_critical (stmt, name); 7354} 7355 7356/* OpenMP 2.5: 7357 # pragma omp flush flush-vars[opt] new-line 7358 7359 flush-vars: 7360 ( variable-list ) */ 7361 7362static void 7363c_parser_omp_flush (c_parser *parser) 7364{ 7365 c_parser_consume_pragma (parser); 7366 if (c_parser_next_token_is (parser, CPP_OPEN_PAREN)) 7367 c_parser_omp_var_list_parens (parser, 0, NULL); 7368 else if (c_parser_next_token_is_not (parser, CPP_PRAGMA_EOL)) 7369 c_parser_error (parser, "expected %<(%> or end of line"); 7370 c_parser_skip_to_pragma_eol (parser); 7371 7372 c_finish_omp_flush (); 7373} 7374 7375/* Parse the restricted form of the for statment allowed by OpenMP. 7376 The real trick here is to determine the loop control variable early 7377 so that we can push a new decl if necessary to make it private. */ 7378 7379static tree 7380c_parser_omp_for_loop (c_parser *parser) 7381{ 7382 tree decl, cond, incr, save_break, save_cont, body, init; 7383 location_t loc; 7384 7385 if (!c_parser_next_token_is_keyword (parser, RID_FOR)) 7386 { 7387 c_parser_error (parser, "for statement expected"); 7388 return NULL; 7389 } 7390 loc = c_parser_peek_token (parser)->location; 7391 c_parser_consume_token (parser); 7392 7393 if (!c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>")) 7394 return NULL; 7395 7396 /* Parse the initialization declaration or expression. */ 7397 if (c_parser_next_token_starts_declspecs (parser)) 7398 { 7399 c_parser_declaration_or_fndef (parser, true, true, true, true); 7400 decl = check_for_loop_decls (); 7401 if (decl == NULL) 7402 goto error_init; 7403 init = decl; 7404 } 7405 else if (c_parser_next_token_is (parser, CPP_NAME) 7406 && c_parser_peek_2nd_token (parser)->type == CPP_EQ) 7407 { 7408 decl = c_parser_postfix_expression (parser).value; 7409 7410 c_parser_require (parser, CPP_EQ, "expected %<=%>"); 7411 7412 init = c_parser_expr_no_commas (parser, NULL).value; 7413 init = build_modify_expr (decl, NOP_EXPR, init); 7414 init = c_process_expr_stmt (init); 7415 7416 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 7417 } 7418 else 7419 goto error_init; 7420 7421 /* Parse the loop condition. */ 7422 cond = NULL_TREE; 7423 if (c_parser_next_token_is_not (parser, CPP_SEMICOLON)) 7424 { 7425 cond = c_parser_expression_conv (parser).value; 7426 cond = c_objc_common_truthvalue_conversion (cond); 7427 if (EXPR_P (cond)) 7428 SET_EXPR_LOCATION (cond, input_location); 7429 } 7430 c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>"); 7431 7432 /* Parse the increment expression. */ 7433 incr = NULL_TREE; 7434 if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN)) 7435 incr = c_process_expr_stmt (c_parser_expression (parser).value); 7436 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7437 7438 parse_body: 7439 save_break = c_break_label; 7440 c_break_label = size_one_node; 7441 save_cont = c_cont_label; 7442 c_cont_label = NULL_TREE; 7443 body = push_stmt_list (); 7444 7445 add_stmt (c_parser_c99_block_statement (parser)); 7446 if (c_cont_label) 7447 add_stmt (build1 (LABEL_EXPR, void_type_node, c_cont_label)); 7448 7449 body = pop_stmt_list (body); 7450 c_break_label = save_break; 7451 c_cont_label = save_cont; 7452 7453 /* Only bother calling c_finish_omp_for if we havn't already generated 7454 an error from the initialization parsing. */ 7455 if (decl != NULL && decl != error_mark_node && init != error_mark_node) 7456 return c_finish_omp_for (loc, decl, init, cond, incr, body, NULL); 7457 return NULL; 7458 7459 error_init: 7460 c_parser_error (parser, "expected iteration declaration or initialization"); 7461 c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, "expected %<)%>"); 7462 decl = init = cond = incr = NULL_TREE; 7463 goto parse_body; 7464} 7465 7466/* OpenMP 2.5: 7467 #pragma omp for for-clause[optseq] new-line 7468 for-loop 7469*/ 7470 7471#define OMP_FOR_CLAUSE_MASK \ 7472 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 7473 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 7474 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ 7475 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 7476 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \ 7477 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \ 7478 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 7479 7480static tree 7481c_parser_omp_for (c_parser *parser) 7482{ 7483 tree block, clauses, ret; 7484 7485 clauses = c_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK, 7486 "#pragma omp for"); 7487 7488 block = c_begin_compound_stmt (true); 7489 ret = c_parser_omp_for_loop (parser); 7490 if (ret) 7491 OMP_FOR_CLAUSES (ret) = clauses; 7492 block = c_end_compound_stmt (block, true); 7493 add_stmt (block); 7494 7495 return ret; 7496} 7497 7498/* OpenMP 2.5: 7499 # pragma omp master new-line 7500 structured-block 7501*/ 7502 7503static tree 7504c_parser_omp_master (c_parser *parser) 7505{ 7506 c_parser_skip_to_pragma_eol (parser); 7507 return c_finish_omp_master (c_parser_omp_structured_block (parser)); 7508} 7509 7510/* OpenMP 2.5: 7511 # pragma omp ordered new-line 7512 structured-block 7513*/ 7514 7515static tree 7516c_parser_omp_ordered (c_parser *parser) 7517{ 7518 c_parser_skip_to_pragma_eol (parser); 7519 return c_finish_omp_ordered (c_parser_omp_structured_block (parser)); 7520} 7521 7522/* OpenMP 2.5: 7523 7524 section-scope: 7525 { section-sequence } 7526 7527 section-sequence: 7528 section-directive[opt] structured-block 7529 section-sequence section-directive structured-block */ 7530 7531static tree 7532c_parser_omp_sections_scope (c_parser *parser) 7533{ 7534 tree stmt, substmt; 7535 bool error_suppress = false; 7536 location_t loc; 7537 7538 if (!c_parser_require (parser, CPP_OPEN_BRACE, "expected %<{%>")) 7539 { 7540 /* Avoid skipping until the end of the block. */ 7541 parser->error = false; 7542 return NULL_TREE; 7543 } 7544 7545 stmt = push_stmt_list (); 7546 7547 loc = c_parser_peek_token (parser)->location; 7548 if (c_parser_peek_token (parser)->pragma_kind != PRAGMA_OMP_SECTION) 7549 { 7550 substmt = push_stmt_list (); 7551 7552 while (1) 7553 { 7554 c_parser_statement (parser); 7555 7556 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION) 7557 break; 7558 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 7559 break; 7560 if (c_parser_next_token_is (parser, CPP_EOF)) 7561 break; 7562 } 7563 7564 substmt = pop_stmt_list (substmt); 7565 substmt = build1 (OMP_SECTION, void_type_node, substmt); 7566 SET_EXPR_LOCATION (substmt, loc); 7567 add_stmt (substmt); 7568 } 7569 7570 while (1) 7571 { 7572 if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE)) 7573 break; 7574 if (c_parser_next_token_is (parser, CPP_EOF)) 7575 break; 7576 7577 loc = c_parser_peek_token (parser)->location; 7578 if (c_parser_peek_token (parser)->pragma_kind == PRAGMA_OMP_SECTION) 7579 { 7580 c_parser_consume_pragma (parser); 7581 c_parser_skip_to_pragma_eol (parser); 7582 error_suppress = false; 7583 } 7584 else if (!error_suppress) 7585 { 7586 error ("expected %<#pragma omp section%> or %<}%>"); 7587 error_suppress = true; 7588 } 7589 7590 substmt = c_parser_omp_structured_block (parser); 7591 substmt = build1 (OMP_SECTION, void_type_node, substmt); 7592 SET_EXPR_LOCATION (substmt, loc); 7593 add_stmt (substmt); 7594 } 7595 c_parser_skip_until_found (parser, CPP_CLOSE_BRACE, 7596 "expected %<#pragma omp section%> or %<}%>"); 7597 7598 substmt = pop_stmt_list (stmt); 7599 7600 stmt = make_node (OMP_SECTIONS); 7601 TREE_TYPE (stmt) = void_type_node; 7602 OMP_SECTIONS_BODY (stmt) = substmt; 7603 7604 return add_stmt (stmt); 7605} 7606 7607/* OpenMP 2.5: 7608 # pragma omp sections sections-clause[optseq] newline 7609 sections-scope 7610*/ 7611 7612#define OMP_SECTIONS_CLAUSE_MASK \ 7613 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 7614 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 7615 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ 7616 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 7617 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 7618 7619static tree 7620c_parser_omp_sections (c_parser *parser) 7621{ 7622 tree block, clauses, ret; 7623 7624 clauses = c_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK, 7625 "#pragma omp sections"); 7626 7627 block = c_begin_compound_stmt (true); 7628 ret = c_parser_omp_sections_scope (parser); 7629 if (ret) 7630 OMP_SECTIONS_CLAUSES (ret) = clauses; 7631 block = c_end_compound_stmt (block, true); 7632 add_stmt (block); 7633 7634 return ret; 7635} 7636 7637/* OpenMP 2.5: 7638 # pragma parallel parallel-clause new-line 7639 # pragma parallel for parallel-for-clause new-line 7640 # pragma parallel sections parallel-sections-clause new-line 7641*/ 7642 7643#define OMP_PARALLEL_CLAUSE_MASK \ 7644 ( (1u << PRAGMA_OMP_CLAUSE_IF) \ 7645 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 7646 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 7647 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ 7648 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \ 7649 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \ 7650 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 7651 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS)) 7652 7653static tree 7654c_parser_omp_parallel (c_parser *parser) 7655{ 7656 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL; 7657 const char *p_name = "#pragma omp parallel"; 7658 tree stmt, clauses, par_clause, ws_clause, block; 7659 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK; 7660 7661 if (c_parser_next_token_is_keyword (parser, RID_FOR)) 7662 { 7663 c_parser_consume_token (parser); 7664 p_kind = PRAGMA_OMP_PARALLEL_FOR; 7665 p_name = "#pragma omp parallel for"; 7666 mask |= OMP_FOR_CLAUSE_MASK; 7667 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); 7668 } 7669 else if (c_parser_next_token_is (parser, CPP_NAME)) 7670 { 7671 const char *p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); 7672 if (strcmp (p, "sections") == 0) 7673 { 7674 c_parser_consume_token (parser); 7675 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS; 7676 p_name = "#pragma omp parallel sections"; 7677 mask |= OMP_SECTIONS_CLAUSE_MASK; 7678 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); 7679 } 7680 } 7681 7682 clauses = c_parser_omp_all_clauses (parser, mask, p_name); 7683 7684 switch (p_kind) 7685 { 7686 case PRAGMA_OMP_PARALLEL: 7687 block = c_begin_omp_parallel (); 7688 c_parser_statement (parser); 7689 stmt = c_finish_omp_parallel (clauses, block); 7690 break; 7691 7692 case PRAGMA_OMP_PARALLEL_FOR: 7693 block = c_begin_omp_parallel (); 7694 c_split_parallel_clauses (clauses, &par_clause, &ws_clause); 7695 stmt = c_parser_omp_for_loop (parser); 7696 if (stmt) 7697 OMP_FOR_CLAUSES (stmt) = ws_clause; 7698 stmt = c_finish_omp_parallel (par_clause, block); 7699 OMP_PARALLEL_COMBINED (stmt) = 1; 7700 break; 7701 7702 case PRAGMA_OMP_PARALLEL_SECTIONS: 7703 block = c_begin_omp_parallel (); 7704 c_split_parallel_clauses (clauses, &par_clause, &ws_clause); 7705 stmt = c_parser_omp_sections_scope (parser); 7706 if (stmt) 7707 OMP_SECTIONS_CLAUSES (stmt) = ws_clause; 7708 stmt = c_finish_omp_parallel (par_clause, block); 7709 OMP_PARALLEL_COMBINED (stmt) = 1; 7710 break; 7711 7712 default: 7713 gcc_unreachable (); 7714 } 7715 7716 return stmt; 7717} 7718 7719/* OpenMP 2.5: 7720 # pragma omp single single-clause[optseq] new-line 7721 structured-block 7722*/ 7723 7724#define OMP_SINGLE_CLAUSE_MASK \ 7725 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 7726 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 7727 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \ 7728 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 7729 7730static tree 7731c_parser_omp_single (c_parser *parser) 7732{ 7733 tree stmt = make_node (OMP_SINGLE); 7734 TREE_TYPE (stmt) = void_type_node; 7735 7736 OMP_SINGLE_CLAUSES (stmt) 7737 = c_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK, 7738 "#pragma omp single"); 7739 OMP_SINGLE_BODY (stmt) = c_parser_omp_structured_block (parser); 7740 7741 return add_stmt (stmt); 7742} 7743 7744 7745/* Main entry point to parsing most OpenMP pragmas. */ 7746 7747static void 7748c_parser_omp_construct (c_parser *parser) 7749{ 7750 enum pragma_kind p_kind; 7751 location_t loc; 7752 tree stmt; 7753 7754 loc = c_parser_peek_token (parser)->location; 7755 p_kind = c_parser_peek_token (parser)->pragma_kind; 7756 c_parser_consume_pragma (parser); 7757 7758 /* For all constructs below except #pragma omp atomic 7759 MUST_NOT_THROW catch handlers are needed when exceptions 7760 are enabled. */ 7761 if (p_kind != PRAGMA_OMP_ATOMIC) 7762 c_maybe_initialize_eh (); 7763 7764 switch (p_kind) 7765 { 7766 case PRAGMA_OMP_ATOMIC: 7767 c_parser_omp_atomic (parser); 7768 return; 7769 case PRAGMA_OMP_CRITICAL: 7770 stmt = c_parser_omp_critical (parser); 7771 break; 7772 case PRAGMA_OMP_FOR: 7773 stmt = c_parser_omp_for (parser); 7774 break; 7775 case PRAGMA_OMP_MASTER: 7776 stmt = c_parser_omp_master (parser); 7777 break; 7778 case PRAGMA_OMP_ORDERED: 7779 stmt = c_parser_omp_ordered (parser); 7780 break; 7781 case PRAGMA_OMP_PARALLEL: 7782 stmt = c_parser_omp_parallel (parser); 7783 break; 7784 case PRAGMA_OMP_SECTIONS: 7785 stmt = c_parser_omp_sections (parser); 7786 break; 7787 case PRAGMA_OMP_SINGLE: 7788 stmt = c_parser_omp_single (parser); 7789 break; 7790 default: 7791 gcc_unreachable (); 7792 } 7793 7794 if (stmt) 7795 SET_EXPR_LOCATION (stmt, loc); 7796} 7797 7798 7799/* OpenMP 2.5: 7800 # pragma omp threadprivate (variable-list) */ 7801 7802static void 7803c_parser_omp_threadprivate (c_parser *parser) 7804{ 7805 tree vars, t; 7806 7807 c_parser_consume_pragma (parser); 7808 vars = c_parser_omp_var_list_parens (parser, 0, NULL); 7809 7810 if (!targetm.have_tls) 7811 sorry ("threadprivate variables not supported in this target"); 7812 7813 /* Mark every variable in VARS to be assigned thread local storage. */ 7814 for (t = vars; t; t = TREE_CHAIN (t)) 7815 { 7816 tree v = TREE_PURPOSE (t); 7817 7818 /* If V had already been marked threadprivate, it doesn't matter 7819 whether it had been used prior to this point. */ 7820 if (TREE_USED (v) && !C_DECL_THREADPRIVATE_P (v)) 7821 error ("%qE declared %<threadprivate%> after first use", v); 7822 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v)) 7823 error ("automatic variable %qE cannot be %<threadprivate%>", v); 7824 else if (! COMPLETE_TYPE_P (TREE_TYPE (v))) 7825 error ("%<threadprivate%> %qE has incomplete type", v); 7826 else 7827 { 7828 if (! DECL_THREAD_LOCAL_P (v)) 7829 { 7830 DECL_TLS_MODEL (v) = decl_default_tls_model (v); 7831 /* If rtl has been already set for this var, call 7832 make_decl_rtl once again, so that encode_section_info 7833 has a chance to look at the new decl flags. */ 7834 if (DECL_RTL_SET_P (v)) 7835 make_decl_rtl (v); 7836 } 7837 C_DECL_THREADPRIVATE_P (v) = 1; 7838 } 7839 } 7840 7841 c_parser_skip_to_pragma_eol (parser); 7842} 7843 7844 7845/* Parse a single source file. */ 7846 7847void 7848c_parse_file (void) 7849{ 7850 /* Use local storage to begin. If the first token is a pragma, parse it. 7851 If it is #pragma GCC pch_preprocess, then this will load a PCH file 7852 which will cause garbage collection. */ 7853 c_parser tparser; 7854 7855 memset (&tparser, 0, sizeof tparser); 7856 the_parser = &tparser; 7857 7858 if (c_parser_peek_token (&tparser)->pragma_kind == PRAGMA_GCC_PCH_PREPROCESS) 7859 c_parser_pragma_pch_preprocess (&tparser); 7860 7861 the_parser = GGC_NEW (c_parser); 7862 *the_parser = tparser; 7863 7864 c_parser_translation_unit (the_parser); 7865 the_parser = NULL; 7866} 7867 7868#include "gt-c-parser.h" 7869