1 /* C++ Parser. 2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 3 2005, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. 4 Written by Mark Mitchell <mark@codesourcery.com>. 5 6 This file is part of GCC. 7 8 GCC is free software; you can redistribute it and/or modify it 9 under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3, or (at your option) 11 any later version. 12 13 GCC is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with GCC; see the file COPYING3. If not see 20 <http://www.gnu.org/licenses/>. */ 21 22 #include "config.h" 23 #include "system.h" 24 #include "coretypes.h" 25 #include "tm.h" 26 #include "timevar.h" 27 #include "cpplib.h" 28 #include "tree.h" 29 #include "cp-tree.h" 30 #include "intl.h" 31 #include "c-family/c-pragma.h" 32 #include "decl.h" 33 #include "flags.h" 34 #include "diagnostic-core.h" 35 #include "output.h" 36 #include "target.h" 37 #include "cgraph.h" 38 #include "c-family/c-common.h" 39 #include "c-family/c-objc.h" 40 #include "plugin.h" 41 #include "tree-pretty-print.h" 42 #include "parser.h" 43 44 45 /* The lexer. */ 46 47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp 48 and c-lex.c) and the C++ parser. */ 49 50 static cp_token eof_token = 51 { 52 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL } 53 }; 54 55 /* The various kinds of non integral constant we encounter. */ 56 typedef enum non_integral_constant { 57 NIC_NONE, 58 /* floating-point literal */ 59 NIC_FLOAT, 60 /* %<this%> */ 61 NIC_THIS, 62 /* %<__FUNCTION__%> */ 63 NIC_FUNC_NAME, 64 /* %<__PRETTY_FUNCTION__%> */ 65 NIC_PRETTY_FUNC, 66 /* %<__func__%> */ 67 NIC_C99_FUNC, 68 /* "%<va_arg%> */ 69 NIC_VA_ARG, 70 /* a cast */ 71 NIC_CAST, 72 /* %<typeid%> operator */ 73 NIC_TYPEID, 74 /* non-constant compound literals */ 75 NIC_NCC, 76 /* a function call */ 77 NIC_FUNC_CALL, 78 /* an increment */ 79 NIC_INC, 80 /* an decrement */ 81 NIC_DEC, 82 /* an array reference */ 83 NIC_ARRAY_REF, 84 /* %<->%> */ 85 NIC_ARROW, 86 /* %<.%> */ 87 NIC_POINT, 88 /* the address of a label */ 89 NIC_ADDR_LABEL, 90 /* %<*%> */ 91 NIC_STAR, 92 /* %<&%> */ 93 NIC_ADDR, 94 /* %<++%> */ 95 NIC_PREINCREMENT, 96 /* %<--%> */ 97 NIC_PREDECREMENT, 98 /* %<new%> */ 99 NIC_NEW, 100 /* %<delete%> */ 101 NIC_DEL, 102 /* calls to overloaded operators */ 103 NIC_OVERLOADED, 104 /* an assignment */ 105 NIC_ASSIGNMENT, 106 /* a comma operator */ 107 NIC_COMMA, 108 /* a call to a constructor */ 109 NIC_CONSTRUCTOR, 110 /* a transaction expression */ 111 NIC_TRANSACTION 112 } non_integral_constant; 113 114 /* The various kinds of errors about name-lookup failing. */ 115 typedef enum name_lookup_error { 116 /* NULL */ 117 NLE_NULL, 118 /* is not a type */ 119 NLE_TYPE, 120 /* is not a class or namespace */ 121 NLE_CXX98, 122 /* is not a class, namespace, or enumeration */ 123 NLE_NOT_CXX98 124 } name_lookup_error; 125 126 /* The various kinds of required token */ 127 typedef enum required_token { 128 RT_NONE, 129 RT_SEMICOLON, /* ';' */ 130 RT_OPEN_PAREN, /* '(' */ 131 RT_CLOSE_BRACE, /* '}' */ 132 RT_OPEN_BRACE, /* '{' */ 133 RT_CLOSE_SQUARE, /* ']' */ 134 RT_OPEN_SQUARE, /* '[' */ 135 RT_COMMA, /* ',' */ 136 RT_SCOPE, /* '::' */ 137 RT_LESS, /* '<' */ 138 RT_GREATER, /* '>' */ 139 RT_EQ, /* '=' */ 140 RT_ELLIPSIS, /* '...' */ 141 RT_MULT, /* '*' */ 142 RT_COMPL, /* '~' */ 143 RT_COLON, /* ':' */ 144 RT_COLON_SCOPE, /* ':' or '::' */ 145 RT_CLOSE_PAREN, /* ')' */ 146 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */ 147 RT_PRAGMA_EOL, /* end of line */ 148 RT_NAME, /* identifier */ 149 150 /* The type is CPP_KEYWORD */ 151 RT_NEW, /* new */ 152 RT_DELETE, /* delete */ 153 RT_RETURN, /* return */ 154 RT_WHILE, /* while */ 155 RT_EXTERN, /* extern */ 156 RT_STATIC_ASSERT, /* static_assert */ 157 RT_DECLTYPE, /* decltype */ 158 RT_OPERATOR, /* operator */ 159 RT_CLASS, /* class */ 160 RT_TEMPLATE, /* template */ 161 RT_NAMESPACE, /* namespace */ 162 RT_USING, /* using */ 163 RT_ASM, /* asm */ 164 RT_TRY, /* try */ 165 RT_CATCH, /* catch */ 166 RT_THROW, /* throw */ 167 RT_LABEL, /* __label__ */ 168 RT_AT_TRY, /* @try */ 169 RT_AT_SYNCHRONIZED, /* @synchronized */ 170 RT_AT_THROW, /* @throw */ 171 172 RT_SELECT, /* selection-statement */ 173 RT_INTERATION, /* iteration-statement */ 174 RT_JUMP, /* jump-statement */ 175 RT_CLASS_KEY, /* class-key */ 176 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */ 177 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */ 178 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */ 179 RT_TRANSACTION_CANCEL /* __transaction_cancel */ 180 } required_token; 181 182 /* Prototypes. */ 183 184 static cp_lexer *cp_lexer_new_main 185 (void); 186 static cp_lexer *cp_lexer_new_from_tokens 187 (cp_token_cache *tokens); 188 static void cp_lexer_destroy 189 (cp_lexer *); 190 static int cp_lexer_saving_tokens 191 (const cp_lexer *); 192 static cp_token *cp_lexer_token_at 193 (cp_lexer *, cp_token_position); 194 static void cp_lexer_get_preprocessor_token 195 (cp_lexer *, cp_token *); 196 static inline cp_token *cp_lexer_peek_token 197 (cp_lexer *); 198 static cp_token *cp_lexer_peek_nth_token 199 (cp_lexer *, size_t); 200 static inline bool cp_lexer_next_token_is 201 (cp_lexer *, enum cpp_ttype); 202 static bool cp_lexer_next_token_is_not 203 (cp_lexer *, enum cpp_ttype); 204 static bool cp_lexer_next_token_is_keyword 205 (cp_lexer *, enum rid); 206 static cp_token *cp_lexer_consume_token 207 (cp_lexer *); 208 static void cp_lexer_purge_token 209 (cp_lexer *); 210 static void cp_lexer_purge_tokens_after 211 (cp_lexer *, cp_token_position); 212 static void cp_lexer_save_tokens 213 (cp_lexer *); 214 static void cp_lexer_commit_tokens 215 (cp_lexer *); 216 static void cp_lexer_rollback_tokens 217 (cp_lexer *); 218 static void cp_lexer_print_token 219 (FILE *, cp_token *); 220 static inline bool cp_lexer_debugging_p 221 (cp_lexer *); 222 static void cp_lexer_start_debugging 223 (cp_lexer *) ATTRIBUTE_UNUSED; 224 static void cp_lexer_stop_debugging 225 (cp_lexer *) ATTRIBUTE_UNUSED; 226 227 static cp_token_cache *cp_token_cache_new 228 (cp_token *, cp_token *); 229 230 static void cp_parser_initial_pragma 231 (cp_token *); 232 233 static tree cp_literal_operator_id 234 (const char *); 235 236 /* Manifest constants. */ 237 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token)) 238 #define CP_SAVED_TOKEN_STACK 5 239 240 /* Variables. */ 241 242 /* The stream to which debugging output should be written. */ 243 static FILE *cp_lexer_debug_stream; 244 245 /* Nonzero if we are parsing an unevaluated operand: an operand to 246 sizeof, typeof, or alignof. */ 247 int cp_unevaluated_operand; 248 249 /* Dump up to NUM tokens in BUFFER to FILE starting with token 250 START_TOKEN. If START_TOKEN is NULL, the dump starts with the 251 first token in BUFFER. If NUM is 0, dump all the tokens. If 252 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be 253 highlighted by surrounding it in [[ ]]. */ 254 255 static void 256 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, 257 cp_token *start_token, unsigned num, 258 cp_token *curr_token) 259 { 260 unsigned i, nprinted; 261 cp_token *token; 262 bool do_print; 263 264 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer)); 265 266 if (buffer == NULL) 267 return; 268 269 if (num == 0) 270 num = VEC_length (cp_token, buffer); 271 272 if (start_token == NULL) 273 start_token = VEC_address (cp_token, buffer); 274 275 if (start_token > VEC_address (cp_token, buffer)) 276 { 277 cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0)); 278 fprintf (file, " ... "); 279 } 280 281 do_print = false; 282 nprinted = 0; 283 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++) 284 { 285 if (token == start_token) 286 do_print = true; 287 288 if (!do_print) 289 continue; 290 291 nprinted++; 292 if (token == curr_token) 293 fprintf (file, "[["); 294 295 cp_lexer_print_token (file, token); 296 297 if (token == curr_token) 298 fprintf (file, "]]"); 299 300 switch (token->type) 301 { 302 case CPP_SEMICOLON: 303 case CPP_OPEN_BRACE: 304 case CPP_CLOSE_BRACE: 305 case CPP_EOF: 306 fputc ('\n', file); 307 break; 308 309 default: 310 fputc (' ', file); 311 } 312 } 313 314 if (i == num && i < VEC_length (cp_token, buffer)) 315 { 316 fprintf (file, " ... "); 317 cp_lexer_print_token (file, VEC_index (cp_token, buffer, 318 VEC_length (cp_token, buffer) - 1)); 319 } 320 321 fprintf (file, "\n"); 322 } 323 324 325 /* Dump all tokens in BUFFER to stderr. */ 326 327 void 328 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer) 329 { 330 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL); 331 } 332 333 334 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the 335 description for T. */ 336 337 static void 338 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t) 339 { 340 if (t) 341 { 342 fprintf (file, "%s: ", desc); 343 print_node_brief (file, "", t, 0); 344 } 345 } 346 347 348 /* Dump parser context C to FILE. */ 349 350 static void 351 cp_debug_print_context (FILE *file, cp_parser_context *c) 352 { 353 const char *status_s[] = { "OK", "ERROR", "COMMITTED" }; 354 fprintf (file, "{ status = %s, scope = ", status_s[c->status]); 355 print_node_brief (file, "", c->object_type, 0); 356 fprintf (file, "}\n"); 357 } 358 359 360 /* Print the stack of parsing contexts to FILE starting with FIRST. */ 361 362 static void 363 cp_debug_print_context_stack (FILE *file, cp_parser_context *first) 364 { 365 unsigned i; 366 cp_parser_context *c; 367 368 fprintf (file, "Parsing context stack:\n"); 369 for (i = 0, c = first; c; c = c->next, i++) 370 { 371 fprintf (file, "\t#%u: ", i); 372 cp_debug_print_context (file, c); 373 } 374 } 375 376 377 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */ 378 379 static void 380 cp_debug_print_flag (FILE *file, const char *desc, bool flag) 381 { 382 if (flag) 383 fprintf (file, "%s: true\n", desc); 384 } 385 386 387 /* Print an unparsed function entry UF to FILE. */ 388 389 static void 390 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf) 391 { 392 unsigned i; 393 cp_default_arg_entry *default_arg_fn; 394 tree fn; 395 396 fprintf (file, "\tFunctions with default args:\n"); 397 for (i = 0; 398 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i, 399 default_arg_fn); 400 i++) 401 { 402 fprintf (file, "\t\tClass type: "); 403 print_node_brief (file, "", default_arg_fn->class_type, 0); 404 fprintf (file, "\t\tDeclaration: "); 405 print_node_brief (file, "", default_arg_fn->decl, 0); 406 fprintf (file, "\n"); 407 } 408 409 fprintf (file, "\n\tFunctions with definitions that require " 410 "post-processing\n\t\t"); 411 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++) 412 { 413 print_node_brief (file, "", fn, 0); 414 fprintf (file, " "); 415 } 416 fprintf (file, "\n"); 417 418 fprintf (file, "\n\tNon-static data members with initializers that require " 419 "post-processing\n\t\t"); 420 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++) 421 { 422 print_node_brief (file, "", fn, 0); 423 fprintf (file, " "); 424 } 425 fprintf (file, "\n"); 426 } 427 428 429 /* Print the stack of unparsed member functions S to FILE. */ 430 431 static void 432 cp_debug_print_unparsed_queues (FILE *file, 433 VEC(cp_unparsed_functions_entry, gc) *s) 434 { 435 unsigned i; 436 cp_unparsed_functions_entry *uf; 437 438 fprintf (file, "Unparsed functions\n"); 439 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++) 440 { 441 fprintf (file, "#%u:\n", i); 442 cp_debug_print_unparsed_function (file, uf); 443 } 444 } 445 446 447 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for 448 the given PARSER. If FILE is NULL, the output is printed on stderr. */ 449 450 static void 451 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size) 452 { 453 cp_token *next_token, *first_token, *start_token; 454 455 if (file == NULL) 456 file = stderr; 457 458 next_token = parser->lexer->next_token; 459 first_token = VEC_address (cp_token, parser->lexer->buffer); 460 start_token = (next_token > first_token + window_size / 2) 461 ? next_token - window_size / 2 462 : first_token; 463 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size, 464 next_token); 465 } 466 467 468 /* Dump debugging information for the given PARSER. If FILE is NULL, 469 the output is printed on stderr. */ 470 471 void 472 cp_debug_parser (FILE *file, cp_parser *parser) 473 { 474 const size_t window_size = 20; 475 cp_token *token; 476 expanded_location eloc; 477 478 if (file == NULL) 479 file = stderr; 480 481 fprintf (file, "Parser state\n\n"); 482 fprintf (file, "Number of tokens: %u\n", 483 VEC_length (cp_token, parser->lexer->buffer)); 484 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope); 485 cp_debug_print_tree_if_set (file, "Object scope", 486 parser->object_scope); 487 cp_debug_print_tree_if_set (file, "Qualifying scope", 488 parser->qualifying_scope); 489 cp_debug_print_context_stack (file, parser->context); 490 cp_debug_print_flag (file, "Allow GNU extensions", 491 parser->allow_gnu_extensions_p); 492 cp_debug_print_flag (file, "'>' token is greater-than", 493 parser->greater_than_is_operator_p); 494 cp_debug_print_flag (file, "Default args allowed in current " 495 "parameter list", parser->default_arg_ok_p); 496 cp_debug_print_flag (file, "Parsing integral constant-expression", 497 parser->integral_constant_expression_p); 498 cp_debug_print_flag (file, "Allow non-constant expression in current " 499 "constant-expression", 500 parser->allow_non_integral_constant_expression_p); 501 cp_debug_print_flag (file, "Seen non-constant expression", 502 parser->non_integral_constant_expression_p); 503 cp_debug_print_flag (file, "Local names and 'this' forbidden in " 504 "current context", 505 parser->local_variables_forbidden_p); 506 cp_debug_print_flag (file, "In unbraced linkage specification", 507 parser->in_unbraced_linkage_specification_p); 508 cp_debug_print_flag (file, "Parsing a declarator", 509 parser->in_declarator_p); 510 cp_debug_print_flag (file, "In template argument list", 511 parser->in_template_argument_list_p); 512 cp_debug_print_flag (file, "Parsing an iteration statement", 513 parser->in_statement & IN_ITERATION_STMT); 514 cp_debug_print_flag (file, "Parsing a switch statement", 515 parser->in_statement & IN_SWITCH_STMT); 516 cp_debug_print_flag (file, "Parsing a structured OpenMP block", 517 parser->in_statement & IN_OMP_BLOCK); 518 cp_debug_print_flag (file, "Parsing a an OpenMP loop", 519 parser->in_statement & IN_OMP_FOR); 520 cp_debug_print_flag (file, "Parsing an if statement", 521 parser->in_statement & IN_IF_STMT); 522 cp_debug_print_flag (file, "Parsing a type-id in an expression " 523 "context", parser->in_type_id_in_expr_p); 524 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"", 525 parser->implicit_extern_c); 526 cp_debug_print_flag (file, "String expressions should be translated " 527 "to execution character set", 528 parser->translate_strings_p); 529 cp_debug_print_flag (file, "Parsing function body outside of a " 530 "local class", parser->in_function_body); 531 cp_debug_print_flag (file, "Auto correct a colon to a scope operator", 532 parser->colon_corrects_to_scope_p); 533 if (parser->type_definition_forbidden_message) 534 fprintf (file, "Error message for forbidden type definitions: %s\n", 535 parser->type_definition_forbidden_message); 536 cp_debug_print_unparsed_queues (file, parser->unparsed_queues); 537 fprintf (file, "Number of class definitions in progress: %u\n", 538 parser->num_classes_being_defined); 539 fprintf (file, "Number of template parameter lists for the current " 540 "declaration: %u\n", parser->num_template_parameter_lists); 541 cp_debug_parser_tokens (file, parser, window_size); 542 token = parser->lexer->next_token; 543 fprintf (file, "Next token to parse:\n"); 544 fprintf (file, "\tToken: "); 545 cp_lexer_print_token (file, token); 546 eloc = expand_location (token->location); 547 fprintf (file, "\n\tFile: %s\n", eloc.file); 548 fprintf (file, "\tLine: %d\n", eloc.line); 549 fprintf (file, "\tColumn: %d\n", eloc.column); 550 } 551 552 553 /* Allocate memory for a new lexer object and return it. */ 554 555 static cp_lexer * 556 cp_lexer_alloc (void) 557 { 558 cp_lexer *lexer; 559 560 c_common_no_more_pch (); 561 562 /* Allocate the memory. */ 563 lexer = ggc_alloc_cleared_cp_lexer (); 564 565 /* Initially we are not debugging. */ 566 lexer->debugging_p = false; 567 568 lexer->saved_tokens = VEC_alloc (cp_token_position, heap, 569 CP_SAVED_TOKEN_STACK); 570 571 /* Create the buffer. */ 572 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE); 573 574 return lexer; 575 } 576 577 578 /* Create a new main C++ lexer, the lexer that gets tokens from the 579 preprocessor. */ 580 581 static cp_lexer * 582 cp_lexer_new_main (void) 583 { 584 cp_lexer *lexer; 585 cp_token token; 586 587 /* It's possible that parsing the first pragma will load a PCH file, 588 which is a GC collection point. So we have to do that before 589 allocating any memory. */ 590 cp_parser_initial_pragma (&token); 591 592 lexer = cp_lexer_alloc (); 593 594 /* Put the first token in the buffer. */ 595 VEC_quick_push (cp_token, lexer->buffer, &token); 596 597 /* Get the remaining tokens from the preprocessor. */ 598 while (token.type != CPP_EOF) 599 { 600 cp_lexer_get_preprocessor_token (lexer, &token); 601 VEC_safe_push (cp_token, gc, lexer->buffer, &token); 602 } 603 604 lexer->last_token = VEC_address (cp_token, lexer->buffer) 605 + VEC_length (cp_token, lexer->buffer) 606 - 1; 607 lexer->next_token = VEC_length (cp_token, lexer->buffer) 608 ? VEC_address (cp_token, lexer->buffer) 609 : &eof_token; 610 611 /* Subsequent preprocessor diagnostics should use compiler 612 diagnostic functions to get the compiler source location. */ 613 done_lexing = true; 614 615 gcc_assert (!lexer->next_token->purged_p); 616 return lexer; 617 } 618 619 /* Create a new lexer whose token stream is primed with the tokens in 620 CACHE. When these tokens are exhausted, no new tokens will be read. */ 621 622 static cp_lexer * 623 cp_lexer_new_from_tokens (cp_token_cache *cache) 624 { 625 cp_token *first = cache->first; 626 cp_token *last = cache->last; 627 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer (); 628 629 /* We do not own the buffer. */ 630 lexer->buffer = NULL; 631 lexer->next_token = first == last ? &eof_token : first; 632 lexer->last_token = last; 633 634 lexer->saved_tokens = VEC_alloc (cp_token_position, heap, 635 CP_SAVED_TOKEN_STACK); 636 637 /* Initially we are not debugging. */ 638 lexer->debugging_p = false; 639 640 gcc_assert (!lexer->next_token->purged_p); 641 return lexer; 642 } 643 644 /* Frees all resources associated with LEXER. */ 645 646 static void 647 cp_lexer_destroy (cp_lexer *lexer) 648 { 649 VEC_free (cp_token, gc, lexer->buffer); 650 VEC_free (cp_token_position, heap, lexer->saved_tokens); 651 ggc_free (lexer); 652 } 653 654 /* Returns nonzero if debugging information should be output. */ 655 656 static inline bool 657 cp_lexer_debugging_p (cp_lexer *lexer) 658 { 659 return lexer->debugging_p; 660 } 661 662 663 static inline cp_token_position 664 cp_lexer_token_position (cp_lexer *lexer, bool previous_p) 665 { 666 gcc_assert (!previous_p || lexer->next_token != &eof_token); 667 668 return lexer->next_token - previous_p; 669 } 670 671 static inline cp_token * 672 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos) 673 { 674 return pos; 675 } 676 677 static inline void 678 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos) 679 { 680 lexer->next_token = cp_lexer_token_at (lexer, pos); 681 } 682 683 static inline cp_token_position 684 cp_lexer_previous_token_position (cp_lexer *lexer) 685 { 686 if (lexer->next_token == &eof_token) 687 return lexer->last_token - 1; 688 else 689 return cp_lexer_token_position (lexer, true); 690 } 691 692 static inline cp_token * 693 cp_lexer_previous_token (cp_lexer *lexer) 694 { 695 cp_token_position tp = cp_lexer_previous_token_position (lexer); 696 697 return cp_lexer_token_at (lexer, tp); 698 } 699 700 /* nonzero if we are presently saving tokens. */ 701 702 static inline int 703 cp_lexer_saving_tokens (const cp_lexer* lexer) 704 { 705 return VEC_length (cp_token_position, lexer->saved_tokens) != 0; 706 } 707 708 /* Store the next token from the preprocessor in *TOKEN. Return true 709 if we reach EOF. If LEXER is NULL, assume we are handling an 710 initial #pragma pch_preprocess, and thus want the lexer to return 711 processed strings. */ 712 713 static void 714 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token) 715 { 716 static int is_extern_c = 0; 717 718 /* Get a new token from the preprocessor. */ 719 token->type 720 = c_lex_with_flags (&token->u.value, &token->location, &token->flags, 721 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN); 722 token->keyword = RID_MAX; 723 token->pragma_kind = PRAGMA_NONE; 724 token->purged_p = false; 725 726 /* On some systems, some header files are surrounded by an 727 implicit extern "C" block. Set a flag in the token if it 728 comes from such a header. */ 729 is_extern_c += pending_lang_change; 730 pending_lang_change = 0; 731 token->implicit_extern_c = is_extern_c > 0; 732 733 /* Check to see if this token is a keyword. */ 734 if (token->type == CPP_NAME) 735 { 736 if (C_IS_RESERVED_WORD (token->u.value)) 737 { 738 /* Mark this token as a keyword. */ 739 token->type = CPP_KEYWORD; 740 /* Record which keyword. */ 741 token->keyword = C_RID_CODE (token->u.value); 742 } 743 else 744 { 745 if (warn_cxx0x_compat 746 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X 747 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X) 748 { 749 /* Warn about the C++0x keyword (but still treat it as 750 an identifier). */ 751 warning (OPT_Wc__0x_compat, 752 "identifier %qE is a keyword in C++11", 753 token->u.value); 754 755 /* Clear out the C_RID_CODE so we don't warn about this 756 particular identifier-turned-keyword again. */ 757 C_SET_RID_CODE (token->u.value, RID_MAX); 758 } 759 760 token->ambiguous_p = false; 761 token->keyword = RID_MAX; 762 } 763 } 764 else if (token->type == CPP_AT_NAME) 765 { 766 /* This only happens in Objective-C++; it must be a keyword. */ 767 token->type = CPP_KEYWORD; 768 switch (C_RID_CODE (token->u.value)) 769 { 770 /* Replace 'class' with '@class', 'private' with '@private', 771 etc. This prevents confusion with the C++ keyword 772 'class', and makes the tokens consistent with other 773 Objective-C 'AT' keywords. For example '@class' is 774 reported as RID_AT_CLASS which is consistent with 775 '@synchronized', which is reported as 776 RID_AT_SYNCHRONIZED. 777 */ 778 case RID_CLASS: token->keyword = RID_AT_CLASS; break; 779 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break; 780 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break; 781 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break; 782 case RID_THROW: token->keyword = RID_AT_THROW; break; 783 case RID_TRY: token->keyword = RID_AT_TRY; break; 784 case RID_CATCH: token->keyword = RID_AT_CATCH; break; 785 default: token->keyword = C_RID_CODE (token->u.value); 786 } 787 } 788 else if (token->type == CPP_PRAGMA) 789 { 790 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */ 791 token->pragma_kind = ((enum pragma_kind) 792 TREE_INT_CST_LOW (token->u.value)); 793 token->u.value = NULL_TREE; 794 } 795 } 796 797 /* Update the globals input_location and the input file stack from TOKEN. */ 798 static inline void 799 cp_lexer_set_source_position_from_token (cp_token *token) 800 { 801 if (token->type != CPP_EOF) 802 { 803 input_location = token->location; 804 } 805 } 806 807 /* Return a pointer to the next token in the token stream, but do not 808 consume it. */ 809 810 static inline cp_token * 811 cp_lexer_peek_token (cp_lexer *lexer) 812 { 813 if (cp_lexer_debugging_p (lexer)) 814 { 815 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream); 816 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token); 817 putc ('\n', cp_lexer_debug_stream); 818 } 819 return lexer->next_token; 820 } 821 822 /* Return true if the next token has the indicated TYPE. */ 823 824 static inline bool 825 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type) 826 { 827 return cp_lexer_peek_token (lexer)->type == type; 828 } 829 830 /* Return true if the next token does not have the indicated TYPE. */ 831 832 static inline bool 833 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type) 834 { 835 return !cp_lexer_next_token_is (lexer, type); 836 } 837 838 /* Return true if the next token is the indicated KEYWORD. */ 839 840 static inline bool 841 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword) 842 { 843 return cp_lexer_peek_token (lexer)->keyword == keyword; 844 } 845 846 /* Return true if the next token is not the indicated KEYWORD. */ 847 848 static inline bool 849 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword) 850 { 851 return cp_lexer_peek_token (lexer)->keyword != keyword; 852 } 853 854 /* Return true if the next token is a keyword for a decl-specifier. */ 855 856 static bool 857 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer) 858 { 859 cp_token *token; 860 861 token = cp_lexer_peek_token (lexer); 862 switch (token->keyword) 863 { 864 /* auto specifier: storage-class-specifier in C++, 865 simple-type-specifier in C++0x. */ 866 case RID_AUTO: 867 /* Storage classes. */ 868 case RID_REGISTER: 869 case RID_STATIC: 870 case RID_EXTERN: 871 case RID_MUTABLE: 872 case RID_THREAD: 873 /* Elaborated type specifiers. */ 874 case RID_ENUM: 875 case RID_CLASS: 876 case RID_STRUCT: 877 case RID_UNION: 878 case RID_TYPENAME: 879 /* Simple type specifiers. */ 880 case RID_CHAR: 881 case RID_CHAR16: 882 case RID_CHAR32: 883 case RID_WCHAR: 884 case RID_BOOL: 885 case RID_SHORT: 886 case RID_INT: 887 case RID_LONG: 888 case RID_INT128: 889 case RID_SIGNED: 890 case RID_UNSIGNED: 891 case RID_FLOAT: 892 case RID_DOUBLE: 893 case RID_VOID: 894 /* GNU extensions. */ 895 case RID_ATTRIBUTE: 896 case RID_TYPEOF: 897 /* C++0x extensions. */ 898 case RID_DECLTYPE: 899 case RID_UNDERLYING_TYPE: 900 return true; 901 902 default: 903 return false; 904 } 905 } 906 907 /* Returns TRUE iff the token T begins a decltype type. */ 908 909 static bool 910 token_is_decltype (cp_token *t) 911 { 912 return (t->keyword == RID_DECLTYPE 913 || t->type == CPP_DECLTYPE); 914 } 915 916 /* Returns TRUE iff the next token begins a decltype type. */ 917 918 static bool 919 cp_lexer_next_token_is_decltype (cp_lexer *lexer) 920 { 921 cp_token *t = cp_lexer_peek_token (lexer); 922 return token_is_decltype (t); 923 } 924 925 /* Return a pointer to the Nth token in the token stream. If N is 1, 926 then this is precisely equivalent to cp_lexer_peek_token (except 927 that it is not inline). One would like to disallow that case, but 928 there is one case (cp_parser_nth_token_starts_template_id) where 929 the caller passes a variable for N and it might be 1. */ 930 931 static cp_token * 932 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n) 933 { 934 cp_token *token; 935 936 /* N is 1-based, not zero-based. */ 937 gcc_assert (n > 0); 938 939 if (cp_lexer_debugging_p (lexer)) 940 fprintf (cp_lexer_debug_stream, 941 "cp_lexer: peeking ahead %ld at token: ", (long)n); 942 943 --n; 944 token = lexer->next_token; 945 gcc_assert (!n || token != &eof_token); 946 while (n != 0) 947 { 948 ++token; 949 if (token == lexer->last_token) 950 { 951 token = &eof_token; 952 break; 953 } 954 955 if (!token->purged_p) 956 --n; 957 } 958 959 if (cp_lexer_debugging_p (lexer)) 960 { 961 cp_lexer_print_token (cp_lexer_debug_stream, token); 962 putc ('\n', cp_lexer_debug_stream); 963 } 964 965 return token; 966 } 967 968 /* Return the next token, and advance the lexer's next_token pointer 969 to point to the next non-purged token. */ 970 971 static cp_token * 972 cp_lexer_consume_token (cp_lexer* lexer) 973 { 974 cp_token *token = lexer->next_token; 975 976 gcc_assert (token != &eof_token); 977 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL); 978 979 do 980 { 981 lexer->next_token++; 982 if (lexer->next_token == lexer->last_token) 983 { 984 lexer->next_token = &eof_token; 985 break; 986 } 987 988 } 989 while (lexer->next_token->purged_p); 990 991 cp_lexer_set_source_position_from_token (token); 992 993 /* Provide debugging output. */ 994 if (cp_lexer_debugging_p (lexer)) 995 { 996 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream); 997 cp_lexer_print_token (cp_lexer_debug_stream, token); 998 putc ('\n', cp_lexer_debug_stream); 999 } 1000 1001 return token; 1002 } 1003 1004 /* Permanently remove the next token from the token stream, and 1005 advance the next_token pointer to refer to the next non-purged 1006 token. */ 1007 1008 static void 1009 cp_lexer_purge_token (cp_lexer *lexer) 1010 { 1011 cp_token *tok = lexer->next_token; 1012 1013 gcc_assert (tok != &eof_token); 1014 tok->purged_p = true; 1015 tok->location = UNKNOWN_LOCATION; 1016 tok->u.value = NULL_TREE; 1017 tok->keyword = RID_MAX; 1018 1019 do 1020 { 1021 tok++; 1022 if (tok == lexer->last_token) 1023 { 1024 tok = &eof_token; 1025 break; 1026 } 1027 } 1028 while (tok->purged_p); 1029 lexer->next_token = tok; 1030 } 1031 1032 /* Permanently remove all tokens after TOK, up to, but not 1033 including, the token that will be returned next by 1034 cp_lexer_peek_token. */ 1035 1036 static void 1037 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok) 1038 { 1039 cp_token *peek = lexer->next_token; 1040 1041 if (peek == &eof_token) 1042 peek = lexer->last_token; 1043 1044 gcc_assert (tok < peek); 1045 1046 for ( tok += 1; tok != peek; tok += 1) 1047 { 1048 tok->purged_p = true; 1049 tok->location = UNKNOWN_LOCATION; 1050 tok->u.value = NULL_TREE; 1051 tok->keyword = RID_MAX; 1052 } 1053 } 1054 1055 /* Begin saving tokens. All tokens consumed after this point will be 1056 preserved. */ 1057 1058 static void 1059 cp_lexer_save_tokens (cp_lexer* lexer) 1060 { 1061 /* Provide debugging output. */ 1062 if (cp_lexer_debugging_p (lexer)) 1063 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n"); 1064 1065 VEC_safe_push (cp_token_position, heap, 1066 lexer->saved_tokens, lexer->next_token); 1067 } 1068 1069 /* Commit to the portion of the token stream most recently saved. */ 1070 1071 static void 1072 cp_lexer_commit_tokens (cp_lexer* lexer) 1073 { 1074 /* Provide debugging output. */ 1075 if (cp_lexer_debugging_p (lexer)) 1076 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n"); 1077 1078 VEC_pop (cp_token_position, lexer->saved_tokens); 1079 } 1080 1081 /* Return all tokens saved since the last call to cp_lexer_save_tokens 1082 to the token stream. Stop saving tokens. */ 1083 1084 static void 1085 cp_lexer_rollback_tokens (cp_lexer* lexer) 1086 { 1087 /* Provide debugging output. */ 1088 if (cp_lexer_debugging_p (lexer)) 1089 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n"); 1090 1091 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens); 1092 } 1093 1094 /* Print a representation of the TOKEN on the STREAM. */ 1095 1096 static void 1097 cp_lexer_print_token (FILE * stream, cp_token *token) 1098 { 1099 /* We don't use cpp_type2name here because the parser defines 1100 a few tokens of its own. */ 1101 static const char *const token_names[] = { 1102 /* cpplib-defined token types */ 1103 #define OP(e, s) #e, 1104 #define TK(e, s) #e, 1105 TTYPE_TABLE 1106 #undef OP 1107 #undef TK 1108 /* C++ parser token types - see "Manifest constants", above. */ 1109 "KEYWORD", 1110 "TEMPLATE_ID", 1111 "NESTED_NAME_SPECIFIER", 1112 }; 1113 1114 /* For some tokens, print the associated data. */ 1115 switch (token->type) 1116 { 1117 case CPP_KEYWORD: 1118 /* Some keywords have a value that is not an IDENTIFIER_NODE. 1119 For example, `struct' is mapped to an INTEGER_CST. */ 1120 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE) 1121 break; 1122 /* else fall through */ 1123 case CPP_NAME: 1124 fputs (IDENTIFIER_POINTER (token->u.value), stream); 1125 break; 1126 1127 case CPP_STRING: 1128 case CPP_STRING16: 1129 case CPP_STRING32: 1130 case CPP_WSTRING: 1131 case CPP_UTF8STRING: 1132 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value)); 1133 break; 1134 1135 case CPP_NUMBER: 1136 print_generic_expr (stream, token->u.value, 0); 1137 break; 1138 1139 default: 1140 /* If we have a name for the token, print it out. Otherwise, we 1141 simply give the numeric code. */ 1142 if (token->type < ARRAY_SIZE(token_names)) 1143 fputs (token_names[token->type], stream); 1144 else 1145 fprintf (stream, "[%d]", token->type); 1146 break; 1147 } 1148 } 1149 1150 /* Start emitting debugging information. */ 1151 1152 static void 1153 cp_lexer_start_debugging (cp_lexer* lexer) 1154 { 1155 lexer->debugging_p = true; 1156 cp_lexer_debug_stream = stderr; 1157 } 1158 1159 /* Stop emitting debugging information. */ 1160 1161 static void 1162 cp_lexer_stop_debugging (cp_lexer* lexer) 1163 { 1164 lexer->debugging_p = false; 1165 cp_lexer_debug_stream = NULL; 1166 } 1167 1168 /* Create a new cp_token_cache, representing a range of tokens. */ 1169 1170 static cp_token_cache * 1171 cp_token_cache_new (cp_token *first, cp_token *last) 1172 { 1173 cp_token_cache *cache = ggc_alloc_cp_token_cache (); 1174 cache->first = first; 1175 cache->last = last; 1176 return cache; 1177 } 1178 1179 1180 /* Decl-specifiers. */ 1181 1182 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */ 1183 1184 static void 1185 clear_decl_specs (cp_decl_specifier_seq *decl_specs) 1186 { 1187 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq)); 1188 } 1189 1190 /* Declarators. */ 1191 1192 /* Nothing other than the parser should be creating declarators; 1193 declarators are a semi-syntactic representation of C++ entities. 1194 Other parts of the front end that need to create entities (like 1195 VAR_DECLs or FUNCTION_DECLs) should do that directly. */ 1196 1197 static cp_declarator *make_call_declarator 1198 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree); 1199 static cp_declarator *make_array_declarator 1200 (cp_declarator *, tree); 1201 static cp_declarator *make_pointer_declarator 1202 (cp_cv_quals, cp_declarator *); 1203 static cp_declarator *make_reference_declarator 1204 (cp_cv_quals, cp_declarator *, bool); 1205 static cp_parameter_declarator *make_parameter_declarator 1206 (cp_decl_specifier_seq *, cp_declarator *, tree); 1207 static cp_declarator *make_ptrmem_declarator 1208 (cp_cv_quals, tree, cp_declarator *); 1209 1210 /* An erroneous declarator. */ 1211 static cp_declarator *cp_error_declarator; 1212 1213 /* The obstack on which declarators and related data structures are 1214 allocated. */ 1215 static struct obstack declarator_obstack; 1216 1217 /* Alloc BYTES from the declarator memory pool. */ 1218 1219 static inline void * 1220 alloc_declarator (size_t bytes) 1221 { 1222 return obstack_alloc (&declarator_obstack, bytes); 1223 } 1224 1225 /* Allocate a declarator of the indicated KIND. Clear fields that are 1226 common to all declarators. */ 1227 1228 static cp_declarator * 1229 make_declarator (cp_declarator_kind kind) 1230 { 1231 cp_declarator *declarator; 1232 1233 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator)); 1234 declarator->kind = kind; 1235 declarator->attributes = NULL_TREE; 1236 declarator->declarator = NULL; 1237 declarator->parameter_pack_p = false; 1238 declarator->id_loc = UNKNOWN_LOCATION; 1239 1240 return declarator; 1241 } 1242 1243 /* Make a declarator for a generalized identifier. If 1244 QUALIFYING_SCOPE is non-NULL, the identifier is 1245 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just 1246 UNQUALIFIED_NAME. SFK indicates the kind of special function this 1247 is, if any. */ 1248 1249 static cp_declarator * 1250 make_id_declarator (tree qualifying_scope, tree unqualified_name, 1251 special_function_kind sfk) 1252 { 1253 cp_declarator *declarator; 1254 1255 /* It is valid to write: 1256 1257 class C { void f(); }; 1258 typedef C D; 1259 void D::f(); 1260 1261 The standard is not clear about whether `typedef const C D' is 1262 legal; as of 2002-09-15 the committee is considering that 1263 question. EDG 3.0 allows that syntax. Therefore, we do as 1264 well. */ 1265 if (qualifying_scope && TYPE_P (qualifying_scope)) 1266 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope); 1267 1268 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE 1269 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR 1270 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR); 1271 1272 declarator = make_declarator (cdk_id); 1273 declarator->u.id.qualifying_scope = qualifying_scope; 1274 declarator->u.id.unqualified_name = unqualified_name; 1275 declarator->u.id.sfk = sfk; 1276 1277 return declarator; 1278 } 1279 1280 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list 1281 of modifiers such as const or volatile to apply to the pointer 1282 type, represented as identifiers. */ 1283 1284 cp_declarator * 1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target) 1286 { 1287 cp_declarator *declarator; 1288 1289 declarator = make_declarator (cdk_pointer); 1290 declarator->declarator = target; 1291 declarator->u.pointer.qualifiers = cv_qualifiers; 1292 declarator->u.pointer.class_type = NULL_TREE; 1293 if (target) 1294 { 1295 declarator->id_loc = target->id_loc; 1296 declarator->parameter_pack_p = target->parameter_pack_p; 1297 target->parameter_pack_p = false; 1298 } 1299 else 1300 declarator->parameter_pack_p = false; 1301 1302 return declarator; 1303 } 1304 1305 /* Like make_pointer_declarator -- but for references. */ 1306 1307 cp_declarator * 1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target, 1309 bool rvalue_ref) 1310 { 1311 cp_declarator *declarator; 1312 1313 declarator = make_declarator (cdk_reference); 1314 declarator->declarator = target; 1315 declarator->u.reference.qualifiers = cv_qualifiers; 1316 declarator->u.reference.rvalue_ref = rvalue_ref; 1317 if (target) 1318 { 1319 declarator->id_loc = target->id_loc; 1320 declarator->parameter_pack_p = target->parameter_pack_p; 1321 target->parameter_pack_p = false; 1322 } 1323 else 1324 declarator->parameter_pack_p = false; 1325 1326 return declarator; 1327 } 1328 1329 /* Like make_pointer_declarator -- but for a pointer to a non-static 1330 member of CLASS_TYPE. */ 1331 1332 cp_declarator * 1333 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type, 1334 cp_declarator *pointee) 1335 { 1336 cp_declarator *declarator; 1337 1338 declarator = make_declarator (cdk_ptrmem); 1339 declarator->declarator = pointee; 1340 declarator->u.pointer.qualifiers = cv_qualifiers; 1341 declarator->u.pointer.class_type = class_type; 1342 1343 if (pointee) 1344 { 1345 declarator->parameter_pack_p = pointee->parameter_pack_p; 1346 pointee->parameter_pack_p = false; 1347 } 1348 else 1349 declarator->parameter_pack_p = false; 1350 1351 return declarator; 1352 } 1353 1354 /* Make a declarator for the function given by TARGET, with the 1355 indicated PARMS. The CV_QUALIFIERS aply to the function, as in 1356 "const"-qualified member function. The EXCEPTION_SPECIFICATION 1357 indicates what exceptions can be thrown. */ 1358 1359 cp_declarator * 1360 make_call_declarator (cp_declarator *target, 1361 tree parms, 1362 cp_cv_quals cv_qualifiers, 1363 cp_virt_specifiers virt_specifiers, 1364 tree exception_specification, 1365 tree late_return_type) 1366 { 1367 cp_declarator *declarator; 1368 1369 declarator = make_declarator (cdk_function); 1370 declarator->declarator = target; 1371 declarator->u.function.parameters = parms; 1372 declarator->u.function.qualifiers = cv_qualifiers; 1373 declarator->u.function.virt_specifiers = virt_specifiers; 1374 declarator->u.function.exception_specification = exception_specification; 1375 declarator->u.function.late_return_type = late_return_type; 1376 if (target) 1377 { 1378 declarator->id_loc = target->id_loc; 1379 declarator->parameter_pack_p = target->parameter_pack_p; 1380 target->parameter_pack_p = false; 1381 } 1382 else 1383 declarator->parameter_pack_p = false; 1384 1385 return declarator; 1386 } 1387 1388 /* Make a declarator for an array of BOUNDS elements, each of which is 1389 defined by ELEMENT. */ 1390 1391 cp_declarator * 1392 make_array_declarator (cp_declarator *element, tree bounds) 1393 { 1394 cp_declarator *declarator; 1395 1396 declarator = make_declarator (cdk_array); 1397 declarator->declarator = element; 1398 declarator->u.array.bounds = bounds; 1399 if (element) 1400 { 1401 declarator->id_loc = element->id_loc; 1402 declarator->parameter_pack_p = element->parameter_pack_p; 1403 element->parameter_pack_p = false; 1404 } 1405 else 1406 declarator->parameter_pack_p = false; 1407 1408 return declarator; 1409 } 1410 1411 /* Determine whether the declarator we've seen so far can be a 1412 parameter pack, when followed by an ellipsis. */ 1413 static bool 1414 declarator_can_be_parameter_pack (cp_declarator *declarator) 1415 { 1416 /* Search for a declarator name, or any other declarator that goes 1417 after the point where the ellipsis could appear in a parameter 1418 pack. If we find any of these, then this declarator can not be 1419 made into a parameter pack. */ 1420 bool found = false; 1421 while (declarator && !found) 1422 { 1423 switch ((int)declarator->kind) 1424 { 1425 case cdk_id: 1426 case cdk_array: 1427 found = true; 1428 break; 1429 1430 case cdk_error: 1431 return true; 1432 1433 default: 1434 declarator = declarator->declarator; 1435 break; 1436 } 1437 } 1438 1439 return !found; 1440 } 1441 1442 cp_parameter_declarator *no_parameters; 1443 1444 /* Create a parameter declarator with the indicated DECL_SPECIFIERS, 1445 DECLARATOR and DEFAULT_ARGUMENT. */ 1446 1447 cp_parameter_declarator * 1448 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers, 1449 cp_declarator *declarator, 1450 tree default_argument) 1451 { 1452 cp_parameter_declarator *parameter; 1453 1454 parameter = ((cp_parameter_declarator *) 1455 alloc_declarator (sizeof (cp_parameter_declarator))); 1456 parameter->next = NULL; 1457 if (decl_specifiers) 1458 parameter->decl_specifiers = *decl_specifiers; 1459 else 1460 clear_decl_specs (¶meter->decl_specifiers); 1461 parameter->declarator = declarator; 1462 parameter->default_argument = default_argument; 1463 parameter->ellipsis_p = false; 1464 1465 return parameter; 1466 } 1467 1468 /* Returns true iff DECLARATOR is a declaration for a function. */ 1469 1470 static bool 1471 function_declarator_p (const cp_declarator *declarator) 1472 { 1473 while (declarator) 1474 { 1475 if (declarator->kind == cdk_function 1476 && declarator->declarator->kind == cdk_id) 1477 return true; 1478 if (declarator->kind == cdk_id 1479 || declarator->kind == cdk_error) 1480 return false; 1481 declarator = declarator->declarator; 1482 } 1483 return false; 1484 } 1485 1486 /* The parser. */ 1487 1488 /* Overview 1489 -------- 1490 1491 A cp_parser parses the token stream as specified by the C++ 1492 grammar. Its job is purely parsing, not semantic analysis. For 1493 example, the parser breaks the token stream into declarators, 1494 expressions, statements, and other similar syntactic constructs. 1495 It does not check that the types of the expressions on either side 1496 of an assignment-statement are compatible, or that a function is 1497 not declared with a parameter of type `void'. 1498 1499 The parser invokes routines elsewhere in the compiler to perform 1500 semantic analysis and to build up the abstract syntax tree for the 1501 code processed. 1502 1503 The parser (and the template instantiation code, which is, in a 1504 way, a close relative of parsing) are the only parts of the 1505 compiler that should be calling push_scope and pop_scope, or 1506 related functions. The parser (and template instantiation code) 1507 keeps track of what scope is presently active; everything else 1508 should simply honor that. (The code that generates static 1509 initializers may also need to set the scope, in order to check 1510 access control correctly when emitting the initializers.) 1511 1512 Methodology 1513 ----------- 1514 1515 The parser is of the standard recursive-descent variety. Upcoming 1516 tokens in the token stream are examined in order to determine which 1517 production to use when parsing a non-terminal. Some C++ constructs 1518 require arbitrary look ahead to disambiguate. For example, it is 1519 impossible, in the general case, to tell whether a statement is an 1520 expression or declaration without scanning the entire statement. 1521 Therefore, the parser is capable of "parsing tentatively." When the 1522 parser is not sure what construct comes next, it enters this mode. 1523 Then, while we attempt to parse the construct, the parser queues up 1524 error messages, rather than issuing them immediately, and saves the 1525 tokens it consumes. If the construct is parsed successfully, the 1526 parser "commits", i.e., it issues any queued error messages and 1527 the tokens that were being preserved are permanently discarded. 1528 If, however, the construct is not parsed successfully, the parser 1529 rolls back its state completely so that it can resume parsing using 1530 a different alternative. 1531 1532 Future Improvements 1533 ------------------- 1534 1535 The performance of the parser could probably be improved substantially. 1536 We could often eliminate the need to parse tentatively by looking ahead 1537 a little bit. In some places, this approach might not entirely eliminate 1538 the need to parse tentatively, but it might still speed up the average 1539 case. */ 1540 1541 /* Flags that are passed to some parsing functions. These values can 1542 be bitwise-ored together. */ 1543 1544 enum 1545 { 1546 /* No flags. */ 1547 CP_PARSER_FLAGS_NONE = 0x0, 1548 /* The construct is optional. If it is not present, then no error 1549 should be issued. */ 1550 CP_PARSER_FLAGS_OPTIONAL = 0x1, 1551 /* When parsing a type-specifier, treat user-defined type-names 1552 as non-type identifiers. */ 1553 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2, 1554 /* When parsing a type-specifier, do not try to parse a class-specifier 1555 or enum-specifier. */ 1556 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4, 1557 /* When parsing a decl-specifier-seq, only allow type-specifier or 1558 constexpr. */ 1559 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8 1560 }; 1561 1562 /* This type is used for parameters and variables which hold 1563 combinations of the above flags. */ 1564 typedef int cp_parser_flags; 1565 1566 /* The different kinds of declarators we want to parse. */ 1567 1568 typedef enum cp_parser_declarator_kind 1569 { 1570 /* We want an abstract declarator. */ 1571 CP_PARSER_DECLARATOR_ABSTRACT, 1572 /* We want a named declarator. */ 1573 CP_PARSER_DECLARATOR_NAMED, 1574 /* We don't mind, but the name must be an unqualified-id. */ 1575 CP_PARSER_DECLARATOR_EITHER 1576 } cp_parser_declarator_kind; 1577 1578 /* The precedence values used to parse binary expressions. The minimum value 1579 of PREC must be 1, because zero is reserved to quickly discriminate 1580 binary operators from other tokens. */ 1581 1582 enum cp_parser_prec 1583 { 1584 PREC_NOT_OPERATOR, 1585 PREC_LOGICAL_OR_EXPRESSION, 1586 PREC_LOGICAL_AND_EXPRESSION, 1587 PREC_INCLUSIVE_OR_EXPRESSION, 1588 PREC_EXCLUSIVE_OR_EXPRESSION, 1589 PREC_AND_EXPRESSION, 1590 PREC_EQUALITY_EXPRESSION, 1591 PREC_RELATIONAL_EXPRESSION, 1592 PREC_SHIFT_EXPRESSION, 1593 PREC_ADDITIVE_EXPRESSION, 1594 PREC_MULTIPLICATIVE_EXPRESSION, 1595 PREC_PM_EXPRESSION, 1596 NUM_PREC_VALUES = PREC_PM_EXPRESSION 1597 }; 1598 1599 /* A mapping from a token type to a corresponding tree node type, with a 1600 precedence value. */ 1601 1602 typedef struct cp_parser_binary_operations_map_node 1603 { 1604 /* The token type. */ 1605 enum cpp_ttype token_type; 1606 /* The corresponding tree code. */ 1607 enum tree_code tree_type; 1608 /* The precedence of this operator. */ 1609 enum cp_parser_prec prec; 1610 } cp_parser_binary_operations_map_node; 1611 1612 typedef struct cp_parser_expression_stack_entry 1613 { 1614 /* Left hand side of the binary operation we are currently 1615 parsing. */ 1616 tree lhs; 1617 /* Original tree code for left hand side, if it was a binary 1618 expression itself (used for -Wparentheses). */ 1619 enum tree_code lhs_type; 1620 /* Tree code for the binary operation we are parsing. */ 1621 enum tree_code tree_type; 1622 /* Precedence of the binary operation we are parsing. */ 1623 enum cp_parser_prec prec; 1624 } cp_parser_expression_stack_entry; 1625 1626 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES 1627 entries because precedence levels on the stack are monotonically 1628 increasing. */ 1629 typedef struct cp_parser_expression_stack_entry 1630 cp_parser_expression_stack[NUM_PREC_VALUES]; 1631 1632 /* Prototypes. */ 1633 1634 /* Constructors and destructors. */ 1635 1636 static cp_parser_context *cp_parser_context_new 1637 (cp_parser_context *); 1638 1639 /* Class variables. */ 1640 1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list; 1642 1643 /* The operator-precedence table used by cp_parser_binary_expression. 1644 Transformed into an associative array (binops_by_token) by 1645 cp_parser_new. */ 1646 1647 static const cp_parser_binary_operations_map_node binops[] = { 1648 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION }, 1649 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION }, 1650 1651 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, 1652 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, 1653 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION }, 1654 1655 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION }, 1656 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION }, 1657 1658 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION }, 1659 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION }, 1660 1661 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION }, 1662 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION }, 1663 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION }, 1664 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION }, 1665 1666 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION }, 1667 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION }, 1668 1669 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION }, 1670 1671 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION }, 1672 1673 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION }, 1674 1675 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION }, 1676 1677 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION } 1678 }; 1679 1680 /* The same as binops, but initialized by cp_parser_new so that 1681 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression 1682 for speed. */ 1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES]; 1684 1685 /* Constructors and destructors. */ 1686 1687 /* Construct a new context. The context below this one on the stack 1688 is given by NEXT. */ 1689 1690 static cp_parser_context * 1691 cp_parser_context_new (cp_parser_context* next) 1692 { 1693 cp_parser_context *context; 1694 1695 /* Allocate the storage. */ 1696 if (cp_parser_context_free_list != NULL) 1697 { 1698 /* Pull the first entry from the free list. */ 1699 context = cp_parser_context_free_list; 1700 cp_parser_context_free_list = context->next; 1701 memset (context, 0, sizeof (*context)); 1702 } 1703 else 1704 context = ggc_alloc_cleared_cp_parser_context (); 1705 1706 /* No errors have occurred yet in this context. */ 1707 context->status = CP_PARSER_STATUS_KIND_NO_ERROR; 1708 /* If this is not the bottommost context, copy information that we 1709 need from the previous context. */ 1710 if (next) 1711 { 1712 /* If, in the NEXT context, we are parsing an `x->' or `x.' 1713 expression, then we are parsing one in this context, too. */ 1714 context->object_type = next->object_type; 1715 /* Thread the stack. */ 1716 context->next = next; 1717 } 1718 1719 return context; 1720 } 1721 1722 /* Managing the unparsed function queues. */ 1723 1724 #define unparsed_funs_with_default_args \ 1725 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args 1726 #define unparsed_funs_with_definitions \ 1727 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions 1728 #define unparsed_nsdmis \ 1729 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis 1730 1731 static void 1732 push_unparsed_function_queues (cp_parser *parser) 1733 { 1734 VEC_safe_push (cp_unparsed_functions_entry, gc, 1735 parser->unparsed_queues, NULL); 1736 unparsed_funs_with_default_args = NULL; 1737 unparsed_funs_with_definitions = make_tree_vector (); 1738 unparsed_nsdmis = NULL; 1739 } 1740 1741 static void 1742 pop_unparsed_function_queues (cp_parser *parser) 1743 { 1744 release_tree_vector (unparsed_funs_with_definitions); 1745 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues); 1746 } 1747 1748 /* Prototypes. */ 1749 1750 /* Constructors and destructors. */ 1751 1752 static cp_parser *cp_parser_new 1753 (void); 1754 1755 /* Routines to parse various constructs. 1756 1757 Those that return `tree' will return the error_mark_node (rather 1758 than NULL_TREE) if a parse error occurs, unless otherwise noted. 1759 Sometimes, they will return an ordinary node if error-recovery was 1760 attempted, even though a parse error occurred. So, to check 1761 whether or not a parse error occurred, you should always use 1762 cp_parser_error_occurred. If the construct is optional (indicated 1763 either by an `_opt' in the name of the function that does the 1764 parsing or via a FLAGS parameter), then NULL_TREE is returned if 1765 the construct is not present. */ 1766 1767 /* Lexical conventions [gram.lex] */ 1768 1769 static tree cp_parser_identifier 1770 (cp_parser *); 1771 static tree cp_parser_string_literal 1772 (cp_parser *, bool, bool); 1773 static tree cp_parser_userdef_char_literal 1774 (cp_parser *); 1775 static tree cp_parser_userdef_string_literal 1776 (cp_token *); 1777 static tree cp_parser_userdef_numeric_literal 1778 (cp_parser *); 1779 1780 /* Basic concepts [gram.basic] */ 1781 1782 static bool cp_parser_translation_unit 1783 (cp_parser *); 1784 1785 /* Expressions [gram.expr] */ 1786 1787 static tree cp_parser_primary_expression 1788 (cp_parser *, bool, bool, bool, cp_id_kind *); 1789 static tree cp_parser_id_expression 1790 (cp_parser *, bool, bool, bool *, bool, bool); 1791 static tree cp_parser_unqualified_id 1792 (cp_parser *, bool, bool, bool, bool); 1793 static tree cp_parser_nested_name_specifier_opt 1794 (cp_parser *, bool, bool, bool, bool); 1795 static tree cp_parser_nested_name_specifier 1796 (cp_parser *, bool, bool, bool, bool); 1797 static tree cp_parser_qualifying_entity 1798 (cp_parser *, bool, bool, bool, bool, bool); 1799 static tree cp_parser_postfix_expression 1800 (cp_parser *, bool, bool, bool, cp_id_kind *); 1801 static tree cp_parser_postfix_open_square_expression 1802 (cp_parser *, tree, bool); 1803 static tree cp_parser_postfix_dot_deref_expression 1804 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t); 1805 static VEC(tree,gc) *cp_parser_parenthesized_expression_list 1806 (cp_parser *, int, bool, bool, bool *); 1807 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */ 1808 enum { non_attr = 0, normal_attr = 1, id_attr = 2 }; 1809 static void cp_parser_pseudo_destructor_name 1810 (cp_parser *, tree *, tree *); 1811 static tree cp_parser_unary_expression 1812 (cp_parser *, bool, bool, cp_id_kind *); 1813 static enum tree_code cp_parser_unary_operator 1814 (cp_token *); 1815 static tree cp_parser_new_expression 1816 (cp_parser *); 1817 static VEC(tree,gc) *cp_parser_new_placement 1818 (cp_parser *); 1819 static tree cp_parser_new_type_id 1820 (cp_parser *, tree *); 1821 static cp_declarator *cp_parser_new_declarator_opt 1822 (cp_parser *); 1823 static cp_declarator *cp_parser_direct_new_declarator 1824 (cp_parser *); 1825 static VEC(tree,gc) *cp_parser_new_initializer 1826 (cp_parser *); 1827 static tree cp_parser_delete_expression 1828 (cp_parser *); 1829 static tree cp_parser_cast_expression 1830 (cp_parser *, bool, bool, cp_id_kind *); 1831 static tree cp_parser_binary_expression 1832 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *); 1833 static tree cp_parser_question_colon_clause 1834 (cp_parser *, tree); 1835 static tree cp_parser_assignment_expression 1836 (cp_parser *, bool, cp_id_kind *); 1837 static enum tree_code cp_parser_assignment_operator_opt 1838 (cp_parser *); 1839 static tree cp_parser_expression 1840 (cp_parser *, bool, cp_id_kind *); 1841 static tree cp_parser_constant_expression 1842 (cp_parser *, bool, bool *); 1843 static tree cp_parser_builtin_offsetof 1844 (cp_parser *); 1845 static tree cp_parser_lambda_expression 1846 (cp_parser *); 1847 static void cp_parser_lambda_introducer 1848 (cp_parser *, tree); 1849 static bool cp_parser_lambda_declarator_opt 1850 (cp_parser *, tree); 1851 static void cp_parser_lambda_body 1852 (cp_parser *, tree); 1853 1854 /* Statements [gram.stmt.stmt] */ 1855 1856 static void cp_parser_statement 1857 (cp_parser *, tree, bool, bool *); 1858 static void cp_parser_label_for_labeled_statement 1859 (cp_parser *); 1860 static tree cp_parser_expression_statement 1861 (cp_parser *, tree); 1862 static tree cp_parser_compound_statement 1863 (cp_parser *, tree, bool, bool); 1864 static void cp_parser_statement_seq_opt 1865 (cp_parser *, tree); 1866 static tree cp_parser_selection_statement 1867 (cp_parser *, bool *); 1868 static tree cp_parser_condition 1869 (cp_parser *); 1870 static tree cp_parser_iteration_statement 1871 (cp_parser *); 1872 static bool cp_parser_for_init_statement 1873 (cp_parser *, tree *decl); 1874 static tree cp_parser_for 1875 (cp_parser *); 1876 static tree cp_parser_c_for 1877 (cp_parser *, tree, tree); 1878 static tree cp_parser_range_for 1879 (cp_parser *, tree, tree, tree); 1880 static void do_range_for_auto_deduction 1881 (tree, tree); 1882 static tree cp_parser_perform_range_for_lookup 1883 (tree, tree *, tree *); 1884 static tree cp_parser_range_for_member_function 1885 (tree, tree); 1886 static tree cp_parser_jump_statement 1887 (cp_parser *); 1888 static void cp_parser_declaration_statement 1889 (cp_parser *); 1890 1891 static tree cp_parser_implicitly_scoped_statement 1892 (cp_parser *, bool *); 1893 static void cp_parser_already_scoped_statement 1894 (cp_parser *); 1895 1896 /* Declarations [gram.dcl.dcl] */ 1897 1898 static void cp_parser_declaration_seq_opt 1899 (cp_parser *); 1900 static void cp_parser_declaration 1901 (cp_parser *); 1902 static void cp_parser_block_declaration 1903 (cp_parser *, bool); 1904 static void cp_parser_simple_declaration 1905 (cp_parser *, bool, tree *); 1906 static void cp_parser_decl_specifier_seq 1907 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *); 1908 static tree cp_parser_storage_class_specifier_opt 1909 (cp_parser *); 1910 static tree cp_parser_function_specifier_opt 1911 (cp_parser *, cp_decl_specifier_seq *); 1912 static tree cp_parser_type_specifier 1913 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool, 1914 int *, bool *); 1915 static tree cp_parser_simple_type_specifier 1916 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags); 1917 static tree cp_parser_type_name 1918 (cp_parser *); 1919 static tree cp_parser_nonclass_name 1920 (cp_parser* parser); 1921 static tree cp_parser_elaborated_type_specifier 1922 (cp_parser *, bool, bool); 1923 static tree cp_parser_enum_specifier 1924 (cp_parser *); 1925 static void cp_parser_enumerator_list 1926 (cp_parser *, tree); 1927 static void cp_parser_enumerator_definition 1928 (cp_parser *, tree); 1929 static tree cp_parser_namespace_name 1930 (cp_parser *); 1931 static void cp_parser_namespace_definition 1932 (cp_parser *); 1933 static void cp_parser_namespace_body 1934 (cp_parser *); 1935 static tree cp_parser_qualified_namespace_specifier 1936 (cp_parser *); 1937 static void cp_parser_namespace_alias_definition 1938 (cp_parser *); 1939 static bool cp_parser_using_declaration 1940 (cp_parser *, bool); 1941 static void cp_parser_using_directive 1942 (cp_parser *); 1943 static tree cp_parser_alias_declaration 1944 (cp_parser *); 1945 static void cp_parser_asm_definition 1946 (cp_parser *); 1947 static void cp_parser_linkage_specification 1948 (cp_parser *); 1949 static void cp_parser_static_assert 1950 (cp_parser *, bool); 1951 static tree cp_parser_decltype 1952 (cp_parser *); 1953 1954 /* Declarators [gram.dcl.decl] */ 1955 1956 static tree cp_parser_init_declarator 1957 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *); 1958 static cp_declarator *cp_parser_declarator 1959 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool); 1960 static cp_declarator *cp_parser_direct_declarator 1961 (cp_parser *, cp_parser_declarator_kind, int *, bool); 1962 static enum tree_code cp_parser_ptr_operator 1963 (cp_parser *, tree *, cp_cv_quals *); 1964 static cp_cv_quals cp_parser_cv_qualifier_seq_opt 1965 (cp_parser *); 1966 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt 1967 (cp_parser *); 1968 static tree cp_parser_late_return_type_opt 1969 (cp_parser *, cp_cv_quals); 1970 static tree cp_parser_declarator_id 1971 (cp_parser *, bool); 1972 static tree cp_parser_type_id 1973 (cp_parser *); 1974 static tree cp_parser_template_type_arg 1975 (cp_parser *); 1976 static tree cp_parser_trailing_type_id (cp_parser *); 1977 static tree cp_parser_type_id_1 1978 (cp_parser *, bool, bool); 1979 static void cp_parser_type_specifier_seq 1980 (cp_parser *, bool, bool, cp_decl_specifier_seq *); 1981 static tree cp_parser_parameter_declaration_clause 1982 (cp_parser *); 1983 static tree cp_parser_parameter_declaration_list 1984 (cp_parser *, bool *); 1985 static cp_parameter_declarator *cp_parser_parameter_declaration 1986 (cp_parser *, bool, bool *); 1987 static tree cp_parser_default_argument 1988 (cp_parser *, bool); 1989 static void cp_parser_function_body 1990 (cp_parser *); 1991 static tree cp_parser_initializer 1992 (cp_parser *, bool *, bool *); 1993 static tree cp_parser_initializer_clause 1994 (cp_parser *, bool *); 1995 static tree cp_parser_braced_list 1996 (cp_parser*, bool*); 1997 static VEC(constructor_elt,gc) *cp_parser_initializer_list 1998 (cp_parser *, bool *); 1999 2000 static bool cp_parser_ctor_initializer_opt_and_function_body 2001 (cp_parser *); 2002 2003 /* Classes [gram.class] */ 2004 2005 static tree cp_parser_class_name 2006 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool); 2007 static tree cp_parser_class_specifier 2008 (cp_parser *); 2009 static tree cp_parser_class_head 2010 (cp_parser *, bool *); 2011 static enum tag_types cp_parser_class_key 2012 (cp_parser *); 2013 static void cp_parser_member_specification_opt 2014 (cp_parser *); 2015 static void cp_parser_member_declaration 2016 (cp_parser *); 2017 static tree cp_parser_pure_specifier 2018 (cp_parser *); 2019 static tree cp_parser_constant_initializer 2020 (cp_parser *); 2021 2022 /* Derived classes [gram.class.derived] */ 2023 2024 static tree cp_parser_base_clause 2025 (cp_parser *); 2026 static tree cp_parser_base_specifier 2027 (cp_parser *); 2028 2029 /* Special member functions [gram.special] */ 2030 2031 static tree cp_parser_conversion_function_id 2032 (cp_parser *); 2033 static tree cp_parser_conversion_type_id 2034 (cp_parser *); 2035 static cp_declarator *cp_parser_conversion_declarator_opt 2036 (cp_parser *); 2037 static bool cp_parser_ctor_initializer_opt 2038 (cp_parser *); 2039 static void cp_parser_mem_initializer_list 2040 (cp_parser *); 2041 static tree cp_parser_mem_initializer 2042 (cp_parser *); 2043 static tree cp_parser_mem_initializer_id 2044 (cp_parser *); 2045 2046 /* Overloading [gram.over] */ 2047 2048 static tree cp_parser_operator_function_id 2049 (cp_parser *); 2050 static tree cp_parser_operator 2051 (cp_parser *); 2052 2053 /* Templates [gram.temp] */ 2054 2055 static void cp_parser_template_declaration 2056 (cp_parser *, bool); 2057 static tree cp_parser_template_parameter_list 2058 (cp_parser *); 2059 static tree cp_parser_template_parameter 2060 (cp_parser *, bool *, bool *); 2061 static tree cp_parser_type_parameter 2062 (cp_parser *, bool *); 2063 static tree cp_parser_template_id 2064 (cp_parser *, bool, bool, bool); 2065 static tree cp_parser_template_name 2066 (cp_parser *, bool, bool, bool, bool *); 2067 static tree cp_parser_template_argument_list 2068 (cp_parser *); 2069 static tree cp_parser_template_argument 2070 (cp_parser *); 2071 static void cp_parser_explicit_instantiation 2072 (cp_parser *); 2073 static void cp_parser_explicit_specialization 2074 (cp_parser *); 2075 2076 /* Exception handling [gram.exception] */ 2077 2078 static tree cp_parser_try_block 2079 (cp_parser *); 2080 static bool cp_parser_function_try_block 2081 (cp_parser *); 2082 static void cp_parser_handler_seq 2083 (cp_parser *); 2084 static void cp_parser_handler 2085 (cp_parser *); 2086 static tree cp_parser_exception_declaration 2087 (cp_parser *); 2088 static tree cp_parser_throw_expression 2089 (cp_parser *); 2090 static tree cp_parser_exception_specification_opt 2091 (cp_parser *); 2092 static tree cp_parser_type_id_list 2093 (cp_parser *); 2094 2095 /* GNU Extensions */ 2096 2097 static tree cp_parser_asm_specification_opt 2098 (cp_parser *); 2099 static tree cp_parser_asm_operand_list 2100 (cp_parser *); 2101 static tree cp_parser_asm_clobber_list 2102 (cp_parser *); 2103 static tree cp_parser_asm_label_list 2104 (cp_parser *); 2105 static tree cp_parser_attributes_opt 2106 (cp_parser *); 2107 static tree cp_parser_attribute_list 2108 (cp_parser *); 2109 static bool cp_parser_extension_opt 2110 (cp_parser *, int *); 2111 static void cp_parser_label_declaration 2112 (cp_parser *); 2113 2114 /* Transactional Memory Extensions */ 2115 2116 static tree cp_parser_transaction 2117 (cp_parser *, enum rid); 2118 static tree cp_parser_transaction_expression 2119 (cp_parser *, enum rid); 2120 static bool cp_parser_function_transaction 2121 (cp_parser *, enum rid); 2122 static tree cp_parser_transaction_cancel 2123 (cp_parser *); 2124 2125 enum pragma_context { pragma_external, pragma_stmt, pragma_compound }; 2126 static bool cp_parser_pragma 2127 (cp_parser *, enum pragma_context); 2128 2129 /* Objective-C++ Productions */ 2130 2131 static tree cp_parser_objc_message_receiver 2132 (cp_parser *); 2133 static tree cp_parser_objc_message_args 2134 (cp_parser *); 2135 static tree cp_parser_objc_message_expression 2136 (cp_parser *); 2137 static tree cp_parser_objc_encode_expression 2138 (cp_parser *); 2139 static tree cp_parser_objc_defs_expression 2140 (cp_parser *); 2141 static tree cp_parser_objc_protocol_expression 2142 (cp_parser *); 2143 static tree cp_parser_objc_selector_expression 2144 (cp_parser *); 2145 static tree cp_parser_objc_expression 2146 (cp_parser *); 2147 static bool cp_parser_objc_selector_p 2148 (enum cpp_ttype); 2149 static tree cp_parser_objc_selector 2150 (cp_parser *); 2151 static tree cp_parser_objc_protocol_refs_opt 2152 (cp_parser *); 2153 static void cp_parser_objc_declaration 2154 (cp_parser *, tree); 2155 static tree cp_parser_objc_statement 2156 (cp_parser *); 2157 static bool cp_parser_objc_valid_prefix_attributes 2158 (cp_parser *, tree *); 2159 static void cp_parser_objc_at_property_declaration 2160 (cp_parser *) ; 2161 static void cp_parser_objc_at_synthesize_declaration 2162 (cp_parser *) ; 2163 static void cp_parser_objc_at_dynamic_declaration 2164 (cp_parser *) ; 2165 static tree cp_parser_objc_struct_declaration 2166 (cp_parser *) ; 2167 2168 /* Utility Routines */ 2169 2170 static tree cp_parser_lookup_name 2171 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t); 2172 static tree cp_parser_lookup_name_simple 2173 (cp_parser *, tree, location_t); 2174 static tree cp_parser_maybe_treat_template_as_class 2175 (tree, bool); 2176 static bool cp_parser_check_declarator_template_parameters 2177 (cp_parser *, cp_declarator *, location_t); 2178 static bool cp_parser_check_template_parameters 2179 (cp_parser *, unsigned, location_t, cp_declarator *); 2180 static tree cp_parser_simple_cast_expression 2181 (cp_parser *); 2182 static tree cp_parser_global_scope_opt 2183 (cp_parser *, bool); 2184 static bool cp_parser_constructor_declarator_p 2185 (cp_parser *, bool); 2186 static tree cp_parser_function_definition_from_specifiers_and_declarator 2187 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *); 2188 static tree cp_parser_function_definition_after_declarator 2189 (cp_parser *, bool); 2190 static void cp_parser_template_declaration_after_export 2191 (cp_parser *, bool); 2192 static void cp_parser_perform_template_parameter_access_checks 2193 (VEC (deferred_access_check,gc)*); 2194 static tree cp_parser_single_declaration 2195 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *); 2196 static tree cp_parser_functional_cast 2197 (cp_parser *, tree); 2198 static tree cp_parser_save_member_function_body 2199 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree); 2200 static tree cp_parser_save_nsdmi 2201 (cp_parser *); 2202 static tree cp_parser_enclosed_template_argument_list 2203 (cp_parser *); 2204 static void cp_parser_save_default_args 2205 (cp_parser *, tree); 2206 static void cp_parser_late_parsing_for_member 2207 (cp_parser *, tree); 2208 static tree cp_parser_late_parse_one_default_arg 2209 (cp_parser *, tree, tree, tree); 2210 static void cp_parser_late_parsing_nsdmi 2211 (cp_parser *, tree); 2212 static void cp_parser_late_parsing_default_args 2213 (cp_parser *, tree); 2214 static tree cp_parser_sizeof_operand 2215 (cp_parser *, enum rid); 2216 static tree cp_parser_trait_expr 2217 (cp_parser *, enum rid); 2218 static bool cp_parser_declares_only_class_p 2219 (cp_parser *); 2220 static void cp_parser_set_storage_class 2221 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t); 2222 static void cp_parser_set_decl_spec_type 2223 (cp_decl_specifier_seq *, tree, location_t, bool); 2224 static bool cp_parser_friend_p 2225 (const cp_decl_specifier_seq *); 2226 static void cp_parser_required_error 2227 (cp_parser *, required_token, bool); 2228 static cp_token *cp_parser_require 2229 (cp_parser *, enum cpp_ttype, required_token); 2230 static cp_token *cp_parser_require_keyword 2231 (cp_parser *, enum rid, required_token); 2232 static bool cp_parser_token_starts_function_definition_p 2233 (cp_token *); 2234 static bool cp_parser_next_token_starts_class_definition_p 2235 (cp_parser *); 2236 static bool cp_parser_next_token_ends_template_argument_p 2237 (cp_parser *); 2238 static bool cp_parser_nth_token_starts_template_argument_list_p 2239 (cp_parser *, size_t); 2240 static enum tag_types cp_parser_token_is_class_key 2241 (cp_token *); 2242 static void cp_parser_check_class_key 2243 (enum tag_types, tree type); 2244 static void cp_parser_check_access_in_redeclaration 2245 (tree type, location_t location); 2246 static bool cp_parser_optional_template_keyword 2247 (cp_parser *); 2248 static void cp_parser_pre_parsed_nested_name_specifier 2249 (cp_parser *); 2250 static bool cp_parser_cache_group 2251 (cp_parser *, enum cpp_ttype, unsigned); 2252 static tree cp_parser_cache_defarg 2253 (cp_parser *parser, bool nsdmi); 2254 static void cp_parser_parse_tentatively 2255 (cp_parser *); 2256 static void cp_parser_commit_to_tentative_parse 2257 (cp_parser *); 2258 static void cp_parser_abort_tentative_parse 2259 (cp_parser *); 2260 static bool cp_parser_parse_definitely 2261 (cp_parser *); 2262 static inline bool cp_parser_parsing_tentatively 2263 (cp_parser *); 2264 static bool cp_parser_uncommitted_to_tentative_parse_p 2265 (cp_parser *); 2266 static void cp_parser_error 2267 (cp_parser *, const char *); 2268 static void cp_parser_name_lookup_error 2269 (cp_parser *, tree, tree, name_lookup_error, location_t); 2270 static bool cp_parser_simulate_error 2271 (cp_parser *); 2272 static bool cp_parser_check_type_definition 2273 (cp_parser *); 2274 static void cp_parser_check_for_definition_in_return_type 2275 (cp_declarator *, tree, location_t type_location); 2276 static void cp_parser_check_for_invalid_template_id 2277 (cp_parser *, tree, location_t location); 2278 static bool cp_parser_non_integral_constant_expression 2279 (cp_parser *, non_integral_constant); 2280 static void cp_parser_diagnose_invalid_type_name 2281 (cp_parser *, tree, tree, location_t); 2282 static bool cp_parser_parse_and_diagnose_invalid_type_name 2283 (cp_parser *); 2284 static int cp_parser_skip_to_closing_parenthesis 2285 (cp_parser *, bool, bool, bool); 2286 static void cp_parser_skip_to_end_of_statement 2287 (cp_parser *); 2288 static void cp_parser_consume_semicolon_at_end_of_statement 2289 (cp_parser *); 2290 static void cp_parser_skip_to_end_of_block_or_statement 2291 (cp_parser *); 2292 static bool cp_parser_skip_to_closing_brace 2293 (cp_parser *); 2294 static void cp_parser_skip_to_end_of_template_parameter_list 2295 (cp_parser *); 2296 static void cp_parser_skip_to_pragma_eol 2297 (cp_parser*, cp_token *); 2298 static bool cp_parser_error_occurred 2299 (cp_parser *); 2300 static bool cp_parser_allow_gnu_extensions_p 2301 (cp_parser *); 2302 static bool cp_parser_is_pure_string_literal 2303 (cp_token *); 2304 static bool cp_parser_is_string_literal 2305 (cp_token *); 2306 static bool cp_parser_is_keyword 2307 (cp_token *, enum rid); 2308 static tree cp_parser_make_typename_type 2309 (cp_parser *, tree, tree, location_t location); 2310 static cp_declarator * cp_parser_make_indirect_declarator 2311 (enum tree_code, tree, cp_cv_quals, cp_declarator *); 2312 2313 /* Returns nonzero if we are parsing tentatively. */ 2314 2315 static inline bool 2316 cp_parser_parsing_tentatively (cp_parser* parser) 2317 { 2318 return parser->context->next != NULL; 2319 } 2320 2321 /* Returns nonzero if TOKEN is a string literal. */ 2322 2323 static bool 2324 cp_parser_is_pure_string_literal (cp_token* token) 2325 { 2326 return (token->type == CPP_STRING || 2327 token->type == CPP_STRING16 || 2328 token->type == CPP_STRING32 || 2329 token->type == CPP_WSTRING || 2330 token->type == CPP_UTF8STRING); 2331 } 2332 2333 /* Returns nonzero if TOKEN is a string literal 2334 of a user-defined string literal. */ 2335 2336 static bool 2337 cp_parser_is_string_literal (cp_token* token) 2338 { 2339 return (cp_parser_is_pure_string_literal (token) || 2340 token->type == CPP_STRING_USERDEF || 2341 token->type == CPP_STRING16_USERDEF || 2342 token->type == CPP_STRING32_USERDEF || 2343 token->type == CPP_WSTRING_USERDEF || 2344 token->type == CPP_UTF8STRING_USERDEF); 2345 } 2346 2347 /* Returns nonzero if TOKEN is the indicated KEYWORD. */ 2348 2349 static bool 2350 cp_parser_is_keyword (cp_token* token, enum rid keyword) 2351 { 2352 return token->keyword == keyword; 2353 } 2354 2355 /* If not parsing tentatively, issue a diagnostic of the form 2356 FILE:LINE: MESSAGE before TOKEN 2357 where TOKEN is the next token in the input stream. MESSAGE 2358 (specified by the caller) is usually of the form "expected 2359 OTHER-TOKEN". */ 2360 2361 static void 2362 cp_parser_error (cp_parser* parser, const char* gmsgid) 2363 { 2364 if (!cp_parser_simulate_error (parser)) 2365 { 2366 cp_token *token = cp_lexer_peek_token (parser->lexer); 2367 /* This diagnostic makes more sense if it is tagged to the line 2368 of the token we just peeked at. */ 2369 cp_lexer_set_source_position_from_token (token); 2370 2371 if (token->type == CPP_PRAGMA) 2372 { 2373 error_at (token->location, 2374 "%<#pragma%> is not allowed here"); 2375 cp_parser_skip_to_pragma_eol (parser, token); 2376 return; 2377 } 2378 2379 c_parse_error (gmsgid, 2380 /* Because c_parser_error does not understand 2381 CPP_KEYWORD, keywords are treated like 2382 identifiers. */ 2383 (token->type == CPP_KEYWORD ? CPP_NAME : token->type), 2384 token->u.value, token->flags); 2385 } 2386 } 2387 2388 /* Issue an error about name-lookup failing. NAME is the 2389 IDENTIFIER_NODE DECL is the result of 2390 the lookup (as returned from cp_parser_lookup_name). DESIRED is 2391 the thing that we hoped to find. */ 2392 2393 static void 2394 cp_parser_name_lookup_error (cp_parser* parser, 2395 tree name, 2396 tree decl, 2397 name_lookup_error desired, 2398 location_t location) 2399 { 2400 /* If name lookup completely failed, tell the user that NAME was not 2401 declared. */ 2402 if (decl == error_mark_node) 2403 { 2404 if (parser->scope && parser->scope != global_namespace) 2405 error_at (location, "%<%E::%E%> has not been declared", 2406 parser->scope, name); 2407 else if (parser->scope == global_namespace) 2408 error_at (location, "%<::%E%> has not been declared", name); 2409 else if (parser->object_scope 2410 && !CLASS_TYPE_P (parser->object_scope)) 2411 error_at (location, "request for member %qE in non-class type %qT", 2412 name, parser->object_scope); 2413 else if (parser->object_scope) 2414 error_at (location, "%<%T::%E%> has not been declared", 2415 parser->object_scope, name); 2416 else 2417 error_at (location, "%qE has not been declared", name); 2418 } 2419 else if (parser->scope && parser->scope != global_namespace) 2420 { 2421 switch (desired) 2422 { 2423 case NLE_TYPE: 2424 error_at (location, "%<%E::%E%> is not a type", 2425 parser->scope, name); 2426 break; 2427 case NLE_CXX98: 2428 error_at (location, "%<%E::%E%> is not a class or namespace", 2429 parser->scope, name); 2430 break; 2431 case NLE_NOT_CXX98: 2432 error_at (location, 2433 "%<%E::%E%> is not a class, namespace, or enumeration", 2434 parser->scope, name); 2435 break; 2436 default: 2437 gcc_unreachable (); 2438 2439 } 2440 } 2441 else if (parser->scope == global_namespace) 2442 { 2443 switch (desired) 2444 { 2445 case NLE_TYPE: 2446 error_at (location, "%<::%E%> is not a type", name); 2447 break; 2448 case NLE_CXX98: 2449 error_at (location, "%<::%E%> is not a class or namespace", name); 2450 break; 2451 case NLE_NOT_CXX98: 2452 error_at (location, 2453 "%<::%E%> is not a class, namespace, or enumeration", 2454 name); 2455 break; 2456 default: 2457 gcc_unreachable (); 2458 } 2459 } 2460 else 2461 { 2462 switch (desired) 2463 { 2464 case NLE_TYPE: 2465 error_at (location, "%qE is not a type", name); 2466 break; 2467 case NLE_CXX98: 2468 error_at (location, "%qE is not a class or namespace", name); 2469 break; 2470 case NLE_NOT_CXX98: 2471 error_at (location, 2472 "%qE is not a class, namespace, or enumeration", name); 2473 break; 2474 default: 2475 gcc_unreachable (); 2476 } 2477 } 2478 } 2479 2480 /* If we are parsing tentatively, remember that an error has occurred 2481 during this tentative parse. Returns true if the error was 2482 simulated; false if a message should be issued by the caller. */ 2483 2484 static bool 2485 cp_parser_simulate_error (cp_parser* parser) 2486 { 2487 if (cp_parser_uncommitted_to_tentative_parse_p (parser)) 2488 { 2489 parser->context->status = CP_PARSER_STATUS_KIND_ERROR; 2490 return true; 2491 } 2492 return false; 2493 } 2494 2495 /* Check for repeated decl-specifiers. */ 2496 2497 static void 2498 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs, 2499 location_t location) 2500 { 2501 int ds; 2502 2503 for (ds = ds_first; ds != ds_last; ++ds) 2504 { 2505 unsigned count = decl_specs->specs[ds]; 2506 if (count < 2) 2507 continue; 2508 /* The "long" specifier is a special case because of "long long". */ 2509 if (ds == ds_long) 2510 { 2511 if (count > 2) 2512 error_at (location, "%<long long long%> is too long for GCC"); 2513 else 2514 pedwarn_cxx98 (location, OPT_Wlong_long, 2515 "ISO C++ 1998 does not support %<long long%>"); 2516 } 2517 else if (count > 1) 2518 { 2519 static const char *const decl_spec_names[] = { 2520 "signed", 2521 "unsigned", 2522 "short", 2523 "long", 2524 "const", 2525 "volatile", 2526 "restrict", 2527 "inline", 2528 "virtual", 2529 "explicit", 2530 "friend", 2531 "typedef", 2532 "using", 2533 "constexpr", 2534 "__complex", 2535 "__thread" 2536 }; 2537 error_at (location, "duplicate %qs", decl_spec_names[ds]); 2538 } 2539 } 2540 } 2541 2542 /* This function is called when a type is defined. If type 2543 definitions are forbidden at this point, an error message is 2544 issued. */ 2545 2546 static bool 2547 cp_parser_check_type_definition (cp_parser* parser) 2548 { 2549 /* If types are forbidden here, issue a message. */ 2550 if (parser->type_definition_forbidden_message) 2551 { 2552 /* Don't use `%s' to print the string, because quotations (`%<', `%>') 2553 in the message need to be interpreted. */ 2554 error (parser->type_definition_forbidden_message); 2555 return false; 2556 } 2557 return true; 2558 } 2559 2560 /* This function is called when the DECLARATOR is processed. The TYPE 2561 was a type defined in the decl-specifiers. If it is invalid to 2562 define a type in the decl-specifiers for DECLARATOR, an error is 2563 issued. TYPE_LOCATION is the location of TYPE and is used 2564 for error reporting. */ 2565 2566 static void 2567 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator, 2568 tree type, location_t type_location) 2569 { 2570 /* [dcl.fct] forbids type definitions in return types. 2571 Unfortunately, it's not easy to know whether or not we are 2572 processing a return type until after the fact. */ 2573 while (declarator 2574 && (declarator->kind == cdk_pointer 2575 || declarator->kind == cdk_reference 2576 || declarator->kind == cdk_ptrmem)) 2577 declarator = declarator->declarator; 2578 if (declarator 2579 && declarator->kind == cdk_function) 2580 { 2581 error_at (type_location, 2582 "new types may not be defined in a return type"); 2583 inform (type_location, 2584 "(perhaps a semicolon is missing after the definition of %qT)", 2585 type); 2586 } 2587 } 2588 2589 /* A type-specifier (TYPE) has been parsed which cannot be followed by 2590 "<" in any valid C++ program. If the next token is indeed "<", 2591 issue a message warning the user about what appears to be an 2592 invalid attempt to form a template-id. LOCATION is the location 2593 of the type-specifier (TYPE) */ 2594 2595 static void 2596 cp_parser_check_for_invalid_template_id (cp_parser* parser, 2597 tree type, location_t location) 2598 { 2599 cp_token_position start = 0; 2600 2601 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS)) 2602 { 2603 if (TYPE_P (type)) 2604 error_at (location, "%qT is not a template", type); 2605 else if (TREE_CODE (type) == IDENTIFIER_NODE) 2606 error_at (location, "%qE is not a template", type); 2607 else 2608 error_at (location, "invalid template-id"); 2609 /* Remember the location of the invalid "<". */ 2610 if (cp_parser_uncommitted_to_tentative_parse_p (parser)) 2611 start = cp_lexer_token_position (parser->lexer, true); 2612 /* Consume the "<". */ 2613 cp_lexer_consume_token (parser->lexer); 2614 /* Parse the template arguments. */ 2615 cp_parser_enclosed_template_argument_list (parser); 2616 /* Permanently remove the invalid template arguments so that 2617 this error message is not issued again. */ 2618 if (start) 2619 cp_lexer_purge_tokens_after (parser->lexer, start); 2620 } 2621 } 2622 2623 /* If parsing an integral constant-expression, issue an error message 2624 about the fact that THING appeared and return true. Otherwise, 2625 return false. In either case, set 2626 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */ 2627 2628 static bool 2629 cp_parser_non_integral_constant_expression (cp_parser *parser, 2630 non_integral_constant thing) 2631 { 2632 parser->non_integral_constant_expression_p = true; 2633 if (parser->integral_constant_expression_p) 2634 { 2635 if (!parser->allow_non_integral_constant_expression_p) 2636 { 2637 const char *msg = NULL; 2638 switch (thing) 2639 { 2640 case NIC_FLOAT: 2641 error ("floating-point literal " 2642 "cannot appear in a constant-expression"); 2643 return true; 2644 case NIC_CAST: 2645 error ("a cast to a type other than an integral or " 2646 "enumeration type cannot appear in a " 2647 "constant-expression"); 2648 return true; 2649 case NIC_TYPEID: 2650 error ("%<typeid%> operator " 2651 "cannot appear in a constant-expression"); 2652 return true; 2653 case NIC_NCC: 2654 error ("non-constant compound literals " 2655 "cannot appear in a constant-expression"); 2656 return true; 2657 case NIC_FUNC_CALL: 2658 error ("a function call " 2659 "cannot appear in a constant-expression"); 2660 return true; 2661 case NIC_INC: 2662 error ("an increment " 2663 "cannot appear in a constant-expression"); 2664 return true; 2665 case NIC_DEC: 2666 error ("an decrement " 2667 "cannot appear in a constant-expression"); 2668 return true; 2669 case NIC_ARRAY_REF: 2670 error ("an array reference " 2671 "cannot appear in a constant-expression"); 2672 return true; 2673 case NIC_ADDR_LABEL: 2674 error ("the address of a label " 2675 "cannot appear in a constant-expression"); 2676 return true; 2677 case NIC_OVERLOADED: 2678 error ("calls to overloaded operators " 2679 "cannot appear in a constant-expression"); 2680 return true; 2681 case NIC_ASSIGNMENT: 2682 error ("an assignment cannot appear in a constant-expression"); 2683 return true; 2684 case NIC_COMMA: 2685 error ("a comma operator " 2686 "cannot appear in a constant-expression"); 2687 return true; 2688 case NIC_CONSTRUCTOR: 2689 error ("a call to a constructor " 2690 "cannot appear in a constant-expression"); 2691 return true; 2692 case NIC_TRANSACTION: 2693 error ("a transaction expression " 2694 "cannot appear in a constant-expression"); 2695 return true; 2696 case NIC_THIS: 2697 msg = "this"; 2698 break; 2699 case NIC_FUNC_NAME: 2700 msg = "__FUNCTION__"; 2701 break; 2702 case NIC_PRETTY_FUNC: 2703 msg = "__PRETTY_FUNCTION__"; 2704 break; 2705 case NIC_C99_FUNC: 2706 msg = "__func__"; 2707 break; 2708 case NIC_VA_ARG: 2709 msg = "va_arg"; 2710 break; 2711 case NIC_ARROW: 2712 msg = "->"; 2713 break; 2714 case NIC_POINT: 2715 msg = "."; 2716 break; 2717 case NIC_STAR: 2718 msg = "*"; 2719 break; 2720 case NIC_ADDR: 2721 msg = "&"; 2722 break; 2723 case NIC_PREINCREMENT: 2724 msg = "++"; 2725 break; 2726 case NIC_PREDECREMENT: 2727 msg = "--"; 2728 break; 2729 case NIC_NEW: 2730 msg = "new"; 2731 break; 2732 case NIC_DEL: 2733 msg = "delete"; 2734 break; 2735 default: 2736 gcc_unreachable (); 2737 } 2738 if (msg) 2739 error ("%qs cannot appear in a constant-expression", msg); 2740 return true; 2741 } 2742 } 2743 return false; 2744 } 2745 2746 /* Emit a diagnostic for an invalid type name. SCOPE is the 2747 qualifying scope (or NULL, if none) for ID. This function commits 2748 to the current active tentative parse, if any. (Otherwise, the 2749 problematic construct might be encountered again later, resulting 2750 in duplicate error messages.) LOCATION is the location of ID. */ 2751 2752 static void 2753 cp_parser_diagnose_invalid_type_name (cp_parser *parser, 2754 tree scope, tree id, 2755 location_t location) 2756 { 2757 tree decl, old_scope; 2758 cp_parser_commit_to_tentative_parse (parser); 2759 /* Try to lookup the identifier. */ 2760 old_scope = parser->scope; 2761 parser->scope = scope; 2762 decl = cp_parser_lookup_name_simple (parser, id, location); 2763 parser->scope = old_scope; 2764 /* If the lookup found a template-name, it means that the user forgot 2765 to specify an argument list. Emit a useful error message. */ 2766 if (TREE_CODE (decl) == TEMPLATE_DECL) 2767 error_at (location, 2768 "invalid use of template-name %qE without an argument list", 2769 decl); 2770 else if (TREE_CODE (id) == BIT_NOT_EXPR) 2771 error_at (location, "invalid use of destructor %qD as a type", id); 2772 else if (TREE_CODE (decl) == TYPE_DECL) 2773 /* Something like 'unsigned A a;' */ 2774 error_at (location, "invalid combination of multiple type-specifiers"); 2775 else if (!parser->scope) 2776 { 2777 /* Issue an error message. */ 2778 error_at (location, "%qE does not name a type", id); 2779 /* If we're in a template class, it's possible that the user was 2780 referring to a type from a base class. For example: 2781 2782 template <typename T> struct A { typedef T X; }; 2783 template <typename T> struct B : public A<T> { X x; }; 2784 2785 The user should have said "typename A<T>::X". */ 2786 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR]) 2787 inform (location, "C++11 %<constexpr%> only available with " 2788 "-std=c++11 or -std=gnu++11"); 2789 else if (processing_template_decl && current_class_type 2790 && TYPE_BINFO (current_class_type)) 2791 { 2792 tree b; 2793 2794 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type)); 2795 b; 2796 b = TREE_CHAIN (b)) 2797 { 2798 tree base_type = BINFO_TYPE (b); 2799 if (CLASS_TYPE_P (base_type) 2800 && dependent_type_p (base_type)) 2801 { 2802 tree field; 2803 /* Go from a particular instantiation of the 2804 template (which will have an empty TYPE_FIELDs), 2805 to the main version. */ 2806 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type); 2807 for (field = TYPE_FIELDS (base_type); 2808 field; 2809 field = DECL_CHAIN (field)) 2810 if (TREE_CODE (field) == TYPE_DECL 2811 && DECL_NAME (field) == id) 2812 { 2813 inform (location, 2814 "(perhaps %<typename %T::%E%> was intended)", 2815 BINFO_TYPE (b), id); 2816 break; 2817 } 2818 if (field) 2819 break; 2820 } 2821 } 2822 } 2823 } 2824 /* Here we diagnose qualified-ids where the scope is actually correct, 2825 but the identifier does not resolve to a valid type name. */ 2826 else if (parser->scope != error_mark_node) 2827 { 2828 if (TREE_CODE (parser->scope) == NAMESPACE_DECL) 2829 error_at (location, "%qE in namespace %qE does not name a type", 2830 id, parser->scope); 2831 else if (CLASS_TYPE_P (parser->scope) 2832 && constructor_name_p (id, parser->scope)) 2833 { 2834 /* A<T>::A<T>() */ 2835 error_at (location, "%<%T::%E%> names the constructor, not" 2836 " the type", parser->scope, id); 2837 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS)) 2838 error_at (location, "and %qT has no template constructors", 2839 parser->scope); 2840 } 2841 else if (TYPE_P (parser->scope) 2842 && dependent_scope_p (parser->scope)) 2843 error_at (location, "need %<typename%> before %<%T::%E%> because " 2844 "%qT is a dependent scope", 2845 parser->scope, id, parser->scope); 2846 else if (TYPE_P (parser->scope)) 2847 error_at (location, "%qE in %q#T does not name a type", 2848 id, parser->scope); 2849 else 2850 gcc_unreachable (); 2851 } 2852 } 2853 2854 /* Check for a common situation where a type-name should be present, 2855 but is not, and issue a sensible error message. Returns true if an 2856 invalid type-name was detected. 2857 2858 The situation handled by this function are variable declarations of the 2859 form `ID a', where `ID' is an id-expression and `a' is a plain identifier. 2860 Usually, `ID' should name a type, but if we got here it means that it 2861 does not. We try to emit the best possible error message depending on 2862 how exactly the id-expression looks like. */ 2863 2864 static bool 2865 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser) 2866 { 2867 tree id; 2868 cp_token *token = cp_lexer_peek_token (parser->lexer); 2869 2870 /* Avoid duplicate error about ambiguous lookup. */ 2871 if (token->type == CPP_NESTED_NAME_SPECIFIER) 2872 { 2873 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2); 2874 if (next->type == CPP_NAME && next->ambiguous_p) 2875 goto out; 2876 } 2877 2878 cp_parser_parse_tentatively (parser); 2879 id = cp_parser_id_expression (parser, 2880 /*template_keyword_p=*/false, 2881 /*check_dependency_p=*/true, 2882 /*template_p=*/NULL, 2883 /*declarator_p=*/true, 2884 /*optional_p=*/false); 2885 /* If the next token is a (, this is a function with no explicit return 2886 type, i.e. constructor, destructor or conversion op. */ 2887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN) 2888 || TREE_CODE (id) == TYPE_DECL) 2889 { 2890 cp_parser_abort_tentative_parse (parser); 2891 return false; 2892 } 2893 if (!cp_parser_parse_definitely (parser)) 2894 return false; 2895 2896 /* Emit a diagnostic for the invalid type. */ 2897 cp_parser_diagnose_invalid_type_name (parser, parser->scope, 2898 id, token->location); 2899 out: 2900 /* If we aren't in the middle of a declarator (i.e. in a 2901 parameter-declaration-clause), skip to the end of the declaration; 2902 there's no point in trying to process it. */ 2903 if (!parser->in_declarator_p) 2904 cp_parser_skip_to_end_of_block_or_statement (parser); 2905 return true; 2906 } 2907 2908 /* Consume tokens up to, and including, the next non-nested closing `)'. 2909 Returns 1 iff we found a closing `)'. RECOVERING is true, if we 2910 are doing error recovery. Returns -1 if OR_COMMA is true and we 2911 found an unnested comma. */ 2912 2913 static int 2914 cp_parser_skip_to_closing_parenthesis (cp_parser *parser, 2915 bool recovering, 2916 bool or_comma, 2917 bool consume_paren) 2918 { 2919 unsigned paren_depth = 0; 2920 unsigned brace_depth = 0; 2921 unsigned square_depth = 0; 2922 2923 if (recovering && !or_comma 2924 && cp_parser_uncommitted_to_tentative_parse_p (parser)) 2925 return 0; 2926 2927 while (true) 2928 { 2929 cp_token * token = cp_lexer_peek_token (parser->lexer); 2930 2931 switch (token->type) 2932 { 2933 case CPP_EOF: 2934 case CPP_PRAGMA_EOL: 2935 /* If we've run out of tokens, then there is no closing `)'. */ 2936 return 0; 2937 2938 /* This is good for lambda expression capture-lists. */ 2939 case CPP_OPEN_SQUARE: 2940 ++square_depth; 2941 break; 2942 case CPP_CLOSE_SQUARE: 2943 if (!square_depth--) 2944 return 0; 2945 break; 2946 2947 case CPP_SEMICOLON: 2948 /* This matches the processing in skip_to_end_of_statement. */ 2949 if (!brace_depth) 2950 return 0; 2951 break; 2952 2953 case CPP_OPEN_BRACE: 2954 ++brace_depth; 2955 break; 2956 case CPP_CLOSE_BRACE: 2957 if (!brace_depth--) 2958 return 0; 2959 break; 2960 2961 case CPP_COMMA: 2962 if (recovering && or_comma && !brace_depth && !paren_depth 2963 && !square_depth) 2964 return -1; 2965 break; 2966 2967 case CPP_OPEN_PAREN: 2968 if (!brace_depth) 2969 ++paren_depth; 2970 break; 2971 2972 case CPP_CLOSE_PAREN: 2973 if (!brace_depth && !paren_depth--) 2974 { 2975 if (consume_paren) 2976 cp_lexer_consume_token (parser->lexer); 2977 return 1; 2978 } 2979 break; 2980 2981 default: 2982 break; 2983 } 2984 2985 /* Consume the token. */ 2986 cp_lexer_consume_token (parser->lexer); 2987 } 2988 } 2989 2990 /* Consume tokens until we reach the end of the current statement. 2991 Normally, that will be just before consuming a `;'. However, if a 2992 non-nested `}' comes first, then we stop before consuming that. */ 2993 2994 static void 2995 cp_parser_skip_to_end_of_statement (cp_parser* parser) 2996 { 2997 unsigned nesting_depth = 0; 2998 2999 while (true) 3000 { 3001 cp_token *token = cp_lexer_peek_token (parser->lexer); 3002 3003 switch (token->type) 3004 { 3005 case CPP_EOF: 3006 case CPP_PRAGMA_EOL: 3007 /* If we've run out of tokens, stop. */ 3008 return; 3009 3010 case CPP_SEMICOLON: 3011 /* If the next token is a `;', we have reached the end of the 3012 statement. */ 3013 if (!nesting_depth) 3014 return; 3015 break; 3016 3017 case CPP_CLOSE_BRACE: 3018 /* If this is a non-nested '}', stop before consuming it. 3019 That way, when confronted with something like: 3020 3021 { 3 + } 3022 3023 we stop before consuming the closing '}', even though we 3024 have not yet reached a `;'. */ 3025 if (nesting_depth == 0) 3026 return; 3027 3028 /* If it is the closing '}' for a block that we have 3029 scanned, stop -- but only after consuming the token. 3030 That way given: 3031 3032 void f g () { ... } 3033 typedef int I; 3034 3035 we will stop after the body of the erroneously declared 3036 function, but before consuming the following `typedef' 3037 declaration. */ 3038 if (--nesting_depth == 0) 3039 { 3040 cp_lexer_consume_token (parser->lexer); 3041 return; 3042 } 3043 3044 case CPP_OPEN_BRACE: 3045 ++nesting_depth; 3046 break; 3047 3048 default: 3049 break; 3050 } 3051 3052 /* Consume the token. */ 3053 cp_lexer_consume_token (parser->lexer); 3054 } 3055 } 3056 3057 /* This function is called at the end of a statement or declaration. 3058 If the next token is a semicolon, it is consumed; otherwise, error 3059 recovery is attempted. */ 3060 3061 static void 3062 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser) 3063 { 3064 /* Look for the trailing `;'. */ 3065 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)) 3066 { 3067 /* If there is additional (erroneous) input, skip to the end of 3068 the statement. */ 3069 cp_parser_skip_to_end_of_statement (parser); 3070 /* If the next token is now a `;', consume it. */ 3071 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 3072 cp_lexer_consume_token (parser->lexer); 3073 } 3074 } 3075 3076 /* Skip tokens until we have consumed an entire block, or until we 3077 have consumed a non-nested `;'. */ 3078 3079 static void 3080 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser) 3081 { 3082 int nesting_depth = 0; 3083 3084 while (nesting_depth >= 0) 3085 { 3086 cp_token *token = cp_lexer_peek_token (parser->lexer); 3087 3088 switch (token->type) 3089 { 3090 case CPP_EOF: 3091 case CPP_PRAGMA_EOL: 3092 /* If we've run out of tokens, stop. */ 3093 return; 3094 3095 case CPP_SEMICOLON: 3096 /* Stop if this is an unnested ';'. */ 3097 if (!nesting_depth) 3098 nesting_depth = -1; 3099 break; 3100 3101 case CPP_CLOSE_BRACE: 3102 /* Stop if this is an unnested '}', or closes the outermost 3103 nesting level. */ 3104 nesting_depth--; 3105 if (nesting_depth < 0) 3106 return; 3107 if (!nesting_depth) 3108 nesting_depth = -1; 3109 break; 3110 3111 case CPP_OPEN_BRACE: 3112 /* Nest. */ 3113 nesting_depth++; 3114 break; 3115 3116 default: 3117 break; 3118 } 3119 3120 /* Consume the token. */ 3121 cp_lexer_consume_token (parser->lexer); 3122 } 3123 } 3124 3125 /* Skip tokens until a non-nested closing curly brace is the next 3126 token, or there are no more tokens. Return true in the first case, 3127 false otherwise. */ 3128 3129 static bool 3130 cp_parser_skip_to_closing_brace (cp_parser *parser) 3131 { 3132 unsigned nesting_depth = 0; 3133 3134 while (true) 3135 { 3136 cp_token *token = cp_lexer_peek_token (parser->lexer); 3137 3138 switch (token->type) 3139 { 3140 case CPP_EOF: 3141 case CPP_PRAGMA_EOL: 3142 /* If we've run out of tokens, stop. */ 3143 return false; 3144 3145 case CPP_CLOSE_BRACE: 3146 /* If the next token is a non-nested `}', then we have reached 3147 the end of the current block. */ 3148 if (nesting_depth-- == 0) 3149 return true; 3150 break; 3151 3152 case CPP_OPEN_BRACE: 3153 /* If it the next token is a `{', then we are entering a new 3154 block. Consume the entire block. */ 3155 ++nesting_depth; 3156 break; 3157 3158 default: 3159 break; 3160 } 3161 3162 /* Consume the token. */ 3163 cp_lexer_consume_token (parser->lexer); 3164 } 3165 } 3166 3167 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK 3168 parameter is the PRAGMA token, allowing us to purge the entire pragma 3169 sequence. */ 3170 3171 static void 3172 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok) 3173 { 3174 cp_token *token; 3175 3176 parser->lexer->in_pragma = false; 3177 3178 do 3179 token = cp_lexer_consume_token (parser->lexer); 3180 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF); 3181 3182 /* Ensure that the pragma is not parsed again. */ 3183 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok); 3184 } 3185 3186 /* Require pragma end of line, resyncing with it as necessary. The 3187 arguments are as for cp_parser_skip_to_pragma_eol. */ 3188 3189 static void 3190 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok) 3191 { 3192 parser->lexer->in_pragma = false; 3193 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL)) 3194 cp_parser_skip_to_pragma_eol (parser, pragma_tok); 3195 } 3196 3197 /* This is a simple wrapper around make_typename_type. When the id is 3198 an unresolved identifier node, we can provide a superior diagnostic 3199 using cp_parser_diagnose_invalid_type_name. */ 3200 3201 static tree 3202 cp_parser_make_typename_type (cp_parser *parser, tree scope, 3203 tree id, location_t id_location) 3204 { 3205 tree result; 3206 if (TREE_CODE (id) == IDENTIFIER_NODE) 3207 { 3208 result = make_typename_type (scope, id, typename_type, 3209 /*complain=*/tf_none); 3210 if (result == error_mark_node) 3211 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location); 3212 return result; 3213 } 3214 return make_typename_type (scope, id, typename_type, tf_error); 3215 } 3216 3217 /* This is a wrapper around the 3218 make_{pointer,ptrmem,reference}_declarator functions that decides 3219 which one to call based on the CODE and CLASS_TYPE arguments. The 3220 CODE argument should be one of the values returned by 3221 cp_parser_ptr_operator. */ 3222 static cp_declarator * 3223 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type, 3224 cp_cv_quals cv_qualifiers, 3225 cp_declarator *target) 3226 { 3227 if (code == ERROR_MARK) 3228 return cp_error_declarator; 3229 3230 if (code == INDIRECT_REF) 3231 if (class_type == NULL_TREE) 3232 return make_pointer_declarator (cv_qualifiers, target); 3233 else 3234 return make_ptrmem_declarator (cv_qualifiers, class_type, target); 3235 else if (code == ADDR_EXPR && class_type == NULL_TREE) 3236 return make_reference_declarator (cv_qualifiers, target, false); 3237 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE) 3238 return make_reference_declarator (cv_qualifiers, target, true); 3239 gcc_unreachable (); 3240 } 3241 3242 /* Create a new C++ parser. */ 3243 3244 static cp_parser * 3245 cp_parser_new (void) 3246 { 3247 cp_parser *parser; 3248 cp_lexer *lexer; 3249 unsigned i; 3250 3251 /* cp_lexer_new_main is called before doing GC allocation because 3252 cp_lexer_new_main might load a PCH file. */ 3253 lexer = cp_lexer_new_main (); 3254 3255 /* Initialize the binops_by_token so that we can get the tree 3256 directly from the token. */ 3257 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++) 3258 binops_by_token[binops[i].token_type] = binops[i]; 3259 3260 parser = ggc_alloc_cleared_cp_parser (); 3261 parser->lexer = lexer; 3262 parser->context = cp_parser_context_new (NULL); 3263 3264 /* For now, we always accept GNU extensions. */ 3265 parser->allow_gnu_extensions_p = 1; 3266 3267 /* The `>' token is a greater-than operator, not the end of a 3268 template-id. */ 3269 parser->greater_than_is_operator_p = true; 3270 3271 parser->default_arg_ok_p = true; 3272 3273 /* We are not parsing a constant-expression. */ 3274 parser->integral_constant_expression_p = false; 3275 parser->allow_non_integral_constant_expression_p = false; 3276 parser->non_integral_constant_expression_p = false; 3277 3278 /* Local variable names are not forbidden. */ 3279 parser->local_variables_forbidden_p = false; 3280 3281 /* We are not processing an `extern "C"' declaration. */ 3282 parser->in_unbraced_linkage_specification_p = false; 3283 3284 /* We are not processing a declarator. */ 3285 parser->in_declarator_p = false; 3286 3287 /* We are not processing a template-argument-list. */ 3288 parser->in_template_argument_list_p = false; 3289 3290 /* We are not in an iteration statement. */ 3291 parser->in_statement = 0; 3292 3293 /* We are not in a switch statement. */ 3294 parser->in_switch_statement_p = false; 3295 3296 /* We are not parsing a type-id inside an expression. */ 3297 parser->in_type_id_in_expr_p = false; 3298 3299 /* Declarations aren't implicitly extern "C". */ 3300 parser->implicit_extern_c = false; 3301 3302 /* String literals should be translated to the execution character set. */ 3303 parser->translate_strings_p = true; 3304 3305 /* We are not parsing a function body. */ 3306 parser->in_function_body = false; 3307 3308 /* We can correct until told otherwise. */ 3309 parser->colon_corrects_to_scope_p = true; 3310 3311 /* The unparsed function queue is empty. */ 3312 push_unparsed_function_queues (parser); 3313 3314 /* There are no classes being defined. */ 3315 parser->num_classes_being_defined = 0; 3316 3317 /* No template parameters apply. */ 3318 parser->num_template_parameter_lists = 0; 3319 3320 return parser; 3321 } 3322 3323 /* Create a cp_lexer structure which will emit the tokens in CACHE 3324 and push it onto the parser's lexer stack. This is used for delayed 3325 parsing of in-class method bodies and default arguments, and should 3326 not be confused with tentative parsing. */ 3327 static void 3328 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache) 3329 { 3330 cp_lexer *lexer = cp_lexer_new_from_tokens (cache); 3331 lexer->next = parser->lexer; 3332 parser->lexer = lexer; 3333 3334 /* Move the current source position to that of the first token in the 3335 new lexer. */ 3336 cp_lexer_set_source_position_from_token (lexer->next_token); 3337 } 3338 3339 /* Pop the top lexer off the parser stack. This is never used for the 3340 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */ 3341 static void 3342 cp_parser_pop_lexer (cp_parser *parser) 3343 { 3344 cp_lexer *lexer = parser->lexer; 3345 parser->lexer = lexer->next; 3346 cp_lexer_destroy (lexer); 3347 3348 /* Put the current source position back where it was before this 3349 lexer was pushed. */ 3350 cp_lexer_set_source_position_from_token (parser->lexer->next_token); 3351 } 3352 3353 /* Lexical conventions [gram.lex] */ 3354 3355 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the 3356 identifier. */ 3357 3358 static tree 3359 cp_parser_identifier (cp_parser* parser) 3360 { 3361 cp_token *token; 3362 3363 /* Look for the identifier. */ 3364 token = cp_parser_require (parser, CPP_NAME, RT_NAME); 3365 /* Return the value. */ 3366 return token ? token->u.value : error_mark_node; 3367 } 3368 3369 /* Parse a sequence of adjacent string constants. Returns a 3370 TREE_STRING representing the combined, nul-terminated string 3371 constant. If TRANSLATE is true, translate the string to the 3372 execution character set. If WIDE_OK is true, a wide string is 3373 invalid here. 3374 3375 C++98 [lex.string] says that if a narrow string literal token is 3376 adjacent to a wide string literal token, the behavior is undefined. 3377 However, C99 6.4.5p4 says that this results in a wide string literal. 3378 We follow C99 here, for consistency with the C front end. 3379 3380 This code is largely lifted from lex_string() in c-lex.c. 3381 3382 FUTURE: ObjC++ will need to handle @-strings here. */ 3383 static tree 3384 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok) 3385 { 3386 tree value; 3387 size_t count; 3388 struct obstack str_ob; 3389 cpp_string str, istr, *strs; 3390 cp_token *tok; 3391 enum cpp_ttype type, curr_type; 3392 int have_suffix_p = 0; 3393 tree string_tree; 3394 tree suffix_id = NULL_TREE; 3395 bool curr_tok_is_userdef_p = false; 3396 3397 tok = cp_lexer_peek_token (parser->lexer); 3398 if (!cp_parser_is_string_literal (tok)) 3399 { 3400 cp_parser_error (parser, "expected string-literal"); 3401 return error_mark_node; 3402 } 3403 3404 if (cpp_userdef_string_p (tok->type)) 3405 { 3406 string_tree = USERDEF_LITERAL_VALUE (tok->u.value); 3407 curr_type = cpp_userdef_string_remove_type (tok->type); 3408 curr_tok_is_userdef_p = true; 3409 } 3410 else 3411 { 3412 string_tree = tok->u.value; 3413 curr_type = tok->type; 3414 } 3415 type = curr_type; 3416 3417 /* Try to avoid the overhead of creating and destroying an obstack 3418 for the common case of just one string. */ 3419 if (!cp_parser_is_string_literal 3420 (cp_lexer_peek_nth_token (parser->lexer, 2))) 3421 { 3422 cp_lexer_consume_token (parser->lexer); 3423 3424 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree); 3425 str.len = TREE_STRING_LENGTH (string_tree); 3426 count = 1; 3427 3428 if (curr_tok_is_userdef_p) 3429 { 3430 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value); 3431 have_suffix_p = 1; 3432 curr_type = cpp_userdef_string_remove_type (tok->type); 3433 } 3434 else 3435 curr_type = tok->type; 3436 3437 strs = &str; 3438 } 3439 else 3440 { 3441 gcc_obstack_init (&str_ob); 3442 count = 0; 3443 3444 do 3445 { 3446 cp_lexer_consume_token (parser->lexer); 3447 count++; 3448 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree); 3449 str.len = TREE_STRING_LENGTH (string_tree); 3450 3451 if (curr_tok_is_userdef_p) 3452 { 3453 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value); 3454 if (have_suffix_p == 0) 3455 { 3456 suffix_id = curr_suffix_id; 3457 have_suffix_p = 1; 3458 } 3459 else if (have_suffix_p == 1 3460 && curr_suffix_id != suffix_id) 3461 { 3462 error ("inconsistent user-defined literal suffixes" 3463 " %qD and %qD in string literal", 3464 suffix_id, curr_suffix_id); 3465 have_suffix_p = -1; 3466 } 3467 curr_type = cpp_userdef_string_remove_type (tok->type); 3468 } 3469 else 3470 curr_type = tok->type; 3471 3472 if (type != curr_type) 3473 { 3474 if (type == CPP_STRING) 3475 type = curr_type; 3476 else if (curr_type != CPP_STRING) 3477 error_at (tok->location, 3478 "unsupported non-standard concatenation " 3479 "of string literals"); 3480 } 3481 3482 obstack_grow (&str_ob, &str, sizeof (cpp_string)); 3483 3484 tok = cp_lexer_peek_token (parser->lexer); 3485 if (cpp_userdef_string_p (tok->type)) 3486 { 3487 string_tree = USERDEF_LITERAL_VALUE (tok->u.value); 3488 curr_type = cpp_userdef_string_remove_type (tok->type); 3489 curr_tok_is_userdef_p = true; 3490 } 3491 else 3492 { 3493 string_tree = tok->u.value; 3494 curr_type = tok->type; 3495 curr_tok_is_userdef_p = false; 3496 } 3497 } 3498 while (cp_parser_is_string_literal (tok)); 3499 3500 strs = (cpp_string *) obstack_finish (&str_ob); 3501 } 3502 3503 if (type != CPP_STRING && !wide_ok) 3504 { 3505 cp_parser_error (parser, "a wide string is invalid in this context"); 3506 type = CPP_STRING; 3507 } 3508 3509 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate) 3510 (parse_in, strs, count, &istr, type)) 3511 { 3512 value = build_string (istr.len, (const char *)istr.text); 3513 free (CONST_CAST (unsigned char *, istr.text)); 3514 3515 switch (type) 3516 { 3517 default: 3518 case CPP_STRING: 3519 case CPP_UTF8STRING: 3520 TREE_TYPE (value) = char_array_type_node; 3521 break; 3522 case CPP_STRING16: 3523 TREE_TYPE (value) = char16_array_type_node; 3524 break; 3525 case CPP_STRING32: 3526 TREE_TYPE (value) = char32_array_type_node; 3527 break; 3528 case CPP_WSTRING: 3529 TREE_TYPE (value) = wchar_array_type_node; 3530 break; 3531 } 3532 3533 value = fix_string_type (value); 3534 3535 if (have_suffix_p) 3536 { 3537 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE); 3538 tok->u.value = literal; 3539 return cp_parser_userdef_string_literal (tok); 3540 } 3541 } 3542 else 3543 /* cpp_interpret_string has issued an error. */ 3544 value = error_mark_node; 3545 3546 if (count > 1) 3547 obstack_free (&str_ob, 0); 3548 3549 return value; 3550 } 3551 3552 /* Look up a literal operator with the name and the exact arguments. */ 3553 3554 static tree 3555 lookup_literal_operator (tree name, VEC(tree,gc) *args) 3556 { 3557 tree decl, fns; 3558 decl = lookup_name (name); 3559 if (!decl || !is_overloaded_fn (decl)) 3560 return error_mark_node; 3561 3562 for (fns = decl; fns; fns = OVL_NEXT (fns)) 3563 { 3564 unsigned int ix; 3565 bool found = true; 3566 tree fn = OVL_CURRENT (fns); 3567 tree argtypes = NULL_TREE; 3568 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)); 3569 if (argtypes != NULL_TREE) 3570 { 3571 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE; 3572 ++ix, argtypes = TREE_CHAIN (argtypes)) 3573 { 3574 tree targ = TREE_VALUE (argtypes); 3575 tree tparm = TREE_TYPE (VEC_index (tree, args, ix)); 3576 bool ptr = TREE_CODE (targ) == POINTER_TYPE; 3577 bool arr = TREE_CODE (tparm) == ARRAY_TYPE; 3578 if ((ptr || arr || !same_type_p (targ, tparm)) 3579 && (!ptr || !arr 3580 || !same_type_p (TREE_TYPE (targ), 3581 TREE_TYPE (tparm)))) 3582 found = false; 3583 } 3584 if (found 3585 && ix == VEC_length (tree, args) 3586 /* May be this should be sufficient_parms_p instead, 3587 depending on how exactly should user-defined literals 3588 work in presence of default arguments on the literal 3589 operator parameters. */ 3590 && argtypes == void_list_node) 3591 return fn; 3592 } 3593 } 3594 3595 return error_mark_node; 3596 } 3597 3598 /* Parse a user-defined char constant. Returns a call to a user-defined 3599 literal operator taking the character as an argument. */ 3600 3601 static tree 3602 cp_parser_userdef_char_literal (cp_parser *parser) 3603 { 3604 cp_token *token = cp_lexer_consume_token (parser->lexer); 3605 tree literal = token->u.value; 3606 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal); 3607 tree value = USERDEF_LITERAL_VALUE (literal); 3608 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id)); 3609 tree decl, result; 3610 3611 /* Build up a call to the user-defined operator */ 3612 /* Lookup the name we got back from the id-expression. */ 3613 VEC(tree,gc) *args = make_tree_vector (); 3614 VEC_safe_push (tree, gc, args, value); 3615 decl = lookup_literal_operator (name, args); 3616 if (!decl || decl == error_mark_node) 3617 { 3618 error ("unable to find character literal operator %qD with %qT argument", 3619 name, TREE_TYPE (value)); 3620 release_tree_vector (args); 3621 return error_mark_node; 3622 } 3623 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error); 3624 release_tree_vector (args); 3625 if (result != error_mark_node) 3626 return result; 3627 3628 error ("unable to find character literal operator %qD with %qT argument", 3629 name, TREE_TYPE (value)); 3630 return error_mark_node; 3631 } 3632 3633 /* A subroutine of cp_parser_userdef_numeric_literal to 3634 create a char... template parameter pack from a string node. */ 3635 3636 static tree 3637 make_char_string_pack (tree value) 3638 { 3639 tree charvec; 3640 tree argpack = make_node (NONTYPE_ARGUMENT_PACK); 3641 const char *str = TREE_STRING_POINTER (value); 3642 int i, len = TREE_STRING_LENGTH (value) - 1; 3643 tree argvec = make_tree_vec (1); 3644 3645 /* Fill in CHARVEC with all of the parameters. */ 3646 charvec = make_tree_vec (len); 3647 for (i = 0; i < len; ++i) 3648 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]); 3649 3650 /* Build the argument packs. */ 3651 SET_ARGUMENT_PACK_ARGS (argpack, charvec); 3652 TREE_TYPE (argpack) = char_type_node; 3653 3654 TREE_VEC_ELT (argvec, 0) = argpack; 3655 3656 return argvec; 3657 } 3658 3659 /* Parse a user-defined numeric constant. returns a call to a user-defined 3660 literal operator. */ 3661 3662 static tree 3663 cp_parser_userdef_numeric_literal (cp_parser *parser) 3664 { 3665 cp_token *token = cp_lexer_consume_token (parser->lexer); 3666 tree literal = token->u.value; 3667 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal); 3668 tree value = USERDEF_LITERAL_VALUE (literal); 3669 tree num_string = USERDEF_LITERAL_NUM_STRING (literal); 3670 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id)); 3671 tree decl, result; 3672 VEC(tree,gc) *args; 3673 3674 /* Look for a literal operator taking the exact type of numeric argument 3675 as the literal value. */ 3676 args = make_tree_vector (); 3677 VEC_safe_push (tree, gc, args, value); 3678 decl = lookup_literal_operator (name, args); 3679 if (decl && decl != error_mark_node) 3680 { 3681 result = finish_call_expr (decl, &args, false, true, tf_none); 3682 if (result != error_mark_node) 3683 { 3684 release_tree_vector (args); 3685 return result; 3686 } 3687 } 3688 release_tree_vector (args); 3689 3690 /* If the numeric argument didn't work, look for a raw literal 3691 operator taking a const char* argument consisting of the number 3692 in string format. */ 3693 args = make_tree_vector (); 3694 VEC_safe_push (tree, gc, args, num_string); 3695 decl = lookup_literal_operator (name, args); 3696 if (decl && decl != error_mark_node) 3697 { 3698 result = finish_call_expr (decl, &args, false, true, tf_none); 3699 if (result != error_mark_node) 3700 { 3701 release_tree_vector (args); 3702 return result; 3703 } 3704 } 3705 release_tree_vector (args); 3706 3707 /* If the raw literal didn't work, look for a non-type template 3708 function with parameter pack char.... Call the function with 3709 template parameter characters representing the number. */ 3710 args = make_tree_vector (); 3711 decl = lookup_literal_operator (name, args); 3712 if (decl && decl != error_mark_node) 3713 { 3714 tree tmpl_args = make_char_string_pack (num_string); 3715 decl = lookup_template_function (decl, tmpl_args); 3716 result = finish_call_expr (decl, &args, false, true, tf_none); 3717 if (result != error_mark_node) 3718 { 3719 release_tree_vector (args); 3720 return result; 3721 } 3722 } 3723 release_tree_vector (args); 3724 3725 error ("unable to find numeric literal operator %qD", name); 3726 return error_mark_node; 3727 } 3728 3729 /* Parse a user-defined string constant. Returns a call to a user-defined 3730 literal operator taking a character pointer and the length of the string 3731 as arguments. */ 3732 3733 static tree 3734 cp_parser_userdef_string_literal (cp_token *token) 3735 { 3736 tree literal = token->u.value; 3737 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal); 3738 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id)); 3739 tree value = USERDEF_LITERAL_VALUE (literal); 3740 int len = TREE_STRING_LENGTH (value) 3741 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1; 3742 tree decl, result; 3743 3744 /* Build up a call to the user-defined operator */ 3745 /* Lookup the name we got back from the id-expression. */ 3746 VEC(tree,gc) *args = make_tree_vector (); 3747 VEC_safe_push (tree, gc, args, value); 3748 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len)); 3749 decl = lookup_name (name); 3750 if (!decl || decl == error_mark_node) 3751 { 3752 error ("unable to find string literal operator %qD", name); 3753 release_tree_vector (args); 3754 return error_mark_node; 3755 } 3756 result = finish_call_expr (decl, &args, false, true, tf_none); 3757 release_tree_vector (args); 3758 if (result != error_mark_node) 3759 return result; 3760 3761 error ("unable to find string literal operator %qD with %qT, %qT arguments", 3762 name, TREE_TYPE (value), size_type_node); 3763 return error_mark_node; 3764 } 3765 3766 3767 /* Basic concepts [gram.basic] */ 3768 3769 /* Parse a translation-unit. 3770 3771 translation-unit: 3772 declaration-seq [opt] 3773 3774 Returns TRUE if all went well. */ 3775 3776 static bool 3777 cp_parser_translation_unit (cp_parser* parser) 3778 { 3779 /* The address of the first non-permanent object on the declarator 3780 obstack. */ 3781 static void *declarator_obstack_base; 3782 3783 bool success; 3784 3785 /* Create the declarator obstack, if necessary. */ 3786 if (!cp_error_declarator) 3787 { 3788 gcc_obstack_init (&declarator_obstack); 3789 /* Create the error declarator. */ 3790 cp_error_declarator = make_declarator (cdk_error); 3791 /* Create the empty parameter list. */ 3792 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE); 3793 /* Remember where the base of the declarator obstack lies. */ 3794 declarator_obstack_base = obstack_next_free (&declarator_obstack); 3795 } 3796 3797 cp_parser_declaration_seq_opt (parser); 3798 3799 /* If there are no tokens left then all went well. */ 3800 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) 3801 { 3802 /* Get rid of the token array; we don't need it any more. */ 3803 cp_lexer_destroy (parser->lexer); 3804 parser->lexer = NULL; 3805 3806 /* This file might have been a context that's implicitly extern 3807 "C". If so, pop the lang context. (Only relevant for PCH.) */ 3808 if (parser->implicit_extern_c) 3809 { 3810 pop_lang_context (); 3811 parser->implicit_extern_c = false; 3812 } 3813 3814 /* Finish up. */ 3815 finish_translation_unit (); 3816 3817 success = true; 3818 } 3819 else 3820 { 3821 cp_parser_error (parser, "expected declaration"); 3822 success = false; 3823 } 3824 3825 /* Make sure the declarator obstack was fully cleaned up. */ 3826 gcc_assert (obstack_next_free (&declarator_obstack) 3827 == declarator_obstack_base); 3828 3829 /* All went well. */ 3830 return success; 3831 } 3832 3833 /* Expressions [gram.expr] */ 3834 3835 /* Parse a primary-expression. 3836 3837 primary-expression: 3838 literal 3839 this 3840 ( expression ) 3841 id-expression 3842 3843 GNU Extensions: 3844 3845 primary-expression: 3846 ( compound-statement ) 3847 __builtin_va_arg ( assignment-expression , type-id ) 3848 __builtin_offsetof ( type-id , offsetof-expression ) 3849 3850 C++ Extensions: 3851 __has_nothrow_assign ( type-id ) 3852 __has_nothrow_constructor ( type-id ) 3853 __has_nothrow_copy ( type-id ) 3854 __has_trivial_assign ( type-id ) 3855 __has_trivial_constructor ( type-id ) 3856 __has_trivial_copy ( type-id ) 3857 __has_trivial_destructor ( type-id ) 3858 __has_virtual_destructor ( type-id ) 3859 __is_abstract ( type-id ) 3860 __is_base_of ( type-id , type-id ) 3861 __is_class ( type-id ) 3862 __is_convertible_to ( type-id , type-id ) 3863 __is_empty ( type-id ) 3864 __is_enum ( type-id ) 3865 __is_final ( type-id ) 3866 __is_literal_type ( type-id ) 3867 __is_pod ( type-id ) 3868 __is_polymorphic ( type-id ) 3869 __is_std_layout ( type-id ) 3870 __is_trivial ( type-id ) 3871 __is_union ( type-id ) 3872 3873 Objective-C++ Extension: 3874 3875 primary-expression: 3876 objc-expression 3877 3878 literal: 3879 __null 3880 3881 ADDRESS_P is true iff this expression was immediately preceded by 3882 "&" and therefore might denote a pointer-to-member. CAST_P is true 3883 iff this expression is the target of a cast. TEMPLATE_ARG_P is 3884 true iff this expression is a template argument. 3885 3886 Returns a representation of the expression. Upon return, *IDK 3887 indicates what kind of id-expression (if any) was present. */ 3888 3889 static tree 3890 cp_parser_primary_expression (cp_parser *parser, 3891 bool address_p, 3892 bool cast_p, 3893 bool template_arg_p, 3894 cp_id_kind *idk) 3895 { 3896 cp_token *token = NULL; 3897 3898 /* Assume the primary expression is not an id-expression. */ 3899 *idk = CP_ID_KIND_NONE; 3900 3901 /* Peek at the next token. */ 3902 token = cp_lexer_peek_token (parser->lexer); 3903 switch (token->type) 3904 { 3905 /* literal: 3906 integer-literal 3907 character-literal 3908 floating-literal 3909 string-literal 3910 boolean-literal 3911 pointer-literal 3912 user-defined-literal */ 3913 case CPP_CHAR: 3914 case CPP_CHAR16: 3915 case CPP_CHAR32: 3916 case CPP_WCHAR: 3917 case CPP_NUMBER: 3918 if (TREE_CODE (token->u.value) == USERDEF_LITERAL) 3919 return cp_parser_userdef_numeric_literal (parser); 3920 token = cp_lexer_consume_token (parser->lexer); 3921 if (TREE_CODE (token->u.value) == FIXED_CST) 3922 { 3923 error_at (token->location, 3924 "fixed-point types not supported in C++"); 3925 return error_mark_node; 3926 } 3927 /* Floating-point literals are only allowed in an integral 3928 constant expression if they are cast to an integral or 3929 enumeration type. */ 3930 if (TREE_CODE (token->u.value) == REAL_CST 3931 && parser->integral_constant_expression_p 3932 && pedantic) 3933 { 3934 /* CAST_P will be set even in invalid code like "int(2.7 + 3935 ...)". Therefore, we have to check that the next token 3936 is sure to end the cast. */ 3937 if (cast_p) 3938 { 3939 cp_token *next_token; 3940 3941 next_token = cp_lexer_peek_token (parser->lexer); 3942 if (/* The comma at the end of an 3943 enumerator-definition. */ 3944 next_token->type != CPP_COMMA 3945 /* The curly brace at the end of an enum-specifier. */ 3946 && next_token->type != CPP_CLOSE_BRACE 3947 /* The end of a statement. */ 3948 && next_token->type != CPP_SEMICOLON 3949 /* The end of the cast-expression. */ 3950 && next_token->type != CPP_CLOSE_PAREN 3951 /* The end of an array bound. */ 3952 && next_token->type != CPP_CLOSE_SQUARE 3953 /* The closing ">" in a template-argument-list. */ 3954 && (next_token->type != CPP_GREATER 3955 || parser->greater_than_is_operator_p) 3956 /* C++0x only: A ">>" treated like two ">" tokens, 3957 in a template-argument-list. */ 3958 && (next_token->type != CPP_RSHIFT 3959 || (cxx_dialect == cxx98) 3960 || parser->greater_than_is_operator_p)) 3961 cast_p = false; 3962 } 3963 3964 /* If we are within a cast, then the constraint that the 3965 cast is to an integral or enumeration type will be 3966 checked at that point. If we are not within a cast, then 3967 this code is invalid. */ 3968 if (!cast_p) 3969 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT); 3970 } 3971 return token->u.value; 3972 3973 case CPP_CHAR_USERDEF: 3974 case CPP_CHAR16_USERDEF: 3975 case CPP_CHAR32_USERDEF: 3976 case CPP_WCHAR_USERDEF: 3977 return cp_parser_userdef_char_literal (parser); 3978 3979 case CPP_STRING: 3980 case CPP_STRING16: 3981 case CPP_STRING32: 3982 case CPP_WSTRING: 3983 case CPP_UTF8STRING: 3984 case CPP_STRING_USERDEF: 3985 case CPP_STRING16_USERDEF: 3986 case CPP_STRING32_USERDEF: 3987 case CPP_WSTRING_USERDEF: 3988 case CPP_UTF8STRING_USERDEF: 3989 /* ??? Should wide strings be allowed when parser->translate_strings_p 3990 is false (i.e. in attributes)? If not, we can kill the third 3991 argument to cp_parser_string_literal. */ 3992 return cp_parser_string_literal (parser, 3993 parser->translate_strings_p, 3994 true); 3995 3996 case CPP_OPEN_PAREN: 3997 { 3998 tree expr; 3999 bool saved_greater_than_is_operator_p; 4000 4001 /* Consume the `('. */ 4002 cp_lexer_consume_token (parser->lexer); 4003 /* Within a parenthesized expression, a `>' token is always 4004 the greater-than operator. */ 4005 saved_greater_than_is_operator_p 4006 = parser->greater_than_is_operator_p; 4007 parser->greater_than_is_operator_p = true; 4008 /* If we see `( { ' then we are looking at the beginning of 4009 a GNU statement-expression. */ 4010 if (cp_parser_allow_gnu_extensions_p (parser) 4011 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 4012 { 4013 /* Statement-expressions are not allowed by the standard. */ 4014 pedwarn (token->location, OPT_pedantic, 4015 "ISO C++ forbids braced-groups within expressions"); 4016 4017 /* And they're not allowed outside of a function-body; you 4018 cannot, for example, write: 4019 4020 int i = ({ int j = 3; j + 1; }); 4021 4022 at class or namespace scope. */ 4023 if (!parser->in_function_body 4024 || parser->in_template_argument_list_p) 4025 { 4026 error_at (token->location, 4027 "statement-expressions are not allowed outside " 4028 "functions nor in template-argument lists"); 4029 cp_parser_skip_to_end_of_block_or_statement (parser); 4030 expr = error_mark_node; 4031 } 4032 else 4033 { 4034 /* Start the statement-expression. */ 4035 expr = begin_stmt_expr (); 4036 /* Parse the compound-statement. */ 4037 cp_parser_compound_statement (parser, expr, false, false); 4038 /* Finish up. */ 4039 expr = finish_stmt_expr (expr, false); 4040 } 4041 } 4042 else 4043 { 4044 /* Parse the parenthesized expression. */ 4045 expr = cp_parser_expression (parser, cast_p, idk); 4046 /* Let the front end know that this expression was 4047 enclosed in parentheses. This matters in case, for 4048 example, the expression is of the form `A::B', since 4049 `&A::B' might be a pointer-to-member, but `&(A::B)' is 4050 not. */ 4051 finish_parenthesized_expr (expr); 4052 /* DR 705: Wrapping an unqualified name in parentheses 4053 suppresses arg-dependent lookup. We want to pass back 4054 CP_ID_KIND_QUALIFIED for suppressing vtable lookup 4055 (c++/37862), but none of the others. */ 4056 if (*idk != CP_ID_KIND_QUALIFIED) 4057 *idk = CP_ID_KIND_NONE; 4058 } 4059 /* The `>' token might be the end of a template-id or 4060 template-parameter-list now. */ 4061 parser->greater_than_is_operator_p 4062 = saved_greater_than_is_operator_p; 4063 /* Consume the `)'. */ 4064 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 4065 cp_parser_skip_to_end_of_statement (parser); 4066 4067 return expr; 4068 } 4069 4070 case CPP_OPEN_SQUARE: 4071 if (c_dialect_objc ()) 4072 /* We have an Objective-C++ message. */ 4073 return cp_parser_objc_expression (parser); 4074 { 4075 tree lam = cp_parser_lambda_expression (parser); 4076 /* Don't warn about a failed tentative parse. */ 4077 if (cp_parser_error_occurred (parser)) 4078 return error_mark_node; 4079 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR); 4080 return lam; 4081 } 4082 4083 case CPP_OBJC_STRING: 4084 if (c_dialect_objc ()) 4085 /* We have an Objective-C++ string literal. */ 4086 return cp_parser_objc_expression (parser); 4087 cp_parser_error (parser, "expected primary-expression"); 4088 return error_mark_node; 4089 4090 case CPP_KEYWORD: 4091 switch (token->keyword) 4092 { 4093 /* These two are the boolean literals. */ 4094 case RID_TRUE: 4095 cp_lexer_consume_token (parser->lexer); 4096 return boolean_true_node; 4097 case RID_FALSE: 4098 cp_lexer_consume_token (parser->lexer); 4099 return boolean_false_node; 4100 4101 /* The `__null' literal. */ 4102 case RID_NULL: 4103 cp_lexer_consume_token (parser->lexer); 4104 return null_node; 4105 4106 /* The `nullptr' literal. */ 4107 case RID_NULLPTR: 4108 cp_lexer_consume_token (parser->lexer); 4109 return nullptr_node; 4110 4111 /* Recognize the `this' keyword. */ 4112 case RID_THIS: 4113 cp_lexer_consume_token (parser->lexer); 4114 if (parser->local_variables_forbidden_p) 4115 { 4116 error_at (token->location, 4117 "%<this%> may not be used in this context"); 4118 return error_mark_node; 4119 } 4120 /* Pointers cannot appear in constant-expressions. */ 4121 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS)) 4122 return error_mark_node; 4123 return finish_this_expr (); 4124 4125 /* The `operator' keyword can be the beginning of an 4126 id-expression. */ 4127 case RID_OPERATOR: 4128 goto id_expression; 4129 4130 case RID_FUNCTION_NAME: 4131 case RID_PRETTY_FUNCTION_NAME: 4132 case RID_C99_FUNCTION_NAME: 4133 { 4134 non_integral_constant name; 4135 4136 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and 4137 __func__ are the names of variables -- but they are 4138 treated specially. Therefore, they are handled here, 4139 rather than relying on the generic id-expression logic 4140 below. Grammatically, these names are id-expressions. 4141 4142 Consume the token. */ 4143 token = cp_lexer_consume_token (parser->lexer); 4144 4145 switch (token->keyword) 4146 { 4147 case RID_FUNCTION_NAME: 4148 name = NIC_FUNC_NAME; 4149 break; 4150 case RID_PRETTY_FUNCTION_NAME: 4151 name = NIC_PRETTY_FUNC; 4152 break; 4153 case RID_C99_FUNCTION_NAME: 4154 name = NIC_C99_FUNC; 4155 break; 4156 default: 4157 gcc_unreachable (); 4158 } 4159 4160 if (cp_parser_non_integral_constant_expression (parser, name)) 4161 return error_mark_node; 4162 4163 /* Look up the name. */ 4164 return finish_fname (token->u.value); 4165 } 4166 4167 case RID_VA_ARG: 4168 { 4169 tree expression; 4170 tree type; 4171 4172 /* The `__builtin_va_arg' construct is used to handle 4173 `va_arg'. Consume the `__builtin_va_arg' token. */ 4174 cp_lexer_consume_token (parser->lexer); 4175 /* Look for the opening `('. */ 4176 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 4177 /* Now, parse the assignment-expression. */ 4178 expression = cp_parser_assignment_expression (parser, 4179 /*cast_p=*/false, NULL); 4180 /* Look for the `,'. */ 4181 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 4182 /* Parse the type-id. */ 4183 type = cp_parser_type_id (parser); 4184 /* Look for the closing `)'. */ 4185 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 4186 /* Using `va_arg' in a constant-expression is not 4187 allowed. */ 4188 if (cp_parser_non_integral_constant_expression (parser, 4189 NIC_VA_ARG)) 4190 return error_mark_node; 4191 return build_x_va_arg (expression, type); 4192 } 4193 4194 case RID_OFFSETOF: 4195 return cp_parser_builtin_offsetof (parser); 4196 4197 case RID_HAS_NOTHROW_ASSIGN: 4198 case RID_HAS_NOTHROW_CONSTRUCTOR: 4199 case RID_HAS_NOTHROW_COPY: 4200 case RID_HAS_TRIVIAL_ASSIGN: 4201 case RID_HAS_TRIVIAL_CONSTRUCTOR: 4202 case RID_HAS_TRIVIAL_COPY: 4203 case RID_HAS_TRIVIAL_DESTRUCTOR: 4204 case RID_HAS_VIRTUAL_DESTRUCTOR: 4205 case RID_IS_ABSTRACT: 4206 case RID_IS_BASE_OF: 4207 case RID_IS_CLASS: 4208 case RID_IS_CONVERTIBLE_TO: 4209 case RID_IS_EMPTY: 4210 case RID_IS_ENUM: 4211 case RID_IS_FINAL: 4212 case RID_IS_LITERAL_TYPE: 4213 case RID_IS_POD: 4214 case RID_IS_POLYMORPHIC: 4215 case RID_IS_STD_LAYOUT: 4216 case RID_IS_TRIVIAL: 4217 case RID_IS_UNION: 4218 return cp_parser_trait_expr (parser, token->keyword); 4219 4220 /* Objective-C++ expressions. */ 4221 case RID_AT_ENCODE: 4222 case RID_AT_PROTOCOL: 4223 case RID_AT_SELECTOR: 4224 return cp_parser_objc_expression (parser); 4225 4226 case RID_TEMPLATE: 4227 if (parser->in_function_body 4228 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 4229 == CPP_LESS)) 4230 { 4231 error_at (token->location, 4232 "a template declaration cannot appear at block scope"); 4233 cp_parser_skip_to_end_of_block_or_statement (parser); 4234 return error_mark_node; 4235 } 4236 default: 4237 cp_parser_error (parser, "expected primary-expression"); 4238 return error_mark_node; 4239 } 4240 4241 /* An id-expression can start with either an identifier, a 4242 `::' as the beginning of a qualified-id, or the "operator" 4243 keyword. */ 4244 case CPP_NAME: 4245 case CPP_SCOPE: 4246 case CPP_TEMPLATE_ID: 4247 case CPP_NESTED_NAME_SPECIFIER: 4248 { 4249 tree id_expression; 4250 tree decl; 4251 const char *error_msg; 4252 bool template_p; 4253 bool done; 4254 cp_token *id_expr_token; 4255 4256 id_expression: 4257 /* Parse the id-expression. */ 4258 id_expression 4259 = cp_parser_id_expression (parser, 4260 /*template_keyword_p=*/false, 4261 /*check_dependency_p=*/true, 4262 &template_p, 4263 /*declarator_p=*/false, 4264 /*optional_p=*/false); 4265 if (id_expression == error_mark_node) 4266 return error_mark_node; 4267 id_expr_token = token; 4268 token = cp_lexer_peek_token (parser->lexer); 4269 done = (token->type != CPP_OPEN_SQUARE 4270 && token->type != CPP_OPEN_PAREN 4271 && token->type != CPP_DOT 4272 && token->type != CPP_DEREF 4273 && token->type != CPP_PLUS_PLUS 4274 && token->type != CPP_MINUS_MINUS); 4275 /* If we have a template-id, then no further lookup is 4276 required. If the template-id was for a template-class, we 4277 will sometimes have a TYPE_DECL at this point. */ 4278 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR 4279 || TREE_CODE (id_expression) == TYPE_DECL) 4280 decl = id_expression; 4281 /* Look up the name. */ 4282 else 4283 { 4284 tree ambiguous_decls; 4285 4286 /* If we already know that this lookup is ambiguous, then 4287 we've already issued an error message; there's no reason 4288 to check again. */ 4289 if (id_expr_token->type == CPP_NAME 4290 && id_expr_token->ambiguous_p) 4291 { 4292 cp_parser_simulate_error (parser); 4293 return error_mark_node; 4294 } 4295 4296 decl = cp_parser_lookup_name (parser, id_expression, 4297 none_type, 4298 template_p, 4299 /*is_namespace=*/false, 4300 /*check_dependency=*/true, 4301 &ambiguous_decls, 4302 id_expr_token->location); 4303 /* If the lookup was ambiguous, an error will already have 4304 been issued. */ 4305 if (ambiguous_decls) 4306 return error_mark_node; 4307 4308 /* In Objective-C++, we may have an Objective-C 2.0 4309 dot-syntax for classes here. */ 4310 if (c_dialect_objc () 4311 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 4312 && TREE_CODE (decl) == TYPE_DECL 4313 && objc_is_class_name (decl)) 4314 { 4315 tree component; 4316 cp_lexer_consume_token (parser->lexer); 4317 component = cp_parser_identifier (parser); 4318 if (component == error_mark_node) 4319 return error_mark_node; 4320 4321 return objc_build_class_component_ref (id_expression, component); 4322 } 4323 4324 /* In Objective-C++, an instance variable (ivar) may be preferred 4325 to whatever cp_parser_lookup_name() found. */ 4326 decl = objc_lookup_ivar (decl, id_expression); 4327 4328 /* If name lookup gives us a SCOPE_REF, then the 4329 qualifying scope was dependent. */ 4330 if (TREE_CODE (decl) == SCOPE_REF) 4331 { 4332 /* At this point, we do not know if DECL is a valid 4333 integral constant expression. We assume that it is 4334 in fact such an expression, so that code like: 4335 4336 template <int N> struct A { 4337 int a[B<N>::i]; 4338 }; 4339 4340 is accepted. At template-instantiation time, we 4341 will check that B<N>::i is actually a constant. */ 4342 return decl; 4343 } 4344 /* Check to see if DECL is a local variable in a context 4345 where that is forbidden. */ 4346 if (parser->local_variables_forbidden_p 4347 && local_variable_p (decl)) 4348 { 4349 /* It might be that we only found DECL because we are 4350 trying to be generous with pre-ISO scoping rules. 4351 For example, consider: 4352 4353 int i; 4354 void g() { 4355 for (int i = 0; i < 10; ++i) {} 4356 extern void f(int j = i); 4357 } 4358 4359 Here, name look up will originally find the out 4360 of scope `i'. We need to issue a warning message, 4361 but then use the global `i'. */ 4362 decl = check_for_out_of_scope_variable (decl); 4363 if (local_variable_p (decl)) 4364 { 4365 error_at (id_expr_token->location, 4366 "local variable %qD may not appear in this context", 4367 decl); 4368 return error_mark_node; 4369 } 4370 } 4371 } 4372 4373 decl = (finish_id_expression 4374 (id_expression, decl, parser->scope, 4375 idk, 4376 parser->integral_constant_expression_p, 4377 parser->allow_non_integral_constant_expression_p, 4378 &parser->non_integral_constant_expression_p, 4379 template_p, done, address_p, 4380 template_arg_p, 4381 &error_msg, 4382 id_expr_token->location)); 4383 if (error_msg) 4384 cp_parser_error (parser, error_msg); 4385 return decl; 4386 } 4387 4388 /* Anything else is an error. */ 4389 default: 4390 cp_parser_error (parser, "expected primary-expression"); 4391 return error_mark_node; 4392 } 4393 } 4394 4395 /* Parse an id-expression. 4396 4397 id-expression: 4398 unqualified-id 4399 qualified-id 4400 4401 qualified-id: 4402 :: [opt] nested-name-specifier template [opt] unqualified-id 4403 :: identifier 4404 :: operator-function-id 4405 :: template-id 4406 4407 Return a representation of the unqualified portion of the 4408 identifier. Sets PARSER->SCOPE to the qualifying scope if there is 4409 a `::' or nested-name-specifier. 4410 4411 Often, if the id-expression was a qualified-id, the caller will 4412 want to make a SCOPE_REF to represent the qualified-id. This 4413 function does not do this in order to avoid wastefully creating 4414 SCOPE_REFs when they are not required. 4415 4416 If TEMPLATE_KEYWORD_P is true, then we have just seen the 4417 `template' keyword. 4418 4419 If CHECK_DEPENDENCY_P is false, then names are looked up inside 4420 uninstantiated templates. 4421 4422 If *TEMPLATE_P is non-NULL, it is set to true iff the 4423 `template' keyword is used to explicitly indicate that the entity 4424 named is a template. 4425 4426 If DECLARATOR_P is true, the id-expression is appearing as part of 4427 a declarator, rather than as part of an expression. */ 4428 4429 static tree 4430 cp_parser_id_expression (cp_parser *parser, 4431 bool template_keyword_p, 4432 bool check_dependency_p, 4433 bool *template_p, 4434 bool declarator_p, 4435 bool optional_p) 4436 { 4437 bool global_scope_p; 4438 bool nested_name_specifier_p; 4439 4440 /* Assume the `template' keyword was not used. */ 4441 if (template_p) 4442 *template_p = template_keyword_p; 4443 4444 /* Look for the optional `::' operator. */ 4445 global_scope_p 4446 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false) 4447 != NULL_TREE); 4448 /* Look for the optional nested-name-specifier. */ 4449 nested_name_specifier_p 4450 = (cp_parser_nested_name_specifier_opt (parser, 4451 /*typename_keyword_p=*/false, 4452 check_dependency_p, 4453 /*type_p=*/false, 4454 declarator_p) 4455 != NULL_TREE); 4456 /* If there is a nested-name-specifier, then we are looking at 4457 the first qualified-id production. */ 4458 if (nested_name_specifier_p) 4459 { 4460 tree saved_scope; 4461 tree saved_object_scope; 4462 tree saved_qualifying_scope; 4463 tree unqualified_id; 4464 bool is_template; 4465 4466 /* See if the next token is the `template' keyword. */ 4467 if (!template_p) 4468 template_p = &is_template; 4469 *template_p = cp_parser_optional_template_keyword (parser); 4470 /* Name lookup we do during the processing of the 4471 unqualified-id might obliterate SCOPE. */ 4472 saved_scope = parser->scope; 4473 saved_object_scope = parser->object_scope; 4474 saved_qualifying_scope = parser->qualifying_scope; 4475 /* Process the final unqualified-id. */ 4476 unqualified_id = cp_parser_unqualified_id (parser, *template_p, 4477 check_dependency_p, 4478 declarator_p, 4479 /*optional_p=*/false); 4480 /* Restore the SAVED_SCOPE for our caller. */ 4481 parser->scope = saved_scope; 4482 parser->object_scope = saved_object_scope; 4483 parser->qualifying_scope = saved_qualifying_scope; 4484 4485 return unqualified_id; 4486 } 4487 /* Otherwise, if we are in global scope, then we are looking at one 4488 of the other qualified-id productions. */ 4489 else if (global_scope_p) 4490 { 4491 cp_token *token; 4492 tree id; 4493 4494 /* Peek at the next token. */ 4495 token = cp_lexer_peek_token (parser->lexer); 4496 4497 /* If it's an identifier, and the next token is not a "<", then 4498 we can avoid the template-id case. This is an optimization 4499 for this common case. */ 4500 if (token->type == CPP_NAME 4501 && !cp_parser_nth_token_starts_template_argument_list_p 4502 (parser, 2)) 4503 return cp_parser_identifier (parser); 4504 4505 cp_parser_parse_tentatively (parser); 4506 /* Try a template-id. */ 4507 id = cp_parser_template_id (parser, 4508 /*template_keyword_p=*/false, 4509 /*check_dependency_p=*/true, 4510 declarator_p); 4511 /* If that worked, we're done. */ 4512 if (cp_parser_parse_definitely (parser)) 4513 return id; 4514 4515 /* Peek at the next token. (Changes in the token buffer may 4516 have invalidated the pointer obtained above.) */ 4517 token = cp_lexer_peek_token (parser->lexer); 4518 4519 switch (token->type) 4520 { 4521 case CPP_NAME: 4522 return cp_parser_identifier (parser); 4523 4524 case CPP_KEYWORD: 4525 if (token->keyword == RID_OPERATOR) 4526 return cp_parser_operator_function_id (parser); 4527 /* Fall through. */ 4528 4529 default: 4530 cp_parser_error (parser, "expected id-expression"); 4531 return error_mark_node; 4532 } 4533 } 4534 else 4535 return cp_parser_unqualified_id (parser, template_keyword_p, 4536 /*check_dependency_p=*/true, 4537 declarator_p, 4538 optional_p); 4539 } 4540 4541 /* Parse an unqualified-id. 4542 4543 unqualified-id: 4544 identifier 4545 operator-function-id 4546 conversion-function-id 4547 ~ class-name 4548 template-id 4549 4550 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template' 4551 keyword, in a construct like `A::template ...'. 4552 4553 Returns a representation of unqualified-id. For the `identifier' 4554 production, an IDENTIFIER_NODE is returned. For the `~ class-name' 4555 production a BIT_NOT_EXPR is returned; the operand of the 4556 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the 4557 other productions, see the documentation accompanying the 4558 corresponding parsing functions. If CHECK_DEPENDENCY_P is false, 4559 names are looked up in uninstantiated templates. If DECLARATOR_P 4560 is true, the unqualified-id is appearing as part of a declarator, 4561 rather than as part of an expression. */ 4562 4563 static tree 4564 cp_parser_unqualified_id (cp_parser* parser, 4565 bool template_keyword_p, 4566 bool check_dependency_p, 4567 bool declarator_p, 4568 bool optional_p) 4569 { 4570 cp_token *token; 4571 4572 /* Peek at the next token. */ 4573 token = cp_lexer_peek_token (parser->lexer); 4574 4575 switch (token->type) 4576 { 4577 case CPP_NAME: 4578 { 4579 tree id; 4580 4581 /* We don't know yet whether or not this will be a 4582 template-id. */ 4583 cp_parser_parse_tentatively (parser); 4584 /* Try a template-id. */ 4585 id = cp_parser_template_id (parser, template_keyword_p, 4586 check_dependency_p, 4587 declarator_p); 4588 /* If it worked, we're done. */ 4589 if (cp_parser_parse_definitely (parser)) 4590 return id; 4591 /* Otherwise, it's an ordinary identifier. */ 4592 return cp_parser_identifier (parser); 4593 } 4594 4595 case CPP_TEMPLATE_ID: 4596 return cp_parser_template_id (parser, template_keyword_p, 4597 check_dependency_p, 4598 declarator_p); 4599 4600 case CPP_COMPL: 4601 { 4602 tree type_decl; 4603 tree qualifying_scope; 4604 tree object_scope; 4605 tree scope; 4606 bool done; 4607 4608 /* Consume the `~' token. */ 4609 cp_lexer_consume_token (parser->lexer); 4610 /* Parse the class-name. The standard, as written, seems to 4611 say that: 4612 4613 template <typename T> struct S { ~S (); }; 4614 template <typename T> S<T>::~S() {} 4615 4616 is invalid, since `~' must be followed by a class-name, but 4617 `S<T>' is dependent, and so not known to be a class. 4618 That's not right; we need to look in uninstantiated 4619 templates. A further complication arises from: 4620 4621 template <typename T> void f(T t) { 4622 t.T::~T(); 4623 } 4624 4625 Here, it is not possible to look up `T' in the scope of `T' 4626 itself. We must look in both the current scope, and the 4627 scope of the containing complete expression. 4628 4629 Yet another issue is: 4630 4631 struct S { 4632 int S; 4633 ~S(); 4634 }; 4635 4636 S::~S() {} 4637 4638 The standard does not seem to say that the `S' in `~S' 4639 should refer to the type `S' and not the data member 4640 `S::S'. */ 4641 4642 /* DR 244 says that we look up the name after the "~" in the 4643 same scope as we looked up the qualifying name. That idea 4644 isn't fully worked out; it's more complicated than that. */ 4645 scope = parser->scope; 4646 object_scope = parser->object_scope; 4647 qualifying_scope = parser->qualifying_scope; 4648 4649 /* Check for invalid scopes. */ 4650 if (scope == error_mark_node) 4651 { 4652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 4653 cp_lexer_consume_token (parser->lexer); 4654 return error_mark_node; 4655 } 4656 if (scope && TREE_CODE (scope) == NAMESPACE_DECL) 4657 { 4658 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) 4659 error_at (token->location, 4660 "scope %qT before %<~%> is not a class-name", 4661 scope); 4662 cp_parser_simulate_error (parser); 4663 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 4664 cp_lexer_consume_token (parser->lexer); 4665 return error_mark_node; 4666 } 4667 gcc_assert (!scope || TYPE_P (scope)); 4668 4669 /* If the name is of the form "X::~X" it's OK even if X is a 4670 typedef. */ 4671 token = cp_lexer_peek_token (parser->lexer); 4672 if (scope 4673 && token->type == CPP_NAME 4674 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 4675 != CPP_LESS) 4676 && (token->u.value == TYPE_IDENTIFIER (scope) 4677 || (CLASS_TYPE_P (scope) 4678 && constructor_name_p (token->u.value, scope)))) 4679 { 4680 cp_lexer_consume_token (parser->lexer); 4681 return build_nt (BIT_NOT_EXPR, scope); 4682 } 4683 4684 /* If there was an explicit qualification (S::~T), first look 4685 in the scope given by the qualification (i.e., S). 4686 4687 Note: in the calls to cp_parser_class_name below we pass 4688 typename_type so that lookup finds the injected-class-name 4689 rather than the constructor. */ 4690 done = false; 4691 type_decl = NULL_TREE; 4692 if (scope) 4693 { 4694 cp_parser_parse_tentatively (parser); 4695 type_decl = cp_parser_class_name (parser, 4696 /*typename_keyword_p=*/false, 4697 /*template_keyword_p=*/false, 4698 typename_type, 4699 /*check_dependency=*/false, 4700 /*class_head_p=*/false, 4701 declarator_p); 4702 if (cp_parser_parse_definitely (parser)) 4703 done = true; 4704 } 4705 /* In "N::S::~S", look in "N" as well. */ 4706 if (!done && scope && qualifying_scope) 4707 { 4708 cp_parser_parse_tentatively (parser); 4709 parser->scope = qualifying_scope; 4710 parser->object_scope = NULL_TREE; 4711 parser->qualifying_scope = NULL_TREE; 4712 type_decl 4713 = cp_parser_class_name (parser, 4714 /*typename_keyword_p=*/false, 4715 /*template_keyword_p=*/false, 4716 typename_type, 4717 /*check_dependency=*/false, 4718 /*class_head_p=*/false, 4719 declarator_p); 4720 if (cp_parser_parse_definitely (parser)) 4721 done = true; 4722 } 4723 /* In "p->S::~T", look in the scope given by "*p" as well. */ 4724 else if (!done && object_scope) 4725 { 4726 cp_parser_parse_tentatively (parser); 4727 parser->scope = object_scope; 4728 parser->object_scope = NULL_TREE; 4729 parser->qualifying_scope = NULL_TREE; 4730 type_decl 4731 = cp_parser_class_name (parser, 4732 /*typename_keyword_p=*/false, 4733 /*template_keyword_p=*/false, 4734 typename_type, 4735 /*check_dependency=*/false, 4736 /*class_head_p=*/false, 4737 declarator_p); 4738 if (cp_parser_parse_definitely (parser)) 4739 done = true; 4740 } 4741 /* Look in the surrounding context. */ 4742 if (!done) 4743 { 4744 parser->scope = NULL_TREE; 4745 parser->object_scope = NULL_TREE; 4746 parser->qualifying_scope = NULL_TREE; 4747 if (processing_template_decl) 4748 cp_parser_parse_tentatively (parser); 4749 type_decl 4750 = cp_parser_class_name (parser, 4751 /*typename_keyword_p=*/false, 4752 /*template_keyword_p=*/false, 4753 typename_type, 4754 /*check_dependency=*/false, 4755 /*class_head_p=*/false, 4756 declarator_p); 4757 if (processing_template_decl 4758 && ! cp_parser_parse_definitely (parser)) 4759 { 4760 /* We couldn't find a type with this name, so just accept 4761 it and check for a match at instantiation time. */ 4762 type_decl = cp_parser_identifier (parser); 4763 if (type_decl != error_mark_node) 4764 type_decl = build_nt (BIT_NOT_EXPR, type_decl); 4765 return type_decl; 4766 } 4767 } 4768 /* If an error occurred, assume that the name of the 4769 destructor is the same as the name of the qualifying 4770 class. That allows us to keep parsing after running 4771 into ill-formed destructor names. */ 4772 if (type_decl == error_mark_node && scope) 4773 return build_nt (BIT_NOT_EXPR, scope); 4774 else if (type_decl == error_mark_node) 4775 return error_mark_node; 4776 4777 /* Check that destructor name and scope match. */ 4778 if (declarator_p && scope && !check_dtor_name (scope, type_decl)) 4779 { 4780 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) 4781 error_at (token->location, 4782 "declaration of %<~%T%> as member of %qT", 4783 type_decl, scope); 4784 cp_parser_simulate_error (parser); 4785 return error_mark_node; 4786 } 4787 4788 /* [class.dtor] 4789 4790 A typedef-name that names a class shall not be used as the 4791 identifier in the declarator for a destructor declaration. */ 4792 if (declarator_p 4793 && !DECL_IMPLICIT_TYPEDEF_P (type_decl) 4794 && !DECL_SELF_REFERENCE_P (type_decl) 4795 && !cp_parser_uncommitted_to_tentative_parse_p (parser)) 4796 error_at (token->location, 4797 "typedef-name %qD used as destructor declarator", 4798 type_decl); 4799 4800 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl)); 4801 } 4802 4803 case CPP_KEYWORD: 4804 if (token->keyword == RID_OPERATOR) 4805 { 4806 tree id; 4807 4808 /* This could be a template-id, so we try that first. */ 4809 cp_parser_parse_tentatively (parser); 4810 /* Try a template-id. */ 4811 id = cp_parser_template_id (parser, template_keyword_p, 4812 /*check_dependency_p=*/true, 4813 declarator_p); 4814 /* If that worked, we're done. */ 4815 if (cp_parser_parse_definitely (parser)) 4816 return id; 4817 /* We still don't know whether we're looking at an 4818 operator-function-id or a conversion-function-id. */ 4819 cp_parser_parse_tentatively (parser); 4820 /* Try an operator-function-id. */ 4821 id = cp_parser_operator_function_id (parser); 4822 /* If that didn't work, try a conversion-function-id. */ 4823 if (!cp_parser_parse_definitely (parser)) 4824 id = cp_parser_conversion_function_id (parser); 4825 else if (UDLIT_OPER_P (id)) 4826 { 4827 /* 17.6.3.3.5 */ 4828 const char *name = UDLIT_OP_SUFFIX (id); 4829 if (name[0] != '_' && !in_system_header) 4830 warning (0, "literal operator suffixes not preceded by %<_%>" 4831 " are reserved for future standardization"); 4832 } 4833 4834 return id; 4835 } 4836 /* Fall through. */ 4837 4838 default: 4839 if (optional_p) 4840 return NULL_TREE; 4841 cp_parser_error (parser, "expected unqualified-id"); 4842 return error_mark_node; 4843 } 4844 } 4845 4846 /* Parse an (optional) nested-name-specifier. 4847 4848 nested-name-specifier: [C++98] 4849 class-or-namespace-name :: nested-name-specifier [opt] 4850 class-or-namespace-name :: template nested-name-specifier [opt] 4851 4852 nested-name-specifier: [C++0x] 4853 type-name :: 4854 namespace-name :: 4855 nested-name-specifier identifier :: 4856 nested-name-specifier template [opt] simple-template-id :: 4857 4858 PARSER->SCOPE should be set appropriately before this function is 4859 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in 4860 effect. TYPE_P is TRUE if we non-type bindings should be ignored 4861 in name lookups. 4862 4863 Sets PARSER->SCOPE to the class (TYPE) or namespace 4864 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves 4865 it unchanged if there is no nested-name-specifier. Returns the new 4866 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. 4867 4868 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be 4869 part of a declaration and/or decl-specifier. */ 4870 4871 static tree 4872 cp_parser_nested_name_specifier_opt (cp_parser *parser, 4873 bool typename_keyword_p, 4874 bool check_dependency_p, 4875 bool type_p, 4876 bool is_declaration) 4877 { 4878 bool success = false; 4879 cp_token_position start = 0; 4880 cp_token *token; 4881 4882 /* Remember where the nested-name-specifier starts. */ 4883 if (cp_parser_uncommitted_to_tentative_parse_p (parser)) 4884 { 4885 start = cp_lexer_token_position (parser->lexer, false); 4886 push_deferring_access_checks (dk_deferred); 4887 } 4888 4889 while (true) 4890 { 4891 tree new_scope; 4892 tree old_scope; 4893 tree saved_qualifying_scope; 4894 bool template_keyword_p; 4895 4896 /* Spot cases that cannot be the beginning of a 4897 nested-name-specifier. */ 4898 token = cp_lexer_peek_token (parser->lexer); 4899 4900 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process 4901 the already parsed nested-name-specifier. */ 4902 if (token->type == CPP_NESTED_NAME_SPECIFIER) 4903 { 4904 /* Grab the nested-name-specifier and continue the loop. */ 4905 cp_parser_pre_parsed_nested_name_specifier (parser); 4906 /* If we originally encountered this nested-name-specifier 4907 with IS_DECLARATION set to false, we will not have 4908 resolved TYPENAME_TYPEs, so we must do so here. */ 4909 if (is_declaration 4910 && TREE_CODE (parser->scope) == TYPENAME_TYPE) 4911 { 4912 new_scope = resolve_typename_type (parser->scope, 4913 /*only_current_p=*/false); 4914 if (TREE_CODE (new_scope) != TYPENAME_TYPE) 4915 parser->scope = new_scope; 4916 } 4917 success = true; 4918 continue; 4919 } 4920 4921 /* Spot cases that cannot be the beginning of a 4922 nested-name-specifier. On the second and subsequent times 4923 through the loop, we look for the `template' keyword. */ 4924 if (success && token->keyword == RID_TEMPLATE) 4925 ; 4926 /* A template-id can start a nested-name-specifier. */ 4927 else if (token->type == CPP_TEMPLATE_ID) 4928 ; 4929 /* DR 743: decltype can be used in a nested-name-specifier. */ 4930 else if (token_is_decltype (token)) 4931 ; 4932 else 4933 { 4934 /* If the next token is not an identifier, then it is 4935 definitely not a type-name or namespace-name. */ 4936 if (token->type != CPP_NAME) 4937 break; 4938 /* If the following token is neither a `<' (to begin a 4939 template-id), nor a `::', then we are not looking at a 4940 nested-name-specifier. */ 4941 token = cp_lexer_peek_nth_token (parser->lexer, 2); 4942 4943 if (token->type == CPP_COLON 4944 && parser->colon_corrects_to_scope_p 4945 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME) 4946 { 4947 error_at (token->location, 4948 "found %<:%> in nested-name-specifier, expected %<::%>"); 4949 token->type = CPP_SCOPE; 4950 } 4951 4952 if (token->type != CPP_SCOPE 4953 && !cp_parser_nth_token_starts_template_argument_list_p 4954 (parser, 2)) 4955 break; 4956 } 4957 4958 /* The nested-name-specifier is optional, so we parse 4959 tentatively. */ 4960 cp_parser_parse_tentatively (parser); 4961 4962 /* Look for the optional `template' keyword, if this isn't the 4963 first time through the loop. */ 4964 if (success) 4965 template_keyword_p = cp_parser_optional_template_keyword (parser); 4966 else 4967 template_keyword_p = false; 4968 4969 /* Save the old scope since the name lookup we are about to do 4970 might destroy it. */ 4971 old_scope = parser->scope; 4972 saved_qualifying_scope = parser->qualifying_scope; 4973 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to 4974 look up names in "X<T>::I" in order to determine that "Y" is 4975 a template. So, if we have a typename at this point, we make 4976 an effort to look through it. */ 4977 if (is_declaration 4978 && !typename_keyword_p 4979 && parser->scope 4980 && TREE_CODE (parser->scope) == TYPENAME_TYPE) 4981 parser->scope = resolve_typename_type (parser->scope, 4982 /*only_current_p=*/false); 4983 /* Parse the qualifying entity. */ 4984 new_scope 4985 = cp_parser_qualifying_entity (parser, 4986 typename_keyword_p, 4987 template_keyword_p, 4988 check_dependency_p, 4989 type_p, 4990 is_declaration); 4991 /* Look for the `::' token. */ 4992 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE); 4993 4994 /* If we found what we wanted, we keep going; otherwise, we're 4995 done. */ 4996 if (!cp_parser_parse_definitely (parser)) 4997 { 4998 bool error_p = false; 4999 5000 /* Restore the OLD_SCOPE since it was valid before the 5001 failed attempt at finding the last 5002 class-or-namespace-name. */ 5003 parser->scope = old_scope; 5004 parser->qualifying_scope = saved_qualifying_scope; 5005 5006 /* If the next token is a decltype, and the one after that is a 5007 `::', then the decltype has failed to resolve to a class or 5008 enumeration type. Give this error even when parsing 5009 tentatively since it can't possibly be valid--and we're going 5010 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we 5011 won't get another chance.*/ 5012 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE) 5013 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 5014 == CPP_SCOPE)) 5015 { 5016 token = cp_lexer_consume_token (parser->lexer); 5017 error_at (token->location, "decltype evaluates to %qT, " 5018 "which is not a class or enumeration type", 5019 token->u.value); 5020 parser->scope = error_mark_node; 5021 error_p = true; 5022 /* As below. */ 5023 success = true; 5024 cp_lexer_consume_token (parser->lexer); 5025 } 5026 5027 if (cp_parser_uncommitted_to_tentative_parse_p (parser)) 5028 break; 5029 /* If the next token is an identifier, and the one after 5030 that is a `::', then any valid interpretation would have 5031 found a class-or-namespace-name. */ 5032 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME) 5033 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 5034 == CPP_SCOPE) 5035 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 5036 != CPP_COMPL)) 5037 { 5038 token = cp_lexer_consume_token (parser->lexer); 5039 if (!error_p) 5040 { 5041 if (!token->ambiguous_p) 5042 { 5043 tree decl; 5044 tree ambiguous_decls; 5045 5046 decl = cp_parser_lookup_name (parser, token->u.value, 5047 none_type, 5048 /*is_template=*/false, 5049 /*is_namespace=*/false, 5050 /*check_dependency=*/true, 5051 &ambiguous_decls, 5052 token->location); 5053 if (TREE_CODE (decl) == TEMPLATE_DECL) 5054 error_at (token->location, 5055 "%qD used without template parameters", 5056 decl); 5057 else if (ambiguous_decls) 5058 { 5059 error_at (token->location, 5060 "reference to %qD is ambiguous", 5061 token->u.value); 5062 print_candidates (ambiguous_decls); 5063 decl = error_mark_node; 5064 } 5065 else 5066 { 5067 if (cxx_dialect != cxx98) 5068 cp_parser_name_lookup_error 5069 (parser, token->u.value, decl, NLE_NOT_CXX98, 5070 token->location); 5071 else 5072 cp_parser_name_lookup_error 5073 (parser, token->u.value, decl, NLE_CXX98, 5074 token->location); 5075 } 5076 } 5077 parser->scope = error_mark_node; 5078 error_p = true; 5079 /* Treat this as a successful nested-name-specifier 5080 due to: 5081 5082 [basic.lookup.qual] 5083 5084 If the name found is not a class-name (clause 5085 _class_) or namespace-name (_namespace.def_), the 5086 program is ill-formed. */ 5087 success = true; 5088 } 5089 cp_lexer_consume_token (parser->lexer); 5090 } 5091 break; 5092 } 5093 /* We've found one valid nested-name-specifier. */ 5094 success = true; 5095 /* Name lookup always gives us a DECL. */ 5096 if (TREE_CODE (new_scope) == TYPE_DECL) 5097 new_scope = TREE_TYPE (new_scope); 5098 /* Uses of "template" must be followed by actual templates. */ 5099 if (template_keyword_p 5100 && !(CLASS_TYPE_P (new_scope) 5101 && ((CLASSTYPE_USE_TEMPLATE (new_scope) 5102 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope))) 5103 || CLASSTYPE_IS_TEMPLATE (new_scope))) 5104 && !(TREE_CODE (new_scope) == TYPENAME_TYPE 5105 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope)) 5106 == TEMPLATE_ID_EXPR))) 5107 permerror (input_location, TYPE_P (new_scope) 5108 ? G_("%qT is not a template") 5109 : G_("%qD is not a template"), 5110 new_scope); 5111 /* If it is a class scope, try to complete it; we are about to 5112 be looking up names inside the class. */ 5113 if (TYPE_P (new_scope) 5114 /* Since checking types for dependency can be expensive, 5115 avoid doing it if the type is already complete. */ 5116 && !COMPLETE_TYPE_P (new_scope) 5117 /* Do not try to complete dependent types. */ 5118 && !dependent_type_p (new_scope)) 5119 { 5120 new_scope = complete_type (new_scope); 5121 /* If it is a typedef to current class, use the current 5122 class instead, as the typedef won't have any names inside 5123 it yet. */ 5124 if (!COMPLETE_TYPE_P (new_scope) 5125 && currently_open_class (new_scope)) 5126 new_scope = TYPE_MAIN_VARIANT (new_scope); 5127 } 5128 /* Make sure we look in the right scope the next time through 5129 the loop. */ 5130 parser->scope = new_scope; 5131 } 5132 5133 /* If parsing tentatively, replace the sequence of tokens that makes 5134 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER 5135 token. That way, should we re-parse the token stream, we will 5136 not have to repeat the effort required to do the parse, nor will 5137 we issue duplicate error messages. */ 5138 if (success && start) 5139 { 5140 cp_token *token; 5141 5142 token = cp_lexer_token_at (parser->lexer, start); 5143 /* Reset the contents of the START token. */ 5144 token->type = CPP_NESTED_NAME_SPECIFIER; 5145 /* Retrieve any deferred checks. Do not pop this access checks yet 5146 so the memory will not be reclaimed during token replacing below. */ 5147 token->u.tree_check_value = ggc_alloc_cleared_tree_check (); 5148 token->u.tree_check_value->value = parser->scope; 5149 token->u.tree_check_value->checks = get_deferred_access_checks (); 5150 token->u.tree_check_value->qualifying_scope = 5151 parser->qualifying_scope; 5152 token->keyword = RID_MAX; 5153 5154 /* Purge all subsequent tokens. */ 5155 cp_lexer_purge_tokens_after (parser->lexer, start); 5156 } 5157 5158 if (start) 5159 pop_to_parent_deferring_access_checks (); 5160 5161 return success ? parser->scope : NULL_TREE; 5162 } 5163 5164 /* Parse a nested-name-specifier. See 5165 cp_parser_nested_name_specifier_opt for details. This function 5166 behaves identically, except that it will an issue an error if no 5167 nested-name-specifier is present. */ 5168 5169 static tree 5170 cp_parser_nested_name_specifier (cp_parser *parser, 5171 bool typename_keyword_p, 5172 bool check_dependency_p, 5173 bool type_p, 5174 bool is_declaration) 5175 { 5176 tree scope; 5177 5178 /* Look for the nested-name-specifier. */ 5179 scope = cp_parser_nested_name_specifier_opt (parser, 5180 typename_keyword_p, 5181 check_dependency_p, 5182 type_p, 5183 is_declaration); 5184 /* If it was not present, issue an error message. */ 5185 if (!scope) 5186 { 5187 cp_parser_error (parser, "expected nested-name-specifier"); 5188 parser->scope = NULL_TREE; 5189 } 5190 5191 return scope; 5192 } 5193 5194 /* Parse the qualifying entity in a nested-name-specifier. For C++98, 5195 this is either a class-name or a namespace-name (which corresponds 5196 to the class-or-namespace-name production in the grammar). For 5197 C++0x, it can also be a type-name that refers to an enumeration 5198 type or a simple-template-id. 5199 5200 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect. 5201 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect. 5202 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up. 5203 TYPE_P is TRUE iff the next name should be taken as a class-name, 5204 even the same name is declared to be another entity in the same 5205 scope. 5206 5207 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL) 5208 specified by the class-or-namespace-name. If neither is found the 5209 ERROR_MARK_NODE is returned. */ 5210 5211 static tree 5212 cp_parser_qualifying_entity (cp_parser *parser, 5213 bool typename_keyword_p, 5214 bool template_keyword_p, 5215 bool check_dependency_p, 5216 bool type_p, 5217 bool is_declaration) 5218 { 5219 tree saved_scope; 5220 tree saved_qualifying_scope; 5221 tree saved_object_scope; 5222 tree scope; 5223 bool only_class_p; 5224 bool successful_parse_p; 5225 5226 /* DR 743: decltype can appear in a nested-name-specifier. */ 5227 if (cp_lexer_next_token_is_decltype (parser->lexer)) 5228 { 5229 scope = cp_parser_decltype (parser); 5230 if (TREE_CODE (scope) != ENUMERAL_TYPE 5231 && !MAYBE_CLASS_TYPE_P (scope)) 5232 { 5233 cp_parser_simulate_error (parser); 5234 return error_mark_node; 5235 } 5236 if (TYPE_NAME (scope)) 5237 scope = TYPE_NAME (scope); 5238 return scope; 5239 } 5240 5241 /* Before we try to parse the class-name, we must save away the 5242 current PARSER->SCOPE since cp_parser_class_name will destroy 5243 it. */ 5244 saved_scope = parser->scope; 5245 saved_qualifying_scope = parser->qualifying_scope; 5246 saved_object_scope = parser->object_scope; 5247 /* Try for a class-name first. If the SAVED_SCOPE is a type, then 5248 there is no need to look for a namespace-name. */ 5249 only_class_p = template_keyword_p 5250 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98); 5251 if (!only_class_p) 5252 cp_parser_parse_tentatively (parser); 5253 scope = cp_parser_class_name (parser, 5254 typename_keyword_p, 5255 template_keyword_p, 5256 type_p ? class_type : none_type, 5257 check_dependency_p, 5258 /*class_head_p=*/false, 5259 is_declaration); 5260 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser); 5261 /* If that didn't work and we're in C++0x mode, try for a type-name. */ 5262 if (!only_class_p 5263 && cxx_dialect != cxx98 5264 && !successful_parse_p) 5265 { 5266 /* Restore the saved scope. */ 5267 parser->scope = saved_scope; 5268 parser->qualifying_scope = saved_qualifying_scope; 5269 parser->object_scope = saved_object_scope; 5270 5271 /* Parse tentatively. */ 5272 cp_parser_parse_tentatively (parser); 5273 5274 /* Parse a type-name */ 5275 scope = cp_parser_type_name (parser); 5276 5277 /* "If the name found does not designate a namespace or a class, 5278 enumeration, or dependent type, the program is ill-formed." 5279 5280 We cover classes and dependent types above and namespaces below, 5281 so this code is only looking for enums. */ 5282 if (!scope || TREE_CODE (scope) != TYPE_DECL 5283 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE) 5284 cp_parser_simulate_error (parser); 5285 5286 successful_parse_p = cp_parser_parse_definitely (parser); 5287 } 5288 /* If that didn't work, try for a namespace-name. */ 5289 if (!only_class_p && !successful_parse_p) 5290 { 5291 /* Restore the saved scope. */ 5292 parser->scope = saved_scope; 5293 parser->qualifying_scope = saved_qualifying_scope; 5294 parser->object_scope = saved_object_scope; 5295 /* If we are not looking at an identifier followed by the scope 5296 resolution operator, then this is not part of a 5297 nested-name-specifier. (Note that this function is only used 5298 to parse the components of a nested-name-specifier.) */ 5299 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME) 5300 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) 5301 return error_mark_node; 5302 scope = cp_parser_namespace_name (parser); 5303 } 5304 5305 return scope; 5306 } 5307 5308 /* Parse a postfix-expression. 5309 5310 postfix-expression: 5311 primary-expression 5312 postfix-expression [ expression ] 5313 postfix-expression ( expression-list [opt] ) 5314 simple-type-specifier ( expression-list [opt] ) 5315 typename :: [opt] nested-name-specifier identifier 5316 ( expression-list [opt] ) 5317 typename :: [opt] nested-name-specifier template [opt] template-id 5318 ( expression-list [opt] ) 5319 postfix-expression . template [opt] id-expression 5320 postfix-expression -> template [opt] id-expression 5321 postfix-expression . pseudo-destructor-name 5322 postfix-expression -> pseudo-destructor-name 5323 postfix-expression ++ 5324 postfix-expression -- 5325 dynamic_cast < type-id > ( expression ) 5326 static_cast < type-id > ( expression ) 5327 reinterpret_cast < type-id > ( expression ) 5328 const_cast < type-id > ( expression ) 5329 typeid ( expression ) 5330 typeid ( type-id ) 5331 5332 GNU Extension: 5333 5334 postfix-expression: 5335 ( type-id ) { initializer-list , [opt] } 5336 5337 This extension is a GNU version of the C99 compound-literal 5338 construct. (The C99 grammar uses `type-name' instead of `type-id', 5339 but they are essentially the same concept.) 5340 5341 If ADDRESS_P is true, the postfix expression is the operand of the 5342 `&' operator. CAST_P is true if this expression is the target of a 5343 cast. 5344 5345 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are 5346 class member access expressions [expr.ref]. 5347 5348 Returns a representation of the expression. */ 5349 5350 static tree 5351 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p, 5352 bool member_access_only_p, 5353 cp_id_kind * pidk_return) 5354 { 5355 cp_token *token; 5356 enum rid keyword; 5357 cp_id_kind idk = CP_ID_KIND_NONE; 5358 tree postfix_expression = NULL_TREE; 5359 bool is_member_access = false; 5360 5361 /* Peek at the next token. */ 5362 token = cp_lexer_peek_token (parser->lexer); 5363 /* Some of the productions are determined by keywords. */ 5364 keyword = token->keyword; 5365 switch (keyword) 5366 { 5367 case RID_DYNCAST: 5368 case RID_STATCAST: 5369 case RID_REINTCAST: 5370 case RID_CONSTCAST: 5371 { 5372 tree type; 5373 tree expression; 5374 const char *saved_message; 5375 5376 /* All of these can be handled in the same way from the point 5377 of view of parsing. Begin by consuming the token 5378 identifying the cast. */ 5379 cp_lexer_consume_token (parser->lexer); 5380 5381 /* New types cannot be defined in the cast. */ 5382 saved_message = parser->type_definition_forbidden_message; 5383 parser->type_definition_forbidden_message 5384 = G_("types may not be defined in casts"); 5385 5386 /* Look for the opening `<'. */ 5387 cp_parser_require (parser, CPP_LESS, RT_LESS); 5388 /* Parse the type to which we are casting. */ 5389 type = cp_parser_type_id (parser); 5390 /* Look for the closing `>'. */ 5391 cp_parser_require (parser, CPP_GREATER, RT_GREATER); 5392 /* Restore the old message. */ 5393 parser->type_definition_forbidden_message = saved_message; 5394 5395 /* And the expression which is being cast. */ 5396 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 5397 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk); 5398 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 5399 5400 /* Only type conversions to integral or enumeration types 5401 can be used in constant-expressions. */ 5402 if (!cast_valid_in_integral_constant_expression_p (type) 5403 && cp_parser_non_integral_constant_expression (parser, NIC_CAST)) 5404 return error_mark_node; 5405 5406 switch (keyword) 5407 { 5408 case RID_DYNCAST: 5409 postfix_expression 5410 = build_dynamic_cast (type, expression, tf_warning_or_error); 5411 break; 5412 case RID_STATCAST: 5413 postfix_expression 5414 = build_static_cast (type, expression, tf_warning_or_error); 5415 break; 5416 case RID_REINTCAST: 5417 postfix_expression 5418 = build_reinterpret_cast (type, expression, 5419 tf_warning_or_error); 5420 break; 5421 case RID_CONSTCAST: 5422 postfix_expression 5423 = build_const_cast (type, expression, tf_warning_or_error); 5424 break; 5425 default: 5426 gcc_unreachable (); 5427 } 5428 } 5429 break; 5430 5431 case RID_TYPEID: 5432 { 5433 tree type; 5434 const char *saved_message; 5435 bool saved_in_type_id_in_expr_p; 5436 5437 /* Consume the `typeid' token. */ 5438 cp_lexer_consume_token (parser->lexer); 5439 /* Look for the `(' token. */ 5440 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 5441 /* Types cannot be defined in a `typeid' expression. */ 5442 saved_message = parser->type_definition_forbidden_message; 5443 parser->type_definition_forbidden_message 5444 = G_("types may not be defined in a %<typeid%> expression"); 5445 /* We can't be sure yet whether we're looking at a type-id or an 5446 expression. */ 5447 cp_parser_parse_tentatively (parser); 5448 /* Try a type-id first. */ 5449 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; 5450 parser->in_type_id_in_expr_p = true; 5451 type = cp_parser_type_id (parser); 5452 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; 5453 /* Look for the `)' token. Otherwise, we can't be sure that 5454 we're not looking at an expression: consider `typeid (int 5455 (3))', for example. */ 5456 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 5457 /* If all went well, simply lookup the type-id. */ 5458 if (cp_parser_parse_definitely (parser)) 5459 postfix_expression = get_typeid (type); 5460 /* Otherwise, fall back to the expression variant. */ 5461 else 5462 { 5463 tree expression; 5464 5465 /* Look for an expression. */ 5466 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk); 5467 /* Compute its typeid. */ 5468 postfix_expression = build_typeid (expression); 5469 /* Look for the `)' token. */ 5470 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 5471 } 5472 /* Restore the saved message. */ 5473 parser->type_definition_forbidden_message = saved_message; 5474 /* `typeid' may not appear in an integral constant expression. */ 5475 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID)) 5476 return error_mark_node; 5477 } 5478 break; 5479 5480 case RID_TYPENAME: 5481 { 5482 tree type; 5483 /* The syntax permitted here is the same permitted for an 5484 elaborated-type-specifier. */ 5485 type = cp_parser_elaborated_type_specifier (parser, 5486 /*is_friend=*/false, 5487 /*is_declaration=*/false); 5488 postfix_expression = cp_parser_functional_cast (parser, type); 5489 } 5490 break; 5491 5492 default: 5493 { 5494 tree type; 5495 5496 /* If the next thing is a simple-type-specifier, we may be 5497 looking at a functional cast. We could also be looking at 5498 an id-expression. So, we try the functional cast, and if 5499 that doesn't work we fall back to the primary-expression. */ 5500 cp_parser_parse_tentatively (parser); 5501 /* Look for the simple-type-specifier. */ 5502 type = cp_parser_simple_type_specifier (parser, 5503 /*decl_specs=*/NULL, 5504 CP_PARSER_FLAGS_NONE); 5505 /* Parse the cast itself. */ 5506 if (!cp_parser_error_occurred (parser)) 5507 postfix_expression 5508 = cp_parser_functional_cast (parser, type); 5509 /* If that worked, we're done. */ 5510 if (cp_parser_parse_definitely (parser)) 5511 break; 5512 5513 /* If the functional-cast didn't work out, try a 5514 compound-literal. */ 5515 if (cp_parser_allow_gnu_extensions_p (parser) 5516 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 5517 { 5518 VEC(constructor_elt,gc) *initializer_list = NULL; 5519 bool saved_in_type_id_in_expr_p; 5520 5521 cp_parser_parse_tentatively (parser); 5522 /* Consume the `('. */ 5523 cp_lexer_consume_token (parser->lexer); 5524 /* Parse the type. */ 5525 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; 5526 parser->in_type_id_in_expr_p = true; 5527 type = cp_parser_type_id (parser); 5528 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; 5529 /* Look for the `)'. */ 5530 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 5531 /* Look for the `{'. */ 5532 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE); 5533 /* If things aren't going well, there's no need to 5534 keep going. */ 5535 if (!cp_parser_error_occurred (parser)) 5536 { 5537 bool non_constant_p; 5538 /* Parse the initializer-list. */ 5539 initializer_list 5540 = cp_parser_initializer_list (parser, &non_constant_p); 5541 /* Allow a trailing `,'. */ 5542 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 5543 cp_lexer_consume_token (parser->lexer); 5544 /* Look for the final `}'. */ 5545 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 5546 } 5547 /* If that worked, we're definitely looking at a 5548 compound-literal expression. */ 5549 if (cp_parser_parse_definitely (parser)) 5550 { 5551 /* Warn the user that a compound literal is not 5552 allowed in standard C++. */ 5553 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals"); 5554 /* For simplicity, we disallow compound literals in 5555 constant-expressions. We could 5556 allow compound literals of integer type, whose 5557 initializer was a constant, in constant 5558 expressions. Permitting that usage, as a further 5559 extension, would not change the meaning of any 5560 currently accepted programs. (Of course, as 5561 compound literals are not part of ISO C++, the 5562 standard has nothing to say.) */ 5563 if (cp_parser_non_integral_constant_expression (parser, 5564 NIC_NCC)) 5565 { 5566 postfix_expression = error_mark_node; 5567 break; 5568 } 5569 /* Form the representation of the compound-literal. */ 5570 postfix_expression 5571 = (finish_compound_literal 5572 (type, build_constructor (init_list_type_node, 5573 initializer_list), 5574 tf_warning_or_error)); 5575 break; 5576 } 5577 } 5578 5579 /* It must be a primary-expression. */ 5580 postfix_expression 5581 = cp_parser_primary_expression (parser, address_p, cast_p, 5582 /*template_arg_p=*/false, 5583 &idk); 5584 } 5585 break; 5586 } 5587 5588 /* Keep looping until the postfix-expression is complete. */ 5589 while (true) 5590 { 5591 if (idk == CP_ID_KIND_UNQUALIFIED 5592 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE 5593 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) 5594 /* It is not a Koenig lookup function call. */ 5595 postfix_expression 5596 = unqualified_name_lookup_error (postfix_expression); 5597 5598 /* Peek at the next token. */ 5599 token = cp_lexer_peek_token (parser->lexer); 5600 5601 switch (token->type) 5602 { 5603 case CPP_OPEN_SQUARE: 5604 postfix_expression 5605 = cp_parser_postfix_open_square_expression (parser, 5606 postfix_expression, 5607 false); 5608 idk = CP_ID_KIND_NONE; 5609 is_member_access = false; 5610 break; 5611 5612 case CPP_OPEN_PAREN: 5613 /* postfix-expression ( expression-list [opt] ) */ 5614 { 5615 bool koenig_p; 5616 bool is_builtin_constant_p; 5617 bool saved_integral_constant_expression_p = false; 5618 bool saved_non_integral_constant_expression_p = false; 5619 VEC(tree,gc) *args; 5620 5621 is_member_access = false; 5622 5623 is_builtin_constant_p 5624 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression); 5625 if (is_builtin_constant_p) 5626 { 5627 /* The whole point of __builtin_constant_p is to allow 5628 non-constant expressions to appear as arguments. */ 5629 saved_integral_constant_expression_p 5630 = parser->integral_constant_expression_p; 5631 saved_non_integral_constant_expression_p 5632 = parser->non_integral_constant_expression_p; 5633 parser->integral_constant_expression_p = false; 5634 } 5635 args = (cp_parser_parenthesized_expression_list 5636 (parser, non_attr, 5637 /*cast_p=*/false, /*allow_expansion_p=*/true, 5638 /*non_constant_p=*/NULL)); 5639 if (is_builtin_constant_p) 5640 { 5641 parser->integral_constant_expression_p 5642 = saved_integral_constant_expression_p; 5643 parser->non_integral_constant_expression_p 5644 = saved_non_integral_constant_expression_p; 5645 } 5646 5647 if (args == NULL) 5648 { 5649 postfix_expression = error_mark_node; 5650 break; 5651 } 5652 5653 /* Function calls are not permitted in 5654 constant-expressions. */ 5655 if (! builtin_valid_in_constant_expr_p (postfix_expression) 5656 && cp_parser_non_integral_constant_expression (parser, 5657 NIC_FUNC_CALL)) 5658 { 5659 postfix_expression = error_mark_node; 5660 release_tree_vector (args); 5661 break; 5662 } 5663 5664 koenig_p = false; 5665 if (idk == CP_ID_KIND_UNQUALIFIED 5666 || idk == CP_ID_KIND_TEMPLATE_ID) 5667 { 5668 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE) 5669 { 5670 if (!VEC_empty (tree, args)) 5671 { 5672 koenig_p = true; 5673 if (!any_type_dependent_arguments_p (args)) 5674 postfix_expression 5675 = perform_koenig_lookup (postfix_expression, args, 5676 /*include_std=*/false, 5677 tf_warning_or_error); 5678 } 5679 else 5680 postfix_expression 5681 = unqualified_fn_lookup_error (postfix_expression); 5682 } 5683 /* We do not perform argument-dependent lookup if 5684 normal lookup finds a non-function, in accordance 5685 with the expected resolution of DR 218. */ 5686 else if (!VEC_empty (tree, args) 5687 && is_overloaded_fn (postfix_expression)) 5688 { 5689 tree fn = get_first_fn (postfix_expression); 5690 fn = STRIP_TEMPLATE (fn); 5691 5692 /* Do not do argument dependent lookup if regular 5693 lookup finds a member function or a block-scope 5694 function declaration. [basic.lookup.argdep]/3 */ 5695 if (!DECL_FUNCTION_MEMBER_P (fn) 5696 && !DECL_LOCAL_FUNCTION_P (fn)) 5697 { 5698 koenig_p = true; 5699 if (!any_type_dependent_arguments_p (args)) 5700 postfix_expression 5701 = perform_koenig_lookup (postfix_expression, args, 5702 /*include_std=*/false, 5703 tf_warning_or_error); 5704 } 5705 } 5706 } 5707 5708 if (TREE_CODE (postfix_expression) == COMPONENT_REF) 5709 { 5710 tree instance = TREE_OPERAND (postfix_expression, 0); 5711 tree fn = TREE_OPERAND (postfix_expression, 1); 5712 5713 if (processing_template_decl 5714 && (type_dependent_expression_p (instance) 5715 || (!BASELINK_P (fn) 5716 && TREE_CODE (fn) != FIELD_DECL) 5717 || type_dependent_expression_p (fn) 5718 || any_type_dependent_arguments_p (args))) 5719 { 5720 postfix_expression 5721 = build_nt_call_vec (postfix_expression, args); 5722 release_tree_vector (args); 5723 break; 5724 } 5725 5726 if (BASELINK_P (fn)) 5727 { 5728 postfix_expression 5729 = (build_new_method_call 5730 (instance, fn, &args, NULL_TREE, 5731 (idk == CP_ID_KIND_QUALIFIED 5732 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL 5733 : LOOKUP_NORMAL), 5734 /*fn_p=*/NULL, 5735 tf_warning_or_error)); 5736 } 5737 else 5738 postfix_expression 5739 = finish_call_expr (postfix_expression, &args, 5740 /*disallow_virtual=*/false, 5741 /*koenig_p=*/false, 5742 tf_warning_or_error); 5743 } 5744 else if (TREE_CODE (postfix_expression) == OFFSET_REF 5745 || TREE_CODE (postfix_expression) == MEMBER_REF 5746 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR) 5747 postfix_expression = (build_offset_ref_call_from_tree 5748 (postfix_expression, &args)); 5749 else if (idk == CP_ID_KIND_QUALIFIED) 5750 /* A call to a static class member, or a namespace-scope 5751 function. */ 5752 postfix_expression 5753 = finish_call_expr (postfix_expression, &args, 5754 /*disallow_virtual=*/true, 5755 koenig_p, 5756 tf_warning_or_error); 5757 else 5758 /* All other function calls. */ 5759 postfix_expression 5760 = finish_call_expr (postfix_expression, &args, 5761 /*disallow_virtual=*/false, 5762 koenig_p, 5763 tf_warning_or_error); 5764 5765 /* The POSTFIX_EXPRESSION is certainly no longer an id. */ 5766 idk = CP_ID_KIND_NONE; 5767 5768 release_tree_vector (args); 5769 } 5770 break; 5771 5772 case CPP_DOT: 5773 case CPP_DEREF: 5774 /* postfix-expression . template [opt] id-expression 5775 postfix-expression . pseudo-destructor-name 5776 postfix-expression -> template [opt] id-expression 5777 postfix-expression -> pseudo-destructor-name */ 5778 5779 /* Consume the `.' or `->' operator. */ 5780 cp_lexer_consume_token (parser->lexer); 5781 5782 postfix_expression 5783 = cp_parser_postfix_dot_deref_expression (parser, token->type, 5784 postfix_expression, 5785 false, &idk, 5786 token->location); 5787 5788 is_member_access = true; 5789 break; 5790 5791 case CPP_PLUS_PLUS: 5792 /* postfix-expression ++ */ 5793 /* Consume the `++' token. */ 5794 cp_lexer_consume_token (parser->lexer); 5795 /* Generate a representation for the complete expression. */ 5796 postfix_expression 5797 = finish_increment_expr (postfix_expression, 5798 POSTINCREMENT_EXPR); 5799 /* Increments may not appear in constant-expressions. */ 5800 if (cp_parser_non_integral_constant_expression (parser, NIC_INC)) 5801 postfix_expression = error_mark_node; 5802 idk = CP_ID_KIND_NONE; 5803 is_member_access = false; 5804 break; 5805 5806 case CPP_MINUS_MINUS: 5807 /* postfix-expression -- */ 5808 /* Consume the `--' token. */ 5809 cp_lexer_consume_token (parser->lexer); 5810 /* Generate a representation for the complete expression. */ 5811 postfix_expression 5812 = finish_increment_expr (postfix_expression, 5813 POSTDECREMENT_EXPR); 5814 /* Decrements may not appear in constant-expressions. */ 5815 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC)) 5816 postfix_expression = error_mark_node; 5817 idk = CP_ID_KIND_NONE; 5818 is_member_access = false; 5819 break; 5820 5821 default: 5822 if (pidk_return != NULL) 5823 * pidk_return = idk; 5824 if (member_access_only_p) 5825 return is_member_access? postfix_expression : error_mark_node; 5826 else 5827 return postfix_expression; 5828 } 5829 } 5830 5831 /* We should never get here. */ 5832 gcc_unreachable (); 5833 return error_mark_node; 5834 } 5835 5836 /* A subroutine of cp_parser_postfix_expression that also gets hijacked 5837 by cp_parser_builtin_offsetof. We're looking for 5838 5839 postfix-expression [ expression ] 5840 postfix-expression [ braced-init-list ] (C++11) 5841 5842 FOR_OFFSETOF is set if we're being called in that context, which 5843 changes how we deal with integer constant expressions. */ 5844 5845 static tree 5846 cp_parser_postfix_open_square_expression (cp_parser *parser, 5847 tree postfix_expression, 5848 bool for_offsetof) 5849 { 5850 tree index; 5851 5852 /* Consume the `[' token. */ 5853 cp_lexer_consume_token (parser->lexer); 5854 5855 /* Parse the index expression. */ 5856 /* ??? For offsetof, there is a question of what to allow here. If 5857 offsetof is not being used in an integral constant expression context, 5858 then we *could* get the right answer by computing the value at runtime. 5859 If we are in an integral constant expression context, then we might 5860 could accept any constant expression; hard to say without analysis. 5861 Rather than open the barn door too wide right away, allow only integer 5862 constant expressions here. */ 5863 if (for_offsetof) 5864 index = cp_parser_constant_expression (parser, false, NULL); 5865 else 5866 { 5867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 5868 { 5869 bool expr_nonconst_p; 5870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 5871 index = cp_parser_braced_list (parser, &expr_nonconst_p); 5872 } 5873 else 5874 index = cp_parser_expression (parser, /*cast_p=*/false, NULL); 5875 } 5876 5877 /* Look for the closing `]'. */ 5878 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 5879 5880 /* Build the ARRAY_REF. */ 5881 postfix_expression = grok_array_decl (postfix_expression, index); 5882 5883 /* When not doing offsetof, array references are not permitted in 5884 constant-expressions. */ 5885 if (!for_offsetof 5886 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF))) 5887 postfix_expression = error_mark_node; 5888 5889 return postfix_expression; 5890 } 5891 5892 /* A subroutine of cp_parser_postfix_expression that also gets hijacked 5893 by cp_parser_builtin_offsetof. We're looking for 5894 5895 postfix-expression . template [opt] id-expression 5896 postfix-expression . pseudo-destructor-name 5897 postfix-expression -> template [opt] id-expression 5898 postfix-expression -> pseudo-destructor-name 5899 5900 FOR_OFFSETOF is set if we're being called in that context. That sorta 5901 limits what of the above we'll actually accept, but nevermind. 5902 TOKEN_TYPE is the "." or "->" token, which will already have been 5903 removed from the stream. */ 5904 5905 static tree 5906 cp_parser_postfix_dot_deref_expression (cp_parser *parser, 5907 enum cpp_ttype token_type, 5908 tree postfix_expression, 5909 bool for_offsetof, cp_id_kind *idk, 5910 location_t location) 5911 { 5912 tree name; 5913 bool dependent_p; 5914 bool pseudo_destructor_p; 5915 tree scope = NULL_TREE; 5916 5917 /* If this is a `->' operator, dereference the pointer. */ 5918 if (token_type == CPP_DEREF) 5919 postfix_expression = build_x_arrow (postfix_expression); 5920 /* Check to see whether or not the expression is type-dependent. */ 5921 dependent_p = type_dependent_expression_p (postfix_expression); 5922 /* The identifier following the `->' or `.' is not qualified. */ 5923 parser->scope = NULL_TREE; 5924 parser->qualifying_scope = NULL_TREE; 5925 parser->object_scope = NULL_TREE; 5926 *idk = CP_ID_KIND_NONE; 5927 5928 /* Enter the scope corresponding to the type of the object 5929 given by the POSTFIX_EXPRESSION. */ 5930 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE) 5931 { 5932 scope = TREE_TYPE (postfix_expression); 5933 /* According to the standard, no expression should ever have 5934 reference type. Unfortunately, we do not currently match 5935 the standard in this respect in that our internal representation 5936 of an expression may have reference type even when the standard 5937 says it does not. Therefore, we have to manually obtain the 5938 underlying type here. */ 5939 scope = non_reference (scope); 5940 /* The type of the POSTFIX_EXPRESSION must be complete. */ 5941 if (scope == unknown_type_node) 5942 { 5943 error_at (location, "%qE does not have class type", 5944 postfix_expression); 5945 scope = NULL_TREE; 5946 } 5947 /* Unlike the object expression in other contexts, *this is not 5948 required to be of complete type for purposes of class member 5949 access (5.2.5) outside the member function body. */ 5950 else if (scope != current_class_ref 5951 && !(processing_template_decl && scope == current_class_type)) 5952 scope = complete_type_or_else (scope, NULL_TREE); 5953 /* Let the name lookup machinery know that we are processing a 5954 class member access expression. */ 5955 parser->context->object_type = scope; 5956 /* If something went wrong, we want to be able to discern that case, 5957 as opposed to the case where there was no SCOPE due to the type 5958 of expression being dependent. */ 5959 if (!scope) 5960 scope = error_mark_node; 5961 /* If the SCOPE was erroneous, make the various semantic analysis 5962 functions exit quickly -- and without issuing additional error 5963 messages. */ 5964 if (scope == error_mark_node) 5965 postfix_expression = error_mark_node; 5966 } 5967 5968 /* Assume this expression is not a pseudo-destructor access. */ 5969 pseudo_destructor_p = false; 5970 5971 /* If the SCOPE is a scalar type, then, if this is a valid program, 5972 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION 5973 is type dependent, it can be pseudo-destructor-name or something else. 5974 Try to parse it as pseudo-destructor-name first. */ 5975 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p) 5976 { 5977 tree s; 5978 tree type; 5979 5980 cp_parser_parse_tentatively (parser); 5981 /* Parse the pseudo-destructor-name. */ 5982 s = NULL_TREE; 5983 cp_parser_pseudo_destructor_name (parser, &s, &type); 5984 if (dependent_p 5985 && (cp_parser_error_occurred (parser) 5986 || TREE_CODE (type) != TYPE_DECL 5987 || !SCALAR_TYPE_P (TREE_TYPE (type)))) 5988 cp_parser_abort_tentative_parse (parser); 5989 else if (cp_parser_parse_definitely (parser)) 5990 { 5991 pseudo_destructor_p = true; 5992 postfix_expression 5993 = finish_pseudo_destructor_expr (postfix_expression, 5994 s, TREE_TYPE (type)); 5995 } 5996 } 5997 5998 if (!pseudo_destructor_p) 5999 { 6000 /* If the SCOPE is not a scalar type, we are looking at an 6001 ordinary class member access expression, rather than a 6002 pseudo-destructor-name. */ 6003 bool template_p; 6004 cp_token *token = cp_lexer_peek_token (parser->lexer); 6005 /* Parse the id-expression. */ 6006 name = (cp_parser_id_expression 6007 (parser, 6008 cp_parser_optional_template_keyword (parser), 6009 /*check_dependency_p=*/true, 6010 &template_p, 6011 /*declarator_p=*/false, 6012 /*optional_p=*/false)); 6013 /* In general, build a SCOPE_REF if the member name is qualified. 6014 However, if the name was not dependent and has already been 6015 resolved; there is no need to build the SCOPE_REF. For example; 6016 6017 struct X { void f(); }; 6018 template <typename T> void f(T* t) { t->X::f(); } 6019 6020 Even though "t" is dependent, "X::f" is not and has been resolved 6021 to a BASELINK; there is no need to include scope information. */ 6022 6023 /* But we do need to remember that there was an explicit scope for 6024 virtual function calls. */ 6025 if (parser->scope) 6026 *idk = CP_ID_KIND_QUALIFIED; 6027 6028 /* If the name is a template-id that names a type, we will get a 6029 TYPE_DECL here. That is invalid code. */ 6030 if (TREE_CODE (name) == TYPE_DECL) 6031 { 6032 error_at (token->location, "invalid use of %qD", name); 6033 postfix_expression = error_mark_node; 6034 } 6035 else 6036 { 6037 if (name != error_mark_node && !BASELINK_P (name) && parser->scope) 6038 { 6039 if (TREE_CODE (parser->scope) == NAMESPACE_DECL) 6040 { 6041 error_at (token->location, "%<%D::%D%> is not a class member", 6042 parser->scope, name); 6043 postfix_expression = error_mark_node; 6044 } 6045 else 6046 name = build_qualified_name (/*type=*/NULL_TREE, 6047 parser->scope, 6048 name, 6049 template_p); 6050 parser->scope = NULL_TREE; 6051 parser->qualifying_scope = NULL_TREE; 6052 parser->object_scope = NULL_TREE; 6053 } 6054 if (parser->scope && name && BASELINK_P (name)) 6055 adjust_result_of_qualified_name_lookup 6056 (name, parser->scope, scope); 6057 postfix_expression 6058 = finish_class_member_access_expr (postfix_expression, name, 6059 template_p, 6060 tf_warning_or_error); 6061 } 6062 } 6063 6064 /* We no longer need to look up names in the scope of the object on 6065 the left-hand side of the `.' or `->' operator. */ 6066 parser->context->object_type = NULL_TREE; 6067 6068 /* Outside of offsetof, these operators may not appear in 6069 constant-expressions. */ 6070 if (!for_offsetof 6071 && (cp_parser_non_integral_constant_expression 6072 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT))) 6073 postfix_expression = error_mark_node; 6074 6075 return postfix_expression; 6076 } 6077 6078 /* Parse a parenthesized expression-list. 6079 6080 expression-list: 6081 assignment-expression 6082 expression-list, assignment-expression 6083 6084 attribute-list: 6085 expression-list 6086 identifier 6087 identifier, expression-list 6088 6089 CAST_P is true if this expression is the target of a cast. 6090 6091 ALLOW_EXPANSION_P is true if this expression allows expansion of an 6092 argument pack. 6093 6094 Returns a vector of trees. Each element is a representation of an 6095 assignment-expression. NULL is returned if the ( and or ) are 6096 missing. An empty, but allocated, vector is returned on no 6097 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr 6098 if we are parsing an attribute list for an attribute that wants a 6099 plain identifier argument, normal_attr for an attribute that wants 6100 an expression, or non_attr if we aren't parsing an attribute list. If 6101 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or 6102 not all of the expressions in the list were constant. */ 6103 6104 static VEC(tree,gc) * 6105 cp_parser_parenthesized_expression_list (cp_parser* parser, 6106 int is_attribute_list, 6107 bool cast_p, 6108 bool allow_expansion_p, 6109 bool *non_constant_p) 6110 { 6111 VEC(tree,gc) *expression_list; 6112 bool fold_expr_p = is_attribute_list != non_attr; 6113 tree identifier = NULL_TREE; 6114 bool saved_greater_than_is_operator_p; 6115 6116 /* Assume all the expressions will be constant. */ 6117 if (non_constant_p) 6118 *non_constant_p = false; 6119 6120 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 6121 return NULL; 6122 6123 expression_list = make_tree_vector (); 6124 6125 /* Within a parenthesized expression, a `>' token is always 6126 the greater-than operator. */ 6127 saved_greater_than_is_operator_p 6128 = parser->greater_than_is_operator_p; 6129 parser->greater_than_is_operator_p = true; 6130 6131 /* Consume expressions until there are no more. */ 6132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) 6133 while (true) 6134 { 6135 tree expr; 6136 6137 /* At the beginning of attribute lists, check to see if the 6138 next token is an identifier. */ 6139 if (is_attribute_list == id_attr 6140 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME) 6141 { 6142 cp_token *token; 6143 6144 /* Consume the identifier. */ 6145 token = cp_lexer_consume_token (parser->lexer); 6146 /* Save the identifier. */ 6147 identifier = token->u.value; 6148 } 6149 else 6150 { 6151 bool expr_non_constant_p; 6152 6153 /* Parse the next assignment-expression. */ 6154 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 6155 { 6156 /* A braced-init-list. */ 6157 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 6158 expr = cp_parser_braced_list (parser, &expr_non_constant_p); 6159 if (non_constant_p && expr_non_constant_p) 6160 *non_constant_p = true; 6161 } 6162 else if (non_constant_p) 6163 { 6164 expr = (cp_parser_constant_expression 6165 (parser, /*allow_non_constant_p=*/true, 6166 &expr_non_constant_p)); 6167 if (expr_non_constant_p) 6168 *non_constant_p = true; 6169 } 6170 else 6171 expr = cp_parser_assignment_expression (parser, cast_p, NULL); 6172 6173 if (fold_expr_p) 6174 expr = fold_non_dependent_expr (expr); 6175 6176 /* If we have an ellipsis, then this is an expression 6177 expansion. */ 6178 if (allow_expansion_p 6179 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 6180 { 6181 /* Consume the `...'. */ 6182 cp_lexer_consume_token (parser->lexer); 6183 6184 /* Build the argument pack. */ 6185 expr = make_pack_expansion (expr); 6186 } 6187 6188 /* Add it to the list. We add error_mark_node 6189 expressions to the list, so that we can still tell if 6190 the correct form for a parenthesized expression-list 6191 is found. That gives better errors. */ 6192 VEC_safe_push (tree, gc, expression_list, expr); 6193 6194 if (expr == error_mark_node) 6195 goto skip_comma; 6196 } 6197 6198 /* After the first item, attribute lists look the same as 6199 expression lists. */ 6200 is_attribute_list = non_attr; 6201 6202 get_comma:; 6203 /* If the next token isn't a `,', then we are done. */ 6204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 6205 break; 6206 6207 /* Otherwise, consume the `,' and keep going. */ 6208 cp_lexer_consume_token (parser->lexer); 6209 } 6210 6211 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 6212 { 6213 int ending; 6214 6215 skip_comma:; 6216 /* We try and resync to an unnested comma, as that will give the 6217 user better diagnostics. */ 6218 ending = cp_parser_skip_to_closing_parenthesis (parser, 6219 /*recovering=*/true, 6220 /*or_comma=*/true, 6221 /*consume_paren=*/true); 6222 if (ending < 0) 6223 goto get_comma; 6224 if (!ending) 6225 { 6226 parser->greater_than_is_operator_p 6227 = saved_greater_than_is_operator_p; 6228 return NULL; 6229 } 6230 } 6231 6232 parser->greater_than_is_operator_p 6233 = saved_greater_than_is_operator_p; 6234 6235 if (identifier) 6236 VEC_safe_insert (tree, gc, expression_list, 0, identifier); 6237 6238 return expression_list; 6239 } 6240 6241 /* Parse a pseudo-destructor-name. 6242 6243 pseudo-destructor-name: 6244 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name 6245 :: [opt] nested-name-specifier template template-id :: ~ type-name 6246 :: [opt] nested-name-specifier [opt] ~ type-name 6247 6248 If either of the first two productions is used, sets *SCOPE to the 6249 TYPE specified before the final `::'. Otherwise, *SCOPE is set to 6250 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name, 6251 or ERROR_MARK_NODE if the parse fails. */ 6252 6253 static void 6254 cp_parser_pseudo_destructor_name (cp_parser* parser, 6255 tree* scope, 6256 tree* type) 6257 { 6258 bool nested_name_specifier_p; 6259 6260 /* Assume that things will not work out. */ 6261 *type = error_mark_node; 6262 6263 /* Look for the optional `::' operator. */ 6264 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true); 6265 /* Look for the optional nested-name-specifier. */ 6266 nested_name_specifier_p 6267 = (cp_parser_nested_name_specifier_opt (parser, 6268 /*typename_keyword_p=*/false, 6269 /*check_dependency_p=*/true, 6270 /*type_p=*/false, 6271 /*is_declaration=*/false) 6272 != NULL_TREE); 6273 /* Now, if we saw a nested-name-specifier, we might be doing the 6274 second production. */ 6275 if (nested_name_specifier_p 6276 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) 6277 { 6278 /* Consume the `template' keyword. */ 6279 cp_lexer_consume_token (parser->lexer); 6280 /* Parse the template-id. */ 6281 cp_parser_template_id (parser, 6282 /*template_keyword_p=*/true, 6283 /*check_dependency_p=*/false, 6284 /*is_declaration=*/true); 6285 /* Look for the `::' token. */ 6286 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE); 6287 } 6288 /* If the next token is not a `~', then there might be some 6289 additional qualification. */ 6290 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL)) 6291 { 6292 /* At this point, we're looking for "type-name :: ~". The type-name 6293 must not be a class-name, since this is a pseudo-destructor. So, 6294 it must be either an enum-name, or a typedef-name -- both of which 6295 are just identifiers. So, we peek ahead to check that the "::" 6296 and "~" tokens are present; if they are not, then we can avoid 6297 calling type_name. */ 6298 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME 6299 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE 6300 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL) 6301 { 6302 cp_parser_error (parser, "non-scalar type"); 6303 return; 6304 } 6305 6306 /* Look for the type-name. */ 6307 *scope = TREE_TYPE (cp_parser_nonclass_name (parser)); 6308 if (*scope == error_mark_node) 6309 return; 6310 6311 /* Look for the `::' token. */ 6312 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE); 6313 } 6314 else 6315 *scope = NULL_TREE; 6316 6317 /* Look for the `~'. */ 6318 cp_parser_require (parser, CPP_COMPL, RT_COMPL); 6319 6320 /* Look for the type-name again. We are not responsible for 6321 checking that it matches the first type-name. */ 6322 *type = cp_parser_nonclass_name (parser); 6323 } 6324 6325 /* Parse a unary-expression. 6326 6327 unary-expression: 6328 postfix-expression 6329 ++ cast-expression 6330 -- cast-expression 6331 unary-operator cast-expression 6332 sizeof unary-expression 6333 sizeof ( type-id ) 6334 alignof ( type-id ) [C++0x] 6335 new-expression 6336 delete-expression 6337 6338 GNU Extensions: 6339 6340 unary-expression: 6341 __extension__ cast-expression 6342 __alignof__ unary-expression 6343 __alignof__ ( type-id ) 6344 alignof unary-expression [C++0x] 6345 __real__ cast-expression 6346 __imag__ cast-expression 6347 && identifier 6348 6349 ADDRESS_P is true iff the unary-expression is appearing as the 6350 operand of the `&' operator. CAST_P is true if this expression is 6351 the target of a cast. 6352 6353 Returns a representation of the expression. */ 6354 6355 static tree 6356 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p, 6357 cp_id_kind * pidk) 6358 { 6359 cp_token *token; 6360 enum tree_code unary_operator; 6361 6362 /* Peek at the next token. */ 6363 token = cp_lexer_peek_token (parser->lexer); 6364 /* Some keywords give away the kind of expression. */ 6365 if (token->type == CPP_KEYWORD) 6366 { 6367 enum rid keyword = token->keyword; 6368 6369 switch (keyword) 6370 { 6371 case RID_ALIGNOF: 6372 case RID_SIZEOF: 6373 { 6374 tree operand; 6375 enum tree_code op; 6376 6377 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; 6378 /* Consume the token. */ 6379 cp_lexer_consume_token (parser->lexer); 6380 /* Parse the operand. */ 6381 operand = cp_parser_sizeof_operand (parser, keyword); 6382 6383 if (TYPE_P (operand)) 6384 return cxx_sizeof_or_alignof_type (operand, op, true); 6385 else 6386 { 6387 /* ISO C++ defines alignof only with types, not with 6388 expressions. So pedwarn if alignof is used with a non- 6389 type expression. However, __alignof__ is ok. */ 6390 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof")) 6391 pedwarn (token->location, OPT_pedantic, 6392 "ISO C++ does not allow %<alignof%> " 6393 "with a non-type"); 6394 6395 return cxx_sizeof_or_alignof_expr (operand, op, true); 6396 } 6397 } 6398 6399 case RID_NEW: 6400 return cp_parser_new_expression (parser); 6401 6402 case RID_DELETE: 6403 return cp_parser_delete_expression (parser); 6404 6405 case RID_EXTENSION: 6406 { 6407 /* The saved value of the PEDANTIC flag. */ 6408 int saved_pedantic; 6409 tree expr; 6410 6411 /* Save away the PEDANTIC flag. */ 6412 cp_parser_extension_opt (parser, &saved_pedantic); 6413 /* Parse the cast-expression. */ 6414 expr = cp_parser_simple_cast_expression (parser); 6415 /* Restore the PEDANTIC flag. */ 6416 pedantic = saved_pedantic; 6417 6418 return expr; 6419 } 6420 6421 case RID_REALPART: 6422 case RID_IMAGPART: 6423 { 6424 tree expression; 6425 6426 /* Consume the `__real__' or `__imag__' token. */ 6427 cp_lexer_consume_token (parser->lexer); 6428 /* Parse the cast-expression. */ 6429 expression = cp_parser_simple_cast_expression (parser); 6430 /* Create the complete representation. */ 6431 return build_x_unary_op ((keyword == RID_REALPART 6432 ? REALPART_EXPR : IMAGPART_EXPR), 6433 expression, 6434 tf_warning_or_error); 6435 } 6436 break; 6437 6438 case RID_TRANSACTION_ATOMIC: 6439 case RID_TRANSACTION_RELAXED: 6440 return cp_parser_transaction_expression (parser, keyword); 6441 6442 case RID_NOEXCEPT: 6443 { 6444 tree expr; 6445 const char *saved_message; 6446 bool saved_integral_constant_expression_p; 6447 bool saved_non_integral_constant_expression_p; 6448 bool saved_greater_than_is_operator_p; 6449 6450 cp_lexer_consume_token (parser->lexer); 6451 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 6452 6453 saved_message = parser->type_definition_forbidden_message; 6454 parser->type_definition_forbidden_message 6455 = G_("types may not be defined in %<noexcept%> expressions"); 6456 6457 saved_integral_constant_expression_p 6458 = parser->integral_constant_expression_p; 6459 saved_non_integral_constant_expression_p 6460 = parser->non_integral_constant_expression_p; 6461 parser->integral_constant_expression_p = false; 6462 6463 saved_greater_than_is_operator_p 6464 = parser->greater_than_is_operator_p; 6465 parser->greater_than_is_operator_p = true; 6466 6467 ++cp_unevaluated_operand; 6468 ++c_inhibit_evaluation_warnings; 6469 expr = cp_parser_expression (parser, false, NULL); 6470 --c_inhibit_evaluation_warnings; 6471 --cp_unevaluated_operand; 6472 6473 parser->greater_than_is_operator_p 6474 = saved_greater_than_is_operator_p; 6475 6476 parser->integral_constant_expression_p 6477 = saved_integral_constant_expression_p; 6478 parser->non_integral_constant_expression_p 6479 = saved_non_integral_constant_expression_p; 6480 6481 parser->type_definition_forbidden_message = saved_message; 6482 6483 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 6484 return finish_noexcept_expr (expr, tf_warning_or_error); 6485 } 6486 6487 default: 6488 break; 6489 } 6490 } 6491 6492 /* Look for the `:: new' and `:: delete', which also signal the 6493 beginning of a new-expression, or delete-expression, 6494 respectively. If the next token is `::', then it might be one of 6495 these. */ 6496 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 6497 { 6498 enum rid keyword; 6499 6500 /* See if the token after the `::' is one of the keywords in 6501 which we're interested. */ 6502 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; 6503 /* If it's `new', we have a new-expression. */ 6504 if (keyword == RID_NEW) 6505 return cp_parser_new_expression (parser); 6506 /* Similarly, for `delete'. */ 6507 else if (keyword == RID_DELETE) 6508 return cp_parser_delete_expression (parser); 6509 } 6510 6511 /* Look for a unary operator. */ 6512 unary_operator = cp_parser_unary_operator (token); 6513 /* The `++' and `--' operators can be handled similarly, even though 6514 they are not technically unary-operators in the grammar. */ 6515 if (unary_operator == ERROR_MARK) 6516 { 6517 if (token->type == CPP_PLUS_PLUS) 6518 unary_operator = PREINCREMENT_EXPR; 6519 else if (token->type == CPP_MINUS_MINUS) 6520 unary_operator = PREDECREMENT_EXPR; 6521 /* Handle the GNU address-of-label extension. */ 6522 else if (cp_parser_allow_gnu_extensions_p (parser) 6523 && token->type == CPP_AND_AND) 6524 { 6525 tree identifier; 6526 tree expression; 6527 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 6528 6529 /* Consume the '&&' token. */ 6530 cp_lexer_consume_token (parser->lexer); 6531 /* Look for the identifier. */ 6532 identifier = cp_parser_identifier (parser); 6533 /* Create an expression representing the address. */ 6534 expression = finish_label_address_expr (identifier, loc); 6535 if (cp_parser_non_integral_constant_expression (parser, 6536 NIC_ADDR_LABEL)) 6537 expression = error_mark_node; 6538 return expression; 6539 } 6540 } 6541 if (unary_operator != ERROR_MARK) 6542 { 6543 tree cast_expression; 6544 tree expression = error_mark_node; 6545 non_integral_constant non_constant_p = NIC_NONE; 6546 6547 /* Consume the operator token. */ 6548 token = cp_lexer_consume_token (parser->lexer); 6549 /* Parse the cast-expression. */ 6550 cast_expression 6551 = cp_parser_cast_expression (parser, 6552 unary_operator == ADDR_EXPR, 6553 /*cast_p=*/false, pidk); 6554 /* Now, build an appropriate representation. */ 6555 switch (unary_operator) 6556 { 6557 case INDIRECT_REF: 6558 non_constant_p = NIC_STAR; 6559 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR, 6560 tf_warning_or_error); 6561 break; 6562 6563 case ADDR_EXPR: 6564 non_constant_p = NIC_ADDR; 6565 /* Fall through. */ 6566 case BIT_NOT_EXPR: 6567 expression = build_x_unary_op (unary_operator, cast_expression, 6568 tf_warning_or_error); 6569 break; 6570 6571 case PREINCREMENT_EXPR: 6572 case PREDECREMENT_EXPR: 6573 non_constant_p = unary_operator == PREINCREMENT_EXPR 6574 ? NIC_PREINCREMENT : NIC_PREDECREMENT; 6575 /* Fall through. */ 6576 case UNARY_PLUS_EXPR: 6577 case NEGATE_EXPR: 6578 case TRUTH_NOT_EXPR: 6579 expression = finish_unary_op_expr (unary_operator, cast_expression); 6580 break; 6581 6582 default: 6583 gcc_unreachable (); 6584 } 6585 6586 if (non_constant_p != NIC_NONE 6587 && cp_parser_non_integral_constant_expression (parser, 6588 non_constant_p)) 6589 expression = error_mark_node; 6590 6591 return expression; 6592 } 6593 6594 return cp_parser_postfix_expression (parser, address_p, cast_p, 6595 /*member_access_only_p=*/false, 6596 pidk); 6597 } 6598 6599 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a 6600 unary-operator, the corresponding tree code is returned. */ 6601 6602 static enum tree_code 6603 cp_parser_unary_operator (cp_token* token) 6604 { 6605 switch (token->type) 6606 { 6607 case CPP_MULT: 6608 return INDIRECT_REF; 6609 6610 case CPP_AND: 6611 return ADDR_EXPR; 6612 6613 case CPP_PLUS: 6614 return UNARY_PLUS_EXPR; 6615 6616 case CPP_MINUS: 6617 return NEGATE_EXPR; 6618 6619 case CPP_NOT: 6620 return TRUTH_NOT_EXPR; 6621 6622 case CPP_COMPL: 6623 return BIT_NOT_EXPR; 6624 6625 default: 6626 return ERROR_MARK; 6627 } 6628 } 6629 6630 /* Parse a new-expression. 6631 6632 new-expression: 6633 :: [opt] new new-placement [opt] new-type-id new-initializer [opt] 6634 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] 6635 6636 Returns a representation of the expression. */ 6637 6638 static tree 6639 cp_parser_new_expression (cp_parser* parser) 6640 { 6641 bool global_scope_p; 6642 VEC(tree,gc) *placement; 6643 tree type; 6644 VEC(tree,gc) *initializer; 6645 tree nelts; 6646 tree ret; 6647 6648 /* Look for the optional `::' operator. */ 6649 global_scope_p 6650 = (cp_parser_global_scope_opt (parser, 6651 /*current_scope_valid_p=*/false) 6652 != NULL_TREE); 6653 /* Look for the `new' operator. */ 6654 cp_parser_require_keyword (parser, RID_NEW, RT_NEW); 6655 /* There's no easy way to tell a new-placement from the 6656 `( type-id )' construct. */ 6657 cp_parser_parse_tentatively (parser); 6658 /* Look for a new-placement. */ 6659 placement = cp_parser_new_placement (parser); 6660 /* If that didn't work out, there's no new-placement. */ 6661 if (!cp_parser_parse_definitely (parser)) 6662 { 6663 if (placement != NULL) 6664 release_tree_vector (placement); 6665 placement = NULL; 6666 } 6667 6668 /* If the next token is a `(', then we have a parenthesized 6669 type-id. */ 6670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 6671 { 6672 cp_token *token; 6673 const char *saved_message = parser->type_definition_forbidden_message; 6674 6675 /* Consume the `('. */ 6676 cp_lexer_consume_token (parser->lexer); 6677 6678 /* Parse the type-id. */ 6679 parser->type_definition_forbidden_message 6680 = G_("types may not be defined in a new-expression"); 6681 type = cp_parser_type_id (parser); 6682 parser->type_definition_forbidden_message = saved_message; 6683 6684 /* Look for the closing `)'. */ 6685 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 6686 token = cp_lexer_peek_token (parser->lexer); 6687 /* There should not be a direct-new-declarator in this production, 6688 but GCC used to allowed this, so we check and emit a sensible error 6689 message for this case. */ 6690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 6691 { 6692 error_at (token->location, 6693 "array bound forbidden after parenthesized type-id"); 6694 inform (token->location, 6695 "try removing the parentheses around the type-id"); 6696 cp_parser_direct_new_declarator (parser); 6697 } 6698 nelts = NULL_TREE; 6699 } 6700 /* Otherwise, there must be a new-type-id. */ 6701 else 6702 type = cp_parser_new_type_id (parser, &nelts); 6703 6704 /* If the next token is a `(' or '{', then we have a new-initializer. */ 6705 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN) 6706 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 6707 initializer = cp_parser_new_initializer (parser); 6708 else 6709 initializer = NULL; 6710 6711 /* A new-expression may not appear in an integral constant 6712 expression. */ 6713 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW)) 6714 ret = error_mark_node; 6715 else 6716 { 6717 /* Create a representation of the new-expression. */ 6718 ret = build_new (&placement, type, nelts, &initializer, global_scope_p, 6719 tf_warning_or_error); 6720 } 6721 6722 if (placement != NULL) 6723 release_tree_vector (placement); 6724 if (initializer != NULL) 6725 release_tree_vector (initializer); 6726 6727 return ret; 6728 } 6729 6730 /* Parse a new-placement. 6731 6732 new-placement: 6733 ( expression-list ) 6734 6735 Returns the same representation as for an expression-list. */ 6736 6737 static VEC(tree,gc) * 6738 cp_parser_new_placement (cp_parser* parser) 6739 { 6740 VEC(tree,gc) *expression_list; 6741 6742 /* Parse the expression-list. */ 6743 expression_list = (cp_parser_parenthesized_expression_list 6744 (parser, non_attr, /*cast_p=*/false, 6745 /*allow_expansion_p=*/true, 6746 /*non_constant_p=*/NULL)); 6747 6748 return expression_list; 6749 } 6750 6751 /* Parse a new-type-id. 6752 6753 new-type-id: 6754 type-specifier-seq new-declarator [opt] 6755 6756 Returns the TYPE allocated. If the new-type-id indicates an array 6757 type, *NELTS is set to the number of elements in the last array 6758 bound; the TYPE will not include the last array bound. */ 6759 6760 static tree 6761 cp_parser_new_type_id (cp_parser* parser, tree *nelts) 6762 { 6763 cp_decl_specifier_seq type_specifier_seq; 6764 cp_declarator *new_declarator; 6765 cp_declarator *declarator; 6766 cp_declarator *outer_declarator; 6767 const char *saved_message; 6768 tree type; 6769 6770 /* The type-specifier sequence must not contain type definitions. 6771 (It cannot contain declarations of new types either, but if they 6772 are not definitions we will catch that because they are not 6773 complete.) */ 6774 saved_message = parser->type_definition_forbidden_message; 6775 parser->type_definition_forbidden_message 6776 = G_("types may not be defined in a new-type-id"); 6777 /* Parse the type-specifier-seq. */ 6778 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false, 6779 /*is_trailing_return=*/false, 6780 &type_specifier_seq); 6781 /* Restore the old message. */ 6782 parser->type_definition_forbidden_message = saved_message; 6783 /* Parse the new-declarator. */ 6784 new_declarator = cp_parser_new_declarator_opt (parser); 6785 6786 /* Determine the number of elements in the last array dimension, if 6787 any. */ 6788 *nelts = NULL_TREE; 6789 /* Skip down to the last array dimension. */ 6790 declarator = new_declarator; 6791 outer_declarator = NULL; 6792 while (declarator && (declarator->kind == cdk_pointer 6793 || declarator->kind == cdk_ptrmem)) 6794 { 6795 outer_declarator = declarator; 6796 declarator = declarator->declarator; 6797 } 6798 while (declarator 6799 && declarator->kind == cdk_array 6800 && declarator->declarator 6801 && declarator->declarator->kind == cdk_array) 6802 { 6803 outer_declarator = declarator; 6804 declarator = declarator->declarator; 6805 } 6806 6807 if (declarator && declarator->kind == cdk_array) 6808 { 6809 *nelts = declarator->u.array.bounds; 6810 if (*nelts == error_mark_node) 6811 *nelts = integer_one_node; 6812 6813 if (outer_declarator) 6814 outer_declarator->declarator = declarator->declarator; 6815 else 6816 new_declarator = NULL; 6817 } 6818 6819 type = groktypename (&type_specifier_seq, new_declarator, false); 6820 return type; 6821 } 6822 6823 /* Parse an (optional) new-declarator. 6824 6825 new-declarator: 6826 ptr-operator new-declarator [opt] 6827 direct-new-declarator 6828 6829 Returns the declarator. */ 6830 6831 static cp_declarator * 6832 cp_parser_new_declarator_opt (cp_parser* parser) 6833 { 6834 enum tree_code code; 6835 tree type; 6836 cp_cv_quals cv_quals; 6837 6838 /* We don't know if there's a ptr-operator next, or not. */ 6839 cp_parser_parse_tentatively (parser); 6840 /* Look for a ptr-operator. */ 6841 code = cp_parser_ptr_operator (parser, &type, &cv_quals); 6842 /* If that worked, look for more new-declarators. */ 6843 if (cp_parser_parse_definitely (parser)) 6844 { 6845 cp_declarator *declarator; 6846 6847 /* Parse another optional declarator. */ 6848 declarator = cp_parser_new_declarator_opt (parser); 6849 6850 return cp_parser_make_indirect_declarator 6851 (code, type, cv_quals, declarator); 6852 } 6853 6854 /* If the next token is a `[', there is a direct-new-declarator. */ 6855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 6856 return cp_parser_direct_new_declarator (parser); 6857 6858 return NULL; 6859 } 6860 6861 /* Parse a direct-new-declarator. 6862 6863 direct-new-declarator: 6864 [ expression ] 6865 direct-new-declarator [constant-expression] 6866 6867 */ 6868 6869 static cp_declarator * 6870 cp_parser_direct_new_declarator (cp_parser* parser) 6871 { 6872 cp_declarator *declarator = NULL; 6873 6874 while (true) 6875 { 6876 tree expression; 6877 6878 /* Look for the opening `['. */ 6879 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); 6880 /* The first expression is not required to be constant. */ 6881 if (!declarator) 6882 { 6883 cp_token *token = cp_lexer_peek_token (parser->lexer); 6884 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); 6885 /* The standard requires that the expression have integral 6886 type. DR 74 adds enumeration types. We believe that the 6887 real intent is that these expressions be handled like the 6888 expression in a `switch' condition, which also allows 6889 classes with a single conversion to integral or 6890 enumeration type. */ 6891 if (!processing_template_decl) 6892 { 6893 expression 6894 = build_expr_type_conversion (WANT_INT | WANT_ENUM, 6895 expression, 6896 /*complain=*/true); 6897 if (!expression) 6898 { 6899 error_at (token->location, 6900 "expression in new-declarator must have integral " 6901 "or enumeration type"); 6902 expression = error_mark_node; 6903 } 6904 } 6905 } 6906 /* But all the other expressions must be. */ 6907 else 6908 expression 6909 = cp_parser_constant_expression (parser, 6910 /*allow_non_constant=*/false, 6911 NULL); 6912 /* Look for the closing `]'. */ 6913 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 6914 6915 /* Add this bound to the declarator. */ 6916 declarator = make_array_declarator (declarator, expression); 6917 6918 /* If the next token is not a `[', then there are no more 6919 bounds. */ 6920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) 6921 break; 6922 } 6923 6924 return declarator; 6925 } 6926 6927 /* Parse a new-initializer. 6928 6929 new-initializer: 6930 ( expression-list [opt] ) 6931 braced-init-list 6932 6933 Returns a representation of the expression-list. */ 6934 6935 static VEC(tree,gc) * 6936 cp_parser_new_initializer (cp_parser* parser) 6937 { 6938 VEC(tree,gc) *expression_list; 6939 6940 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 6941 { 6942 tree t; 6943 bool expr_non_constant_p; 6944 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 6945 t = cp_parser_braced_list (parser, &expr_non_constant_p); 6946 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1; 6947 expression_list = make_tree_vector_single (t); 6948 } 6949 else 6950 expression_list = (cp_parser_parenthesized_expression_list 6951 (parser, non_attr, /*cast_p=*/false, 6952 /*allow_expansion_p=*/true, 6953 /*non_constant_p=*/NULL)); 6954 6955 return expression_list; 6956 } 6957 6958 /* Parse a delete-expression. 6959 6960 delete-expression: 6961 :: [opt] delete cast-expression 6962 :: [opt] delete [ ] cast-expression 6963 6964 Returns a representation of the expression. */ 6965 6966 static tree 6967 cp_parser_delete_expression (cp_parser* parser) 6968 { 6969 bool global_scope_p; 6970 bool array_p; 6971 tree expression; 6972 6973 /* Look for the optional `::' operator. */ 6974 global_scope_p 6975 = (cp_parser_global_scope_opt (parser, 6976 /*current_scope_valid_p=*/false) 6977 != NULL_TREE); 6978 /* Look for the `delete' keyword. */ 6979 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE); 6980 /* See if the array syntax is in use. */ 6981 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 6982 { 6983 /* Consume the `[' token. */ 6984 cp_lexer_consume_token (parser->lexer); 6985 /* Look for the `]' token. */ 6986 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 6987 /* Remember that this is the `[]' construct. */ 6988 array_p = true; 6989 } 6990 else 6991 array_p = false; 6992 6993 /* Parse the cast-expression. */ 6994 expression = cp_parser_simple_cast_expression (parser); 6995 6996 /* A delete-expression may not appear in an integral constant 6997 expression. */ 6998 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL)) 6999 return error_mark_node; 7000 7001 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p, 7002 tf_warning_or_error); 7003 } 7004 7005 /* Returns true if TOKEN may start a cast-expression and false 7006 otherwise. */ 7007 7008 static bool 7009 cp_parser_tokens_start_cast_expression (cp_parser *parser) 7010 { 7011 cp_token *token = cp_lexer_peek_token (parser->lexer); 7012 switch (token->type) 7013 { 7014 case CPP_COMMA: 7015 case CPP_SEMICOLON: 7016 case CPP_QUERY: 7017 case CPP_COLON: 7018 case CPP_CLOSE_SQUARE: 7019 case CPP_CLOSE_PAREN: 7020 case CPP_CLOSE_BRACE: 7021 case CPP_DOT: 7022 case CPP_DOT_STAR: 7023 case CPP_DEREF: 7024 case CPP_DEREF_STAR: 7025 case CPP_DIV: 7026 case CPP_MOD: 7027 case CPP_LSHIFT: 7028 case CPP_RSHIFT: 7029 case CPP_LESS: 7030 case CPP_GREATER: 7031 case CPP_LESS_EQ: 7032 case CPP_GREATER_EQ: 7033 case CPP_EQ_EQ: 7034 case CPP_NOT_EQ: 7035 case CPP_EQ: 7036 case CPP_MULT_EQ: 7037 case CPP_DIV_EQ: 7038 case CPP_MOD_EQ: 7039 case CPP_PLUS_EQ: 7040 case CPP_MINUS_EQ: 7041 case CPP_RSHIFT_EQ: 7042 case CPP_LSHIFT_EQ: 7043 case CPP_AND_EQ: 7044 case CPP_XOR_EQ: 7045 case CPP_OR_EQ: 7046 case CPP_XOR: 7047 case CPP_OR: 7048 case CPP_OR_OR: 7049 case CPP_EOF: 7050 return false; 7051 7052 case CPP_OPEN_PAREN: 7053 /* In ((type ()) () the last () isn't a valid cast-expression, 7054 so the whole must be parsed as postfix-expression. */ 7055 return cp_lexer_peek_nth_token (parser->lexer, 2)->type 7056 != CPP_CLOSE_PAREN; 7057 7058 /* '[' may start a primary-expression in obj-c++. */ 7059 case CPP_OPEN_SQUARE: 7060 return c_dialect_objc (); 7061 7062 default: 7063 return true; 7064 } 7065 } 7066 7067 /* Parse a cast-expression. 7068 7069 cast-expression: 7070 unary-expression 7071 ( type-id ) cast-expression 7072 7073 ADDRESS_P is true iff the unary-expression is appearing as the 7074 operand of the `&' operator. CAST_P is true if this expression is 7075 the target of a cast. 7076 7077 Returns a representation of the expression. */ 7078 7079 static tree 7080 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p, 7081 cp_id_kind * pidk) 7082 { 7083 /* If it's a `(', then we might be looking at a cast. */ 7084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 7085 { 7086 tree type = NULL_TREE; 7087 tree expr = NULL_TREE; 7088 bool compound_literal_p; 7089 const char *saved_message; 7090 7091 /* There's no way to know yet whether or not this is a cast. 7092 For example, `(int (3))' is a unary-expression, while `(int) 7093 3' is a cast. So, we resort to parsing tentatively. */ 7094 cp_parser_parse_tentatively (parser); 7095 /* Types may not be defined in a cast. */ 7096 saved_message = parser->type_definition_forbidden_message; 7097 parser->type_definition_forbidden_message 7098 = G_("types may not be defined in casts"); 7099 /* Consume the `('. */ 7100 cp_lexer_consume_token (parser->lexer); 7101 /* A very tricky bit is that `(struct S) { 3 }' is a 7102 compound-literal (which we permit in C++ as an extension). 7103 But, that construct is not a cast-expression -- it is a 7104 postfix-expression. (The reason is that `(struct S) { 3 }.i' 7105 is legal; if the compound-literal were a cast-expression, 7106 you'd need an extra set of parentheses.) But, if we parse 7107 the type-id, and it happens to be a class-specifier, then we 7108 will commit to the parse at that point, because we cannot 7109 undo the action that is done when creating a new class. So, 7110 then we cannot back up and do a postfix-expression. 7111 7112 Therefore, we scan ahead to the closing `)', and check to see 7113 if the token after the `)' is a `{'. If so, we are not 7114 looking at a cast-expression. 7115 7116 Save tokens so that we can put them back. */ 7117 cp_lexer_save_tokens (parser->lexer); 7118 /* Skip tokens until the next token is a closing parenthesis. 7119 If we find the closing `)', and the next token is a `{', then 7120 we are looking at a compound-literal. */ 7121 compound_literal_p 7122 = (cp_parser_skip_to_closing_parenthesis (parser, false, false, 7123 /*consume_paren=*/true) 7124 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); 7125 /* Roll back the tokens we skipped. */ 7126 cp_lexer_rollback_tokens (parser->lexer); 7127 /* If we were looking at a compound-literal, simulate an error 7128 so that the call to cp_parser_parse_definitely below will 7129 fail. */ 7130 if (compound_literal_p) 7131 cp_parser_simulate_error (parser); 7132 else 7133 { 7134 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; 7135 parser->in_type_id_in_expr_p = true; 7136 /* Look for the type-id. */ 7137 type = cp_parser_type_id (parser); 7138 /* Look for the closing `)'. */ 7139 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 7140 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; 7141 } 7142 7143 /* Restore the saved message. */ 7144 parser->type_definition_forbidden_message = saved_message; 7145 7146 /* At this point this can only be either a cast or a 7147 parenthesized ctor such as `(T ())' that looks like a cast to 7148 function returning T. */ 7149 if (!cp_parser_error_occurred (parser) 7150 && cp_parser_tokens_start_cast_expression (parser)) 7151 { 7152 cp_parser_parse_definitely (parser); 7153 expr = cp_parser_cast_expression (parser, 7154 /*address_p=*/false, 7155 /*cast_p=*/true, pidk); 7156 7157 /* Warn about old-style casts, if so requested. */ 7158 if (warn_old_style_cast 7159 && !in_system_header 7160 && !VOID_TYPE_P (type) 7161 && current_lang_name != lang_name_c) 7162 warning (OPT_Wold_style_cast, "use of old-style cast"); 7163 7164 /* Only type conversions to integral or enumeration types 7165 can be used in constant-expressions. */ 7166 if (!cast_valid_in_integral_constant_expression_p (type) 7167 && cp_parser_non_integral_constant_expression (parser, 7168 NIC_CAST)) 7169 return error_mark_node; 7170 7171 /* Perform the cast. */ 7172 expr = build_c_cast (input_location, type, expr); 7173 return expr; 7174 } 7175 else 7176 cp_parser_abort_tentative_parse (parser); 7177 } 7178 7179 /* If we get here, then it's not a cast, so it must be a 7180 unary-expression. */ 7181 return cp_parser_unary_expression (parser, address_p, cast_p, pidk); 7182 } 7183 7184 /* Parse a binary expression of the general form: 7185 7186 pm-expression: 7187 cast-expression 7188 pm-expression .* cast-expression 7189 pm-expression ->* cast-expression 7190 7191 multiplicative-expression: 7192 pm-expression 7193 multiplicative-expression * pm-expression 7194 multiplicative-expression / pm-expression 7195 multiplicative-expression % pm-expression 7196 7197 additive-expression: 7198 multiplicative-expression 7199 additive-expression + multiplicative-expression 7200 additive-expression - multiplicative-expression 7201 7202 shift-expression: 7203 additive-expression 7204 shift-expression << additive-expression 7205 shift-expression >> additive-expression 7206 7207 relational-expression: 7208 shift-expression 7209 relational-expression < shift-expression 7210 relational-expression > shift-expression 7211 relational-expression <= shift-expression 7212 relational-expression >= shift-expression 7213 7214 GNU Extension: 7215 7216 relational-expression: 7217 relational-expression <? shift-expression 7218 relational-expression >? shift-expression 7219 7220 equality-expression: 7221 relational-expression 7222 equality-expression == relational-expression 7223 equality-expression != relational-expression 7224 7225 and-expression: 7226 equality-expression 7227 and-expression & equality-expression 7228 7229 exclusive-or-expression: 7230 and-expression 7231 exclusive-or-expression ^ and-expression 7232 7233 inclusive-or-expression: 7234 exclusive-or-expression 7235 inclusive-or-expression | exclusive-or-expression 7236 7237 logical-and-expression: 7238 inclusive-or-expression 7239 logical-and-expression && inclusive-or-expression 7240 7241 logical-or-expression: 7242 logical-and-expression 7243 logical-or-expression || logical-and-expression 7244 7245 All these are implemented with a single function like: 7246 7247 binary-expression: 7248 simple-cast-expression 7249 binary-expression <token> binary-expression 7250 7251 CAST_P is true if this expression is the target of a cast. 7252 7253 The binops_by_token map is used to get the tree codes for each <token> type. 7254 binary-expressions are associated according to a precedence table. */ 7255 7256 #define TOKEN_PRECEDENCE(token) \ 7257 (((token->type == CPP_GREATER \ 7258 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \ 7259 && !parser->greater_than_is_operator_p) \ 7260 ? PREC_NOT_OPERATOR \ 7261 : binops_by_token[token->type].prec) 7262 7263 static tree 7264 cp_parser_binary_expression (cp_parser* parser, bool cast_p, 7265 bool no_toplevel_fold_p, 7266 enum cp_parser_prec prec, 7267 cp_id_kind * pidk) 7268 { 7269 cp_parser_expression_stack stack; 7270 cp_parser_expression_stack_entry *sp = &stack[0]; 7271 tree lhs, rhs; 7272 cp_token *token; 7273 enum tree_code tree_type, lhs_type, rhs_type; 7274 enum cp_parser_prec new_prec, lookahead_prec; 7275 tree overload; 7276 7277 /* Parse the first expression. */ 7278 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk); 7279 lhs_type = ERROR_MARK; 7280 7281 if (cp_parser_error_occurred (parser)) 7282 return error_mark_node; 7283 7284 for (;;) 7285 { 7286 /* Get an operator token. */ 7287 token = cp_lexer_peek_token (parser->lexer); 7288 7289 if (warn_cxx0x_compat 7290 && token->type == CPP_RSHIFT 7291 && !parser->greater_than_is_operator_p) 7292 { 7293 if (warning_at (token->location, OPT_Wc__0x_compat, 7294 "%<>>%> operator is treated as" 7295 " two right angle brackets in C++11")) 7296 inform (token->location, 7297 "suggest parentheses around %<>>%> expression"); 7298 } 7299 7300 new_prec = TOKEN_PRECEDENCE (token); 7301 7302 /* Popping an entry off the stack means we completed a subexpression: 7303 - either we found a token which is not an operator (`>' where it is not 7304 an operator, or prec == PREC_NOT_OPERATOR), in which case popping 7305 will happen repeatedly; 7306 - or, we found an operator which has lower priority. This is the case 7307 where the recursive descent *ascends*, as in `3 * 4 + 5' after 7308 parsing `3 * 4'. */ 7309 if (new_prec <= prec) 7310 { 7311 if (sp == stack) 7312 break; 7313 else 7314 goto pop; 7315 } 7316 7317 get_rhs: 7318 tree_type = binops_by_token[token->type].tree_type; 7319 7320 /* We used the operator token. */ 7321 cp_lexer_consume_token (parser->lexer); 7322 7323 /* For "false && x" or "true || x", x will never be executed; 7324 disable warnings while evaluating it. */ 7325 if (tree_type == TRUTH_ANDIF_EXPR) 7326 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node; 7327 else if (tree_type == TRUTH_ORIF_EXPR) 7328 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node; 7329 7330 /* Extract another operand. It may be the RHS of this expression 7331 or the LHS of a new, higher priority expression. */ 7332 rhs = cp_parser_simple_cast_expression (parser); 7333 rhs_type = ERROR_MARK; 7334 7335 /* Get another operator token. Look up its precedence to avoid 7336 building a useless (immediately popped) stack entry for common 7337 cases such as 3 + 4 + 5 or 3 * 4 + 5. */ 7338 token = cp_lexer_peek_token (parser->lexer); 7339 lookahead_prec = TOKEN_PRECEDENCE (token); 7340 if (lookahead_prec > new_prec) 7341 { 7342 /* ... and prepare to parse the RHS of the new, higher priority 7343 expression. Since precedence levels on the stack are 7344 monotonically increasing, we do not have to care about 7345 stack overflows. */ 7346 sp->prec = prec; 7347 sp->tree_type = tree_type; 7348 sp->lhs = lhs; 7349 sp->lhs_type = lhs_type; 7350 sp++; 7351 lhs = rhs; 7352 lhs_type = rhs_type; 7353 prec = new_prec; 7354 new_prec = lookahead_prec; 7355 goto get_rhs; 7356 7357 pop: 7358 lookahead_prec = new_prec; 7359 /* If the stack is not empty, we have parsed into LHS the right side 7360 (`4' in the example above) of an expression we had suspended. 7361 We can use the information on the stack to recover the LHS (`3') 7362 from the stack together with the tree code (`MULT_EXPR'), and 7363 the precedence of the higher level subexpression 7364 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token, 7365 which will be used to actually build the additive expression. */ 7366 --sp; 7367 prec = sp->prec; 7368 tree_type = sp->tree_type; 7369 rhs = lhs; 7370 rhs_type = lhs_type; 7371 lhs = sp->lhs; 7372 lhs_type = sp->lhs_type; 7373 } 7374 7375 /* Undo the disabling of warnings done above. */ 7376 if (tree_type == TRUTH_ANDIF_EXPR) 7377 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node; 7378 else if (tree_type == TRUTH_ORIF_EXPR) 7379 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node; 7380 7381 overload = NULL; 7382 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type == 7383 ERROR_MARK for everything that is not a binary expression. 7384 This makes warn_about_parentheses miss some warnings that 7385 involve unary operators. For unary expressions we should 7386 pass the correct tree_code unless the unary expression was 7387 surrounded by parentheses. 7388 */ 7389 if (no_toplevel_fold_p 7390 && lookahead_prec <= prec 7391 && sp == stack 7392 && TREE_CODE_CLASS (tree_type) == tcc_comparison) 7393 lhs = build2 (tree_type, boolean_type_node, lhs, rhs); 7394 else 7395 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type, 7396 &overload, tf_warning_or_error); 7397 lhs_type = tree_type; 7398 7399 /* If the binary operator required the use of an overloaded operator, 7400 then this expression cannot be an integral constant-expression. 7401 An overloaded operator can be used even if both operands are 7402 otherwise permissible in an integral constant-expression if at 7403 least one of the operands is of enumeration type. */ 7404 7405 if (overload 7406 && cp_parser_non_integral_constant_expression (parser, 7407 NIC_OVERLOADED)) 7408 return error_mark_node; 7409 } 7410 7411 return lhs; 7412 } 7413 7414 7415 /* Parse the `? expression : assignment-expression' part of a 7416 conditional-expression. The LOGICAL_OR_EXPR is the 7417 logical-or-expression that started the conditional-expression. 7418 Returns a representation of the entire conditional-expression. 7419 7420 This routine is used by cp_parser_assignment_expression. 7421 7422 ? expression : assignment-expression 7423 7424 GNU Extensions: 7425 7426 ? : assignment-expression */ 7427 7428 static tree 7429 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) 7430 { 7431 tree expr; 7432 tree assignment_expr; 7433 struct cp_token *token; 7434 7435 /* Consume the `?' token. */ 7436 cp_lexer_consume_token (parser->lexer); 7437 token = cp_lexer_peek_token (parser->lexer); 7438 if (cp_parser_allow_gnu_extensions_p (parser) 7439 && token->type == CPP_COLON) 7440 { 7441 pedwarn (token->location, OPT_pedantic, 7442 "ISO C++ does not allow ?: with omitted middle operand"); 7443 /* Implicit true clause. */ 7444 expr = NULL_TREE; 7445 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node; 7446 warn_for_omitted_condop (token->location, logical_or_expr); 7447 } 7448 else 7449 { 7450 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 7451 parser->colon_corrects_to_scope_p = false; 7452 /* Parse the expression. */ 7453 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node; 7454 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 7455 c_inhibit_evaluation_warnings += 7456 ((logical_or_expr == truthvalue_true_node) 7457 - (logical_or_expr == truthvalue_false_node)); 7458 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 7459 } 7460 7461 /* The next token should be a `:'. */ 7462 cp_parser_require (parser, CPP_COLON, RT_COLON); 7463 /* Parse the assignment-expression. */ 7464 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL); 7465 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node; 7466 7467 /* Build the conditional-expression. */ 7468 return build_x_conditional_expr (logical_or_expr, 7469 expr, 7470 assignment_expr, 7471 tf_warning_or_error); 7472 } 7473 7474 /* Parse an assignment-expression. 7475 7476 assignment-expression: 7477 conditional-expression 7478 logical-or-expression assignment-operator assignment_expression 7479 throw-expression 7480 7481 CAST_P is true if this expression is the target of a cast. 7482 7483 Returns a representation for the expression. */ 7484 7485 static tree 7486 cp_parser_assignment_expression (cp_parser* parser, bool cast_p, 7487 cp_id_kind * pidk) 7488 { 7489 tree expr; 7490 7491 /* If the next token is the `throw' keyword, then we're looking at 7492 a throw-expression. */ 7493 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) 7494 expr = cp_parser_throw_expression (parser); 7495 /* Otherwise, it must be that we are looking at a 7496 logical-or-expression. */ 7497 else 7498 { 7499 /* Parse the binary expressions (logical-or-expression). */ 7500 expr = cp_parser_binary_expression (parser, cast_p, false, 7501 PREC_NOT_OPERATOR, pidk); 7502 /* If the next token is a `?' then we're actually looking at a 7503 conditional-expression. */ 7504 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) 7505 return cp_parser_question_colon_clause (parser, expr); 7506 else 7507 { 7508 enum tree_code assignment_operator; 7509 7510 /* If it's an assignment-operator, we're using the second 7511 production. */ 7512 assignment_operator 7513 = cp_parser_assignment_operator_opt (parser); 7514 if (assignment_operator != ERROR_MARK) 7515 { 7516 bool non_constant_p; 7517 7518 /* Parse the right-hand side of the assignment. */ 7519 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p); 7520 7521 if (BRACE_ENCLOSED_INITIALIZER_P (rhs)) 7522 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 7523 7524 /* An assignment may not appear in a 7525 constant-expression. */ 7526 if (cp_parser_non_integral_constant_expression (parser, 7527 NIC_ASSIGNMENT)) 7528 return error_mark_node; 7529 /* Build the assignment expression. */ 7530 expr = build_x_modify_expr (expr, 7531 assignment_operator, 7532 rhs, 7533 tf_warning_or_error); 7534 } 7535 } 7536 } 7537 7538 return expr; 7539 } 7540 7541 /* Parse an (optional) assignment-operator. 7542 7543 assignment-operator: one of 7544 = *= /= %= += -= >>= <<= &= ^= |= 7545 7546 GNU Extension: 7547 7548 assignment-operator: one of 7549 <?= >?= 7550 7551 If the next token is an assignment operator, the corresponding tree 7552 code is returned, and the token is consumed. For example, for 7553 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is 7554 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', 7555 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment 7556 operator, ERROR_MARK is returned. */ 7557 7558 static enum tree_code 7559 cp_parser_assignment_operator_opt (cp_parser* parser) 7560 { 7561 enum tree_code op; 7562 cp_token *token; 7563 7564 /* Peek at the next token. */ 7565 token = cp_lexer_peek_token (parser->lexer); 7566 7567 switch (token->type) 7568 { 7569 case CPP_EQ: 7570 op = NOP_EXPR; 7571 break; 7572 7573 case CPP_MULT_EQ: 7574 op = MULT_EXPR; 7575 break; 7576 7577 case CPP_DIV_EQ: 7578 op = TRUNC_DIV_EXPR; 7579 break; 7580 7581 case CPP_MOD_EQ: 7582 op = TRUNC_MOD_EXPR; 7583 break; 7584 7585 case CPP_PLUS_EQ: 7586 op = PLUS_EXPR; 7587 break; 7588 7589 case CPP_MINUS_EQ: 7590 op = MINUS_EXPR; 7591 break; 7592 7593 case CPP_RSHIFT_EQ: 7594 op = RSHIFT_EXPR; 7595 break; 7596 7597 case CPP_LSHIFT_EQ: 7598 op = LSHIFT_EXPR; 7599 break; 7600 7601 case CPP_AND_EQ: 7602 op = BIT_AND_EXPR; 7603 break; 7604 7605 case CPP_XOR_EQ: 7606 op = BIT_XOR_EXPR; 7607 break; 7608 7609 case CPP_OR_EQ: 7610 op = BIT_IOR_EXPR; 7611 break; 7612 7613 default: 7614 /* Nothing else is an assignment operator. */ 7615 op = ERROR_MARK; 7616 } 7617 7618 /* If it was an assignment operator, consume it. */ 7619 if (op != ERROR_MARK) 7620 cp_lexer_consume_token (parser->lexer); 7621 7622 return op; 7623 } 7624 7625 /* Parse an expression. 7626 7627 expression: 7628 assignment-expression 7629 expression , assignment-expression 7630 7631 CAST_P is true if this expression is the target of a cast. 7632 7633 Returns a representation of the expression. */ 7634 7635 static tree 7636 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk) 7637 { 7638 tree expression = NULL_TREE; 7639 7640 while (true) 7641 { 7642 tree assignment_expression; 7643 7644 /* Parse the next assignment-expression. */ 7645 assignment_expression 7646 = cp_parser_assignment_expression (parser, cast_p, pidk); 7647 /* If this is the first assignment-expression, we can just 7648 save it away. */ 7649 if (!expression) 7650 expression = assignment_expression; 7651 else 7652 expression = build_x_compound_expr (expression, 7653 assignment_expression, 7654 tf_warning_or_error); 7655 /* If the next token is not a comma, then we are done with the 7656 expression. */ 7657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 7658 break; 7659 /* Consume the `,'. */ 7660 cp_lexer_consume_token (parser->lexer); 7661 /* A comma operator cannot appear in a constant-expression. */ 7662 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA)) 7663 expression = error_mark_node; 7664 } 7665 7666 return expression; 7667 } 7668 7669 /* Parse a constant-expression. 7670 7671 constant-expression: 7672 conditional-expression 7673 7674 If ALLOW_NON_CONSTANT_P a non-constant expression is silently 7675 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not 7676 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P 7677 is false, NON_CONSTANT_P should be NULL. */ 7678 7679 static tree 7680 cp_parser_constant_expression (cp_parser* parser, 7681 bool allow_non_constant_p, 7682 bool *non_constant_p) 7683 { 7684 bool saved_integral_constant_expression_p; 7685 bool saved_allow_non_integral_constant_expression_p; 7686 bool saved_non_integral_constant_expression_p; 7687 tree expression; 7688 7689 /* It might seem that we could simply parse the 7690 conditional-expression, and then check to see if it were 7691 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is 7692 one that the compiler can figure out is constant, possibly after 7693 doing some simplifications or optimizations. The standard has a 7694 precise definition of constant-expression, and we must honor 7695 that, even though it is somewhat more restrictive. 7696 7697 For example: 7698 7699 int i[(2, 3)]; 7700 7701 is not a legal declaration, because `(2, 3)' is not a 7702 constant-expression. The `,' operator is forbidden in a 7703 constant-expression. However, GCC's constant-folding machinery 7704 will fold this operation to an INTEGER_CST for `3'. */ 7705 7706 /* Save the old settings. */ 7707 saved_integral_constant_expression_p = parser->integral_constant_expression_p; 7708 saved_allow_non_integral_constant_expression_p 7709 = parser->allow_non_integral_constant_expression_p; 7710 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p; 7711 /* We are now parsing a constant-expression. */ 7712 parser->integral_constant_expression_p = true; 7713 parser->allow_non_integral_constant_expression_p 7714 = (allow_non_constant_p || cxx_dialect >= cxx0x); 7715 parser->non_integral_constant_expression_p = false; 7716 /* Although the grammar says "conditional-expression", we parse an 7717 "assignment-expression", which also permits "throw-expression" 7718 and the use of assignment operators. In the case that 7719 ALLOW_NON_CONSTANT_P is false, we get better errors than we would 7720 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is 7721 actually essential that we look for an assignment-expression. 7722 For example, cp_parser_initializer_clauses uses this function to 7723 determine whether a particular assignment-expression is in fact 7724 constant. */ 7725 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL); 7726 /* Restore the old settings. */ 7727 parser->integral_constant_expression_p 7728 = saved_integral_constant_expression_p; 7729 parser->allow_non_integral_constant_expression_p 7730 = saved_allow_non_integral_constant_expression_p; 7731 if (cxx_dialect >= cxx0x) 7732 { 7733 /* Require an rvalue constant expression here; that's what our 7734 callers expect. Reference constant expressions are handled 7735 separately in e.g. cp_parser_template_argument. */ 7736 bool is_const = potential_rvalue_constant_expression (expression); 7737 parser->non_integral_constant_expression_p = !is_const; 7738 if (!is_const && !allow_non_constant_p) 7739 require_potential_rvalue_constant_expression (expression); 7740 } 7741 if (allow_non_constant_p) 7742 *non_constant_p = parser->non_integral_constant_expression_p; 7743 parser->non_integral_constant_expression_p 7744 = saved_non_integral_constant_expression_p; 7745 7746 return expression; 7747 } 7748 7749 /* Parse __builtin_offsetof. 7750 7751 offsetof-expression: 7752 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")" 7753 7754 offsetof-member-designator: 7755 id-expression 7756 | offsetof-member-designator "." id-expression 7757 | offsetof-member-designator "[" expression "]" 7758 | offsetof-member-designator "->" id-expression */ 7759 7760 static tree 7761 cp_parser_builtin_offsetof (cp_parser *parser) 7762 { 7763 int save_ice_p, save_non_ice_p; 7764 tree type, expr; 7765 cp_id_kind dummy; 7766 cp_token *token; 7767 7768 /* We're about to accept non-integral-constant things, but will 7769 definitely yield an integral constant expression. Save and 7770 restore these values around our local parsing. */ 7771 save_ice_p = parser->integral_constant_expression_p; 7772 save_non_ice_p = parser->non_integral_constant_expression_p; 7773 7774 /* Consume the "__builtin_offsetof" token. */ 7775 cp_lexer_consume_token (parser->lexer); 7776 /* Consume the opening `('. */ 7777 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 7778 /* Parse the type-id. */ 7779 type = cp_parser_type_id (parser); 7780 /* Look for the `,'. */ 7781 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 7782 token = cp_lexer_peek_token (parser->lexer); 7783 7784 /* Build the (type *)null that begins the traditional offsetof macro. */ 7785 expr = build_static_cast (build_pointer_type (type), null_pointer_node, 7786 tf_warning_or_error); 7787 7788 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */ 7789 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr, 7790 true, &dummy, token->location); 7791 while (true) 7792 { 7793 token = cp_lexer_peek_token (parser->lexer); 7794 switch (token->type) 7795 { 7796 case CPP_OPEN_SQUARE: 7797 /* offsetof-member-designator "[" expression "]" */ 7798 expr = cp_parser_postfix_open_square_expression (parser, expr, true); 7799 break; 7800 7801 case CPP_DEREF: 7802 /* offsetof-member-designator "->" identifier */ 7803 expr = grok_array_decl (expr, integer_zero_node); 7804 /* FALLTHRU */ 7805 7806 case CPP_DOT: 7807 /* offsetof-member-designator "." identifier */ 7808 cp_lexer_consume_token (parser->lexer); 7809 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, 7810 expr, true, &dummy, 7811 token->location); 7812 break; 7813 7814 case CPP_CLOSE_PAREN: 7815 /* Consume the ")" token. */ 7816 cp_lexer_consume_token (parser->lexer); 7817 goto success; 7818 7819 default: 7820 /* Error. We know the following require will fail, but 7821 that gives the proper error message. */ 7822 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 7823 cp_parser_skip_to_closing_parenthesis (parser, true, false, true); 7824 expr = error_mark_node; 7825 goto failure; 7826 } 7827 } 7828 7829 success: 7830 /* If we're processing a template, we can't finish the semantics yet. 7831 Otherwise we can fold the entire expression now. */ 7832 if (processing_template_decl) 7833 expr = build1 (OFFSETOF_EXPR, size_type_node, expr); 7834 else 7835 expr = finish_offsetof (expr); 7836 7837 failure: 7838 parser->integral_constant_expression_p = save_ice_p; 7839 parser->non_integral_constant_expression_p = save_non_ice_p; 7840 7841 return expr; 7842 } 7843 7844 /* Parse a trait expression. 7845 7846 Returns a representation of the expression, the underlying type 7847 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */ 7848 7849 static tree 7850 cp_parser_trait_expr (cp_parser* parser, enum rid keyword) 7851 { 7852 cp_trait_kind kind; 7853 tree type1, type2 = NULL_TREE; 7854 bool binary = false; 7855 cp_decl_specifier_seq decl_specs; 7856 7857 switch (keyword) 7858 { 7859 case RID_HAS_NOTHROW_ASSIGN: 7860 kind = CPTK_HAS_NOTHROW_ASSIGN; 7861 break; 7862 case RID_HAS_NOTHROW_CONSTRUCTOR: 7863 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR; 7864 break; 7865 case RID_HAS_NOTHROW_COPY: 7866 kind = CPTK_HAS_NOTHROW_COPY; 7867 break; 7868 case RID_HAS_TRIVIAL_ASSIGN: 7869 kind = CPTK_HAS_TRIVIAL_ASSIGN; 7870 break; 7871 case RID_HAS_TRIVIAL_CONSTRUCTOR: 7872 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR; 7873 break; 7874 case RID_HAS_TRIVIAL_COPY: 7875 kind = CPTK_HAS_TRIVIAL_COPY; 7876 break; 7877 case RID_HAS_TRIVIAL_DESTRUCTOR: 7878 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR; 7879 break; 7880 case RID_HAS_VIRTUAL_DESTRUCTOR: 7881 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR; 7882 break; 7883 case RID_IS_ABSTRACT: 7884 kind = CPTK_IS_ABSTRACT; 7885 break; 7886 case RID_IS_BASE_OF: 7887 kind = CPTK_IS_BASE_OF; 7888 binary = true; 7889 break; 7890 case RID_IS_CLASS: 7891 kind = CPTK_IS_CLASS; 7892 break; 7893 case RID_IS_CONVERTIBLE_TO: 7894 kind = CPTK_IS_CONVERTIBLE_TO; 7895 binary = true; 7896 break; 7897 case RID_IS_EMPTY: 7898 kind = CPTK_IS_EMPTY; 7899 break; 7900 case RID_IS_ENUM: 7901 kind = CPTK_IS_ENUM; 7902 break; 7903 case RID_IS_FINAL: 7904 kind = CPTK_IS_FINAL; 7905 break; 7906 case RID_IS_LITERAL_TYPE: 7907 kind = CPTK_IS_LITERAL_TYPE; 7908 break; 7909 case RID_IS_POD: 7910 kind = CPTK_IS_POD; 7911 break; 7912 case RID_IS_POLYMORPHIC: 7913 kind = CPTK_IS_POLYMORPHIC; 7914 break; 7915 case RID_IS_STD_LAYOUT: 7916 kind = CPTK_IS_STD_LAYOUT; 7917 break; 7918 case RID_IS_TRIVIAL: 7919 kind = CPTK_IS_TRIVIAL; 7920 break; 7921 case RID_IS_UNION: 7922 kind = CPTK_IS_UNION; 7923 break; 7924 case RID_UNDERLYING_TYPE: 7925 kind = CPTK_UNDERLYING_TYPE; 7926 break; 7927 case RID_BASES: 7928 kind = CPTK_BASES; 7929 break; 7930 case RID_DIRECT_BASES: 7931 kind = CPTK_DIRECT_BASES; 7932 break; 7933 default: 7934 gcc_unreachable (); 7935 } 7936 7937 /* Consume the token. */ 7938 cp_lexer_consume_token (parser->lexer); 7939 7940 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 7941 7942 type1 = cp_parser_type_id (parser); 7943 7944 if (type1 == error_mark_node) 7945 return error_mark_node; 7946 7947 /* Build a trivial decl-specifier-seq. */ 7948 clear_decl_specs (&decl_specs); 7949 decl_specs.type = type1; 7950 7951 /* Call grokdeclarator to figure out what type this is. */ 7952 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME, 7953 /*initialized=*/0, /*attrlist=*/NULL); 7954 7955 if (binary) 7956 { 7957 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 7958 7959 type2 = cp_parser_type_id (parser); 7960 7961 if (type2 == error_mark_node) 7962 return error_mark_node; 7963 7964 /* Build a trivial decl-specifier-seq. */ 7965 clear_decl_specs (&decl_specs); 7966 decl_specs.type = type2; 7967 7968 /* Call grokdeclarator to figure out what type this is. */ 7969 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME, 7970 /*initialized=*/0, /*attrlist=*/NULL); 7971 } 7972 7973 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 7974 7975 /* Complete the trait expression, which may mean either processing 7976 the trait expr now or saving it for template instantiation. */ 7977 switch(kind) 7978 { 7979 case CPTK_UNDERLYING_TYPE: 7980 return finish_underlying_type (type1); 7981 case CPTK_BASES: 7982 return finish_bases (type1, false); 7983 case CPTK_DIRECT_BASES: 7984 return finish_bases (type1, true); 7985 default: 7986 return finish_trait_expr (kind, type1, type2); 7987 } 7988 } 7989 7990 /* Lambdas that appear in variable initializer or default argument scope 7991 get that in their mangling, so we need to record it. We might as well 7992 use the count for function and namespace scopes as well. */ 7993 static GTY(()) tree lambda_scope; 7994 static GTY(()) int lambda_count; 7995 typedef struct GTY(()) tree_int 7996 { 7997 tree t; 7998 int i; 7999 } tree_int; 8000 DEF_VEC_O(tree_int); 8001 DEF_VEC_ALLOC_O(tree_int,gc); 8002 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack; 8003 8004 static void 8005 start_lambda_scope (tree decl) 8006 { 8007 tree_int ti; 8008 gcc_assert (decl); 8009 /* Once we're inside a function, we ignore other scopes and just push 8010 the function again so that popping works properly. */ 8011 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL) 8012 decl = current_function_decl; 8013 ti.t = lambda_scope; 8014 ti.i = lambda_count; 8015 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti); 8016 if (lambda_scope != decl) 8017 { 8018 /* Don't reset the count if we're still in the same function. */ 8019 lambda_scope = decl; 8020 lambda_count = 0; 8021 } 8022 } 8023 8024 static void 8025 record_lambda_scope (tree lambda) 8026 { 8027 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope; 8028 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++; 8029 } 8030 8031 static void 8032 finish_lambda_scope (void) 8033 { 8034 tree_int *p = VEC_last (tree_int, lambda_scope_stack); 8035 if (lambda_scope != p->t) 8036 { 8037 lambda_scope = p->t; 8038 lambda_count = p->i; 8039 } 8040 VEC_pop (tree_int, lambda_scope_stack); 8041 } 8042 8043 /* Parse a lambda expression. 8044 8045 lambda-expression: 8046 lambda-introducer lambda-declarator [opt] compound-statement 8047 8048 Returns a representation of the expression. */ 8049 8050 static tree 8051 cp_parser_lambda_expression (cp_parser* parser) 8052 { 8053 tree lambda_expr = build_lambda_expr (); 8054 tree type; 8055 bool ok; 8056 8057 LAMBDA_EXPR_LOCATION (lambda_expr) 8058 = cp_lexer_peek_token (parser->lexer)->location; 8059 8060 if (cp_unevaluated_operand) 8061 error_at (LAMBDA_EXPR_LOCATION (lambda_expr), 8062 "lambda-expression in unevaluated context"); 8063 8064 /* We may be in the middle of deferred access check. Disable 8065 it now. */ 8066 push_deferring_access_checks (dk_no_deferred); 8067 8068 cp_parser_lambda_introducer (parser, lambda_expr); 8069 8070 type = begin_lambda_type (lambda_expr); 8071 if (type == error_mark_node) 8072 return error_mark_node; 8073 8074 record_lambda_scope (lambda_expr); 8075 8076 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */ 8077 determine_visibility (TYPE_NAME (type)); 8078 8079 /* Now that we've started the type, add the capture fields for any 8080 explicit captures. */ 8081 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)); 8082 8083 { 8084 /* Inside the class, surrounding template-parameter-lists do not apply. */ 8085 unsigned int saved_num_template_parameter_lists 8086 = parser->num_template_parameter_lists; 8087 unsigned char in_statement = parser->in_statement; 8088 bool in_switch_statement_p = parser->in_switch_statement_p; 8089 8090 parser->num_template_parameter_lists = 0; 8091 parser->in_statement = 0; 8092 parser->in_switch_statement_p = false; 8093 8094 /* By virtue of defining a local class, a lambda expression has access to 8095 the private variables of enclosing classes. */ 8096 8097 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr); 8098 8099 if (ok) 8100 cp_parser_lambda_body (parser, lambda_expr); 8101 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 8102 cp_parser_skip_to_end_of_block_or_statement (parser); 8103 8104 /* The capture list was built up in reverse order; fix that now. */ 8105 { 8106 tree newlist = NULL_TREE; 8107 tree elt, next; 8108 8109 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); 8110 elt; elt = next) 8111 { 8112 next = TREE_CHAIN (elt); 8113 TREE_CHAIN (elt) = newlist; 8114 newlist = elt; 8115 } 8116 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist; 8117 } 8118 8119 if (ok) 8120 maybe_add_lambda_conv_op (type); 8121 8122 type = finish_struct (type, /*attributes=*/NULL_TREE); 8123 8124 parser->num_template_parameter_lists = saved_num_template_parameter_lists; 8125 parser->in_statement = in_statement; 8126 parser->in_switch_statement_p = in_switch_statement_p; 8127 } 8128 8129 pop_deferring_access_checks (); 8130 8131 /* This field is only used during parsing of the lambda. */ 8132 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE; 8133 8134 /* This lambda shouldn't have any proxies left at this point. */ 8135 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL); 8136 /* And now that we're done, push proxies for an enclosing lambda. */ 8137 insert_pending_capture_proxies (); 8138 8139 if (ok) 8140 return build_lambda_object (lambda_expr); 8141 else 8142 return error_mark_node; 8143 } 8144 8145 /* Parse the beginning of a lambda expression. 8146 8147 lambda-introducer: 8148 [ lambda-capture [opt] ] 8149 8150 LAMBDA_EXPR is the current representation of the lambda expression. */ 8151 8152 static void 8153 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr) 8154 { 8155 /* Need commas after the first capture. */ 8156 bool first = true; 8157 8158 /* Eat the leading `['. */ 8159 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); 8160 8161 /* Record default capture mode. "[&" "[=" "[&," "[=," */ 8162 if (cp_lexer_next_token_is (parser->lexer, CPP_AND) 8163 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME) 8164 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE; 8165 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 8166 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY; 8167 8168 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE) 8169 { 8170 cp_lexer_consume_token (parser->lexer); 8171 first = false; 8172 } 8173 8174 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE)) 8175 { 8176 cp_token* capture_token; 8177 tree capture_id; 8178 tree capture_init_expr; 8179 cp_id_kind idk = CP_ID_KIND_NONE; 8180 bool explicit_init_p = false; 8181 8182 enum capture_kind_type 8183 { 8184 BY_COPY, 8185 BY_REFERENCE 8186 }; 8187 enum capture_kind_type capture_kind = BY_COPY; 8188 8189 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) 8190 { 8191 error ("expected end of capture-list"); 8192 return; 8193 } 8194 8195 if (first) 8196 first = false; 8197 else 8198 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 8199 8200 /* Possibly capture `this'. */ 8201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS)) 8202 { 8203 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 8204 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY) 8205 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant " 8206 "with by-copy capture default"); 8207 cp_lexer_consume_token (parser->lexer); 8208 add_capture (lambda_expr, 8209 /*id=*/this_identifier, 8210 /*initializer=*/finish_this_expr(), 8211 /*by_reference_p=*/false, 8212 explicit_init_p); 8213 continue; 8214 } 8215 8216 /* Remember whether we want to capture as a reference or not. */ 8217 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)) 8218 { 8219 capture_kind = BY_REFERENCE; 8220 cp_lexer_consume_token (parser->lexer); 8221 } 8222 8223 /* Get the identifier. */ 8224 capture_token = cp_lexer_peek_token (parser->lexer); 8225 capture_id = cp_parser_identifier (parser); 8226 8227 if (capture_id == error_mark_node) 8228 /* Would be nice to have a cp_parser_skip_to_closing_x for general 8229 delimiters, but I modified this to stop on unnested ']' as well. It 8230 was already changed to stop on unnested '}', so the 8231 "closing_parenthesis" name is no more misleading with my change. */ 8232 { 8233 cp_parser_skip_to_closing_parenthesis (parser, 8234 /*recovering=*/true, 8235 /*or_comma=*/true, 8236 /*consume_paren=*/true); 8237 break; 8238 } 8239 8240 /* Find the initializer for this capture. */ 8241 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 8242 { 8243 /* An explicit expression exists. */ 8244 cp_lexer_consume_token (parser->lexer); 8245 pedwarn (input_location, OPT_pedantic, 8246 "ISO C++ does not allow initializers " 8247 "in lambda expression capture lists"); 8248 capture_init_expr = cp_parser_assignment_expression (parser, 8249 /*cast_p=*/true, 8250 &idk); 8251 explicit_init_p = true; 8252 } 8253 else 8254 { 8255 const char* error_msg; 8256 8257 /* Turn the identifier into an id-expression. */ 8258 capture_init_expr 8259 = cp_parser_lookup_name 8260 (parser, 8261 capture_id, 8262 none_type, 8263 /*is_template=*/false, 8264 /*is_namespace=*/false, 8265 /*check_dependency=*/true, 8266 /*ambiguous_decls=*/NULL, 8267 capture_token->location); 8268 8269 if (capture_init_expr == error_mark_node) 8270 { 8271 unqualified_name_lookup_error (capture_id); 8272 continue; 8273 } 8274 else if (DECL_P (capture_init_expr) 8275 && (TREE_CODE (capture_init_expr) != VAR_DECL 8276 && TREE_CODE (capture_init_expr) != PARM_DECL)) 8277 { 8278 error_at (capture_token->location, 8279 "capture of non-variable %qD ", 8280 capture_init_expr); 8281 inform (0, "%q+#D declared here", capture_init_expr); 8282 continue; 8283 } 8284 if (TREE_CODE (capture_init_expr) == VAR_DECL 8285 && decl_storage_duration (capture_init_expr) != dk_auto) 8286 { 8287 pedwarn (capture_token->location, 0, "capture of variable " 8288 "%qD with non-automatic storage duration", 8289 capture_init_expr); 8290 inform (0, "%q+#D declared here", capture_init_expr); 8291 continue; 8292 } 8293 8294 capture_init_expr 8295 = finish_id_expression 8296 (capture_id, 8297 capture_init_expr, 8298 parser->scope, 8299 &idk, 8300 /*integral_constant_expression_p=*/false, 8301 /*allow_non_integral_constant_expression_p=*/false, 8302 /*non_integral_constant_expression_p=*/NULL, 8303 /*template_p=*/false, 8304 /*done=*/true, 8305 /*address_p=*/false, 8306 /*template_arg_p=*/false, 8307 &error_msg, 8308 capture_token->location); 8309 } 8310 8311 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE 8312 && !explicit_init_p) 8313 { 8314 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY 8315 && capture_kind == BY_COPY) 8316 pedwarn (capture_token->location, 0, "explicit by-copy capture " 8317 "of %qD redundant with by-copy capture default", 8318 capture_id); 8319 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE 8320 && capture_kind == BY_REFERENCE) 8321 pedwarn (capture_token->location, 0, "explicit by-reference " 8322 "capture of %qD redundant with by-reference capture " 8323 "default", capture_id); 8324 } 8325 8326 add_capture (lambda_expr, 8327 capture_id, 8328 capture_init_expr, 8329 /*by_reference_p=*/capture_kind == BY_REFERENCE, 8330 explicit_init_p); 8331 } 8332 8333 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 8334 } 8335 8336 /* Parse the (optional) middle of a lambda expression. 8337 8338 lambda-declarator: 8339 ( parameter-declaration-clause [opt] ) 8340 attribute-specifier [opt] 8341 mutable [opt] 8342 exception-specification [opt] 8343 lambda-return-type-clause [opt] 8344 8345 LAMBDA_EXPR is the current representation of the lambda expression. */ 8346 8347 static bool 8348 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr) 8349 { 8350 /* 5.1.1.4 of the standard says: 8351 If a lambda-expression does not include a lambda-declarator, it is as if 8352 the lambda-declarator were (). 8353 This means an empty parameter list, no attributes, and no exception 8354 specification. */ 8355 tree param_list = void_list_node; 8356 tree attributes = NULL_TREE; 8357 tree exception_spec = NULL_TREE; 8358 tree t; 8359 8360 /* The lambda-declarator is optional, but must begin with an opening 8361 parenthesis if present. */ 8362 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 8363 { 8364 cp_lexer_consume_token (parser->lexer); 8365 8366 begin_scope (sk_function_parms, /*entity=*/NULL_TREE); 8367 8368 /* Parse parameters. */ 8369 param_list = cp_parser_parameter_declaration_clause (parser); 8370 8371 /* Default arguments shall not be specified in the 8372 parameter-declaration-clause of a lambda-declarator. */ 8373 for (t = param_list; t; t = TREE_CHAIN (t)) 8374 if (TREE_PURPOSE (t)) 8375 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic, 8376 "default argument specified for lambda parameter"); 8377 8378 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 8379 8380 attributes = cp_parser_attributes_opt (parser); 8381 8382 /* Parse optional `mutable' keyword. */ 8383 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE)) 8384 { 8385 cp_lexer_consume_token (parser->lexer); 8386 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1; 8387 } 8388 8389 /* Parse optional exception specification. */ 8390 exception_spec = cp_parser_exception_specification_opt (parser); 8391 8392 /* Parse optional trailing return type. */ 8393 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF)) 8394 { 8395 cp_lexer_consume_token (parser->lexer); 8396 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser); 8397 } 8398 8399 /* The function parameters must be in scope all the way until after the 8400 trailing-return-type in case of decltype. */ 8401 for (t = current_binding_level->names; t; t = DECL_CHAIN (t)) 8402 pop_binding (DECL_NAME (t), t); 8403 8404 leave_scope (); 8405 } 8406 8407 /* Create the function call operator. 8408 8409 Messing with declarators like this is no uglier than building up the 8410 FUNCTION_DECL by hand, and this is less likely to get out of sync with 8411 other code. */ 8412 { 8413 cp_decl_specifier_seq return_type_specs; 8414 cp_declarator* declarator; 8415 tree fco; 8416 int quals; 8417 void *p; 8418 8419 clear_decl_specs (&return_type_specs); 8420 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr)) 8421 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr); 8422 else 8423 /* Maybe we will deduce the return type later, but we can use void 8424 as a placeholder return type anyways. */ 8425 return_type_specs.type = void_type_node; 8426 8427 p = obstack_alloc (&declarator_obstack, 0); 8428 8429 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR), 8430 sfk_none); 8431 8432 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr) 8433 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST); 8434 declarator = make_call_declarator (declarator, param_list, quals, 8435 VIRT_SPEC_UNSPECIFIED, 8436 exception_spec, 8437 /*late_return_type=*/NULL_TREE); 8438 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr); 8439 8440 fco = grokmethod (&return_type_specs, 8441 declarator, 8442 attributes); 8443 if (fco != error_mark_node) 8444 { 8445 DECL_INITIALIZED_IN_CLASS_P (fco) = 1; 8446 DECL_ARTIFICIAL (fco) = 1; 8447 /* Give the object parameter a different name. */ 8448 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure"); 8449 } 8450 8451 finish_member_declaration (fco); 8452 8453 obstack_free (&declarator_obstack, p); 8454 8455 return (fco != error_mark_node); 8456 } 8457 } 8458 8459 /* Parse the body of a lambda expression, which is simply 8460 8461 compound-statement 8462 8463 but which requires special handling. 8464 LAMBDA_EXPR is the current representation of the lambda expression. */ 8465 8466 static void 8467 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr) 8468 { 8469 bool nested = (current_function_decl != NULL_TREE); 8470 bool local_variables_forbidden_p = parser->local_variables_forbidden_p; 8471 if (nested) 8472 push_function_context (); 8473 else 8474 /* Still increment function_depth so that we don't GC in the 8475 middle of an expression. */ 8476 ++function_depth; 8477 /* Clear this in case we're in the middle of a default argument. */ 8478 parser->local_variables_forbidden_p = false; 8479 8480 /* Finish the function call operator 8481 - class_specifier 8482 + late_parsing_for_member 8483 + function_definition_after_declarator 8484 + ctor_initializer_opt_and_function_body */ 8485 { 8486 tree fco = lambda_function (lambda_expr); 8487 tree body; 8488 bool done = false; 8489 tree compound_stmt; 8490 tree cap; 8491 8492 /* Let the front end know that we are going to be defining this 8493 function. */ 8494 start_preparsed_function (fco, 8495 NULL_TREE, 8496 SF_PRE_PARSED | SF_INCLASS_INLINE); 8497 8498 start_lambda_scope (fco); 8499 body = begin_function_body (); 8500 8501 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 8502 goto out; 8503 8504 /* Push the proxies for any explicit captures. */ 8505 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap; 8506 cap = TREE_CHAIN (cap)) 8507 build_capture_proxy (TREE_PURPOSE (cap)); 8508 8509 compound_stmt = begin_compound_stmt (0); 8510 8511 /* 5.1.1.4 of the standard says: 8512 If a lambda-expression does not include a trailing-return-type, it 8513 is as if the trailing-return-type denotes the following type: 8514 * if the compound-statement is of the form 8515 { return attribute-specifier [opt] expression ; } 8516 the type of the returned expression after lvalue-to-rvalue 8517 conversion (_conv.lval_ 4.1), array-to-pointer conversion 8518 (_conv.array_ 4.2), and function-to-pointer conversion 8519 (_conv.func_ 4.3); 8520 * otherwise, void. */ 8521 8522 /* In a lambda that has neither a lambda-return-type-clause 8523 nor a deducible form, errors should be reported for return statements 8524 in the body. Since we used void as the placeholder return type, parsing 8525 the body as usual will give such desired behavior. */ 8526 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr) 8527 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN 8528 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON) 8529 { 8530 tree expr = NULL_TREE; 8531 cp_id_kind idk = CP_ID_KIND_NONE; 8532 8533 /* Parse tentatively in case there's more after the initial return 8534 statement. */ 8535 cp_parser_parse_tentatively (parser); 8536 8537 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN); 8538 8539 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk); 8540 8541 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 8542 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 8543 8544 if (cp_parser_parse_definitely (parser)) 8545 { 8546 apply_lambda_return_type (lambda_expr, lambda_return_type (expr)); 8547 8548 /* Will get error here if type not deduced yet. */ 8549 finish_return_stmt (expr); 8550 8551 done = true; 8552 } 8553 } 8554 8555 if (!done) 8556 { 8557 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)) 8558 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true; 8559 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL)) 8560 cp_parser_label_declaration (parser); 8561 cp_parser_statement_seq_opt (parser, NULL_TREE); 8562 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 8563 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false; 8564 } 8565 8566 finish_compound_stmt (compound_stmt); 8567 8568 out: 8569 finish_function_body (body); 8570 finish_lambda_scope (); 8571 8572 /* Finish the function and generate code for it if necessary. */ 8573 expand_or_defer_fn (finish_function (/*inline*/2)); 8574 } 8575 8576 parser->local_variables_forbidden_p = local_variables_forbidden_p; 8577 if (nested) 8578 pop_function_context(); 8579 else 8580 --function_depth; 8581 } 8582 8583 /* Statements [gram.stmt.stmt] */ 8584 8585 /* Parse a statement. 8586 8587 statement: 8588 labeled-statement 8589 expression-statement 8590 compound-statement 8591 selection-statement 8592 iteration-statement 8593 jump-statement 8594 declaration-statement 8595 try-block 8596 8597 TM Extension: 8598 8599 statement: 8600 atomic-statement 8601 8602 IN_COMPOUND is true when the statement is nested inside a 8603 cp_parser_compound_statement; this matters for certain pragmas. 8604 8605 If IF_P is not NULL, *IF_P is set to indicate whether the statement 8606 is a (possibly labeled) if statement which is not enclosed in braces 8607 and has an else clause. This is used to implement -Wparentheses. */ 8608 8609 static void 8610 cp_parser_statement (cp_parser* parser, tree in_statement_expr, 8611 bool in_compound, bool *if_p) 8612 { 8613 tree statement; 8614 cp_token *token; 8615 location_t statement_location; 8616 8617 restart: 8618 if (if_p != NULL) 8619 *if_p = false; 8620 /* There is no statement yet. */ 8621 statement = NULL_TREE; 8622 /* Peek at the next token. */ 8623 token = cp_lexer_peek_token (parser->lexer); 8624 /* Remember the location of the first token in the statement. */ 8625 statement_location = token->location; 8626 /* If this is a keyword, then that will often determine what kind of 8627 statement we have. */ 8628 if (token->type == CPP_KEYWORD) 8629 { 8630 enum rid keyword = token->keyword; 8631 8632 switch (keyword) 8633 { 8634 case RID_CASE: 8635 case RID_DEFAULT: 8636 /* Looks like a labeled-statement with a case label. 8637 Parse the label, and then use tail recursion to parse 8638 the statement. */ 8639 cp_parser_label_for_labeled_statement (parser); 8640 goto restart; 8641 8642 case RID_IF: 8643 case RID_SWITCH: 8644 statement = cp_parser_selection_statement (parser, if_p); 8645 break; 8646 8647 case RID_WHILE: 8648 case RID_DO: 8649 case RID_FOR: 8650 statement = cp_parser_iteration_statement (parser); 8651 break; 8652 8653 case RID_BREAK: 8654 case RID_CONTINUE: 8655 case RID_RETURN: 8656 case RID_GOTO: 8657 statement = cp_parser_jump_statement (parser); 8658 break; 8659 8660 /* Objective-C++ exception-handling constructs. */ 8661 case RID_AT_TRY: 8662 case RID_AT_CATCH: 8663 case RID_AT_FINALLY: 8664 case RID_AT_SYNCHRONIZED: 8665 case RID_AT_THROW: 8666 statement = cp_parser_objc_statement (parser); 8667 break; 8668 8669 case RID_TRY: 8670 statement = cp_parser_try_block (parser); 8671 break; 8672 8673 case RID_NAMESPACE: 8674 /* This must be a namespace alias definition. */ 8675 cp_parser_declaration_statement (parser); 8676 return; 8677 8678 case RID_TRANSACTION_ATOMIC: 8679 case RID_TRANSACTION_RELAXED: 8680 statement = cp_parser_transaction (parser, keyword); 8681 break; 8682 case RID_TRANSACTION_CANCEL: 8683 statement = cp_parser_transaction_cancel (parser); 8684 break; 8685 8686 default: 8687 /* It might be a keyword like `int' that can start a 8688 declaration-statement. */ 8689 break; 8690 } 8691 } 8692 else if (token->type == CPP_NAME) 8693 { 8694 /* If the next token is a `:', then we are looking at a 8695 labeled-statement. */ 8696 token = cp_lexer_peek_nth_token (parser->lexer, 2); 8697 if (token->type == CPP_COLON) 8698 { 8699 /* Looks like a labeled-statement with an ordinary label. 8700 Parse the label, and then use tail recursion to parse 8701 the statement. */ 8702 cp_parser_label_for_labeled_statement (parser); 8703 goto restart; 8704 } 8705 } 8706 /* Anything that starts with a `{' must be a compound-statement. */ 8707 else if (token->type == CPP_OPEN_BRACE) 8708 statement = cp_parser_compound_statement (parser, NULL, false, false); 8709 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes 8710 a statement all its own. */ 8711 else if (token->type == CPP_PRAGMA) 8712 { 8713 /* Only certain OpenMP pragmas are attached to statements, and thus 8714 are considered statements themselves. All others are not. In 8715 the context of a compound, accept the pragma as a "statement" and 8716 return so that we can check for a close brace. Otherwise we 8717 require a real statement and must go back and read one. */ 8718 if (in_compound) 8719 cp_parser_pragma (parser, pragma_compound); 8720 else if (!cp_parser_pragma (parser, pragma_stmt)) 8721 goto restart; 8722 return; 8723 } 8724 else if (token->type == CPP_EOF) 8725 { 8726 cp_parser_error (parser, "expected statement"); 8727 return; 8728 } 8729 8730 /* Everything else must be a declaration-statement or an 8731 expression-statement. Try for the declaration-statement 8732 first, unless we are looking at a `;', in which case we know that 8733 we have an expression-statement. */ 8734 if (!statement) 8735 { 8736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 8737 { 8738 cp_parser_parse_tentatively (parser); 8739 /* Try to parse the declaration-statement. */ 8740 cp_parser_declaration_statement (parser); 8741 /* If that worked, we're done. */ 8742 if (cp_parser_parse_definitely (parser)) 8743 return; 8744 } 8745 /* Look for an expression-statement instead. */ 8746 statement = cp_parser_expression_statement (parser, in_statement_expr); 8747 } 8748 8749 /* Set the line number for the statement. */ 8750 if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) 8751 SET_EXPR_LOCATION (statement, statement_location); 8752 } 8753 8754 /* Parse the label for a labeled-statement, i.e. 8755 8756 identifier : 8757 case constant-expression : 8758 default : 8759 8760 GNU Extension: 8761 case constant-expression ... constant-expression : statement 8762 8763 When a label is parsed without errors, the label is added to the 8764 parse tree by the finish_* functions, so this function doesn't 8765 have to return the label. */ 8766 8767 static void 8768 cp_parser_label_for_labeled_statement (cp_parser* parser) 8769 { 8770 cp_token *token; 8771 tree label = NULL_TREE; 8772 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 8773 8774 /* The next token should be an identifier. */ 8775 token = cp_lexer_peek_token (parser->lexer); 8776 if (token->type != CPP_NAME 8777 && token->type != CPP_KEYWORD) 8778 { 8779 cp_parser_error (parser, "expected labeled-statement"); 8780 return; 8781 } 8782 8783 parser->colon_corrects_to_scope_p = false; 8784 switch (token->keyword) 8785 { 8786 case RID_CASE: 8787 { 8788 tree expr, expr_hi; 8789 cp_token *ellipsis; 8790 8791 /* Consume the `case' token. */ 8792 cp_lexer_consume_token (parser->lexer); 8793 /* Parse the constant-expression. */ 8794 expr = cp_parser_constant_expression (parser, 8795 /*allow_non_constant_p=*/false, 8796 NULL); 8797 8798 ellipsis = cp_lexer_peek_token (parser->lexer); 8799 if (ellipsis->type == CPP_ELLIPSIS) 8800 { 8801 /* Consume the `...' token. */ 8802 cp_lexer_consume_token (parser->lexer); 8803 expr_hi = 8804 cp_parser_constant_expression (parser, 8805 /*allow_non_constant_p=*/false, 8806 NULL); 8807 /* We don't need to emit warnings here, as the common code 8808 will do this for us. */ 8809 } 8810 else 8811 expr_hi = NULL_TREE; 8812 8813 if (parser->in_switch_statement_p) 8814 finish_case_label (token->location, expr, expr_hi); 8815 else 8816 error_at (token->location, 8817 "case label %qE not within a switch statement", 8818 expr); 8819 } 8820 break; 8821 8822 case RID_DEFAULT: 8823 /* Consume the `default' token. */ 8824 cp_lexer_consume_token (parser->lexer); 8825 8826 if (parser->in_switch_statement_p) 8827 finish_case_label (token->location, NULL_TREE, NULL_TREE); 8828 else 8829 error_at (token->location, "case label not within a switch statement"); 8830 break; 8831 8832 default: 8833 /* Anything else must be an ordinary label. */ 8834 label = finish_label_stmt (cp_parser_identifier (parser)); 8835 break; 8836 } 8837 8838 /* Require the `:' token. */ 8839 cp_parser_require (parser, CPP_COLON, RT_COLON); 8840 8841 /* An ordinary label may optionally be followed by attributes. 8842 However, this is only permitted if the attributes are then 8843 followed by a semicolon. This is because, for backward 8844 compatibility, when parsing 8845 lab: __attribute__ ((unused)) int i; 8846 we want the attribute to attach to "i", not "lab". */ 8847 if (label != NULL_TREE 8848 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) 8849 { 8850 tree attrs; 8851 8852 cp_parser_parse_tentatively (parser); 8853 attrs = cp_parser_attributes_opt (parser); 8854 if (attrs == NULL_TREE 8855 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 8856 cp_parser_abort_tentative_parse (parser); 8857 else if (!cp_parser_parse_definitely (parser)) 8858 ; 8859 else 8860 cplus_decl_attributes (&label, attrs, 0); 8861 } 8862 8863 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 8864 } 8865 8866 /* Parse an expression-statement. 8867 8868 expression-statement: 8869 expression [opt] ; 8870 8871 Returns the new EXPR_STMT -- or NULL_TREE if the expression 8872 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P 8873 indicates whether this expression-statement is part of an 8874 expression statement. */ 8875 8876 static tree 8877 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr) 8878 { 8879 tree statement = NULL_TREE; 8880 cp_token *token = cp_lexer_peek_token (parser->lexer); 8881 8882 /* If the next token is a ';', then there is no expression 8883 statement. */ 8884 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 8885 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL); 8886 8887 /* Give a helpful message for "A<T>::type t;" and the like. */ 8888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) 8889 && !cp_parser_uncommitted_to_tentative_parse_p (parser)) 8890 { 8891 if (TREE_CODE (statement) == SCOPE_REF) 8892 error_at (token->location, "need %<typename%> before %qE because " 8893 "%qT is a dependent scope", 8894 statement, TREE_OPERAND (statement, 0)); 8895 else if (is_overloaded_fn (statement) 8896 && DECL_CONSTRUCTOR_P (get_first_fn (statement))) 8897 { 8898 /* A::A a; */ 8899 tree fn = get_first_fn (statement); 8900 error_at (token->location, 8901 "%<%T::%D%> names the constructor, not the type", 8902 DECL_CONTEXT (fn), DECL_NAME (fn)); 8903 } 8904 } 8905 8906 /* Consume the final `;'. */ 8907 cp_parser_consume_semicolon_at_end_of_statement (parser); 8908 8909 if (in_statement_expr 8910 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 8911 /* This is the final expression statement of a statement 8912 expression. */ 8913 statement = finish_stmt_expr_expr (statement, in_statement_expr); 8914 else if (statement) 8915 statement = finish_expr_stmt (statement); 8916 else 8917 finish_stmt (); 8918 8919 return statement; 8920 } 8921 8922 /* Parse a compound-statement. 8923 8924 compound-statement: 8925 { statement-seq [opt] } 8926 8927 GNU extension: 8928 8929 compound-statement: 8930 { label-declaration-seq [opt] statement-seq [opt] } 8931 8932 label-declaration-seq: 8933 label-declaration 8934 label-declaration-seq label-declaration 8935 8936 Returns a tree representing the statement. */ 8937 8938 static tree 8939 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr, 8940 bool in_try, bool function_body) 8941 { 8942 tree compound_stmt; 8943 8944 /* Consume the `{'. */ 8945 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 8946 return error_mark_node; 8947 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl) 8948 && !function_body) 8949 pedwarn (input_location, OPT_pedantic, 8950 "compound-statement in constexpr function"); 8951 /* Begin the compound-statement. */ 8952 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0); 8953 /* If the next keyword is `__label__' we have a label declaration. */ 8954 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL)) 8955 cp_parser_label_declaration (parser); 8956 /* Parse an (optional) statement-seq. */ 8957 cp_parser_statement_seq_opt (parser, in_statement_expr); 8958 /* Finish the compound-statement. */ 8959 finish_compound_stmt (compound_stmt); 8960 /* Consume the `}'. */ 8961 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 8962 8963 return compound_stmt; 8964 } 8965 8966 /* Parse an (optional) statement-seq. 8967 8968 statement-seq: 8969 statement 8970 statement-seq [opt] statement */ 8971 8972 static void 8973 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr) 8974 { 8975 /* Scan statements until there aren't any more. */ 8976 while (true) 8977 { 8978 cp_token *token = cp_lexer_peek_token (parser->lexer); 8979 8980 /* If we are looking at a `}', then we have run out of 8981 statements; the same is true if we have reached the end 8982 of file, or have stumbled upon a stray '@end'. */ 8983 if (token->type == CPP_CLOSE_BRACE 8984 || token->type == CPP_EOF 8985 || token->type == CPP_PRAGMA_EOL 8986 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)) 8987 break; 8988 8989 /* If we are in a compound statement and find 'else' then 8990 something went wrong. */ 8991 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE) 8992 { 8993 if (parser->in_statement & IN_IF_STMT) 8994 break; 8995 else 8996 { 8997 token = cp_lexer_consume_token (parser->lexer); 8998 error_at (token->location, "%<else%> without a previous %<if%>"); 8999 } 9000 } 9001 9002 /* Parse the statement. */ 9003 cp_parser_statement (parser, in_statement_expr, true, NULL); 9004 } 9005 } 9006 9007 /* Parse a selection-statement. 9008 9009 selection-statement: 9010 if ( condition ) statement 9011 if ( condition ) statement else statement 9012 switch ( condition ) statement 9013 9014 Returns the new IF_STMT or SWITCH_STMT. 9015 9016 If IF_P is not NULL, *IF_P is set to indicate whether the statement 9017 is a (possibly labeled) if statement which is not enclosed in 9018 braces and has an else clause. This is used to implement 9019 -Wparentheses. */ 9020 9021 static tree 9022 cp_parser_selection_statement (cp_parser* parser, bool *if_p) 9023 { 9024 cp_token *token; 9025 enum rid keyword; 9026 9027 if (if_p != NULL) 9028 *if_p = false; 9029 9030 /* Peek at the next token. */ 9031 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT); 9032 9033 /* See what kind of keyword it is. */ 9034 keyword = token->keyword; 9035 switch (keyword) 9036 { 9037 case RID_IF: 9038 case RID_SWITCH: 9039 { 9040 tree statement; 9041 tree condition; 9042 9043 /* Look for the `('. */ 9044 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 9045 { 9046 cp_parser_skip_to_end_of_statement (parser); 9047 return error_mark_node; 9048 } 9049 9050 /* Begin the selection-statement. */ 9051 if (keyword == RID_IF) 9052 statement = begin_if_stmt (); 9053 else 9054 statement = begin_switch_stmt (); 9055 9056 /* Parse the condition. */ 9057 condition = cp_parser_condition (parser); 9058 /* Look for the `)'. */ 9059 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 9060 cp_parser_skip_to_closing_parenthesis (parser, true, false, 9061 /*consume_paren=*/true); 9062 9063 if (keyword == RID_IF) 9064 { 9065 bool nested_if; 9066 unsigned char in_statement; 9067 9068 /* Add the condition. */ 9069 finish_if_stmt_cond (condition, statement); 9070 9071 /* Parse the then-clause. */ 9072 in_statement = parser->in_statement; 9073 parser->in_statement |= IN_IF_STMT; 9074 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 9075 { 9076 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 9077 add_stmt (build_empty_stmt (loc)); 9078 cp_lexer_consume_token (parser->lexer); 9079 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE)) 9080 warning_at (loc, OPT_Wempty_body, "suggest braces around " 9081 "empty body in an %<if%> statement"); 9082 nested_if = false; 9083 } 9084 else 9085 cp_parser_implicitly_scoped_statement (parser, &nested_if); 9086 parser->in_statement = in_statement; 9087 9088 finish_then_clause (statement); 9089 9090 /* If the next token is `else', parse the else-clause. */ 9091 if (cp_lexer_next_token_is_keyword (parser->lexer, 9092 RID_ELSE)) 9093 { 9094 /* Consume the `else' keyword. */ 9095 cp_lexer_consume_token (parser->lexer); 9096 begin_else_clause (statement); 9097 /* Parse the else-clause. */ 9098 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 9099 { 9100 location_t loc; 9101 loc = cp_lexer_peek_token (parser->lexer)->location; 9102 warning_at (loc, 9103 OPT_Wempty_body, "suggest braces around " 9104 "empty body in an %<else%> statement"); 9105 add_stmt (build_empty_stmt (loc)); 9106 cp_lexer_consume_token (parser->lexer); 9107 } 9108 else 9109 cp_parser_implicitly_scoped_statement (parser, NULL); 9110 9111 finish_else_clause (statement); 9112 9113 /* If we are currently parsing a then-clause, then 9114 IF_P will not be NULL. We set it to true to 9115 indicate that this if statement has an else clause. 9116 This may trigger the Wparentheses warning below 9117 when we get back up to the parent if statement. */ 9118 if (if_p != NULL) 9119 *if_p = true; 9120 } 9121 else 9122 { 9123 /* This if statement does not have an else clause. If 9124 NESTED_IF is true, then the then-clause is an if 9125 statement which does have an else clause. We warn 9126 about the potential ambiguity. */ 9127 if (nested_if) 9128 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses, 9129 "suggest explicit braces to avoid ambiguous" 9130 " %<else%>"); 9131 } 9132 9133 /* Now we're all done with the if-statement. */ 9134 finish_if_stmt (statement); 9135 } 9136 else 9137 { 9138 bool in_switch_statement_p; 9139 unsigned char in_statement; 9140 9141 /* Add the condition. */ 9142 finish_switch_cond (condition, statement); 9143 9144 /* Parse the body of the switch-statement. */ 9145 in_switch_statement_p = parser->in_switch_statement_p; 9146 in_statement = parser->in_statement; 9147 parser->in_switch_statement_p = true; 9148 parser->in_statement |= IN_SWITCH_STMT; 9149 cp_parser_implicitly_scoped_statement (parser, NULL); 9150 parser->in_switch_statement_p = in_switch_statement_p; 9151 parser->in_statement = in_statement; 9152 9153 /* Now we're all done with the switch-statement. */ 9154 finish_switch_stmt (statement); 9155 } 9156 9157 return statement; 9158 } 9159 break; 9160 9161 default: 9162 cp_parser_error (parser, "expected selection-statement"); 9163 return error_mark_node; 9164 } 9165 } 9166 9167 /* Parse a condition. 9168 9169 condition: 9170 expression 9171 type-specifier-seq declarator = initializer-clause 9172 type-specifier-seq declarator braced-init-list 9173 9174 GNU Extension: 9175 9176 condition: 9177 type-specifier-seq declarator asm-specification [opt] 9178 attributes [opt] = assignment-expression 9179 9180 Returns the expression that should be tested. */ 9181 9182 static tree 9183 cp_parser_condition (cp_parser* parser) 9184 { 9185 cp_decl_specifier_seq type_specifiers; 9186 const char *saved_message; 9187 int declares_class_or_enum; 9188 9189 /* Try the declaration first. */ 9190 cp_parser_parse_tentatively (parser); 9191 /* New types are not allowed in the type-specifier-seq for a 9192 condition. */ 9193 saved_message = parser->type_definition_forbidden_message; 9194 parser->type_definition_forbidden_message 9195 = G_("types may not be defined in conditions"); 9196 /* Parse the type-specifier-seq. */ 9197 cp_parser_decl_specifier_seq (parser, 9198 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR, 9199 &type_specifiers, 9200 &declares_class_or_enum); 9201 /* Restore the saved message. */ 9202 parser->type_definition_forbidden_message = saved_message; 9203 /* If all is well, we might be looking at a declaration. */ 9204 if (!cp_parser_error_occurred (parser)) 9205 { 9206 tree decl; 9207 tree asm_specification; 9208 tree attributes; 9209 cp_declarator *declarator; 9210 tree initializer = NULL_TREE; 9211 9212 /* Parse the declarator. */ 9213 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 9214 /*ctor_dtor_or_conv_p=*/NULL, 9215 /*parenthesized_p=*/NULL, 9216 /*member_p=*/false); 9217 /* Parse the attributes. */ 9218 attributes = cp_parser_attributes_opt (parser); 9219 /* Parse the asm-specification. */ 9220 asm_specification = cp_parser_asm_specification_opt (parser); 9221 /* If the next token is not an `=' or '{', then we might still be 9222 looking at an expression. For example: 9223 9224 if (A(a).x) 9225 9226 looks like a decl-specifier-seq and a declarator -- but then 9227 there is no `=', so this is an expression. */ 9228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) 9229 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) 9230 cp_parser_simulate_error (parser); 9231 9232 /* If we did see an `=' or '{', then we are looking at a declaration 9233 for sure. */ 9234 if (cp_parser_parse_definitely (parser)) 9235 { 9236 tree pushed_scope; 9237 bool non_constant_p; 9238 bool flags = LOOKUP_ONLYCONVERTING; 9239 9240 /* Create the declaration. */ 9241 decl = start_decl (declarator, &type_specifiers, 9242 /*initialized_p=*/true, 9243 attributes, /*prefix_attributes=*/NULL_TREE, 9244 &pushed_scope); 9245 9246 /* Parse the initializer. */ 9247 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 9248 { 9249 initializer = cp_parser_braced_list (parser, &non_constant_p); 9250 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1; 9251 flags = 0; 9252 } 9253 else 9254 { 9255 /* Consume the `='. */ 9256 cp_parser_require (parser, CPP_EQ, RT_EQ); 9257 initializer = cp_parser_initializer_clause (parser, &non_constant_p); 9258 } 9259 if (BRACE_ENCLOSED_INITIALIZER_P (initializer)) 9260 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 9261 9262 /* Process the initializer. */ 9263 cp_finish_decl (decl, 9264 initializer, !non_constant_p, 9265 asm_specification, 9266 flags); 9267 9268 if (pushed_scope) 9269 pop_scope (pushed_scope); 9270 9271 return convert_from_reference (decl); 9272 } 9273 } 9274 /* If we didn't even get past the declarator successfully, we are 9275 definitely not looking at a declaration. */ 9276 else 9277 cp_parser_abort_tentative_parse (parser); 9278 9279 /* Otherwise, we are looking at an expression. */ 9280 return cp_parser_expression (parser, /*cast_p=*/false, NULL); 9281 } 9282 9283 /* Parses a for-statement or range-for-statement until the closing ')', 9284 not included. */ 9285 9286 static tree 9287 cp_parser_for (cp_parser *parser) 9288 { 9289 tree init, scope, decl; 9290 bool is_range_for; 9291 9292 /* Begin the for-statement. */ 9293 scope = begin_for_scope (&init); 9294 9295 /* Parse the initialization. */ 9296 is_range_for = cp_parser_for_init_statement (parser, &decl); 9297 9298 if (is_range_for) 9299 return cp_parser_range_for (parser, scope, init, decl); 9300 else 9301 return cp_parser_c_for (parser, scope, init); 9302 } 9303 9304 static tree 9305 cp_parser_c_for (cp_parser *parser, tree scope, tree init) 9306 { 9307 /* Normal for loop */ 9308 tree condition = NULL_TREE; 9309 tree expression = NULL_TREE; 9310 tree stmt; 9311 9312 stmt = begin_for_stmt (scope, init); 9313 /* The for-init-statement has already been parsed in 9314 cp_parser_for_init_statement, so no work is needed here. */ 9315 finish_for_init_stmt (stmt); 9316 9317 /* If there's a condition, process it. */ 9318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 9319 condition = cp_parser_condition (parser); 9320 finish_for_cond (condition, stmt); 9321 /* Look for the `;'. */ 9322 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9323 9324 /* If there's an expression, process it. */ 9325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) 9326 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); 9327 finish_for_expr (expression, stmt); 9328 9329 return stmt; 9330 } 9331 9332 /* Tries to parse a range-based for-statement: 9333 9334 range-based-for: 9335 decl-specifier-seq declarator : expression 9336 9337 The decl-specifier-seq declarator and the `:' are already parsed by 9338 cp_parser_for_init_statement. If processing_template_decl it returns a 9339 newly created RANGE_FOR_STMT; if not, it is converted to a 9340 regular FOR_STMT. */ 9341 9342 static tree 9343 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl) 9344 { 9345 tree stmt, range_expr; 9346 9347 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 9348 { 9349 bool expr_non_constant_p; 9350 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p); 9351 } 9352 else 9353 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 9354 9355 /* If in template, STMT is converted to a normal for-statement 9356 at instantiation. If not, it is done just ahead. */ 9357 if (processing_template_decl) 9358 { 9359 if (check_for_bare_parameter_packs (range_expr)) 9360 range_expr = error_mark_node; 9361 stmt = begin_range_for_stmt (scope, init); 9362 finish_range_for_decl (stmt, range_decl, range_expr); 9363 if (range_expr != error_mark_node 9364 && !type_dependent_expression_p (range_expr) 9365 /* The length of an array might be dependent. */ 9366 && COMPLETE_TYPE_P (TREE_TYPE (range_expr)) 9367 /* do_auto_deduction doesn't mess with template init-lists. */ 9368 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr)) 9369 do_range_for_auto_deduction (range_decl, range_expr); 9370 } 9371 else 9372 { 9373 stmt = begin_for_stmt (scope, init); 9374 stmt = cp_convert_range_for (stmt, range_decl, range_expr); 9375 } 9376 return stmt; 9377 } 9378 9379 /* Subroutine of cp_convert_range_for: given the initializer expression, 9380 builds up the range temporary. */ 9381 9382 static tree 9383 build_range_temp (tree range_expr) 9384 { 9385 tree range_type, range_temp; 9386 9387 /* Find out the type deduced by the declaration 9388 `auto &&__range = range_expr'. */ 9389 range_type = cp_build_reference_type (make_auto (), true); 9390 range_type = do_auto_deduction (range_type, range_expr, 9391 type_uses_auto (range_type)); 9392 9393 /* Create the __range variable. */ 9394 range_temp = build_decl (input_location, VAR_DECL, 9395 get_identifier ("__for_range"), range_type); 9396 TREE_USED (range_temp) = 1; 9397 DECL_ARTIFICIAL (range_temp) = 1; 9398 9399 return range_temp; 9400 } 9401 9402 /* Used by cp_parser_range_for in template context: we aren't going to 9403 do a full conversion yet, but we still need to resolve auto in the 9404 type of the for-range-declaration if present. This is basically 9405 a shortcut version of cp_convert_range_for. */ 9406 9407 static void 9408 do_range_for_auto_deduction (tree decl, tree range_expr) 9409 { 9410 tree auto_node = type_uses_auto (TREE_TYPE (decl)); 9411 if (auto_node) 9412 { 9413 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl; 9414 range_temp = convert_from_reference (build_range_temp (range_expr)); 9415 iter_type = (cp_parser_perform_range_for_lookup 9416 (range_temp, &begin_dummy, &end_dummy)); 9417 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type); 9418 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL, 9419 tf_warning_or_error); 9420 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl), 9421 iter_decl, auto_node); 9422 } 9423 } 9424 9425 /* Converts a range-based for-statement into a normal 9426 for-statement, as per the definition. 9427 9428 for (RANGE_DECL : RANGE_EXPR) 9429 BLOCK 9430 9431 should be equivalent to: 9432 9433 { 9434 auto &&__range = RANGE_EXPR; 9435 for (auto __begin = BEGIN_EXPR, end = END_EXPR; 9436 __begin != __end; 9437 ++__begin) 9438 { 9439 RANGE_DECL = *__begin; 9440 BLOCK 9441 } 9442 } 9443 9444 If RANGE_EXPR is an array: 9445 BEGIN_EXPR = __range 9446 END_EXPR = __range + ARRAY_SIZE(__range) 9447 Else if RANGE_EXPR has a member 'begin' or 'end': 9448 BEGIN_EXPR = __range.begin() 9449 END_EXPR = __range.end() 9450 Else: 9451 BEGIN_EXPR = begin(__range) 9452 END_EXPR = end(__range); 9453 9454 If __range has a member 'begin' but not 'end', or vice versa, we must 9455 still use the second alternative (it will surely fail, however). 9456 When calling begin()/end() in the third alternative we must use 9457 argument dependent lookup, but always considering 'std' as an associated 9458 namespace. */ 9459 9460 tree 9461 cp_convert_range_for (tree statement, tree range_decl, tree range_expr) 9462 { 9463 tree begin, end; 9464 tree iter_type, begin_expr, end_expr; 9465 tree condition, expression; 9466 9467 if (range_decl == error_mark_node || range_expr == error_mark_node) 9468 /* If an error happened previously do nothing or else a lot of 9469 unhelpful errors would be issued. */ 9470 begin_expr = end_expr = iter_type = error_mark_node; 9471 else 9472 { 9473 tree range_temp = build_range_temp (range_expr); 9474 pushdecl (range_temp); 9475 cp_finish_decl (range_temp, range_expr, 9476 /*is_constant_init*/false, NULL_TREE, 9477 LOOKUP_ONLYCONVERTING); 9478 9479 range_temp = convert_from_reference (range_temp); 9480 iter_type = cp_parser_perform_range_for_lookup (range_temp, 9481 &begin_expr, &end_expr); 9482 } 9483 9484 /* The new for initialization statement. */ 9485 begin = build_decl (input_location, VAR_DECL, 9486 get_identifier ("__for_begin"), iter_type); 9487 TREE_USED (begin) = 1; 9488 DECL_ARTIFICIAL (begin) = 1; 9489 pushdecl (begin); 9490 cp_finish_decl (begin, begin_expr, 9491 /*is_constant_init*/false, NULL_TREE, 9492 LOOKUP_ONLYCONVERTING); 9493 9494 end = build_decl (input_location, VAR_DECL, 9495 get_identifier ("__for_end"), iter_type); 9496 TREE_USED (end) = 1; 9497 DECL_ARTIFICIAL (end) = 1; 9498 pushdecl (end); 9499 cp_finish_decl (end, end_expr, 9500 /*is_constant_init*/false, NULL_TREE, 9501 LOOKUP_ONLYCONVERTING); 9502 9503 finish_for_init_stmt (statement); 9504 9505 /* The new for condition. */ 9506 condition = build_x_binary_op (NE_EXPR, 9507 begin, ERROR_MARK, 9508 end, ERROR_MARK, 9509 NULL, tf_warning_or_error); 9510 finish_for_cond (condition, statement); 9511 9512 /* The new increment expression. */ 9513 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin); 9514 finish_for_expr (expression, statement); 9515 9516 /* The declaration is initialized with *__begin inside the loop body. */ 9517 cp_finish_decl (range_decl, 9518 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error), 9519 /*is_constant_init*/false, NULL_TREE, 9520 LOOKUP_ONLYCONVERTING); 9521 9522 return statement; 9523 } 9524 9525 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for. 9526 We need to solve both at the same time because the method used 9527 depends on the existence of members begin or end. 9528 Returns the type deduced for the iterator expression. */ 9529 9530 static tree 9531 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end) 9532 { 9533 if (error_operand_p (range)) 9534 { 9535 *begin = *end = error_mark_node; 9536 return error_mark_node; 9537 } 9538 9539 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range)))) 9540 { 9541 error ("range-based %<for%> expression of type %qT " 9542 "has incomplete type", TREE_TYPE (range)); 9543 *begin = *end = error_mark_node; 9544 return error_mark_node; 9545 } 9546 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE) 9547 { 9548 /* If RANGE is an array, we will use pointer arithmetic. */ 9549 *begin = range; 9550 *end = build_binary_op (input_location, PLUS_EXPR, 9551 range, 9552 array_type_nelts_top (TREE_TYPE (range)), 9553 0); 9554 return build_pointer_type (TREE_TYPE (TREE_TYPE (range))); 9555 } 9556 else 9557 { 9558 /* If it is not an array, we must do a bit of magic. */ 9559 tree id_begin, id_end; 9560 tree member_begin, member_end; 9561 9562 *begin = *end = error_mark_node; 9563 9564 id_begin = get_identifier ("begin"); 9565 id_end = get_identifier ("end"); 9566 member_begin = lookup_member (TREE_TYPE (range), id_begin, 9567 /*protect=*/2, /*want_type=*/false, 9568 tf_warning_or_error); 9569 member_end = lookup_member (TREE_TYPE (range), id_end, 9570 /*protect=*/2, /*want_type=*/false, 9571 tf_warning_or_error); 9572 9573 if (member_begin != NULL_TREE || member_end != NULL_TREE) 9574 { 9575 /* Use the member functions. */ 9576 if (member_begin != NULL_TREE) 9577 *begin = cp_parser_range_for_member_function (range, id_begin); 9578 else 9579 error ("range-based %<for%> expression of type %qT has an " 9580 "%<end%> member but not a %<begin%>", TREE_TYPE (range)); 9581 9582 if (member_end != NULL_TREE) 9583 *end = cp_parser_range_for_member_function (range, id_end); 9584 else 9585 error ("range-based %<for%> expression of type %qT has a " 9586 "%<begin%> member but not an %<end%>", TREE_TYPE (range)); 9587 } 9588 else 9589 { 9590 /* Use global functions with ADL. */ 9591 VEC(tree,gc) *vec; 9592 vec = make_tree_vector (); 9593 9594 VEC_safe_push (tree, gc, vec, range); 9595 9596 member_begin = perform_koenig_lookup (id_begin, vec, 9597 /*include_std=*/true, 9598 tf_warning_or_error); 9599 *begin = finish_call_expr (member_begin, &vec, false, true, 9600 tf_warning_or_error); 9601 member_end = perform_koenig_lookup (id_end, vec, 9602 /*include_std=*/true, 9603 tf_warning_or_error); 9604 *end = finish_call_expr (member_end, &vec, false, true, 9605 tf_warning_or_error); 9606 9607 release_tree_vector (vec); 9608 } 9609 9610 /* Last common checks. */ 9611 if (*begin == error_mark_node || *end == error_mark_node) 9612 { 9613 /* If one of the expressions is an error do no more checks. */ 9614 *begin = *end = error_mark_node; 9615 return error_mark_node; 9616 } 9617 else 9618 { 9619 tree iter_type = cv_unqualified (TREE_TYPE (*begin)); 9620 /* The unqualified type of the __begin and __end temporaries should 9621 be the same, as required by the multiple auto declaration. */ 9622 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end)))) 9623 error ("inconsistent begin/end types in range-based %<for%> " 9624 "statement: %qT and %qT", 9625 TREE_TYPE (*begin), TREE_TYPE (*end)); 9626 return iter_type; 9627 } 9628 } 9629 } 9630 9631 /* Helper function for cp_parser_perform_range_for_lookup. 9632 Builds a tree for RANGE.IDENTIFIER(). */ 9633 9634 static tree 9635 cp_parser_range_for_member_function (tree range, tree identifier) 9636 { 9637 tree member, res; 9638 VEC(tree,gc) *vec; 9639 9640 member = finish_class_member_access_expr (range, identifier, 9641 false, tf_warning_or_error); 9642 if (member == error_mark_node) 9643 return error_mark_node; 9644 9645 vec = make_tree_vector (); 9646 res = finish_call_expr (member, &vec, 9647 /*disallow_virtual=*/false, 9648 /*koenig_p=*/false, 9649 tf_warning_or_error); 9650 release_tree_vector (vec); 9651 return res; 9652 } 9653 9654 /* Parse an iteration-statement. 9655 9656 iteration-statement: 9657 while ( condition ) statement 9658 do statement while ( expression ) ; 9659 for ( for-init-statement condition [opt] ; expression [opt] ) 9660 statement 9661 9662 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */ 9663 9664 static tree 9665 cp_parser_iteration_statement (cp_parser* parser) 9666 { 9667 cp_token *token; 9668 enum rid keyword; 9669 tree statement; 9670 unsigned char in_statement; 9671 9672 /* Peek at the next token. */ 9673 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION); 9674 if (!token) 9675 return error_mark_node; 9676 9677 /* Remember whether or not we are already within an iteration 9678 statement. */ 9679 in_statement = parser->in_statement; 9680 9681 /* See what kind of keyword it is. */ 9682 keyword = token->keyword; 9683 switch (keyword) 9684 { 9685 case RID_WHILE: 9686 { 9687 tree condition; 9688 9689 /* Begin the while-statement. */ 9690 statement = begin_while_stmt (); 9691 /* Look for the `('. */ 9692 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 9693 /* Parse the condition. */ 9694 condition = cp_parser_condition (parser); 9695 finish_while_stmt_cond (condition, statement); 9696 /* Look for the `)'. */ 9697 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 9698 /* Parse the dependent statement. */ 9699 parser->in_statement = IN_ITERATION_STMT; 9700 cp_parser_already_scoped_statement (parser); 9701 parser->in_statement = in_statement; 9702 /* We're done with the while-statement. */ 9703 finish_while_stmt (statement); 9704 } 9705 break; 9706 9707 case RID_DO: 9708 { 9709 tree expression; 9710 9711 /* Begin the do-statement. */ 9712 statement = begin_do_stmt (); 9713 /* Parse the body of the do-statement. */ 9714 parser->in_statement = IN_ITERATION_STMT; 9715 cp_parser_implicitly_scoped_statement (parser, NULL); 9716 parser->in_statement = in_statement; 9717 finish_do_body (statement); 9718 /* Look for the `while' keyword. */ 9719 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE); 9720 /* Look for the `('. */ 9721 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 9722 /* Parse the expression. */ 9723 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); 9724 /* We're done with the do-statement. */ 9725 finish_do_stmt (expression, statement); 9726 /* Look for the `)'. */ 9727 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 9728 /* Look for the `;'. */ 9729 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9730 } 9731 break; 9732 9733 case RID_FOR: 9734 { 9735 /* Look for the `('. */ 9736 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 9737 9738 statement = cp_parser_for (parser); 9739 9740 /* Look for the `)'. */ 9741 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 9742 9743 /* Parse the body of the for-statement. */ 9744 parser->in_statement = IN_ITERATION_STMT; 9745 cp_parser_already_scoped_statement (parser); 9746 parser->in_statement = in_statement; 9747 9748 /* We're done with the for-statement. */ 9749 finish_for_stmt (statement); 9750 } 9751 break; 9752 9753 default: 9754 cp_parser_error (parser, "expected iteration-statement"); 9755 statement = error_mark_node; 9756 break; 9757 } 9758 9759 return statement; 9760 } 9761 9762 /* Parse a for-init-statement or the declarator of a range-based-for. 9763 Returns true if a range-based-for declaration is seen. 9764 9765 for-init-statement: 9766 expression-statement 9767 simple-declaration */ 9768 9769 static bool 9770 cp_parser_for_init_statement (cp_parser* parser, tree *decl) 9771 { 9772 /* If the next token is a `;', then we have an empty 9773 expression-statement. Grammatically, this is also a 9774 simple-declaration, but an invalid one, because it does not 9775 declare anything. Therefore, if we did not handle this case 9776 specially, we would issue an error message about an invalid 9777 declaration. */ 9778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 9779 { 9780 bool is_range_for = false; 9781 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 9782 9783 parser->colon_corrects_to_scope_p = false; 9784 9785 /* We're going to speculatively look for a declaration, falling back 9786 to an expression, if necessary. */ 9787 cp_parser_parse_tentatively (parser); 9788 /* Parse the declaration. */ 9789 cp_parser_simple_declaration (parser, 9790 /*function_definition_allowed_p=*/false, 9791 decl); 9792 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 9793 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 9794 { 9795 /* It is a range-for, consume the ':' */ 9796 cp_lexer_consume_token (parser->lexer); 9797 is_range_for = true; 9798 if (cxx_dialect < cxx0x) 9799 { 9800 error_at (cp_lexer_peek_token (parser->lexer)->location, 9801 "range-based %<for%> loops are not allowed " 9802 "in C++98 mode"); 9803 *decl = error_mark_node; 9804 } 9805 } 9806 else 9807 /* The ';' is not consumed yet because we told 9808 cp_parser_simple_declaration not to. */ 9809 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9810 9811 if (cp_parser_parse_definitely (parser)) 9812 return is_range_for; 9813 /* If the tentative parse failed, then we shall need to look for an 9814 expression-statement. */ 9815 } 9816 /* If we are here, it is an expression-statement. */ 9817 cp_parser_expression_statement (parser, NULL_TREE); 9818 return false; 9819 } 9820 9821 /* Parse a jump-statement. 9822 9823 jump-statement: 9824 break ; 9825 continue ; 9826 return expression [opt] ; 9827 return braced-init-list ; 9828 goto identifier ; 9829 9830 GNU extension: 9831 9832 jump-statement: 9833 goto * expression ; 9834 9835 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */ 9836 9837 static tree 9838 cp_parser_jump_statement (cp_parser* parser) 9839 { 9840 tree statement = error_mark_node; 9841 cp_token *token; 9842 enum rid keyword; 9843 unsigned char in_statement; 9844 9845 /* Peek at the next token. */ 9846 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP); 9847 if (!token) 9848 return error_mark_node; 9849 9850 /* See what kind of keyword it is. */ 9851 keyword = token->keyword; 9852 switch (keyword) 9853 { 9854 case RID_BREAK: 9855 in_statement = parser->in_statement & ~IN_IF_STMT; 9856 switch (in_statement) 9857 { 9858 case 0: 9859 error_at (token->location, "break statement not within loop or switch"); 9860 break; 9861 default: 9862 gcc_assert ((in_statement & IN_SWITCH_STMT) 9863 || in_statement == IN_ITERATION_STMT); 9864 statement = finish_break_stmt (); 9865 break; 9866 case IN_OMP_BLOCK: 9867 error_at (token->location, "invalid exit from OpenMP structured block"); 9868 break; 9869 case IN_OMP_FOR: 9870 error_at (token->location, "break statement used with OpenMP for loop"); 9871 break; 9872 } 9873 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9874 break; 9875 9876 case RID_CONTINUE: 9877 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT)) 9878 { 9879 case 0: 9880 error_at (token->location, "continue statement not within a loop"); 9881 break; 9882 case IN_ITERATION_STMT: 9883 case IN_OMP_FOR: 9884 statement = finish_continue_stmt (); 9885 break; 9886 case IN_OMP_BLOCK: 9887 error_at (token->location, "invalid exit from OpenMP structured block"); 9888 break; 9889 default: 9890 gcc_unreachable (); 9891 } 9892 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9893 break; 9894 9895 case RID_RETURN: 9896 { 9897 tree expr; 9898 bool expr_non_constant_p; 9899 9900 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 9901 { 9902 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 9903 expr = cp_parser_braced_list (parser, &expr_non_constant_p); 9904 } 9905 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 9906 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 9907 else 9908 /* If the next token is a `;', then there is no 9909 expression. */ 9910 expr = NULL_TREE; 9911 /* Build the return-statement. */ 9912 statement = finish_return_stmt (expr); 9913 /* Look for the final `;'. */ 9914 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9915 } 9916 break; 9917 9918 case RID_GOTO: 9919 /* Create the goto-statement. */ 9920 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) 9921 { 9922 /* Issue a warning about this use of a GNU extension. */ 9923 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos"); 9924 /* Consume the '*' token. */ 9925 cp_lexer_consume_token (parser->lexer); 9926 /* Parse the dependent expression. */ 9927 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL)); 9928 } 9929 else 9930 finish_goto_stmt (cp_parser_identifier (parser)); 9931 /* Look for the final `;'. */ 9932 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9933 break; 9934 9935 default: 9936 cp_parser_error (parser, "expected jump-statement"); 9937 break; 9938 } 9939 9940 return statement; 9941 } 9942 9943 /* Parse a declaration-statement. 9944 9945 declaration-statement: 9946 block-declaration */ 9947 9948 static void 9949 cp_parser_declaration_statement (cp_parser* parser) 9950 { 9951 void *p; 9952 9953 /* Get the high-water mark for the DECLARATOR_OBSTACK. */ 9954 p = obstack_alloc (&declarator_obstack, 0); 9955 9956 /* Parse the block-declaration. */ 9957 cp_parser_block_declaration (parser, /*statement_p=*/true); 9958 9959 /* Free any declarators allocated. */ 9960 obstack_free (&declarator_obstack, p); 9961 9962 /* Finish off the statement. */ 9963 finish_stmt (); 9964 } 9965 9966 /* Some dependent statements (like `if (cond) statement'), are 9967 implicitly in their own scope. In other words, if the statement is 9968 a single statement (as opposed to a compound-statement), it is 9969 none-the-less treated as if it were enclosed in braces. Any 9970 declarations appearing in the dependent statement are out of scope 9971 after control passes that point. This function parses a statement, 9972 but ensures that is in its own scope, even if it is not a 9973 compound-statement. 9974 9975 If IF_P is not NULL, *IF_P is set to indicate whether the statement 9976 is a (possibly labeled) if statement which is not enclosed in 9977 braces and has an else clause. This is used to implement 9978 -Wparentheses. 9979 9980 Returns the new statement. */ 9981 9982 static tree 9983 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p) 9984 { 9985 tree statement; 9986 9987 if (if_p != NULL) 9988 *if_p = false; 9989 9990 /* Mark if () ; with a special NOP_EXPR. */ 9991 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 9992 { 9993 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 9994 cp_lexer_consume_token (parser->lexer); 9995 statement = add_stmt (build_empty_stmt (loc)); 9996 } 9997 /* if a compound is opened, we simply parse the statement directly. */ 9998 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 9999 statement = cp_parser_compound_statement (parser, NULL, false, false); 10000 /* If the token is not a `{', then we must take special action. */ 10001 else 10002 { 10003 /* Create a compound-statement. */ 10004 statement = begin_compound_stmt (0); 10005 /* Parse the dependent-statement. */ 10006 cp_parser_statement (parser, NULL_TREE, false, if_p); 10007 /* Finish the dummy compound-statement. */ 10008 finish_compound_stmt (statement); 10009 } 10010 10011 /* Return the statement. */ 10012 return statement; 10013 } 10014 10015 /* For some dependent statements (like `while (cond) statement'), we 10016 have already created a scope. Therefore, even if the dependent 10017 statement is a compound-statement, we do not want to create another 10018 scope. */ 10019 10020 static void 10021 cp_parser_already_scoped_statement (cp_parser* parser) 10022 { 10023 /* If the token is a `{', then we must take special action. */ 10024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) 10025 cp_parser_statement (parser, NULL_TREE, false, NULL); 10026 else 10027 { 10028 /* Avoid calling cp_parser_compound_statement, so that we 10029 don't create a new scope. Do everything else by hand. */ 10030 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE); 10031 /* If the next keyword is `__label__' we have a label declaration. */ 10032 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL)) 10033 cp_parser_label_declaration (parser); 10034 /* Parse an (optional) statement-seq. */ 10035 cp_parser_statement_seq_opt (parser, NULL_TREE); 10036 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 10037 } 10038 } 10039 10040 /* Declarations [gram.dcl.dcl] */ 10041 10042 /* Parse an optional declaration-sequence. 10043 10044 declaration-seq: 10045 declaration 10046 declaration-seq declaration */ 10047 10048 static void 10049 cp_parser_declaration_seq_opt (cp_parser* parser) 10050 { 10051 while (true) 10052 { 10053 cp_token *token; 10054 10055 token = cp_lexer_peek_token (parser->lexer); 10056 10057 if (token->type == CPP_CLOSE_BRACE 10058 || token->type == CPP_EOF 10059 || token->type == CPP_PRAGMA_EOL) 10060 break; 10061 10062 if (token->type == CPP_SEMICOLON) 10063 { 10064 /* A declaration consisting of a single semicolon is 10065 invalid. Allow it unless we're being pedantic. */ 10066 cp_lexer_consume_token (parser->lexer); 10067 if (!in_system_header) 10068 pedwarn (input_location, OPT_pedantic, "extra %<;%>"); 10069 continue; 10070 } 10071 10072 /* If we're entering or exiting a region that's implicitly 10073 extern "C", modify the lang context appropriately. */ 10074 if (!parser->implicit_extern_c && token->implicit_extern_c) 10075 { 10076 push_lang_context (lang_name_c); 10077 parser->implicit_extern_c = true; 10078 } 10079 else if (parser->implicit_extern_c && !token->implicit_extern_c) 10080 { 10081 pop_lang_context (); 10082 parser->implicit_extern_c = false; 10083 } 10084 10085 if (token->type == CPP_PRAGMA) 10086 { 10087 /* A top-level declaration can consist solely of a #pragma. 10088 A nested declaration cannot, so this is done here and not 10089 in cp_parser_declaration. (A #pragma at block scope is 10090 handled in cp_parser_statement.) */ 10091 cp_parser_pragma (parser, pragma_external); 10092 continue; 10093 } 10094 10095 /* Parse the declaration itself. */ 10096 cp_parser_declaration (parser); 10097 } 10098 } 10099 10100 /* Parse a declaration. 10101 10102 declaration: 10103 block-declaration 10104 function-definition 10105 template-declaration 10106 explicit-instantiation 10107 explicit-specialization 10108 linkage-specification 10109 namespace-definition 10110 10111 GNU extension: 10112 10113 declaration: 10114 __extension__ declaration */ 10115 10116 static void 10117 cp_parser_declaration (cp_parser* parser) 10118 { 10119 cp_token token1; 10120 cp_token token2; 10121 int saved_pedantic; 10122 void *p; 10123 tree attributes = NULL_TREE; 10124 10125 /* Check for the `__extension__' keyword. */ 10126 if (cp_parser_extension_opt (parser, &saved_pedantic)) 10127 { 10128 /* Parse the qualified declaration. */ 10129 cp_parser_declaration (parser); 10130 /* Restore the PEDANTIC flag. */ 10131 pedantic = saved_pedantic; 10132 10133 return; 10134 } 10135 10136 /* Try to figure out what kind of declaration is present. */ 10137 token1 = *cp_lexer_peek_token (parser->lexer); 10138 10139 if (token1.type != CPP_EOF) 10140 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); 10141 else 10142 { 10143 token2.type = CPP_EOF; 10144 token2.keyword = RID_MAX; 10145 } 10146 10147 /* Get the high-water mark for the DECLARATOR_OBSTACK. */ 10148 p = obstack_alloc (&declarator_obstack, 0); 10149 10150 /* If the next token is `extern' and the following token is a string 10151 literal, then we have a linkage specification. */ 10152 if (token1.keyword == RID_EXTERN 10153 && cp_parser_is_pure_string_literal (&token2)) 10154 cp_parser_linkage_specification (parser); 10155 /* If the next token is `template', then we have either a template 10156 declaration, an explicit instantiation, or an explicit 10157 specialization. */ 10158 else if (token1.keyword == RID_TEMPLATE) 10159 { 10160 /* `template <>' indicates a template specialization. */ 10161 if (token2.type == CPP_LESS 10162 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) 10163 cp_parser_explicit_specialization (parser); 10164 /* `template <' indicates a template declaration. */ 10165 else if (token2.type == CPP_LESS) 10166 cp_parser_template_declaration (parser, /*member_p=*/false); 10167 /* Anything else must be an explicit instantiation. */ 10168 else 10169 cp_parser_explicit_instantiation (parser); 10170 } 10171 /* If the next token is `export', then we have a template 10172 declaration. */ 10173 else if (token1.keyword == RID_EXPORT) 10174 cp_parser_template_declaration (parser, /*member_p=*/false); 10175 /* If the next token is `extern', 'static' or 'inline' and the one 10176 after that is `template', we have a GNU extended explicit 10177 instantiation directive. */ 10178 else if (cp_parser_allow_gnu_extensions_p (parser) 10179 && (token1.keyword == RID_EXTERN 10180 || token1.keyword == RID_STATIC 10181 || token1.keyword == RID_INLINE) 10182 && token2.keyword == RID_TEMPLATE) 10183 cp_parser_explicit_instantiation (parser); 10184 /* If the next token is `namespace', check for a named or unnamed 10185 namespace definition. */ 10186 else if (token1.keyword == RID_NAMESPACE 10187 && (/* A named namespace definition. */ 10188 (token2.type == CPP_NAME 10189 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 10190 != CPP_EQ)) 10191 /* An unnamed namespace definition. */ 10192 || token2.type == CPP_OPEN_BRACE 10193 || token2.keyword == RID_ATTRIBUTE)) 10194 cp_parser_namespace_definition (parser); 10195 /* An inline (associated) namespace definition. */ 10196 else if (token1.keyword == RID_INLINE 10197 && token2.keyword == RID_NAMESPACE) 10198 cp_parser_namespace_definition (parser); 10199 /* Objective-C++ declaration/definition. */ 10200 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword)) 10201 cp_parser_objc_declaration (parser, NULL_TREE); 10202 else if (c_dialect_objc () 10203 && token1.keyword == RID_ATTRIBUTE 10204 && cp_parser_objc_valid_prefix_attributes (parser, &attributes)) 10205 cp_parser_objc_declaration (parser, attributes); 10206 /* We must have either a block declaration or a function 10207 definition. */ 10208 else 10209 /* Try to parse a block-declaration, or a function-definition. */ 10210 cp_parser_block_declaration (parser, /*statement_p=*/false); 10211 10212 /* Free any declarators allocated. */ 10213 obstack_free (&declarator_obstack, p); 10214 } 10215 10216 /* Parse a block-declaration. 10217 10218 block-declaration: 10219 simple-declaration 10220 asm-definition 10221 namespace-alias-definition 10222 using-declaration 10223 using-directive 10224 10225 GNU Extension: 10226 10227 block-declaration: 10228 __extension__ block-declaration 10229 10230 C++0x Extension: 10231 10232 block-declaration: 10233 static_assert-declaration 10234 10235 If STATEMENT_P is TRUE, then this block-declaration is occurring as 10236 part of a declaration-statement. */ 10237 10238 static void 10239 cp_parser_block_declaration (cp_parser *parser, 10240 bool statement_p) 10241 { 10242 cp_token *token1; 10243 int saved_pedantic; 10244 10245 /* Check for the `__extension__' keyword. */ 10246 if (cp_parser_extension_opt (parser, &saved_pedantic)) 10247 { 10248 /* Parse the qualified declaration. */ 10249 cp_parser_block_declaration (parser, statement_p); 10250 /* Restore the PEDANTIC flag. */ 10251 pedantic = saved_pedantic; 10252 10253 return; 10254 } 10255 10256 /* Peek at the next token to figure out which kind of declaration is 10257 present. */ 10258 token1 = cp_lexer_peek_token (parser->lexer); 10259 10260 /* If the next keyword is `asm', we have an asm-definition. */ 10261 if (token1->keyword == RID_ASM) 10262 { 10263 if (statement_p) 10264 cp_parser_commit_to_tentative_parse (parser); 10265 cp_parser_asm_definition (parser); 10266 } 10267 /* If the next keyword is `namespace', we have a 10268 namespace-alias-definition. */ 10269 else if (token1->keyword == RID_NAMESPACE) 10270 cp_parser_namespace_alias_definition (parser); 10271 /* If the next keyword is `using', we have a 10272 using-declaration, a using-directive, or an alias-declaration. */ 10273 else if (token1->keyword == RID_USING) 10274 { 10275 cp_token *token2; 10276 10277 if (statement_p) 10278 cp_parser_commit_to_tentative_parse (parser); 10279 /* If the token after `using' is `namespace', then we have a 10280 using-directive. */ 10281 token2 = cp_lexer_peek_nth_token (parser->lexer, 2); 10282 if (token2->keyword == RID_NAMESPACE) 10283 cp_parser_using_directive (parser); 10284 /* If the second token after 'using' is '=', then we have an 10285 alias-declaration. */ 10286 else if (cxx_dialect >= cxx0x 10287 && token2->type == CPP_NAME 10288 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ) 10289 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword 10290 == RID_ATTRIBUTE))) 10291 cp_parser_alias_declaration (parser); 10292 /* Otherwise, it's a using-declaration. */ 10293 else 10294 cp_parser_using_declaration (parser, 10295 /*access_declaration_p=*/false); 10296 } 10297 /* If the next keyword is `__label__' we have a misplaced label 10298 declaration. */ 10299 else if (token1->keyword == RID_LABEL) 10300 { 10301 cp_lexer_consume_token (parser->lexer); 10302 error_at (token1->location, "%<__label__%> not at the beginning of a block"); 10303 cp_parser_skip_to_end_of_statement (parser); 10304 /* If the next token is now a `;', consume it. */ 10305 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 10306 cp_lexer_consume_token (parser->lexer); 10307 } 10308 /* If the next token is `static_assert' we have a static assertion. */ 10309 else if (token1->keyword == RID_STATIC_ASSERT) 10310 cp_parser_static_assert (parser, /*member_p=*/false); 10311 /* Anything else must be a simple-declaration. */ 10312 else 10313 cp_parser_simple_declaration (parser, !statement_p, 10314 /*maybe_range_for_decl*/NULL); 10315 } 10316 10317 /* Parse a simple-declaration. 10318 10319 simple-declaration: 10320 decl-specifier-seq [opt] init-declarator-list [opt] ; 10321 10322 init-declarator-list: 10323 init-declarator 10324 init-declarator-list , init-declarator 10325 10326 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a 10327 function-definition as a simple-declaration. 10328 10329 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the 10330 parsed declaration if it is an uninitialized single declarator not followed 10331 by a `;', or to error_mark_node otherwise. Either way, the trailing `;', 10332 if present, will not be consumed. */ 10333 10334 static void 10335 cp_parser_simple_declaration (cp_parser* parser, 10336 bool function_definition_allowed_p, 10337 tree *maybe_range_for_decl) 10338 { 10339 cp_decl_specifier_seq decl_specifiers; 10340 int declares_class_or_enum; 10341 bool saw_declarator; 10342 10343 if (maybe_range_for_decl) 10344 *maybe_range_for_decl = NULL_TREE; 10345 10346 /* Defer access checks until we know what is being declared; the 10347 checks for names appearing in the decl-specifier-seq should be 10348 done as if we were in the scope of the thing being declared. */ 10349 push_deferring_access_checks (dk_deferred); 10350 10351 /* Parse the decl-specifier-seq. We have to keep track of whether 10352 or not the decl-specifier-seq declares a named class or 10353 enumeration type, since that is the only case in which the 10354 init-declarator-list is allowed to be empty. 10355 10356 [dcl.dcl] 10357 10358 In a simple-declaration, the optional init-declarator-list can be 10359 omitted only when declaring a class or enumeration, that is when 10360 the decl-specifier-seq contains either a class-specifier, an 10361 elaborated-type-specifier, or an enum-specifier. */ 10362 cp_parser_decl_specifier_seq (parser, 10363 CP_PARSER_FLAGS_OPTIONAL, 10364 &decl_specifiers, 10365 &declares_class_or_enum); 10366 /* We no longer need to defer access checks. */ 10367 stop_deferring_access_checks (); 10368 10369 /* In a block scope, a valid declaration must always have a 10370 decl-specifier-seq. By not trying to parse declarators, we can 10371 resolve the declaration/expression ambiguity more quickly. */ 10372 if (!function_definition_allowed_p 10373 && !decl_specifiers.any_specifiers_p) 10374 { 10375 cp_parser_error (parser, "expected declaration"); 10376 goto done; 10377 } 10378 10379 /* If the next two tokens are both identifiers, the code is 10380 erroneous. The usual cause of this situation is code like: 10381 10382 T t; 10383 10384 where "T" should name a type -- but does not. */ 10385 if (!decl_specifiers.any_type_specifiers_p 10386 && cp_parser_parse_and_diagnose_invalid_type_name (parser)) 10387 { 10388 /* If parsing tentatively, we should commit; we really are 10389 looking at a declaration. */ 10390 cp_parser_commit_to_tentative_parse (parser); 10391 /* Give up. */ 10392 goto done; 10393 } 10394 10395 /* If we have seen at least one decl-specifier, and the next token 10396 is not a parenthesis, then we must be looking at a declaration. 10397 (After "int (" we might be looking at a functional cast.) */ 10398 if (decl_specifiers.any_specifiers_p 10399 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN) 10400 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) 10401 && !cp_parser_error_occurred (parser)) 10402 cp_parser_commit_to_tentative_parse (parser); 10403 10404 /* Keep going until we hit the `;' at the end of the simple 10405 declaration. */ 10406 saw_declarator = false; 10407 while (cp_lexer_next_token_is_not (parser->lexer, 10408 CPP_SEMICOLON)) 10409 { 10410 cp_token *token; 10411 bool function_definition_p; 10412 tree decl; 10413 10414 if (saw_declarator) 10415 { 10416 /* If we are processing next declarator, coma is expected */ 10417 token = cp_lexer_peek_token (parser->lexer); 10418 gcc_assert (token->type == CPP_COMMA); 10419 cp_lexer_consume_token (parser->lexer); 10420 if (maybe_range_for_decl) 10421 *maybe_range_for_decl = error_mark_node; 10422 } 10423 else 10424 saw_declarator = true; 10425 10426 /* Parse the init-declarator. */ 10427 decl = cp_parser_init_declarator (parser, &decl_specifiers, 10428 /*checks=*/NULL, 10429 function_definition_allowed_p, 10430 /*member_p=*/false, 10431 declares_class_or_enum, 10432 &function_definition_p, 10433 maybe_range_for_decl); 10434 /* If an error occurred while parsing tentatively, exit quickly. 10435 (That usually happens when in the body of a function; each 10436 statement is treated as a declaration-statement until proven 10437 otherwise.) */ 10438 if (cp_parser_error_occurred (parser)) 10439 goto done; 10440 /* Handle function definitions specially. */ 10441 if (function_definition_p) 10442 { 10443 /* If the next token is a `,', then we are probably 10444 processing something like: 10445 10446 void f() {}, *p; 10447 10448 which is erroneous. */ 10449 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 10450 { 10451 cp_token *token = cp_lexer_peek_token (parser->lexer); 10452 error_at (token->location, 10453 "mixing" 10454 " declarations and function-definitions is forbidden"); 10455 } 10456 /* Otherwise, we're done with the list of declarators. */ 10457 else 10458 { 10459 pop_deferring_access_checks (); 10460 return; 10461 } 10462 } 10463 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE) 10464 *maybe_range_for_decl = decl; 10465 /* The next token should be either a `,' or a `;'. */ 10466 token = cp_lexer_peek_token (parser->lexer); 10467 /* If it's a `,', there are more declarators to come. */ 10468 if (token->type == CPP_COMMA) 10469 /* will be consumed next time around */; 10470 /* If it's a `;', we are done. */ 10471 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl) 10472 break; 10473 /* Anything else is an error. */ 10474 else 10475 { 10476 /* If we have already issued an error message we don't need 10477 to issue another one. */ 10478 if (decl != error_mark_node 10479 || cp_parser_uncommitted_to_tentative_parse_p (parser)) 10480 cp_parser_error (parser, "expected %<,%> or %<;%>"); 10481 /* Skip tokens until we reach the end of the statement. */ 10482 cp_parser_skip_to_end_of_statement (parser); 10483 /* If the next token is now a `;', consume it. */ 10484 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 10485 cp_lexer_consume_token (parser->lexer); 10486 goto done; 10487 } 10488 /* After the first time around, a function-definition is not 10489 allowed -- even if it was OK at first. For example: 10490 10491 int i, f() {} 10492 10493 is not valid. */ 10494 function_definition_allowed_p = false; 10495 } 10496 10497 /* Issue an error message if no declarators are present, and the 10498 decl-specifier-seq does not itself declare a class or 10499 enumeration. */ 10500 if (!saw_declarator) 10501 { 10502 if (cp_parser_declares_only_class_p (parser)) 10503 shadow_tag (&decl_specifiers); 10504 /* Perform any deferred access checks. */ 10505 perform_deferred_access_checks (); 10506 } 10507 10508 /* Consume the `;'. */ 10509 if (!maybe_range_for_decl) 10510 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 10511 10512 done: 10513 pop_deferring_access_checks (); 10514 } 10515 10516 /* Parse a decl-specifier-seq. 10517 10518 decl-specifier-seq: 10519 decl-specifier-seq [opt] decl-specifier 10520 10521 decl-specifier: 10522 storage-class-specifier 10523 type-specifier 10524 function-specifier 10525 friend 10526 typedef 10527 10528 GNU Extension: 10529 10530 decl-specifier: 10531 attributes 10532 10533 Set *DECL_SPECS to a representation of the decl-specifier-seq. 10534 10535 The parser flags FLAGS is used to control type-specifier parsing. 10536 10537 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following 10538 flags: 10539 10540 1: one of the decl-specifiers is an elaborated-type-specifier 10541 (i.e., a type declaration) 10542 2: one of the decl-specifiers is an enum-specifier or a 10543 class-specifier (i.e., a type definition) 10544 10545 */ 10546 10547 static void 10548 cp_parser_decl_specifier_seq (cp_parser* parser, 10549 cp_parser_flags flags, 10550 cp_decl_specifier_seq *decl_specs, 10551 int* declares_class_or_enum) 10552 { 10553 bool constructor_possible_p = !parser->in_declarator_p; 10554 cp_token *start_token = NULL; 10555 10556 /* Clear DECL_SPECS. */ 10557 clear_decl_specs (decl_specs); 10558 10559 /* Assume no class or enumeration type is declared. */ 10560 *declares_class_or_enum = 0; 10561 10562 /* Keep reading specifiers until there are no more to read. */ 10563 while (true) 10564 { 10565 bool constructor_p; 10566 bool found_decl_spec; 10567 cp_token *token; 10568 10569 /* Peek at the next token. */ 10570 token = cp_lexer_peek_token (parser->lexer); 10571 10572 /* Save the first token of the decl spec list for error 10573 reporting. */ 10574 if (!start_token) 10575 start_token = token; 10576 /* Handle attributes. */ 10577 if (token->keyword == RID_ATTRIBUTE) 10578 { 10579 /* Parse the attributes. */ 10580 decl_specs->attributes 10581 = chainon (decl_specs->attributes, 10582 cp_parser_attributes_opt (parser)); 10583 continue; 10584 } 10585 /* Assume we will find a decl-specifier keyword. */ 10586 found_decl_spec = true; 10587 /* If the next token is an appropriate keyword, we can simply 10588 add it to the list. */ 10589 switch (token->keyword) 10590 { 10591 /* decl-specifier: 10592 friend 10593 constexpr */ 10594 case RID_FRIEND: 10595 if (!at_class_scope_p ()) 10596 { 10597 error_at (token->location, "%<friend%> used outside of class"); 10598 cp_lexer_purge_token (parser->lexer); 10599 } 10600 else 10601 { 10602 ++decl_specs->specs[(int) ds_friend]; 10603 /* Consume the token. */ 10604 cp_lexer_consume_token (parser->lexer); 10605 } 10606 break; 10607 10608 case RID_CONSTEXPR: 10609 ++decl_specs->specs[(int) ds_constexpr]; 10610 cp_lexer_consume_token (parser->lexer); 10611 break; 10612 10613 /* function-specifier: 10614 inline 10615 virtual 10616 explicit */ 10617 case RID_INLINE: 10618 case RID_VIRTUAL: 10619 case RID_EXPLICIT: 10620 cp_parser_function_specifier_opt (parser, decl_specs); 10621 break; 10622 10623 /* decl-specifier: 10624 typedef */ 10625 case RID_TYPEDEF: 10626 ++decl_specs->specs[(int) ds_typedef]; 10627 /* Consume the token. */ 10628 cp_lexer_consume_token (parser->lexer); 10629 /* A constructor declarator cannot appear in a typedef. */ 10630 constructor_possible_p = false; 10631 /* The "typedef" keyword can only occur in a declaration; we 10632 may as well commit at this point. */ 10633 cp_parser_commit_to_tentative_parse (parser); 10634 10635 if (decl_specs->storage_class != sc_none) 10636 decl_specs->conflicting_specifiers_p = true; 10637 break; 10638 10639 /* storage-class-specifier: 10640 auto 10641 register 10642 static 10643 extern 10644 mutable 10645 10646 GNU Extension: 10647 thread */ 10648 case RID_AUTO: 10649 if (cxx_dialect == cxx98) 10650 { 10651 /* Consume the token. */ 10652 cp_lexer_consume_token (parser->lexer); 10653 10654 /* Complain about `auto' as a storage specifier, if 10655 we're complaining about C++0x compatibility. */ 10656 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>" 10657 " changes meaning in C++11; please remove it"); 10658 10659 /* Set the storage class anyway. */ 10660 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO, 10661 token->location); 10662 } 10663 else 10664 /* C++0x auto type-specifier. */ 10665 found_decl_spec = false; 10666 break; 10667 10668 case RID_REGISTER: 10669 case RID_STATIC: 10670 case RID_EXTERN: 10671 case RID_MUTABLE: 10672 /* Consume the token. */ 10673 cp_lexer_consume_token (parser->lexer); 10674 cp_parser_set_storage_class (parser, decl_specs, token->keyword, 10675 token->location); 10676 break; 10677 case RID_THREAD: 10678 /* Consume the token. */ 10679 cp_lexer_consume_token (parser->lexer); 10680 ++decl_specs->specs[(int) ds_thread]; 10681 break; 10682 10683 default: 10684 /* We did not yet find a decl-specifier yet. */ 10685 found_decl_spec = false; 10686 break; 10687 } 10688 10689 if (found_decl_spec 10690 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR) 10691 && token->keyword != RID_CONSTEXPR) 10692 error ("decl-specifier invalid in condition"); 10693 10694 /* Constructors are a special case. The `S' in `S()' is not a 10695 decl-specifier; it is the beginning of the declarator. */ 10696 constructor_p 10697 = (!found_decl_spec 10698 && constructor_possible_p 10699 && (cp_parser_constructor_declarator_p 10700 (parser, decl_specs->specs[(int) ds_friend] != 0))); 10701 10702 /* If we don't have a DECL_SPEC yet, then we must be looking at 10703 a type-specifier. */ 10704 if (!found_decl_spec && !constructor_p) 10705 { 10706 int decl_spec_declares_class_or_enum; 10707 bool is_cv_qualifier; 10708 tree type_spec; 10709 10710 type_spec 10711 = cp_parser_type_specifier (parser, flags, 10712 decl_specs, 10713 /*is_declaration=*/true, 10714 &decl_spec_declares_class_or_enum, 10715 &is_cv_qualifier); 10716 *declares_class_or_enum |= decl_spec_declares_class_or_enum; 10717 10718 /* If this type-specifier referenced a user-defined type 10719 (a typedef, class-name, etc.), then we can't allow any 10720 more such type-specifiers henceforth. 10721 10722 [dcl.spec] 10723 10724 The longest sequence of decl-specifiers that could 10725 possibly be a type name is taken as the 10726 decl-specifier-seq of a declaration. The sequence shall 10727 be self-consistent as described below. 10728 10729 [dcl.type] 10730 10731 As a general rule, at most one type-specifier is allowed 10732 in the complete decl-specifier-seq of a declaration. The 10733 only exceptions are the following: 10734 10735 -- const or volatile can be combined with any other 10736 type-specifier. 10737 10738 -- signed or unsigned can be combined with char, long, 10739 short, or int. 10740 10741 -- .. 10742 10743 Example: 10744 10745 typedef char* Pc; 10746 void g (const int Pc); 10747 10748 Here, Pc is *not* part of the decl-specifier seq; it's 10749 the declarator. Therefore, once we see a type-specifier 10750 (other than a cv-qualifier), we forbid any additional 10751 user-defined types. We *do* still allow things like `int 10752 int' to be considered a decl-specifier-seq, and issue the 10753 error message later. */ 10754 if (type_spec && !is_cv_qualifier) 10755 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; 10756 /* A constructor declarator cannot follow a type-specifier. */ 10757 if (type_spec) 10758 { 10759 constructor_possible_p = false; 10760 found_decl_spec = true; 10761 if (!is_cv_qualifier) 10762 decl_specs->any_type_specifiers_p = true; 10763 } 10764 } 10765 10766 /* If we still do not have a DECL_SPEC, then there are no more 10767 decl-specifiers. */ 10768 if (!found_decl_spec) 10769 break; 10770 10771 decl_specs->any_specifiers_p = true; 10772 /* After we see one decl-specifier, further decl-specifiers are 10773 always optional. */ 10774 flags |= CP_PARSER_FLAGS_OPTIONAL; 10775 } 10776 10777 cp_parser_check_decl_spec (decl_specs, start_token->location); 10778 10779 /* Don't allow a friend specifier with a class definition. */ 10780 if (decl_specs->specs[(int) ds_friend] != 0 10781 && (*declares_class_or_enum & 2)) 10782 error_at (start_token->location, 10783 "class definition may not be declared a friend"); 10784 } 10785 10786 /* Parse an (optional) storage-class-specifier. 10787 10788 storage-class-specifier: 10789 auto 10790 register 10791 static 10792 extern 10793 mutable 10794 10795 GNU Extension: 10796 10797 storage-class-specifier: 10798 thread 10799 10800 Returns an IDENTIFIER_NODE corresponding to the keyword used. */ 10801 10802 static tree 10803 cp_parser_storage_class_specifier_opt (cp_parser* parser) 10804 { 10805 switch (cp_lexer_peek_token (parser->lexer)->keyword) 10806 { 10807 case RID_AUTO: 10808 if (cxx_dialect != cxx98) 10809 return NULL_TREE; 10810 /* Fall through for C++98. */ 10811 10812 case RID_REGISTER: 10813 case RID_STATIC: 10814 case RID_EXTERN: 10815 case RID_MUTABLE: 10816 case RID_THREAD: 10817 /* Consume the token. */ 10818 return cp_lexer_consume_token (parser->lexer)->u.value; 10819 10820 default: 10821 return NULL_TREE; 10822 } 10823 } 10824 10825 /* Parse an (optional) function-specifier. 10826 10827 function-specifier: 10828 inline 10829 virtual 10830 explicit 10831 10832 Returns an IDENTIFIER_NODE corresponding to the keyword used. 10833 Updates DECL_SPECS, if it is non-NULL. */ 10834 10835 static tree 10836 cp_parser_function_specifier_opt (cp_parser* parser, 10837 cp_decl_specifier_seq *decl_specs) 10838 { 10839 cp_token *token = cp_lexer_peek_token (parser->lexer); 10840 switch (token->keyword) 10841 { 10842 case RID_INLINE: 10843 if (decl_specs) 10844 ++decl_specs->specs[(int) ds_inline]; 10845 break; 10846 10847 case RID_VIRTUAL: 10848 /* 14.5.2.3 [temp.mem] 10849 10850 A member function template shall not be virtual. */ 10851 if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 10852 error_at (token->location, "templates may not be %<virtual%>"); 10853 else if (decl_specs) 10854 ++decl_specs->specs[(int) ds_virtual]; 10855 break; 10856 10857 case RID_EXPLICIT: 10858 if (decl_specs) 10859 ++decl_specs->specs[(int) ds_explicit]; 10860 break; 10861 10862 default: 10863 return NULL_TREE; 10864 } 10865 10866 /* Consume the token. */ 10867 return cp_lexer_consume_token (parser->lexer)->u.value; 10868 } 10869 10870 /* Parse a linkage-specification. 10871 10872 linkage-specification: 10873 extern string-literal { declaration-seq [opt] } 10874 extern string-literal declaration */ 10875 10876 static void 10877 cp_parser_linkage_specification (cp_parser* parser) 10878 { 10879 tree linkage; 10880 10881 /* Look for the `extern' keyword. */ 10882 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN); 10883 10884 /* Look for the string-literal. */ 10885 linkage = cp_parser_string_literal (parser, false, false); 10886 10887 /* Transform the literal into an identifier. If the literal is a 10888 wide-character string, or contains embedded NULs, then we can't 10889 handle it as the user wants. */ 10890 if (strlen (TREE_STRING_POINTER (linkage)) 10891 != (size_t) (TREE_STRING_LENGTH (linkage) - 1)) 10892 { 10893 cp_parser_error (parser, "invalid linkage-specification"); 10894 /* Assume C++ linkage. */ 10895 linkage = lang_name_cplusplus; 10896 } 10897 else 10898 linkage = get_identifier (TREE_STRING_POINTER (linkage)); 10899 10900 /* We're now using the new linkage. */ 10901 push_lang_context (linkage); 10902 10903 /* If the next token is a `{', then we're using the first 10904 production. */ 10905 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 10906 { 10907 /* Consume the `{' token. */ 10908 cp_lexer_consume_token (parser->lexer); 10909 /* Parse the declarations. */ 10910 cp_parser_declaration_seq_opt (parser); 10911 /* Look for the closing `}'. */ 10912 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 10913 } 10914 /* Otherwise, there's just one declaration. */ 10915 else 10916 { 10917 bool saved_in_unbraced_linkage_specification_p; 10918 10919 saved_in_unbraced_linkage_specification_p 10920 = parser->in_unbraced_linkage_specification_p; 10921 parser->in_unbraced_linkage_specification_p = true; 10922 cp_parser_declaration (parser); 10923 parser->in_unbraced_linkage_specification_p 10924 = saved_in_unbraced_linkage_specification_p; 10925 } 10926 10927 /* We're done with the linkage-specification. */ 10928 pop_lang_context (); 10929 } 10930 10931 /* Parse a static_assert-declaration. 10932 10933 static_assert-declaration: 10934 static_assert ( constant-expression , string-literal ) ; 10935 10936 If MEMBER_P, this static_assert is a class member. */ 10937 10938 static void 10939 cp_parser_static_assert(cp_parser *parser, bool member_p) 10940 { 10941 tree condition; 10942 tree message; 10943 cp_token *token; 10944 location_t saved_loc; 10945 bool dummy; 10946 10947 /* Peek at the `static_assert' token so we can keep track of exactly 10948 where the static assertion started. */ 10949 token = cp_lexer_peek_token (parser->lexer); 10950 saved_loc = token->location; 10951 10952 /* Look for the `static_assert' keyword. */ 10953 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 10954 RT_STATIC_ASSERT)) 10955 return; 10956 10957 /* We know we are in a static assertion; commit to any tentative 10958 parse. */ 10959 if (cp_parser_parsing_tentatively (parser)) 10960 cp_parser_commit_to_tentative_parse (parser); 10961 10962 /* Parse the `(' starting the static assertion condition. */ 10963 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 10964 10965 /* Parse the constant-expression. Allow a non-constant expression 10966 here in order to give better diagnostics in finish_static_assert. */ 10967 condition = 10968 cp_parser_constant_expression (parser, 10969 /*allow_non_constant_p=*/true, 10970 /*non_constant_p=*/&dummy); 10971 10972 /* Parse the separating `,'. */ 10973 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 10974 10975 /* Parse the string-literal message. */ 10976 message = cp_parser_string_literal (parser, 10977 /*translate=*/false, 10978 /*wide_ok=*/true); 10979 10980 /* A `)' completes the static assertion. */ 10981 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 10982 cp_parser_skip_to_closing_parenthesis (parser, 10983 /*recovering=*/true, 10984 /*or_comma=*/false, 10985 /*consume_paren=*/true); 10986 10987 /* A semicolon terminates the declaration. */ 10988 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 10989 10990 /* Complete the static assertion, which may mean either processing 10991 the static assert now or saving it for template instantiation. */ 10992 finish_static_assert (condition, message, saved_loc, member_p); 10993 } 10994 10995 /* Parse a `decltype' type. Returns the type. 10996 10997 simple-type-specifier: 10998 decltype ( expression ) */ 10999 11000 static tree 11001 cp_parser_decltype (cp_parser *parser) 11002 { 11003 tree expr; 11004 bool id_expression_or_member_access_p = false; 11005 const char *saved_message; 11006 bool saved_integral_constant_expression_p; 11007 bool saved_non_integral_constant_expression_p; 11008 cp_token *id_expr_start_token; 11009 cp_token *start_token = cp_lexer_peek_token (parser->lexer); 11010 11011 if (start_token->type == CPP_DECLTYPE) 11012 { 11013 /* Already parsed. */ 11014 cp_lexer_consume_token (parser->lexer); 11015 return start_token->u.value; 11016 } 11017 11018 /* Look for the `decltype' token. */ 11019 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE)) 11020 return error_mark_node; 11021 11022 /* Types cannot be defined in a `decltype' expression. Save away the 11023 old message. */ 11024 saved_message = parser->type_definition_forbidden_message; 11025 11026 /* And create the new one. */ 11027 parser->type_definition_forbidden_message 11028 = G_("types may not be defined in %<decltype%> expressions"); 11029 11030 /* The restrictions on constant-expressions do not apply inside 11031 decltype expressions. */ 11032 saved_integral_constant_expression_p 11033 = parser->integral_constant_expression_p; 11034 saved_non_integral_constant_expression_p 11035 = parser->non_integral_constant_expression_p; 11036 parser->integral_constant_expression_p = false; 11037 11038 /* Do not actually evaluate the expression. */ 11039 ++cp_unevaluated_operand; 11040 11041 /* Do not warn about problems with the expression. */ 11042 ++c_inhibit_evaluation_warnings; 11043 11044 /* Parse the opening `('. */ 11045 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 11046 return error_mark_node; 11047 11048 /* First, try parsing an id-expression. */ 11049 id_expr_start_token = cp_lexer_peek_token (parser->lexer); 11050 cp_parser_parse_tentatively (parser); 11051 expr = cp_parser_id_expression (parser, 11052 /*template_keyword_p=*/false, 11053 /*check_dependency_p=*/true, 11054 /*template_p=*/NULL, 11055 /*declarator_p=*/false, 11056 /*optional_p=*/false); 11057 11058 if (!cp_parser_error_occurred (parser) && expr != error_mark_node) 11059 { 11060 bool non_integral_constant_expression_p = false; 11061 tree id_expression = expr; 11062 cp_id_kind idk; 11063 const char *error_msg; 11064 11065 if (TREE_CODE (expr) == IDENTIFIER_NODE) 11066 /* Lookup the name we got back from the id-expression. */ 11067 expr = cp_parser_lookup_name (parser, expr, 11068 none_type, 11069 /*is_template=*/false, 11070 /*is_namespace=*/false, 11071 /*check_dependency=*/true, 11072 /*ambiguous_decls=*/NULL, 11073 id_expr_start_token->location); 11074 11075 if (expr 11076 && expr != error_mark_node 11077 && TREE_CODE (expr) != TEMPLATE_ID_EXPR 11078 && TREE_CODE (expr) != TYPE_DECL 11079 && (TREE_CODE (expr) != BIT_NOT_EXPR 11080 || !TYPE_P (TREE_OPERAND (expr, 0))) 11081 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN) 11082 { 11083 /* Complete lookup of the id-expression. */ 11084 expr = (finish_id_expression 11085 (id_expression, expr, parser->scope, &idk, 11086 /*integral_constant_expression_p=*/false, 11087 /*allow_non_integral_constant_expression_p=*/true, 11088 &non_integral_constant_expression_p, 11089 /*template_p=*/false, 11090 /*done=*/true, 11091 /*address_p=*/false, 11092 /*template_arg_p=*/false, 11093 &error_msg, 11094 id_expr_start_token->location)); 11095 11096 if (expr == error_mark_node) 11097 /* We found an id-expression, but it was something that we 11098 should not have found. This is an error, not something 11099 we can recover from, so note that we found an 11100 id-expression and we'll recover as gracefully as 11101 possible. */ 11102 id_expression_or_member_access_p = true; 11103 } 11104 11105 if (expr 11106 && expr != error_mark_node 11107 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN) 11108 /* We have an id-expression. */ 11109 id_expression_or_member_access_p = true; 11110 } 11111 11112 if (!id_expression_or_member_access_p) 11113 { 11114 /* Abort the id-expression parse. */ 11115 cp_parser_abort_tentative_parse (parser); 11116 11117 /* Parsing tentatively, again. */ 11118 cp_parser_parse_tentatively (parser); 11119 11120 /* Parse a class member access. */ 11121 expr = cp_parser_postfix_expression (parser, /*address_p=*/false, 11122 /*cast_p=*/false, 11123 /*member_access_only_p=*/true, NULL); 11124 11125 if (expr 11126 && expr != error_mark_node 11127 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN) 11128 /* We have an id-expression. */ 11129 id_expression_or_member_access_p = true; 11130 } 11131 11132 if (id_expression_or_member_access_p) 11133 /* We have parsed the complete id-expression or member access. */ 11134 cp_parser_parse_definitely (parser); 11135 else 11136 { 11137 bool saved_greater_than_is_operator_p; 11138 11139 /* Abort our attempt to parse an id-expression or member access 11140 expression. */ 11141 cp_parser_abort_tentative_parse (parser); 11142 11143 /* Within a parenthesized expression, a `>' token is always 11144 the greater-than operator. */ 11145 saved_greater_than_is_operator_p 11146 = parser->greater_than_is_operator_p; 11147 parser->greater_than_is_operator_p = true; 11148 11149 /* Parse a full expression. */ 11150 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 11151 11152 /* The `>' token might be the end of a template-id or 11153 template-parameter-list now. */ 11154 parser->greater_than_is_operator_p 11155 = saved_greater_than_is_operator_p; 11156 } 11157 11158 /* Go back to evaluating expressions. */ 11159 --cp_unevaluated_operand; 11160 --c_inhibit_evaluation_warnings; 11161 11162 /* Restore the old message and the integral constant expression 11163 flags. */ 11164 parser->type_definition_forbidden_message = saved_message; 11165 parser->integral_constant_expression_p 11166 = saved_integral_constant_expression_p; 11167 parser->non_integral_constant_expression_p 11168 = saved_non_integral_constant_expression_p; 11169 11170 /* Parse to the closing `)'. */ 11171 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 11172 { 11173 cp_parser_skip_to_closing_parenthesis (parser, true, false, 11174 /*consume_paren=*/true); 11175 return error_mark_node; 11176 } 11177 11178 expr = finish_decltype_type (expr, id_expression_or_member_access_p, 11179 tf_warning_or_error); 11180 11181 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse 11182 it again. */ 11183 start_token->type = CPP_DECLTYPE; 11184 start_token->u.value = expr; 11185 start_token->keyword = RID_MAX; 11186 cp_lexer_purge_tokens_after (parser->lexer, start_token); 11187 11188 return expr; 11189 } 11190 11191 /* Special member functions [gram.special] */ 11192 11193 /* Parse a conversion-function-id. 11194 11195 conversion-function-id: 11196 operator conversion-type-id 11197 11198 Returns an IDENTIFIER_NODE representing the operator. */ 11199 11200 static tree 11201 cp_parser_conversion_function_id (cp_parser* parser) 11202 { 11203 tree type; 11204 tree saved_scope; 11205 tree saved_qualifying_scope; 11206 tree saved_object_scope; 11207 tree pushed_scope = NULL_TREE; 11208 11209 /* Look for the `operator' token. */ 11210 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR)) 11211 return error_mark_node; 11212 /* When we parse the conversion-type-id, the current scope will be 11213 reset. However, we need that information in able to look up the 11214 conversion function later, so we save it here. */ 11215 saved_scope = parser->scope; 11216 saved_qualifying_scope = parser->qualifying_scope; 11217 saved_object_scope = parser->object_scope; 11218 /* We must enter the scope of the class so that the names of 11219 entities declared within the class are available in the 11220 conversion-type-id. For example, consider: 11221 11222 struct S { 11223 typedef int I; 11224 operator I(); 11225 }; 11226 11227 S::operator I() { ... } 11228 11229 In order to see that `I' is a type-name in the definition, we 11230 must be in the scope of `S'. */ 11231 if (saved_scope) 11232 pushed_scope = push_scope (saved_scope); 11233 /* Parse the conversion-type-id. */ 11234 type = cp_parser_conversion_type_id (parser); 11235 /* Leave the scope of the class, if any. */ 11236 if (pushed_scope) 11237 pop_scope (pushed_scope); 11238 /* Restore the saved scope. */ 11239 parser->scope = saved_scope; 11240 parser->qualifying_scope = saved_qualifying_scope; 11241 parser->object_scope = saved_object_scope; 11242 /* If the TYPE is invalid, indicate failure. */ 11243 if (type == error_mark_node) 11244 return error_mark_node; 11245 return mangle_conv_op_name_for_type (type); 11246 } 11247 11248 /* Parse a conversion-type-id: 11249 11250 conversion-type-id: 11251 type-specifier-seq conversion-declarator [opt] 11252 11253 Returns the TYPE specified. */ 11254 11255 static tree 11256 cp_parser_conversion_type_id (cp_parser* parser) 11257 { 11258 tree attributes; 11259 cp_decl_specifier_seq type_specifiers; 11260 cp_declarator *declarator; 11261 tree type_specified; 11262 11263 /* Parse the attributes. */ 11264 attributes = cp_parser_attributes_opt (parser); 11265 /* Parse the type-specifiers. */ 11266 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false, 11267 /*is_trailing_return=*/false, 11268 &type_specifiers); 11269 /* If that didn't work, stop. */ 11270 if (type_specifiers.type == error_mark_node) 11271 return error_mark_node; 11272 /* Parse the conversion-declarator. */ 11273 declarator = cp_parser_conversion_declarator_opt (parser); 11274 11275 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME, 11276 /*initialized=*/0, &attributes); 11277 if (attributes) 11278 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0); 11279 11280 /* Don't give this error when parsing tentatively. This happens to 11281 work because we always parse this definitively once. */ 11282 if (! cp_parser_uncommitted_to_tentative_parse_p (parser) 11283 && type_uses_auto (type_specified)) 11284 { 11285 error ("invalid use of %<auto%> in conversion operator"); 11286 return error_mark_node; 11287 } 11288 11289 return type_specified; 11290 } 11291 11292 /* Parse an (optional) conversion-declarator. 11293 11294 conversion-declarator: 11295 ptr-operator conversion-declarator [opt] 11296 11297 */ 11298 11299 static cp_declarator * 11300 cp_parser_conversion_declarator_opt (cp_parser* parser) 11301 { 11302 enum tree_code code; 11303 tree class_type; 11304 cp_cv_quals cv_quals; 11305 11306 /* We don't know if there's a ptr-operator next, or not. */ 11307 cp_parser_parse_tentatively (parser); 11308 /* Try the ptr-operator. */ 11309 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals); 11310 /* If it worked, look for more conversion-declarators. */ 11311 if (cp_parser_parse_definitely (parser)) 11312 { 11313 cp_declarator *declarator; 11314 11315 /* Parse another optional declarator. */ 11316 declarator = cp_parser_conversion_declarator_opt (parser); 11317 11318 return cp_parser_make_indirect_declarator 11319 (code, class_type, cv_quals, declarator); 11320 } 11321 11322 return NULL; 11323 } 11324 11325 /* Parse an (optional) ctor-initializer. 11326 11327 ctor-initializer: 11328 : mem-initializer-list 11329 11330 Returns TRUE iff the ctor-initializer was actually present. */ 11331 11332 static bool 11333 cp_parser_ctor_initializer_opt (cp_parser* parser) 11334 { 11335 /* If the next token is not a `:', then there is no 11336 ctor-initializer. */ 11337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) 11338 { 11339 /* Do default initialization of any bases and members. */ 11340 if (DECL_CONSTRUCTOR_P (current_function_decl)) 11341 finish_mem_initializers (NULL_TREE); 11342 11343 return false; 11344 } 11345 11346 /* Consume the `:' token. */ 11347 cp_lexer_consume_token (parser->lexer); 11348 /* And the mem-initializer-list. */ 11349 cp_parser_mem_initializer_list (parser); 11350 11351 return true; 11352 } 11353 11354 /* Parse a mem-initializer-list. 11355 11356 mem-initializer-list: 11357 mem-initializer ... [opt] 11358 mem-initializer ... [opt] , mem-initializer-list */ 11359 11360 static void 11361 cp_parser_mem_initializer_list (cp_parser* parser) 11362 { 11363 tree mem_initializer_list = NULL_TREE; 11364 tree target_ctor = error_mark_node; 11365 cp_token *token = cp_lexer_peek_token (parser->lexer); 11366 11367 /* Let the semantic analysis code know that we are starting the 11368 mem-initializer-list. */ 11369 if (!DECL_CONSTRUCTOR_P (current_function_decl)) 11370 error_at (token->location, 11371 "only constructors take member initializers"); 11372 11373 /* Loop through the list. */ 11374 while (true) 11375 { 11376 tree mem_initializer; 11377 11378 token = cp_lexer_peek_token (parser->lexer); 11379 /* Parse the mem-initializer. */ 11380 mem_initializer = cp_parser_mem_initializer (parser); 11381 /* If the next token is a `...', we're expanding member initializers. */ 11382 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 11383 { 11384 /* Consume the `...'. */ 11385 cp_lexer_consume_token (parser->lexer); 11386 11387 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers 11388 can be expanded but members cannot. */ 11389 if (mem_initializer != error_mark_node 11390 && !TYPE_P (TREE_PURPOSE (mem_initializer))) 11391 { 11392 error_at (token->location, 11393 "cannot expand initializer for member %<%D%>", 11394 TREE_PURPOSE (mem_initializer)); 11395 mem_initializer = error_mark_node; 11396 } 11397 11398 /* Construct the pack expansion type. */ 11399 if (mem_initializer != error_mark_node) 11400 mem_initializer = make_pack_expansion (mem_initializer); 11401 } 11402 if (target_ctor != error_mark_node 11403 && mem_initializer != error_mark_node) 11404 { 11405 error ("mem-initializer for %qD follows constructor delegation", 11406 TREE_PURPOSE (mem_initializer)); 11407 mem_initializer = error_mark_node; 11408 } 11409 /* Look for a target constructor. */ 11410 if (mem_initializer != error_mark_node 11411 && TYPE_P (TREE_PURPOSE (mem_initializer)) 11412 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type)) 11413 { 11414 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS); 11415 if (mem_initializer_list) 11416 { 11417 error ("constructor delegation follows mem-initializer for %qD", 11418 TREE_PURPOSE (mem_initializer_list)); 11419 mem_initializer = error_mark_node; 11420 } 11421 target_ctor = mem_initializer; 11422 } 11423 /* Add it to the list, unless it was erroneous. */ 11424 if (mem_initializer != error_mark_node) 11425 { 11426 TREE_CHAIN (mem_initializer) = mem_initializer_list; 11427 mem_initializer_list = mem_initializer; 11428 } 11429 /* If the next token is not a `,', we're done. */ 11430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 11431 break; 11432 /* Consume the `,' token. */ 11433 cp_lexer_consume_token (parser->lexer); 11434 } 11435 11436 /* Perform semantic analysis. */ 11437 if (DECL_CONSTRUCTOR_P (current_function_decl)) 11438 finish_mem_initializers (mem_initializer_list); 11439 } 11440 11441 /* Parse a mem-initializer. 11442 11443 mem-initializer: 11444 mem-initializer-id ( expression-list [opt] ) 11445 mem-initializer-id braced-init-list 11446 11447 GNU extension: 11448 11449 mem-initializer: 11450 ( expression-list [opt] ) 11451 11452 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base 11453 class) or FIELD_DECL (for a non-static data member) to initialize; 11454 the TREE_VALUE is the expression-list. An empty initialization 11455 list is represented by void_list_node. */ 11456 11457 static tree 11458 cp_parser_mem_initializer (cp_parser* parser) 11459 { 11460 tree mem_initializer_id; 11461 tree expression_list; 11462 tree member; 11463 cp_token *token = cp_lexer_peek_token (parser->lexer); 11464 11465 /* Find out what is being initialized. */ 11466 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 11467 { 11468 permerror (token->location, 11469 "anachronistic old-style base class initializer"); 11470 mem_initializer_id = NULL_TREE; 11471 } 11472 else 11473 { 11474 mem_initializer_id = cp_parser_mem_initializer_id (parser); 11475 if (mem_initializer_id == error_mark_node) 11476 return mem_initializer_id; 11477 } 11478 member = expand_member_init (mem_initializer_id); 11479 if (member && !DECL_P (member)) 11480 in_base_initializer = 1; 11481 11482 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 11483 { 11484 bool expr_non_constant_p; 11485 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 11486 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p); 11487 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1; 11488 expression_list = build_tree_list (NULL_TREE, expression_list); 11489 } 11490 else 11491 { 11492 VEC(tree,gc)* vec; 11493 vec = cp_parser_parenthesized_expression_list (parser, non_attr, 11494 /*cast_p=*/false, 11495 /*allow_expansion_p=*/true, 11496 /*non_constant_p=*/NULL); 11497 if (vec == NULL) 11498 return error_mark_node; 11499 expression_list = build_tree_list_vec (vec); 11500 release_tree_vector (vec); 11501 } 11502 11503 if (expression_list == error_mark_node) 11504 return error_mark_node; 11505 if (!expression_list) 11506 expression_list = void_type_node; 11507 11508 in_base_initializer = 0; 11509 11510 return member ? build_tree_list (member, expression_list) : error_mark_node; 11511 } 11512 11513 /* Parse a mem-initializer-id. 11514 11515 mem-initializer-id: 11516 :: [opt] nested-name-specifier [opt] class-name 11517 identifier 11518 11519 Returns a TYPE indicating the class to be initializer for the first 11520 production. Returns an IDENTIFIER_NODE indicating the data member 11521 to be initialized for the second production. */ 11522 11523 static tree 11524 cp_parser_mem_initializer_id (cp_parser* parser) 11525 { 11526 bool global_scope_p; 11527 bool nested_name_specifier_p; 11528 bool template_p = false; 11529 tree id; 11530 11531 cp_token *token = cp_lexer_peek_token (parser->lexer); 11532 11533 /* `typename' is not allowed in this context ([temp.res]). */ 11534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) 11535 { 11536 error_at (token->location, 11537 "keyword %<typename%> not allowed in this context (a qualified " 11538 "member initializer is implicitly a type)"); 11539 cp_lexer_consume_token (parser->lexer); 11540 } 11541 /* Look for the optional `::' operator. */ 11542 global_scope_p 11543 = (cp_parser_global_scope_opt (parser, 11544 /*current_scope_valid_p=*/false) 11545 != NULL_TREE); 11546 /* Look for the optional nested-name-specifier. The simplest way to 11547 implement: 11548 11549 [temp.res] 11550 11551 The keyword `typename' is not permitted in a base-specifier or 11552 mem-initializer; in these contexts a qualified name that 11553 depends on a template-parameter is implicitly assumed to be a 11554 type name. 11555 11556 is to assume that we have seen the `typename' keyword at this 11557 point. */ 11558 nested_name_specifier_p 11559 = (cp_parser_nested_name_specifier_opt (parser, 11560 /*typename_keyword_p=*/true, 11561 /*check_dependency_p=*/true, 11562 /*type_p=*/true, 11563 /*is_declaration=*/true) 11564 != NULL_TREE); 11565 if (nested_name_specifier_p) 11566 template_p = cp_parser_optional_template_keyword (parser); 11567 /* If there is a `::' operator or a nested-name-specifier, then we 11568 are definitely looking for a class-name. */ 11569 if (global_scope_p || nested_name_specifier_p) 11570 return cp_parser_class_name (parser, 11571 /*typename_keyword_p=*/true, 11572 /*template_keyword_p=*/template_p, 11573 typename_type, 11574 /*check_dependency_p=*/true, 11575 /*class_head_p=*/false, 11576 /*is_declaration=*/true); 11577 /* Otherwise, we could also be looking for an ordinary identifier. */ 11578 cp_parser_parse_tentatively (parser); 11579 /* Try a class-name. */ 11580 id = cp_parser_class_name (parser, 11581 /*typename_keyword_p=*/true, 11582 /*template_keyword_p=*/false, 11583 none_type, 11584 /*check_dependency_p=*/true, 11585 /*class_head_p=*/false, 11586 /*is_declaration=*/true); 11587 /* If we found one, we're done. */ 11588 if (cp_parser_parse_definitely (parser)) 11589 return id; 11590 /* Otherwise, look for an ordinary identifier. */ 11591 return cp_parser_identifier (parser); 11592 } 11593 11594 /* Overloading [gram.over] */ 11595 11596 /* Parse an operator-function-id. 11597 11598 operator-function-id: 11599 operator operator 11600 11601 Returns an IDENTIFIER_NODE for the operator which is a 11602 human-readable spelling of the identifier, e.g., `operator +'. */ 11603 11604 static tree 11605 cp_parser_operator_function_id (cp_parser* parser) 11606 { 11607 /* Look for the `operator' keyword. */ 11608 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR)) 11609 return error_mark_node; 11610 /* And then the name of the operator itself. */ 11611 return cp_parser_operator (parser); 11612 } 11613 11614 /* Return an identifier node for a user-defined literal operator. 11615 The suffix identifier is chained to the operator name identifier. */ 11616 11617 static tree 11618 cp_literal_operator_id (const char* name) 11619 { 11620 tree identifier; 11621 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX) 11622 + strlen (name) + 10); 11623 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name); 11624 identifier = get_identifier (buffer); 11625 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */ 11626 11627 return identifier; 11628 } 11629 11630 /* Parse an operator. 11631 11632 operator: 11633 new delete new[] delete[] + - * / % ^ & | ~ ! = < > 11634 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && 11635 || ++ -- , ->* -> () [] 11636 11637 GNU Extensions: 11638 11639 operator: 11640 <? >? <?= >?= 11641 11642 Returns an IDENTIFIER_NODE for the operator which is a 11643 human-readable spelling of the identifier, e.g., `operator +'. */ 11644 11645 static tree 11646 cp_parser_operator (cp_parser* parser) 11647 { 11648 tree id = NULL_TREE; 11649 cp_token *token; 11650 11651 /* Peek at the next token. */ 11652 token = cp_lexer_peek_token (parser->lexer); 11653 /* Figure out which operator we have. */ 11654 switch (token->type) 11655 { 11656 case CPP_KEYWORD: 11657 { 11658 enum tree_code op; 11659 11660 /* The keyword should be either `new' or `delete'. */ 11661 if (token->keyword == RID_NEW) 11662 op = NEW_EXPR; 11663 else if (token->keyword == RID_DELETE) 11664 op = DELETE_EXPR; 11665 else 11666 break; 11667 11668 /* Consume the `new' or `delete' token. */ 11669 cp_lexer_consume_token (parser->lexer); 11670 11671 /* Peek at the next token. */ 11672 token = cp_lexer_peek_token (parser->lexer); 11673 /* If it's a `[' token then this is the array variant of the 11674 operator. */ 11675 if (token->type == CPP_OPEN_SQUARE) 11676 { 11677 /* Consume the `[' token. */ 11678 cp_lexer_consume_token (parser->lexer); 11679 /* Look for the `]' token. */ 11680 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 11681 id = ansi_opname (op == NEW_EXPR 11682 ? VEC_NEW_EXPR : VEC_DELETE_EXPR); 11683 } 11684 /* Otherwise, we have the non-array variant. */ 11685 else 11686 id = ansi_opname (op); 11687 11688 return id; 11689 } 11690 11691 case CPP_PLUS: 11692 id = ansi_opname (PLUS_EXPR); 11693 break; 11694 11695 case CPP_MINUS: 11696 id = ansi_opname (MINUS_EXPR); 11697 break; 11698 11699 case CPP_MULT: 11700 id = ansi_opname (MULT_EXPR); 11701 break; 11702 11703 case CPP_DIV: 11704 id = ansi_opname (TRUNC_DIV_EXPR); 11705 break; 11706 11707 case CPP_MOD: 11708 id = ansi_opname (TRUNC_MOD_EXPR); 11709 break; 11710 11711 case CPP_XOR: 11712 id = ansi_opname (BIT_XOR_EXPR); 11713 break; 11714 11715 case CPP_AND: 11716 id = ansi_opname (BIT_AND_EXPR); 11717 break; 11718 11719 case CPP_OR: 11720 id = ansi_opname (BIT_IOR_EXPR); 11721 break; 11722 11723 case CPP_COMPL: 11724 id = ansi_opname (BIT_NOT_EXPR); 11725 break; 11726 11727 case CPP_NOT: 11728 id = ansi_opname (TRUTH_NOT_EXPR); 11729 break; 11730 11731 case CPP_EQ: 11732 id = ansi_assopname (NOP_EXPR); 11733 break; 11734 11735 case CPP_LESS: 11736 id = ansi_opname (LT_EXPR); 11737 break; 11738 11739 case CPP_GREATER: 11740 id = ansi_opname (GT_EXPR); 11741 break; 11742 11743 case CPP_PLUS_EQ: 11744 id = ansi_assopname (PLUS_EXPR); 11745 break; 11746 11747 case CPP_MINUS_EQ: 11748 id = ansi_assopname (MINUS_EXPR); 11749 break; 11750 11751 case CPP_MULT_EQ: 11752 id = ansi_assopname (MULT_EXPR); 11753 break; 11754 11755 case CPP_DIV_EQ: 11756 id = ansi_assopname (TRUNC_DIV_EXPR); 11757 break; 11758 11759 case CPP_MOD_EQ: 11760 id = ansi_assopname (TRUNC_MOD_EXPR); 11761 break; 11762 11763 case CPP_XOR_EQ: 11764 id = ansi_assopname (BIT_XOR_EXPR); 11765 break; 11766 11767 case CPP_AND_EQ: 11768 id = ansi_assopname (BIT_AND_EXPR); 11769 break; 11770 11771 case CPP_OR_EQ: 11772 id = ansi_assopname (BIT_IOR_EXPR); 11773 break; 11774 11775 case CPP_LSHIFT: 11776 id = ansi_opname (LSHIFT_EXPR); 11777 break; 11778 11779 case CPP_RSHIFT: 11780 id = ansi_opname (RSHIFT_EXPR); 11781 break; 11782 11783 case CPP_LSHIFT_EQ: 11784 id = ansi_assopname (LSHIFT_EXPR); 11785 break; 11786 11787 case CPP_RSHIFT_EQ: 11788 id = ansi_assopname (RSHIFT_EXPR); 11789 break; 11790 11791 case CPP_EQ_EQ: 11792 id = ansi_opname (EQ_EXPR); 11793 break; 11794 11795 case CPP_NOT_EQ: 11796 id = ansi_opname (NE_EXPR); 11797 break; 11798 11799 case CPP_LESS_EQ: 11800 id = ansi_opname (LE_EXPR); 11801 break; 11802 11803 case CPP_GREATER_EQ: 11804 id = ansi_opname (GE_EXPR); 11805 break; 11806 11807 case CPP_AND_AND: 11808 id = ansi_opname (TRUTH_ANDIF_EXPR); 11809 break; 11810 11811 case CPP_OR_OR: 11812 id = ansi_opname (TRUTH_ORIF_EXPR); 11813 break; 11814 11815 case CPP_PLUS_PLUS: 11816 id = ansi_opname (POSTINCREMENT_EXPR); 11817 break; 11818 11819 case CPP_MINUS_MINUS: 11820 id = ansi_opname (PREDECREMENT_EXPR); 11821 break; 11822 11823 case CPP_COMMA: 11824 id = ansi_opname (COMPOUND_EXPR); 11825 break; 11826 11827 case CPP_DEREF_STAR: 11828 id = ansi_opname (MEMBER_REF); 11829 break; 11830 11831 case CPP_DEREF: 11832 id = ansi_opname (COMPONENT_REF); 11833 break; 11834 11835 case CPP_OPEN_PAREN: 11836 /* Consume the `('. */ 11837 cp_lexer_consume_token (parser->lexer); 11838 /* Look for the matching `)'. */ 11839 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 11840 return ansi_opname (CALL_EXPR); 11841 11842 case CPP_OPEN_SQUARE: 11843 /* Consume the `['. */ 11844 cp_lexer_consume_token (parser->lexer); 11845 /* Look for the matching `]'. */ 11846 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 11847 return ansi_opname (ARRAY_REF); 11848 11849 case CPP_STRING: 11850 if (cxx_dialect == cxx98) 11851 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS); 11852 if (TREE_STRING_LENGTH (token->u.value) > 2) 11853 { 11854 error ("expected empty string after %<operator%> keyword"); 11855 return error_mark_node; 11856 } 11857 /* Consume the string. */ 11858 cp_lexer_consume_token (parser->lexer); 11859 /* Look for the suffix identifier. */ 11860 token = cp_lexer_peek_token (parser->lexer); 11861 if (token->type == CPP_NAME) 11862 { 11863 id = cp_parser_identifier (parser); 11864 if (id != error_mark_node) 11865 { 11866 const char *name = IDENTIFIER_POINTER (id); 11867 return cp_literal_operator_id (name); 11868 } 11869 } 11870 else 11871 { 11872 error ("expected suffix identifier"); 11873 return error_mark_node; 11874 } 11875 11876 case CPP_STRING_USERDEF: 11877 error ("missing space between %<\"\"%> and suffix identifier"); 11878 return error_mark_node; 11879 11880 default: 11881 /* Anything else is an error. */ 11882 break; 11883 } 11884 11885 /* If we have selected an identifier, we need to consume the 11886 operator token. */ 11887 if (id) 11888 cp_lexer_consume_token (parser->lexer); 11889 /* Otherwise, no valid operator name was present. */ 11890 else 11891 { 11892 cp_parser_error (parser, "expected operator"); 11893 id = error_mark_node; 11894 } 11895 11896 return id; 11897 } 11898 11899 /* Parse a template-declaration. 11900 11901 template-declaration: 11902 export [opt] template < template-parameter-list > declaration 11903 11904 If MEMBER_P is TRUE, this template-declaration occurs within a 11905 class-specifier. 11906 11907 The grammar rule given by the standard isn't correct. What 11908 is really meant is: 11909 11910 template-declaration: 11911 export [opt] template-parameter-list-seq 11912 decl-specifier-seq [opt] init-declarator [opt] ; 11913 export [opt] template-parameter-list-seq 11914 function-definition 11915 11916 template-parameter-list-seq: 11917 template-parameter-list-seq [opt] 11918 template < template-parameter-list > */ 11919 11920 static void 11921 cp_parser_template_declaration (cp_parser* parser, bool member_p) 11922 { 11923 /* Check for `export'. */ 11924 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) 11925 { 11926 /* Consume the `export' token. */ 11927 cp_lexer_consume_token (parser->lexer); 11928 /* Warn that we do not support `export'. */ 11929 warning (0, "keyword %<export%> not implemented, and will be ignored"); 11930 } 11931 11932 cp_parser_template_declaration_after_export (parser, member_p); 11933 } 11934 11935 /* Parse a template-parameter-list. 11936 11937 template-parameter-list: 11938 template-parameter 11939 template-parameter-list , template-parameter 11940 11941 Returns a TREE_LIST. Each node represents a template parameter. 11942 The nodes are connected via their TREE_CHAINs. */ 11943 11944 static tree 11945 cp_parser_template_parameter_list (cp_parser* parser) 11946 { 11947 tree parameter_list = NULL_TREE; 11948 11949 begin_template_parm_list (); 11950 11951 /* The loop below parses the template parms. We first need to know 11952 the total number of template parms to be able to compute proper 11953 canonical types of each dependent type. So after the loop, when 11954 we know the total number of template parms, 11955 end_template_parm_list computes the proper canonical types and 11956 fixes up the dependent types accordingly. */ 11957 while (true) 11958 { 11959 tree parameter; 11960 bool is_non_type; 11961 bool is_parameter_pack; 11962 location_t parm_loc; 11963 11964 /* Parse the template-parameter. */ 11965 parm_loc = cp_lexer_peek_token (parser->lexer)->location; 11966 parameter = cp_parser_template_parameter (parser, 11967 &is_non_type, 11968 &is_parameter_pack); 11969 /* Add it to the list. */ 11970 if (parameter != error_mark_node) 11971 parameter_list = process_template_parm (parameter_list, 11972 parm_loc, 11973 parameter, 11974 is_non_type, 11975 is_parameter_pack); 11976 else 11977 { 11978 tree err_parm = build_tree_list (parameter, parameter); 11979 parameter_list = chainon (parameter_list, err_parm); 11980 } 11981 11982 /* If the next token is not a `,', we're done. */ 11983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 11984 break; 11985 /* Otherwise, consume the `,' token. */ 11986 cp_lexer_consume_token (parser->lexer); 11987 } 11988 11989 return end_template_parm_list (parameter_list); 11990 } 11991 11992 /* Parse a template-parameter. 11993 11994 template-parameter: 11995 type-parameter 11996 parameter-declaration 11997 11998 If all goes well, returns a TREE_LIST. The TREE_VALUE represents 11999 the parameter. The TREE_PURPOSE is the default value, if any. 12000 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true 12001 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is 12002 set to true iff this parameter is a parameter pack. */ 12003 12004 static tree 12005 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type, 12006 bool *is_parameter_pack) 12007 { 12008 cp_token *token; 12009 cp_parameter_declarator *parameter_declarator; 12010 cp_declarator *id_declarator; 12011 tree parm; 12012 12013 /* Assume it is a type parameter or a template parameter. */ 12014 *is_non_type = false; 12015 /* Assume it not a parameter pack. */ 12016 *is_parameter_pack = false; 12017 /* Peek at the next token. */ 12018 token = cp_lexer_peek_token (parser->lexer); 12019 /* If it is `class' or `template', we have a type-parameter. */ 12020 if (token->keyword == RID_TEMPLATE) 12021 return cp_parser_type_parameter (parser, is_parameter_pack); 12022 /* If it is `class' or `typename' we do not know yet whether it is a 12023 type parameter or a non-type parameter. Consider: 12024 12025 template <typename T, typename T::X X> ... 12026 12027 or: 12028 12029 template <class C, class D*> ... 12030 12031 Here, the first parameter is a type parameter, and the second is 12032 a non-type parameter. We can tell by looking at the token after 12033 the identifier -- if it is a `,', `=', or `>' then we have a type 12034 parameter. */ 12035 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) 12036 { 12037 /* Peek at the token after `class' or `typename'. */ 12038 token = cp_lexer_peek_nth_token (parser->lexer, 2); 12039 /* If it's an ellipsis, we have a template type parameter 12040 pack. */ 12041 if (token->type == CPP_ELLIPSIS) 12042 return cp_parser_type_parameter (parser, is_parameter_pack); 12043 /* If it's an identifier, skip it. */ 12044 if (token->type == CPP_NAME) 12045 token = cp_lexer_peek_nth_token (parser->lexer, 3); 12046 /* Now, see if the token looks like the end of a template 12047 parameter. */ 12048 if (token->type == CPP_COMMA 12049 || token->type == CPP_EQ 12050 || token->type == CPP_GREATER) 12051 return cp_parser_type_parameter (parser, is_parameter_pack); 12052 } 12053 12054 /* Otherwise, it is a non-type parameter. 12055 12056 [temp.param] 12057 12058 When parsing a default template-argument for a non-type 12059 template-parameter, the first non-nested `>' is taken as the end 12060 of the template parameter-list rather than a greater-than 12061 operator. */ 12062 *is_non_type = true; 12063 parameter_declarator 12064 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true, 12065 /*parenthesized_p=*/NULL); 12066 12067 /* If the parameter declaration is marked as a parameter pack, set 12068 *IS_PARAMETER_PACK to notify the caller. Also, unmark the 12069 declarator's PACK_EXPANSION_P, otherwise we'll get errors from 12070 grokdeclarator. */ 12071 if (parameter_declarator 12072 && parameter_declarator->declarator 12073 && parameter_declarator->declarator->parameter_pack_p) 12074 { 12075 *is_parameter_pack = true; 12076 parameter_declarator->declarator->parameter_pack_p = false; 12077 } 12078 12079 /* If the next token is an ellipsis, and we don't already have it 12080 marked as a parameter pack, then we have a parameter pack (that 12081 has no declarator). */ 12082 if (!*is_parameter_pack 12083 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS) 12084 && declarator_can_be_parameter_pack (parameter_declarator->declarator)) 12085 { 12086 /* Consume the `...'. */ 12087 cp_lexer_consume_token (parser->lexer); 12088 maybe_warn_variadic_templates (); 12089 12090 *is_parameter_pack = true; 12091 } 12092 /* We might end up with a pack expansion as the type of the non-type 12093 template parameter, in which case this is a non-type template 12094 parameter pack. */ 12095 else if (parameter_declarator 12096 && parameter_declarator->decl_specifiers.type 12097 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type)) 12098 { 12099 *is_parameter_pack = true; 12100 parameter_declarator->decl_specifiers.type = 12101 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type); 12102 } 12103 12104 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 12105 { 12106 /* Parameter packs cannot have default arguments. However, a 12107 user may try to do so, so we'll parse them and give an 12108 appropriate diagnostic here. */ 12109 12110 cp_token *start_token = cp_lexer_peek_token (parser->lexer); 12111 12112 /* Find the name of the parameter pack. */ 12113 id_declarator = parameter_declarator->declarator; 12114 while (id_declarator && id_declarator->kind != cdk_id) 12115 id_declarator = id_declarator->declarator; 12116 12117 if (id_declarator && id_declarator->kind == cdk_id) 12118 error_at (start_token->location, 12119 "template parameter pack %qD cannot have a default argument", 12120 id_declarator->u.id.unqualified_name); 12121 else 12122 error_at (start_token->location, 12123 "template parameter pack cannot have a default argument"); 12124 12125 /* Parse the default argument, but throw away the result. */ 12126 cp_parser_default_argument (parser, /*template_parm_p=*/true); 12127 } 12128 12129 parm = grokdeclarator (parameter_declarator->declarator, 12130 ¶meter_declarator->decl_specifiers, 12131 TPARM, /*initialized=*/0, 12132 /*attrlist=*/NULL); 12133 if (parm == error_mark_node) 12134 return error_mark_node; 12135 12136 return build_tree_list (parameter_declarator->default_argument, parm); 12137 } 12138 12139 /* Parse a type-parameter. 12140 12141 type-parameter: 12142 class identifier [opt] 12143 class identifier [opt] = type-id 12144 typename identifier [opt] 12145 typename identifier [opt] = type-id 12146 template < template-parameter-list > class identifier [opt] 12147 template < template-parameter-list > class identifier [opt] 12148 = id-expression 12149 12150 GNU Extension (variadic templates): 12151 12152 type-parameter: 12153 class ... identifier [opt] 12154 typename ... identifier [opt] 12155 12156 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The 12157 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is 12158 the declaration of the parameter. 12159 12160 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */ 12161 12162 static tree 12163 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack) 12164 { 12165 cp_token *token; 12166 tree parameter; 12167 12168 /* Look for a keyword to tell us what kind of parameter this is. */ 12169 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE); 12170 if (!token) 12171 return error_mark_node; 12172 12173 switch (token->keyword) 12174 { 12175 case RID_CLASS: 12176 case RID_TYPENAME: 12177 { 12178 tree identifier; 12179 tree default_argument; 12180 12181 /* If the next token is an ellipsis, we have a template 12182 argument pack. */ 12183 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 12184 { 12185 /* Consume the `...' token. */ 12186 cp_lexer_consume_token (parser->lexer); 12187 maybe_warn_variadic_templates (); 12188 12189 *is_parameter_pack = true; 12190 } 12191 12192 /* If the next token is an identifier, then it names the 12193 parameter. */ 12194 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 12195 identifier = cp_parser_identifier (parser); 12196 else 12197 identifier = NULL_TREE; 12198 12199 /* Create the parameter. */ 12200 parameter = finish_template_type_parm (class_type_node, identifier); 12201 12202 /* If the next token is an `=', we have a default argument. */ 12203 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 12204 { 12205 /* Consume the `=' token. */ 12206 cp_lexer_consume_token (parser->lexer); 12207 /* Parse the default-argument. */ 12208 push_deferring_access_checks (dk_no_deferred); 12209 default_argument = cp_parser_type_id (parser); 12210 12211 /* Template parameter packs cannot have default 12212 arguments. */ 12213 if (*is_parameter_pack) 12214 { 12215 if (identifier) 12216 error_at (token->location, 12217 "template parameter pack %qD cannot have a " 12218 "default argument", identifier); 12219 else 12220 error_at (token->location, 12221 "template parameter packs cannot have " 12222 "default arguments"); 12223 default_argument = NULL_TREE; 12224 } 12225 pop_deferring_access_checks (); 12226 } 12227 else 12228 default_argument = NULL_TREE; 12229 12230 /* Create the combined representation of the parameter and the 12231 default argument. */ 12232 parameter = build_tree_list (default_argument, parameter); 12233 } 12234 break; 12235 12236 case RID_TEMPLATE: 12237 { 12238 tree identifier; 12239 tree default_argument; 12240 12241 /* Look for the `<'. */ 12242 cp_parser_require (parser, CPP_LESS, RT_LESS); 12243 /* Parse the template-parameter-list. */ 12244 cp_parser_template_parameter_list (parser); 12245 /* Look for the `>'. */ 12246 cp_parser_require (parser, CPP_GREATER, RT_GREATER); 12247 /* Look for the `class' keyword. */ 12248 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS); 12249 /* If the next token is an ellipsis, we have a template 12250 argument pack. */ 12251 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 12252 { 12253 /* Consume the `...' token. */ 12254 cp_lexer_consume_token (parser->lexer); 12255 maybe_warn_variadic_templates (); 12256 12257 *is_parameter_pack = true; 12258 } 12259 /* If the next token is an `=', then there is a 12260 default-argument. If the next token is a `>', we are at 12261 the end of the parameter-list. If the next token is a `,', 12262 then we are at the end of this parameter. */ 12263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) 12264 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) 12265 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 12266 { 12267 identifier = cp_parser_identifier (parser); 12268 /* Treat invalid names as if the parameter were nameless. */ 12269 if (identifier == error_mark_node) 12270 identifier = NULL_TREE; 12271 } 12272 else 12273 identifier = NULL_TREE; 12274 12275 /* Create the template parameter. */ 12276 parameter = finish_template_template_parm (class_type_node, 12277 identifier); 12278 12279 /* If the next token is an `=', then there is a 12280 default-argument. */ 12281 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 12282 { 12283 bool is_template; 12284 12285 /* Consume the `='. */ 12286 cp_lexer_consume_token (parser->lexer); 12287 /* Parse the id-expression. */ 12288 push_deferring_access_checks (dk_no_deferred); 12289 /* save token before parsing the id-expression, for error 12290 reporting */ 12291 token = cp_lexer_peek_token (parser->lexer); 12292 default_argument 12293 = cp_parser_id_expression (parser, 12294 /*template_keyword_p=*/false, 12295 /*check_dependency_p=*/true, 12296 /*template_p=*/&is_template, 12297 /*declarator_p=*/false, 12298 /*optional_p=*/false); 12299 if (TREE_CODE (default_argument) == TYPE_DECL) 12300 /* If the id-expression was a template-id that refers to 12301 a template-class, we already have the declaration here, 12302 so no further lookup is needed. */ 12303 ; 12304 else 12305 /* Look up the name. */ 12306 default_argument 12307 = cp_parser_lookup_name (parser, default_argument, 12308 none_type, 12309 /*is_template=*/is_template, 12310 /*is_namespace=*/false, 12311 /*check_dependency=*/true, 12312 /*ambiguous_decls=*/NULL, 12313 token->location); 12314 /* See if the default argument is valid. */ 12315 default_argument 12316 = check_template_template_default_arg (default_argument); 12317 12318 /* Template parameter packs cannot have default 12319 arguments. */ 12320 if (*is_parameter_pack) 12321 { 12322 if (identifier) 12323 error_at (token->location, 12324 "template parameter pack %qD cannot " 12325 "have a default argument", 12326 identifier); 12327 else 12328 error_at (token->location, "template parameter packs cannot " 12329 "have default arguments"); 12330 default_argument = NULL_TREE; 12331 } 12332 pop_deferring_access_checks (); 12333 } 12334 else 12335 default_argument = NULL_TREE; 12336 12337 /* Create the combined representation of the parameter and the 12338 default argument. */ 12339 parameter = build_tree_list (default_argument, parameter); 12340 } 12341 break; 12342 12343 default: 12344 gcc_unreachable (); 12345 break; 12346 } 12347 12348 return parameter; 12349 } 12350 12351 /* Parse a template-id. 12352 12353 template-id: 12354 template-name < template-argument-list [opt] > 12355 12356 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the 12357 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be 12358 returned. Otherwise, if the template-name names a function, or set 12359 of functions, returns a TEMPLATE_ID_EXPR. If the template-name 12360 names a class, returns a TYPE_DECL for the specialization. 12361 12362 If CHECK_DEPENDENCY_P is FALSE, names are looked up in 12363 uninstantiated templates. */ 12364 12365 static tree 12366 cp_parser_template_id (cp_parser *parser, 12367 bool template_keyword_p, 12368 bool check_dependency_p, 12369 bool is_declaration) 12370 { 12371 int i; 12372 tree templ; 12373 tree arguments; 12374 tree template_id; 12375 cp_token_position start_of_id = 0; 12376 deferred_access_check *chk; 12377 VEC (deferred_access_check,gc) *access_check; 12378 cp_token *next_token = NULL, *next_token_2 = NULL; 12379 bool is_identifier; 12380 12381 /* If the next token corresponds to a template-id, there is no need 12382 to reparse it. */ 12383 next_token = cp_lexer_peek_token (parser->lexer); 12384 if (next_token->type == CPP_TEMPLATE_ID) 12385 { 12386 struct tree_check *check_value; 12387 12388 /* Get the stored value. */ 12389 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value; 12390 /* Perform any access checks that were deferred. */ 12391 access_check = check_value->checks; 12392 if (access_check) 12393 { 12394 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk) 12395 perform_or_defer_access_check (chk->binfo, 12396 chk->decl, 12397 chk->diag_decl); 12398 } 12399 /* Return the stored value. */ 12400 return check_value->value; 12401 } 12402 12403 /* Avoid performing name lookup if there is no possibility of 12404 finding a template-id. */ 12405 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) 12406 || (next_token->type == CPP_NAME 12407 && !cp_parser_nth_token_starts_template_argument_list_p 12408 (parser, 2))) 12409 { 12410 cp_parser_error (parser, "expected template-id"); 12411 return error_mark_node; 12412 } 12413 12414 /* Remember where the template-id starts. */ 12415 if (cp_parser_uncommitted_to_tentative_parse_p (parser)) 12416 start_of_id = cp_lexer_token_position (parser->lexer, false); 12417 12418 push_deferring_access_checks (dk_deferred); 12419 12420 /* Parse the template-name. */ 12421 is_identifier = false; 12422 templ = cp_parser_template_name (parser, template_keyword_p, 12423 check_dependency_p, 12424 is_declaration, 12425 &is_identifier); 12426 if (templ == error_mark_node || is_identifier) 12427 { 12428 pop_deferring_access_checks (); 12429 return templ; 12430 } 12431 12432 /* If we find the sequence `[:' after a template-name, it's probably 12433 a digraph-typo for `< ::'. Substitute the tokens and check if we can 12434 parse correctly the argument list. */ 12435 next_token = cp_lexer_peek_token (parser->lexer); 12436 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2); 12437 if (next_token->type == CPP_OPEN_SQUARE 12438 && next_token->flags & DIGRAPH 12439 && next_token_2->type == CPP_COLON 12440 && !(next_token_2->flags & PREV_WHITE)) 12441 { 12442 cp_parser_parse_tentatively (parser); 12443 /* Change `:' into `::'. */ 12444 next_token_2->type = CPP_SCOPE; 12445 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is 12446 CPP_LESS. */ 12447 cp_lexer_consume_token (parser->lexer); 12448 12449 /* Parse the arguments. */ 12450 arguments = cp_parser_enclosed_template_argument_list (parser); 12451 if (!cp_parser_parse_definitely (parser)) 12452 { 12453 /* If we couldn't parse an argument list, then we revert our changes 12454 and return simply an error. Maybe this is not a template-id 12455 after all. */ 12456 next_token_2->type = CPP_COLON; 12457 cp_parser_error (parser, "expected %<<%>"); 12458 pop_deferring_access_checks (); 12459 return error_mark_node; 12460 } 12461 /* Otherwise, emit an error about the invalid digraph, but continue 12462 parsing because we got our argument list. */ 12463 if (permerror (next_token->location, 12464 "%<<::%> cannot begin a template-argument list")) 12465 { 12466 static bool hint = false; 12467 inform (next_token->location, 12468 "%<<:%> is an alternate spelling for %<[%>." 12469 " Insert whitespace between %<<%> and %<::%>"); 12470 if (!hint && !flag_permissive) 12471 { 12472 inform (next_token->location, "(if you use %<-fpermissive%>" 12473 " G++ will accept your code)"); 12474 hint = true; 12475 } 12476 } 12477 } 12478 else 12479 { 12480 /* Look for the `<' that starts the template-argument-list. */ 12481 if (!cp_parser_require (parser, CPP_LESS, RT_LESS)) 12482 { 12483 pop_deferring_access_checks (); 12484 return error_mark_node; 12485 } 12486 /* Parse the arguments. */ 12487 arguments = cp_parser_enclosed_template_argument_list (parser); 12488 } 12489 12490 /* Build a representation of the specialization. */ 12491 if (TREE_CODE (templ) == IDENTIFIER_NODE) 12492 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments); 12493 else if (DECL_TYPE_TEMPLATE_P (templ) 12494 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ)) 12495 { 12496 bool entering_scope; 12497 /* In "template <typename T> ... A<T>::", A<T> is the abstract A 12498 template (rather than some instantiation thereof) only if 12499 is not nested within some other construct. For example, in 12500 "template <typename T> void f(T) { A<T>::", A<T> is just an 12501 instantiation of A. */ 12502 entering_scope = (template_parm_scope_p () 12503 && cp_lexer_next_token_is (parser->lexer, 12504 CPP_SCOPE)); 12505 template_id 12506 = finish_template_type (templ, arguments, entering_scope); 12507 } 12508 else 12509 { 12510 /* If it's not a class-template or a template-template, it should be 12511 a function-template. */ 12512 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ) 12513 || TREE_CODE (templ) == OVERLOAD 12514 || BASELINK_P (templ))); 12515 12516 template_id = lookup_template_function (templ, arguments); 12517 } 12518 12519 /* If parsing tentatively, replace the sequence of tokens that makes 12520 up the template-id with a CPP_TEMPLATE_ID token. That way, 12521 should we re-parse the token stream, we will not have to repeat 12522 the effort required to do the parse, nor will we issue duplicate 12523 error messages about problems during instantiation of the 12524 template. */ 12525 if (start_of_id) 12526 { 12527 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id); 12528 12529 /* Reset the contents of the START_OF_ID token. */ 12530 token->type = CPP_TEMPLATE_ID; 12531 /* Retrieve any deferred checks. Do not pop this access checks yet 12532 so the memory will not be reclaimed during token replacing below. */ 12533 token->u.tree_check_value = ggc_alloc_cleared_tree_check (); 12534 token->u.tree_check_value->value = template_id; 12535 token->u.tree_check_value->checks = get_deferred_access_checks (); 12536 token->keyword = RID_MAX; 12537 12538 /* Purge all subsequent tokens. */ 12539 cp_lexer_purge_tokens_after (parser->lexer, start_of_id); 12540 12541 /* ??? Can we actually assume that, if template_id == 12542 error_mark_node, we will have issued a diagnostic to the 12543 user, as opposed to simply marking the tentative parse as 12544 failed? */ 12545 if (cp_parser_error_occurred (parser) && template_id != error_mark_node) 12546 error_at (token->location, "parse error in template argument list"); 12547 } 12548 12549 pop_deferring_access_checks (); 12550 return template_id; 12551 } 12552 12553 /* Parse a template-name. 12554 12555 template-name: 12556 identifier 12557 12558 The standard should actually say: 12559 12560 template-name: 12561 identifier 12562 operator-function-id 12563 12564 A defect report has been filed about this issue. 12565 12566 A conversion-function-id cannot be a template name because they cannot 12567 be part of a template-id. In fact, looking at this code: 12568 12569 a.operator K<int>() 12570 12571 the conversion-function-id is "operator K<int>", and K<int> is a type-id. 12572 It is impossible to call a templated conversion-function-id with an 12573 explicit argument list, since the only allowed template parameter is 12574 the type to which it is converting. 12575 12576 If TEMPLATE_KEYWORD_P is true, then we have just seen the 12577 `template' keyword, in a construction like: 12578 12579 T::template f<3>() 12580 12581 In that case `f' is taken to be a template-name, even though there 12582 is no way of knowing for sure. 12583 12584 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the 12585 name refers to a set of overloaded functions, at least one of which 12586 is a template, or an IDENTIFIER_NODE with the name of the template, 12587 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, 12588 names are looked up inside uninstantiated templates. */ 12589 12590 static tree 12591 cp_parser_template_name (cp_parser* parser, 12592 bool template_keyword_p, 12593 bool check_dependency_p, 12594 bool is_declaration, 12595 bool *is_identifier) 12596 { 12597 tree identifier; 12598 tree decl; 12599 tree fns; 12600 cp_token *token = cp_lexer_peek_token (parser->lexer); 12601 12602 /* If the next token is `operator', then we have either an 12603 operator-function-id or a conversion-function-id. */ 12604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) 12605 { 12606 /* We don't know whether we're looking at an 12607 operator-function-id or a conversion-function-id. */ 12608 cp_parser_parse_tentatively (parser); 12609 /* Try an operator-function-id. */ 12610 identifier = cp_parser_operator_function_id (parser); 12611 /* If that didn't work, try a conversion-function-id. */ 12612 if (!cp_parser_parse_definitely (parser)) 12613 { 12614 cp_parser_error (parser, "expected template-name"); 12615 return error_mark_node; 12616 } 12617 } 12618 /* Look for the identifier. */ 12619 else 12620 identifier = cp_parser_identifier (parser); 12621 12622 /* If we didn't find an identifier, we don't have a template-id. */ 12623 if (identifier == error_mark_node) 12624 return error_mark_node; 12625 12626 /* If the name immediately followed the `template' keyword, then it 12627 is a template-name. However, if the next token is not `<', then 12628 we do not treat it as a template-name, since it is not being used 12629 as part of a template-id. This enables us to handle constructs 12630 like: 12631 12632 template <typename T> struct S { S(); }; 12633 template <typename T> S<T>::S(); 12634 12635 correctly. We would treat `S' as a template -- if it were `S<T>' 12636 -- but we do not if there is no `<'. */ 12637 12638 if (processing_template_decl 12639 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)) 12640 { 12641 /* In a declaration, in a dependent context, we pretend that the 12642 "template" keyword was present in order to improve error 12643 recovery. For example, given: 12644 12645 template <typename T> void f(T::X<int>); 12646 12647 we want to treat "X<int>" as a template-id. */ 12648 if (is_declaration 12649 && !template_keyword_p 12650 && parser->scope && TYPE_P (parser->scope) 12651 && check_dependency_p 12652 && dependent_scope_p (parser->scope) 12653 /* Do not do this for dtors (or ctors), since they never 12654 need the template keyword before their name. */ 12655 && !constructor_name_p (identifier, parser->scope)) 12656 { 12657 cp_token_position start = 0; 12658 12659 /* Explain what went wrong. */ 12660 error_at (token->location, "non-template %qD used as template", 12661 identifier); 12662 inform (token->location, "use %<%T::template %D%> to indicate that it is a template", 12663 parser->scope, identifier); 12664 /* If parsing tentatively, find the location of the "<" token. */ 12665 if (cp_parser_simulate_error (parser)) 12666 start = cp_lexer_token_position (parser->lexer, true); 12667 /* Parse the template arguments so that we can issue error 12668 messages about them. */ 12669 cp_lexer_consume_token (parser->lexer); 12670 cp_parser_enclosed_template_argument_list (parser); 12671 /* Skip tokens until we find a good place from which to 12672 continue parsing. */ 12673 cp_parser_skip_to_closing_parenthesis (parser, 12674 /*recovering=*/true, 12675 /*or_comma=*/true, 12676 /*consume_paren=*/false); 12677 /* If parsing tentatively, permanently remove the 12678 template argument list. That will prevent duplicate 12679 error messages from being issued about the missing 12680 "template" keyword. */ 12681 if (start) 12682 cp_lexer_purge_tokens_after (parser->lexer, start); 12683 if (is_identifier) 12684 *is_identifier = true; 12685 return identifier; 12686 } 12687 12688 /* If the "template" keyword is present, then there is generally 12689 no point in doing name-lookup, so we just return IDENTIFIER. 12690 But, if the qualifying scope is non-dependent then we can 12691 (and must) do name-lookup normally. */ 12692 if (template_keyword_p 12693 && (!parser->scope 12694 || (TYPE_P (parser->scope) 12695 && dependent_type_p (parser->scope)))) 12696 return identifier; 12697 } 12698 12699 /* Look up the name. */ 12700 decl = cp_parser_lookup_name (parser, identifier, 12701 none_type, 12702 /*is_template=*/true, 12703 /*is_namespace=*/false, 12704 check_dependency_p, 12705 /*ambiguous_decls=*/NULL, 12706 token->location); 12707 12708 /* If DECL is a template, then the name was a template-name. */ 12709 if (TREE_CODE (decl) == TEMPLATE_DECL) 12710 ; 12711 else 12712 { 12713 tree fn = NULL_TREE; 12714 12715 /* The standard does not explicitly indicate whether a name that 12716 names a set of overloaded declarations, some of which are 12717 templates, is a template-name. However, such a name should 12718 be a template-name; otherwise, there is no way to form a 12719 template-id for the overloaded templates. */ 12720 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; 12721 if (TREE_CODE (fns) == OVERLOAD) 12722 for (fn = fns; fn; fn = OVL_NEXT (fn)) 12723 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) 12724 break; 12725 12726 if (!fn) 12727 { 12728 /* The name does not name a template. */ 12729 cp_parser_error (parser, "expected template-name"); 12730 return error_mark_node; 12731 } 12732 } 12733 12734 /* If DECL is dependent, and refers to a function, then just return 12735 its name; we will look it up again during template instantiation. */ 12736 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) 12737 { 12738 tree scope = ovl_scope (decl); 12739 if (TYPE_P (scope) && dependent_type_p (scope)) 12740 return identifier; 12741 } 12742 12743 return decl; 12744 } 12745 12746 /* Parse a template-argument-list. 12747 12748 template-argument-list: 12749 template-argument ... [opt] 12750 template-argument-list , template-argument ... [opt] 12751 12752 Returns a TREE_VEC containing the arguments. */ 12753 12754 static tree 12755 cp_parser_template_argument_list (cp_parser* parser) 12756 { 12757 tree fixed_args[10]; 12758 unsigned n_args = 0; 12759 unsigned alloced = 10; 12760 tree *arg_ary = fixed_args; 12761 tree vec; 12762 bool saved_in_template_argument_list_p; 12763 bool saved_ice_p; 12764 bool saved_non_ice_p; 12765 12766 saved_in_template_argument_list_p = parser->in_template_argument_list_p; 12767 parser->in_template_argument_list_p = true; 12768 /* Even if the template-id appears in an integral 12769 constant-expression, the contents of the argument list do 12770 not. */ 12771 saved_ice_p = parser->integral_constant_expression_p; 12772 parser->integral_constant_expression_p = false; 12773 saved_non_ice_p = parser->non_integral_constant_expression_p; 12774 parser->non_integral_constant_expression_p = false; 12775 12776 /* Parse the arguments. */ 12777 do 12778 { 12779 tree argument; 12780 12781 if (n_args) 12782 /* Consume the comma. */ 12783 cp_lexer_consume_token (parser->lexer); 12784 12785 /* Parse the template-argument. */ 12786 argument = cp_parser_template_argument (parser); 12787 12788 /* If the next token is an ellipsis, we're expanding a template 12789 argument pack. */ 12790 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 12791 { 12792 if (argument == error_mark_node) 12793 { 12794 cp_token *token = cp_lexer_peek_token (parser->lexer); 12795 error_at (token->location, 12796 "expected parameter pack before %<...%>"); 12797 } 12798 /* Consume the `...' token. */ 12799 cp_lexer_consume_token (parser->lexer); 12800 12801 /* Make the argument into a TYPE_PACK_EXPANSION or 12802 EXPR_PACK_EXPANSION. */ 12803 argument = make_pack_expansion (argument); 12804 } 12805 12806 if (n_args == alloced) 12807 { 12808 alloced *= 2; 12809 12810 if (arg_ary == fixed_args) 12811 { 12812 arg_ary = XNEWVEC (tree, alloced); 12813 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); 12814 } 12815 else 12816 arg_ary = XRESIZEVEC (tree, arg_ary, alloced); 12817 } 12818 arg_ary[n_args++] = argument; 12819 } 12820 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); 12821 12822 vec = make_tree_vec (n_args); 12823 12824 while (n_args--) 12825 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; 12826 12827 if (arg_ary != fixed_args) 12828 free (arg_ary); 12829 parser->non_integral_constant_expression_p = saved_non_ice_p; 12830 parser->integral_constant_expression_p = saved_ice_p; 12831 parser->in_template_argument_list_p = saved_in_template_argument_list_p; 12832 #ifdef ENABLE_CHECKING 12833 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec)); 12834 #endif 12835 return vec; 12836 } 12837 12838 /* Parse a template-argument. 12839 12840 template-argument: 12841 assignment-expression 12842 type-id 12843 id-expression 12844 12845 The representation is that of an assignment-expression, type-id, or 12846 id-expression -- except that the qualified id-expression is 12847 evaluated, so that the value returned is either a DECL or an 12848 OVERLOAD. 12849 12850 Although the standard says "assignment-expression", it forbids 12851 throw-expressions or assignments in the template argument. 12852 Therefore, we use "conditional-expression" instead. */ 12853 12854 static tree 12855 cp_parser_template_argument (cp_parser* parser) 12856 { 12857 tree argument; 12858 bool template_p; 12859 bool address_p; 12860 bool maybe_type_id = false; 12861 cp_token *token = NULL, *argument_start_token = NULL; 12862 cp_id_kind idk; 12863 12864 /* There's really no way to know what we're looking at, so we just 12865 try each alternative in order. 12866 12867 [temp.arg] 12868 12869 In a template-argument, an ambiguity between a type-id and an 12870 expression is resolved to a type-id, regardless of the form of 12871 the corresponding template-parameter. 12872 12873 Therefore, we try a type-id first. */ 12874 cp_parser_parse_tentatively (parser); 12875 argument = cp_parser_template_type_arg (parser); 12876 /* If there was no error parsing the type-id but the next token is a 12877 '>>', our behavior depends on which dialect of C++ we're 12878 parsing. In C++98, we probably found a typo for '> >'. But there 12879 are type-id which are also valid expressions. For instance: 12880 12881 struct X { int operator >> (int); }; 12882 template <int V> struct Foo {}; 12883 Foo<X () >> 5> r; 12884 12885 Here 'X()' is a valid type-id of a function type, but the user just 12886 wanted to write the expression "X() >> 5". Thus, we remember that we 12887 found a valid type-id, but we still try to parse the argument as an 12888 expression to see what happens. 12889 12890 In C++0x, the '>>' will be considered two separate '>' 12891 tokens. */ 12892 if (!cp_parser_error_occurred (parser) 12893 && cxx_dialect == cxx98 12894 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) 12895 { 12896 maybe_type_id = true; 12897 cp_parser_abort_tentative_parse (parser); 12898 } 12899 else 12900 { 12901 /* If the next token isn't a `,' or a `>', then this argument wasn't 12902 really finished. This means that the argument is not a valid 12903 type-id. */ 12904 if (!cp_parser_next_token_ends_template_argument_p (parser)) 12905 cp_parser_error (parser, "expected template-argument"); 12906 /* If that worked, we're done. */ 12907 if (cp_parser_parse_definitely (parser)) 12908 return argument; 12909 } 12910 /* We're still not sure what the argument will be. */ 12911 cp_parser_parse_tentatively (parser); 12912 /* Try a template. */ 12913 argument_start_token = cp_lexer_peek_token (parser->lexer); 12914 argument = cp_parser_id_expression (parser, 12915 /*template_keyword_p=*/false, 12916 /*check_dependency_p=*/true, 12917 &template_p, 12918 /*declarator_p=*/false, 12919 /*optional_p=*/false); 12920 /* If the next token isn't a `,' or a `>', then this argument wasn't 12921 really finished. */ 12922 if (!cp_parser_next_token_ends_template_argument_p (parser)) 12923 cp_parser_error (parser, "expected template-argument"); 12924 if (!cp_parser_error_occurred (parser)) 12925 { 12926 /* Figure out what is being referred to. If the id-expression 12927 was for a class template specialization, then we will have a 12928 TYPE_DECL at this point. There is no need to do name lookup 12929 at this point in that case. */ 12930 if (TREE_CODE (argument) != TYPE_DECL) 12931 argument = cp_parser_lookup_name (parser, argument, 12932 none_type, 12933 /*is_template=*/template_p, 12934 /*is_namespace=*/false, 12935 /*check_dependency=*/true, 12936 /*ambiguous_decls=*/NULL, 12937 argument_start_token->location); 12938 if (TREE_CODE (argument) != TEMPLATE_DECL 12939 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) 12940 cp_parser_error (parser, "expected template-name"); 12941 } 12942 if (cp_parser_parse_definitely (parser)) 12943 return argument; 12944 /* It must be a non-type argument. There permitted cases are given 12945 in [temp.arg.nontype]: 12946 12947 -- an integral constant-expression of integral or enumeration 12948 type; or 12949 12950 -- the name of a non-type template-parameter; or 12951 12952 -- the name of an object or function with external linkage... 12953 12954 -- the address of an object or function with external linkage... 12955 12956 -- a pointer to member... */ 12957 /* Look for a non-type template parameter. */ 12958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 12959 { 12960 cp_parser_parse_tentatively (parser); 12961 argument = cp_parser_primary_expression (parser, 12962 /*address_p=*/false, 12963 /*cast_p=*/false, 12964 /*template_arg_p=*/true, 12965 &idk); 12966 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX 12967 || !cp_parser_next_token_ends_template_argument_p (parser)) 12968 cp_parser_simulate_error (parser); 12969 if (cp_parser_parse_definitely (parser)) 12970 return argument; 12971 } 12972 12973 /* If the next token is "&", the argument must be the address of an 12974 object or function with external linkage. */ 12975 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); 12976 if (address_p) 12977 cp_lexer_consume_token (parser->lexer); 12978 /* See if we might have an id-expression. */ 12979 token = cp_lexer_peek_token (parser->lexer); 12980 if (token->type == CPP_NAME 12981 || token->keyword == RID_OPERATOR 12982 || token->type == CPP_SCOPE 12983 || token->type == CPP_TEMPLATE_ID 12984 || token->type == CPP_NESTED_NAME_SPECIFIER) 12985 { 12986 cp_parser_parse_tentatively (parser); 12987 argument = cp_parser_primary_expression (parser, 12988 address_p, 12989 /*cast_p=*/false, 12990 /*template_arg_p=*/true, 12991 &idk); 12992 if (cp_parser_error_occurred (parser) 12993 || !cp_parser_next_token_ends_template_argument_p (parser)) 12994 cp_parser_abort_tentative_parse (parser); 12995 else 12996 { 12997 tree probe; 12998 12999 if (TREE_CODE (argument) == INDIRECT_REF) 13000 { 13001 gcc_assert (REFERENCE_REF_P (argument)); 13002 argument = TREE_OPERAND (argument, 0); 13003 } 13004 13005 /* If we're in a template, we represent a qualified-id referring 13006 to a static data member as a SCOPE_REF even if the scope isn't 13007 dependent so that we can check access control later. */ 13008 probe = argument; 13009 if (TREE_CODE (probe) == SCOPE_REF) 13010 probe = TREE_OPERAND (probe, 1); 13011 if (TREE_CODE (probe) == VAR_DECL) 13012 { 13013 /* A variable without external linkage might still be a 13014 valid constant-expression, so no error is issued here 13015 if the external-linkage check fails. */ 13016 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe)) 13017 cp_parser_simulate_error (parser); 13018 } 13019 else if (is_overloaded_fn (argument)) 13020 /* All overloaded functions are allowed; if the external 13021 linkage test does not pass, an error will be issued 13022 later. */ 13023 ; 13024 else if (address_p 13025 && (TREE_CODE (argument) == OFFSET_REF 13026 || TREE_CODE (argument) == SCOPE_REF)) 13027 /* A pointer-to-member. */ 13028 ; 13029 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX) 13030 ; 13031 else 13032 cp_parser_simulate_error (parser); 13033 13034 if (cp_parser_parse_definitely (parser)) 13035 { 13036 if (address_p) 13037 argument = build_x_unary_op (ADDR_EXPR, argument, 13038 tf_warning_or_error); 13039 return argument; 13040 } 13041 } 13042 } 13043 /* If the argument started with "&", there are no other valid 13044 alternatives at this point. */ 13045 if (address_p) 13046 { 13047 cp_parser_error (parser, "invalid non-type template argument"); 13048 return error_mark_node; 13049 } 13050 13051 /* If the argument wasn't successfully parsed as a type-id followed 13052 by '>>', the argument can only be a constant expression now. 13053 Otherwise, we try parsing the constant-expression tentatively, 13054 because the argument could really be a type-id. */ 13055 if (maybe_type_id) 13056 cp_parser_parse_tentatively (parser); 13057 argument = cp_parser_constant_expression (parser, 13058 /*allow_non_constant_p=*/false, 13059 /*non_constant_p=*/NULL); 13060 argument = fold_non_dependent_expr (argument); 13061 if (!maybe_type_id) 13062 return argument; 13063 if (!cp_parser_next_token_ends_template_argument_p (parser)) 13064 cp_parser_error (parser, "expected template-argument"); 13065 if (cp_parser_parse_definitely (parser)) 13066 return argument; 13067 /* We did our best to parse the argument as a non type-id, but that 13068 was the only alternative that matched (albeit with a '>' after 13069 it). We can assume it's just a typo from the user, and a 13070 diagnostic will then be issued. */ 13071 return cp_parser_template_type_arg (parser); 13072 } 13073 13074 /* Parse an explicit-instantiation. 13075 13076 explicit-instantiation: 13077 template declaration 13078 13079 Although the standard says `declaration', what it really means is: 13080 13081 explicit-instantiation: 13082 template decl-specifier-seq [opt] declarator [opt] ; 13083 13084 Things like `template int S<int>::i = 5, int S<double>::j;' are not 13085 supposed to be allowed. A defect report has been filed about this 13086 issue. 13087 13088 GNU Extension: 13089 13090 explicit-instantiation: 13091 storage-class-specifier template 13092 decl-specifier-seq [opt] declarator [opt] ; 13093 function-specifier template 13094 decl-specifier-seq [opt] declarator [opt] ; */ 13095 13096 static void 13097 cp_parser_explicit_instantiation (cp_parser* parser) 13098 { 13099 int declares_class_or_enum; 13100 cp_decl_specifier_seq decl_specifiers; 13101 tree extension_specifier = NULL_TREE; 13102 13103 timevar_push (TV_TEMPLATE_INST); 13104 13105 /* Look for an (optional) storage-class-specifier or 13106 function-specifier. */ 13107 if (cp_parser_allow_gnu_extensions_p (parser)) 13108 { 13109 extension_specifier 13110 = cp_parser_storage_class_specifier_opt (parser); 13111 if (!extension_specifier) 13112 extension_specifier 13113 = cp_parser_function_specifier_opt (parser, 13114 /*decl_specs=*/NULL); 13115 } 13116 13117 /* Look for the `template' keyword. */ 13118 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE); 13119 /* Let the front end know that we are processing an explicit 13120 instantiation. */ 13121 begin_explicit_instantiation (); 13122 /* [temp.explicit] says that we are supposed to ignore access 13123 control while processing explicit instantiation directives. */ 13124 push_deferring_access_checks (dk_no_check); 13125 /* Parse a decl-specifier-seq. */ 13126 cp_parser_decl_specifier_seq (parser, 13127 CP_PARSER_FLAGS_OPTIONAL, 13128 &decl_specifiers, 13129 &declares_class_or_enum); 13130 /* If there was exactly one decl-specifier, and it declared a class, 13131 and there's no declarator, then we have an explicit type 13132 instantiation. */ 13133 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) 13134 { 13135 tree type; 13136 13137 type = check_tag_decl (&decl_specifiers); 13138 /* Turn access control back on for names used during 13139 template instantiation. */ 13140 pop_deferring_access_checks (); 13141 if (type) 13142 do_type_instantiation (type, extension_specifier, 13143 /*complain=*/tf_error); 13144 } 13145 else 13146 { 13147 cp_declarator *declarator; 13148 tree decl; 13149 13150 /* Parse the declarator. */ 13151 declarator 13152 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 13153 /*ctor_dtor_or_conv_p=*/NULL, 13154 /*parenthesized_p=*/NULL, 13155 /*member_p=*/false); 13156 if (declares_class_or_enum & 2) 13157 cp_parser_check_for_definition_in_return_type (declarator, 13158 decl_specifiers.type, 13159 decl_specifiers.type_location); 13160 if (declarator != cp_error_declarator) 13161 { 13162 if (decl_specifiers.specs[(int)ds_inline]) 13163 permerror (input_location, "explicit instantiation shall not use" 13164 " %<inline%> specifier"); 13165 if (decl_specifiers.specs[(int)ds_constexpr]) 13166 permerror (input_location, "explicit instantiation shall not use" 13167 " %<constexpr%> specifier"); 13168 13169 decl = grokdeclarator (declarator, &decl_specifiers, 13170 NORMAL, 0, &decl_specifiers.attributes); 13171 /* Turn access control back on for names used during 13172 template instantiation. */ 13173 pop_deferring_access_checks (); 13174 /* Do the explicit instantiation. */ 13175 do_decl_instantiation (decl, extension_specifier); 13176 } 13177 else 13178 { 13179 pop_deferring_access_checks (); 13180 /* Skip the body of the explicit instantiation. */ 13181 cp_parser_skip_to_end_of_statement (parser); 13182 } 13183 } 13184 /* We're done with the instantiation. */ 13185 end_explicit_instantiation (); 13186 13187 cp_parser_consume_semicolon_at_end_of_statement (parser); 13188 13189 timevar_pop (TV_TEMPLATE_INST); 13190 } 13191 13192 /* Parse an explicit-specialization. 13193 13194 explicit-specialization: 13195 template < > declaration 13196 13197 Although the standard says `declaration', what it really means is: 13198 13199 explicit-specialization: 13200 template <> decl-specifier [opt] init-declarator [opt] ; 13201 template <> function-definition 13202 template <> explicit-specialization 13203 template <> template-declaration */ 13204 13205 static void 13206 cp_parser_explicit_specialization (cp_parser* parser) 13207 { 13208 bool need_lang_pop; 13209 cp_token *token = cp_lexer_peek_token (parser->lexer); 13210 13211 /* Look for the `template' keyword. */ 13212 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE); 13213 /* Look for the `<'. */ 13214 cp_parser_require (parser, CPP_LESS, RT_LESS); 13215 /* Look for the `>'. */ 13216 cp_parser_require (parser, CPP_GREATER, RT_GREATER); 13217 /* We have processed another parameter list. */ 13218 ++parser->num_template_parameter_lists; 13219 /* [temp] 13220 13221 A template ... explicit specialization ... shall not have C 13222 linkage. */ 13223 if (current_lang_name == lang_name_c) 13224 { 13225 error_at (token->location, "template specialization with C linkage"); 13226 /* Give it C++ linkage to avoid confusing other parts of the 13227 front end. */ 13228 push_lang_context (lang_name_cplusplus); 13229 need_lang_pop = true; 13230 } 13231 else 13232 need_lang_pop = false; 13233 /* Let the front end know that we are beginning a specialization. */ 13234 if (!begin_specialization ()) 13235 { 13236 end_specialization (); 13237 return; 13238 } 13239 13240 /* If the next keyword is `template', we need to figure out whether 13241 or not we're looking a template-declaration. */ 13242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) 13243 { 13244 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS 13245 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) 13246 cp_parser_template_declaration_after_export (parser, 13247 /*member_p=*/false); 13248 else 13249 cp_parser_explicit_specialization (parser); 13250 } 13251 else 13252 /* Parse the dependent declaration. */ 13253 cp_parser_single_declaration (parser, 13254 /*checks=*/NULL, 13255 /*member_p=*/false, 13256 /*explicit_specialization_p=*/true, 13257 /*friend_p=*/NULL); 13258 /* We're done with the specialization. */ 13259 end_specialization (); 13260 /* For the erroneous case of a template with C linkage, we pushed an 13261 implicit C++ linkage scope; exit that scope now. */ 13262 if (need_lang_pop) 13263 pop_lang_context (); 13264 /* We're done with this parameter list. */ 13265 --parser->num_template_parameter_lists; 13266 } 13267 13268 /* Parse a type-specifier. 13269 13270 type-specifier: 13271 simple-type-specifier 13272 class-specifier 13273 enum-specifier 13274 elaborated-type-specifier 13275 cv-qualifier 13276 13277 GNU Extension: 13278 13279 type-specifier: 13280 __complex__ 13281 13282 Returns a representation of the type-specifier. For a 13283 class-specifier, enum-specifier, or elaborated-type-specifier, a 13284 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. 13285 13286 The parser flags FLAGS is used to control type-specifier parsing. 13287 13288 If IS_DECLARATION is TRUE, then this type-specifier is appearing 13289 in a decl-specifier-seq. 13290 13291 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a 13292 class-specifier, enum-specifier, or elaborated-type-specifier, then 13293 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 13294 if a type is declared; 2 if it is defined. Otherwise, it is set to 13295 zero. 13296 13297 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a 13298 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it 13299 is set to FALSE. */ 13300 13301 static tree 13302 cp_parser_type_specifier (cp_parser* parser, 13303 cp_parser_flags flags, 13304 cp_decl_specifier_seq *decl_specs, 13305 bool is_declaration, 13306 int* declares_class_or_enum, 13307 bool* is_cv_qualifier) 13308 { 13309 tree type_spec = NULL_TREE; 13310 cp_token *token; 13311 enum rid keyword; 13312 cp_decl_spec ds = ds_last; 13313 13314 /* Assume this type-specifier does not declare a new type. */ 13315 if (declares_class_or_enum) 13316 *declares_class_or_enum = 0; 13317 /* And that it does not specify a cv-qualifier. */ 13318 if (is_cv_qualifier) 13319 *is_cv_qualifier = false; 13320 /* Peek at the next token. */ 13321 token = cp_lexer_peek_token (parser->lexer); 13322 13323 /* If we're looking at a keyword, we can use that to guide the 13324 production we choose. */ 13325 keyword = token->keyword; 13326 switch (keyword) 13327 { 13328 case RID_ENUM: 13329 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS)) 13330 goto elaborated_type_specifier; 13331 13332 /* Look for the enum-specifier. */ 13333 type_spec = cp_parser_enum_specifier (parser); 13334 /* If that worked, we're done. */ 13335 if (type_spec) 13336 { 13337 if (declares_class_or_enum) 13338 *declares_class_or_enum = 2; 13339 if (decl_specs) 13340 cp_parser_set_decl_spec_type (decl_specs, 13341 type_spec, 13342 token->location, 13343 /*type_definition_p=*/true); 13344 return type_spec; 13345 } 13346 else 13347 goto elaborated_type_specifier; 13348 13349 /* Any of these indicate either a class-specifier, or an 13350 elaborated-type-specifier. */ 13351 case RID_CLASS: 13352 case RID_STRUCT: 13353 case RID_UNION: 13354 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS)) 13355 goto elaborated_type_specifier; 13356 13357 /* Parse tentatively so that we can back up if we don't find a 13358 class-specifier. */ 13359 cp_parser_parse_tentatively (parser); 13360 /* Look for the class-specifier. */ 13361 type_spec = cp_parser_class_specifier (parser); 13362 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec); 13363 /* If that worked, we're done. */ 13364 if (cp_parser_parse_definitely (parser)) 13365 { 13366 if (declares_class_or_enum) 13367 *declares_class_or_enum = 2; 13368 if (decl_specs) 13369 cp_parser_set_decl_spec_type (decl_specs, 13370 type_spec, 13371 token->location, 13372 /*type_definition_p=*/true); 13373 return type_spec; 13374 } 13375 13376 /* Fall through. */ 13377 elaborated_type_specifier: 13378 /* We're declaring (not defining) a class or enum. */ 13379 if (declares_class_or_enum) 13380 *declares_class_or_enum = 1; 13381 13382 /* Fall through. */ 13383 case RID_TYPENAME: 13384 /* Look for an elaborated-type-specifier. */ 13385 type_spec 13386 = (cp_parser_elaborated_type_specifier 13387 (parser, 13388 decl_specs && decl_specs->specs[(int) ds_friend], 13389 is_declaration)); 13390 if (decl_specs) 13391 cp_parser_set_decl_spec_type (decl_specs, 13392 type_spec, 13393 token->location, 13394 /*type_definition_p=*/false); 13395 return type_spec; 13396 13397 case RID_CONST: 13398 ds = ds_const; 13399 if (is_cv_qualifier) 13400 *is_cv_qualifier = true; 13401 break; 13402 13403 case RID_VOLATILE: 13404 ds = ds_volatile; 13405 if (is_cv_qualifier) 13406 *is_cv_qualifier = true; 13407 break; 13408 13409 case RID_RESTRICT: 13410 ds = ds_restrict; 13411 if (is_cv_qualifier) 13412 *is_cv_qualifier = true; 13413 break; 13414 13415 case RID_COMPLEX: 13416 /* The `__complex__' keyword is a GNU extension. */ 13417 ds = ds_complex; 13418 break; 13419 13420 default: 13421 break; 13422 } 13423 13424 /* Handle simple keywords. */ 13425 if (ds != ds_last) 13426 { 13427 if (decl_specs) 13428 { 13429 ++decl_specs->specs[(int)ds]; 13430 decl_specs->any_specifiers_p = true; 13431 } 13432 return cp_lexer_consume_token (parser->lexer)->u.value; 13433 } 13434 13435 /* If we do not already have a type-specifier, assume we are looking 13436 at a simple-type-specifier. */ 13437 type_spec = cp_parser_simple_type_specifier (parser, 13438 decl_specs, 13439 flags); 13440 13441 /* If we didn't find a type-specifier, and a type-specifier was not 13442 optional in this context, issue an error message. */ 13443 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) 13444 { 13445 cp_parser_error (parser, "expected type specifier"); 13446 return error_mark_node; 13447 } 13448 13449 return type_spec; 13450 } 13451 13452 /* Parse a simple-type-specifier. 13453 13454 simple-type-specifier: 13455 :: [opt] nested-name-specifier [opt] type-name 13456 :: [opt] nested-name-specifier template template-id 13457 char 13458 wchar_t 13459 bool 13460 short 13461 int 13462 long 13463 signed 13464 unsigned 13465 float 13466 double 13467 void 13468 13469 C++0x Extension: 13470 13471 simple-type-specifier: 13472 auto 13473 decltype ( expression ) 13474 char16_t 13475 char32_t 13476 __underlying_type ( type-id ) 13477 13478 GNU Extension: 13479 13480 simple-type-specifier: 13481 __int128 13482 __typeof__ unary-expression 13483 __typeof__ ( type-id ) 13484 13485 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is 13486 appropriately updated. */ 13487 13488 static tree 13489 cp_parser_simple_type_specifier (cp_parser* parser, 13490 cp_decl_specifier_seq *decl_specs, 13491 cp_parser_flags flags) 13492 { 13493 tree type = NULL_TREE; 13494 cp_token *token; 13495 13496 /* Peek at the next token. */ 13497 token = cp_lexer_peek_token (parser->lexer); 13498 13499 /* If we're looking at a keyword, things are easy. */ 13500 switch (token->keyword) 13501 { 13502 case RID_CHAR: 13503 if (decl_specs) 13504 decl_specs->explicit_char_p = true; 13505 type = char_type_node; 13506 break; 13507 case RID_CHAR16: 13508 type = char16_type_node; 13509 break; 13510 case RID_CHAR32: 13511 type = char32_type_node; 13512 break; 13513 case RID_WCHAR: 13514 type = wchar_type_node; 13515 break; 13516 case RID_BOOL: 13517 type = boolean_type_node; 13518 break; 13519 case RID_SHORT: 13520 if (decl_specs) 13521 ++decl_specs->specs[(int) ds_short]; 13522 type = short_integer_type_node; 13523 break; 13524 case RID_INT: 13525 if (decl_specs) 13526 decl_specs->explicit_int_p = true; 13527 type = integer_type_node; 13528 break; 13529 case RID_INT128: 13530 if (!int128_integer_type_node) 13531 break; 13532 if (decl_specs) 13533 decl_specs->explicit_int128_p = true; 13534 type = int128_integer_type_node; 13535 break; 13536 case RID_LONG: 13537 if (decl_specs) 13538 ++decl_specs->specs[(int) ds_long]; 13539 type = long_integer_type_node; 13540 break; 13541 case RID_SIGNED: 13542 if (decl_specs) 13543 ++decl_specs->specs[(int) ds_signed]; 13544 type = integer_type_node; 13545 break; 13546 case RID_UNSIGNED: 13547 if (decl_specs) 13548 ++decl_specs->specs[(int) ds_unsigned]; 13549 type = unsigned_type_node; 13550 break; 13551 case RID_FLOAT: 13552 type = float_type_node; 13553 break; 13554 case RID_DOUBLE: 13555 type = double_type_node; 13556 break; 13557 case RID_VOID: 13558 type = void_type_node; 13559 break; 13560 13561 case RID_AUTO: 13562 maybe_warn_cpp0x (CPP0X_AUTO); 13563 type = make_auto (); 13564 break; 13565 13566 case RID_DECLTYPE: 13567 /* Since DR 743, decltype can either be a simple-type-specifier by 13568 itself or begin a nested-name-specifier. Parsing it will replace 13569 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE 13570 handling below decide what to do. */ 13571 cp_parser_decltype (parser); 13572 cp_lexer_set_token_position (parser->lexer, token); 13573 break; 13574 13575 case RID_TYPEOF: 13576 /* Consume the `typeof' token. */ 13577 cp_lexer_consume_token (parser->lexer); 13578 /* Parse the operand to `typeof'. */ 13579 type = cp_parser_sizeof_operand (parser, RID_TYPEOF); 13580 /* If it is not already a TYPE, take its type. */ 13581 if (!TYPE_P (type)) 13582 type = finish_typeof (type); 13583 13584 if (decl_specs) 13585 cp_parser_set_decl_spec_type (decl_specs, type, 13586 token->location, 13587 /*type_definition_p=*/false); 13588 13589 return type; 13590 13591 case RID_UNDERLYING_TYPE: 13592 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE); 13593 if (decl_specs) 13594 cp_parser_set_decl_spec_type (decl_specs, type, 13595 token->location, 13596 /*type_definition_p=*/false); 13597 13598 return type; 13599 13600 case RID_BASES: 13601 case RID_DIRECT_BASES: 13602 type = cp_parser_trait_expr (parser, token->keyword); 13603 if (decl_specs) 13604 cp_parser_set_decl_spec_type (decl_specs, type, 13605 token->location, 13606 /*type_definition_p=*/false); 13607 return type; 13608 default: 13609 break; 13610 } 13611 13612 /* If token is an already-parsed decltype not followed by ::, 13613 it's a simple-type-specifier. */ 13614 if (token->type == CPP_DECLTYPE 13615 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) 13616 { 13617 type = token->u.value; 13618 if (decl_specs) 13619 cp_parser_set_decl_spec_type (decl_specs, type, 13620 token->location, 13621 /*type_definition_p=*/false); 13622 cp_lexer_consume_token (parser->lexer); 13623 return type; 13624 } 13625 13626 /* If the type-specifier was for a built-in type, we're done. */ 13627 if (type) 13628 { 13629 /* Record the type. */ 13630 if (decl_specs 13631 && (token->keyword != RID_SIGNED 13632 && token->keyword != RID_UNSIGNED 13633 && token->keyword != RID_SHORT 13634 && token->keyword != RID_LONG)) 13635 cp_parser_set_decl_spec_type (decl_specs, 13636 type, 13637 token->location, 13638 /*type_definition_p=*/false); 13639 if (decl_specs) 13640 decl_specs->any_specifiers_p = true; 13641 13642 /* Consume the token. */ 13643 cp_lexer_consume_token (parser->lexer); 13644 13645 /* There is no valid C++ program where a non-template type is 13646 followed by a "<". That usually indicates that the user thought 13647 that the type was a template. */ 13648 cp_parser_check_for_invalid_template_id (parser, type, token->location); 13649 13650 return TYPE_NAME (type); 13651 } 13652 13653 /* The type-specifier must be a user-defined type. */ 13654 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 13655 { 13656 bool qualified_p; 13657 bool global_p; 13658 13659 /* Don't gobble tokens or issue error messages if this is an 13660 optional type-specifier. */ 13661 if (flags & CP_PARSER_FLAGS_OPTIONAL) 13662 cp_parser_parse_tentatively (parser); 13663 13664 /* Look for the optional `::' operator. */ 13665 global_p 13666 = (cp_parser_global_scope_opt (parser, 13667 /*current_scope_valid_p=*/false) 13668 != NULL_TREE); 13669 /* Look for the nested-name specifier. */ 13670 qualified_p 13671 = (cp_parser_nested_name_specifier_opt (parser, 13672 /*typename_keyword_p=*/false, 13673 /*check_dependency_p=*/true, 13674 /*type_p=*/false, 13675 /*is_declaration=*/false) 13676 != NULL_TREE); 13677 token = cp_lexer_peek_token (parser->lexer); 13678 /* If we have seen a nested-name-specifier, and the next token 13679 is `template', then we are using the template-id production. */ 13680 if (parser->scope 13681 && cp_parser_optional_template_keyword (parser)) 13682 { 13683 /* Look for the template-id. */ 13684 type = cp_parser_template_id (parser, 13685 /*template_keyword_p=*/true, 13686 /*check_dependency_p=*/true, 13687 /*is_declaration=*/false); 13688 /* If the template-id did not name a type, we are out of 13689 luck. */ 13690 if (TREE_CODE (type) != TYPE_DECL) 13691 { 13692 cp_parser_error (parser, "expected template-id for type"); 13693 type = NULL_TREE; 13694 } 13695 } 13696 /* Otherwise, look for a type-name. */ 13697 else 13698 type = cp_parser_type_name (parser); 13699 /* Keep track of all name-lookups performed in class scopes. */ 13700 if (type 13701 && !global_p 13702 && !qualified_p 13703 && TREE_CODE (type) == TYPE_DECL 13704 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE) 13705 maybe_note_name_used_in_class (DECL_NAME (type), type); 13706 /* If it didn't work out, we don't have a TYPE. */ 13707 if ((flags & CP_PARSER_FLAGS_OPTIONAL) 13708 && !cp_parser_parse_definitely (parser)) 13709 type = NULL_TREE; 13710 if (type && decl_specs) 13711 cp_parser_set_decl_spec_type (decl_specs, type, 13712 token->location, 13713 /*type_definition_p=*/false); 13714 } 13715 13716 /* If we didn't get a type-name, issue an error message. */ 13717 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) 13718 { 13719 cp_parser_error (parser, "expected type-name"); 13720 return error_mark_node; 13721 } 13722 13723 if (type && type != error_mark_node) 13724 { 13725 /* See if TYPE is an Objective-C type, and if so, parse and 13726 accept any protocol references following it. Do this before 13727 the cp_parser_check_for_invalid_template_id() call, because 13728 Objective-C types can be followed by '<...>' which would 13729 enclose protocol names rather than template arguments, and so 13730 everything is fine. */ 13731 if (c_dialect_objc () && !parser->scope 13732 && (objc_is_id (type) || objc_is_class_name (type))) 13733 { 13734 tree protos = cp_parser_objc_protocol_refs_opt (parser); 13735 tree qual_type = objc_get_protocol_qualified_type (type, protos); 13736 13737 /* Clobber the "unqualified" type previously entered into 13738 DECL_SPECS with the new, improved protocol-qualified version. */ 13739 if (decl_specs) 13740 decl_specs->type = qual_type; 13741 13742 return qual_type; 13743 } 13744 13745 /* There is no valid C++ program where a non-template type is 13746 followed by a "<". That usually indicates that the user 13747 thought that the type was a template. */ 13748 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type), 13749 token->location); 13750 } 13751 13752 return type; 13753 } 13754 13755 /* Parse a type-name. 13756 13757 type-name: 13758 class-name 13759 enum-name 13760 typedef-name 13761 simple-template-id [in c++0x] 13762 13763 enum-name: 13764 identifier 13765 13766 typedef-name: 13767 identifier 13768 13769 Returns a TYPE_DECL for the type. */ 13770 13771 static tree 13772 cp_parser_type_name (cp_parser* parser) 13773 { 13774 tree type_decl; 13775 13776 /* We can't know yet whether it is a class-name or not. */ 13777 cp_parser_parse_tentatively (parser); 13778 /* Try a class-name. */ 13779 type_decl = cp_parser_class_name (parser, 13780 /*typename_keyword_p=*/false, 13781 /*template_keyword_p=*/false, 13782 none_type, 13783 /*check_dependency_p=*/true, 13784 /*class_head_p=*/false, 13785 /*is_declaration=*/false); 13786 /* If it's not a class-name, keep looking. */ 13787 if (!cp_parser_parse_definitely (parser)) 13788 { 13789 if (cxx_dialect < cxx0x) 13790 /* It must be a typedef-name or an enum-name. */ 13791 return cp_parser_nonclass_name (parser); 13792 13793 cp_parser_parse_tentatively (parser); 13794 /* It is either a simple-template-id representing an 13795 instantiation of an alias template... */ 13796 type_decl = cp_parser_template_id (parser, 13797 /*template_keyword_p=*/false, 13798 /*check_dependency_p=*/false, 13799 /*is_declaration=*/false); 13800 /* Note that this must be an instantiation of an alias template 13801 because [temp.names]/6 says: 13802 13803 A template-id that names an alias template specialization 13804 is a type-name. 13805 13806 Whereas [temp.names]/7 says: 13807 13808 A simple-template-id that names a class template 13809 specialization is a class-name. */ 13810 if (type_decl != NULL_TREE 13811 && TREE_CODE (type_decl) == TYPE_DECL 13812 && TYPE_DECL_ALIAS_P (type_decl)) 13813 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl)); 13814 else 13815 cp_parser_simulate_error (parser); 13816 13817 if (!cp_parser_parse_definitely (parser)) 13818 /* ... Or a typedef-name or an enum-name. */ 13819 return cp_parser_nonclass_name (parser); 13820 } 13821 13822 return type_decl; 13823 } 13824 13825 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name. 13826 13827 enum-name: 13828 identifier 13829 13830 typedef-name: 13831 identifier 13832 13833 Returns a TYPE_DECL for the type. */ 13834 13835 static tree 13836 cp_parser_nonclass_name (cp_parser* parser) 13837 { 13838 tree type_decl; 13839 tree identifier; 13840 13841 cp_token *token = cp_lexer_peek_token (parser->lexer); 13842 identifier = cp_parser_identifier (parser); 13843 if (identifier == error_mark_node) 13844 return error_mark_node; 13845 13846 /* Look up the type-name. */ 13847 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location); 13848 13849 type_decl = strip_using_decl (type_decl); 13850 13851 if (TREE_CODE (type_decl) != TYPE_DECL 13852 && (objc_is_id (identifier) || objc_is_class_name (identifier))) 13853 { 13854 /* See if this is an Objective-C type. */ 13855 tree protos = cp_parser_objc_protocol_refs_opt (parser); 13856 tree type = objc_get_protocol_qualified_type (identifier, protos); 13857 if (type) 13858 type_decl = TYPE_NAME (type); 13859 } 13860 13861 /* Issue an error if we did not find a type-name. */ 13862 if (TREE_CODE (type_decl) != TYPE_DECL 13863 /* In Objective-C, we have the complication that class names are 13864 normally type names and start declarations (eg, the 13865 "NSObject" in "NSObject *object;"), but can be used in an 13866 Objective-C 2.0 dot-syntax (as in "NSObject.version") which 13867 is an expression. So, a classname followed by a dot is not a 13868 valid type-name. */ 13869 || (objc_is_class_name (TREE_TYPE (type_decl)) 13870 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT)) 13871 { 13872 if (!cp_parser_simulate_error (parser)) 13873 cp_parser_name_lookup_error (parser, identifier, type_decl, 13874 NLE_TYPE, token->location); 13875 return error_mark_node; 13876 } 13877 /* Remember that the name was used in the definition of the 13878 current class so that we can check later to see if the 13879 meaning would have been different after the class was 13880 entirely defined. */ 13881 else if (type_decl != error_mark_node 13882 && !parser->scope) 13883 maybe_note_name_used_in_class (identifier, type_decl); 13884 13885 return type_decl; 13886 } 13887 13888 /* Parse an elaborated-type-specifier. Note that the grammar given 13889 here incorporates the resolution to DR68. 13890 13891 elaborated-type-specifier: 13892 class-key :: [opt] nested-name-specifier [opt] identifier 13893 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id 13894 enum-key :: [opt] nested-name-specifier [opt] identifier 13895 typename :: [opt] nested-name-specifier identifier 13896 typename :: [opt] nested-name-specifier template [opt] 13897 template-id 13898 13899 GNU extension: 13900 13901 elaborated-type-specifier: 13902 class-key attributes :: [opt] nested-name-specifier [opt] identifier 13903 class-key attributes :: [opt] nested-name-specifier [opt] 13904 template [opt] template-id 13905 enum attributes :: [opt] nested-name-specifier [opt] identifier 13906 13907 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being 13908 declared `friend'. If IS_DECLARATION is TRUE, then this 13909 elaborated-type-specifier appears in a decl-specifiers-seq, i.e., 13910 something is being declared. 13911 13912 Returns the TYPE specified. */ 13913 13914 static tree 13915 cp_parser_elaborated_type_specifier (cp_parser* parser, 13916 bool is_friend, 13917 bool is_declaration) 13918 { 13919 enum tag_types tag_type; 13920 tree identifier; 13921 tree type = NULL_TREE; 13922 tree attributes = NULL_TREE; 13923 tree globalscope; 13924 cp_token *token = NULL; 13925 13926 /* See if we're looking at the `enum' keyword. */ 13927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) 13928 { 13929 /* Consume the `enum' token. */ 13930 cp_lexer_consume_token (parser->lexer); 13931 /* Remember that it's an enumeration type. */ 13932 tag_type = enum_type; 13933 /* Issue a warning if the `struct' or `class' key (for C++0x scoped 13934 enums) is used here. */ 13935 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS) 13936 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT)) 13937 { 13938 pedwarn (input_location, 0, "elaborated-type-specifier " 13939 "for a scoped enum must not use the %<%D%> keyword", 13940 cp_lexer_peek_token (parser->lexer)->u.value); 13941 /* Consume the `struct' or `class' and parse it anyway. */ 13942 cp_lexer_consume_token (parser->lexer); 13943 } 13944 /* Parse the attributes. */ 13945 attributes = cp_parser_attributes_opt (parser); 13946 } 13947 /* Or, it might be `typename'. */ 13948 else if (cp_lexer_next_token_is_keyword (parser->lexer, 13949 RID_TYPENAME)) 13950 { 13951 /* Consume the `typename' token. */ 13952 cp_lexer_consume_token (parser->lexer); 13953 /* Remember that it's a `typename' type. */ 13954 tag_type = typename_type; 13955 } 13956 /* Otherwise it must be a class-key. */ 13957 else 13958 { 13959 tag_type = cp_parser_class_key (parser); 13960 if (tag_type == none_type) 13961 return error_mark_node; 13962 /* Parse the attributes. */ 13963 attributes = cp_parser_attributes_opt (parser); 13964 } 13965 13966 /* Look for the `::' operator. */ 13967 globalscope = cp_parser_global_scope_opt (parser, 13968 /*current_scope_valid_p=*/false); 13969 /* Look for the nested-name-specifier. */ 13970 if (tag_type == typename_type && !globalscope) 13971 { 13972 if (!cp_parser_nested_name_specifier (parser, 13973 /*typename_keyword_p=*/true, 13974 /*check_dependency_p=*/true, 13975 /*type_p=*/true, 13976 is_declaration)) 13977 return error_mark_node; 13978 } 13979 else 13980 /* Even though `typename' is not present, the proposed resolution 13981 to Core Issue 180 says that in `class A<T>::B', `B' should be 13982 considered a type-name, even if `A<T>' is dependent. */ 13983 cp_parser_nested_name_specifier_opt (parser, 13984 /*typename_keyword_p=*/true, 13985 /*check_dependency_p=*/true, 13986 /*type_p=*/true, 13987 is_declaration); 13988 /* For everything but enumeration types, consider a template-id. 13989 For an enumeration type, consider only a plain identifier. */ 13990 if (tag_type != enum_type) 13991 { 13992 bool template_p = false; 13993 tree decl; 13994 13995 /* Allow the `template' keyword. */ 13996 template_p = cp_parser_optional_template_keyword (parser); 13997 /* If we didn't see `template', we don't know if there's a 13998 template-id or not. */ 13999 if (!template_p) 14000 cp_parser_parse_tentatively (parser); 14001 /* Parse the template-id. */ 14002 token = cp_lexer_peek_token (parser->lexer); 14003 decl = cp_parser_template_id (parser, template_p, 14004 /*check_dependency_p=*/true, 14005 is_declaration); 14006 /* If we didn't find a template-id, look for an ordinary 14007 identifier. */ 14008 if (!template_p && !cp_parser_parse_definitely (parser)) 14009 ; 14010 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is 14011 in effect, then we must assume that, upon instantiation, the 14012 template will correspond to a class. */ 14013 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 14014 && tag_type == typename_type) 14015 type = make_typename_type (parser->scope, decl, 14016 typename_type, 14017 /*complain=*/tf_error); 14018 /* If the `typename' keyword is in effect and DECL is not a type 14019 decl, then type is non existent. */ 14020 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL) 14021 ; 14022 else if (TREE_CODE (decl) == TYPE_DECL) 14023 type = check_elaborated_type_specifier (tag_type, decl, 14024 /*allow_template_p=*/true); 14025 else if (decl == error_mark_node) 14026 type = error_mark_node; 14027 } 14028 14029 if (!type) 14030 { 14031 token = cp_lexer_peek_token (parser->lexer); 14032 identifier = cp_parser_identifier (parser); 14033 14034 if (identifier == error_mark_node) 14035 { 14036 parser->scope = NULL_TREE; 14037 return error_mark_node; 14038 } 14039 14040 /* For a `typename', we needn't call xref_tag. */ 14041 if (tag_type == typename_type 14042 && TREE_CODE (parser->scope) != NAMESPACE_DECL) 14043 return cp_parser_make_typename_type (parser, parser->scope, 14044 identifier, 14045 token->location); 14046 /* Look up a qualified name in the usual way. */ 14047 if (parser->scope) 14048 { 14049 tree decl; 14050 tree ambiguous_decls; 14051 14052 decl = cp_parser_lookup_name (parser, identifier, 14053 tag_type, 14054 /*is_template=*/false, 14055 /*is_namespace=*/false, 14056 /*check_dependency=*/true, 14057 &ambiguous_decls, 14058 token->location); 14059 14060 /* If the lookup was ambiguous, an error will already have been 14061 issued. */ 14062 if (ambiguous_decls) 14063 return error_mark_node; 14064 14065 /* If we are parsing friend declaration, DECL may be a 14066 TEMPLATE_DECL tree node here. However, we need to check 14067 whether this TEMPLATE_DECL results in valid code. Consider 14068 the following example: 14069 14070 namespace N { 14071 template <class T> class C {}; 14072 } 14073 class X { 14074 template <class T> friend class N::C; // #1, valid code 14075 }; 14076 template <class T> class Y { 14077 friend class N::C; // #2, invalid code 14078 }; 14079 14080 For both case #1 and #2, we arrive at a TEMPLATE_DECL after 14081 name lookup of `N::C'. We see that friend declaration must 14082 be template for the code to be valid. Note that 14083 processing_template_decl does not work here since it is 14084 always 1 for the above two cases. */ 14085 14086 decl = (cp_parser_maybe_treat_template_as_class 14087 (decl, /*tag_name_p=*/is_friend 14088 && parser->num_template_parameter_lists)); 14089 14090 if (TREE_CODE (decl) != TYPE_DECL) 14091 { 14092 cp_parser_diagnose_invalid_type_name (parser, 14093 parser->scope, 14094 identifier, 14095 token->location); 14096 return error_mark_node; 14097 } 14098 14099 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) 14100 { 14101 bool allow_template = (parser->num_template_parameter_lists 14102 || DECL_SELF_REFERENCE_P (decl)); 14103 type = check_elaborated_type_specifier (tag_type, decl, 14104 allow_template); 14105 14106 if (type == error_mark_node) 14107 return error_mark_node; 14108 } 14109 14110 /* Forward declarations of nested types, such as 14111 14112 class C1::C2; 14113 class C1::C2::C3; 14114 14115 are invalid unless all components preceding the final '::' 14116 are complete. If all enclosing types are complete, these 14117 declarations become merely pointless. 14118 14119 Invalid forward declarations of nested types are errors 14120 caught elsewhere in parsing. Those that are pointless arrive 14121 here. */ 14122 14123 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 14124 && !is_friend && !processing_explicit_instantiation) 14125 warning (0, "declaration %qD does not declare anything", decl); 14126 14127 type = TREE_TYPE (decl); 14128 } 14129 else 14130 { 14131 /* An elaborated-type-specifier sometimes introduces a new type and 14132 sometimes names an existing type. Normally, the rule is that it 14133 introduces a new type only if there is not an existing type of 14134 the same name already in scope. For example, given: 14135 14136 struct S {}; 14137 void f() { struct S s; } 14138 14139 the `struct S' in the body of `f' is the same `struct S' as in 14140 the global scope; the existing definition is used. However, if 14141 there were no global declaration, this would introduce a new 14142 local class named `S'. 14143 14144 An exception to this rule applies to the following code: 14145 14146 namespace N { struct S; } 14147 14148 Here, the elaborated-type-specifier names a new type 14149 unconditionally; even if there is already an `S' in the 14150 containing scope this declaration names a new type. 14151 This exception only applies if the elaborated-type-specifier 14152 forms the complete declaration: 14153 14154 [class.name] 14155 14156 A declaration consisting solely of `class-key identifier ;' is 14157 either a redeclaration of the name in the current scope or a 14158 forward declaration of the identifier as a class name. It 14159 introduces the name into the current scope. 14160 14161 We are in this situation precisely when the next token is a `;'. 14162 14163 An exception to the exception is that a `friend' declaration does 14164 *not* name a new type; i.e., given: 14165 14166 struct S { friend struct T; }; 14167 14168 `T' is not a new type in the scope of `S'. 14169 14170 Also, `new struct S' or `sizeof (struct S)' never results in the 14171 definition of a new type; a new type can only be declared in a 14172 declaration context. */ 14173 14174 tag_scope ts; 14175 bool template_p; 14176 14177 if (is_friend) 14178 /* Friends have special name lookup rules. */ 14179 ts = ts_within_enclosing_non_class; 14180 else if (is_declaration 14181 && cp_lexer_next_token_is (parser->lexer, 14182 CPP_SEMICOLON)) 14183 /* This is a `class-key identifier ;' */ 14184 ts = ts_current; 14185 else 14186 ts = ts_global; 14187 14188 template_p = 14189 (parser->num_template_parameter_lists 14190 && (cp_parser_next_token_starts_class_definition_p (parser) 14191 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))); 14192 /* An unqualified name was used to reference this type, so 14193 there were no qualifying templates. */ 14194 if (!cp_parser_check_template_parameters (parser, 14195 /*num_templates=*/0, 14196 token->location, 14197 /*declarator=*/NULL)) 14198 return error_mark_node; 14199 type = xref_tag (tag_type, identifier, ts, template_p); 14200 } 14201 } 14202 14203 if (type == error_mark_node) 14204 return error_mark_node; 14205 14206 /* Allow attributes on forward declarations of classes. */ 14207 if (attributes) 14208 { 14209 if (TREE_CODE (type) == TYPENAME_TYPE) 14210 warning (OPT_Wattributes, 14211 "attributes ignored on uninstantiated type"); 14212 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type) 14213 && ! processing_explicit_instantiation) 14214 warning (OPT_Wattributes, 14215 "attributes ignored on template instantiation"); 14216 else if (is_declaration && cp_parser_declares_only_class_p (parser)) 14217 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 14218 else 14219 warning (OPT_Wattributes, 14220 "attributes ignored on elaborated-type-specifier that is not a forward declaration"); 14221 } 14222 14223 if (tag_type != enum_type) 14224 { 14225 /* Indicate whether this class was declared as a `class' or as a 14226 `struct'. */ 14227 if (TREE_CODE (type) == RECORD_TYPE) 14228 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type); 14229 cp_parser_check_class_key (tag_type, type); 14230 } 14231 14232 /* A "<" cannot follow an elaborated type specifier. If that 14233 happens, the user was probably trying to form a template-id. */ 14234 cp_parser_check_for_invalid_template_id (parser, type, token->location); 14235 14236 return type; 14237 } 14238 14239 /* Parse an enum-specifier. 14240 14241 enum-specifier: 14242 enum-head { enumerator-list [opt] } 14243 enum-head { enumerator-list , } [C++0x] 14244 14245 enum-head: 14246 enum-key identifier [opt] enum-base [opt] 14247 enum-key nested-name-specifier identifier enum-base [opt] 14248 14249 enum-key: 14250 enum 14251 enum class [C++0x] 14252 enum struct [C++0x] 14253 14254 enum-base: [C++0x] 14255 : type-specifier-seq 14256 14257 opaque-enum-specifier: 14258 enum-key identifier enum-base [opt] ; 14259 14260 GNU Extensions: 14261 enum-key attributes[opt] identifier [opt] enum-base [opt] 14262 { enumerator-list [opt] }attributes[opt] 14263 enum-key attributes[opt] identifier [opt] enum-base [opt] 14264 { enumerator-list, }attributes[opt] [C++0x] 14265 14266 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE 14267 if the token stream isn't an enum-specifier after all. */ 14268 14269 static tree 14270 cp_parser_enum_specifier (cp_parser* parser) 14271 { 14272 tree identifier; 14273 tree type = NULL_TREE; 14274 tree prev_scope; 14275 tree nested_name_specifier = NULL_TREE; 14276 tree attributes; 14277 bool scoped_enum_p = false; 14278 bool has_underlying_type = false; 14279 bool nested_being_defined = false; 14280 bool new_value_list = false; 14281 bool is_new_type = false; 14282 bool is_anonymous = false; 14283 tree underlying_type = NULL_TREE; 14284 cp_token *type_start_token = NULL; 14285 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 14286 14287 parser->colon_corrects_to_scope_p = false; 14288 14289 /* Parse tentatively so that we can back up if we don't find a 14290 enum-specifier. */ 14291 cp_parser_parse_tentatively (parser); 14292 14293 /* Caller guarantees that the current token is 'enum', an identifier 14294 possibly follows, and the token after that is an opening brace. 14295 If we don't have an identifier, fabricate an anonymous name for 14296 the enumeration being defined. */ 14297 cp_lexer_consume_token (parser->lexer); 14298 14299 /* Parse the "class" or "struct", which indicates a scoped 14300 enumeration type in C++0x. */ 14301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS) 14302 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT)) 14303 { 14304 if (cxx_dialect < cxx0x) 14305 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS); 14306 14307 /* Consume the `struct' or `class' token. */ 14308 cp_lexer_consume_token (parser->lexer); 14309 14310 scoped_enum_p = true; 14311 } 14312 14313 attributes = cp_parser_attributes_opt (parser); 14314 14315 /* Clear the qualification. */ 14316 parser->scope = NULL_TREE; 14317 parser->qualifying_scope = NULL_TREE; 14318 parser->object_scope = NULL_TREE; 14319 14320 /* Figure out in what scope the declaration is being placed. */ 14321 prev_scope = current_scope (); 14322 14323 type_start_token = cp_lexer_peek_token (parser->lexer); 14324 14325 push_deferring_access_checks (dk_no_check); 14326 nested_name_specifier 14327 = cp_parser_nested_name_specifier_opt (parser, 14328 /*typename_keyword_p=*/true, 14329 /*check_dependency_p=*/false, 14330 /*type_p=*/false, 14331 /*is_declaration=*/false); 14332 14333 if (nested_name_specifier) 14334 { 14335 tree name; 14336 14337 identifier = cp_parser_identifier (parser); 14338 name = cp_parser_lookup_name (parser, identifier, 14339 enum_type, 14340 /*is_template=*/false, 14341 /*is_namespace=*/false, 14342 /*check_dependency=*/true, 14343 /*ambiguous_decls=*/NULL, 14344 input_location); 14345 if (name) 14346 { 14347 type = TREE_TYPE (name); 14348 if (TREE_CODE (type) == TYPENAME_TYPE) 14349 { 14350 /* Are template enums allowed in ISO? */ 14351 if (template_parm_scope_p ()) 14352 pedwarn (type_start_token->location, OPT_pedantic, 14353 "%qD is an enumeration template", name); 14354 /* ignore a typename reference, for it will be solved by name 14355 in start_enum. */ 14356 type = NULL_TREE; 14357 } 14358 } 14359 else 14360 error_at (type_start_token->location, 14361 "%qD is not an enumerator-name", identifier); 14362 } 14363 else 14364 { 14365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 14366 identifier = cp_parser_identifier (parser); 14367 else 14368 { 14369 identifier = make_anon_name (); 14370 is_anonymous = true; 14371 } 14372 } 14373 pop_deferring_access_checks (); 14374 14375 /* Check for the `:' that denotes a specified underlying type in C++0x. 14376 Note that a ':' could also indicate a bitfield width, however. */ 14377 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 14378 { 14379 cp_decl_specifier_seq type_specifiers; 14380 14381 /* Consume the `:'. */ 14382 cp_lexer_consume_token (parser->lexer); 14383 14384 /* Parse the type-specifier-seq. */ 14385 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false, 14386 /*is_trailing_return=*/false, 14387 &type_specifiers); 14388 14389 /* At this point this is surely not elaborated type specifier. */ 14390 if (!cp_parser_parse_definitely (parser)) 14391 return NULL_TREE; 14392 14393 if (cxx_dialect < cxx0x) 14394 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS); 14395 14396 has_underlying_type = true; 14397 14398 /* If that didn't work, stop. */ 14399 if (type_specifiers.type != error_mark_node) 14400 { 14401 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME, 14402 /*initialized=*/0, NULL); 14403 if (underlying_type == error_mark_node) 14404 underlying_type = NULL_TREE; 14405 } 14406 } 14407 14408 /* Look for the `{' but don't consume it yet. */ 14409 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 14410 { 14411 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type)) 14412 { 14413 cp_parser_error (parser, "expected %<{%>"); 14414 if (has_underlying_type) 14415 { 14416 type = NULL_TREE; 14417 goto out; 14418 } 14419 } 14420 /* An opaque-enum-specifier must have a ';' here. */ 14421 if ((scoped_enum_p || underlying_type) 14422 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 14423 { 14424 cp_parser_error (parser, "expected %<;%> or %<{%>"); 14425 if (has_underlying_type) 14426 { 14427 type = NULL_TREE; 14428 goto out; 14429 } 14430 } 14431 } 14432 14433 if (!has_underlying_type && !cp_parser_parse_definitely (parser)) 14434 return NULL_TREE; 14435 14436 if (nested_name_specifier) 14437 { 14438 if (CLASS_TYPE_P (nested_name_specifier)) 14439 { 14440 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier); 14441 TYPE_BEING_DEFINED (nested_name_specifier) = 1; 14442 push_scope (nested_name_specifier); 14443 } 14444 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL) 14445 { 14446 push_nested_namespace (nested_name_specifier); 14447 } 14448 } 14449 14450 /* Issue an error message if type-definitions are forbidden here. */ 14451 if (!cp_parser_check_type_definition (parser)) 14452 type = error_mark_node; 14453 else 14454 /* Create the new type. We do this before consuming the opening 14455 brace so the enum will be recorded as being on the line of its 14456 tag (or the 'enum' keyword, if there is no tag). */ 14457 type = start_enum (identifier, type, underlying_type, 14458 scoped_enum_p, &is_new_type); 14459 14460 /* If the next token is not '{' it is an opaque-enum-specifier or an 14461 elaborated-type-specifier. */ 14462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 14463 { 14464 timevar_push (TV_PARSE_ENUM); 14465 if (nested_name_specifier) 14466 { 14467 /* The following catches invalid code such as: 14468 enum class S<int>::E { A, B, C }; */ 14469 if (!processing_specialization 14470 && CLASS_TYPE_P (nested_name_specifier) 14471 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier)) 14472 error_at (type_start_token->location, "cannot add an enumerator " 14473 "list to a template instantiation"); 14474 14475 /* If that scope does not contain the scope in which the 14476 class was originally declared, the program is invalid. */ 14477 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier)) 14478 { 14479 if (at_namespace_scope_p ()) 14480 error_at (type_start_token->location, 14481 "declaration of %qD in namespace %qD which does not " 14482 "enclose %qD", 14483 type, prev_scope, nested_name_specifier); 14484 else 14485 error_at (type_start_token->location, 14486 "declaration of %qD in %qD which does not enclose %qD", 14487 type, prev_scope, nested_name_specifier); 14488 type = error_mark_node; 14489 } 14490 } 14491 14492 if (scoped_enum_p) 14493 begin_scope (sk_scoped_enum, type); 14494 14495 /* Consume the opening brace. */ 14496 cp_lexer_consume_token (parser->lexer); 14497 14498 if (type == error_mark_node) 14499 ; /* Nothing to add */ 14500 else if (OPAQUE_ENUM_P (type) 14501 || (cxx_dialect > cxx98 && processing_specialization)) 14502 { 14503 new_value_list = true; 14504 SET_OPAQUE_ENUM_P (type, false); 14505 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location; 14506 } 14507 else 14508 { 14509 error_at (type_start_token->location, "multiple definition of %q#T", type); 14510 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), 14511 "previous definition here"); 14512 type = error_mark_node; 14513 } 14514 14515 if (type == error_mark_node) 14516 cp_parser_skip_to_end_of_block_or_statement (parser); 14517 /* If the next token is not '}', then there are some enumerators. */ 14518 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) 14519 cp_parser_enumerator_list (parser, type); 14520 14521 /* Consume the final '}'. */ 14522 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 14523 14524 if (scoped_enum_p) 14525 finish_scope (); 14526 timevar_pop (TV_PARSE_ENUM); 14527 } 14528 else 14529 { 14530 /* If a ';' follows, then it is an opaque-enum-specifier 14531 and additional restrictions apply. */ 14532 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 14533 { 14534 if (is_anonymous) 14535 error_at (type_start_token->location, 14536 "opaque-enum-specifier without name"); 14537 else if (nested_name_specifier) 14538 error_at (type_start_token->location, 14539 "opaque-enum-specifier must use a simple identifier"); 14540 } 14541 } 14542 14543 /* Look for trailing attributes to apply to this enumeration, and 14544 apply them if appropriate. */ 14545 if (cp_parser_allow_gnu_extensions_p (parser)) 14546 { 14547 tree trailing_attr = cp_parser_attributes_opt (parser); 14548 trailing_attr = chainon (trailing_attr, attributes); 14549 cplus_decl_attributes (&type, 14550 trailing_attr, 14551 (int) ATTR_FLAG_TYPE_IN_PLACE); 14552 } 14553 14554 /* Finish up the enumeration. */ 14555 if (type != error_mark_node) 14556 { 14557 if (new_value_list) 14558 finish_enum_value_list (type); 14559 if (is_new_type) 14560 finish_enum (type); 14561 } 14562 14563 if (nested_name_specifier) 14564 { 14565 if (CLASS_TYPE_P (nested_name_specifier)) 14566 { 14567 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined; 14568 pop_scope (nested_name_specifier); 14569 } 14570 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL) 14571 { 14572 pop_nested_namespace (nested_name_specifier); 14573 } 14574 } 14575 out: 14576 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 14577 return type; 14578 } 14579 14580 /* Parse an enumerator-list. The enumerators all have the indicated 14581 TYPE. 14582 14583 enumerator-list: 14584 enumerator-definition 14585 enumerator-list , enumerator-definition */ 14586 14587 static void 14588 cp_parser_enumerator_list (cp_parser* parser, tree type) 14589 { 14590 while (true) 14591 { 14592 /* Parse an enumerator-definition. */ 14593 cp_parser_enumerator_definition (parser, type); 14594 14595 /* If the next token is not a ',', we've reached the end of 14596 the list. */ 14597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 14598 break; 14599 /* Otherwise, consume the `,' and keep going. */ 14600 cp_lexer_consume_token (parser->lexer); 14601 /* If the next token is a `}', there is a trailing comma. */ 14602 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 14603 { 14604 if (cxx_dialect < cxx0x && !in_system_header) 14605 pedwarn (input_location, OPT_pedantic, 14606 "comma at end of enumerator list"); 14607 break; 14608 } 14609 } 14610 } 14611 14612 /* Parse an enumerator-definition. The enumerator has the indicated 14613 TYPE. 14614 14615 enumerator-definition: 14616 enumerator 14617 enumerator = constant-expression 14618 14619 enumerator: 14620 identifier */ 14621 14622 static void 14623 cp_parser_enumerator_definition (cp_parser* parser, tree type) 14624 { 14625 tree identifier; 14626 tree value; 14627 location_t loc; 14628 14629 /* Save the input location because we are interested in the location 14630 of the identifier and not the location of the explicit value. */ 14631 loc = cp_lexer_peek_token (parser->lexer)->location; 14632 14633 /* Look for the identifier. */ 14634 identifier = cp_parser_identifier (parser); 14635 if (identifier == error_mark_node) 14636 return; 14637 14638 /* If the next token is an '=', then there is an explicit value. */ 14639 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 14640 { 14641 /* Consume the `=' token. */ 14642 cp_lexer_consume_token (parser->lexer); 14643 /* Parse the value. */ 14644 value = cp_parser_constant_expression (parser, 14645 /*allow_non_constant_p=*/false, 14646 NULL); 14647 } 14648 else 14649 value = NULL_TREE; 14650 14651 /* If we are processing a template, make sure the initializer of the 14652 enumerator doesn't contain any bare template parameter pack. */ 14653 if (check_for_bare_parameter_packs (value)) 14654 value = error_mark_node; 14655 14656 /* integral_constant_value will pull out this expression, so make sure 14657 it's folded as appropriate. */ 14658 value = fold_non_dependent_expr (value); 14659 14660 /* Create the enumerator. */ 14661 build_enumerator (identifier, value, type, loc); 14662 } 14663 14664 /* Parse a namespace-name. 14665 14666 namespace-name: 14667 original-namespace-name 14668 namespace-alias 14669 14670 Returns the NAMESPACE_DECL for the namespace. */ 14671 14672 static tree 14673 cp_parser_namespace_name (cp_parser* parser) 14674 { 14675 tree identifier; 14676 tree namespace_decl; 14677 14678 cp_token *token = cp_lexer_peek_token (parser->lexer); 14679 14680 /* Get the name of the namespace. */ 14681 identifier = cp_parser_identifier (parser); 14682 if (identifier == error_mark_node) 14683 return error_mark_node; 14684 14685 /* Look up the identifier in the currently active scope. Look only 14686 for namespaces, due to: 14687 14688 [basic.lookup.udir] 14689 14690 When looking up a namespace-name in a using-directive or alias 14691 definition, only namespace names are considered. 14692 14693 And: 14694 14695 [basic.lookup.qual] 14696 14697 During the lookup of a name preceding the :: scope resolution 14698 operator, object, function, and enumerator names are ignored. 14699 14700 (Note that cp_parser_qualifying_entity only calls this 14701 function if the token after the name is the scope resolution 14702 operator.) */ 14703 namespace_decl = cp_parser_lookup_name (parser, identifier, 14704 none_type, 14705 /*is_template=*/false, 14706 /*is_namespace=*/true, 14707 /*check_dependency=*/true, 14708 /*ambiguous_decls=*/NULL, 14709 token->location); 14710 /* If it's not a namespace, issue an error. */ 14711 if (namespace_decl == error_mark_node 14712 || TREE_CODE (namespace_decl) != NAMESPACE_DECL) 14713 { 14714 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) 14715 error_at (token->location, "%qD is not a namespace-name", identifier); 14716 cp_parser_error (parser, "expected namespace-name"); 14717 namespace_decl = error_mark_node; 14718 } 14719 14720 return namespace_decl; 14721 } 14722 14723 /* Parse a namespace-definition. 14724 14725 namespace-definition: 14726 named-namespace-definition 14727 unnamed-namespace-definition 14728 14729 named-namespace-definition: 14730 original-namespace-definition 14731 extension-namespace-definition 14732 14733 original-namespace-definition: 14734 namespace identifier { namespace-body } 14735 14736 extension-namespace-definition: 14737 namespace original-namespace-name { namespace-body } 14738 14739 unnamed-namespace-definition: 14740 namespace { namespace-body } */ 14741 14742 static void 14743 cp_parser_namespace_definition (cp_parser* parser) 14744 { 14745 tree identifier, attribs; 14746 bool has_visibility; 14747 bool is_inline; 14748 14749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE)) 14750 { 14751 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES); 14752 is_inline = true; 14753 cp_lexer_consume_token (parser->lexer); 14754 } 14755 else 14756 is_inline = false; 14757 14758 /* Look for the `namespace' keyword. */ 14759 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE); 14760 14761 /* Get the name of the namespace. We do not attempt to distinguish 14762 between an original-namespace-definition and an 14763 extension-namespace-definition at this point. The semantic 14764 analysis routines are responsible for that. */ 14765 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 14766 identifier = cp_parser_identifier (parser); 14767 else 14768 identifier = NULL_TREE; 14769 14770 /* Parse any specified attributes. */ 14771 attribs = cp_parser_attributes_opt (parser); 14772 14773 /* Look for the `{' to start the namespace. */ 14774 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE); 14775 /* Start the namespace. */ 14776 push_namespace (identifier); 14777 14778 /* "inline namespace" is equivalent to a stub namespace definition 14779 followed by a strong using directive. */ 14780 if (is_inline) 14781 { 14782 tree name_space = current_namespace; 14783 /* Set up namespace association. */ 14784 DECL_NAMESPACE_ASSOCIATIONS (name_space) 14785 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE, 14786 DECL_NAMESPACE_ASSOCIATIONS (name_space)); 14787 /* Import the contents of the inline namespace. */ 14788 pop_namespace (); 14789 do_using_directive (name_space); 14790 push_namespace (identifier); 14791 } 14792 14793 has_visibility = handle_namespace_attrs (current_namespace, attribs); 14794 14795 /* Parse the body of the namespace. */ 14796 cp_parser_namespace_body (parser); 14797 14798 if (has_visibility) 14799 pop_visibility (1); 14800 14801 /* Finish the namespace. */ 14802 pop_namespace (); 14803 /* Look for the final `}'. */ 14804 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 14805 } 14806 14807 /* Parse a namespace-body. 14808 14809 namespace-body: 14810 declaration-seq [opt] */ 14811 14812 static void 14813 cp_parser_namespace_body (cp_parser* parser) 14814 { 14815 cp_parser_declaration_seq_opt (parser); 14816 } 14817 14818 /* Parse a namespace-alias-definition. 14819 14820 namespace-alias-definition: 14821 namespace identifier = qualified-namespace-specifier ; */ 14822 14823 static void 14824 cp_parser_namespace_alias_definition (cp_parser* parser) 14825 { 14826 tree identifier; 14827 tree namespace_specifier; 14828 14829 cp_token *token = cp_lexer_peek_token (parser->lexer); 14830 14831 /* Look for the `namespace' keyword. */ 14832 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE); 14833 /* Look for the identifier. */ 14834 identifier = cp_parser_identifier (parser); 14835 if (identifier == error_mark_node) 14836 return; 14837 /* Look for the `=' token. */ 14838 if (!cp_parser_uncommitted_to_tentative_parse_p (parser) 14839 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 14840 { 14841 error_at (token->location, "%<namespace%> definition is not allowed here"); 14842 /* Skip the definition. */ 14843 cp_lexer_consume_token (parser->lexer); 14844 if (cp_parser_skip_to_closing_brace (parser)) 14845 cp_lexer_consume_token (parser->lexer); 14846 return; 14847 } 14848 cp_parser_require (parser, CPP_EQ, RT_EQ); 14849 /* Look for the qualified-namespace-specifier. */ 14850 namespace_specifier 14851 = cp_parser_qualified_namespace_specifier (parser); 14852 /* Look for the `;' token. */ 14853 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 14854 14855 /* Register the alias in the symbol table. */ 14856 do_namespace_alias (identifier, namespace_specifier); 14857 } 14858 14859 /* Parse a qualified-namespace-specifier. 14860 14861 qualified-namespace-specifier: 14862 :: [opt] nested-name-specifier [opt] namespace-name 14863 14864 Returns a NAMESPACE_DECL corresponding to the specified 14865 namespace. */ 14866 14867 static tree 14868 cp_parser_qualified_namespace_specifier (cp_parser* parser) 14869 { 14870 /* Look for the optional `::'. */ 14871 cp_parser_global_scope_opt (parser, 14872 /*current_scope_valid_p=*/false); 14873 14874 /* Look for the optional nested-name-specifier. */ 14875 cp_parser_nested_name_specifier_opt (parser, 14876 /*typename_keyword_p=*/false, 14877 /*check_dependency_p=*/true, 14878 /*type_p=*/false, 14879 /*is_declaration=*/true); 14880 14881 return cp_parser_namespace_name (parser); 14882 } 14883 14884 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an 14885 access declaration. 14886 14887 using-declaration: 14888 using typename [opt] :: [opt] nested-name-specifier unqualified-id ; 14889 using :: unqualified-id ; 14890 14891 access-declaration: 14892 qualified-id ; 14893 14894 */ 14895 14896 static bool 14897 cp_parser_using_declaration (cp_parser* parser, 14898 bool access_declaration_p) 14899 { 14900 cp_token *token; 14901 bool typename_p = false; 14902 bool global_scope_p; 14903 tree decl; 14904 tree identifier; 14905 tree qscope; 14906 int oldcount = errorcount; 14907 cp_token *diag_token = NULL; 14908 14909 if (access_declaration_p) 14910 { 14911 diag_token = cp_lexer_peek_token (parser->lexer); 14912 cp_parser_parse_tentatively (parser); 14913 } 14914 else 14915 { 14916 /* Look for the `using' keyword. */ 14917 cp_parser_require_keyword (parser, RID_USING, RT_USING); 14918 14919 /* Peek at the next token. */ 14920 token = cp_lexer_peek_token (parser->lexer); 14921 /* See if it's `typename'. */ 14922 if (token->keyword == RID_TYPENAME) 14923 { 14924 /* Remember that we've seen it. */ 14925 typename_p = true; 14926 /* Consume the `typename' token. */ 14927 cp_lexer_consume_token (parser->lexer); 14928 } 14929 } 14930 14931 /* Look for the optional global scope qualification. */ 14932 global_scope_p 14933 = (cp_parser_global_scope_opt (parser, 14934 /*current_scope_valid_p=*/false) 14935 != NULL_TREE); 14936 14937 /* If we saw `typename', or didn't see `::', then there must be a 14938 nested-name-specifier present. */ 14939 if (typename_p || !global_scope_p) 14940 qscope = cp_parser_nested_name_specifier (parser, typename_p, 14941 /*check_dependency_p=*/true, 14942 /*type_p=*/false, 14943 /*is_declaration=*/true); 14944 /* Otherwise, we could be in either of the two productions. In that 14945 case, treat the nested-name-specifier as optional. */ 14946 else 14947 qscope = cp_parser_nested_name_specifier_opt (parser, 14948 /*typename_keyword_p=*/false, 14949 /*check_dependency_p=*/true, 14950 /*type_p=*/false, 14951 /*is_declaration=*/true); 14952 if (!qscope) 14953 qscope = global_namespace; 14954 14955 if (access_declaration_p && cp_parser_error_occurred (parser)) 14956 /* Something has already gone wrong; there's no need to parse 14957 further. Since an error has occurred, the return value of 14958 cp_parser_parse_definitely will be false, as required. */ 14959 return cp_parser_parse_definitely (parser); 14960 14961 token = cp_lexer_peek_token (parser->lexer); 14962 /* Parse the unqualified-id. */ 14963 identifier = cp_parser_unqualified_id (parser, 14964 /*template_keyword_p=*/false, 14965 /*check_dependency_p=*/true, 14966 /*declarator_p=*/true, 14967 /*optional_p=*/false); 14968 14969 if (access_declaration_p) 14970 { 14971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 14972 cp_parser_simulate_error (parser); 14973 if (!cp_parser_parse_definitely (parser)) 14974 return false; 14975 } 14976 14977 /* The function we call to handle a using-declaration is different 14978 depending on what scope we are in. */ 14979 if (qscope == error_mark_node || identifier == error_mark_node) 14980 ; 14981 else if (TREE_CODE (identifier) != IDENTIFIER_NODE 14982 && TREE_CODE (identifier) != BIT_NOT_EXPR) 14983 /* [namespace.udecl] 14984 14985 A using declaration shall not name a template-id. */ 14986 error_at (token->location, 14987 "a template-id may not appear in a using-declaration"); 14988 else 14989 { 14990 if (at_class_scope_p ()) 14991 { 14992 /* Create the USING_DECL. */ 14993 decl = do_class_using_decl (parser->scope, identifier); 14994 14995 if (decl && typename_p) 14996 USING_DECL_TYPENAME_P (decl) = 1; 14997 14998 if (check_for_bare_parameter_packs (decl)) 14999 return false; 15000 else 15001 /* Add it to the list of members in this class. */ 15002 finish_member_declaration (decl); 15003 } 15004 else 15005 { 15006 decl = cp_parser_lookup_name_simple (parser, 15007 identifier, 15008 token->location); 15009 if (decl == error_mark_node) 15010 cp_parser_name_lookup_error (parser, identifier, 15011 decl, NLE_NULL, 15012 token->location); 15013 else if (check_for_bare_parameter_packs (decl)) 15014 return false; 15015 else if (!at_namespace_scope_p ()) 15016 do_local_using_decl (decl, qscope, identifier); 15017 else 15018 do_toplevel_using_decl (decl, qscope, identifier); 15019 } 15020 } 15021 15022 /* Look for the final `;'. */ 15023 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 15024 15025 if (access_declaration_p && errorcount == oldcount) 15026 warning_at (diag_token->location, OPT_Wdeprecated, 15027 "access declarations are deprecated " 15028 "in favour of using-declarations; " 15029 "suggestion: add the %<using%> keyword"); 15030 15031 return true; 15032 } 15033 15034 /* Parse an alias-declaration. 15035 15036 alias-declaration: 15037 using identifier attribute-specifier-seq [opt] = type-id */ 15038 15039 static tree 15040 cp_parser_alias_declaration (cp_parser* parser) 15041 { 15042 tree id, type, decl, pushed_scope = NULL_TREE, attributes; 15043 location_t id_location; 15044 cp_declarator *declarator; 15045 cp_decl_specifier_seq decl_specs; 15046 bool member_p; 15047 const char *saved_message = NULL; 15048 15049 /* Look for the `using' keyword. */ 15050 cp_parser_require_keyword (parser, RID_USING, RT_USING); 15051 id_location = cp_lexer_peek_token (parser->lexer)->location; 15052 id = cp_parser_identifier (parser); 15053 if (id == error_mark_node) 15054 return error_mark_node; 15055 15056 attributes = cp_parser_attributes_opt (parser); 15057 if (attributes == error_mark_node) 15058 return error_mark_node; 15059 15060 cp_parser_require (parser, CPP_EQ, RT_EQ); 15061 15062 if (cp_parser_error_occurred (parser)) 15063 return error_mark_node; 15064 15065 /* Now we are going to parse the type-id of the declaration. */ 15066 15067 /* 15068 [dcl.type]/3 says: 15069 15070 "A type-specifier-seq shall not define a class or enumeration 15071 unless it appears in the type-id of an alias-declaration (7.1.3) that 15072 is not the declaration of a template-declaration." 15073 15074 In other words, if we currently are in an alias template, the 15075 type-id should not define a type. 15076 15077 So let's set parser->type_definition_forbidden_message in that 15078 case; cp_parser_check_type_definition (called by 15079 cp_parser_class_specifier) will then emit an error if a type is 15080 defined in the type-id. */ 15081 if (parser->num_template_parameter_lists) 15082 { 15083 saved_message = parser->type_definition_forbidden_message; 15084 parser->type_definition_forbidden_message = 15085 G_("types may not be defined in alias template declarations"); 15086 } 15087 15088 type = cp_parser_type_id (parser); 15089 15090 /* Restore the error message if need be. */ 15091 if (parser->num_template_parameter_lists) 15092 parser->type_definition_forbidden_message = saved_message; 15093 15094 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 15095 15096 if (cp_parser_error_occurred (parser)) 15097 return error_mark_node; 15098 15099 /* A typedef-name can also be introduced by an alias-declaration. The 15100 identifier following the using keyword becomes a typedef-name. It has 15101 the same semantics as if it were introduced by the typedef 15102 specifier. In particular, it does not define a new type and it shall 15103 not appear in the type-id. */ 15104 15105 clear_decl_specs (&decl_specs); 15106 decl_specs.type = type; 15107 decl_specs.attributes = attributes; 15108 ++decl_specs.specs[(int) ds_typedef]; 15109 ++decl_specs.specs[(int) ds_alias]; 15110 15111 declarator = make_id_declarator (NULL_TREE, id, sfk_none); 15112 declarator->id_loc = id_location; 15113 15114 member_p = at_class_scope_p (); 15115 if (member_p) 15116 decl = grokfield (declarator, &decl_specs, NULL_TREE, false, 15117 NULL_TREE, attributes); 15118 else 15119 decl = start_decl (declarator, &decl_specs, 0, 15120 attributes, NULL_TREE, &pushed_scope); 15121 if (decl == error_mark_node) 15122 return decl; 15123 15124 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0); 15125 15126 if (pushed_scope) 15127 pop_scope (pushed_scope); 15128 15129 /* If decl is a template, return its TEMPLATE_DECL so that it gets 15130 added into the symbol table; otherwise, return the TYPE_DECL. */ 15131 if (DECL_LANG_SPECIFIC (decl) 15132 && DECL_TEMPLATE_INFO (decl) 15133 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) 15134 { 15135 decl = DECL_TI_TEMPLATE (decl); 15136 if (member_p) 15137 check_member_template (decl); 15138 } 15139 15140 return decl; 15141 } 15142 15143 /* Parse a using-directive. 15144 15145 using-directive: 15146 using namespace :: [opt] nested-name-specifier [opt] 15147 namespace-name ; */ 15148 15149 static void 15150 cp_parser_using_directive (cp_parser* parser) 15151 { 15152 tree namespace_decl; 15153 tree attribs; 15154 15155 /* Look for the `using' keyword. */ 15156 cp_parser_require_keyword (parser, RID_USING, RT_USING); 15157 /* And the `namespace' keyword. */ 15158 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE); 15159 /* Look for the optional `::' operator. */ 15160 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); 15161 /* And the optional nested-name-specifier. */ 15162 cp_parser_nested_name_specifier_opt (parser, 15163 /*typename_keyword_p=*/false, 15164 /*check_dependency_p=*/true, 15165 /*type_p=*/false, 15166 /*is_declaration=*/true); 15167 /* Get the namespace being used. */ 15168 namespace_decl = cp_parser_namespace_name (parser); 15169 /* And any specified attributes. */ 15170 attribs = cp_parser_attributes_opt (parser); 15171 /* Update the symbol table. */ 15172 parse_using_directive (namespace_decl, attribs); 15173 /* Look for the final `;'. */ 15174 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 15175 } 15176 15177 /* Parse an asm-definition. 15178 15179 asm-definition: 15180 asm ( string-literal ) ; 15181 15182 GNU Extension: 15183 15184 asm-definition: 15185 asm volatile [opt] ( string-literal ) ; 15186 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; 15187 asm volatile [opt] ( string-literal : asm-operand-list [opt] 15188 : asm-operand-list [opt] ) ; 15189 asm volatile [opt] ( string-literal : asm-operand-list [opt] 15190 : asm-operand-list [opt] 15191 : asm-clobber-list [opt] ) ; 15192 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt] 15193 : asm-clobber-list [opt] 15194 : asm-goto-list ) ; */ 15195 15196 static void 15197 cp_parser_asm_definition (cp_parser* parser) 15198 { 15199 tree string; 15200 tree outputs = NULL_TREE; 15201 tree inputs = NULL_TREE; 15202 tree clobbers = NULL_TREE; 15203 tree labels = NULL_TREE; 15204 tree asm_stmt; 15205 bool volatile_p = false; 15206 bool extended_p = false; 15207 bool invalid_inputs_p = false; 15208 bool invalid_outputs_p = false; 15209 bool goto_p = false; 15210 required_token missing = RT_NONE; 15211 15212 /* Look for the `asm' keyword. */ 15213 cp_parser_require_keyword (parser, RID_ASM, RT_ASM); 15214 /* See if the next token is `volatile'. */ 15215 if (cp_parser_allow_gnu_extensions_p (parser) 15216 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) 15217 { 15218 /* Remember that we saw the `volatile' keyword. */ 15219 volatile_p = true; 15220 /* Consume the token. */ 15221 cp_lexer_consume_token (parser->lexer); 15222 } 15223 if (cp_parser_allow_gnu_extensions_p (parser) 15224 && parser->in_function_body 15225 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO)) 15226 { 15227 /* Remember that we saw the `goto' keyword. */ 15228 goto_p = true; 15229 /* Consume the token. */ 15230 cp_lexer_consume_token (parser->lexer); 15231 } 15232 /* Look for the opening `('. */ 15233 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 15234 return; 15235 /* Look for the string. */ 15236 string = cp_parser_string_literal (parser, false, false); 15237 if (string == error_mark_node) 15238 { 15239 cp_parser_skip_to_closing_parenthesis (parser, true, false, 15240 /*consume_paren=*/true); 15241 return; 15242 } 15243 15244 /* If we're allowing GNU extensions, check for the extended assembly 15245 syntax. Unfortunately, the `:' tokens need not be separated by 15246 a space in C, and so, for compatibility, we tolerate that here 15247 too. Doing that means that we have to treat the `::' operator as 15248 two `:' tokens. */ 15249 if (cp_parser_allow_gnu_extensions_p (parser) 15250 && parser->in_function_body 15251 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) 15252 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) 15253 { 15254 bool inputs_p = false; 15255 bool clobbers_p = false; 15256 bool labels_p = false; 15257 15258 /* The extended syntax was used. */ 15259 extended_p = true; 15260 15261 /* Look for outputs. */ 15262 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 15263 { 15264 /* Consume the `:'. */ 15265 cp_lexer_consume_token (parser->lexer); 15266 /* Parse the output-operands. */ 15267 if (cp_lexer_next_token_is_not (parser->lexer, 15268 CPP_COLON) 15269 && cp_lexer_next_token_is_not (parser->lexer, 15270 CPP_SCOPE) 15271 && cp_lexer_next_token_is_not (parser->lexer, 15272 CPP_CLOSE_PAREN) 15273 && !goto_p) 15274 outputs = cp_parser_asm_operand_list (parser); 15275 15276 if (outputs == error_mark_node) 15277 invalid_outputs_p = true; 15278 } 15279 /* If the next token is `::', there are no outputs, and the 15280 next token is the beginning of the inputs. */ 15281 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 15282 /* The inputs are coming next. */ 15283 inputs_p = true; 15284 15285 /* Look for inputs. */ 15286 if (inputs_p 15287 || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 15288 { 15289 /* Consume the `:' or `::'. */ 15290 cp_lexer_consume_token (parser->lexer); 15291 /* Parse the output-operands. */ 15292 if (cp_lexer_next_token_is_not (parser->lexer, 15293 CPP_COLON) 15294 && cp_lexer_next_token_is_not (parser->lexer, 15295 CPP_SCOPE) 15296 && cp_lexer_next_token_is_not (parser->lexer, 15297 CPP_CLOSE_PAREN)) 15298 inputs = cp_parser_asm_operand_list (parser); 15299 15300 if (inputs == error_mark_node) 15301 invalid_inputs_p = true; 15302 } 15303 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 15304 /* The clobbers are coming next. */ 15305 clobbers_p = true; 15306 15307 /* Look for clobbers. */ 15308 if (clobbers_p 15309 || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 15310 { 15311 clobbers_p = true; 15312 /* Consume the `:' or `::'. */ 15313 cp_lexer_consume_token (parser->lexer); 15314 /* Parse the clobbers. */ 15315 if (cp_lexer_next_token_is_not (parser->lexer, 15316 CPP_COLON) 15317 && cp_lexer_next_token_is_not (parser->lexer, 15318 CPP_CLOSE_PAREN)) 15319 clobbers = cp_parser_asm_clobber_list (parser); 15320 } 15321 else if (goto_p 15322 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 15323 /* The labels are coming next. */ 15324 labels_p = true; 15325 15326 /* Look for labels. */ 15327 if (labels_p 15328 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON))) 15329 { 15330 labels_p = true; 15331 /* Consume the `:' or `::'. */ 15332 cp_lexer_consume_token (parser->lexer); 15333 /* Parse the labels. */ 15334 labels = cp_parser_asm_label_list (parser); 15335 } 15336 15337 if (goto_p && !labels_p) 15338 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE; 15339 } 15340 else if (goto_p) 15341 missing = RT_COLON_SCOPE; 15342 15343 /* Look for the closing `)'. */ 15344 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN, 15345 missing ? missing : RT_CLOSE_PAREN)) 15346 cp_parser_skip_to_closing_parenthesis (parser, true, false, 15347 /*consume_paren=*/true); 15348 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 15349 15350 if (!invalid_inputs_p && !invalid_outputs_p) 15351 { 15352 /* Create the ASM_EXPR. */ 15353 if (parser->in_function_body) 15354 { 15355 asm_stmt = finish_asm_stmt (volatile_p, string, outputs, 15356 inputs, clobbers, labels); 15357 /* If the extended syntax was not used, mark the ASM_EXPR. */ 15358 if (!extended_p) 15359 { 15360 tree temp = asm_stmt; 15361 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR) 15362 temp = TREE_OPERAND (temp, 0); 15363 15364 ASM_INPUT_P (temp) = 1; 15365 } 15366 } 15367 else 15368 cgraph_add_asm_node (string); 15369 } 15370 } 15371 15372 /* Declarators [gram.dcl.decl] */ 15373 15374 /* Parse an init-declarator. 15375 15376 init-declarator: 15377 declarator initializer [opt] 15378 15379 GNU Extension: 15380 15381 init-declarator: 15382 declarator asm-specification [opt] attributes [opt] initializer [opt] 15383 15384 function-definition: 15385 decl-specifier-seq [opt] declarator ctor-initializer [opt] 15386 function-body 15387 decl-specifier-seq [opt] declarator function-try-block 15388 15389 GNU Extension: 15390 15391 function-definition: 15392 __extension__ function-definition 15393 15394 TM Extension: 15395 15396 function-definition: 15397 decl-specifier-seq [opt] declarator function-transaction-block 15398 15399 The DECL_SPECIFIERS apply to this declarator. Returns a 15400 representation of the entity declared. If MEMBER_P is TRUE, then 15401 this declarator appears in a class scope. The new DECL created by 15402 this declarator is returned. 15403 15404 The CHECKS are access checks that should be performed once we know 15405 what entity is being declared (and, therefore, what classes have 15406 befriended it). 15407 15408 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and 15409 for a function-definition here as well. If the declarator is a 15410 declarator for a function-definition, *FUNCTION_DEFINITION_P will 15411 be TRUE upon return. By that point, the function-definition will 15412 have been completely parsed. 15413 15414 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P 15415 is FALSE. 15416 15417 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the 15418 parsed declaration if it is an uninitialized single declarator not followed 15419 by a `;', or to error_mark_node otherwise. Either way, the trailing `;', 15420 if present, will not be consumed. If returned, this declarator will be 15421 created with SD_INITIALIZED but will not call cp_finish_decl. */ 15422 15423 static tree 15424 cp_parser_init_declarator (cp_parser* parser, 15425 cp_decl_specifier_seq *decl_specifiers, 15426 VEC (deferred_access_check,gc)* checks, 15427 bool function_definition_allowed_p, 15428 bool member_p, 15429 int declares_class_or_enum, 15430 bool* function_definition_p, 15431 tree* maybe_range_for_decl) 15432 { 15433 cp_token *token = NULL, *asm_spec_start_token = NULL, 15434 *attributes_start_token = NULL; 15435 cp_declarator *declarator; 15436 tree prefix_attributes; 15437 tree attributes; 15438 tree asm_specification; 15439 tree initializer; 15440 tree decl = NULL_TREE; 15441 tree scope; 15442 int is_initialized; 15443 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if 15444 initialized with "= ..", CPP_OPEN_PAREN if initialized with 15445 "(...)". */ 15446 enum cpp_ttype initialization_kind; 15447 bool is_direct_init = false; 15448 bool is_non_constant_init; 15449 int ctor_dtor_or_conv_p; 15450 bool friend_p; 15451 tree pushed_scope = NULL_TREE; 15452 bool range_for_decl_p = false; 15453 15454 /* Gather the attributes that were provided with the 15455 decl-specifiers. */ 15456 prefix_attributes = decl_specifiers->attributes; 15457 15458 /* Assume that this is not the declarator for a function 15459 definition. */ 15460 if (function_definition_p) 15461 *function_definition_p = false; 15462 15463 /* Defer access checks while parsing the declarator; we cannot know 15464 what names are accessible until we know what is being 15465 declared. */ 15466 resume_deferring_access_checks (); 15467 15468 /* Parse the declarator. */ 15469 token = cp_lexer_peek_token (parser->lexer); 15470 declarator 15471 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 15472 &ctor_dtor_or_conv_p, 15473 /*parenthesized_p=*/NULL, 15474 member_p); 15475 /* Gather up the deferred checks. */ 15476 stop_deferring_access_checks (); 15477 15478 /* If the DECLARATOR was erroneous, there's no need to go 15479 further. */ 15480 if (declarator == cp_error_declarator) 15481 return error_mark_node; 15482 15483 /* Check that the number of template-parameter-lists is OK. */ 15484 if (!cp_parser_check_declarator_template_parameters (parser, declarator, 15485 token->location)) 15486 return error_mark_node; 15487 15488 if (declares_class_or_enum & 2) 15489 cp_parser_check_for_definition_in_return_type (declarator, 15490 decl_specifiers->type, 15491 decl_specifiers->type_location); 15492 15493 /* Figure out what scope the entity declared by the DECLARATOR is 15494 located in. `grokdeclarator' sometimes changes the scope, so 15495 we compute it now. */ 15496 scope = get_scope_of_declarator (declarator); 15497 15498 /* Perform any lookups in the declared type which were thought to be 15499 dependent, but are not in the scope of the declarator. */ 15500 decl_specifiers->type 15501 = maybe_update_decl_type (decl_specifiers->type, scope); 15502 15503 /* If we're allowing GNU extensions, look for an asm-specification 15504 and attributes. */ 15505 if (cp_parser_allow_gnu_extensions_p (parser)) 15506 { 15507 /* Look for an asm-specification. */ 15508 asm_spec_start_token = cp_lexer_peek_token (parser->lexer); 15509 asm_specification = cp_parser_asm_specification_opt (parser); 15510 /* And attributes. */ 15511 attributes_start_token = cp_lexer_peek_token (parser->lexer); 15512 attributes = cp_parser_attributes_opt (parser); 15513 } 15514 else 15515 { 15516 asm_specification = NULL_TREE; 15517 attributes = NULL_TREE; 15518 } 15519 15520 /* Peek at the next token. */ 15521 token = cp_lexer_peek_token (parser->lexer); 15522 /* Check to see if the token indicates the start of a 15523 function-definition. */ 15524 if (function_declarator_p (declarator) 15525 && cp_parser_token_starts_function_definition_p (token)) 15526 { 15527 if (!function_definition_allowed_p) 15528 { 15529 /* If a function-definition should not appear here, issue an 15530 error message. */ 15531 cp_parser_error (parser, 15532 "a function-definition is not allowed here"); 15533 return error_mark_node; 15534 } 15535 else 15536 { 15537 location_t func_brace_location 15538 = cp_lexer_peek_token (parser->lexer)->location; 15539 15540 /* Neither attributes nor an asm-specification are allowed 15541 on a function-definition. */ 15542 if (asm_specification) 15543 error_at (asm_spec_start_token->location, 15544 "an asm-specification is not allowed " 15545 "on a function-definition"); 15546 if (attributes) 15547 error_at (attributes_start_token->location, 15548 "attributes are not allowed on a function-definition"); 15549 /* This is a function-definition. */ 15550 *function_definition_p = true; 15551 15552 /* Parse the function definition. */ 15553 if (member_p) 15554 decl = cp_parser_save_member_function_body (parser, 15555 decl_specifiers, 15556 declarator, 15557 prefix_attributes); 15558 else 15559 decl 15560 = (cp_parser_function_definition_from_specifiers_and_declarator 15561 (parser, decl_specifiers, prefix_attributes, declarator)); 15562 15563 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl)) 15564 { 15565 /* This is where the prologue starts... */ 15566 DECL_STRUCT_FUNCTION (decl)->function_start_locus 15567 = func_brace_location; 15568 } 15569 15570 return decl; 15571 } 15572 } 15573 15574 /* [dcl.dcl] 15575 15576 Only in function declarations for constructors, destructors, and 15577 type conversions can the decl-specifier-seq be omitted. 15578 15579 We explicitly postpone this check past the point where we handle 15580 function-definitions because we tolerate function-definitions 15581 that are missing their return types in some modes. */ 15582 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0) 15583 { 15584 cp_parser_error (parser, 15585 "expected constructor, destructor, or type conversion"); 15586 return error_mark_node; 15587 } 15588 15589 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */ 15590 if (token->type == CPP_EQ 15591 || token->type == CPP_OPEN_PAREN 15592 || token->type == CPP_OPEN_BRACE) 15593 { 15594 is_initialized = SD_INITIALIZED; 15595 initialization_kind = token->type; 15596 if (maybe_range_for_decl) 15597 *maybe_range_for_decl = error_mark_node; 15598 15599 if (token->type == CPP_EQ 15600 && function_declarator_p (declarator)) 15601 { 15602 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2); 15603 if (t2->keyword == RID_DEFAULT) 15604 is_initialized = SD_DEFAULTED; 15605 else if (t2->keyword == RID_DELETE) 15606 is_initialized = SD_DELETED; 15607 } 15608 } 15609 else 15610 { 15611 /* If the init-declarator isn't initialized and isn't followed by a 15612 `,' or `;', it's not a valid init-declarator. */ 15613 if (token->type != CPP_COMMA 15614 && token->type != CPP_SEMICOLON) 15615 { 15616 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node) 15617 range_for_decl_p = true; 15618 else 15619 { 15620 cp_parser_error (parser, "expected initializer"); 15621 return error_mark_node; 15622 } 15623 } 15624 is_initialized = SD_UNINITIALIZED; 15625 initialization_kind = CPP_EOF; 15626 } 15627 15628 /* Because start_decl has side-effects, we should only call it if we 15629 know we're going ahead. By this point, we know that we cannot 15630 possibly be looking at any other construct. */ 15631 cp_parser_commit_to_tentative_parse (parser); 15632 15633 /* If the decl specifiers were bad, issue an error now that we're 15634 sure this was intended to be a declarator. Then continue 15635 declaring the variable(s), as int, to try to cut down on further 15636 errors. */ 15637 if (decl_specifiers->any_specifiers_p 15638 && decl_specifiers->type == error_mark_node) 15639 { 15640 cp_parser_error (parser, "invalid type in declaration"); 15641 decl_specifiers->type = integer_type_node; 15642 } 15643 15644 /* Check to see whether or not this declaration is a friend. */ 15645 friend_p = cp_parser_friend_p (decl_specifiers); 15646 15647 /* Enter the newly declared entry in the symbol table. If we're 15648 processing a declaration in a class-specifier, we wait until 15649 after processing the initializer. */ 15650 if (!member_p) 15651 { 15652 if (parser->in_unbraced_linkage_specification_p) 15653 decl_specifiers->storage_class = sc_extern; 15654 decl = start_decl (declarator, decl_specifiers, 15655 range_for_decl_p? SD_INITIALIZED : is_initialized, 15656 attributes, prefix_attributes, 15657 &pushed_scope); 15658 /* Adjust location of decl if declarator->id_loc is more appropriate: 15659 set, and decl wasn't merged with another decl, in which case its 15660 location would be different from input_location, and more accurate. */ 15661 if (DECL_P (decl) 15662 && declarator->id_loc != UNKNOWN_LOCATION 15663 && DECL_SOURCE_LOCATION (decl) == input_location) 15664 DECL_SOURCE_LOCATION (decl) = declarator->id_loc; 15665 } 15666 else if (scope) 15667 /* Enter the SCOPE. That way unqualified names appearing in the 15668 initializer will be looked up in SCOPE. */ 15669 pushed_scope = push_scope (scope); 15670 15671 /* Perform deferred access control checks, now that we know in which 15672 SCOPE the declared entity resides. */ 15673 if (!member_p && decl) 15674 { 15675 tree saved_current_function_decl = NULL_TREE; 15676 15677 /* If the entity being declared is a function, pretend that we 15678 are in its scope. If it is a `friend', it may have access to 15679 things that would not otherwise be accessible. */ 15680 if (TREE_CODE (decl) == FUNCTION_DECL) 15681 { 15682 saved_current_function_decl = current_function_decl; 15683 current_function_decl = decl; 15684 } 15685 15686 /* Perform access checks for template parameters. */ 15687 cp_parser_perform_template_parameter_access_checks (checks); 15688 15689 /* Perform the access control checks for the declarator and the 15690 decl-specifiers. */ 15691 perform_deferred_access_checks (); 15692 15693 /* Restore the saved value. */ 15694 if (TREE_CODE (decl) == FUNCTION_DECL) 15695 current_function_decl = saved_current_function_decl; 15696 } 15697 15698 /* Parse the initializer. */ 15699 initializer = NULL_TREE; 15700 is_direct_init = false; 15701 is_non_constant_init = true; 15702 if (is_initialized) 15703 { 15704 if (function_declarator_p (declarator)) 15705 { 15706 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer); 15707 if (initialization_kind == CPP_EQ) 15708 initializer = cp_parser_pure_specifier (parser); 15709 else 15710 { 15711 /* If the declaration was erroneous, we don't really 15712 know what the user intended, so just silently 15713 consume the initializer. */ 15714 if (decl != error_mark_node) 15715 error_at (initializer_start_token->location, 15716 "initializer provided for function"); 15717 cp_parser_skip_to_closing_parenthesis (parser, 15718 /*recovering=*/true, 15719 /*or_comma=*/false, 15720 /*consume_paren=*/true); 15721 } 15722 } 15723 else 15724 { 15725 /* We want to record the extra mangling scope for in-class 15726 initializers of class members and initializers of static data 15727 member templates. The former involves deferring 15728 parsing of the initializer until end of class as with default 15729 arguments. So right here we only handle the latter. */ 15730 if (!member_p && processing_template_decl) 15731 start_lambda_scope (decl); 15732 initializer = cp_parser_initializer (parser, 15733 &is_direct_init, 15734 &is_non_constant_init); 15735 if (!member_p && processing_template_decl) 15736 finish_lambda_scope (); 15737 } 15738 } 15739 15740 /* The old parser allows attributes to appear after a parenthesized 15741 initializer. Mark Mitchell proposed removing this functionality 15742 on the GCC mailing lists on 2002-08-13. This parser accepts the 15743 attributes -- but ignores them. */ 15744 if (cp_parser_allow_gnu_extensions_p (parser) 15745 && initialization_kind == CPP_OPEN_PAREN) 15746 if (cp_parser_attributes_opt (parser)) 15747 warning (OPT_Wattributes, 15748 "attributes after parenthesized initializer ignored"); 15749 15750 /* For an in-class declaration, use `grokfield' to create the 15751 declaration. */ 15752 if (member_p) 15753 { 15754 if (pushed_scope) 15755 { 15756 pop_scope (pushed_scope); 15757 pushed_scope = NULL_TREE; 15758 } 15759 decl = grokfield (declarator, decl_specifiers, 15760 initializer, !is_non_constant_init, 15761 /*asmspec=*/NULL_TREE, 15762 prefix_attributes); 15763 if (decl && TREE_CODE (decl) == FUNCTION_DECL) 15764 cp_parser_save_default_args (parser, decl); 15765 } 15766 15767 /* Finish processing the declaration. But, skip member 15768 declarations. */ 15769 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p) 15770 { 15771 cp_finish_decl (decl, 15772 initializer, !is_non_constant_init, 15773 asm_specification, 15774 /* If the initializer is in parentheses, then this is 15775 a direct-initialization, which means that an 15776 `explicit' constructor is OK. Otherwise, an 15777 `explicit' constructor cannot be used. */ 15778 ((is_direct_init || !is_initialized) 15779 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT)); 15780 } 15781 else if ((cxx_dialect != cxx98) && friend_p 15782 && decl && TREE_CODE (decl) == FUNCTION_DECL) 15783 /* Core issue #226 (C++0x only): A default template-argument 15784 shall not be specified in a friend class template 15785 declaration. */ 15786 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 15787 /*is_partial=*/0, /*is_friend_decl=*/1); 15788 15789 if (!friend_p && pushed_scope) 15790 pop_scope (pushed_scope); 15791 15792 return decl; 15793 } 15794 15795 /* Parse a declarator. 15796 15797 declarator: 15798 direct-declarator 15799 ptr-operator declarator 15800 15801 abstract-declarator: 15802 ptr-operator abstract-declarator [opt] 15803 direct-abstract-declarator 15804 15805 GNU Extensions: 15806 15807 declarator: 15808 attributes [opt] direct-declarator 15809 attributes [opt] ptr-operator declarator 15810 15811 abstract-declarator: 15812 attributes [opt] ptr-operator abstract-declarator [opt] 15813 attributes [opt] direct-abstract-declarator 15814 15815 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to 15816 detect constructor, destructor or conversion operators. It is set 15817 to -1 if the declarator is a name, and +1 if it is a 15818 function. Otherwise it is set to zero. Usually you just want to 15819 test for >0, but internally the negative value is used. 15820 15821 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have 15822 a decl-specifier-seq unless it declares a constructor, destructor, 15823 or conversion. It might seem that we could check this condition in 15824 semantic analysis, rather than parsing, but that makes it difficult 15825 to handle something like `f()'. We want to notice that there are 15826 no decl-specifiers, and therefore realize that this is an 15827 expression, not a declaration.) 15828 15829 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff 15830 the declarator is a direct-declarator of the form "(...)". 15831 15832 MEMBER_P is true iff this declarator is a member-declarator. */ 15833 15834 static cp_declarator * 15835 cp_parser_declarator (cp_parser* parser, 15836 cp_parser_declarator_kind dcl_kind, 15837 int* ctor_dtor_or_conv_p, 15838 bool* parenthesized_p, 15839 bool member_p) 15840 { 15841 cp_declarator *declarator; 15842 enum tree_code code; 15843 cp_cv_quals cv_quals; 15844 tree class_type; 15845 tree attributes = NULL_TREE; 15846 15847 /* Assume this is not a constructor, destructor, or type-conversion 15848 operator. */ 15849 if (ctor_dtor_or_conv_p) 15850 *ctor_dtor_or_conv_p = 0; 15851 15852 if (cp_parser_allow_gnu_extensions_p (parser)) 15853 attributes = cp_parser_attributes_opt (parser); 15854 15855 /* Check for the ptr-operator production. */ 15856 cp_parser_parse_tentatively (parser); 15857 /* Parse the ptr-operator. */ 15858 code = cp_parser_ptr_operator (parser, 15859 &class_type, 15860 &cv_quals); 15861 /* If that worked, then we have a ptr-operator. */ 15862 if (cp_parser_parse_definitely (parser)) 15863 { 15864 /* If a ptr-operator was found, then this declarator was not 15865 parenthesized. */ 15866 if (parenthesized_p) 15867 *parenthesized_p = true; 15868 /* The dependent declarator is optional if we are parsing an 15869 abstract-declarator. */ 15870 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) 15871 cp_parser_parse_tentatively (parser); 15872 15873 /* Parse the dependent declarator. */ 15874 declarator = cp_parser_declarator (parser, dcl_kind, 15875 /*ctor_dtor_or_conv_p=*/NULL, 15876 /*parenthesized_p=*/NULL, 15877 /*member_p=*/false); 15878 15879 /* If we are parsing an abstract-declarator, we must handle the 15880 case where the dependent declarator is absent. */ 15881 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED 15882 && !cp_parser_parse_definitely (parser)) 15883 declarator = NULL; 15884 15885 declarator = cp_parser_make_indirect_declarator 15886 (code, class_type, cv_quals, declarator); 15887 } 15888 /* Everything else is a direct-declarator. */ 15889 else 15890 { 15891 if (parenthesized_p) 15892 *parenthesized_p = cp_lexer_next_token_is (parser->lexer, 15893 CPP_OPEN_PAREN); 15894 declarator = cp_parser_direct_declarator (parser, dcl_kind, 15895 ctor_dtor_or_conv_p, 15896 member_p); 15897 } 15898 15899 if (attributes && declarator && declarator != cp_error_declarator) 15900 declarator->attributes = attributes; 15901 15902 return declarator; 15903 } 15904 15905 /* Parse a direct-declarator or direct-abstract-declarator. 15906 15907 direct-declarator: 15908 declarator-id 15909 direct-declarator ( parameter-declaration-clause ) 15910 cv-qualifier-seq [opt] 15911 exception-specification [opt] 15912 direct-declarator [ constant-expression [opt] ] 15913 ( declarator ) 15914 15915 direct-abstract-declarator: 15916 direct-abstract-declarator [opt] 15917 ( parameter-declaration-clause ) 15918 cv-qualifier-seq [opt] 15919 exception-specification [opt] 15920 direct-abstract-declarator [opt] [ constant-expression [opt] ] 15921 ( abstract-declarator ) 15922 15923 Returns a representation of the declarator. DCL_KIND is 15924 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a 15925 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if 15926 we are parsing a direct-declarator. It is 15927 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case 15928 of ambiguity we prefer an abstract declarator, as per 15929 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for 15930 cp_parser_declarator. */ 15931 15932 static cp_declarator * 15933 cp_parser_direct_declarator (cp_parser* parser, 15934 cp_parser_declarator_kind dcl_kind, 15935 int* ctor_dtor_or_conv_p, 15936 bool member_p) 15937 { 15938 cp_token *token; 15939 cp_declarator *declarator = NULL; 15940 tree scope = NULL_TREE; 15941 bool saved_default_arg_ok_p = parser->default_arg_ok_p; 15942 bool saved_in_declarator_p = parser->in_declarator_p; 15943 bool first = true; 15944 tree pushed_scope = NULL_TREE; 15945 15946 while (true) 15947 { 15948 /* Peek at the next token. */ 15949 token = cp_lexer_peek_token (parser->lexer); 15950 if (token->type == CPP_OPEN_PAREN) 15951 { 15952 /* This is either a parameter-declaration-clause, or a 15953 parenthesized declarator. When we know we are parsing a 15954 named declarator, it must be a parenthesized declarator 15955 if FIRST is true. For instance, `(int)' is a 15956 parameter-declaration-clause, with an omitted 15957 direct-abstract-declarator. But `((*))', is a 15958 parenthesized abstract declarator. Finally, when T is a 15959 template parameter `(T)' is a 15960 parameter-declaration-clause, and not a parenthesized 15961 named declarator. 15962 15963 We first try and parse a parameter-declaration-clause, 15964 and then try a nested declarator (if FIRST is true). 15965 15966 It is not an error for it not to be a 15967 parameter-declaration-clause, even when FIRST is 15968 false. Consider, 15969 15970 int i (int); 15971 int i (3); 15972 15973 The first is the declaration of a function while the 15974 second is the definition of a variable, including its 15975 initializer. 15976 15977 Having seen only the parenthesis, we cannot know which of 15978 these two alternatives should be selected. Even more 15979 complex are examples like: 15980 15981 int i (int (a)); 15982 int i (int (3)); 15983 15984 The former is a function-declaration; the latter is a 15985 variable initialization. 15986 15987 Thus again, we try a parameter-declaration-clause, and if 15988 that fails, we back out and return. */ 15989 15990 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) 15991 { 15992 tree params; 15993 unsigned saved_num_template_parameter_lists; 15994 bool is_declarator = false; 15995 tree t; 15996 15997 /* In a member-declarator, the only valid interpretation 15998 of a parenthesis is the start of a 15999 parameter-declaration-clause. (It is invalid to 16000 initialize a static data member with a parenthesized 16001 initializer; only the "=" form of initialization is 16002 permitted.) */ 16003 if (!member_p) 16004 cp_parser_parse_tentatively (parser); 16005 16006 /* Consume the `('. */ 16007 cp_lexer_consume_token (parser->lexer); 16008 if (first) 16009 { 16010 /* If this is going to be an abstract declarator, we're 16011 in a declarator and we can't have default args. */ 16012 parser->default_arg_ok_p = false; 16013 parser->in_declarator_p = true; 16014 } 16015 16016 /* Inside the function parameter list, surrounding 16017 template-parameter-lists do not apply. */ 16018 saved_num_template_parameter_lists 16019 = parser->num_template_parameter_lists; 16020 parser->num_template_parameter_lists = 0; 16021 16022 begin_scope (sk_function_parms, NULL_TREE); 16023 16024 /* Parse the parameter-declaration-clause. */ 16025 params = cp_parser_parameter_declaration_clause (parser); 16026 16027 parser->num_template_parameter_lists 16028 = saved_num_template_parameter_lists; 16029 16030 /* Consume the `)'. */ 16031 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 16032 16033 /* If all went well, parse the cv-qualifier-seq and the 16034 exception-specification. */ 16035 if (member_p || cp_parser_parse_definitely (parser)) 16036 { 16037 cp_cv_quals cv_quals; 16038 cp_virt_specifiers virt_specifiers; 16039 tree exception_specification; 16040 tree late_return; 16041 16042 is_declarator = true; 16043 16044 if (ctor_dtor_or_conv_p) 16045 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; 16046 first = false; 16047 16048 /* Parse the cv-qualifier-seq. */ 16049 cv_quals = cp_parser_cv_qualifier_seq_opt (parser); 16050 /* And the exception-specification. */ 16051 exception_specification 16052 = cp_parser_exception_specification_opt (parser); 16053 /* Parse the virt-specifier-seq. */ 16054 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser); 16055 16056 late_return = (cp_parser_late_return_type_opt 16057 (parser, member_p ? cv_quals : -1)); 16058 16059 /* Create the function-declarator. */ 16060 declarator = make_call_declarator (declarator, 16061 params, 16062 cv_quals, 16063 virt_specifiers, 16064 exception_specification, 16065 late_return); 16066 /* Any subsequent parameter lists are to do with 16067 return type, so are not those of the declared 16068 function. */ 16069 parser->default_arg_ok_p = false; 16070 } 16071 16072 /* Remove the function parms from scope. */ 16073 for (t = current_binding_level->names; t; t = DECL_CHAIN (t)) 16074 pop_binding (DECL_NAME (t), t); 16075 leave_scope(); 16076 16077 if (is_declarator) 16078 /* Repeat the main loop. */ 16079 continue; 16080 } 16081 16082 /* If this is the first, we can try a parenthesized 16083 declarator. */ 16084 if (first) 16085 { 16086 bool saved_in_type_id_in_expr_p; 16087 16088 parser->default_arg_ok_p = saved_default_arg_ok_p; 16089 parser->in_declarator_p = saved_in_declarator_p; 16090 16091 /* Consume the `('. */ 16092 cp_lexer_consume_token (parser->lexer); 16093 /* Parse the nested declarator. */ 16094 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; 16095 parser->in_type_id_in_expr_p = true; 16096 declarator 16097 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p, 16098 /*parenthesized_p=*/NULL, 16099 member_p); 16100 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; 16101 first = false; 16102 /* Expect a `)'. */ 16103 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 16104 declarator = cp_error_declarator; 16105 if (declarator == cp_error_declarator) 16106 break; 16107 16108 goto handle_declarator; 16109 } 16110 /* Otherwise, we must be done. */ 16111 else 16112 break; 16113 } 16114 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) 16115 && token->type == CPP_OPEN_SQUARE) 16116 { 16117 /* Parse an array-declarator. */ 16118 tree bounds; 16119 16120 if (ctor_dtor_or_conv_p) 16121 *ctor_dtor_or_conv_p = 0; 16122 16123 first = false; 16124 parser->default_arg_ok_p = false; 16125 parser->in_declarator_p = true; 16126 /* Consume the `['. */ 16127 cp_lexer_consume_token (parser->lexer); 16128 /* Peek at the next token. */ 16129 token = cp_lexer_peek_token (parser->lexer); 16130 /* If the next token is `]', then there is no 16131 constant-expression. */ 16132 if (token->type != CPP_CLOSE_SQUARE) 16133 { 16134 bool non_constant_p; 16135 16136 bounds 16137 = cp_parser_constant_expression (parser, 16138 /*allow_non_constant=*/true, 16139 &non_constant_p); 16140 if (!non_constant_p) 16141 /* OK */; 16142 else if (error_operand_p (bounds)) 16143 /* Already gave an error. */; 16144 else if (!parser->in_function_body 16145 || current_binding_level->kind == sk_function_parms) 16146 { 16147 /* Normally, the array bound must be an integral constant 16148 expression. However, as an extension, we allow VLAs 16149 in function scopes as long as they aren't part of a 16150 parameter declaration. */ 16151 cp_parser_error (parser, 16152 "array bound is not an integer constant"); 16153 bounds = error_mark_node; 16154 } 16155 else if (processing_template_decl) 16156 { 16157 /* Remember this wasn't a constant-expression. */ 16158 bounds = build_nop (TREE_TYPE (bounds), bounds); 16159 TREE_SIDE_EFFECTS (bounds) = 1; 16160 } 16161 } 16162 else 16163 bounds = NULL_TREE; 16164 /* Look for the closing `]'. */ 16165 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)) 16166 { 16167 declarator = cp_error_declarator; 16168 break; 16169 } 16170 16171 declarator = make_array_declarator (declarator, bounds); 16172 } 16173 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) 16174 { 16175 { 16176 tree qualifying_scope; 16177 tree unqualified_name; 16178 special_function_kind sfk; 16179 bool abstract_ok; 16180 bool pack_expansion_p = false; 16181 cp_token *declarator_id_start_token; 16182 16183 /* Parse a declarator-id */ 16184 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER); 16185 if (abstract_ok) 16186 { 16187 cp_parser_parse_tentatively (parser); 16188 16189 /* If we see an ellipsis, we should be looking at a 16190 parameter pack. */ 16191 if (token->type == CPP_ELLIPSIS) 16192 { 16193 /* Consume the `...' */ 16194 cp_lexer_consume_token (parser->lexer); 16195 16196 pack_expansion_p = true; 16197 } 16198 } 16199 16200 declarator_id_start_token = cp_lexer_peek_token (parser->lexer); 16201 unqualified_name 16202 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok); 16203 qualifying_scope = parser->scope; 16204 if (abstract_ok) 16205 { 16206 bool okay = false; 16207 16208 if (!unqualified_name && pack_expansion_p) 16209 { 16210 /* Check whether an error occurred. */ 16211 okay = !cp_parser_error_occurred (parser); 16212 16213 /* We already consumed the ellipsis to mark a 16214 parameter pack, but we have no way to report it, 16215 so abort the tentative parse. We will be exiting 16216 immediately anyway. */ 16217 cp_parser_abort_tentative_parse (parser); 16218 } 16219 else 16220 okay = cp_parser_parse_definitely (parser); 16221 16222 if (!okay) 16223 unqualified_name = error_mark_node; 16224 else if (unqualified_name 16225 && (qualifying_scope 16226 || (TREE_CODE (unqualified_name) 16227 != IDENTIFIER_NODE))) 16228 { 16229 cp_parser_error (parser, "expected unqualified-id"); 16230 unqualified_name = error_mark_node; 16231 } 16232 } 16233 16234 if (!unqualified_name) 16235 return NULL; 16236 if (unqualified_name == error_mark_node) 16237 { 16238 declarator = cp_error_declarator; 16239 pack_expansion_p = false; 16240 declarator->parameter_pack_p = false; 16241 break; 16242 } 16243 16244 if (qualifying_scope && at_namespace_scope_p () 16245 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE) 16246 { 16247 /* In the declaration of a member of a template class 16248 outside of the class itself, the SCOPE will sometimes 16249 be a TYPENAME_TYPE. For example, given: 16250 16251 template <typename T> 16252 int S<T>::R::i = 3; 16253 16254 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In 16255 this context, we must resolve S<T>::R to an ordinary 16256 type, rather than a typename type. 16257 16258 The reason we normally avoid resolving TYPENAME_TYPEs 16259 is that a specialization of `S' might render 16260 `S<T>::R' not a type. However, if `S' is 16261 specialized, then this `i' will not be used, so there 16262 is no harm in resolving the types here. */ 16263 tree type; 16264 16265 /* Resolve the TYPENAME_TYPE. */ 16266 type = resolve_typename_type (qualifying_scope, 16267 /*only_current_p=*/false); 16268 /* If that failed, the declarator is invalid. */ 16269 if (TREE_CODE (type) == TYPENAME_TYPE) 16270 { 16271 if (typedef_variant_p (type)) 16272 error_at (declarator_id_start_token->location, 16273 "cannot define member of dependent typedef " 16274 "%qT", type); 16275 else 16276 error_at (declarator_id_start_token->location, 16277 "%<%T::%E%> is not a type", 16278 TYPE_CONTEXT (qualifying_scope), 16279 TYPE_IDENTIFIER (qualifying_scope)); 16280 } 16281 qualifying_scope = type; 16282 } 16283 16284 sfk = sfk_none; 16285 16286 if (unqualified_name) 16287 { 16288 tree class_type; 16289 16290 if (qualifying_scope 16291 && CLASS_TYPE_P (qualifying_scope)) 16292 class_type = qualifying_scope; 16293 else 16294 class_type = current_class_type; 16295 16296 if (TREE_CODE (unqualified_name) == TYPE_DECL) 16297 { 16298 tree name_type = TREE_TYPE (unqualified_name); 16299 if (class_type && same_type_p (name_type, class_type)) 16300 { 16301 if (qualifying_scope 16302 && CLASSTYPE_USE_TEMPLATE (name_type)) 16303 { 16304 error_at (declarator_id_start_token->location, 16305 "invalid use of constructor as a template"); 16306 inform (declarator_id_start_token->location, 16307 "use %<%T::%D%> instead of %<%T::%D%> to " 16308 "name the constructor in a qualified name", 16309 class_type, 16310 DECL_NAME (TYPE_TI_TEMPLATE (class_type)), 16311 class_type, name_type); 16312 declarator = cp_error_declarator; 16313 break; 16314 } 16315 else 16316 unqualified_name = constructor_name (class_type); 16317 } 16318 else 16319 { 16320 /* We do not attempt to print the declarator 16321 here because we do not have enough 16322 information about its original syntactic 16323 form. */ 16324 cp_parser_error (parser, "invalid declarator"); 16325 declarator = cp_error_declarator; 16326 break; 16327 } 16328 } 16329 16330 if (class_type) 16331 { 16332 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR) 16333 sfk = sfk_destructor; 16334 else if (IDENTIFIER_TYPENAME_P (unqualified_name)) 16335 sfk = sfk_conversion; 16336 else if (/* There's no way to declare a constructor 16337 for an anonymous type, even if the type 16338 got a name for linkage purposes. */ 16339 !TYPE_WAS_ANONYMOUS (class_type) 16340 && constructor_name_p (unqualified_name, 16341 class_type)) 16342 { 16343 unqualified_name = constructor_name (class_type); 16344 sfk = sfk_constructor; 16345 } 16346 else if (is_overloaded_fn (unqualified_name) 16347 && DECL_CONSTRUCTOR_P (get_first_fn 16348 (unqualified_name))) 16349 sfk = sfk_constructor; 16350 16351 if (ctor_dtor_or_conv_p && sfk != sfk_none) 16352 *ctor_dtor_or_conv_p = -1; 16353 } 16354 } 16355 declarator = make_id_declarator (qualifying_scope, 16356 unqualified_name, 16357 sfk); 16358 declarator->id_loc = token->location; 16359 declarator->parameter_pack_p = pack_expansion_p; 16360 16361 if (pack_expansion_p) 16362 maybe_warn_variadic_templates (); 16363 } 16364 16365 handle_declarator:; 16366 scope = get_scope_of_declarator (declarator); 16367 if (scope) 16368 /* Any names that appear after the declarator-id for a 16369 member are looked up in the containing scope. */ 16370 pushed_scope = push_scope (scope); 16371 parser->in_declarator_p = true; 16372 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) 16373 || (declarator && declarator->kind == cdk_id)) 16374 /* Default args are only allowed on function 16375 declarations. */ 16376 parser->default_arg_ok_p = saved_default_arg_ok_p; 16377 else 16378 parser->default_arg_ok_p = false; 16379 16380 first = false; 16381 } 16382 /* We're done. */ 16383 else 16384 break; 16385 } 16386 16387 /* For an abstract declarator, we might wind up with nothing at this 16388 point. That's an error; the declarator is not optional. */ 16389 if (!declarator) 16390 cp_parser_error (parser, "expected declarator"); 16391 16392 /* If we entered a scope, we must exit it now. */ 16393 if (pushed_scope) 16394 pop_scope (pushed_scope); 16395 16396 parser->default_arg_ok_p = saved_default_arg_ok_p; 16397 parser->in_declarator_p = saved_in_declarator_p; 16398 16399 return declarator; 16400 } 16401 16402 /* Parse a ptr-operator. 16403 16404 ptr-operator: 16405 * cv-qualifier-seq [opt] 16406 & 16407 :: [opt] nested-name-specifier * cv-qualifier-seq [opt] 16408 16409 GNU Extension: 16410 16411 ptr-operator: 16412 & cv-qualifier-seq [opt] 16413 16414 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used. 16415 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for 16416 an rvalue reference. In the case of a pointer-to-member, *TYPE is 16417 filled in with the TYPE containing the member. *CV_QUALS is 16418 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there 16419 are no cv-qualifiers. Returns ERROR_MARK if an error occurred. 16420 Note that the tree codes returned by this function have nothing 16421 to do with the types of trees that will be eventually be created 16422 to represent the pointer or reference type being parsed. They are 16423 just constants with suggestive names. */ 16424 static enum tree_code 16425 cp_parser_ptr_operator (cp_parser* parser, 16426 tree* type, 16427 cp_cv_quals *cv_quals) 16428 { 16429 enum tree_code code = ERROR_MARK; 16430 cp_token *token; 16431 16432 /* Assume that it's not a pointer-to-member. */ 16433 *type = NULL_TREE; 16434 /* And that there are no cv-qualifiers. */ 16435 *cv_quals = TYPE_UNQUALIFIED; 16436 16437 /* Peek at the next token. */ 16438 token = cp_lexer_peek_token (parser->lexer); 16439 16440 /* If it's a `*', `&' or `&&' we have a pointer or reference. */ 16441 if (token->type == CPP_MULT) 16442 code = INDIRECT_REF; 16443 else if (token->type == CPP_AND) 16444 code = ADDR_EXPR; 16445 else if ((cxx_dialect != cxx98) && 16446 token->type == CPP_AND_AND) /* C++0x only */ 16447 code = NON_LVALUE_EXPR; 16448 16449 if (code != ERROR_MARK) 16450 { 16451 /* Consume the `*', `&' or `&&'. */ 16452 cp_lexer_consume_token (parser->lexer); 16453 16454 /* A `*' can be followed by a cv-qualifier-seq, and so can a 16455 `&', if we are allowing GNU extensions. (The only qualifier 16456 that can legally appear after `&' is `restrict', but that is 16457 enforced during semantic analysis. */ 16458 if (code == INDIRECT_REF 16459 || cp_parser_allow_gnu_extensions_p (parser)) 16460 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); 16461 } 16462 else 16463 { 16464 /* Try the pointer-to-member case. */ 16465 cp_parser_parse_tentatively (parser); 16466 /* Look for the optional `::' operator. */ 16467 cp_parser_global_scope_opt (parser, 16468 /*current_scope_valid_p=*/false); 16469 /* Look for the nested-name specifier. */ 16470 token = cp_lexer_peek_token (parser->lexer); 16471 cp_parser_nested_name_specifier (parser, 16472 /*typename_keyword_p=*/false, 16473 /*check_dependency_p=*/true, 16474 /*type_p=*/false, 16475 /*is_declaration=*/false); 16476 /* If we found it, and the next token is a `*', then we are 16477 indeed looking at a pointer-to-member operator. */ 16478 if (!cp_parser_error_occurred (parser) 16479 && cp_parser_require (parser, CPP_MULT, RT_MULT)) 16480 { 16481 /* Indicate that the `*' operator was used. */ 16482 code = INDIRECT_REF; 16483 16484 if (TREE_CODE (parser->scope) == NAMESPACE_DECL) 16485 error_at (token->location, "%qD is a namespace", parser->scope); 16486 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE) 16487 error_at (token->location, "cannot form pointer to member of " 16488 "non-class %q#T", parser->scope); 16489 else 16490 { 16491 /* The type of which the member is a member is given by the 16492 current SCOPE. */ 16493 *type = parser->scope; 16494 /* The next name will not be qualified. */ 16495 parser->scope = NULL_TREE; 16496 parser->qualifying_scope = NULL_TREE; 16497 parser->object_scope = NULL_TREE; 16498 /* Look for the optional cv-qualifier-seq. */ 16499 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); 16500 } 16501 } 16502 /* If that didn't work we don't have a ptr-operator. */ 16503 if (!cp_parser_parse_definitely (parser)) 16504 cp_parser_error (parser, "expected ptr-operator"); 16505 } 16506 16507 return code; 16508 } 16509 16510 /* Parse an (optional) cv-qualifier-seq. 16511 16512 cv-qualifier-seq: 16513 cv-qualifier cv-qualifier-seq [opt] 16514 16515 cv-qualifier: 16516 const 16517 volatile 16518 16519 GNU Extension: 16520 16521 cv-qualifier: 16522 __restrict__ 16523 16524 Returns a bitmask representing the cv-qualifiers. */ 16525 16526 static cp_cv_quals 16527 cp_parser_cv_qualifier_seq_opt (cp_parser* parser) 16528 { 16529 cp_cv_quals cv_quals = TYPE_UNQUALIFIED; 16530 16531 while (true) 16532 { 16533 cp_token *token; 16534 cp_cv_quals cv_qualifier; 16535 16536 /* Peek at the next token. */ 16537 token = cp_lexer_peek_token (parser->lexer); 16538 /* See if it's a cv-qualifier. */ 16539 switch (token->keyword) 16540 { 16541 case RID_CONST: 16542 cv_qualifier = TYPE_QUAL_CONST; 16543 break; 16544 16545 case RID_VOLATILE: 16546 cv_qualifier = TYPE_QUAL_VOLATILE; 16547 break; 16548 16549 case RID_RESTRICT: 16550 cv_qualifier = TYPE_QUAL_RESTRICT; 16551 break; 16552 16553 default: 16554 cv_qualifier = TYPE_UNQUALIFIED; 16555 break; 16556 } 16557 16558 if (!cv_qualifier) 16559 break; 16560 16561 if (cv_quals & cv_qualifier) 16562 { 16563 error_at (token->location, "duplicate cv-qualifier"); 16564 cp_lexer_purge_token (parser->lexer); 16565 } 16566 else 16567 { 16568 cp_lexer_consume_token (parser->lexer); 16569 cv_quals |= cv_qualifier; 16570 } 16571 } 16572 16573 return cv_quals; 16574 } 16575 16576 /* Parse an (optional) virt-specifier-seq. 16577 16578 virt-specifier-seq: 16579 virt-specifier virt-specifier-seq [opt] 16580 16581 virt-specifier: 16582 override 16583 final 16584 16585 Returns a bitmask representing the virt-specifiers. */ 16586 16587 static cp_virt_specifiers 16588 cp_parser_virt_specifier_seq_opt (cp_parser* parser) 16589 { 16590 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED; 16591 16592 while (true) 16593 { 16594 cp_token *token; 16595 cp_virt_specifiers virt_specifier; 16596 16597 /* Peek at the next token. */ 16598 token = cp_lexer_peek_token (parser->lexer); 16599 /* See if it's a virt-specifier-qualifier. */ 16600 if (token->type != CPP_NAME) 16601 break; 16602 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override")) 16603 { 16604 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS); 16605 virt_specifier = VIRT_SPEC_OVERRIDE; 16606 } 16607 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final")) 16608 { 16609 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS); 16610 virt_specifier = VIRT_SPEC_FINAL; 16611 } 16612 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final")) 16613 { 16614 virt_specifier = VIRT_SPEC_FINAL; 16615 } 16616 else 16617 break; 16618 16619 if (virt_specifiers & virt_specifier) 16620 { 16621 error_at (token->location, "duplicate virt-specifier"); 16622 cp_lexer_purge_token (parser->lexer); 16623 } 16624 else 16625 { 16626 cp_lexer_consume_token (parser->lexer); 16627 virt_specifiers |= virt_specifier; 16628 } 16629 } 16630 return virt_specifiers; 16631 } 16632 16633 /* Used by handling of trailing-return-types and NSDMI, in which 'this' 16634 is in scope even though it isn't real. */ 16635 16636 static void 16637 inject_this_parameter (tree ctype, cp_cv_quals quals) 16638 { 16639 tree this_parm; 16640 16641 if (current_class_ptr) 16642 { 16643 /* We don't clear this between NSDMIs. Is it already what we want? */ 16644 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr)); 16645 if (same_type_ignoring_top_level_qualifiers_p (ctype, type) 16646 && cp_type_quals (type) == quals) 16647 return; 16648 } 16649 16650 this_parm = build_this_parm (ctype, quals); 16651 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */ 16652 current_class_ptr = NULL_TREE; 16653 current_class_ref 16654 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error); 16655 current_class_ptr = this_parm; 16656 } 16657 16658 /* Parse a late-specified return type, if any. This is not a separate 16659 non-terminal, but part of a function declarator, which looks like 16660 16661 -> trailing-type-specifier-seq abstract-declarator(opt) 16662 16663 Returns the type indicated by the type-id. 16664 16665 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member 16666 function. */ 16667 16668 static tree 16669 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals) 16670 { 16671 cp_token *token; 16672 tree type, save_ccp, save_ccr; 16673 16674 /* Peek at the next token. */ 16675 token = cp_lexer_peek_token (parser->lexer); 16676 /* A late-specified return type is indicated by an initial '->'. */ 16677 if (token->type != CPP_DEREF) 16678 return NULL_TREE; 16679 16680 /* Consume the ->. */ 16681 cp_lexer_consume_token (parser->lexer); 16682 16683 save_ccp = current_class_ptr; 16684 save_ccr = current_class_ref; 16685 if (quals >= 0) 16686 { 16687 /* DR 1207: 'this' is in scope in the trailing return type. */ 16688 inject_this_parameter (current_class_type, quals); 16689 } 16690 16691 type = cp_parser_trailing_type_id (parser); 16692 16693 if (quals >= 0) 16694 { 16695 current_class_ptr = save_ccp; 16696 current_class_ref = save_ccr; 16697 } 16698 16699 return type; 16700 } 16701 16702 /* Parse a declarator-id. 16703 16704 declarator-id: 16705 id-expression 16706 :: [opt] nested-name-specifier [opt] type-name 16707 16708 In the `id-expression' case, the value returned is as for 16709 cp_parser_id_expression if the id-expression was an unqualified-id. 16710 If the id-expression was a qualified-id, then a SCOPE_REF is 16711 returned. The first operand is the scope (either a NAMESPACE_DECL 16712 or TREE_TYPE), but the second is still just a representation of an 16713 unqualified-id. */ 16714 16715 static tree 16716 cp_parser_declarator_id (cp_parser* parser, bool optional_p) 16717 { 16718 tree id; 16719 /* The expression must be an id-expression. Assume that qualified 16720 names are the names of types so that: 16721 16722 template <class T> 16723 int S<T>::R::i = 3; 16724 16725 will work; we must treat `S<T>::R' as the name of a type. 16726 Similarly, assume that qualified names are templates, where 16727 required, so that: 16728 16729 template <class T> 16730 int S<T>::R<T>::i = 3; 16731 16732 will work, too. */ 16733 id = cp_parser_id_expression (parser, 16734 /*template_keyword_p=*/false, 16735 /*check_dependency_p=*/false, 16736 /*template_p=*/NULL, 16737 /*declarator_p=*/true, 16738 optional_p); 16739 if (id && BASELINK_P (id)) 16740 id = BASELINK_FUNCTIONS (id); 16741 return id; 16742 } 16743 16744 /* Parse a type-id. 16745 16746 type-id: 16747 type-specifier-seq abstract-declarator [opt] 16748 16749 Returns the TYPE specified. */ 16750 16751 static tree 16752 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg, 16753 bool is_trailing_return) 16754 { 16755 cp_decl_specifier_seq type_specifier_seq; 16756 cp_declarator *abstract_declarator; 16757 16758 /* Parse the type-specifier-seq. */ 16759 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false, 16760 is_trailing_return, 16761 &type_specifier_seq); 16762 if (type_specifier_seq.type == error_mark_node) 16763 return error_mark_node; 16764 16765 /* There might or might not be an abstract declarator. */ 16766 cp_parser_parse_tentatively (parser); 16767 /* Look for the declarator. */ 16768 abstract_declarator 16769 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL, 16770 /*parenthesized_p=*/NULL, 16771 /*member_p=*/false); 16772 /* Check to see if there really was a declarator. */ 16773 if (!cp_parser_parse_definitely (parser)) 16774 abstract_declarator = NULL; 16775 16776 if (type_specifier_seq.type 16777 && type_uses_auto (type_specifier_seq.type)) 16778 { 16779 /* A type-id with type 'auto' is only ok if the abstract declarator 16780 is a function declarator with a late-specified return type. */ 16781 if (abstract_declarator 16782 && abstract_declarator->kind == cdk_function 16783 && abstract_declarator->u.function.late_return_type) 16784 /* OK */; 16785 else 16786 { 16787 error ("invalid use of %<auto%>"); 16788 return error_mark_node; 16789 } 16790 } 16791 16792 return groktypename (&type_specifier_seq, abstract_declarator, 16793 is_template_arg); 16794 } 16795 16796 static tree cp_parser_type_id (cp_parser *parser) 16797 { 16798 return cp_parser_type_id_1 (parser, false, false); 16799 } 16800 16801 static tree cp_parser_template_type_arg (cp_parser *parser) 16802 { 16803 tree r; 16804 const char *saved_message = parser->type_definition_forbidden_message; 16805 parser->type_definition_forbidden_message 16806 = G_("types may not be defined in template arguments"); 16807 r = cp_parser_type_id_1 (parser, true, false); 16808 parser->type_definition_forbidden_message = saved_message; 16809 return r; 16810 } 16811 16812 static tree cp_parser_trailing_type_id (cp_parser *parser) 16813 { 16814 return cp_parser_type_id_1 (parser, false, true); 16815 } 16816 16817 /* Parse a type-specifier-seq. 16818 16819 type-specifier-seq: 16820 type-specifier type-specifier-seq [opt] 16821 16822 GNU extension: 16823 16824 type-specifier-seq: 16825 attributes type-specifier-seq [opt] 16826 16827 If IS_DECLARATION is true, we are at the start of a "condition" or 16828 exception-declaration, so we might be followed by a declarator-id. 16829 16830 If IS_TRAILING_RETURN is true, we are in a trailing-return-type, 16831 i.e. we've just seen "->". 16832 16833 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */ 16834 16835 static void 16836 cp_parser_type_specifier_seq (cp_parser* parser, 16837 bool is_declaration, 16838 bool is_trailing_return, 16839 cp_decl_specifier_seq *type_specifier_seq) 16840 { 16841 bool seen_type_specifier = false; 16842 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL; 16843 cp_token *start_token = NULL; 16844 16845 /* Clear the TYPE_SPECIFIER_SEQ. */ 16846 clear_decl_specs (type_specifier_seq); 16847 16848 /* In the context of a trailing return type, enum E { } is an 16849 elaborated-type-specifier followed by a function-body, not an 16850 enum-specifier. */ 16851 if (is_trailing_return) 16852 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS; 16853 16854 /* Parse the type-specifiers and attributes. */ 16855 while (true) 16856 { 16857 tree type_specifier; 16858 bool is_cv_qualifier; 16859 16860 /* Check for attributes first. */ 16861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) 16862 { 16863 type_specifier_seq->attributes = 16864 chainon (type_specifier_seq->attributes, 16865 cp_parser_attributes_opt (parser)); 16866 continue; 16867 } 16868 16869 /* record the token of the beginning of the type specifier seq, 16870 for error reporting purposes*/ 16871 if (!start_token) 16872 start_token = cp_lexer_peek_token (parser->lexer); 16873 16874 /* Look for the type-specifier. */ 16875 type_specifier = cp_parser_type_specifier (parser, 16876 flags, 16877 type_specifier_seq, 16878 /*is_declaration=*/false, 16879 NULL, 16880 &is_cv_qualifier); 16881 if (!type_specifier) 16882 { 16883 /* If the first type-specifier could not be found, this is not a 16884 type-specifier-seq at all. */ 16885 if (!seen_type_specifier) 16886 { 16887 cp_parser_error (parser, "expected type-specifier"); 16888 type_specifier_seq->type = error_mark_node; 16889 return; 16890 } 16891 /* If subsequent type-specifiers could not be found, the 16892 type-specifier-seq is complete. */ 16893 break; 16894 } 16895 16896 seen_type_specifier = true; 16897 /* The standard says that a condition can be: 16898 16899 type-specifier-seq declarator = assignment-expression 16900 16901 However, given: 16902 16903 struct S {}; 16904 if (int S = ...) 16905 16906 we should treat the "S" as a declarator, not as a 16907 type-specifier. The standard doesn't say that explicitly for 16908 type-specifier-seq, but it does say that for 16909 decl-specifier-seq in an ordinary declaration. Perhaps it 16910 would be clearer just to allow a decl-specifier-seq here, and 16911 then add a semantic restriction that if any decl-specifiers 16912 that are not type-specifiers appear, the program is invalid. */ 16913 if (is_declaration && !is_cv_qualifier) 16914 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; 16915 } 16916 16917 cp_parser_check_decl_spec (type_specifier_seq, start_token->location); 16918 } 16919 16920 /* Parse a parameter-declaration-clause. 16921 16922 parameter-declaration-clause: 16923 parameter-declaration-list [opt] ... [opt] 16924 parameter-declaration-list , ... 16925 16926 Returns a representation for the parameter declarations. A return 16927 value of NULL indicates a parameter-declaration-clause consisting 16928 only of an ellipsis. */ 16929 16930 static tree 16931 cp_parser_parameter_declaration_clause (cp_parser* parser) 16932 { 16933 tree parameters; 16934 cp_token *token; 16935 bool ellipsis_p; 16936 bool is_error; 16937 16938 /* Peek at the next token. */ 16939 token = cp_lexer_peek_token (parser->lexer); 16940 /* Check for trivial parameter-declaration-clauses. */ 16941 if (token->type == CPP_ELLIPSIS) 16942 { 16943 /* Consume the `...' token. */ 16944 cp_lexer_consume_token (parser->lexer); 16945 return NULL_TREE; 16946 } 16947 else if (token->type == CPP_CLOSE_PAREN) 16948 /* There are no parameters. */ 16949 { 16950 #ifndef NO_IMPLICIT_EXTERN_C 16951 if (in_system_header && current_class_type == NULL 16952 && current_lang_name == lang_name_c) 16953 return NULL_TREE; 16954 else 16955 #endif 16956 return void_list_node; 16957 } 16958 /* Check for `(void)', too, which is a special case. */ 16959 else if (token->keyword == RID_VOID 16960 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 16961 == CPP_CLOSE_PAREN)) 16962 { 16963 /* Consume the `void' token. */ 16964 cp_lexer_consume_token (parser->lexer); 16965 /* There are no parameters. */ 16966 return void_list_node; 16967 } 16968 16969 /* Parse the parameter-declaration-list. */ 16970 parameters = cp_parser_parameter_declaration_list (parser, &is_error); 16971 /* If a parse error occurred while parsing the 16972 parameter-declaration-list, then the entire 16973 parameter-declaration-clause is erroneous. */ 16974 if (is_error) 16975 return NULL; 16976 16977 /* Peek at the next token. */ 16978 token = cp_lexer_peek_token (parser->lexer); 16979 /* If it's a `,', the clause should terminate with an ellipsis. */ 16980 if (token->type == CPP_COMMA) 16981 { 16982 /* Consume the `,'. */ 16983 cp_lexer_consume_token (parser->lexer); 16984 /* Expect an ellipsis. */ 16985 ellipsis_p 16986 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL); 16987 } 16988 /* It might also be `...' if the optional trailing `,' was 16989 omitted. */ 16990 else if (token->type == CPP_ELLIPSIS) 16991 { 16992 /* Consume the `...' token. */ 16993 cp_lexer_consume_token (parser->lexer); 16994 /* And remember that we saw it. */ 16995 ellipsis_p = true; 16996 } 16997 else 16998 ellipsis_p = false; 16999 17000 /* Finish the parameter list. */ 17001 if (!ellipsis_p) 17002 parameters = chainon (parameters, void_list_node); 17003 17004 return parameters; 17005 } 17006 17007 /* Parse a parameter-declaration-list. 17008 17009 parameter-declaration-list: 17010 parameter-declaration 17011 parameter-declaration-list , parameter-declaration 17012 17013 Returns a representation of the parameter-declaration-list, as for 17014 cp_parser_parameter_declaration_clause. However, the 17015 `void_list_node' is never appended to the list. Upon return, 17016 *IS_ERROR will be true iff an error occurred. */ 17017 17018 static tree 17019 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error) 17020 { 17021 tree parameters = NULL_TREE; 17022 tree *tail = ¶meters; 17023 bool saved_in_unbraced_linkage_specification_p; 17024 int index = 0; 17025 17026 /* Assume all will go well. */ 17027 *is_error = false; 17028 /* The special considerations that apply to a function within an 17029 unbraced linkage specifications do not apply to the parameters 17030 to the function. */ 17031 saved_in_unbraced_linkage_specification_p 17032 = parser->in_unbraced_linkage_specification_p; 17033 parser->in_unbraced_linkage_specification_p = false; 17034 17035 /* Look for more parameters. */ 17036 while (true) 17037 { 17038 cp_parameter_declarator *parameter; 17039 tree decl = error_mark_node; 17040 bool parenthesized_p = false; 17041 /* Parse the parameter. */ 17042 parameter 17043 = cp_parser_parameter_declaration (parser, 17044 /*template_parm_p=*/false, 17045 &parenthesized_p); 17046 17047 /* We don't know yet if the enclosing context is deprecated, so wait 17048 and warn in grokparms if appropriate. */ 17049 deprecated_state = DEPRECATED_SUPPRESS; 17050 17051 if (parameter) 17052 decl = grokdeclarator (parameter->declarator, 17053 ¶meter->decl_specifiers, 17054 PARM, 17055 parameter->default_argument != NULL_TREE, 17056 ¶meter->decl_specifiers.attributes); 17057 17058 deprecated_state = DEPRECATED_NORMAL; 17059 17060 /* If a parse error occurred parsing the parameter declaration, 17061 then the entire parameter-declaration-list is erroneous. */ 17062 if (decl == error_mark_node) 17063 { 17064 *is_error = true; 17065 parameters = error_mark_node; 17066 break; 17067 } 17068 17069 if (parameter->decl_specifiers.attributes) 17070 cplus_decl_attributes (&decl, 17071 parameter->decl_specifiers.attributes, 17072 0); 17073 if (DECL_NAME (decl)) 17074 decl = pushdecl (decl); 17075 17076 if (decl != error_mark_node) 17077 { 17078 retrofit_lang_decl (decl); 17079 DECL_PARM_INDEX (decl) = ++index; 17080 DECL_PARM_LEVEL (decl) = function_parm_depth (); 17081 } 17082 17083 /* Add the new parameter to the list. */ 17084 *tail = build_tree_list (parameter->default_argument, decl); 17085 tail = &TREE_CHAIN (*tail); 17086 17087 /* Peek at the next token. */ 17088 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) 17089 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS) 17090 /* These are for Objective-C++ */ 17091 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 17092 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 17093 /* The parameter-declaration-list is complete. */ 17094 break; 17095 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 17096 { 17097 cp_token *token; 17098 17099 /* Peek at the next token. */ 17100 token = cp_lexer_peek_nth_token (parser->lexer, 2); 17101 /* If it's an ellipsis, then the list is complete. */ 17102 if (token->type == CPP_ELLIPSIS) 17103 break; 17104 /* Otherwise, there must be more parameters. Consume the 17105 `,'. */ 17106 cp_lexer_consume_token (parser->lexer); 17107 /* When parsing something like: 17108 17109 int i(float f, double d) 17110 17111 we can tell after seeing the declaration for "f" that we 17112 are not looking at an initialization of a variable "i", 17113 but rather at the declaration of a function "i". 17114 17115 Due to the fact that the parsing of template arguments 17116 (as specified to a template-id) requires backtracking we 17117 cannot use this technique when inside a template argument 17118 list. */ 17119 if (!parser->in_template_argument_list_p 17120 && !parser->in_type_id_in_expr_p 17121 && cp_parser_uncommitted_to_tentative_parse_p (parser) 17122 /* However, a parameter-declaration of the form 17123 "foat(f)" (which is a valid declaration of a 17124 parameter "f") can also be interpreted as an 17125 expression (the conversion of "f" to "float"). */ 17126 && !parenthesized_p) 17127 cp_parser_commit_to_tentative_parse (parser); 17128 } 17129 else 17130 { 17131 cp_parser_error (parser, "expected %<,%> or %<...%>"); 17132 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) 17133 cp_parser_skip_to_closing_parenthesis (parser, 17134 /*recovering=*/true, 17135 /*or_comma=*/false, 17136 /*consume_paren=*/false); 17137 break; 17138 } 17139 } 17140 17141 parser->in_unbraced_linkage_specification_p 17142 = saved_in_unbraced_linkage_specification_p; 17143 17144 return parameters; 17145 } 17146 17147 /* Parse a parameter declaration. 17148 17149 parameter-declaration: 17150 decl-specifier-seq ... [opt] declarator 17151 decl-specifier-seq declarator = assignment-expression 17152 decl-specifier-seq ... [opt] abstract-declarator [opt] 17153 decl-specifier-seq abstract-declarator [opt] = assignment-expression 17154 17155 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration 17156 declares a template parameter. (In that case, a non-nested `>' 17157 token encountered during the parsing of the assignment-expression 17158 is not interpreted as a greater-than operator.) 17159 17160 Returns a representation of the parameter, or NULL if an error 17161 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to 17162 true iff the declarator is of the form "(p)". */ 17163 17164 static cp_parameter_declarator * 17165 cp_parser_parameter_declaration (cp_parser *parser, 17166 bool template_parm_p, 17167 bool *parenthesized_p) 17168 { 17169 int declares_class_or_enum; 17170 cp_decl_specifier_seq decl_specifiers; 17171 cp_declarator *declarator; 17172 tree default_argument; 17173 cp_token *token = NULL, *declarator_token_start = NULL; 17174 const char *saved_message; 17175 17176 /* In a template parameter, `>' is not an operator. 17177 17178 [temp.param] 17179 17180 When parsing a default template-argument for a non-type 17181 template-parameter, the first non-nested `>' is taken as the end 17182 of the template parameter-list rather than a greater-than 17183 operator. */ 17184 17185 /* Type definitions may not appear in parameter types. */ 17186 saved_message = parser->type_definition_forbidden_message; 17187 parser->type_definition_forbidden_message 17188 = G_("types may not be defined in parameter types"); 17189 17190 /* Parse the declaration-specifiers. */ 17191 cp_parser_decl_specifier_seq (parser, 17192 CP_PARSER_FLAGS_NONE, 17193 &decl_specifiers, 17194 &declares_class_or_enum); 17195 17196 /* Complain about missing 'typename' or other invalid type names. */ 17197 if (!decl_specifiers.any_type_specifiers_p) 17198 cp_parser_parse_and_diagnose_invalid_type_name (parser); 17199 17200 /* If an error occurred, there's no reason to attempt to parse the 17201 rest of the declaration. */ 17202 if (cp_parser_error_occurred (parser)) 17203 { 17204 parser->type_definition_forbidden_message = saved_message; 17205 return NULL; 17206 } 17207 17208 /* Peek at the next token. */ 17209 token = cp_lexer_peek_token (parser->lexer); 17210 17211 /* If the next token is a `)', `,', `=', `>', or `...', then there 17212 is no declarator. However, when variadic templates are enabled, 17213 there may be a declarator following `...'. */ 17214 if (token->type == CPP_CLOSE_PAREN 17215 || token->type == CPP_COMMA 17216 || token->type == CPP_EQ 17217 || token->type == CPP_GREATER) 17218 { 17219 declarator = NULL; 17220 if (parenthesized_p) 17221 *parenthesized_p = false; 17222 } 17223 /* Otherwise, there should be a declarator. */ 17224 else 17225 { 17226 bool saved_default_arg_ok_p = parser->default_arg_ok_p; 17227 parser->default_arg_ok_p = false; 17228 17229 /* After seeing a decl-specifier-seq, if the next token is not a 17230 "(", there is no possibility that the code is a valid 17231 expression. Therefore, if parsing tentatively, we commit at 17232 this point. */ 17233 if (!parser->in_template_argument_list_p 17234 /* In an expression context, having seen: 17235 17236 (int((char ... 17237 17238 we cannot be sure whether we are looking at a 17239 function-type (taking a "char" as a parameter) or a cast 17240 of some object of type "char" to "int". */ 17241 && !parser->in_type_id_in_expr_p 17242 && cp_parser_uncommitted_to_tentative_parse_p (parser) 17243 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) 17244 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) 17245 cp_parser_commit_to_tentative_parse (parser); 17246 /* Parse the declarator. */ 17247 declarator_token_start = token; 17248 declarator = cp_parser_declarator (parser, 17249 CP_PARSER_DECLARATOR_EITHER, 17250 /*ctor_dtor_or_conv_p=*/NULL, 17251 parenthesized_p, 17252 /*member_p=*/false); 17253 parser->default_arg_ok_p = saved_default_arg_ok_p; 17254 /* After the declarator, allow more attributes. */ 17255 decl_specifiers.attributes 17256 = chainon (decl_specifiers.attributes, 17257 cp_parser_attributes_opt (parser)); 17258 } 17259 17260 /* If the next token is an ellipsis, and we have not seen a 17261 declarator name, and the type of the declarator contains parameter 17262 packs but it is not a TYPE_PACK_EXPANSION, then we actually have 17263 a parameter pack expansion expression. Otherwise, leave the 17264 ellipsis for a C-style variadic function. */ 17265 token = cp_lexer_peek_token (parser->lexer); 17266 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 17267 { 17268 tree type = decl_specifiers.type; 17269 17270 if (type && DECL_P (type)) 17271 type = TREE_TYPE (type); 17272 17273 if (type 17274 && TREE_CODE (type) != TYPE_PACK_EXPANSION 17275 && declarator_can_be_parameter_pack (declarator) 17276 && (!declarator || !declarator->parameter_pack_p) 17277 && uses_parameter_packs (type)) 17278 { 17279 /* Consume the `...'. */ 17280 cp_lexer_consume_token (parser->lexer); 17281 maybe_warn_variadic_templates (); 17282 17283 /* Build a pack expansion type */ 17284 if (declarator) 17285 declarator->parameter_pack_p = true; 17286 else 17287 decl_specifiers.type = make_pack_expansion (type); 17288 } 17289 } 17290 17291 /* The restriction on defining new types applies only to the type 17292 of the parameter, not to the default argument. */ 17293 parser->type_definition_forbidden_message = saved_message; 17294 17295 /* If the next token is `=', then process a default argument. */ 17296 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 17297 { 17298 token = cp_lexer_peek_token (parser->lexer); 17299 /* If we are defining a class, then the tokens that make up the 17300 default argument must be saved and processed later. */ 17301 if (!template_parm_p && at_class_scope_p () 17302 && TYPE_BEING_DEFINED (current_class_type) 17303 && !LAMBDA_TYPE_P (current_class_type)) 17304 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false); 17305 /* Outside of a class definition, we can just parse the 17306 assignment-expression. */ 17307 else 17308 default_argument 17309 = cp_parser_default_argument (parser, template_parm_p); 17310 17311 if (!parser->default_arg_ok_p) 17312 { 17313 if (flag_permissive) 17314 warning (0, "deprecated use of default argument for parameter of non-function"); 17315 else 17316 { 17317 error_at (token->location, 17318 "default arguments are only " 17319 "permitted for function parameters"); 17320 default_argument = NULL_TREE; 17321 } 17322 } 17323 else if ((declarator && declarator->parameter_pack_p) 17324 || (decl_specifiers.type 17325 && PACK_EXPANSION_P (decl_specifiers.type))) 17326 { 17327 /* Find the name of the parameter pack. */ 17328 cp_declarator *id_declarator = declarator; 17329 while (id_declarator && id_declarator->kind != cdk_id) 17330 id_declarator = id_declarator->declarator; 17331 17332 if (id_declarator && id_declarator->kind == cdk_id) 17333 error_at (declarator_token_start->location, 17334 template_parm_p 17335 ? G_("template parameter pack %qD " 17336 "cannot have a default argument") 17337 : G_("parameter pack %qD cannot have " 17338 "a default argument"), 17339 id_declarator->u.id.unqualified_name); 17340 else 17341 error_at (declarator_token_start->location, 17342 template_parm_p 17343 ? G_("template parameter pack cannot have " 17344 "a default argument") 17345 : G_("parameter pack cannot have a " 17346 "default argument")); 17347 17348 default_argument = NULL_TREE; 17349 } 17350 } 17351 else 17352 default_argument = NULL_TREE; 17353 17354 return make_parameter_declarator (&decl_specifiers, 17355 declarator, 17356 default_argument); 17357 } 17358 17359 /* Parse a default argument and return it. 17360 17361 TEMPLATE_PARM_P is true if this is a default argument for a 17362 non-type template parameter. */ 17363 static tree 17364 cp_parser_default_argument (cp_parser *parser, bool template_parm_p) 17365 { 17366 tree default_argument = NULL_TREE; 17367 bool saved_greater_than_is_operator_p; 17368 bool saved_local_variables_forbidden_p; 17369 bool non_constant_p, is_direct_init; 17370 17371 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is 17372 set correctly. */ 17373 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p; 17374 parser->greater_than_is_operator_p = !template_parm_p; 17375 /* Local variable names (and the `this' keyword) may not 17376 appear in a default argument. */ 17377 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; 17378 parser->local_variables_forbidden_p = true; 17379 /* Parse the assignment-expression. */ 17380 if (template_parm_p) 17381 push_deferring_access_checks (dk_no_deferred); 17382 default_argument 17383 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p); 17384 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument)) 17385 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 17386 if (template_parm_p) 17387 pop_deferring_access_checks (); 17388 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p; 17389 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; 17390 17391 return default_argument; 17392 } 17393 17394 /* Parse a function-body. 17395 17396 function-body: 17397 compound_statement */ 17398 17399 static void 17400 cp_parser_function_body (cp_parser *parser) 17401 { 17402 cp_parser_compound_statement (parser, NULL, false, true); 17403 } 17404 17405 /* Parse a ctor-initializer-opt followed by a function-body. Return 17406 true if a ctor-initializer was present. */ 17407 17408 static bool 17409 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) 17410 { 17411 tree body, list; 17412 bool ctor_initializer_p; 17413 const bool check_body_p = 17414 DECL_CONSTRUCTOR_P (current_function_decl) 17415 && DECL_DECLARED_CONSTEXPR_P (current_function_decl); 17416 tree last = NULL; 17417 17418 /* Begin the function body. */ 17419 body = begin_function_body (); 17420 /* Parse the optional ctor-initializer. */ 17421 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); 17422 17423 /* If we're parsing a constexpr constructor definition, we need 17424 to check that the constructor body is indeed empty. However, 17425 before we get to cp_parser_function_body lot of junk has been 17426 generated, so we can't just check that we have an empty block. 17427 Rather we take a snapshot of the outermost block, and check whether 17428 cp_parser_function_body changed its state. */ 17429 if (check_body_p) 17430 { 17431 list = cur_stmt_list; 17432 if (STATEMENT_LIST_TAIL (list)) 17433 last = STATEMENT_LIST_TAIL (list)->stmt; 17434 } 17435 /* Parse the function-body. */ 17436 cp_parser_function_body (parser); 17437 if (check_body_p) 17438 check_constexpr_ctor_body (last, list); 17439 /* Finish the function body. */ 17440 finish_function_body (body); 17441 17442 return ctor_initializer_p; 17443 } 17444 17445 /* Parse an initializer. 17446 17447 initializer: 17448 = initializer-clause 17449 ( expression-list ) 17450 17451 Returns an expression representing the initializer. If no 17452 initializer is present, NULL_TREE is returned. 17453 17454 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause' 17455 production is used, and TRUE otherwise. *IS_DIRECT_INIT is 17456 set to TRUE if there is no initializer present. If there is an 17457 initializer, and it is not a constant-expression, *NON_CONSTANT_P 17458 is set to true; otherwise it is set to false. */ 17459 17460 static tree 17461 cp_parser_initializer (cp_parser* parser, bool* is_direct_init, 17462 bool* non_constant_p) 17463 { 17464 cp_token *token; 17465 tree init; 17466 17467 /* Peek at the next token. */ 17468 token = cp_lexer_peek_token (parser->lexer); 17469 17470 /* Let our caller know whether or not this initializer was 17471 parenthesized. */ 17472 *is_direct_init = (token->type != CPP_EQ); 17473 /* Assume that the initializer is constant. */ 17474 *non_constant_p = false; 17475 17476 if (token->type == CPP_EQ) 17477 { 17478 /* Consume the `='. */ 17479 cp_lexer_consume_token (parser->lexer); 17480 /* Parse the initializer-clause. */ 17481 init = cp_parser_initializer_clause (parser, non_constant_p); 17482 } 17483 else if (token->type == CPP_OPEN_PAREN) 17484 { 17485 VEC(tree,gc) *vec; 17486 vec = cp_parser_parenthesized_expression_list (parser, non_attr, 17487 /*cast_p=*/false, 17488 /*allow_expansion_p=*/true, 17489 non_constant_p); 17490 if (vec == NULL) 17491 return error_mark_node; 17492 init = build_tree_list_vec (vec); 17493 release_tree_vector (vec); 17494 } 17495 else if (token->type == CPP_OPEN_BRACE) 17496 { 17497 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 17498 init = cp_parser_braced_list (parser, non_constant_p); 17499 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1; 17500 } 17501 else 17502 { 17503 /* Anything else is an error. */ 17504 cp_parser_error (parser, "expected initializer"); 17505 init = error_mark_node; 17506 } 17507 17508 return init; 17509 } 17510 17511 /* Parse an initializer-clause. 17512 17513 initializer-clause: 17514 assignment-expression 17515 braced-init-list 17516 17517 Returns an expression representing the initializer. 17518 17519 If the `assignment-expression' production is used the value 17520 returned is simply a representation for the expression. 17521 17522 Otherwise, calls cp_parser_braced_list. */ 17523 17524 static tree 17525 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) 17526 { 17527 tree initializer; 17528 17529 /* Assume the expression is constant. */ 17530 *non_constant_p = false; 17531 17532 /* If it is not a `{', then we are looking at an 17533 assignment-expression. */ 17534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) 17535 { 17536 initializer 17537 = cp_parser_constant_expression (parser, 17538 /*allow_non_constant_p=*/true, 17539 non_constant_p); 17540 } 17541 else 17542 initializer = cp_parser_braced_list (parser, non_constant_p); 17543 17544 return initializer; 17545 } 17546 17547 /* Parse a brace-enclosed initializer list. 17548 17549 braced-init-list: 17550 { initializer-list , [opt] } 17551 { } 17552 17553 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be 17554 the elements of the initializer-list (or NULL, if the last 17555 production is used). The TREE_TYPE for the CONSTRUCTOR will be 17556 NULL_TREE. There is no way to detect whether or not the optional 17557 trailing `,' was provided. NON_CONSTANT_P is as for 17558 cp_parser_initializer. */ 17559 17560 static tree 17561 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p) 17562 { 17563 tree initializer; 17564 17565 /* Consume the `{' token. */ 17566 cp_lexer_consume_token (parser->lexer); 17567 /* Create a CONSTRUCTOR to represent the braced-initializer. */ 17568 initializer = make_node (CONSTRUCTOR); 17569 /* If it's not a `}', then there is a non-trivial initializer. */ 17570 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) 17571 { 17572 /* Parse the initializer list. */ 17573 CONSTRUCTOR_ELTS (initializer) 17574 = cp_parser_initializer_list (parser, non_constant_p); 17575 /* A trailing `,' token is allowed. */ 17576 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 17577 cp_lexer_consume_token (parser->lexer); 17578 } 17579 /* Now, there should be a trailing `}'. */ 17580 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 17581 TREE_TYPE (initializer) = init_list_type_node; 17582 return initializer; 17583 } 17584 17585 /* Parse an initializer-list. 17586 17587 initializer-list: 17588 initializer-clause ... [opt] 17589 initializer-list , initializer-clause ... [opt] 17590 17591 GNU Extension: 17592 17593 initializer-list: 17594 designation initializer-clause ...[opt] 17595 initializer-list , designation initializer-clause ...[opt] 17596 17597 designation: 17598 . identifier = 17599 identifier : 17600 [ constant-expression ] = 17601 17602 Returns a VEC of constructor_elt. The VALUE of each elt is an expression 17603 for the initializer. If the INDEX of the elt is non-NULL, it is the 17604 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is 17605 as for cp_parser_initializer. */ 17606 17607 static VEC(constructor_elt,gc) * 17608 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) 17609 { 17610 VEC(constructor_elt,gc) *v = NULL; 17611 17612 /* Assume all of the expressions are constant. */ 17613 *non_constant_p = false; 17614 17615 /* Parse the rest of the list. */ 17616 while (true) 17617 { 17618 cp_token *token; 17619 tree designator; 17620 tree initializer; 17621 bool clause_non_constant_p; 17622 17623 /* If the next token is an identifier and the following one is a 17624 colon, we are looking at the GNU designated-initializer 17625 syntax. */ 17626 if (cp_parser_allow_gnu_extensions_p (parser) 17627 && cp_lexer_next_token_is (parser->lexer, CPP_NAME) 17628 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) 17629 { 17630 /* Warn the user that they are using an extension. */ 17631 pedwarn (input_location, OPT_pedantic, 17632 "ISO C++ does not allow designated initializers"); 17633 /* Consume the identifier. */ 17634 designator = cp_lexer_consume_token (parser->lexer)->u.value; 17635 /* Consume the `:'. */ 17636 cp_lexer_consume_token (parser->lexer); 17637 } 17638 /* Also handle the C99 syntax, '. id ='. */ 17639 else if (cp_parser_allow_gnu_extensions_p (parser) 17640 && cp_lexer_next_token_is (parser->lexer, CPP_DOT) 17641 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME 17642 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ) 17643 { 17644 /* Warn the user that they are using an extension. */ 17645 pedwarn (input_location, OPT_pedantic, 17646 "ISO C++ does not allow C99 designated initializers"); 17647 /* Consume the `.'. */ 17648 cp_lexer_consume_token (parser->lexer); 17649 /* Consume the identifier. */ 17650 designator = cp_lexer_consume_token (parser->lexer)->u.value; 17651 /* Consume the `='. */ 17652 cp_lexer_consume_token (parser->lexer); 17653 } 17654 /* Also handle C99 array designators, '[ const ] ='. */ 17655 else if (cp_parser_allow_gnu_extensions_p (parser) 17656 && !c_dialect_objc () 17657 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 17658 { 17659 /* In C++11, [ could start a lambda-introducer. */ 17660 bool non_const = false; 17661 17662 cp_parser_parse_tentatively (parser); 17663 cp_lexer_consume_token (parser->lexer); 17664 designator = cp_parser_constant_expression (parser, true, &non_const); 17665 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 17666 cp_parser_require (parser, CPP_EQ, RT_EQ); 17667 if (!cp_parser_parse_definitely (parser)) 17668 designator = NULL_TREE; 17669 else if (non_const) 17670 require_potential_rvalue_constant_expression (designator); 17671 } 17672 else 17673 designator = NULL_TREE; 17674 17675 /* Parse the initializer. */ 17676 initializer = cp_parser_initializer_clause (parser, 17677 &clause_non_constant_p); 17678 /* If any clause is non-constant, so is the entire initializer. */ 17679 if (clause_non_constant_p) 17680 *non_constant_p = true; 17681 17682 /* If we have an ellipsis, this is an initializer pack 17683 expansion. */ 17684 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 17685 { 17686 /* Consume the `...'. */ 17687 cp_lexer_consume_token (parser->lexer); 17688 17689 /* Turn the initializer into an initializer expansion. */ 17690 initializer = make_pack_expansion (initializer); 17691 } 17692 17693 /* Add it to the vector. */ 17694 CONSTRUCTOR_APPEND_ELT (v, designator, initializer); 17695 17696 /* If the next token is not a comma, we have reached the end of 17697 the list. */ 17698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 17699 break; 17700 17701 /* Peek at the next token. */ 17702 token = cp_lexer_peek_nth_token (parser->lexer, 2); 17703 /* If the next token is a `}', then we're still done. An 17704 initializer-clause can have a trailing `,' after the 17705 initializer-list and before the closing `}'. */ 17706 if (token->type == CPP_CLOSE_BRACE) 17707 break; 17708 17709 /* Consume the `,' token. */ 17710 cp_lexer_consume_token (parser->lexer); 17711 } 17712 17713 return v; 17714 } 17715 17716 /* Classes [gram.class] */ 17717 17718 /* Parse a class-name. 17719 17720 class-name: 17721 identifier 17722 template-id 17723 17724 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used 17725 to indicate that names looked up in dependent types should be 17726 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' 17727 keyword has been used to indicate that the name that appears next 17728 is a template. TAG_TYPE indicates the explicit tag given before 17729 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are 17730 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class 17731 is the class being defined in a class-head. 17732 17733 Returns the TYPE_DECL representing the class. */ 17734 17735 static tree 17736 cp_parser_class_name (cp_parser *parser, 17737 bool typename_keyword_p, 17738 bool template_keyword_p, 17739 enum tag_types tag_type, 17740 bool check_dependency_p, 17741 bool class_head_p, 17742 bool is_declaration) 17743 { 17744 tree decl; 17745 tree scope; 17746 bool typename_p; 17747 cp_token *token; 17748 tree identifier = NULL_TREE; 17749 17750 /* All class-names start with an identifier. */ 17751 token = cp_lexer_peek_token (parser->lexer); 17752 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) 17753 { 17754 cp_parser_error (parser, "expected class-name"); 17755 return error_mark_node; 17756 } 17757 17758 /* PARSER->SCOPE can be cleared when parsing the template-arguments 17759 to a template-id, so we save it here. */ 17760 scope = parser->scope; 17761 if (scope == error_mark_node) 17762 return error_mark_node; 17763 17764 /* Any name names a type if we're following the `typename' keyword 17765 in a qualified name where the enclosing scope is type-dependent. */ 17766 typename_p = (typename_keyword_p && scope && TYPE_P (scope) 17767 && dependent_type_p (scope)); 17768 /* Handle the common case (an identifier, but not a template-id) 17769 efficiently. */ 17770 if (token->type == CPP_NAME 17771 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2)) 17772 { 17773 cp_token *identifier_token; 17774 bool ambiguous_p; 17775 17776 /* Look for the identifier. */ 17777 identifier_token = cp_lexer_peek_token (parser->lexer); 17778 ambiguous_p = identifier_token->ambiguous_p; 17779 identifier = cp_parser_identifier (parser); 17780 /* If the next token isn't an identifier, we are certainly not 17781 looking at a class-name. */ 17782 if (identifier == error_mark_node) 17783 decl = error_mark_node; 17784 /* If we know this is a type-name, there's no need to look it 17785 up. */ 17786 else if (typename_p) 17787 decl = identifier; 17788 else 17789 { 17790 tree ambiguous_decls; 17791 /* If we already know that this lookup is ambiguous, then 17792 we've already issued an error message; there's no reason 17793 to check again. */ 17794 if (ambiguous_p) 17795 { 17796 cp_parser_simulate_error (parser); 17797 return error_mark_node; 17798 } 17799 /* If the next token is a `::', then the name must be a type 17800 name. 17801 17802 [basic.lookup.qual] 17803 17804 During the lookup for a name preceding the :: scope 17805 resolution operator, object, function, and enumerator 17806 names are ignored. */ 17807 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 17808 tag_type = typename_type; 17809 /* Look up the name. */ 17810 decl = cp_parser_lookup_name (parser, identifier, 17811 tag_type, 17812 /*is_template=*/false, 17813 /*is_namespace=*/false, 17814 check_dependency_p, 17815 &ambiguous_decls, 17816 identifier_token->location); 17817 if (ambiguous_decls) 17818 { 17819 if (cp_parser_parsing_tentatively (parser)) 17820 cp_parser_simulate_error (parser); 17821 return error_mark_node; 17822 } 17823 } 17824 } 17825 else 17826 { 17827 /* Try a template-id. */ 17828 decl = cp_parser_template_id (parser, template_keyword_p, 17829 check_dependency_p, 17830 is_declaration); 17831 if (decl == error_mark_node) 17832 return error_mark_node; 17833 } 17834 17835 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); 17836 17837 /* If this is a typename, create a TYPENAME_TYPE. */ 17838 if (typename_p && decl != error_mark_node) 17839 { 17840 decl = make_typename_type (scope, decl, typename_type, 17841 /*complain=*/tf_error); 17842 if (decl != error_mark_node) 17843 decl = TYPE_NAME (decl); 17844 } 17845 17846 decl = strip_using_decl (decl); 17847 17848 /* Check to see that it is really the name of a class. */ 17849 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 17850 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE 17851 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 17852 /* Situations like this: 17853 17854 template <typename T> struct A { 17855 typename T::template X<int>::I i; 17856 }; 17857 17858 are problematic. Is `T::template X<int>' a class-name? The 17859 standard does not seem to be definitive, but there is no other 17860 valid interpretation of the following `::'. Therefore, those 17861 names are considered class-names. */ 17862 { 17863 decl = make_typename_type (scope, decl, tag_type, tf_error); 17864 if (decl != error_mark_node) 17865 decl = TYPE_NAME (decl); 17866 } 17867 else if (TREE_CODE (decl) != TYPE_DECL 17868 || TREE_TYPE (decl) == error_mark_node 17869 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)) 17870 /* In Objective-C 2.0, a classname followed by '.' starts a 17871 dot-syntax expression, and it's not a type-name. */ 17872 || (c_dialect_objc () 17873 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 17874 && objc_is_class_name (decl))) 17875 decl = error_mark_node; 17876 17877 if (decl == error_mark_node) 17878 cp_parser_error (parser, "expected class-name"); 17879 else if (identifier && !parser->scope) 17880 maybe_note_name_used_in_class (identifier, decl); 17881 17882 return decl; 17883 } 17884 17885 /* Parse a class-specifier. 17886 17887 class-specifier: 17888 class-head { member-specification [opt] } 17889 17890 Returns the TREE_TYPE representing the class. */ 17891 17892 static tree 17893 cp_parser_class_specifier_1 (cp_parser* parser) 17894 { 17895 tree type; 17896 tree attributes = NULL_TREE; 17897 bool nested_name_specifier_p; 17898 unsigned saved_num_template_parameter_lists; 17899 bool saved_in_function_body; 17900 unsigned char in_statement; 17901 bool in_switch_statement_p; 17902 bool saved_in_unbraced_linkage_specification_p; 17903 tree old_scope = NULL_TREE; 17904 tree scope = NULL_TREE; 17905 cp_token *closing_brace; 17906 17907 push_deferring_access_checks (dk_no_deferred); 17908 17909 /* Parse the class-head. */ 17910 type = cp_parser_class_head (parser, 17911 &nested_name_specifier_p); 17912 /* If the class-head was a semantic disaster, skip the entire body 17913 of the class. */ 17914 if (!type) 17915 { 17916 cp_parser_skip_to_end_of_block_or_statement (parser); 17917 pop_deferring_access_checks (); 17918 return error_mark_node; 17919 } 17920 17921 /* Look for the `{'. */ 17922 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 17923 { 17924 pop_deferring_access_checks (); 17925 return error_mark_node; 17926 } 17927 17928 /* Issue an error message if type-definitions are forbidden here. */ 17929 cp_parser_check_type_definition (parser); 17930 /* Remember that we are defining one more class. */ 17931 ++parser->num_classes_being_defined; 17932 /* Inside the class, surrounding template-parameter-lists do not 17933 apply. */ 17934 saved_num_template_parameter_lists 17935 = parser->num_template_parameter_lists; 17936 parser->num_template_parameter_lists = 0; 17937 /* We are not in a function body. */ 17938 saved_in_function_body = parser->in_function_body; 17939 parser->in_function_body = false; 17940 /* Or in a loop. */ 17941 in_statement = parser->in_statement; 17942 parser->in_statement = 0; 17943 /* Or in a switch. */ 17944 in_switch_statement_p = parser->in_switch_statement_p; 17945 parser->in_switch_statement_p = false; 17946 /* We are not immediately inside an extern "lang" block. */ 17947 saved_in_unbraced_linkage_specification_p 17948 = parser->in_unbraced_linkage_specification_p; 17949 parser->in_unbraced_linkage_specification_p = false; 17950 17951 /* Start the class. */ 17952 if (nested_name_specifier_p) 17953 { 17954 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)); 17955 old_scope = push_inner_scope (scope); 17956 } 17957 type = begin_class_definition (type); 17958 17959 if (type == error_mark_node) 17960 /* If the type is erroneous, skip the entire body of the class. */ 17961 cp_parser_skip_to_closing_brace (parser); 17962 else 17963 /* Parse the member-specification. */ 17964 cp_parser_member_specification_opt (parser); 17965 17966 /* Look for the trailing `}'. */ 17967 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 17968 /* Look for trailing attributes to apply to this class. */ 17969 if (cp_parser_allow_gnu_extensions_p (parser)) 17970 attributes = cp_parser_attributes_opt (parser); 17971 if (type != error_mark_node) 17972 type = finish_struct (type, attributes); 17973 if (nested_name_specifier_p) 17974 pop_inner_scope (old_scope, scope); 17975 17976 /* We've finished a type definition. Check for the common syntax 17977 error of forgetting a semicolon after the definition. We need to 17978 be careful, as we can't just check for not-a-semicolon and be done 17979 with it; the user might have typed: 17980 17981 class X { } c = ...; 17982 class X { } *p = ...; 17983 17984 and so forth. Instead, enumerate all the possible tokens that 17985 might follow this production; if we don't see one of them, then 17986 complain and silently insert the semicolon. */ 17987 { 17988 cp_token *token = cp_lexer_peek_token (parser->lexer); 17989 bool want_semicolon = true; 17990 17991 switch (token->type) 17992 { 17993 case CPP_NAME: 17994 case CPP_SEMICOLON: 17995 case CPP_MULT: 17996 case CPP_AND: 17997 case CPP_OPEN_PAREN: 17998 case CPP_CLOSE_PAREN: 17999 case CPP_COMMA: 18000 want_semicolon = false; 18001 break; 18002 18003 /* While it's legal for type qualifiers and storage class 18004 specifiers to follow type definitions in the grammar, only 18005 compiler testsuites contain code like that. Assume that if 18006 we see such code, then what we're really seeing is a case 18007 like: 18008 18009 class X { } 18010 const <type> var = ...; 18011 18012 or 18013 18014 class Y { } 18015 static <type> func (...) ... 18016 18017 i.e. the qualifier or specifier applies to the next 18018 declaration. To do so, however, we need to look ahead one 18019 more token to see if *that* token is a type specifier. 18020 18021 This code could be improved to handle: 18022 18023 class Z { } 18024 static const <type> var = ...; */ 18025 case CPP_KEYWORD: 18026 if (keyword_is_decl_specifier (token->keyword)) 18027 { 18028 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2); 18029 18030 /* Handling user-defined types here would be nice, but very 18031 tricky. */ 18032 want_semicolon 18033 = (lookahead->type == CPP_KEYWORD 18034 && keyword_begins_type_specifier (lookahead->keyword)); 18035 } 18036 break; 18037 default: 18038 break; 18039 } 18040 18041 /* If we don't have a type, then something is very wrong and we 18042 shouldn't try to do anything clever. Likewise for not seeing the 18043 closing brace. */ 18044 if (closing_brace && TYPE_P (type) && want_semicolon) 18045 { 18046 cp_token_position prev 18047 = cp_lexer_previous_token_position (parser->lexer); 18048 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev); 18049 location_t loc = prev_token->location; 18050 18051 if (CLASSTYPE_DECLARED_CLASS (type)) 18052 error_at (loc, "expected %<;%> after class definition"); 18053 else if (TREE_CODE (type) == RECORD_TYPE) 18054 error_at (loc, "expected %<;%> after struct definition"); 18055 else if (TREE_CODE (type) == UNION_TYPE) 18056 error_at (loc, "expected %<;%> after union definition"); 18057 else 18058 gcc_unreachable (); 18059 18060 /* Unget one token and smash it to look as though we encountered 18061 a semicolon in the input stream. */ 18062 cp_lexer_set_token_position (parser->lexer, prev); 18063 token = cp_lexer_peek_token (parser->lexer); 18064 token->type = CPP_SEMICOLON; 18065 token->keyword = RID_MAX; 18066 } 18067 } 18068 18069 /* If this class is not itself within the scope of another class, 18070 then we need to parse the bodies of all of the queued function 18071 definitions. Note that the queued functions defined in a class 18072 are not always processed immediately following the 18073 class-specifier for that class. Consider: 18074 18075 struct A { 18076 struct B { void f() { sizeof (A); } }; 18077 }; 18078 18079 If `f' were processed before the processing of `A' were 18080 completed, there would be no way to compute the size of `A'. 18081 Note that the nesting we are interested in here is lexical -- 18082 not the semantic nesting given by TYPE_CONTEXT. In particular, 18083 for: 18084 18085 struct A { struct B; }; 18086 struct A::B { void f() { } }; 18087 18088 there is no need to delay the parsing of `A::B::f'. */ 18089 if (--parser->num_classes_being_defined == 0) 18090 { 18091 tree decl; 18092 tree class_type = NULL_TREE; 18093 tree pushed_scope = NULL_TREE; 18094 unsigned ix; 18095 cp_default_arg_entry *e; 18096 tree save_ccp, save_ccr; 18097 18098 /* In a first pass, parse default arguments to the functions. 18099 Then, in a second pass, parse the bodies of the functions. 18100 This two-phased approach handles cases like: 18101 18102 struct S { 18103 void f() { g(); } 18104 void g(int i = 3); 18105 }; 18106 18107 */ 18108 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args, 18109 ix, e) 18110 { 18111 decl = e->decl; 18112 /* If there are default arguments that have not yet been processed, 18113 take care of them now. */ 18114 if (class_type != e->class_type) 18115 { 18116 if (pushed_scope) 18117 pop_scope (pushed_scope); 18118 class_type = e->class_type; 18119 pushed_scope = push_scope (class_type); 18120 } 18121 /* Make sure that any template parameters are in scope. */ 18122 maybe_begin_member_template_processing (decl); 18123 /* Parse the default argument expressions. */ 18124 cp_parser_late_parsing_default_args (parser, decl); 18125 /* Remove any template parameters from the symbol table. */ 18126 maybe_end_member_template_processing (); 18127 } 18128 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0); 18129 /* Now parse any NSDMIs. */ 18130 save_ccp = current_class_ptr; 18131 save_ccr = current_class_ref; 18132 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl) 18133 { 18134 if (class_type != DECL_CONTEXT (decl)) 18135 { 18136 if (pushed_scope) 18137 pop_scope (pushed_scope); 18138 class_type = DECL_CONTEXT (decl); 18139 pushed_scope = push_scope (class_type); 18140 } 18141 inject_this_parameter (class_type, TYPE_UNQUALIFIED); 18142 cp_parser_late_parsing_nsdmi (parser, decl); 18143 } 18144 VEC_truncate (tree, unparsed_nsdmis, 0); 18145 current_class_ptr = save_ccp; 18146 current_class_ref = save_ccr; 18147 if (pushed_scope) 18148 pop_scope (pushed_scope); 18149 /* Now parse the body of the functions. */ 18150 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl) 18151 cp_parser_late_parsing_for_member (parser, decl); 18152 VEC_truncate (tree, unparsed_funs_with_definitions, 0); 18153 } 18154 18155 /* Put back any saved access checks. */ 18156 pop_deferring_access_checks (); 18157 18158 /* Restore saved state. */ 18159 parser->in_switch_statement_p = in_switch_statement_p; 18160 parser->in_statement = in_statement; 18161 parser->in_function_body = saved_in_function_body; 18162 parser->num_template_parameter_lists 18163 = saved_num_template_parameter_lists; 18164 parser->in_unbraced_linkage_specification_p 18165 = saved_in_unbraced_linkage_specification_p; 18166 18167 return type; 18168 } 18169 18170 static tree 18171 cp_parser_class_specifier (cp_parser* parser) 18172 { 18173 tree ret; 18174 timevar_push (TV_PARSE_STRUCT); 18175 ret = cp_parser_class_specifier_1 (parser); 18176 timevar_pop (TV_PARSE_STRUCT); 18177 return ret; 18178 } 18179 18180 /* Parse a class-head. 18181 18182 class-head: 18183 class-key identifier [opt] base-clause [opt] 18184 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt] 18185 class-key nested-name-specifier [opt] template-id 18186 base-clause [opt] 18187 18188 class-virt-specifier: 18189 final 18190 18191 GNU Extensions: 18192 class-key attributes identifier [opt] base-clause [opt] 18193 class-key attributes nested-name-specifier identifier base-clause [opt] 18194 class-key attributes nested-name-specifier [opt] template-id 18195 base-clause [opt] 18196 18197 Upon return BASES is initialized to the list of base classes (or 18198 NULL, if there are none) in the same form returned by 18199 cp_parser_base_clause. 18200 18201 Returns the TYPE of the indicated class. Sets 18202 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions 18203 involving a nested-name-specifier was used, and FALSE otherwise. 18204 18205 Returns error_mark_node if this is not a class-head. 18206 18207 Returns NULL_TREE if the class-head is syntactically valid, but 18208 semantically invalid in a way that means we should skip the entire 18209 body of the class. */ 18210 18211 static tree 18212 cp_parser_class_head (cp_parser* parser, 18213 bool* nested_name_specifier_p) 18214 { 18215 tree nested_name_specifier; 18216 enum tag_types class_key; 18217 tree id = NULL_TREE; 18218 tree type = NULL_TREE; 18219 tree attributes; 18220 tree bases; 18221 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED; 18222 bool template_id_p = false; 18223 bool qualified_p = false; 18224 bool invalid_nested_name_p = false; 18225 bool invalid_explicit_specialization_p = false; 18226 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 18227 tree pushed_scope = NULL_TREE; 18228 unsigned num_templates; 18229 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL; 18230 /* Assume no nested-name-specifier will be present. */ 18231 *nested_name_specifier_p = false; 18232 /* Assume no template parameter lists will be used in defining the 18233 type. */ 18234 num_templates = 0; 18235 parser->colon_corrects_to_scope_p = false; 18236 18237 /* Look for the class-key. */ 18238 class_key = cp_parser_class_key (parser); 18239 if (class_key == none_type) 18240 return error_mark_node; 18241 18242 /* Parse the attributes. */ 18243 attributes = cp_parser_attributes_opt (parser); 18244 18245 /* If the next token is `::', that is invalid -- but sometimes 18246 people do try to write: 18247 18248 struct ::S {}; 18249 18250 Handle this gracefully by accepting the extra qualifier, and then 18251 issuing an error about it later if this really is a 18252 class-head. If it turns out just to be an elaborated type 18253 specifier, remain silent. */ 18254 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) 18255 qualified_p = true; 18256 18257 push_deferring_access_checks (dk_no_check); 18258 18259 /* Determine the name of the class. Begin by looking for an 18260 optional nested-name-specifier. */ 18261 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer); 18262 nested_name_specifier 18263 = cp_parser_nested_name_specifier_opt (parser, 18264 /*typename_keyword_p=*/false, 18265 /*check_dependency_p=*/false, 18266 /*type_p=*/false, 18267 /*is_declaration=*/false); 18268 /* If there was a nested-name-specifier, then there *must* be an 18269 identifier. */ 18270 if (nested_name_specifier) 18271 { 18272 type_start_token = cp_lexer_peek_token (parser->lexer); 18273 /* Although the grammar says `identifier', it really means 18274 `class-name' or `template-name'. You are only allowed to 18275 define a class that has already been declared with this 18276 syntax. 18277 18278 The proposed resolution for Core Issue 180 says that wherever 18279 you see `class T::X' you should treat `X' as a type-name. 18280 18281 It is OK to define an inaccessible class; for example: 18282 18283 class A { class B; }; 18284 class A::B {}; 18285 18286 We do not know if we will see a class-name, or a 18287 template-name. We look for a class-name first, in case the 18288 class-name is a template-id; if we looked for the 18289 template-name first we would stop after the template-name. */ 18290 cp_parser_parse_tentatively (parser); 18291 type = cp_parser_class_name (parser, 18292 /*typename_keyword_p=*/false, 18293 /*template_keyword_p=*/false, 18294 class_type, 18295 /*check_dependency_p=*/false, 18296 /*class_head_p=*/true, 18297 /*is_declaration=*/false); 18298 /* If that didn't work, ignore the nested-name-specifier. */ 18299 if (!cp_parser_parse_definitely (parser)) 18300 { 18301 invalid_nested_name_p = true; 18302 type_start_token = cp_lexer_peek_token (parser->lexer); 18303 id = cp_parser_identifier (parser); 18304 if (id == error_mark_node) 18305 id = NULL_TREE; 18306 } 18307 /* If we could not find a corresponding TYPE, treat this 18308 declaration like an unqualified declaration. */ 18309 if (type == error_mark_node) 18310 nested_name_specifier = NULL_TREE; 18311 /* Otherwise, count the number of templates used in TYPE and its 18312 containing scopes. */ 18313 else 18314 { 18315 tree scope; 18316 18317 for (scope = TREE_TYPE (type); 18318 scope && TREE_CODE (scope) != NAMESPACE_DECL; 18319 scope = (TYPE_P (scope) 18320 ? TYPE_CONTEXT (scope) 18321 : DECL_CONTEXT (scope))) 18322 if (TYPE_P (scope) 18323 && CLASS_TYPE_P (scope) 18324 && CLASSTYPE_TEMPLATE_INFO (scope) 18325 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) 18326 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) 18327 ++num_templates; 18328 } 18329 } 18330 /* Otherwise, the identifier is optional. */ 18331 else 18332 { 18333 /* We don't know whether what comes next is a template-id, 18334 an identifier, or nothing at all. */ 18335 cp_parser_parse_tentatively (parser); 18336 /* Check for a template-id. */ 18337 type_start_token = cp_lexer_peek_token (parser->lexer); 18338 id = cp_parser_template_id (parser, 18339 /*template_keyword_p=*/false, 18340 /*check_dependency_p=*/true, 18341 /*is_declaration=*/true); 18342 /* If that didn't work, it could still be an identifier. */ 18343 if (!cp_parser_parse_definitely (parser)) 18344 { 18345 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 18346 { 18347 type_start_token = cp_lexer_peek_token (parser->lexer); 18348 id = cp_parser_identifier (parser); 18349 } 18350 else 18351 id = NULL_TREE; 18352 } 18353 else 18354 { 18355 template_id_p = true; 18356 ++num_templates; 18357 } 18358 } 18359 18360 pop_deferring_access_checks (); 18361 18362 if (id) 18363 { 18364 cp_parser_check_for_invalid_template_id (parser, id, 18365 type_start_token->location); 18366 } 18367 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser); 18368 18369 /* If it's not a `:' or a `{' then we can't really be looking at a 18370 class-head, since a class-head only appears as part of a 18371 class-specifier. We have to detect this situation before calling 18372 xref_tag, since that has irreversible side-effects. */ 18373 if (!cp_parser_next_token_starts_class_definition_p (parser)) 18374 { 18375 cp_parser_error (parser, "expected %<{%> or %<:%>"); 18376 type = error_mark_node; 18377 goto out; 18378 } 18379 18380 /* At this point, we're going ahead with the class-specifier, even 18381 if some other problem occurs. */ 18382 cp_parser_commit_to_tentative_parse (parser); 18383 if (virt_specifiers & VIRT_SPEC_OVERRIDE) 18384 { 18385 cp_parser_error (parser, 18386 "cannot specify %<override%> for a class"); 18387 type = error_mark_node; 18388 goto out; 18389 } 18390 /* Issue the error about the overly-qualified name now. */ 18391 if (qualified_p) 18392 { 18393 cp_parser_error (parser, 18394 "global qualification of class name is invalid"); 18395 type = error_mark_node; 18396 goto out; 18397 } 18398 else if (invalid_nested_name_p) 18399 { 18400 cp_parser_error (parser, 18401 "qualified name does not name a class"); 18402 type = error_mark_node; 18403 goto out; 18404 } 18405 else if (nested_name_specifier) 18406 { 18407 tree scope; 18408 18409 /* Reject typedef-names in class heads. */ 18410 if (!DECL_IMPLICIT_TYPEDEF_P (type)) 18411 { 18412 error_at (type_start_token->location, 18413 "invalid class name in declaration of %qD", 18414 type); 18415 type = NULL_TREE; 18416 goto done; 18417 } 18418 18419 /* Figure out in what scope the declaration is being placed. */ 18420 scope = current_scope (); 18421 /* If that scope does not contain the scope in which the 18422 class was originally declared, the program is invalid. */ 18423 if (scope && !is_ancestor (scope, nested_name_specifier)) 18424 { 18425 if (at_namespace_scope_p ()) 18426 error_at (type_start_token->location, 18427 "declaration of %qD in namespace %qD which does not " 18428 "enclose %qD", 18429 type, scope, nested_name_specifier); 18430 else 18431 error_at (type_start_token->location, 18432 "declaration of %qD in %qD which does not enclose %qD", 18433 type, scope, nested_name_specifier); 18434 type = NULL_TREE; 18435 goto done; 18436 } 18437 /* [dcl.meaning] 18438 18439 A declarator-id shall not be qualified except for the 18440 definition of a ... nested class outside of its class 18441 ... [or] the definition or explicit instantiation of a 18442 class member of a namespace outside of its namespace. */ 18443 if (scope == nested_name_specifier) 18444 { 18445 permerror (nested_name_specifier_token_start->location, 18446 "extra qualification not allowed"); 18447 nested_name_specifier = NULL_TREE; 18448 num_templates = 0; 18449 } 18450 } 18451 /* An explicit-specialization must be preceded by "template <>". If 18452 it is not, try to recover gracefully. */ 18453 if (at_namespace_scope_p () 18454 && parser->num_template_parameter_lists == 0 18455 && template_id_p) 18456 { 18457 error_at (type_start_token->location, 18458 "an explicit specialization must be preceded by %<template <>%>"); 18459 invalid_explicit_specialization_p = true; 18460 /* Take the same action that would have been taken by 18461 cp_parser_explicit_specialization. */ 18462 ++parser->num_template_parameter_lists; 18463 begin_specialization (); 18464 } 18465 /* There must be no "return" statements between this point and the 18466 end of this function; set "type "to the correct return value and 18467 use "goto done;" to return. */ 18468 /* Make sure that the right number of template parameters were 18469 present. */ 18470 if (!cp_parser_check_template_parameters (parser, num_templates, 18471 type_start_token->location, 18472 /*declarator=*/NULL)) 18473 { 18474 /* If something went wrong, there is no point in even trying to 18475 process the class-definition. */ 18476 type = NULL_TREE; 18477 goto done; 18478 } 18479 18480 /* Look up the type. */ 18481 if (template_id_p) 18482 { 18483 if (TREE_CODE (id) == TEMPLATE_ID_EXPR 18484 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0)) 18485 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD)) 18486 { 18487 error_at (type_start_token->location, 18488 "function template %qD redeclared as a class template", id); 18489 type = error_mark_node; 18490 } 18491 else 18492 { 18493 type = TREE_TYPE (id); 18494 type = maybe_process_partial_specialization (type); 18495 } 18496 if (nested_name_specifier) 18497 pushed_scope = push_scope (nested_name_specifier); 18498 } 18499 else if (nested_name_specifier) 18500 { 18501 tree class_type; 18502 18503 /* Given: 18504 18505 template <typename T> struct S { struct T }; 18506 template <typename T> struct S<T>::T { }; 18507 18508 we will get a TYPENAME_TYPE when processing the definition of 18509 `S::T'. We need to resolve it to the actual type before we 18510 try to define it. */ 18511 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) 18512 { 18513 class_type = resolve_typename_type (TREE_TYPE (type), 18514 /*only_current_p=*/false); 18515 if (TREE_CODE (class_type) != TYPENAME_TYPE) 18516 type = TYPE_NAME (class_type); 18517 else 18518 { 18519 cp_parser_error (parser, "could not resolve typename type"); 18520 type = error_mark_node; 18521 } 18522 } 18523 18524 if (maybe_process_partial_specialization (TREE_TYPE (type)) 18525 == error_mark_node) 18526 { 18527 type = NULL_TREE; 18528 goto done; 18529 } 18530 18531 class_type = current_class_type; 18532 /* Enter the scope indicated by the nested-name-specifier. */ 18533 pushed_scope = push_scope (nested_name_specifier); 18534 /* Get the canonical version of this type. */ 18535 type = TYPE_MAIN_DECL (TREE_TYPE (type)); 18536 if (PROCESSING_REAL_TEMPLATE_DECL_P () 18537 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) 18538 { 18539 type = push_template_decl (type); 18540 if (type == error_mark_node) 18541 { 18542 type = NULL_TREE; 18543 goto done; 18544 } 18545 } 18546 18547 type = TREE_TYPE (type); 18548 *nested_name_specifier_p = true; 18549 } 18550 else /* The name is not a nested name. */ 18551 { 18552 /* If the class was unnamed, create a dummy name. */ 18553 if (!id) 18554 id = make_anon_name (); 18555 type = xref_tag (class_key, id, /*tag_scope=*/ts_current, 18556 parser->num_template_parameter_lists); 18557 } 18558 18559 /* Indicate whether this class was declared as a `class' or as a 18560 `struct'. */ 18561 if (TREE_CODE (type) == RECORD_TYPE) 18562 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); 18563 cp_parser_check_class_key (class_key, type); 18564 18565 /* If this type was already complete, and we see another definition, 18566 that's an error. */ 18567 if (type != error_mark_node && COMPLETE_TYPE_P (type)) 18568 { 18569 error_at (type_start_token->location, "redefinition of %q#T", 18570 type); 18571 error_at (type_start_token->location, "previous definition of %q+#T", 18572 type); 18573 type = NULL_TREE; 18574 goto done; 18575 } 18576 else if (type == error_mark_node) 18577 type = NULL_TREE; 18578 18579 if (type) 18580 { 18581 /* Apply attributes now, before any use of the class as a template 18582 argument in its base list. */ 18583 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE); 18584 fixup_attribute_variants (type); 18585 } 18586 18587 /* We will have entered the scope containing the class; the names of 18588 base classes should be looked up in that context. For example: 18589 18590 struct A { struct B {}; struct C; }; 18591 struct A::C : B {}; 18592 18593 is valid. */ 18594 18595 /* Get the list of base-classes, if there is one. */ 18596 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 18597 bases = cp_parser_base_clause (parser); 18598 else 18599 bases = NULL_TREE; 18600 18601 /* If we're really defining a class, process the base classes. 18602 If they're invalid, fail. */ 18603 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE) 18604 && !xref_basetypes (type, bases)) 18605 type = NULL_TREE; 18606 18607 done: 18608 /* Leave the scope given by the nested-name-specifier. We will 18609 enter the class scope itself while processing the members. */ 18610 if (pushed_scope) 18611 pop_scope (pushed_scope); 18612 18613 if (invalid_explicit_specialization_p) 18614 { 18615 end_specialization (); 18616 --parser->num_template_parameter_lists; 18617 } 18618 18619 if (type) 18620 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location; 18621 if (type && (virt_specifiers & VIRT_SPEC_FINAL)) 18622 CLASSTYPE_FINAL (type) = 1; 18623 out: 18624 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 18625 return type; 18626 } 18627 18628 /* Parse a class-key. 18629 18630 class-key: 18631 class 18632 struct 18633 union 18634 18635 Returns the kind of class-key specified, or none_type to indicate 18636 error. */ 18637 18638 static enum tag_types 18639 cp_parser_class_key (cp_parser* parser) 18640 { 18641 cp_token *token; 18642 enum tag_types tag_type; 18643 18644 /* Look for the class-key. */ 18645 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY); 18646 if (!token) 18647 return none_type; 18648 18649 /* Check to see if the TOKEN is a class-key. */ 18650 tag_type = cp_parser_token_is_class_key (token); 18651 if (!tag_type) 18652 cp_parser_error (parser, "expected class-key"); 18653 return tag_type; 18654 } 18655 18656 /* Parse an (optional) member-specification. 18657 18658 member-specification: 18659 member-declaration member-specification [opt] 18660 access-specifier : member-specification [opt] */ 18661 18662 static void 18663 cp_parser_member_specification_opt (cp_parser* parser) 18664 { 18665 while (true) 18666 { 18667 cp_token *token; 18668 enum rid keyword; 18669 18670 /* Peek at the next token. */ 18671 token = cp_lexer_peek_token (parser->lexer); 18672 /* If it's a `}', or EOF then we've seen all the members. */ 18673 if (token->type == CPP_CLOSE_BRACE 18674 || token->type == CPP_EOF 18675 || token->type == CPP_PRAGMA_EOL) 18676 break; 18677 18678 /* See if this token is a keyword. */ 18679 keyword = token->keyword; 18680 switch (keyword) 18681 { 18682 case RID_PUBLIC: 18683 case RID_PROTECTED: 18684 case RID_PRIVATE: 18685 /* Consume the access-specifier. */ 18686 cp_lexer_consume_token (parser->lexer); 18687 /* Remember which access-specifier is active. */ 18688 current_access_specifier = token->u.value; 18689 /* Look for the `:'. */ 18690 cp_parser_require (parser, CPP_COLON, RT_COLON); 18691 break; 18692 18693 default: 18694 /* Accept #pragmas at class scope. */ 18695 if (token->type == CPP_PRAGMA) 18696 { 18697 cp_parser_pragma (parser, pragma_external); 18698 break; 18699 } 18700 18701 /* Otherwise, the next construction must be a 18702 member-declaration. */ 18703 cp_parser_member_declaration (parser); 18704 } 18705 } 18706 } 18707 18708 /* Parse a member-declaration. 18709 18710 member-declaration: 18711 decl-specifier-seq [opt] member-declarator-list [opt] ; 18712 function-definition ; [opt] 18713 :: [opt] nested-name-specifier template [opt] unqualified-id ; 18714 using-declaration 18715 template-declaration 18716 alias-declaration 18717 18718 member-declarator-list: 18719 member-declarator 18720 member-declarator-list , member-declarator 18721 18722 member-declarator: 18723 declarator pure-specifier [opt] 18724 declarator constant-initializer [opt] 18725 identifier [opt] : constant-expression 18726 18727 GNU Extensions: 18728 18729 member-declaration: 18730 __extension__ member-declaration 18731 18732 member-declarator: 18733 declarator attributes [opt] pure-specifier [opt] 18734 declarator attributes [opt] constant-initializer [opt] 18735 identifier [opt] attributes [opt] : constant-expression 18736 18737 C++0x Extensions: 18738 18739 member-declaration: 18740 static_assert-declaration */ 18741 18742 static void 18743 cp_parser_member_declaration (cp_parser* parser) 18744 { 18745 cp_decl_specifier_seq decl_specifiers; 18746 tree prefix_attributes; 18747 tree decl; 18748 int declares_class_or_enum; 18749 bool friend_p; 18750 cp_token *token = NULL; 18751 cp_token *decl_spec_token_start = NULL; 18752 cp_token *initializer_token_start = NULL; 18753 int saved_pedantic; 18754 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 18755 18756 /* Check for the `__extension__' keyword. */ 18757 if (cp_parser_extension_opt (parser, &saved_pedantic)) 18758 { 18759 /* Recurse. */ 18760 cp_parser_member_declaration (parser); 18761 /* Restore the old value of the PEDANTIC flag. */ 18762 pedantic = saved_pedantic; 18763 18764 return; 18765 } 18766 18767 /* Check for a template-declaration. */ 18768 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) 18769 { 18770 /* An explicit specialization here is an error condition, and we 18771 expect the specialization handler to detect and report this. */ 18772 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS 18773 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) 18774 cp_parser_explicit_specialization (parser); 18775 else 18776 cp_parser_template_declaration (parser, /*member_p=*/true); 18777 18778 return; 18779 } 18780 18781 /* Check for a using-declaration. */ 18782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) 18783 { 18784 if (cxx_dialect < cxx0x) 18785 { 18786 /* Parse the using-declaration. */ 18787 cp_parser_using_declaration (parser, 18788 /*access_declaration_p=*/false); 18789 return; 18790 } 18791 else 18792 { 18793 tree decl; 18794 cp_parser_parse_tentatively (parser); 18795 decl = cp_parser_alias_declaration (parser); 18796 if (cp_parser_parse_definitely (parser)) 18797 finish_member_declaration (decl); 18798 else 18799 cp_parser_using_declaration (parser, 18800 /*access_declaration_p=*/false); 18801 return; 18802 } 18803 } 18804 18805 /* Check for @defs. */ 18806 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS)) 18807 { 18808 tree ivar, member; 18809 tree ivar_chains = cp_parser_objc_defs_expression (parser); 18810 ivar = ivar_chains; 18811 while (ivar) 18812 { 18813 member = ivar; 18814 ivar = TREE_CHAIN (member); 18815 TREE_CHAIN (member) = NULL_TREE; 18816 finish_member_declaration (member); 18817 } 18818 return; 18819 } 18820 18821 /* If the next token is `static_assert' we have a static assertion. */ 18822 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT)) 18823 { 18824 cp_parser_static_assert (parser, /*member_p=*/true); 18825 return; 18826 } 18827 18828 parser->colon_corrects_to_scope_p = false; 18829 18830 if (cp_parser_using_declaration (parser, /*access_declaration=*/true)) 18831 goto out; 18832 18833 /* Parse the decl-specifier-seq. */ 18834 decl_spec_token_start = cp_lexer_peek_token (parser->lexer); 18835 cp_parser_decl_specifier_seq (parser, 18836 CP_PARSER_FLAGS_OPTIONAL, 18837 &decl_specifiers, 18838 &declares_class_or_enum); 18839 prefix_attributes = decl_specifiers.attributes; 18840 decl_specifiers.attributes = NULL_TREE; 18841 /* Check for an invalid type-name. */ 18842 if (!decl_specifiers.any_type_specifiers_p 18843 && cp_parser_parse_and_diagnose_invalid_type_name (parser)) 18844 goto out; 18845 /* If there is no declarator, then the decl-specifier-seq should 18846 specify a type. */ 18847 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 18848 { 18849 /* If there was no decl-specifier-seq, and the next token is a 18850 `;', then we have something like: 18851 18852 struct S { ; }; 18853 18854 [class.mem] 18855 18856 Each member-declaration shall declare at least one member 18857 name of the class. */ 18858 if (!decl_specifiers.any_specifiers_p) 18859 { 18860 cp_token *token = cp_lexer_peek_token (parser->lexer); 18861 if (!in_system_header_at (token->location)) 18862 pedwarn (token->location, OPT_pedantic, "extra %<;%>"); 18863 } 18864 else 18865 { 18866 tree type; 18867 18868 /* See if this declaration is a friend. */ 18869 friend_p = cp_parser_friend_p (&decl_specifiers); 18870 /* If there were decl-specifiers, check to see if there was 18871 a class-declaration. */ 18872 type = check_tag_decl (&decl_specifiers); 18873 /* Nested classes have already been added to the class, but 18874 a `friend' needs to be explicitly registered. */ 18875 if (friend_p) 18876 { 18877 /* If the `friend' keyword was present, the friend must 18878 be introduced with a class-key. */ 18879 if (!declares_class_or_enum && cxx_dialect < cxx0x) 18880 pedwarn (decl_spec_token_start->location, OPT_pedantic, 18881 "in C++03 a class-key must be used " 18882 "when declaring a friend"); 18883 /* In this case: 18884 18885 template <typename T> struct A { 18886 friend struct A<T>::B; 18887 }; 18888 18889 A<T>::B will be represented by a TYPENAME_TYPE, and 18890 therefore not recognized by check_tag_decl. */ 18891 if (!type) 18892 { 18893 type = decl_specifiers.type; 18894 if (type && TREE_CODE (type) == TYPE_DECL) 18895 type = TREE_TYPE (type); 18896 } 18897 if (!type || !TYPE_P (type)) 18898 error_at (decl_spec_token_start->location, 18899 "friend declaration does not name a class or " 18900 "function"); 18901 else 18902 make_friend_class (current_class_type, type, 18903 /*complain=*/true); 18904 } 18905 /* If there is no TYPE, an error message will already have 18906 been issued. */ 18907 else if (!type || type == error_mark_node) 18908 ; 18909 /* An anonymous aggregate has to be handled specially; such 18910 a declaration really declares a data member (with a 18911 particular type), as opposed to a nested class. */ 18912 else if (ANON_AGGR_TYPE_P (type)) 18913 { 18914 /* Remove constructors and such from TYPE, now that we 18915 know it is an anonymous aggregate. */ 18916 fixup_anonymous_aggr (type); 18917 /* And make the corresponding data member. */ 18918 decl = build_decl (decl_spec_token_start->location, 18919 FIELD_DECL, NULL_TREE, type); 18920 /* Add it to the class. */ 18921 finish_member_declaration (decl); 18922 } 18923 else 18924 cp_parser_check_access_in_redeclaration 18925 (TYPE_NAME (type), 18926 decl_spec_token_start->location); 18927 } 18928 } 18929 else 18930 { 18931 bool assume_semicolon = false; 18932 18933 /* See if these declarations will be friends. */ 18934 friend_p = cp_parser_friend_p (&decl_specifiers); 18935 18936 /* Keep going until we hit the `;' at the end of the 18937 declaration. */ 18938 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 18939 { 18940 tree attributes = NULL_TREE; 18941 tree first_attribute; 18942 18943 /* Peek at the next token. */ 18944 token = cp_lexer_peek_token (parser->lexer); 18945 18946 /* Check for a bitfield declaration. */ 18947 if (token->type == CPP_COLON 18948 || (token->type == CPP_NAME 18949 && cp_lexer_peek_nth_token (parser->lexer, 2)->type 18950 == CPP_COLON)) 18951 { 18952 tree identifier; 18953 tree width; 18954 18955 /* Get the name of the bitfield. Note that we cannot just 18956 check TOKEN here because it may have been invalidated by 18957 the call to cp_lexer_peek_nth_token above. */ 18958 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) 18959 identifier = cp_parser_identifier (parser); 18960 else 18961 identifier = NULL_TREE; 18962 18963 /* Consume the `:' token. */ 18964 cp_lexer_consume_token (parser->lexer); 18965 /* Get the width of the bitfield. */ 18966 width 18967 = cp_parser_constant_expression (parser, 18968 /*allow_non_constant=*/false, 18969 NULL); 18970 18971 /* Look for attributes that apply to the bitfield. */ 18972 attributes = cp_parser_attributes_opt (parser); 18973 /* Remember which attributes are prefix attributes and 18974 which are not. */ 18975 first_attribute = attributes; 18976 /* Combine the attributes. */ 18977 attributes = chainon (prefix_attributes, attributes); 18978 18979 /* Create the bitfield declaration. */ 18980 decl = grokbitfield (identifier 18981 ? make_id_declarator (NULL_TREE, 18982 identifier, 18983 sfk_none) 18984 : NULL, 18985 &decl_specifiers, 18986 width, 18987 attributes); 18988 } 18989 else 18990 { 18991 cp_declarator *declarator; 18992 tree initializer; 18993 tree asm_specification; 18994 int ctor_dtor_or_conv_p; 18995 18996 /* Parse the declarator. */ 18997 declarator 18998 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 18999 &ctor_dtor_or_conv_p, 19000 /*parenthesized_p=*/NULL, 19001 /*member_p=*/true); 19002 19003 /* If something went wrong parsing the declarator, make sure 19004 that we at least consume some tokens. */ 19005 if (declarator == cp_error_declarator) 19006 { 19007 /* Skip to the end of the statement. */ 19008 cp_parser_skip_to_end_of_statement (parser); 19009 /* If the next token is not a semicolon, that is 19010 probably because we just skipped over the body of 19011 a function. So, we consume a semicolon if 19012 present, but do not issue an error message if it 19013 is not present. */ 19014 if (cp_lexer_next_token_is (parser->lexer, 19015 CPP_SEMICOLON)) 19016 cp_lexer_consume_token (parser->lexer); 19017 goto out; 19018 } 19019 19020 if (declares_class_or_enum & 2) 19021 cp_parser_check_for_definition_in_return_type 19022 (declarator, decl_specifiers.type, 19023 decl_specifiers.type_location); 19024 19025 /* Look for an asm-specification. */ 19026 asm_specification = cp_parser_asm_specification_opt (parser); 19027 /* Look for attributes that apply to the declaration. */ 19028 attributes = cp_parser_attributes_opt (parser); 19029 /* Remember which attributes are prefix attributes and 19030 which are not. */ 19031 first_attribute = attributes; 19032 /* Combine the attributes. */ 19033 attributes = chainon (prefix_attributes, attributes); 19034 19035 /* If it's an `=', then we have a constant-initializer or a 19036 pure-specifier. It is not correct to parse the 19037 initializer before registering the member declaration 19038 since the member declaration should be in scope while 19039 its initializer is processed. However, the rest of the 19040 front end does not yet provide an interface that allows 19041 us to handle this correctly. */ 19042 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 19043 { 19044 /* In [class.mem]: 19045 19046 A pure-specifier shall be used only in the declaration of 19047 a virtual function. 19048 19049 A member-declarator can contain a constant-initializer 19050 only if it declares a static member of integral or 19051 enumeration type. 19052 19053 Therefore, if the DECLARATOR is for a function, we look 19054 for a pure-specifier; otherwise, we look for a 19055 constant-initializer. When we call `grokfield', it will 19056 perform more stringent semantics checks. */ 19057 initializer_token_start = cp_lexer_peek_token (parser->lexer); 19058 if (function_declarator_p (declarator) 19059 || (decl_specifiers.type 19060 && TREE_CODE (decl_specifiers.type) == TYPE_DECL 19061 && (TREE_CODE (TREE_TYPE (decl_specifiers.type)) 19062 == FUNCTION_TYPE))) 19063 initializer = cp_parser_pure_specifier (parser); 19064 else if (decl_specifiers.storage_class != sc_static) 19065 initializer = cp_parser_save_nsdmi (parser); 19066 else if (cxx_dialect >= cxx0x) 19067 { 19068 bool nonconst; 19069 /* Don't require a constant rvalue in C++11, since we 19070 might want a reference constant. We'll enforce 19071 constancy later. */ 19072 cp_lexer_consume_token (parser->lexer); 19073 /* Parse the initializer. */ 19074 initializer = cp_parser_initializer_clause (parser, 19075 &nonconst); 19076 } 19077 else 19078 /* Parse the initializer. */ 19079 initializer = cp_parser_constant_initializer (parser); 19080 } 19081 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE) 19082 && !function_declarator_p (declarator)) 19083 { 19084 bool x; 19085 if (decl_specifiers.storage_class != sc_static) 19086 initializer = cp_parser_save_nsdmi (parser); 19087 else 19088 initializer = cp_parser_initializer (parser, &x, &x); 19089 } 19090 /* Otherwise, there is no initializer. */ 19091 else 19092 initializer = NULL_TREE; 19093 19094 /* See if we are probably looking at a function 19095 definition. We are certainly not looking at a 19096 member-declarator. Calling `grokfield' has 19097 side-effects, so we must not do it unless we are sure 19098 that we are looking at a member-declarator. */ 19099 if (cp_parser_token_starts_function_definition_p 19100 (cp_lexer_peek_token (parser->lexer))) 19101 { 19102 /* The grammar does not allow a pure-specifier to be 19103 used when a member function is defined. (It is 19104 possible that this fact is an oversight in the 19105 standard, since a pure function may be defined 19106 outside of the class-specifier. */ 19107 if (initializer && initializer_token_start) 19108 error_at (initializer_token_start->location, 19109 "pure-specifier on function-definition"); 19110 decl = cp_parser_save_member_function_body (parser, 19111 &decl_specifiers, 19112 declarator, 19113 attributes); 19114 /* If the member was not a friend, declare it here. */ 19115 if (!friend_p) 19116 finish_member_declaration (decl); 19117 /* Peek at the next token. */ 19118 token = cp_lexer_peek_token (parser->lexer); 19119 /* If the next token is a semicolon, consume it. */ 19120 if (token->type == CPP_SEMICOLON) 19121 cp_lexer_consume_token (parser->lexer); 19122 goto out; 19123 } 19124 else 19125 if (declarator->kind == cdk_function) 19126 declarator->id_loc = token->location; 19127 /* Create the declaration. */ 19128 decl = grokfield (declarator, &decl_specifiers, 19129 initializer, /*init_const_expr_p=*/true, 19130 asm_specification, 19131 attributes); 19132 } 19133 19134 /* Reset PREFIX_ATTRIBUTES. */ 19135 while (attributes && TREE_CHAIN (attributes) != first_attribute) 19136 attributes = TREE_CHAIN (attributes); 19137 if (attributes) 19138 TREE_CHAIN (attributes) = NULL_TREE; 19139 19140 /* If there is any qualification still in effect, clear it 19141 now; we will be starting fresh with the next declarator. */ 19142 parser->scope = NULL_TREE; 19143 parser->qualifying_scope = NULL_TREE; 19144 parser->object_scope = NULL_TREE; 19145 /* If it's a `,', then there are more declarators. */ 19146 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 19147 cp_lexer_consume_token (parser->lexer); 19148 /* If the next token isn't a `;', then we have a parse error. */ 19149 else if (cp_lexer_next_token_is_not (parser->lexer, 19150 CPP_SEMICOLON)) 19151 { 19152 /* The next token might be a ways away from where the 19153 actual semicolon is missing. Find the previous token 19154 and use that for our error position. */ 19155 cp_token *token = cp_lexer_previous_token (parser->lexer); 19156 error_at (token->location, 19157 "expected %<;%> at end of member declaration"); 19158 19159 /* Assume that the user meant to provide a semicolon. If 19160 we were to cp_parser_skip_to_end_of_statement, we might 19161 skip to a semicolon inside a member function definition 19162 and issue nonsensical error messages. */ 19163 assume_semicolon = true; 19164 } 19165 19166 if (decl) 19167 { 19168 /* Add DECL to the list of members. */ 19169 if (!friend_p) 19170 finish_member_declaration (decl); 19171 19172 if (TREE_CODE (decl) == FUNCTION_DECL) 19173 cp_parser_save_default_args (parser, decl); 19174 else if (TREE_CODE (decl) == FIELD_DECL 19175 && !DECL_C_BIT_FIELD (decl) 19176 && DECL_INITIAL (decl)) 19177 /* Add DECL to the queue of NSDMI to be parsed later. */ 19178 VEC_safe_push (tree, gc, unparsed_nsdmis, decl); 19179 } 19180 19181 if (assume_semicolon) 19182 goto out; 19183 } 19184 } 19185 19186 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 19187 out: 19188 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 19189 } 19190 19191 /* Parse a pure-specifier. 19192 19193 pure-specifier: 19194 = 0 19195 19196 Returns INTEGER_ZERO_NODE if a pure specifier is found. 19197 Otherwise, ERROR_MARK_NODE is returned. */ 19198 19199 static tree 19200 cp_parser_pure_specifier (cp_parser* parser) 19201 { 19202 cp_token *token; 19203 19204 /* Look for the `=' token. */ 19205 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 19206 return error_mark_node; 19207 /* Look for the `0' token. */ 19208 token = cp_lexer_peek_token (parser->lexer); 19209 19210 if (token->type == CPP_EOF 19211 || token->type == CPP_PRAGMA_EOL) 19212 return error_mark_node; 19213 19214 cp_lexer_consume_token (parser->lexer); 19215 19216 /* Accept = default or = delete in c++0x mode. */ 19217 if (token->keyword == RID_DEFAULT 19218 || token->keyword == RID_DELETE) 19219 { 19220 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED); 19221 return token->u.value; 19222 } 19223 19224 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */ 19225 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO)) 19226 { 19227 cp_parser_error (parser, 19228 "invalid pure specifier (only %<= 0%> is allowed)"); 19229 cp_parser_skip_to_end_of_statement (parser); 19230 return error_mark_node; 19231 } 19232 if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 19233 { 19234 error_at (token->location, "templates may not be %<virtual%>"); 19235 return error_mark_node; 19236 } 19237 19238 return integer_zero_node; 19239 } 19240 19241 /* Parse a constant-initializer. 19242 19243 constant-initializer: 19244 = constant-expression 19245 19246 Returns a representation of the constant-expression. */ 19247 19248 static tree 19249 cp_parser_constant_initializer (cp_parser* parser) 19250 { 19251 /* Look for the `=' token. */ 19252 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 19253 return error_mark_node; 19254 19255 /* It is invalid to write: 19256 19257 struct S { static const int i = { 7 }; }; 19258 19259 */ 19260 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 19261 { 19262 cp_parser_error (parser, 19263 "a brace-enclosed initializer is not allowed here"); 19264 /* Consume the opening brace. */ 19265 cp_lexer_consume_token (parser->lexer); 19266 /* Skip the initializer. */ 19267 cp_parser_skip_to_closing_brace (parser); 19268 /* Look for the trailing `}'. */ 19269 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 19270 19271 return error_mark_node; 19272 } 19273 19274 return cp_parser_constant_expression (parser, 19275 /*allow_non_constant=*/false, 19276 NULL); 19277 } 19278 19279 /* Derived classes [gram.class.derived] */ 19280 19281 /* Parse a base-clause. 19282 19283 base-clause: 19284 : base-specifier-list 19285 19286 base-specifier-list: 19287 base-specifier ... [opt] 19288 base-specifier-list , base-specifier ... [opt] 19289 19290 Returns a TREE_LIST representing the base-classes, in the order in 19291 which they were declared. The representation of each node is as 19292 described by cp_parser_base_specifier. 19293 19294 In the case that no bases are specified, this function will return 19295 NULL_TREE, not ERROR_MARK_NODE. */ 19296 19297 static tree 19298 cp_parser_base_clause (cp_parser* parser) 19299 { 19300 tree bases = NULL_TREE; 19301 19302 /* Look for the `:' that begins the list. */ 19303 cp_parser_require (parser, CPP_COLON, RT_COLON); 19304 19305 /* Scan the base-specifier-list. */ 19306 while (true) 19307 { 19308 cp_token *token; 19309 tree base; 19310 bool pack_expansion_p = false; 19311 19312 /* Look for the base-specifier. */ 19313 base = cp_parser_base_specifier (parser); 19314 /* Look for the (optional) ellipsis. */ 19315 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 19316 { 19317 /* Consume the `...'. */ 19318 cp_lexer_consume_token (parser->lexer); 19319 19320 pack_expansion_p = true; 19321 } 19322 19323 /* Add BASE to the front of the list. */ 19324 if (base && base != error_mark_node) 19325 { 19326 if (pack_expansion_p) 19327 /* Make this a pack expansion type. */ 19328 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base)); 19329 19330 if (!check_for_bare_parameter_packs (TREE_VALUE (base))) 19331 { 19332 TREE_CHAIN (base) = bases; 19333 bases = base; 19334 } 19335 } 19336 /* Peek at the next token. */ 19337 token = cp_lexer_peek_token (parser->lexer); 19338 /* If it's not a comma, then the list is complete. */ 19339 if (token->type != CPP_COMMA) 19340 break; 19341 /* Consume the `,'. */ 19342 cp_lexer_consume_token (parser->lexer); 19343 } 19344 19345 /* PARSER->SCOPE may still be non-NULL at this point, if the last 19346 base class had a qualified name. However, the next name that 19347 appears is certainly not qualified. */ 19348 parser->scope = NULL_TREE; 19349 parser->qualifying_scope = NULL_TREE; 19350 parser->object_scope = NULL_TREE; 19351 19352 return nreverse (bases); 19353 } 19354 19355 /* Parse a base-specifier. 19356 19357 base-specifier: 19358 :: [opt] nested-name-specifier [opt] class-name 19359 virtual access-specifier [opt] :: [opt] nested-name-specifier 19360 [opt] class-name 19361 access-specifier virtual [opt] :: [opt] nested-name-specifier 19362 [opt] class-name 19363 19364 Returns a TREE_LIST. The TREE_PURPOSE will be one of 19365 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to 19366 indicate the specifiers provided. The TREE_VALUE will be a TYPE 19367 (or the ERROR_MARK_NODE) indicating the type that was specified. */ 19368 19369 static tree 19370 cp_parser_base_specifier (cp_parser* parser) 19371 { 19372 cp_token *token; 19373 bool done = false; 19374 bool virtual_p = false; 19375 bool duplicate_virtual_error_issued_p = false; 19376 bool duplicate_access_error_issued_p = false; 19377 bool class_scope_p, template_p; 19378 tree access = access_default_node; 19379 tree type; 19380 19381 /* Process the optional `virtual' and `access-specifier'. */ 19382 while (!done) 19383 { 19384 /* Peek at the next token. */ 19385 token = cp_lexer_peek_token (parser->lexer); 19386 /* Process `virtual'. */ 19387 switch (token->keyword) 19388 { 19389 case RID_VIRTUAL: 19390 /* If `virtual' appears more than once, issue an error. */ 19391 if (virtual_p && !duplicate_virtual_error_issued_p) 19392 { 19393 cp_parser_error (parser, 19394 "%<virtual%> specified more than once in base-specified"); 19395 duplicate_virtual_error_issued_p = true; 19396 } 19397 19398 virtual_p = true; 19399 19400 /* Consume the `virtual' token. */ 19401 cp_lexer_consume_token (parser->lexer); 19402 19403 break; 19404 19405 case RID_PUBLIC: 19406 case RID_PROTECTED: 19407 case RID_PRIVATE: 19408 /* If more than one access specifier appears, issue an 19409 error. */ 19410 if (access != access_default_node 19411 && !duplicate_access_error_issued_p) 19412 { 19413 cp_parser_error (parser, 19414 "more than one access specifier in base-specified"); 19415 duplicate_access_error_issued_p = true; 19416 } 19417 19418 access = ridpointers[(int) token->keyword]; 19419 19420 /* Consume the access-specifier. */ 19421 cp_lexer_consume_token (parser->lexer); 19422 19423 break; 19424 19425 default: 19426 done = true; 19427 break; 19428 } 19429 } 19430 /* It is not uncommon to see programs mechanically, erroneously, use 19431 the 'typename' keyword to denote (dependent) qualified types 19432 as base classes. */ 19433 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) 19434 { 19435 token = cp_lexer_peek_token (parser->lexer); 19436 if (!processing_template_decl) 19437 error_at (token->location, 19438 "keyword %<typename%> not allowed outside of templates"); 19439 else 19440 error_at (token->location, 19441 "keyword %<typename%> not allowed in this context " 19442 "(the base class is implicitly a type)"); 19443 cp_lexer_consume_token (parser->lexer); 19444 } 19445 19446 /* Look for the optional `::' operator. */ 19447 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); 19448 /* Look for the nested-name-specifier. The simplest way to 19449 implement: 19450 19451 [temp.res] 19452 19453 The keyword `typename' is not permitted in a base-specifier or 19454 mem-initializer; in these contexts a qualified name that 19455 depends on a template-parameter is implicitly assumed to be a 19456 type name. 19457 19458 is to pretend that we have seen the `typename' keyword at this 19459 point. */ 19460 cp_parser_nested_name_specifier_opt (parser, 19461 /*typename_keyword_p=*/true, 19462 /*check_dependency_p=*/true, 19463 typename_type, 19464 /*is_declaration=*/true); 19465 /* If the base class is given by a qualified name, assume that names 19466 we see are type names or templates, as appropriate. */ 19467 class_scope_p = (parser->scope && TYPE_P (parser->scope)); 19468 template_p = class_scope_p && cp_parser_optional_template_keyword (parser); 19469 19470 if (!parser->scope 19471 && cp_lexer_next_token_is_decltype (parser->lexer)) 19472 /* DR 950 allows decltype as a base-specifier. */ 19473 type = cp_parser_decltype (parser); 19474 else 19475 { 19476 /* Otherwise, look for the class-name. */ 19477 type = cp_parser_class_name (parser, 19478 class_scope_p, 19479 template_p, 19480 typename_type, 19481 /*check_dependency_p=*/true, 19482 /*class_head_p=*/false, 19483 /*is_declaration=*/true); 19484 type = TREE_TYPE (type); 19485 } 19486 19487 if (type == error_mark_node) 19488 return error_mark_node; 19489 19490 return finish_base_specifier (type, access, virtual_p); 19491 } 19492 19493 /* Exception handling [gram.exception] */ 19494 19495 /* Parse an (optional) noexcept-specification. 19496 19497 noexcept-specification: 19498 noexcept ( constant-expression ) [opt] 19499 19500 If no noexcept-specification is present, returns NULL_TREE. 19501 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any 19502 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if 19503 there are no parentheses. CONSUMED_EXPR will be set accordingly. 19504 Otherwise, returns a noexcept specification unless RETURN_COND is true, 19505 in which case a boolean condition is returned instead. */ 19506 19507 static tree 19508 cp_parser_noexcept_specification_opt (cp_parser* parser, 19509 bool require_constexpr, 19510 bool* consumed_expr, 19511 bool return_cond) 19512 { 19513 cp_token *token; 19514 const char *saved_message; 19515 19516 /* Peek at the next token. */ 19517 token = cp_lexer_peek_token (parser->lexer); 19518 19519 /* Is it a noexcept-specification? */ 19520 if (cp_parser_is_keyword (token, RID_NOEXCEPT)) 19521 { 19522 tree expr; 19523 cp_lexer_consume_token (parser->lexer); 19524 19525 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN) 19526 { 19527 cp_lexer_consume_token (parser->lexer); 19528 19529 if (require_constexpr) 19530 { 19531 /* Types may not be defined in an exception-specification. */ 19532 saved_message = parser->type_definition_forbidden_message; 19533 parser->type_definition_forbidden_message 19534 = G_("types may not be defined in an exception-specification"); 19535 19536 expr = cp_parser_constant_expression (parser, false, NULL); 19537 19538 /* Restore the saved message. */ 19539 parser->type_definition_forbidden_message = saved_message; 19540 } 19541 else 19542 { 19543 expr = cp_parser_expression (parser, false, NULL); 19544 *consumed_expr = true; 19545 } 19546 19547 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19548 } 19549 else 19550 { 19551 expr = boolean_true_node; 19552 if (!require_constexpr) 19553 *consumed_expr = false; 19554 } 19555 19556 /* We cannot build a noexcept-spec right away because this will check 19557 that expr is a constexpr. */ 19558 if (!return_cond) 19559 return build_noexcept_spec (expr, tf_warning_or_error); 19560 else 19561 return expr; 19562 } 19563 else 19564 return NULL_TREE; 19565 } 19566 19567 /* Parse an (optional) exception-specification. 19568 19569 exception-specification: 19570 throw ( type-id-list [opt] ) 19571 19572 Returns a TREE_LIST representing the exception-specification. The 19573 TREE_VALUE of each node is a type. */ 19574 19575 static tree 19576 cp_parser_exception_specification_opt (cp_parser* parser) 19577 { 19578 cp_token *token; 19579 tree type_id_list; 19580 const char *saved_message; 19581 19582 /* Peek at the next token. */ 19583 token = cp_lexer_peek_token (parser->lexer); 19584 19585 /* Is it a noexcept-specification? */ 19586 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL, 19587 false); 19588 if (type_id_list != NULL_TREE) 19589 return type_id_list; 19590 19591 /* If it's not `throw', then there's no exception-specification. */ 19592 if (!cp_parser_is_keyword (token, RID_THROW)) 19593 return NULL_TREE; 19594 19595 #if 0 19596 /* Enable this once a lot of code has transitioned to noexcept? */ 19597 if (cxx_dialect == cxx0x && !in_system_header) 19598 warning (OPT_Wdeprecated, "dynamic exception specifications are " 19599 "deprecated in C++0x; use %<noexcept%> instead"); 19600 #endif 19601 19602 /* Consume the `throw'. */ 19603 cp_lexer_consume_token (parser->lexer); 19604 19605 /* Look for the `('. */ 19606 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 19607 19608 /* Peek at the next token. */ 19609 token = cp_lexer_peek_token (parser->lexer); 19610 /* If it's not a `)', then there is a type-id-list. */ 19611 if (token->type != CPP_CLOSE_PAREN) 19612 { 19613 /* Types may not be defined in an exception-specification. */ 19614 saved_message = parser->type_definition_forbidden_message; 19615 parser->type_definition_forbidden_message 19616 = G_("types may not be defined in an exception-specification"); 19617 /* Parse the type-id-list. */ 19618 type_id_list = cp_parser_type_id_list (parser); 19619 /* Restore the saved message. */ 19620 parser->type_definition_forbidden_message = saved_message; 19621 } 19622 else 19623 type_id_list = empty_except_spec; 19624 19625 /* Look for the `)'. */ 19626 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19627 19628 return type_id_list; 19629 } 19630 19631 /* Parse an (optional) type-id-list. 19632 19633 type-id-list: 19634 type-id ... [opt] 19635 type-id-list , type-id ... [opt] 19636 19637 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, 19638 in the order that the types were presented. */ 19639 19640 static tree 19641 cp_parser_type_id_list (cp_parser* parser) 19642 { 19643 tree types = NULL_TREE; 19644 19645 while (true) 19646 { 19647 cp_token *token; 19648 tree type; 19649 19650 /* Get the next type-id. */ 19651 type = cp_parser_type_id (parser); 19652 /* Parse the optional ellipsis. */ 19653 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 19654 { 19655 /* Consume the `...'. */ 19656 cp_lexer_consume_token (parser->lexer); 19657 19658 /* Turn the type into a pack expansion expression. */ 19659 type = make_pack_expansion (type); 19660 } 19661 /* Add it to the list. */ 19662 types = add_exception_specifier (types, type, /*complain=*/1); 19663 /* Peek at the next token. */ 19664 token = cp_lexer_peek_token (parser->lexer); 19665 /* If it is not a `,', we are done. */ 19666 if (token->type != CPP_COMMA) 19667 break; 19668 /* Consume the `,'. */ 19669 cp_lexer_consume_token (parser->lexer); 19670 } 19671 19672 return nreverse (types); 19673 } 19674 19675 /* Parse a try-block. 19676 19677 try-block: 19678 try compound-statement handler-seq */ 19679 19680 static tree 19681 cp_parser_try_block (cp_parser* parser) 19682 { 19683 tree try_block; 19684 19685 cp_parser_require_keyword (parser, RID_TRY, RT_TRY); 19686 try_block = begin_try_block (); 19687 cp_parser_compound_statement (parser, NULL, true, false); 19688 finish_try_block (try_block); 19689 cp_parser_handler_seq (parser); 19690 finish_handler_sequence (try_block); 19691 19692 return try_block; 19693 } 19694 19695 /* Parse a function-try-block. 19696 19697 function-try-block: 19698 try ctor-initializer [opt] function-body handler-seq */ 19699 19700 static bool 19701 cp_parser_function_try_block (cp_parser* parser) 19702 { 19703 tree compound_stmt; 19704 tree try_block; 19705 bool ctor_initializer_p; 19706 19707 /* Look for the `try' keyword. */ 19708 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY)) 19709 return false; 19710 /* Let the rest of the front end know where we are. */ 19711 try_block = begin_function_try_block (&compound_stmt); 19712 /* Parse the function-body. */ 19713 ctor_initializer_p 19714 = cp_parser_ctor_initializer_opt_and_function_body (parser); 19715 /* We're done with the `try' part. */ 19716 finish_function_try_block (try_block); 19717 /* Parse the handlers. */ 19718 cp_parser_handler_seq (parser); 19719 /* We're done with the handlers. */ 19720 finish_function_handler_sequence (try_block, compound_stmt); 19721 19722 return ctor_initializer_p; 19723 } 19724 19725 /* Parse a handler-seq. 19726 19727 handler-seq: 19728 handler handler-seq [opt] */ 19729 19730 static void 19731 cp_parser_handler_seq (cp_parser* parser) 19732 { 19733 while (true) 19734 { 19735 cp_token *token; 19736 19737 /* Parse the handler. */ 19738 cp_parser_handler (parser); 19739 /* Peek at the next token. */ 19740 token = cp_lexer_peek_token (parser->lexer); 19741 /* If it's not `catch' then there are no more handlers. */ 19742 if (!cp_parser_is_keyword (token, RID_CATCH)) 19743 break; 19744 } 19745 } 19746 19747 /* Parse a handler. 19748 19749 handler: 19750 catch ( exception-declaration ) compound-statement */ 19751 19752 static void 19753 cp_parser_handler (cp_parser* parser) 19754 { 19755 tree handler; 19756 tree declaration; 19757 19758 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH); 19759 handler = begin_handler (); 19760 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 19761 declaration = cp_parser_exception_declaration (parser); 19762 finish_handler_parms (declaration, handler); 19763 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19764 cp_parser_compound_statement (parser, NULL, false, false); 19765 finish_handler (handler); 19766 } 19767 19768 /* Parse an exception-declaration. 19769 19770 exception-declaration: 19771 type-specifier-seq declarator 19772 type-specifier-seq abstract-declarator 19773 type-specifier-seq 19774 ... 19775 19776 Returns a VAR_DECL for the declaration, or NULL_TREE if the 19777 ellipsis variant is used. */ 19778 19779 static tree 19780 cp_parser_exception_declaration (cp_parser* parser) 19781 { 19782 cp_decl_specifier_seq type_specifiers; 19783 cp_declarator *declarator; 19784 const char *saved_message; 19785 19786 /* If it's an ellipsis, it's easy to handle. */ 19787 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 19788 { 19789 /* Consume the `...' token. */ 19790 cp_lexer_consume_token (parser->lexer); 19791 return NULL_TREE; 19792 } 19793 19794 /* Types may not be defined in exception-declarations. */ 19795 saved_message = parser->type_definition_forbidden_message; 19796 parser->type_definition_forbidden_message 19797 = G_("types may not be defined in exception-declarations"); 19798 19799 /* Parse the type-specifier-seq. */ 19800 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true, 19801 /*is_trailing_return=*/false, 19802 &type_specifiers); 19803 /* If it's a `)', then there is no declarator. */ 19804 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) 19805 declarator = NULL; 19806 else 19807 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, 19808 /*ctor_dtor_or_conv_p=*/NULL, 19809 /*parenthesized_p=*/NULL, 19810 /*member_p=*/false); 19811 19812 /* Restore the saved message. */ 19813 parser->type_definition_forbidden_message = saved_message; 19814 19815 if (!type_specifiers.any_specifiers_p) 19816 return error_mark_node; 19817 19818 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL); 19819 } 19820 19821 /* Parse a throw-expression. 19822 19823 throw-expression: 19824 throw assignment-expression [opt] 19825 19826 Returns a THROW_EXPR representing the throw-expression. */ 19827 19828 static tree 19829 cp_parser_throw_expression (cp_parser* parser) 19830 { 19831 tree expression; 19832 cp_token* token; 19833 19834 cp_parser_require_keyword (parser, RID_THROW, RT_THROW); 19835 token = cp_lexer_peek_token (parser->lexer); 19836 /* Figure out whether or not there is an assignment-expression 19837 following the "throw" keyword. */ 19838 if (token->type == CPP_COMMA 19839 || token->type == CPP_SEMICOLON 19840 || token->type == CPP_CLOSE_PAREN 19841 || token->type == CPP_CLOSE_SQUARE 19842 || token->type == CPP_CLOSE_BRACE 19843 || token->type == CPP_COLON) 19844 expression = NULL_TREE; 19845 else 19846 expression = cp_parser_assignment_expression (parser, 19847 /*cast_p=*/false, NULL); 19848 19849 return build_throw (expression); 19850 } 19851 19852 /* GNU Extensions */ 19853 19854 /* Parse an (optional) asm-specification. 19855 19856 asm-specification: 19857 asm ( string-literal ) 19858 19859 If the asm-specification is present, returns a STRING_CST 19860 corresponding to the string-literal. Otherwise, returns 19861 NULL_TREE. */ 19862 19863 static tree 19864 cp_parser_asm_specification_opt (cp_parser* parser) 19865 { 19866 cp_token *token; 19867 tree asm_specification; 19868 19869 /* Peek at the next token. */ 19870 token = cp_lexer_peek_token (parser->lexer); 19871 /* If the next token isn't the `asm' keyword, then there's no 19872 asm-specification. */ 19873 if (!cp_parser_is_keyword (token, RID_ASM)) 19874 return NULL_TREE; 19875 19876 /* Consume the `asm' token. */ 19877 cp_lexer_consume_token (parser->lexer); 19878 /* Look for the `('. */ 19879 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 19880 19881 /* Look for the string-literal. */ 19882 asm_specification = cp_parser_string_literal (parser, false, false); 19883 19884 /* Look for the `)'. */ 19885 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19886 19887 return asm_specification; 19888 } 19889 19890 /* Parse an asm-operand-list. 19891 19892 asm-operand-list: 19893 asm-operand 19894 asm-operand-list , asm-operand 19895 19896 asm-operand: 19897 string-literal ( expression ) 19898 [ string-literal ] string-literal ( expression ) 19899 19900 Returns a TREE_LIST representing the operands. The TREE_VALUE of 19901 each node is the expression. The TREE_PURPOSE is itself a 19902 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed 19903 string-literal (or NULL_TREE if not present) and whose TREE_VALUE 19904 is a STRING_CST for the string literal before the parenthesis. Returns 19905 ERROR_MARK_NODE if any of the operands are invalid. */ 19906 19907 static tree 19908 cp_parser_asm_operand_list (cp_parser* parser) 19909 { 19910 tree asm_operands = NULL_TREE; 19911 bool invalid_operands = false; 19912 19913 while (true) 19914 { 19915 tree string_literal; 19916 tree expression; 19917 tree name; 19918 19919 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 19920 { 19921 /* Consume the `[' token. */ 19922 cp_lexer_consume_token (parser->lexer); 19923 /* Read the operand name. */ 19924 name = cp_parser_identifier (parser); 19925 if (name != error_mark_node) 19926 name = build_string (IDENTIFIER_LENGTH (name), 19927 IDENTIFIER_POINTER (name)); 19928 /* Look for the closing `]'. */ 19929 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 19930 } 19931 else 19932 name = NULL_TREE; 19933 /* Look for the string-literal. */ 19934 string_literal = cp_parser_string_literal (parser, false, false); 19935 19936 /* Look for the `('. */ 19937 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 19938 /* Parse the expression. */ 19939 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); 19940 /* Look for the `)'. */ 19941 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19942 19943 if (name == error_mark_node 19944 || string_literal == error_mark_node 19945 || expression == error_mark_node) 19946 invalid_operands = true; 19947 19948 /* Add this operand to the list. */ 19949 asm_operands = tree_cons (build_tree_list (name, string_literal), 19950 expression, 19951 asm_operands); 19952 /* If the next token is not a `,', there are no more 19953 operands. */ 19954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 19955 break; 19956 /* Consume the `,'. */ 19957 cp_lexer_consume_token (parser->lexer); 19958 } 19959 19960 return invalid_operands ? error_mark_node : nreverse (asm_operands); 19961 } 19962 19963 /* Parse an asm-clobber-list. 19964 19965 asm-clobber-list: 19966 string-literal 19967 asm-clobber-list , string-literal 19968 19969 Returns a TREE_LIST, indicating the clobbers in the order that they 19970 appeared. The TREE_VALUE of each node is a STRING_CST. */ 19971 19972 static tree 19973 cp_parser_asm_clobber_list (cp_parser* parser) 19974 { 19975 tree clobbers = NULL_TREE; 19976 19977 while (true) 19978 { 19979 tree string_literal; 19980 19981 /* Look for the string literal. */ 19982 string_literal = cp_parser_string_literal (parser, false, false); 19983 /* Add it to the list. */ 19984 clobbers = tree_cons (NULL_TREE, string_literal, clobbers); 19985 /* If the next token is not a `,', then the list is 19986 complete. */ 19987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 19988 break; 19989 /* Consume the `,' token. */ 19990 cp_lexer_consume_token (parser->lexer); 19991 } 19992 19993 return clobbers; 19994 } 19995 19996 /* Parse an asm-label-list. 19997 19998 asm-label-list: 19999 identifier 20000 asm-label-list , identifier 20001 20002 Returns a TREE_LIST, indicating the labels in the order that they 20003 appeared. The TREE_VALUE of each node is a label. */ 20004 20005 static tree 20006 cp_parser_asm_label_list (cp_parser* parser) 20007 { 20008 tree labels = NULL_TREE; 20009 20010 while (true) 20011 { 20012 tree identifier, label, name; 20013 20014 /* Look for the identifier. */ 20015 identifier = cp_parser_identifier (parser); 20016 if (!error_operand_p (identifier)) 20017 { 20018 label = lookup_label (identifier); 20019 if (TREE_CODE (label) == LABEL_DECL) 20020 { 20021 TREE_USED (label) = 1; 20022 check_goto (label); 20023 name = build_string (IDENTIFIER_LENGTH (identifier), 20024 IDENTIFIER_POINTER (identifier)); 20025 labels = tree_cons (name, label, labels); 20026 } 20027 } 20028 /* If the next token is not a `,', then the list is 20029 complete. */ 20030 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 20031 break; 20032 /* Consume the `,' token. */ 20033 cp_lexer_consume_token (parser->lexer); 20034 } 20035 20036 return nreverse (labels); 20037 } 20038 20039 /* Parse an (optional) series of attributes. 20040 20041 attributes: 20042 attributes attribute 20043 20044 attribute: 20045 __attribute__ (( attribute-list [opt] )) 20046 20047 The return value is as for cp_parser_attribute_list. */ 20048 20049 static tree 20050 cp_parser_attributes_opt (cp_parser* parser) 20051 { 20052 tree attributes = NULL_TREE; 20053 20054 while (true) 20055 { 20056 cp_token *token; 20057 tree attribute_list; 20058 20059 /* Peek at the next token. */ 20060 token = cp_lexer_peek_token (parser->lexer); 20061 /* If it's not `__attribute__', then we're done. */ 20062 if (token->keyword != RID_ATTRIBUTE) 20063 break; 20064 20065 /* Consume the `__attribute__' keyword. */ 20066 cp_lexer_consume_token (parser->lexer); 20067 /* Look for the two `(' tokens. */ 20068 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 20069 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 20070 20071 /* Peek at the next token. */ 20072 token = cp_lexer_peek_token (parser->lexer); 20073 if (token->type != CPP_CLOSE_PAREN) 20074 /* Parse the attribute-list. */ 20075 attribute_list = cp_parser_attribute_list (parser); 20076 else 20077 /* If the next token is a `)', then there is no attribute 20078 list. */ 20079 attribute_list = NULL; 20080 20081 /* Look for the two `)' tokens. */ 20082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 20083 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 20084 20085 /* Add these new attributes to the list. */ 20086 attributes = chainon (attributes, attribute_list); 20087 } 20088 20089 return attributes; 20090 } 20091 20092 /* Parse an attribute-list. 20093 20094 attribute-list: 20095 attribute 20096 attribute-list , attribute 20097 20098 attribute: 20099 identifier 20100 identifier ( identifier ) 20101 identifier ( identifier , expression-list ) 20102 identifier ( expression-list ) 20103 20104 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds 20105 to an attribute. The TREE_PURPOSE of each node is the identifier 20106 indicating which attribute is in use. The TREE_VALUE represents 20107 the arguments, if any. */ 20108 20109 static tree 20110 cp_parser_attribute_list (cp_parser* parser) 20111 { 20112 tree attribute_list = NULL_TREE; 20113 bool save_translate_strings_p = parser->translate_strings_p; 20114 20115 parser->translate_strings_p = false; 20116 while (true) 20117 { 20118 cp_token *token; 20119 tree identifier; 20120 tree attribute; 20121 20122 /* Look for the identifier. We also allow keywords here; for 20123 example `__attribute__ ((const))' is legal. */ 20124 token = cp_lexer_peek_token (parser->lexer); 20125 if (token->type == CPP_NAME 20126 || token->type == CPP_KEYWORD) 20127 { 20128 tree arguments = NULL_TREE; 20129 20130 /* Consume the token. */ 20131 token = cp_lexer_consume_token (parser->lexer); 20132 20133 /* Save away the identifier that indicates which attribute 20134 this is. */ 20135 identifier = (token->type == CPP_KEYWORD) 20136 /* For keywords, use the canonical spelling, not the 20137 parsed identifier. */ 20138 ? ridpointers[(int) token->keyword] 20139 : token->u.value; 20140 20141 attribute = build_tree_list (identifier, NULL_TREE); 20142 20143 /* Peek at the next token. */ 20144 token = cp_lexer_peek_token (parser->lexer); 20145 /* If it's an `(', then parse the attribute arguments. */ 20146 if (token->type == CPP_OPEN_PAREN) 20147 { 20148 VEC(tree,gc) *vec; 20149 int attr_flag = (attribute_takes_identifier_p (identifier) 20150 ? id_attr : normal_attr); 20151 vec = cp_parser_parenthesized_expression_list 20152 (parser, attr_flag, /*cast_p=*/false, 20153 /*allow_expansion_p=*/false, 20154 /*non_constant_p=*/NULL); 20155 if (vec == NULL) 20156 arguments = error_mark_node; 20157 else 20158 { 20159 arguments = build_tree_list_vec (vec); 20160 release_tree_vector (vec); 20161 } 20162 /* Save the arguments away. */ 20163 TREE_VALUE (attribute) = arguments; 20164 } 20165 20166 if (arguments != error_mark_node) 20167 { 20168 /* Add this attribute to the list. */ 20169 TREE_CHAIN (attribute) = attribute_list; 20170 attribute_list = attribute; 20171 } 20172 20173 token = cp_lexer_peek_token (parser->lexer); 20174 } 20175 /* Now, look for more attributes. If the next token isn't a 20176 `,', we're done. */ 20177 if (token->type != CPP_COMMA) 20178 break; 20179 20180 /* Consume the comma and keep going. */ 20181 cp_lexer_consume_token (parser->lexer); 20182 } 20183 parser->translate_strings_p = save_translate_strings_p; 20184 20185 /* We built up the list in reverse order. */ 20186 return nreverse (attribute_list); 20187 } 20188 20189 /* Parse an optional `__extension__' keyword. Returns TRUE if it is 20190 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the 20191 current value of the PEDANTIC flag, regardless of whether or not 20192 the `__extension__' keyword is present. The caller is responsible 20193 for restoring the value of the PEDANTIC flag. */ 20194 20195 static bool 20196 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) 20197 { 20198 /* Save the old value of the PEDANTIC flag. */ 20199 *saved_pedantic = pedantic; 20200 20201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) 20202 { 20203 /* Consume the `__extension__' token. */ 20204 cp_lexer_consume_token (parser->lexer); 20205 /* We're not being pedantic while the `__extension__' keyword is 20206 in effect. */ 20207 pedantic = 0; 20208 20209 return true; 20210 } 20211 20212 return false; 20213 } 20214 20215 /* Parse a label declaration. 20216 20217 label-declaration: 20218 __label__ label-declarator-seq ; 20219 20220 label-declarator-seq: 20221 identifier , label-declarator-seq 20222 identifier */ 20223 20224 static void 20225 cp_parser_label_declaration (cp_parser* parser) 20226 { 20227 /* Look for the `__label__' keyword. */ 20228 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL); 20229 20230 while (true) 20231 { 20232 tree identifier; 20233 20234 /* Look for an identifier. */ 20235 identifier = cp_parser_identifier (parser); 20236 /* If we failed, stop. */ 20237 if (identifier == error_mark_node) 20238 break; 20239 /* Declare it as a label. */ 20240 finish_label_decl (identifier); 20241 /* If the next token is a `;', stop. */ 20242 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 20243 break; 20244 /* Look for the `,' separating the label declarations. */ 20245 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 20246 } 20247 20248 /* Look for the final `;'. */ 20249 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 20250 } 20251 20252 /* Support Functions */ 20253 20254 /* Looks up NAME in the current scope, as given by PARSER->SCOPE. 20255 NAME should have one of the representations used for an 20256 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE 20257 is returned. If PARSER->SCOPE is a dependent type, then a 20258 SCOPE_REF is returned. 20259 20260 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately 20261 returned; the name was already resolved when the TEMPLATE_ID_EXPR 20262 was formed. Abstractly, such entities should not be passed to this 20263 function, because they do not need to be looked up, but it is 20264 simpler to check for this special case here, rather than at the 20265 call-sites. 20266 20267 In cases not explicitly covered above, this function returns a 20268 DECL, OVERLOAD, or baselink representing the result of the lookup. 20269 If there was no entity with the indicated NAME, the ERROR_MARK_NODE 20270 is returned. 20271 20272 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword 20273 (e.g., "struct") that was used. In that case bindings that do not 20274 refer to types are ignored. 20275 20276 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are 20277 ignored. 20278 20279 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces 20280 are ignored. 20281 20282 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent 20283 types. 20284 20285 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a 20286 TREE_LIST of candidates if name-lookup results in an ambiguity, and 20287 NULL_TREE otherwise. */ 20288 20289 static tree 20290 cp_parser_lookup_name (cp_parser *parser, tree name, 20291 enum tag_types tag_type, 20292 bool is_template, 20293 bool is_namespace, 20294 bool check_dependency, 20295 tree *ambiguous_decls, 20296 location_t name_location) 20297 { 20298 int flags = 0; 20299 tree decl; 20300 tree object_type = parser->context->object_type; 20301 20302 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) 20303 flags |= LOOKUP_COMPLAIN; 20304 20305 /* Assume that the lookup will be unambiguous. */ 20306 if (ambiguous_decls) 20307 *ambiguous_decls = NULL_TREE; 20308 20309 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is 20310 no longer valid. Note that if we are parsing tentatively, and 20311 the parse fails, OBJECT_TYPE will be automatically restored. */ 20312 parser->context->object_type = NULL_TREE; 20313 20314 if (name == error_mark_node) 20315 return error_mark_node; 20316 20317 /* A template-id has already been resolved; there is no lookup to 20318 do. */ 20319 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 20320 return name; 20321 if (BASELINK_P (name)) 20322 { 20323 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name)) 20324 == TEMPLATE_ID_EXPR); 20325 return name; 20326 } 20327 20328 /* A BIT_NOT_EXPR is used to represent a destructor. By this point, 20329 it should already have been checked to make sure that the name 20330 used matches the type being destroyed. */ 20331 if (TREE_CODE (name) == BIT_NOT_EXPR) 20332 { 20333 tree type; 20334 20335 /* Figure out to which type this destructor applies. */ 20336 if (parser->scope) 20337 type = parser->scope; 20338 else if (object_type) 20339 type = object_type; 20340 else 20341 type = current_class_type; 20342 /* If that's not a class type, there is no destructor. */ 20343 if (!type || !CLASS_TYPE_P (type)) 20344 return error_mark_node; 20345 if (CLASSTYPE_LAZY_DESTRUCTOR (type)) 20346 lazily_declare_fn (sfk_destructor, type); 20347 if (!CLASSTYPE_DESTRUCTORS (type)) 20348 return error_mark_node; 20349 /* If it was a class type, return the destructor. */ 20350 return CLASSTYPE_DESTRUCTORS (type); 20351 } 20352 20353 /* By this point, the NAME should be an ordinary identifier. If 20354 the id-expression was a qualified name, the qualifying scope is 20355 stored in PARSER->SCOPE at this point. */ 20356 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 20357 20358 /* Perform the lookup. */ 20359 if (parser->scope) 20360 { 20361 bool dependent_p; 20362 20363 if (parser->scope == error_mark_node) 20364 return error_mark_node; 20365 20366 /* If the SCOPE is dependent, the lookup must be deferred until 20367 the template is instantiated -- unless we are explicitly 20368 looking up names in uninstantiated templates. Even then, we 20369 cannot look up the name if the scope is not a class type; it 20370 might, for example, be a template type parameter. */ 20371 dependent_p = (TYPE_P (parser->scope) 20372 && dependent_scope_p (parser->scope)); 20373 if ((check_dependency || !CLASS_TYPE_P (parser->scope)) 20374 && dependent_p) 20375 /* Defer lookup. */ 20376 decl = error_mark_node; 20377 else 20378 { 20379 tree pushed_scope = NULL_TREE; 20380 20381 /* If PARSER->SCOPE is a dependent type, then it must be a 20382 class type, and we must not be checking dependencies; 20383 otherwise, we would have processed this lookup above. So 20384 that PARSER->SCOPE is not considered a dependent base by 20385 lookup_member, we must enter the scope here. */ 20386 if (dependent_p) 20387 pushed_scope = push_scope (parser->scope); 20388 20389 /* If the PARSER->SCOPE is a template specialization, it 20390 may be instantiated during name lookup. In that case, 20391 errors may be issued. Even if we rollback the current 20392 tentative parse, those errors are valid. */ 20393 decl = lookup_qualified_name (parser->scope, name, 20394 tag_type != none_type, 20395 /*complain=*/true); 20396 20397 /* 3.4.3.1: In a lookup in which the constructor is an acceptable 20398 lookup result and the nested-name-specifier nominates a class C: 20399 * if the name specified after the nested-name-specifier, when 20400 looked up in C, is the injected-class-name of C (Clause 9), or 20401 * if the name specified after the nested-name-specifier is the 20402 same as the identifier or the simple-template-id's template- 20403 name in the last component of the nested-name-specifier, 20404 the name is instead considered to name the constructor of 20405 class C. [ Note: for example, the constructor is not an 20406 acceptable lookup result in an elaborated-type-specifier so 20407 the constructor would not be used in place of the 20408 injected-class-name. --end note ] Such a constructor name 20409 shall be used only in the declarator-id of a declaration that 20410 names a constructor or in a using-declaration. */ 20411 if (tag_type == none_type 20412 && DECL_SELF_REFERENCE_P (decl) 20413 && same_type_p (DECL_CONTEXT (decl), parser->scope)) 20414 decl = lookup_qualified_name (parser->scope, ctor_identifier, 20415 tag_type != none_type, 20416 /*complain=*/true); 20417 20418 /* If we have a single function from a using decl, pull it out. */ 20419 if (TREE_CODE (decl) == OVERLOAD 20420 && !really_overloaded_fn (decl)) 20421 decl = OVL_FUNCTION (decl); 20422 20423 if (pushed_scope) 20424 pop_scope (pushed_scope); 20425 } 20426 20427 /* If the scope is a dependent type and either we deferred lookup or 20428 we did lookup but didn't find the name, rememeber the name. */ 20429 if (decl == error_mark_node && TYPE_P (parser->scope) 20430 && dependent_type_p (parser->scope)) 20431 { 20432 if (tag_type) 20433 { 20434 tree type; 20435 20436 /* The resolution to Core Issue 180 says that `struct 20437 A::B' should be considered a type-name, even if `A' 20438 is dependent. */ 20439 type = make_typename_type (parser->scope, name, tag_type, 20440 /*complain=*/tf_error); 20441 decl = TYPE_NAME (type); 20442 } 20443 else if (is_template 20444 && (cp_parser_next_token_ends_template_argument_p (parser) 20445 || cp_lexer_next_token_is (parser->lexer, 20446 CPP_CLOSE_PAREN))) 20447 decl = make_unbound_class_template (parser->scope, 20448 name, NULL_TREE, 20449 /*complain=*/tf_error); 20450 else 20451 decl = build_qualified_name (/*type=*/NULL_TREE, 20452 parser->scope, name, 20453 is_template); 20454 } 20455 parser->qualifying_scope = parser->scope; 20456 parser->object_scope = NULL_TREE; 20457 } 20458 else if (object_type) 20459 { 20460 tree object_decl = NULL_TREE; 20461 /* Look up the name in the scope of the OBJECT_TYPE, unless the 20462 OBJECT_TYPE is not a class. */ 20463 if (CLASS_TYPE_P (object_type)) 20464 /* If the OBJECT_TYPE is a template specialization, it may 20465 be instantiated during name lookup. In that case, errors 20466 may be issued. Even if we rollback the current tentative 20467 parse, those errors are valid. */ 20468 object_decl = lookup_member (object_type, 20469 name, 20470 /*protect=*/0, 20471 tag_type != none_type, 20472 tf_warning_or_error); 20473 /* Look it up in the enclosing context, too. */ 20474 decl = lookup_name_real (name, tag_type != none_type, 20475 /*nonclass=*/0, 20476 /*block_p=*/true, is_namespace, flags); 20477 parser->object_scope = object_type; 20478 parser->qualifying_scope = NULL_TREE; 20479 if (object_decl) 20480 decl = object_decl; 20481 } 20482 else 20483 { 20484 decl = lookup_name_real (name, tag_type != none_type, 20485 /*nonclass=*/0, 20486 /*block_p=*/true, is_namespace, flags); 20487 parser->qualifying_scope = NULL_TREE; 20488 parser->object_scope = NULL_TREE; 20489 } 20490 20491 /* If the lookup failed, let our caller know. */ 20492 if (!decl || decl == error_mark_node) 20493 return error_mark_node; 20494 20495 /* Pull out the template from an injected-class-name (or multiple). */ 20496 if (is_template) 20497 decl = maybe_get_template_decl_from_type_decl (decl); 20498 20499 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ 20500 if (TREE_CODE (decl) == TREE_LIST) 20501 { 20502 if (ambiguous_decls) 20503 *ambiguous_decls = decl; 20504 /* The error message we have to print is too complicated for 20505 cp_parser_error, so we incorporate its actions directly. */ 20506 if (!cp_parser_simulate_error (parser)) 20507 { 20508 error_at (name_location, "reference to %qD is ambiguous", 20509 name); 20510 print_candidates (decl); 20511 } 20512 return error_mark_node; 20513 } 20514 20515 gcc_assert (DECL_P (decl) 20516 || TREE_CODE (decl) == OVERLOAD 20517 || TREE_CODE (decl) == SCOPE_REF 20518 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE 20519 || BASELINK_P (decl)); 20520 20521 /* If we have resolved the name of a member declaration, check to 20522 see if the declaration is accessible. When the name resolves to 20523 set of overloaded functions, accessibility is checked when 20524 overload resolution is done. 20525 20526 During an explicit instantiation, access is not checked at all, 20527 as per [temp.explicit]. */ 20528 if (DECL_P (decl)) 20529 check_accessibility_of_qualified_id (decl, object_type, parser->scope); 20530 20531 maybe_record_typedef_use (decl); 20532 20533 return decl; 20534 } 20535 20536 /* Like cp_parser_lookup_name, but for use in the typical case where 20537 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE, 20538 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */ 20539 20540 static tree 20541 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location) 20542 { 20543 return cp_parser_lookup_name (parser, name, 20544 none_type, 20545 /*is_template=*/false, 20546 /*is_namespace=*/false, 20547 /*check_dependency=*/true, 20548 /*ambiguous_decls=*/NULL, 20549 location); 20550 } 20551 20552 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in 20553 the current context, return the TYPE_DECL. If TAG_NAME_P is 20554 true, the DECL indicates the class being defined in a class-head, 20555 or declared in an elaborated-type-specifier. 20556 20557 Otherwise, return DECL. */ 20558 20559 static tree 20560 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) 20561 { 20562 /* If the TEMPLATE_DECL is being declared as part of a class-head, 20563 the translation from TEMPLATE_DECL to TYPE_DECL occurs: 20564 20565 struct A { 20566 template <typename T> struct B; 20567 }; 20568 20569 template <typename T> struct A::B {}; 20570 20571 Similarly, in an elaborated-type-specifier: 20572 20573 namespace N { struct X{}; } 20574 20575 struct A { 20576 template <typename T> friend struct N::X; 20577 }; 20578 20579 However, if the DECL refers to a class type, and we are in 20580 the scope of the class, then the name lookup automatically 20581 finds the TYPE_DECL created by build_self_reference rather 20582 than a TEMPLATE_DECL. For example, in: 20583 20584 template <class T> struct S { 20585 S s; 20586 }; 20587 20588 there is no need to handle such case. */ 20589 20590 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) 20591 return DECL_TEMPLATE_RESULT (decl); 20592 20593 return decl; 20594 } 20595 20596 /* If too many, or too few, template-parameter lists apply to the 20597 declarator, issue an error message. Returns TRUE if all went well, 20598 and FALSE otherwise. */ 20599 20600 static bool 20601 cp_parser_check_declarator_template_parameters (cp_parser* parser, 20602 cp_declarator *declarator, 20603 location_t declarator_location) 20604 { 20605 unsigned num_templates; 20606 20607 /* We haven't seen any classes that involve template parameters yet. */ 20608 num_templates = 0; 20609 20610 switch (declarator->kind) 20611 { 20612 case cdk_id: 20613 if (declarator->u.id.qualifying_scope) 20614 { 20615 tree scope; 20616 20617 scope = declarator->u.id.qualifying_scope; 20618 20619 while (scope && CLASS_TYPE_P (scope)) 20620 { 20621 /* You're supposed to have one `template <...>' 20622 for every template class, but you don't need one 20623 for a full specialization. For example: 20624 20625 template <class T> struct S{}; 20626 template <> struct S<int> { void f(); }; 20627 void S<int>::f () {} 20628 20629 is correct; there shouldn't be a `template <>' for 20630 the definition of `S<int>::f'. */ 20631 if (!CLASSTYPE_TEMPLATE_INFO (scope)) 20632 /* If SCOPE does not have template information of any 20633 kind, then it is not a template, nor is it nested 20634 within a template. */ 20635 break; 20636 if (explicit_class_specialization_p (scope)) 20637 break; 20638 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) 20639 ++num_templates; 20640 20641 scope = TYPE_CONTEXT (scope); 20642 } 20643 } 20644 else if (TREE_CODE (declarator->u.id.unqualified_name) 20645 == TEMPLATE_ID_EXPR) 20646 /* If the DECLARATOR has the form `X<y>' then it uses one 20647 additional level of template parameters. */ 20648 ++num_templates; 20649 20650 return cp_parser_check_template_parameters 20651 (parser, num_templates, declarator_location, declarator); 20652 20653 20654 case cdk_function: 20655 case cdk_array: 20656 case cdk_pointer: 20657 case cdk_reference: 20658 case cdk_ptrmem: 20659 return (cp_parser_check_declarator_template_parameters 20660 (parser, declarator->declarator, declarator_location)); 20661 20662 case cdk_error: 20663 return true; 20664 20665 default: 20666 gcc_unreachable (); 20667 } 20668 return false; 20669 } 20670 20671 /* NUM_TEMPLATES were used in the current declaration. If that is 20672 invalid, return FALSE and issue an error messages. Otherwise, 20673 return TRUE. If DECLARATOR is non-NULL, then we are checking a 20674 declarator and we can print more accurate diagnostics. */ 20675 20676 static bool 20677 cp_parser_check_template_parameters (cp_parser* parser, 20678 unsigned num_templates, 20679 location_t location, 20680 cp_declarator *declarator) 20681 { 20682 /* If there are the same number of template classes and parameter 20683 lists, that's OK. */ 20684 if (parser->num_template_parameter_lists == num_templates) 20685 return true; 20686 /* If there are more, but only one more, then we are referring to a 20687 member template. That's OK too. */ 20688 if (parser->num_template_parameter_lists == num_templates + 1) 20689 return true; 20690 /* If there are more template classes than parameter lists, we have 20691 something like: 20692 20693 template <class T> void S<T>::R<T>::f (); */ 20694 if (parser->num_template_parameter_lists < num_templates) 20695 { 20696 if (declarator && !current_function_decl) 20697 error_at (location, "specializing member %<%T::%E%> " 20698 "requires %<template<>%> syntax", 20699 declarator->u.id.qualifying_scope, 20700 declarator->u.id.unqualified_name); 20701 else if (declarator) 20702 error_at (location, "invalid declaration of %<%T::%E%>", 20703 declarator->u.id.qualifying_scope, 20704 declarator->u.id.unqualified_name); 20705 else 20706 error_at (location, "too few template-parameter-lists"); 20707 return false; 20708 } 20709 /* Otherwise, there are too many template parameter lists. We have 20710 something like: 20711 20712 template <class T> template <class U> void S::f(); */ 20713 error_at (location, "too many template-parameter-lists"); 20714 return false; 20715 } 20716 20717 /* Parse an optional `::' token indicating that the following name is 20718 from the global namespace. If so, PARSER->SCOPE is set to the 20719 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, 20720 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. 20721 Returns the new value of PARSER->SCOPE, if the `::' token is 20722 present, and NULL_TREE otherwise. */ 20723 20724 static tree 20725 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) 20726 { 20727 cp_token *token; 20728 20729 /* Peek at the next token. */ 20730 token = cp_lexer_peek_token (parser->lexer); 20731 /* If we're looking at a `::' token then we're starting from the 20732 global namespace, not our current location. */ 20733 if (token->type == CPP_SCOPE) 20734 { 20735 /* Consume the `::' token. */ 20736 cp_lexer_consume_token (parser->lexer); 20737 /* Set the SCOPE so that we know where to start the lookup. */ 20738 parser->scope = global_namespace; 20739 parser->qualifying_scope = global_namespace; 20740 parser->object_scope = NULL_TREE; 20741 20742 return parser->scope; 20743 } 20744 else if (!current_scope_valid_p) 20745 { 20746 parser->scope = NULL_TREE; 20747 parser->qualifying_scope = NULL_TREE; 20748 parser->object_scope = NULL_TREE; 20749 } 20750 20751 return NULL_TREE; 20752 } 20753 20754 /* Returns TRUE if the upcoming token sequence is the start of a 20755 constructor declarator. If FRIEND_P is true, the declarator is 20756 preceded by the `friend' specifier. */ 20757 20758 static bool 20759 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) 20760 { 20761 bool constructor_p; 20762 tree nested_name_specifier; 20763 cp_token *next_token; 20764 20765 /* The common case is that this is not a constructor declarator, so 20766 try to avoid doing lots of work if at all possible. It's not 20767 valid declare a constructor at function scope. */ 20768 if (parser->in_function_body) 20769 return false; 20770 /* And only certain tokens can begin a constructor declarator. */ 20771 next_token = cp_lexer_peek_token (parser->lexer); 20772 if (next_token->type != CPP_NAME 20773 && next_token->type != CPP_SCOPE 20774 && next_token->type != CPP_NESTED_NAME_SPECIFIER 20775 && next_token->type != CPP_TEMPLATE_ID) 20776 return false; 20777 20778 /* Parse tentatively; we are going to roll back all of the tokens 20779 consumed here. */ 20780 cp_parser_parse_tentatively (parser); 20781 /* Assume that we are looking at a constructor declarator. */ 20782 constructor_p = true; 20783 20784 /* Look for the optional `::' operator. */ 20785 cp_parser_global_scope_opt (parser, 20786 /*current_scope_valid_p=*/false); 20787 /* Look for the nested-name-specifier. */ 20788 nested_name_specifier 20789 = (cp_parser_nested_name_specifier_opt (parser, 20790 /*typename_keyword_p=*/false, 20791 /*check_dependency_p=*/false, 20792 /*type_p=*/false, 20793 /*is_declaration=*/false)); 20794 /* Outside of a class-specifier, there must be a 20795 nested-name-specifier. */ 20796 if (!nested_name_specifier && 20797 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) 20798 || friend_p)) 20799 constructor_p = false; 20800 else if (nested_name_specifier == error_mark_node) 20801 constructor_p = false; 20802 20803 /* If we have a class scope, this is easy; DR 147 says that S::S always 20804 names the constructor, and no other qualified name could. */ 20805 if (constructor_p && nested_name_specifier 20806 && CLASS_TYPE_P (nested_name_specifier)) 20807 { 20808 tree id = cp_parser_unqualified_id (parser, 20809 /*template_keyword_p=*/false, 20810 /*check_dependency_p=*/false, 20811 /*declarator_p=*/true, 20812 /*optional_p=*/false); 20813 if (is_overloaded_fn (id)) 20814 id = DECL_NAME (get_first_fn (id)); 20815 if (!constructor_name_p (id, nested_name_specifier)) 20816 constructor_p = false; 20817 } 20818 /* If we still think that this might be a constructor-declarator, 20819 look for a class-name. */ 20820 else if (constructor_p) 20821 { 20822 /* If we have: 20823 20824 template <typename T> struct S { 20825 S(); 20826 }; 20827 20828 we must recognize that the nested `S' names a class. */ 20829 tree type_decl; 20830 type_decl = cp_parser_class_name (parser, 20831 /*typename_keyword_p=*/false, 20832 /*template_keyword_p=*/false, 20833 none_type, 20834 /*check_dependency_p=*/false, 20835 /*class_head_p=*/false, 20836 /*is_declaration=*/false); 20837 /* If there was no class-name, then this is not a constructor. */ 20838 constructor_p = !cp_parser_error_occurred (parser); 20839 20840 /* If we're still considering a constructor, we have to see a `(', 20841 to begin the parameter-declaration-clause, followed by either a 20842 `)', an `...', or a decl-specifier. We need to check for a 20843 type-specifier to avoid being fooled into thinking that: 20844 20845 S (f) (int); 20846 20847 is a constructor. (It is actually a function named `f' that 20848 takes one parameter (of type `int') and returns a value of type 20849 `S'. */ 20850 if (constructor_p 20851 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 20852 constructor_p = false; 20853 20854 if (constructor_p 20855 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) 20856 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) 20857 /* A parameter declaration begins with a decl-specifier, 20858 which is either the "attribute" keyword, a storage class 20859 specifier, or (usually) a type-specifier. */ 20860 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)) 20861 { 20862 tree type; 20863 tree pushed_scope = NULL_TREE; 20864 unsigned saved_num_template_parameter_lists; 20865 20866 /* Names appearing in the type-specifier should be looked up 20867 in the scope of the class. */ 20868 if (current_class_type) 20869 type = NULL_TREE; 20870 else 20871 { 20872 type = TREE_TYPE (type_decl); 20873 if (TREE_CODE (type) == TYPENAME_TYPE) 20874 { 20875 type = resolve_typename_type (type, 20876 /*only_current_p=*/false); 20877 if (TREE_CODE (type) == TYPENAME_TYPE) 20878 { 20879 cp_parser_abort_tentative_parse (parser); 20880 return false; 20881 } 20882 } 20883 pushed_scope = push_scope (type); 20884 } 20885 20886 /* Inside the constructor parameter list, surrounding 20887 template-parameter-lists do not apply. */ 20888 saved_num_template_parameter_lists 20889 = parser->num_template_parameter_lists; 20890 parser->num_template_parameter_lists = 0; 20891 20892 /* Look for the type-specifier. */ 20893 cp_parser_type_specifier (parser, 20894 CP_PARSER_FLAGS_NONE, 20895 /*decl_specs=*/NULL, 20896 /*is_declarator=*/true, 20897 /*declares_class_or_enum=*/NULL, 20898 /*is_cv_qualifier=*/NULL); 20899 20900 parser->num_template_parameter_lists 20901 = saved_num_template_parameter_lists; 20902 20903 /* Leave the scope of the class. */ 20904 if (pushed_scope) 20905 pop_scope (pushed_scope); 20906 20907 constructor_p = !cp_parser_error_occurred (parser); 20908 } 20909 } 20910 20911 /* We did not really want to consume any tokens. */ 20912 cp_parser_abort_tentative_parse (parser); 20913 20914 return constructor_p; 20915 } 20916 20917 /* Parse the definition of the function given by the DECL_SPECIFIERS, 20918 ATTRIBUTES, and DECLARATOR. The access checks have been deferred; 20919 they must be performed once we are in the scope of the function. 20920 20921 Returns the function defined. */ 20922 20923 static tree 20924 cp_parser_function_definition_from_specifiers_and_declarator 20925 (cp_parser* parser, 20926 cp_decl_specifier_seq *decl_specifiers, 20927 tree attributes, 20928 const cp_declarator *declarator) 20929 { 20930 tree fn; 20931 bool success_p; 20932 20933 /* Begin the function-definition. */ 20934 success_p = start_function (decl_specifiers, declarator, attributes); 20935 20936 /* The things we're about to see are not directly qualified by any 20937 template headers we've seen thus far. */ 20938 reset_specialization (); 20939 20940 /* If there were names looked up in the decl-specifier-seq that we 20941 did not check, check them now. We must wait until we are in the 20942 scope of the function to perform the checks, since the function 20943 might be a friend. */ 20944 perform_deferred_access_checks (); 20945 20946 if (!success_p) 20947 { 20948 /* Skip the entire function. */ 20949 cp_parser_skip_to_end_of_block_or_statement (parser); 20950 fn = error_mark_node; 20951 } 20952 else if (DECL_INITIAL (current_function_decl) != error_mark_node) 20953 { 20954 /* Seen already, skip it. An error message has already been output. */ 20955 cp_parser_skip_to_end_of_block_or_statement (parser); 20956 fn = current_function_decl; 20957 current_function_decl = NULL_TREE; 20958 /* If this is a function from a class, pop the nested class. */ 20959 if (current_class_name) 20960 pop_nested_class (); 20961 } 20962 else 20963 { 20964 timevar_id_t tv; 20965 if (DECL_DECLARED_INLINE_P (current_function_decl)) 20966 tv = TV_PARSE_INLINE; 20967 else 20968 tv = TV_PARSE_FUNC; 20969 timevar_push (tv); 20970 fn = cp_parser_function_definition_after_declarator (parser, 20971 /*inline_p=*/false); 20972 timevar_pop (tv); 20973 } 20974 20975 return fn; 20976 } 20977 20978 /* Parse the part of a function-definition that follows the 20979 declarator. INLINE_P is TRUE iff this function is an inline 20980 function defined within a class-specifier. 20981 20982 Returns the function defined. */ 20983 20984 static tree 20985 cp_parser_function_definition_after_declarator (cp_parser* parser, 20986 bool inline_p) 20987 { 20988 tree fn; 20989 bool ctor_initializer_p = false; 20990 bool saved_in_unbraced_linkage_specification_p; 20991 bool saved_in_function_body; 20992 unsigned saved_num_template_parameter_lists; 20993 cp_token *token; 20994 20995 saved_in_function_body = parser->in_function_body; 20996 parser->in_function_body = true; 20997 /* If the next token is `return', then the code may be trying to 20998 make use of the "named return value" extension that G++ used to 20999 support. */ 21000 token = cp_lexer_peek_token (parser->lexer); 21001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) 21002 { 21003 /* Consume the `return' keyword. */ 21004 cp_lexer_consume_token (parser->lexer); 21005 /* Look for the identifier that indicates what value is to be 21006 returned. */ 21007 cp_parser_identifier (parser); 21008 /* Issue an error message. */ 21009 error_at (token->location, 21010 "named return values are no longer supported"); 21011 /* Skip tokens until we reach the start of the function body. */ 21012 while (true) 21013 { 21014 cp_token *token = cp_lexer_peek_token (parser->lexer); 21015 if (token->type == CPP_OPEN_BRACE 21016 || token->type == CPP_EOF 21017 || token->type == CPP_PRAGMA_EOL) 21018 break; 21019 cp_lexer_consume_token (parser->lexer); 21020 } 21021 } 21022 /* The `extern' in `extern "C" void f () { ... }' does not apply to 21023 anything declared inside `f'. */ 21024 saved_in_unbraced_linkage_specification_p 21025 = parser->in_unbraced_linkage_specification_p; 21026 parser->in_unbraced_linkage_specification_p = false; 21027 /* Inside the function, surrounding template-parameter-lists do not 21028 apply. */ 21029 saved_num_template_parameter_lists 21030 = parser->num_template_parameter_lists; 21031 parser->num_template_parameter_lists = 0; 21032 21033 start_lambda_scope (current_function_decl); 21034 21035 /* If the next token is `try', `__transaction_atomic', or 21036 `__transaction_relaxed`, then we are looking at either function-try-block 21037 or function-transaction-block. Note that all of these include the 21038 function-body. */ 21039 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC)) 21040 ctor_initializer_p = cp_parser_function_transaction (parser, 21041 RID_TRANSACTION_ATOMIC); 21042 else if (cp_lexer_next_token_is_keyword (parser->lexer, 21043 RID_TRANSACTION_RELAXED)) 21044 ctor_initializer_p = cp_parser_function_transaction (parser, 21045 RID_TRANSACTION_RELAXED); 21046 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) 21047 ctor_initializer_p = cp_parser_function_try_block (parser); 21048 else 21049 ctor_initializer_p 21050 = cp_parser_ctor_initializer_opt_and_function_body (parser); 21051 21052 finish_lambda_scope (); 21053 21054 /* Finish the function. */ 21055 fn = finish_function ((ctor_initializer_p ? 1 : 0) | 21056 (inline_p ? 2 : 0)); 21057 /* Generate code for it, if necessary. */ 21058 expand_or_defer_fn (fn); 21059 /* Restore the saved values. */ 21060 parser->in_unbraced_linkage_specification_p 21061 = saved_in_unbraced_linkage_specification_p; 21062 parser->num_template_parameter_lists 21063 = saved_num_template_parameter_lists; 21064 parser->in_function_body = saved_in_function_body; 21065 21066 return fn; 21067 } 21068 21069 /* Parse a template-declaration, assuming that the `export' (and 21070 `extern') keywords, if present, has already been scanned. MEMBER_P 21071 is as for cp_parser_template_declaration. */ 21072 21073 static void 21074 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) 21075 { 21076 tree decl = NULL_TREE; 21077 VEC (deferred_access_check,gc) *checks; 21078 tree parameter_list; 21079 bool friend_p = false; 21080 bool need_lang_pop; 21081 cp_token *token; 21082 21083 /* Look for the `template' keyword. */ 21084 token = cp_lexer_peek_token (parser->lexer); 21085 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE)) 21086 return; 21087 21088 /* And the `<'. */ 21089 if (!cp_parser_require (parser, CPP_LESS, RT_LESS)) 21090 return; 21091 if (at_class_scope_p () && current_function_decl) 21092 { 21093 /* 14.5.2.2 [temp.mem] 21094 21095 A local class shall not have member templates. */ 21096 error_at (token->location, 21097 "invalid declaration of member template in local class"); 21098 cp_parser_skip_to_end_of_block_or_statement (parser); 21099 return; 21100 } 21101 /* [temp] 21102 21103 A template ... shall not have C linkage. */ 21104 if (current_lang_name == lang_name_c) 21105 { 21106 error_at (token->location, "template with C linkage"); 21107 /* Give it C++ linkage to avoid confusing other parts of the 21108 front end. */ 21109 push_lang_context (lang_name_cplusplus); 21110 need_lang_pop = true; 21111 } 21112 else 21113 need_lang_pop = false; 21114 21115 /* We cannot perform access checks on the template parameter 21116 declarations until we know what is being declared, just as we 21117 cannot check the decl-specifier list. */ 21118 push_deferring_access_checks (dk_deferred); 21119 21120 /* If the next token is `>', then we have an invalid 21121 specialization. Rather than complain about an invalid template 21122 parameter, issue an error message here. */ 21123 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) 21124 { 21125 cp_parser_error (parser, "invalid explicit specialization"); 21126 begin_specialization (); 21127 parameter_list = NULL_TREE; 21128 } 21129 else 21130 { 21131 /* Parse the template parameters. */ 21132 parameter_list = cp_parser_template_parameter_list (parser); 21133 } 21134 21135 /* Get the deferred access checks from the parameter list. These 21136 will be checked once we know what is being declared, as for a 21137 member template the checks must be performed in the scope of the 21138 class containing the member. */ 21139 checks = get_deferred_access_checks (); 21140 21141 /* Look for the `>'. */ 21142 cp_parser_skip_to_end_of_template_parameter_list (parser); 21143 /* We just processed one more parameter list. */ 21144 ++parser->num_template_parameter_lists; 21145 /* If the next token is `template', there are more template 21146 parameters. */ 21147 if (cp_lexer_next_token_is_keyword (parser->lexer, 21148 RID_TEMPLATE)) 21149 cp_parser_template_declaration_after_export (parser, member_p); 21150 else if (cxx_dialect >= cxx0x 21151 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) 21152 decl = cp_parser_alias_declaration (parser); 21153 else 21154 { 21155 /* There are no access checks when parsing a template, as we do not 21156 know if a specialization will be a friend. */ 21157 push_deferring_access_checks (dk_no_check); 21158 token = cp_lexer_peek_token (parser->lexer); 21159 decl = cp_parser_single_declaration (parser, 21160 checks, 21161 member_p, 21162 /*explicit_specialization_p=*/false, 21163 &friend_p); 21164 pop_deferring_access_checks (); 21165 21166 /* If this is a member template declaration, let the front 21167 end know. */ 21168 if (member_p && !friend_p && decl) 21169 { 21170 if (TREE_CODE (decl) == TYPE_DECL) 21171 cp_parser_check_access_in_redeclaration (decl, token->location); 21172 21173 decl = finish_member_template_decl (decl); 21174 } 21175 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) 21176 make_friend_class (current_class_type, TREE_TYPE (decl), 21177 /*complain=*/true); 21178 } 21179 /* We are done with the current parameter list. */ 21180 --parser->num_template_parameter_lists; 21181 21182 pop_deferring_access_checks (); 21183 21184 /* Finish up. */ 21185 finish_template_decl (parameter_list); 21186 21187 /* Check the template arguments for a literal operator template. */ 21188 if (decl 21189 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl)) 21190 && UDLIT_OPER_P (DECL_NAME (decl))) 21191 { 21192 bool ok = true; 21193 if (parameter_list == NULL_TREE) 21194 ok = false; 21195 else 21196 { 21197 int num_parms = TREE_VEC_LENGTH (parameter_list); 21198 if (num_parms != 1) 21199 ok = false; 21200 else 21201 { 21202 tree parm_list = TREE_VEC_ELT (parameter_list, 0); 21203 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list); 21204 if (TREE_TYPE (parm) != char_type_node 21205 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) 21206 ok = false; 21207 } 21208 } 21209 if (!ok) 21210 error ("literal operator template %qD has invalid parameter list." 21211 " Expected non-type template argument pack <char...>", 21212 decl); 21213 } 21214 /* Register member declarations. */ 21215 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) 21216 finish_member_declaration (decl); 21217 /* For the erroneous case of a template with C linkage, we pushed an 21218 implicit C++ linkage scope; exit that scope now. */ 21219 if (need_lang_pop) 21220 pop_lang_context (); 21221 /* If DECL is a function template, we must return to parse it later. 21222 (Even though there is no definition, there might be default 21223 arguments that need handling.) */ 21224 if (member_p && decl 21225 && (TREE_CODE (decl) == FUNCTION_DECL 21226 || DECL_FUNCTION_TEMPLATE_P (decl))) 21227 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl); 21228 } 21229 21230 /* Perform the deferred access checks from a template-parameter-list. 21231 CHECKS is a TREE_LIST of access checks, as returned by 21232 get_deferred_access_checks. */ 21233 21234 static void 21235 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks) 21236 { 21237 ++processing_template_parmlist; 21238 perform_access_checks (checks); 21239 --processing_template_parmlist; 21240 } 21241 21242 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or 21243 `function-definition' sequence. MEMBER_P is true, this declaration 21244 appears in a class scope. 21245 21246 Returns the DECL for the declared entity. If FRIEND_P is non-NULL, 21247 *FRIEND_P is set to TRUE iff the declaration is a friend. */ 21248 21249 static tree 21250 cp_parser_single_declaration (cp_parser* parser, 21251 VEC (deferred_access_check,gc)* checks, 21252 bool member_p, 21253 bool explicit_specialization_p, 21254 bool* friend_p) 21255 { 21256 int declares_class_or_enum; 21257 tree decl = NULL_TREE; 21258 cp_decl_specifier_seq decl_specifiers; 21259 bool function_definition_p = false; 21260 cp_token *decl_spec_token_start; 21261 21262 /* This function is only used when processing a template 21263 declaration. */ 21264 gcc_assert (innermost_scope_kind () == sk_template_parms 21265 || innermost_scope_kind () == sk_template_spec); 21266 21267 /* Defer access checks until we know what is being declared. */ 21268 push_deferring_access_checks (dk_deferred); 21269 21270 /* Try the `decl-specifier-seq [opt] init-declarator [opt]' 21271 alternative. */ 21272 decl_spec_token_start = cp_lexer_peek_token (parser->lexer); 21273 cp_parser_decl_specifier_seq (parser, 21274 CP_PARSER_FLAGS_OPTIONAL, 21275 &decl_specifiers, 21276 &declares_class_or_enum); 21277 if (friend_p) 21278 *friend_p = cp_parser_friend_p (&decl_specifiers); 21279 21280 /* There are no template typedefs. */ 21281 if (decl_specifiers.specs[(int) ds_typedef]) 21282 { 21283 error_at (decl_spec_token_start->location, 21284 "template declaration of %<typedef%>"); 21285 decl = error_mark_node; 21286 } 21287 21288 /* Gather up the access checks that occurred the 21289 decl-specifier-seq. */ 21290 stop_deferring_access_checks (); 21291 21292 /* Check for the declaration of a template class. */ 21293 if (declares_class_or_enum) 21294 { 21295 if (cp_parser_declares_only_class_p (parser)) 21296 { 21297 decl = shadow_tag (&decl_specifiers); 21298 21299 /* In this case: 21300 21301 struct C { 21302 friend template <typename T> struct A<T>::B; 21303 }; 21304 21305 A<T>::B will be represented by a TYPENAME_TYPE, and 21306 therefore not recognized by shadow_tag. */ 21307 if (friend_p && *friend_p 21308 && !decl 21309 && decl_specifiers.type 21310 && TYPE_P (decl_specifiers.type)) 21311 decl = decl_specifiers.type; 21312 21313 if (decl && decl != error_mark_node) 21314 decl = TYPE_NAME (decl); 21315 else 21316 decl = error_mark_node; 21317 21318 /* Perform access checks for template parameters. */ 21319 cp_parser_perform_template_parameter_access_checks (checks); 21320 } 21321 } 21322 21323 /* Complain about missing 'typename' or other invalid type names. */ 21324 if (!decl_specifiers.any_type_specifiers_p 21325 && cp_parser_parse_and_diagnose_invalid_type_name (parser)) 21326 { 21327 /* cp_parser_parse_and_diagnose_invalid_type_name calls 21328 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse 21329 the rest of this declaration. */ 21330 decl = error_mark_node; 21331 goto out; 21332 } 21333 21334 /* If it's not a template class, try for a template function. If 21335 the next token is a `;', then this declaration does not declare 21336 anything. But, if there were errors in the decl-specifiers, then 21337 the error might well have come from an attempted class-specifier. 21338 In that case, there's no need to warn about a missing declarator. */ 21339 if (!decl 21340 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) 21341 || decl_specifiers.type != error_mark_node)) 21342 { 21343 decl = cp_parser_init_declarator (parser, 21344 &decl_specifiers, 21345 checks, 21346 /*function_definition_allowed_p=*/true, 21347 member_p, 21348 declares_class_or_enum, 21349 &function_definition_p, 21350 NULL); 21351 21352 /* 7.1.1-1 [dcl.stc] 21353 21354 A storage-class-specifier shall not be specified in an explicit 21355 specialization... */ 21356 if (decl 21357 && explicit_specialization_p 21358 && decl_specifiers.storage_class != sc_none) 21359 { 21360 error_at (decl_spec_token_start->location, 21361 "explicit template specialization cannot have a storage class"); 21362 decl = error_mark_node; 21363 } 21364 } 21365 21366 /* Look for a trailing `;' after the declaration. */ 21367 if (!function_definition_p 21368 && (decl == error_mark_node 21369 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))) 21370 cp_parser_skip_to_end_of_block_or_statement (parser); 21371 21372 out: 21373 pop_deferring_access_checks (); 21374 21375 /* Clear any current qualification; whatever comes next is the start 21376 of something new. */ 21377 parser->scope = NULL_TREE; 21378 parser->qualifying_scope = NULL_TREE; 21379 parser->object_scope = NULL_TREE; 21380 21381 return decl; 21382 } 21383 21384 /* Parse a cast-expression that is not the operand of a unary "&". */ 21385 21386 static tree 21387 cp_parser_simple_cast_expression (cp_parser *parser) 21388 { 21389 return cp_parser_cast_expression (parser, /*address_p=*/false, 21390 /*cast_p=*/false, NULL); 21391 } 21392 21393 /* Parse a functional cast to TYPE. Returns an expression 21394 representing the cast. */ 21395 21396 static tree 21397 cp_parser_functional_cast (cp_parser* parser, tree type) 21398 { 21399 VEC(tree,gc) *vec; 21400 tree expression_list; 21401 tree cast; 21402 bool nonconst_p; 21403 21404 if (!type) 21405 type = error_mark_node; 21406 21407 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 21408 { 21409 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 21410 expression_list = cp_parser_braced_list (parser, &nonconst_p); 21411 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1; 21412 if (TREE_CODE (type) == TYPE_DECL) 21413 type = TREE_TYPE (type); 21414 return finish_compound_literal (type, expression_list, 21415 tf_warning_or_error); 21416 } 21417 21418 21419 vec = cp_parser_parenthesized_expression_list (parser, non_attr, 21420 /*cast_p=*/true, 21421 /*allow_expansion_p=*/true, 21422 /*non_constant_p=*/NULL); 21423 if (vec == NULL) 21424 expression_list = error_mark_node; 21425 else 21426 { 21427 expression_list = build_tree_list_vec (vec); 21428 release_tree_vector (vec); 21429 } 21430 21431 cast = build_functional_cast (type, expression_list, 21432 tf_warning_or_error); 21433 /* [expr.const]/1: In an integral constant expression "only type 21434 conversions to integral or enumeration type can be used". */ 21435 if (TREE_CODE (type) == TYPE_DECL) 21436 type = TREE_TYPE (type); 21437 if (cast != error_mark_node 21438 && !cast_valid_in_integral_constant_expression_p (type) 21439 && cp_parser_non_integral_constant_expression (parser, 21440 NIC_CONSTRUCTOR)) 21441 return error_mark_node; 21442 return cast; 21443 } 21444 21445 /* Save the tokens that make up the body of a member function defined 21446 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have 21447 already been parsed. The ATTRIBUTES are any GNU "__attribute__" 21448 specifiers applied to the declaration. Returns the FUNCTION_DECL 21449 for the member function. */ 21450 21451 static tree 21452 cp_parser_save_member_function_body (cp_parser* parser, 21453 cp_decl_specifier_seq *decl_specifiers, 21454 cp_declarator *declarator, 21455 tree attributes) 21456 { 21457 cp_token *first; 21458 cp_token *last; 21459 tree fn; 21460 21461 /* Create the FUNCTION_DECL. */ 21462 fn = grokmethod (decl_specifiers, declarator, attributes); 21463 /* If something went badly wrong, bail out now. */ 21464 if (fn == error_mark_node) 21465 { 21466 /* If there's a function-body, skip it. */ 21467 if (cp_parser_token_starts_function_definition_p 21468 (cp_lexer_peek_token (parser->lexer))) 21469 cp_parser_skip_to_end_of_block_or_statement (parser); 21470 return error_mark_node; 21471 } 21472 21473 /* Remember it, if there default args to post process. */ 21474 cp_parser_save_default_args (parser, fn); 21475 21476 /* Save away the tokens that make up the body of the 21477 function. */ 21478 first = parser->lexer->next_token; 21479 /* We can have braced-init-list mem-initializers before the fn body. */ 21480 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 21481 { 21482 cp_lexer_consume_token (parser->lexer); 21483 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) 21484 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY)) 21485 { 21486 /* cache_group will stop after an un-nested { } pair, too. */ 21487 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0)) 21488 break; 21489 21490 /* variadic mem-inits have ... after the ')'. */ 21491 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 21492 cp_lexer_consume_token (parser->lexer); 21493 } 21494 } 21495 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); 21496 /* Handle function try blocks. */ 21497 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) 21498 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); 21499 last = parser->lexer->next_token; 21500 21501 /* Save away the inline definition; we will process it when the 21502 class is complete. */ 21503 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last); 21504 DECL_PENDING_INLINE_P (fn) = 1; 21505 21506 /* We need to know that this was defined in the class, so that 21507 friend templates are handled correctly. */ 21508 DECL_INITIALIZED_IN_CLASS_P (fn) = 1; 21509 21510 /* Add FN to the queue of functions to be parsed later. */ 21511 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn); 21512 21513 return fn; 21514 } 21515 21516 /* Save the tokens that make up the in-class initializer for a non-static 21517 data member. Returns a DEFAULT_ARG. */ 21518 21519 static tree 21520 cp_parser_save_nsdmi (cp_parser* parser) 21521 { 21522 return cp_parser_cache_defarg (parser, /*nsdmi=*/true); 21523 } 21524 21525 /* Parse a template-argument-list, as well as the trailing ">" (but 21526 not the opening "<"). See cp_parser_template_argument_list for the 21527 return value. */ 21528 21529 static tree 21530 cp_parser_enclosed_template_argument_list (cp_parser* parser) 21531 { 21532 tree arguments; 21533 tree saved_scope; 21534 tree saved_qualifying_scope; 21535 tree saved_object_scope; 21536 bool saved_greater_than_is_operator_p; 21537 int saved_unevaluated_operand; 21538 int saved_inhibit_evaluation_warnings; 21539 21540 /* [temp.names] 21541 21542 When parsing a template-id, the first non-nested `>' is taken as 21543 the end of the template-argument-list rather than a greater-than 21544 operator. */ 21545 saved_greater_than_is_operator_p 21546 = parser->greater_than_is_operator_p; 21547 parser->greater_than_is_operator_p = false; 21548 /* Parsing the argument list may modify SCOPE, so we save it 21549 here. */ 21550 saved_scope = parser->scope; 21551 saved_qualifying_scope = parser->qualifying_scope; 21552 saved_object_scope = parser->object_scope; 21553 /* We need to evaluate the template arguments, even though this 21554 template-id may be nested within a "sizeof". */ 21555 saved_unevaluated_operand = cp_unevaluated_operand; 21556 cp_unevaluated_operand = 0; 21557 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; 21558 c_inhibit_evaluation_warnings = 0; 21559 /* Parse the template-argument-list itself. */ 21560 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER) 21561 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) 21562 arguments = NULL_TREE; 21563 else 21564 arguments = cp_parser_template_argument_list (parser); 21565 /* Look for the `>' that ends the template-argument-list. If we find 21566 a '>>' instead, it's probably just a typo. */ 21567 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) 21568 { 21569 if (cxx_dialect != cxx98) 21570 { 21571 /* In C++0x, a `>>' in a template argument list or cast 21572 expression is considered to be two separate `>' 21573 tokens. So, change the current token to a `>', but don't 21574 consume it: it will be consumed later when the outer 21575 template argument list (or cast expression) is parsed. 21576 Note that this replacement of `>' for `>>' is necessary 21577 even if we are parsing tentatively: in the tentative 21578 case, after calling 21579 cp_parser_enclosed_template_argument_list we will always 21580 throw away all of the template arguments and the first 21581 closing `>', either because the template argument list 21582 was erroneous or because we are replacing those tokens 21583 with a CPP_TEMPLATE_ID token. The second `>' (which will 21584 not have been thrown away) is needed either to close an 21585 outer template argument list or to complete a new-style 21586 cast. */ 21587 cp_token *token = cp_lexer_peek_token (parser->lexer); 21588 token->type = CPP_GREATER; 21589 } 21590 else if (!saved_greater_than_is_operator_p) 21591 { 21592 /* If we're in a nested template argument list, the '>>' has 21593 to be a typo for '> >'. We emit the error message, but we 21594 continue parsing and we push a '>' as next token, so that 21595 the argument list will be parsed correctly. Note that the 21596 global source location is still on the token before the 21597 '>>', so we need to say explicitly where we want it. */ 21598 cp_token *token = cp_lexer_peek_token (parser->lexer); 21599 error_at (token->location, "%<>>%> should be %<> >%> " 21600 "within a nested template argument list"); 21601 21602 token->type = CPP_GREATER; 21603 } 21604 else 21605 { 21606 /* If this is not a nested template argument list, the '>>' 21607 is a typo for '>'. Emit an error message and continue. 21608 Same deal about the token location, but here we can get it 21609 right by consuming the '>>' before issuing the diagnostic. */ 21610 cp_token *token = cp_lexer_consume_token (parser->lexer); 21611 error_at (token->location, 21612 "spurious %<>>%>, use %<>%> to terminate " 21613 "a template argument list"); 21614 } 21615 } 21616 else 21617 cp_parser_skip_to_end_of_template_parameter_list (parser); 21618 /* The `>' token might be a greater-than operator again now. */ 21619 parser->greater_than_is_operator_p 21620 = saved_greater_than_is_operator_p; 21621 /* Restore the SAVED_SCOPE. */ 21622 parser->scope = saved_scope; 21623 parser->qualifying_scope = saved_qualifying_scope; 21624 parser->object_scope = saved_object_scope; 21625 cp_unevaluated_operand = saved_unevaluated_operand; 21626 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; 21627 21628 return arguments; 21629 } 21630 21631 /* MEMBER_FUNCTION is a member function, or a friend. If default 21632 arguments, or the body of the function have not yet been parsed, 21633 parse them now. */ 21634 21635 static void 21636 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) 21637 { 21638 timevar_push (TV_PARSE_INMETH); 21639 /* If this member is a template, get the underlying 21640 FUNCTION_DECL. */ 21641 if (DECL_FUNCTION_TEMPLATE_P (member_function)) 21642 member_function = DECL_TEMPLATE_RESULT (member_function); 21643 21644 /* There should not be any class definitions in progress at this 21645 point; the bodies of members are only parsed outside of all class 21646 definitions. */ 21647 gcc_assert (parser->num_classes_being_defined == 0); 21648 /* While we're parsing the member functions we might encounter more 21649 classes. We want to handle them right away, but we don't want 21650 them getting mixed up with functions that are currently in the 21651 queue. */ 21652 push_unparsed_function_queues (parser); 21653 21654 /* Make sure that any template parameters are in scope. */ 21655 maybe_begin_member_template_processing (member_function); 21656 21657 /* If the body of the function has not yet been parsed, parse it 21658 now. */ 21659 if (DECL_PENDING_INLINE_P (member_function)) 21660 { 21661 tree function_scope; 21662 cp_token_cache *tokens; 21663 21664 /* The function is no longer pending; we are processing it. */ 21665 tokens = DECL_PENDING_INLINE_INFO (member_function); 21666 DECL_PENDING_INLINE_INFO (member_function) = NULL; 21667 DECL_PENDING_INLINE_P (member_function) = 0; 21668 21669 /* If this is a local class, enter the scope of the containing 21670 function. */ 21671 function_scope = current_function_decl; 21672 if (function_scope) 21673 push_function_context (); 21674 21675 /* Push the body of the function onto the lexer stack. */ 21676 cp_parser_push_lexer_for_tokens (parser, tokens); 21677 21678 /* Let the front end know that we going to be defining this 21679 function. */ 21680 start_preparsed_function (member_function, NULL_TREE, 21681 SF_PRE_PARSED | SF_INCLASS_INLINE); 21682 21683 /* Don't do access checking if it is a templated function. */ 21684 if (processing_template_decl) 21685 push_deferring_access_checks (dk_no_check); 21686 21687 /* Now, parse the body of the function. */ 21688 cp_parser_function_definition_after_declarator (parser, 21689 /*inline_p=*/true); 21690 21691 if (processing_template_decl) 21692 pop_deferring_access_checks (); 21693 21694 /* Leave the scope of the containing function. */ 21695 if (function_scope) 21696 pop_function_context (); 21697 cp_parser_pop_lexer (parser); 21698 } 21699 21700 /* Remove any template parameters from the symbol table. */ 21701 maybe_end_member_template_processing (); 21702 21703 /* Restore the queue. */ 21704 pop_unparsed_function_queues (parser); 21705 timevar_pop (TV_PARSE_INMETH); 21706 } 21707 21708 /* If DECL contains any default args, remember it on the unparsed 21709 functions queue. */ 21710 21711 static void 21712 cp_parser_save_default_args (cp_parser* parser, tree decl) 21713 { 21714 tree probe; 21715 21716 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); 21717 probe; 21718 probe = TREE_CHAIN (probe)) 21719 if (TREE_PURPOSE (probe)) 21720 { 21721 cp_default_arg_entry *entry 21722 = VEC_safe_push (cp_default_arg_entry, gc, 21723 unparsed_funs_with_default_args, NULL); 21724 entry->class_type = current_class_type; 21725 entry->decl = decl; 21726 break; 21727 } 21728 } 21729 21730 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL, 21731 which is either a FIELD_DECL or PARM_DECL. Parse it and return 21732 the result. For a PARM_DECL, PARMTYPE is the corresponding type 21733 from the parameter-type-list. */ 21734 21735 static tree 21736 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl, 21737 tree default_arg, tree parmtype) 21738 { 21739 cp_token_cache *tokens; 21740 tree parsed_arg; 21741 bool dummy; 21742 21743 if (default_arg == error_mark_node) 21744 return error_mark_node; 21745 21746 /* Push the saved tokens for the default argument onto the parser's 21747 lexer stack. */ 21748 tokens = DEFARG_TOKENS (default_arg); 21749 cp_parser_push_lexer_for_tokens (parser, tokens); 21750 21751 start_lambda_scope (decl); 21752 21753 /* Parse the default argument. */ 21754 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy); 21755 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)) 21756 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 21757 21758 finish_lambda_scope (); 21759 21760 if (!processing_template_decl) 21761 { 21762 /* In a non-template class, check conversions now. In a template, 21763 we'll wait and instantiate these as needed. */ 21764 if (TREE_CODE (decl) == PARM_DECL) 21765 parsed_arg = check_default_argument (parmtype, parsed_arg); 21766 else 21767 { 21768 int flags = LOOKUP_IMPLICIT; 21769 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg) 21770 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg)) 21771 flags = LOOKUP_NORMAL; 21772 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags); 21773 } 21774 } 21775 21776 /* If the token stream has not been completely used up, then 21777 there was extra junk after the end of the default 21778 argument. */ 21779 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF)) 21780 { 21781 if (TREE_CODE (decl) == PARM_DECL) 21782 cp_parser_error (parser, "expected %<,%>"); 21783 else 21784 cp_parser_error (parser, "expected %<;%>"); 21785 } 21786 21787 /* Revert to the main lexer. */ 21788 cp_parser_pop_lexer (parser); 21789 21790 return parsed_arg; 21791 } 21792 21793 /* FIELD is a non-static data member with an initializer which we saved for 21794 later; parse it now. */ 21795 21796 static void 21797 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field) 21798 { 21799 tree def; 21800 21801 push_unparsed_function_queues (parser); 21802 def = cp_parser_late_parse_one_default_arg (parser, field, 21803 DECL_INITIAL (field), 21804 NULL_TREE); 21805 pop_unparsed_function_queues (parser); 21806 21807 DECL_INITIAL (field) = def; 21808 } 21809 21810 /* FN is a FUNCTION_DECL which may contains a parameter with an 21811 unparsed DEFAULT_ARG. Parse the default args now. This function 21812 assumes that the current scope is the scope in which the default 21813 argument should be processed. */ 21814 21815 static void 21816 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) 21817 { 21818 bool saved_local_variables_forbidden_p; 21819 tree parm, parmdecl; 21820 21821 /* While we're parsing the default args, we might (due to the 21822 statement expression extension) encounter more classes. We want 21823 to handle them right away, but we don't want them getting mixed 21824 up with default args that are currently in the queue. */ 21825 push_unparsed_function_queues (parser); 21826 21827 /* Local variable names (and the `this' keyword) may not appear 21828 in a default argument. */ 21829 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; 21830 parser->local_variables_forbidden_p = true; 21831 21832 push_defarg_context (fn); 21833 21834 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)), 21835 parmdecl = DECL_ARGUMENTS (fn); 21836 parm && parm != void_list_node; 21837 parm = TREE_CHAIN (parm), 21838 parmdecl = DECL_CHAIN (parmdecl)) 21839 { 21840 tree default_arg = TREE_PURPOSE (parm); 21841 tree parsed_arg; 21842 VEC(tree,gc) *insts; 21843 tree copy; 21844 unsigned ix; 21845 21846 if (!default_arg) 21847 continue; 21848 21849 if (TREE_CODE (default_arg) != DEFAULT_ARG) 21850 /* This can happen for a friend declaration for a function 21851 already declared with default arguments. */ 21852 continue; 21853 21854 parsed_arg 21855 = cp_parser_late_parse_one_default_arg (parser, parmdecl, 21856 default_arg, 21857 TREE_VALUE (parm)); 21858 if (parsed_arg == error_mark_node) 21859 { 21860 continue; 21861 } 21862 21863 TREE_PURPOSE (parm) = parsed_arg; 21864 21865 /* Update any instantiations we've already created. */ 21866 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0; 21867 VEC_iterate (tree, insts, ix, copy); ix++) 21868 TREE_PURPOSE (copy) = parsed_arg; 21869 } 21870 21871 pop_defarg_context (); 21872 21873 /* Make sure no default arg is missing. */ 21874 check_default_args (fn); 21875 21876 /* Restore the state of local_variables_forbidden_p. */ 21877 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; 21878 21879 /* Restore the queue. */ 21880 pop_unparsed_function_queues (parser); 21881 } 21882 21883 /* Parse the operand of `sizeof' (or a similar operator). Returns 21884 either a TYPE or an expression, depending on the form of the 21885 input. The KEYWORD indicates which kind of expression we have 21886 encountered. */ 21887 21888 static tree 21889 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) 21890 { 21891 tree expr = NULL_TREE; 21892 const char *saved_message; 21893 char *tmp; 21894 bool saved_integral_constant_expression_p; 21895 bool saved_non_integral_constant_expression_p; 21896 bool pack_expansion_p = false; 21897 21898 /* Types cannot be defined in a `sizeof' expression. Save away the 21899 old message. */ 21900 saved_message = parser->type_definition_forbidden_message; 21901 /* And create the new one. */ 21902 tmp = concat ("types may not be defined in %<", 21903 IDENTIFIER_POINTER (ridpointers[keyword]), 21904 "%> expressions", NULL); 21905 parser->type_definition_forbidden_message = tmp; 21906 21907 /* The restrictions on constant-expressions do not apply inside 21908 sizeof expressions. */ 21909 saved_integral_constant_expression_p 21910 = parser->integral_constant_expression_p; 21911 saved_non_integral_constant_expression_p 21912 = parser->non_integral_constant_expression_p; 21913 parser->integral_constant_expression_p = false; 21914 21915 /* If it's a `...', then we are computing the length of a parameter 21916 pack. */ 21917 if (keyword == RID_SIZEOF 21918 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 21919 { 21920 /* Consume the `...'. */ 21921 cp_lexer_consume_token (parser->lexer); 21922 maybe_warn_variadic_templates (); 21923 21924 /* Note that this is an expansion. */ 21925 pack_expansion_p = true; 21926 } 21927 21928 /* Do not actually evaluate the expression. */ 21929 ++cp_unevaluated_operand; 21930 ++c_inhibit_evaluation_warnings; 21931 /* If it's a `(', then we might be looking at the type-id 21932 construction. */ 21933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 21934 { 21935 tree type; 21936 bool saved_in_type_id_in_expr_p; 21937 21938 /* We can't be sure yet whether we're looking at a type-id or an 21939 expression. */ 21940 cp_parser_parse_tentatively (parser); 21941 /* Consume the `('. */ 21942 cp_lexer_consume_token (parser->lexer); 21943 /* Parse the type-id. */ 21944 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; 21945 parser->in_type_id_in_expr_p = true; 21946 type = cp_parser_type_id (parser); 21947 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; 21948 /* Now, look for the trailing `)'. */ 21949 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 21950 /* If all went well, then we're done. */ 21951 if (cp_parser_parse_definitely (parser)) 21952 { 21953 cp_decl_specifier_seq decl_specs; 21954 21955 /* Build a trivial decl-specifier-seq. */ 21956 clear_decl_specs (&decl_specs); 21957 decl_specs.type = type; 21958 21959 /* Call grokdeclarator to figure out what type this is. */ 21960 expr = grokdeclarator (NULL, 21961 &decl_specs, 21962 TYPENAME, 21963 /*initialized=*/0, 21964 /*attrlist=*/NULL); 21965 } 21966 } 21967 21968 /* If the type-id production did not work out, then we must be 21969 looking at the unary-expression production. */ 21970 if (!expr) 21971 expr = cp_parser_unary_expression (parser, /*address_p=*/false, 21972 /*cast_p=*/false, NULL); 21973 21974 if (pack_expansion_p) 21975 /* Build a pack expansion. */ 21976 expr = make_pack_expansion (expr); 21977 21978 /* Go back to evaluating expressions. */ 21979 --cp_unevaluated_operand; 21980 --c_inhibit_evaluation_warnings; 21981 21982 /* Free the message we created. */ 21983 free (tmp); 21984 /* And restore the old one. */ 21985 parser->type_definition_forbidden_message = saved_message; 21986 parser->integral_constant_expression_p 21987 = saved_integral_constant_expression_p; 21988 parser->non_integral_constant_expression_p 21989 = saved_non_integral_constant_expression_p; 21990 21991 return expr; 21992 } 21993 21994 /* If the current declaration has no declarator, return true. */ 21995 21996 static bool 21997 cp_parser_declares_only_class_p (cp_parser *parser) 21998 { 21999 /* If the next token is a `;' or a `,' then there is no 22000 declarator. */ 22001 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 22002 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); 22003 } 22004 22005 /* Update the DECL_SPECS to reflect the storage class indicated by 22006 KEYWORD. */ 22007 22008 static void 22009 cp_parser_set_storage_class (cp_parser *parser, 22010 cp_decl_specifier_seq *decl_specs, 22011 enum rid keyword, 22012 location_t location) 22013 { 22014 cp_storage_class storage_class; 22015 22016 if (parser->in_unbraced_linkage_specification_p) 22017 { 22018 error_at (location, "invalid use of %qD in linkage specification", 22019 ridpointers[keyword]); 22020 return; 22021 } 22022 else if (decl_specs->storage_class != sc_none) 22023 { 22024 decl_specs->conflicting_specifiers_p = true; 22025 return; 22026 } 22027 22028 if ((keyword == RID_EXTERN || keyword == RID_STATIC) 22029 && decl_specs->specs[(int) ds_thread]) 22030 { 22031 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]); 22032 decl_specs->specs[(int) ds_thread] = 0; 22033 } 22034 22035 switch (keyword) 22036 { 22037 case RID_AUTO: 22038 storage_class = sc_auto; 22039 break; 22040 case RID_REGISTER: 22041 storage_class = sc_register; 22042 break; 22043 case RID_STATIC: 22044 storage_class = sc_static; 22045 break; 22046 case RID_EXTERN: 22047 storage_class = sc_extern; 22048 break; 22049 case RID_MUTABLE: 22050 storage_class = sc_mutable; 22051 break; 22052 default: 22053 gcc_unreachable (); 22054 } 22055 decl_specs->storage_class = storage_class; 22056 22057 /* A storage class specifier cannot be applied alongside a typedef 22058 specifier. If there is a typedef specifier present then set 22059 conflicting_specifiers_p which will trigger an error later 22060 on in grokdeclarator. */ 22061 if (decl_specs->specs[(int)ds_typedef]) 22062 decl_specs->conflicting_specifiers_p = true; 22063 } 22064 22065 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P 22066 is true, the type is a class or enum definition. */ 22067 22068 static void 22069 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs, 22070 tree type_spec, 22071 location_t location, 22072 bool type_definition_p) 22073 { 22074 decl_specs->any_specifiers_p = true; 22075 22076 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t 22077 (with, for example, in "typedef int wchar_t;") we remember that 22078 this is what happened. In system headers, we ignore these 22079 declarations so that G++ can work with system headers that are not 22080 C++-safe. */ 22081 if (decl_specs->specs[(int) ds_typedef] 22082 && !type_definition_p 22083 && (type_spec == boolean_type_node 22084 || type_spec == char16_type_node 22085 || type_spec == char32_type_node 22086 || type_spec == wchar_type_node) 22087 && (decl_specs->type 22088 || decl_specs->specs[(int) ds_long] 22089 || decl_specs->specs[(int) ds_short] 22090 || decl_specs->specs[(int) ds_unsigned] 22091 || decl_specs->specs[(int) ds_signed])) 22092 { 22093 decl_specs->redefined_builtin_type = type_spec; 22094 if (!decl_specs->type) 22095 { 22096 decl_specs->type = type_spec; 22097 decl_specs->type_definition_p = false; 22098 decl_specs->type_location = location; 22099 } 22100 } 22101 else if (decl_specs->type) 22102 decl_specs->multiple_types_p = true; 22103 else 22104 { 22105 decl_specs->type = type_spec; 22106 decl_specs->type_definition_p = type_definition_p; 22107 decl_specs->redefined_builtin_type = NULL_TREE; 22108 decl_specs->type_location = location; 22109 } 22110 } 22111 22112 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. 22113 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ 22114 22115 static bool 22116 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers) 22117 { 22118 return decl_specifiers->specs[(int) ds_friend] != 0; 22119 } 22120 22121 /* Issue an error message indicating that TOKEN_DESC was expected. 22122 If KEYWORD is true, it indicated this function is called by 22123 cp_parser_require_keword and the required token can only be 22124 a indicated keyword. */ 22125 22126 static void 22127 cp_parser_required_error (cp_parser *parser, 22128 required_token token_desc, 22129 bool keyword) 22130 { 22131 switch (token_desc) 22132 { 22133 case RT_NEW: 22134 cp_parser_error (parser, "expected %<new%>"); 22135 return; 22136 case RT_DELETE: 22137 cp_parser_error (parser, "expected %<delete%>"); 22138 return; 22139 case RT_RETURN: 22140 cp_parser_error (parser, "expected %<return%>"); 22141 return; 22142 case RT_WHILE: 22143 cp_parser_error (parser, "expected %<while%>"); 22144 return; 22145 case RT_EXTERN: 22146 cp_parser_error (parser, "expected %<extern%>"); 22147 return; 22148 case RT_STATIC_ASSERT: 22149 cp_parser_error (parser, "expected %<static_assert%>"); 22150 return; 22151 case RT_DECLTYPE: 22152 cp_parser_error (parser, "expected %<decltype%>"); 22153 return; 22154 case RT_OPERATOR: 22155 cp_parser_error (parser, "expected %<operator%>"); 22156 return; 22157 case RT_CLASS: 22158 cp_parser_error (parser, "expected %<class%>"); 22159 return; 22160 case RT_TEMPLATE: 22161 cp_parser_error (parser, "expected %<template%>"); 22162 return; 22163 case RT_NAMESPACE: 22164 cp_parser_error (parser, "expected %<namespace%>"); 22165 return; 22166 case RT_USING: 22167 cp_parser_error (parser, "expected %<using%>"); 22168 return; 22169 case RT_ASM: 22170 cp_parser_error (parser, "expected %<asm%>"); 22171 return; 22172 case RT_TRY: 22173 cp_parser_error (parser, "expected %<try%>"); 22174 return; 22175 case RT_CATCH: 22176 cp_parser_error (parser, "expected %<catch%>"); 22177 return; 22178 case RT_THROW: 22179 cp_parser_error (parser, "expected %<throw%>"); 22180 return; 22181 case RT_LABEL: 22182 cp_parser_error (parser, "expected %<__label__%>"); 22183 return; 22184 case RT_AT_TRY: 22185 cp_parser_error (parser, "expected %<@try%>"); 22186 return; 22187 case RT_AT_SYNCHRONIZED: 22188 cp_parser_error (parser, "expected %<@synchronized%>"); 22189 return; 22190 case RT_AT_THROW: 22191 cp_parser_error (parser, "expected %<@throw%>"); 22192 return; 22193 case RT_TRANSACTION_ATOMIC: 22194 cp_parser_error (parser, "expected %<__transaction_atomic%>"); 22195 return; 22196 case RT_TRANSACTION_RELAXED: 22197 cp_parser_error (parser, "expected %<__transaction_relaxed%>"); 22198 return; 22199 default: 22200 break; 22201 } 22202 if (!keyword) 22203 { 22204 switch (token_desc) 22205 { 22206 case RT_SEMICOLON: 22207 cp_parser_error (parser, "expected %<;%>"); 22208 return; 22209 case RT_OPEN_PAREN: 22210 cp_parser_error (parser, "expected %<(%>"); 22211 return; 22212 case RT_CLOSE_BRACE: 22213 cp_parser_error (parser, "expected %<}%>"); 22214 return; 22215 case RT_OPEN_BRACE: 22216 cp_parser_error (parser, "expected %<{%>"); 22217 return; 22218 case RT_CLOSE_SQUARE: 22219 cp_parser_error (parser, "expected %<]%>"); 22220 return; 22221 case RT_OPEN_SQUARE: 22222 cp_parser_error (parser, "expected %<[%>"); 22223 return; 22224 case RT_COMMA: 22225 cp_parser_error (parser, "expected %<,%>"); 22226 return; 22227 case RT_SCOPE: 22228 cp_parser_error (parser, "expected %<::%>"); 22229 return; 22230 case RT_LESS: 22231 cp_parser_error (parser, "expected %<<%>"); 22232 return; 22233 case RT_GREATER: 22234 cp_parser_error (parser, "expected %<>%>"); 22235 return; 22236 case RT_EQ: 22237 cp_parser_error (parser, "expected %<=%>"); 22238 return; 22239 case RT_ELLIPSIS: 22240 cp_parser_error (parser, "expected %<...%>"); 22241 return; 22242 case RT_MULT: 22243 cp_parser_error (parser, "expected %<*%>"); 22244 return; 22245 case RT_COMPL: 22246 cp_parser_error (parser, "expected %<~%>"); 22247 return; 22248 case RT_COLON: 22249 cp_parser_error (parser, "expected %<:%>"); 22250 return; 22251 case RT_COLON_SCOPE: 22252 cp_parser_error (parser, "expected %<:%> or %<::%>"); 22253 return; 22254 case RT_CLOSE_PAREN: 22255 cp_parser_error (parser, "expected %<)%>"); 22256 return; 22257 case RT_COMMA_CLOSE_PAREN: 22258 cp_parser_error (parser, "expected %<,%> or %<)%>"); 22259 return; 22260 case RT_PRAGMA_EOL: 22261 cp_parser_error (parser, "expected end of line"); 22262 return; 22263 case RT_NAME: 22264 cp_parser_error (parser, "expected identifier"); 22265 return; 22266 case RT_SELECT: 22267 cp_parser_error (parser, "expected selection-statement"); 22268 return; 22269 case RT_INTERATION: 22270 cp_parser_error (parser, "expected iteration-statement"); 22271 return; 22272 case RT_JUMP: 22273 cp_parser_error (parser, "expected jump-statement"); 22274 return; 22275 case RT_CLASS_KEY: 22276 cp_parser_error (parser, "expected class-key"); 22277 return; 22278 case RT_CLASS_TYPENAME_TEMPLATE: 22279 cp_parser_error (parser, 22280 "expected %<class%>, %<typename%>, or %<template%>"); 22281 return; 22282 default: 22283 gcc_unreachable (); 22284 } 22285 } 22286 else 22287 gcc_unreachable (); 22288 } 22289 22290 22291 22292 /* If the next token is of the indicated TYPE, consume it. Otherwise, 22293 issue an error message indicating that TOKEN_DESC was expected. 22294 22295 Returns the token consumed, if the token had the appropriate type. 22296 Otherwise, returns NULL. */ 22297 22298 static cp_token * 22299 cp_parser_require (cp_parser* parser, 22300 enum cpp_ttype type, 22301 required_token token_desc) 22302 { 22303 if (cp_lexer_next_token_is (parser->lexer, type)) 22304 return cp_lexer_consume_token (parser->lexer); 22305 else 22306 { 22307 /* Output the MESSAGE -- unless we're parsing tentatively. */ 22308 if (!cp_parser_simulate_error (parser)) 22309 cp_parser_required_error (parser, token_desc, /*keyword=*/false); 22310 return NULL; 22311 } 22312 } 22313 22314 /* An error message is produced if the next token is not '>'. 22315 All further tokens are skipped until the desired token is 22316 found or '{', '}', ';' or an unbalanced ')' or ']'. */ 22317 22318 static void 22319 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser) 22320 { 22321 /* Current level of '< ... >'. */ 22322 unsigned level = 0; 22323 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */ 22324 unsigned nesting_depth = 0; 22325 22326 /* Are we ready, yet? If not, issue error message. */ 22327 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER)) 22328 return; 22329 22330 /* Skip tokens until the desired token is found. */ 22331 while (true) 22332 { 22333 /* Peek at the next token. */ 22334 switch (cp_lexer_peek_token (parser->lexer)->type) 22335 { 22336 case CPP_LESS: 22337 if (!nesting_depth) 22338 ++level; 22339 break; 22340 22341 case CPP_RSHIFT: 22342 if (cxx_dialect == cxx98) 22343 /* C++0x views the `>>' operator as two `>' tokens, but 22344 C++98 does not. */ 22345 break; 22346 else if (!nesting_depth && level-- == 0) 22347 { 22348 /* We've hit a `>>' where the first `>' closes the 22349 template argument list, and the second `>' is 22350 spurious. Just consume the `>>' and stop; we've 22351 already produced at least one error. */ 22352 cp_lexer_consume_token (parser->lexer); 22353 return; 22354 } 22355 /* Fall through for C++0x, so we handle the second `>' in 22356 the `>>'. */ 22357 22358 case CPP_GREATER: 22359 if (!nesting_depth && level-- == 0) 22360 { 22361 /* We've reached the token we want, consume it and stop. */ 22362 cp_lexer_consume_token (parser->lexer); 22363 return; 22364 } 22365 break; 22366 22367 case CPP_OPEN_PAREN: 22368 case CPP_OPEN_SQUARE: 22369 ++nesting_depth; 22370 break; 22371 22372 case CPP_CLOSE_PAREN: 22373 case CPP_CLOSE_SQUARE: 22374 if (nesting_depth-- == 0) 22375 return; 22376 break; 22377 22378 case CPP_EOF: 22379 case CPP_PRAGMA_EOL: 22380 case CPP_SEMICOLON: 22381 case CPP_OPEN_BRACE: 22382 case CPP_CLOSE_BRACE: 22383 /* The '>' was probably forgotten, don't look further. */ 22384 return; 22385 22386 default: 22387 break; 22388 } 22389 22390 /* Consume this token. */ 22391 cp_lexer_consume_token (parser->lexer); 22392 } 22393 } 22394 22395 /* If the next token is the indicated keyword, consume it. Otherwise, 22396 issue an error message indicating that TOKEN_DESC was expected. 22397 22398 Returns the token consumed, if the token had the appropriate type. 22399 Otherwise, returns NULL. */ 22400 22401 static cp_token * 22402 cp_parser_require_keyword (cp_parser* parser, 22403 enum rid keyword, 22404 required_token token_desc) 22405 { 22406 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); 22407 22408 if (token && token->keyword != keyword) 22409 { 22410 cp_parser_required_error (parser, token_desc, /*keyword=*/true); 22411 return NULL; 22412 } 22413 22414 return token; 22415 } 22416 22417 /* Returns TRUE iff TOKEN is a token that can begin the body of a 22418 function-definition. */ 22419 22420 static bool 22421 cp_parser_token_starts_function_definition_p (cp_token* token) 22422 { 22423 return (/* An ordinary function-body begins with an `{'. */ 22424 token->type == CPP_OPEN_BRACE 22425 /* A ctor-initializer begins with a `:'. */ 22426 || token->type == CPP_COLON 22427 /* A function-try-block begins with `try'. */ 22428 || token->keyword == RID_TRY 22429 /* A function-transaction-block begins with `__transaction_atomic' 22430 or `__transaction_relaxed'. */ 22431 || token->keyword == RID_TRANSACTION_ATOMIC 22432 || token->keyword == RID_TRANSACTION_RELAXED 22433 /* The named return value extension begins with `return'. */ 22434 || token->keyword == RID_RETURN); 22435 } 22436 22437 /* Returns TRUE iff the next token is the ":" or "{" beginning a class 22438 definition. */ 22439 22440 static bool 22441 cp_parser_next_token_starts_class_definition_p (cp_parser *parser) 22442 { 22443 cp_token *token; 22444 22445 token = cp_lexer_peek_token (parser->lexer); 22446 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); 22447 } 22448 22449 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in 22450 C++0x) ending a template-argument. */ 22451 22452 static bool 22453 cp_parser_next_token_ends_template_argument_p (cp_parser *parser) 22454 { 22455 cp_token *token; 22456 22457 token = cp_lexer_peek_token (parser->lexer); 22458 return (token->type == CPP_COMMA 22459 || token->type == CPP_GREATER 22460 || token->type == CPP_ELLIPSIS 22461 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)); 22462 } 22463 22464 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the 22465 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */ 22466 22467 static bool 22468 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, 22469 size_t n) 22470 { 22471 cp_token *token; 22472 22473 token = cp_lexer_peek_nth_token (parser->lexer, n); 22474 if (token->type == CPP_LESS) 22475 return true; 22476 /* Check for the sequence `<::' in the original code. It would be lexed as 22477 `[:', where `[' is a digraph, and there is no whitespace before 22478 `:'. */ 22479 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH) 22480 { 22481 cp_token *token2; 22482 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1); 22483 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE)) 22484 return true; 22485 } 22486 return false; 22487 } 22488 22489 /* Returns the kind of tag indicated by TOKEN, if it is a class-key, 22490 or none_type otherwise. */ 22491 22492 static enum tag_types 22493 cp_parser_token_is_class_key (cp_token* token) 22494 { 22495 switch (token->keyword) 22496 { 22497 case RID_CLASS: 22498 return class_type; 22499 case RID_STRUCT: 22500 return record_type; 22501 case RID_UNION: 22502 return union_type; 22503 22504 default: 22505 return none_type; 22506 } 22507 } 22508 22509 /* Issue an error message if the CLASS_KEY does not match the TYPE. */ 22510 22511 static void 22512 cp_parser_check_class_key (enum tag_types class_key, tree type) 22513 { 22514 if (type == error_mark_node) 22515 return; 22516 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) 22517 { 22518 permerror (input_location, "%qs tag used in naming %q#T", 22519 class_key == union_type ? "union" 22520 : class_key == record_type ? "struct" : "class", 22521 type); 22522 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)), 22523 "%q#T was previously declared here", type); 22524 } 22525 } 22526 22527 /* Issue an error message if DECL is redeclared with different 22528 access than its original declaration [class.access.spec/3]. 22529 This applies to nested classes and nested class templates. 22530 [class.mem/1]. */ 22531 22532 static void 22533 cp_parser_check_access_in_redeclaration (tree decl, location_t location) 22534 { 22535 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl))) 22536 return; 22537 22538 if ((TREE_PRIVATE (decl) 22539 != (current_access_specifier == access_private_node)) 22540 || (TREE_PROTECTED (decl) 22541 != (current_access_specifier == access_protected_node))) 22542 error_at (location, "%qD redeclared with different access", decl); 22543 } 22544 22545 /* Look for the `template' keyword, as a syntactic disambiguator. 22546 Return TRUE iff it is present, in which case it will be 22547 consumed. */ 22548 22549 static bool 22550 cp_parser_optional_template_keyword (cp_parser *parser) 22551 { 22552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) 22553 { 22554 /* The `template' keyword can only be used within templates; 22555 outside templates the parser can always figure out what is a 22556 template and what is not. */ 22557 if (!processing_template_decl) 22558 { 22559 cp_token *token = cp_lexer_peek_token (parser->lexer); 22560 error_at (token->location, 22561 "%<template%> (as a disambiguator) is only allowed " 22562 "within templates"); 22563 /* If this part of the token stream is rescanned, the same 22564 error message would be generated. So, we purge the token 22565 from the stream. */ 22566 cp_lexer_purge_token (parser->lexer); 22567 return false; 22568 } 22569 else 22570 { 22571 /* Consume the `template' keyword. */ 22572 cp_lexer_consume_token (parser->lexer); 22573 return true; 22574 } 22575 } 22576 22577 return false; 22578 } 22579 22580 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, 22581 set PARSER->SCOPE, and perform other related actions. */ 22582 22583 static void 22584 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) 22585 { 22586 int i; 22587 struct tree_check *check_value; 22588 deferred_access_check *chk; 22589 VEC (deferred_access_check,gc) *checks; 22590 22591 /* Get the stored value. */ 22592 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value; 22593 /* Perform any access checks that were deferred. */ 22594 checks = check_value->checks; 22595 if (checks) 22596 { 22597 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk) 22598 perform_or_defer_access_check (chk->binfo, 22599 chk->decl, 22600 chk->diag_decl); 22601 } 22602 /* Set the scope from the stored value. */ 22603 parser->scope = check_value->value; 22604 parser->qualifying_scope = check_value->qualifying_scope; 22605 parser->object_scope = NULL_TREE; 22606 } 22607 22608 /* Consume tokens up through a non-nested END token. Returns TRUE if we 22609 encounter the end of a block before what we were looking for. */ 22610 22611 static bool 22612 cp_parser_cache_group (cp_parser *parser, 22613 enum cpp_ttype end, 22614 unsigned depth) 22615 { 22616 while (true) 22617 { 22618 cp_token *token = cp_lexer_peek_token (parser->lexer); 22619 22620 /* Abort a parenthesized expression if we encounter a semicolon. */ 22621 if ((end == CPP_CLOSE_PAREN || depth == 0) 22622 && token->type == CPP_SEMICOLON) 22623 return true; 22624 /* If we've reached the end of the file, stop. */ 22625 if (token->type == CPP_EOF 22626 || (end != CPP_PRAGMA_EOL 22627 && token->type == CPP_PRAGMA_EOL)) 22628 return true; 22629 if (token->type == CPP_CLOSE_BRACE && depth == 0) 22630 /* We've hit the end of an enclosing block, so there's been some 22631 kind of syntax error. */ 22632 return true; 22633 22634 /* Consume the token. */ 22635 cp_lexer_consume_token (parser->lexer); 22636 /* See if it starts a new group. */ 22637 if (token->type == CPP_OPEN_BRACE) 22638 { 22639 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1); 22640 /* In theory this should probably check end == '}', but 22641 cp_parser_save_member_function_body needs it to exit 22642 after either '}' or ')' when called with ')'. */ 22643 if (depth == 0) 22644 return false; 22645 } 22646 else if (token->type == CPP_OPEN_PAREN) 22647 { 22648 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1); 22649 if (depth == 0 && end == CPP_CLOSE_PAREN) 22650 return false; 22651 } 22652 else if (token->type == CPP_PRAGMA) 22653 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1); 22654 else if (token->type == end) 22655 return false; 22656 } 22657 } 22658 22659 /* Like above, for caching a default argument or NSDMI. Both of these are 22660 terminated by a non-nested comma, but it can be unclear whether or not a 22661 comma is nested in a template argument list unless we do more parsing. 22662 In order to handle this ambiguity, when we encounter a ',' after a '<' 22663 we try to parse what follows as a parameter-declaration-list (in the 22664 case of a default argument) or a member-declarator (in the case of an 22665 NSDMI). If that succeeds, then we stop caching. */ 22666 22667 static tree 22668 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi) 22669 { 22670 unsigned depth = 0; 22671 int maybe_template_id = 0; 22672 cp_token *first_token; 22673 cp_token *token; 22674 tree default_argument; 22675 22676 /* Add tokens until we have processed the entire default 22677 argument. We add the range [first_token, token). */ 22678 first_token = cp_lexer_peek_token (parser->lexer); 22679 if (first_token->type == CPP_OPEN_BRACE) 22680 { 22681 /* For list-initialization, this is straightforward. */ 22682 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); 22683 token = cp_lexer_peek_token (parser->lexer); 22684 } 22685 else while (true) 22686 { 22687 bool done = false; 22688 22689 /* Peek at the next token. */ 22690 token = cp_lexer_peek_token (parser->lexer); 22691 /* What we do depends on what token we have. */ 22692 switch (token->type) 22693 { 22694 /* In valid code, a default argument must be 22695 immediately followed by a `,' `)', or `...'. */ 22696 case CPP_COMMA: 22697 if (depth == 0 && maybe_template_id) 22698 { 22699 /* If we've seen a '<', we might be in a 22700 template-argument-list. Until Core issue 325 is 22701 resolved, we don't know how this situation ought 22702 to be handled, so try to DTRT. We check whether 22703 what comes after the comma is a valid parameter 22704 declaration list. If it is, then the comma ends 22705 the default argument; otherwise the default 22706 argument continues. */ 22707 bool error = false; 22708 tree t; 22709 22710 /* Set ITALP so cp_parser_parameter_declaration_list 22711 doesn't decide to commit to this parse. */ 22712 bool saved_italp = parser->in_template_argument_list_p; 22713 parser->in_template_argument_list_p = true; 22714 22715 cp_parser_parse_tentatively (parser); 22716 cp_lexer_consume_token (parser->lexer); 22717 22718 if (nsdmi) 22719 { 22720 int ctor_dtor_or_conv_p; 22721 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 22722 &ctor_dtor_or_conv_p, 22723 /*parenthesized_p=*/NULL, 22724 /*member_p=*/true); 22725 } 22726 else 22727 { 22728 begin_scope (sk_function_parms, NULL_TREE); 22729 cp_parser_parameter_declaration_list (parser, &error); 22730 for (t = current_binding_level->names; t; t = DECL_CHAIN (t)) 22731 pop_binding (DECL_NAME (t), t); 22732 leave_scope (); 22733 } 22734 if (!cp_parser_error_occurred (parser) && !error) 22735 done = true; 22736 cp_parser_abort_tentative_parse (parser); 22737 22738 parser->in_template_argument_list_p = saved_italp; 22739 break; 22740 } 22741 case CPP_CLOSE_PAREN: 22742 case CPP_ELLIPSIS: 22743 /* If we run into a non-nested `;', `}', or `]', 22744 then the code is invalid -- but the default 22745 argument is certainly over. */ 22746 case CPP_SEMICOLON: 22747 case CPP_CLOSE_BRACE: 22748 case CPP_CLOSE_SQUARE: 22749 if (depth == 0) 22750 done = true; 22751 /* Update DEPTH, if necessary. */ 22752 else if (token->type == CPP_CLOSE_PAREN 22753 || token->type == CPP_CLOSE_BRACE 22754 || token->type == CPP_CLOSE_SQUARE) 22755 --depth; 22756 break; 22757 22758 case CPP_OPEN_PAREN: 22759 case CPP_OPEN_SQUARE: 22760 case CPP_OPEN_BRACE: 22761 ++depth; 22762 break; 22763 22764 case CPP_LESS: 22765 if (depth == 0) 22766 /* This might be the comparison operator, or it might 22767 start a template argument list. */ 22768 ++maybe_template_id; 22769 break; 22770 22771 case CPP_RSHIFT: 22772 if (cxx_dialect == cxx98) 22773 break; 22774 /* Fall through for C++0x, which treats the `>>' 22775 operator like two `>' tokens in certain 22776 cases. */ 22777 22778 case CPP_GREATER: 22779 if (depth == 0) 22780 { 22781 /* This might be an operator, or it might close a 22782 template argument list. But if a previous '<' 22783 started a template argument list, this will have 22784 closed it, so we can't be in one anymore. */ 22785 maybe_template_id -= 1 + (token->type == CPP_RSHIFT); 22786 if (maybe_template_id < 0) 22787 maybe_template_id = 0; 22788 } 22789 break; 22790 22791 /* If we run out of tokens, issue an error message. */ 22792 case CPP_EOF: 22793 case CPP_PRAGMA_EOL: 22794 error_at (token->location, "file ends in default argument"); 22795 done = true; 22796 break; 22797 22798 case CPP_NAME: 22799 case CPP_SCOPE: 22800 /* In these cases, we should look for template-ids. 22801 For example, if the default argument is 22802 `X<int, double>()', we need to do name lookup to 22803 figure out whether or not `X' is a template; if 22804 so, the `,' does not end the default argument. 22805 22806 That is not yet done. */ 22807 break; 22808 22809 default: 22810 break; 22811 } 22812 22813 /* If we've reached the end, stop. */ 22814 if (done) 22815 break; 22816 22817 /* Add the token to the token block. */ 22818 token = cp_lexer_consume_token (parser->lexer); 22819 } 22820 22821 /* Create a DEFAULT_ARG to represent the unparsed default 22822 argument. */ 22823 default_argument = make_node (DEFAULT_ARG); 22824 DEFARG_TOKENS (default_argument) 22825 = cp_token_cache_new (first_token, token); 22826 DEFARG_INSTANTIATIONS (default_argument) = NULL; 22827 22828 return default_argument; 22829 } 22830 22831 /* Begin parsing tentatively. We always save tokens while parsing 22832 tentatively so that if the tentative parsing fails we can restore the 22833 tokens. */ 22834 22835 static void 22836 cp_parser_parse_tentatively (cp_parser* parser) 22837 { 22838 /* Enter a new parsing context. */ 22839 parser->context = cp_parser_context_new (parser->context); 22840 /* Begin saving tokens. */ 22841 cp_lexer_save_tokens (parser->lexer); 22842 /* In order to avoid repetitive access control error messages, 22843 access checks are queued up until we are no longer parsing 22844 tentatively. */ 22845 push_deferring_access_checks (dk_deferred); 22846 } 22847 22848 /* Commit to the currently active tentative parse. */ 22849 22850 static void 22851 cp_parser_commit_to_tentative_parse (cp_parser* parser) 22852 { 22853 cp_parser_context *context; 22854 cp_lexer *lexer; 22855 22856 /* Mark all of the levels as committed. */ 22857 lexer = parser->lexer; 22858 for (context = parser->context; context->next; context = context->next) 22859 { 22860 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) 22861 break; 22862 context->status = CP_PARSER_STATUS_KIND_COMMITTED; 22863 while (!cp_lexer_saving_tokens (lexer)) 22864 lexer = lexer->next; 22865 cp_lexer_commit_tokens (lexer); 22866 } 22867 } 22868 22869 /* Abort the currently active tentative parse. All consumed tokens 22870 will be rolled back, and no diagnostics will be issued. */ 22871 22872 static void 22873 cp_parser_abort_tentative_parse (cp_parser* parser) 22874 { 22875 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED 22876 || errorcount > 0); 22877 cp_parser_simulate_error (parser); 22878 /* Now, pretend that we want to see if the construct was 22879 successfully parsed. */ 22880 cp_parser_parse_definitely (parser); 22881 } 22882 22883 /* Stop parsing tentatively. If a parse error has occurred, restore the 22884 token stream. Otherwise, commit to the tokens we have consumed. 22885 Returns true if no error occurred; false otherwise. */ 22886 22887 static bool 22888 cp_parser_parse_definitely (cp_parser* parser) 22889 { 22890 bool error_occurred; 22891 cp_parser_context *context; 22892 22893 /* Remember whether or not an error occurred, since we are about to 22894 destroy that information. */ 22895 error_occurred = cp_parser_error_occurred (parser); 22896 /* Remove the topmost context from the stack. */ 22897 context = parser->context; 22898 parser->context = context->next; 22899 /* If no parse errors occurred, commit to the tentative parse. */ 22900 if (!error_occurred) 22901 { 22902 /* Commit to the tokens read tentatively, unless that was 22903 already done. */ 22904 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) 22905 cp_lexer_commit_tokens (parser->lexer); 22906 22907 pop_to_parent_deferring_access_checks (); 22908 } 22909 /* Otherwise, if errors occurred, roll back our state so that things 22910 are just as they were before we began the tentative parse. */ 22911 else 22912 { 22913 cp_lexer_rollback_tokens (parser->lexer); 22914 pop_deferring_access_checks (); 22915 } 22916 /* Add the context to the front of the free list. */ 22917 context->next = cp_parser_context_free_list; 22918 cp_parser_context_free_list = context; 22919 22920 return !error_occurred; 22921 } 22922 22923 /* Returns true if we are parsing tentatively and are not committed to 22924 this tentative parse. */ 22925 22926 static bool 22927 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser) 22928 { 22929 return (cp_parser_parsing_tentatively (parser) 22930 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED); 22931 } 22932 22933 /* Returns nonzero iff an error has occurred during the most recent 22934 tentative parse. */ 22935 22936 static bool 22937 cp_parser_error_occurred (cp_parser* parser) 22938 { 22939 return (cp_parser_parsing_tentatively (parser) 22940 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); 22941 } 22942 22943 /* Returns nonzero if GNU extensions are allowed. */ 22944 22945 static bool 22946 cp_parser_allow_gnu_extensions_p (cp_parser* parser) 22947 { 22948 return parser->allow_gnu_extensions_p; 22949 } 22950 22951 /* Objective-C++ Productions */ 22952 22953 22954 /* Parse an Objective-C expression, which feeds into a primary-expression 22955 above. 22956 22957 objc-expression: 22958 objc-message-expression 22959 objc-string-literal 22960 objc-encode-expression 22961 objc-protocol-expression 22962 objc-selector-expression 22963 22964 Returns a tree representation of the expression. */ 22965 22966 static tree 22967 cp_parser_objc_expression (cp_parser* parser) 22968 { 22969 /* Try to figure out what kind of declaration is present. */ 22970 cp_token *kwd = cp_lexer_peek_token (parser->lexer); 22971 22972 switch (kwd->type) 22973 { 22974 case CPP_OPEN_SQUARE: 22975 return cp_parser_objc_message_expression (parser); 22976 22977 case CPP_OBJC_STRING: 22978 kwd = cp_lexer_consume_token (parser->lexer); 22979 return objc_build_string_object (kwd->u.value); 22980 22981 case CPP_KEYWORD: 22982 switch (kwd->keyword) 22983 { 22984 case RID_AT_ENCODE: 22985 return cp_parser_objc_encode_expression (parser); 22986 22987 case RID_AT_PROTOCOL: 22988 return cp_parser_objc_protocol_expression (parser); 22989 22990 case RID_AT_SELECTOR: 22991 return cp_parser_objc_selector_expression (parser); 22992 22993 default: 22994 break; 22995 } 22996 default: 22997 error_at (kwd->location, 22998 "misplaced %<@%D%> Objective-C++ construct", 22999 kwd->u.value); 23000 cp_parser_skip_to_end_of_block_or_statement (parser); 23001 } 23002 23003 return error_mark_node; 23004 } 23005 23006 /* Parse an Objective-C message expression. 23007 23008 objc-message-expression: 23009 [ objc-message-receiver objc-message-args ] 23010 23011 Returns a representation of an Objective-C message. */ 23012 23013 static tree 23014 cp_parser_objc_message_expression (cp_parser* parser) 23015 { 23016 tree receiver, messageargs; 23017 23018 cp_lexer_consume_token (parser->lexer); /* Eat '['. */ 23019 receiver = cp_parser_objc_message_receiver (parser); 23020 messageargs = cp_parser_objc_message_args (parser); 23021 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 23022 23023 return objc_build_message_expr (receiver, messageargs); 23024 } 23025 23026 /* Parse an objc-message-receiver. 23027 23028 objc-message-receiver: 23029 expression 23030 simple-type-specifier 23031 23032 Returns a representation of the type or expression. */ 23033 23034 static tree 23035 cp_parser_objc_message_receiver (cp_parser* parser) 23036 { 23037 tree rcv; 23038 23039 /* An Objective-C message receiver may be either (1) a type 23040 or (2) an expression. */ 23041 cp_parser_parse_tentatively (parser); 23042 rcv = cp_parser_expression (parser, false, NULL); 23043 23044 if (cp_parser_parse_definitely (parser)) 23045 return rcv; 23046 23047 rcv = cp_parser_simple_type_specifier (parser, 23048 /*decl_specs=*/NULL, 23049 CP_PARSER_FLAGS_NONE); 23050 23051 return objc_get_class_reference (rcv); 23052 } 23053 23054 /* Parse the arguments and selectors comprising an Objective-C message. 23055 23056 objc-message-args: 23057 objc-selector 23058 objc-selector-args 23059 objc-selector-args , objc-comma-args 23060 23061 objc-selector-args: 23062 objc-selector [opt] : assignment-expression 23063 objc-selector-args objc-selector [opt] : assignment-expression 23064 23065 objc-comma-args: 23066 assignment-expression 23067 objc-comma-args , assignment-expression 23068 23069 Returns a TREE_LIST, with TREE_PURPOSE containing a list of 23070 selector arguments and TREE_VALUE containing a list of comma 23071 arguments. */ 23072 23073 static tree 23074 cp_parser_objc_message_args (cp_parser* parser) 23075 { 23076 tree sel_args = NULL_TREE, addl_args = NULL_TREE; 23077 bool maybe_unary_selector_p = true; 23078 cp_token *token = cp_lexer_peek_token (parser->lexer); 23079 23080 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) 23081 { 23082 tree selector = NULL_TREE, arg; 23083 23084 if (token->type != CPP_COLON) 23085 selector = cp_parser_objc_selector (parser); 23086 23087 /* Detect if we have a unary selector. */ 23088 if (maybe_unary_selector_p 23089 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) 23090 return build_tree_list (selector, NULL_TREE); 23091 23092 maybe_unary_selector_p = false; 23093 cp_parser_require (parser, CPP_COLON, RT_COLON); 23094 arg = cp_parser_assignment_expression (parser, false, NULL); 23095 23096 sel_args 23097 = chainon (sel_args, 23098 build_tree_list (selector, arg)); 23099 23100 token = cp_lexer_peek_token (parser->lexer); 23101 } 23102 23103 /* Handle non-selector arguments, if any. */ 23104 while (token->type == CPP_COMMA) 23105 { 23106 tree arg; 23107 23108 cp_lexer_consume_token (parser->lexer); 23109 arg = cp_parser_assignment_expression (parser, false, NULL); 23110 23111 addl_args 23112 = chainon (addl_args, 23113 build_tree_list (NULL_TREE, arg)); 23114 23115 token = cp_lexer_peek_token (parser->lexer); 23116 } 23117 23118 if (sel_args == NULL_TREE && addl_args == NULL_TREE) 23119 { 23120 cp_parser_error (parser, "objective-c++ message argument(s) are expected"); 23121 return build_tree_list (error_mark_node, error_mark_node); 23122 } 23123 23124 return build_tree_list (sel_args, addl_args); 23125 } 23126 23127 /* Parse an Objective-C encode expression. 23128 23129 objc-encode-expression: 23130 @encode objc-typename 23131 23132 Returns an encoded representation of the type argument. */ 23133 23134 static tree 23135 cp_parser_objc_encode_expression (cp_parser* parser) 23136 { 23137 tree type; 23138 cp_token *token; 23139 23140 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */ 23141 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 23142 token = cp_lexer_peek_token (parser->lexer); 23143 type = complete_type (cp_parser_type_id (parser)); 23144 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23145 23146 if (!type) 23147 { 23148 error_at (token->location, 23149 "%<@encode%> must specify a type as an argument"); 23150 return error_mark_node; 23151 } 23152 23153 /* This happens if we find @encode(T) (where T is a template 23154 typename or something dependent on a template typename) when 23155 parsing a template. In that case, we can't compile it 23156 immediately, but we rather create an AT_ENCODE_EXPR which will 23157 need to be instantiated when the template is used. 23158 */ 23159 if (dependent_type_p (type)) 23160 { 23161 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type); 23162 TREE_READONLY (value) = 1; 23163 return value; 23164 } 23165 23166 return objc_build_encode_expr (type); 23167 } 23168 23169 /* Parse an Objective-C @defs expression. */ 23170 23171 static tree 23172 cp_parser_objc_defs_expression (cp_parser *parser) 23173 { 23174 tree name; 23175 23176 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */ 23177 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 23178 name = cp_parser_identifier (parser); 23179 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23180 23181 return objc_get_class_ivars (name); 23182 } 23183 23184 /* Parse an Objective-C protocol expression. 23185 23186 objc-protocol-expression: 23187 @protocol ( identifier ) 23188 23189 Returns a representation of the protocol expression. */ 23190 23191 static tree 23192 cp_parser_objc_protocol_expression (cp_parser* parser) 23193 { 23194 tree proto; 23195 23196 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */ 23197 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 23198 proto = cp_parser_identifier (parser); 23199 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23200 23201 return objc_build_protocol_expr (proto); 23202 } 23203 23204 /* Parse an Objective-C selector expression. 23205 23206 objc-selector-expression: 23207 @selector ( objc-method-signature ) 23208 23209 objc-method-signature: 23210 objc-selector 23211 objc-selector-seq 23212 23213 objc-selector-seq: 23214 objc-selector : 23215 objc-selector-seq objc-selector : 23216 23217 Returns a representation of the method selector. */ 23218 23219 static tree 23220 cp_parser_objc_selector_expression (cp_parser* parser) 23221 { 23222 tree sel_seq = NULL_TREE; 23223 bool maybe_unary_selector_p = true; 23224 cp_token *token; 23225 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 23226 23227 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */ 23228 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 23229 token = cp_lexer_peek_token (parser->lexer); 23230 23231 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON 23232 || token->type == CPP_SCOPE) 23233 { 23234 tree selector = NULL_TREE; 23235 23236 if (token->type != CPP_COLON 23237 || token->type == CPP_SCOPE) 23238 selector = cp_parser_objc_selector (parser); 23239 23240 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON) 23241 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)) 23242 { 23243 /* Detect if we have a unary selector. */ 23244 if (maybe_unary_selector_p) 23245 { 23246 sel_seq = selector; 23247 goto finish_selector; 23248 } 23249 else 23250 { 23251 cp_parser_error (parser, "expected %<:%>"); 23252 } 23253 } 23254 maybe_unary_selector_p = false; 23255 token = cp_lexer_consume_token (parser->lexer); 23256 23257 if (token->type == CPP_SCOPE) 23258 { 23259 sel_seq 23260 = chainon (sel_seq, 23261 build_tree_list (selector, NULL_TREE)); 23262 sel_seq 23263 = chainon (sel_seq, 23264 build_tree_list (NULL_TREE, NULL_TREE)); 23265 } 23266 else 23267 sel_seq 23268 = chainon (sel_seq, 23269 build_tree_list (selector, NULL_TREE)); 23270 23271 token = cp_lexer_peek_token (parser->lexer); 23272 } 23273 23274 finish_selector: 23275 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23276 23277 return objc_build_selector_expr (loc, sel_seq); 23278 } 23279 23280 /* Parse a list of identifiers. 23281 23282 objc-identifier-list: 23283 identifier 23284 objc-identifier-list , identifier 23285 23286 Returns a TREE_LIST of identifier nodes. */ 23287 23288 static tree 23289 cp_parser_objc_identifier_list (cp_parser* parser) 23290 { 23291 tree identifier; 23292 tree list; 23293 cp_token *sep; 23294 23295 identifier = cp_parser_identifier (parser); 23296 if (identifier == error_mark_node) 23297 return error_mark_node; 23298 23299 list = build_tree_list (NULL_TREE, identifier); 23300 sep = cp_lexer_peek_token (parser->lexer); 23301 23302 while (sep->type == CPP_COMMA) 23303 { 23304 cp_lexer_consume_token (parser->lexer); /* Eat ','. */ 23305 identifier = cp_parser_identifier (parser); 23306 if (identifier == error_mark_node) 23307 return list; 23308 23309 list = chainon (list, build_tree_list (NULL_TREE, 23310 identifier)); 23311 sep = cp_lexer_peek_token (parser->lexer); 23312 } 23313 23314 return list; 23315 } 23316 23317 /* Parse an Objective-C alias declaration. 23318 23319 objc-alias-declaration: 23320 @compatibility_alias identifier identifier ; 23321 23322 This function registers the alias mapping with the Objective-C front end. 23323 It returns nothing. */ 23324 23325 static void 23326 cp_parser_objc_alias_declaration (cp_parser* parser) 23327 { 23328 tree alias, orig; 23329 23330 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */ 23331 alias = cp_parser_identifier (parser); 23332 orig = cp_parser_identifier (parser); 23333 objc_declare_alias (alias, orig); 23334 cp_parser_consume_semicolon_at_end_of_statement (parser); 23335 } 23336 23337 /* Parse an Objective-C class forward-declaration. 23338 23339 objc-class-declaration: 23340 @class objc-identifier-list ; 23341 23342 The function registers the forward declarations with the Objective-C 23343 front end. It returns nothing. */ 23344 23345 static void 23346 cp_parser_objc_class_declaration (cp_parser* parser) 23347 { 23348 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */ 23349 while (true) 23350 { 23351 tree id; 23352 23353 id = cp_parser_identifier (parser); 23354 if (id == error_mark_node) 23355 break; 23356 23357 objc_declare_class (id); 23358 23359 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 23360 cp_lexer_consume_token (parser->lexer); 23361 else 23362 break; 23363 } 23364 cp_parser_consume_semicolon_at_end_of_statement (parser); 23365 } 23366 23367 /* Parse a list of Objective-C protocol references. 23368 23369 objc-protocol-refs-opt: 23370 objc-protocol-refs [opt] 23371 23372 objc-protocol-refs: 23373 < objc-identifier-list > 23374 23375 Returns a TREE_LIST of identifiers, if any. */ 23376 23377 static tree 23378 cp_parser_objc_protocol_refs_opt (cp_parser* parser) 23379 { 23380 tree protorefs = NULL_TREE; 23381 23382 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS)) 23383 { 23384 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */ 23385 protorefs = cp_parser_objc_identifier_list (parser); 23386 cp_parser_require (parser, CPP_GREATER, RT_GREATER); 23387 } 23388 23389 return protorefs; 23390 } 23391 23392 /* Parse a Objective-C visibility specification. */ 23393 23394 static void 23395 cp_parser_objc_visibility_spec (cp_parser* parser) 23396 { 23397 cp_token *vis = cp_lexer_peek_token (parser->lexer); 23398 23399 switch (vis->keyword) 23400 { 23401 case RID_AT_PRIVATE: 23402 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE); 23403 break; 23404 case RID_AT_PROTECTED: 23405 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED); 23406 break; 23407 case RID_AT_PUBLIC: 23408 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC); 23409 break; 23410 case RID_AT_PACKAGE: 23411 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE); 23412 break; 23413 default: 23414 return; 23415 } 23416 23417 /* Eat '@private'/'@protected'/'@public'. */ 23418 cp_lexer_consume_token (parser->lexer); 23419 } 23420 23421 /* Parse an Objective-C method type. Return 'true' if it is a class 23422 (+) method, and 'false' if it is an instance (-) method. */ 23423 23424 static inline bool 23425 cp_parser_objc_method_type (cp_parser* parser) 23426 { 23427 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS) 23428 return true; 23429 else 23430 return false; 23431 } 23432 23433 /* Parse an Objective-C protocol qualifier. */ 23434 23435 static tree 23436 cp_parser_objc_protocol_qualifiers (cp_parser* parser) 23437 { 23438 tree quals = NULL_TREE, node; 23439 cp_token *token = cp_lexer_peek_token (parser->lexer); 23440 23441 node = token->u.value; 23442 23443 while (node && TREE_CODE (node) == IDENTIFIER_NODE 23444 && (node == ridpointers [(int) RID_IN] 23445 || node == ridpointers [(int) RID_OUT] 23446 || node == ridpointers [(int) RID_INOUT] 23447 || node == ridpointers [(int) RID_BYCOPY] 23448 || node == ridpointers [(int) RID_BYREF] 23449 || node == ridpointers [(int) RID_ONEWAY])) 23450 { 23451 quals = tree_cons (NULL_TREE, node, quals); 23452 cp_lexer_consume_token (parser->lexer); 23453 token = cp_lexer_peek_token (parser->lexer); 23454 node = token->u.value; 23455 } 23456 23457 return quals; 23458 } 23459 23460 /* Parse an Objective-C typename. */ 23461 23462 static tree 23463 cp_parser_objc_typename (cp_parser* parser) 23464 { 23465 tree type_name = NULL_TREE; 23466 23467 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 23468 { 23469 tree proto_quals, cp_type = NULL_TREE; 23470 23471 cp_lexer_consume_token (parser->lexer); /* Eat '('. */ 23472 proto_quals = cp_parser_objc_protocol_qualifiers (parser); 23473 23474 /* An ObjC type name may consist of just protocol qualifiers, in which 23475 case the type shall default to 'id'. */ 23476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) 23477 { 23478 cp_type = cp_parser_type_id (parser); 23479 23480 /* If the type could not be parsed, an error has already 23481 been produced. For error recovery, behave as if it had 23482 not been specified, which will use the default type 23483 'id'. */ 23484 if (cp_type == error_mark_node) 23485 { 23486 cp_type = NULL_TREE; 23487 /* We need to skip to the closing parenthesis as 23488 cp_parser_type_id() does not seem to do it for 23489 us. */ 23490 cp_parser_skip_to_closing_parenthesis (parser, 23491 /*recovering=*/true, 23492 /*or_comma=*/false, 23493 /*consume_paren=*/false); 23494 } 23495 } 23496 23497 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23498 type_name = build_tree_list (proto_quals, cp_type); 23499 } 23500 23501 return type_name; 23502 } 23503 23504 /* Check to see if TYPE refers to an Objective-C selector name. */ 23505 23506 static bool 23507 cp_parser_objc_selector_p (enum cpp_ttype type) 23508 { 23509 return (type == CPP_NAME || type == CPP_KEYWORD 23510 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND 23511 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT 23512 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ 23513 || type == CPP_XOR || type == CPP_XOR_EQ); 23514 } 23515 23516 /* Parse an Objective-C selector. */ 23517 23518 static tree 23519 cp_parser_objc_selector (cp_parser* parser) 23520 { 23521 cp_token *token = cp_lexer_consume_token (parser->lexer); 23522 23523 if (!cp_parser_objc_selector_p (token->type)) 23524 { 23525 error_at (token->location, "invalid Objective-C++ selector name"); 23526 return error_mark_node; 23527 } 23528 23529 /* C++ operator names are allowed to appear in ObjC selectors. */ 23530 switch (token->type) 23531 { 23532 case CPP_AND_AND: return get_identifier ("and"); 23533 case CPP_AND_EQ: return get_identifier ("and_eq"); 23534 case CPP_AND: return get_identifier ("bitand"); 23535 case CPP_OR: return get_identifier ("bitor"); 23536 case CPP_COMPL: return get_identifier ("compl"); 23537 case CPP_NOT: return get_identifier ("not"); 23538 case CPP_NOT_EQ: return get_identifier ("not_eq"); 23539 case CPP_OR_OR: return get_identifier ("or"); 23540 case CPP_OR_EQ: return get_identifier ("or_eq"); 23541 case CPP_XOR: return get_identifier ("xor"); 23542 case CPP_XOR_EQ: return get_identifier ("xor_eq"); 23543 default: return token->u.value; 23544 } 23545 } 23546 23547 /* Parse an Objective-C params list. */ 23548 23549 static tree 23550 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes) 23551 { 23552 tree params = NULL_TREE; 23553 bool maybe_unary_selector_p = true; 23554 cp_token *token = cp_lexer_peek_token (parser->lexer); 23555 23556 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) 23557 { 23558 tree selector = NULL_TREE, type_name, identifier; 23559 tree parm_attr = NULL_TREE; 23560 23561 if (token->keyword == RID_ATTRIBUTE) 23562 break; 23563 23564 if (token->type != CPP_COLON) 23565 selector = cp_parser_objc_selector (parser); 23566 23567 /* Detect if we have a unary selector. */ 23568 if (maybe_unary_selector_p 23569 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) 23570 { 23571 params = selector; /* Might be followed by attributes. */ 23572 break; 23573 } 23574 23575 maybe_unary_selector_p = false; 23576 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)) 23577 { 23578 /* Something went quite wrong. There should be a colon 23579 here, but there is not. Stop parsing parameters. */ 23580 break; 23581 } 23582 type_name = cp_parser_objc_typename (parser); 23583 /* New ObjC allows attributes on parameters too. */ 23584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) 23585 parm_attr = cp_parser_attributes_opt (parser); 23586 identifier = cp_parser_identifier (parser); 23587 23588 params 23589 = chainon (params, 23590 objc_build_keyword_decl (selector, 23591 type_name, 23592 identifier, 23593 parm_attr)); 23594 23595 token = cp_lexer_peek_token (parser->lexer); 23596 } 23597 23598 if (params == NULL_TREE) 23599 { 23600 cp_parser_error (parser, "objective-c++ method declaration is expected"); 23601 return error_mark_node; 23602 } 23603 23604 /* We allow tail attributes for the method. */ 23605 if (token->keyword == RID_ATTRIBUTE) 23606 { 23607 *attributes = cp_parser_attributes_opt (parser); 23608 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 23609 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 23610 return params; 23611 cp_parser_error (parser, 23612 "method attributes must be specified at the end"); 23613 return error_mark_node; 23614 } 23615 23616 if (params == NULL_TREE) 23617 { 23618 cp_parser_error (parser, "objective-c++ method declaration is expected"); 23619 return error_mark_node; 23620 } 23621 return params; 23622 } 23623 23624 /* Parse the non-keyword Objective-C params. */ 23625 23626 static tree 23627 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 23628 tree* attributes) 23629 { 23630 tree params = make_node (TREE_LIST); 23631 cp_token *token = cp_lexer_peek_token (parser->lexer); 23632 *ellipsisp = false; /* Initially, assume no ellipsis. */ 23633 23634 while (token->type == CPP_COMMA) 23635 { 23636 cp_parameter_declarator *parmdecl; 23637 tree parm; 23638 23639 cp_lexer_consume_token (parser->lexer); /* Eat ','. */ 23640 token = cp_lexer_peek_token (parser->lexer); 23641 23642 if (token->type == CPP_ELLIPSIS) 23643 { 23644 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */ 23645 *ellipsisp = true; 23646 token = cp_lexer_peek_token (parser->lexer); 23647 break; 23648 } 23649 23650 /* TODO: parse attributes for tail parameters. */ 23651 parmdecl = cp_parser_parameter_declaration (parser, false, NULL); 23652 parm = grokdeclarator (parmdecl->declarator, 23653 &parmdecl->decl_specifiers, 23654 PARM, /*initialized=*/0, 23655 /*attrlist=*/NULL); 23656 23657 chainon (params, build_tree_list (NULL_TREE, parm)); 23658 token = cp_lexer_peek_token (parser->lexer); 23659 } 23660 23661 /* We allow tail attributes for the method. */ 23662 if (token->keyword == RID_ATTRIBUTE) 23663 { 23664 if (*attributes == NULL_TREE) 23665 { 23666 *attributes = cp_parser_attributes_opt (parser); 23667 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 23668 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 23669 return params; 23670 } 23671 else 23672 /* We have an error, but parse the attributes, so that we can 23673 carry on. */ 23674 *attributes = cp_parser_attributes_opt (parser); 23675 23676 cp_parser_error (parser, 23677 "method attributes must be specified at the end"); 23678 return error_mark_node; 23679 } 23680 23681 return params; 23682 } 23683 23684 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */ 23685 23686 static void 23687 cp_parser_objc_interstitial_code (cp_parser* parser) 23688 { 23689 cp_token *token = cp_lexer_peek_token (parser->lexer); 23690 23691 /* If the next token is `extern' and the following token is a string 23692 literal, then we have a linkage specification. */ 23693 if (token->keyword == RID_EXTERN 23694 && cp_parser_is_pure_string_literal 23695 (cp_lexer_peek_nth_token (parser->lexer, 2))) 23696 cp_parser_linkage_specification (parser); 23697 /* Handle #pragma, if any. */ 23698 else if (token->type == CPP_PRAGMA) 23699 cp_parser_pragma (parser, pragma_external); 23700 /* Allow stray semicolons. */ 23701 else if (token->type == CPP_SEMICOLON) 23702 cp_lexer_consume_token (parser->lexer); 23703 /* Mark methods as optional or required, when building protocols. */ 23704 else if (token->keyword == RID_AT_OPTIONAL) 23705 { 23706 cp_lexer_consume_token (parser->lexer); 23707 objc_set_method_opt (true); 23708 } 23709 else if (token->keyword == RID_AT_REQUIRED) 23710 { 23711 cp_lexer_consume_token (parser->lexer); 23712 objc_set_method_opt (false); 23713 } 23714 else if (token->keyword == RID_NAMESPACE) 23715 cp_parser_namespace_definition (parser); 23716 /* Other stray characters must generate errors. */ 23717 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE) 23718 { 23719 cp_lexer_consume_token (parser->lexer); 23720 error ("stray %qs between Objective-C++ methods", 23721 token->type == CPP_OPEN_BRACE ? "{" : "}"); 23722 } 23723 /* Finally, try to parse a block-declaration, or a function-definition. */ 23724 else 23725 cp_parser_block_declaration (parser, /*statement_p=*/false); 23726 } 23727 23728 /* Parse a method signature. */ 23729 23730 static tree 23731 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes) 23732 { 23733 tree rettype, kwdparms, optparms; 23734 bool ellipsis = false; 23735 bool is_class_method; 23736 23737 is_class_method = cp_parser_objc_method_type (parser); 23738 rettype = cp_parser_objc_typename (parser); 23739 *attributes = NULL_TREE; 23740 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes); 23741 if (kwdparms == error_mark_node) 23742 return error_mark_node; 23743 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes); 23744 if (optparms == error_mark_node) 23745 return error_mark_node; 23746 23747 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis); 23748 } 23749 23750 static bool 23751 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser) 23752 { 23753 tree tattr; 23754 cp_lexer_save_tokens (parser->lexer); 23755 tattr = cp_parser_attributes_opt (parser); 23756 gcc_assert (tattr) ; 23757 23758 /* If the attributes are followed by a method introducer, this is not allowed. 23759 Dump the attributes and flag the situation. */ 23760 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS) 23761 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS)) 23762 return true; 23763 23764 /* Otherwise, the attributes introduce some interstitial code, possibly so 23765 rewind to allow that check. */ 23766 cp_lexer_rollback_tokens (parser->lexer); 23767 return false; 23768 } 23769 23770 /* Parse an Objective-C method prototype list. */ 23771 23772 static void 23773 cp_parser_objc_method_prototype_list (cp_parser* parser) 23774 { 23775 cp_token *token = cp_lexer_peek_token (parser->lexer); 23776 23777 while (token->keyword != RID_AT_END && token->type != CPP_EOF) 23778 { 23779 if (token->type == CPP_PLUS || token->type == CPP_MINUS) 23780 { 23781 tree attributes, sig; 23782 bool is_class_method; 23783 if (token->type == CPP_PLUS) 23784 is_class_method = true; 23785 else 23786 is_class_method = false; 23787 sig = cp_parser_objc_method_signature (parser, &attributes); 23788 if (sig == error_mark_node) 23789 { 23790 cp_parser_skip_to_end_of_block_or_statement (parser); 23791 token = cp_lexer_peek_token (parser->lexer); 23792 continue; 23793 } 23794 objc_add_method_declaration (is_class_method, sig, attributes); 23795 cp_parser_consume_semicolon_at_end_of_statement (parser); 23796 } 23797 else if (token->keyword == RID_AT_PROPERTY) 23798 cp_parser_objc_at_property_declaration (parser); 23799 else if (token->keyword == RID_ATTRIBUTE 23800 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser)) 23801 warning_at (cp_lexer_peek_token (parser->lexer)->location, 23802 OPT_Wattributes, 23803 "prefix attributes are ignored for methods"); 23804 else 23805 /* Allow for interspersed non-ObjC++ code. */ 23806 cp_parser_objc_interstitial_code (parser); 23807 23808 token = cp_lexer_peek_token (parser->lexer); 23809 } 23810 23811 if (token->type != CPP_EOF) 23812 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ 23813 else 23814 cp_parser_error (parser, "expected %<@end%>"); 23815 23816 objc_finish_interface (); 23817 } 23818 23819 /* Parse an Objective-C method definition list. */ 23820 23821 static void 23822 cp_parser_objc_method_definition_list (cp_parser* parser) 23823 { 23824 cp_token *token = cp_lexer_peek_token (parser->lexer); 23825 23826 while (token->keyword != RID_AT_END && token->type != CPP_EOF) 23827 { 23828 tree meth; 23829 23830 if (token->type == CPP_PLUS || token->type == CPP_MINUS) 23831 { 23832 cp_token *ptk; 23833 tree sig, attribute; 23834 bool is_class_method; 23835 if (token->type == CPP_PLUS) 23836 is_class_method = true; 23837 else 23838 is_class_method = false; 23839 push_deferring_access_checks (dk_deferred); 23840 sig = cp_parser_objc_method_signature (parser, &attribute); 23841 if (sig == error_mark_node) 23842 { 23843 cp_parser_skip_to_end_of_block_or_statement (parser); 23844 token = cp_lexer_peek_token (parser->lexer); 23845 continue; 23846 } 23847 objc_start_method_definition (is_class_method, sig, attribute, 23848 NULL_TREE); 23849 23850 /* For historical reasons, we accept an optional semicolon. */ 23851 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 23852 cp_lexer_consume_token (parser->lexer); 23853 23854 ptk = cp_lexer_peek_token (parser->lexer); 23855 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 23856 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END)) 23857 { 23858 perform_deferred_access_checks (); 23859 stop_deferring_access_checks (); 23860 meth = cp_parser_function_definition_after_declarator (parser, 23861 false); 23862 pop_deferring_access_checks (); 23863 objc_finish_method_definition (meth); 23864 } 23865 } 23866 /* The following case will be removed once @synthesize is 23867 completely implemented. */ 23868 else if (token->keyword == RID_AT_PROPERTY) 23869 cp_parser_objc_at_property_declaration (parser); 23870 else if (token->keyword == RID_AT_SYNTHESIZE) 23871 cp_parser_objc_at_synthesize_declaration (parser); 23872 else if (token->keyword == RID_AT_DYNAMIC) 23873 cp_parser_objc_at_dynamic_declaration (parser); 23874 else if (token->keyword == RID_ATTRIBUTE 23875 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser)) 23876 warning_at (token->location, OPT_Wattributes, 23877 "prefix attributes are ignored for methods"); 23878 else 23879 /* Allow for interspersed non-ObjC++ code. */ 23880 cp_parser_objc_interstitial_code (parser); 23881 23882 token = cp_lexer_peek_token (parser->lexer); 23883 } 23884 23885 if (token->type != CPP_EOF) 23886 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ 23887 else 23888 cp_parser_error (parser, "expected %<@end%>"); 23889 23890 objc_finish_implementation (); 23891 } 23892 23893 /* Parse Objective-C ivars. */ 23894 23895 static void 23896 cp_parser_objc_class_ivars (cp_parser* parser) 23897 { 23898 cp_token *token = cp_lexer_peek_token (parser->lexer); 23899 23900 if (token->type != CPP_OPEN_BRACE) 23901 return; /* No ivars specified. */ 23902 23903 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */ 23904 token = cp_lexer_peek_token (parser->lexer); 23905 23906 while (token->type != CPP_CLOSE_BRACE 23907 && token->keyword != RID_AT_END && token->type != CPP_EOF) 23908 { 23909 cp_decl_specifier_seq declspecs; 23910 int decl_class_or_enum_p; 23911 tree prefix_attributes; 23912 23913 cp_parser_objc_visibility_spec (parser); 23914 23915 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 23916 break; 23917 23918 cp_parser_decl_specifier_seq (parser, 23919 CP_PARSER_FLAGS_OPTIONAL, 23920 &declspecs, 23921 &decl_class_or_enum_p); 23922 23923 /* auto, register, static, extern, mutable. */ 23924 if (declspecs.storage_class != sc_none) 23925 { 23926 cp_parser_error (parser, "invalid type for instance variable"); 23927 declspecs.storage_class = sc_none; 23928 } 23929 23930 /* __thread. */ 23931 if (declspecs.specs[(int) ds_thread]) 23932 { 23933 cp_parser_error (parser, "invalid type for instance variable"); 23934 declspecs.specs[(int) ds_thread] = 0; 23935 } 23936 23937 /* typedef. */ 23938 if (declspecs.specs[(int) ds_typedef]) 23939 { 23940 cp_parser_error (parser, "invalid type for instance variable"); 23941 declspecs.specs[(int) ds_typedef] = 0; 23942 } 23943 23944 prefix_attributes = declspecs.attributes; 23945 declspecs.attributes = NULL_TREE; 23946 23947 /* Keep going until we hit the `;' at the end of the 23948 declaration. */ 23949 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 23950 { 23951 tree width = NULL_TREE, attributes, first_attribute, decl; 23952 cp_declarator *declarator = NULL; 23953 int ctor_dtor_or_conv_p; 23954 23955 /* Check for a (possibly unnamed) bitfield declaration. */ 23956 token = cp_lexer_peek_token (parser->lexer); 23957 if (token->type == CPP_COLON) 23958 goto eat_colon; 23959 23960 if (token->type == CPP_NAME 23961 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 23962 == CPP_COLON)) 23963 { 23964 /* Get the name of the bitfield. */ 23965 declarator = make_id_declarator (NULL_TREE, 23966 cp_parser_identifier (parser), 23967 sfk_none); 23968 23969 eat_colon: 23970 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */ 23971 /* Get the width of the bitfield. */ 23972 width 23973 = cp_parser_constant_expression (parser, 23974 /*allow_non_constant=*/false, 23975 NULL); 23976 } 23977 else 23978 { 23979 /* Parse the declarator. */ 23980 declarator 23981 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 23982 &ctor_dtor_or_conv_p, 23983 /*parenthesized_p=*/NULL, 23984 /*member_p=*/false); 23985 } 23986 23987 /* Look for attributes that apply to the ivar. */ 23988 attributes = cp_parser_attributes_opt (parser); 23989 /* Remember which attributes are prefix attributes and 23990 which are not. */ 23991 first_attribute = attributes; 23992 /* Combine the attributes. */ 23993 attributes = chainon (prefix_attributes, attributes); 23994 23995 if (width) 23996 /* Create the bitfield declaration. */ 23997 decl = grokbitfield (declarator, &declspecs, 23998 width, 23999 attributes); 24000 else 24001 decl = grokfield (declarator, &declspecs, 24002 NULL_TREE, /*init_const_expr_p=*/false, 24003 NULL_TREE, attributes); 24004 24005 /* Add the instance variable. */ 24006 if (decl != error_mark_node && decl != NULL_TREE) 24007 objc_add_instance_variable (decl); 24008 24009 /* Reset PREFIX_ATTRIBUTES. */ 24010 while (attributes && TREE_CHAIN (attributes) != first_attribute) 24011 attributes = TREE_CHAIN (attributes); 24012 if (attributes) 24013 TREE_CHAIN (attributes) = NULL_TREE; 24014 24015 token = cp_lexer_peek_token (parser->lexer); 24016 24017 if (token->type == CPP_COMMA) 24018 { 24019 cp_lexer_consume_token (parser->lexer); /* Eat ','. */ 24020 continue; 24021 } 24022 break; 24023 } 24024 24025 cp_parser_consume_semicolon_at_end_of_statement (parser); 24026 token = cp_lexer_peek_token (parser->lexer); 24027 } 24028 24029 if (token->keyword == RID_AT_END) 24030 cp_parser_error (parser, "expected %<}%>"); 24031 24032 /* Do not consume the RID_AT_END, so it will be read again as terminating 24033 the @interface of @implementation. */ 24034 if (token->keyword != RID_AT_END && token->type != CPP_EOF) 24035 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */ 24036 24037 /* For historical reasons, we accept an optional semicolon. */ 24038 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 24039 cp_lexer_consume_token (parser->lexer); 24040 } 24041 24042 /* Parse an Objective-C protocol declaration. */ 24043 24044 static void 24045 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes) 24046 { 24047 tree proto, protorefs; 24048 cp_token *tok; 24049 24050 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */ 24051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)) 24052 { 24053 tok = cp_lexer_peek_token (parser->lexer); 24054 error_at (tok->location, "identifier expected after %<@protocol%>"); 24055 cp_parser_consume_semicolon_at_end_of_statement (parser); 24056 return; 24057 } 24058 24059 /* See if we have a forward declaration or a definition. */ 24060 tok = cp_lexer_peek_nth_token (parser->lexer, 2); 24061 24062 /* Try a forward declaration first. */ 24063 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON) 24064 { 24065 while (true) 24066 { 24067 tree id; 24068 24069 id = cp_parser_identifier (parser); 24070 if (id == error_mark_node) 24071 break; 24072 24073 objc_declare_protocol (id, attributes); 24074 24075 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 24076 cp_lexer_consume_token (parser->lexer); 24077 else 24078 break; 24079 } 24080 cp_parser_consume_semicolon_at_end_of_statement (parser); 24081 } 24082 24083 /* Ok, we got a full-fledged definition (or at least should). */ 24084 else 24085 { 24086 proto = cp_parser_identifier (parser); 24087 protorefs = cp_parser_objc_protocol_refs_opt (parser); 24088 objc_start_protocol (proto, protorefs, attributes); 24089 cp_parser_objc_method_prototype_list (parser); 24090 } 24091 } 24092 24093 /* Parse an Objective-C superclass or category. */ 24094 24095 static void 24096 cp_parser_objc_superclass_or_category (cp_parser *parser, 24097 bool iface_p, 24098 tree *super, 24099 tree *categ, bool *is_class_extension) 24100 { 24101 cp_token *next = cp_lexer_peek_token (parser->lexer); 24102 24103 *super = *categ = NULL_TREE; 24104 *is_class_extension = false; 24105 if (next->type == CPP_COLON) 24106 { 24107 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */ 24108 *super = cp_parser_identifier (parser); 24109 } 24110 else if (next->type == CPP_OPEN_PAREN) 24111 { 24112 cp_lexer_consume_token (parser->lexer); /* Eat '('. */ 24113 24114 /* If there is no category name, and this is an @interface, we 24115 have a class extension. */ 24116 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) 24117 { 24118 *categ = NULL_TREE; 24119 *is_class_extension = true; 24120 } 24121 else 24122 *categ = cp_parser_identifier (parser); 24123 24124 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 24125 } 24126 } 24127 24128 /* Parse an Objective-C class interface. */ 24129 24130 static void 24131 cp_parser_objc_class_interface (cp_parser* parser, tree attributes) 24132 { 24133 tree name, super, categ, protos; 24134 bool is_class_extension; 24135 24136 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */ 24137 name = cp_parser_identifier (parser); 24138 if (name == error_mark_node) 24139 { 24140 /* It's hard to recover because even if valid @interface stuff 24141 is to follow, we can't compile it (or validate it) if we 24142 don't even know which class it refers to. Let's assume this 24143 was a stray '@interface' token in the stream and skip it. 24144 */ 24145 return; 24146 } 24147 cp_parser_objc_superclass_or_category (parser, true, &super, &categ, 24148 &is_class_extension); 24149 protos = cp_parser_objc_protocol_refs_opt (parser); 24150 24151 /* We have either a class or a category on our hands. */ 24152 if (categ || is_class_extension) 24153 objc_start_category_interface (name, categ, protos, attributes); 24154 else 24155 { 24156 objc_start_class_interface (name, super, protos, attributes); 24157 /* Handle instance variable declarations, if any. */ 24158 cp_parser_objc_class_ivars (parser); 24159 objc_continue_interface (); 24160 } 24161 24162 cp_parser_objc_method_prototype_list (parser); 24163 } 24164 24165 /* Parse an Objective-C class implementation. */ 24166 24167 static void 24168 cp_parser_objc_class_implementation (cp_parser* parser) 24169 { 24170 tree name, super, categ; 24171 bool is_class_extension; 24172 24173 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */ 24174 name = cp_parser_identifier (parser); 24175 if (name == error_mark_node) 24176 { 24177 /* It's hard to recover because even if valid @implementation 24178 stuff is to follow, we can't compile it (or validate it) if 24179 we don't even know which class it refers to. Let's assume 24180 this was a stray '@implementation' token in the stream and 24181 skip it. 24182 */ 24183 return; 24184 } 24185 cp_parser_objc_superclass_or_category (parser, false, &super, &categ, 24186 &is_class_extension); 24187 24188 /* We have either a class or a category on our hands. */ 24189 if (categ) 24190 objc_start_category_implementation (name, categ); 24191 else 24192 { 24193 objc_start_class_implementation (name, super); 24194 /* Handle instance variable declarations, if any. */ 24195 cp_parser_objc_class_ivars (parser); 24196 objc_continue_implementation (); 24197 } 24198 24199 cp_parser_objc_method_definition_list (parser); 24200 } 24201 24202 /* Consume the @end token and finish off the implementation. */ 24203 24204 static void 24205 cp_parser_objc_end_implementation (cp_parser* parser) 24206 { 24207 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ 24208 objc_finish_implementation (); 24209 } 24210 24211 /* Parse an Objective-C declaration. */ 24212 24213 static void 24214 cp_parser_objc_declaration (cp_parser* parser, tree attributes) 24215 { 24216 /* Try to figure out what kind of declaration is present. */ 24217 cp_token *kwd = cp_lexer_peek_token (parser->lexer); 24218 24219 if (attributes) 24220 switch (kwd->keyword) 24221 { 24222 case RID_AT_ALIAS: 24223 case RID_AT_CLASS: 24224 case RID_AT_END: 24225 error_at (kwd->location, "attributes may not be specified before" 24226 " the %<@%D%> Objective-C++ keyword", 24227 kwd->u.value); 24228 attributes = NULL; 24229 break; 24230 case RID_AT_IMPLEMENTATION: 24231 warning_at (kwd->location, OPT_Wattributes, 24232 "prefix attributes are ignored before %<@%D%>", 24233 kwd->u.value); 24234 attributes = NULL; 24235 default: 24236 break; 24237 } 24238 24239 switch (kwd->keyword) 24240 { 24241 case RID_AT_ALIAS: 24242 cp_parser_objc_alias_declaration (parser); 24243 break; 24244 case RID_AT_CLASS: 24245 cp_parser_objc_class_declaration (parser); 24246 break; 24247 case RID_AT_PROTOCOL: 24248 cp_parser_objc_protocol_declaration (parser, attributes); 24249 break; 24250 case RID_AT_INTERFACE: 24251 cp_parser_objc_class_interface (parser, attributes); 24252 break; 24253 case RID_AT_IMPLEMENTATION: 24254 cp_parser_objc_class_implementation (parser); 24255 break; 24256 case RID_AT_END: 24257 cp_parser_objc_end_implementation (parser); 24258 break; 24259 default: 24260 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct", 24261 kwd->u.value); 24262 cp_parser_skip_to_end_of_block_or_statement (parser); 24263 } 24264 } 24265 24266 /* Parse an Objective-C try-catch-finally statement. 24267 24268 objc-try-catch-finally-stmt: 24269 @try compound-statement objc-catch-clause-seq [opt] 24270 objc-finally-clause [opt] 24271 24272 objc-catch-clause-seq: 24273 objc-catch-clause objc-catch-clause-seq [opt] 24274 24275 objc-catch-clause: 24276 @catch ( objc-exception-declaration ) compound-statement 24277 24278 objc-finally-clause: 24279 @finally compound-statement 24280 24281 objc-exception-declaration: 24282 parameter-declaration 24283 '...' 24284 24285 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS. 24286 24287 Returns NULL_TREE. 24288 24289 PS: This function is identical to c_parser_objc_try_catch_finally_statement 24290 for C. Keep them in sync. */ 24291 24292 static tree 24293 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) 24294 { 24295 location_t location; 24296 tree stmt; 24297 24298 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY); 24299 location = cp_lexer_peek_token (parser->lexer)->location; 24300 objc_maybe_warn_exceptions (location); 24301 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST 24302 node, lest it get absorbed into the surrounding block. */ 24303 stmt = push_stmt_list (); 24304 cp_parser_compound_statement (parser, NULL, false, false); 24305 objc_begin_try_stmt (location, pop_stmt_list (stmt)); 24306 24307 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH)) 24308 { 24309 cp_parameter_declarator *parm; 24310 tree parameter_declaration = error_mark_node; 24311 bool seen_open_paren = false; 24312 24313 cp_lexer_consume_token (parser->lexer); 24314 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 24315 seen_open_paren = true; 24316 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 24317 { 24318 /* We have "@catch (...)" (where the '...' are literally 24319 what is in the code). Skip the '...'. 24320 parameter_declaration is set to NULL_TREE, and 24321 objc_being_catch_clauses() knows that that means 24322 '...'. */ 24323 cp_lexer_consume_token (parser->lexer); 24324 parameter_declaration = NULL_TREE; 24325 } 24326 else 24327 { 24328 /* We have "@catch (NSException *exception)" or something 24329 like that. Parse the parameter declaration. */ 24330 parm = cp_parser_parameter_declaration (parser, false, NULL); 24331 if (parm == NULL) 24332 parameter_declaration = error_mark_node; 24333 else 24334 parameter_declaration = grokdeclarator (parm->declarator, 24335 &parm->decl_specifiers, 24336 PARM, /*initialized=*/0, 24337 /*attrlist=*/NULL); 24338 } 24339 if (seen_open_paren) 24340 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 24341 else 24342 { 24343 /* If there was no open parenthesis, we are recovering from 24344 an error, and we are trying to figure out what mistake 24345 the user has made. */ 24346 24347 /* If there is an immediate closing parenthesis, the user 24348 probably forgot the opening one (ie, they typed "@catch 24349 NSException *e)". Parse the closing parenthesis and keep 24350 going. */ 24351 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) 24352 cp_lexer_consume_token (parser->lexer); 24353 24354 /* If these is no immediate closing parenthesis, the user 24355 probably doesn't know that parenthesis are required at 24356 all (ie, they typed "@catch NSException *e"). So, just 24357 forget about the closing parenthesis and keep going. */ 24358 } 24359 objc_begin_catch_clause (parameter_declaration); 24360 cp_parser_compound_statement (parser, NULL, false, false); 24361 objc_finish_catch_clause (); 24362 } 24363 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY)) 24364 { 24365 cp_lexer_consume_token (parser->lexer); 24366 location = cp_lexer_peek_token (parser->lexer)->location; 24367 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST 24368 node, lest it get absorbed into the surrounding block. */ 24369 stmt = push_stmt_list (); 24370 cp_parser_compound_statement (parser, NULL, false, false); 24371 objc_build_finally_clause (location, pop_stmt_list (stmt)); 24372 } 24373 24374 return objc_finish_try_stmt (); 24375 } 24376 24377 /* Parse an Objective-C synchronized statement. 24378 24379 objc-synchronized-stmt: 24380 @synchronized ( expression ) compound-statement 24381 24382 Returns NULL_TREE. */ 24383 24384 static tree 24385 cp_parser_objc_synchronized_statement (cp_parser *parser) 24386 { 24387 location_t location; 24388 tree lock, stmt; 24389 24390 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED); 24391 24392 location = cp_lexer_peek_token (parser->lexer)->location; 24393 objc_maybe_warn_exceptions (location); 24394 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 24395 lock = cp_parser_expression (parser, false, NULL); 24396 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 24397 24398 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST 24399 node, lest it get absorbed into the surrounding block. */ 24400 stmt = push_stmt_list (); 24401 cp_parser_compound_statement (parser, NULL, false, false); 24402 24403 return objc_build_synchronized (location, lock, pop_stmt_list (stmt)); 24404 } 24405 24406 /* Parse an Objective-C throw statement. 24407 24408 objc-throw-stmt: 24409 @throw assignment-expression [opt] ; 24410 24411 Returns a constructed '@throw' statement. */ 24412 24413 static tree 24414 cp_parser_objc_throw_statement (cp_parser *parser) 24415 { 24416 tree expr = NULL_TREE; 24417 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 24418 24419 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW); 24420 24421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 24422 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 24423 24424 cp_parser_consume_semicolon_at_end_of_statement (parser); 24425 24426 return objc_build_throw_stmt (loc, expr); 24427 } 24428 24429 /* Parse an Objective-C statement. */ 24430 24431 static tree 24432 cp_parser_objc_statement (cp_parser * parser) 24433 { 24434 /* Try to figure out what kind of declaration is present. */ 24435 cp_token *kwd = cp_lexer_peek_token (parser->lexer); 24436 24437 switch (kwd->keyword) 24438 { 24439 case RID_AT_TRY: 24440 return cp_parser_objc_try_catch_finally_statement (parser); 24441 case RID_AT_SYNCHRONIZED: 24442 return cp_parser_objc_synchronized_statement (parser); 24443 case RID_AT_THROW: 24444 return cp_parser_objc_throw_statement (parser); 24445 default: 24446 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct", 24447 kwd->u.value); 24448 cp_parser_skip_to_end_of_block_or_statement (parser); 24449 } 24450 24451 return error_mark_node; 24452 } 24453 24454 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 24455 look ahead to see if an objc keyword follows the attributes. This 24456 is to detect the use of prefix attributes on ObjC @interface and 24457 @protocol. */ 24458 24459 static bool 24460 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib) 24461 { 24462 cp_lexer_save_tokens (parser->lexer); 24463 *attrib = cp_parser_attributes_opt (parser); 24464 gcc_assert (*attrib); 24465 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword)) 24466 { 24467 cp_lexer_commit_tokens (parser->lexer); 24468 return true; 24469 } 24470 cp_lexer_rollback_tokens (parser->lexer); 24471 return false; 24472 } 24473 24474 /* This routine is a minimal replacement for 24475 c_parser_struct_declaration () used when parsing the list of 24476 types/names or ObjC++ properties. For example, when parsing the 24477 code 24478 24479 @property (readonly) int a, b, c; 24480 24481 this function is responsible for parsing "int a, int b, int c" and 24482 returning the declarations as CHAIN of DECLs. 24483 24484 TODO: Share this code with cp_parser_objc_class_ivars. It's very 24485 similar parsing. */ 24486 static tree 24487 cp_parser_objc_struct_declaration (cp_parser *parser) 24488 { 24489 tree decls = NULL_TREE; 24490 cp_decl_specifier_seq declspecs; 24491 int decl_class_or_enum_p; 24492 tree prefix_attributes; 24493 24494 cp_parser_decl_specifier_seq (parser, 24495 CP_PARSER_FLAGS_NONE, 24496 &declspecs, 24497 &decl_class_or_enum_p); 24498 24499 if (declspecs.type == error_mark_node) 24500 return error_mark_node; 24501 24502 /* auto, register, static, extern, mutable. */ 24503 if (declspecs.storage_class != sc_none) 24504 { 24505 cp_parser_error (parser, "invalid type for property"); 24506 declspecs.storage_class = sc_none; 24507 } 24508 24509 /* __thread. */ 24510 if (declspecs.specs[(int) ds_thread]) 24511 { 24512 cp_parser_error (parser, "invalid type for property"); 24513 declspecs.specs[(int) ds_thread] = 0; 24514 } 24515 24516 /* typedef. */ 24517 if (declspecs.specs[(int) ds_typedef]) 24518 { 24519 cp_parser_error (parser, "invalid type for property"); 24520 declspecs.specs[(int) ds_typedef] = 0; 24521 } 24522 24523 prefix_attributes = declspecs.attributes; 24524 declspecs.attributes = NULL_TREE; 24525 24526 /* Keep going until we hit the `;' at the end of the declaration. */ 24527 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 24528 { 24529 tree attributes, first_attribute, decl; 24530 cp_declarator *declarator; 24531 cp_token *token; 24532 24533 /* Parse the declarator. */ 24534 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 24535 NULL, NULL, false); 24536 24537 /* Look for attributes that apply to the ivar. */ 24538 attributes = cp_parser_attributes_opt (parser); 24539 /* Remember which attributes are prefix attributes and 24540 which are not. */ 24541 first_attribute = attributes; 24542 /* Combine the attributes. */ 24543 attributes = chainon (prefix_attributes, attributes); 24544 24545 decl = grokfield (declarator, &declspecs, 24546 NULL_TREE, /*init_const_expr_p=*/false, 24547 NULL_TREE, attributes); 24548 24549 if (decl == error_mark_node || decl == NULL_TREE) 24550 return error_mark_node; 24551 24552 /* Reset PREFIX_ATTRIBUTES. */ 24553 while (attributes && TREE_CHAIN (attributes) != first_attribute) 24554 attributes = TREE_CHAIN (attributes); 24555 if (attributes) 24556 TREE_CHAIN (attributes) = NULL_TREE; 24557 24558 DECL_CHAIN (decl) = decls; 24559 decls = decl; 24560 24561 token = cp_lexer_peek_token (parser->lexer); 24562 if (token->type == CPP_COMMA) 24563 { 24564 cp_lexer_consume_token (parser->lexer); /* Eat ','. */ 24565 continue; 24566 } 24567 else 24568 break; 24569 } 24570 return decls; 24571 } 24572 24573 /* Parse an Objective-C @property declaration. The syntax is: 24574 24575 objc-property-declaration: 24576 '@property' objc-property-attributes[opt] struct-declaration ; 24577 24578 objc-property-attributes: 24579 '(' objc-property-attribute-list ')' 24580 24581 objc-property-attribute-list: 24582 objc-property-attribute 24583 objc-property-attribute-list, objc-property-attribute 24584 24585 objc-property-attribute 24586 'getter' = identifier 24587 'setter' = identifier 24588 'readonly' 24589 'readwrite' 24590 'assign' 24591 'retain' 24592 'copy' 24593 'nonatomic' 24594 24595 For example: 24596 @property NSString *name; 24597 @property (readonly) id object; 24598 @property (retain, nonatomic, getter=getTheName) id name; 24599 @property int a, b, c; 24600 24601 PS: This function is identical to 24602 c_parser_objc_at_property_declaration for C. Keep them in sync. */ 24603 static void 24604 cp_parser_objc_at_property_declaration (cp_parser *parser) 24605 { 24606 /* The following variables hold the attributes of the properties as 24607 parsed. They are 'false' or 'NULL_TREE' if the attribute was not 24608 seen. When we see an attribute, we set them to 'true' (if they 24609 are boolean properties) or to the identifier (if they have an 24610 argument, ie, for getter and setter). Note that here we only 24611 parse the list of attributes, check the syntax and accumulate the 24612 attributes that we find. objc_add_property_declaration() will 24613 then process the information. */ 24614 bool property_assign = false; 24615 bool property_copy = false; 24616 tree property_getter_ident = NULL_TREE; 24617 bool property_nonatomic = false; 24618 bool property_readonly = false; 24619 bool property_readwrite = false; 24620 bool property_retain = false; 24621 tree property_setter_ident = NULL_TREE; 24622 24623 /* 'properties' is the list of properties that we read. Usually a 24624 single one, but maybe more (eg, in "@property int a, b, c;" there 24625 are three). */ 24626 tree properties; 24627 location_t loc; 24628 24629 loc = cp_lexer_peek_token (parser->lexer)->location; 24630 24631 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */ 24632 24633 /* Parse the optional attribute list... */ 24634 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 24635 { 24636 /* Eat the '('. */ 24637 cp_lexer_consume_token (parser->lexer); 24638 24639 while (true) 24640 { 24641 bool syntax_error = false; 24642 cp_token *token = cp_lexer_peek_token (parser->lexer); 24643 enum rid keyword; 24644 24645 if (token->type != CPP_NAME) 24646 { 24647 cp_parser_error (parser, "expected identifier"); 24648 break; 24649 } 24650 keyword = C_RID_CODE (token->u.value); 24651 cp_lexer_consume_token (parser->lexer); 24652 switch (keyword) 24653 { 24654 case RID_ASSIGN: property_assign = true; break; 24655 case RID_COPY: property_copy = true; break; 24656 case RID_NONATOMIC: property_nonatomic = true; break; 24657 case RID_READONLY: property_readonly = true; break; 24658 case RID_READWRITE: property_readwrite = true; break; 24659 case RID_RETAIN: property_retain = true; break; 24660 24661 case RID_GETTER: 24662 case RID_SETTER: 24663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)) 24664 { 24665 if (keyword == RID_GETTER) 24666 cp_parser_error (parser, 24667 "missing %<=%> (after %<getter%> attribute)"); 24668 else 24669 cp_parser_error (parser, 24670 "missing %<=%> (after %<setter%> attribute)"); 24671 syntax_error = true; 24672 break; 24673 } 24674 cp_lexer_consume_token (parser->lexer); /* eat the = */ 24675 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type)) 24676 { 24677 cp_parser_error (parser, "expected identifier"); 24678 syntax_error = true; 24679 break; 24680 } 24681 if (keyword == RID_SETTER) 24682 { 24683 if (property_setter_ident != NULL_TREE) 24684 { 24685 cp_parser_error (parser, "the %<setter%> attribute may only be specified once"); 24686 cp_lexer_consume_token (parser->lexer); 24687 } 24688 else 24689 property_setter_ident = cp_parser_objc_selector (parser); 24690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) 24691 cp_parser_error (parser, "setter name must terminate with %<:%>"); 24692 else 24693 cp_lexer_consume_token (parser->lexer); 24694 } 24695 else 24696 { 24697 if (property_getter_ident != NULL_TREE) 24698 { 24699 cp_parser_error (parser, "the %<getter%> attribute may only be specified once"); 24700 cp_lexer_consume_token (parser->lexer); 24701 } 24702 else 24703 property_getter_ident = cp_parser_objc_selector (parser); 24704 } 24705 break; 24706 default: 24707 cp_parser_error (parser, "unknown property attribute"); 24708 syntax_error = true; 24709 break; 24710 } 24711 24712 if (syntax_error) 24713 break; 24714 24715 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 24716 cp_lexer_consume_token (parser->lexer); 24717 else 24718 break; 24719 } 24720 24721 /* FIXME: "@property (setter, assign);" will generate a spurious 24722 "error: expected ‘)’ before ‘,’ token". This is because 24723 cp_parser_require, unlike the C counterpart, will produce an 24724 error even if we are in error recovery. */ 24725 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 24726 { 24727 cp_parser_skip_to_closing_parenthesis (parser, 24728 /*recovering=*/true, 24729 /*or_comma=*/false, 24730 /*consume_paren=*/true); 24731 } 24732 } 24733 24734 /* ... and the property declaration(s). */ 24735 properties = cp_parser_objc_struct_declaration (parser); 24736 24737 if (properties == error_mark_node) 24738 { 24739 cp_parser_skip_to_end_of_statement (parser); 24740 /* If the next token is now a `;', consume it. */ 24741 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 24742 cp_lexer_consume_token (parser->lexer); 24743 return; 24744 } 24745 24746 if (properties == NULL_TREE) 24747 cp_parser_error (parser, "expected identifier"); 24748 else 24749 { 24750 /* Comma-separated properties are chained together in 24751 reverse order; add them one by one. */ 24752 properties = nreverse (properties); 24753 24754 for (; properties; properties = TREE_CHAIN (properties)) 24755 objc_add_property_declaration (loc, copy_node (properties), 24756 property_readonly, property_readwrite, 24757 property_assign, property_retain, 24758 property_copy, property_nonatomic, 24759 property_getter_ident, property_setter_ident); 24760 } 24761 24762 cp_parser_consume_semicolon_at_end_of_statement (parser); 24763 } 24764 24765 /* Parse an Objective-C++ @synthesize declaration. The syntax is: 24766 24767 objc-synthesize-declaration: 24768 @synthesize objc-synthesize-identifier-list ; 24769 24770 objc-synthesize-identifier-list: 24771 objc-synthesize-identifier 24772 objc-synthesize-identifier-list, objc-synthesize-identifier 24773 24774 objc-synthesize-identifier 24775 identifier 24776 identifier = identifier 24777 24778 For example: 24779 @synthesize MyProperty; 24780 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty; 24781 24782 PS: This function is identical to c_parser_objc_at_synthesize_declaration 24783 for C. Keep them in sync. 24784 */ 24785 static void 24786 cp_parser_objc_at_synthesize_declaration (cp_parser *parser) 24787 { 24788 tree list = NULL_TREE; 24789 location_t loc; 24790 loc = cp_lexer_peek_token (parser->lexer)->location; 24791 24792 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */ 24793 while (true) 24794 { 24795 tree property, ivar; 24796 property = cp_parser_identifier (parser); 24797 if (property == error_mark_node) 24798 { 24799 cp_parser_consume_semicolon_at_end_of_statement (parser); 24800 return; 24801 } 24802 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 24803 { 24804 cp_lexer_consume_token (parser->lexer); 24805 ivar = cp_parser_identifier (parser); 24806 if (ivar == error_mark_node) 24807 { 24808 cp_parser_consume_semicolon_at_end_of_statement (parser); 24809 return; 24810 } 24811 } 24812 else 24813 ivar = NULL_TREE; 24814 list = chainon (list, build_tree_list (ivar, property)); 24815 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 24816 cp_lexer_consume_token (parser->lexer); 24817 else 24818 break; 24819 } 24820 cp_parser_consume_semicolon_at_end_of_statement (parser); 24821 objc_add_synthesize_declaration (loc, list); 24822 } 24823 24824 /* Parse an Objective-C++ @dynamic declaration. The syntax is: 24825 24826 objc-dynamic-declaration: 24827 @dynamic identifier-list ; 24828 24829 For example: 24830 @dynamic MyProperty; 24831 @dynamic MyProperty, AnotherProperty; 24832 24833 PS: This function is identical to c_parser_objc_at_dynamic_declaration 24834 for C. Keep them in sync. 24835 */ 24836 static void 24837 cp_parser_objc_at_dynamic_declaration (cp_parser *parser) 24838 { 24839 tree list = NULL_TREE; 24840 location_t loc; 24841 loc = cp_lexer_peek_token (parser->lexer)->location; 24842 24843 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */ 24844 while (true) 24845 { 24846 tree property; 24847 property = cp_parser_identifier (parser); 24848 if (property == error_mark_node) 24849 { 24850 cp_parser_consume_semicolon_at_end_of_statement (parser); 24851 return; 24852 } 24853 list = chainon (list, build_tree_list (NULL, property)); 24854 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 24855 cp_lexer_consume_token (parser->lexer); 24856 else 24857 break; 24858 } 24859 cp_parser_consume_semicolon_at_end_of_statement (parser); 24860 objc_add_dynamic_declaration (loc, list); 24861 } 24862 24863 24864 /* OpenMP 2.5 parsing routines. */ 24865 24866 /* Returns name of the next clause. 24867 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and 24868 the token is not consumed. Otherwise appropriate pragma_omp_clause is 24869 returned and the token is consumed. */ 24870 24871 static pragma_omp_clause 24872 cp_parser_omp_clause_name (cp_parser *parser) 24873 { 24874 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE; 24875 24876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF)) 24877 result = PRAGMA_OMP_CLAUSE_IF; 24878 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)) 24879 result = PRAGMA_OMP_CLAUSE_DEFAULT; 24880 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE)) 24881 result = PRAGMA_OMP_CLAUSE_PRIVATE; 24882 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 24883 { 24884 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 24885 const char *p = IDENTIFIER_POINTER (id); 24886 24887 switch (p[0]) 24888 { 24889 case 'c': 24890 if (!strcmp ("collapse", p)) 24891 result = PRAGMA_OMP_CLAUSE_COLLAPSE; 24892 else if (!strcmp ("copyin", p)) 24893 result = PRAGMA_OMP_CLAUSE_COPYIN; 24894 else if (!strcmp ("copyprivate", p)) 24895 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE; 24896 break; 24897 case 'f': 24898 if (!strcmp ("final", p)) 24899 result = PRAGMA_OMP_CLAUSE_FINAL; 24900 else if (!strcmp ("firstprivate", p)) 24901 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE; 24902 break; 24903 case 'l': 24904 if (!strcmp ("lastprivate", p)) 24905 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE; 24906 break; 24907 case 'm': 24908 if (!strcmp ("mergeable", p)) 24909 result = PRAGMA_OMP_CLAUSE_MERGEABLE; 24910 break; 24911 case 'n': 24912 if (!strcmp ("nowait", p)) 24913 result = PRAGMA_OMP_CLAUSE_NOWAIT; 24914 else if (!strcmp ("num_threads", p)) 24915 result = PRAGMA_OMP_CLAUSE_NUM_THREADS; 24916 break; 24917 case 'o': 24918 if (!strcmp ("ordered", p)) 24919 result = PRAGMA_OMP_CLAUSE_ORDERED; 24920 break; 24921 case 'r': 24922 if (!strcmp ("reduction", p)) 24923 result = PRAGMA_OMP_CLAUSE_REDUCTION; 24924 break; 24925 case 's': 24926 if (!strcmp ("schedule", p)) 24927 result = PRAGMA_OMP_CLAUSE_SCHEDULE; 24928 else if (!strcmp ("shared", p)) 24929 result = PRAGMA_OMP_CLAUSE_SHARED; 24930 break; 24931 case 'u': 24932 if (!strcmp ("untied", p)) 24933 result = PRAGMA_OMP_CLAUSE_UNTIED; 24934 break; 24935 } 24936 } 24937 24938 if (result != PRAGMA_OMP_CLAUSE_NONE) 24939 cp_lexer_consume_token (parser->lexer); 24940 24941 return result; 24942 } 24943 24944 /* Validate that a clause of the given type does not already exist. */ 24945 24946 static void 24947 check_no_duplicate_clause (tree clauses, enum omp_clause_code code, 24948 const char *name, location_t location) 24949 { 24950 tree c; 24951 24952 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) 24953 if (OMP_CLAUSE_CODE (c) == code) 24954 { 24955 error_at (location, "too many %qs clauses", name); 24956 break; 24957 } 24958 } 24959 24960 /* OpenMP 2.5: 24961 variable-list: 24962 identifier 24963 variable-list , identifier 24964 24965 In addition, we match a closing parenthesis. An opening parenthesis 24966 will have been consumed by the caller. 24967 24968 If KIND is nonzero, create the appropriate node and install the decl 24969 in OMP_CLAUSE_DECL and add the node to the head of the list. 24970 24971 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE; 24972 return the list created. */ 24973 24974 static tree 24975 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind, 24976 tree list) 24977 { 24978 cp_token *token; 24979 while (1) 24980 { 24981 tree name, decl; 24982 24983 token = cp_lexer_peek_token (parser->lexer); 24984 name = cp_parser_id_expression (parser, /*template_p=*/false, 24985 /*check_dependency_p=*/true, 24986 /*template_p=*/NULL, 24987 /*declarator_p=*/false, 24988 /*optional_p=*/false); 24989 if (name == error_mark_node) 24990 goto skip_comma; 24991 24992 decl = cp_parser_lookup_name_simple (parser, name, token->location); 24993 if (decl == error_mark_node) 24994 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, 24995 token->location); 24996 else if (kind != 0) 24997 { 24998 tree u = build_omp_clause (token->location, kind); 24999 OMP_CLAUSE_DECL (u) = decl; 25000 OMP_CLAUSE_CHAIN (u) = list; 25001 list = u; 25002 } 25003 else 25004 list = tree_cons (decl, NULL_TREE, list); 25005 25006 get_comma: 25007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 25008 break; 25009 cp_lexer_consume_token (parser->lexer); 25010 } 25011 25012 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25013 { 25014 int ending; 25015 25016 /* Try to resync to an unnested comma. Copied from 25017 cp_parser_parenthesized_expression_list. */ 25018 skip_comma: 25019 ending = cp_parser_skip_to_closing_parenthesis (parser, 25020 /*recovering=*/true, 25021 /*or_comma=*/true, 25022 /*consume_paren=*/true); 25023 if (ending < 0) 25024 goto get_comma; 25025 } 25026 25027 return list; 25028 } 25029 25030 /* Similarly, but expect leading and trailing parenthesis. This is a very 25031 common case for omp clauses. */ 25032 25033 static tree 25034 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list) 25035 { 25036 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25037 return cp_parser_omp_var_list_no_open (parser, kind, list); 25038 return list; 25039 } 25040 25041 /* OpenMP 3.0: 25042 collapse ( constant-expression ) */ 25043 25044 static tree 25045 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location) 25046 { 25047 tree c, num; 25048 location_t loc; 25049 HOST_WIDE_INT n; 25050 25051 loc = cp_lexer_peek_token (parser->lexer)->location; 25052 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25053 return list; 25054 25055 num = cp_parser_constant_expression (parser, false, NULL); 25056 25057 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25058 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25059 /*or_comma=*/false, 25060 /*consume_paren=*/true); 25061 25062 if (num == error_mark_node) 25063 return list; 25064 num = fold_non_dependent_expr (num); 25065 if (!INTEGRAL_TYPE_P (TREE_TYPE (num)) 25066 || !host_integerp (num, 0) 25067 || (n = tree_low_cst (num, 0)) <= 0 25068 || (int) n != n) 25069 { 25070 error_at (loc, "collapse argument needs positive constant integer expression"); 25071 return list; 25072 } 25073 25074 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location); 25075 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE); 25076 OMP_CLAUSE_CHAIN (c) = list; 25077 OMP_CLAUSE_COLLAPSE_EXPR (c) = num; 25078 25079 return c; 25080 } 25081 25082 /* OpenMP 2.5: 25083 default ( shared | none ) */ 25084 25085 static tree 25086 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location) 25087 { 25088 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED; 25089 tree c; 25090 25091 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25092 return list; 25093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 25094 { 25095 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 25096 const char *p = IDENTIFIER_POINTER (id); 25097 25098 switch (p[0]) 25099 { 25100 case 'n': 25101 if (strcmp ("none", p) != 0) 25102 goto invalid_kind; 25103 kind = OMP_CLAUSE_DEFAULT_NONE; 25104 break; 25105 25106 case 's': 25107 if (strcmp ("shared", p) != 0) 25108 goto invalid_kind; 25109 kind = OMP_CLAUSE_DEFAULT_SHARED; 25110 break; 25111 25112 default: 25113 goto invalid_kind; 25114 } 25115 25116 cp_lexer_consume_token (parser->lexer); 25117 } 25118 else 25119 { 25120 invalid_kind: 25121 cp_parser_error (parser, "expected %<none%> or %<shared%>"); 25122 } 25123 25124 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25125 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25126 /*or_comma=*/false, 25127 /*consume_paren=*/true); 25128 25129 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED) 25130 return list; 25131 25132 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location); 25133 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT); 25134 OMP_CLAUSE_CHAIN (c) = list; 25135 OMP_CLAUSE_DEFAULT_KIND (c) = kind; 25136 25137 return c; 25138 } 25139 25140 /* OpenMP 3.1: 25141 final ( expression ) */ 25142 25143 static tree 25144 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location) 25145 { 25146 tree t, c; 25147 25148 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25149 return list; 25150 25151 t = cp_parser_condition (parser); 25152 25153 if (t == error_mark_node 25154 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25156 /*or_comma=*/false, 25157 /*consume_paren=*/true); 25158 25159 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location); 25160 25161 c = build_omp_clause (location, OMP_CLAUSE_FINAL); 25162 OMP_CLAUSE_FINAL_EXPR (c) = t; 25163 OMP_CLAUSE_CHAIN (c) = list; 25164 25165 return c; 25166 } 25167 25168 /* OpenMP 2.5: 25169 if ( expression ) */ 25170 25171 static tree 25172 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location) 25173 { 25174 tree t, c; 25175 25176 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25177 return list; 25178 25179 t = cp_parser_condition (parser); 25180 25181 if (t == error_mark_node 25182 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25183 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25184 /*or_comma=*/false, 25185 /*consume_paren=*/true); 25186 25187 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location); 25188 25189 c = build_omp_clause (location, OMP_CLAUSE_IF); 25190 OMP_CLAUSE_IF_EXPR (c) = t; 25191 OMP_CLAUSE_CHAIN (c) = list; 25192 25193 return c; 25194 } 25195 25196 /* OpenMP 3.1: 25197 mergeable */ 25198 25199 static tree 25200 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED, 25201 tree list, location_t location) 25202 { 25203 tree c; 25204 25205 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable", 25206 location); 25207 25208 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE); 25209 OMP_CLAUSE_CHAIN (c) = list; 25210 return c; 25211 } 25212 25213 /* OpenMP 2.5: 25214 nowait */ 25215 25216 static tree 25217 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, 25218 tree list, location_t location) 25219 { 25220 tree c; 25221 25222 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location); 25223 25224 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT); 25225 OMP_CLAUSE_CHAIN (c) = list; 25226 return c; 25227 } 25228 25229 /* OpenMP 2.5: 25230 num_threads ( expression ) */ 25231 25232 static tree 25233 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list, 25234 location_t location) 25235 { 25236 tree t, c; 25237 25238 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25239 return list; 25240 25241 t = cp_parser_expression (parser, false, NULL); 25242 25243 if (t == error_mark_node 25244 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25245 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25246 /*or_comma=*/false, 25247 /*consume_paren=*/true); 25248 25249 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, 25250 "num_threads", location); 25251 25252 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS); 25253 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t; 25254 OMP_CLAUSE_CHAIN (c) = list; 25255 25256 return c; 25257 } 25258 25259 /* OpenMP 2.5: 25260 ordered */ 25261 25262 static tree 25263 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, 25264 tree list, location_t location) 25265 { 25266 tree c; 25267 25268 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, 25269 "ordered", location); 25270 25271 c = build_omp_clause (location, OMP_CLAUSE_ORDERED); 25272 OMP_CLAUSE_CHAIN (c) = list; 25273 return c; 25274 } 25275 25276 /* OpenMP 2.5: 25277 reduction ( reduction-operator : variable-list ) 25278 25279 reduction-operator: 25280 One of: + * - & ^ | && || 25281 25282 OpenMP 3.1: 25283 25284 reduction-operator: 25285 One of: + * - & ^ | && || min max */ 25286 25287 static tree 25288 cp_parser_omp_clause_reduction (cp_parser *parser, tree list) 25289 { 25290 enum tree_code code; 25291 tree nlist, c; 25292 25293 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25294 return list; 25295 25296 switch (cp_lexer_peek_token (parser->lexer)->type) 25297 { 25298 case CPP_PLUS: 25299 code = PLUS_EXPR; 25300 break; 25301 case CPP_MULT: 25302 code = MULT_EXPR; 25303 break; 25304 case CPP_MINUS: 25305 code = MINUS_EXPR; 25306 break; 25307 case CPP_AND: 25308 code = BIT_AND_EXPR; 25309 break; 25310 case CPP_XOR: 25311 code = BIT_XOR_EXPR; 25312 break; 25313 case CPP_OR: 25314 code = BIT_IOR_EXPR; 25315 break; 25316 case CPP_AND_AND: 25317 code = TRUTH_ANDIF_EXPR; 25318 break; 25319 case CPP_OR_OR: 25320 code = TRUTH_ORIF_EXPR; 25321 break; 25322 case CPP_NAME: 25323 { 25324 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 25325 const char *p = IDENTIFIER_POINTER (id); 25326 25327 if (strcmp (p, "min") == 0) 25328 { 25329 code = MIN_EXPR; 25330 break; 25331 } 25332 if (strcmp (p, "max") == 0) 25333 { 25334 code = MAX_EXPR; 25335 break; 25336 } 25337 } 25338 /* FALLTHROUGH */ 25339 default: 25340 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, " 25341 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>"); 25342 resync_fail: 25343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25344 /*or_comma=*/false, 25345 /*consume_paren=*/true); 25346 return list; 25347 } 25348 cp_lexer_consume_token (parser->lexer); 25349 25350 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)) 25351 goto resync_fail; 25352 25353 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list); 25354 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c)) 25355 OMP_CLAUSE_REDUCTION_CODE (c) = code; 25356 25357 return nlist; 25358 } 25359 25360 /* OpenMP 2.5: 25361 schedule ( schedule-kind ) 25362 schedule ( schedule-kind , expression ) 25363 25364 schedule-kind: 25365 static | dynamic | guided | runtime | auto */ 25366 25367 static tree 25368 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location) 25369 { 25370 tree c, t; 25371 25372 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25373 return list; 25374 25375 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE); 25376 25377 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 25378 { 25379 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 25380 const char *p = IDENTIFIER_POINTER (id); 25381 25382 switch (p[0]) 25383 { 25384 case 'd': 25385 if (strcmp ("dynamic", p) != 0) 25386 goto invalid_kind; 25387 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC; 25388 break; 25389 25390 case 'g': 25391 if (strcmp ("guided", p) != 0) 25392 goto invalid_kind; 25393 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED; 25394 break; 25395 25396 case 'r': 25397 if (strcmp ("runtime", p) != 0) 25398 goto invalid_kind; 25399 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME; 25400 break; 25401 25402 default: 25403 goto invalid_kind; 25404 } 25405 } 25406 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC)) 25407 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC; 25408 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)) 25409 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO; 25410 else 25411 goto invalid_kind; 25412 cp_lexer_consume_token (parser->lexer); 25413 25414 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 25415 { 25416 cp_token *token; 25417 cp_lexer_consume_token (parser->lexer); 25418 25419 token = cp_lexer_peek_token (parser->lexer); 25420 t = cp_parser_assignment_expression (parser, false, NULL); 25421 25422 if (t == error_mark_node) 25423 goto resync_fail; 25424 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME) 25425 error_at (token->location, "schedule %<runtime%> does not take " 25426 "a %<chunk_size%> parameter"); 25427 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO) 25428 error_at (token->location, "schedule %<auto%> does not take " 25429 "a %<chunk_size%> parameter"); 25430 else 25431 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t; 25432 25433 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25434 goto resync_fail; 25435 } 25436 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN)) 25437 goto resync_fail; 25438 25439 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location); 25440 OMP_CLAUSE_CHAIN (c) = list; 25441 return c; 25442 25443 invalid_kind: 25444 cp_parser_error (parser, "invalid schedule kind"); 25445 resync_fail: 25446 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25447 /*or_comma=*/false, 25448 /*consume_paren=*/true); 25449 return list; 25450 } 25451 25452 /* OpenMP 3.0: 25453 untied */ 25454 25455 static tree 25456 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED, 25457 tree list, location_t location) 25458 { 25459 tree c; 25460 25461 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location); 25462 25463 c = build_omp_clause (location, OMP_CLAUSE_UNTIED); 25464 OMP_CLAUSE_CHAIN (c) = list; 25465 return c; 25466 } 25467 25468 /* Parse all OpenMP clauses. The set clauses allowed by the directive 25469 is a bitmask in MASK. Return the list of clauses found; the result 25470 of clause default goes in *pdefault. */ 25471 25472 static tree 25473 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask, 25474 const char *where, cp_token *pragma_tok) 25475 { 25476 tree clauses = NULL; 25477 bool first = true; 25478 cp_token *token = NULL; 25479 25480 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)) 25481 { 25482 pragma_omp_clause c_kind; 25483 const char *c_name; 25484 tree prev = clauses; 25485 25486 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 25487 cp_lexer_consume_token (parser->lexer); 25488 25489 token = cp_lexer_peek_token (parser->lexer); 25490 c_kind = cp_parser_omp_clause_name (parser); 25491 first = false; 25492 25493 switch (c_kind) 25494 { 25495 case PRAGMA_OMP_CLAUSE_COLLAPSE: 25496 clauses = cp_parser_omp_clause_collapse (parser, clauses, 25497 token->location); 25498 c_name = "collapse"; 25499 break; 25500 case PRAGMA_OMP_CLAUSE_COPYIN: 25501 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses); 25502 c_name = "copyin"; 25503 break; 25504 case PRAGMA_OMP_CLAUSE_COPYPRIVATE: 25505 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE, 25506 clauses); 25507 c_name = "copyprivate"; 25508 break; 25509 case PRAGMA_OMP_CLAUSE_DEFAULT: 25510 clauses = cp_parser_omp_clause_default (parser, clauses, 25511 token->location); 25512 c_name = "default"; 25513 break; 25514 case PRAGMA_OMP_CLAUSE_FINAL: 25515 clauses = cp_parser_omp_clause_final (parser, clauses, token->location); 25516 c_name = "final"; 25517 break; 25518 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE: 25519 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE, 25520 clauses); 25521 c_name = "firstprivate"; 25522 break; 25523 case PRAGMA_OMP_CLAUSE_IF: 25524 clauses = cp_parser_omp_clause_if (parser, clauses, token->location); 25525 c_name = "if"; 25526 break; 25527 case PRAGMA_OMP_CLAUSE_LASTPRIVATE: 25528 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE, 25529 clauses); 25530 c_name = "lastprivate"; 25531 break; 25532 case PRAGMA_OMP_CLAUSE_MERGEABLE: 25533 clauses = cp_parser_omp_clause_mergeable (parser, clauses, 25534 token->location); 25535 c_name = "mergeable"; 25536 break; 25537 case PRAGMA_OMP_CLAUSE_NOWAIT: 25538 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location); 25539 c_name = "nowait"; 25540 break; 25541 case PRAGMA_OMP_CLAUSE_NUM_THREADS: 25542 clauses = cp_parser_omp_clause_num_threads (parser, clauses, 25543 token->location); 25544 c_name = "num_threads"; 25545 break; 25546 case PRAGMA_OMP_CLAUSE_ORDERED: 25547 clauses = cp_parser_omp_clause_ordered (parser, clauses, 25548 token->location); 25549 c_name = "ordered"; 25550 break; 25551 case PRAGMA_OMP_CLAUSE_PRIVATE: 25552 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, 25553 clauses); 25554 c_name = "private"; 25555 break; 25556 case PRAGMA_OMP_CLAUSE_REDUCTION: 25557 clauses = cp_parser_omp_clause_reduction (parser, clauses); 25558 c_name = "reduction"; 25559 break; 25560 case PRAGMA_OMP_CLAUSE_SCHEDULE: 25561 clauses = cp_parser_omp_clause_schedule (parser, clauses, 25562 token->location); 25563 c_name = "schedule"; 25564 break; 25565 case PRAGMA_OMP_CLAUSE_SHARED: 25566 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED, 25567 clauses); 25568 c_name = "shared"; 25569 break; 25570 case PRAGMA_OMP_CLAUSE_UNTIED: 25571 clauses = cp_parser_omp_clause_untied (parser, clauses, 25572 token->location); 25573 c_name = "nowait"; 25574 break; 25575 default: 25576 cp_parser_error (parser, "expected %<#pragma omp%> clause"); 25577 goto saw_error; 25578 } 25579 25580 if (((mask >> c_kind) & 1) == 0) 25581 { 25582 /* Remove the invalid clause(s) from the list to avoid 25583 confusing the rest of the compiler. */ 25584 clauses = prev; 25585 error_at (token->location, "%qs is not valid for %qs", c_name, where); 25586 } 25587 } 25588 saw_error: 25589 cp_parser_skip_to_pragma_eol (parser, pragma_tok); 25590 return finish_omp_clauses (clauses); 25591 } 25592 25593 /* OpenMP 2.5: 25594 structured-block: 25595 statement 25596 25597 In practice, we're also interested in adding the statement to an 25598 outer node. So it is convenient if we work around the fact that 25599 cp_parser_statement calls add_stmt. */ 25600 25601 static unsigned 25602 cp_parser_begin_omp_structured_block (cp_parser *parser) 25603 { 25604 unsigned save = parser->in_statement; 25605 25606 /* Only move the values to IN_OMP_BLOCK if they weren't false. 25607 This preserves the "not within loop or switch" style error messages 25608 for nonsense cases like 25609 void foo() { 25610 #pragma omp single 25611 break; 25612 } 25613 */ 25614 if (parser->in_statement) 25615 parser->in_statement = IN_OMP_BLOCK; 25616 25617 return save; 25618 } 25619 25620 static void 25621 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save) 25622 { 25623 parser->in_statement = save; 25624 } 25625 25626 static tree 25627 cp_parser_omp_structured_block (cp_parser *parser) 25628 { 25629 tree stmt = begin_omp_structured_block (); 25630 unsigned int save = cp_parser_begin_omp_structured_block (parser); 25631 25632 cp_parser_statement (parser, NULL_TREE, false, NULL); 25633 25634 cp_parser_end_omp_structured_block (parser, save); 25635 return finish_omp_structured_block (stmt); 25636 } 25637 25638 /* OpenMP 2.5: 25639 # pragma omp atomic new-line 25640 expression-stmt 25641 25642 expression-stmt: 25643 x binop= expr | x++ | ++x | x-- | --x 25644 binop: 25645 +, *, -, /, &, ^, |, <<, >> 25646 25647 where x is an lvalue expression with scalar type. 25648 25649 OpenMP 3.1: 25650 # pragma omp atomic new-line 25651 update-stmt 25652 25653 # pragma omp atomic read new-line 25654 read-stmt 25655 25656 # pragma omp atomic write new-line 25657 write-stmt 25658 25659 # pragma omp atomic update new-line 25660 update-stmt 25661 25662 # pragma omp atomic capture new-line 25663 capture-stmt 25664 25665 # pragma omp atomic capture new-line 25666 capture-block 25667 25668 read-stmt: 25669 v = x 25670 write-stmt: 25671 x = expr 25672 update-stmt: 25673 expression-stmt | x = x binop expr 25674 capture-stmt: 25675 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x 25676 capture-block: 25677 { v = x; update-stmt; } | { update-stmt; v = x; } 25678 25679 where x and v are lvalue expressions with scalar type. */ 25680 25681 static void 25682 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok) 25683 { 25684 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE; 25685 tree rhs1 = NULL_TREE, orig_lhs; 25686 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR; 25687 bool structured_block = false; 25688 25689 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 25690 { 25691 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 25692 const char *p = IDENTIFIER_POINTER (id); 25693 25694 if (!strcmp (p, "read")) 25695 code = OMP_ATOMIC_READ; 25696 else if (!strcmp (p, "write")) 25697 code = NOP_EXPR; 25698 else if (!strcmp (p, "update")) 25699 code = OMP_ATOMIC; 25700 else if (!strcmp (p, "capture")) 25701 code = OMP_ATOMIC_CAPTURE_NEW; 25702 else 25703 p = NULL; 25704 if (p) 25705 cp_lexer_consume_token (parser->lexer); 25706 } 25707 cp_parser_require_pragma_eol (parser, pragma_tok); 25708 25709 switch (code) 25710 { 25711 case OMP_ATOMIC_READ: 25712 case NOP_EXPR: /* atomic write */ 25713 v = cp_parser_unary_expression (parser, /*address_p=*/false, 25714 /*cast_p=*/false, NULL); 25715 if (v == error_mark_node) 25716 goto saw_error; 25717 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 25718 goto saw_error; 25719 if (code == NOP_EXPR) 25720 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL); 25721 else 25722 lhs = cp_parser_unary_expression (parser, /*address_p=*/false, 25723 /*cast_p=*/false, NULL); 25724 if (lhs == error_mark_node) 25725 goto saw_error; 25726 if (code == NOP_EXPR) 25727 { 25728 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR 25729 opcode. */ 25730 code = OMP_ATOMIC; 25731 rhs = lhs; 25732 lhs = v; 25733 v = NULL_TREE; 25734 } 25735 goto done; 25736 case OMP_ATOMIC_CAPTURE_NEW: 25737 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 25738 { 25739 cp_lexer_consume_token (parser->lexer); 25740 structured_block = true; 25741 } 25742 else 25743 { 25744 v = cp_parser_unary_expression (parser, /*address_p=*/false, 25745 /*cast_p=*/false, NULL); 25746 if (v == error_mark_node) 25747 goto saw_error; 25748 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 25749 goto saw_error; 25750 } 25751 default: 25752 break; 25753 } 25754 25755 restart: 25756 lhs = cp_parser_unary_expression (parser, /*address_p=*/false, 25757 /*cast_p=*/false, NULL); 25758 orig_lhs = lhs; 25759 switch (TREE_CODE (lhs)) 25760 { 25761 case ERROR_MARK: 25762 goto saw_error; 25763 25764 case POSTINCREMENT_EXPR: 25765 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block) 25766 code = OMP_ATOMIC_CAPTURE_OLD; 25767 /* FALLTHROUGH */ 25768 case PREINCREMENT_EXPR: 25769 lhs = TREE_OPERAND (lhs, 0); 25770 opcode = PLUS_EXPR; 25771 rhs = integer_one_node; 25772 break; 25773 25774 case POSTDECREMENT_EXPR: 25775 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block) 25776 code = OMP_ATOMIC_CAPTURE_OLD; 25777 /* FALLTHROUGH */ 25778 case PREDECREMENT_EXPR: 25779 lhs = TREE_OPERAND (lhs, 0); 25780 opcode = MINUS_EXPR; 25781 rhs = integer_one_node; 25782 break; 25783 25784 case COMPOUND_EXPR: 25785 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR 25786 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR 25787 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR 25788 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0) 25789 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND 25790 (TREE_OPERAND (lhs, 1), 0), 0))) 25791 == BOOLEAN_TYPE) 25792 /* Undo effects of boolean_increment for post {in,de}crement. */ 25793 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0); 25794 /* FALLTHRU */ 25795 case MODIFY_EXPR: 25796 if (TREE_CODE (lhs) == MODIFY_EXPR 25797 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE) 25798 { 25799 /* Undo effects of boolean_increment. */ 25800 if (integer_onep (TREE_OPERAND (lhs, 1))) 25801 { 25802 /* This is pre or post increment. */ 25803 rhs = TREE_OPERAND (lhs, 1); 25804 lhs = TREE_OPERAND (lhs, 0); 25805 opcode = NOP_EXPR; 25806 if (code == OMP_ATOMIC_CAPTURE_NEW 25807 && !structured_block 25808 && TREE_CODE (orig_lhs) == COMPOUND_EXPR) 25809 code = OMP_ATOMIC_CAPTURE_OLD; 25810 break; 25811 } 25812 } 25813 /* FALLTHRU */ 25814 default: 25815 switch (cp_lexer_peek_token (parser->lexer)->type) 25816 { 25817 case CPP_MULT_EQ: 25818 opcode = MULT_EXPR; 25819 break; 25820 case CPP_DIV_EQ: 25821 opcode = TRUNC_DIV_EXPR; 25822 break; 25823 case CPP_PLUS_EQ: 25824 opcode = PLUS_EXPR; 25825 break; 25826 case CPP_MINUS_EQ: 25827 opcode = MINUS_EXPR; 25828 break; 25829 case CPP_LSHIFT_EQ: 25830 opcode = LSHIFT_EXPR; 25831 break; 25832 case CPP_RSHIFT_EQ: 25833 opcode = RSHIFT_EXPR; 25834 break; 25835 case CPP_AND_EQ: 25836 opcode = BIT_AND_EXPR; 25837 break; 25838 case CPP_OR_EQ: 25839 opcode = BIT_IOR_EXPR; 25840 break; 25841 case CPP_XOR_EQ: 25842 opcode = BIT_XOR_EXPR; 25843 break; 25844 case CPP_EQ: 25845 if (structured_block || code == OMP_ATOMIC) 25846 { 25847 enum cp_parser_prec oprec; 25848 cp_token *token; 25849 cp_lexer_consume_token (parser->lexer); 25850 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false, 25851 /*cast_p=*/false, NULL); 25852 if (rhs1 == error_mark_node) 25853 goto saw_error; 25854 token = cp_lexer_peek_token (parser->lexer); 25855 switch (token->type) 25856 { 25857 case CPP_SEMICOLON: 25858 if (code == OMP_ATOMIC_CAPTURE_NEW) 25859 { 25860 code = OMP_ATOMIC_CAPTURE_OLD; 25861 v = lhs; 25862 lhs = NULL_TREE; 25863 lhs1 = rhs1; 25864 rhs1 = NULL_TREE; 25865 cp_lexer_consume_token (parser->lexer); 25866 goto restart; 25867 } 25868 cp_parser_error (parser, 25869 "invalid form of %<#pragma omp atomic%>"); 25870 goto saw_error; 25871 case CPP_MULT: 25872 opcode = MULT_EXPR; 25873 break; 25874 case CPP_DIV: 25875 opcode = TRUNC_DIV_EXPR; 25876 break; 25877 case CPP_PLUS: 25878 opcode = PLUS_EXPR; 25879 break; 25880 case CPP_MINUS: 25881 opcode = MINUS_EXPR; 25882 break; 25883 case CPP_LSHIFT: 25884 opcode = LSHIFT_EXPR; 25885 break; 25886 case CPP_RSHIFT: 25887 opcode = RSHIFT_EXPR; 25888 break; 25889 case CPP_AND: 25890 opcode = BIT_AND_EXPR; 25891 break; 25892 case CPP_OR: 25893 opcode = BIT_IOR_EXPR; 25894 break; 25895 case CPP_XOR: 25896 opcode = BIT_XOR_EXPR; 25897 break; 25898 default: 25899 cp_parser_error (parser, 25900 "invalid operator for %<#pragma omp atomic%>"); 25901 goto saw_error; 25902 } 25903 oprec = TOKEN_PRECEDENCE (token); 25904 gcc_assert (oprec != PREC_NOT_OPERATOR); 25905 if (commutative_tree_code (opcode)) 25906 oprec = (enum cp_parser_prec) (oprec - 1); 25907 cp_lexer_consume_token (parser->lexer); 25908 rhs = cp_parser_binary_expression (parser, false, false, 25909 oprec, NULL); 25910 if (rhs == error_mark_node) 25911 goto saw_error; 25912 goto stmt_done; 25913 } 25914 /* FALLTHROUGH */ 25915 default: 25916 cp_parser_error (parser, 25917 "invalid operator for %<#pragma omp atomic%>"); 25918 goto saw_error; 25919 } 25920 cp_lexer_consume_token (parser->lexer); 25921 25922 rhs = cp_parser_expression (parser, false, NULL); 25923 if (rhs == error_mark_node) 25924 goto saw_error; 25925 break; 25926 } 25927 stmt_done: 25928 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW) 25929 { 25930 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)) 25931 goto saw_error; 25932 v = cp_parser_unary_expression (parser, /*address_p=*/false, 25933 /*cast_p=*/false, NULL); 25934 if (v == error_mark_node) 25935 goto saw_error; 25936 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 25937 goto saw_error; 25938 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false, 25939 /*cast_p=*/false, NULL); 25940 if (lhs1 == error_mark_node) 25941 goto saw_error; 25942 } 25943 if (structured_block) 25944 { 25945 cp_parser_consume_semicolon_at_end_of_statement (parser); 25946 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 25947 } 25948 done: 25949 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1); 25950 if (!structured_block) 25951 cp_parser_consume_semicolon_at_end_of_statement (parser); 25952 return; 25953 25954 saw_error: 25955 cp_parser_skip_to_end_of_block_or_statement (parser); 25956 if (structured_block) 25957 { 25958 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 25959 cp_lexer_consume_token (parser->lexer); 25960 else if (code == OMP_ATOMIC_CAPTURE_NEW) 25961 { 25962 cp_parser_skip_to_end_of_block_or_statement (parser); 25963 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 25964 cp_lexer_consume_token (parser->lexer); 25965 } 25966 } 25967 } 25968 25969 25970 /* OpenMP 2.5: 25971 # pragma omp barrier new-line */ 25972 25973 static void 25974 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok) 25975 { 25976 cp_parser_require_pragma_eol (parser, pragma_tok); 25977 finish_omp_barrier (); 25978 } 25979 25980 /* OpenMP 2.5: 25981 # pragma omp critical [(name)] new-line 25982 structured-block */ 25983 25984 static tree 25985 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok) 25986 { 25987 tree stmt, name = NULL; 25988 25989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 25990 { 25991 cp_lexer_consume_token (parser->lexer); 25992 25993 name = cp_parser_identifier (parser); 25994 25995 if (name == error_mark_node 25996 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25997 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25998 /*or_comma=*/false, 25999 /*consume_paren=*/true); 26000 if (name == error_mark_node) 26001 name = NULL; 26002 } 26003 cp_parser_require_pragma_eol (parser, pragma_tok); 26004 26005 stmt = cp_parser_omp_structured_block (parser); 26006 return c_finish_omp_critical (input_location, stmt, name); 26007 } 26008 26009 /* OpenMP 2.5: 26010 # pragma omp flush flush-vars[opt] new-line 26011 26012 flush-vars: 26013 ( variable-list ) */ 26014 26015 static void 26016 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok) 26017 { 26018 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 26019 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL); 26020 cp_parser_require_pragma_eol (parser, pragma_tok); 26021 26022 finish_omp_flush (); 26023 } 26024 26025 /* Helper function, to parse omp for increment expression. */ 26026 26027 static tree 26028 cp_parser_omp_for_cond (cp_parser *parser, tree decl) 26029 { 26030 tree cond = cp_parser_binary_expression (parser, false, true, 26031 PREC_NOT_OPERATOR, NULL); 26032 if (cond == error_mark_node 26033 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 26034 { 26035 cp_parser_skip_to_end_of_statement (parser); 26036 return error_mark_node; 26037 } 26038 26039 switch (TREE_CODE (cond)) 26040 { 26041 case GT_EXPR: 26042 case GE_EXPR: 26043 case LT_EXPR: 26044 case LE_EXPR: 26045 break; 26046 default: 26047 return error_mark_node; 26048 } 26049 26050 /* If decl is an iterator, preserve LHS and RHS of the relational 26051 expr until finish_omp_for. */ 26052 if (decl 26053 && (type_dependent_expression_p (decl) 26054 || CLASS_TYPE_P (TREE_TYPE (decl)))) 26055 return cond; 26056 26057 return build_x_binary_op (TREE_CODE (cond), 26058 TREE_OPERAND (cond, 0), ERROR_MARK, 26059 TREE_OPERAND (cond, 1), ERROR_MARK, 26060 /*overload=*/NULL, tf_warning_or_error); 26061 } 26062 26063 /* Helper function, to parse omp for increment expression. */ 26064 26065 static tree 26066 cp_parser_omp_for_incr (cp_parser *parser, tree decl) 26067 { 26068 cp_token *token = cp_lexer_peek_token (parser->lexer); 26069 enum tree_code op; 26070 tree lhs, rhs; 26071 cp_id_kind idk; 26072 bool decl_first; 26073 26074 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS) 26075 { 26076 op = (token->type == CPP_PLUS_PLUS 26077 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR); 26078 cp_lexer_consume_token (parser->lexer); 26079 lhs = cp_parser_cast_expression (parser, false, false, NULL); 26080 if (lhs != decl) 26081 return error_mark_node; 26082 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE); 26083 } 26084 26085 lhs = cp_parser_primary_expression (parser, false, false, false, &idk); 26086 if (lhs != decl) 26087 return error_mark_node; 26088 26089 token = cp_lexer_peek_token (parser->lexer); 26090 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS) 26091 { 26092 op = (token->type == CPP_PLUS_PLUS 26093 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR); 26094 cp_lexer_consume_token (parser->lexer); 26095 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE); 26096 } 26097 26098 op = cp_parser_assignment_operator_opt (parser); 26099 if (op == ERROR_MARK) 26100 return error_mark_node; 26101 26102 if (op != NOP_EXPR) 26103 { 26104 rhs = cp_parser_assignment_expression (parser, false, NULL); 26105 rhs = build2 (op, TREE_TYPE (decl), decl, rhs); 26106 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs); 26107 } 26108 26109 lhs = cp_parser_binary_expression (parser, false, false, 26110 PREC_ADDITIVE_EXPRESSION, NULL); 26111 token = cp_lexer_peek_token (parser->lexer); 26112 decl_first = lhs == decl; 26113 if (decl_first) 26114 lhs = NULL_TREE; 26115 if (token->type != CPP_PLUS 26116 && token->type != CPP_MINUS) 26117 return error_mark_node; 26118 26119 do 26120 { 26121 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR; 26122 cp_lexer_consume_token (parser->lexer); 26123 rhs = cp_parser_binary_expression (parser, false, false, 26124 PREC_ADDITIVE_EXPRESSION, NULL); 26125 token = cp_lexer_peek_token (parser->lexer); 26126 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first) 26127 { 26128 if (lhs == NULL_TREE) 26129 { 26130 if (op == PLUS_EXPR) 26131 lhs = rhs; 26132 else 26133 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error); 26134 } 26135 else 26136 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK, 26137 NULL, tf_warning_or_error); 26138 } 26139 } 26140 while (token->type == CPP_PLUS || token->type == CPP_MINUS); 26141 26142 if (!decl_first) 26143 { 26144 if (rhs != decl || op == MINUS_EXPR) 26145 return error_mark_node; 26146 rhs = build2 (op, TREE_TYPE (decl), lhs, decl); 26147 } 26148 else 26149 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs); 26150 26151 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs); 26152 } 26153 26154 /* Parse the restricted form of the for statement allowed by OpenMP. */ 26155 26156 static tree 26157 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses) 26158 { 26159 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret; 26160 tree real_decl, initv, condv, incrv, declv; 26161 tree this_pre_body, cl; 26162 location_t loc_first; 26163 bool collapse_err = false; 26164 int i, collapse = 1, nbraces = 0; 26165 VEC(tree,gc) *for_block = make_tree_vector (); 26166 26167 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl)) 26168 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE) 26169 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0); 26170 26171 gcc_assert (collapse >= 1); 26172 26173 declv = make_tree_vec (collapse); 26174 initv = make_tree_vec (collapse); 26175 condv = make_tree_vec (collapse); 26176 incrv = make_tree_vec (collapse); 26177 26178 loc_first = cp_lexer_peek_token (parser->lexer)->location; 26179 26180 for (i = 0; i < collapse; i++) 26181 { 26182 int bracecount = 0; 26183 bool add_private_clause = false; 26184 location_t loc; 26185 26186 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR)) 26187 { 26188 cp_parser_error (parser, "for statement expected"); 26189 return NULL; 26190 } 26191 loc = cp_lexer_consume_token (parser->lexer)->location; 26192 26193 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 26194 return NULL; 26195 26196 init = decl = real_decl = NULL; 26197 this_pre_body = push_stmt_list (); 26198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 26199 { 26200 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too): 26201 26202 init-expr: 26203 var = lb 26204 integer-type var = lb 26205 random-access-iterator-type var = lb 26206 pointer-type var = lb 26207 */ 26208 cp_decl_specifier_seq type_specifiers; 26209 26210 /* First, try to parse as an initialized declaration. See 26211 cp_parser_condition, from whence the bulk of this is copied. */ 26212 26213 cp_parser_parse_tentatively (parser); 26214 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true, 26215 /*is_trailing_return=*/false, 26216 &type_specifiers); 26217 if (cp_parser_parse_definitely (parser)) 26218 { 26219 /* If parsing a type specifier seq succeeded, then this 26220 MUST be a initialized declaration. */ 26221 tree asm_specification, attributes; 26222 cp_declarator *declarator; 26223 26224 declarator = cp_parser_declarator (parser, 26225 CP_PARSER_DECLARATOR_NAMED, 26226 /*ctor_dtor_or_conv_p=*/NULL, 26227 /*parenthesized_p=*/NULL, 26228 /*member_p=*/false); 26229 attributes = cp_parser_attributes_opt (parser); 26230 asm_specification = cp_parser_asm_specification_opt (parser); 26231 26232 if (declarator == cp_error_declarator) 26233 cp_parser_skip_to_end_of_statement (parser); 26234 26235 else 26236 { 26237 tree pushed_scope, auto_node; 26238 26239 decl = start_decl (declarator, &type_specifiers, 26240 SD_INITIALIZED, attributes, 26241 /*prefix_attributes=*/NULL_TREE, 26242 &pushed_scope); 26243 26244 auto_node = type_uses_auto (TREE_TYPE (decl)); 26245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)) 26246 { 26247 if (cp_lexer_next_token_is (parser->lexer, 26248 CPP_OPEN_PAREN)) 26249 error ("parenthesized initialization is not allowed in " 26250 "OpenMP %<for%> loop"); 26251 else 26252 /* Trigger an error. */ 26253 cp_parser_require (parser, CPP_EQ, RT_EQ); 26254 26255 init = error_mark_node; 26256 cp_parser_skip_to_end_of_statement (parser); 26257 } 26258 else if (CLASS_TYPE_P (TREE_TYPE (decl)) 26259 || type_dependent_expression_p (decl) 26260 || auto_node) 26261 { 26262 bool is_direct_init, is_non_constant_init; 26263 26264 init = cp_parser_initializer (parser, 26265 &is_direct_init, 26266 &is_non_constant_init); 26267 26268 if (auto_node) 26269 { 26270 TREE_TYPE (decl) 26271 = do_auto_deduction (TREE_TYPE (decl), init, 26272 auto_node); 26273 26274 if (!CLASS_TYPE_P (TREE_TYPE (decl)) 26275 && !type_dependent_expression_p (decl)) 26276 goto non_class; 26277 } 26278 26279 cp_finish_decl (decl, init, !is_non_constant_init, 26280 asm_specification, 26281 LOOKUP_ONLYCONVERTING); 26282 if (CLASS_TYPE_P (TREE_TYPE (decl))) 26283 { 26284 VEC_safe_push (tree, gc, for_block, this_pre_body); 26285 init = NULL_TREE; 26286 } 26287 else 26288 init = pop_stmt_list (this_pre_body); 26289 this_pre_body = NULL_TREE; 26290 } 26291 else 26292 { 26293 /* Consume '='. */ 26294 cp_lexer_consume_token (parser->lexer); 26295 init = cp_parser_assignment_expression (parser, false, NULL); 26296 26297 non_class: 26298 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE) 26299 init = error_mark_node; 26300 else 26301 cp_finish_decl (decl, NULL_TREE, 26302 /*init_const_expr_p=*/false, 26303 asm_specification, 26304 LOOKUP_ONLYCONVERTING); 26305 } 26306 26307 if (pushed_scope) 26308 pop_scope (pushed_scope); 26309 } 26310 } 26311 else 26312 { 26313 cp_id_kind idk; 26314 /* If parsing a type specifier sequence failed, then 26315 this MUST be a simple expression. */ 26316 cp_parser_parse_tentatively (parser); 26317 decl = cp_parser_primary_expression (parser, false, false, 26318 false, &idk); 26319 if (!cp_parser_error_occurred (parser) 26320 && decl 26321 && DECL_P (decl) 26322 && CLASS_TYPE_P (TREE_TYPE (decl))) 26323 { 26324 tree rhs; 26325 26326 cp_parser_parse_definitely (parser); 26327 cp_parser_require (parser, CPP_EQ, RT_EQ); 26328 rhs = cp_parser_assignment_expression (parser, false, NULL); 26329 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR, 26330 rhs, 26331 tf_warning_or_error)); 26332 add_private_clause = true; 26333 } 26334 else 26335 { 26336 decl = NULL; 26337 cp_parser_abort_tentative_parse (parser); 26338 init = cp_parser_expression (parser, false, NULL); 26339 if (init) 26340 { 26341 if (TREE_CODE (init) == MODIFY_EXPR 26342 || TREE_CODE (init) == MODOP_EXPR) 26343 real_decl = TREE_OPERAND (init, 0); 26344 } 26345 } 26346 } 26347 } 26348 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 26349 if (this_pre_body) 26350 { 26351 this_pre_body = pop_stmt_list (this_pre_body); 26352 if (pre_body) 26353 { 26354 tree t = pre_body; 26355 pre_body = push_stmt_list (); 26356 add_stmt (t); 26357 add_stmt (this_pre_body); 26358 pre_body = pop_stmt_list (pre_body); 26359 } 26360 else 26361 pre_body = this_pre_body; 26362 } 26363 26364 if (decl) 26365 real_decl = decl; 26366 if (par_clauses != NULL && real_decl != NULL_TREE) 26367 { 26368 tree *c; 26369 for (c = par_clauses; *c ; ) 26370 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE 26371 && OMP_CLAUSE_DECL (*c) == real_decl) 26372 { 26373 error_at (loc, "iteration variable %qD" 26374 " should not be firstprivate", real_decl); 26375 *c = OMP_CLAUSE_CHAIN (*c); 26376 } 26377 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE 26378 && OMP_CLAUSE_DECL (*c) == real_decl) 26379 { 26380 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES, 26381 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */ 26382 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE); 26383 OMP_CLAUSE_DECL (l) = real_decl; 26384 OMP_CLAUSE_CHAIN (l) = clauses; 26385 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c); 26386 clauses = l; 26387 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED); 26388 CP_OMP_CLAUSE_INFO (*c) = NULL; 26389 add_private_clause = false; 26390 } 26391 else 26392 { 26393 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE 26394 && OMP_CLAUSE_DECL (*c) == real_decl) 26395 add_private_clause = false; 26396 c = &OMP_CLAUSE_CHAIN (*c); 26397 } 26398 } 26399 26400 if (add_private_clause) 26401 { 26402 tree c; 26403 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) 26404 { 26405 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE 26406 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) 26407 && OMP_CLAUSE_DECL (c) == decl) 26408 break; 26409 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE 26410 && OMP_CLAUSE_DECL (c) == decl) 26411 error_at (loc, "iteration variable %qD " 26412 "should not be firstprivate", 26413 decl); 26414 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION 26415 && OMP_CLAUSE_DECL (c) == decl) 26416 error_at (loc, "iteration variable %qD should not be reduction", 26417 decl); 26418 } 26419 if (c == NULL) 26420 { 26421 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE); 26422 OMP_CLAUSE_DECL (c) = decl; 26423 c = finish_omp_clauses (c); 26424 if (c) 26425 { 26426 OMP_CLAUSE_CHAIN (c) = clauses; 26427 clauses = c; 26428 } 26429 } 26430 } 26431 26432 cond = NULL; 26433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 26434 cond = cp_parser_omp_for_cond (parser, decl); 26435 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 26436 26437 incr = NULL; 26438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) 26439 { 26440 /* If decl is an iterator, preserve the operator on decl 26441 until finish_omp_for. */ 26442 if (real_decl 26443 && ((processing_template_decl 26444 && !POINTER_TYPE_P (TREE_TYPE (real_decl))) 26445 || CLASS_TYPE_P (TREE_TYPE (real_decl)))) 26446 incr = cp_parser_omp_for_incr (parser, real_decl); 26447 else 26448 incr = cp_parser_expression (parser, false, NULL); 26449 } 26450 26451 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 26452 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 26453 /*or_comma=*/false, 26454 /*consume_paren=*/true); 26455 26456 TREE_VEC_ELT (declv, i) = decl; 26457 TREE_VEC_ELT (initv, i) = init; 26458 TREE_VEC_ELT (condv, i) = cond; 26459 TREE_VEC_ELT (incrv, i) = incr; 26460 26461 if (i == collapse - 1) 26462 break; 26463 26464 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed 26465 in between the collapsed for loops to be still considered perfectly 26466 nested. Hopefully the final version clarifies this. 26467 For now handle (multiple) {'s and empty statements. */ 26468 cp_parser_parse_tentatively (parser); 26469 do 26470 { 26471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR)) 26472 break; 26473 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 26474 { 26475 cp_lexer_consume_token (parser->lexer); 26476 bracecount++; 26477 } 26478 else if (bracecount 26479 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 26480 cp_lexer_consume_token (parser->lexer); 26481 else 26482 { 26483 loc = cp_lexer_peek_token (parser->lexer)->location; 26484 error_at (loc, "not enough collapsed for loops"); 26485 collapse_err = true; 26486 cp_parser_abort_tentative_parse (parser); 26487 declv = NULL_TREE; 26488 break; 26489 } 26490 } 26491 while (1); 26492 26493 if (declv) 26494 { 26495 cp_parser_parse_definitely (parser); 26496 nbraces += bracecount; 26497 } 26498 } 26499 26500 /* Note that we saved the original contents of this flag when we entered 26501 the structured block, and so we don't need to re-save it here. */ 26502 parser->in_statement = IN_OMP_FOR; 26503 26504 /* Note that the grammar doesn't call for a structured block here, 26505 though the loop as a whole is a structured block. */ 26506 body = push_stmt_list (); 26507 cp_parser_statement (parser, NULL_TREE, false, NULL); 26508 body = pop_stmt_list (body); 26509 26510 if (declv == NULL_TREE) 26511 ret = NULL_TREE; 26512 else 26513 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body, 26514 pre_body, clauses); 26515 26516 while (nbraces) 26517 { 26518 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 26519 { 26520 cp_lexer_consume_token (parser->lexer); 26521 nbraces--; 26522 } 26523 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 26524 cp_lexer_consume_token (parser->lexer); 26525 else 26526 { 26527 if (!collapse_err) 26528 { 26529 error_at (cp_lexer_peek_token (parser->lexer)->location, 26530 "collapsed loops not perfectly nested"); 26531 } 26532 collapse_err = true; 26533 cp_parser_statement_seq_opt (parser, NULL); 26534 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) 26535 break; 26536 } 26537 } 26538 26539 while (!VEC_empty (tree, for_block)) 26540 add_stmt (pop_stmt_list (VEC_pop (tree, for_block))); 26541 release_tree_vector (for_block); 26542 26543 return ret; 26544 } 26545 26546 /* OpenMP 2.5: 26547 #pragma omp for for-clause[optseq] new-line 26548 for-loop */ 26549 26550 #define OMP_FOR_CLAUSE_MASK \ 26551 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26552 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26553 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ 26554 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 26555 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \ 26556 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \ 26557 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \ 26558 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE)) 26559 26560 static tree 26561 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok) 26562 { 26563 tree clauses, sb, ret; 26564 unsigned int save; 26565 26566 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK, 26567 "#pragma omp for", pragma_tok); 26568 26569 sb = begin_omp_structured_block (); 26570 save = cp_parser_begin_omp_structured_block (parser); 26571 26572 ret = cp_parser_omp_for_loop (parser, clauses, NULL); 26573 26574 cp_parser_end_omp_structured_block (parser, save); 26575 add_stmt (finish_omp_structured_block (sb)); 26576 26577 return ret; 26578 } 26579 26580 /* OpenMP 2.5: 26581 # pragma omp master new-line 26582 structured-block */ 26583 26584 static tree 26585 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok) 26586 { 26587 cp_parser_require_pragma_eol (parser, pragma_tok); 26588 return c_finish_omp_master (input_location, 26589 cp_parser_omp_structured_block (parser)); 26590 } 26591 26592 /* OpenMP 2.5: 26593 # pragma omp ordered new-line 26594 structured-block */ 26595 26596 static tree 26597 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok) 26598 { 26599 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 26600 cp_parser_require_pragma_eol (parser, pragma_tok); 26601 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser)); 26602 } 26603 26604 /* OpenMP 2.5: 26605 26606 section-scope: 26607 { section-sequence } 26608 26609 section-sequence: 26610 section-directive[opt] structured-block 26611 section-sequence section-directive structured-block */ 26612 26613 static tree 26614 cp_parser_omp_sections_scope (cp_parser *parser) 26615 { 26616 tree stmt, substmt; 26617 bool error_suppress = false; 26618 cp_token *tok; 26619 26620 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 26621 return NULL_TREE; 26622 26623 stmt = push_stmt_list (); 26624 26625 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION) 26626 { 26627 unsigned save; 26628 26629 substmt = begin_omp_structured_block (); 26630 save = cp_parser_begin_omp_structured_block (parser); 26631 26632 while (1) 26633 { 26634 cp_parser_statement (parser, NULL_TREE, false, NULL); 26635 26636 tok = cp_lexer_peek_token (parser->lexer); 26637 if (tok->pragma_kind == PRAGMA_OMP_SECTION) 26638 break; 26639 if (tok->type == CPP_CLOSE_BRACE) 26640 break; 26641 if (tok->type == CPP_EOF) 26642 break; 26643 } 26644 26645 cp_parser_end_omp_structured_block (parser, save); 26646 substmt = finish_omp_structured_block (substmt); 26647 substmt = build1 (OMP_SECTION, void_type_node, substmt); 26648 add_stmt (substmt); 26649 } 26650 26651 while (1) 26652 { 26653 tok = cp_lexer_peek_token (parser->lexer); 26654 if (tok->type == CPP_CLOSE_BRACE) 26655 break; 26656 if (tok->type == CPP_EOF) 26657 break; 26658 26659 if (tok->pragma_kind == PRAGMA_OMP_SECTION) 26660 { 26661 cp_lexer_consume_token (parser->lexer); 26662 cp_parser_require_pragma_eol (parser, tok); 26663 error_suppress = false; 26664 } 26665 else if (!error_suppress) 26666 { 26667 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>"); 26668 error_suppress = true; 26669 } 26670 26671 substmt = cp_parser_omp_structured_block (parser); 26672 substmt = build1 (OMP_SECTION, void_type_node, substmt); 26673 add_stmt (substmt); 26674 } 26675 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 26676 26677 substmt = pop_stmt_list (stmt); 26678 26679 stmt = make_node (OMP_SECTIONS); 26680 TREE_TYPE (stmt) = void_type_node; 26681 OMP_SECTIONS_BODY (stmt) = substmt; 26682 26683 add_stmt (stmt); 26684 return stmt; 26685 } 26686 26687 /* OpenMP 2.5: 26688 # pragma omp sections sections-clause[optseq] newline 26689 sections-scope */ 26690 26691 #define OMP_SECTIONS_CLAUSE_MASK \ 26692 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26693 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26694 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ 26695 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 26696 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 26697 26698 static tree 26699 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok) 26700 { 26701 tree clauses, ret; 26702 26703 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK, 26704 "#pragma omp sections", pragma_tok); 26705 26706 ret = cp_parser_omp_sections_scope (parser); 26707 if (ret) 26708 OMP_SECTIONS_CLAUSES (ret) = clauses; 26709 26710 return ret; 26711 } 26712 26713 /* OpenMP 2.5: 26714 # pragma parallel parallel-clause new-line 26715 # pragma parallel for parallel-for-clause new-line 26716 # pragma parallel sections parallel-sections-clause new-line */ 26717 26718 #define OMP_PARALLEL_CLAUSE_MASK \ 26719 ( (1u << PRAGMA_OMP_CLAUSE_IF) \ 26720 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26721 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26722 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ 26723 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \ 26724 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \ 26725 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 26726 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS)) 26727 26728 static tree 26729 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok) 26730 { 26731 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL; 26732 const char *p_name = "#pragma omp parallel"; 26733 tree stmt, clauses, par_clause, ws_clause, block; 26734 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK; 26735 unsigned int save; 26736 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 26737 26738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR)) 26739 { 26740 cp_lexer_consume_token (parser->lexer); 26741 p_kind = PRAGMA_OMP_PARALLEL_FOR; 26742 p_name = "#pragma omp parallel for"; 26743 mask |= OMP_FOR_CLAUSE_MASK; 26744 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); 26745 } 26746 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 26747 { 26748 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 26749 const char *p = IDENTIFIER_POINTER (id); 26750 if (strcmp (p, "sections") == 0) 26751 { 26752 cp_lexer_consume_token (parser->lexer); 26753 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS; 26754 p_name = "#pragma omp parallel sections"; 26755 mask |= OMP_SECTIONS_CLAUSE_MASK; 26756 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); 26757 } 26758 } 26759 26760 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok); 26761 block = begin_omp_parallel (); 26762 save = cp_parser_begin_omp_structured_block (parser); 26763 26764 switch (p_kind) 26765 { 26766 case PRAGMA_OMP_PARALLEL: 26767 cp_parser_statement (parser, NULL_TREE, false, NULL); 26768 par_clause = clauses; 26769 break; 26770 26771 case PRAGMA_OMP_PARALLEL_FOR: 26772 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause); 26773 cp_parser_omp_for_loop (parser, ws_clause, &par_clause); 26774 break; 26775 26776 case PRAGMA_OMP_PARALLEL_SECTIONS: 26777 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause); 26778 stmt = cp_parser_omp_sections_scope (parser); 26779 if (stmt) 26780 OMP_SECTIONS_CLAUSES (stmt) = ws_clause; 26781 break; 26782 26783 default: 26784 gcc_unreachable (); 26785 } 26786 26787 cp_parser_end_omp_structured_block (parser, save); 26788 stmt = finish_omp_parallel (par_clause, block); 26789 if (p_kind != PRAGMA_OMP_PARALLEL) 26790 OMP_PARALLEL_COMBINED (stmt) = 1; 26791 return stmt; 26792 } 26793 26794 /* OpenMP 2.5: 26795 # pragma omp single single-clause[optseq] new-line 26796 structured-block */ 26797 26798 #define OMP_SINGLE_CLAUSE_MASK \ 26799 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26800 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26801 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \ 26802 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 26803 26804 static tree 26805 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok) 26806 { 26807 tree stmt = make_node (OMP_SINGLE); 26808 TREE_TYPE (stmt) = void_type_node; 26809 26810 OMP_SINGLE_CLAUSES (stmt) 26811 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK, 26812 "#pragma omp single", pragma_tok); 26813 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser); 26814 26815 return add_stmt (stmt); 26816 } 26817 26818 /* OpenMP 3.0: 26819 # pragma omp task task-clause[optseq] new-line 26820 structured-block */ 26821 26822 #define OMP_TASK_CLAUSE_MASK \ 26823 ( (1u << PRAGMA_OMP_CLAUSE_IF) \ 26824 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \ 26825 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ 26826 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26827 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26828 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \ 26829 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \ 26830 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE)) 26831 26832 static tree 26833 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok) 26834 { 26835 tree clauses, block; 26836 unsigned int save; 26837 26838 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK, 26839 "#pragma omp task", pragma_tok); 26840 block = begin_omp_task (); 26841 save = cp_parser_begin_omp_structured_block (parser); 26842 cp_parser_statement (parser, NULL_TREE, false, NULL); 26843 cp_parser_end_omp_structured_block (parser, save); 26844 return finish_omp_task (clauses, block); 26845 } 26846 26847 /* OpenMP 3.0: 26848 # pragma omp taskwait new-line */ 26849 26850 static void 26851 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok) 26852 { 26853 cp_parser_require_pragma_eol (parser, pragma_tok); 26854 finish_omp_taskwait (); 26855 } 26856 26857 /* OpenMP 3.1: 26858 # pragma omp taskyield new-line */ 26859 26860 static void 26861 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok) 26862 { 26863 cp_parser_require_pragma_eol (parser, pragma_tok); 26864 finish_omp_taskyield (); 26865 } 26866 26867 /* OpenMP 2.5: 26868 # pragma omp threadprivate (variable-list) */ 26869 26870 static void 26871 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok) 26872 { 26873 tree vars; 26874 26875 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL); 26876 cp_parser_require_pragma_eol (parser, pragma_tok); 26877 26878 finish_omp_threadprivate (vars); 26879 } 26880 26881 /* Main entry point to OpenMP statement pragmas. */ 26882 26883 static void 26884 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok) 26885 { 26886 tree stmt; 26887 26888 switch (pragma_tok->pragma_kind) 26889 { 26890 case PRAGMA_OMP_ATOMIC: 26891 cp_parser_omp_atomic (parser, pragma_tok); 26892 return; 26893 case PRAGMA_OMP_CRITICAL: 26894 stmt = cp_parser_omp_critical (parser, pragma_tok); 26895 break; 26896 case PRAGMA_OMP_FOR: 26897 stmt = cp_parser_omp_for (parser, pragma_tok); 26898 break; 26899 case PRAGMA_OMP_MASTER: 26900 stmt = cp_parser_omp_master (parser, pragma_tok); 26901 break; 26902 case PRAGMA_OMP_ORDERED: 26903 stmt = cp_parser_omp_ordered (parser, pragma_tok); 26904 break; 26905 case PRAGMA_OMP_PARALLEL: 26906 stmt = cp_parser_omp_parallel (parser, pragma_tok); 26907 break; 26908 case PRAGMA_OMP_SECTIONS: 26909 stmt = cp_parser_omp_sections (parser, pragma_tok); 26910 break; 26911 case PRAGMA_OMP_SINGLE: 26912 stmt = cp_parser_omp_single (parser, pragma_tok); 26913 break; 26914 case PRAGMA_OMP_TASK: 26915 stmt = cp_parser_omp_task (parser, pragma_tok); 26916 break; 26917 default: 26918 gcc_unreachable (); 26919 } 26920 26921 if (stmt) 26922 SET_EXPR_LOCATION (stmt, pragma_tok->location); 26923 } 26924 26925 /* Transactional Memory parsing routines. */ 26926 26927 /* Parse a transaction attribute. 26928 26929 txn-attribute: 26930 attribute 26931 [ [ identifier ] ] 26932 26933 ??? Simplify this when C++0x bracket attributes are 26934 implemented properly. */ 26935 26936 static tree 26937 cp_parser_txn_attribute_opt (cp_parser *parser) 26938 { 26939 cp_token *token; 26940 tree attr_name, attr = NULL; 26941 26942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) 26943 return cp_parser_attributes_opt (parser); 26944 26945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) 26946 return NULL_TREE; 26947 cp_lexer_consume_token (parser->lexer); 26948 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE)) 26949 goto error1; 26950 26951 token = cp_lexer_peek_token (parser->lexer); 26952 if (token->type == CPP_NAME || token->type == CPP_KEYWORD) 26953 { 26954 token = cp_lexer_consume_token (parser->lexer); 26955 26956 attr_name = (token->type == CPP_KEYWORD 26957 /* For keywords, use the canonical spelling, 26958 not the parsed identifier. */ 26959 ? ridpointers[(int) token->keyword] 26960 : token->u.value); 26961 attr = build_tree_list (attr_name, NULL_TREE); 26962 } 26963 else 26964 cp_parser_error (parser, "expected identifier"); 26965 26966 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 26967 error1: 26968 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 26969 return attr; 26970 } 26971 26972 /* Parse a __transaction_atomic or __transaction_relaxed statement. 26973 26974 transaction-statement: 26975 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt] 26976 compound-statement 26977 __transaction_relaxed txn-noexcept-spec[opt] compound-statement 26978 */ 26979 26980 static tree 26981 cp_parser_transaction (cp_parser *parser, enum rid keyword) 26982 { 26983 unsigned char old_in = parser->in_transaction; 26984 unsigned char this_in = 1, new_in; 26985 cp_token *token; 26986 tree stmt, attrs, noex; 26987 26988 gcc_assert (keyword == RID_TRANSACTION_ATOMIC 26989 || keyword == RID_TRANSACTION_RELAXED); 26990 token = cp_parser_require_keyword (parser, keyword, 26991 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC 26992 : RT_TRANSACTION_RELAXED)); 26993 gcc_assert (token != NULL); 26994 26995 if (keyword == RID_TRANSACTION_RELAXED) 26996 this_in |= TM_STMT_ATTR_RELAXED; 26997 else 26998 { 26999 attrs = cp_parser_txn_attribute_opt (parser); 27000 if (attrs) 27001 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER); 27002 } 27003 27004 /* Parse a noexcept specification. */ 27005 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true); 27006 27007 /* Keep track if we're in the lexical scope of an outer transaction. */ 27008 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER); 27009 27010 stmt = begin_transaction_stmt (token->location, NULL, this_in); 27011 27012 parser->in_transaction = new_in; 27013 cp_parser_compound_statement (parser, NULL, false, false); 27014 parser->in_transaction = old_in; 27015 27016 finish_transaction_stmt (stmt, NULL, this_in, noex); 27017 27018 return stmt; 27019 } 27020 27021 /* Parse a __transaction_atomic or __transaction_relaxed expression. 27022 27023 transaction-expression: 27024 __transaction_atomic txn-noexcept-spec[opt] ( expression ) 27025 __transaction_relaxed txn-noexcept-spec[opt] ( expression ) 27026 */ 27027 27028 static tree 27029 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword) 27030 { 27031 unsigned char old_in = parser->in_transaction; 27032 unsigned char this_in = 1; 27033 cp_token *token; 27034 tree expr, noex; 27035 bool noex_expr; 27036 27037 gcc_assert (keyword == RID_TRANSACTION_ATOMIC 27038 || keyword == RID_TRANSACTION_RELAXED); 27039 27040 if (!flag_tm) 27041 error (keyword == RID_TRANSACTION_RELAXED 27042 ? G_("%<__transaction_relaxed%> without transactional memory " 27043 "support enabled") 27044 : G_("%<__transaction_atomic%> without transactional memory " 27045 "support enabled")); 27046 27047 token = cp_parser_require_keyword (parser, keyword, 27048 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC 27049 : RT_TRANSACTION_RELAXED)); 27050 gcc_assert (token != NULL); 27051 27052 if (keyword == RID_TRANSACTION_RELAXED) 27053 this_in |= TM_STMT_ATTR_RELAXED; 27054 27055 /* Set this early. This might mean that we allow transaction_cancel in 27056 an expression that we find out later actually has to be a constexpr. 27057 However, we expect that cxx_constant_value will be able to deal with 27058 this; also, if the noexcept has no constexpr, then what we parse next 27059 really is a transaction's body. */ 27060 parser->in_transaction = this_in; 27061 27062 /* Parse a noexcept specification. */ 27063 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr, 27064 true); 27065 27066 if (!noex || !noex_expr 27067 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN) 27068 { 27069 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 27070 27071 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 27072 finish_parenthesized_expr (expr); 27073 27074 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 27075 } 27076 else 27077 { 27078 /* The only expression that is available got parsed for the noexcept 27079 already. noexcept is true then. */ 27080 expr = noex; 27081 noex = boolean_true_node; 27082 } 27083 27084 expr = build_transaction_expr (token->location, expr, this_in, noex); 27085 parser->in_transaction = old_in; 27086 27087 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION)) 27088 return error_mark_node; 27089 27090 return (flag_tm ? expr : error_mark_node); 27091 } 27092 27093 /* Parse a function-transaction-block. 27094 27095 function-transaction-block: 27096 __transaction_atomic txn-attribute[opt] ctor-initializer[opt] 27097 function-body 27098 __transaction_atomic txn-attribute[opt] function-try-block 27099 __transaction_relaxed ctor-initializer[opt] function-body 27100 __transaction_relaxed function-try-block 27101 */ 27102 27103 static bool 27104 cp_parser_function_transaction (cp_parser *parser, enum rid keyword) 27105 { 27106 unsigned char old_in = parser->in_transaction; 27107 unsigned char new_in = 1; 27108 tree compound_stmt, stmt, attrs; 27109 bool ctor_initializer_p; 27110 cp_token *token; 27111 27112 gcc_assert (keyword == RID_TRANSACTION_ATOMIC 27113 || keyword == RID_TRANSACTION_RELAXED); 27114 token = cp_parser_require_keyword (parser, keyword, 27115 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC 27116 : RT_TRANSACTION_RELAXED)); 27117 gcc_assert (token != NULL); 27118 27119 if (keyword == RID_TRANSACTION_RELAXED) 27120 new_in |= TM_STMT_ATTR_RELAXED; 27121 else 27122 { 27123 attrs = cp_parser_txn_attribute_opt (parser); 27124 if (attrs) 27125 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER); 27126 } 27127 27128 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in); 27129 27130 parser->in_transaction = new_in; 27131 27132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) 27133 ctor_initializer_p = cp_parser_function_try_block (parser); 27134 else 27135 ctor_initializer_p 27136 = cp_parser_ctor_initializer_opt_and_function_body (parser); 27137 27138 parser->in_transaction = old_in; 27139 27140 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE); 27141 27142 return ctor_initializer_p; 27143 } 27144 27145 /* Parse a __transaction_cancel statement. 27146 27147 cancel-statement: 27148 __transaction_cancel txn-attribute[opt] ; 27149 __transaction_cancel txn-attribute[opt] throw-expression ; 27150 27151 ??? Cancel and throw is not yet implemented. */ 27152 27153 static tree 27154 cp_parser_transaction_cancel (cp_parser *parser) 27155 { 27156 cp_token *token; 27157 bool is_outer = false; 27158 tree stmt, attrs; 27159 27160 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL, 27161 RT_TRANSACTION_CANCEL); 27162 gcc_assert (token != NULL); 27163 27164 attrs = cp_parser_txn_attribute_opt (parser); 27165 if (attrs) 27166 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0); 27167 27168 /* ??? Parse cancel-and-throw here. */ 27169 27170 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 27171 27172 if (!flag_tm) 27173 { 27174 error_at (token->location, "%<__transaction_cancel%> without " 27175 "transactional memory support enabled"); 27176 return error_mark_node; 27177 } 27178 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED) 27179 { 27180 error_at (token->location, "%<__transaction_cancel%> within a " 27181 "%<__transaction_relaxed%>"); 27182 return error_mark_node; 27183 } 27184 else if (is_outer) 27185 { 27186 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0 27187 && !is_tm_may_cancel_outer (current_function_decl)) 27188 { 27189 error_at (token->location, "outer %<__transaction_cancel%> not " 27190 "within outer %<__transaction_atomic%>"); 27191 error_at (token->location, 27192 " or a %<transaction_may_cancel_outer%> function"); 27193 return error_mark_node; 27194 } 27195 } 27196 else if (parser->in_transaction == 0) 27197 { 27198 error_at (token->location, "%<__transaction_cancel%> not within " 27199 "%<__transaction_atomic%>"); 27200 return error_mark_node; 27201 } 27202 27203 stmt = build_tm_abort_call (token->location, is_outer); 27204 add_stmt (stmt); 27205 finish_stmt (); 27206 27207 return stmt; 27208 } 27209 27210 /* The parser. */ 27211 27212 static GTY (()) cp_parser *the_parser; 27213 27214 27215 /* Special handling for the first token or line in the file. The first 27216 thing in the file might be #pragma GCC pch_preprocess, which loads a 27217 PCH file, which is a GC collection point. So we need to handle this 27218 first pragma without benefit of an existing lexer structure. 27219 27220 Always returns one token to the caller in *FIRST_TOKEN. This is 27221 either the true first token of the file, or the first token after 27222 the initial pragma. */ 27223 27224 static void 27225 cp_parser_initial_pragma (cp_token *first_token) 27226 { 27227 tree name = NULL; 27228 27229 cp_lexer_get_preprocessor_token (NULL, first_token); 27230 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS) 27231 return; 27232 27233 cp_lexer_get_preprocessor_token (NULL, first_token); 27234 if (first_token->type == CPP_STRING) 27235 { 27236 name = first_token->u.value; 27237 27238 cp_lexer_get_preprocessor_token (NULL, first_token); 27239 if (first_token->type != CPP_PRAGMA_EOL) 27240 error_at (first_token->location, 27241 "junk at end of %<#pragma GCC pch_preprocess%>"); 27242 } 27243 else 27244 error_at (first_token->location, "expected string literal"); 27245 27246 /* Skip to the end of the pragma. */ 27247 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF) 27248 cp_lexer_get_preprocessor_token (NULL, first_token); 27249 27250 /* Now actually load the PCH file. */ 27251 if (name) 27252 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name)); 27253 27254 /* Read one more token to return to our caller. We have to do this 27255 after reading the PCH file in, since its pointers have to be 27256 live. */ 27257 cp_lexer_get_preprocessor_token (NULL, first_token); 27258 } 27259 27260 /* Normal parsing of a pragma token. Here we can (and must) use the 27261 regular lexer. */ 27262 27263 static bool 27264 cp_parser_pragma (cp_parser *parser, enum pragma_context context) 27265 { 27266 cp_token *pragma_tok; 27267 unsigned int id; 27268 27269 pragma_tok = cp_lexer_consume_token (parser->lexer); 27270 gcc_assert (pragma_tok->type == CPP_PRAGMA); 27271 parser->lexer->in_pragma = true; 27272 27273 id = pragma_tok->pragma_kind; 27274 switch (id) 27275 { 27276 case PRAGMA_GCC_PCH_PREPROCESS: 27277 error_at (pragma_tok->location, 27278 "%<#pragma GCC pch_preprocess%> must be first"); 27279 break; 27280 27281 case PRAGMA_OMP_BARRIER: 27282 switch (context) 27283 { 27284 case pragma_compound: 27285 cp_parser_omp_barrier (parser, pragma_tok); 27286 return false; 27287 case pragma_stmt: 27288 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be " 27289 "used in compound statements"); 27290 break; 27291 default: 27292 goto bad_stmt; 27293 } 27294 break; 27295 27296 case PRAGMA_OMP_FLUSH: 27297 switch (context) 27298 { 27299 case pragma_compound: 27300 cp_parser_omp_flush (parser, pragma_tok); 27301 return false; 27302 case pragma_stmt: 27303 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be " 27304 "used in compound statements"); 27305 break; 27306 default: 27307 goto bad_stmt; 27308 } 27309 break; 27310 27311 case PRAGMA_OMP_TASKWAIT: 27312 switch (context) 27313 { 27314 case pragma_compound: 27315 cp_parser_omp_taskwait (parser, pragma_tok); 27316 return false; 27317 case pragma_stmt: 27318 error_at (pragma_tok->location, 27319 "%<#pragma omp taskwait%> may only be " 27320 "used in compound statements"); 27321 break; 27322 default: 27323 goto bad_stmt; 27324 } 27325 break; 27326 27327 case PRAGMA_OMP_TASKYIELD: 27328 switch (context) 27329 { 27330 case pragma_compound: 27331 cp_parser_omp_taskyield (parser, pragma_tok); 27332 return false; 27333 case pragma_stmt: 27334 error_at (pragma_tok->location, 27335 "%<#pragma omp taskyield%> may only be " 27336 "used in compound statements"); 27337 break; 27338 default: 27339 goto bad_stmt; 27340 } 27341 break; 27342 27343 case PRAGMA_OMP_THREADPRIVATE: 27344 cp_parser_omp_threadprivate (parser, pragma_tok); 27345 return false; 27346 27347 case PRAGMA_OMP_ATOMIC: 27348 case PRAGMA_OMP_CRITICAL: 27349 case PRAGMA_OMP_FOR: 27350 case PRAGMA_OMP_MASTER: 27351 case PRAGMA_OMP_ORDERED: 27352 case PRAGMA_OMP_PARALLEL: 27353 case PRAGMA_OMP_SECTIONS: 27354 case PRAGMA_OMP_SINGLE: 27355 case PRAGMA_OMP_TASK: 27356 if (context == pragma_external) 27357 goto bad_stmt; 27358 cp_parser_omp_construct (parser, pragma_tok); 27359 return true; 27360 27361 case PRAGMA_OMP_SECTION: 27362 error_at (pragma_tok->location, 27363 "%<#pragma omp section%> may only be used in " 27364 "%<#pragma omp sections%> construct"); 27365 break; 27366 27367 default: 27368 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL); 27369 c_invoke_pragma_handler (id); 27370 break; 27371 27372 bad_stmt: 27373 cp_parser_error (parser, "expected declaration specifiers"); 27374 break; 27375 } 27376 27377 cp_parser_skip_to_pragma_eol (parser, pragma_tok); 27378 return false; 27379 } 27380 27381 /* The interface the pragma parsers have to the lexer. */ 27382 27383 enum cpp_ttype 27384 pragma_lex (tree *value) 27385 { 27386 cp_token *tok; 27387 enum cpp_ttype ret; 27388 27389 tok = cp_lexer_peek_token (the_parser->lexer); 27390 27391 ret = tok->type; 27392 *value = tok->u.value; 27393 27394 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF) 27395 ret = CPP_EOF; 27396 else if (ret == CPP_STRING) 27397 *value = cp_parser_string_literal (the_parser, false, false); 27398 else 27399 { 27400 cp_lexer_consume_token (the_parser->lexer); 27401 if (ret == CPP_KEYWORD) 27402 ret = CPP_NAME; 27403 } 27404 27405 return ret; 27406 } 27407 27408 27409 /* External interface. */ 27410 27411 /* Parse one entire translation unit. */ 27412 27413 void 27414 c_parse_file (void) 27415 { 27416 static bool already_called = false; 27417 27418 if (already_called) 27419 { 27420 sorry ("inter-module optimizations not implemented for C++"); 27421 return; 27422 } 27423 already_called = true; 27424 27425 the_parser = cp_parser_new (); 27426 push_deferring_access_checks (flag_access_control 27427 ? dk_no_deferred : dk_no_check); 27428 cp_parser_translation_unit (the_parser); 27429 the_parser = NULL; 27430 } 27431 27432 #include "gt-cp-parser.h" 27433