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 /* Once we see the ~, this has to be a pseudo-destructor. */ 6321 if (!processing_template_decl && !cp_parser_error_occurred (parser)) 6322 cp_parser_commit_to_tentative_parse (parser); 6323 6324 /* Look for the type-name again. We are not responsible for 6325 checking that it matches the first type-name. */ 6326 *type = cp_parser_nonclass_name (parser); 6327 } 6328 6329 /* Parse a unary-expression. 6330 6331 unary-expression: 6332 postfix-expression 6333 ++ cast-expression 6334 -- cast-expression 6335 unary-operator cast-expression 6336 sizeof unary-expression 6337 sizeof ( type-id ) 6338 alignof ( type-id ) [C++0x] 6339 new-expression 6340 delete-expression 6341 6342 GNU Extensions: 6343 6344 unary-expression: 6345 __extension__ cast-expression 6346 __alignof__ unary-expression 6347 __alignof__ ( type-id ) 6348 alignof unary-expression [C++0x] 6349 __real__ cast-expression 6350 __imag__ cast-expression 6351 && identifier 6352 6353 ADDRESS_P is true iff the unary-expression is appearing as the 6354 operand of the `&' operator. CAST_P is true if this expression is 6355 the target of a cast. 6356 6357 Returns a representation of the expression. */ 6358 6359 static tree 6360 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p, 6361 cp_id_kind * pidk) 6362 { 6363 cp_token *token; 6364 enum tree_code unary_operator; 6365 6366 /* Peek at the next token. */ 6367 token = cp_lexer_peek_token (parser->lexer); 6368 /* Some keywords give away the kind of expression. */ 6369 if (token->type == CPP_KEYWORD) 6370 { 6371 enum rid keyword = token->keyword; 6372 6373 switch (keyword) 6374 { 6375 case RID_ALIGNOF: 6376 case RID_SIZEOF: 6377 { 6378 tree operand; 6379 enum tree_code op; 6380 6381 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR; 6382 /* Consume the token. */ 6383 cp_lexer_consume_token (parser->lexer); 6384 /* Parse the operand. */ 6385 operand = cp_parser_sizeof_operand (parser, keyword); 6386 6387 if (TYPE_P (operand)) 6388 return cxx_sizeof_or_alignof_type (operand, op, true); 6389 else 6390 { 6391 /* ISO C++ defines alignof only with types, not with 6392 expressions. So pedwarn if alignof is used with a non- 6393 type expression. However, __alignof__ is ok. */ 6394 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof")) 6395 pedwarn (token->location, OPT_pedantic, 6396 "ISO C++ does not allow %<alignof%> " 6397 "with a non-type"); 6398 6399 return cxx_sizeof_or_alignof_expr (operand, op, true); 6400 } 6401 } 6402 6403 case RID_NEW: 6404 return cp_parser_new_expression (parser); 6405 6406 case RID_DELETE: 6407 return cp_parser_delete_expression (parser); 6408 6409 case RID_EXTENSION: 6410 { 6411 /* The saved value of the PEDANTIC flag. */ 6412 int saved_pedantic; 6413 tree expr; 6414 6415 /* Save away the PEDANTIC flag. */ 6416 cp_parser_extension_opt (parser, &saved_pedantic); 6417 /* Parse the cast-expression. */ 6418 expr = cp_parser_simple_cast_expression (parser); 6419 /* Restore the PEDANTIC flag. */ 6420 pedantic = saved_pedantic; 6421 6422 return expr; 6423 } 6424 6425 case RID_REALPART: 6426 case RID_IMAGPART: 6427 { 6428 tree expression; 6429 6430 /* Consume the `__real__' or `__imag__' token. */ 6431 cp_lexer_consume_token (parser->lexer); 6432 /* Parse the cast-expression. */ 6433 expression = cp_parser_simple_cast_expression (parser); 6434 /* Create the complete representation. */ 6435 return build_x_unary_op ((keyword == RID_REALPART 6436 ? REALPART_EXPR : IMAGPART_EXPR), 6437 expression, 6438 tf_warning_or_error); 6439 } 6440 break; 6441 6442 case RID_TRANSACTION_ATOMIC: 6443 case RID_TRANSACTION_RELAXED: 6444 return cp_parser_transaction_expression (parser, keyword); 6445 6446 case RID_NOEXCEPT: 6447 { 6448 tree expr; 6449 const char *saved_message; 6450 bool saved_integral_constant_expression_p; 6451 bool saved_non_integral_constant_expression_p; 6452 bool saved_greater_than_is_operator_p; 6453 6454 cp_lexer_consume_token (parser->lexer); 6455 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 6456 6457 saved_message = parser->type_definition_forbidden_message; 6458 parser->type_definition_forbidden_message 6459 = G_("types may not be defined in %<noexcept%> expressions"); 6460 6461 saved_integral_constant_expression_p 6462 = parser->integral_constant_expression_p; 6463 saved_non_integral_constant_expression_p 6464 = parser->non_integral_constant_expression_p; 6465 parser->integral_constant_expression_p = false; 6466 6467 saved_greater_than_is_operator_p 6468 = parser->greater_than_is_operator_p; 6469 parser->greater_than_is_operator_p = true; 6470 6471 ++cp_unevaluated_operand; 6472 ++c_inhibit_evaluation_warnings; 6473 expr = cp_parser_expression (parser, false, NULL); 6474 --c_inhibit_evaluation_warnings; 6475 --cp_unevaluated_operand; 6476 6477 parser->greater_than_is_operator_p 6478 = saved_greater_than_is_operator_p; 6479 6480 parser->integral_constant_expression_p 6481 = saved_integral_constant_expression_p; 6482 parser->non_integral_constant_expression_p 6483 = saved_non_integral_constant_expression_p; 6484 6485 parser->type_definition_forbidden_message = saved_message; 6486 6487 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 6488 return finish_noexcept_expr (expr, tf_warning_or_error); 6489 } 6490 6491 default: 6492 break; 6493 } 6494 } 6495 6496 /* Look for the `:: new' and `:: delete', which also signal the 6497 beginning of a new-expression, or delete-expression, 6498 respectively. If the next token is `::', then it might be one of 6499 these. */ 6500 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 6501 { 6502 enum rid keyword; 6503 6504 /* See if the token after the `::' is one of the keywords in 6505 which we're interested. */ 6506 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword; 6507 /* If it's `new', we have a new-expression. */ 6508 if (keyword == RID_NEW) 6509 return cp_parser_new_expression (parser); 6510 /* Similarly, for `delete'. */ 6511 else if (keyword == RID_DELETE) 6512 return cp_parser_delete_expression (parser); 6513 } 6514 6515 /* Look for a unary operator. */ 6516 unary_operator = cp_parser_unary_operator (token); 6517 /* The `++' and `--' operators can be handled similarly, even though 6518 they are not technically unary-operators in the grammar. */ 6519 if (unary_operator == ERROR_MARK) 6520 { 6521 if (token->type == CPP_PLUS_PLUS) 6522 unary_operator = PREINCREMENT_EXPR; 6523 else if (token->type == CPP_MINUS_MINUS) 6524 unary_operator = PREDECREMENT_EXPR; 6525 /* Handle the GNU address-of-label extension. */ 6526 else if (cp_parser_allow_gnu_extensions_p (parser) 6527 && token->type == CPP_AND_AND) 6528 { 6529 tree identifier; 6530 tree expression; 6531 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 6532 6533 /* Consume the '&&' token. */ 6534 cp_lexer_consume_token (parser->lexer); 6535 /* Look for the identifier. */ 6536 identifier = cp_parser_identifier (parser); 6537 /* Create an expression representing the address. */ 6538 expression = finish_label_address_expr (identifier, loc); 6539 if (cp_parser_non_integral_constant_expression (parser, 6540 NIC_ADDR_LABEL)) 6541 expression = error_mark_node; 6542 return expression; 6543 } 6544 } 6545 if (unary_operator != ERROR_MARK) 6546 { 6547 tree cast_expression; 6548 tree expression = error_mark_node; 6549 non_integral_constant non_constant_p = NIC_NONE; 6550 6551 /* Consume the operator token. */ 6552 token = cp_lexer_consume_token (parser->lexer); 6553 /* Parse the cast-expression. */ 6554 cast_expression 6555 = cp_parser_cast_expression (parser, 6556 unary_operator == ADDR_EXPR, 6557 /*cast_p=*/false, pidk); 6558 /* Now, build an appropriate representation. */ 6559 switch (unary_operator) 6560 { 6561 case INDIRECT_REF: 6562 non_constant_p = NIC_STAR; 6563 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR, 6564 tf_warning_or_error); 6565 break; 6566 6567 case ADDR_EXPR: 6568 non_constant_p = NIC_ADDR; 6569 /* Fall through. */ 6570 case BIT_NOT_EXPR: 6571 expression = build_x_unary_op (unary_operator, cast_expression, 6572 tf_warning_or_error); 6573 break; 6574 6575 case PREINCREMENT_EXPR: 6576 case PREDECREMENT_EXPR: 6577 non_constant_p = unary_operator == PREINCREMENT_EXPR 6578 ? NIC_PREINCREMENT : NIC_PREDECREMENT; 6579 /* Fall through. */ 6580 case UNARY_PLUS_EXPR: 6581 case NEGATE_EXPR: 6582 case TRUTH_NOT_EXPR: 6583 expression = finish_unary_op_expr (unary_operator, cast_expression); 6584 break; 6585 6586 default: 6587 gcc_unreachable (); 6588 } 6589 6590 if (non_constant_p != NIC_NONE 6591 && cp_parser_non_integral_constant_expression (parser, 6592 non_constant_p)) 6593 expression = error_mark_node; 6594 6595 return expression; 6596 } 6597 6598 return cp_parser_postfix_expression (parser, address_p, cast_p, 6599 /*member_access_only_p=*/false, 6600 pidk); 6601 } 6602 6603 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a 6604 unary-operator, the corresponding tree code is returned. */ 6605 6606 static enum tree_code 6607 cp_parser_unary_operator (cp_token* token) 6608 { 6609 switch (token->type) 6610 { 6611 case CPP_MULT: 6612 return INDIRECT_REF; 6613 6614 case CPP_AND: 6615 return ADDR_EXPR; 6616 6617 case CPP_PLUS: 6618 return UNARY_PLUS_EXPR; 6619 6620 case CPP_MINUS: 6621 return NEGATE_EXPR; 6622 6623 case CPP_NOT: 6624 return TRUTH_NOT_EXPR; 6625 6626 case CPP_COMPL: 6627 return BIT_NOT_EXPR; 6628 6629 default: 6630 return ERROR_MARK; 6631 } 6632 } 6633 6634 /* Parse a new-expression. 6635 6636 new-expression: 6637 :: [opt] new new-placement [opt] new-type-id new-initializer [opt] 6638 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt] 6639 6640 Returns a representation of the expression. */ 6641 6642 static tree 6643 cp_parser_new_expression (cp_parser* parser) 6644 { 6645 bool global_scope_p; 6646 VEC(tree,gc) *placement; 6647 tree type; 6648 VEC(tree,gc) *initializer; 6649 tree nelts; 6650 tree ret; 6651 6652 /* Look for the optional `::' operator. */ 6653 global_scope_p 6654 = (cp_parser_global_scope_opt (parser, 6655 /*current_scope_valid_p=*/false) 6656 != NULL_TREE); 6657 /* Look for the `new' operator. */ 6658 cp_parser_require_keyword (parser, RID_NEW, RT_NEW); 6659 /* There's no easy way to tell a new-placement from the 6660 `( type-id )' construct. */ 6661 cp_parser_parse_tentatively (parser); 6662 /* Look for a new-placement. */ 6663 placement = cp_parser_new_placement (parser); 6664 /* If that didn't work out, there's no new-placement. */ 6665 if (!cp_parser_parse_definitely (parser)) 6666 { 6667 if (placement != NULL) 6668 release_tree_vector (placement); 6669 placement = NULL; 6670 } 6671 6672 /* If the next token is a `(', then we have a parenthesized 6673 type-id. */ 6674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 6675 { 6676 cp_token *token; 6677 const char *saved_message = parser->type_definition_forbidden_message; 6678 6679 /* Consume the `('. */ 6680 cp_lexer_consume_token (parser->lexer); 6681 6682 /* Parse the type-id. */ 6683 parser->type_definition_forbidden_message 6684 = G_("types may not be defined in a new-expression"); 6685 type = cp_parser_type_id (parser); 6686 parser->type_definition_forbidden_message = saved_message; 6687 6688 /* Look for the closing `)'. */ 6689 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 6690 token = cp_lexer_peek_token (parser->lexer); 6691 /* There should not be a direct-new-declarator in this production, 6692 but GCC used to allowed this, so we check and emit a sensible error 6693 message for this case. */ 6694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 6695 { 6696 error_at (token->location, 6697 "array bound forbidden after parenthesized type-id"); 6698 inform (token->location, 6699 "try removing the parentheses around the type-id"); 6700 cp_parser_direct_new_declarator (parser); 6701 } 6702 nelts = NULL_TREE; 6703 } 6704 /* Otherwise, there must be a new-type-id. */ 6705 else 6706 type = cp_parser_new_type_id (parser, &nelts); 6707 6708 /* If the next token is a `(' or '{', then we have a new-initializer. */ 6709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN) 6710 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 6711 initializer = cp_parser_new_initializer (parser); 6712 else 6713 initializer = NULL; 6714 6715 /* A new-expression may not appear in an integral constant 6716 expression. */ 6717 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW)) 6718 ret = error_mark_node; 6719 else 6720 { 6721 /* Create a representation of the new-expression. */ 6722 ret = build_new (&placement, type, nelts, &initializer, global_scope_p, 6723 tf_warning_or_error); 6724 } 6725 6726 if (placement != NULL) 6727 release_tree_vector (placement); 6728 if (initializer != NULL) 6729 release_tree_vector (initializer); 6730 6731 return ret; 6732 } 6733 6734 /* Parse a new-placement. 6735 6736 new-placement: 6737 ( expression-list ) 6738 6739 Returns the same representation as for an expression-list. */ 6740 6741 static VEC(tree,gc) * 6742 cp_parser_new_placement (cp_parser* parser) 6743 { 6744 VEC(tree,gc) *expression_list; 6745 6746 /* Parse the expression-list. */ 6747 expression_list = (cp_parser_parenthesized_expression_list 6748 (parser, non_attr, /*cast_p=*/false, 6749 /*allow_expansion_p=*/true, 6750 /*non_constant_p=*/NULL)); 6751 6752 return expression_list; 6753 } 6754 6755 /* Parse a new-type-id. 6756 6757 new-type-id: 6758 type-specifier-seq new-declarator [opt] 6759 6760 Returns the TYPE allocated. If the new-type-id indicates an array 6761 type, *NELTS is set to the number of elements in the last array 6762 bound; the TYPE will not include the last array bound. */ 6763 6764 static tree 6765 cp_parser_new_type_id (cp_parser* parser, tree *nelts) 6766 { 6767 cp_decl_specifier_seq type_specifier_seq; 6768 cp_declarator *new_declarator; 6769 cp_declarator *declarator; 6770 cp_declarator *outer_declarator; 6771 const char *saved_message; 6772 tree type; 6773 6774 /* The type-specifier sequence must not contain type definitions. 6775 (It cannot contain declarations of new types either, but if they 6776 are not definitions we will catch that because they are not 6777 complete.) */ 6778 saved_message = parser->type_definition_forbidden_message; 6779 parser->type_definition_forbidden_message 6780 = G_("types may not be defined in a new-type-id"); 6781 /* Parse the type-specifier-seq. */ 6782 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false, 6783 /*is_trailing_return=*/false, 6784 &type_specifier_seq); 6785 /* Restore the old message. */ 6786 parser->type_definition_forbidden_message = saved_message; 6787 /* Parse the new-declarator. */ 6788 new_declarator = cp_parser_new_declarator_opt (parser); 6789 6790 /* Determine the number of elements in the last array dimension, if 6791 any. */ 6792 *nelts = NULL_TREE; 6793 /* Skip down to the last array dimension. */ 6794 declarator = new_declarator; 6795 outer_declarator = NULL; 6796 while (declarator && (declarator->kind == cdk_pointer 6797 || declarator->kind == cdk_ptrmem)) 6798 { 6799 outer_declarator = declarator; 6800 declarator = declarator->declarator; 6801 } 6802 while (declarator 6803 && declarator->kind == cdk_array 6804 && declarator->declarator 6805 && declarator->declarator->kind == cdk_array) 6806 { 6807 outer_declarator = declarator; 6808 declarator = declarator->declarator; 6809 } 6810 6811 if (declarator && declarator->kind == cdk_array) 6812 { 6813 *nelts = declarator->u.array.bounds; 6814 if (*nelts == error_mark_node) 6815 *nelts = integer_one_node; 6816 6817 if (outer_declarator) 6818 outer_declarator->declarator = declarator->declarator; 6819 else 6820 new_declarator = NULL; 6821 } 6822 6823 type = groktypename (&type_specifier_seq, new_declarator, false); 6824 return type; 6825 } 6826 6827 /* Parse an (optional) new-declarator. 6828 6829 new-declarator: 6830 ptr-operator new-declarator [opt] 6831 direct-new-declarator 6832 6833 Returns the declarator. */ 6834 6835 static cp_declarator * 6836 cp_parser_new_declarator_opt (cp_parser* parser) 6837 { 6838 enum tree_code code; 6839 tree type; 6840 cp_cv_quals cv_quals; 6841 6842 /* We don't know if there's a ptr-operator next, or not. */ 6843 cp_parser_parse_tentatively (parser); 6844 /* Look for a ptr-operator. */ 6845 code = cp_parser_ptr_operator (parser, &type, &cv_quals); 6846 /* If that worked, look for more new-declarators. */ 6847 if (cp_parser_parse_definitely (parser)) 6848 { 6849 cp_declarator *declarator; 6850 6851 /* Parse another optional declarator. */ 6852 declarator = cp_parser_new_declarator_opt (parser); 6853 6854 return cp_parser_make_indirect_declarator 6855 (code, type, cv_quals, declarator); 6856 } 6857 6858 /* If the next token is a `[', there is a direct-new-declarator. */ 6859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 6860 return cp_parser_direct_new_declarator (parser); 6861 6862 return NULL; 6863 } 6864 6865 /* Parse a direct-new-declarator. 6866 6867 direct-new-declarator: 6868 [ expression ] 6869 direct-new-declarator [constant-expression] 6870 6871 */ 6872 6873 static cp_declarator * 6874 cp_parser_direct_new_declarator (cp_parser* parser) 6875 { 6876 cp_declarator *declarator = NULL; 6877 6878 while (true) 6879 { 6880 tree expression; 6881 6882 /* Look for the opening `['. */ 6883 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); 6884 /* The first expression is not required to be constant. */ 6885 if (!declarator) 6886 { 6887 cp_token *token = cp_lexer_peek_token (parser->lexer); 6888 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); 6889 /* The standard requires that the expression have integral 6890 type. DR 74 adds enumeration types. We believe that the 6891 real intent is that these expressions be handled like the 6892 expression in a `switch' condition, which also allows 6893 classes with a single conversion to integral or 6894 enumeration type. */ 6895 if (!processing_template_decl) 6896 { 6897 expression 6898 = build_expr_type_conversion (WANT_INT | WANT_ENUM, 6899 expression, 6900 /*complain=*/true); 6901 if (!expression) 6902 { 6903 error_at (token->location, 6904 "expression in new-declarator must have integral " 6905 "or enumeration type"); 6906 expression = error_mark_node; 6907 } 6908 } 6909 } 6910 /* But all the other expressions must be. */ 6911 else 6912 expression 6913 = cp_parser_constant_expression (parser, 6914 /*allow_non_constant=*/false, 6915 NULL); 6916 /* Look for the closing `]'. */ 6917 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 6918 6919 /* Add this bound to the declarator. */ 6920 declarator = make_array_declarator (declarator, expression); 6921 6922 /* If the next token is not a `[', then there are no more 6923 bounds. */ 6924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) 6925 break; 6926 } 6927 6928 return declarator; 6929 } 6930 6931 /* Parse a new-initializer. 6932 6933 new-initializer: 6934 ( expression-list [opt] ) 6935 braced-init-list 6936 6937 Returns a representation of the expression-list. */ 6938 6939 static VEC(tree,gc) * 6940 cp_parser_new_initializer (cp_parser* parser) 6941 { 6942 VEC(tree,gc) *expression_list; 6943 6944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 6945 { 6946 tree t; 6947 bool expr_non_constant_p; 6948 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 6949 t = cp_parser_braced_list (parser, &expr_non_constant_p); 6950 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1; 6951 expression_list = make_tree_vector_single (t); 6952 } 6953 else 6954 expression_list = (cp_parser_parenthesized_expression_list 6955 (parser, non_attr, /*cast_p=*/false, 6956 /*allow_expansion_p=*/true, 6957 /*non_constant_p=*/NULL)); 6958 6959 return expression_list; 6960 } 6961 6962 /* Parse a delete-expression. 6963 6964 delete-expression: 6965 :: [opt] delete cast-expression 6966 :: [opt] delete [ ] cast-expression 6967 6968 Returns a representation of the expression. */ 6969 6970 static tree 6971 cp_parser_delete_expression (cp_parser* parser) 6972 { 6973 bool global_scope_p; 6974 bool array_p; 6975 tree expression; 6976 6977 /* Look for the optional `::' operator. */ 6978 global_scope_p 6979 = (cp_parser_global_scope_opt (parser, 6980 /*current_scope_valid_p=*/false) 6981 != NULL_TREE); 6982 /* Look for the `delete' keyword. */ 6983 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE); 6984 /* See if the array syntax is in use. */ 6985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 6986 { 6987 /* Consume the `[' token. */ 6988 cp_lexer_consume_token (parser->lexer); 6989 /* Look for the `]' token. */ 6990 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 6991 /* Remember that this is the `[]' construct. */ 6992 array_p = true; 6993 } 6994 else 6995 array_p = false; 6996 6997 /* Parse the cast-expression. */ 6998 expression = cp_parser_simple_cast_expression (parser); 6999 7000 /* A delete-expression may not appear in an integral constant 7001 expression. */ 7002 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL)) 7003 return error_mark_node; 7004 7005 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p, 7006 tf_warning_or_error); 7007 } 7008 7009 /* Returns true if TOKEN may start a cast-expression and false 7010 otherwise. */ 7011 7012 static bool 7013 cp_parser_tokens_start_cast_expression (cp_parser *parser) 7014 { 7015 cp_token *token = cp_lexer_peek_token (parser->lexer); 7016 switch (token->type) 7017 { 7018 case CPP_COMMA: 7019 case CPP_SEMICOLON: 7020 case CPP_QUERY: 7021 case CPP_COLON: 7022 case CPP_CLOSE_SQUARE: 7023 case CPP_CLOSE_PAREN: 7024 case CPP_CLOSE_BRACE: 7025 case CPP_DOT: 7026 case CPP_DOT_STAR: 7027 case CPP_DEREF: 7028 case CPP_DEREF_STAR: 7029 case CPP_DIV: 7030 case CPP_MOD: 7031 case CPP_LSHIFT: 7032 case CPP_RSHIFT: 7033 case CPP_LESS: 7034 case CPP_GREATER: 7035 case CPP_LESS_EQ: 7036 case CPP_GREATER_EQ: 7037 case CPP_EQ_EQ: 7038 case CPP_NOT_EQ: 7039 case CPP_EQ: 7040 case CPP_MULT_EQ: 7041 case CPP_DIV_EQ: 7042 case CPP_MOD_EQ: 7043 case CPP_PLUS_EQ: 7044 case CPP_MINUS_EQ: 7045 case CPP_RSHIFT_EQ: 7046 case CPP_LSHIFT_EQ: 7047 case CPP_AND_EQ: 7048 case CPP_XOR_EQ: 7049 case CPP_OR_EQ: 7050 case CPP_XOR: 7051 case CPP_OR: 7052 case CPP_OR_OR: 7053 case CPP_EOF: 7054 return false; 7055 7056 case CPP_OPEN_PAREN: 7057 /* In ((type ()) () the last () isn't a valid cast-expression, 7058 so the whole must be parsed as postfix-expression. */ 7059 return cp_lexer_peek_nth_token (parser->lexer, 2)->type 7060 != CPP_CLOSE_PAREN; 7061 7062 /* '[' may start a primary-expression in obj-c++. */ 7063 case CPP_OPEN_SQUARE: 7064 return c_dialect_objc (); 7065 7066 default: 7067 return true; 7068 } 7069 } 7070 7071 /* Parse a cast-expression. 7072 7073 cast-expression: 7074 unary-expression 7075 ( type-id ) cast-expression 7076 7077 ADDRESS_P is true iff the unary-expression is appearing as the 7078 operand of the `&' operator. CAST_P is true if this expression is 7079 the target of a cast. 7080 7081 Returns a representation of the expression. */ 7082 7083 static tree 7084 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p, 7085 cp_id_kind * pidk) 7086 { 7087 /* If it's a `(', then we might be looking at a cast. */ 7088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 7089 { 7090 tree type = NULL_TREE; 7091 tree expr = NULL_TREE; 7092 bool compound_literal_p; 7093 const char *saved_message; 7094 7095 /* There's no way to know yet whether or not this is a cast. 7096 For example, `(int (3))' is a unary-expression, while `(int) 7097 3' is a cast. So, we resort to parsing tentatively. */ 7098 cp_parser_parse_tentatively (parser); 7099 /* Types may not be defined in a cast. */ 7100 saved_message = parser->type_definition_forbidden_message; 7101 parser->type_definition_forbidden_message 7102 = G_("types may not be defined in casts"); 7103 /* Consume the `('. */ 7104 cp_lexer_consume_token (parser->lexer); 7105 /* A very tricky bit is that `(struct S) { 3 }' is a 7106 compound-literal (which we permit in C++ as an extension). 7107 But, that construct is not a cast-expression -- it is a 7108 postfix-expression. (The reason is that `(struct S) { 3 }.i' 7109 is legal; if the compound-literal were a cast-expression, 7110 you'd need an extra set of parentheses.) But, if we parse 7111 the type-id, and it happens to be a class-specifier, then we 7112 will commit to the parse at that point, because we cannot 7113 undo the action that is done when creating a new class. So, 7114 then we cannot back up and do a postfix-expression. 7115 7116 Therefore, we scan ahead to the closing `)', and check to see 7117 if the token after the `)' is a `{'. If so, we are not 7118 looking at a cast-expression. 7119 7120 Save tokens so that we can put them back. */ 7121 cp_lexer_save_tokens (parser->lexer); 7122 /* Skip tokens until the next token is a closing parenthesis. 7123 If we find the closing `)', and the next token is a `{', then 7124 we are looking at a compound-literal. */ 7125 compound_literal_p 7126 = (cp_parser_skip_to_closing_parenthesis (parser, false, false, 7127 /*consume_paren=*/true) 7128 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)); 7129 /* Roll back the tokens we skipped. */ 7130 cp_lexer_rollback_tokens (parser->lexer); 7131 /* If we were looking at a compound-literal, simulate an error 7132 so that the call to cp_parser_parse_definitely below will 7133 fail. */ 7134 if (compound_literal_p) 7135 cp_parser_simulate_error (parser); 7136 else 7137 { 7138 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; 7139 parser->in_type_id_in_expr_p = true; 7140 /* Look for the type-id. */ 7141 type = cp_parser_type_id (parser); 7142 /* Look for the closing `)'. */ 7143 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 7144 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; 7145 } 7146 7147 /* Restore the saved message. */ 7148 parser->type_definition_forbidden_message = saved_message; 7149 7150 /* At this point this can only be either a cast or a 7151 parenthesized ctor such as `(T ())' that looks like a cast to 7152 function returning T. */ 7153 if (!cp_parser_error_occurred (parser) 7154 && cp_parser_tokens_start_cast_expression (parser)) 7155 { 7156 cp_parser_parse_definitely (parser); 7157 expr = cp_parser_cast_expression (parser, 7158 /*address_p=*/false, 7159 /*cast_p=*/true, pidk); 7160 7161 /* Warn about old-style casts, if so requested. */ 7162 if (warn_old_style_cast 7163 && !in_system_header 7164 && !VOID_TYPE_P (type) 7165 && current_lang_name != lang_name_c) 7166 warning (OPT_Wold_style_cast, "use of old-style cast"); 7167 7168 /* Only type conversions to integral or enumeration types 7169 can be used in constant-expressions. */ 7170 if (!cast_valid_in_integral_constant_expression_p (type) 7171 && cp_parser_non_integral_constant_expression (parser, 7172 NIC_CAST)) 7173 return error_mark_node; 7174 7175 /* Perform the cast. */ 7176 expr = build_c_cast (input_location, type, expr); 7177 return expr; 7178 } 7179 else 7180 cp_parser_abort_tentative_parse (parser); 7181 } 7182 7183 /* If we get here, then it's not a cast, so it must be a 7184 unary-expression. */ 7185 return cp_parser_unary_expression (parser, address_p, cast_p, pidk); 7186 } 7187 7188 /* Parse a binary expression of the general form: 7189 7190 pm-expression: 7191 cast-expression 7192 pm-expression .* cast-expression 7193 pm-expression ->* cast-expression 7194 7195 multiplicative-expression: 7196 pm-expression 7197 multiplicative-expression * pm-expression 7198 multiplicative-expression / pm-expression 7199 multiplicative-expression % pm-expression 7200 7201 additive-expression: 7202 multiplicative-expression 7203 additive-expression + multiplicative-expression 7204 additive-expression - multiplicative-expression 7205 7206 shift-expression: 7207 additive-expression 7208 shift-expression << additive-expression 7209 shift-expression >> additive-expression 7210 7211 relational-expression: 7212 shift-expression 7213 relational-expression < shift-expression 7214 relational-expression > shift-expression 7215 relational-expression <= shift-expression 7216 relational-expression >= shift-expression 7217 7218 GNU Extension: 7219 7220 relational-expression: 7221 relational-expression <? shift-expression 7222 relational-expression >? shift-expression 7223 7224 equality-expression: 7225 relational-expression 7226 equality-expression == relational-expression 7227 equality-expression != relational-expression 7228 7229 and-expression: 7230 equality-expression 7231 and-expression & equality-expression 7232 7233 exclusive-or-expression: 7234 and-expression 7235 exclusive-or-expression ^ and-expression 7236 7237 inclusive-or-expression: 7238 exclusive-or-expression 7239 inclusive-or-expression | exclusive-or-expression 7240 7241 logical-and-expression: 7242 inclusive-or-expression 7243 logical-and-expression && inclusive-or-expression 7244 7245 logical-or-expression: 7246 logical-and-expression 7247 logical-or-expression || logical-and-expression 7248 7249 All these are implemented with a single function like: 7250 7251 binary-expression: 7252 simple-cast-expression 7253 binary-expression <token> binary-expression 7254 7255 CAST_P is true if this expression is the target of a cast. 7256 7257 The binops_by_token map is used to get the tree codes for each <token> type. 7258 binary-expressions are associated according to a precedence table. */ 7259 7260 #define TOKEN_PRECEDENCE(token) \ 7261 (((token->type == CPP_GREATER \ 7262 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \ 7263 && !parser->greater_than_is_operator_p) \ 7264 ? PREC_NOT_OPERATOR \ 7265 : binops_by_token[token->type].prec) 7266 7267 static tree 7268 cp_parser_binary_expression (cp_parser* parser, bool cast_p, 7269 bool no_toplevel_fold_p, 7270 enum cp_parser_prec prec, 7271 cp_id_kind * pidk) 7272 { 7273 cp_parser_expression_stack stack; 7274 cp_parser_expression_stack_entry *sp = &stack[0]; 7275 tree lhs, rhs; 7276 cp_token *token; 7277 enum tree_code tree_type, lhs_type, rhs_type; 7278 enum cp_parser_prec new_prec, lookahead_prec; 7279 tree overload; 7280 7281 /* Parse the first expression. */ 7282 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk); 7283 lhs_type = ERROR_MARK; 7284 7285 if (cp_parser_error_occurred (parser)) 7286 return error_mark_node; 7287 7288 for (;;) 7289 { 7290 /* Get an operator token. */ 7291 token = cp_lexer_peek_token (parser->lexer); 7292 7293 if (warn_cxx0x_compat 7294 && token->type == CPP_RSHIFT 7295 && !parser->greater_than_is_operator_p) 7296 { 7297 if (warning_at (token->location, OPT_Wc__0x_compat, 7298 "%<>>%> operator is treated as" 7299 " two right angle brackets in C++11")) 7300 inform (token->location, 7301 "suggest parentheses around %<>>%> expression"); 7302 } 7303 7304 new_prec = TOKEN_PRECEDENCE (token); 7305 7306 /* Popping an entry off the stack means we completed a subexpression: 7307 - either we found a token which is not an operator (`>' where it is not 7308 an operator, or prec == PREC_NOT_OPERATOR), in which case popping 7309 will happen repeatedly; 7310 - or, we found an operator which has lower priority. This is the case 7311 where the recursive descent *ascends*, as in `3 * 4 + 5' after 7312 parsing `3 * 4'. */ 7313 if (new_prec <= prec) 7314 { 7315 if (sp == stack) 7316 break; 7317 else 7318 goto pop; 7319 } 7320 7321 get_rhs: 7322 tree_type = binops_by_token[token->type].tree_type; 7323 7324 /* We used the operator token. */ 7325 cp_lexer_consume_token (parser->lexer); 7326 7327 /* For "false && x" or "true || x", x will never be executed; 7328 disable warnings while evaluating it. */ 7329 if (tree_type == TRUTH_ANDIF_EXPR) 7330 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node; 7331 else if (tree_type == TRUTH_ORIF_EXPR) 7332 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node; 7333 7334 /* Extract another operand. It may be the RHS of this expression 7335 or the LHS of a new, higher priority expression. */ 7336 rhs = cp_parser_simple_cast_expression (parser); 7337 rhs_type = ERROR_MARK; 7338 7339 /* Get another operator token. Look up its precedence to avoid 7340 building a useless (immediately popped) stack entry for common 7341 cases such as 3 + 4 + 5 or 3 * 4 + 5. */ 7342 token = cp_lexer_peek_token (parser->lexer); 7343 lookahead_prec = TOKEN_PRECEDENCE (token); 7344 if (lookahead_prec > new_prec) 7345 { 7346 /* ... and prepare to parse the RHS of the new, higher priority 7347 expression. Since precedence levels on the stack are 7348 monotonically increasing, we do not have to care about 7349 stack overflows. */ 7350 sp->prec = prec; 7351 sp->tree_type = tree_type; 7352 sp->lhs = lhs; 7353 sp->lhs_type = lhs_type; 7354 sp++; 7355 lhs = rhs; 7356 lhs_type = rhs_type; 7357 prec = new_prec; 7358 new_prec = lookahead_prec; 7359 goto get_rhs; 7360 7361 pop: 7362 lookahead_prec = new_prec; 7363 /* If the stack is not empty, we have parsed into LHS the right side 7364 (`4' in the example above) of an expression we had suspended. 7365 We can use the information on the stack to recover the LHS (`3') 7366 from the stack together with the tree code (`MULT_EXPR'), and 7367 the precedence of the higher level subexpression 7368 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token, 7369 which will be used to actually build the additive expression. */ 7370 --sp; 7371 prec = sp->prec; 7372 tree_type = sp->tree_type; 7373 rhs = lhs; 7374 rhs_type = lhs_type; 7375 lhs = sp->lhs; 7376 lhs_type = sp->lhs_type; 7377 } 7378 7379 /* Undo the disabling of warnings done above. */ 7380 if (tree_type == TRUTH_ANDIF_EXPR) 7381 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node; 7382 else if (tree_type == TRUTH_ORIF_EXPR) 7383 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node; 7384 7385 overload = NULL; 7386 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type == 7387 ERROR_MARK for everything that is not a binary expression. 7388 This makes warn_about_parentheses miss some warnings that 7389 involve unary operators. For unary expressions we should 7390 pass the correct tree_code unless the unary expression was 7391 surrounded by parentheses. 7392 */ 7393 if (no_toplevel_fold_p 7394 && lookahead_prec <= prec 7395 && sp == stack 7396 && TREE_CODE_CLASS (tree_type) == tcc_comparison) 7397 lhs = build2 (tree_type, boolean_type_node, lhs, rhs); 7398 else 7399 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type, 7400 &overload, tf_warning_or_error); 7401 lhs_type = tree_type; 7402 7403 /* If the binary operator required the use of an overloaded operator, 7404 then this expression cannot be an integral constant-expression. 7405 An overloaded operator can be used even if both operands are 7406 otherwise permissible in an integral constant-expression if at 7407 least one of the operands is of enumeration type. */ 7408 7409 if (overload 7410 && cp_parser_non_integral_constant_expression (parser, 7411 NIC_OVERLOADED)) 7412 return error_mark_node; 7413 } 7414 7415 return lhs; 7416 } 7417 7418 7419 /* Parse the `? expression : assignment-expression' part of a 7420 conditional-expression. The LOGICAL_OR_EXPR is the 7421 logical-or-expression that started the conditional-expression. 7422 Returns a representation of the entire conditional-expression. 7423 7424 This routine is used by cp_parser_assignment_expression. 7425 7426 ? expression : assignment-expression 7427 7428 GNU Extensions: 7429 7430 ? : assignment-expression */ 7431 7432 static tree 7433 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr) 7434 { 7435 tree expr; 7436 tree assignment_expr; 7437 struct cp_token *token; 7438 7439 /* Consume the `?' token. */ 7440 cp_lexer_consume_token (parser->lexer); 7441 token = cp_lexer_peek_token (parser->lexer); 7442 if (cp_parser_allow_gnu_extensions_p (parser) 7443 && token->type == CPP_COLON) 7444 { 7445 pedwarn (token->location, OPT_pedantic, 7446 "ISO C++ does not allow ?: with omitted middle operand"); 7447 /* Implicit true clause. */ 7448 expr = NULL_TREE; 7449 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node; 7450 warn_for_omitted_condop (token->location, logical_or_expr); 7451 } 7452 else 7453 { 7454 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 7455 parser->colon_corrects_to_scope_p = false; 7456 /* Parse the expression. */ 7457 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node; 7458 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 7459 c_inhibit_evaluation_warnings += 7460 ((logical_or_expr == truthvalue_true_node) 7461 - (logical_or_expr == truthvalue_false_node)); 7462 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 7463 } 7464 7465 /* The next token should be a `:'. */ 7466 cp_parser_require (parser, CPP_COLON, RT_COLON); 7467 /* Parse the assignment-expression. */ 7468 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL); 7469 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node; 7470 7471 /* Build the conditional-expression. */ 7472 return build_x_conditional_expr (logical_or_expr, 7473 expr, 7474 assignment_expr, 7475 tf_warning_or_error); 7476 } 7477 7478 /* Parse an assignment-expression. 7479 7480 assignment-expression: 7481 conditional-expression 7482 logical-or-expression assignment-operator assignment_expression 7483 throw-expression 7484 7485 CAST_P is true if this expression is the target of a cast. 7486 7487 Returns a representation for the expression. */ 7488 7489 static tree 7490 cp_parser_assignment_expression (cp_parser* parser, bool cast_p, 7491 cp_id_kind * pidk) 7492 { 7493 tree expr; 7494 7495 /* If the next token is the `throw' keyword, then we're looking at 7496 a throw-expression. */ 7497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW)) 7498 expr = cp_parser_throw_expression (parser); 7499 /* Otherwise, it must be that we are looking at a 7500 logical-or-expression. */ 7501 else 7502 { 7503 /* Parse the binary expressions (logical-or-expression). */ 7504 expr = cp_parser_binary_expression (parser, cast_p, false, 7505 PREC_NOT_OPERATOR, pidk); 7506 /* If the next token is a `?' then we're actually looking at a 7507 conditional-expression. */ 7508 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY)) 7509 return cp_parser_question_colon_clause (parser, expr); 7510 else 7511 { 7512 enum tree_code assignment_operator; 7513 7514 /* If it's an assignment-operator, we're using the second 7515 production. */ 7516 assignment_operator 7517 = cp_parser_assignment_operator_opt (parser); 7518 if (assignment_operator != ERROR_MARK) 7519 { 7520 bool non_constant_p; 7521 7522 /* Parse the right-hand side of the assignment. */ 7523 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p); 7524 7525 if (BRACE_ENCLOSED_INITIALIZER_P (rhs)) 7526 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 7527 7528 /* An assignment may not appear in a 7529 constant-expression. */ 7530 if (cp_parser_non_integral_constant_expression (parser, 7531 NIC_ASSIGNMENT)) 7532 return error_mark_node; 7533 /* Build the assignment expression. */ 7534 expr = build_x_modify_expr (expr, 7535 assignment_operator, 7536 rhs, 7537 tf_warning_or_error); 7538 } 7539 } 7540 } 7541 7542 return expr; 7543 } 7544 7545 /* Parse an (optional) assignment-operator. 7546 7547 assignment-operator: one of 7548 = *= /= %= += -= >>= <<= &= ^= |= 7549 7550 GNU Extension: 7551 7552 assignment-operator: one of 7553 <?= >?= 7554 7555 If the next token is an assignment operator, the corresponding tree 7556 code is returned, and the token is consumed. For example, for 7557 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is 7558 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%', 7559 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment 7560 operator, ERROR_MARK is returned. */ 7561 7562 static enum tree_code 7563 cp_parser_assignment_operator_opt (cp_parser* parser) 7564 { 7565 enum tree_code op; 7566 cp_token *token; 7567 7568 /* Peek at the next token. */ 7569 token = cp_lexer_peek_token (parser->lexer); 7570 7571 switch (token->type) 7572 { 7573 case CPP_EQ: 7574 op = NOP_EXPR; 7575 break; 7576 7577 case CPP_MULT_EQ: 7578 op = MULT_EXPR; 7579 break; 7580 7581 case CPP_DIV_EQ: 7582 op = TRUNC_DIV_EXPR; 7583 break; 7584 7585 case CPP_MOD_EQ: 7586 op = TRUNC_MOD_EXPR; 7587 break; 7588 7589 case CPP_PLUS_EQ: 7590 op = PLUS_EXPR; 7591 break; 7592 7593 case CPP_MINUS_EQ: 7594 op = MINUS_EXPR; 7595 break; 7596 7597 case CPP_RSHIFT_EQ: 7598 op = RSHIFT_EXPR; 7599 break; 7600 7601 case CPP_LSHIFT_EQ: 7602 op = LSHIFT_EXPR; 7603 break; 7604 7605 case CPP_AND_EQ: 7606 op = BIT_AND_EXPR; 7607 break; 7608 7609 case CPP_XOR_EQ: 7610 op = BIT_XOR_EXPR; 7611 break; 7612 7613 case CPP_OR_EQ: 7614 op = BIT_IOR_EXPR; 7615 break; 7616 7617 default: 7618 /* Nothing else is an assignment operator. */ 7619 op = ERROR_MARK; 7620 } 7621 7622 /* If it was an assignment operator, consume it. */ 7623 if (op != ERROR_MARK) 7624 cp_lexer_consume_token (parser->lexer); 7625 7626 return op; 7627 } 7628 7629 /* Parse an expression. 7630 7631 expression: 7632 assignment-expression 7633 expression , assignment-expression 7634 7635 CAST_P is true if this expression is the target of a cast. 7636 7637 Returns a representation of the expression. */ 7638 7639 static tree 7640 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk) 7641 { 7642 tree expression = NULL_TREE; 7643 7644 while (true) 7645 { 7646 tree assignment_expression; 7647 7648 /* Parse the next assignment-expression. */ 7649 assignment_expression 7650 = cp_parser_assignment_expression (parser, cast_p, pidk); 7651 /* If this is the first assignment-expression, we can just 7652 save it away. */ 7653 if (!expression) 7654 expression = assignment_expression; 7655 else 7656 expression = build_x_compound_expr (expression, 7657 assignment_expression, 7658 tf_warning_or_error); 7659 /* If the next token is not a comma, then we are done with the 7660 expression. */ 7661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 7662 break; 7663 /* Consume the `,'. */ 7664 cp_lexer_consume_token (parser->lexer); 7665 /* A comma operator cannot appear in a constant-expression. */ 7666 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA)) 7667 expression = error_mark_node; 7668 } 7669 7670 return expression; 7671 } 7672 7673 /* Parse a constant-expression. 7674 7675 constant-expression: 7676 conditional-expression 7677 7678 If ALLOW_NON_CONSTANT_P a non-constant expression is silently 7679 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not 7680 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P 7681 is false, NON_CONSTANT_P should be NULL. */ 7682 7683 static tree 7684 cp_parser_constant_expression (cp_parser* parser, 7685 bool allow_non_constant_p, 7686 bool *non_constant_p) 7687 { 7688 bool saved_integral_constant_expression_p; 7689 bool saved_allow_non_integral_constant_expression_p; 7690 bool saved_non_integral_constant_expression_p; 7691 tree expression; 7692 7693 /* It might seem that we could simply parse the 7694 conditional-expression, and then check to see if it were 7695 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is 7696 one that the compiler can figure out is constant, possibly after 7697 doing some simplifications or optimizations. The standard has a 7698 precise definition of constant-expression, and we must honor 7699 that, even though it is somewhat more restrictive. 7700 7701 For example: 7702 7703 int i[(2, 3)]; 7704 7705 is not a legal declaration, because `(2, 3)' is not a 7706 constant-expression. The `,' operator is forbidden in a 7707 constant-expression. However, GCC's constant-folding machinery 7708 will fold this operation to an INTEGER_CST for `3'. */ 7709 7710 /* Save the old settings. */ 7711 saved_integral_constant_expression_p = parser->integral_constant_expression_p; 7712 saved_allow_non_integral_constant_expression_p 7713 = parser->allow_non_integral_constant_expression_p; 7714 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p; 7715 /* We are now parsing a constant-expression. */ 7716 parser->integral_constant_expression_p = true; 7717 parser->allow_non_integral_constant_expression_p 7718 = (allow_non_constant_p || cxx_dialect >= cxx0x); 7719 parser->non_integral_constant_expression_p = false; 7720 /* Although the grammar says "conditional-expression", we parse an 7721 "assignment-expression", which also permits "throw-expression" 7722 and the use of assignment operators. In the case that 7723 ALLOW_NON_CONSTANT_P is false, we get better errors than we would 7724 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is 7725 actually essential that we look for an assignment-expression. 7726 For example, cp_parser_initializer_clauses uses this function to 7727 determine whether a particular assignment-expression is in fact 7728 constant. */ 7729 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL); 7730 /* Restore the old settings. */ 7731 parser->integral_constant_expression_p 7732 = saved_integral_constant_expression_p; 7733 parser->allow_non_integral_constant_expression_p 7734 = saved_allow_non_integral_constant_expression_p; 7735 if (cxx_dialect >= cxx0x) 7736 { 7737 /* Require an rvalue constant expression here; that's what our 7738 callers expect. Reference constant expressions are handled 7739 separately in e.g. cp_parser_template_argument. */ 7740 bool is_const = potential_rvalue_constant_expression (expression); 7741 parser->non_integral_constant_expression_p = !is_const; 7742 if (!is_const && !allow_non_constant_p) 7743 require_potential_rvalue_constant_expression (expression); 7744 } 7745 if (allow_non_constant_p) 7746 *non_constant_p = parser->non_integral_constant_expression_p; 7747 parser->non_integral_constant_expression_p 7748 = saved_non_integral_constant_expression_p; 7749 7750 return expression; 7751 } 7752 7753 /* Parse __builtin_offsetof. 7754 7755 offsetof-expression: 7756 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")" 7757 7758 offsetof-member-designator: 7759 id-expression 7760 | offsetof-member-designator "." id-expression 7761 | offsetof-member-designator "[" expression "]" 7762 | offsetof-member-designator "->" id-expression */ 7763 7764 static tree 7765 cp_parser_builtin_offsetof (cp_parser *parser) 7766 { 7767 int save_ice_p, save_non_ice_p; 7768 tree type, expr; 7769 cp_id_kind dummy; 7770 cp_token *token; 7771 7772 /* We're about to accept non-integral-constant things, but will 7773 definitely yield an integral constant expression. Save and 7774 restore these values around our local parsing. */ 7775 save_ice_p = parser->integral_constant_expression_p; 7776 save_non_ice_p = parser->non_integral_constant_expression_p; 7777 7778 /* Consume the "__builtin_offsetof" token. */ 7779 cp_lexer_consume_token (parser->lexer); 7780 /* Consume the opening `('. */ 7781 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 7782 /* Parse the type-id. */ 7783 type = cp_parser_type_id (parser); 7784 /* Look for the `,'. */ 7785 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 7786 token = cp_lexer_peek_token (parser->lexer); 7787 7788 /* Build the (type *)null that begins the traditional offsetof macro. */ 7789 expr = build_static_cast (build_pointer_type (type), null_pointer_node, 7790 tf_warning_or_error); 7791 7792 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */ 7793 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr, 7794 true, &dummy, token->location); 7795 while (true) 7796 { 7797 token = cp_lexer_peek_token (parser->lexer); 7798 switch (token->type) 7799 { 7800 case CPP_OPEN_SQUARE: 7801 /* offsetof-member-designator "[" expression "]" */ 7802 expr = cp_parser_postfix_open_square_expression (parser, expr, true); 7803 break; 7804 7805 case CPP_DEREF: 7806 /* offsetof-member-designator "->" identifier */ 7807 expr = grok_array_decl (expr, integer_zero_node); 7808 /* FALLTHRU */ 7809 7810 case CPP_DOT: 7811 /* offsetof-member-designator "." identifier */ 7812 cp_lexer_consume_token (parser->lexer); 7813 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, 7814 expr, true, &dummy, 7815 token->location); 7816 break; 7817 7818 case CPP_CLOSE_PAREN: 7819 /* Consume the ")" token. */ 7820 cp_lexer_consume_token (parser->lexer); 7821 goto success; 7822 7823 default: 7824 /* Error. We know the following require will fail, but 7825 that gives the proper error message. */ 7826 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 7827 cp_parser_skip_to_closing_parenthesis (parser, true, false, true); 7828 expr = error_mark_node; 7829 goto failure; 7830 } 7831 } 7832 7833 success: 7834 /* If we're processing a template, we can't finish the semantics yet. 7835 Otherwise we can fold the entire expression now. */ 7836 if (processing_template_decl) 7837 expr = build1 (OFFSETOF_EXPR, size_type_node, expr); 7838 else 7839 expr = finish_offsetof (expr); 7840 7841 failure: 7842 parser->integral_constant_expression_p = save_ice_p; 7843 parser->non_integral_constant_expression_p = save_non_ice_p; 7844 7845 return expr; 7846 } 7847 7848 /* Parse a trait expression. 7849 7850 Returns a representation of the expression, the underlying type 7851 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */ 7852 7853 static tree 7854 cp_parser_trait_expr (cp_parser* parser, enum rid keyword) 7855 { 7856 cp_trait_kind kind; 7857 tree type1, type2 = NULL_TREE; 7858 bool binary = false; 7859 cp_decl_specifier_seq decl_specs; 7860 7861 switch (keyword) 7862 { 7863 case RID_HAS_NOTHROW_ASSIGN: 7864 kind = CPTK_HAS_NOTHROW_ASSIGN; 7865 break; 7866 case RID_HAS_NOTHROW_CONSTRUCTOR: 7867 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR; 7868 break; 7869 case RID_HAS_NOTHROW_COPY: 7870 kind = CPTK_HAS_NOTHROW_COPY; 7871 break; 7872 case RID_HAS_TRIVIAL_ASSIGN: 7873 kind = CPTK_HAS_TRIVIAL_ASSIGN; 7874 break; 7875 case RID_HAS_TRIVIAL_CONSTRUCTOR: 7876 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR; 7877 break; 7878 case RID_HAS_TRIVIAL_COPY: 7879 kind = CPTK_HAS_TRIVIAL_COPY; 7880 break; 7881 case RID_HAS_TRIVIAL_DESTRUCTOR: 7882 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR; 7883 break; 7884 case RID_HAS_VIRTUAL_DESTRUCTOR: 7885 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR; 7886 break; 7887 case RID_IS_ABSTRACT: 7888 kind = CPTK_IS_ABSTRACT; 7889 break; 7890 case RID_IS_BASE_OF: 7891 kind = CPTK_IS_BASE_OF; 7892 binary = true; 7893 break; 7894 case RID_IS_CLASS: 7895 kind = CPTK_IS_CLASS; 7896 break; 7897 case RID_IS_CONVERTIBLE_TO: 7898 kind = CPTK_IS_CONVERTIBLE_TO; 7899 binary = true; 7900 break; 7901 case RID_IS_EMPTY: 7902 kind = CPTK_IS_EMPTY; 7903 break; 7904 case RID_IS_ENUM: 7905 kind = CPTK_IS_ENUM; 7906 break; 7907 case RID_IS_FINAL: 7908 kind = CPTK_IS_FINAL; 7909 break; 7910 case RID_IS_LITERAL_TYPE: 7911 kind = CPTK_IS_LITERAL_TYPE; 7912 break; 7913 case RID_IS_POD: 7914 kind = CPTK_IS_POD; 7915 break; 7916 case RID_IS_POLYMORPHIC: 7917 kind = CPTK_IS_POLYMORPHIC; 7918 break; 7919 case RID_IS_STD_LAYOUT: 7920 kind = CPTK_IS_STD_LAYOUT; 7921 break; 7922 case RID_IS_TRIVIAL: 7923 kind = CPTK_IS_TRIVIAL; 7924 break; 7925 case RID_IS_UNION: 7926 kind = CPTK_IS_UNION; 7927 break; 7928 case RID_UNDERLYING_TYPE: 7929 kind = CPTK_UNDERLYING_TYPE; 7930 break; 7931 case RID_BASES: 7932 kind = CPTK_BASES; 7933 break; 7934 case RID_DIRECT_BASES: 7935 kind = CPTK_DIRECT_BASES; 7936 break; 7937 default: 7938 gcc_unreachable (); 7939 } 7940 7941 /* Consume the token. */ 7942 cp_lexer_consume_token (parser->lexer); 7943 7944 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 7945 7946 type1 = cp_parser_type_id (parser); 7947 7948 if (type1 == error_mark_node) 7949 return error_mark_node; 7950 7951 /* Build a trivial decl-specifier-seq. */ 7952 clear_decl_specs (&decl_specs); 7953 decl_specs.type = type1; 7954 7955 /* Call grokdeclarator to figure out what type this is. */ 7956 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME, 7957 /*initialized=*/0, /*attrlist=*/NULL); 7958 7959 if (binary) 7960 { 7961 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 7962 7963 type2 = cp_parser_type_id (parser); 7964 7965 if (type2 == error_mark_node) 7966 return error_mark_node; 7967 7968 /* Build a trivial decl-specifier-seq. */ 7969 clear_decl_specs (&decl_specs); 7970 decl_specs.type = type2; 7971 7972 /* Call grokdeclarator to figure out what type this is. */ 7973 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME, 7974 /*initialized=*/0, /*attrlist=*/NULL); 7975 } 7976 7977 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 7978 7979 /* Complete the trait expression, which may mean either processing 7980 the trait expr now or saving it for template instantiation. */ 7981 switch(kind) 7982 { 7983 case CPTK_UNDERLYING_TYPE: 7984 return finish_underlying_type (type1); 7985 case CPTK_BASES: 7986 return finish_bases (type1, false); 7987 case CPTK_DIRECT_BASES: 7988 return finish_bases (type1, true); 7989 default: 7990 return finish_trait_expr (kind, type1, type2); 7991 } 7992 } 7993 7994 /* Lambdas that appear in variable initializer or default argument scope 7995 get that in their mangling, so we need to record it. We might as well 7996 use the count for function and namespace scopes as well. */ 7997 static GTY(()) tree lambda_scope; 7998 static GTY(()) int lambda_count; 7999 typedef struct GTY(()) tree_int 8000 { 8001 tree t; 8002 int i; 8003 } tree_int; 8004 DEF_VEC_O(tree_int); 8005 DEF_VEC_ALLOC_O(tree_int,gc); 8006 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack; 8007 8008 static void 8009 start_lambda_scope (tree decl) 8010 { 8011 tree_int ti; 8012 gcc_assert (decl); 8013 /* Once we're inside a function, we ignore other scopes and just push 8014 the function again so that popping works properly. */ 8015 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL) 8016 decl = current_function_decl; 8017 ti.t = lambda_scope; 8018 ti.i = lambda_count; 8019 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti); 8020 if (lambda_scope != decl) 8021 { 8022 /* Don't reset the count if we're still in the same function. */ 8023 lambda_scope = decl; 8024 lambda_count = 0; 8025 } 8026 } 8027 8028 static void 8029 record_lambda_scope (tree lambda) 8030 { 8031 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope; 8032 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++; 8033 } 8034 8035 static void 8036 finish_lambda_scope (void) 8037 { 8038 tree_int *p = VEC_last (tree_int, lambda_scope_stack); 8039 if (lambda_scope != p->t) 8040 { 8041 lambda_scope = p->t; 8042 lambda_count = p->i; 8043 } 8044 VEC_pop (tree_int, lambda_scope_stack); 8045 } 8046 8047 /* Parse a lambda expression. 8048 8049 lambda-expression: 8050 lambda-introducer lambda-declarator [opt] compound-statement 8051 8052 Returns a representation of the expression. */ 8053 8054 static tree 8055 cp_parser_lambda_expression (cp_parser* parser) 8056 { 8057 tree lambda_expr = build_lambda_expr (); 8058 tree type; 8059 bool ok; 8060 8061 LAMBDA_EXPR_LOCATION (lambda_expr) 8062 = cp_lexer_peek_token (parser->lexer)->location; 8063 8064 if (cp_unevaluated_operand) 8065 error_at (LAMBDA_EXPR_LOCATION (lambda_expr), 8066 "lambda-expression in unevaluated context"); 8067 8068 /* We may be in the middle of deferred access check. Disable 8069 it now. */ 8070 push_deferring_access_checks (dk_no_deferred); 8071 8072 cp_parser_lambda_introducer (parser, lambda_expr); 8073 8074 type = begin_lambda_type (lambda_expr); 8075 if (type == error_mark_node) 8076 return error_mark_node; 8077 8078 record_lambda_scope (lambda_expr); 8079 8080 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */ 8081 determine_visibility (TYPE_NAME (type)); 8082 8083 /* Now that we've started the type, add the capture fields for any 8084 explicit captures. */ 8085 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)); 8086 8087 { 8088 /* Inside the class, surrounding template-parameter-lists do not apply. */ 8089 unsigned int saved_num_template_parameter_lists 8090 = parser->num_template_parameter_lists; 8091 unsigned char in_statement = parser->in_statement; 8092 bool in_switch_statement_p = parser->in_switch_statement_p; 8093 8094 parser->num_template_parameter_lists = 0; 8095 parser->in_statement = 0; 8096 parser->in_switch_statement_p = false; 8097 8098 /* By virtue of defining a local class, a lambda expression has access to 8099 the private variables of enclosing classes. */ 8100 8101 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr); 8102 8103 if (ok) 8104 cp_parser_lambda_body (parser, lambda_expr); 8105 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 8106 cp_parser_skip_to_end_of_block_or_statement (parser); 8107 8108 /* The capture list was built up in reverse order; fix that now. */ 8109 { 8110 tree newlist = NULL_TREE; 8111 tree elt, next; 8112 8113 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); 8114 elt; elt = next) 8115 { 8116 next = TREE_CHAIN (elt); 8117 TREE_CHAIN (elt) = newlist; 8118 newlist = elt; 8119 } 8120 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist; 8121 } 8122 8123 if (ok) 8124 maybe_add_lambda_conv_op (type); 8125 8126 type = finish_struct (type, /*attributes=*/NULL_TREE); 8127 8128 parser->num_template_parameter_lists = saved_num_template_parameter_lists; 8129 parser->in_statement = in_statement; 8130 parser->in_switch_statement_p = in_switch_statement_p; 8131 } 8132 8133 pop_deferring_access_checks (); 8134 8135 /* This field is only used during parsing of the lambda. */ 8136 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE; 8137 8138 /* This lambda shouldn't have any proxies left at this point. */ 8139 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL); 8140 /* And now that we're done, push proxies for an enclosing lambda. */ 8141 insert_pending_capture_proxies (); 8142 8143 if (ok) 8144 return build_lambda_object (lambda_expr); 8145 else 8146 return error_mark_node; 8147 } 8148 8149 /* Parse the beginning of a lambda expression. 8150 8151 lambda-introducer: 8152 [ lambda-capture [opt] ] 8153 8154 LAMBDA_EXPR is the current representation of the lambda expression. */ 8155 8156 static void 8157 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr) 8158 { 8159 /* Need commas after the first capture. */ 8160 bool first = true; 8161 8162 /* Eat the leading `['. */ 8163 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE); 8164 8165 /* Record default capture mode. "[&" "[=" "[&," "[=," */ 8166 if (cp_lexer_next_token_is (parser->lexer, CPP_AND) 8167 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME) 8168 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE; 8169 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 8170 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY; 8171 8172 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE) 8173 { 8174 cp_lexer_consume_token (parser->lexer); 8175 first = false; 8176 } 8177 8178 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE)) 8179 { 8180 cp_token* capture_token; 8181 tree capture_id; 8182 tree capture_init_expr; 8183 cp_id_kind idk = CP_ID_KIND_NONE; 8184 bool explicit_init_p = false; 8185 8186 enum capture_kind_type 8187 { 8188 BY_COPY, 8189 BY_REFERENCE 8190 }; 8191 enum capture_kind_type capture_kind = BY_COPY; 8192 8193 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) 8194 { 8195 error ("expected end of capture-list"); 8196 return; 8197 } 8198 8199 if (first) 8200 first = false; 8201 else 8202 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 8203 8204 /* Possibly capture `this'. */ 8205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS)) 8206 { 8207 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 8208 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY) 8209 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant " 8210 "with by-copy capture default"); 8211 cp_lexer_consume_token (parser->lexer); 8212 add_capture (lambda_expr, 8213 /*id=*/this_identifier, 8214 /*initializer=*/finish_this_expr(), 8215 /*by_reference_p=*/false, 8216 explicit_init_p); 8217 continue; 8218 } 8219 8220 /* Remember whether we want to capture as a reference or not. */ 8221 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)) 8222 { 8223 capture_kind = BY_REFERENCE; 8224 cp_lexer_consume_token (parser->lexer); 8225 } 8226 8227 /* Get the identifier. */ 8228 capture_token = cp_lexer_peek_token (parser->lexer); 8229 capture_id = cp_parser_identifier (parser); 8230 8231 if (capture_id == error_mark_node) 8232 /* Would be nice to have a cp_parser_skip_to_closing_x for general 8233 delimiters, but I modified this to stop on unnested ']' as well. It 8234 was already changed to stop on unnested '}', so the 8235 "closing_parenthesis" name is no more misleading with my change. */ 8236 { 8237 cp_parser_skip_to_closing_parenthesis (parser, 8238 /*recovering=*/true, 8239 /*or_comma=*/true, 8240 /*consume_paren=*/true); 8241 break; 8242 } 8243 8244 /* Find the initializer for this capture. */ 8245 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 8246 { 8247 /* An explicit expression exists. */ 8248 cp_lexer_consume_token (parser->lexer); 8249 pedwarn (input_location, OPT_pedantic, 8250 "ISO C++ does not allow initializers " 8251 "in lambda expression capture lists"); 8252 capture_init_expr = cp_parser_assignment_expression (parser, 8253 /*cast_p=*/true, 8254 &idk); 8255 explicit_init_p = true; 8256 } 8257 else 8258 { 8259 const char* error_msg; 8260 8261 /* Turn the identifier into an id-expression. */ 8262 capture_init_expr 8263 = cp_parser_lookup_name 8264 (parser, 8265 capture_id, 8266 none_type, 8267 /*is_template=*/false, 8268 /*is_namespace=*/false, 8269 /*check_dependency=*/true, 8270 /*ambiguous_decls=*/NULL, 8271 capture_token->location); 8272 8273 if (capture_init_expr == error_mark_node) 8274 { 8275 unqualified_name_lookup_error (capture_id); 8276 continue; 8277 } 8278 else if (DECL_P (capture_init_expr) 8279 && (TREE_CODE (capture_init_expr) != VAR_DECL 8280 && TREE_CODE (capture_init_expr) != PARM_DECL)) 8281 { 8282 error_at (capture_token->location, 8283 "capture of non-variable %qD ", 8284 capture_init_expr); 8285 inform (0, "%q+#D declared here", capture_init_expr); 8286 continue; 8287 } 8288 if (TREE_CODE (capture_init_expr) == VAR_DECL 8289 && decl_storage_duration (capture_init_expr) != dk_auto) 8290 { 8291 pedwarn (capture_token->location, 0, "capture of variable " 8292 "%qD with non-automatic storage duration", 8293 capture_init_expr); 8294 inform (0, "%q+#D declared here", capture_init_expr); 8295 continue; 8296 } 8297 8298 capture_init_expr 8299 = finish_id_expression 8300 (capture_id, 8301 capture_init_expr, 8302 parser->scope, 8303 &idk, 8304 /*integral_constant_expression_p=*/false, 8305 /*allow_non_integral_constant_expression_p=*/false, 8306 /*non_integral_constant_expression_p=*/NULL, 8307 /*template_p=*/false, 8308 /*done=*/true, 8309 /*address_p=*/false, 8310 /*template_arg_p=*/false, 8311 &error_msg, 8312 capture_token->location); 8313 } 8314 8315 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE 8316 && !explicit_init_p) 8317 { 8318 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY 8319 && capture_kind == BY_COPY) 8320 pedwarn (capture_token->location, 0, "explicit by-copy capture " 8321 "of %qD redundant with by-copy capture default", 8322 capture_id); 8323 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE 8324 && capture_kind == BY_REFERENCE) 8325 pedwarn (capture_token->location, 0, "explicit by-reference " 8326 "capture of %qD redundant with by-reference capture " 8327 "default", capture_id); 8328 } 8329 8330 add_capture (lambda_expr, 8331 capture_id, 8332 capture_init_expr, 8333 /*by_reference_p=*/capture_kind == BY_REFERENCE, 8334 explicit_init_p); 8335 } 8336 8337 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 8338 } 8339 8340 /* Parse the (optional) middle of a lambda expression. 8341 8342 lambda-declarator: 8343 ( parameter-declaration-clause [opt] ) 8344 attribute-specifier [opt] 8345 mutable [opt] 8346 exception-specification [opt] 8347 lambda-return-type-clause [opt] 8348 8349 LAMBDA_EXPR is the current representation of the lambda expression. */ 8350 8351 static bool 8352 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr) 8353 { 8354 /* 5.1.1.4 of the standard says: 8355 If a lambda-expression does not include a lambda-declarator, it is as if 8356 the lambda-declarator were (). 8357 This means an empty parameter list, no attributes, and no exception 8358 specification. */ 8359 tree param_list = void_list_node; 8360 tree attributes = NULL_TREE; 8361 tree exception_spec = NULL_TREE; 8362 tree t; 8363 8364 /* The lambda-declarator is optional, but must begin with an opening 8365 parenthesis if present. */ 8366 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 8367 { 8368 cp_lexer_consume_token (parser->lexer); 8369 8370 begin_scope (sk_function_parms, /*entity=*/NULL_TREE); 8371 8372 /* Parse parameters. */ 8373 param_list = cp_parser_parameter_declaration_clause (parser); 8374 8375 /* Default arguments shall not be specified in the 8376 parameter-declaration-clause of a lambda-declarator. */ 8377 for (t = param_list; t; t = TREE_CHAIN (t)) 8378 if (TREE_PURPOSE (t)) 8379 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic, 8380 "default argument specified for lambda parameter"); 8381 8382 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 8383 8384 attributes = cp_parser_attributes_opt (parser); 8385 8386 /* Parse optional `mutable' keyword. */ 8387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE)) 8388 { 8389 cp_lexer_consume_token (parser->lexer); 8390 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1; 8391 } 8392 8393 /* Parse optional exception specification. */ 8394 exception_spec = cp_parser_exception_specification_opt (parser); 8395 8396 /* Parse optional trailing return type. */ 8397 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF)) 8398 { 8399 cp_lexer_consume_token (parser->lexer); 8400 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser); 8401 } 8402 8403 /* The function parameters must be in scope all the way until after the 8404 trailing-return-type in case of decltype. */ 8405 for (t = current_binding_level->names; t; t = DECL_CHAIN (t)) 8406 pop_binding (DECL_NAME (t), t); 8407 8408 leave_scope (); 8409 } 8410 8411 /* Create the function call operator. 8412 8413 Messing with declarators like this is no uglier than building up the 8414 FUNCTION_DECL by hand, and this is less likely to get out of sync with 8415 other code. */ 8416 { 8417 cp_decl_specifier_seq return_type_specs; 8418 cp_declarator* declarator; 8419 tree fco; 8420 int quals; 8421 void *p; 8422 8423 clear_decl_specs (&return_type_specs); 8424 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr)) 8425 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr); 8426 else 8427 /* Maybe we will deduce the return type later, but we can use void 8428 as a placeholder return type anyways. */ 8429 return_type_specs.type = void_type_node; 8430 8431 p = obstack_alloc (&declarator_obstack, 0); 8432 8433 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR), 8434 sfk_none); 8435 8436 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr) 8437 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST); 8438 declarator = make_call_declarator (declarator, param_list, quals, 8439 VIRT_SPEC_UNSPECIFIED, 8440 exception_spec, 8441 /*late_return_type=*/NULL_TREE); 8442 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr); 8443 8444 fco = grokmethod (&return_type_specs, 8445 declarator, 8446 attributes); 8447 if (fco != error_mark_node) 8448 { 8449 DECL_INITIALIZED_IN_CLASS_P (fco) = 1; 8450 DECL_ARTIFICIAL (fco) = 1; 8451 /* Give the object parameter a different name. */ 8452 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure"); 8453 } 8454 8455 finish_member_declaration (fco); 8456 8457 obstack_free (&declarator_obstack, p); 8458 8459 return (fco != error_mark_node); 8460 } 8461 } 8462 8463 /* Parse the body of a lambda expression, which is simply 8464 8465 compound-statement 8466 8467 but which requires special handling. 8468 LAMBDA_EXPR is the current representation of the lambda expression. */ 8469 8470 static void 8471 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr) 8472 { 8473 bool nested = (current_function_decl != NULL_TREE); 8474 bool local_variables_forbidden_p = parser->local_variables_forbidden_p; 8475 if (nested) 8476 push_function_context (); 8477 else 8478 /* Still increment function_depth so that we don't GC in the 8479 middle of an expression. */ 8480 ++function_depth; 8481 /* Clear this in case we're in the middle of a default argument. */ 8482 parser->local_variables_forbidden_p = false; 8483 8484 /* Finish the function call operator 8485 - class_specifier 8486 + late_parsing_for_member 8487 + function_definition_after_declarator 8488 + ctor_initializer_opt_and_function_body */ 8489 { 8490 tree fco = lambda_function (lambda_expr); 8491 tree body; 8492 bool done = false; 8493 tree compound_stmt; 8494 tree cap; 8495 8496 /* Let the front end know that we are going to be defining this 8497 function. */ 8498 start_preparsed_function (fco, 8499 NULL_TREE, 8500 SF_PRE_PARSED | SF_INCLASS_INLINE); 8501 8502 start_lambda_scope (fco); 8503 body = begin_function_body (); 8504 8505 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 8506 goto out; 8507 8508 /* Push the proxies for any explicit captures. */ 8509 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap; 8510 cap = TREE_CHAIN (cap)) 8511 build_capture_proxy (TREE_PURPOSE (cap)); 8512 8513 compound_stmt = begin_compound_stmt (0); 8514 8515 /* 5.1.1.4 of the standard says: 8516 If a lambda-expression does not include a trailing-return-type, it 8517 is as if the trailing-return-type denotes the following type: 8518 * if the compound-statement is of the form 8519 { return attribute-specifier [opt] expression ; } 8520 the type of the returned expression after lvalue-to-rvalue 8521 conversion (_conv.lval_ 4.1), array-to-pointer conversion 8522 (_conv.array_ 4.2), and function-to-pointer conversion 8523 (_conv.func_ 4.3); 8524 * otherwise, void. */ 8525 8526 /* In a lambda that has neither a lambda-return-type-clause 8527 nor a deducible form, errors should be reported for return statements 8528 in the body. Since we used void as the placeholder return type, parsing 8529 the body as usual will give such desired behavior. */ 8530 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr) 8531 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN 8532 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON) 8533 { 8534 tree expr = NULL_TREE; 8535 cp_id_kind idk = CP_ID_KIND_NONE; 8536 8537 /* Parse tentatively in case there's more after the initial return 8538 statement. */ 8539 cp_parser_parse_tentatively (parser); 8540 8541 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN); 8542 8543 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk); 8544 8545 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 8546 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 8547 8548 if (cp_parser_parse_definitely (parser)) 8549 { 8550 apply_lambda_return_type (lambda_expr, lambda_return_type (expr)); 8551 8552 /* Will get error here if type not deduced yet. */ 8553 finish_return_stmt (expr); 8554 8555 done = true; 8556 } 8557 } 8558 8559 if (!done) 8560 { 8561 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)) 8562 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true; 8563 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL)) 8564 cp_parser_label_declaration (parser); 8565 cp_parser_statement_seq_opt (parser, NULL_TREE); 8566 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 8567 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false; 8568 } 8569 8570 finish_compound_stmt (compound_stmt); 8571 8572 out: 8573 finish_function_body (body); 8574 finish_lambda_scope (); 8575 8576 /* Finish the function and generate code for it if necessary. */ 8577 expand_or_defer_fn (finish_function (/*inline*/2)); 8578 } 8579 8580 parser->local_variables_forbidden_p = local_variables_forbidden_p; 8581 if (nested) 8582 pop_function_context(); 8583 else 8584 --function_depth; 8585 } 8586 8587 /* Statements [gram.stmt.stmt] */ 8588 8589 /* Parse a statement. 8590 8591 statement: 8592 labeled-statement 8593 expression-statement 8594 compound-statement 8595 selection-statement 8596 iteration-statement 8597 jump-statement 8598 declaration-statement 8599 try-block 8600 8601 TM Extension: 8602 8603 statement: 8604 atomic-statement 8605 8606 IN_COMPOUND is true when the statement is nested inside a 8607 cp_parser_compound_statement; this matters for certain pragmas. 8608 8609 If IF_P is not NULL, *IF_P is set to indicate whether the statement 8610 is a (possibly labeled) if statement which is not enclosed in braces 8611 and has an else clause. This is used to implement -Wparentheses. */ 8612 8613 static void 8614 cp_parser_statement (cp_parser* parser, tree in_statement_expr, 8615 bool in_compound, bool *if_p) 8616 { 8617 tree statement; 8618 cp_token *token; 8619 location_t statement_location; 8620 8621 restart: 8622 if (if_p != NULL) 8623 *if_p = false; 8624 /* There is no statement yet. */ 8625 statement = NULL_TREE; 8626 /* Peek at the next token. */ 8627 token = cp_lexer_peek_token (parser->lexer); 8628 /* Remember the location of the first token in the statement. */ 8629 statement_location = token->location; 8630 /* If this is a keyword, then that will often determine what kind of 8631 statement we have. */ 8632 if (token->type == CPP_KEYWORD) 8633 { 8634 enum rid keyword = token->keyword; 8635 8636 switch (keyword) 8637 { 8638 case RID_CASE: 8639 case RID_DEFAULT: 8640 /* Looks like a labeled-statement with a case label. 8641 Parse the label, and then use tail recursion to parse 8642 the statement. */ 8643 cp_parser_label_for_labeled_statement (parser); 8644 goto restart; 8645 8646 case RID_IF: 8647 case RID_SWITCH: 8648 statement = cp_parser_selection_statement (parser, if_p); 8649 break; 8650 8651 case RID_WHILE: 8652 case RID_DO: 8653 case RID_FOR: 8654 statement = cp_parser_iteration_statement (parser); 8655 break; 8656 8657 case RID_BREAK: 8658 case RID_CONTINUE: 8659 case RID_RETURN: 8660 case RID_GOTO: 8661 statement = cp_parser_jump_statement (parser); 8662 break; 8663 8664 /* Objective-C++ exception-handling constructs. */ 8665 case RID_AT_TRY: 8666 case RID_AT_CATCH: 8667 case RID_AT_FINALLY: 8668 case RID_AT_SYNCHRONIZED: 8669 case RID_AT_THROW: 8670 statement = cp_parser_objc_statement (parser); 8671 break; 8672 8673 case RID_TRY: 8674 statement = cp_parser_try_block (parser); 8675 break; 8676 8677 case RID_NAMESPACE: 8678 /* This must be a namespace alias definition. */ 8679 cp_parser_declaration_statement (parser); 8680 return; 8681 8682 case RID_TRANSACTION_ATOMIC: 8683 case RID_TRANSACTION_RELAXED: 8684 statement = cp_parser_transaction (parser, keyword); 8685 break; 8686 case RID_TRANSACTION_CANCEL: 8687 statement = cp_parser_transaction_cancel (parser); 8688 break; 8689 8690 default: 8691 /* It might be a keyword like `int' that can start a 8692 declaration-statement. */ 8693 break; 8694 } 8695 } 8696 else if (token->type == CPP_NAME) 8697 { 8698 /* If the next token is a `:', then we are looking at a 8699 labeled-statement. */ 8700 token = cp_lexer_peek_nth_token (parser->lexer, 2); 8701 if (token->type == CPP_COLON) 8702 { 8703 /* Looks like a labeled-statement with an ordinary label. 8704 Parse the label, and then use tail recursion to parse 8705 the statement. */ 8706 cp_parser_label_for_labeled_statement (parser); 8707 goto restart; 8708 } 8709 } 8710 /* Anything that starts with a `{' must be a compound-statement. */ 8711 else if (token->type == CPP_OPEN_BRACE) 8712 statement = cp_parser_compound_statement (parser, NULL, false, false); 8713 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes 8714 a statement all its own. */ 8715 else if (token->type == CPP_PRAGMA) 8716 { 8717 /* Only certain OpenMP pragmas are attached to statements, and thus 8718 are considered statements themselves. All others are not. In 8719 the context of a compound, accept the pragma as a "statement" and 8720 return so that we can check for a close brace. Otherwise we 8721 require a real statement and must go back and read one. */ 8722 if (in_compound) 8723 cp_parser_pragma (parser, pragma_compound); 8724 else if (!cp_parser_pragma (parser, pragma_stmt)) 8725 goto restart; 8726 return; 8727 } 8728 else if (token->type == CPP_EOF) 8729 { 8730 cp_parser_error (parser, "expected statement"); 8731 return; 8732 } 8733 8734 /* Everything else must be a declaration-statement or an 8735 expression-statement. Try for the declaration-statement 8736 first, unless we are looking at a `;', in which case we know that 8737 we have an expression-statement. */ 8738 if (!statement) 8739 { 8740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 8741 { 8742 cp_parser_parse_tentatively (parser); 8743 /* Try to parse the declaration-statement. */ 8744 cp_parser_declaration_statement (parser); 8745 /* If that worked, we're done. */ 8746 if (cp_parser_parse_definitely (parser)) 8747 return; 8748 } 8749 /* Look for an expression-statement instead. */ 8750 statement = cp_parser_expression_statement (parser, in_statement_expr); 8751 } 8752 8753 /* Set the line number for the statement. */ 8754 if (statement && STATEMENT_CODE_P (TREE_CODE (statement))) 8755 SET_EXPR_LOCATION (statement, statement_location); 8756 } 8757 8758 /* Parse the label for a labeled-statement, i.e. 8759 8760 identifier : 8761 case constant-expression : 8762 default : 8763 8764 GNU Extension: 8765 case constant-expression ... constant-expression : statement 8766 8767 When a label is parsed without errors, the label is added to the 8768 parse tree by the finish_* functions, so this function doesn't 8769 have to return the label. */ 8770 8771 static void 8772 cp_parser_label_for_labeled_statement (cp_parser* parser) 8773 { 8774 cp_token *token; 8775 tree label = NULL_TREE; 8776 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 8777 8778 /* The next token should be an identifier. */ 8779 token = cp_lexer_peek_token (parser->lexer); 8780 if (token->type != CPP_NAME 8781 && token->type != CPP_KEYWORD) 8782 { 8783 cp_parser_error (parser, "expected labeled-statement"); 8784 return; 8785 } 8786 8787 parser->colon_corrects_to_scope_p = false; 8788 switch (token->keyword) 8789 { 8790 case RID_CASE: 8791 { 8792 tree expr, expr_hi; 8793 cp_token *ellipsis; 8794 8795 /* Consume the `case' token. */ 8796 cp_lexer_consume_token (parser->lexer); 8797 /* Parse the constant-expression. */ 8798 expr = cp_parser_constant_expression (parser, 8799 /*allow_non_constant_p=*/false, 8800 NULL); 8801 8802 ellipsis = cp_lexer_peek_token (parser->lexer); 8803 if (ellipsis->type == CPP_ELLIPSIS) 8804 { 8805 /* Consume the `...' token. */ 8806 cp_lexer_consume_token (parser->lexer); 8807 expr_hi = 8808 cp_parser_constant_expression (parser, 8809 /*allow_non_constant_p=*/false, 8810 NULL); 8811 /* We don't need to emit warnings here, as the common code 8812 will do this for us. */ 8813 } 8814 else 8815 expr_hi = NULL_TREE; 8816 8817 if (parser->in_switch_statement_p) 8818 finish_case_label (token->location, expr, expr_hi); 8819 else 8820 error_at (token->location, 8821 "case label %qE not within a switch statement", 8822 expr); 8823 } 8824 break; 8825 8826 case RID_DEFAULT: 8827 /* Consume the `default' token. */ 8828 cp_lexer_consume_token (parser->lexer); 8829 8830 if (parser->in_switch_statement_p) 8831 finish_case_label (token->location, NULL_TREE, NULL_TREE); 8832 else 8833 error_at (token->location, "case label not within a switch statement"); 8834 break; 8835 8836 default: 8837 /* Anything else must be an ordinary label. */ 8838 label = finish_label_stmt (cp_parser_identifier (parser)); 8839 break; 8840 } 8841 8842 /* Require the `:' token. */ 8843 cp_parser_require (parser, CPP_COLON, RT_COLON); 8844 8845 /* An ordinary label may optionally be followed by attributes. 8846 However, this is only permitted if the attributes are then 8847 followed by a semicolon. This is because, for backward 8848 compatibility, when parsing 8849 lab: __attribute__ ((unused)) int i; 8850 we want the attribute to attach to "i", not "lab". */ 8851 if (label != NULL_TREE 8852 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) 8853 { 8854 tree attrs; 8855 8856 cp_parser_parse_tentatively (parser); 8857 attrs = cp_parser_attributes_opt (parser); 8858 if (attrs == NULL_TREE 8859 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 8860 cp_parser_abort_tentative_parse (parser); 8861 else if (!cp_parser_parse_definitely (parser)) 8862 ; 8863 else 8864 cplus_decl_attributes (&label, attrs, 0); 8865 } 8866 8867 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 8868 } 8869 8870 /* Parse an expression-statement. 8871 8872 expression-statement: 8873 expression [opt] ; 8874 8875 Returns the new EXPR_STMT -- or NULL_TREE if the expression 8876 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P 8877 indicates whether this expression-statement is part of an 8878 expression statement. */ 8879 8880 static tree 8881 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr) 8882 { 8883 tree statement = NULL_TREE; 8884 cp_token *token = cp_lexer_peek_token (parser->lexer); 8885 8886 /* If the next token is a ';', then there is no expression 8887 statement. */ 8888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 8889 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL); 8890 8891 /* Give a helpful message for "A<T>::type t;" and the like. */ 8892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) 8893 && !cp_parser_uncommitted_to_tentative_parse_p (parser)) 8894 { 8895 if (TREE_CODE (statement) == SCOPE_REF) 8896 error_at (token->location, "need %<typename%> before %qE because " 8897 "%qT is a dependent scope", 8898 statement, TREE_OPERAND (statement, 0)); 8899 else if (is_overloaded_fn (statement) 8900 && DECL_CONSTRUCTOR_P (get_first_fn (statement))) 8901 { 8902 /* A::A a; */ 8903 tree fn = get_first_fn (statement); 8904 error_at (token->location, 8905 "%<%T::%D%> names the constructor, not the type", 8906 DECL_CONTEXT (fn), DECL_NAME (fn)); 8907 } 8908 } 8909 8910 /* Consume the final `;'. */ 8911 cp_parser_consume_semicolon_at_end_of_statement (parser); 8912 8913 if (in_statement_expr 8914 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 8915 /* This is the final expression statement of a statement 8916 expression. */ 8917 statement = finish_stmt_expr_expr (statement, in_statement_expr); 8918 else if (statement) 8919 statement = finish_expr_stmt (statement); 8920 else 8921 finish_stmt (); 8922 8923 return statement; 8924 } 8925 8926 /* Parse a compound-statement. 8927 8928 compound-statement: 8929 { statement-seq [opt] } 8930 8931 GNU extension: 8932 8933 compound-statement: 8934 { label-declaration-seq [opt] statement-seq [opt] } 8935 8936 label-declaration-seq: 8937 label-declaration 8938 label-declaration-seq label-declaration 8939 8940 Returns a tree representing the statement. */ 8941 8942 static tree 8943 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr, 8944 bool in_try, bool function_body) 8945 { 8946 tree compound_stmt; 8947 8948 /* Consume the `{'. */ 8949 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 8950 return error_mark_node; 8951 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl) 8952 && !function_body) 8953 pedwarn (input_location, OPT_pedantic, 8954 "compound-statement in constexpr function"); 8955 /* Begin the compound-statement. */ 8956 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0); 8957 /* If the next keyword is `__label__' we have a label declaration. */ 8958 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL)) 8959 cp_parser_label_declaration (parser); 8960 /* Parse an (optional) statement-seq. */ 8961 cp_parser_statement_seq_opt (parser, in_statement_expr); 8962 /* Finish the compound-statement. */ 8963 finish_compound_stmt (compound_stmt); 8964 /* Consume the `}'. */ 8965 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 8966 8967 return compound_stmt; 8968 } 8969 8970 /* Parse an (optional) statement-seq. 8971 8972 statement-seq: 8973 statement 8974 statement-seq [opt] statement */ 8975 8976 static void 8977 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr) 8978 { 8979 /* Scan statements until there aren't any more. */ 8980 while (true) 8981 { 8982 cp_token *token = cp_lexer_peek_token (parser->lexer); 8983 8984 /* If we are looking at a `}', then we have run out of 8985 statements; the same is true if we have reached the end 8986 of file, or have stumbled upon a stray '@end'. */ 8987 if (token->type == CPP_CLOSE_BRACE 8988 || token->type == CPP_EOF 8989 || token->type == CPP_PRAGMA_EOL 8990 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)) 8991 break; 8992 8993 /* If we are in a compound statement and find 'else' then 8994 something went wrong. */ 8995 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE) 8996 { 8997 if (parser->in_statement & IN_IF_STMT) 8998 break; 8999 else 9000 { 9001 token = cp_lexer_consume_token (parser->lexer); 9002 error_at (token->location, "%<else%> without a previous %<if%>"); 9003 } 9004 } 9005 9006 /* Parse the statement. */ 9007 cp_parser_statement (parser, in_statement_expr, true, NULL); 9008 } 9009 } 9010 9011 /* Parse a selection-statement. 9012 9013 selection-statement: 9014 if ( condition ) statement 9015 if ( condition ) statement else statement 9016 switch ( condition ) statement 9017 9018 Returns the new IF_STMT or SWITCH_STMT. 9019 9020 If IF_P is not NULL, *IF_P is set to indicate whether the statement 9021 is a (possibly labeled) if statement which is not enclosed in 9022 braces and has an else clause. This is used to implement 9023 -Wparentheses. */ 9024 9025 static tree 9026 cp_parser_selection_statement (cp_parser* parser, bool *if_p) 9027 { 9028 cp_token *token; 9029 enum rid keyword; 9030 9031 if (if_p != NULL) 9032 *if_p = false; 9033 9034 /* Peek at the next token. */ 9035 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT); 9036 9037 /* See what kind of keyword it is. */ 9038 keyword = token->keyword; 9039 switch (keyword) 9040 { 9041 case RID_IF: 9042 case RID_SWITCH: 9043 { 9044 tree statement; 9045 tree condition; 9046 9047 /* Look for the `('. */ 9048 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 9049 { 9050 cp_parser_skip_to_end_of_statement (parser); 9051 return error_mark_node; 9052 } 9053 9054 /* Begin the selection-statement. */ 9055 if (keyword == RID_IF) 9056 statement = begin_if_stmt (); 9057 else 9058 statement = begin_switch_stmt (); 9059 9060 /* Parse the condition. */ 9061 condition = cp_parser_condition (parser); 9062 /* Look for the `)'. */ 9063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 9064 cp_parser_skip_to_closing_parenthesis (parser, true, false, 9065 /*consume_paren=*/true); 9066 9067 if (keyword == RID_IF) 9068 { 9069 bool nested_if; 9070 unsigned char in_statement; 9071 9072 /* Add the condition. */ 9073 finish_if_stmt_cond (condition, statement); 9074 9075 /* Parse the then-clause. */ 9076 in_statement = parser->in_statement; 9077 parser->in_statement |= IN_IF_STMT; 9078 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 9079 { 9080 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 9081 add_stmt (build_empty_stmt (loc)); 9082 cp_lexer_consume_token (parser->lexer); 9083 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE)) 9084 warning_at (loc, OPT_Wempty_body, "suggest braces around " 9085 "empty body in an %<if%> statement"); 9086 nested_if = false; 9087 } 9088 else 9089 cp_parser_implicitly_scoped_statement (parser, &nested_if); 9090 parser->in_statement = in_statement; 9091 9092 finish_then_clause (statement); 9093 9094 /* If the next token is `else', parse the else-clause. */ 9095 if (cp_lexer_next_token_is_keyword (parser->lexer, 9096 RID_ELSE)) 9097 { 9098 /* Consume the `else' keyword. */ 9099 cp_lexer_consume_token (parser->lexer); 9100 begin_else_clause (statement); 9101 /* Parse the else-clause. */ 9102 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 9103 { 9104 location_t loc; 9105 loc = cp_lexer_peek_token (parser->lexer)->location; 9106 warning_at (loc, 9107 OPT_Wempty_body, "suggest braces around " 9108 "empty body in an %<else%> statement"); 9109 add_stmt (build_empty_stmt (loc)); 9110 cp_lexer_consume_token (parser->lexer); 9111 } 9112 else 9113 cp_parser_implicitly_scoped_statement (parser, NULL); 9114 9115 finish_else_clause (statement); 9116 9117 /* If we are currently parsing a then-clause, then 9118 IF_P will not be NULL. We set it to true to 9119 indicate that this if statement has an else clause. 9120 This may trigger the Wparentheses warning below 9121 when we get back up to the parent if statement. */ 9122 if (if_p != NULL) 9123 *if_p = true; 9124 } 9125 else 9126 { 9127 /* This if statement does not have an else clause. If 9128 NESTED_IF is true, then the then-clause is an if 9129 statement which does have an else clause. We warn 9130 about the potential ambiguity. */ 9131 if (nested_if) 9132 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses, 9133 "suggest explicit braces to avoid ambiguous" 9134 " %<else%>"); 9135 } 9136 9137 /* Now we're all done with the if-statement. */ 9138 finish_if_stmt (statement); 9139 } 9140 else 9141 { 9142 bool in_switch_statement_p; 9143 unsigned char in_statement; 9144 9145 /* Add the condition. */ 9146 finish_switch_cond (condition, statement); 9147 9148 /* Parse the body of the switch-statement. */ 9149 in_switch_statement_p = parser->in_switch_statement_p; 9150 in_statement = parser->in_statement; 9151 parser->in_switch_statement_p = true; 9152 parser->in_statement |= IN_SWITCH_STMT; 9153 cp_parser_implicitly_scoped_statement (parser, NULL); 9154 parser->in_switch_statement_p = in_switch_statement_p; 9155 parser->in_statement = in_statement; 9156 9157 /* Now we're all done with the switch-statement. */ 9158 finish_switch_stmt (statement); 9159 } 9160 9161 return statement; 9162 } 9163 break; 9164 9165 default: 9166 cp_parser_error (parser, "expected selection-statement"); 9167 return error_mark_node; 9168 } 9169 } 9170 9171 /* Parse a condition. 9172 9173 condition: 9174 expression 9175 type-specifier-seq declarator = initializer-clause 9176 type-specifier-seq declarator braced-init-list 9177 9178 GNU Extension: 9179 9180 condition: 9181 type-specifier-seq declarator asm-specification [opt] 9182 attributes [opt] = assignment-expression 9183 9184 Returns the expression that should be tested. */ 9185 9186 static tree 9187 cp_parser_condition (cp_parser* parser) 9188 { 9189 cp_decl_specifier_seq type_specifiers; 9190 const char *saved_message; 9191 int declares_class_or_enum; 9192 9193 /* Try the declaration first. */ 9194 cp_parser_parse_tentatively (parser); 9195 /* New types are not allowed in the type-specifier-seq for a 9196 condition. */ 9197 saved_message = parser->type_definition_forbidden_message; 9198 parser->type_definition_forbidden_message 9199 = G_("types may not be defined in conditions"); 9200 /* Parse the type-specifier-seq. */ 9201 cp_parser_decl_specifier_seq (parser, 9202 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR, 9203 &type_specifiers, 9204 &declares_class_or_enum); 9205 /* Restore the saved message. */ 9206 parser->type_definition_forbidden_message = saved_message; 9207 /* If all is well, we might be looking at a declaration. */ 9208 if (!cp_parser_error_occurred (parser)) 9209 { 9210 tree decl; 9211 tree asm_specification; 9212 tree attributes; 9213 cp_declarator *declarator; 9214 tree initializer = NULL_TREE; 9215 9216 /* Parse the declarator. */ 9217 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 9218 /*ctor_dtor_or_conv_p=*/NULL, 9219 /*parenthesized_p=*/NULL, 9220 /*member_p=*/false); 9221 /* Parse the attributes. */ 9222 attributes = cp_parser_attributes_opt (parser); 9223 /* Parse the asm-specification. */ 9224 asm_specification = cp_parser_asm_specification_opt (parser); 9225 /* If the next token is not an `=' or '{', then we might still be 9226 looking at an expression. For example: 9227 9228 if (A(a).x) 9229 9230 looks like a decl-specifier-seq and a declarator -- but then 9231 there is no `=', so this is an expression. */ 9232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) 9233 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) 9234 cp_parser_simulate_error (parser); 9235 9236 /* If we did see an `=' or '{', then we are looking at a declaration 9237 for sure. */ 9238 if (cp_parser_parse_definitely (parser)) 9239 { 9240 tree pushed_scope; 9241 bool non_constant_p; 9242 bool flags = LOOKUP_ONLYCONVERTING; 9243 9244 /* Create the declaration. */ 9245 decl = start_decl (declarator, &type_specifiers, 9246 /*initialized_p=*/true, 9247 attributes, /*prefix_attributes=*/NULL_TREE, 9248 &pushed_scope); 9249 9250 /* Parse the initializer. */ 9251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 9252 { 9253 initializer = cp_parser_braced_list (parser, &non_constant_p); 9254 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1; 9255 flags = 0; 9256 } 9257 else 9258 { 9259 /* Consume the `='. */ 9260 cp_parser_require (parser, CPP_EQ, RT_EQ); 9261 initializer = cp_parser_initializer_clause (parser, &non_constant_p); 9262 } 9263 if (BRACE_ENCLOSED_INITIALIZER_P (initializer)) 9264 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 9265 9266 /* Process the initializer. */ 9267 cp_finish_decl (decl, 9268 initializer, !non_constant_p, 9269 asm_specification, 9270 flags); 9271 9272 if (pushed_scope) 9273 pop_scope (pushed_scope); 9274 9275 return convert_from_reference (decl); 9276 } 9277 } 9278 /* If we didn't even get past the declarator successfully, we are 9279 definitely not looking at a declaration. */ 9280 else 9281 cp_parser_abort_tentative_parse (parser); 9282 9283 /* Otherwise, we are looking at an expression. */ 9284 return cp_parser_expression (parser, /*cast_p=*/false, NULL); 9285 } 9286 9287 /* Parses a for-statement or range-for-statement until the closing ')', 9288 not included. */ 9289 9290 static tree 9291 cp_parser_for (cp_parser *parser) 9292 { 9293 tree init, scope, decl; 9294 bool is_range_for; 9295 9296 /* Begin the for-statement. */ 9297 scope = begin_for_scope (&init); 9298 9299 /* Parse the initialization. */ 9300 is_range_for = cp_parser_for_init_statement (parser, &decl); 9301 9302 if (is_range_for) 9303 return cp_parser_range_for (parser, scope, init, decl); 9304 else 9305 return cp_parser_c_for (parser, scope, init); 9306 } 9307 9308 static tree 9309 cp_parser_c_for (cp_parser *parser, tree scope, tree init) 9310 { 9311 /* Normal for loop */ 9312 tree condition = NULL_TREE; 9313 tree expression = NULL_TREE; 9314 tree stmt; 9315 9316 stmt = begin_for_stmt (scope, init); 9317 /* The for-init-statement has already been parsed in 9318 cp_parser_for_init_statement, so no work is needed here. */ 9319 finish_for_init_stmt (stmt); 9320 9321 /* If there's a condition, process it. */ 9322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 9323 condition = cp_parser_condition (parser); 9324 finish_for_cond (condition, stmt); 9325 /* Look for the `;'. */ 9326 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9327 9328 /* If there's an expression, process it. */ 9329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) 9330 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); 9331 finish_for_expr (expression, stmt); 9332 9333 return stmt; 9334 } 9335 9336 /* Tries to parse a range-based for-statement: 9337 9338 range-based-for: 9339 decl-specifier-seq declarator : expression 9340 9341 The decl-specifier-seq declarator and the `:' are already parsed by 9342 cp_parser_for_init_statement. If processing_template_decl it returns a 9343 newly created RANGE_FOR_STMT; if not, it is converted to a 9344 regular FOR_STMT. */ 9345 9346 static tree 9347 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl) 9348 { 9349 tree stmt, range_expr; 9350 9351 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 9352 { 9353 bool expr_non_constant_p; 9354 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p); 9355 } 9356 else 9357 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 9358 9359 /* If in template, STMT is converted to a normal for-statement 9360 at instantiation. If not, it is done just ahead. */ 9361 if (processing_template_decl) 9362 { 9363 if (check_for_bare_parameter_packs (range_expr)) 9364 range_expr = error_mark_node; 9365 stmt = begin_range_for_stmt (scope, init); 9366 finish_range_for_decl (stmt, range_decl, range_expr); 9367 if (range_expr != error_mark_node 9368 && !type_dependent_expression_p (range_expr) 9369 /* The length of an array might be dependent. */ 9370 && COMPLETE_TYPE_P (TREE_TYPE (range_expr)) 9371 /* do_auto_deduction doesn't mess with template init-lists. */ 9372 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr)) 9373 do_range_for_auto_deduction (range_decl, range_expr); 9374 } 9375 else 9376 { 9377 stmt = begin_for_stmt (scope, init); 9378 stmt = cp_convert_range_for (stmt, range_decl, range_expr); 9379 } 9380 return stmt; 9381 } 9382 9383 /* Subroutine of cp_convert_range_for: given the initializer expression, 9384 builds up the range temporary. */ 9385 9386 static tree 9387 build_range_temp (tree range_expr) 9388 { 9389 tree range_type, range_temp; 9390 9391 /* Find out the type deduced by the declaration 9392 `auto &&__range = range_expr'. */ 9393 range_type = cp_build_reference_type (make_auto (), true); 9394 range_type = do_auto_deduction (range_type, range_expr, 9395 type_uses_auto (range_type)); 9396 9397 /* Create the __range variable. */ 9398 range_temp = build_decl (input_location, VAR_DECL, 9399 get_identifier ("__for_range"), range_type); 9400 TREE_USED (range_temp) = 1; 9401 DECL_ARTIFICIAL (range_temp) = 1; 9402 9403 return range_temp; 9404 } 9405 9406 /* Used by cp_parser_range_for in template context: we aren't going to 9407 do a full conversion yet, but we still need to resolve auto in the 9408 type of the for-range-declaration if present. This is basically 9409 a shortcut version of cp_convert_range_for. */ 9410 9411 static void 9412 do_range_for_auto_deduction (tree decl, tree range_expr) 9413 { 9414 tree auto_node = type_uses_auto (TREE_TYPE (decl)); 9415 if (auto_node) 9416 { 9417 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl; 9418 range_temp = convert_from_reference (build_range_temp (range_expr)); 9419 iter_type = (cp_parser_perform_range_for_lookup 9420 (range_temp, &begin_dummy, &end_dummy)); 9421 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type); 9422 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL, 9423 tf_warning_or_error); 9424 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl), 9425 iter_decl, auto_node); 9426 } 9427 } 9428 9429 /* Converts a range-based for-statement into a normal 9430 for-statement, as per the definition. 9431 9432 for (RANGE_DECL : RANGE_EXPR) 9433 BLOCK 9434 9435 should be equivalent to: 9436 9437 { 9438 auto &&__range = RANGE_EXPR; 9439 for (auto __begin = BEGIN_EXPR, end = END_EXPR; 9440 __begin != __end; 9441 ++__begin) 9442 { 9443 RANGE_DECL = *__begin; 9444 BLOCK 9445 } 9446 } 9447 9448 If RANGE_EXPR is an array: 9449 BEGIN_EXPR = __range 9450 END_EXPR = __range + ARRAY_SIZE(__range) 9451 Else if RANGE_EXPR has a member 'begin' or 'end': 9452 BEGIN_EXPR = __range.begin() 9453 END_EXPR = __range.end() 9454 Else: 9455 BEGIN_EXPR = begin(__range) 9456 END_EXPR = end(__range); 9457 9458 If __range has a member 'begin' but not 'end', or vice versa, we must 9459 still use the second alternative (it will surely fail, however). 9460 When calling begin()/end() in the third alternative we must use 9461 argument dependent lookup, but always considering 'std' as an associated 9462 namespace. */ 9463 9464 tree 9465 cp_convert_range_for (tree statement, tree range_decl, tree range_expr) 9466 { 9467 tree begin, end; 9468 tree iter_type, begin_expr, end_expr; 9469 tree condition, expression; 9470 9471 if (range_decl == error_mark_node || range_expr == error_mark_node) 9472 /* If an error happened previously do nothing or else a lot of 9473 unhelpful errors would be issued. */ 9474 begin_expr = end_expr = iter_type = error_mark_node; 9475 else 9476 { 9477 tree range_temp = build_range_temp (range_expr); 9478 pushdecl (range_temp); 9479 cp_finish_decl (range_temp, range_expr, 9480 /*is_constant_init*/false, NULL_TREE, 9481 LOOKUP_ONLYCONVERTING); 9482 9483 range_temp = convert_from_reference (range_temp); 9484 iter_type = cp_parser_perform_range_for_lookup (range_temp, 9485 &begin_expr, &end_expr); 9486 } 9487 9488 /* The new for initialization statement. */ 9489 begin = build_decl (input_location, VAR_DECL, 9490 get_identifier ("__for_begin"), iter_type); 9491 TREE_USED (begin) = 1; 9492 DECL_ARTIFICIAL (begin) = 1; 9493 pushdecl (begin); 9494 cp_finish_decl (begin, begin_expr, 9495 /*is_constant_init*/false, NULL_TREE, 9496 LOOKUP_ONLYCONVERTING); 9497 9498 end = build_decl (input_location, VAR_DECL, 9499 get_identifier ("__for_end"), iter_type); 9500 TREE_USED (end) = 1; 9501 DECL_ARTIFICIAL (end) = 1; 9502 pushdecl (end); 9503 cp_finish_decl (end, end_expr, 9504 /*is_constant_init*/false, NULL_TREE, 9505 LOOKUP_ONLYCONVERTING); 9506 9507 finish_for_init_stmt (statement); 9508 9509 /* The new for condition. */ 9510 condition = build_x_binary_op (NE_EXPR, 9511 begin, ERROR_MARK, 9512 end, ERROR_MARK, 9513 NULL, tf_warning_or_error); 9514 finish_for_cond (condition, statement); 9515 9516 /* The new increment expression. */ 9517 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin); 9518 finish_for_expr (expression, statement); 9519 9520 /* The declaration is initialized with *__begin inside the loop body. */ 9521 cp_finish_decl (range_decl, 9522 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error), 9523 /*is_constant_init*/false, NULL_TREE, 9524 LOOKUP_ONLYCONVERTING); 9525 9526 return statement; 9527 } 9528 9529 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for. 9530 We need to solve both at the same time because the method used 9531 depends on the existence of members begin or end. 9532 Returns the type deduced for the iterator expression. */ 9533 9534 static tree 9535 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end) 9536 { 9537 if (error_operand_p (range)) 9538 { 9539 *begin = *end = error_mark_node; 9540 return error_mark_node; 9541 } 9542 9543 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range)))) 9544 { 9545 error ("range-based %<for%> expression of type %qT " 9546 "has incomplete type", TREE_TYPE (range)); 9547 *begin = *end = error_mark_node; 9548 return error_mark_node; 9549 } 9550 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE) 9551 { 9552 /* If RANGE is an array, we will use pointer arithmetic. */ 9553 *begin = range; 9554 *end = build_binary_op (input_location, PLUS_EXPR, 9555 range, 9556 array_type_nelts_top (TREE_TYPE (range)), 9557 0); 9558 return build_pointer_type (TREE_TYPE (TREE_TYPE (range))); 9559 } 9560 else 9561 { 9562 /* If it is not an array, we must do a bit of magic. */ 9563 tree id_begin, id_end; 9564 tree member_begin, member_end; 9565 9566 *begin = *end = error_mark_node; 9567 9568 id_begin = get_identifier ("begin"); 9569 id_end = get_identifier ("end"); 9570 member_begin = lookup_member (TREE_TYPE (range), id_begin, 9571 /*protect=*/2, /*want_type=*/false, 9572 tf_warning_or_error); 9573 member_end = lookup_member (TREE_TYPE (range), id_end, 9574 /*protect=*/2, /*want_type=*/false, 9575 tf_warning_or_error); 9576 9577 if (member_begin != NULL_TREE || member_end != NULL_TREE) 9578 { 9579 /* Use the member functions. */ 9580 if (member_begin != NULL_TREE) 9581 *begin = cp_parser_range_for_member_function (range, id_begin); 9582 else 9583 error ("range-based %<for%> expression of type %qT has an " 9584 "%<end%> member but not a %<begin%>", TREE_TYPE (range)); 9585 9586 if (member_end != NULL_TREE) 9587 *end = cp_parser_range_for_member_function (range, id_end); 9588 else 9589 error ("range-based %<for%> expression of type %qT has a " 9590 "%<begin%> member but not an %<end%>", TREE_TYPE (range)); 9591 } 9592 else 9593 { 9594 /* Use global functions with ADL. */ 9595 VEC(tree,gc) *vec; 9596 vec = make_tree_vector (); 9597 9598 VEC_safe_push (tree, gc, vec, range); 9599 9600 member_begin = perform_koenig_lookup (id_begin, vec, 9601 /*include_std=*/true, 9602 tf_warning_or_error); 9603 *begin = finish_call_expr (member_begin, &vec, false, true, 9604 tf_warning_or_error); 9605 member_end = perform_koenig_lookup (id_end, vec, 9606 /*include_std=*/true, 9607 tf_warning_or_error); 9608 *end = finish_call_expr (member_end, &vec, false, true, 9609 tf_warning_or_error); 9610 9611 release_tree_vector (vec); 9612 } 9613 9614 /* Last common checks. */ 9615 if (*begin == error_mark_node || *end == error_mark_node) 9616 { 9617 /* If one of the expressions is an error do no more checks. */ 9618 *begin = *end = error_mark_node; 9619 return error_mark_node; 9620 } 9621 else 9622 { 9623 tree iter_type = cv_unqualified (TREE_TYPE (*begin)); 9624 /* The unqualified type of the __begin and __end temporaries should 9625 be the same, as required by the multiple auto declaration. */ 9626 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end)))) 9627 error ("inconsistent begin/end types in range-based %<for%> " 9628 "statement: %qT and %qT", 9629 TREE_TYPE (*begin), TREE_TYPE (*end)); 9630 return iter_type; 9631 } 9632 } 9633 } 9634 9635 /* Helper function for cp_parser_perform_range_for_lookup. 9636 Builds a tree for RANGE.IDENTIFIER(). */ 9637 9638 static tree 9639 cp_parser_range_for_member_function (tree range, tree identifier) 9640 { 9641 tree member, res; 9642 VEC(tree,gc) *vec; 9643 9644 member = finish_class_member_access_expr (range, identifier, 9645 false, tf_warning_or_error); 9646 if (member == error_mark_node) 9647 return error_mark_node; 9648 9649 vec = make_tree_vector (); 9650 res = finish_call_expr (member, &vec, 9651 /*disallow_virtual=*/false, 9652 /*koenig_p=*/false, 9653 tf_warning_or_error); 9654 release_tree_vector (vec); 9655 return res; 9656 } 9657 9658 /* Parse an iteration-statement. 9659 9660 iteration-statement: 9661 while ( condition ) statement 9662 do statement while ( expression ) ; 9663 for ( for-init-statement condition [opt] ; expression [opt] ) 9664 statement 9665 9666 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */ 9667 9668 static tree 9669 cp_parser_iteration_statement (cp_parser* parser) 9670 { 9671 cp_token *token; 9672 enum rid keyword; 9673 tree statement; 9674 unsigned char in_statement; 9675 9676 /* Peek at the next token. */ 9677 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION); 9678 if (!token) 9679 return error_mark_node; 9680 9681 /* Remember whether or not we are already within an iteration 9682 statement. */ 9683 in_statement = parser->in_statement; 9684 9685 /* See what kind of keyword it is. */ 9686 keyword = token->keyword; 9687 switch (keyword) 9688 { 9689 case RID_WHILE: 9690 { 9691 tree condition; 9692 9693 /* Begin the while-statement. */ 9694 statement = begin_while_stmt (); 9695 /* Look for the `('. */ 9696 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 9697 /* Parse the condition. */ 9698 condition = cp_parser_condition (parser); 9699 finish_while_stmt_cond (condition, statement); 9700 /* Look for the `)'. */ 9701 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 9702 /* Parse the dependent statement. */ 9703 parser->in_statement = IN_ITERATION_STMT; 9704 cp_parser_already_scoped_statement (parser); 9705 parser->in_statement = in_statement; 9706 /* We're done with the while-statement. */ 9707 finish_while_stmt (statement); 9708 } 9709 break; 9710 9711 case RID_DO: 9712 { 9713 tree expression; 9714 9715 /* Begin the do-statement. */ 9716 statement = begin_do_stmt (); 9717 /* Parse the body of the do-statement. */ 9718 parser->in_statement = IN_ITERATION_STMT; 9719 cp_parser_implicitly_scoped_statement (parser, NULL); 9720 parser->in_statement = in_statement; 9721 finish_do_body (statement); 9722 /* Look for the `while' keyword. */ 9723 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE); 9724 /* Look for the `('. */ 9725 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 9726 /* Parse the expression. */ 9727 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); 9728 /* We're done with the do-statement. */ 9729 finish_do_stmt (expression, statement); 9730 /* Look for the `)'. */ 9731 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 9732 /* Look for the `;'. */ 9733 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9734 } 9735 break; 9736 9737 case RID_FOR: 9738 { 9739 /* Look for the `('. */ 9740 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 9741 9742 statement = cp_parser_for (parser); 9743 9744 /* Look for the `)'. */ 9745 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 9746 9747 /* Parse the body of the for-statement. */ 9748 parser->in_statement = IN_ITERATION_STMT; 9749 cp_parser_already_scoped_statement (parser); 9750 parser->in_statement = in_statement; 9751 9752 /* We're done with the for-statement. */ 9753 finish_for_stmt (statement); 9754 } 9755 break; 9756 9757 default: 9758 cp_parser_error (parser, "expected iteration-statement"); 9759 statement = error_mark_node; 9760 break; 9761 } 9762 9763 return statement; 9764 } 9765 9766 /* Parse a for-init-statement or the declarator of a range-based-for. 9767 Returns true if a range-based-for declaration is seen. 9768 9769 for-init-statement: 9770 expression-statement 9771 simple-declaration */ 9772 9773 static bool 9774 cp_parser_for_init_statement (cp_parser* parser, tree *decl) 9775 { 9776 /* If the next token is a `;', then we have an empty 9777 expression-statement. Grammatically, this is also a 9778 simple-declaration, but an invalid one, because it does not 9779 declare anything. Therefore, if we did not handle this case 9780 specially, we would issue an error message about an invalid 9781 declaration. */ 9782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 9783 { 9784 bool is_range_for = false; 9785 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 9786 9787 parser->colon_corrects_to_scope_p = false; 9788 9789 /* We're going to speculatively look for a declaration, falling back 9790 to an expression, if necessary. */ 9791 cp_parser_parse_tentatively (parser); 9792 /* Parse the declaration. */ 9793 cp_parser_simple_declaration (parser, 9794 /*function_definition_allowed_p=*/false, 9795 decl); 9796 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 9797 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 9798 { 9799 /* It is a range-for, consume the ':' */ 9800 cp_lexer_consume_token (parser->lexer); 9801 is_range_for = true; 9802 if (cxx_dialect < cxx0x) 9803 { 9804 error_at (cp_lexer_peek_token (parser->lexer)->location, 9805 "range-based %<for%> loops are not allowed " 9806 "in C++98 mode"); 9807 *decl = error_mark_node; 9808 } 9809 } 9810 else 9811 /* The ';' is not consumed yet because we told 9812 cp_parser_simple_declaration not to. */ 9813 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9814 9815 if (cp_parser_parse_definitely (parser)) 9816 return is_range_for; 9817 /* If the tentative parse failed, then we shall need to look for an 9818 expression-statement. */ 9819 } 9820 /* If we are here, it is an expression-statement. */ 9821 cp_parser_expression_statement (parser, NULL_TREE); 9822 return false; 9823 } 9824 9825 /* Parse a jump-statement. 9826 9827 jump-statement: 9828 break ; 9829 continue ; 9830 return expression [opt] ; 9831 return braced-init-list ; 9832 goto identifier ; 9833 9834 GNU extension: 9835 9836 jump-statement: 9837 goto * expression ; 9838 9839 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */ 9840 9841 static tree 9842 cp_parser_jump_statement (cp_parser* parser) 9843 { 9844 tree statement = error_mark_node; 9845 cp_token *token; 9846 enum rid keyword; 9847 unsigned char in_statement; 9848 9849 /* Peek at the next token. */ 9850 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP); 9851 if (!token) 9852 return error_mark_node; 9853 9854 /* See what kind of keyword it is. */ 9855 keyword = token->keyword; 9856 switch (keyword) 9857 { 9858 case RID_BREAK: 9859 in_statement = parser->in_statement & ~IN_IF_STMT; 9860 switch (in_statement) 9861 { 9862 case 0: 9863 error_at (token->location, "break statement not within loop or switch"); 9864 break; 9865 default: 9866 gcc_assert ((in_statement & IN_SWITCH_STMT) 9867 || in_statement == IN_ITERATION_STMT); 9868 statement = finish_break_stmt (); 9869 break; 9870 case IN_OMP_BLOCK: 9871 error_at (token->location, "invalid exit from OpenMP structured block"); 9872 break; 9873 case IN_OMP_FOR: 9874 error_at (token->location, "break statement used with OpenMP for loop"); 9875 break; 9876 } 9877 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9878 break; 9879 9880 case RID_CONTINUE: 9881 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT)) 9882 { 9883 case 0: 9884 error_at (token->location, "continue statement not within a loop"); 9885 break; 9886 case IN_ITERATION_STMT: 9887 case IN_OMP_FOR: 9888 statement = finish_continue_stmt (); 9889 break; 9890 case IN_OMP_BLOCK: 9891 error_at (token->location, "invalid exit from OpenMP structured block"); 9892 break; 9893 default: 9894 gcc_unreachable (); 9895 } 9896 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9897 break; 9898 9899 case RID_RETURN: 9900 { 9901 tree expr; 9902 bool expr_non_constant_p; 9903 9904 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 9905 { 9906 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 9907 expr = cp_parser_braced_list (parser, &expr_non_constant_p); 9908 } 9909 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 9910 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 9911 else 9912 /* If the next token is a `;', then there is no 9913 expression. */ 9914 expr = NULL_TREE; 9915 /* Build the return-statement. */ 9916 statement = finish_return_stmt (expr); 9917 /* Look for the final `;'. */ 9918 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9919 } 9920 break; 9921 9922 case RID_GOTO: 9923 /* Create the goto-statement. */ 9924 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)) 9925 { 9926 /* Issue a warning about this use of a GNU extension. */ 9927 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos"); 9928 /* Consume the '*' token. */ 9929 cp_lexer_consume_token (parser->lexer); 9930 /* Parse the dependent expression. */ 9931 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL)); 9932 } 9933 else 9934 finish_goto_stmt (cp_parser_identifier (parser)); 9935 /* Look for the final `;'. */ 9936 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 9937 break; 9938 9939 default: 9940 cp_parser_error (parser, "expected jump-statement"); 9941 break; 9942 } 9943 9944 return statement; 9945 } 9946 9947 /* Parse a declaration-statement. 9948 9949 declaration-statement: 9950 block-declaration */ 9951 9952 static void 9953 cp_parser_declaration_statement (cp_parser* parser) 9954 { 9955 void *p; 9956 9957 /* Get the high-water mark for the DECLARATOR_OBSTACK. */ 9958 p = obstack_alloc (&declarator_obstack, 0); 9959 9960 /* Parse the block-declaration. */ 9961 cp_parser_block_declaration (parser, /*statement_p=*/true); 9962 9963 /* Free any declarators allocated. */ 9964 obstack_free (&declarator_obstack, p); 9965 9966 /* Finish off the statement. */ 9967 finish_stmt (); 9968 } 9969 9970 /* Some dependent statements (like `if (cond) statement'), are 9971 implicitly in their own scope. In other words, if the statement is 9972 a single statement (as opposed to a compound-statement), it is 9973 none-the-less treated as if it were enclosed in braces. Any 9974 declarations appearing in the dependent statement are out of scope 9975 after control passes that point. This function parses a statement, 9976 but ensures that is in its own scope, even if it is not a 9977 compound-statement. 9978 9979 If IF_P is not NULL, *IF_P is set to indicate whether the statement 9980 is a (possibly labeled) if statement which is not enclosed in 9981 braces and has an else clause. This is used to implement 9982 -Wparentheses. 9983 9984 Returns the new statement. */ 9985 9986 static tree 9987 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p) 9988 { 9989 tree statement; 9990 9991 if (if_p != NULL) 9992 *if_p = false; 9993 9994 /* Mark if () ; with a special NOP_EXPR. */ 9995 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 9996 { 9997 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 9998 cp_lexer_consume_token (parser->lexer); 9999 statement = add_stmt (build_empty_stmt (loc)); 10000 } 10001 /* if a compound is opened, we simply parse the statement directly. */ 10002 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 10003 statement = cp_parser_compound_statement (parser, NULL, false, false); 10004 /* If the token is not a `{', then we must take special action. */ 10005 else 10006 { 10007 /* Create a compound-statement. */ 10008 statement = begin_compound_stmt (0); 10009 /* Parse the dependent-statement. */ 10010 cp_parser_statement (parser, NULL_TREE, false, if_p); 10011 /* Finish the dummy compound-statement. */ 10012 finish_compound_stmt (statement); 10013 } 10014 10015 /* Return the statement. */ 10016 return statement; 10017 } 10018 10019 /* For some dependent statements (like `while (cond) statement'), we 10020 have already created a scope. Therefore, even if the dependent 10021 statement is a compound-statement, we do not want to create another 10022 scope. */ 10023 10024 static void 10025 cp_parser_already_scoped_statement (cp_parser* parser) 10026 { 10027 /* If the token is a `{', then we must take special action. */ 10028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) 10029 cp_parser_statement (parser, NULL_TREE, false, NULL); 10030 else 10031 { 10032 /* Avoid calling cp_parser_compound_statement, so that we 10033 don't create a new scope. Do everything else by hand. */ 10034 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE); 10035 /* If the next keyword is `__label__' we have a label declaration. */ 10036 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL)) 10037 cp_parser_label_declaration (parser); 10038 /* Parse an (optional) statement-seq. */ 10039 cp_parser_statement_seq_opt (parser, NULL_TREE); 10040 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 10041 } 10042 } 10043 10044 /* Declarations [gram.dcl.dcl] */ 10045 10046 /* Parse an optional declaration-sequence. 10047 10048 declaration-seq: 10049 declaration 10050 declaration-seq declaration */ 10051 10052 static void 10053 cp_parser_declaration_seq_opt (cp_parser* parser) 10054 { 10055 while (true) 10056 { 10057 cp_token *token; 10058 10059 token = cp_lexer_peek_token (parser->lexer); 10060 10061 if (token->type == CPP_CLOSE_BRACE 10062 || token->type == CPP_EOF 10063 || token->type == CPP_PRAGMA_EOL) 10064 break; 10065 10066 if (token->type == CPP_SEMICOLON) 10067 { 10068 /* A declaration consisting of a single semicolon is 10069 invalid. Allow it unless we're being pedantic. */ 10070 cp_lexer_consume_token (parser->lexer); 10071 if (!in_system_header) 10072 pedwarn (input_location, OPT_pedantic, "extra %<;%>"); 10073 continue; 10074 } 10075 10076 /* If we're entering or exiting a region that's implicitly 10077 extern "C", modify the lang context appropriately. */ 10078 if (!parser->implicit_extern_c && token->implicit_extern_c) 10079 { 10080 push_lang_context (lang_name_c); 10081 parser->implicit_extern_c = true; 10082 } 10083 else if (parser->implicit_extern_c && !token->implicit_extern_c) 10084 { 10085 pop_lang_context (); 10086 parser->implicit_extern_c = false; 10087 } 10088 10089 if (token->type == CPP_PRAGMA) 10090 { 10091 /* A top-level declaration can consist solely of a #pragma. 10092 A nested declaration cannot, so this is done here and not 10093 in cp_parser_declaration. (A #pragma at block scope is 10094 handled in cp_parser_statement.) */ 10095 cp_parser_pragma (parser, pragma_external); 10096 continue; 10097 } 10098 10099 /* Parse the declaration itself. */ 10100 cp_parser_declaration (parser); 10101 } 10102 } 10103 10104 /* Parse a declaration. 10105 10106 declaration: 10107 block-declaration 10108 function-definition 10109 template-declaration 10110 explicit-instantiation 10111 explicit-specialization 10112 linkage-specification 10113 namespace-definition 10114 10115 GNU extension: 10116 10117 declaration: 10118 __extension__ declaration */ 10119 10120 static void 10121 cp_parser_declaration (cp_parser* parser) 10122 { 10123 cp_token token1; 10124 cp_token token2; 10125 int saved_pedantic; 10126 void *p; 10127 tree attributes = NULL_TREE; 10128 10129 /* Check for the `__extension__' keyword. */ 10130 if (cp_parser_extension_opt (parser, &saved_pedantic)) 10131 { 10132 /* Parse the qualified declaration. */ 10133 cp_parser_declaration (parser); 10134 /* Restore the PEDANTIC flag. */ 10135 pedantic = saved_pedantic; 10136 10137 return; 10138 } 10139 10140 /* Try to figure out what kind of declaration is present. */ 10141 token1 = *cp_lexer_peek_token (parser->lexer); 10142 10143 if (token1.type != CPP_EOF) 10144 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2); 10145 else 10146 { 10147 token2.type = CPP_EOF; 10148 token2.keyword = RID_MAX; 10149 } 10150 10151 /* Get the high-water mark for the DECLARATOR_OBSTACK. */ 10152 p = obstack_alloc (&declarator_obstack, 0); 10153 10154 /* If the next token is `extern' and the following token is a string 10155 literal, then we have a linkage specification. */ 10156 if (token1.keyword == RID_EXTERN 10157 && cp_parser_is_pure_string_literal (&token2)) 10158 cp_parser_linkage_specification (parser); 10159 /* If the next token is `template', then we have either a template 10160 declaration, an explicit instantiation, or an explicit 10161 specialization. */ 10162 else if (token1.keyword == RID_TEMPLATE) 10163 { 10164 /* `template <>' indicates a template specialization. */ 10165 if (token2.type == CPP_LESS 10166 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) 10167 cp_parser_explicit_specialization (parser); 10168 /* `template <' indicates a template declaration. */ 10169 else if (token2.type == CPP_LESS) 10170 cp_parser_template_declaration (parser, /*member_p=*/false); 10171 /* Anything else must be an explicit instantiation. */ 10172 else 10173 cp_parser_explicit_instantiation (parser); 10174 } 10175 /* If the next token is `export', then we have a template 10176 declaration. */ 10177 else if (token1.keyword == RID_EXPORT) 10178 cp_parser_template_declaration (parser, /*member_p=*/false); 10179 /* If the next token is `extern', 'static' or 'inline' and the one 10180 after that is `template', we have a GNU extended explicit 10181 instantiation directive. */ 10182 else if (cp_parser_allow_gnu_extensions_p (parser) 10183 && (token1.keyword == RID_EXTERN 10184 || token1.keyword == RID_STATIC 10185 || token1.keyword == RID_INLINE) 10186 && token2.keyword == RID_TEMPLATE) 10187 cp_parser_explicit_instantiation (parser); 10188 /* If the next token is `namespace', check for a named or unnamed 10189 namespace definition. */ 10190 else if (token1.keyword == RID_NAMESPACE 10191 && (/* A named namespace definition. */ 10192 (token2.type == CPP_NAME 10193 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type 10194 != CPP_EQ)) 10195 /* An unnamed namespace definition. */ 10196 || token2.type == CPP_OPEN_BRACE 10197 || token2.keyword == RID_ATTRIBUTE)) 10198 cp_parser_namespace_definition (parser); 10199 /* An inline (associated) namespace definition. */ 10200 else if (token1.keyword == RID_INLINE 10201 && token2.keyword == RID_NAMESPACE) 10202 cp_parser_namespace_definition (parser); 10203 /* Objective-C++ declaration/definition. */ 10204 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword)) 10205 cp_parser_objc_declaration (parser, NULL_TREE); 10206 else if (c_dialect_objc () 10207 && token1.keyword == RID_ATTRIBUTE 10208 && cp_parser_objc_valid_prefix_attributes (parser, &attributes)) 10209 cp_parser_objc_declaration (parser, attributes); 10210 /* We must have either a block declaration or a function 10211 definition. */ 10212 else 10213 /* Try to parse a block-declaration, or a function-definition. */ 10214 cp_parser_block_declaration (parser, /*statement_p=*/false); 10215 10216 /* Free any declarators allocated. */ 10217 obstack_free (&declarator_obstack, p); 10218 } 10219 10220 /* Parse a block-declaration. 10221 10222 block-declaration: 10223 simple-declaration 10224 asm-definition 10225 namespace-alias-definition 10226 using-declaration 10227 using-directive 10228 10229 GNU Extension: 10230 10231 block-declaration: 10232 __extension__ block-declaration 10233 10234 C++0x Extension: 10235 10236 block-declaration: 10237 static_assert-declaration 10238 10239 If STATEMENT_P is TRUE, then this block-declaration is occurring as 10240 part of a declaration-statement. */ 10241 10242 static void 10243 cp_parser_block_declaration (cp_parser *parser, 10244 bool statement_p) 10245 { 10246 cp_token *token1; 10247 int saved_pedantic; 10248 10249 /* Check for the `__extension__' keyword. */ 10250 if (cp_parser_extension_opt (parser, &saved_pedantic)) 10251 { 10252 /* Parse the qualified declaration. */ 10253 cp_parser_block_declaration (parser, statement_p); 10254 /* Restore the PEDANTIC flag. */ 10255 pedantic = saved_pedantic; 10256 10257 return; 10258 } 10259 10260 /* Peek at the next token to figure out which kind of declaration is 10261 present. */ 10262 token1 = cp_lexer_peek_token (parser->lexer); 10263 10264 /* If the next keyword is `asm', we have an asm-definition. */ 10265 if (token1->keyword == RID_ASM) 10266 { 10267 if (statement_p) 10268 cp_parser_commit_to_tentative_parse (parser); 10269 cp_parser_asm_definition (parser); 10270 } 10271 /* If the next keyword is `namespace', we have a 10272 namespace-alias-definition. */ 10273 else if (token1->keyword == RID_NAMESPACE) 10274 cp_parser_namespace_alias_definition (parser); 10275 /* If the next keyword is `using', we have a 10276 using-declaration, a using-directive, or an alias-declaration. */ 10277 else if (token1->keyword == RID_USING) 10278 { 10279 cp_token *token2; 10280 10281 if (statement_p) 10282 cp_parser_commit_to_tentative_parse (parser); 10283 /* If the token after `using' is `namespace', then we have a 10284 using-directive. */ 10285 token2 = cp_lexer_peek_nth_token (parser->lexer, 2); 10286 if (token2->keyword == RID_NAMESPACE) 10287 cp_parser_using_directive (parser); 10288 /* If the second token after 'using' is '=', then we have an 10289 alias-declaration. */ 10290 else if (cxx_dialect >= cxx0x 10291 && token2->type == CPP_NAME 10292 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ) 10293 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword 10294 == RID_ATTRIBUTE))) 10295 cp_parser_alias_declaration (parser); 10296 /* Otherwise, it's a using-declaration. */ 10297 else 10298 cp_parser_using_declaration (parser, 10299 /*access_declaration_p=*/false); 10300 } 10301 /* If the next keyword is `__label__' we have a misplaced label 10302 declaration. */ 10303 else if (token1->keyword == RID_LABEL) 10304 { 10305 cp_lexer_consume_token (parser->lexer); 10306 error_at (token1->location, "%<__label__%> not at the beginning of a block"); 10307 cp_parser_skip_to_end_of_statement (parser); 10308 /* If the next token is now a `;', consume it. */ 10309 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 10310 cp_lexer_consume_token (parser->lexer); 10311 } 10312 /* If the next token is `static_assert' we have a static assertion. */ 10313 else if (token1->keyword == RID_STATIC_ASSERT) 10314 cp_parser_static_assert (parser, /*member_p=*/false); 10315 /* Anything else must be a simple-declaration. */ 10316 else 10317 cp_parser_simple_declaration (parser, !statement_p, 10318 /*maybe_range_for_decl*/NULL); 10319 } 10320 10321 /* Parse a simple-declaration. 10322 10323 simple-declaration: 10324 decl-specifier-seq [opt] init-declarator-list [opt] ; 10325 10326 init-declarator-list: 10327 init-declarator 10328 init-declarator-list , init-declarator 10329 10330 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a 10331 function-definition as a simple-declaration. 10332 10333 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the 10334 parsed declaration if it is an uninitialized single declarator not followed 10335 by a `;', or to error_mark_node otherwise. Either way, the trailing `;', 10336 if present, will not be consumed. */ 10337 10338 static void 10339 cp_parser_simple_declaration (cp_parser* parser, 10340 bool function_definition_allowed_p, 10341 tree *maybe_range_for_decl) 10342 { 10343 cp_decl_specifier_seq decl_specifiers; 10344 int declares_class_or_enum; 10345 bool saw_declarator; 10346 10347 if (maybe_range_for_decl) 10348 *maybe_range_for_decl = NULL_TREE; 10349 10350 /* Defer access checks until we know what is being declared; the 10351 checks for names appearing in the decl-specifier-seq should be 10352 done as if we were in the scope of the thing being declared. */ 10353 push_deferring_access_checks (dk_deferred); 10354 10355 /* Parse the decl-specifier-seq. We have to keep track of whether 10356 or not the decl-specifier-seq declares a named class or 10357 enumeration type, since that is the only case in which the 10358 init-declarator-list is allowed to be empty. 10359 10360 [dcl.dcl] 10361 10362 In a simple-declaration, the optional init-declarator-list can be 10363 omitted only when declaring a class or enumeration, that is when 10364 the decl-specifier-seq contains either a class-specifier, an 10365 elaborated-type-specifier, or an enum-specifier. */ 10366 cp_parser_decl_specifier_seq (parser, 10367 CP_PARSER_FLAGS_OPTIONAL, 10368 &decl_specifiers, 10369 &declares_class_or_enum); 10370 /* We no longer need to defer access checks. */ 10371 stop_deferring_access_checks (); 10372 10373 /* In a block scope, a valid declaration must always have a 10374 decl-specifier-seq. By not trying to parse declarators, we can 10375 resolve the declaration/expression ambiguity more quickly. */ 10376 if (!function_definition_allowed_p 10377 && !decl_specifiers.any_specifiers_p) 10378 { 10379 cp_parser_error (parser, "expected declaration"); 10380 goto done; 10381 } 10382 10383 /* If the next two tokens are both identifiers, the code is 10384 erroneous. The usual cause of this situation is code like: 10385 10386 T t; 10387 10388 where "T" should name a type -- but does not. */ 10389 if (!decl_specifiers.any_type_specifiers_p 10390 && cp_parser_parse_and_diagnose_invalid_type_name (parser)) 10391 { 10392 /* If parsing tentatively, we should commit; we really are 10393 looking at a declaration. */ 10394 cp_parser_commit_to_tentative_parse (parser); 10395 /* Give up. */ 10396 goto done; 10397 } 10398 10399 /* If we have seen at least one decl-specifier, and the next token 10400 is not a parenthesis, then we must be looking at a declaration. 10401 (After "int (" we might be looking at a functional cast.) */ 10402 if (decl_specifiers.any_specifiers_p 10403 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN) 10404 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) 10405 && !cp_parser_error_occurred (parser)) 10406 cp_parser_commit_to_tentative_parse (parser); 10407 10408 /* Keep going until we hit the `;' at the end of the simple 10409 declaration. */ 10410 saw_declarator = false; 10411 while (cp_lexer_next_token_is_not (parser->lexer, 10412 CPP_SEMICOLON)) 10413 { 10414 cp_token *token; 10415 bool function_definition_p; 10416 tree decl; 10417 10418 if (saw_declarator) 10419 { 10420 /* If we are processing next declarator, coma is expected */ 10421 token = cp_lexer_peek_token (parser->lexer); 10422 gcc_assert (token->type == CPP_COMMA); 10423 cp_lexer_consume_token (parser->lexer); 10424 if (maybe_range_for_decl) 10425 *maybe_range_for_decl = error_mark_node; 10426 } 10427 else 10428 saw_declarator = true; 10429 10430 /* Parse the init-declarator. */ 10431 decl = cp_parser_init_declarator (parser, &decl_specifiers, 10432 /*checks=*/NULL, 10433 function_definition_allowed_p, 10434 /*member_p=*/false, 10435 declares_class_or_enum, 10436 &function_definition_p, 10437 maybe_range_for_decl); 10438 /* If an error occurred while parsing tentatively, exit quickly. 10439 (That usually happens when in the body of a function; each 10440 statement is treated as a declaration-statement until proven 10441 otherwise.) */ 10442 if (cp_parser_error_occurred (parser)) 10443 goto done; 10444 /* Handle function definitions specially. */ 10445 if (function_definition_p) 10446 { 10447 /* If the next token is a `,', then we are probably 10448 processing something like: 10449 10450 void f() {}, *p; 10451 10452 which is erroneous. */ 10453 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 10454 { 10455 cp_token *token = cp_lexer_peek_token (parser->lexer); 10456 error_at (token->location, 10457 "mixing" 10458 " declarations and function-definitions is forbidden"); 10459 } 10460 /* Otherwise, we're done with the list of declarators. */ 10461 else 10462 { 10463 pop_deferring_access_checks (); 10464 return; 10465 } 10466 } 10467 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE) 10468 *maybe_range_for_decl = decl; 10469 /* The next token should be either a `,' or a `;'. */ 10470 token = cp_lexer_peek_token (parser->lexer); 10471 /* If it's a `,', there are more declarators to come. */ 10472 if (token->type == CPP_COMMA) 10473 /* will be consumed next time around */; 10474 /* If it's a `;', we are done. */ 10475 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl) 10476 break; 10477 /* Anything else is an error. */ 10478 else 10479 { 10480 /* If we have already issued an error message we don't need 10481 to issue another one. */ 10482 if (decl != error_mark_node 10483 || cp_parser_uncommitted_to_tentative_parse_p (parser)) 10484 cp_parser_error (parser, "expected %<,%> or %<;%>"); 10485 /* Skip tokens until we reach the end of the statement. */ 10486 cp_parser_skip_to_end_of_statement (parser); 10487 /* If the next token is now a `;', consume it. */ 10488 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 10489 cp_lexer_consume_token (parser->lexer); 10490 goto done; 10491 } 10492 /* After the first time around, a function-definition is not 10493 allowed -- even if it was OK at first. For example: 10494 10495 int i, f() {} 10496 10497 is not valid. */ 10498 function_definition_allowed_p = false; 10499 } 10500 10501 /* Issue an error message if no declarators are present, and the 10502 decl-specifier-seq does not itself declare a class or 10503 enumeration. */ 10504 if (!saw_declarator) 10505 { 10506 if (cp_parser_declares_only_class_p (parser)) 10507 shadow_tag (&decl_specifiers); 10508 /* Perform any deferred access checks. */ 10509 perform_deferred_access_checks (); 10510 } 10511 10512 /* Consume the `;'. */ 10513 if (!maybe_range_for_decl) 10514 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 10515 10516 done: 10517 pop_deferring_access_checks (); 10518 } 10519 10520 /* Parse a decl-specifier-seq. 10521 10522 decl-specifier-seq: 10523 decl-specifier-seq [opt] decl-specifier 10524 10525 decl-specifier: 10526 storage-class-specifier 10527 type-specifier 10528 function-specifier 10529 friend 10530 typedef 10531 10532 GNU Extension: 10533 10534 decl-specifier: 10535 attributes 10536 10537 Set *DECL_SPECS to a representation of the decl-specifier-seq. 10538 10539 The parser flags FLAGS is used to control type-specifier parsing. 10540 10541 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following 10542 flags: 10543 10544 1: one of the decl-specifiers is an elaborated-type-specifier 10545 (i.e., a type declaration) 10546 2: one of the decl-specifiers is an enum-specifier or a 10547 class-specifier (i.e., a type definition) 10548 10549 */ 10550 10551 static void 10552 cp_parser_decl_specifier_seq (cp_parser* parser, 10553 cp_parser_flags flags, 10554 cp_decl_specifier_seq *decl_specs, 10555 int* declares_class_or_enum) 10556 { 10557 bool constructor_possible_p = !parser->in_declarator_p; 10558 cp_token *start_token = NULL; 10559 10560 /* Clear DECL_SPECS. */ 10561 clear_decl_specs (decl_specs); 10562 10563 /* Assume no class or enumeration type is declared. */ 10564 *declares_class_or_enum = 0; 10565 10566 /* Keep reading specifiers until there are no more to read. */ 10567 while (true) 10568 { 10569 bool constructor_p; 10570 bool found_decl_spec; 10571 cp_token *token; 10572 10573 /* Peek at the next token. */ 10574 token = cp_lexer_peek_token (parser->lexer); 10575 10576 /* Save the first token of the decl spec list for error 10577 reporting. */ 10578 if (!start_token) 10579 start_token = token; 10580 /* Handle attributes. */ 10581 if (token->keyword == RID_ATTRIBUTE) 10582 { 10583 /* Parse the attributes. */ 10584 decl_specs->attributes 10585 = chainon (decl_specs->attributes, 10586 cp_parser_attributes_opt (parser)); 10587 continue; 10588 } 10589 /* Assume we will find a decl-specifier keyword. */ 10590 found_decl_spec = true; 10591 /* If the next token is an appropriate keyword, we can simply 10592 add it to the list. */ 10593 switch (token->keyword) 10594 { 10595 /* decl-specifier: 10596 friend 10597 constexpr */ 10598 case RID_FRIEND: 10599 if (!at_class_scope_p ()) 10600 { 10601 error_at (token->location, "%<friend%> used outside of class"); 10602 cp_lexer_purge_token (parser->lexer); 10603 } 10604 else 10605 { 10606 ++decl_specs->specs[(int) ds_friend]; 10607 /* Consume the token. */ 10608 cp_lexer_consume_token (parser->lexer); 10609 } 10610 break; 10611 10612 case RID_CONSTEXPR: 10613 ++decl_specs->specs[(int) ds_constexpr]; 10614 cp_lexer_consume_token (parser->lexer); 10615 break; 10616 10617 /* function-specifier: 10618 inline 10619 virtual 10620 explicit */ 10621 case RID_INLINE: 10622 case RID_VIRTUAL: 10623 case RID_EXPLICIT: 10624 cp_parser_function_specifier_opt (parser, decl_specs); 10625 break; 10626 10627 /* decl-specifier: 10628 typedef */ 10629 case RID_TYPEDEF: 10630 ++decl_specs->specs[(int) ds_typedef]; 10631 /* Consume the token. */ 10632 cp_lexer_consume_token (parser->lexer); 10633 /* A constructor declarator cannot appear in a typedef. */ 10634 constructor_possible_p = false; 10635 /* The "typedef" keyword can only occur in a declaration; we 10636 may as well commit at this point. */ 10637 cp_parser_commit_to_tentative_parse (parser); 10638 10639 if (decl_specs->storage_class != sc_none) 10640 decl_specs->conflicting_specifiers_p = true; 10641 break; 10642 10643 /* storage-class-specifier: 10644 auto 10645 register 10646 static 10647 extern 10648 mutable 10649 10650 GNU Extension: 10651 thread */ 10652 case RID_AUTO: 10653 if (cxx_dialect == cxx98) 10654 { 10655 /* Consume the token. */ 10656 cp_lexer_consume_token (parser->lexer); 10657 10658 /* Complain about `auto' as a storage specifier, if 10659 we're complaining about C++0x compatibility. */ 10660 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>" 10661 " changes meaning in C++11; please remove it"); 10662 10663 /* Set the storage class anyway. */ 10664 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO, 10665 token->location); 10666 } 10667 else 10668 /* C++0x auto type-specifier. */ 10669 found_decl_spec = false; 10670 break; 10671 10672 case RID_REGISTER: 10673 case RID_STATIC: 10674 case RID_EXTERN: 10675 case RID_MUTABLE: 10676 /* Consume the token. */ 10677 cp_lexer_consume_token (parser->lexer); 10678 cp_parser_set_storage_class (parser, decl_specs, token->keyword, 10679 token->location); 10680 break; 10681 case RID_THREAD: 10682 /* Consume the token. */ 10683 cp_lexer_consume_token (parser->lexer); 10684 ++decl_specs->specs[(int) ds_thread]; 10685 break; 10686 10687 default: 10688 /* We did not yet find a decl-specifier yet. */ 10689 found_decl_spec = false; 10690 break; 10691 } 10692 10693 if (found_decl_spec 10694 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR) 10695 && token->keyword != RID_CONSTEXPR) 10696 error ("decl-specifier invalid in condition"); 10697 10698 /* Constructors are a special case. The `S' in `S()' is not a 10699 decl-specifier; it is the beginning of the declarator. */ 10700 constructor_p 10701 = (!found_decl_spec 10702 && constructor_possible_p 10703 && (cp_parser_constructor_declarator_p 10704 (parser, decl_specs->specs[(int) ds_friend] != 0))); 10705 10706 /* If we don't have a DECL_SPEC yet, then we must be looking at 10707 a type-specifier. */ 10708 if (!found_decl_spec && !constructor_p) 10709 { 10710 int decl_spec_declares_class_or_enum; 10711 bool is_cv_qualifier; 10712 tree type_spec; 10713 10714 type_spec 10715 = cp_parser_type_specifier (parser, flags, 10716 decl_specs, 10717 /*is_declaration=*/true, 10718 &decl_spec_declares_class_or_enum, 10719 &is_cv_qualifier); 10720 *declares_class_or_enum |= decl_spec_declares_class_or_enum; 10721 10722 /* If this type-specifier referenced a user-defined type 10723 (a typedef, class-name, etc.), then we can't allow any 10724 more such type-specifiers henceforth. 10725 10726 [dcl.spec] 10727 10728 The longest sequence of decl-specifiers that could 10729 possibly be a type name is taken as the 10730 decl-specifier-seq of a declaration. The sequence shall 10731 be self-consistent as described below. 10732 10733 [dcl.type] 10734 10735 As a general rule, at most one type-specifier is allowed 10736 in the complete decl-specifier-seq of a declaration. The 10737 only exceptions are the following: 10738 10739 -- const or volatile can be combined with any other 10740 type-specifier. 10741 10742 -- signed or unsigned can be combined with char, long, 10743 short, or int. 10744 10745 -- .. 10746 10747 Example: 10748 10749 typedef char* Pc; 10750 void g (const int Pc); 10751 10752 Here, Pc is *not* part of the decl-specifier seq; it's 10753 the declarator. Therefore, once we see a type-specifier 10754 (other than a cv-qualifier), we forbid any additional 10755 user-defined types. We *do* still allow things like `int 10756 int' to be considered a decl-specifier-seq, and issue the 10757 error message later. */ 10758 if (type_spec && !is_cv_qualifier) 10759 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; 10760 /* A constructor declarator cannot follow a type-specifier. */ 10761 if (type_spec) 10762 { 10763 constructor_possible_p = false; 10764 found_decl_spec = true; 10765 if (!is_cv_qualifier) 10766 decl_specs->any_type_specifiers_p = true; 10767 } 10768 } 10769 10770 /* If we still do not have a DECL_SPEC, then there are no more 10771 decl-specifiers. */ 10772 if (!found_decl_spec) 10773 break; 10774 10775 decl_specs->any_specifiers_p = true; 10776 /* After we see one decl-specifier, further decl-specifiers are 10777 always optional. */ 10778 flags |= CP_PARSER_FLAGS_OPTIONAL; 10779 } 10780 10781 cp_parser_check_decl_spec (decl_specs, start_token->location); 10782 10783 /* Don't allow a friend specifier with a class definition. */ 10784 if (decl_specs->specs[(int) ds_friend] != 0 10785 && (*declares_class_or_enum & 2)) 10786 error_at (start_token->location, 10787 "class definition may not be declared a friend"); 10788 } 10789 10790 /* Parse an (optional) storage-class-specifier. 10791 10792 storage-class-specifier: 10793 auto 10794 register 10795 static 10796 extern 10797 mutable 10798 10799 GNU Extension: 10800 10801 storage-class-specifier: 10802 thread 10803 10804 Returns an IDENTIFIER_NODE corresponding to the keyword used. */ 10805 10806 static tree 10807 cp_parser_storage_class_specifier_opt (cp_parser* parser) 10808 { 10809 switch (cp_lexer_peek_token (parser->lexer)->keyword) 10810 { 10811 case RID_AUTO: 10812 if (cxx_dialect != cxx98) 10813 return NULL_TREE; 10814 /* Fall through for C++98. */ 10815 10816 case RID_REGISTER: 10817 case RID_STATIC: 10818 case RID_EXTERN: 10819 case RID_MUTABLE: 10820 case RID_THREAD: 10821 /* Consume the token. */ 10822 return cp_lexer_consume_token (parser->lexer)->u.value; 10823 10824 default: 10825 return NULL_TREE; 10826 } 10827 } 10828 10829 /* Parse an (optional) function-specifier. 10830 10831 function-specifier: 10832 inline 10833 virtual 10834 explicit 10835 10836 Returns an IDENTIFIER_NODE corresponding to the keyword used. 10837 Updates DECL_SPECS, if it is non-NULL. */ 10838 10839 static tree 10840 cp_parser_function_specifier_opt (cp_parser* parser, 10841 cp_decl_specifier_seq *decl_specs) 10842 { 10843 cp_token *token = cp_lexer_peek_token (parser->lexer); 10844 switch (token->keyword) 10845 { 10846 case RID_INLINE: 10847 if (decl_specs) 10848 ++decl_specs->specs[(int) ds_inline]; 10849 break; 10850 10851 case RID_VIRTUAL: 10852 /* 14.5.2.3 [temp.mem] 10853 10854 A member function template shall not be virtual. */ 10855 if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 10856 error_at (token->location, "templates may not be %<virtual%>"); 10857 else if (decl_specs) 10858 ++decl_specs->specs[(int) ds_virtual]; 10859 break; 10860 10861 case RID_EXPLICIT: 10862 if (decl_specs) 10863 ++decl_specs->specs[(int) ds_explicit]; 10864 break; 10865 10866 default: 10867 return NULL_TREE; 10868 } 10869 10870 /* Consume the token. */ 10871 return cp_lexer_consume_token (parser->lexer)->u.value; 10872 } 10873 10874 /* Parse a linkage-specification. 10875 10876 linkage-specification: 10877 extern string-literal { declaration-seq [opt] } 10878 extern string-literal declaration */ 10879 10880 static void 10881 cp_parser_linkage_specification (cp_parser* parser) 10882 { 10883 tree linkage; 10884 10885 /* Look for the `extern' keyword. */ 10886 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN); 10887 10888 /* Look for the string-literal. */ 10889 linkage = cp_parser_string_literal (parser, false, false); 10890 10891 /* Transform the literal into an identifier. If the literal is a 10892 wide-character string, or contains embedded NULs, then we can't 10893 handle it as the user wants. */ 10894 if (strlen (TREE_STRING_POINTER (linkage)) 10895 != (size_t) (TREE_STRING_LENGTH (linkage) - 1)) 10896 { 10897 cp_parser_error (parser, "invalid linkage-specification"); 10898 /* Assume C++ linkage. */ 10899 linkage = lang_name_cplusplus; 10900 } 10901 else 10902 linkage = get_identifier (TREE_STRING_POINTER (linkage)); 10903 10904 /* We're now using the new linkage. */ 10905 push_lang_context (linkage); 10906 10907 /* If the next token is a `{', then we're using the first 10908 production. */ 10909 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 10910 { 10911 /* Consume the `{' token. */ 10912 cp_lexer_consume_token (parser->lexer); 10913 /* Parse the declarations. */ 10914 cp_parser_declaration_seq_opt (parser); 10915 /* Look for the closing `}'. */ 10916 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 10917 } 10918 /* Otherwise, there's just one declaration. */ 10919 else 10920 { 10921 bool saved_in_unbraced_linkage_specification_p; 10922 10923 saved_in_unbraced_linkage_specification_p 10924 = parser->in_unbraced_linkage_specification_p; 10925 parser->in_unbraced_linkage_specification_p = true; 10926 cp_parser_declaration (parser); 10927 parser->in_unbraced_linkage_specification_p 10928 = saved_in_unbraced_linkage_specification_p; 10929 } 10930 10931 /* We're done with the linkage-specification. */ 10932 pop_lang_context (); 10933 } 10934 10935 /* Parse a static_assert-declaration. 10936 10937 static_assert-declaration: 10938 static_assert ( constant-expression , string-literal ) ; 10939 10940 If MEMBER_P, this static_assert is a class member. */ 10941 10942 static void 10943 cp_parser_static_assert(cp_parser *parser, bool member_p) 10944 { 10945 tree condition; 10946 tree message; 10947 cp_token *token; 10948 location_t saved_loc; 10949 bool dummy; 10950 10951 /* Peek at the `static_assert' token so we can keep track of exactly 10952 where the static assertion started. */ 10953 token = cp_lexer_peek_token (parser->lexer); 10954 saved_loc = token->location; 10955 10956 /* Look for the `static_assert' keyword. */ 10957 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 10958 RT_STATIC_ASSERT)) 10959 return; 10960 10961 /* We know we are in a static assertion; commit to any tentative 10962 parse. */ 10963 if (cp_parser_parsing_tentatively (parser)) 10964 cp_parser_commit_to_tentative_parse (parser); 10965 10966 /* Parse the `(' starting the static assertion condition. */ 10967 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 10968 10969 /* Parse the constant-expression. Allow a non-constant expression 10970 here in order to give better diagnostics in finish_static_assert. */ 10971 condition = 10972 cp_parser_constant_expression (parser, 10973 /*allow_non_constant_p=*/true, 10974 /*non_constant_p=*/&dummy); 10975 10976 /* Parse the separating `,'. */ 10977 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 10978 10979 /* Parse the string-literal message. */ 10980 message = cp_parser_string_literal (parser, 10981 /*translate=*/false, 10982 /*wide_ok=*/true); 10983 10984 /* A `)' completes the static assertion. */ 10985 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 10986 cp_parser_skip_to_closing_parenthesis (parser, 10987 /*recovering=*/true, 10988 /*or_comma=*/false, 10989 /*consume_paren=*/true); 10990 10991 /* A semicolon terminates the declaration. */ 10992 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 10993 10994 /* Complete the static assertion, which may mean either processing 10995 the static assert now or saving it for template instantiation. */ 10996 finish_static_assert (condition, message, saved_loc, member_p); 10997 } 10998 10999 /* Parse a `decltype' type. Returns the type. 11000 11001 simple-type-specifier: 11002 decltype ( expression ) */ 11003 11004 static tree 11005 cp_parser_decltype (cp_parser *parser) 11006 { 11007 tree expr; 11008 bool id_expression_or_member_access_p = false; 11009 const char *saved_message; 11010 bool saved_integral_constant_expression_p; 11011 bool saved_non_integral_constant_expression_p; 11012 cp_token *id_expr_start_token; 11013 cp_token *start_token = cp_lexer_peek_token (parser->lexer); 11014 11015 if (start_token->type == CPP_DECLTYPE) 11016 { 11017 /* Already parsed. */ 11018 cp_lexer_consume_token (parser->lexer); 11019 return start_token->u.value; 11020 } 11021 11022 /* Look for the `decltype' token. */ 11023 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE)) 11024 return error_mark_node; 11025 11026 /* Types cannot be defined in a `decltype' expression. Save away the 11027 old message. */ 11028 saved_message = parser->type_definition_forbidden_message; 11029 11030 /* And create the new one. */ 11031 parser->type_definition_forbidden_message 11032 = G_("types may not be defined in %<decltype%> expressions"); 11033 11034 /* The restrictions on constant-expressions do not apply inside 11035 decltype expressions. */ 11036 saved_integral_constant_expression_p 11037 = parser->integral_constant_expression_p; 11038 saved_non_integral_constant_expression_p 11039 = parser->non_integral_constant_expression_p; 11040 parser->integral_constant_expression_p = false; 11041 11042 /* Do not actually evaluate the expression. */ 11043 ++cp_unevaluated_operand; 11044 11045 /* Do not warn about problems with the expression. */ 11046 ++c_inhibit_evaluation_warnings; 11047 11048 /* Parse the opening `('. */ 11049 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 11050 return error_mark_node; 11051 11052 /* First, try parsing an id-expression. */ 11053 id_expr_start_token = cp_lexer_peek_token (parser->lexer); 11054 cp_parser_parse_tentatively (parser); 11055 expr = cp_parser_id_expression (parser, 11056 /*template_keyword_p=*/false, 11057 /*check_dependency_p=*/true, 11058 /*template_p=*/NULL, 11059 /*declarator_p=*/false, 11060 /*optional_p=*/false); 11061 11062 if (!cp_parser_error_occurred (parser) && expr != error_mark_node) 11063 { 11064 bool non_integral_constant_expression_p = false; 11065 tree id_expression = expr; 11066 cp_id_kind idk; 11067 const char *error_msg; 11068 11069 if (TREE_CODE (expr) == IDENTIFIER_NODE) 11070 /* Lookup the name we got back from the id-expression. */ 11071 expr = cp_parser_lookup_name (parser, expr, 11072 none_type, 11073 /*is_template=*/false, 11074 /*is_namespace=*/false, 11075 /*check_dependency=*/true, 11076 /*ambiguous_decls=*/NULL, 11077 id_expr_start_token->location); 11078 11079 if (expr 11080 && expr != error_mark_node 11081 && TREE_CODE (expr) != TEMPLATE_ID_EXPR 11082 && TREE_CODE (expr) != TYPE_DECL 11083 && (TREE_CODE (expr) != BIT_NOT_EXPR 11084 || !TYPE_P (TREE_OPERAND (expr, 0))) 11085 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN) 11086 { 11087 /* Complete lookup of the id-expression. */ 11088 expr = (finish_id_expression 11089 (id_expression, expr, parser->scope, &idk, 11090 /*integral_constant_expression_p=*/false, 11091 /*allow_non_integral_constant_expression_p=*/true, 11092 &non_integral_constant_expression_p, 11093 /*template_p=*/false, 11094 /*done=*/true, 11095 /*address_p=*/false, 11096 /*template_arg_p=*/false, 11097 &error_msg, 11098 id_expr_start_token->location)); 11099 11100 if (expr == error_mark_node) 11101 /* We found an id-expression, but it was something that we 11102 should not have found. This is an error, not something 11103 we can recover from, so note that we found an 11104 id-expression and we'll recover as gracefully as 11105 possible. */ 11106 id_expression_or_member_access_p = true; 11107 } 11108 11109 if (expr 11110 && expr != error_mark_node 11111 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN) 11112 /* We have an id-expression. */ 11113 id_expression_or_member_access_p = true; 11114 } 11115 11116 if (!id_expression_or_member_access_p) 11117 { 11118 /* Abort the id-expression parse. */ 11119 cp_parser_abort_tentative_parse (parser); 11120 11121 /* Parsing tentatively, again. */ 11122 cp_parser_parse_tentatively (parser); 11123 11124 /* Parse a class member access. */ 11125 expr = cp_parser_postfix_expression (parser, /*address_p=*/false, 11126 /*cast_p=*/false, 11127 /*member_access_only_p=*/true, NULL); 11128 11129 if (expr 11130 && expr != error_mark_node 11131 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN) 11132 /* We have an id-expression. */ 11133 id_expression_or_member_access_p = true; 11134 } 11135 11136 if (id_expression_or_member_access_p) 11137 /* We have parsed the complete id-expression or member access. */ 11138 cp_parser_parse_definitely (parser); 11139 else 11140 { 11141 bool saved_greater_than_is_operator_p; 11142 11143 /* Abort our attempt to parse an id-expression or member access 11144 expression. */ 11145 cp_parser_abort_tentative_parse (parser); 11146 11147 /* Within a parenthesized expression, a `>' token is always 11148 the greater-than operator. */ 11149 saved_greater_than_is_operator_p 11150 = parser->greater_than_is_operator_p; 11151 parser->greater_than_is_operator_p = true; 11152 11153 /* Parse a full expression. */ 11154 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 11155 11156 /* The `>' token might be the end of a template-id or 11157 template-parameter-list now. */ 11158 parser->greater_than_is_operator_p 11159 = saved_greater_than_is_operator_p; 11160 } 11161 11162 /* Go back to evaluating expressions. */ 11163 --cp_unevaluated_operand; 11164 --c_inhibit_evaluation_warnings; 11165 11166 /* Restore the old message and the integral constant expression 11167 flags. */ 11168 parser->type_definition_forbidden_message = saved_message; 11169 parser->integral_constant_expression_p 11170 = saved_integral_constant_expression_p; 11171 parser->non_integral_constant_expression_p 11172 = saved_non_integral_constant_expression_p; 11173 11174 /* Parse to the closing `)'. */ 11175 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 11176 { 11177 cp_parser_skip_to_closing_parenthesis (parser, true, false, 11178 /*consume_paren=*/true); 11179 return error_mark_node; 11180 } 11181 11182 expr = finish_decltype_type (expr, id_expression_or_member_access_p, 11183 tf_warning_or_error); 11184 11185 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse 11186 it again. */ 11187 start_token->type = CPP_DECLTYPE; 11188 start_token->u.value = expr; 11189 start_token->keyword = RID_MAX; 11190 cp_lexer_purge_tokens_after (parser->lexer, start_token); 11191 11192 return expr; 11193 } 11194 11195 /* Special member functions [gram.special] */ 11196 11197 /* Parse a conversion-function-id. 11198 11199 conversion-function-id: 11200 operator conversion-type-id 11201 11202 Returns an IDENTIFIER_NODE representing the operator. */ 11203 11204 static tree 11205 cp_parser_conversion_function_id (cp_parser* parser) 11206 { 11207 tree type; 11208 tree saved_scope; 11209 tree saved_qualifying_scope; 11210 tree saved_object_scope; 11211 tree pushed_scope = NULL_TREE; 11212 11213 /* Look for the `operator' token. */ 11214 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR)) 11215 return error_mark_node; 11216 /* When we parse the conversion-type-id, the current scope will be 11217 reset. However, we need that information in able to look up the 11218 conversion function later, so we save it here. */ 11219 saved_scope = parser->scope; 11220 saved_qualifying_scope = parser->qualifying_scope; 11221 saved_object_scope = parser->object_scope; 11222 /* We must enter the scope of the class so that the names of 11223 entities declared within the class are available in the 11224 conversion-type-id. For example, consider: 11225 11226 struct S { 11227 typedef int I; 11228 operator I(); 11229 }; 11230 11231 S::operator I() { ... } 11232 11233 In order to see that `I' is a type-name in the definition, we 11234 must be in the scope of `S'. */ 11235 if (saved_scope) 11236 pushed_scope = push_scope (saved_scope); 11237 /* Parse the conversion-type-id. */ 11238 type = cp_parser_conversion_type_id (parser); 11239 /* Leave the scope of the class, if any. */ 11240 if (pushed_scope) 11241 pop_scope (pushed_scope); 11242 /* Restore the saved scope. */ 11243 parser->scope = saved_scope; 11244 parser->qualifying_scope = saved_qualifying_scope; 11245 parser->object_scope = saved_object_scope; 11246 /* If the TYPE is invalid, indicate failure. */ 11247 if (type == error_mark_node) 11248 return error_mark_node; 11249 return mangle_conv_op_name_for_type (type); 11250 } 11251 11252 /* Parse a conversion-type-id: 11253 11254 conversion-type-id: 11255 type-specifier-seq conversion-declarator [opt] 11256 11257 Returns the TYPE specified. */ 11258 11259 static tree 11260 cp_parser_conversion_type_id (cp_parser* parser) 11261 { 11262 tree attributes; 11263 cp_decl_specifier_seq type_specifiers; 11264 cp_declarator *declarator; 11265 tree type_specified; 11266 11267 /* Parse the attributes. */ 11268 attributes = cp_parser_attributes_opt (parser); 11269 /* Parse the type-specifiers. */ 11270 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false, 11271 /*is_trailing_return=*/false, 11272 &type_specifiers); 11273 /* If that didn't work, stop. */ 11274 if (type_specifiers.type == error_mark_node) 11275 return error_mark_node; 11276 /* Parse the conversion-declarator. */ 11277 declarator = cp_parser_conversion_declarator_opt (parser); 11278 11279 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME, 11280 /*initialized=*/0, &attributes); 11281 if (attributes) 11282 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0); 11283 11284 /* Don't give this error when parsing tentatively. This happens to 11285 work because we always parse this definitively once. */ 11286 if (! cp_parser_uncommitted_to_tentative_parse_p (parser) 11287 && type_uses_auto (type_specified)) 11288 { 11289 error ("invalid use of %<auto%> in conversion operator"); 11290 return error_mark_node; 11291 } 11292 11293 return type_specified; 11294 } 11295 11296 /* Parse an (optional) conversion-declarator. 11297 11298 conversion-declarator: 11299 ptr-operator conversion-declarator [opt] 11300 11301 */ 11302 11303 static cp_declarator * 11304 cp_parser_conversion_declarator_opt (cp_parser* parser) 11305 { 11306 enum tree_code code; 11307 tree class_type; 11308 cp_cv_quals cv_quals; 11309 11310 /* We don't know if there's a ptr-operator next, or not. */ 11311 cp_parser_parse_tentatively (parser); 11312 /* Try the ptr-operator. */ 11313 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals); 11314 /* If it worked, look for more conversion-declarators. */ 11315 if (cp_parser_parse_definitely (parser)) 11316 { 11317 cp_declarator *declarator; 11318 11319 /* Parse another optional declarator. */ 11320 declarator = cp_parser_conversion_declarator_opt (parser); 11321 11322 return cp_parser_make_indirect_declarator 11323 (code, class_type, cv_quals, declarator); 11324 } 11325 11326 return NULL; 11327 } 11328 11329 /* Parse an (optional) ctor-initializer. 11330 11331 ctor-initializer: 11332 : mem-initializer-list 11333 11334 Returns TRUE iff the ctor-initializer was actually present. */ 11335 11336 static bool 11337 cp_parser_ctor_initializer_opt (cp_parser* parser) 11338 { 11339 /* If the next token is not a `:', then there is no 11340 ctor-initializer. */ 11341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) 11342 { 11343 /* Do default initialization of any bases and members. */ 11344 if (DECL_CONSTRUCTOR_P (current_function_decl)) 11345 finish_mem_initializers (NULL_TREE); 11346 11347 return false; 11348 } 11349 11350 /* Consume the `:' token. */ 11351 cp_lexer_consume_token (parser->lexer); 11352 /* And the mem-initializer-list. */ 11353 cp_parser_mem_initializer_list (parser); 11354 11355 return true; 11356 } 11357 11358 /* Parse a mem-initializer-list. 11359 11360 mem-initializer-list: 11361 mem-initializer ... [opt] 11362 mem-initializer ... [opt] , mem-initializer-list */ 11363 11364 static void 11365 cp_parser_mem_initializer_list (cp_parser* parser) 11366 { 11367 tree mem_initializer_list = NULL_TREE; 11368 tree target_ctor = error_mark_node; 11369 cp_token *token = cp_lexer_peek_token (parser->lexer); 11370 11371 /* Let the semantic analysis code know that we are starting the 11372 mem-initializer-list. */ 11373 if (!DECL_CONSTRUCTOR_P (current_function_decl)) 11374 error_at (token->location, 11375 "only constructors take member initializers"); 11376 11377 /* Loop through the list. */ 11378 while (true) 11379 { 11380 tree mem_initializer; 11381 11382 token = cp_lexer_peek_token (parser->lexer); 11383 /* Parse the mem-initializer. */ 11384 mem_initializer = cp_parser_mem_initializer (parser); 11385 /* If the next token is a `...', we're expanding member initializers. */ 11386 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 11387 { 11388 /* Consume the `...'. */ 11389 cp_lexer_consume_token (parser->lexer); 11390 11391 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers 11392 can be expanded but members cannot. */ 11393 if (mem_initializer != error_mark_node 11394 && !TYPE_P (TREE_PURPOSE (mem_initializer))) 11395 { 11396 error_at (token->location, 11397 "cannot expand initializer for member %<%D%>", 11398 TREE_PURPOSE (mem_initializer)); 11399 mem_initializer = error_mark_node; 11400 } 11401 11402 /* Construct the pack expansion type. */ 11403 if (mem_initializer != error_mark_node) 11404 mem_initializer = make_pack_expansion (mem_initializer); 11405 } 11406 if (target_ctor != error_mark_node 11407 && mem_initializer != error_mark_node) 11408 { 11409 error ("mem-initializer for %qD follows constructor delegation", 11410 TREE_PURPOSE (mem_initializer)); 11411 mem_initializer = error_mark_node; 11412 } 11413 /* Look for a target constructor. */ 11414 if (mem_initializer != error_mark_node 11415 && TYPE_P (TREE_PURPOSE (mem_initializer)) 11416 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type)) 11417 { 11418 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS); 11419 if (mem_initializer_list) 11420 { 11421 error ("constructor delegation follows mem-initializer for %qD", 11422 TREE_PURPOSE (mem_initializer_list)); 11423 mem_initializer = error_mark_node; 11424 } 11425 target_ctor = mem_initializer; 11426 } 11427 /* Add it to the list, unless it was erroneous. */ 11428 if (mem_initializer != error_mark_node) 11429 { 11430 TREE_CHAIN (mem_initializer) = mem_initializer_list; 11431 mem_initializer_list = mem_initializer; 11432 } 11433 /* If the next token is not a `,', we're done. */ 11434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 11435 break; 11436 /* Consume the `,' token. */ 11437 cp_lexer_consume_token (parser->lexer); 11438 } 11439 11440 /* Perform semantic analysis. */ 11441 if (DECL_CONSTRUCTOR_P (current_function_decl)) 11442 finish_mem_initializers (mem_initializer_list); 11443 } 11444 11445 /* Parse a mem-initializer. 11446 11447 mem-initializer: 11448 mem-initializer-id ( expression-list [opt] ) 11449 mem-initializer-id braced-init-list 11450 11451 GNU extension: 11452 11453 mem-initializer: 11454 ( expression-list [opt] ) 11455 11456 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base 11457 class) or FIELD_DECL (for a non-static data member) to initialize; 11458 the TREE_VALUE is the expression-list. An empty initialization 11459 list is represented by void_list_node. */ 11460 11461 static tree 11462 cp_parser_mem_initializer (cp_parser* parser) 11463 { 11464 tree mem_initializer_id; 11465 tree expression_list; 11466 tree member; 11467 cp_token *token = cp_lexer_peek_token (parser->lexer); 11468 11469 /* Find out what is being initialized. */ 11470 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 11471 { 11472 permerror (token->location, 11473 "anachronistic old-style base class initializer"); 11474 mem_initializer_id = NULL_TREE; 11475 } 11476 else 11477 { 11478 mem_initializer_id = cp_parser_mem_initializer_id (parser); 11479 if (mem_initializer_id == error_mark_node) 11480 return mem_initializer_id; 11481 } 11482 member = expand_member_init (mem_initializer_id); 11483 if (member && !DECL_P (member)) 11484 in_base_initializer = 1; 11485 11486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 11487 { 11488 bool expr_non_constant_p; 11489 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 11490 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p); 11491 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1; 11492 expression_list = build_tree_list (NULL_TREE, expression_list); 11493 } 11494 else 11495 { 11496 VEC(tree,gc)* vec; 11497 vec = cp_parser_parenthesized_expression_list (parser, non_attr, 11498 /*cast_p=*/false, 11499 /*allow_expansion_p=*/true, 11500 /*non_constant_p=*/NULL); 11501 if (vec == NULL) 11502 return error_mark_node; 11503 expression_list = build_tree_list_vec (vec); 11504 release_tree_vector (vec); 11505 } 11506 11507 if (expression_list == error_mark_node) 11508 return error_mark_node; 11509 if (!expression_list) 11510 expression_list = void_type_node; 11511 11512 in_base_initializer = 0; 11513 11514 return member ? build_tree_list (member, expression_list) : error_mark_node; 11515 } 11516 11517 /* Parse a mem-initializer-id. 11518 11519 mem-initializer-id: 11520 :: [opt] nested-name-specifier [opt] class-name 11521 identifier 11522 11523 Returns a TYPE indicating the class to be initializer for the first 11524 production. Returns an IDENTIFIER_NODE indicating the data member 11525 to be initialized for the second production. */ 11526 11527 static tree 11528 cp_parser_mem_initializer_id (cp_parser* parser) 11529 { 11530 bool global_scope_p; 11531 bool nested_name_specifier_p; 11532 bool template_p = false; 11533 tree id; 11534 11535 cp_token *token = cp_lexer_peek_token (parser->lexer); 11536 11537 /* `typename' is not allowed in this context ([temp.res]). */ 11538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) 11539 { 11540 error_at (token->location, 11541 "keyword %<typename%> not allowed in this context (a qualified " 11542 "member initializer is implicitly a type)"); 11543 cp_lexer_consume_token (parser->lexer); 11544 } 11545 /* Look for the optional `::' operator. */ 11546 global_scope_p 11547 = (cp_parser_global_scope_opt (parser, 11548 /*current_scope_valid_p=*/false) 11549 != NULL_TREE); 11550 /* Look for the optional nested-name-specifier. The simplest way to 11551 implement: 11552 11553 [temp.res] 11554 11555 The keyword `typename' is not permitted in a base-specifier or 11556 mem-initializer; in these contexts a qualified name that 11557 depends on a template-parameter is implicitly assumed to be a 11558 type name. 11559 11560 is to assume that we have seen the `typename' keyword at this 11561 point. */ 11562 nested_name_specifier_p 11563 = (cp_parser_nested_name_specifier_opt (parser, 11564 /*typename_keyword_p=*/true, 11565 /*check_dependency_p=*/true, 11566 /*type_p=*/true, 11567 /*is_declaration=*/true) 11568 != NULL_TREE); 11569 if (nested_name_specifier_p) 11570 template_p = cp_parser_optional_template_keyword (parser); 11571 /* If there is a `::' operator or a nested-name-specifier, then we 11572 are definitely looking for a class-name. */ 11573 if (global_scope_p || nested_name_specifier_p) 11574 return cp_parser_class_name (parser, 11575 /*typename_keyword_p=*/true, 11576 /*template_keyword_p=*/template_p, 11577 typename_type, 11578 /*check_dependency_p=*/true, 11579 /*class_head_p=*/false, 11580 /*is_declaration=*/true); 11581 /* Otherwise, we could also be looking for an ordinary identifier. */ 11582 cp_parser_parse_tentatively (parser); 11583 /* Try a class-name. */ 11584 id = cp_parser_class_name (parser, 11585 /*typename_keyword_p=*/true, 11586 /*template_keyword_p=*/false, 11587 none_type, 11588 /*check_dependency_p=*/true, 11589 /*class_head_p=*/false, 11590 /*is_declaration=*/true); 11591 /* If we found one, we're done. */ 11592 if (cp_parser_parse_definitely (parser)) 11593 return id; 11594 /* Otherwise, look for an ordinary identifier. */ 11595 return cp_parser_identifier (parser); 11596 } 11597 11598 /* Overloading [gram.over] */ 11599 11600 /* Parse an operator-function-id. 11601 11602 operator-function-id: 11603 operator operator 11604 11605 Returns an IDENTIFIER_NODE for the operator which is a 11606 human-readable spelling of the identifier, e.g., `operator +'. */ 11607 11608 static tree 11609 cp_parser_operator_function_id (cp_parser* parser) 11610 { 11611 /* Look for the `operator' keyword. */ 11612 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR)) 11613 return error_mark_node; 11614 /* And then the name of the operator itself. */ 11615 return cp_parser_operator (parser); 11616 } 11617 11618 /* Return an identifier node for a user-defined literal operator. 11619 The suffix identifier is chained to the operator name identifier. */ 11620 11621 static tree 11622 cp_literal_operator_id (const char* name) 11623 { 11624 tree identifier; 11625 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX) 11626 + strlen (name) + 10); 11627 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name); 11628 identifier = get_identifier (buffer); 11629 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */ 11630 11631 return identifier; 11632 } 11633 11634 /* Parse an operator. 11635 11636 operator: 11637 new delete new[] delete[] + - * / % ^ & | ~ ! = < > 11638 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && 11639 || ++ -- , ->* -> () [] 11640 11641 GNU Extensions: 11642 11643 operator: 11644 <? >? <?= >?= 11645 11646 Returns an IDENTIFIER_NODE for the operator which is a 11647 human-readable spelling of the identifier, e.g., `operator +'. */ 11648 11649 static tree 11650 cp_parser_operator (cp_parser* parser) 11651 { 11652 tree id = NULL_TREE; 11653 cp_token *token; 11654 11655 /* Peek at the next token. */ 11656 token = cp_lexer_peek_token (parser->lexer); 11657 /* Figure out which operator we have. */ 11658 switch (token->type) 11659 { 11660 case CPP_KEYWORD: 11661 { 11662 enum tree_code op; 11663 11664 /* The keyword should be either `new' or `delete'. */ 11665 if (token->keyword == RID_NEW) 11666 op = NEW_EXPR; 11667 else if (token->keyword == RID_DELETE) 11668 op = DELETE_EXPR; 11669 else 11670 break; 11671 11672 /* Consume the `new' or `delete' token. */ 11673 cp_lexer_consume_token (parser->lexer); 11674 11675 /* Peek at the next token. */ 11676 token = cp_lexer_peek_token (parser->lexer); 11677 /* If it's a `[' token then this is the array variant of the 11678 operator. */ 11679 if (token->type == CPP_OPEN_SQUARE) 11680 { 11681 /* Consume the `[' token. */ 11682 cp_lexer_consume_token (parser->lexer); 11683 /* Look for the `]' token. */ 11684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 11685 id = ansi_opname (op == NEW_EXPR 11686 ? VEC_NEW_EXPR : VEC_DELETE_EXPR); 11687 } 11688 /* Otherwise, we have the non-array variant. */ 11689 else 11690 id = ansi_opname (op); 11691 11692 return id; 11693 } 11694 11695 case CPP_PLUS: 11696 id = ansi_opname (PLUS_EXPR); 11697 break; 11698 11699 case CPP_MINUS: 11700 id = ansi_opname (MINUS_EXPR); 11701 break; 11702 11703 case CPP_MULT: 11704 id = ansi_opname (MULT_EXPR); 11705 break; 11706 11707 case CPP_DIV: 11708 id = ansi_opname (TRUNC_DIV_EXPR); 11709 break; 11710 11711 case CPP_MOD: 11712 id = ansi_opname (TRUNC_MOD_EXPR); 11713 break; 11714 11715 case CPP_XOR: 11716 id = ansi_opname (BIT_XOR_EXPR); 11717 break; 11718 11719 case CPP_AND: 11720 id = ansi_opname (BIT_AND_EXPR); 11721 break; 11722 11723 case CPP_OR: 11724 id = ansi_opname (BIT_IOR_EXPR); 11725 break; 11726 11727 case CPP_COMPL: 11728 id = ansi_opname (BIT_NOT_EXPR); 11729 break; 11730 11731 case CPP_NOT: 11732 id = ansi_opname (TRUTH_NOT_EXPR); 11733 break; 11734 11735 case CPP_EQ: 11736 id = ansi_assopname (NOP_EXPR); 11737 break; 11738 11739 case CPP_LESS: 11740 id = ansi_opname (LT_EXPR); 11741 break; 11742 11743 case CPP_GREATER: 11744 id = ansi_opname (GT_EXPR); 11745 break; 11746 11747 case CPP_PLUS_EQ: 11748 id = ansi_assopname (PLUS_EXPR); 11749 break; 11750 11751 case CPP_MINUS_EQ: 11752 id = ansi_assopname (MINUS_EXPR); 11753 break; 11754 11755 case CPP_MULT_EQ: 11756 id = ansi_assopname (MULT_EXPR); 11757 break; 11758 11759 case CPP_DIV_EQ: 11760 id = ansi_assopname (TRUNC_DIV_EXPR); 11761 break; 11762 11763 case CPP_MOD_EQ: 11764 id = ansi_assopname (TRUNC_MOD_EXPR); 11765 break; 11766 11767 case CPP_XOR_EQ: 11768 id = ansi_assopname (BIT_XOR_EXPR); 11769 break; 11770 11771 case CPP_AND_EQ: 11772 id = ansi_assopname (BIT_AND_EXPR); 11773 break; 11774 11775 case CPP_OR_EQ: 11776 id = ansi_assopname (BIT_IOR_EXPR); 11777 break; 11778 11779 case CPP_LSHIFT: 11780 id = ansi_opname (LSHIFT_EXPR); 11781 break; 11782 11783 case CPP_RSHIFT: 11784 id = ansi_opname (RSHIFT_EXPR); 11785 break; 11786 11787 case CPP_LSHIFT_EQ: 11788 id = ansi_assopname (LSHIFT_EXPR); 11789 break; 11790 11791 case CPP_RSHIFT_EQ: 11792 id = ansi_assopname (RSHIFT_EXPR); 11793 break; 11794 11795 case CPP_EQ_EQ: 11796 id = ansi_opname (EQ_EXPR); 11797 break; 11798 11799 case CPP_NOT_EQ: 11800 id = ansi_opname (NE_EXPR); 11801 break; 11802 11803 case CPP_LESS_EQ: 11804 id = ansi_opname (LE_EXPR); 11805 break; 11806 11807 case CPP_GREATER_EQ: 11808 id = ansi_opname (GE_EXPR); 11809 break; 11810 11811 case CPP_AND_AND: 11812 id = ansi_opname (TRUTH_ANDIF_EXPR); 11813 break; 11814 11815 case CPP_OR_OR: 11816 id = ansi_opname (TRUTH_ORIF_EXPR); 11817 break; 11818 11819 case CPP_PLUS_PLUS: 11820 id = ansi_opname (POSTINCREMENT_EXPR); 11821 break; 11822 11823 case CPP_MINUS_MINUS: 11824 id = ansi_opname (PREDECREMENT_EXPR); 11825 break; 11826 11827 case CPP_COMMA: 11828 id = ansi_opname (COMPOUND_EXPR); 11829 break; 11830 11831 case CPP_DEREF_STAR: 11832 id = ansi_opname (MEMBER_REF); 11833 break; 11834 11835 case CPP_DEREF: 11836 id = ansi_opname (COMPONENT_REF); 11837 break; 11838 11839 case CPP_OPEN_PAREN: 11840 /* Consume the `('. */ 11841 cp_lexer_consume_token (parser->lexer); 11842 /* Look for the matching `)'. */ 11843 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 11844 return ansi_opname (CALL_EXPR); 11845 11846 case CPP_OPEN_SQUARE: 11847 /* Consume the `['. */ 11848 cp_lexer_consume_token (parser->lexer); 11849 /* Look for the matching `]'. */ 11850 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 11851 return ansi_opname (ARRAY_REF); 11852 11853 case CPP_STRING: 11854 if (cxx_dialect == cxx98) 11855 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS); 11856 if (TREE_STRING_LENGTH (token->u.value) > 2) 11857 { 11858 error ("expected empty string after %<operator%> keyword"); 11859 return error_mark_node; 11860 } 11861 /* Consume the string. */ 11862 cp_lexer_consume_token (parser->lexer); 11863 /* Look for the suffix identifier. */ 11864 token = cp_lexer_peek_token (parser->lexer); 11865 if (token->type == CPP_NAME) 11866 { 11867 id = cp_parser_identifier (parser); 11868 if (id != error_mark_node) 11869 { 11870 const char *name = IDENTIFIER_POINTER (id); 11871 return cp_literal_operator_id (name); 11872 } 11873 } 11874 else 11875 { 11876 error ("expected suffix identifier"); 11877 return error_mark_node; 11878 } 11879 11880 case CPP_STRING_USERDEF: 11881 error ("missing space between %<\"\"%> and suffix identifier"); 11882 return error_mark_node; 11883 11884 default: 11885 /* Anything else is an error. */ 11886 break; 11887 } 11888 11889 /* If we have selected an identifier, we need to consume the 11890 operator token. */ 11891 if (id) 11892 cp_lexer_consume_token (parser->lexer); 11893 /* Otherwise, no valid operator name was present. */ 11894 else 11895 { 11896 cp_parser_error (parser, "expected operator"); 11897 id = error_mark_node; 11898 } 11899 11900 return id; 11901 } 11902 11903 /* Parse a template-declaration. 11904 11905 template-declaration: 11906 export [opt] template < template-parameter-list > declaration 11907 11908 If MEMBER_P is TRUE, this template-declaration occurs within a 11909 class-specifier. 11910 11911 The grammar rule given by the standard isn't correct. What 11912 is really meant is: 11913 11914 template-declaration: 11915 export [opt] template-parameter-list-seq 11916 decl-specifier-seq [opt] init-declarator [opt] ; 11917 export [opt] template-parameter-list-seq 11918 function-definition 11919 11920 template-parameter-list-seq: 11921 template-parameter-list-seq [opt] 11922 template < template-parameter-list > */ 11923 11924 static void 11925 cp_parser_template_declaration (cp_parser* parser, bool member_p) 11926 { 11927 /* Check for `export'. */ 11928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT)) 11929 { 11930 /* Consume the `export' token. */ 11931 cp_lexer_consume_token (parser->lexer); 11932 /* Warn that we do not support `export'. */ 11933 warning (0, "keyword %<export%> not implemented, and will be ignored"); 11934 } 11935 11936 cp_parser_template_declaration_after_export (parser, member_p); 11937 } 11938 11939 /* Parse a template-parameter-list. 11940 11941 template-parameter-list: 11942 template-parameter 11943 template-parameter-list , template-parameter 11944 11945 Returns a TREE_LIST. Each node represents a template parameter. 11946 The nodes are connected via their TREE_CHAINs. */ 11947 11948 static tree 11949 cp_parser_template_parameter_list (cp_parser* parser) 11950 { 11951 tree parameter_list = NULL_TREE; 11952 11953 begin_template_parm_list (); 11954 11955 /* The loop below parses the template parms. We first need to know 11956 the total number of template parms to be able to compute proper 11957 canonical types of each dependent type. So after the loop, when 11958 we know the total number of template parms, 11959 end_template_parm_list computes the proper canonical types and 11960 fixes up the dependent types accordingly. */ 11961 while (true) 11962 { 11963 tree parameter; 11964 bool is_non_type; 11965 bool is_parameter_pack; 11966 location_t parm_loc; 11967 11968 /* Parse the template-parameter. */ 11969 parm_loc = cp_lexer_peek_token (parser->lexer)->location; 11970 parameter = cp_parser_template_parameter (parser, 11971 &is_non_type, 11972 &is_parameter_pack); 11973 /* Add it to the list. */ 11974 if (parameter != error_mark_node) 11975 parameter_list = process_template_parm (parameter_list, 11976 parm_loc, 11977 parameter, 11978 is_non_type, 11979 is_parameter_pack); 11980 else 11981 { 11982 tree err_parm = build_tree_list (parameter, parameter); 11983 parameter_list = chainon (parameter_list, err_parm); 11984 } 11985 11986 /* If the next token is not a `,', we're done. */ 11987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 11988 break; 11989 /* Otherwise, consume the `,' token. */ 11990 cp_lexer_consume_token (parser->lexer); 11991 } 11992 11993 return end_template_parm_list (parameter_list); 11994 } 11995 11996 /* Parse a template-parameter. 11997 11998 template-parameter: 11999 type-parameter 12000 parameter-declaration 12001 12002 If all goes well, returns a TREE_LIST. The TREE_VALUE represents 12003 the parameter. The TREE_PURPOSE is the default value, if any. 12004 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true 12005 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is 12006 set to true iff this parameter is a parameter pack. */ 12007 12008 static tree 12009 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type, 12010 bool *is_parameter_pack) 12011 { 12012 cp_token *token; 12013 cp_parameter_declarator *parameter_declarator; 12014 cp_declarator *id_declarator; 12015 tree parm; 12016 12017 /* Assume it is a type parameter or a template parameter. */ 12018 *is_non_type = false; 12019 /* Assume it not a parameter pack. */ 12020 *is_parameter_pack = false; 12021 /* Peek at the next token. */ 12022 token = cp_lexer_peek_token (parser->lexer); 12023 /* If it is `class' or `template', we have a type-parameter. */ 12024 if (token->keyword == RID_TEMPLATE) 12025 return cp_parser_type_parameter (parser, is_parameter_pack); 12026 /* If it is `class' or `typename' we do not know yet whether it is a 12027 type parameter or a non-type parameter. Consider: 12028 12029 template <typename T, typename T::X X> ... 12030 12031 or: 12032 12033 template <class C, class D*> ... 12034 12035 Here, the first parameter is a type parameter, and the second is 12036 a non-type parameter. We can tell by looking at the token after 12037 the identifier -- if it is a `,', `=', or `>' then we have a type 12038 parameter. */ 12039 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS) 12040 { 12041 /* Peek at the token after `class' or `typename'. */ 12042 token = cp_lexer_peek_nth_token (parser->lexer, 2); 12043 /* If it's an ellipsis, we have a template type parameter 12044 pack. */ 12045 if (token->type == CPP_ELLIPSIS) 12046 return cp_parser_type_parameter (parser, is_parameter_pack); 12047 /* If it's an identifier, skip it. */ 12048 if (token->type == CPP_NAME) 12049 token = cp_lexer_peek_nth_token (parser->lexer, 3); 12050 /* Now, see if the token looks like the end of a template 12051 parameter. */ 12052 if (token->type == CPP_COMMA 12053 || token->type == CPP_EQ 12054 || token->type == CPP_GREATER) 12055 return cp_parser_type_parameter (parser, is_parameter_pack); 12056 } 12057 12058 /* Otherwise, it is a non-type parameter. 12059 12060 [temp.param] 12061 12062 When parsing a default template-argument for a non-type 12063 template-parameter, the first non-nested `>' is taken as the end 12064 of the template parameter-list rather than a greater-than 12065 operator. */ 12066 *is_non_type = true; 12067 parameter_declarator 12068 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true, 12069 /*parenthesized_p=*/NULL); 12070 12071 /* If the parameter declaration is marked as a parameter pack, set 12072 *IS_PARAMETER_PACK to notify the caller. Also, unmark the 12073 declarator's PACK_EXPANSION_P, otherwise we'll get errors from 12074 grokdeclarator. */ 12075 if (parameter_declarator 12076 && parameter_declarator->declarator 12077 && parameter_declarator->declarator->parameter_pack_p) 12078 { 12079 *is_parameter_pack = true; 12080 parameter_declarator->declarator->parameter_pack_p = false; 12081 } 12082 12083 /* If the next token is an ellipsis, and we don't already have it 12084 marked as a parameter pack, then we have a parameter pack (that 12085 has no declarator). */ 12086 if (!*is_parameter_pack 12087 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS) 12088 && declarator_can_be_parameter_pack (parameter_declarator->declarator)) 12089 { 12090 /* Consume the `...'. */ 12091 cp_lexer_consume_token (parser->lexer); 12092 maybe_warn_variadic_templates (); 12093 12094 *is_parameter_pack = true; 12095 } 12096 /* We might end up with a pack expansion as the type of the non-type 12097 template parameter, in which case this is a non-type template 12098 parameter pack. */ 12099 else if (parameter_declarator 12100 && parameter_declarator->decl_specifiers.type 12101 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type)) 12102 { 12103 *is_parameter_pack = true; 12104 parameter_declarator->decl_specifiers.type = 12105 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type); 12106 } 12107 12108 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 12109 { 12110 /* Parameter packs cannot have default arguments. However, a 12111 user may try to do so, so we'll parse them and give an 12112 appropriate diagnostic here. */ 12113 12114 cp_token *start_token = cp_lexer_peek_token (parser->lexer); 12115 12116 /* Find the name of the parameter pack. */ 12117 id_declarator = parameter_declarator->declarator; 12118 while (id_declarator && id_declarator->kind != cdk_id) 12119 id_declarator = id_declarator->declarator; 12120 12121 if (id_declarator && id_declarator->kind == cdk_id) 12122 error_at (start_token->location, 12123 "template parameter pack %qD cannot have a default argument", 12124 id_declarator->u.id.unqualified_name); 12125 else 12126 error_at (start_token->location, 12127 "template parameter pack cannot have a default argument"); 12128 12129 /* Parse the default argument, but throw away the result. */ 12130 cp_parser_default_argument (parser, /*template_parm_p=*/true); 12131 } 12132 12133 parm = grokdeclarator (parameter_declarator->declarator, 12134 ¶meter_declarator->decl_specifiers, 12135 TPARM, /*initialized=*/0, 12136 /*attrlist=*/NULL); 12137 if (parm == error_mark_node) 12138 return error_mark_node; 12139 12140 return build_tree_list (parameter_declarator->default_argument, parm); 12141 } 12142 12143 /* Parse a type-parameter. 12144 12145 type-parameter: 12146 class identifier [opt] 12147 class identifier [opt] = type-id 12148 typename identifier [opt] 12149 typename identifier [opt] = type-id 12150 template < template-parameter-list > class identifier [opt] 12151 template < template-parameter-list > class identifier [opt] 12152 = id-expression 12153 12154 GNU Extension (variadic templates): 12155 12156 type-parameter: 12157 class ... identifier [opt] 12158 typename ... identifier [opt] 12159 12160 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The 12161 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is 12162 the declaration of the parameter. 12163 12164 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */ 12165 12166 static tree 12167 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack) 12168 { 12169 cp_token *token; 12170 tree parameter; 12171 12172 /* Look for a keyword to tell us what kind of parameter this is. */ 12173 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE); 12174 if (!token) 12175 return error_mark_node; 12176 12177 switch (token->keyword) 12178 { 12179 case RID_CLASS: 12180 case RID_TYPENAME: 12181 { 12182 tree identifier; 12183 tree default_argument; 12184 12185 /* If the next token is an ellipsis, we have a template 12186 argument pack. */ 12187 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 12188 { 12189 /* Consume the `...' token. */ 12190 cp_lexer_consume_token (parser->lexer); 12191 maybe_warn_variadic_templates (); 12192 12193 *is_parameter_pack = true; 12194 } 12195 12196 /* If the next token is an identifier, then it names the 12197 parameter. */ 12198 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 12199 identifier = cp_parser_identifier (parser); 12200 else 12201 identifier = NULL_TREE; 12202 12203 /* Create the parameter. */ 12204 parameter = finish_template_type_parm (class_type_node, identifier); 12205 12206 /* If the next token is an `=', we have a default argument. */ 12207 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 12208 { 12209 /* Consume the `=' token. */ 12210 cp_lexer_consume_token (parser->lexer); 12211 /* Parse the default-argument. */ 12212 push_deferring_access_checks (dk_no_deferred); 12213 default_argument = cp_parser_type_id (parser); 12214 12215 /* Template parameter packs cannot have default 12216 arguments. */ 12217 if (*is_parameter_pack) 12218 { 12219 if (identifier) 12220 error_at (token->location, 12221 "template parameter pack %qD cannot have a " 12222 "default argument", identifier); 12223 else 12224 error_at (token->location, 12225 "template parameter packs cannot have " 12226 "default arguments"); 12227 default_argument = NULL_TREE; 12228 } 12229 pop_deferring_access_checks (); 12230 } 12231 else 12232 default_argument = NULL_TREE; 12233 12234 /* Create the combined representation of the parameter and the 12235 default argument. */ 12236 parameter = build_tree_list (default_argument, parameter); 12237 } 12238 break; 12239 12240 case RID_TEMPLATE: 12241 { 12242 tree identifier; 12243 tree default_argument; 12244 12245 /* Look for the `<'. */ 12246 cp_parser_require (parser, CPP_LESS, RT_LESS); 12247 /* Parse the template-parameter-list. */ 12248 cp_parser_template_parameter_list (parser); 12249 /* Look for the `>'. */ 12250 cp_parser_require (parser, CPP_GREATER, RT_GREATER); 12251 /* Look for the `class' keyword. */ 12252 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS); 12253 /* If the next token is an ellipsis, we have a template 12254 argument pack. */ 12255 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 12256 { 12257 /* Consume the `...' token. */ 12258 cp_lexer_consume_token (parser->lexer); 12259 maybe_warn_variadic_templates (); 12260 12261 *is_parameter_pack = true; 12262 } 12263 /* If the next token is an `=', then there is a 12264 default-argument. If the next token is a `>', we are at 12265 the end of the parameter-list. If the next token is a `,', 12266 then we are at the end of this parameter. */ 12267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ) 12268 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER) 12269 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 12270 { 12271 identifier = cp_parser_identifier (parser); 12272 /* Treat invalid names as if the parameter were nameless. */ 12273 if (identifier == error_mark_node) 12274 identifier = NULL_TREE; 12275 } 12276 else 12277 identifier = NULL_TREE; 12278 12279 /* Create the template parameter. */ 12280 parameter = finish_template_template_parm (class_type_node, 12281 identifier); 12282 12283 /* If the next token is an `=', then there is a 12284 default-argument. */ 12285 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 12286 { 12287 bool is_template; 12288 12289 /* Consume the `='. */ 12290 cp_lexer_consume_token (parser->lexer); 12291 /* Parse the id-expression. */ 12292 push_deferring_access_checks (dk_no_deferred); 12293 /* save token before parsing the id-expression, for error 12294 reporting */ 12295 token = cp_lexer_peek_token (parser->lexer); 12296 default_argument 12297 = cp_parser_id_expression (parser, 12298 /*template_keyword_p=*/false, 12299 /*check_dependency_p=*/true, 12300 /*template_p=*/&is_template, 12301 /*declarator_p=*/false, 12302 /*optional_p=*/false); 12303 if (TREE_CODE (default_argument) == TYPE_DECL) 12304 /* If the id-expression was a template-id that refers to 12305 a template-class, we already have the declaration here, 12306 so no further lookup is needed. */ 12307 ; 12308 else 12309 /* Look up the name. */ 12310 default_argument 12311 = cp_parser_lookup_name (parser, default_argument, 12312 none_type, 12313 /*is_template=*/is_template, 12314 /*is_namespace=*/false, 12315 /*check_dependency=*/true, 12316 /*ambiguous_decls=*/NULL, 12317 token->location); 12318 /* See if the default argument is valid. */ 12319 default_argument 12320 = check_template_template_default_arg (default_argument); 12321 12322 /* Template parameter packs cannot have default 12323 arguments. */ 12324 if (*is_parameter_pack) 12325 { 12326 if (identifier) 12327 error_at (token->location, 12328 "template parameter pack %qD cannot " 12329 "have a default argument", 12330 identifier); 12331 else 12332 error_at (token->location, "template parameter packs cannot " 12333 "have default arguments"); 12334 default_argument = NULL_TREE; 12335 } 12336 pop_deferring_access_checks (); 12337 } 12338 else 12339 default_argument = NULL_TREE; 12340 12341 /* Create the combined representation of the parameter and the 12342 default argument. */ 12343 parameter = build_tree_list (default_argument, parameter); 12344 } 12345 break; 12346 12347 default: 12348 gcc_unreachable (); 12349 break; 12350 } 12351 12352 return parameter; 12353 } 12354 12355 /* Parse a template-id. 12356 12357 template-id: 12358 template-name < template-argument-list [opt] > 12359 12360 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the 12361 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be 12362 returned. Otherwise, if the template-name names a function, or set 12363 of functions, returns a TEMPLATE_ID_EXPR. If the template-name 12364 names a class, returns a TYPE_DECL for the specialization. 12365 12366 If CHECK_DEPENDENCY_P is FALSE, names are looked up in 12367 uninstantiated templates. */ 12368 12369 static tree 12370 cp_parser_template_id (cp_parser *parser, 12371 bool template_keyword_p, 12372 bool check_dependency_p, 12373 bool is_declaration) 12374 { 12375 int i; 12376 tree templ; 12377 tree arguments; 12378 tree template_id; 12379 cp_token_position start_of_id = 0; 12380 deferred_access_check *chk; 12381 VEC (deferred_access_check,gc) *access_check; 12382 cp_token *next_token = NULL, *next_token_2 = NULL; 12383 bool is_identifier; 12384 12385 /* If the next token corresponds to a template-id, there is no need 12386 to reparse it. */ 12387 next_token = cp_lexer_peek_token (parser->lexer); 12388 if (next_token->type == CPP_TEMPLATE_ID) 12389 { 12390 struct tree_check *check_value; 12391 12392 /* Get the stored value. */ 12393 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value; 12394 /* Perform any access checks that were deferred. */ 12395 access_check = check_value->checks; 12396 if (access_check) 12397 { 12398 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk) 12399 perform_or_defer_access_check (chk->binfo, 12400 chk->decl, 12401 chk->diag_decl); 12402 } 12403 /* Return the stored value. */ 12404 return check_value->value; 12405 } 12406 12407 /* Avoid performing name lookup if there is no possibility of 12408 finding a template-id. */ 12409 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR) 12410 || (next_token->type == CPP_NAME 12411 && !cp_parser_nth_token_starts_template_argument_list_p 12412 (parser, 2))) 12413 { 12414 cp_parser_error (parser, "expected template-id"); 12415 return error_mark_node; 12416 } 12417 12418 /* Remember where the template-id starts. */ 12419 if (cp_parser_uncommitted_to_tentative_parse_p (parser)) 12420 start_of_id = cp_lexer_token_position (parser->lexer, false); 12421 12422 push_deferring_access_checks (dk_deferred); 12423 12424 /* Parse the template-name. */ 12425 is_identifier = false; 12426 templ = cp_parser_template_name (parser, template_keyword_p, 12427 check_dependency_p, 12428 is_declaration, 12429 &is_identifier); 12430 if (templ == error_mark_node || is_identifier) 12431 { 12432 pop_deferring_access_checks (); 12433 return templ; 12434 } 12435 12436 /* If we find the sequence `[:' after a template-name, it's probably 12437 a digraph-typo for `< ::'. Substitute the tokens and check if we can 12438 parse correctly the argument list. */ 12439 next_token = cp_lexer_peek_token (parser->lexer); 12440 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2); 12441 if (next_token->type == CPP_OPEN_SQUARE 12442 && next_token->flags & DIGRAPH 12443 && next_token_2->type == CPP_COLON 12444 && !(next_token_2->flags & PREV_WHITE)) 12445 { 12446 cp_parser_parse_tentatively (parser); 12447 /* Change `:' into `::'. */ 12448 next_token_2->type = CPP_SCOPE; 12449 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is 12450 CPP_LESS. */ 12451 cp_lexer_consume_token (parser->lexer); 12452 12453 /* Parse the arguments. */ 12454 arguments = cp_parser_enclosed_template_argument_list (parser); 12455 if (!cp_parser_parse_definitely (parser)) 12456 { 12457 /* If we couldn't parse an argument list, then we revert our changes 12458 and return simply an error. Maybe this is not a template-id 12459 after all. */ 12460 next_token_2->type = CPP_COLON; 12461 cp_parser_error (parser, "expected %<<%>"); 12462 pop_deferring_access_checks (); 12463 return error_mark_node; 12464 } 12465 /* Otherwise, emit an error about the invalid digraph, but continue 12466 parsing because we got our argument list. */ 12467 if (permerror (next_token->location, 12468 "%<<::%> cannot begin a template-argument list")) 12469 { 12470 static bool hint = false; 12471 inform (next_token->location, 12472 "%<<:%> is an alternate spelling for %<[%>." 12473 " Insert whitespace between %<<%> and %<::%>"); 12474 if (!hint && !flag_permissive) 12475 { 12476 inform (next_token->location, "(if you use %<-fpermissive%>" 12477 " G++ will accept your code)"); 12478 hint = true; 12479 } 12480 } 12481 } 12482 else 12483 { 12484 /* Look for the `<' that starts the template-argument-list. */ 12485 if (!cp_parser_require (parser, CPP_LESS, RT_LESS)) 12486 { 12487 pop_deferring_access_checks (); 12488 return error_mark_node; 12489 } 12490 /* Parse the arguments. */ 12491 arguments = cp_parser_enclosed_template_argument_list (parser); 12492 } 12493 12494 /* Build a representation of the specialization. */ 12495 if (TREE_CODE (templ) == IDENTIFIER_NODE) 12496 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments); 12497 else if (DECL_TYPE_TEMPLATE_P (templ) 12498 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ)) 12499 { 12500 bool entering_scope; 12501 /* In "template <typename T> ... A<T>::", A<T> is the abstract A 12502 template (rather than some instantiation thereof) only if 12503 is not nested within some other construct. For example, in 12504 "template <typename T> void f(T) { A<T>::", A<T> is just an 12505 instantiation of A. */ 12506 entering_scope = (template_parm_scope_p () 12507 && cp_lexer_next_token_is (parser->lexer, 12508 CPP_SCOPE)); 12509 template_id 12510 = finish_template_type (templ, arguments, entering_scope); 12511 } 12512 else 12513 { 12514 /* If it's not a class-template or a template-template, it should be 12515 a function-template. */ 12516 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ) 12517 || TREE_CODE (templ) == OVERLOAD 12518 || BASELINK_P (templ))); 12519 12520 template_id = lookup_template_function (templ, arguments); 12521 } 12522 12523 /* If parsing tentatively, replace the sequence of tokens that makes 12524 up the template-id with a CPP_TEMPLATE_ID token. That way, 12525 should we re-parse the token stream, we will not have to repeat 12526 the effort required to do the parse, nor will we issue duplicate 12527 error messages about problems during instantiation of the 12528 template. */ 12529 if (start_of_id) 12530 { 12531 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id); 12532 12533 /* Reset the contents of the START_OF_ID token. */ 12534 token->type = CPP_TEMPLATE_ID; 12535 /* Retrieve any deferred checks. Do not pop this access checks yet 12536 so the memory will not be reclaimed during token replacing below. */ 12537 token->u.tree_check_value = ggc_alloc_cleared_tree_check (); 12538 token->u.tree_check_value->value = template_id; 12539 token->u.tree_check_value->checks = get_deferred_access_checks (); 12540 token->keyword = RID_MAX; 12541 12542 /* Purge all subsequent tokens. */ 12543 cp_lexer_purge_tokens_after (parser->lexer, start_of_id); 12544 12545 /* ??? Can we actually assume that, if template_id == 12546 error_mark_node, we will have issued a diagnostic to the 12547 user, as opposed to simply marking the tentative parse as 12548 failed? */ 12549 if (cp_parser_error_occurred (parser) && template_id != error_mark_node) 12550 error_at (token->location, "parse error in template argument list"); 12551 } 12552 12553 pop_deferring_access_checks (); 12554 return template_id; 12555 } 12556 12557 /* Parse a template-name. 12558 12559 template-name: 12560 identifier 12561 12562 The standard should actually say: 12563 12564 template-name: 12565 identifier 12566 operator-function-id 12567 12568 A defect report has been filed about this issue. 12569 12570 A conversion-function-id cannot be a template name because they cannot 12571 be part of a template-id. In fact, looking at this code: 12572 12573 a.operator K<int>() 12574 12575 the conversion-function-id is "operator K<int>", and K<int> is a type-id. 12576 It is impossible to call a templated conversion-function-id with an 12577 explicit argument list, since the only allowed template parameter is 12578 the type to which it is converting. 12579 12580 If TEMPLATE_KEYWORD_P is true, then we have just seen the 12581 `template' keyword, in a construction like: 12582 12583 T::template f<3>() 12584 12585 In that case `f' is taken to be a template-name, even though there 12586 is no way of knowing for sure. 12587 12588 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the 12589 name refers to a set of overloaded functions, at least one of which 12590 is a template, or an IDENTIFIER_NODE with the name of the template, 12591 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE, 12592 names are looked up inside uninstantiated templates. */ 12593 12594 static tree 12595 cp_parser_template_name (cp_parser* parser, 12596 bool template_keyword_p, 12597 bool check_dependency_p, 12598 bool is_declaration, 12599 bool *is_identifier) 12600 { 12601 tree identifier; 12602 tree decl; 12603 tree fns; 12604 cp_token *token = cp_lexer_peek_token (parser->lexer); 12605 12606 /* If the next token is `operator', then we have either an 12607 operator-function-id or a conversion-function-id. */ 12608 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR)) 12609 { 12610 /* We don't know whether we're looking at an 12611 operator-function-id or a conversion-function-id. */ 12612 cp_parser_parse_tentatively (parser); 12613 /* Try an operator-function-id. */ 12614 identifier = cp_parser_operator_function_id (parser); 12615 /* If that didn't work, try a conversion-function-id. */ 12616 if (!cp_parser_parse_definitely (parser)) 12617 { 12618 cp_parser_error (parser, "expected template-name"); 12619 return error_mark_node; 12620 } 12621 } 12622 /* Look for the identifier. */ 12623 else 12624 identifier = cp_parser_identifier (parser); 12625 12626 /* If we didn't find an identifier, we don't have a template-id. */ 12627 if (identifier == error_mark_node) 12628 return error_mark_node; 12629 12630 /* If the name immediately followed the `template' keyword, then it 12631 is a template-name. However, if the next token is not `<', then 12632 we do not treat it as a template-name, since it is not being used 12633 as part of a template-id. This enables us to handle constructs 12634 like: 12635 12636 template <typename T> struct S { S(); }; 12637 template <typename T> S<T>::S(); 12638 12639 correctly. We would treat `S' as a template -- if it were `S<T>' 12640 -- but we do not if there is no `<'. */ 12641 12642 if (processing_template_decl 12643 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)) 12644 { 12645 /* In a declaration, in a dependent context, we pretend that the 12646 "template" keyword was present in order to improve error 12647 recovery. For example, given: 12648 12649 template <typename T> void f(T::X<int>); 12650 12651 we want to treat "X<int>" as a template-id. */ 12652 if (is_declaration 12653 && !template_keyword_p 12654 && parser->scope && TYPE_P (parser->scope) 12655 && check_dependency_p 12656 && dependent_scope_p (parser->scope) 12657 /* Do not do this for dtors (or ctors), since they never 12658 need the template keyword before their name. */ 12659 && !constructor_name_p (identifier, parser->scope)) 12660 { 12661 cp_token_position start = 0; 12662 12663 /* Explain what went wrong. */ 12664 error_at (token->location, "non-template %qD used as template", 12665 identifier); 12666 inform (token->location, "use %<%T::template %D%> to indicate that it is a template", 12667 parser->scope, identifier); 12668 /* If parsing tentatively, find the location of the "<" token. */ 12669 if (cp_parser_simulate_error (parser)) 12670 start = cp_lexer_token_position (parser->lexer, true); 12671 /* Parse the template arguments so that we can issue error 12672 messages about them. */ 12673 cp_lexer_consume_token (parser->lexer); 12674 cp_parser_enclosed_template_argument_list (parser); 12675 /* Skip tokens until we find a good place from which to 12676 continue parsing. */ 12677 cp_parser_skip_to_closing_parenthesis (parser, 12678 /*recovering=*/true, 12679 /*or_comma=*/true, 12680 /*consume_paren=*/false); 12681 /* If parsing tentatively, permanently remove the 12682 template argument list. That will prevent duplicate 12683 error messages from being issued about the missing 12684 "template" keyword. */ 12685 if (start) 12686 cp_lexer_purge_tokens_after (parser->lexer, start); 12687 if (is_identifier) 12688 *is_identifier = true; 12689 return identifier; 12690 } 12691 12692 /* If the "template" keyword is present, then there is generally 12693 no point in doing name-lookup, so we just return IDENTIFIER. 12694 But, if the qualifying scope is non-dependent then we can 12695 (and must) do name-lookup normally. */ 12696 if (template_keyword_p 12697 && (!parser->scope 12698 || (TYPE_P (parser->scope) 12699 && dependent_type_p (parser->scope)))) 12700 return identifier; 12701 } 12702 12703 /* Look up the name. */ 12704 decl = cp_parser_lookup_name (parser, identifier, 12705 none_type, 12706 /*is_template=*/true, 12707 /*is_namespace=*/false, 12708 check_dependency_p, 12709 /*ambiguous_decls=*/NULL, 12710 token->location); 12711 12712 /* If DECL is a template, then the name was a template-name. */ 12713 if (TREE_CODE (decl) == TEMPLATE_DECL) 12714 ; 12715 else 12716 { 12717 tree fn = NULL_TREE; 12718 12719 /* The standard does not explicitly indicate whether a name that 12720 names a set of overloaded declarations, some of which are 12721 templates, is a template-name. However, such a name should 12722 be a template-name; otherwise, there is no way to form a 12723 template-id for the overloaded templates. */ 12724 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl; 12725 if (TREE_CODE (fns) == OVERLOAD) 12726 for (fn = fns; fn; fn = OVL_NEXT (fn)) 12727 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL) 12728 break; 12729 12730 if (!fn) 12731 { 12732 /* The name does not name a template. */ 12733 cp_parser_error (parser, "expected template-name"); 12734 return error_mark_node; 12735 } 12736 } 12737 12738 /* If DECL is dependent, and refers to a function, then just return 12739 its name; we will look it up again during template instantiation. */ 12740 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl)) 12741 { 12742 tree scope = ovl_scope (decl); 12743 if (TYPE_P (scope) && dependent_type_p (scope)) 12744 return identifier; 12745 } 12746 12747 return decl; 12748 } 12749 12750 /* Parse a template-argument-list. 12751 12752 template-argument-list: 12753 template-argument ... [opt] 12754 template-argument-list , template-argument ... [opt] 12755 12756 Returns a TREE_VEC containing the arguments. */ 12757 12758 static tree 12759 cp_parser_template_argument_list (cp_parser* parser) 12760 { 12761 tree fixed_args[10]; 12762 unsigned n_args = 0; 12763 unsigned alloced = 10; 12764 tree *arg_ary = fixed_args; 12765 tree vec; 12766 bool saved_in_template_argument_list_p; 12767 bool saved_ice_p; 12768 bool saved_non_ice_p; 12769 12770 saved_in_template_argument_list_p = parser->in_template_argument_list_p; 12771 parser->in_template_argument_list_p = true; 12772 /* Even if the template-id appears in an integral 12773 constant-expression, the contents of the argument list do 12774 not. */ 12775 saved_ice_p = parser->integral_constant_expression_p; 12776 parser->integral_constant_expression_p = false; 12777 saved_non_ice_p = parser->non_integral_constant_expression_p; 12778 parser->non_integral_constant_expression_p = false; 12779 12780 /* Parse the arguments. */ 12781 do 12782 { 12783 tree argument; 12784 12785 if (n_args) 12786 /* Consume the comma. */ 12787 cp_lexer_consume_token (parser->lexer); 12788 12789 /* Parse the template-argument. */ 12790 argument = cp_parser_template_argument (parser); 12791 12792 /* If the next token is an ellipsis, we're expanding a template 12793 argument pack. */ 12794 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 12795 { 12796 if (argument == error_mark_node) 12797 { 12798 cp_token *token = cp_lexer_peek_token (parser->lexer); 12799 error_at (token->location, 12800 "expected parameter pack before %<...%>"); 12801 } 12802 /* Consume the `...' token. */ 12803 cp_lexer_consume_token (parser->lexer); 12804 12805 /* Make the argument into a TYPE_PACK_EXPANSION or 12806 EXPR_PACK_EXPANSION. */ 12807 argument = make_pack_expansion (argument); 12808 } 12809 12810 if (n_args == alloced) 12811 { 12812 alloced *= 2; 12813 12814 if (arg_ary == fixed_args) 12815 { 12816 arg_ary = XNEWVEC (tree, alloced); 12817 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args); 12818 } 12819 else 12820 arg_ary = XRESIZEVEC (tree, arg_ary, alloced); 12821 } 12822 arg_ary[n_args++] = argument; 12823 } 12824 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); 12825 12826 vec = make_tree_vec (n_args); 12827 12828 while (n_args--) 12829 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args]; 12830 12831 if (arg_ary != fixed_args) 12832 free (arg_ary); 12833 parser->non_integral_constant_expression_p = saved_non_ice_p; 12834 parser->integral_constant_expression_p = saved_ice_p; 12835 parser->in_template_argument_list_p = saved_in_template_argument_list_p; 12836 #ifdef ENABLE_CHECKING 12837 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec)); 12838 #endif 12839 return vec; 12840 } 12841 12842 /* Parse a template-argument. 12843 12844 template-argument: 12845 assignment-expression 12846 type-id 12847 id-expression 12848 12849 The representation is that of an assignment-expression, type-id, or 12850 id-expression -- except that the qualified id-expression is 12851 evaluated, so that the value returned is either a DECL or an 12852 OVERLOAD. 12853 12854 Although the standard says "assignment-expression", it forbids 12855 throw-expressions or assignments in the template argument. 12856 Therefore, we use "conditional-expression" instead. */ 12857 12858 static tree 12859 cp_parser_template_argument (cp_parser* parser) 12860 { 12861 tree argument; 12862 bool template_p; 12863 bool address_p; 12864 bool maybe_type_id = false; 12865 cp_token *token = NULL, *argument_start_token = NULL; 12866 cp_id_kind idk; 12867 12868 /* There's really no way to know what we're looking at, so we just 12869 try each alternative in order. 12870 12871 [temp.arg] 12872 12873 In a template-argument, an ambiguity between a type-id and an 12874 expression is resolved to a type-id, regardless of the form of 12875 the corresponding template-parameter. 12876 12877 Therefore, we try a type-id first. */ 12878 cp_parser_parse_tentatively (parser); 12879 argument = cp_parser_template_type_arg (parser); 12880 /* If there was no error parsing the type-id but the next token is a 12881 '>>', our behavior depends on which dialect of C++ we're 12882 parsing. In C++98, we probably found a typo for '> >'. But there 12883 are type-id which are also valid expressions. For instance: 12884 12885 struct X { int operator >> (int); }; 12886 template <int V> struct Foo {}; 12887 Foo<X () >> 5> r; 12888 12889 Here 'X()' is a valid type-id of a function type, but the user just 12890 wanted to write the expression "X() >> 5". Thus, we remember that we 12891 found a valid type-id, but we still try to parse the argument as an 12892 expression to see what happens. 12893 12894 In C++0x, the '>>' will be considered two separate '>' 12895 tokens. */ 12896 if (!cp_parser_error_occurred (parser) 12897 && cxx_dialect == cxx98 12898 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) 12899 { 12900 maybe_type_id = true; 12901 cp_parser_abort_tentative_parse (parser); 12902 } 12903 else 12904 { 12905 /* If the next token isn't a `,' or a `>', then this argument wasn't 12906 really finished. This means that the argument is not a valid 12907 type-id. */ 12908 if (!cp_parser_next_token_ends_template_argument_p (parser)) 12909 cp_parser_error (parser, "expected template-argument"); 12910 /* If that worked, we're done. */ 12911 if (cp_parser_parse_definitely (parser)) 12912 return argument; 12913 } 12914 /* We're still not sure what the argument will be. */ 12915 cp_parser_parse_tentatively (parser); 12916 /* Try a template. */ 12917 argument_start_token = cp_lexer_peek_token (parser->lexer); 12918 argument = cp_parser_id_expression (parser, 12919 /*template_keyword_p=*/false, 12920 /*check_dependency_p=*/true, 12921 &template_p, 12922 /*declarator_p=*/false, 12923 /*optional_p=*/false); 12924 /* If the next token isn't a `,' or a `>', then this argument wasn't 12925 really finished. */ 12926 if (!cp_parser_next_token_ends_template_argument_p (parser)) 12927 cp_parser_error (parser, "expected template-argument"); 12928 if (!cp_parser_error_occurred (parser)) 12929 { 12930 /* Figure out what is being referred to. If the id-expression 12931 was for a class template specialization, then we will have a 12932 TYPE_DECL at this point. There is no need to do name lookup 12933 at this point in that case. */ 12934 if (TREE_CODE (argument) != TYPE_DECL) 12935 argument = cp_parser_lookup_name (parser, argument, 12936 none_type, 12937 /*is_template=*/template_p, 12938 /*is_namespace=*/false, 12939 /*check_dependency=*/true, 12940 /*ambiguous_decls=*/NULL, 12941 argument_start_token->location); 12942 if (TREE_CODE (argument) != TEMPLATE_DECL 12943 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE) 12944 cp_parser_error (parser, "expected template-name"); 12945 } 12946 if (cp_parser_parse_definitely (parser)) 12947 return argument; 12948 /* It must be a non-type argument. There permitted cases are given 12949 in [temp.arg.nontype]: 12950 12951 -- an integral constant-expression of integral or enumeration 12952 type; or 12953 12954 -- the name of a non-type template-parameter; or 12955 12956 -- the name of an object or function with external linkage... 12957 12958 -- the address of an object or function with external linkage... 12959 12960 -- a pointer to member... */ 12961 /* Look for a non-type template parameter. */ 12962 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 12963 { 12964 cp_parser_parse_tentatively (parser); 12965 argument = cp_parser_primary_expression (parser, 12966 /*address_p=*/false, 12967 /*cast_p=*/false, 12968 /*template_arg_p=*/true, 12969 &idk); 12970 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX 12971 || !cp_parser_next_token_ends_template_argument_p (parser)) 12972 cp_parser_simulate_error (parser); 12973 if (cp_parser_parse_definitely (parser)) 12974 return argument; 12975 } 12976 12977 /* If the next token is "&", the argument must be the address of an 12978 object or function with external linkage. */ 12979 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND); 12980 if (address_p) 12981 cp_lexer_consume_token (parser->lexer); 12982 /* See if we might have an id-expression. */ 12983 token = cp_lexer_peek_token (parser->lexer); 12984 if (token->type == CPP_NAME 12985 || token->keyword == RID_OPERATOR 12986 || token->type == CPP_SCOPE 12987 || token->type == CPP_TEMPLATE_ID 12988 || token->type == CPP_NESTED_NAME_SPECIFIER) 12989 { 12990 cp_parser_parse_tentatively (parser); 12991 argument = cp_parser_primary_expression (parser, 12992 address_p, 12993 /*cast_p=*/false, 12994 /*template_arg_p=*/true, 12995 &idk); 12996 if (cp_parser_error_occurred (parser) 12997 || !cp_parser_next_token_ends_template_argument_p (parser)) 12998 cp_parser_abort_tentative_parse (parser); 12999 else 13000 { 13001 tree probe; 13002 13003 if (TREE_CODE (argument) == INDIRECT_REF) 13004 { 13005 gcc_assert (REFERENCE_REF_P (argument)); 13006 argument = TREE_OPERAND (argument, 0); 13007 } 13008 13009 /* If we're in a template, we represent a qualified-id referring 13010 to a static data member as a SCOPE_REF even if the scope isn't 13011 dependent so that we can check access control later. */ 13012 probe = argument; 13013 if (TREE_CODE (probe) == SCOPE_REF) 13014 probe = TREE_OPERAND (probe, 1); 13015 if (TREE_CODE (probe) == VAR_DECL) 13016 { 13017 /* A variable without external linkage might still be a 13018 valid constant-expression, so no error is issued here 13019 if the external-linkage check fails. */ 13020 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe)) 13021 cp_parser_simulate_error (parser); 13022 } 13023 else if (is_overloaded_fn (argument)) 13024 /* All overloaded functions are allowed; if the external 13025 linkage test does not pass, an error will be issued 13026 later. */ 13027 ; 13028 else if (address_p 13029 && (TREE_CODE (argument) == OFFSET_REF 13030 || TREE_CODE (argument) == SCOPE_REF)) 13031 /* A pointer-to-member. */ 13032 ; 13033 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX) 13034 ; 13035 else 13036 cp_parser_simulate_error (parser); 13037 13038 if (cp_parser_parse_definitely (parser)) 13039 { 13040 if (address_p) 13041 argument = build_x_unary_op (ADDR_EXPR, argument, 13042 tf_warning_or_error); 13043 return argument; 13044 } 13045 } 13046 } 13047 /* If the argument started with "&", there are no other valid 13048 alternatives at this point. */ 13049 if (address_p) 13050 { 13051 cp_parser_error (parser, "invalid non-type template argument"); 13052 return error_mark_node; 13053 } 13054 13055 /* If the argument wasn't successfully parsed as a type-id followed 13056 by '>>', the argument can only be a constant expression now. 13057 Otherwise, we try parsing the constant-expression tentatively, 13058 because the argument could really be a type-id. */ 13059 if (maybe_type_id) 13060 cp_parser_parse_tentatively (parser); 13061 argument = cp_parser_constant_expression (parser, 13062 /*allow_non_constant_p=*/false, 13063 /*non_constant_p=*/NULL); 13064 argument = fold_non_dependent_expr (argument); 13065 if (!maybe_type_id) 13066 return argument; 13067 if (!cp_parser_next_token_ends_template_argument_p (parser)) 13068 cp_parser_error (parser, "expected template-argument"); 13069 if (cp_parser_parse_definitely (parser)) 13070 return argument; 13071 /* We did our best to parse the argument as a non type-id, but that 13072 was the only alternative that matched (albeit with a '>' after 13073 it). We can assume it's just a typo from the user, and a 13074 diagnostic will then be issued. */ 13075 return cp_parser_template_type_arg (parser); 13076 } 13077 13078 /* Parse an explicit-instantiation. 13079 13080 explicit-instantiation: 13081 template declaration 13082 13083 Although the standard says `declaration', what it really means is: 13084 13085 explicit-instantiation: 13086 template decl-specifier-seq [opt] declarator [opt] ; 13087 13088 Things like `template int S<int>::i = 5, int S<double>::j;' are not 13089 supposed to be allowed. A defect report has been filed about this 13090 issue. 13091 13092 GNU Extension: 13093 13094 explicit-instantiation: 13095 storage-class-specifier template 13096 decl-specifier-seq [opt] declarator [opt] ; 13097 function-specifier template 13098 decl-specifier-seq [opt] declarator [opt] ; */ 13099 13100 static void 13101 cp_parser_explicit_instantiation (cp_parser* parser) 13102 { 13103 int declares_class_or_enum; 13104 cp_decl_specifier_seq decl_specifiers; 13105 tree extension_specifier = NULL_TREE; 13106 13107 timevar_push (TV_TEMPLATE_INST); 13108 13109 /* Look for an (optional) storage-class-specifier or 13110 function-specifier. */ 13111 if (cp_parser_allow_gnu_extensions_p (parser)) 13112 { 13113 extension_specifier 13114 = cp_parser_storage_class_specifier_opt (parser); 13115 if (!extension_specifier) 13116 extension_specifier 13117 = cp_parser_function_specifier_opt (parser, 13118 /*decl_specs=*/NULL); 13119 } 13120 13121 /* Look for the `template' keyword. */ 13122 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE); 13123 /* Let the front end know that we are processing an explicit 13124 instantiation. */ 13125 begin_explicit_instantiation (); 13126 /* [temp.explicit] says that we are supposed to ignore access 13127 control while processing explicit instantiation directives. */ 13128 push_deferring_access_checks (dk_no_check); 13129 /* Parse a decl-specifier-seq. */ 13130 cp_parser_decl_specifier_seq (parser, 13131 CP_PARSER_FLAGS_OPTIONAL, 13132 &decl_specifiers, 13133 &declares_class_or_enum); 13134 /* If there was exactly one decl-specifier, and it declared a class, 13135 and there's no declarator, then we have an explicit type 13136 instantiation. */ 13137 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser)) 13138 { 13139 tree type; 13140 13141 type = check_tag_decl (&decl_specifiers); 13142 /* Turn access control back on for names used during 13143 template instantiation. */ 13144 pop_deferring_access_checks (); 13145 if (type) 13146 do_type_instantiation (type, extension_specifier, 13147 /*complain=*/tf_error); 13148 } 13149 else 13150 { 13151 cp_declarator *declarator; 13152 tree decl; 13153 13154 /* Parse the declarator. */ 13155 declarator 13156 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 13157 /*ctor_dtor_or_conv_p=*/NULL, 13158 /*parenthesized_p=*/NULL, 13159 /*member_p=*/false); 13160 if (declares_class_or_enum & 2) 13161 cp_parser_check_for_definition_in_return_type (declarator, 13162 decl_specifiers.type, 13163 decl_specifiers.type_location); 13164 if (declarator != cp_error_declarator) 13165 { 13166 if (decl_specifiers.specs[(int)ds_inline]) 13167 permerror (input_location, "explicit instantiation shall not use" 13168 " %<inline%> specifier"); 13169 if (decl_specifiers.specs[(int)ds_constexpr]) 13170 permerror (input_location, "explicit instantiation shall not use" 13171 " %<constexpr%> specifier"); 13172 13173 decl = grokdeclarator (declarator, &decl_specifiers, 13174 NORMAL, 0, &decl_specifiers.attributes); 13175 /* Turn access control back on for names used during 13176 template instantiation. */ 13177 pop_deferring_access_checks (); 13178 /* Do the explicit instantiation. */ 13179 do_decl_instantiation (decl, extension_specifier); 13180 } 13181 else 13182 { 13183 pop_deferring_access_checks (); 13184 /* Skip the body of the explicit instantiation. */ 13185 cp_parser_skip_to_end_of_statement (parser); 13186 } 13187 } 13188 /* We're done with the instantiation. */ 13189 end_explicit_instantiation (); 13190 13191 cp_parser_consume_semicolon_at_end_of_statement (parser); 13192 13193 timevar_pop (TV_TEMPLATE_INST); 13194 } 13195 13196 /* Parse an explicit-specialization. 13197 13198 explicit-specialization: 13199 template < > declaration 13200 13201 Although the standard says `declaration', what it really means is: 13202 13203 explicit-specialization: 13204 template <> decl-specifier [opt] init-declarator [opt] ; 13205 template <> function-definition 13206 template <> explicit-specialization 13207 template <> template-declaration */ 13208 13209 static void 13210 cp_parser_explicit_specialization (cp_parser* parser) 13211 { 13212 bool need_lang_pop; 13213 cp_token *token = cp_lexer_peek_token (parser->lexer); 13214 13215 /* Look for the `template' keyword. */ 13216 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE); 13217 /* Look for the `<'. */ 13218 cp_parser_require (parser, CPP_LESS, RT_LESS); 13219 /* Look for the `>'. */ 13220 cp_parser_require (parser, CPP_GREATER, RT_GREATER); 13221 /* We have processed another parameter list. */ 13222 ++parser->num_template_parameter_lists; 13223 /* [temp] 13224 13225 A template ... explicit specialization ... shall not have C 13226 linkage. */ 13227 if (current_lang_name == lang_name_c) 13228 { 13229 error_at (token->location, "template specialization with C linkage"); 13230 /* Give it C++ linkage to avoid confusing other parts of the 13231 front end. */ 13232 push_lang_context (lang_name_cplusplus); 13233 need_lang_pop = true; 13234 } 13235 else 13236 need_lang_pop = false; 13237 /* Let the front end know that we are beginning a specialization. */ 13238 if (!begin_specialization ()) 13239 { 13240 end_specialization (); 13241 return; 13242 } 13243 13244 /* If the next keyword is `template', we need to figure out whether 13245 or not we're looking a template-declaration. */ 13246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) 13247 { 13248 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS 13249 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER) 13250 cp_parser_template_declaration_after_export (parser, 13251 /*member_p=*/false); 13252 else 13253 cp_parser_explicit_specialization (parser); 13254 } 13255 else 13256 /* Parse the dependent declaration. */ 13257 cp_parser_single_declaration (parser, 13258 /*checks=*/NULL, 13259 /*member_p=*/false, 13260 /*explicit_specialization_p=*/true, 13261 /*friend_p=*/NULL); 13262 /* We're done with the specialization. */ 13263 end_specialization (); 13264 /* For the erroneous case of a template with C linkage, we pushed an 13265 implicit C++ linkage scope; exit that scope now. */ 13266 if (need_lang_pop) 13267 pop_lang_context (); 13268 /* We're done with this parameter list. */ 13269 --parser->num_template_parameter_lists; 13270 } 13271 13272 /* Parse a type-specifier. 13273 13274 type-specifier: 13275 simple-type-specifier 13276 class-specifier 13277 enum-specifier 13278 elaborated-type-specifier 13279 cv-qualifier 13280 13281 GNU Extension: 13282 13283 type-specifier: 13284 __complex__ 13285 13286 Returns a representation of the type-specifier. For a 13287 class-specifier, enum-specifier, or elaborated-type-specifier, a 13288 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned. 13289 13290 The parser flags FLAGS is used to control type-specifier parsing. 13291 13292 If IS_DECLARATION is TRUE, then this type-specifier is appearing 13293 in a decl-specifier-seq. 13294 13295 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a 13296 class-specifier, enum-specifier, or elaborated-type-specifier, then 13297 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1 13298 if a type is declared; 2 if it is defined. Otherwise, it is set to 13299 zero. 13300 13301 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a 13302 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it 13303 is set to FALSE. */ 13304 13305 static tree 13306 cp_parser_type_specifier (cp_parser* parser, 13307 cp_parser_flags flags, 13308 cp_decl_specifier_seq *decl_specs, 13309 bool is_declaration, 13310 int* declares_class_or_enum, 13311 bool* is_cv_qualifier) 13312 { 13313 tree type_spec = NULL_TREE; 13314 cp_token *token; 13315 enum rid keyword; 13316 cp_decl_spec ds = ds_last; 13317 13318 /* Assume this type-specifier does not declare a new type. */ 13319 if (declares_class_or_enum) 13320 *declares_class_or_enum = 0; 13321 /* And that it does not specify a cv-qualifier. */ 13322 if (is_cv_qualifier) 13323 *is_cv_qualifier = false; 13324 /* Peek at the next token. */ 13325 token = cp_lexer_peek_token (parser->lexer); 13326 13327 /* If we're looking at a keyword, we can use that to guide the 13328 production we choose. */ 13329 keyword = token->keyword; 13330 switch (keyword) 13331 { 13332 case RID_ENUM: 13333 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS)) 13334 goto elaborated_type_specifier; 13335 13336 /* Look for the enum-specifier. */ 13337 type_spec = cp_parser_enum_specifier (parser); 13338 /* If that worked, we're done. */ 13339 if (type_spec) 13340 { 13341 if (declares_class_or_enum) 13342 *declares_class_or_enum = 2; 13343 if (decl_specs) 13344 cp_parser_set_decl_spec_type (decl_specs, 13345 type_spec, 13346 token->location, 13347 /*type_definition_p=*/true); 13348 return type_spec; 13349 } 13350 else 13351 goto elaborated_type_specifier; 13352 13353 /* Any of these indicate either a class-specifier, or an 13354 elaborated-type-specifier. */ 13355 case RID_CLASS: 13356 case RID_STRUCT: 13357 case RID_UNION: 13358 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS)) 13359 goto elaborated_type_specifier; 13360 13361 /* Parse tentatively so that we can back up if we don't find a 13362 class-specifier. */ 13363 cp_parser_parse_tentatively (parser); 13364 /* Look for the class-specifier. */ 13365 type_spec = cp_parser_class_specifier (parser); 13366 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec); 13367 /* If that worked, we're done. */ 13368 if (cp_parser_parse_definitely (parser)) 13369 { 13370 if (declares_class_or_enum) 13371 *declares_class_or_enum = 2; 13372 if (decl_specs) 13373 cp_parser_set_decl_spec_type (decl_specs, 13374 type_spec, 13375 token->location, 13376 /*type_definition_p=*/true); 13377 return type_spec; 13378 } 13379 13380 /* Fall through. */ 13381 elaborated_type_specifier: 13382 /* We're declaring (not defining) a class or enum. */ 13383 if (declares_class_or_enum) 13384 *declares_class_or_enum = 1; 13385 13386 /* Fall through. */ 13387 case RID_TYPENAME: 13388 /* Look for an elaborated-type-specifier. */ 13389 type_spec 13390 = (cp_parser_elaborated_type_specifier 13391 (parser, 13392 decl_specs && decl_specs->specs[(int) ds_friend], 13393 is_declaration)); 13394 if (decl_specs) 13395 cp_parser_set_decl_spec_type (decl_specs, 13396 type_spec, 13397 token->location, 13398 /*type_definition_p=*/false); 13399 return type_spec; 13400 13401 case RID_CONST: 13402 ds = ds_const; 13403 if (is_cv_qualifier) 13404 *is_cv_qualifier = true; 13405 break; 13406 13407 case RID_VOLATILE: 13408 ds = ds_volatile; 13409 if (is_cv_qualifier) 13410 *is_cv_qualifier = true; 13411 break; 13412 13413 case RID_RESTRICT: 13414 ds = ds_restrict; 13415 if (is_cv_qualifier) 13416 *is_cv_qualifier = true; 13417 break; 13418 13419 case RID_COMPLEX: 13420 /* The `__complex__' keyword is a GNU extension. */ 13421 ds = ds_complex; 13422 break; 13423 13424 default: 13425 break; 13426 } 13427 13428 /* Handle simple keywords. */ 13429 if (ds != ds_last) 13430 { 13431 if (decl_specs) 13432 { 13433 ++decl_specs->specs[(int)ds]; 13434 decl_specs->any_specifiers_p = true; 13435 } 13436 return cp_lexer_consume_token (parser->lexer)->u.value; 13437 } 13438 13439 /* If we do not already have a type-specifier, assume we are looking 13440 at a simple-type-specifier. */ 13441 type_spec = cp_parser_simple_type_specifier (parser, 13442 decl_specs, 13443 flags); 13444 13445 /* If we didn't find a type-specifier, and a type-specifier was not 13446 optional in this context, issue an error message. */ 13447 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL)) 13448 { 13449 cp_parser_error (parser, "expected type specifier"); 13450 return error_mark_node; 13451 } 13452 13453 return type_spec; 13454 } 13455 13456 /* Parse a simple-type-specifier. 13457 13458 simple-type-specifier: 13459 :: [opt] nested-name-specifier [opt] type-name 13460 :: [opt] nested-name-specifier template template-id 13461 char 13462 wchar_t 13463 bool 13464 short 13465 int 13466 long 13467 signed 13468 unsigned 13469 float 13470 double 13471 void 13472 13473 C++0x Extension: 13474 13475 simple-type-specifier: 13476 auto 13477 decltype ( expression ) 13478 char16_t 13479 char32_t 13480 __underlying_type ( type-id ) 13481 13482 GNU Extension: 13483 13484 simple-type-specifier: 13485 __int128 13486 __typeof__ unary-expression 13487 __typeof__ ( type-id ) 13488 13489 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is 13490 appropriately updated. */ 13491 13492 static tree 13493 cp_parser_simple_type_specifier (cp_parser* parser, 13494 cp_decl_specifier_seq *decl_specs, 13495 cp_parser_flags flags) 13496 { 13497 tree type = NULL_TREE; 13498 cp_token *token; 13499 13500 /* Peek at the next token. */ 13501 token = cp_lexer_peek_token (parser->lexer); 13502 13503 /* If we're looking at a keyword, things are easy. */ 13504 switch (token->keyword) 13505 { 13506 case RID_CHAR: 13507 if (decl_specs) 13508 decl_specs->explicit_char_p = true; 13509 type = char_type_node; 13510 break; 13511 case RID_CHAR16: 13512 type = char16_type_node; 13513 break; 13514 case RID_CHAR32: 13515 type = char32_type_node; 13516 break; 13517 case RID_WCHAR: 13518 type = wchar_type_node; 13519 break; 13520 case RID_BOOL: 13521 type = boolean_type_node; 13522 break; 13523 case RID_SHORT: 13524 if (decl_specs) 13525 ++decl_specs->specs[(int) ds_short]; 13526 type = short_integer_type_node; 13527 break; 13528 case RID_INT: 13529 if (decl_specs) 13530 decl_specs->explicit_int_p = true; 13531 type = integer_type_node; 13532 break; 13533 case RID_INT128: 13534 if (!int128_integer_type_node) 13535 break; 13536 if (decl_specs) 13537 decl_specs->explicit_int128_p = true; 13538 type = int128_integer_type_node; 13539 break; 13540 case RID_LONG: 13541 if (decl_specs) 13542 ++decl_specs->specs[(int) ds_long]; 13543 type = long_integer_type_node; 13544 break; 13545 case RID_SIGNED: 13546 if (decl_specs) 13547 ++decl_specs->specs[(int) ds_signed]; 13548 type = integer_type_node; 13549 break; 13550 case RID_UNSIGNED: 13551 if (decl_specs) 13552 ++decl_specs->specs[(int) ds_unsigned]; 13553 type = unsigned_type_node; 13554 break; 13555 case RID_FLOAT: 13556 type = float_type_node; 13557 break; 13558 case RID_DOUBLE: 13559 type = double_type_node; 13560 break; 13561 case RID_VOID: 13562 type = void_type_node; 13563 break; 13564 13565 case RID_AUTO: 13566 maybe_warn_cpp0x (CPP0X_AUTO); 13567 type = make_auto (); 13568 break; 13569 13570 case RID_DECLTYPE: 13571 /* Since DR 743, decltype can either be a simple-type-specifier by 13572 itself or begin a nested-name-specifier. Parsing it will replace 13573 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE 13574 handling below decide what to do. */ 13575 cp_parser_decltype (parser); 13576 cp_lexer_set_token_position (parser->lexer, token); 13577 break; 13578 13579 case RID_TYPEOF: 13580 /* Consume the `typeof' token. */ 13581 cp_lexer_consume_token (parser->lexer); 13582 /* Parse the operand to `typeof'. */ 13583 type = cp_parser_sizeof_operand (parser, RID_TYPEOF); 13584 /* If it is not already a TYPE, take its type. */ 13585 if (!TYPE_P (type)) 13586 type = finish_typeof (type); 13587 13588 if (decl_specs) 13589 cp_parser_set_decl_spec_type (decl_specs, type, 13590 token->location, 13591 /*type_definition_p=*/false); 13592 13593 return type; 13594 13595 case RID_UNDERLYING_TYPE: 13596 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE); 13597 if (decl_specs) 13598 cp_parser_set_decl_spec_type (decl_specs, type, 13599 token->location, 13600 /*type_definition_p=*/false); 13601 13602 return type; 13603 13604 case RID_BASES: 13605 case RID_DIRECT_BASES: 13606 type = cp_parser_trait_expr (parser, token->keyword); 13607 if (decl_specs) 13608 cp_parser_set_decl_spec_type (decl_specs, type, 13609 token->location, 13610 /*type_definition_p=*/false); 13611 return type; 13612 default: 13613 break; 13614 } 13615 13616 /* If token is an already-parsed decltype not followed by ::, 13617 it's a simple-type-specifier. */ 13618 if (token->type == CPP_DECLTYPE 13619 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE) 13620 { 13621 type = token->u.value; 13622 if (decl_specs) 13623 cp_parser_set_decl_spec_type (decl_specs, type, 13624 token->location, 13625 /*type_definition_p=*/false); 13626 cp_lexer_consume_token (parser->lexer); 13627 return type; 13628 } 13629 13630 /* If the type-specifier was for a built-in type, we're done. */ 13631 if (type) 13632 { 13633 /* Record the type. */ 13634 if (decl_specs 13635 && (token->keyword != RID_SIGNED 13636 && token->keyword != RID_UNSIGNED 13637 && token->keyword != RID_SHORT 13638 && token->keyword != RID_LONG)) 13639 cp_parser_set_decl_spec_type (decl_specs, 13640 type, 13641 token->location, 13642 /*type_definition_p=*/false); 13643 if (decl_specs) 13644 decl_specs->any_specifiers_p = true; 13645 13646 /* Consume the token. */ 13647 cp_lexer_consume_token (parser->lexer); 13648 13649 /* There is no valid C++ program where a non-template type is 13650 followed by a "<". That usually indicates that the user thought 13651 that the type was a template. */ 13652 cp_parser_check_for_invalid_template_id (parser, type, token->location); 13653 13654 return TYPE_NAME (type); 13655 } 13656 13657 /* The type-specifier must be a user-defined type. */ 13658 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES)) 13659 { 13660 bool qualified_p; 13661 bool global_p; 13662 13663 /* Don't gobble tokens or issue error messages if this is an 13664 optional type-specifier. */ 13665 if (flags & CP_PARSER_FLAGS_OPTIONAL) 13666 cp_parser_parse_tentatively (parser); 13667 13668 /* Look for the optional `::' operator. */ 13669 global_p 13670 = (cp_parser_global_scope_opt (parser, 13671 /*current_scope_valid_p=*/false) 13672 != NULL_TREE); 13673 /* Look for the nested-name specifier. */ 13674 qualified_p 13675 = (cp_parser_nested_name_specifier_opt (parser, 13676 /*typename_keyword_p=*/false, 13677 /*check_dependency_p=*/true, 13678 /*type_p=*/false, 13679 /*is_declaration=*/false) 13680 != NULL_TREE); 13681 token = cp_lexer_peek_token (parser->lexer); 13682 /* If we have seen a nested-name-specifier, and the next token 13683 is `template', then we are using the template-id production. */ 13684 if (parser->scope 13685 && cp_parser_optional_template_keyword (parser)) 13686 { 13687 /* Look for the template-id. */ 13688 type = cp_parser_template_id (parser, 13689 /*template_keyword_p=*/true, 13690 /*check_dependency_p=*/true, 13691 /*is_declaration=*/false); 13692 /* If the template-id did not name a type, we are out of 13693 luck. */ 13694 if (TREE_CODE (type) != TYPE_DECL) 13695 { 13696 cp_parser_error (parser, "expected template-id for type"); 13697 type = NULL_TREE; 13698 } 13699 } 13700 /* Otherwise, look for a type-name. */ 13701 else 13702 type = cp_parser_type_name (parser); 13703 /* Keep track of all name-lookups performed in class scopes. */ 13704 if (type 13705 && !global_p 13706 && !qualified_p 13707 && TREE_CODE (type) == TYPE_DECL 13708 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE) 13709 maybe_note_name_used_in_class (DECL_NAME (type), type); 13710 /* If it didn't work out, we don't have a TYPE. */ 13711 if ((flags & CP_PARSER_FLAGS_OPTIONAL) 13712 && !cp_parser_parse_definitely (parser)) 13713 type = NULL_TREE; 13714 if (type && decl_specs) 13715 cp_parser_set_decl_spec_type (decl_specs, type, 13716 token->location, 13717 /*type_definition_p=*/false); 13718 } 13719 13720 /* If we didn't get a type-name, issue an error message. */ 13721 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL)) 13722 { 13723 cp_parser_error (parser, "expected type-name"); 13724 return error_mark_node; 13725 } 13726 13727 if (type && type != error_mark_node) 13728 { 13729 /* See if TYPE is an Objective-C type, and if so, parse and 13730 accept any protocol references following it. Do this before 13731 the cp_parser_check_for_invalid_template_id() call, because 13732 Objective-C types can be followed by '<...>' which would 13733 enclose protocol names rather than template arguments, and so 13734 everything is fine. */ 13735 if (c_dialect_objc () && !parser->scope 13736 && (objc_is_id (type) || objc_is_class_name (type))) 13737 { 13738 tree protos = cp_parser_objc_protocol_refs_opt (parser); 13739 tree qual_type = objc_get_protocol_qualified_type (type, protos); 13740 13741 /* Clobber the "unqualified" type previously entered into 13742 DECL_SPECS with the new, improved protocol-qualified version. */ 13743 if (decl_specs) 13744 decl_specs->type = qual_type; 13745 13746 return qual_type; 13747 } 13748 13749 /* There is no valid C++ program where a non-template type is 13750 followed by a "<". That usually indicates that the user 13751 thought that the type was a template. */ 13752 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type), 13753 token->location); 13754 } 13755 13756 return type; 13757 } 13758 13759 /* Parse a type-name. 13760 13761 type-name: 13762 class-name 13763 enum-name 13764 typedef-name 13765 simple-template-id [in c++0x] 13766 13767 enum-name: 13768 identifier 13769 13770 typedef-name: 13771 identifier 13772 13773 Returns a TYPE_DECL for the type. */ 13774 13775 static tree 13776 cp_parser_type_name (cp_parser* parser) 13777 { 13778 tree type_decl; 13779 13780 /* We can't know yet whether it is a class-name or not. */ 13781 cp_parser_parse_tentatively (parser); 13782 /* Try a class-name. */ 13783 type_decl = cp_parser_class_name (parser, 13784 /*typename_keyword_p=*/false, 13785 /*template_keyword_p=*/false, 13786 none_type, 13787 /*check_dependency_p=*/true, 13788 /*class_head_p=*/false, 13789 /*is_declaration=*/false); 13790 /* If it's not a class-name, keep looking. */ 13791 if (!cp_parser_parse_definitely (parser)) 13792 { 13793 if (cxx_dialect < cxx0x) 13794 /* It must be a typedef-name or an enum-name. */ 13795 return cp_parser_nonclass_name (parser); 13796 13797 cp_parser_parse_tentatively (parser); 13798 /* It is either a simple-template-id representing an 13799 instantiation of an alias template... */ 13800 type_decl = cp_parser_template_id (parser, 13801 /*template_keyword_p=*/false, 13802 /*check_dependency_p=*/false, 13803 /*is_declaration=*/false); 13804 /* Note that this must be an instantiation of an alias template 13805 because [temp.names]/6 says: 13806 13807 A template-id that names an alias template specialization 13808 is a type-name. 13809 13810 Whereas [temp.names]/7 says: 13811 13812 A simple-template-id that names a class template 13813 specialization is a class-name. */ 13814 if (type_decl != NULL_TREE 13815 && TREE_CODE (type_decl) == TYPE_DECL 13816 && TYPE_DECL_ALIAS_P (type_decl)) 13817 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl)); 13818 else 13819 cp_parser_simulate_error (parser); 13820 13821 if (!cp_parser_parse_definitely (parser)) 13822 /* ... Or a typedef-name or an enum-name. */ 13823 return cp_parser_nonclass_name (parser); 13824 } 13825 13826 return type_decl; 13827 } 13828 13829 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name. 13830 13831 enum-name: 13832 identifier 13833 13834 typedef-name: 13835 identifier 13836 13837 Returns a TYPE_DECL for the type. */ 13838 13839 static tree 13840 cp_parser_nonclass_name (cp_parser* parser) 13841 { 13842 tree type_decl; 13843 tree identifier; 13844 13845 cp_token *token = cp_lexer_peek_token (parser->lexer); 13846 identifier = cp_parser_identifier (parser); 13847 if (identifier == error_mark_node) 13848 return error_mark_node; 13849 13850 /* Look up the type-name. */ 13851 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location); 13852 13853 if (TREE_CODE (type_decl) == USING_DECL) 13854 { 13855 if (!DECL_DEPENDENT_P (type_decl)) 13856 type_decl = strip_using_decl (type_decl); 13857 else if (USING_DECL_TYPENAME_P (type_decl)) 13858 { 13859 /* We have found a type introduced by a using 13860 declaration at class scope that refers to a dependent 13861 type. 13862 13863 using typename :: [opt] nested-name-specifier unqualified-id ; 13864 */ 13865 type_decl = make_typename_type (TREE_TYPE (type_decl), 13866 DECL_NAME (type_decl), 13867 typename_type, tf_error); 13868 if (type_decl != error_mark_node) 13869 type_decl = TYPE_NAME (type_decl); 13870 } 13871 } 13872 13873 if (TREE_CODE (type_decl) != TYPE_DECL 13874 && (objc_is_id (identifier) || objc_is_class_name (identifier))) 13875 { 13876 /* See if this is an Objective-C type. */ 13877 tree protos = cp_parser_objc_protocol_refs_opt (parser); 13878 tree type = objc_get_protocol_qualified_type (identifier, protos); 13879 if (type) 13880 type_decl = TYPE_NAME (type); 13881 } 13882 13883 /* Issue an error if we did not find a type-name. */ 13884 if (TREE_CODE (type_decl) != TYPE_DECL 13885 /* In Objective-C, we have the complication that class names are 13886 normally type names and start declarations (eg, the 13887 "NSObject" in "NSObject *object;"), but can be used in an 13888 Objective-C 2.0 dot-syntax (as in "NSObject.version") which 13889 is an expression. So, a classname followed by a dot is not a 13890 valid type-name. */ 13891 || (objc_is_class_name (TREE_TYPE (type_decl)) 13892 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT)) 13893 { 13894 if (!cp_parser_simulate_error (parser)) 13895 cp_parser_name_lookup_error (parser, identifier, type_decl, 13896 NLE_TYPE, token->location); 13897 return error_mark_node; 13898 } 13899 /* Remember that the name was used in the definition of the 13900 current class so that we can check later to see if the 13901 meaning would have been different after the class was 13902 entirely defined. */ 13903 else if (type_decl != error_mark_node 13904 && !parser->scope) 13905 maybe_note_name_used_in_class (identifier, type_decl); 13906 13907 return type_decl; 13908 } 13909 13910 /* Parse an elaborated-type-specifier. Note that the grammar given 13911 here incorporates the resolution to DR68. 13912 13913 elaborated-type-specifier: 13914 class-key :: [opt] nested-name-specifier [opt] identifier 13915 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id 13916 enum-key :: [opt] nested-name-specifier [opt] identifier 13917 typename :: [opt] nested-name-specifier identifier 13918 typename :: [opt] nested-name-specifier template [opt] 13919 template-id 13920 13921 GNU extension: 13922 13923 elaborated-type-specifier: 13924 class-key attributes :: [opt] nested-name-specifier [opt] identifier 13925 class-key attributes :: [opt] nested-name-specifier [opt] 13926 template [opt] template-id 13927 enum attributes :: [opt] nested-name-specifier [opt] identifier 13928 13929 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being 13930 declared `friend'. If IS_DECLARATION is TRUE, then this 13931 elaborated-type-specifier appears in a decl-specifiers-seq, i.e., 13932 something is being declared. 13933 13934 Returns the TYPE specified. */ 13935 13936 static tree 13937 cp_parser_elaborated_type_specifier (cp_parser* parser, 13938 bool is_friend, 13939 bool is_declaration) 13940 { 13941 enum tag_types tag_type; 13942 tree identifier; 13943 tree type = NULL_TREE; 13944 tree attributes = NULL_TREE; 13945 tree globalscope; 13946 cp_token *token = NULL; 13947 13948 /* See if we're looking at the `enum' keyword. */ 13949 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM)) 13950 { 13951 /* Consume the `enum' token. */ 13952 cp_lexer_consume_token (parser->lexer); 13953 /* Remember that it's an enumeration type. */ 13954 tag_type = enum_type; 13955 /* Issue a warning if the `struct' or `class' key (for C++0x scoped 13956 enums) is used here. */ 13957 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS) 13958 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT)) 13959 { 13960 pedwarn (input_location, 0, "elaborated-type-specifier " 13961 "for a scoped enum must not use the %<%D%> keyword", 13962 cp_lexer_peek_token (parser->lexer)->u.value); 13963 /* Consume the `struct' or `class' and parse it anyway. */ 13964 cp_lexer_consume_token (parser->lexer); 13965 } 13966 /* Parse the attributes. */ 13967 attributes = cp_parser_attributes_opt (parser); 13968 } 13969 /* Or, it might be `typename'. */ 13970 else if (cp_lexer_next_token_is_keyword (parser->lexer, 13971 RID_TYPENAME)) 13972 { 13973 /* Consume the `typename' token. */ 13974 cp_lexer_consume_token (parser->lexer); 13975 /* Remember that it's a `typename' type. */ 13976 tag_type = typename_type; 13977 } 13978 /* Otherwise it must be a class-key. */ 13979 else 13980 { 13981 tag_type = cp_parser_class_key (parser); 13982 if (tag_type == none_type) 13983 return error_mark_node; 13984 /* Parse the attributes. */ 13985 attributes = cp_parser_attributes_opt (parser); 13986 } 13987 13988 /* Look for the `::' operator. */ 13989 globalscope = cp_parser_global_scope_opt (parser, 13990 /*current_scope_valid_p=*/false); 13991 /* Look for the nested-name-specifier. */ 13992 if (tag_type == typename_type && !globalscope) 13993 { 13994 if (!cp_parser_nested_name_specifier (parser, 13995 /*typename_keyword_p=*/true, 13996 /*check_dependency_p=*/true, 13997 /*type_p=*/true, 13998 is_declaration)) 13999 return error_mark_node; 14000 } 14001 else 14002 /* Even though `typename' is not present, the proposed resolution 14003 to Core Issue 180 says that in `class A<T>::B', `B' should be 14004 considered a type-name, even if `A<T>' is dependent. */ 14005 cp_parser_nested_name_specifier_opt (parser, 14006 /*typename_keyword_p=*/true, 14007 /*check_dependency_p=*/true, 14008 /*type_p=*/true, 14009 is_declaration); 14010 /* For everything but enumeration types, consider a template-id. 14011 For an enumeration type, consider only a plain identifier. */ 14012 if (tag_type != enum_type) 14013 { 14014 bool template_p = false; 14015 tree decl; 14016 14017 /* Allow the `template' keyword. */ 14018 template_p = cp_parser_optional_template_keyword (parser); 14019 /* If we didn't see `template', we don't know if there's a 14020 template-id or not. */ 14021 if (!template_p) 14022 cp_parser_parse_tentatively (parser); 14023 /* Parse the template-id. */ 14024 token = cp_lexer_peek_token (parser->lexer); 14025 decl = cp_parser_template_id (parser, template_p, 14026 /*check_dependency_p=*/true, 14027 is_declaration); 14028 /* If we didn't find a template-id, look for an ordinary 14029 identifier. */ 14030 if (!template_p && !cp_parser_parse_definitely (parser)) 14031 ; 14032 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is 14033 in effect, then we must assume that, upon instantiation, the 14034 template will correspond to a class. */ 14035 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 14036 && tag_type == typename_type) 14037 type = make_typename_type (parser->scope, decl, 14038 typename_type, 14039 /*complain=*/tf_error); 14040 /* If the `typename' keyword is in effect and DECL is not a type 14041 decl, then type is non existent. */ 14042 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL) 14043 ; 14044 else if (TREE_CODE (decl) == TYPE_DECL) 14045 type = check_elaborated_type_specifier (tag_type, decl, 14046 /*allow_template_p=*/true); 14047 else if (decl == error_mark_node) 14048 type = error_mark_node; 14049 } 14050 14051 if (!type) 14052 { 14053 token = cp_lexer_peek_token (parser->lexer); 14054 identifier = cp_parser_identifier (parser); 14055 14056 if (identifier == error_mark_node) 14057 { 14058 parser->scope = NULL_TREE; 14059 return error_mark_node; 14060 } 14061 14062 /* For a `typename', we needn't call xref_tag. */ 14063 if (tag_type == typename_type 14064 && TREE_CODE (parser->scope) != NAMESPACE_DECL) 14065 return cp_parser_make_typename_type (parser, parser->scope, 14066 identifier, 14067 token->location); 14068 /* Look up a qualified name in the usual way. */ 14069 if (parser->scope) 14070 { 14071 tree decl; 14072 tree ambiguous_decls; 14073 14074 decl = cp_parser_lookup_name (parser, identifier, 14075 tag_type, 14076 /*is_template=*/false, 14077 /*is_namespace=*/false, 14078 /*check_dependency=*/true, 14079 &ambiguous_decls, 14080 token->location); 14081 14082 /* If the lookup was ambiguous, an error will already have been 14083 issued. */ 14084 if (ambiguous_decls) 14085 return error_mark_node; 14086 14087 /* If we are parsing friend declaration, DECL may be a 14088 TEMPLATE_DECL tree node here. However, we need to check 14089 whether this TEMPLATE_DECL results in valid code. Consider 14090 the following example: 14091 14092 namespace N { 14093 template <class T> class C {}; 14094 } 14095 class X { 14096 template <class T> friend class N::C; // #1, valid code 14097 }; 14098 template <class T> class Y { 14099 friend class N::C; // #2, invalid code 14100 }; 14101 14102 For both case #1 and #2, we arrive at a TEMPLATE_DECL after 14103 name lookup of `N::C'. We see that friend declaration must 14104 be template for the code to be valid. Note that 14105 processing_template_decl does not work here since it is 14106 always 1 for the above two cases. */ 14107 14108 decl = (cp_parser_maybe_treat_template_as_class 14109 (decl, /*tag_name_p=*/is_friend 14110 && parser->num_template_parameter_lists)); 14111 14112 if (TREE_CODE (decl) != TYPE_DECL) 14113 { 14114 cp_parser_diagnose_invalid_type_name (parser, 14115 parser->scope, 14116 identifier, 14117 token->location); 14118 return error_mark_node; 14119 } 14120 14121 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE) 14122 { 14123 bool allow_template = (parser->num_template_parameter_lists 14124 || DECL_SELF_REFERENCE_P (decl)); 14125 type = check_elaborated_type_specifier (tag_type, decl, 14126 allow_template); 14127 14128 if (type == error_mark_node) 14129 return error_mark_node; 14130 } 14131 14132 /* Forward declarations of nested types, such as 14133 14134 class C1::C2; 14135 class C1::C2::C3; 14136 14137 are invalid unless all components preceding the final '::' 14138 are complete. If all enclosing types are complete, these 14139 declarations become merely pointless. 14140 14141 Invalid forward declarations of nested types are errors 14142 caught elsewhere in parsing. Those that are pointless arrive 14143 here. */ 14144 14145 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 14146 && !is_friend && !processing_explicit_instantiation) 14147 warning (0, "declaration %qD does not declare anything", decl); 14148 14149 type = TREE_TYPE (decl); 14150 } 14151 else 14152 { 14153 /* An elaborated-type-specifier sometimes introduces a new type and 14154 sometimes names an existing type. Normally, the rule is that it 14155 introduces a new type only if there is not an existing type of 14156 the same name already in scope. For example, given: 14157 14158 struct S {}; 14159 void f() { struct S s; } 14160 14161 the `struct S' in the body of `f' is the same `struct S' as in 14162 the global scope; the existing definition is used. However, if 14163 there were no global declaration, this would introduce a new 14164 local class named `S'. 14165 14166 An exception to this rule applies to the following code: 14167 14168 namespace N { struct S; } 14169 14170 Here, the elaborated-type-specifier names a new type 14171 unconditionally; even if there is already an `S' in the 14172 containing scope this declaration names a new type. 14173 This exception only applies if the elaborated-type-specifier 14174 forms the complete declaration: 14175 14176 [class.name] 14177 14178 A declaration consisting solely of `class-key identifier ;' is 14179 either a redeclaration of the name in the current scope or a 14180 forward declaration of the identifier as a class name. It 14181 introduces the name into the current scope. 14182 14183 We are in this situation precisely when the next token is a `;'. 14184 14185 An exception to the exception is that a `friend' declaration does 14186 *not* name a new type; i.e., given: 14187 14188 struct S { friend struct T; }; 14189 14190 `T' is not a new type in the scope of `S'. 14191 14192 Also, `new struct S' or `sizeof (struct S)' never results in the 14193 definition of a new type; a new type can only be declared in a 14194 declaration context. */ 14195 14196 tag_scope ts; 14197 bool template_p; 14198 14199 if (is_friend) 14200 /* Friends have special name lookup rules. */ 14201 ts = ts_within_enclosing_non_class; 14202 else if (is_declaration 14203 && cp_lexer_next_token_is (parser->lexer, 14204 CPP_SEMICOLON)) 14205 /* This is a `class-key identifier ;' */ 14206 ts = ts_current; 14207 else 14208 ts = ts_global; 14209 14210 template_p = 14211 (parser->num_template_parameter_lists 14212 && (cp_parser_next_token_starts_class_definition_p (parser) 14213 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))); 14214 /* An unqualified name was used to reference this type, so 14215 there were no qualifying templates. */ 14216 if (!cp_parser_check_template_parameters (parser, 14217 /*num_templates=*/0, 14218 token->location, 14219 /*declarator=*/NULL)) 14220 return error_mark_node; 14221 type = xref_tag (tag_type, identifier, ts, template_p); 14222 } 14223 } 14224 14225 if (type == error_mark_node) 14226 return error_mark_node; 14227 14228 /* Allow attributes on forward declarations of classes. */ 14229 if (attributes) 14230 { 14231 if (TREE_CODE (type) == TYPENAME_TYPE) 14232 warning (OPT_Wattributes, 14233 "attributes ignored on uninstantiated type"); 14234 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type) 14235 && ! processing_explicit_instantiation) 14236 warning (OPT_Wattributes, 14237 "attributes ignored on template instantiation"); 14238 else if (is_declaration && cp_parser_declares_only_class_p (parser)) 14239 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 14240 else 14241 warning (OPT_Wattributes, 14242 "attributes ignored on elaborated-type-specifier that is not a forward declaration"); 14243 } 14244 14245 if (tag_type != enum_type) 14246 { 14247 /* Indicate whether this class was declared as a `class' or as a 14248 `struct'. */ 14249 if (TREE_CODE (type) == RECORD_TYPE) 14250 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type); 14251 cp_parser_check_class_key (tag_type, type); 14252 } 14253 14254 /* A "<" cannot follow an elaborated type specifier. If that 14255 happens, the user was probably trying to form a template-id. */ 14256 cp_parser_check_for_invalid_template_id (parser, type, token->location); 14257 14258 return type; 14259 } 14260 14261 /* Parse an enum-specifier. 14262 14263 enum-specifier: 14264 enum-head { enumerator-list [opt] } 14265 enum-head { enumerator-list , } [C++0x] 14266 14267 enum-head: 14268 enum-key identifier [opt] enum-base [opt] 14269 enum-key nested-name-specifier identifier enum-base [opt] 14270 14271 enum-key: 14272 enum 14273 enum class [C++0x] 14274 enum struct [C++0x] 14275 14276 enum-base: [C++0x] 14277 : type-specifier-seq 14278 14279 opaque-enum-specifier: 14280 enum-key identifier enum-base [opt] ; 14281 14282 GNU Extensions: 14283 enum-key attributes[opt] identifier [opt] enum-base [opt] 14284 { enumerator-list [opt] }attributes[opt] 14285 enum-key attributes[opt] identifier [opt] enum-base [opt] 14286 { enumerator-list, }attributes[opt] [C++0x] 14287 14288 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE 14289 if the token stream isn't an enum-specifier after all. */ 14290 14291 static tree 14292 cp_parser_enum_specifier (cp_parser* parser) 14293 { 14294 tree identifier; 14295 tree type = NULL_TREE; 14296 tree prev_scope; 14297 tree nested_name_specifier = NULL_TREE; 14298 tree attributes; 14299 bool scoped_enum_p = false; 14300 bool has_underlying_type = false; 14301 bool nested_being_defined = false; 14302 bool new_value_list = false; 14303 bool is_new_type = false; 14304 bool is_anonymous = false; 14305 tree underlying_type = NULL_TREE; 14306 cp_token *type_start_token = NULL; 14307 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 14308 14309 parser->colon_corrects_to_scope_p = false; 14310 14311 /* Parse tentatively so that we can back up if we don't find a 14312 enum-specifier. */ 14313 cp_parser_parse_tentatively (parser); 14314 14315 /* Caller guarantees that the current token is 'enum', an identifier 14316 possibly follows, and the token after that is an opening brace. 14317 If we don't have an identifier, fabricate an anonymous name for 14318 the enumeration being defined. */ 14319 cp_lexer_consume_token (parser->lexer); 14320 14321 /* Parse the "class" or "struct", which indicates a scoped 14322 enumeration type in C++0x. */ 14323 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS) 14324 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT)) 14325 { 14326 if (cxx_dialect < cxx0x) 14327 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS); 14328 14329 /* Consume the `struct' or `class' token. */ 14330 cp_lexer_consume_token (parser->lexer); 14331 14332 scoped_enum_p = true; 14333 } 14334 14335 attributes = cp_parser_attributes_opt (parser); 14336 14337 /* Clear the qualification. */ 14338 parser->scope = NULL_TREE; 14339 parser->qualifying_scope = NULL_TREE; 14340 parser->object_scope = NULL_TREE; 14341 14342 /* Figure out in what scope the declaration is being placed. */ 14343 prev_scope = current_scope (); 14344 14345 type_start_token = cp_lexer_peek_token (parser->lexer); 14346 14347 push_deferring_access_checks (dk_no_check); 14348 nested_name_specifier 14349 = cp_parser_nested_name_specifier_opt (parser, 14350 /*typename_keyword_p=*/true, 14351 /*check_dependency_p=*/false, 14352 /*type_p=*/false, 14353 /*is_declaration=*/false); 14354 14355 if (nested_name_specifier) 14356 { 14357 tree name; 14358 14359 identifier = cp_parser_identifier (parser); 14360 name = cp_parser_lookup_name (parser, identifier, 14361 enum_type, 14362 /*is_template=*/false, 14363 /*is_namespace=*/false, 14364 /*check_dependency=*/true, 14365 /*ambiguous_decls=*/NULL, 14366 input_location); 14367 if (name) 14368 { 14369 type = TREE_TYPE (name); 14370 if (TREE_CODE (type) == TYPENAME_TYPE) 14371 { 14372 /* Are template enums allowed in ISO? */ 14373 if (template_parm_scope_p ()) 14374 pedwarn (type_start_token->location, OPT_pedantic, 14375 "%qD is an enumeration template", name); 14376 /* ignore a typename reference, for it will be solved by name 14377 in start_enum. */ 14378 type = NULL_TREE; 14379 } 14380 } 14381 else 14382 error_at (type_start_token->location, 14383 "%qD is not an enumerator-name", identifier); 14384 } 14385 else 14386 { 14387 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 14388 identifier = cp_parser_identifier (parser); 14389 else 14390 { 14391 identifier = make_anon_name (); 14392 is_anonymous = true; 14393 } 14394 } 14395 pop_deferring_access_checks (); 14396 14397 /* Check for the `:' that denotes a specified underlying type in C++0x. 14398 Note that a ':' could also indicate a bitfield width, however. */ 14399 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 14400 { 14401 cp_decl_specifier_seq type_specifiers; 14402 14403 /* Consume the `:'. */ 14404 cp_lexer_consume_token (parser->lexer); 14405 14406 /* Parse the type-specifier-seq. */ 14407 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false, 14408 /*is_trailing_return=*/false, 14409 &type_specifiers); 14410 14411 /* At this point this is surely not elaborated type specifier. */ 14412 if (!cp_parser_parse_definitely (parser)) 14413 return NULL_TREE; 14414 14415 if (cxx_dialect < cxx0x) 14416 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS); 14417 14418 has_underlying_type = true; 14419 14420 /* If that didn't work, stop. */ 14421 if (type_specifiers.type != error_mark_node) 14422 { 14423 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME, 14424 /*initialized=*/0, NULL); 14425 if (underlying_type == error_mark_node) 14426 underlying_type = NULL_TREE; 14427 } 14428 } 14429 14430 /* Look for the `{' but don't consume it yet. */ 14431 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 14432 { 14433 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type)) 14434 { 14435 cp_parser_error (parser, "expected %<{%>"); 14436 if (has_underlying_type) 14437 { 14438 type = NULL_TREE; 14439 goto out; 14440 } 14441 } 14442 /* An opaque-enum-specifier must have a ';' here. */ 14443 if ((scoped_enum_p || underlying_type) 14444 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 14445 { 14446 cp_parser_error (parser, "expected %<;%> or %<{%>"); 14447 if (has_underlying_type) 14448 { 14449 type = NULL_TREE; 14450 goto out; 14451 } 14452 } 14453 } 14454 14455 if (!has_underlying_type && !cp_parser_parse_definitely (parser)) 14456 return NULL_TREE; 14457 14458 if (nested_name_specifier) 14459 { 14460 if (CLASS_TYPE_P (nested_name_specifier)) 14461 { 14462 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier); 14463 TYPE_BEING_DEFINED (nested_name_specifier) = 1; 14464 push_scope (nested_name_specifier); 14465 } 14466 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL) 14467 { 14468 push_nested_namespace (nested_name_specifier); 14469 } 14470 } 14471 14472 /* Issue an error message if type-definitions are forbidden here. */ 14473 if (!cp_parser_check_type_definition (parser)) 14474 type = error_mark_node; 14475 else 14476 /* Create the new type. We do this before consuming the opening 14477 brace so the enum will be recorded as being on the line of its 14478 tag (or the 'enum' keyword, if there is no tag). */ 14479 type = start_enum (identifier, type, underlying_type, 14480 scoped_enum_p, &is_new_type); 14481 14482 /* If the next token is not '{' it is an opaque-enum-specifier or an 14483 elaborated-type-specifier. */ 14484 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 14485 { 14486 timevar_push (TV_PARSE_ENUM); 14487 if (nested_name_specifier) 14488 { 14489 /* The following catches invalid code such as: 14490 enum class S<int>::E { A, B, C }; */ 14491 if (!processing_specialization 14492 && CLASS_TYPE_P (nested_name_specifier) 14493 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier)) 14494 error_at (type_start_token->location, "cannot add an enumerator " 14495 "list to a template instantiation"); 14496 14497 /* If that scope does not contain the scope in which the 14498 class was originally declared, the program is invalid. */ 14499 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier)) 14500 { 14501 if (at_namespace_scope_p ()) 14502 error_at (type_start_token->location, 14503 "declaration of %qD in namespace %qD which does not " 14504 "enclose %qD", 14505 type, prev_scope, nested_name_specifier); 14506 else 14507 error_at (type_start_token->location, 14508 "declaration of %qD in %qD which does not enclose %qD", 14509 type, prev_scope, nested_name_specifier); 14510 type = error_mark_node; 14511 } 14512 } 14513 14514 if (scoped_enum_p) 14515 begin_scope (sk_scoped_enum, type); 14516 14517 /* Consume the opening brace. */ 14518 cp_lexer_consume_token (parser->lexer); 14519 14520 if (type == error_mark_node) 14521 ; /* Nothing to add */ 14522 else if (OPAQUE_ENUM_P (type) 14523 || (cxx_dialect > cxx98 && processing_specialization)) 14524 { 14525 new_value_list = true; 14526 SET_OPAQUE_ENUM_P (type, false); 14527 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location; 14528 } 14529 else 14530 { 14531 error_at (type_start_token->location, "multiple definition of %q#T", type); 14532 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), 14533 "previous definition here"); 14534 type = error_mark_node; 14535 } 14536 14537 if (type == error_mark_node) 14538 cp_parser_skip_to_end_of_block_or_statement (parser); 14539 /* If the next token is not '}', then there are some enumerators. */ 14540 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) 14541 cp_parser_enumerator_list (parser, type); 14542 14543 /* Consume the final '}'. */ 14544 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 14545 14546 if (scoped_enum_p) 14547 finish_scope (); 14548 timevar_pop (TV_PARSE_ENUM); 14549 } 14550 else 14551 { 14552 /* If a ';' follows, then it is an opaque-enum-specifier 14553 and additional restrictions apply. */ 14554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 14555 { 14556 if (is_anonymous) 14557 error_at (type_start_token->location, 14558 "opaque-enum-specifier without name"); 14559 else if (nested_name_specifier) 14560 error_at (type_start_token->location, 14561 "opaque-enum-specifier must use a simple identifier"); 14562 } 14563 } 14564 14565 /* Look for trailing attributes to apply to this enumeration, and 14566 apply them if appropriate. */ 14567 if (cp_parser_allow_gnu_extensions_p (parser)) 14568 { 14569 tree trailing_attr = cp_parser_attributes_opt (parser); 14570 trailing_attr = chainon (trailing_attr, attributes); 14571 cplus_decl_attributes (&type, 14572 trailing_attr, 14573 (int) ATTR_FLAG_TYPE_IN_PLACE); 14574 } 14575 14576 /* Finish up the enumeration. */ 14577 if (type != error_mark_node) 14578 { 14579 if (new_value_list) 14580 finish_enum_value_list (type); 14581 if (is_new_type) 14582 finish_enum (type); 14583 } 14584 14585 if (nested_name_specifier) 14586 { 14587 if (CLASS_TYPE_P (nested_name_specifier)) 14588 { 14589 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined; 14590 pop_scope (nested_name_specifier); 14591 } 14592 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL) 14593 { 14594 pop_nested_namespace (nested_name_specifier); 14595 } 14596 } 14597 out: 14598 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 14599 return type; 14600 } 14601 14602 /* Parse an enumerator-list. The enumerators all have the indicated 14603 TYPE. 14604 14605 enumerator-list: 14606 enumerator-definition 14607 enumerator-list , enumerator-definition */ 14608 14609 static void 14610 cp_parser_enumerator_list (cp_parser* parser, tree type) 14611 { 14612 while (true) 14613 { 14614 /* Parse an enumerator-definition. */ 14615 cp_parser_enumerator_definition (parser, type); 14616 14617 /* If the next token is not a ',', we've reached the end of 14618 the list. */ 14619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 14620 break; 14621 /* Otherwise, consume the `,' and keep going. */ 14622 cp_lexer_consume_token (parser->lexer); 14623 /* If the next token is a `}', there is a trailing comma. */ 14624 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 14625 { 14626 if (cxx_dialect < cxx0x && !in_system_header) 14627 pedwarn (input_location, OPT_pedantic, 14628 "comma at end of enumerator list"); 14629 break; 14630 } 14631 } 14632 } 14633 14634 /* Parse an enumerator-definition. The enumerator has the indicated 14635 TYPE. 14636 14637 enumerator-definition: 14638 enumerator 14639 enumerator = constant-expression 14640 14641 enumerator: 14642 identifier */ 14643 14644 static void 14645 cp_parser_enumerator_definition (cp_parser* parser, tree type) 14646 { 14647 tree identifier; 14648 tree value; 14649 location_t loc; 14650 14651 /* Save the input location because we are interested in the location 14652 of the identifier and not the location of the explicit value. */ 14653 loc = cp_lexer_peek_token (parser->lexer)->location; 14654 14655 /* Look for the identifier. */ 14656 identifier = cp_parser_identifier (parser); 14657 if (identifier == error_mark_node) 14658 return; 14659 14660 /* If the next token is an '=', then there is an explicit value. */ 14661 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 14662 { 14663 /* Consume the `=' token. */ 14664 cp_lexer_consume_token (parser->lexer); 14665 /* Parse the value. */ 14666 value = cp_parser_constant_expression (parser, 14667 /*allow_non_constant_p=*/false, 14668 NULL); 14669 } 14670 else 14671 value = NULL_TREE; 14672 14673 /* If we are processing a template, make sure the initializer of the 14674 enumerator doesn't contain any bare template parameter pack. */ 14675 if (check_for_bare_parameter_packs (value)) 14676 value = error_mark_node; 14677 14678 /* integral_constant_value will pull out this expression, so make sure 14679 it's folded as appropriate. */ 14680 value = fold_non_dependent_expr (value); 14681 14682 /* Create the enumerator. */ 14683 build_enumerator (identifier, value, type, loc); 14684 } 14685 14686 /* Parse a namespace-name. 14687 14688 namespace-name: 14689 original-namespace-name 14690 namespace-alias 14691 14692 Returns the NAMESPACE_DECL for the namespace. */ 14693 14694 static tree 14695 cp_parser_namespace_name (cp_parser* parser) 14696 { 14697 tree identifier; 14698 tree namespace_decl; 14699 14700 cp_token *token = cp_lexer_peek_token (parser->lexer); 14701 14702 /* Get the name of the namespace. */ 14703 identifier = cp_parser_identifier (parser); 14704 if (identifier == error_mark_node) 14705 return error_mark_node; 14706 14707 /* Look up the identifier in the currently active scope. Look only 14708 for namespaces, due to: 14709 14710 [basic.lookup.udir] 14711 14712 When looking up a namespace-name in a using-directive or alias 14713 definition, only namespace names are considered. 14714 14715 And: 14716 14717 [basic.lookup.qual] 14718 14719 During the lookup of a name preceding the :: scope resolution 14720 operator, object, function, and enumerator names are ignored. 14721 14722 (Note that cp_parser_qualifying_entity only calls this 14723 function if the token after the name is the scope resolution 14724 operator.) */ 14725 namespace_decl = cp_parser_lookup_name (parser, identifier, 14726 none_type, 14727 /*is_template=*/false, 14728 /*is_namespace=*/true, 14729 /*check_dependency=*/true, 14730 /*ambiguous_decls=*/NULL, 14731 token->location); 14732 /* If it's not a namespace, issue an error. */ 14733 if (namespace_decl == error_mark_node 14734 || TREE_CODE (namespace_decl) != NAMESPACE_DECL) 14735 { 14736 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) 14737 error_at (token->location, "%qD is not a namespace-name", identifier); 14738 cp_parser_error (parser, "expected namespace-name"); 14739 namespace_decl = error_mark_node; 14740 } 14741 14742 return namespace_decl; 14743 } 14744 14745 /* Parse a namespace-definition. 14746 14747 namespace-definition: 14748 named-namespace-definition 14749 unnamed-namespace-definition 14750 14751 named-namespace-definition: 14752 original-namespace-definition 14753 extension-namespace-definition 14754 14755 original-namespace-definition: 14756 namespace identifier { namespace-body } 14757 14758 extension-namespace-definition: 14759 namespace original-namespace-name { namespace-body } 14760 14761 unnamed-namespace-definition: 14762 namespace { namespace-body } */ 14763 14764 static void 14765 cp_parser_namespace_definition (cp_parser* parser) 14766 { 14767 tree identifier, attribs; 14768 bool has_visibility; 14769 bool is_inline; 14770 14771 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE)) 14772 { 14773 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES); 14774 is_inline = true; 14775 cp_lexer_consume_token (parser->lexer); 14776 } 14777 else 14778 is_inline = false; 14779 14780 /* Look for the `namespace' keyword. */ 14781 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE); 14782 14783 /* Get the name of the namespace. We do not attempt to distinguish 14784 between an original-namespace-definition and an 14785 extension-namespace-definition at this point. The semantic 14786 analysis routines are responsible for that. */ 14787 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 14788 identifier = cp_parser_identifier (parser); 14789 else 14790 identifier = NULL_TREE; 14791 14792 /* Parse any specified attributes. */ 14793 attribs = cp_parser_attributes_opt (parser); 14794 14795 /* Look for the `{' to start the namespace. */ 14796 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE); 14797 /* Start the namespace. */ 14798 push_namespace (identifier); 14799 14800 /* "inline namespace" is equivalent to a stub namespace definition 14801 followed by a strong using directive. */ 14802 if (is_inline) 14803 { 14804 tree name_space = current_namespace; 14805 /* Set up namespace association. */ 14806 DECL_NAMESPACE_ASSOCIATIONS (name_space) 14807 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE, 14808 DECL_NAMESPACE_ASSOCIATIONS (name_space)); 14809 /* Import the contents of the inline namespace. */ 14810 pop_namespace (); 14811 do_using_directive (name_space); 14812 push_namespace (identifier); 14813 } 14814 14815 has_visibility = handle_namespace_attrs (current_namespace, attribs); 14816 14817 /* Parse the body of the namespace. */ 14818 cp_parser_namespace_body (parser); 14819 14820 if (has_visibility) 14821 pop_visibility (1); 14822 14823 /* Finish the namespace. */ 14824 pop_namespace (); 14825 /* Look for the final `}'. */ 14826 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 14827 } 14828 14829 /* Parse a namespace-body. 14830 14831 namespace-body: 14832 declaration-seq [opt] */ 14833 14834 static void 14835 cp_parser_namespace_body (cp_parser* parser) 14836 { 14837 cp_parser_declaration_seq_opt (parser); 14838 } 14839 14840 /* Parse a namespace-alias-definition. 14841 14842 namespace-alias-definition: 14843 namespace identifier = qualified-namespace-specifier ; */ 14844 14845 static void 14846 cp_parser_namespace_alias_definition (cp_parser* parser) 14847 { 14848 tree identifier; 14849 tree namespace_specifier; 14850 14851 cp_token *token = cp_lexer_peek_token (parser->lexer); 14852 14853 /* Look for the `namespace' keyword. */ 14854 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE); 14855 /* Look for the identifier. */ 14856 identifier = cp_parser_identifier (parser); 14857 if (identifier == error_mark_node) 14858 return; 14859 /* Look for the `=' token. */ 14860 if (!cp_parser_uncommitted_to_tentative_parse_p (parser) 14861 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 14862 { 14863 error_at (token->location, "%<namespace%> definition is not allowed here"); 14864 /* Skip the definition. */ 14865 cp_lexer_consume_token (parser->lexer); 14866 if (cp_parser_skip_to_closing_brace (parser)) 14867 cp_lexer_consume_token (parser->lexer); 14868 return; 14869 } 14870 cp_parser_require (parser, CPP_EQ, RT_EQ); 14871 /* Look for the qualified-namespace-specifier. */ 14872 namespace_specifier 14873 = cp_parser_qualified_namespace_specifier (parser); 14874 /* Look for the `;' token. */ 14875 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 14876 14877 /* Register the alias in the symbol table. */ 14878 do_namespace_alias (identifier, namespace_specifier); 14879 } 14880 14881 /* Parse a qualified-namespace-specifier. 14882 14883 qualified-namespace-specifier: 14884 :: [opt] nested-name-specifier [opt] namespace-name 14885 14886 Returns a NAMESPACE_DECL corresponding to the specified 14887 namespace. */ 14888 14889 static tree 14890 cp_parser_qualified_namespace_specifier (cp_parser* parser) 14891 { 14892 /* Look for the optional `::'. */ 14893 cp_parser_global_scope_opt (parser, 14894 /*current_scope_valid_p=*/false); 14895 14896 /* Look for the optional nested-name-specifier. */ 14897 cp_parser_nested_name_specifier_opt (parser, 14898 /*typename_keyword_p=*/false, 14899 /*check_dependency_p=*/true, 14900 /*type_p=*/false, 14901 /*is_declaration=*/true); 14902 14903 return cp_parser_namespace_name (parser); 14904 } 14905 14906 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an 14907 access declaration. 14908 14909 using-declaration: 14910 using typename [opt] :: [opt] nested-name-specifier unqualified-id ; 14911 using :: unqualified-id ; 14912 14913 access-declaration: 14914 qualified-id ; 14915 14916 */ 14917 14918 static bool 14919 cp_parser_using_declaration (cp_parser* parser, 14920 bool access_declaration_p) 14921 { 14922 cp_token *token; 14923 bool typename_p = false; 14924 bool global_scope_p; 14925 tree decl; 14926 tree identifier; 14927 tree qscope; 14928 int oldcount = errorcount; 14929 cp_token *diag_token = NULL; 14930 14931 if (access_declaration_p) 14932 { 14933 diag_token = cp_lexer_peek_token (parser->lexer); 14934 cp_parser_parse_tentatively (parser); 14935 } 14936 else 14937 { 14938 /* Look for the `using' keyword. */ 14939 cp_parser_require_keyword (parser, RID_USING, RT_USING); 14940 14941 /* Peek at the next token. */ 14942 token = cp_lexer_peek_token (parser->lexer); 14943 /* See if it's `typename'. */ 14944 if (token->keyword == RID_TYPENAME) 14945 { 14946 /* Remember that we've seen it. */ 14947 typename_p = true; 14948 /* Consume the `typename' token. */ 14949 cp_lexer_consume_token (parser->lexer); 14950 } 14951 } 14952 14953 /* Look for the optional global scope qualification. */ 14954 global_scope_p 14955 = (cp_parser_global_scope_opt (parser, 14956 /*current_scope_valid_p=*/false) 14957 != NULL_TREE); 14958 14959 /* If we saw `typename', or didn't see `::', then there must be a 14960 nested-name-specifier present. */ 14961 if (typename_p || !global_scope_p) 14962 qscope = cp_parser_nested_name_specifier (parser, typename_p, 14963 /*check_dependency_p=*/true, 14964 /*type_p=*/false, 14965 /*is_declaration=*/true); 14966 /* Otherwise, we could be in either of the two productions. In that 14967 case, treat the nested-name-specifier as optional. */ 14968 else 14969 qscope = cp_parser_nested_name_specifier_opt (parser, 14970 /*typename_keyword_p=*/false, 14971 /*check_dependency_p=*/true, 14972 /*type_p=*/false, 14973 /*is_declaration=*/true); 14974 if (!qscope) 14975 qscope = global_namespace; 14976 14977 if (access_declaration_p && cp_parser_error_occurred (parser)) 14978 /* Something has already gone wrong; there's no need to parse 14979 further. Since an error has occurred, the return value of 14980 cp_parser_parse_definitely will be false, as required. */ 14981 return cp_parser_parse_definitely (parser); 14982 14983 token = cp_lexer_peek_token (parser->lexer); 14984 /* Parse the unqualified-id. */ 14985 identifier = cp_parser_unqualified_id (parser, 14986 /*template_keyword_p=*/false, 14987 /*check_dependency_p=*/true, 14988 /*declarator_p=*/true, 14989 /*optional_p=*/false); 14990 14991 if (access_declaration_p) 14992 { 14993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 14994 cp_parser_simulate_error (parser); 14995 if (!cp_parser_parse_definitely (parser)) 14996 return false; 14997 } 14998 14999 /* The function we call to handle a using-declaration is different 15000 depending on what scope we are in. */ 15001 if (qscope == error_mark_node || identifier == error_mark_node) 15002 ; 15003 else if (TREE_CODE (identifier) != IDENTIFIER_NODE 15004 && TREE_CODE (identifier) != BIT_NOT_EXPR) 15005 /* [namespace.udecl] 15006 15007 A using declaration shall not name a template-id. */ 15008 error_at (token->location, 15009 "a template-id may not appear in a using-declaration"); 15010 else 15011 { 15012 if (at_class_scope_p ()) 15013 { 15014 /* Create the USING_DECL. */ 15015 decl = do_class_using_decl (parser->scope, identifier); 15016 15017 if (decl && typename_p) 15018 USING_DECL_TYPENAME_P (decl) = 1; 15019 15020 if (check_for_bare_parameter_packs (decl)) 15021 return false; 15022 else 15023 /* Add it to the list of members in this class. */ 15024 finish_member_declaration (decl); 15025 } 15026 else 15027 { 15028 decl = cp_parser_lookup_name_simple (parser, 15029 identifier, 15030 token->location); 15031 if (decl == error_mark_node) 15032 cp_parser_name_lookup_error (parser, identifier, 15033 decl, NLE_NULL, 15034 token->location); 15035 else if (check_for_bare_parameter_packs (decl)) 15036 return false; 15037 else if (!at_namespace_scope_p ()) 15038 do_local_using_decl (decl, qscope, identifier); 15039 else 15040 do_toplevel_using_decl (decl, qscope, identifier); 15041 } 15042 } 15043 15044 /* Look for the final `;'. */ 15045 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 15046 15047 if (access_declaration_p && errorcount == oldcount) 15048 warning_at (diag_token->location, OPT_Wdeprecated, 15049 "access declarations are deprecated " 15050 "in favour of using-declarations; " 15051 "suggestion: add the %<using%> keyword"); 15052 15053 return true; 15054 } 15055 15056 /* Parse an alias-declaration. 15057 15058 alias-declaration: 15059 using identifier attribute-specifier-seq [opt] = type-id */ 15060 15061 static tree 15062 cp_parser_alias_declaration (cp_parser* parser) 15063 { 15064 tree id, type, decl, pushed_scope = NULL_TREE, attributes; 15065 location_t id_location; 15066 cp_declarator *declarator; 15067 cp_decl_specifier_seq decl_specs; 15068 bool member_p; 15069 const char *saved_message = NULL; 15070 15071 /* Look for the `using' keyword. */ 15072 cp_parser_require_keyword (parser, RID_USING, RT_USING); 15073 id_location = cp_lexer_peek_token (parser->lexer)->location; 15074 id = cp_parser_identifier (parser); 15075 if (id == error_mark_node) 15076 return error_mark_node; 15077 15078 attributes = cp_parser_attributes_opt (parser); 15079 if (attributes == error_mark_node) 15080 return error_mark_node; 15081 15082 cp_parser_require (parser, CPP_EQ, RT_EQ); 15083 15084 if (cp_parser_error_occurred (parser)) 15085 return error_mark_node; 15086 15087 /* Now we are going to parse the type-id of the declaration. */ 15088 15089 /* 15090 [dcl.type]/3 says: 15091 15092 "A type-specifier-seq shall not define a class or enumeration 15093 unless it appears in the type-id of an alias-declaration (7.1.3) that 15094 is not the declaration of a template-declaration." 15095 15096 In other words, if we currently are in an alias template, the 15097 type-id should not define a type. 15098 15099 So let's set parser->type_definition_forbidden_message in that 15100 case; cp_parser_check_type_definition (called by 15101 cp_parser_class_specifier) will then emit an error if a type is 15102 defined in the type-id. */ 15103 if (parser->num_template_parameter_lists) 15104 { 15105 saved_message = parser->type_definition_forbidden_message; 15106 parser->type_definition_forbidden_message = 15107 G_("types may not be defined in alias template declarations"); 15108 } 15109 15110 type = cp_parser_type_id (parser); 15111 15112 /* Restore the error message if need be. */ 15113 if (parser->num_template_parameter_lists) 15114 parser->type_definition_forbidden_message = saved_message; 15115 15116 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 15117 15118 if (cp_parser_error_occurred (parser)) 15119 return error_mark_node; 15120 15121 /* A typedef-name can also be introduced by an alias-declaration. The 15122 identifier following the using keyword becomes a typedef-name. It has 15123 the same semantics as if it were introduced by the typedef 15124 specifier. In particular, it does not define a new type and it shall 15125 not appear in the type-id. */ 15126 15127 clear_decl_specs (&decl_specs); 15128 decl_specs.type = type; 15129 decl_specs.attributes = attributes; 15130 ++decl_specs.specs[(int) ds_typedef]; 15131 ++decl_specs.specs[(int) ds_alias]; 15132 15133 declarator = make_id_declarator (NULL_TREE, id, sfk_none); 15134 declarator->id_loc = id_location; 15135 15136 member_p = at_class_scope_p (); 15137 if (member_p) 15138 decl = grokfield (declarator, &decl_specs, NULL_TREE, false, 15139 NULL_TREE, attributes); 15140 else 15141 decl = start_decl (declarator, &decl_specs, 0, 15142 attributes, NULL_TREE, &pushed_scope); 15143 if (decl == error_mark_node) 15144 return decl; 15145 15146 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0); 15147 15148 if (pushed_scope) 15149 pop_scope (pushed_scope); 15150 15151 /* If decl is a template, return its TEMPLATE_DECL so that it gets 15152 added into the symbol table; otherwise, return the TYPE_DECL. */ 15153 if (DECL_LANG_SPECIFIC (decl) 15154 && DECL_TEMPLATE_INFO (decl) 15155 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) 15156 { 15157 decl = DECL_TI_TEMPLATE (decl); 15158 if (member_p) 15159 check_member_template (decl); 15160 } 15161 15162 return decl; 15163 } 15164 15165 /* Parse a using-directive. 15166 15167 using-directive: 15168 using namespace :: [opt] nested-name-specifier [opt] 15169 namespace-name ; */ 15170 15171 static void 15172 cp_parser_using_directive (cp_parser* parser) 15173 { 15174 tree namespace_decl; 15175 tree attribs; 15176 15177 /* Look for the `using' keyword. */ 15178 cp_parser_require_keyword (parser, RID_USING, RT_USING); 15179 /* And the `namespace' keyword. */ 15180 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE); 15181 /* Look for the optional `::' operator. */ 15182 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); 15183 /* And the optional nested-name-specifier. */ 15184 cp_parser_nested_name_specifier_opt (parser, 15185 /*typename_keyword_p=*/false, 15186 /*check_dependency_p=*/true, 15187 /*type_p=*/false, 15188 /*is_declaration=*/true); 15189 /* Get the namespace being used. */ 15190 namespace_decl = cp_parser_namespace_name (parser); 15191 /* And any specified attributes. */ 15192 attribs = cp_parser_attributes_opt (parser); 15193 /* Update the symbol table. */ 15194 parse_using_directive (namespace_decl, attribs); 15195 /* Look for the final `;'. */ 15196 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 15197 } 15198 15199 /* Parse an asm-definition. 15200 15201 asm-definition: 15202 asm ( string-literal ) ; 15203 15204 GNU Extension: 15205 15206 asm-definition: 15207 asm volatile [opt] ( string-literal ) ; 15208 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ; 15209 asm volatile [opt] ( string-literal : asm-operand-list [opt] 15210 : asm-operand-list [opt] ) ; 15211 asm volatile [opt] ( string-literal : asm-operand-list [opt] 15212 : asm-operand-list [opt] 15213 : asm-clobber-list [opt] ) ; 15214 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt] 15215 : asm-clobber-list [opt] 15216 : asm-goto-list ) ; */ 15217 15218 static void 15219 cp_parser_asm_definition (cp_parser* parser) 15220 { 15221 tree string; 15222 tree outputs = NULL_TREE; 15223 tree inputs = NULL_TREE; 15224 tree clobbers = NULL_TREE; 15225 tree labels = NULL_TREE; 15226 tree asm_stmt; 15227 bool volatile_p = false; 15228 bool extended_p = false; 15229 bool invalid_inputs_p = false; 15230 bool invalid_outputs_p = false; 15231 bool goto_p = false; 15232 required_token missing = RT_NONE; 15233 15234 /* Look for the `asm' keyword. */ 15235 cp_parser_require_keyword (parser, RID_ASM, RT_ASM); 15236 /* See if the next token is `volatile'. */ 15237 if (cp_parser_allow_gnu_extensions_p (parser) 15238 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE)) 15239 { 15240 /* Remember that we saw the `volatile' keyword. */ 15241 volatile_p = true; 15242 /* Consume the token. */ 15243 cp_lexer_consume_token (parser->lexer); 15244 } 15245 if (cp_parser_allow_gnu_extensions_p (parser) 15246 && parser->in_function_body 15247 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO)) 15248 { 15249 /* Remember that we saw the `goto' keyword. */ 15250 goto_p = true; 15251 /* Consume the token. */ 15252 cp_lexer_consume_token (parser->lexer); 15253 } 15254 /* Look for the opening `('. */ 15255 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 15256 return; 15257 /* Look for the string. */ 15258 string = cp_parser_string_literal (parser, false, false); 15259 if (string == error_mark_node) 15260 { 15261 cp_parser_skip_to_closing_parenthesis (parser, true, false, 15262 /*consume_paren=*/true); 15263 return; 15264 } 15265 15266 /* If we're allowing GNU extensions, check for the extended assembly 15267 syntax. Unfortunately, the `:' tokens need not be separated by 15268 a space in C, and so, for compatibility, we tolerate that here 15269 too. Doing that means that we have to treat the `::' operator as 15270 two `:' tokens. */ 15271 if (cp_parser_allow_gnu_extensions_p (parser) 15272 && parser->in_function_body 15273 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON) 15274 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))) 15275 { 15276 bool inputs_p = false; 15277 bool clobbers_p = false; 15278 bool labels_p = false; 15279 15280 /* The extended syntax was used. */ 15281 extended_p = true; 15282 15283 /* Look for outputs. */ 15284 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 15285 { 15286 /* Consume the `:'. */ 15287 cp_lexer_consume_token (parser->lexer); 15288 /* Parse the output-operands. */ 15289 if (cp_lexer_next_token_is_not (parser->lexer, 15290 CPP_COLON) 15291 && cp_lexer_next_token_is_not (parser->lexer, 15292 CPP_SCOPE) 15293 && cp_lexer_next_token_is_not (parser->lexer, 15294 CPP_CLOSE_PAREN) 15295 && !goto_p) 15296 outputs = cp_parser_asm_operand_list (parser); 15297 15298 if (outputs == error_mark_node) 15299 invalid_outputs_p = true; 15300 } 15301 /* If the next token is `::', there are no outputs, and the 15302 next token is the beginning of the inputs. */ 15303 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 15304 /* The inputs are coming next. */ 15305 inputs_p = true; 15306 15307 /* Look for inputs. */ 15308 if (inputs_p 15309 || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 15310 { 15311 /* Consume the `:' or `::'. */ 15312 cp_lexer_consume_token (parser->lexer); 15313 /* Parse the output-operands. */ 15314 if (cp_lexer_next_token_is_not (parser->lexer, 15315 CPP_COLON) 15316 && cp_lexer_next_token_is_not (parser->lexer, 15317 CPP_SCOPE) 15318 && cp_lexer_next_token_is_not (parser->lexer, 15319 CPP_CLOSE_PAREN)) 15320 inputs = cp_parser_asm_operand_list (parser); 15321 15322 if (inputs == error_mark_node) 15323 invalid_inputs_p = true; 15324 } 15325 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 15326 /* The clobbers are coming next. */ 15327 clobbers_p = true; 15328 15329 /* Look for clobbers. */ 15330 if (clobbers_p 15331 || cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 15332 { 15333 clobbers_p = true; 15334 /* Consume the `:' or `::'. */ 15335 cp_lexer_consume_token (parser->lexer); 15336 /* Parse the clobbers. */ 15337 if (cp_lexer_next_token_is_not (parser->lexer, 15338 CPP_COLON) 15339 && cp_lexer_next_token_is_not (parser->lexer, 15340 CPP_CLOSE_PAREN)) 15341 clobbers = cp_parser_asm_clobber_list (parser); 15342 } 15343 else if (goto_p 15344 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 15345 /* The labels are coming next. */ 15346 labels_p = true; 15347 15348 /* Look for labels. */ 15349 if (labels_p 15350 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON))) 15351 { 15352 labels_p = true; 15353 /* Consume the `:' or `::'. */ 15354 cp_lexer_consume_token (parser->lexer); 15355 /* Parse the labels. */ 15356 labels = cp_parser_asm_label_list (parser); 15357 } 15358 15359 if (goto_p && !labels_p) 15360 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE; 15361 } 15362 else if (goto_p) 15363 missing = RT_COLON_SCOPE; 15364 15365 /* Look for the closing `)'. */ 15366 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN, 15367 missing ? missing : RT_CLOSE_PAREN)) 15368 cp_parser_skip_to_closing_parenthesis (parser, true, false, 15369 /*consume_paren=*/true); 15370 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 15371 15372 if (!invalid_inputs_p && !invalid_outputs_p) 15373 { 15374 /* Create the ASM_EXPR. */ 15375 if (parser->in_function_body) 15376 { 15377 asm_stmt = finish_asm_stmt (volatile_p, string, outputs, 15378 inputs, clobbers, labels); 15379 /* If the extended syntax was not used, mark the ASM_EXPR. */ 15380 if (!extended_p) 15381 { 15382 tree temp = asm_stmt; 15383 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR) 15384 temp = TREE_OPERAND (temp, 0); 15385 15386 ASM_INPUT_P (temp) = 1; 15387 } 15388 } 15389 else 15390 cgraph_add_asm_node (string); 15391 } 15392 } 15393 15394 /* Declarators [gram.dcl.decl] */ 15395 15396 /* Parse an init-declarator. 15397 15398 init-declarator: 15399 declarator initializer [opt] 15400 15401 GNU Extension: 15402 15403 init-declarator: 15404 declarator asm-specification [opt] attributes [opt] initializer [opt] 15405 15406 function-definition: 15407 decl-specifier-seq [opt] declarator ctor-initializer [opt] 15408 function-body 15409 decl-specifier-seq [opt] declarator function-try-block 15410 15411 GNU Extension: 15412 15413 function-definition: 15414 __extension__ function-definition 15415 15416 TM Extension: 15417 15418 function-definition: 15419 decl-specifier-seq [opt] declarator function-transaction-block 15420 15421 The DECL_SPECIFIERS apply to this declarator. Returns a 15422 representation of the entity declared. If MEMBER_P is TRUE, then 15423 this declarator appears in a class scope. The new DECL created by 15424 this declarator is returned. 15425 15426 The CHECKS are access checks that should be performed once we know 15427 what entity is being declared (and, therefore, what classes have 15428 befriended it). 15429 15430 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and 15431 for a function-definition here as well. If the declarator is a 15432 declarator for a function-definition, *FUNCTION_DEFINITION_P will 15433 be TRUE upon return. By that point, the function-definition will 15434 have been completely parsed. 15435 15436 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P 15437 is FALSE. 15438 15439 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the 15440 parsed declaration if it is an uninitialized single declarator not followed 15441 by a `;', or to error_mark_node otherwise. Either way, the trailing `;', 15442 if present, will not be consumed. If returned, this declarator will be 15443 created with SD_INITIALIZED but will not call cp_finish_decl. */ 15444 15445 static tree 15446 cp_parser_init_declarator (cp_parser* parser, 15447 cp_decl_specifier_seq *decl_specifiers, 15448 VEC (deferred_access_check,gc)* checks, 15449 bool function_definition_allowed_p, 15450 bool member_p, 15451 int declares_class_or_enum, 15452 bool* function_definition_p, 15453 tree* maybe_range_for_decl) 15454 { 15455 cp_token *token = NULL, *asm_spec_start_token = NULL, 15456 *attributes_start_token = NULL; 15457 cp_declarator *declarator; 15458 tree prefix_attributes; 15459 tree attributes; 15460 tree asm_specification; 15461 tree initializer; 15462 tree decl = NULL_TREE; 15463 tree scope; 15464 int is_initialized; 15465 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if 15466 initialized with "= ..", CPP_OPEN_PAREN if initialized with 15467 "(...)". */ 15468 enum cpp_ttype initialization_kind; 15469 bool is_direct_init = false; 15470 bool is_non_constant_init; 15471 int ctor_dtor_or_conv_p; 15472 bool friend_p; 15473 tree pushed_scope = NULL_TREE; 15474 bool range_for_decl_p = false; 15475 15476 /* Gather the attributes that were provided with the 15477 decl-specifiers. */ 15478 prefix_attributes = decl_specifiers->attributes; 15479 15480 /* Assume that this is not the declarator for a function 15481 definition. */ 15482 if (function_definition_p) 15483 *function_definition_p = false; 15484 15485 /* Defer access checks while parsing the declarator; we cannot know 15486 what names are accessible until we know what is being 15487 declared. */ 15488 resume_deferring_access_checks (); 15489 15490 /* Parse the declarator. */ 15491 token = cp_lexer_peek_token (parser->lexer); 15492 declarator 15493 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 15494 &ctor_dtor_or_conv_p, 15495 /*parenthesized_p=*/NULL, 15496 member_p); 15497 /* Gather up the deferred checks. */ 15498 stop_deferring_access_checks (); 15499 15500 /* If the DECLARATOR was erroneous, there's no need to go 15501 further. */ 15502 if (declarator == cp_error_declarator) 15503 return error_mark_node; 15504 15505 /* Check that the number of template-parameter-lists is OK. */ 15506 if (!cp_parser_check_declarator_template_parameters (parser, declarator, 15507 token->location)) 15508 return error_mark_node; 15509 15510 if (declares_class_or_enum & 2) 15511 cp_parser_check_for_definition_in_return_type (declarator, 15512 decl_specifiers->type, 15513 decl_specifiers->type_location); 15514 15515 /* Figure out what scope the entity declared by the DECLARATOR is 15516 located in. `grokdeclarator' sometimes changes the scope, so 15517 we compute it now. */ 15518 scope = get_scope_of_declarator (declarator); 15519 15520 /* Perform any lookups in the declared type which were thought to be 15521 dependent, but are not in the scope of the declarator. */ 15522 decl_specifiers->type 15523 = maybe_update_decl_type (decl_specifiers->type, scope); 15524 15525 /* If we're allowing GNU extensions, look for an asm-specification 15526 and attributes. */ 15527 if (cp_parser_allow_gnu_extensions_p (parser)) 15528 { 15529 /* Look for an asm-specification. */ 15530 asm_spec_start_token = cp_lexer_peek_token (parser->lexer); 15531 asm_specification = cp_parser_asm_specification_opt (parser); 15532 /* And attributes. */ 15533 attributes_start_token = cp_lexer_peek_token (parser->lexer); 15534 attributes = cp_parser_attributes_opt (parser); 15535 } 15536 else 15537 { 15538 asm_specification = NULL_TREE; 15539 attributes = NULL_TREE; 15540 } 15541 15542 /* Peek at the next token. */ 15543 token = cp_lexer_peek_token (parser->lexer); 15544 /* Check to see if the token indicates the start of a 15545 function-definition. */ 15546 if (function_declarator_p (declarator) 15547 && cp_parser_token_starts_function_definition_p (token)) 15548 { 15549 if (!function_definition_allowed_p) 15550 { 15551 /* If a function-definition should not appear here, issue an 15552 error message. */ 15553 cp_parser_error (parser, 15554 "a function-definition is not allowed here"); 15555 return error_mark_node; 15556 } 15557 else 15558 { 15559 location_t func_brace_location 15560 = cp_lexer_peek_token (parser->lexer)->location; 15561 15562 /* Neither attributes nor an asm-specification are allowed 15563 on a function-definition. */ 15564 if (asm_specification) 15565 error_at (asm_spec_start_token->location, 15566 "an asm-specification is not allowed " 15567 "on a function-definition"); 15568 if (attributes) 15569 error_at (attributes_start_token->location, 15570 "attributes are not allowed on a function-definition"); 15571 /* This is a function-definition. */ 15572 *function_definition_p = true; 15573 15574 /* Parse the function definition. */ 15575 if (member_p) 15576 decl = cp_parser_save_member_function_body (parser, 15577 decl_specifiers, 15578 declarator, 15579 prefix_attributes); 15580 else 15581 decl 15582 = (cp_parser_function_definition_from_specifiers_and_declarator 15583 (parser, decl_specifiers, prefix_attributes, declarator)); 15584 15585 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl)) 15586 { 15587 /* This is where the prologue starts... */ 15588 DECL_STRUCT_FUNCTION (decl)->function_start_locus 15589 = func_brace_location; 15590 } 15591 15592 return decl; 15593 } 15594 } 15595 15596 /* [dcl.dcl] 15597 15598 Only in function declarations for constructors, destructors, and 15599 type conversions can the decl-specifier-seq be omitted. 15600 15601 We explicitly postpone this check past the point where we handle 15602 function-definitions because we tolerate function-definitions 15603 that are missing their return types in some modes. */ 15604 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0) 15605 { 15606 cp_parser_error (parser, 15607 "expected constructor, destructor, or type conversion"); 15608 return error_mark_node; 15609 } 15610 15611 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */ 15612 if (token->type == CPP_EQ 15613 || token->type == CPP_OPEN_PAREN 15614 || token->type == CPP_OPEN_BRACE) 15615 { 15616 is_initialized = SD_INITIALIZED; 15617 initialization_kind = token->type; 15618 if (maybe_range_for_decl) 15619 *maybe_range_for_decl = error_mark_node; 15620 15621 if (token->type == CPP_EQ 15622 && function_declarator_p (declarator)) 15623 { 15624 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2); 15625 if (t2->keyword == RID_DEFAULT) 15626 is_initialized = SD_DEFAULTED; 15627 else if (t2->keyword == RID_DELETE) 15628 is_initialized = SD_DELETED; 15629 } 15630 } 15631 else 15632 { 15633 /* If the init-declarator isn't initialized and isn't followed by a 15634 `,' or `;', it's not a valid init-declarator. */ 15635 if (token->type != CPP_COMMA 15636 && token->type != CPP_SEMICOLON) 15637 { 15638 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node) 15639 range_for_decl_p = true; 15640 else 15641 { 15642 cp_parser_error (parser, "expected initializer"); 15643 return error_mark_node; 15644 } 15645 } 15646 is_initialized = SD_UNINITIALIZED; 15647 initialization_kind = CPP_EOF; 15648 } 15649 15650 /* Because start_decl has side-effects, we should only call it if we 15651 know we're going ahead. By this point, we know that we cannot 15652 possibly be looking at any other construct. */ 15653 cp_parser_commit_to_tentative_parse (parser); 15654 15655 /* If the decl specifiers were bad, issue an error now that we're 15656 sure this was intended to be a declarator. Then continue 15657 declaring the variable(s), as int, to try to cut down on further 15658 errors. */ 15659 if (decl_specifiers->any_specifiers_p 15660 && decl_specifiers->type == error_mark_node) 15661 { 15662 cp_parser_error (parser, "invalid type in declaration"); 15663 decl_specifiers->type = integer_type_node; 15664 } 15665 15666 /* Check to see whether or not this declaration is a friend. */ 15667 friend_p = cp_parser_friend_p (decl_specifiers); 15668 15669 /* Enter the newly declared entry in the symbol table. If we're 15670 processing a declaration in a class-specifier, we wait until 15671 after processing the initializer. */ 15672 if (!member_p) 15673 { 15674 if (parser->in_unbraced_linkage_specification_p) 15675 decl_specifiers->storage_class = sc_extern; 15676 decl = start_decl (declarator, decl_specifiers, 15677 range_for_decl_p? SD_INITIALIZED : is_initialized, 15678 attributes, prefix_attributes, 15679 &pushed_scope); 15680 /* Adjust location of decl if declarator->id_loc is more appropriate: 15681 set, and decl wasn't merged with another decl, in which case its 15682 location would be different from input_location, and more accurate. */ 15683 if (DECL_P (decl) 15684 && declarator->id_loc != UNKNOWN_LOCATION 15685 && DECL_SOURCE_LOCATION (decl) == input_location) 15686 DECL_SOURCE_LOCATION (decl) = declarator->id_loc; 15687 } 15688 else if (scope) 15689 /* Enter the SCOPE. That way unqualified names appearing in the 15690 initializer will be looked up in SCOPE. */ 15691 pushed_scope = push_scope (scope); 15692 15693 /* Perform deferred access control checks, now that we know in which 15694 SCOPE the declared entity resides. */ 15695 if (!member_p && decl) 15696 { 15697 tree saved_current_function_decl = NULL_TREE; 15698 15699 /* If the entity being declared is a function, pretend that we 15700 are in its scope. If it is a `friend', it may have access to 15701 things that would not otherwise be accessible. */ 15702 if (TREE_CODE (decl) == FUNCTION_DECL) 15703 { 15704 saved_current_function_decl = current_function_decl; 15705 current_function_decl = decl; 15706 } 15707 15708 /* Perform access checks for template parameters. */ 15709 cp_parser_perform_template_parameter_access_checks (checks); 15710 15711 /* Perform the access control checks for the declarator and the 15712 decl-specifiers. */ 15713 perform_deferred_access_checks (); 15714 15715 /* Restore the saved value. */ 15716 if (TREE_CODE (decl) == FUNCTION_DECL) 15717 current_function_decl = saved_current_function_decl; 15718 } 15719 15720 /* Parse the initializer. */ 15721 initializer = NULL_TREE; 15722 is_direct_init = false; 15723 is_non_constant_init = true; 15724 if (is_initialized) 15725 { 15726 if (function_declarator_p (declarator)) 15727 { 15728 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer); 15729 if (initialization_kind == CPP_EQ) 15730 initializer = cp_parser_pure_specifier (parser); 15731 else 15732 { 15733 /* If the declaration was erroneous, we don't really 15734 know what the user intended, so just silently 15735 consume the initializer. */ 15736 if (decl != error_mark_node) 15737 error_at (initializer_start_token->location, 15738 "initializer provided for function"); 15739 cp_parser_skip_to_closing_parenthesis (parser, 15740 /*recovering=*/true, 15741 /*or_comma=*/false, 15742 /*consume_paren=*/true); 15743 } 15744 } 15745 else 15746 { 15747 /* We want to record the extra mangling scope for in-class 15748 initializers of class members and initializers of static data 15749 member templates. The former involves deferring 15750 parsing of the initializer until end of class as with default 15751 arguments. So right here we only handle the latter. */ 15752 if (!member_p && processing_template_decl) 15753 start_lambda_scope (decl); 15754 initializer = cp_parser_initializer (parser, 15755 &is_direct_init, 15756 &is_non_constant_init); 15757 if (!member_p && processing_template_decl) 15758 finish_lambda_scope (); 15759 } 15760 } 15761 15762 /* The old parser allows attributes to appear after a parenthesized 15763 initializer. Mark Mitchell proposed removing this functionality 15764 on the GCC mailing lists on 2002-08-13. This parser accepts the 15765 attributes -- but ignores them. */ 15766 if (cp_parser_allow_gnu_extensions_p (parser) 15767 && initialization_kind == CPP_OPEN_PAREN) 15768 if (cp_parser_attributes_opt (parser)) 15769 warning (OPT_Wattributes, 15770 "attributes after parenthesized initializer ignored"); 15771 15772 /* For an in-class declaration, use `grokfield' to create the 15773 declaration. */ 15774 if (member_p) 15775 { 15776 if (pushed_scope) 15777 { 15778 pop_scope (pushed_scope); 15779 pushed_scope = NULL_TREE; 15780 } 15781 decl = grokfield (declarator, decl_specifiers, 15782 initializer, !is_non_constant_init, 15783 /*asmspec=*/NULL_TREE, 15784 prefix_attributes); 15785 if (decl && TREE_CODE (decl) == FUNCTION_DECL) 15786 cp_parser_save_default_args (parser, decl); 15787 } 15788 15789 /* Finish processing the declaration. But, skip member 15790 declarations. */ 15791 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p) 15792 { 15793 cp_finish_decl (decl, 15794 initializer, !is_non_constant_init, 15795 asm_specification, 15796 /* If the initializer is in parentheses, then this is 15797 a direct-initialization, which means that an 15798 `explicit' constructor is OK. Otherwise, an 15799 `explicit' constructor cannot be used. */ 15800 ((is_direct_init || !is_initialized) 15801 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT)); 15802 } 15803 else if ((cxx_dialect != cxx98) && friend_p 15804 && decl && TREE_CODE (decl) == FUNCTION_DECL) 15805 /* Core issue #226 (C++0x only): A default template-argument 15806 shall not be specified in a friend class template 15807 declaration. */ 15808 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 15809 /*is_partial=*/0, /*is_friend_decl=*/1); 15810 15811 if (!friend_p && pushed_scope) 15812 pop_scope (pushed_scope); 15813 15814 return decl; 15815 } 15816 15817 /* Parse a declarator. 15818 15819 declarator: 15820 direct-declarator 15821 ptr-operator declarator 15822 15823 abstract-declarator: 15824 ptr-operator abstract-declarator [opt] 15825 direct-abstract-declarator 15826 15827 GNU Extensions: 15828 15829 declarator: 15830 attributes [opt] direct-declarator 15831 attributes [opt] ptr-operator declarator 15832 15833 abstract-declarator: 15834 attributes [opt] ptr-operator abstract-declarator [opt] 15835 attributes [opt] direct-abstract-declarator 15836 15837 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to 15838 detect constructor, destructor or conversion operators. It is set 15839 to -1 if the declarator is a name, and +1 if it is a 15840 function. Otherwise it is set to zero. Usually you just want to 15841 test for >0, but internally the negative value is used. 15842 15843 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have 15844 a decl-specifier-seq unless it declares a constructor, destructor, 15845 or conversion. It might seem that we could check this condition in 15846 semantic analysis, rather than parsing, but that makes it difficult 15847 to handle something like `f()'. We want to notice that there are 15848 no decl-specifiers, and therefore realize that this is an 15849 expression, not a declaration.) 15850 15851 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff 15852 the declarator is a direct-declarator of the form "(...)". 15853 15854 MEMBER_P is true iff this declarator is a member-declarator. */ 15855 15856 static cp_declarator * 15857 cp_parser_declarator (cp_parser* parser, 15858 cp_parser_declarator_kind dcl_kind, 15859 int* ctor_dtor_or_conv_p, 15860 bool* parenthesized_p, 15861 bool member_p) 15862 { 15863 cp_declarator *declarator; 15864 enum tree_code code; 15865 cp_cv_quals cv_quals; 15866 tree class_type; 15867 tree attributes = NULL_TREE; 15868 15869 /* Assume this is not a constructor, destructor, or type-conversion 15870 operator. */ 15871 if (ctor_dtor_or_conv_p) 15872 *ctor_dtor_or_conv_p = 0; 15873 15874 if (cp_parser_allow_gnu_extensions_p (parser)) 15875 attributes = cp_parser_attributes_opt (parser); 15876 15877 /* Check for the ptr-operator production. */ 15878 cp_parser_parse_tentatively (parser); 15879 /* Parse the ptr-operator. */ 15880 code = cp_parser_ptr_operator (parser, 15881 &class_type, 15882 &cv_quals); 15883 /* If that worked, then we have a ptr-operator. */ 15884 if (cp_parser_parse_definitely (parser)) 15885 { 15886 /* If a ptr-operator was found, then this declarator was not 15887 parenthesized. */ 15888 if (parenthesized_p) 15889 *parenthesized_p = true; 15890 /* The dependent declarator is optional if we are parsing an 15891 abstract-declarator. */ 15892 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED) 15893 cp_parser_parse_tentatively (parser); 15894 15895 /* Parse the dependent declarator. */ 15896 declarator = cp_parser_declarator (parser, dcl_kind, 15897 /*ctor_dtor_or_conv_p=*/NULL, 15898 /*parenthesized_p=*/NULL, 15899 /*member_p=*/false); 15900 15901 /* If we are parsing an abstract-declarator, we must handle the 15902 case where the dependent declarator is absent. */ 15903 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED 15904 && !cp_parser_parse_definitely (parser)) 15905 declarator = NULL; 15906 15907 declarator = cp_parser_make_indirect_declarator 15908 (code, class_type, cv_quals, declarator); 15909 } 15910 /* Everything else is a direct-declarator. */ 15911 else 15912 { 15913 if (parenthesized_p) 15914 *parenthesized_p = cp_lexer_next_token_is (parser->lexer, 15915 CPP_OPEN_PAREN); 15916 declarator = cp_parser_direct_declarator (parser, dcl_kind, 15917 ctor_dtor_or_conv_p, 15918 member_p); 15919 } 15920 15921 if (attributes && declarator && declarator != cp_error_declarator) 15922 declarator->attributes = attributes; 15923 15924 return declarator; 15925 } 15926 15927 /* Parse a direct-declarator or direct-abstract-declarator. 15928 15929 direct-declarator: 15930 declarator-id 15931 direct-declarator ( parameter-declaration-clause ) 15932 cv-qualifier-seq [opt] 15933 exception-specification [opt] 15934 direct-declarator [ constant-expression [opt] ] 15935 ( declarator ) 15936 15937 direct-abstract-declarator: 15938 direct-abstract-declarator [opt] 15939 ( parameter-declaration-clause ) 15940 cv-qualifier-seq [opt] 15941 exception-specification [opt] 15942 direct-abstract-declarator [opt] [ constant-expression [opt] ] 15943 ( abstract-declarator ) 15944 15945 Returns a representation of the declarator. DCL_KIND is 15946 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a 15947 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if 15948 we are parsing a direct-declarator. It is 15949 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case 15950 of ambiguity we prefer an abstract declarator, as per 15951 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for 15952 cp_parser_declarator. */ 15953 15954 static cp_declarator * 15955 cp_parser_direct_declarator (cp_parser* parser, 15956 cp_parser_declarator_kind dcl_kind, 15957 int* ctor_dtor_or_conv_p, 15958 bool member_p) 15959 { 15960 cp_token *token; 15961 cp_declarator *declarator = NULL; 15962 tree scope = NULL_TREE; 15963 bool saved_default_arg_ok_p = parser->default_arg_ok_p; 15964 bool saved_in_declarator_p = parser->in_declarator_p; 15965 bool first = true; 15966 tree pushed_scope = NULL_TREE; 15967 15968 while (true) 15969 { 15970 /* Peek at the next token. */ 15971 token = cp_lexer_peek_token (parser->lexer); 15972 if (token->type == CPP_OPEN_PAREN) 15973 { 15974 /* This is either a parameter-declaration-clause, or a 15975 parenthesized declarator. When we know we are parsing a 15976 named declarator, it must be a parenthesized declarator 15977 if FIRST is true. For instance, `(int)' is a 15978 parameter-declaration-clause, with an omitted 15979 direct-abstract-declarator. But `((*))', is a 15980 parenthesized abstract declarator. Finally, when T is a 15981 template parameter `(T)' is a 15982 parameter-declaration-clause, and not a parenthesized 15983 named declarator. 15984 15985 We first try and parse a parameter-declaration-clause, 15986 and then try a nested declarator (if FIRST is true). 15987 15988 It is not an error for it not to be a 15989 parameter-declaration-clause, even when FIRST is 15990 false. Consider, 15991 15992 int i (int); 15993 int i (3); 15994 15995 The first is the declaration of a function while the 15996 second is the definition of a variable, including its 15997 initializer. 15998 15999 Having seen only the parenthesis, we cannot know which of 16000 these two alternatives should be selected. Even more 16001 complex are examples like: 16002 16003 int i (int (a)); 16004 int i (int (3)); 16005 16006 The former is a function-declaration; the latter is a 16007 variable initialization. 16008 16009 Thus again, we try a parameter-declaration-clause, and if 16010 that fails, we back out and return. */ 16011 16012 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) 16013 { 16014 tree params; 16015 unsigned saved_num_template_parameter_lists; 16016 bool is_declarator = false; 16017 tree t; 16018 16019 /* In a member-declarator, the only valid interpretation 16020 of a parenthesis is the start of a 16021 parameter-declaration-clause. (It is invalid to 16022 initialize a static data member with a parenthesized 16023 initializer; only the "=" form of initialization is 16024 permitted.) */ 16025 if (!member_p) 16026 cp_parser_parse_tentatively (parser); 16027 16028 /* Consume the `('. */ 16029 cp_lexer_consume_token (parser->lexer); 16030 if (first) 16031 { 16032 /* If this is going to be an abstract declarator, we're 16033 in a declarator and we can't have default args. */ 16034 parser->default_arg_ok_p = false; 16035 parser->in_declarator_p = true; 16036 } 16037 16038 /* Inside the function parameter list, surrounding 16039 template-parameter-lists do not apply. */ 16040 saved_num_template_parameter_lists 16041 = parser->num_template_parameter_lists; 16042 parser->num_template_parameter_lists = 0; 16043 16044 begin_scope (sk_function_parms, NULL_TREE); 16045 16046 /* Parse the parameter-declaration-clause. */ 16047 params = cp_parser_parameter_declaration_clause (parser); 16048 16049 parser->num_template_parameter_lists 16050 = saved_num_template_parameter_lists; 16051 16052 /* Consume the `)'. */ 16053 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 16054 16055 /* If all went well, parse the cv-qualifier-seq and the 16056 exception-specification. */ 16057 if (member_p || cp_parser_parse_definitely (parser)) 16058 { 16059 cp_cv_quals cv_quals; 16060 cp_virt_specifiers virt_specifiers; 16061 tree exception_specification; 16062 tree late_return; 16063 16064 is_declarator = true; 16065 16066 if (ctor_dtor_or_conv_p) 16067 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0; 16068 first = false; 16069 16070 /* Parse the cv-qualifier-seq. */ 16071 cv_quals = cp_parser_cv_qualifier_seq_opt (parser); 16072 /* And the exception-specification. */ 16073 exception_specification 16074 = cp_parser_exception_specification_opt (parser); 16075 /* Parse the virt-specifier-seq. */ 16076 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser); 16077 16078 late_return = (cp_parser_late_return_type_opt 16079 (parser, member_p ? cv_quals : -1)); 16080 16081 /* Create the function-declarator. */ 16082 declarator = make_call_declarator (declarator, 16083 params, 16084 cv_quals, 16085 virt_specifiers, 16086 exception_specification, 16087 late_return); 16088 /* Any subsequent parameter lists are to do with 16089 return type, so are not those of the declared 16090 function. */ 16091 parser->default_arg_ok_p = false; 16092 } 16093 16094 /* Remove the function parms from scope. */ 16095 for (t = current_binding_level->names; t; t = DECL_CHAIN (t)) 16096 pop_binding (DECL_NAME (t), t); 16097 leave_scope(); 16098 16099 if (is_declarator) 16100 /* Repeat the main loop. */ 16101 continue; 16102 } 16103 16104 /* If this is the first, we can try a parenthesized 16105 declarator. */ 16106 if (first) 16107 { 16108 bool saved_in_type_id_in_expr_p; 16109 16110 parser->default_arg_ok_p = saved_default_arg_ok_p; 16111 parser->in_declarator_p = saved_in_declarator_p; 16112 16113 /* Consume the `('. */ 16114 cp_lexer_consume_token (parser->lexer); 16115 /* Parse the nested declarator. */ 16116 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; 16117 parser->in_type_id_in_expr_p = true; 16118 declarator 16119 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p, 16120 /*parenthesized_p=*/NULL, 16121 member_p); 16122 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; 16123 first = false; 16124 /* Expect a `)'. */ 16125 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 16126 declarator = cp_error_declarator; 16127 if (declarator == cp_error_declarator) 16128 break; 16129 16130 goto handle_declarator; 16131 } 16132 /* Otherwise, we must be done. */ 16133 else 16134 break; 16135 } 16136 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) 16137 && token->type == CPP_OPEN_SQUARE) 16138 { 16139 /* Parse an array-declarator. */ 16140 tree bounds; 16141 16142 if (ctor_dtor_or_conv_p) 16143 *ctor_dtor_or_conv_p = 0; 16144 16145 first = false; 16146 parser->default_arg_ok_p = false; 16147 parser->in_declarator_p = true; 16148 /* Consume the `['. */ 16149 cp_lexer_consume_token (parser->lexer); 16150 /* Peek at the next token. */ 16151 token = cp_lexer_peek_token (parser->lexer); 16152 /* If the next token is `]', then there is no 16153 constant-expression. */ 16154 if (token->type != CPP_CLOSE_SQUARE) 16155 { 16156 bool non_constant_p; 16157 16158 bounds 16159 = cp_parser_constant_expression (parser, 16160 /*allow_non_constant=*/true, 16161 &non_constant_p); 16162 if (!non_constant_p) 16163 /* OK */; 16164 else if (error_operand_p (bounds)) 16165 /* Already gave an error. */; 16166 else if (!parser->in_function_body 16167 || current_binding_level->kind == sk_function_parms) 16168 { 16169 /* Normally, the array bound must be an integral constant 16170 expression. However, as an extension, we allow VLAs 16171 in function scopes as long as they aren't part of a 16172 parameter declaration. */ 16173 cp_parser_error (parser, 16174 "array bound is not an integer constant"); 16175 bounds = error_mark_node; 16176 } 16177 else if (processing_template_decl) 16178 { 16179 /* Remember this wasn't a constant-expression. */ 16180 bounds = build_nop (TREE_TYPE (bounds), bounds); 16181 TREE_SIDE_EFFECTS (bounds) = 1; 16182 } 16183 } 16184 else 16185 bounds = NULL_TREE; 16186 /* Look for the closing `]'. */ 16187 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)) 16188 { 16189 declarator = cp_error_declarator; 16190 break; 16191 } 16192 16193 declarator = make_array_declarator (declarator, bounds); 16194 } 16195 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) 16196 { 16197 { 16198 tree qualifying_scope; 16199 tree unqualified_name; 16200 special_function_kind sfk; 16201 bool abstract_ok; 16202 bool pack_expansion_p = false; 16203 cp_token *declarator_id_start_token; 16204 16205 /* Parse a declarator-id */ 16206 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER); 16207 if (abstract_ok) 16208 { 16209 cp_parser_parse_tentatively (parser); 16210 16211 /* If we see an ellipsis, we should be looking at a 16212 parameter pack. */ 16213 if (token->type == CPP_ELLIPSIS) 16214 { 16215 /* Consume the `...' */ 16216 cp_lexer_consume_token (parser->lexer); 16217 16218 pack_expansion_p = true; 16219 } 16220 } 16221 16222 declarator_id_start_token = cp_lexer_peek_token (parser->lexer); 16223 unqualified_name 16224 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok); 16225 qualifying_scope = parser->scope; 16226 if (abstract_ok) 16227 { 16228 bool okay = false; 16229 16230 if (!unqualified_name && pack_expansion_p) 16231 { 16232 /* Check whether an error occurred. */ 16233 okay = !cp_parser_error_occurred (parser); 16234 16235 /* We already consumed the ellipsis to mark a 16236 parameter pack, but we have no way to report it, 16237 so abort the tentative parse. We will be exiting 16238 immediately anyway. */ 16239 cp_parser_abort_tentative_parse (parser); 16240 } 16241 else 16242 okay = cp_parser_parse_definitely (parser); 16243 16244 if (!okay) 16245 unqualified_name = error_mark_node; 16246 else if (unqualified_name 16247 && (qualifying_scope 16248 || (TREE_CODE (unqualified_name) 16249 != IDENTIFIER_NODE))) 16250 { 16251 cp_parser_error (parser, "expected unqualified-id"); 16252 unqualified_name = error_mark_node; 16253 } 16254 } 16255 16256 if (!unqualified_name) 16257 return NULL; 16258 if (unqualified_name == error_mark_node) 16259 { 16260 declarator = cp_error_declarator; 16261 pack_expansion_p = false; 16262 declarator->parameter_pack_p = false; 16263 break; 16264 } 16265 16266 if (qualifying_scope && at_namespace_scope_p () 16267 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE) 16268 { 16269 /* In the declaration of a member of a template class 16270 outside of the class itself, the SCOPE will sometimes 16271 be a TYPENAME_TYPE. For example, given: 16272 16273 template <typename T> 16274 int S<T>::R::i = 3; 16275 16276 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In 16277 this context, we must resolve S<T>::R to an ordinary 16278 type, rather than a typename type. 16279 16280 The reason we normally avoid resolving TYPENAME_TYPEs 16281 is that a specialization of `S' might render 16282 `S<T>::R' not a type. However, if `S' is 16283 specialized, then this `i' will not be used, so there 16284 is no harm in resolving the types here. */ 16285 tree type; 16286 16287 /* Resolve the TYPENAME_TYPE. */ 16288 type = resolve_typename_type (qualifying_scope, 16289 /*only_current_p=*/false); 16290 /* If that failed, the declarator is invalid. */ 16291 if (TREE_CODE (type) == TYPENAME_TYPE) 16292 { 16293 if (typedef_variant_p (type)) 16294 error_at (declarator_id_start_token->location, 16295 "cannot define member of dependent typedef " 16296 "%qT", type); 16297 else 16298 error_at (declarator_id_start_token->location, 16299 "%<%T::%E%> is not a type", 16300 TYPE_CONTEXT (qualifying_scope), 16301 TYPE_IDENTIFIER (qualifying_scope)); 16302 } 16303 qualifying_scope = type; 16304 } 16305 16306 sfk = sfk_none; 16307 16308 if (unqualified_name) 16309 { 16310 tree class_type; 16311 16312 if (qualifying_scope 16313 && CLASS_TYPE_P (qualifying_scope)) 16314 class_type = qualifying_scope; 16315 else 16316 class_type = current_class_type; 16317 16318 if (TREE_CODE (unqualified_name) == TYPE_DECL) 16319 { 16320 tree name_type = TREE_TYPE (unqualified_name); 16321 if (class_type && same_type_p (name_type, class_type)) 16322 { 16323 if (qualifying_scope 16324 && CLASSTYPE_USE_TEMPLATE (name_type)) 16325 { 16326 error_at (declarator_id_start_token->location, 16327 "invalid use of constructor as a template"); 16328 inform (declarator_id_start_token->location, 16329 "use %<%T::%D%> instead of %<%T::%D%> to " 16330 "name the constructor in a qualified name", 16331 class_type, 16332 DECL_NAME (TYPE_TI_TEMPLATE (class_type)), 16333 class_type, name_type); 16334 declarator = cp_error_declarator; 16335 break; 16336 } 16337 else 16338 unqualified_name = constructor_name (class_type); 16339 } 16340 else 16341 { 16342 /* We do not attempt to print the declarator 16343 here because we do not have enough 16344 information about its original syntactic 16345 form. */ 16346 cp_parser_error (parser, "invalid declarator"); 16347 declarator = cp_error_declarator; 16348 break; 16349 } 16350 } 16351 16352 if (class_type) 16353 { 16354 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR) 16355 sfk = sfk_destructor; 16356 else if (IDENTIFIER_TYPENAME_P (unqualified_name)) 16357 sfk = sfk_conversion; 16358 else if (/* There's no way to declare a constructor 16359 for an anonymous type, even if the type 16360 got a name for linkage purposes. */ 16361 !TYPE_WAS_ANONYMOUS (class_type) 16362 && constructor_name_p (unqualified_name, 16363 class_type)) 16364 { 16365 unqualified_name = constructor_name (class_type); 16366 sfk = sfk_constructor; 16367 } 16368 else if (is_overloaded_fn (unqualified_name) 16369 && DECL_CONSTRUCTOR_P (get_first_fn 16370 (unqualified_name))) 16371 sfk = sfk_constructor; 16372 16373 if (ctor_dtor_or_conv_p && sfk != sfk_none) 16374 *ctor_dtor_or_conv_p = -1; 16375 } 16376 } 16377 declarator = make_id_declarator (qualifying_scope, 16378 unqualified_name, 16379 sfk); 16380 declarator->id_loc = token->location; 16381 declarator->parameter_pack_p = pack_expansion_p; 16382 16383 if (pack_expansion_p) 16384 maybe_warn_variadic_templates (); 16385 } 16386 16387 handle_declarator:; 16388 scope = get_scope_of_declarator (declarator); 16389 if (scope) 16390 /* Any names that appear after the declarator-id for a 16391 member are looked up in the containing scope. */ 16392 pushed_scope = push_scope (scope); 16393 parser->in_declarator_p = true; 16394 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p) 16395 || (declarator && declarator->kind == cdk_id)) 16396 /* Default args are only allowed on function 16397 declarations. */ 16398 parser->default_arg_ok_p = saved_default_arg_ok_p; 16399 else 16400 parser->default_arg_ok_p = false; 16401 16402 first = false; 16403 } 16404 /* We're done. */ 16405 else 16406 break; 16407 } 16408 16409 /* For an abstract declarator, we might wind up with nothing at this 16410 point. That's an error; the declarator is not optional. */ 16411 if (!declarator) 16412 cp_parser_error (parser, "expected declarator"); 16413 16414 /* If we entered a scope, we must exit it now. */ 16415 if (pushed_scope) 16416 pop_scope (pushed_scope); 16417 16418 parser->default_arg_ok_p = saved_default_arg_ok_p; 16419 parser->in_declarator_p = saved_in_declarator_p; 16420 16421 return declarator; 16422 } 16423 16424 /* Parse a ptr-operator. 16425 16426 ptr-operator: 16427 * cv-qualifier-seq [opt] 16428 & 16429 :: [opt] nested-name-specifier * cv-qualifier-seq [opt] 16430 16431 GNU Extension: 16432 16433 ptr-operator: 16434 & cv-qualifier-seq [opt] 16435 16436 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used. 16437 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for 16438 an rvalue reference. In the case of a pointer-to-member, *TYPE is 16439 filled in with the TYPE containing the member. *CV_QUALS is 16440 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there 16441 are no cv-qualifiers. Returns ERROR_MARK if an error occurred. 16442 Note that the tree codes returned by this function have nothing 16443 to do with the types of trees that will be eventually be created 16444 to represent the pointer or reference type being parsed. They are 16445 just constants with suggestive names. */ 16446 static enum tree_code 16447 cp_parser_ptr_operator (cp_parser* parser, 16448 tree* type, 16449 cp_cv_quals *cv_quals) 16450 { 16451 enum tree_code code = ERROR_MARK; 16452 cp_token *token; 16453 16454 /* Assume that it's not a pointer-to-member. */ 16455 *type = NULL_TREE; 16456 /* And that there are no cv-qualifiers. */ 16457 *cv_quals = TYPE_UNQUALIFIED; 16458 16459 /* Peek at the next token. */ 16460 token = cp_lexer_peek_token (parser->lexer); 16461 16462 /* If it's a `*', `&' or `&&' we have a pointer or reference. */ 16463 if (token->type == CPP_MULT) 16464 code = INDIRECT_REF; 16465 else if (token->type == CPP_AND) 16466 code = ADDR_EXPR; 16467 else if ((cxx_dialect != cxx98) && 16468 token->type == CPP_AND_AND) /* C++0x only */ 16469 code = NON_LVALUE_EXPR; 16470 16471 if (code != ERROR_MARK) 16472 { 16473 /* Consume the `*', `&' or `&&'. */ 16474 cp_lexer_consume_token (parser->lexer); 16475 16476 /* A `*' can be followed by a cv-qualifier-seq, and so can a 16477 `&', if we are allowing GNU extensions. (The only qualifier 16478 that can legally appear after `&' is `restrict', but that is 16479 enforced during semantic analysis. */ 16480 if (code == INDIRECT_REF 16481 || cp_parser_allow_gnu_extensions_p (parser)) 16482 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); 16483 } 16484 else 16485 { 16486 /* Try the pointer-to-member case. */ 16487 cp_parser_parse_tentatively (parser); 16488 /* Look for the optional `::' operator. */ 16489 cp_parser_global_scope_opt (parser, 16490 /*current_scope_valid_p=*/false); 16491 /* Look for the nested-name specifier. */ 16492 token = cp_lexer_peek_token (parser->lexer); 16493 cp_parser_nested_name_specifier (parser, 16494 /*typename_keyword_p=*/false, 16495 /*check_dependency_p=*/true, 16496 /*type_p=*/false, 16497 /*is_declaration=*/false); 16498 /* If we found it, and the next token is a `*', then we are 16499 indeed looking at a pointer-to-member operator. */ 16500 if (!cp_parser_error_occurred (parser) 16501 && cp_parser_require (parser, CPP_MULT, RT_MULT)) 16502 { 16503 /* Indicate that the `*' operator was used. */ 16504 code = INDIRECT_REF; 16505 16506 if (TREE_CODE (parser->scope) == NAMESPACE_DECL) 16507 error_at (token->location, "%qD is a namespace", parser->scope); 16508 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE) 16509 error_at (token->location, "cannot form pointer to member of " 16510 "non-class %q#T", parser->scope); 16511 else 16512 { 16513 /* The type of which the member is a member is given by the 16514 current SCOPE. */ 16515 *type = parser->scope; 16516 /* The next name will not be qualified. */ 16517 parser->scope = NULL_TREE; 16518 parser->qualifying_scope = NULL_TREE; 16519 parser->object_scope = NULL_TREE; 16520 /* Look for the optional cv-qualifier-seq. */ 16521 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser); 16522 } 16523 } 16524 /* If that didn't work we don't have a ptr-operator. */ 16525 if (!cp_parser_parse_definitely (parser)) 16526 cp_parser_error (parser, "expected ptr-operator"); 16527 } 16528 16529 return code; 16530 } 16531 16532 /* Parse an (optional) cv-qualifier-seq. 16533 16534 cv-qualifier-seq: 16535 cv-qualifier cv-qualifier-seq [opt] 16536 16537 cv-qualifier: 16538 const 16539 volatile 16540 16541 GNU Extension: 16542 16543 cv-qualifier: 16544 __restrict__ 16545 16546 Returns a bitmask representing the cv-qualifiers. */ 16547 16548 static cp_cv_quals 16549 cp_parser_cv_qualifier_seq_opt (cp_parser* parser) 16550 { 16551 cp_cv_quals cv_quals = TYPE_UNQUALIFIED; 16552 16553 while (true) 16554 { 16555 cp_token *token; 16556 cp_cv_quals cv_qualifier; 16557 16558 /* Peek at the next token. */ 16559 token = cp_lexer_peek_token (parser->lexer); 16560 /* See if it's a cv-qualifier. */ 16561 switch (token->keyword) 16562 { 16563 case RID_CONST: 16564 cv_qualifier = TYPE_QUAL_CONST; 16565 break; 16566 16567 case RID_VOLATILE: 16568 cv_qualifier = TYPE_QUAL_VOLATILE; 16569 break; 16570 16571 case RID_RESTRICT: 16572 cv_qualifier = TYPE_QUAL_RESTRICT; 16573 break; 16574 16575 default: 16576 cv_qualifier = TYPE_UNQUALIFIED; 16577 break; 16578 } 16579 16580 if (!cv_qualifier) 16581 break; 16582 16583 if (cv_quals & cv_qualifier) 16584 { 16585 error_at (token->location, "duplicate cv-qualifier"); 16586 cp_lexer_purge_token (parser->lexer); 16587 } 16588 else 16589 { 16590 cp_lexer_consume_token (parser->lexer); 16591 cv_quals |= cv_qualifier; 16592 } 16593 } 16594 16595 return cv_quals; 16596 } 16597 16598 /* Parse an (optional) virt-specifier-seq. 16599 16600 virt-specifier-seq: 16601 virt-specifier virt-specifier-seq [opt] 16602 16603 virt-specifier: 16604 override 16605 final 16606 16607 Returns a bitmask representing the virt-specifiers. */ 16608 16609 static cp_virt_specifiers 16610 cp_parser_virt_specifier_seq_opt (cp_parser* parser) 16611 { 16612 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED; 16613 16614 while (true) 16615 { 16616 cp_token *token; 16617 cp_virt_specifiers virt_specifier; 16618 16619 /* Peek at the next token. */ 16620 token = cp_lexer_peek_token (parser->lexer); 16621 /* See if it's a virt-specifier-qualifier. */ 16622 if (token->type != CPP_NAME) 16623 break; 16624 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override")) 16625 { 16626 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS); 16627 virt_specifier = VIRT_SPEC_OVERRIDE; 16628 } 16629 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final")) 16630 { 16631 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS); 16632 virt_specifier = VIRT_SPEC_FINAL; 16633 } 16634 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final")) 16635 { 16636 virt_specifier = VIRT_SPEC_FINAL; 16637 } 16638 else 16639 break; 16640 16641 if (virt_specifiers & virt_specifier) 16642 { 16643 error_at (token->location, "duplicate virt-specifier"); 16644 cp_lexer_purge_token (parser->lexer); 16645 } 16646 else 16647 { 16648 cp_lexer_consume_token (parser->lexer); 16649 virt_specifiers |= virt_specifier; 16650 } 16651 } 16652 return virt_specifiers; 16653 } 16654 16655 /* Used by handling of trailing-return-types and NSDMI, in which 'this' 16656 is in scope even though it isn't real. */ 16657 16658 static void 16659 inject_this_parameter (tree ctype, cp_cv_quals quals) 16660 { 16661 tree this_parm; 16662 16663 if (current_class_ptr) 16664 { 16665 /* We don't clear this between NSDMIs. Is it already what we want? */ 16666 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr)); 16667 if (same_type_ignoring_top_level_qualifiers_p (ctype, type) 16668 && cp_type_quals (type) == quals) 16669 return; 16670 } 16671 16672 this_parm = build_this_parm (ctype, quals); 16673 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */ 16674 current_class_ptr = NULL_TREE; 16675 current_class_ref 16676 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error); 16677 current_class_ptr = this_parm; 16678 } 16679 16680 /* Parse a late-specified return type, if any. This is not a separate 16681 non-terminal, but part of a function declarator, which looks like 16682 16683 -> trailing-type-specifier-seq abstract-declarator(opt) 16684 16685 Returns the type indicated by the type-id. 16686 16687 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member 16688 function. */ 16689 16690 static tree 16691 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals) 16692 { 16693 cp_token *token; 16694 tree type; 16695 16696 /* Peek at the next token. */ 16697 token = cp_lexer_peek_token (parser->lexer); 16698 /* A late-specified return type is indicated by an initial '->'. */ 16699 if (token->type != CPP_DEREF) 16700 return NULL_TREE; 16701 16702 /* Consume the ->. */ 16703 cp_lexer_consume_token (parser->lexer); 16704 16705 tree save_ccp = current_class_ptr; 16706 tree save_ccr = current_class_ref; 16707 if (quals >= 0) 16708 { 16709 /* DR 1207: 'this' is in scope in the trailing return type. */ 16710 inject_this_parameter (current_class_type, quals); 16711 } 16712 16713 type = cp_parser_trailing_type_id (parser); 16714 16715 if (quals >= 0) 16716 { 16717 current_class_ptr = save_ccp; 16718 current_class_ref = save_ccr; 16719 } 16720 16721 return type; 16722 } 16723 16724 /* Parse a declarator-id. 16725 16726 declarator-id: 16727 id-expression 16728 :: [opt] nested-name-specifier [opt] type-name 16729 16730 In the `id-expression' case, the value returned is as for 16731 cp_parser_id_expression if the id-expression was an unqualified-id. 16732 If the id-expression was a qualified-id, then a SCOPE_REF is 16733 returned. The first operand is the scope (either a NAMESPACE_DECL 16734 or TREE_TYPE), but the second is still just a representation of an 16735 unqualified-id. */ 16736 16737 static tree 16738 cp_parser_declarator_id (cp_parser* parser, bool optional_p) 16739 { 16740 tree id; 16741 /* The expression must be an id-expression. Assume that qualified 16742 names are the names of types so that: 16743 16744 template <class T> 16745 int S<T>::R::i = 3; 16746 16747 will work; we must treat `S<T>::R' as the name of a type. 16748 Similarly, assume that qualified names are templates, where 16749 required, so that: 16750 16751 template <class T> 16752 int S<T>::R<T>::i = 3; 16753 16754 will work, too. */ 16755 id = cp_parser_id_expression (parser, 16756 /*template_keyword_p=*/false, 16757 /*check_dependency_p=*/false, 16758 /*template_p=*/NULL, 16759 /*declarator_p=*/true, 16760 optional_p); 16761 if (id && BASELINK_P (id)) 16762 id = BASELINK_FUNCTIONS (id); 16763 return id; 16764 } 16765 16766 /* Parse a type-id. 16767 16768 type-id: 16769 type-specifier-seq abstract-declarator [opt] 16770 16771 Returns the TYPE specified. */ 16772 16773 static tree 16774 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg, 16775 bool is_trailing_return) 16776 { 16777 cp_decl_specifier_seq type_specifier_seq; 16778 cp_declarator *abstract_declarator; 16779 16780 /* Parse the type-specifier-seq. */ 16781 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false, 16782 is_trailing_return, 16783 &type_specifier_seq); 16784 if (type_specifier_seq.type == error_mark_node) 16785 return error_mark_node; 16786 16787 /* There might or might not be an abstract declarator. */ 16788 cp_parser_parse_tentatively (parser); 16789 /* Look for the declarator. */ 16790 abstract_declarator 16791 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL, 16792 /*parenthesized_p=*/NULL, 16793 /*member_p=*/false); 16794 /* Check to see if there really was a declarator. */ 16795 if (!cp_parser_parse_definitely (parser)) 16796 abstract_declarator = NULL; 16797 16798 if (type_specifier_seq.type 16799 && type_uses_auto (type_specifier_seq.type)) 16800 { 16801 /* A type-id with type 'auto' is only ok if the abstract declarator 16802 is a function declarator with a late-specified return type. */ 16803 if (abstract_declarator 16804 && abstract_declarator->kind == cdk_function 16805 && abstract_declarator->u.function.late_return_type) 16806 /* OK */; 16807 else 16808 { 16809 error ("invalid use of %<auto%>"); 16810 return error_mark_node; 16811 } 16812 } 16813 16814 return groktypename (&type_specifier_seq, abstract_declarator, 16815 is_template_arg); 16816 } 16817 16818 static tree cp_parser_type_id (cp_parser *parser) 16819 { 16820 return cp_parser_type_id_1 (parser, false, false); 16821 } 16822 16823 static tree cp_parser_template_type_arg (cp_parser *parser) 16824 { 16825 tree r; 16826 const char *saved_message = parser->type_definition_forbidden_message; 16827 parser->type_definition_forbidden_message 16828 = G_("types may not be defined in template arguments"); 16829 r = cp_parser_type_id_1 (parser, true, false); 16830 parser->type_definition_forbidden_message = saved_message; 16831 return r; 16832 } 16833 16834 static tree cp_parser_trailing_type_id (cp_parser *parser) 16835 { 16836 return cp_parser_type_id_1 (parser, false, true); 16837 } 16838 16839 /* Parse a type-specifier-seq. 16840 16841 type-specifier-seq: 16842 type-specifier type-specifier-seq [opt] 16843 16844 GNU extension: 16845 16846 type-specifier-seq: 16847 attributes type-specifier-seq [opt] 16848 16849 If IS_DECLARATION is true, we are at the start of a "condition" or 16850 exception-declaration, so we might be followed by a declarator-id. 16851 16852 If IS_TRAILING_RETURN is true, we are in a trailing-return-type, 16853 i.e. we've just seen "->". 16854 16855 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */ 16856 16857 static void 16858 cp_parser_type_specifier_seq (cp_parser* parser, 16859 bool is_declaration, 16860 bool is_trailing_return, 16861 cp_decl_specifier_seq *type_specifier_seq) 16862 { 16863 bool seen_type_specifier = false; 16864 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL; 16865 cp_token *start_token = NULL; 16866 16867 /* Clear the TYPE_SPECIFIER_SEQ. */ 16868 clear_decl_specs (type_specifier_seq); 16869 16870 /* In the context of a trailing return type, enum E { } is an 16871 elaborated-type-specifier followed by a function-body, not an 16872 enum-specifier. */ 16873 if (is_trailing_return) 16874 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS; 16875 16876 /* Parse the type-specifiers and attributes. */ 16877 while (true) 16878 { 16879 tree type_specifier; 16880 bool is_cv_qualifier; 16881 16882 /* Check for attributes first. */ 16883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) 16884 { 16885 type_specifier_seq->attributes = 16886 chainon (type_specifier_seq->attributes, 16887 cp_parser_attributes_opt (parser)); 16888 continue; 16889 } 16890 16891 /* record the token of the beginning of the type specifier seq, 16892 for error reporting purposes*/ 16893 if (!start_token) 16894 start_token = cp_lexer_peek_token (parser->lexer); 16895 16896 /* Look for the type-specifier. */ 16897 type_specifier = cp_parser_type_specifier (parser, 16898 flags, 16899 type_specifier_seq, 16900 /*is_declaration=*/false, 16901 NULL, 16902 &is_cv_qualifier); 16903 if (!type_specifier) 16904 { 16905 /* If the first type-specifier could not be found, this is not a 16906 type-specifier-seq at all. */ 16907 if (!seen_type_specifier) 16908 { 16909 cp_parser_error (parser, "expected type-specifier"); 16910 type_specifier_seq->type = error_mark_node; 16911 return; 16912 } 16913 /* If subsequent type-specifiers could not be found, the 16914 type-specifier-seq is complete. */ 16915 break; 16916 } 16917 16918 seen_type_specifier = true; 16919 /* The standard says that a condition can be: 16920 16921 type-specifier-seq declarator = assignment-expression 16922 16923 However, given: 16924 16925 struct S {}; 16926 if (int S = ...) 16927 16928 we should treat the "S" as a declarator, not as a 16929 type-specifier. The standard doesn't say that explicitly for 16930 type-specifier-seq, but it does say that for 16931 decl-specifier-seq in an ordinary declaration. Perhaps it 16932 would be clearer just to allow a decl-specifier-seq here, and 16933 then add a semantic restriction that if any decl-specifiers 16934 that are not type-specifiers appear, the program is invalid. */ 16935 if (is_declaration && !is_cv_qualifier) 16936 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; 16937 } 16938 16939 cp_parser_check_decl_spec (type_specifier_seq, start_token->location); 16940 } 16941 16942 /* Parse a parameter-declaration-clause. 16943 16944 parameter-declaration-clause: 16945 parameter-declaration-list [opt] ... [opt] 16946 parameter-declaration-list , ... 16947 16948 Returns a representation for the parameter declarations. A return 16949 value of NULL indicates a parameter-declaration-clause consisting 16950 only of an ellipsis. */ 16951 16952 static tree 16953 cp_parser_parameter_declaration_clause (cp_parser* parser) 16954 { 16955 tree parameters; 16956 cp_token *token; 16957 bool ellipsis_p; 16958 bool is_error; 16959 16960 /* Peek at the next token. */ 16961 token = cp_lexer_peek_token (parser->lexer); 16962 /* Check for trivial parameter-declaration-clauses. */ 16963 if (token->type == CPP_ELLIPSIS) 16964 { 16965 /* Consume the `...' token. */ 16966 cp_lexer_consume_token (parser->lexer); 16967 return NULL_TREE; 16968 } 16969 else if (token->type == CPP_CLOSE_PAREN) 16970 /* There are no parameters. */ 16971 { 16972 #ifndef NO_IMPLICIT_EXTERN_C 16973 if (in_system_header && current_class_type == NULL 16974 && current_lang_name == lang_name_c) 16975 return NULL_TREE; 16976 else 16977 #endif 16978 return void_list_node; 16979 } 16980 /* Check for `(void)', too, which is a special case. */ 16981 else if (token->keyword == RID_VOID 16982 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 16983 == CPP_CLOSE_PAREN)) 16984 { 16985 /* Consume the `void' token. */ 16986 cp_lexer_consume_token (parser->lexer); 16987 /* There are no parameters. */ 16988 return void_list_node; 16989 } 16990 16991 /* Parse the parameter-declaration-list. */ 16992 parameters = cp_parser_parameter_declaration_list (parser, &is_error); 16993 /* If a parse error occurred while parsing the 16994 parameter-declaration-list, then the entire 16995 parameter-declaration-clause is erroneous. */ 16996 if (is_error) 16997 return NULL; 16998 16999 /* Peek at the next token. */ 17000 token = cp_lexer_peek_token (parser->lexer); 17001 /* If it's a `,', the clause should terminate with an ellipsis. */ 17002 if (token->type == CPP_COMMA) 17003 { 17004 /* Consume the `,'. */ 17005 cp_lexer_consume_token (parser->lexer); 17006 /* Expect an ellipsis. */ 17007 ellipsis_p 17008 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL); 17009 } 17010 /* It might also be `...' if the optional trailing `,' was 17011 omitted. */ 17012 else if (token->type == CPP_ELLIPSIS) 17013 { 17014 /* Consume the `...' token. */ 17015 cp_lexer_consume_token (parser->lexer); 17016 /* And remember that we saw it. */ 17017 ellipsis_p = true; 17018 } 17019 else 17020 ellipsis_p = false; 17021 17022 /* Finish the parameter list. */ 17023 if (!ellipsis_p) 17024 parameters = chainon (parameters, void_list_node); 17025 17026 return parameters; 17027 } 17028 17029 /* Parse a parameter-declaration-list. 17030 17031 parameter-declaration-list: 17032 parameter-declaration 17033 parameter-declaration-list , parameter-declaration 17034 17035 Returns a representation of the parameter-declaration-list, as for 17036 cp_parser_parameter_declaration_clause. However, the 17037 `void_list_node' is never appended to the list. Upon return, 17038 *IS_ERROR will be true iff an error occurred. */ 17039 17040 static tree 17041 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error) 17042 { 17043 tree parameters = NULL_TREE; 17044 tree *tail = ¶meters; 17045 bool saved_in_unbraced_linkage_specification_p; 17046 int index = 0; 17047 17048 /* Assume all will go well. */ 17049 *is_error = false; 17050 /* The special considerations that apply to a function within an 17051 unbraced linkage specifications do not apply to the parameters 17052 to the function. */ 17053 saved_in_unbraced_linkage_specification_p 17054 = parser->in_unbraced_linkage_specification_p; 17055 parser->in_unbraced_linkage_specification_p = false; 17056 17057 /* Look for more parameters. */ 17058 while (true) 17059 { 17060 cp_parameter_declarator *parameter; 17061 tree decl = error_mark_node; 17062 bool parenthesized_p = false; 17063 /* Parse the parameter. */ 17064 parameter 17065 = cp_parser_parameter_declaration (parser, 17066 /*template_parm_p=*/false, 17067 &parenthesized_p); 17068 17069 /* We don't know yet if the enclosing context is deprecated, so wait 17070 and warn in grokparms if appropriate. */ 17071 deprecated_state = DEPRECATED_SUPPRESS; 17072 17073 if (parameter) 17074 decl = grokdeclarator (parameter->declarator, 17075 ¶meter->decl_specifiers, 17076 PARM, 17077 parameter->default_argument != NULL_TREE, 17078 ¶meter->decl_specifiers.attributes); 17079 17080 deprecated_state = DEPRECATED_NORMAL; 17081 17082 /* If a parse error occurred parsing the parameter declaration, 17083 then the entire parameter-declaration-list is erroneous. */ 17084 if (decl == error_mark_node) 17085 { 17086 *is_error = true; 17087 parameters = error_mark_node; 17088 break; 17089 } 17090 17091 if (parameter->decl_specifiers.attributes) 17092 cplus_decl_attributes (&decl, 17093 parameter->decl_specifiers.attributes, 17094 0); 17095 if (DECL_NAME (decl)) 17096 decl = pushdecl (decl); 17097 17098 if (decl != error_mark_node) 17099 { 17100 retrofit_lang_decl (decl); 17101 DECL_PARM_INDEX (decl) = ++index; 17102 DECL_PARM_LEVEL (decl) = function_parm_depth (); 17103 } 17104 17105 /* Add the new parameter to the list. */ 17106 *tail = build_tree_list (parameter->default_argument, decl); 17107 tail = &TREE_CHAIN (*tail); 17108 17109 /* Peek at the next token. */ 17110 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN) 17111 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS) 17112 /* These are for Objective-C++ */ 17113 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 17114 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 17115 /* The parameter-declaration-list is complete. */ 17116 break; 17117 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 17118 { 17119 cp_token *token; 17120 17121 /* Peek at the next token. */ 17122 token = cp_lexer_peek_nth_token (parser->lexer, 2); 17123 /* If it's an ellipsis, then the list is complete. */ 17124 if (token->type == CPP_ELLIPSIS) 17125 break; 17126 /* Otherwise, there must be more parameters. Consume the 17127 `,'. */ 17128 cp_lexer_consume_token (parser->lexer); 17129 /* When parsing something like: 17130 17131 int i(float f, double d) 17132 17133 we can tell after seeing the declaration for "f" that we 17134 are not looking at an initialization of a variable "i", 17135 but rather at the declaration of a function "i". 17136 17137 Due to the fact that the parsing of template arguments 17138 (as specified to a template-id) requires backtracking we 17139 cannot use this technique when inside a template argument 17140 list. */ 17141 if (!parser->in_template_argument_list_p 17142 && !parser->in_type_id_in_expr_p 17143 && cp_parser_uncommitted_to_tentative_parse_p (parser) 17144 /* However, a parameter-declaration of the form 17145 "foat(f)" (which is a valid declaration of a 17146 parameter "f") can also be interpreted as an 17147 expression (the conversion of "f" to "float"). */ 17148 && !parenthesized_p) 17149 cp_parser_commit_to_tentative_parse (parser); 17150 } 17151 else 17152 { 17153 cp_parser_error (parser, "expected %<,%> or %<...%>"); 17154 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) 17155 cp_parser_skip_to_closing_parenthesis (parser, 17156 /*recovering=*/true, 17157 /*or_comma=*/false, 17158 /*consume_paren=*/false); 17159 break; 17160 } 17161 } 17162 17163 parser->in_unbraced_linkage_specification_p 17164 = saved_in_unbraced_linkage_specification_p; 17165 17166 return parameters; 17167 } 17168 17169 /* Parse a parameter declaration. 17170 17171 parameter-declaration: 17172 decl-specifier-seq ... [opt] declarator 17173 decl-specifier-seq declarator = assignment-expression 17174 decl-specifier-seq ... [opt] abstract-declarator [opt] 17175 decl-specifier-seq abstract-declarator [opt] = assignment-expression 17176 17177 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration 17178 declares a template parameter. (In that case, a non-nested `>' 17179 token encountered during the parsing of the assignment-expression 17180 is not interpreted as a greater-than operator.) 17181 17182 Returns a representation of the parameter, or NULL if an error 17183 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to 17184 true iff the declarator is of the form "(p)". */ 17185 17186 static cp_parameter_declarator * 17187 cp_parser_parameter_declaration (cp_parser *parser, 17188 bool template_parm_p, 17189 bool *parenthesized_p) 17190 { 17191 int declares_class_or_enum; 17192 cp_decl_specifier_seq decl_specifiers; 17193 cp_declarator *declarator; 17194 tree default_argument; 17195 cp_token *token = NULL, *declarator_token_start = NULL; 17196 const char *saved_message; 17197 17198 /* In a template parameter, `>' is not an operator. 17199 17200 [temp.param] 17201 17202 When parsing a default template-argument for a non-type 17203 template-parameter, the first non-nested `>' is taken as the end 17204 of the template parameter-list rather than a greater-than 17205 operator. */ 17206 17207 /* Type definitions may not appear in parameter types. */ 17208 saved_message = parser->type_definition_forbidden_message; 17209 parser->type_definition_forbidden_message 17210 = G_("types may not be defined in parameter types"); 17211 17212 /* Parse the declaration-specifiers. */ 17213 cp_parser_decl_specifier_seq (parser, 17214 CP_PARSER_FLAGS_NONE, 17215 &decl_specifiers, 17216 &declares_class_or_enum); 17217 17218 /* Complain about missing 'typename' or other invalid type names. */ 17219 if (!decl_specifiers.any_type_specifiers_p) 17220 cp_parser_parse_and_diagnose_invalid_type_name (parser); 17221 17222 /* If an error occurred, there's no reason to attempt to parse the 17223 rest of the declaration. */ 17224 if (cp_parser_error_occurred (parser)) 17225 { 17226 parser->type_definition_forbidden_message = saved_message; 17227 return NULL; 17228 } 17229 17230 /* Peek at the next token. */ 17231 token = cp_lexer_peek_token (parser->lexer); 17232 17233 /* If the next token is a `)', `,', `=', `>', or `...', then there 17234 is no declarator. However, when variadic templates are enabled, 17235 there may be a declarator following `...'. */ 17236 if (token->type == CPP_CLOSE_PAREN 17237 || token->type == CPP_COMMA 17238 || token->type == CPP_EQ 17239 || token->type == CPP_GREATER) 17240 { 17241 declarator = NULL; 17242 if (parenthesized_p) 17243 *parenthesized_p = false; 17244 } 17245 /* Otherwise, there should be a declarator. */ 17246 else 17247 { 17248 bool saved_default_arg_ok_p = parser->default_arg_ok_p; 17249 parser->default_arg_ok_p = false; 17250 17251 /* After seeing a decl-specifier-seq, if the next token is not a 17252 "(", there is no possibility that the code is a valid 17253 expression. Therefore, if parsing tentatively, we commit at 17254 this point. */ 17255 if (!parser->in_template_argument_list_p 17256 /* In an expression context, having seen: 17257 17258 (int((char ... 17259 17260 we cannot be sure whether we are looking at a 17261 function-type (taking a "char" as a parameter) or a cast 17262 of some object of type "char" to "int". */ 17263 && !parser->in_type_id_in_expr_p 17264 && cp_parser_uncommitted_to_tentative_parse_p (parser) 17265 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) 17266 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)) 17267 cp_parser_commit_to_tentative_parse (parser); 17268 /* Parse the declarator. */ 17269 declarator_token_start = token; 17270 declarator = cp_parser_declarator (parser, 17271 CP_PARSER_DECLARATOR_EITHER, 17272 /*ctor_dtor_or_conv_p=*/NULL, 17273 parenthesized_p, 17274 /*member_p=*/false); 17275 parser->default_arg_ok_p = saved_default_arg_ok_p; 17276 /* After the declarator, allow more attributes. */ 17277 decl_specifiers.attributes 17278 = chainon (decl_specifiers.attributes, 17279 cp_parser_attributes_opt (parser)); 17280 } 17281 17282 /* If the next token is an ellipsis, and we have not seen a 17283 declarator name, and the type of the declarator contains parameter 17284 packs but it is not a TYPE_PACK_EXPANSION, then we actually have 17285 a parameter pack expansion expression. Otherwise, leave the 17286 ellipsis for a C-style variadic function. */ 17287 token = cp_lexer_peek_token (parser->lexer); 17288 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 17289 { 17290 tree type = decl_specifiers.type; 17291 17292 if (type && DECL_P (type)) 17293 type = TREE_TYPE (type); 17294 17295 if (type 17296 && TREE_CODE (type) != TYPE_PACK_EXPANSION 17297 && declarator_can_be_parameter_pack (declarator) 17298 && (!declarator || !declarator->parameter_pack_p) 17299 && uses_parameter_packs (type)) 17300 { 17301 /* Consume the `...'. */ 17302 cp_lexer_consume_token (parser->lexer); 17303 maybe_warn_variadic_templates (); 17304 17305 /* Build a pack expansion type */ 17306 if (declarator) 17307 declarator->parameter_pack_p = true; 17308 else 17309 decl_specifiers.type = make_pack_expansion (type); 17310 } 17311 } 17312 17313 /* The restriction on defining new types applies only to the type 17314 of the parameter, not to the default argument. */ 17315 parser->type_definition_forbidden_message = saved_message; 17316 17317 /* If the next token is `=', then process a default argument. */ 17318 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 17319 { 17320 token = cp_lexer_peek_token (parser->lexer); 17321 /* If we are defining a class, then the tokens that make up the 17322 default argument must be saved and processed later. */ 17323 if (!template_parm_p && at_class_scope_p () 17324 && TYPE_BEING_DEFINED (current_class_type) 17325 && !LAMBDA_TYPE_P (current_class_type)) 17326 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false); 17327 /* Outside of a class definition, we can just parse the 17328 assignment-expression. */ 17329 else 17330 default_argument 17331 = cp_parser_default_argument (parser, template_parm_p); 17332 17333 if (!parser->default_arg_ok_p) 17334 { 17335 if (flag_permissive) 17336 warning (0, "deprecated use of default argument for parameter of non-function"); 17337 else 17338 { 17339 error_at (token->location, 17340 "default arguments are only " 17341 "permitted for function parameters"); 17342 default_argument = NULL_TREE; 17343 } 17344 } 17345 else if ((declarator && declarator->parameter_pack_p) 17346 || (decl_specifiers.type 17347 && PACK_EXPANSION_P (decl_specifiers.type))) 17348 { 17349 /* Find the name of the parameter pack. */ 17350 cp_declarator *id_declarator = declarator; 17351 while (id_declarator && id_declarator->kind != cdk_id) 17352 id_declarator = id_declarator->declarator; 17353 17354 if (id_declarator && id_declarator->kind == cdk_id) 17355 error_at (declarator_token_start->location, 17356 template_parm_p 17357 ? G_("template parameter pack %qD " 17358 "cannot have a default argument") 17359 : G_("parameter pack %qD cannot have " 17360 "a default argument"), 17361 id_declarator->u.id.unqualified_name); 17362 else 17363 error_at (declarator_token_start->location, 17364 template_parm_p 17365 ? G_("template parameter pack cannot have " 17366 "a default argument") 17367 : G_("parameter pack cannot have a " 17368 "default argument")); 17369 17370 default_argument = NULL_TREE; 17371 } 17372 } 17373 else 17374 default_argument = NULL_TREE; 17375 17376 return make_parameter_declarator (&decl_specifiers, 17377 declarator, 17378 default_argument); 17379 } 17380 17381 /* Parse a default argument and return it. 17382 17383 TEMPLATE_PARM_P is true if this is a default argument for a 17384 non-type template parameter. */ 17385 static tree 17386 cp_parser_default_argument (cp_parser *parser, bool template_parm_p) 17387 { 17388 tree default_argument = NULL_TREE; 17389 bool saved_greater_than_is_operator_p; 17390 bool saved_local_variables_forbidden_p; 17391 bool non_constant_p, is_direct_init; 17392 17393 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is 17394 set correctly. */ 17395 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p; 17396 parser->greater_than_is_operator_p = !template_parm_p; 17397 /* Local variable names (and the `this' keyword) may not 17398 appear in a default argument. */ 17399 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; 17400 parser->local_variables_forbidden_p = true; 17401 /* Parse the assignment-expression. */ 17402 if (template_parm_p) 17403 push_deferring_access_checks (dk_no_deferred); 17404 default_argument 17405 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p); 17406 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument)) 17407 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 17408 if (template_parm_p) 17409 pop_deferring_access_checks (); 17410 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p; 17411 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; 17412 17413 return default_argument; 17414 } 17415 17416 /* Parse a function-body. 17417 17418 function-body: 17419 compound_statement */ 17420 17421 static void 17422 cp_parser_function_body (cp_parser *parser) 17423 { 17424 cp_parser_compound_statement (parser, NULL, false, true); 17425 } 17426 17427 /* Parse a ctor-initializer-opt followed by a function-body. Return 17428 true if a ctor-initializer was present. */ 17429 17430 static bool 17431 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser) 17432 { 17433 tree body, list; 17434 bool ctor_initializer_p; 17435 const bool check_body_p = 17436 DECL_CONSTRUCTOR_P (current_function_decl) 17437 && DECL_DECLARED_CONSTEXPR_P (current_function_decl); 17438 tree last = NULL; 17439 17440 /* Begin the function body. */ 17441 body = begin_function_body (); 17442 /* Parse the optional ctor-initializer. */ 17443 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser); 17444 17445 /* If we're parsing a constexpr constructor definition, we need 17446 to check that the constructor body is indeed empty. However, 17447 before we get to cp_parser_function_body lot of junk has been 17448 generated, so we can't just check that we have an empty block. 17449 Rather we take a snapshot of the outermost block, and check whether 17450 cp_parser_function_body changed its state. */ 17451 if (check_body_p) 17452 { 17453 list = cur_stmt_list; 17454 if (STATEMENT_LIST_TAIL (list)) 17455 last = STATEMENT_LIST_TAIL (list)->stmt; 17456 } 17457 /* Parse the function-body. */ 17458 cp_parser_function_body (parser); 17459 if (check_body_p) 17460 check_constexpr_ctor_body (last, list); 17461 /* Finish the function body. */ 17462 finish_function_body (body); 17463 17464 return ctor_initializer_p; 17465 } 17466 17467 /* Parse an initializer. 17468 17469 initializer: 17470 = initializer-clause 17471 ( expression-list ) 17472 17473 Returns an expression representing the initializer. If no 17474 initializer is present, NULL_TREE is returned. 17475 17476 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause' 17477 production is used, and TRUE otherwise. *IS_DIRECT_INIT is 17478 set to TRUE if there is no initializer present. If there is an 17479 initializer, and it is not a constant-expression, *NON_CONSTANT_P 17480 is set to true; otherwise it is set to false. */ 17481 17482 static tree 17483 cp_parser_initializer (cp_parser* parser, bool* is_direct_init, 17484 bool* non_constant_p) 17485 { 17486 cp_token *token; 17487 tree init; 17488 17489 /* Peek at the next token. */ 17490 token = cp_lexer_peek_token (parser->lexer); 17491 17492 /* Let our caller know whether or not this initializer was 17493 parenthesized. */ 17494 *is_direct_init = (token->type != CPP_EQ); 17495 /* Assume that the initializer is constant. */ 17496 *non_constant_p = false; 17497 17498 if (token->type == CPP_EQ) 17499 { 17500 /* Consume the `='. */ 17501 cp_lexer_consume_token (parser->lexer); 17502 /* Parse the initializer-clause. */ 17503 init = cp_parser_initializer_clause (parser, non_constant_p); 17504 } 17505 else if (token->type == CPP_OPEN_PAREN) 17506 { 17507 VEC(tree,gc) *vec; 17508 vec = cp_parser_parenthesized_expression_list (parser, non_attr, 17509 /*cast_p=*/false, 17510 /*allow_expansion_p=*/true, 17511 non_constant_p); 17512 if (vec == NULL) 17513 return error_mark_node; 17514 init = build_tree_list_vec (vec); 17515 release_tree_vector (vec); 17516 } 17517 else if (token->type == CPP_OPEN_BRACE) 17518 { 17519 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 17520 init = cp_parser_braced_list (parser, non_constant_p); 17521 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1; 17522 } 17523 else 17524 { 17525 /* Anything else is an error. */ 17526 cp_parser_error (parser, "expected initializer"); 17527 init = error_mark_node; 17528 } 17529 17530 return init; 17531 } 17532 17533 /* Parse an initializer-clause. 17534 17535 initializer-clause: 17536 assignment-expression 17537 braced-init-list 17538 17539 Returns an expression representing the initializer. 17540 17541 If the `assignment-expression' production is used the value 17542 returned is simply a representation for the expression. 17543 17544 Otherwise, calls cp_parser_braced_list. */ 17545 17546 static tree 17547 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p) 17548 { 17549 tree initializer; 17550 17551 /* Assume the expression is constant. */ 17552 *non_constant_p = false; 17553 17554 /* If it is not a `{', then we are looking at an 17555 assignment-expression. */ 17556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)) 17557 { 17558 initializer 17559 = cp_parser_constant_expression (parser, 17560 /*allow_non_constant_p=*/true, 17561 non_constant_p); 17562 } 17563 else 17564 initializer = cp_parser_braced_list (parser, non_constant_p); 17565 17566 return initializer; 17567 } 17568 17569 /* Parse a brace-enclosed initializer list. 17570 17571 braced-init-list: 17572 { initializer-list , [opt] } 17573 { } 17574 17575 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be 17576 the elements of the initializer-list (or NULL, if the last 17577 production is used). The TREE_TYPE for the CONSTRUCTOR will be 17578 NULL_TREE. There is no way to detect whether or not the optional 17579 trailing `,' was provided. NON_CONSTANT_P is as for 17580 cp_parser_initializer. */ 17581 17582 static tree 17583 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p) 17584 { 17585 tree initializer; 17586 17587 /* Consume the `{' token. */ 17588 cp_lexer_consume_token (parser->lexer); 17589 /* Create a CONSTRUCTOR to represent the braced-initializer. */ 17590 initializer = make_node (CONSTRUCTOR); 17591 /* If it's not a `}', then there is a non-trivial initializer. */ 17592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)) 17593 { 17594 /* Parse the initializer list. */ 17595 CONSTRUCTOR_ELTS (initializer) 17596 = cp_parser_initializer_list (parser, non_constant_p); 17597 /* A trailing `,' token is allowed. */ 17598 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 17599 cp_lexer_consume_token (parser->lexer); 17600 } 17601 /* Now, there should be a trailing `}'. */ 17602 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 17603 TREE_TYPE (initializer) = init_list_type_node; 17604 return initializer; 17605 } 17606 17607 /* Parse an initializer-list. 17608 17609 initializer-list: 17610 initializer-clause ... [opt] 17611 initializer-list , initializer-clause ... [opt] 17612 17613 GNU Extension: 17614 17615 initializer-list: 17616 designation initializer-clause ...[opt] 17617 initializer-list , designation initializer-clause ...[opt] 17618 17619 designation: 17620 . identifier = 17621 identifier : 17622 [ constant-expression ] = 17623 17624 Returns a VEC of constructor_elt. The VALUE of each elt is an expression 17625 for the initializer. If the INDEX of the elt is non-NULL, it is the 17626 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is 17627 as for cp_parser_initializer. */ 17628 17629 static VEC(constructor_elt,gc) * 17630 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p) 17631 { 17632 VEC(constructor_elt,gc) *v = NULL; 17633 17634 /* Assume all of the expressions are constant. */ 17635 *non_constant_p = false; 17636 17637 /* Parse the rest of the list. */ 17638 while (true) 17639 { 17640 cp_token *token; 17641 tree designator; 17642 tree initializer; 17643 bool clause_non_constant_p; 17644 17645 /* If the next token is an identifier and the following one is a 17646 colon, we are looking at the GNU designated-initializer 17647 syntax. */ 17648 if (cp_parser_allow_gnu_extensions_p (parser) 17649 && cp_lexer_next_token_is (parser->lexer, CPP_NAME) 17650 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON) 17651 { 17652 /* Warn the user that they are using an extension. */ 17653 pedwarn (input_location, OPT_pedantic, 17654 "ISO C++ does not allow designated initializers"); 17655 /* Consume the identifier. */ 17656 designator = cp_lexer_consume_token (parser->lexer)->u.value; 17657 /* Consume the `:'. */ 17658 cp_lexer_consume_token (parser->lexer); 17659 } 17660 /* Also handle the C99 syntax, '. id ='. */ 17661 else if (cp_parser_allow_gnu_extensions_p (parser) 17662 && cp_lexer_next_token_is (parser->lexer, CPP_DOT) 17663 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME 17664 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ) 17665 { 17666 /* Warn the user that they are using an extension. */ 17667 pedwarn (input_location, OPT_pedantic, 17668 "ISO C++ does not allow C99 designated initializers"); 17669 /* Consume the `.'. */ 17670 cp_lexer_consume_token (parser->lexer); 17671 /* Consume the identifier. */ 17672 designator = cp_lexer_consume_token (parser->lexer)->u.value; 17673 /* Consume the `='. */ 17674 cp_lexer_consume_token (parser->lexer); 17675 } 17676 /* Also handle C99 array designators, '[ const ] ='. */ 17677 else if (cp_parser_allow_gnu_extensions_p (parser) 17678 && !c_dialect_objc () 17679 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 17680 { 17681 /* In C++11, [ could start a lambda-introducer. */ 17682 bool non_const = false; 17683 17684 cp_parser_parse_tentatively (parser); 17685 cp_lexer_consume_token (parser->lexer); 17686 designator = cp_parser_constant_expression (parser, true, &non_const); 17687 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 17688 cp_parser_require (parser, CPP_EQ, RT_EQ); 17689 if (!cp_parser_parse_definitely (parser)) 17690 designator = NULL_TREE; 17691 else if (non_const) 17692 require_potential_rvalue_constant_expression (designator); 17693 } 17694 else 17695 designator = NULL_TREE; 17696 17697 /* Parse the initializer. */ 17698 initializer = cp_parser_initializer_clause (parser, 17699 &clause_non_constant_p); 17700 /* If any clause is non-constant, so is the entire initializer. */ 17701 if (clause_non_constant_p) 17702 *non_constant_p = true; 17703 17704 /* If we have an ellipsis, this is an initializer pack 17705 expansion. */ 17706 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 17707 { 17708 /* Consume the `...'. */ 17709 cp_lexer_consume_token (parser->lexer); 17710 17711 /* Turn the initializer into an initializer expansion. */ 17712 initializer = make_pack_expansion (initializer); 17713 } 17714 17715 /* Add it to the vector. */ 17716 CONSTRUCTOR_APPEND_ELT (v, designator, initializer); 17717 17718 /* If the next token is not a comma, we have reached the end of 17719 the list. */ 17720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 17721 break; 17722 17723 /* Peek at the next token. */ 17724 token = cp_lexer_peek_nth_token (parser->lexer, 2); 17725 /* If the next token is a `}', then we're still done. An 17726 initializer-clause can have a trailing `,' after the 17727 initializer-list and before the closing `}'. */ 17728 if (token->type == CPP_CLOSE_BRACE) 17729 break; 17730 17731 /* Consume the `,' token. */ 17732 cp_lexer_consume_token (parser->lexer); 17733 } 17734 17735 return v; 17736 } 17737 17738 /* Classes [gram.class] */ 17739 17740 /* Parse a class-name. 17741 17742 class-name: 17743 identifier 17744 template-id 17745 17746 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used 17747 to indicate that names looked up in dependent types should be 17748 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template' 17749 keyword has been used to indicate that the name that appears next 17750 is a template. TAG_TYPE indicates the explicit tag given before 17751 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are 17752 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class 17753 is the class being defined in a class-head. 17754 17755 Returns the TYPE_DECL representing the class. */ 17756 17757 static tree 17758 cp_parser_class_name (cp_parser *parser, 17759 bool typename_keyword_p, 17760 bool template_keyword_p, 17761 enum tag_types tag_type, 17762 bool check_dependency_p, 17763 bool class_head_p, 17764 bool is_declaration) 17765 { 17766 tree decl; 17767 tree scope; 17768 bool typename_p; 17769 cp_token *token; 17770 tree identifier = NULL_TREE; 17771 17772 /* All class-names start with an identifier. */ 17773 token = cp_lexer_peek_token (parser->lexer); 17774 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID) 17775 { 17776 cp_parser_error (parser, "expected class-name"); 17777 return error_mark_node; 17778 } 17779 17780 /* PARSER->SCOPE can be cleared when parsing the template-arguments 17781 to a template-id, so we save it here. */ 17782 scope = parser->scope; 17783 if (scope == error_mark_node) 17784 return error_mark_node; 17785 17786 /* Any name names a type if we're following the `typename' keyword 17787 in a qualified name where the enclosing scope is type-dependent. */ 17788 typename_p = (typename_keyword_p && scope && TYPE_P (scope) 17789 && dependent_type_p (scope)); 17790 /* Handle the common case (an identifier, but not a template-id) 17791 efficiently. */ 17792 if (token->type == CPP_NAME 17793 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2)) 17794 { 17795 cp_token *identifier_token; 17796 bool ambiguous_p; 17797 17798 /* Look for the identifier. */ 17799 identifier_token = cp_lexer_peek_token (parser->lexer); 17800 ambiguous_p = identifier_token->ambiguous_p; 17801 identifier = cp_parser_identifier (parser); 17802 /* If the next token isn't an identifier, we are certainly not 17803 looking at a class-name. */ 17804 if (identifier == error_mark_node) 17805 decl = error_mark_node; 17806 /* If we know this is a type-name, there's no need to look it 17807 up. */ 17808 else if (typename_p) 17809 decl = identifier; 17810 else 17811 { 17812 tree ambiguous_decls; 17813 /* If we already know that this lookup is ambiguous, then 17814 we've already issued an error message; there's no reason 17815 to check again. */ 17816 if (ambiguous_p) 17817 { 17818 cp_parser_simulate_error (parser); 17819 return error_mark_node; 17820 } 17821 /* If the next token is a `::', then the name must be a type 17822 name. 17823 17824 [basic.lookup.qual] 17825 17826 During the lookup for a name preceding the :: scope 17827 resolution operator, object, function, and enumerator 17828 names are ignored. */ 17829 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 17830 tag_type = typename_type; 17831 /* Look up the name. */ 17832 decl = cp_parser_lookup_name (parser, identifier, 17833 tag_type, 17834 /*is_template=*/false, 17835 /*is_namespace=*/false, 17836 check_dependency_p, 17837 &ambiguous_decls, 17838 identifier_token->location); 17839 if (ambiguous_decls) 17840 { 17841 if (cp_parser_parsing_tentatively (parser)) 17842 cp_parser_simulate_error (parser); 17843 return error_mark_node; 17844 } 17845 } 17846 } 17847 else 17848 { 17849 /* Try a template-id. */ 17850 decl = cp_parser_template_id (parser, template_keyword_p, 17851 check_dependency_p, 17852 is_declaration); 17853 if (decl == error_mark_node) 17854 return error_mark_node; 17855 } 17856 17857 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p); 17858 17859 /* If this is a typename, create a TYPENAME_TYPE. */ 17860 if (typename_p && decl != error_mark_node) 17861 { 17862 decl = make_typename_type (scope, decl, typename_type, 17863 /*complain=*/tf_error); 17864 if (decl != error_mark_node) 17865 decl = TYPE_NAME (decl); 17866 } 17867 17868 decl = strip_using_decl (decl); 17869 17870 /* Check to see that it is really the name of a class. */ 17871 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR 17872 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE 17873 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)) 17874 /* Situations like this: 17875 17876 template <typename T> struct A { 17877 typename T::template X<int>::I i; 17878 }; 17879 17880 are problematic. Is `T::template X<int>' a class-name? The 17881 standard does not seem to be definitive, but there is no other 17882 valid interpretation of the following `::'. Therefore, those 17883 names are considered class-names. */ 17884 { 17885 decl = make_typename_type (scope, decl, tag_type, tf_error); 17886 if (decl != error_mark_node) 17887 decl = TYPE_NAME (decl); 17888 } 17889 else if (TREE_CODE (decl) != TYPE_DECL 17890 || TREE_TYPE (decl) == error_mark_node 17891 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)) 17892 /* In Objective-C 2.0, a classname followed by '.' starts a 17893 dot-syntax expression, and it's not a type-name. */ 17894 || (c_dialect_objc () 17895 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 17896 && objc_is_class_name (decl))) 17897 decl = error_mark_node; 17898 17899 if (decl == error_mark_node) 17900 cp_parser_error (parser, "expected class-name"); 17901 else if (identifier && !parser->scope) 17902 maybe_note_name_used_in_class (identifier, decl); 17903 17904 return decl; 17905 } 17906 17907 /* Parse a class-specifier. 17908 17909 class-specifier: 17910 class-head { member-specification [opt] } 17911 17912 Returns the TREE_TYPE representing the class. */ 17913 17914 static tree 17915 cp_parser_class_specifier_1 (cp_parser* parser) 17916 { 17917 tree type; 17918 tree attributes = NULL_TREE; 17919 bool nested_name_specifier_p; 17920 unsigned saved_num_template_parameter_lists; 17921 bool saved_in_function_body; 17922 unsigned char in_statement; 17923 bool in_switch_statement_p; 17924 bool saved_in_unbraced_linkage_specification_p; 17925 tree old_scope = NULL_TREE; 17926 tree scope = NULL_TREE; 17927 cp_token *closing_brace; 17928 17929 push_deferring_access_checks (dk_no_deferred); 17930 17931 /* Parse the class-head. */ 17932 type = cp_parser_class_head (parser, 17933 &nested_name_specifier_p); 17934 /* If the class-head was a semantic disaster, skip the entire body 17935 of the class. */ 17936 if (!type) 17937 { 17938 cp_parser_skip_to_end_of_block_or_statement (parser); 17939 pop_deferring_access_checks (); 17940 return error_mark_node; 17941 } 17942 17943 /* Look for the `{'. */ 17944 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 17945 { 17946 pop_deferring_access_checks (); 17947 return error_mark_node; 17948 } 17949 17950 /* Issue an error message if type-definitions are forbidden here. */ 17951 cp_parser_check_type_definition (parser); 17952 /* Remember that we are defining one more class. */ 17953 ++parser->num_classes_being_defined; 17954 /* Inside the class, surrounding template-parameter-lists do not 17955 apply. */ 17956 saved_num_template_parameter_lists 17957 = parser->num_template_parameter_lists; 17958 parser->num_template_parameter_lists = 0; 17959 /* We are not in a function body. */ 17960 saved_in_function_body = parser->in_function_body; 17961 parser->in_function_body = false; 17962 /* Or in a loop. */ 17963 in_statement = parser->in_statement; 17964 parser->in_statement = 0; 17965 /* Or in a switch. */ 17966 in_switch_statement_p = parser->in_switch_statement_p; 17967 parser->in_switch_statement_p = false; 17968 /* We are not immediately inside an extern "lang" block. */ 17969 saved_in_unbraced_linkage_specification_p 17970 = parser->in_unbraced_linkage_specification_p; 17971 parser->in_unbraced_linkage_specification_p = false; 17972 17973 /* Start the class. */ 17974 if (nested_name_specifier_p) 17975 { 17976 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)); 17977 old_scope = push_inner_scope (scope); 17978 } 17979 type = begin_class_definition (type); 17980 17981 if (type == error_mark_node) 17982 /* If the type is erroneous, skip the entire body of the class. */ 17983 cp_parser_skip_to_closing_brace (parser); 17984 else 17985 /* Parse the member-specification. */ 17986 cp_parser_member_specification_opt (parser); 17987 17988 /* Look for the trailing `}'. */ 17989 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 17990 /* Look for trailing attributes to apply to this class. */ 17991 if (cp_parser_allow_gnu_extensions_p (parser)) 17992 attributes = cp_parser_attributes_opt (parser); 17993 if (type != error_mark_node) 17994 type = finish_struct (type, attributes); 17995 if (nested_name_specifier_p) 17996 pop_inner_scope (old_scope, scope); 17997 17998 /* We've finished a type definition. Check for the common syntax 17999 error of forgetting a semicolon after the definition. We need to 18000 be careful, as we can't just check for not-a-semicolon and be done 18001 with it; the user might have typed: 18002 18003 class X { } c = ...; 18004 class X { } *p = ...; 18005 18006 and so forth. Instead, enumerate all the possible tokens that 18007 might follow this production; if we don't see one of them, then 18008 complain and silently insert the semicolon. */ 18009 { 18010 cp_token *token = cp_lexer_peek_token (parser->lexer); 18011 bool want_semicolon = true; 18012 18013 switch (token->type) 18014 { 18015 case CPP_NAME: 18016 case CPP_SEMICOLON: 18017 case CPP_MULT: 18018 case CPP_AND: 18019 case CPP_OPEN_PAREN: 18020 case CPP_CLOSE_PAREN: 18021 case CPP_COMMA: 18022 want_semicolon = false; 18023 break; 18024 18025 /* While it's legal for type qualifiers and storage class 18026 specifiers to follow type definitions in the grammar, only 18027 compiler testsuites contain code like that. Assume that if 18028 we see such code, then what we're really seeing is a case 18029 like: 18030 18031 class X { } 18032 const <type> var = ...; 18033 18034 or 18035 18036 class Y { } 18037 static <type> func (...) ... 18038 18039 i.e. the qualifier or specifier applies to the next 18040 declaration. To do so, however, we need to look ahead one 18041 more token to see if *that* token is a type specifier. 18042 18043 This code could be improved to handle: 18044 18045 class Z { } 18046 static const <type> var = ...; */ 18047 case CPP_KEYWORD: 18048 if (keyword_is_decl_specifier (token->keyword)) 18049 { 18050 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2); 18051 18052 /* Handling user-defined types here would be nice, but very 18053 tricky. */ 18054 want_semicolon 18055 = (lookahead->type == CPP_KEYWORD 18056 && keyword_begins_type_specifier (lookahead->keyword)); 18057 } 18058 break; 18059 default: 18060 break; 18061 } 18062 18063 /* If we don't have a type, then something is very wrong and we 18064 shouldn't try to do anything clever. Likewise for not seeing the 18065 closing brace. */ 18066 if (closing_brace && TYPE_P (type) && want_semicolon) 18067 { 18068 cp_token_position prev 18069 = cp_lexer_previous_token_position (parser->lexer); 18070 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev); 18071 location_t loc = prev_token->location; 18072 18073 if (CLASSTYPE_DECLARED_CLASS (type)) 18074 error_at (loc, "expected %<;%> after class definition"); 18075 else if (TREE_CODE (type) == RECORD_TYPE) 18076 error_at (loc, "expected %<;%> after struct definition"); 18077 else if (TREE_CODE (type) == UNION_TYPE) 18078 error_at (loc, "expected %<;%> after union definition"); 18079 else 18080 gcc_unreachable (); 18081 18082 /* Unget one token and smash it to look as though we encountered 18083 a semicolon in the input stream. */ 18084 cp_lexer_set_token_position (parser->lexer, prev); 18085 token = cp_lexer_peek_token (parser->lexer); 18086 token->type = CPP_SEMICOLON; 18087 token->keyword = RID_MAX; 18088 } 18089 } 18090 18091 /* If this class is not itself within the scope of another class, 18092 then we need to parse the bodies of all of the queued function 18093 definitions. Note that the queued functions defined in a class 18094 are not always processed immediately following the 18095 class-specifier for that class. Consider: 18096 18097 struct A { 18098 struct B { void f() { sizeof (A); } }; 18099 }; 18100 18101 If `f' were processed before the processing of `A' were 18102 completed, there would be no way to compute the size of `A'. 18103 Note that the nesting we are interested in here is lexical -- 18104 not the semantic nesting given by TYPE_CONTEXT. In particular, 18105 for: 18106 18107 struct A { struct B; }; 18108 struct A::B { void f() { } }; 18109 18110 there is no need to delay the parsing of `A::B::f'. */ 18111 if (--parser->num_classes_being_defined == 0) 18112 { 18113 tree decl; 18114 tree class_type = NULL_TREE; 18115 tree pushed_scope = NULL_TREE; 18116 unsigned ix; 18117 cp_default_arg_entry *e; 18118 tree save_ccp, save_ccr; 18119 18120 /* In a first pass, parse default arguments to the functions. 18121 Then, in a second pass, parse the bodies of the functions. 18122 This two-phased approach handles cases like: 18123 18124 struct S { 18125 void f() { g(); } 18126 void g(int i = 3); 18127 }; 18128 18129 */ 18130 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args, 18131 ix, e) 18132 { 18133 decl = e->decl; 18134 /* If there are default arguments that have not yet been processed, 18135 take care of them now. */ 18136 if (class_type != e->class_type) 18137 { 18138 if (pushed_scope) 18139 pop_scope (pushed_scope); 18140 class_type = e->class_type; 18141 pushed_scope = push_scope (class_type); 18142 } 18143 /* Make sure that any template parameters are in scope. */ 18144 maybe_begin_member_template_processing (decl); 18145 /* Parse the default argument expressions. */ 18146 cp_parser_late_parsing_default_args (parser, decl); 18147 /* Remove any template parameters from the symbol table. */ 18148 maybe_end_member_template_processing (); 18149 } 18150 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0); 18151 /* Now parse any NSDMIs. */ 18152 save_ccp = current_class_ptr; 18153 save_ccr = current_class_ref; 18154 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl) 18155 { 18156 if (class_type != DECL_CONTEXT (decl)) 18157 { 18158 if (pushed_scope) 18159 pop_scope (pushed_scope); 18160 class_type = DECL_CONTEXT (decl); 18161 pushed_scope = push_scope (class_type); 18162 } 18163 inject_this_parameter (class_type, TYPE_UNQUALIFIED); 18164 cp_parser_late_parsing_nsdmi (parser, decl); 18165 } 18166 VEC_truncate (tree, unparsed_nsdmis, 0); 18167 current_class_ptr = save_ccp; 18168 current_class_ref = save_ccr; 18169 if (pushed_scope) 18170 pop_scope (pushed_scope); 18171 /* Now parse the body of the functions. */ 18172 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl) 18173 cp_parser_late_parsing_for_member (parser, decl); 18174 VEC_truncate (tree, unparsed_funs_with_definitions, 0); 18175 } 18176 18177 /* Put back any saved access checks. */ 18178 pop_deferring_access_checks (); 18179 18180 /* Restore saved state. */ 18181 parser->in_switch_statement_p = in_switch_statement_p; 18182 parser->in_statement = in_statement; 18183 parser->in_function_body = saved_in_function_body; 18184 parser->num_template_parameter_lists 18185 = saved_num_template_parameter_lists; 18186 parser->in_unbraced_linkage_specification_p 18187 = saved_in_unbraced_linkage_specification_p; 18188 18189 return type; 18190 } 18191 18192 static tree 18193 cp_parser_class_specifier (cp_parser* parser) 18194 { 18195 tree ret; 18196 timevar_push (TV_PARSE_STRUCT); 18197 ret = cp_parser_class_specifier_1 (parser); 18198 timevar_pop (TV_PARSE_STRUCT); 18199 return ret; 18200 } 18201 18202 /* Parse a class-head. 18203 18204 class-head: 18205 class-key identifier [opt] base-clause [opt] 18206 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt] 18207 class-key nested-name-specifier [opt] template-id 18208 base-clause [opt] 18209 18210 class-virt-specifier: 18211 final 18212 18213 GNU Extensions: 18214 class-key attributes identifier [opt] base-clause [opt] 18215 class-key attributes nested-name-specifier identifier base-clause [opt] 18216 class-key attributes nested-name-specifier [opt] template-id 18217 base-clause [opt] 18218 18219 Upon return BASES is initialized to the list of base classes (or 18220 NULL, if there are none) in the same form returned by 18221 cp_parser_base_clause. 18222 18223 Returns the TYPE of the indicated class. Sets 18224 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions 18225 involving a nested-name-specifier was used, and FALSE otherwise. 18226 18227 Returns error_mark_node if this is not a class-head. 18228 18229 Returns NULL_TREE if the class-head is syntactically valid, but 18230 semantically invalid in a way that means we should skip the entire 18231 body of the class. */ 18232 18233 static tree 18234 cp_parser_class_head (cp_parser* parser, 18235 bool* nested_name_specifier_p) 18236 { 18237 tree nested_name_specifier; 18238 enum tag_types class_key; 18239 tree id = NULL_TREE; 18240 tree type = NULL_TREE; 18241 tree attributes; 18242 tree bases; 18243 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED; 18244 bool template_id_p = false; 18245 bool qualified_p = false; 18246 bool invalid_nested_name_p = false; 18247 bool invalid_explicit_specialization_p = false; 18248 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 18249 tree pushed_scope = NULL_TREE; 18250 unsigned num_templates; 18251 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL; 18252 /* Assume no nested-name-specifier will be present. */ 18253 *nested_name_specifier_p = false; 18254 /* Assume no template parameter lists will be used in defining the 18255 type. */ 18256 num_templates = 0; 18257 parser->colon_corrects_to_scope_p = false; 18258 18259 /* Look for the class-key. */ 18260 class_key = cp_parser_class_key (parser); 18261 if (class_key == none_type) 18262 return error_mark_node; 18263 18264 /* Parse the attributes. */ 18265 attributes = cp_parser_attributes_opt (parser); 18266 18267 /* If the next token is `::', that is invalid -- but sometimes 18268 people do try to write: 18269 18270 struct ::S {}; 18271 18272 Handle this gracefully by accepting the extra qualifier, and then 18273 issuing an error about it later if this really is a 18274 class-head. If it turns out just to be an elaborated type 18275 specifier, remain silent. */ 18276 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)) 18277 qualified_p = true; 18278 18279 push_deferring_access_checks (dk_no_check); 18280 18281 /* Determine the name of the class. Begin by looking for an 18282 optional nested-name-specifier. */ 18283 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer); 18284 nested_name_specifier 18285 = cp_parser_nested_name_specifier_opt (parser, 18286 /*typename_keyword_p=*/false, 18287 /*check_dependency_p=*/false, 18288 /*type_p=*/false, 18289 /*is_declaration=*/false); 18290 /* If there was a nested-name-specifier, then there *must* be an 18291 identifier. */ 18292 if (nested_name_specifier) 18293 { 18294 type_start_token = cp_lexer_peek_token (parser->lexer); 18295 /* Although the grammar says `identifier', it really means 18296 `class-name' or `template-name'. You are only allowed to 18297 define a class that has already been declared with this 18298 syntax. 18299 18300 The proposed resolution for Core Issue 180 says that wherever 18301 you see `class T::X' you should treat `X' as a type-name. 18302 18303 It is OK to define an inaccessible class; for example: 18304 18305 class A { class B; }; 18306 class A::B {}; 18307 18308 We do not know if we will see a class-name, or a 18309 template-name. We look for a class-name first, in case the 18310 class-name is a template-id; if we looked for the 18311 template-name first we would stop after the template-name. */ 18312 cp_parser_parse_tentatively (parser); 18313 type = cp_parser_class_name (parser, 18314 /*typename_keyword_p=*/false, 18315 /*template_keyword_p=*/false, 18316 class_type, 18317 /*check_dependency_p=*/false, 18318 /*class_head_p=*/true, 18319 /*is_declaration=*/false); 18320 /* If that didn't work, ignore the nested-name-specifier. */ 18321 if (!cp_parser_parse_definitely (parser)) 18322 { 18323 invalid_nested_name_p = true; 18324 type_start_token = cp_lexer_peek_token (parser->lexer); 18325 id = cp_parser_identifier (parser); 18326 if (id == error_mark_node) 18327 id = NULL_TREE; 18328 } 18329 /* If we could not find a corresponding TYPE, treat this 18330 declaration like an unqualified declaration. */ 18331 if (type == error_mark_node) 18332 nested_name_specifier = NULL_TREE; 18333 /* Otherwise, count the number of templates used in TYPE and its 18334 containing scopes. */ 18335 else 18336 { 18337 tree scope; 18338 18339 for (scope = TREE_TYPE (type); 18340 scope && TREE_CODE (scope) != NAMESPACE_DECL; 18341 scope = (TYPE_P (scope) 18342 ? TYPE_CONTEXT (scope) 18343 : DECL_CONTEXT (scope))) 18344 if (TYPE_P (scope) 18345 && CLASS_TYPE_P (scope) 18346 && CLASSTYPE_TEMPLATE_INFO (scope) 18347 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)) 18348 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)) 18349 ++num_templates; 18350 } 18351 } 18352 /* Otherwise, the identifier is optional. */ 18353 else 18354 { 18355 /* We don't know whether what comes next is a template-id, 18356 an identifier, or nothing at all. */ 18357 cp_parser_parse_tentatively (parser); 18358 /* Check for a template-id. */ 18359 type_start_token = cp_lexer_peek_token (parser->lexer); 18360 id = cp_parser_template_id (parser, 18361 /*template_keyword_p=*/false, 18362 /*check_dependency_p=*/true, 18363 /*is_declaration=*/true); 18364 /* If that didn't work, it could still be an identifier. */ 18365 if (!cp_parser_parse_definitely (parser)) 18366 { 18367 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 18368 { 18369 type_start_token = cp_lexer_peek_token (parser->lexer); 18370 id = cp_parser_identifier (parser); 18371 } 18372 else 18373 id = NULL_TREE; 18374 } 18375 else 18376 { 18377 template_id_p = true; 18378 ++num_templates; 18379 } 18380 } 18381 18382 pop_deferring_access_checks (); 18383 18384 if (id) 18385 { 18386 cp_parser_check_for_invalid_template_id (parser, id, 18387 type_start_token->location); 18388 } 18389 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser); 18390 18391 /* If it's not a `:' or a `{' then we can't really be looking at a 18392 class-head, since a class-head only appears as part of a 18393 class-specifier. We have to detect this situation before calling 18394 xref_tag, since that has irreversible side-effects. */ 18395 if (!cp_parser_next_token_starts_class_definition_p (parser)) 18396 { 18397 cp_parser_error (parser, "expected %<{%> or %<:%>"); 18398 type = error_mark_node; 18399 goto out; 18400 } 18401 18402 /* At this point, we're going ahead with the class-specifier, even 18403 if some other problem occurs. */ 18404 cp_parser_commit_to_tentative_parse (parser); 18405 if (virt_specifiers & VIRT_SPEC_OVERRIDE) 18406 { 18407 cp_parser_error (parser, 18408 "cannot specify %<override%> for a class"); 18409 type = error_mark_node; 18410 goto out; 18411 } 18412 /* Issue the error about the overly-qualified name now. */ 18413 if (qualified_p) 18414 { 18415 cp_parser_error (parser, 18416 "global qualification of class name is invalid"); 18417 type = error_mark_node; 18418 goto out; 18419 } 18420 else if (invalid_nested_name_p) 18421 { 18422 cp_parser_error (parser, 18423 "qualified name does not name a class"); 18424 type = error_mark_node; 18425 goto out; 18426 } 18427 else if (nested_name_specifier) 18428 { 18429 tree scope; 18430 18431 /* Reject typedef-names in class heads. */ 18432 if (!DECL_IMPLICIT_TYPEDEF_P (type)) 18433 { 18434 error_at (type_start_token->location, 18435 "invalid class name in declaration of %qD", 18436 type); 18437 type = NULL_TREE; 18438 goto done; 18439 } 18440 18441 /* Figure out in what scope the declaration is being placed. */ 18442 scope = current_scope (); 18443 /* If that scope does not contain the scope in which the 18444 class was originally declared, the program is invalid. */ 18445 if (scope && !is_ancestor (scope, nested_name_specifier)) 18446 { 18447 if (at_namespace_scope_p ()) 18448 error_at (type_start_token->location, 18449 "declaration of %qD in namespace %qD which does not " 18450 "enclose %qD", 18451 type, scope, nested_name_specifier); 18452 else 18453 error_at (type_start_token->location, 18454 "declaration of %qD in %qD which does not enclose %qD", 18455 type, scope, nested_name_specifier); 18456 type = NULL_TREE; 18457 goto done; 18458 } 18459 /* [dcl.meaning] 18460 18461 A declarator-id shall not be qualified except for the 18462 definition of a ... nested class outside of its class 18463 ... [or] the definition or explicit instantiation of a 18464 class member of a namespace outside of its namespace. */ 18465 if (scope == nested_name_specifier) 18466 { 18467 permerror (nested_name_specifier_token_start->location, 18468 "extra qualification not allowed"); 18469 nested_name_specifier = NULL_TREE; 18470 num_templates = 0; 18471 } 18472 } 18473 /* An explicit-specialization must be preceded by "template <>". If 18474 it is not, try to recover gracefully. */ 18475 if (at_namespace_scope_p () 18476 && parser->num_template_parameter_lists == 0 18477 && template_id_p) 18478 { 18479 error_at (type_start_token->location, 18480 "an explicit specialization must be preceded by %<template <>%>"); 18481 invalid_explicit_specialization_p = true; 18482 /* Take the same action that would have been taken by 18483 cp_parser_explicit_specialization. */ 18484 ++parser->num_template_parameter_lists; 18485 begin_specialization (); 18486 } 18487 /* There must be no "return" statements between this point and the 18488 end of this function; set "type "to the correct return value and 18489 use "goto done;" to return. */ 18490 /* Make sure that the right number of template parameters were 18491 present. */ 18492 if (!cp_parser_check_template_parameters (parser, num_templates, 18493 type_start_token->location, 18494 /*declarator=*/NULL)) 18495 { 18496 /* If something went wrong, there is no point in even trying to 18497 process the class-definition. */ 18498 type = NULL_TREE; 18499 goto done; 18500 } 18501 18502 /* Look up the type. */ 18503 if (template_id_p) 18504 { 18505 if (TREE_CODE (id) == TEMPLATE_ID_EXPR 18506 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0)) 18507 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD)) 18508 { 18509 error_at (type_start_token->location, 18510 "function template %qD redeclared as a class template", id); 18511 type = error_mark_node; 18512 } 18513 else 18514 { 18515 type = TREE_TYPE (id); 18516 type = maybe_process_partial_specialization (type); 18517 } 18518 if (nested_name_specifier) 18519 pushed_scope = push_scope (nested_name_specifier); 18520 } 18521 else if (nested_name_specifier) 18522 { 18523 tree class_type; 18524 18525 /* Given: 18526 18527 template <typename T> struct S { struct T }; 18528 template <typename T> struct S<T>::T { }; 18529 18530 we will get a TYPENAME_TYPE when processing the definition of 18531 `S::T'. We need to resolve it to the actual type before we 18532 try to define it. */ 18533 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE) 18534 { 18535 class_type = resolve_typename_type (TREE_TYPE (type), 18536 /*only_current_p=*/false); 18537 if (TREE_CODE (class_type) != TYPENAME_TYPE) 18538 type = TYPE_NAME (class_type); 18539 else 18540 { 18541 cp_parser_error (parser, "could not resolve typename type"); 18542 type = error_mark_node; 18543 } 18544 } 18545 18546 if (maybe_process_partial_specialization (TREE_TYPE (type)) 18547 == error_mark_node) 18548 { 18549 type = NULL_TREE; 18550 goto done; 18551 } 18552 18553 class_type = current_class_type; 18554 /* Enter the scope indicated by the nested-name-specifier. */ 18555 pushed_scope = push_scope (nested_name_specifier); 18556 /* Get the canonical version of this type. */ 18557 type = TYPE_MAIN_DECL (TREE_TYPE (type)); 18558 if (PROCESSING_REAL_TEMPLATE_DECL_P () 18559 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type))) 18560 { 18561 type = push_template_decl (type); 18562 if (type == error_mark_node) 18563 { 18564 type = NULL_TREE; 18565 goto done; 18566 } 18567 } 18568 18569 type = TREE_TYPE (type); 18570 *nested_name_specifier_p = true; 18571 } 18572 else /* The name is not a nested name. */ 18573 { 18574 /* If the class was unnamed, create a dummy name. */ 18575 if (!id) 18576 id = make_anon_name (); 18577 type = xref_tag (class_key, id, /*tag_scope=*/ts_current, 18578 parser->num_template_parameter_lists); 18579 } 18580 18581 /* Indicate whether this class was declared as a `class' or as a 18582 `struct'. */ 18583 if (TREE_CODE (type) == RECORD_TYPE) 18584 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type); 18585 cp_parser_check_class_key (class_key, type); 18586 18587 /* If this type was already complete, and we see another definition, 18588 that's an error. */ 18589 if (type != error_mark_node && COMPLETE_TYPE_P (type)) 18590 { 18591 error_at (type_start_token->location, "redefinition of %q#T", 18592 type); 18593 error_at (type_start_token->location, "previous definition of %q+#T", 18594 type); 18595 type = NULL_TREE; 18596 goto done; 18597 } 18598 else if (type == error_mark_node) 18599 type = NULL_TREE; 18600 18601 if (type) 18602 { 18603 /* Apply attributes now, before any use of the class as a template 18604 argument in its base list. */ 18605 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE); 18606 fixup_attribute_variants (type); 18607 } 18608 18609 /* We will have entered the scope containing the class; the names of 18610 base classes should be looked up in that context. For example: 18611 18612 struct A { struct B {}; struct C; }; 18613 struct A::C : B {}; 18614 18615 is valid. */ 18616 18617 /* Get the list of base-classes, if there is one. */ 18618 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 18619 bases = cp_parser_base_clause (parser); 18620 else 18621 bases = NULL_TREE; 18622 18623 /* If we're really defining a class, process the base classes. 18624 If they're invalid, fail. */ 18625 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE) 18626 && !xref_basetypes (type, bases)) 18627 type = NULL_TREE; 18628 18629 done: 18630 /* Leave the scope given by the nested-name-specifier. We will 18631 enter the class scope itself while processing the members. */ 18632 if (pushed_scope) 18633 pop_scope (pushed_scope); 18634 18635 if (invalid_explicit_specialization_p) 18636 { 18637 end_specialization (); 18638 --parser->num_template_parameter_lists; 18639 } 18640 18641 if (type) 18642 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location; 18643 if (type && (virt_specifiers & VIRT_SPEC_FINAL)) 18644 CLASSTYPE_FINAL (type) = 1; 18645 out: 18646 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 18647 return type; 18648 } 18649 18650 /* Parse a class-key. 18651 18652 class-key: 18653 class 18654 struct 18655 union 18656 18657 Returns the kind of class-key specified, or none_type to indicate 18658 error. */ 18659 18660 static enum tag_types 18661 cp_parser_class_key (cp_parser* parser) 18662 { 18663 cp_token *token; 18664 enum tag_types tag_type; 18665 18666 /* Look for the class-key. */ 18667 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY); 18668 if (!token) 18669 return none_type; 18670 18671 /* Check to see if the TOKEN is a class-key. */ 18672 tag_type = cp_parser_token_is_class_key (token); 18673 if (!tag_type) 18674 cp_parser_error (parser, "expected class-key"); 18675 return tag_type; 18676 } 18677 18678 /* Parse an (optional) member-specification. 18679 18680 member-specification: 18681 member-declaration member-specification [opt] 18682 access-specifier : member-specification [opt] */ 18683 18684 static void 18685 cp_parser_member_specification_opt (cp_parser* parser) 18686 { 18687 while (true) 18688 { 18689 cp_token *token; 18690 enum rid keyword; 18691 18692 /* Peek at the next token. */ 18693 token = cp_lexer_peek_token (parser->lexer); 18694 /* If it's a `}', or EOF then we've seen all the members. */ 18695 if (token->type == CPP_CLOSE_BRACE 18696 || token->type == CPP_EOF 18697 || token->type == CPP_PRAGMA_EOL) 18698 break; 18699 18700 /* See if this token is a keyword. */ 18701 keyword = token->keyword; 18702 switch (keyword) 18703 { 18704 case RID_PUBLIC: 18705 case RID_PROTECTED: 18706 case RID_PRIVATE: 18707 /* Consume the access-specifier. */ 18708 cp_lexer_consume_token (parser->lexer); 18709 /* Remember which access-specifier is active. */ 18710 current_access_specifier = token->u.value; 18711 /* Look for the `:'. */ 18712 cp_parser_require (parser, CPP_COLON, RT_COLON); 18713 break; 18714 18715 default: 18716 /* Accept #pragmas at class scope. */ 18717 if (token->type == CPP_PRAGMA) 18718 { 18719 cp_parser_pragma (parser, pragma_external); 18720 break; 18721 } 18722 18723 /* Otherwise, the next construction must be a 18724 member-declaration. */ 18725 cp_parser_member_declaration (parser); 18726 } 18727 } 18728 } 18729 18730 /* Parse a member-declaration. 18731 18732 member-declaration: 18733 decl-specifier-seq [opt] member-declarator-list [opt] ; 18734 function-definition ; [opt] 18735 :: [opt] nested-name-specifier template [opt] unqualified-id ; 18736 using-declaration 18737 template-declaration 18738 alias-declaration 18739 18740 member-declarator-list: 18741 member-declarator 18742 member-declarator-list , member-declarator 18743 18744 member-declarator: 18745 declarator pure-specifier [opt] 18746 declarator constant-initializer [opt] 18747 identifier [opt] : constant-expression 18748 18749 GNU Extensions: 18750 18751 member-declaration: 18752 __extension__ member-declaration 18753 18754 member-declarator: 18755 declarator attributes [opt] pure-specifier [opt] 18756 declarator attributes [opt] constant-initializer [opt] 18757 identifier [opt] attributes [opt] : constant-expression 18758 18759 C++0x Extensions: 18760 18761 member-declaration: 18762 static_assert-declaration */ 18763 18764 static void 18765 cp_parser_member_declaration (cp_parser* parser) 18766 { 18767 cp_decl_specifier_seq decl_specifiers; 18768 tree prefix_attributes; 18769 tree decl; 18770 int declares_class_or_enum; 18771 bool friend_p; 18772 cp_token *token = NULL; 18773 cp_token *decl_spec_token_start = NULL; 18774 cp_token *initializer_token_start = NULL; 18775 int saved_pedantic; 18776 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; 18777 18778 /* Check for the `__extension__' keyword. */ 18779 if (cp_parser_extension_opt (parser, &saved_pedantic)) 18780 { 18781 /* Recurse. */ 18782 cp_parser_member_declaration (parser); 18783 /* Restore the old value of the PEDANTIC flag. */ 18784 pedantic = saved_pedantic; 18785 18786 return; 18787 } 18788 18789 /* Check for a template-declaration. */ 18790 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) 18791 { 18792 /* An explicit specialization here is an error condition, and we 18793 expect the specialization handler to detect and report this. */ 18794 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS 18795 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER) 18796 cp_parser_explicit_specialization (parser); 18797 else 18798 cp_parser_template_declaration (parser, /*member_p=*/true); 18799 18800 return; 18801 } 18802 18803 /* Check for a using-declaration. */ 18804 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) 18805 { 18806 if (cxx_dialect < cxx0x) 18807 { 18808 /* Parse the using-declaration. */ 18809 cp_parser_using_declaration (parser, 18810 /*access_declaration_p=*/false); 18811 return; 18812 } 18813 else 18814 { 18815 tree decl; 18816 cp_parser_parse_tentatively (parser); 18817 decl = cp_parser_alias_declaration (parser); 18818 if (cp_parser_parse_definitely (parser)) 18819 finish_member_declaration (decl); 18820 else 18821 cp_parser_using_declaration (parser, 18822 /*access_declaration_p=*/false); 18823 return; 18824 } 18825 } 18826 18827 /* Check for @defs. */ 18828 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS)) 18829 { 18830 tree ivar, member; 18831 tree ivar_chains = cp_parser_objc_defs_expression (parser); 18832 ivar = ivar_chains; 18833 while (ivar) 18834 { 18835 member = ivar; 18836 ivar = TREE_CHAIN (member); 18837 TREE_CHAIN (member) = NULL_TREE; 18838 finish_member_declaration (member); 18839 } 18840 return; 18841 } 18842 18843 /* If the next token is `static_assert' we have a static assertion. */ 18844 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT)) 18845 { 18846 cp_parser_static_assert (parser, /*member_p=*/true); 18847 return; 18848 } 18849 18850 parser->colon_corrects_to_scope_p = false; 18851 18852 if (cp_parser_using_declaration (parser, /*access_declaration=*/true)) 18853 goto out; 18854 18855 /* Parse the decl-specifier-seq. */ 18856 decl_spec_token_start = cp_lexer_peek_token (parser->lexer); 18857 cp_parser_decl_specifier_seq (parser, 18858 CP_PARSER_FLAGS_OPTIONAL, 18859 &decl_specifiers, 18860 &declares_class_or_enum); 18861 prefix_attributes = decl_specifiers.attributes; 18862 decl_specifiers.attributes = NULL_TREE; 18863 /* Check for an invalid type-name. */ 18864 if (!decl_specifiers.any_type_specifiers_p 18865 && cp_parser_parse_and_diagnose_invalid_type_name (parser)) 18866 goto out; 18867 /* If there is no declarator, then the decl-specifier-seq should 18868 specify a type. */ 18869 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 18870 { 18871 /* If there was no decl-specifier-seq, and the next token is a 18872 `;', then we have something like: 18873 18874 struct S { ; }; 18875 18876 [class.mem] 18877 18878 Each member-declaration shall declare at least one member 18879 name of the class. */ 18880 if (!decl_specifiers.any_specifiers_p) 18881 { 18882 cp_token *token = cp_lexer_peek_token (parser->lexer); 18883 if (!in_system_header_at (token->location)) 18884 pedwarn (token->location, OPT_pedantic, "extra %<;%>"); 18885 } 18886 else 18887 { 18888 tree type; 18889 18890 /* See if this declaration is a friend. */ 18891 friend_p = cp_parser_friend_p (&decl_specifiers); 18892 /* If there were decl-specifiers, check to see if there was 18893 a class-declaration. */ 18894 type = check_tag_decl (&decl_specifiers); 18895 /* Nested classes have already been added to the class, but 18896 a `friend' needs to be explicitly registered. */ 18897 if (friend_p) 18898 { 18899 /* If the `friend' keyword was present, the friend must 18900 be introduced with a class-key. */ 18901 if (!declares_class_or_enum && cxx_dialect < cxx0x) 18902 pedwarn (decl_spec_token_start->location, OPT_pedantic, 18903 "in C++03 a class-key must be used " 18904 "when declaring a friend"); 18905 /* In this case: 18906 18907 template <typename T> struct A { 18908 friend struct A<T>::B; 18909 }; 18910 18911 A<T>::B will be represented by a TYPENAME_TYPE, and 18912 therefore not recognized by check_tag_decl. */ 18913 if (!type) 18914 { 18915 type = decl_specifiers.type; 18916 if (type && TREE_CODE (type) == TYPE_DECL) 18917 type = TREE_TYPE (type); 18918 } 18919 if (!type || !TYPE_P (type)) 18920 error_at (decl_spec_token_start->location, 18921 "friend declaration does not name a class or " 18922 "function"); 18923 else 18924 make_friend_class (current_class_type, type, 18925 /*complain=*/true); 18926 } 18927 /* If there is no TYPE, an error message will already have 18928 been issued. */ 18929 else if (!type || type == error_mark_node) 18930 ; 18931 /* An anonymous aggregate has to be handled specially; such 18932 a declaration really declares a data member (with a 18933 particular type), as opposed to a nested class. */ 18934 else if (ANON_AGGR_TYPE_P (type)) 18935 { 18936 /* Remove constructors and such from TYPE, now that we 18937 know it is an anonymous aggregate. */ 18938 fixup_anonymous_aggr (type); 18939 /* And make the corresponding data member. */ 18940 decl = build_decl (decl_spec_token_start->location, 18941 FIELD_DECL, NULL_TREE, type); 18942 /* Add it to the class. */ 18943 finish_member_declaration (decl); 18944 } 18945 else 18946 cp_parser_check_access_in_redeclaration 18947 (TYPE_NAME (type), 18948 decl_spec_token_start->location); 18949 } 18950 } 18951 else 18952 { 18953 bool assume_semicolon = false; 18954 18955 /* See if these declarations will be friends. */ 18956 friend_p = cp_parser_friend_p (&decl_specifiers); 18957 18958 /* Keep going until we hit the `;' at the end of the 18959 declaration. */ 18960 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 18961 { 18962 tree attributes = NULL_TREE; 18963 tree first_attribute; 18964 18965 /* Peek at the next token. */ 18966 token = cp_lexer_peek_token (parser->lexer); 18967 18968 /* Check for a bitfield declaration. */ 18969 if (token->type == CPP_COLON 18970 || (token->type == CPP_NAME 18971 && cp_lexer_peek_nth_token (parser->lexer, 2)->type 18972 == CPP_COLON)) 18973 { 18974 tree identifier; 18975 tree width; 18976 18977 /* Get the name of the bitfield. Note that we cannot just 18978 check TOKEN here because it may have been invalidated by 18979 the call to cp_lexer_peek_nth_token above. */ 18980 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON) 18981 identifier = cp_parser_identifier (parser); 18982 else 18983 identifier = NULL_TREE; 18984 18985 /* Consume the `:' token. */ 18986 cp_lexer_consume_token (parser->lexer); 18987 /* Get the width of the bitfield. */ 18988 width 18989 = cp_parser_constant_expression (parser, 18990 /*allow_non_constant=*/false, 18991 NULL); 18992 18993 /* Look for attributes that apply to the bitfield. */ 18994 attributes = cp_parser_attributes_opt (parser); 18995 /* Remember which attributes are prefix attributes and 18996 which are not. */ 18997 first_attribute = attributes; 18998 /* Combine the attributes. */ 18999 attributes = chainon (prefix_attributes, attributes); 19000 19001 /* Create the bitfield declaration. */ 19002 decl = grokbitfield (identifier 19003 ? make_id_declarator (NULL_TREE, 19004 identifier, 19005 sfk_none) 19006 : NULL, 19007 &decl_specifiers, 19008 width, 19009 attributes); 19010 } 19011 else 19012 { 19013 cp_declarator *declarator; 19014 tree initializer; 19015 tree asm_specification; 19016 int ctor_dtor_or_conv_p; 19017 19018 /* Parse the declarator. */ 19019 declarator 19020 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 19021 &ctor_dtor_or_conv_p, 19022 /*parenthesized_p=*/NULL, 19023 /*member_p=*/true); 19024 19025 /* If something went wrong parsing the declarator, make sure 19026 that we at least consume some tokens. */ 19027 if (declarator == cp_error_declarator) 19028 { 19029 /* Skip to the end of the statement. */ 19030 cp_parser_skip_to_end_of_statement (parser); 19031 /* If the next token is not a semicolon, that is 19032 probably because we just skipped over the body of 19033 a function. So, we consume a semicolon if 19034 present, but do not issue an error message if it 19035 is not present. */ 19036 if (cp_lexer_next_token_is (parser->lexer, 19037 CPP_SEMICOLON)) 19038 cp_lexer_consume_token (parser->lexer); 19039 goto out; 19040 } 19041 19042 if (declares_class_or_enum & 2) 19043 cp_parser_check_for_definition_in_return_type 19044 (declarator, decl_specifiers.type, 19045 decl_specifiers.type_location); 19046 19047 /* Look for an asm-specification. */ 19048 asm_specification = cp_parser_asm_specification_opt (parser); 19049 /* Look for attributes that apply to the declaration. */ 19050 attributes = cp_parser_attributes_opt (parser); 19051 /* Remember which attributes are prefix attributes and 19052 which are not. */ 19053 first_attribute = attributes; 19054 /* Combine the attributes. */ 19055 attributes = chainon (prefix_attributes, attributes); 19056 19057 /* If it's an `=', then we have a constant-initializer or a 19058 pure-specifier. It is not correct to parse the 19059 initializer before registering the member declaration 19060 since the member declaration should be in scope while 19061 its initializer is processed. However, the rest of the 19062 front end does not yet provide an interface that allows 19063 us to handle this correctly. */ 19064 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 19065 { 19066 /* In [class.mem]: 19067 19068 A pure-specifier shall be used only in the declaration of 19069 a virtual function. 19070 19071 A member-declarator can contain a constant-initializer 19072 only if it declares a static member of integral or 19073 enumeration type. 19074 19075 Therefore, if the DECLARATOR is for a function, we look 19076 for a pure-specifier; otherwise, we look for a 19077 constant-initializer. When we call `grokfield', it will 19078 perform more stringent semantics checks. */ 19079 initializer_token_start = cp_lexer_peek_token (parser->lexer); 19080 if (function_declarator_p (declarator) 19081 || (decl_specifiers.type 19082 && TREE_CODE (decl_specifiers.type) == TYPE_DECL 19083 && (TREE_CODE (TREE_TYPE (decl_specifiers.type)) 19084 == FUNCTION_TYPE))) 19085 initializer = cp_parser_pure_specifier (parser); 19086 else if (decl_specifiers.storage_class != sc_static) 19087 initializer = cp_parser_save_nsdmi (parser); 19088 else if (cxx_dialect >= cxx0x) 19089 { 19090 bool nonconst; 19091 /* Don't require a constant rvalue in C++11, since we 19092 might want a reference constant. We'll enforce 19093 constancy later. */ 19094 cp_lexer_consume_token (parser->lexer); 19095 /* Parse the initializer. */ 19096 initializer = cp_parser_initializer_clause (parser, 19097 &nonconst); 19098 } 19099 else 19100 /* Parse the initializer. */ 19101 initializer = cp_parser_constant_initializer (parser); 19102 } 19103 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE) 19104 && !function_declarator_p (declarator)) 19105 { 19106 bool x; 19107 if (decl_specifiers.storage_class != sc_static) 19108 initializer = cp_parser_save_nsdmi (parser); 19109 else 19110 initializer = cp_parser_initializer (parser, &x, &x); 19111 } 19112 /* Otherwise, there is no initializer. */ 19113 else 19114 initializer = NULL_TREE; 19115 19116 /* See if we are probably looking at a function 19117 definition. We are certainly not looking at a 19118 member-declarator. Calling `grokfield' has 19119 side-effects, so we must not do it unless we are sure 19120 that we are looking at a member-declarator. */ 19121 if (cp_parser_token_starts_function_definition_p 19122 (cp_lexer_peek_token (parser->lexer))) 19123 { 19124 /* The grammar does not allow a pure-specifier to be 19125 used when a member function is defined. (It is 19126 possible that this fact is an oversight in the 19127 standard, since a pure function may be defined 19128 outside of the class-specifier. */ 19129 if (initializer && initializer_token_start) 19130 error_at (initializer_token_start->location, 19131 "pure-specifier on function-definition"); 19132 decl = cp_parser_save_member_function_body (parser, 19133 &decl_specifiers, 19134 declarator, 19135 attributes); 19136 /* If the member was not a friend, declare it here. */ 19137 if (!friend_p) 19138 finish_member_declaration (decl); 19139 /* Peek at the next token. */ 19140 token = cp_lexer_peek_token (parser->lexer); 19141 /* If the next token is a semicolon, consume it. */ 19142 if (token->type == CPP_SEMICOLON) 19143 cp_lexer_consume_token (parser->lexer); 19144 goto out; 19145 } 19146 else 19147 if (declarator->kind == cdk_function) 19148 declarator->id_loc = token->location; 19149 /* Create the declaration. */ 19150 decl = grokfield (declarator, &decl_specifiers, 19151 initializer, /*init_const_expr_p=*/true, 19152 asm_specification, 19153 attributes); 19154 } 19155 19156 /* Reset PREFIX_ATTRIBUTES. */ 19157 while (attributes && TREE_CHAIN (attributes) != first_attribute) 19158 attributes = TREE_CHAIN (attributes); 19159 if (attributes) 19160 TREE_CHAIN (attributes) = NULL_TREE; 19161 19162 /* If there is any qualification still in effect, clear it 19163 now; we will be starting fresh with the next declarator. */ 19164 parser->scope = NULL_TREE; 19165 parser->qualifying_scope = NULL_TREE; 19166 parser->object_scope = NULL_TREE; 19167 /* If it's a `,', then there are more declarators. */ 19168 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 19169 cp_lexer_consume_token (parser->lexer); 19170 /* If the next token isn't a `;', then we have a parse error. */ 19171 else if (cp_lexer_next_token_is_not (parser->lexer, 19172 CPP_SEMICOLON)) 19173 { 19174 /* The next token might be a ways away from where the 19175 actual semicolon is missing. Find the previous token 19176 and use that for our error position. */ 19177 cp_token *token = cp_lexer_previous_token (parser->lexer); 19178 error_at (token->location, 19179 "expected %<;%> at end of member declaration"); 19180 19181 /* Assume that the user meant to provide a semicolon. If 19182 we were to cp_parser_skip_to_end_of_statement, we might 19183 skip to a semicolon inside a member function definition 19184 and issue nonsensical error messages. */ 19185 assume_semicolon = true; 19186 } 19187 19188 if (decl) 19189 { 19190 /* Add DECL to the list of members. */ 19191 if (!friend_p) 19192 finish_member_declaration (decl); 19193 19194 if (TREE_CODE (decl) == FUNCTION_DECL) 19195 cp_parser_save_default_args (parser, decl); 19196 else if (TREE_CODE (decl) == FIELD_DECL 19197 && !DECL_C_BIT_FIELD (decl) 19198 && DECL_INITIAL (decl)) 19199 /* Add DECL to the queue of NSDMI to be parsed later. */ 19200 VEC_safe_push (tree, gc, unparsed_nsdmis, decl); 19201 } 19202 19203 if (assume_semicolon) 19204 goto out; 19205 } 19206 } 19207 19208 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 19209 out: 19210 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; 19211 } 19212 19213 /* Parse a pure-specifier. 19214 19215 pure-specifier: 19216 = 0 19217 19218 Returns INTEGER_ZERO_NODE if a pure specifier is found. 19219 Otherwise, ERROR_MARK_NODE is returned. */ 19220 19221 static tree 19222 cp_parser_pure_specifier (cp_parser* parser) 19223 { 19224 cp_token *token; 19225 19226 /* Look for the `=' token. */ 19227 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 19228 return error_mark_node; 19229 /* Look for the `0' token. */ 19230 token = cp_lexer_peek_token (parser->lexer); 19231 19232 if (token->type == CPP_EOF 19233 || token->type == CPP_PRAGMA_EOL) 19234 return error_mark_node; 19235 19236 cp_lexer_consume_token (parser->lexer); 19237 19238 /* Accept = default or = delete in c++0x mode. */ 19239 if (token->keyword == RID_DEFAULT 19240 || token->keyword == RID_DELETE) 19241 { 19242 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED); 19243 return token->u.value; 19244 } 19245 19246 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */ 19247 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO)) 19248 { 19249 cp_parser_error (parser, 19250 "invalid pure specifier (only %<= 0%> is allowed)"); 19251 cp_parser_skip_to_end_of_statement (parser); 19252 return error_mark_node; 19253 } 19254 if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 19255 { 19256 error_at (token->location, "templates may not be %<virtual%>"); 19257 return error_mark_node; 19258 } 19259 19260 return integer_zero_node; 19261 } 19262 19263 /* Parse a constant-initializer. 19264 19265 constant-initializer: 19266 = constant-expression 19267 19268 Returns a representation of the constant-expression. */ 19269 19270 static tree 19271 cp_parser_constant_initializer (cp_parser* parser) 19272 { 19273 /* Look for the `=' token. */ 19274 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 19275 return error_mark_node; 19276 19277 /* It is invalid to write: 19278 19279 struct S { static const int i = { 7 }; }; 19280 19281 */ 19282 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 19283 { 19284 cp_parser_error (parser, 19285 "a brace-enclosed initializer is not allowed here"); 19286 /* Consume the opening brace. */ 19287 cp_lexer_consume_token (parser->lexer); 19288 /* Skip the initializer. */ 19289 cp_parser_skip_to_closing_brace (parser); 19290 /* Look for the trailing `}'. */ 19291 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 19292 19293 return error_mark_node; 19294 } 19295 19296 return cp_parser_constant_expression (parser, 19297 /*allow_non_constant=*/false, 19298 NULL); 19299 } 19300 19301 /* Derived classes [gram.class.derived] */ 19302 19303 /* Parse a base-clause. 19304 19305 base-clause: 19306 : base-specifier-list 19307 19308 base-specifier-list: 19309 base-specifier ... [opt] 19310 base-specifier-list , base-specifier ... [opt] 19311 19312 Returns a TREE_LIST representing the base-classes, in the order in 19313 which they were declared. The representation of each node is as 19314 described by cp_parser_base_specifier. 19315 19316 In the case that no bases are specified, this function will return 19317 NULL_TREE, not ERROR_MARK_NODE. */ 19318 19319 static tree 19320 cp_parser_base_clause (cp_parser* parser) 19321 { 19322 tree bases = NULL_TREE; 19323 19324 /* Look for the `:' that begins the list. */ 19325 cp_parser_require (parser, CPP_COLON, RT_COLON); 19326 19327 /* Scan the base-specifier-list. */ 19328 while (true) 19329 { 19330 cp_token *token; 19331 tree base; 19332 bool pack_expansion_p = false; 19333 19334 /* Look for the base-specifier. */ 19335 base = cp_parser_base_specifier (parser); 19336 /* Look for the (optional) ellipsis. */ 19337 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 19338 { 19339 /* Consume the `...'. */ 19340 cp_lexer_consume_token (parser->lexer); 19341 19342 pack_expansion_p = true; 19343 } 19344 19345 /* Add BASE to the front of the list. */ 19346 if (base && base != error_mark_node) 19347 { 19348 if (pack_expansion_p) 19349 /* Make this a pack expansion type. */ 19350 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base)); 19351 19352 if (!check_for_bare_parameter_packs (TREE_VALUE (base))) 19353 { 19354 TREE_CHAIN (base) = bases; 19355 bases = base; 19356 } 19357 } 19358 /* Peek at the next token. */ 19359 token = cp_lexer_peek_token (parser->lexer); 19360 /* If it's not a comma, then the list is complete. */ 19361 if (token->type != CPP_COMMA) 19362 break; 19363 /* Consume the `,'. */ 19364 cp_lexer_consume_token (parser->lexer); 19365 } 19366 19367 /* PARSER->SCOPE may still be non-NULL at this point, if the last 19368 base class had a qualified name. However, the next name that 19369 appears is certainly not qualified. */ 19370 parser->scope = NULL_TREE; 19371 parser->qualifying_scope = NULL_TREE; 19372 parser->object_scope = NULL_TREE; 19373 19374 return nreverse (bases); 19375 } 19376 19377 /* Parse a base-specifier. 19378 19379 base-specifier: 19380 :: [opt] nested-name-specifier [opt] class-name 19381 virtual access-specifier [opt] :: [opt] nested-name-specifier 19382 [opt] class-name 19383 access-specifier virtual [opt] :: [opt] nested-name-specifier 19384 [opt] class-name 19385 19386 Returns a TREE_LIST. The TREE_PURPOSE will be one of 19387 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to 19388 indicate the specifiers provided. The TREE_VALUE will be a TYPE 19389 (or the ERROR_MARK_NODE) indicating the type that was specified. */ 19390 19391 static tree 19392 cp_parser_base_specifier (cp_parser* parser) 19393 { 19394 cp_token *token; 19395 bool done = false; 19396 bool virtual_p = false; 19397 bool duplicate_virtual_error_issued_p = false; 19398 bool duplicate_access_error_issued_p = false; 19399 bool class_scope_p, template_p; 19400 tree access = access_default_node; 19401 tree type; 19402 19403 /* Process the optional `virtual' and `access-specifier'. */ 19404 while (!done) 19405 { 19406 /* Peek at the next token. */ 19407 token = cp_lexer_peek_token (parser->lexer); 19408 /* Process `virtual'. */ 19409 switch (token->keyword) 19410 { 19411 case RID_VIRTUAL: 19412 /* If `virtual' appears more than once, issue an error. */ 19413 if (virtual_p && !duplicate_virtual_error_issued_p) 19414 { 19415 cp_parser_error (parser, 19416 "%<virtual%> specified more than once in base-specified"); 19417 duplicate_virtual_error_issued_p = true; 19418 } 19419 19420 virtual_p = true; 19421 19422 /* Consume the `virtual' token. */ 19423 cp_lexer_consume_token (parser->lexer); 19424 19425 break; 19426 19427 case RID_PUBLIC: 19428 case RID_PROTECTED: 19429 case RID_PRIVATE: 19430 /* If more than one access specifier appears, issue an 19431 error. */ 19432 if (access != access_default_node 19433 && !duplicate_access_error_issued_p) 19434 { 19435 cp_parser_error (parser, 19436 "more than one access specifier in base-specified"); 19437 duplicate_access_error_issued_p = true; 19438 } 19439 19440 access = ridpointers[(int) token->keyword]; 19441 19442 /* Consume the access-specifier. */ 19443 cp_lexer_consume_token (parser->lexer); 19444 19445 break; 19446 19447 default: 19448 done = true; 19449 break; 19450 } 19451 } 19452 /* It is not uncommon to see programs mechanically, erroneously, use 19453 the 'typename' keyword to denote (dependent) qualified types 19454 as base classes. */ 19455 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME)) 19456 { 19457 token = cp_lexer_peek_token (parser->lexer); 19458 if (!processing_template_decl) 19459 error_at (token->location, 19460 "keyword %<typename%> not allowed outside of templates"); 19461 else 19462 error_at (token->location, 19463 "keyword %<typename%> not allowed in this context " 19464 "(the base class is implicitly a type)"); 19465 cp_lexer_consume_token (parser->lexer); 19466 } 19467 19468 /* Look for the optional `::' operator. */ 19469 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false); 19470 /* Look for the nested-name-specifier. The simplest way to 19471 implement: 19472 19473 [temp.res] 19474 19475 The keyword `typename' is not permitted in a base-specifier or 19476 mem-initializer; in these contexts a qualified name that 19477 depends on a template-parameter is implicitly assumed to be a 19478 type name. 19479 19480 is to pretend that we have seen the `typename' keyword at this 19481 point. */ 19482 cp_parser_nested_name_specifier_opt (parser, 19483 /*typename_keyword_p=*/true, 19484 /*check_dependency_p=*/true, 19485 typename_type, 19486 /*is_declaration=*/true); 19487 /* If the base class is given by a qualified name, assume that names 19488 we see are type names or templates, as appropriate. */ 19489 class_scope_p = (parser->scope && TYPE_P (parser->scope)); 19490 template_p = class_scope_p && cp_parser_optional_template_keyword (parser); 19491 19492 if (!parser->scope 19493 && cp_lexer_next_token_is_decltype (parser->lexer)) 19494 /* DR 950 allows decltype as a base-specifier. */ 19495 type = cp_parser_decltype (parser); 19496 else 19497 { 19498 /* Otherwise, look for the class-name. */ 19499 type = cp_parser_class_name (parser, 19500 class_scope_p, 19501 template_p, 19502 typename_type, 19503 /*check_dependency_p=*/true, 19504 /*class_head_p=*/false, 19505 /*is_declaration=*/true); 19506 type = TREE_TYPE (type); 19507 } 19508 19509 if (type == error_mark_node) 19510 return error_mark_node; 19511 19512 return finish_base_specifier (type, access, virtual_p); 19513 } 19514 19515 /* Exception handling [gram.exception] */ 19516 19517 /* Parse an (optional) noexcept-specification. 19518 19519 noexcept-specification: 19520 noexcept ( constant-expression ) [opt] 19521 19522 If no noexcept-specification is present, returns NULL_TREE. 19523 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any 19524 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if 19525 there are no parentheses. CONSUMED_EXPR will be set accordingly. 19526 Otherwise, returns a noexcept specification unless RETURN_COND is true, 19527 in which case a boolean condition is returned instead. */ 19528 19529 static tree 19530 cp_parser_noexcept_specification_opt (cp_parser* parser, 19531 bool require_constexpr, 19532 bool* consumed_expr, 19533 bool return_cond) 19534 { 19535 cp_token *token; 19536 const char *saved_message; 19537 19538 /* Peek at the next token. */ 19539 token = cp_lexer_peek_token (parser->lexer); 19540 19541 /* Is it a noexcept-specification? */ 19542 if (cp_parser_is_keyword (token, RID_NOEXCEPT)) 19543 { 19544 tree expr; 19545 cp_lexer_consume_token (parser->lexer); 19546 19547 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN) 19548 { 19549 cp_lexer_consume_token (parser->lexer); 19550 19551 if (require_constexpr) 19552 { 19553 /* Types may not be defined in an exception-specification. */ 19554 saved_message = parser->type_definition_forbidden_message; 19555 parser->type_definition_forbidden_message 19556 = G_("types may not be defined in an exception-specification"); 19557 19558 expr = cp_parser_constant_expression (parser, false, NULL); 19559 19560 /* Restore the saved message. */ 19561 parser->type_definition_forbidden_message = saved_message; 19562 } 19563 else 19564 { 19565 expr = cp_parser_expression (parser, false, NULL); 19566 *consumed_expr = true; 19567 } 19568 19569 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19570 } 19571 else 19572 { 19573 expr = boolean_true_node; 19574 if (!require_constexpr) 19575 *consumed_expr = false; 19576 } 19577 19578 /* We cannot build a noexcept-spec right away because this will check 19579 that expr is a constexpr. */ 19580 if (!return_cond) 19581 return build_noexcept_spec (expr, tf_warning_or_error); 19582 else 19583 return expr; 19584 } 19585 else 19586 return NULL_TREE; 19587 } 19588 19589 /* Parse an (optional) exception-specification. 19590 19591 exception-specification: 19592 throw ( type-id-list [opt] ) 19593 19594 Returns a TREE_LIST representing the exception-specification. The 19595 TREE_VALUE of each node is a type. */ 19596 19597 static tree 19598 cp_parser_exception_specification_opt (cp_parser* parser) 19599 { 19600 cp_token *token; 19601 tree type_id_list; 19602 const char *saved_message; 19603 19604 /* Peek at the next token. */ 19605 token = cp_lexer_peek_token (parser->lexer); 19606 19607 /* Is it a noexcept-specification? */ 19608 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL, 19609 false); 19610 if (type_id_list != NULL_TREE) 19611 return type_id_list; 19612 19613 /* If it's not `throw', then there's no exception-specification. */ 19614 if (!cp_parser_is_keyword (token, RID_THROW)) 19615 return NULL_TREE; 19616 19617 #if 0 19618 /* Enable this once a lot of code has transitioned to noexcept? */ 19619 if (cxx_dialect == cxx0x && !in_system_header) 19620 warning (OPT_Wdeprecated, "dynamic exception specifications are " 19621 "deprecated in C++0x; use %<noexcept%> instead"); 19622 #endif 19623 19624 /* Consume the `throw'. */ 19625 cp_lexer_consume_token (parser->lexer); 19626 19627 /* Look for the `('. */ 19628 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 19629 19630 /* Peek at the next token. */ 19631 token = cp_lexer_peek_token (parser->lexer); 19632 /* If it's not a `)', then there is a type-id-list. */ 19633 if (token->type != CPP_CLOSE_PAREN) 19634 { 19635 /* Types may not be defined in an exception-specification. */ 19636 saved_message = parser->type_definition_forbidden_message; 19637 parser->type_definition_forbidden_message 19638 = G_("types may not be defined in an exception-specification"); 19639 /* Parse the type-id-list. */ 19640 type_id_list = cp_parser_type_id_list (parser); 19641 /* Restore the saved message. */ 19642 parser->type_definition_forbidden_message = saved_message; 19643 } 19644 else 19645 type_id_list = empty_except_spec; 19646 19647 /* Look for the `)'. */ 19648 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19649 19650 return type_id_list; 19651 } 19652 19653 /* Parse an (optional) type-id-list. 19654 19655 type-id-list: 19656 type-id ... [opt] 19657 type-id-list , type-id ... [opt] 19658 19659 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE, 19660 in the order that the types were presented. */ 19661 19662 static tree 19663 cp_parser_type_id_list (cp_parser* parser) 19664 { 19665 tree types = NULL_TREE; 19666 19667 while (true) 19668 { 19669 cp_token *token; 19670 tree type; 19671 19672 /* Get the next type-id. */ 19673 type = cp_parser_type_id (parser); 19674 /* Parse the optional ellipsis. */ 19675 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 19676 { 19677 /* Consume the `...'. */ 19678 cp_lexer_consume_token (parser->lexer); 19679 19680 /* Turn the type into a pack expansion expression. */ 19681 type = make_pack_expansion (type); 19682 } 19683 /* Add it to the list. */ 19684 types = add_exception_specifier (types, type, /*complain=*/1); 19685 /* Peek at the next token. */ 19686 token = cp_lexer_peek_token (parser->lexer); 19687 /* If it is not a `,', we are done. */ 19688 if (token->type != CPP_COMMA) 19689 break; 19690 /* Consume the `,'. */ 19691 cp_lexer_consume_token (parser->lexer); 19692 } 19693 19694 return nreverse (types); 19695 } 19696 19697 /* Parse a try-block. 19698 19699 try-block: 19700 try compound-statement handler-seq */ 19701 19702 static tree 19703 cp_parser_try_block (cp_parser* parser) 19704 { 19705 tree try_block; 19706 19707 cp_parser_require_keyword (parser, RID_TRY, RT_TRY); 19708 try_block = begin_try_block (); 19709 cp_parser_compound_statement (parser, NULL, true, false); 19710 finish_try_block (try_block); 19711 cp_parser_handler_seq (parser); 19712 finish_handler_sequence (try_block); 19713 19714 return try_block; 19715 } 19716 19717 /* Parse a function-try-block. 19718 19719 function-try-block: 19720 try ctor-initializer [opt] function-body handler-seq */ 19721 19722 static bool 19723 cp_parser_function_try_block (cp_parser* parser) 19724 { 19725 tree compound_stmt; 19726 tree try_block; 19727 bool ctor_initializer_p; 19728 19729 /* Look for the `try' keyword. */ 19730 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY)) 19731 return false; 19732 /* Let the rest of the front end know where we are. */ 19733 try_block = begin_function_try_block (&compound_stmt); 19734 /* Parse the function-body. */ 19735 ctor_initializer_p 19736 = cp_parser_ctor_initializer_opt_and_function_body (parser); 19737 /* We're done with the `try' part. */ 19738 finish_function_try_block (try_block); 19739 /* Parse the handlers. */ 19740 cp_parser_handler_seq (parser); 19741 /* We're done with the handlers. */ 19742 finish_function_handler_sequence (try_block, compound_stmt); 19743 19744 return ctor_initializer_p; 19745 } 19746 19747 /* Parse a handler-seq. 19748 19749 handler-seq: 19750 handler handler-seq [opt] */ 19751 19752 static void 19753 cp_parser_handler_seq (cp_parser* parser) 19754 { 19755 while (true) 19756 { 19757 cp_token *token; 19758 19759 /* Parse the handler. */ 19760 cp_parser_handler (parser); 19761 /* Peek at the next token. */ 19762 token = cp_lexer_peek_token (parser->lexer); 19763 /* If it's not `catch' then there are no more handlers. */ 19764 if (!cp_parser_is_keyword (token, RID_CATCH)) 19765 break; 19766 } 19767 } 19768 19769 /* Parse a handler. 19770 19771 handler: 19772 catch ( exception-declaration ) compound-statement */ 19773 19774 static void 19775 cp_parser_handler (cp_parser* parser) 19776 { 19777 tree handler; 19778 tree declaration; 19779 19780 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH); 19781 handler = begin_handler (); 19782 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 19783 declaration = cp_parser_exception_declaration (parser); 19784 finish_handler_parms (declaration, handler); 19785 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19786 cp_parser_compound_statement (parser, NULL, false, false); 19787 finish_handler (handler); 19788 } 19789 19790 /* Parse an exception-declaration. 19791 19792 exception-declaration: 19793 type-specifier-seq declarator 19794 type-specifier-seq abstract-declarator 19795 type-specifier-seq 19796 ... 19797 19798 Returns a VAR_DECL for the declaration, or NULL_TREE if the 19799 ellipsis variant is used. */ 19800 19801 static tree 19802 cp_parser_exception_declaration (cp_parser* parser) 19803 { 19804 cp_decl_specifier_seq type_specifiers; 19805 cp_declarator *declarator; 19806 const char *saved_message; 19807 19808 /* If it's an ellipsis, it's easy to handle. */ 19809 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 19810 { 19811 /* Consume the `...' token. */ 19812 cp_lexer_consume_token (parser->lexer); 19813 return NULL_TREE; 19814 } 19815 19816 /* Types may not be defined in exception-declarations. */ 19817 saved_message = parser->type_definition_forbidden_message; 19818 parser->type_definition_forbidden_message 19819 = G_("types may not be defined in exception-declarations"); 19820 19821 /* Parse the type-specifier-seq. */ 19822 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true, 19823 /*is_trailing_return=*/false, 19824 &type_specifiers); 19825 /* If it's a `)', then there is no declarator. */ 19826 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) 19827 declarator = NULL; 19828 else 19829 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER, 19830 /*ctor_dtor_or_conv_p=*/NULL, 19831 /*parenthesized_p=*/NULL, 19832 /*member_p=*/false); 19833 19834 /* Restore the saved message. */ 19835 parser->type_definition_forbidden_message = saved_message; 19836 19837 if (!type_specifiers.any_specifiers_p) 19838 return error_mark_node; 19839 19840 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL); 19841 } 19842 19843 /* Parse a throw-expression. 19844 19845 throw-expression: 19846 throw assignment-expression [opt] 19847 19848 Returns a THROW_EXPR representing the throw-expression. */ 19849 19850 static tree 19851 cp_parser_throw_expression (cp_parser* parser) 19852 { 19853 tree expression; 19854 cp_token* token; 19855 19856 cp_parser_require_keyword (parser, RID_THROW, RT_THROW); 19857 token = cp_lexer_peek_token (parser->lexer); 19858 /* Figure out whether or not there is an assignment-expression 19859 following the "throw" keyword. */ 19860 if (token->type == CPP_COMMA 19861 || token->type == CPP_SEMICOLON 19862 || token->type == CPP_CLOSE_PAREN 19863 || token->type == CPP_CLOSE_SQUARE 19864 || token->type == CPP_CLOSE_BRACE 19865 || token->type == CPP_COLON) 19866 expression = NULL_TREE; 19867 else 19868 expression = cp_parser_assignment_expression (parser, 19869 /*cast_p=*/false, NULL); 19870 19871 return build_throw (expression); 19872 } 19873 19874 /* GNU Extensions */ 19875 19876 /* Parse an (optional) asm-specification. 19877 19878 asm-specification: 19879 asm ( string-literal ) 19880 19881 If the asm-specification is present, returns a STRING_CST 19882 corresponding to the string-literal. Otherwise, returns 19883 NULL_TREE. */ 19884 19885 static tree 19886 cp_parser_asm_specification_opt (cp_parser* parser) 19887 { 19888 cp_token *token; 19889 tree asm_specification; 19890 19891 /* Peek at the next token. */ 19892 token = cp_lexer_peek_token (parser->lexer); 19893 /* If the next token isn't the `asm' keyword, then there's no 19894 asm-specification. */ 19895 if (!cp_parser_is_keyword (token, RID_ASM)) 19896 return NULL_TREE; 19897 19898 /* Consume the `asm' token. */ 19899 cp_lexer_consume_token (parser->lexer); 19900 /* Look for the `('. */ 19901 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 19902 19903 /* Look for the string-literal. */ 19904 asm_specification = cp_parser_string_literal (parser, false, false); 19905 19906 /* Look for the `)'. */ 19907 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19908 19909 return asm_specification; 19910 } 19911 19912 /* Parse an asm-operand-list. 19913 19914 asm-operand-list: 19915 asm-operand 19916 asm-operand-list , asm-operand 19917 19918 asm-operand: 19919 string-literal ( expression ) 19920 [ string-literal ] string-literal ( expression ) 19921 19922 Returns a TREE_LIST representing the operands. The TREE_VALUE of 19923 each node is the expression. The TREE_PURPOSE is itself a 19924 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed 19925 string-literal (or NULL_TREE if not present) and whose TREE_VALUE 19926 is a STRING_CST for the string literal before the parenthesis. Returns 19927 ERROR_MARK_NODE if any of the operands are invalid. */ 19928 19929 static tree 19930 cp_parser_asm_operand_list (cp_parser* parser) 19931 { 19932 tree asm_operands = NULL_TREE; 19933 bool invalid_operands = false; 19934 19935 while (true) 19936 { 19937 tree string_literal; 19938 tree expression; 19939 tree name; 19940 19941 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)) 19942 { 19943 /* Consume the `[' token. */ 19944 cp_lexer_consume_token (parser->lexer); 19945 /* Read the operand name. */ 19946 name = cp_parser_identifier (parser); 19947 if (name != error_mark_node) 19948 name = build_string (IDENTIFIER_LENGTH (name), 19949 IDENTIFIER_POINTER (name)); 19950 /* Look for the closing `]'. */ 19951 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 19952 } 19953 else 19954 name = NULL_TREE; 19955 /* Look for the string-literal. */ 19956 string_literal = cp_parser_string_literal (parser, false, false); 19957 19958 /* Look for the `('. */ 19959 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 19960 /* Parse the expression. */ 19961 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL); 19962 /* Look for the `)'. */ 19963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 19964 19965 if (name == error_mark_node 19966 || string_literal == error_mark_node 19967 || expression == error_mark_node) 19968 invalid_operands = true; 19969 19970 /* Add this operand to the list. */ 19971 asm_operands = tree_cons (build_tree_list (name, string_literal), 19972 expression, 19973 asm_operands); 19974 /* If the next token is not a `,', there are no more 19975 operands. */ 19976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 19977 break; 19978 /* Consume the `,'. */ 19979 cp_lexer_consume_token (parser->lexer); 19980 } 19981 19982 return invalid_operands ? error_mark_node : nreverse (asm_operands); 19983 } 19984 19985 /* Parse an asm-clobber-list. 19986 19987 asm-clobber-list: 19988 string-literal 19989 asm-clobber-list , string-literal 19990 19991 Returns a TREE_LIST, indicating the clobbers in the order that they 19992 appeared. The TREE_VALUE of each node is a STRING_CST. */ 19993 19994 static tree 19995 cp_parser_asm_clobber_list (cp_parser* parser) 19996 { 19997 tree clobbers = NULL_TREE; 19998 19999 while (true) 20000 { 20001 tree string_literal; 20002 20003 /* Look for the string literal. */ 20004 string_literal = cp_parser_string_literal (parser, false, false); 20005 /* Add it to the list. */ 20006 clobbers = tree_cons (NULL_TREE, string_literal, clobbers); 20007 /* If the next token is not a `,', then the list is 20008 complete. */ 20009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 20010 break; 20011 /* Consume the `,' token. */ 20012 cp_lexer_consume_token (parser->lexer); 20013 } 20014 20015 return clobbers; 20016 } 20017 20018 /* Parse an asm-label-list. 20019 20020 asm-label-list: 20021 identifier 20022 asm-label-list , identifier 20023 20024 Returns a TREE_LIST, indicating the labels in the order that they 20025 appeared. The TREE_VALUE of each node is a label. */ 20026 20027 static tree 20028 cp_parser_asm_label_list (cp_parser* parser) 20029 { 20030 tree labels = NULL_TREE; 20031 20032 while (true) 20033 { 20034 tree identifier, label, name; 20035 20036 /* Look for the identifier. */ 20037 identifier = cp_parser_identifier (parser); 20038 if (!error_operand_p (identifier)) 20039 { 20040 label = lookup_label (identifier); 20041 if (TREE_CODE (label) == LABEL_DECL) 20042 { 20043 TREE_USED (label) = 1; 20044 check_goto (label); 20045 name = build_string (IDENTIFIER_LENGTH (identifier), 20046 IDENTIFIER_POINTER (identifier)); 20047 labels = tree_cons (name, label, labels); 20048 } 20049 } 20050 /* If the next token is not a `,', then the list is 20051 complete. */ 20052 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 20053 break; 20054 /* Consume the `,' token. */ 20055 cp_lexer_consume_token (parser->lexer); 20056 } 20057 20058 return nreverse (labels); 20059 } 20060 20061 /* Parse an (optional) series of attributes. 20062 20063 attributes: 20064 attributes attribute 20065 20066 attribute: 20067 __attribute__ (( attribute-list [opt] )) 20068 20069 The return value is as for cp_parser_attribute_list. */ 20070 20071 static tree 20072 cp_parser_attributes_opt (cp_parser* parser) 20073 { 20074 tree attributes = NULL_TREE; 20075 20076 while (true) 20077 { 20078 cp_token *token; 20079 tree attribute_list; 20080 20081 /* Peek at the next token. */ 20082 token = cp_lexer_peek_token (parser->lexer); 20083 /* If it's not `__attribute__', then we're done. */ 20084 if (token->keyword != RID_ATTRIBUTE) 20085 break; 20086 20087 /* Consume the `__attribute__' keyword. */ 20088 cp_lexer_consume_token (parser->lexer); 20089 /* Look for the two `(' tokens. */ 20090 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 20091 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 20092 20093 /* Peek at the next token. */ 20094 token = cp_lexer_peek_token (parser->lexer); 20095 if (token->type != CPP_CLOSE_PAREN) 20096 /* Parse the attribute-list. */ 20097 attribute_list = cp_parser_attribute_list (parser); 20098 else 20099 /* If the next token is a `)', then there is no attribute 20100 list. */ 20101 attribute_list = NULL; 20102 20103 /* Look for the two `)' tokens. */ 20104 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 20105 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 20106 20107 /* Add these new attributes to the list. */ 20108 attributes = chainon (attributes, attribute_list); 20109 } 20110 20111 return attributes; 20112 } 20113 20114 /* Parse an attribute-list. 20115 20116 attribute-list: 20117 attribute 20118 attribute-list , attribute 20119 20120 attribute: 20121 identifier 20122 identifier ( identifier ) 20123 identifier ( identifier , expression-list ) 20124 identifier ( expression-list ) 20125 20126 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds 20127 to an attribute. The TREE_PURPOSE of each node is the identifier 20128 indicating which attribute is in use. The TREE_VALUE represents 20129 the arguments, if any. */ 20130 20131 static tree 20132 cp_parser_attribute_list (cp_parser* parser) 20133 { 20134 tree attribute_list = NULL_TREE; 20135 bool save_translate_strings_p = parser->translate_strings_p; 20136 20137 parser->translate_strings_p = false; 20138 while (true) 20139 { 20140 cp_token *token; 20141 tree identifier; 20142 tree attribute; 20143 20144 /* Look for the identifier. We also allow keywords here; for 20145 example `__attribute__ ((const))' is legal. */ 20146 token = cp_lexer_peek_token (parser->lexer); 20147 if (token->type == CPP_NAME 20148 || token->type == CPP_KEYWORD) 20149 { 20150 tree arguments = NULL_TREE; 20151 20152 /* Consume the token. */ 20153 token = cp_lexer_consume_token (parser->lexer); 20154 20155 /* Save away the identifier that indicates which attribute 20156 this is. */ 20157 identifier = (token->type == CPP_KEYWORD) 20158 /* For keywords, use the canonical spelling, not the 20159 parsed identifier. */ 20160 ? ridpointers[(int) token->keyword] 20161 : token->u.value; 20162 20163 attribute = build_tree_list (identifier, NULL_TREE); 20164 20165 /* Peek at the next token. */ 20166 token = cp_lexer_peek_token (parser->lexer); 20167 /* If it's an `(', then parse the attribute arguments. */ 20168 if (token->type == CPP_OPEN_PAREN) 20169 { 20170 VEC(tree,gc) *vec; 20171 int attr_flag = (attribute_takes_identifier_p (identifier) 20172 ? id_attr : normal_attr); 20173 vec = cp_parser_parenthesized_expression_list 20174 (parser, attr_flag, /*cast_p=*/false, 20175 /*allow_expansion_p=*/false, 20176 /*non_constant_p=*/NULL); 20177 if (vec == NULL) 20178 arguments = error_mark_node; 20179 else 20180 { 20181 arguments = build_tree_list_vec (vec); 20182 release_tree_vector (vec); 20183 } 20184 /* Save the arguments away. */ 20185 TREE_VALUE (attribute) = arguments; 20186 } 20187 20188 if (arguments != error_mark_node) 20189 { 20190 /* Add this attribute to the list. */ 20191 TREE_CHAIN (attribute) = attribute_list; 20192 attribute_list = attribute; 20193 } 20194 20195 token = cp_lexer_peek_token (parser->lexer); 20196 } 20197 /* Now, look for more attributes. If the next token isn't a 20198 `,', we're done. */ 20199 if (token->type != CPP_COMMA) 20200 break; 20201 20202 /* Consume the comma and keep going. */ 20203 cp_lexer_consume_token (parser->lexer); 20204 } 20205 parser->translate_strings_p = save_translate_strings_p; 20206 20207 /* We built up the list in reverse order. */ 20208 return nreverse (attribute_list); 20209 } 20210 20211 /* Parse an optional `__extension__' keyword. Returns TRUE if it is 20212 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the 20213 current value of the PEDANTIC flag, regardless of whether or not 20214 the `__extension__' keyword is present. The caller is responsible 20215 for restoring the value of the PEDANTIC flag. */ 20216 20217 static bool 20218 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic) 20219 { 20220 /* Save the old value of the PEDANTIC flag. */ 20221 *saved_pedantic = pedantic; 20222 20223 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION)) 20224 { 20225 /* Consume the `__extension__' token. */ 20226 cp_lexer_consume_token (parser->lexer); 20227 /* We're not being pedantic while the `__extension__' keyword is 20228 in effect. */ 20229 pedantic = 0; 20230 20231 return true; 20232 } 20233 20234 return false; 20235 } 20236 20237 /* Parse a label declaration. 20238 20239 label-declaration: 20240 __label__ label-declarator-seq ; 20241 20242 label-declarator-seq: 20243 identifier , label-declarator-seq 20244 identifier */ 20245 20246 static void 20247 cp_parser_label_declaration (cp_parser* parser) 20248 { 20249 /* Look for the `__label__' keyword. */ 20250 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL); 20251 20252 while (true) 20253 { 20254 tree identifier; 20255 20256 /* Look for an identifier. */ 20257 identifier = cp_parser_identifier (parser); 20258 /* If we failed, stop. */ 20259 if (identifier == error_mark_node) 20260 break; 20261 /* Declare it as a label. */ 20262 finish_label_decl (identifier); 20263 /* If the next token is a `;', stop. */ 20264 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 20265 break; 20266 /* Look for the `,' separating the label declarations. */ 20267 cp_parser_require (parser, CPP_COMMA, RT_COMMA); 20268 } 20269 20270 /* Look for the final `;'. */ 20271 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 20272 } 20273 20274 /* Support Functions */ 20275 20276 /* Looks up NAME in the current scope, as given by PARSER->SCOPE. 20277 NAME should have one of the representations used for an 20278 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE 20279 is returned. If PARSER->SCOPE is a dependent type, then a 20280 SCOPE_REF is returned. 20281 20282 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately 20283 returned; the name was already resolved when the TEMPLATE_ID_EXPR 20284 was formed. Abstractly, such entities should not be passed to this 20285 function, because they do not need to be looked up, but it is 20286 simpler to check for this special case here, rather than at the 20287 call-sites. 20288 20289 In cases not explicitly covered above, this function returns a 20290 DECL, OVERLOAD, or baselink representing the result of the lookup. 20291 If there was no entity with the indicated NAME, the ERROR_MARK_NODE 20292 is returned. 20293 20294 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword 20295 (e.g., "struct") that was used. In that case bindings that do not 20296 refer to types are ignored. 20297 20298 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are 20299 ignored. 20300 20301 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces 20302 are ignored. 20303 20304 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent 20305 types. 20306 20307 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a 20308 TREE_LIST of candidates if name-lookup results in an ambiguity, and 20309 NULL_TREE otherwise. */ 20310 20311 static tree 20312 cp_parser_lookup_name (cp_parser *parser, tree name, 20313 enum tag_types tag_type, 20314 bool is_template, 20315 bool is_namespace, 20316 bool check_dependency, 20317 tree *ambiguous_decls, 20318 location_t name_location) 20319 { 20320 int flags = 0; 20321 tree decl; 20322 tree object_type = parser->context->object_type; 20323 20324 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)) 20325 flags |= LOOKUP_COMPLAIN; 20326 20327 /* Assume that the lookup will be unambiguous. */ 20328 if (ambiguous_decls) 20329 *ambiguous_decls = NULL_TREE; 20330 20331 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is 20332 no longer valid. Note that if we are parsing tentatively, and 20333 the parse fails, OBJECT_TYPE will be automatically restored. */ 20334 parser->context->object_type = NULL_TREE; 20335 20336 if (name == error_mark_node) 20337 return error_mark_node; 20338 20339 /* A template-id has already been resolved; there is no lookup to 20340 do. */ 20341 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 20342 return name; 20343 if (BASELINK_P (name)) 20344 { 20345 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name)) 20346 == TEMPLATE_ID_EXPR); 20347 return name; 20348 } 20349 20350 /* A BIT_NOT_EXPR is used to represent a destructor. By this point, 20351 it should already have been checked to make sure that the name 20352 used matches the type being destroyed. */ 20353 if (TREE_CODE (name) == BIT_NOT_EXPR) 20354 { 20355 tree type; 20356 20357 /* Figure out to which type this destructor applies. */ 20358 if (parser->scope) 20359 type = parser->scope; 20360 else if (object_type) 20361 type = object_type; 20362 else 20363 type = current_class_type; 20364 /* If that's not a class type, there is no destructor. */ 20365 if (!type || !CLASS_TYPE_P (type)) 20366 return error_mark_node; 20367 if (CLASSTYPE_LAZY_DESTRUCTOR (type)) 20368 lazily_declare_fn (sfk_destructor, type); 20369 if (!CLASSTYPE_DESTRUCTORS (type)) 20370 return error_mark_node; 20371 /* If it was a class type, return the destructor. */ 20372 return CLASSTYPE_DESTRUCTORS (type); 20373 } 20374 20375 /* By this point, the NAME should be an ordinary identifier. If 20376 the id-expression was a qualified name, the qualifying scope is 20377 stored in PARSER->SCOPE at this point. */ 20378 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 20379 20380 /* Perform the lookup. */ 20381 if (parser->scope) 20382 { 20383 bool dependent_p; 20384 20385 if (parser->scope == error_mark_node) 20386 return error_mark_node; 20387 20388 /* If the SCOPE is dependent, the lookup must be deferred until 20389 the template is instantiated -- unless we are explicitly 20390 looking up names in uninstantiated templates. Even then, we 20391 cannot look up the name if the scope is not a class type; it 20392 might, for example, be a template type parameter. */ 20393 dependent_p = (TYPE_P (parser->scope) 20394 && dependent_scope_p (parser->scope)); 20395 if ((check_dependency || !CLASS_TYPE_P (parser->scope)) 20396 && dependent_p) 20397 /* Defer lookup. */ 20398 decl = error_mark_node; 20399 else 20400 { 20401 tree pushed_scope = NULL_TREE; 20402 20403 /* If PARSER->SCOPE is a dependent type, then it must be a 20404 class type, and we must not be checking dependencies; 20405 otherwise, we would have processed this lookup above. So 20406 that PARSER->SCOPE is not considered a dependent base by 20407 lookup_member, we must enter the scope here. */ 20408 if (dependent_p) 20409 pushed_scope = push_scope (parser->scope); 20410 20411 /* If the PARSER->SCOPE is a template specialization, it 20412 may be instantiated during name lookup. In that case, 20413 errors may be issued. Even if we rollback the current 20414 tentative parse, those errors are valid. */ 20415 decl = lookup_qualified_name (parser->scope, name, 20416 tag_type != none_type, 20417 /*complain=*/true); 20418 20419 /* 3.4.3.1: In a lookup in which the constructor is an acceptable 20420 lookup result and the nested-name-specifier nominates a class C: 20421 * if the name specified after the nested-name-specifier, when 20422 looked up in C, is the injected-class-name of C (Clause 9), or 20423 * if the name specified after the nested-name-specifier is the 20424 same as the identifier or the simple-template-id's template- 20425 name in the last component of the nested-name-specifier, 20426 the name is instead considered to name the constructor of 20427 class C. [ Note: for example, the constructor is not an 20428 acceptable lookup result in an elaborated-type-specifier so 20429 the constructor would not be used in place of the 20430 injected-class-name. --end note ] Such a constructor name 20431 shall be used only in the declarator-id of a declaration that 20432 names a constructor or in a using-declaration. */ 20433 if (tag_type == none_type 20434 && DECL_SELF_REFERENCE_P (decl) 20435 && same_type_p (DECL_CONTEXT (decl), parser->scope)) 20436 decl = lookup_qualified_name (parser->scope, ctor_identifier, 20437 tag_type != none_type, 20438 /*complain=*/true); 20439 20440 /* If we have a single function from a using decl, pull it out. */ 20441 if (TREE_CODE (decl) == OVERLOAD 20442 && !really_overloaded_fn (decl)) 20443 decl = OVL_FUNCTION (decl); 20444 20445 if (pushed_scope) 20446 pop_scope (pushed_scope); 20447 } 20448 20449 /* If the scope is a dependent type and either we deferred lookup or 20450 we did lookup but didn't find the name, rememeber the name. */ 20451 if (decl == error_mark_node && TYPE_P (parser->scope) 20452 && dependent_type_p (parser->scope)) 20453 { 20454 if (tag_type) 20455 { 20456 tree type; 20457 20458 /* The resolution to Core Issue 180 says that `struct 20459 A::B' should be considered a type-name, even if `A' 20460 is dependent. */ 20461 type = make_typename_type (parser->scope, name, tag_type, 20462 /*complain=*/tf_error); 20463 decl = TYPE_NAME (type); 20464 } 20465 else if (is_template 20466 && (cp_parser_next_token_ends_template_argument_p (parser) 20467 || cp_lexer_next_token_is (parser->lexer, 20468 CPP_CLOSE_PAREN))) 20469 decl = make_unbound_class_template (parser->scope, 20470 name, NULL_TREE, 20471 /*complain=*/tf_error); 20472 else 20473 decl = build_qualified_name (/*type=*/NULL_TREE, 20474 parser->scope, name, 20475 is_template); 20476 } 20477 parser->qualifying_scope = parser->scope; 20478 parser->object_scope = NULL_TREE; 20479 } 20480 else if (object_type) 20481 { 20482 tree object_decl = NULL_TREE; 20483 /* Look up the name in the scope of the OBJECT_TYPE, unless the 20484 OBJECT_TYPE is not a class. */ 20485 if (CLASS_TYPE_P (object_type)) 20486 /* If the OBJECT_TYPE is a template specialization, it may 20487 be instantiated during name lookup. In that case, errors 20488 may be issued. Even if we rollback the current tentative 20489 parse, those errors are valid. */ 20490 object_decl = lookup_member (object_type, 20491 name, 20492 /*protect=*/0, 20493 tag_type != none_type, 20494 tf_warning_or_error); 20495 /* Look it up in the enclosing context, too. */ 20496 decl = lookup_name_real (name, tag_type != none_type, 20497 /*nonclass=*/0, 20498 /*block_p=*/true, is_namespace, flags); 20499 parser->object_scope = object_type; 20500 parser->qualifying_scope = NULL_TREE; 20501 if (object_decl) 20502 decl = object_decl; 20503 } 20504 else 20505 { 20506 decl = lookup_name_real (name, tag_type != none_type, 20507 /*nonclass=*/0, 20508 /*block_p=*/true, is_namespace, flags); 20509 parser->qualifying_scope = NULL_TREE; 20510 parser->object_scope = NULL_TREE; 20511 } 20512 20513 /* If the lookup failed, let our caller know. */ 20514 if (!decl || decl == error_mark_node) 20515 return error_mark_node; 20516 20517 /* Pull out the template from an injected-class-name (or multiple). */ 20518 if (is_template) 20519 decl = maybe_get_template_decl_from_type_decl (decl); 20520 20521 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ 20522 if (TREE_CODE (decl) == TREE_LIST) 20523 { 20524 if (ambiguous_decls) 20525 *ambiguous_decls = decl; 20526 /* The error message we have to print is too complicated for 20527 cp_parser_error, so we incorporate its actions directly. */ 20528 if (!cp_parser_simulate_error (parser)) 20529 { 20530 error_at (name_location, "reference to %qD is ambiguous", 20531 name); 20532 print_candidates (decl); 20533 } 20534 return error_mark_node; 20535 } 20536 20537 gcc_assert (DECL_P (decl) 20538 || TREE_CODE (decl) == OVERLOAD 20539 || TREE_CODE (decl) == SCOPE_REF 20540 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE 20541 || BASELINK_P (decl)); 20542 20543 /* If we have resolved the name of a member declaration, check to 20544 see if the declaration is accessible. When the name resolves to 20545 set of overloaded functions, accessibility is checked when 20546 overload resolution is done. 20547 20548 During an explicit instantiation, access is not checked at all, 20549 as per [temp.explicit]. */ 20550 if (DECL_P (decl)) 20551 check_accessibility_of_qualified_id (decl, object_type, parser->scope); 20552 20553 maybe_record_typedef_use (decl); 20554 20555 return decl; 20556 } 20557 20558 /* Like cp_parser_lookup_name, but for use in the typical case where 20559 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE, 20560 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */ 20561 20562 static tree 20563 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location) 20564 { 20565 return cp_parser_lookup_name (parser, name, 20566 none_type, 20567 /*is_template=*/false, 20568 /*is_namespace=*/false, 20569 /*check_dependency=*/true, 20570 /*ambiguous_decls=*/NULL, 20571 location); 20572 } 20573 20574 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in 20575 the current context, return the TYPE_DECL. If TAG_NAME_P is 20576 true, the DECL indicates the class being defined in a class-head, 20577 or declared in an elaborated-type-specifier. 20578 20579 Otherwise, return DECL. */ 20580 20581 static tree 20582 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p) 20583 { 20584 /* If the TEMPLATE_DECL is being declared as part of a class-head, 20585 the translation from TEMPLATE_DECL to TYPE_DECL occurs: 20586 20587 struct A { 20588 template <typename T> struct B; 20589 }; 20590 20591 template <typename T> struct A::B {}; 20592 20593 Similarly, in an elaborated-type-specifier: 20594 20595 namespace N { struct X{}; } 20596 20597 struct A { 20598 template <typename T> friend struct N::X; 20599 }; 20600 20601 However, if the DECL refers to a class type, and we are in 20602 the scope of the class, then the name lookup automatically 20603 finds the TYPE_DECL created by build_self_reference rather 20604 than a TEMPLATE_DECL. For example, in: 20605 20606 template <class T> struct S { 20607 S s; 20608 }; 20609 20610 there is no need to handle such case. */ 20611 20612 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p) 20613 return DECL_TEMPLATE_RESULT (decl); 20614 20615 return decl; 20616 } 20617 20618 /* If too many, or too few, template-parameter lists apply to the 20619 declarator, issue an error message. Returns TRUE if all went well, 20620 and FALSE otherwise. */ 20621 20622 static bool 20623 cp_parser_check_declarator_template_parameters (cp_parser* parser, 20624 cp_declarator *declarator, 20625 location_t declarator_location) 20626 { 20627 unsigned num_templates; 20628 20629 /* We haven't seen any classes that involve template parameters yet. */ 20630 num_templates = 0; 20631 20632 switch (declarator->kind) 20633 { 20634 case cdk_id: 20635 if (declarator->u.id.qualifying_scope) 20636 { 20637 tree scope; 20638 20639 scope = declarator->u.id.qualifying_scope; 20640 20641 while (scope && CLASS_TYPE_P (scope)) 20642 { 20643 /* You're supposed to have one `template <...>' 20644 for every template class, but you don't need one 20645 for a full specialization. For example: 20646 20647 template <class T> struct S{}; 20648 template <> struct S<int> { void f(); }; 20649 void S<int>::f () {} 20650 20651 is correct; there shouldn't be a `template <>' for 20652 the definition of `S<int>::f'. */ 20653 if (!CLASSTYPE_TEMPLATE_INFO (scope)) 20654 /* If SCOPE does not have template information of any 20655 kind, then it is not a template, nor is it nested 20656 within a template. */ 20657 break; 20658 if (explicit_class_specialization_p (scope)) 20659 break; 20660 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) 20661 ++num_templates; 20662 20663 scope = TYPE_CONTEXT (scope); 20664 } 20665 } 20666 else if (TREE_CODE (declarator->u.id.unqualified_name) 20667 == TEMPLATE_ID_EXPR) 20668 /* If the DECLARATOR has the form `X<y>' then it uses one 20669 additional level of template parameters. */ 20670 ++num_templates; 20671 20672 return cp_parser_check_template_parameters 20673 (parser, num_templates, declarator_location, declarator); 20674 20675 20676 case cdk_function: 20677 case cdk_array: 20678 case cdk_pointer: 20679 case cdk_reference: 20680 case cdk_ptrmem: 20681 return (cp_parser_check_declarator_template_parameters 20682 (parser, declarator->declarator, declarator_location)); 20683 20684 case cdk_error: 20685 return true; 20686 20687 default: 20688 gcc_unreachable (); 20689 } 20690 return false; 20691 } 20692 20693 /* NUM_TEMPLATES were used in the current declaration. If that is 20694 invalid, return FALSE and issue an error messages. Otherwise, 20695 return TRUE. If DECLARATOR is non-NULL, then we are checking a 20696 declarator and we can print more accurate diagnostics. */ 20697 20698 static bool 20699 cp_parser_check_template_parameters (cp_parser* parser, 20700 unsigned num_templates, 20701 location_t location, 20702 cp_declarator *declarator) 20703 { 20704 /* If there are the same number of template classes and parameter 20705 lists, that's OK. */ 20706 if (parser->num_template_parameter_lists == num_templates) 20707 return true; 20708 /* If there are more, but only one more, then we are referring to a 20709 member template. That's OK too. */ 20710 if (parser->num_template_parameter_lists == num_templates + 1) 20711 return true; 20712 /* If there are more template classes than parameter lists, we have 20713 something like: 20714 20715 template <class T> void S<T>::R<T>::f (); */ 20716 if (parser->num_template_parameter_lists < num_templates) 20717 { 20718 if (declarator && !current_function_decl) 20719 error_at (location, "specializing member %<%T::%E%> " 20720 "requires %<template<>%> syntax", 20721 declarator->u.id.qualifying_scope, 20722 declarator->u.id.unqualified_name); 20723 else if (declarator) 20724 error_at (location, "invalid declaration of %<%T::%E%>", 20725 declarator->u.id.qualifying_scope, 20726 declarator->u.id.unqualified_name); 20727 else 20728 error_at (location, "too few template-parameter-lists"); 20729 return false; 20730 } 20731 /* Otherwise, there are too many template parameter lists. We have 20732 something like: 20733 20734 template <class T> template <class U> void S::f(); */ 20735 error_at (location, "too many template-parameter-lists"); 20736 return false; 20737 } 20738 20739 /* Parse an optional `::' token indicating that the following name is 20740 from the global namespace. If so, PARSER->SCOPE is set to the 20741 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE, 20742 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone. 20743 Returns the new value of PARSER->SCOPE, if the `::' token is 20744 present, and NULL_TREE otherwise. */ 20745 20746 static tree 20747 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p) 20748 { 20749 cp_token *token; 20750 20751 /* Peek at the next token. */ 20752 token = cp_lexer_peek_token (parser->lexer); 20753 /* If we're looking at a `::' token then we're starting from the 20754 global namespace, not our current location. */ 20755 if (token->type == CPP_SCOPE) 20756 { 20757 /* Consume the `::' token. */ 20758 cp_lexer_consume_token (parser->lexer); 20759 /* Set the SCOPE so that we know where to start the lookup. */ 20760 parser->scope = global_namespace; 20761 parser->qualifying_scope = global_namespace; 20762 parser->object_scope = NULL_TREE; 20763 20764 return parser->scope; 20765 } 20766 else if (!current_scope_valid_p) 20767 { 20768 parser->scope = NULL_TREE; 20769 parser->qualifying_scope = NULL_TREE; 20770 parser->object_scope = NULL_TREE; 20771 } 20772 20773 return NULL_TREE; 20774 } 20775 20776 /* Returns TRUE if the upcoming token sequence is the start of a 20777 constructor declarator. If FRIEND_P is true, the declarator is 20778 preceded by the `friend' specifier. */ 20779 20780 static bool 20781 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p) 20782 { 20783 bool constructor_p; 20784 tree nested_name_specifier; 20785 cp_token *next_token; 20786 20787 /* The common case is that this is not a constructor declarator, so 20788 try to avoid doing lots of work if at all possible. It's not 20789 valid declare a constructor at function scope. */ 20790 if (parser->in_function_body) 20791 return false; 20792 /* And only certain tokens can begin a constructor declarator. */ 20793 next_token = cp_lexer_peek_token (parser->lexer); 20794 if (next_token->type != CPP_NAME 20795 && next_token->type != CPP_SCOPE 20796 && next_token->type != CPP_NESTED_NAME_SPECIFIER 20797 && next_token->type != CPP_TEMPLATE_ID) 20798 return false; 20799 20800 /* Parse tentatively; we are going to roll back all of the tokens 20801 consumed here. */ 20802 cp_parser_parse_tentatively (parser); 20803 /* Assume that we are looking at a constructor declarator. */ 20804 constructor_p = true; 20805 20806 /* Look for the optional `::' operator. */ 20807 cp_parser_global_scope_opt (parser, 20808 /*current_scope_valid_p=*/false); 20809 /* Look for the nested-name-specifier. */ 20810 nested_name_specifier 20811 = (cp_parser_nested_name_specifier_opt (parser, 20812 /*typename_keyword_p=*/false, 20813 /*check_dependency_p=*/false, 20814 /*type_p=*/false, 20815 /*is_declaration=*/false)); 20816 /* Outside of a class-specifier, there must be a 20817 nested-name-specifier. */ 20818 if (!nested_name_specifier && 20819 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type) 20820 || friend_p)) 20821 constructor_p = false; 20822 else if (nested_name_specifier == error_mark_node) 20823 constructor_p = false; 20824 20825 /* If we have a class scope, this is easy; DR 147 says that S::S always 20826 names the constructor, and no other qualified name could. */ 20827 if (constructor_p && nested_name_specifier 20828 && CLASS_TYPE_P (nested_name_specifier)) 20829 { 20830 tree id = cp_parser_unqualified_id (parser, 20831 /*template_keyword_p=*/false, 20832 /*check_dependency_p=*/false, 20833 /*declarator_p=*/true, 20834 /*optional_p=*/false); 20835 if (is_overloaded_fn (id)) 20836 id = DECL_NAME (get_first_fn (id)); 20837 if (!constructor_name_p (id, nested_name_specifier)) 20838 constructor_p = false; 20839 } 20840 /* If we still think that this might be a constructor-declarator, 20841 look for a class-name. */ 20842 else if (constructor_p) 20843 { 20844 /* If we have: 20845 20846 template <typename T> struct S { 20847 S(); 20848 }; 20849 20850 we must recognize that the nested `S' names a class. */ 20851 tree type_decl; 20852 type_decl = cp_parser_class_name (parser, 20853 /*typename_keyword_p=*/false, 20854 /*template_keyword_p=*/false, 20855 none_type, 20856 /*check_dependency_p=*/false, 20857 /*class_head_p=*/false, 20858 /*is_declaration=*/false); 20859 /* If there was no class-name, then this is not a constructor. */ 20860 constructor_p = !cp_parser_error_occurred (parser); 20861 20862 /* If we're still considering a constructor, we have to see a `(', 20863 to begin the parameter-declaration-clause, followed by either a 20864 `)', an `...', or a decl-specifier. We need to check for a 20865 type-specifier to avoid being fooled into thinking that: 20866 20867 S (f) (int); 20868 20869 is a constructor. (It is actually a function named `f' that 20870 takes one parameter (of type `int') and returns a value of type 20871 `S'. */ 20872 if (constructor_p 20873 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 20874 constructor_p = false; 20875 20876 if (constructor_p 20877 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN) 20878 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS) 20879 /* A parameter declaration begins with a decl-specifier, 20880 which is either the "attribute" keyword, a storage class 20881 specifier, or (usually) a type-specifier. */ 20882 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)) 20883 { 20884 tree type; 20885 tree pushed_scope = NULL_TREE; 20886 unsigned saved_num_template_parameter_lists; 20887 20888 /* Names appearing in the type-specifier should be looked up 20889 in the scope of the class. */ 20890 if (current_class_type) 20891 type = NULL_TREE; 20892 else 20893 { 20894 type = TREE_TYPE (type_decl); 20895 if (TREE_CODE (type) == TYPENAME_TYPE) 20896 { 20897 type = resolve_typename_type (type, 20898 /*only_current_p=*/false); 20899 if (TREE_CODE (type) == TYPENAME_TYPE) 20900 { 20901 cp_parser_abort_tentative_parse (parser); 20902 return false; 20903 } 20904 } 20905 pushed_scope = push_scope (type); 20906 } 20907 20908 /* Inside the constructor parameter list, surrounding 20909 template-parameter-lists do not apply. */ 20910 saved_num_template_parameter_lists 20911 = parser->num_template_parameter_lists; 20912 parser->num_template_parameter_lists = 0; 20913 20914 /* Look for the type-specifier. */ 20915 cp_parser_type_specifier (parser, 20916 CP_PARSER_FLAGS_NONE, 20917 /*decl_specs=*/NULL, 20918 /*is_declarator=*/true, 20919 /*declares_class_or_enum=*/NULL, 20920 /*is_cv_qualifier=*/NULL); 20921 20922 parser->num_template_parameter_lists 20923 = saved_num_template_parameter_lists; 20924 20925 /* Leave the scope of the class. */ 20926 if (pushed_scope) 20927 pop_scope (pushed_scope); 20928 20929 constructor_p = !cp_parser_error_occurred (parser); 20930 } 20931 } 20932 20933 /* We did not really want to consume any tokens. */ 20934 cp_parser_abort_tentative_parse (parser); 20935 20936 return constructor_p; 20937 } 20938 20939 /* Parse the definition of the function given by the DECL_SPECIFIERS, 20940 ATTRIBUTES, and DECLARATOR. The access checks have been deferred; 20941 they must be performed once we are in the scope of the function. 20942 20943 Returns the function defined. */ 20944 20945 static tree 20946 cp_parser_function_definition_from_specifiers_and_declarator 20947 (cp_parser* parser, 20948 cp_decl_specifier_seq *decl_specifiers, 20949 tree attributes, 20950 const cp_declarator *declarator) 20951 { 20952 tree fn; 20953 bool success_p; 20954 20955 /* Begin the function-definition. */ 20956 success_p = start_function (decl_specifiers, declarator, attributes); 20957 20958 /* The things we're about to see are not directly qualified by any 20959 template headers we've seen thus far. */ 20960 reset_specialization (); 20961 20962 /* If there were names looked up in the decl-specifier-seq that we 20963 did not check, check them now. We must wait until we are in the 20964 scope of the function to perform the checks, since the function 20965 might be a friend. */ 20966 perform_deferred_access_checks (); 20967 20968 if (!success_p) 20969 { 20970 /* Skip the entire function. */ 20971 cp_parser_skip_to_end_of_block_or_statement (parser); 20972 fn = error_mark_node; 20973 } 20974 else if (DECL_INITIAL (current_function_decl) != error_mark_node) 20975 { 20976 /* Seen already, skip it. An error message has already been output. */ 20977 cp_parser_skip_to_end_of_block_or_statement (parser); 20978 fn = current_function_decl; 20979 current_function_decl = NULL_TREE; 20980 /* If this is a function from a class, pop the nested class. */ 20981 if (current_class_name) 20982 pop_nested_class (); 20983 } 20984 else 20985 { 20986 timevar_id_t tv; 20987 if (DECL_DECLARED_INLINE_P (current_function_decl)) 20988 tv = TV_PARSE_INLINE; 20989 else 20990 tv = TV_PARSE_FUNC; 20991 timevar_push (tv); 20992 fn = cp_parser_function_definition_after_declarator (parser, 20993 /*inline_p=*/false); 20994 timevar_pop (tv); 20995 } 20996 20997 return fn; 20998 } 20999 21000 /* Parse the part of a function-definition that follows the 21001 declarator. INLINE_P is TRUE iff this function is an inline 21002 function defined within a class-specifier. 21003 21004 Returns the function defined. */ 21005 21006 static tree 21007 cp_parser_function_definition_after_declarator (cp_parser* parser, 21008 bool inline_p) 21009 { 21010 tree fn; 21011 bool ctor_initializer_p = false; 21012 bool saved_in_unbraced_linkage_specification_p; 21013 bool saved_in_function_body; 21014 unsigned saved_num_template_parameter_lists; 21015 cp_token *token; 21016 21017 saved_in_function_body = parser->in_function_body; 21018 parser->in_function_body = true; 21019 /* If the next token is `return', then the code may be trying to 21020 make use of the "named return value" extension that G++ used to 21021 support. */ 21022 token = cp_lexer_peek_token (parser->lexer); 21023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN)) 21024 { 21025 /* Consume the `return' keyword. */ 21026 cp_lexer_consume_token (parser->lexer); 21027 /* Look for the identifier that indicates what value is to be 21028 returned. */ 21029 cp_parser_identifier (parser); 21030 /* Issue an error message. */ 21031 error_at (token->location, 21032 "named return values are no longer supported"); 21033 /* Skip tokens until we reach the start of the function body. */ 21034 while (true) 21035 { 21036 cp_token *token = cp_lexer_peek_token (parser->lexer); 21037 if (token->type == CPP_OPEN_BRACE 21038 || token->type == CPP_EOF 21039 || token->type == CPP_PRAGMA_EOL) 21040 break; 21041 cp_lexer_consume_token (parser->lexer); 21042 } 21043 } 21044 /* The `extern' in `extern "C" void f () { ... }' does not apply to 21045 anything declared inside `f'. */ 21046 saved_in_unbraced_linkage_specification_p 21047 = parser->in_unbraced_linkage_specification_p; 21048 parser->in_unbraced_linkage_specification_p = false; 21049 /* Inside the function, surrounding template-parameter-lists do not 21050 apply. */ 21051 saved_num_template_parameter_lists 21052 = parser->num_template_parameter_lists; 21053 parser->num_template_parameter_lists = 0; 21054 21055 start_lambda_scope (current_function_decl); 21056 21057 /* If the next token is `try', `__transaction_atomic', or 21058 `__transaction_relaxed`, then we are looking at either function-try-block 21059 or function-transaction-block. Note that all of these include the 21060 function-body. */ 21061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC)) 21062 ctor_initializer_p = cp_parser_function_transaction (parser, 21063 RID_TRANSACTION_ATOMIC); 21064 else if (cp_lexer_next_token_is_keyword (parser->lexer, 21065 RID_TRANSACTION_RELAXED)) 21066 ctor_initializer_p = cp_parser_function_transaction (parser, 21067 RID_TRANSACTION_RELAXED); 21068 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) 21069 ctor_initializer_p = cp_parser_function_try_block (parser); 21070 else 21071 ctor_initializer_p 21072 = cp_parser_ctor_initializer_opt_and_function_body (parser); 21073 21074 finish_lambda_scope (); 21075 21076 /* Finish the function. */ 21077 fn = finish_function ((ctor_initializer_p ? 1 : 0) | 21078 (inline_p ? 2 : 0)); 21079 /* Generate code for it, if necessary. */ 21080 expand_or_defer_fn (fn); 21081 /* Restore the saved values. */ 21082 parser->in_unbraced_linkage_specification_p 21083 = saved_in_unbraced_linkage_specification_p; 21084 parser->num_template_parameter_lists 21085 = saved_num_template_parameter_lists; 21086 parser->in_function_body = saved_in_function_body; 21087 21088 return fn; 21089 } 21090 21091 /* Parse a template-declaration, assuming that the `export' (and 21092 `extern') keywords, if present, has already been scanned. MEMBER_P 21093 is as for cp_parser_template_declaration. */ 21094 21095 static void 21096 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p) 21097 { 21098 tree decl = NULL_TREE; 21099 VEC (deferred_access_check,gc) *checks; 21100 tree parameter_list; 21101 bool friend_p = false; 21102 bool need_lang_pop; 21103 cp_token *token; 21104 21105 /* Look for the `template' keyword. */ 21106 token = cp_lexer_peek_token (parser->lexer); 21107 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE)) 21108 return; 21109 21110 /* And the `<'. */ 21111 if (!cp_parser_require (parser, CPP_LESS, RT_LESS)) 21112 return; 21113 if (at_class_scope_p () && current_function_decl) 21114 { 21115 /* 14.5.2.2 [temp.mem] 21116 21117 A local class shall not have member templates. */ 21118 error_at (token->location, 21119 "invalid declaration of member template in local class"); 21120 cp_parser_skip_to_end_of_block_or_statement (parser); 21121 return; 21122 } 21123 /* [temp] 21124 21125 A template ... shall not have C linkage. */ 21126 if (current_lang_name == lang_name_c) 21127 { 21128 error_at (token->location, "template with C linkage"); 21129 /* Give it C++ linkage to avoid confusing other parts of the 21130 front end. */ 21131 push_lang_context (lang_name_cplusplus); 21132 need_lang_pop = true; 21133 } 21134 else 21135 need_lang_pop = false; 21136 21137 /* We cannot perform access checks on the template parameter 21138 declarations until we know what is being declared, just as we 21139 cannot check the decl-specifier list. */ 21140 push_deferring_access_checks (dk_deferred); 21141 21142 /* If the next token is `>', then we have an invalid 21143 specialization. Rather than complain about an invalid template 21144 parameter, issue an error message here. */ 21145 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)) 21146 { 21147 cp_parser_error (parser, "invalid explicit specialization"); 21148 begin_specialization (); 21149 parameter_list = NULL_TREE; 21150 } 21151 else 21152 { 21153 /* Parse the template parameters. */ 21154 parameter_list = cp_parser_template_parameter_list (parser); 21155 } 21156 21157 /* Get the deferred access checks from the parameter list. These 21158 will be checked once we know what is being declared, as for a 21159 member template the checks must be performed in the scope of the 21160 class containing the member. */ 21161 checks = get_deferred_access_checks (); 21162 21163 /* Look for the `>'. */ 21164 cp_parser_skip_to_end_of_template_parameter_list (parser); 21165 /* We just processed one more parameter list. */ 21166 ++parser->num_template_parameter_lists; 21167 /* If the next token is `template', there are more template 21168 parameters. */ 21169 if (cp_lexer_next_token_is_keyword (parser->lexer, 21170 RID_TEMPLATE)) 21171 cp_parser_template_declaration_after_export (parser, member_p); 21172 else if (cxx_dialect >= cxx0x 21173 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING)) 21174 decl = cp_parser_alias_declaration (parser); 21175 else 21176 { 21177 /* There are no access checks when parsing a template, as we do not 21178 know if a specialization will be a friend. */ 21179 push_deferring_access_checks (dk_no_check); 21180 token = cp_lexer_peek_token (parser->lexer); 21181 decl = cp_parser_single_declaration (parser, 21182 checks, 21183 member_p, 21184 /*explicit_specialization_p=*/false, 21185 &friend_p); 21186 pop_deferring_access_checks (); 21187 21188 /* If this is a member template declaration, let the front 21189 end know. */ 21190 if (member_p && !friend_p && decl) 21191 { 21192 if (TREE_CODE (decl) == TYPE_DECL) 21193 cp_parser_check_access_in_redeclaration (decl, token->location); 21194 21195 decl = finish_member_template_decl (decl); 21196 } 21197 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL) 21198 make_friend_class (current_class_type, TREE_TYPE (decl), 21199 /*complain=*/true); 21200 } 21201 /* We are done with the current parameter list. */ 21202 --parser->num_template_parameter_lists; 21203 21204 pop_deferring_access_checks (); 21205 21206 /* Finish up. */ 21207 finish_template_decl (parameter_list); 21208 21209 /* Check the template arguments for a literal operator template. */ 21210 if (decl 21211 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl)) 21212 && UDLIT_OPER_P (DECL_NAME (decl))) 21213 { 21214 bool ok = true; 21215 if (parameter_list == NULL_TREE) 21216 ok = false; 21217 else 21218 { 21219 int num_parms = TREE_VEC_LENGTH (parameter_list); 21220 if (num_parms != 1) 21221 ok = false; 21222 else 21223 { 21224 tree parm_list = TREE_VEC_ELT (parameter_list, 0); 21225 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list); 21226 if (TREE_TYPE (parm) != char_type_node 21227 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) 21228 ok = false; 21229 } 21230 } 21231 if (!ok) 21232 error ("literal operator template %qD has invalid parameter list." 21233 " Expected non-type template argument pack <char...>", 21234 decl); 21235 } 21236 /* Register member declarations. */ 21237 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl)) 21238 finish_member_declaration (decl); 21239 /* For the erroneous case of a template with C linkage, we pushed an 21240 implicit C++ linkage scope; exit that scope now. */ 21241 if (need_lang_pop) 21242 pop_lang_context (); 21243 /* If DECL is a function template, we must return to parse it later. 21244 (Even though there is no definition, there might be default 21245 arguments that need handling.) */ 21246 if (member_p && decl 21247 && (TREE_CODE (decl) == FUNCTION_DECL 21248 || DECL_FUNCTION_TEMPLATE_P (decl))) 21249 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl); 21250 } 21251 21252 /* Perform the deferred access checks from a template-parameter-list. 21253 CHECKS is a TREE_LIST of access checks, as returned by 21254 get_deferred_access_checks. */ 21255 21256 static void 21257 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks) 21258 { 21259 ++processing_template_parmlist; 21260 perform_access_checks (checks); 21261 --processing_template_parmlist; 21262 } 21263 21264 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or 21265 `function-definition' sequence. MEMBER_P is true, this declaration 21266 appears in a class scope. 21267 21268 Returns the DECL for the declared entity. If FRIEND_P is non-NULL, 21269 *FRIEND_P is set to TRUE iff the declaration is a friend. */ 21270 21271 static tree 21272 cp_parser_single_declaration (cp_parser* parser, 21273 VEC (deferred_access_check,gc)* checks, 21274 bool member_p, 21275 bool explicit_specialization_p, 21276 bool* friend_p) 21277 { 21278 int declares_class_or_enum; 21279 tree decl = NULL_TREE; 21280 cp_decl_specifier_seq decl_specifiers; 21281 bool function_definition_p = false; 21282 cp_token *decl_spec_token_start; 21283 21284 /* This function is only used when processing a template 21285 declaration. */ 21286 gcc_assert (innermost_scope_kind () == sk_template_parms 21287 || innermost_scope_kind () == sk_template_spec); 21288 21289 /* Defer access checks until we know what is being declared. */ 21290 push_deferring_access_checks (dk_deferred); 21291 21292 /* Try the `decl-specifier-seq [opt] init-declarator [opt]' 21293 alternative. */ 21294 decl_spec_token_start = cp_lexer_peek_token (parser->lexer); 21295 cp_parser_decl_specifier_seq (parser, 21296 CP_PARSER_FLAGS_OPTIONAL, 21297 &decl_specifiers, 21298 &declares_class_or_enum); 21299 if (friend_p) 21300 *friend_p = cp_parser_friend_p (&decl_specifiers); 21301 21302 /* There are no template typedefs. */ 21303 if (decl_specifiers.specs[(int) ds_typedef]) 21304 { 21305 error_at (decl_spec_token_start->location, 21306 "template declaration of %<typedef%>"); 21307 decl = error_mark_node; 21308 } 21309 21310 /* Gather up the access checks that occurred the 21311 decl-specifier-seq. */ 21312 stop_deferring_access_checks (); 21313 21314 /* Check for the declaration of a template class. */ 21315 if (declares_class_or_enum) 21316 { 21317 if (cp_parser_declares_only_class_p (parser)) 21318 { 21319 decl = shadow_tag (&decl_specifiers); 21320 21321 /* In this case: 21322 21323 struct C { 21324 friend template <typename T> struct A<T>::B; 21325 }; 21326 21327 A<T>::B will be represented by a TYPENAME_TYPE, and 21328 therefore not recognized by shadow_tag. */ 21329 if (friend_p && *friend_p 21330 && !decl 21331 && decl_specifiers.type 21332 && TYPE_P (decl_specifiers.type)) 21333 decl = decl_specifiers.type; 21334 21335 if (decl && decl != error_mark_node) 21336 decl = TYPE_NAME (decl); 21337 else 21338 decl = error_mark_node; 21339 21340 /* Perform access checks for template parameters. */ 21341 cp_parser_perform_template_parameter_access_checks (checks); 21342 } 21343 } 21344 21345 /* Complain about missing 'typename' or other invalid type names. */ 21346 if (!decl_specifiers.any_type_specifiers_p 21347 && cp_parser_parse_and_diagnose_invalid_type_name (parser)) 21348 { 21349 /* cp_parser_parse_and_diagnose_invalid_type_name calls 21350 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse 21351 the rest of this declaration. */ 21352 decl = error_mark_node; 21353 goto out; 21354 } 21355 21356 /* If it's not a template class, try for a template function. If 21357 the next token is a `;', then this declaration does not declare 21358 anything. But, if there were errors in the decl-specifiers, then 21359 the error might well have come from an attempted class-specifier. 21360 In that case, there's no need to warn about a missing declarator. */ 21361 if (!decl 21362 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON) 21363 || decl_specifiers.type != error_mark_node)) 21364 { 21365 decl = cp_parser_init_declarator (parser, 21366 &decl_specifiers, 21367 checks, 21368 /*function_definition_allowed_p=*/true, 21369 member_p, 21370 declares_class_or_enum, 21371 &function_definition_p, 21372 NULL); 21373 21374 /* 7.1.1-1 [dcl.stc] 21375 21376 A storage-class-specifier shall not be specified in an explicit 21377 specialization... */ 21378 if (decl 21379 && explicit_specialization_p 21380 && decl_specifiers.storage_class != sc_none) 21381 { 21382 error_at (decl_spec_token_start->location, 21383 "explicit template specialization cannot have a storage class"); 21384 decl = error_mark_node; 21385 } 21386 } 21387 21388 /* Look for a trailing `;' after the declaration. */ 21389 if (!function_definition_p 21390 && (decl == error_mark_node 21391 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))) 21392 cp_parser_skip_to_end_of_block_or_statement (parser); 21393 21394 out: 21395 pop_deferring_access_checks (); 21396 21397 /* Clear any current qualification; whatever comes next is the start 21398 of something new. */ 21399 parser->scope = NULL_TREE; 21400 parser->qualifying_scope = NULL_TREE; 21401 parser->object_scope = NULL_TREE; 21402 21403 return decl; 21404 } 21405 21406 /* Parse a cast-expression that is not the operand of a unary "&". */ 21407 21408 static tree 21409 cp_parser_simple_cast_expression (cp_parser *parser) 21410 { 21411 return cp_parser_cast_expression (parser, /*address_p=*/false, 21412 /*cast_p=*/false, NULL); 21413 } 21414 21415 /* Parse a functional cast to TYPE. Returns an expression 21416 representing the cast. */ 21417 21418 static tree 21419 cp_parser_functional_cast (cp_parser* parser, tree type) 21420 { 21421 VEC(tree,gc) *vec; 21422 tree expression_list; 21423 tree cast; 21424 bool nonconst_p; 21425 21426 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 21427 { 21428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 21429 expression_list = cp_parser_braced_list (parser, &nonconst_p); 21430 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1; 21431 if (TREE_CODE (type) == TYPE_DECL) 21432 type = TREE_TYPE (type); 21433 return finish_compound_literal (type, expression_list, 21434 tf_warning_or_error); 21435 } 21436 21437 21438 vec = cp_parser_parenthesized_expression_list (parser, non_attr, 21439 /*cast_p=*/true, 21440 /*allow_expansion_p=*/true, 21441 /*non_constant_p=*/NULL); 21442 if (vec == NULL) 21443 expression_list = error_mark_node; 21444 else 21445 { 21446 expression_list = build_tree_list_vec (vec); 21447 release_tree_vector (vec); 21448 } 21449 21450 cast = build_functional_cast (type, expression_list, 21451 tf_warning_or_error); 21452 /* [expr.const]/1: In an integral constant expression "only type 21453 conversions to integral or enumeration type can be used". */ 21454 if (TREE_CODE (type) == TYPE_DECL) 21455 type = TREE_TYPE (type); 21456 if (cast != error_mark_node 21457 && !cast_valid_in_integral_constant_expression_p (type) 21458 && cp_parser_non_integral_constant_expression (parser, 21459 NIC_CONSTRUCTOR)) 21460 return error_mark_node; 21461 return cast; 21462 } 21463 21464 /* Save the tokens that make up the body of a member function defined 21465 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have 21466 already been parsed. The ATTRIBUTES are any GNU "__attribute__" 21467 specifiers applied to the declaration. Returns the FUNCTION_DECL 21468 for the member function. */ 21469 21470 static tree 21471 cp_parser_save_member_function_body (cp_parser* parser, 21472 cp_decl_specifier_seq *decl_specifiers, 21473 cp_declarator *declarator, 21474 tree attributes) 21475 { 21476 cp_token *first; 21477 cp_token *last; 21478 tree fn; 21479 21480 /* Create the FUNCTION_DECL. */ 21481 fn = grokmethod (decl_specifiers, declarator, attributes); 21482 /* If something went badly wrong, bail out now. */ 21483 if (fn == error_mark_node) 21484 { 21485 /* If there's a function-body, skip it. */ 21486 if (cp_parser_token_starts_function_definition_p 21487 (cp_lexer_peek_token (parser->lexer))) 21488 cp_parser_skip_to_end_of_block_or_statement (parser); 21489 return error_mark_node; 21490 } 21491 21492 /* Remember it, if there default args to post process. */ 21493 cp_parser_save_default_args (parser, fn); 21494 21495 /* Save away the tokens that make up the body of the 21496 function. */ 21497 first = parser->lexer->next_token; 21498 /* We can have braced-init-list mem-initializers before the fn body. */ 21499 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) 21500 { 21501 cp_lexer_consume_token (parser->lexer); 21502 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE) 21503 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY)) 21504 { 21505 /* cache_group will stop after an un-nested { } pair, too. */ 21506 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0)) 21507 break; 21508 21509 /* variadic mem-inits have ... after the ')'. */ 21510 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 21511 cp_lexer_consume_token (parser->lexer); 21512 } 21513 } 21514 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); 21515 /* Handle function try blocks. */ 21516 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH)) 21517 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); 21518 last = parser->lexer->next_token; 21519 21520 /* Save away the inline definition; we will process it when the 21521 class is complete. */ 21522 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last); 21523 DECL_PENDING_INLINE_P (fn) = 1; 21524 21525 /* We need to know that this was defined in the class, so that 21526 friend templates are handled correctly. */ 21527 DECL_INITIALIZED_IN_CLASS_P (fn) = 1; 21528 21529 /* Add FN to the queue of functions to be parsed later. */ 21530 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn); 21531 21532 return fn; 21533 } 21534 21535 /* Save the tokens that make up the in-class initializer for a non-static 21536 data member. Returns a DEFAULT_ARG. */ 21537 21538 static tree 21539 cp_parser_save_nsdmi (cp_parser* parser) 21540 { 21541 return cp_parser_cache_defarg (parser, /*nsdmi=*/true); 21542 } 21543 21544 /* Parse a template-argument-list, as well as the trailing ">" (but 21545 not the opening "<"). See cp_parser_template_argument_list for the 21546 return value. */ 21547 21548 static tree 21549 cp_parser_enclosed_template_argument_list (cp_parser* parser) 21550 { 21551 tree arguments; 21552 tree saved_scope; 21553 tree saved_qualifying_scope; 21554 tree saved_object_scope; 21555 bool saved_greater_than_is_operator_p; 21556 int saved_unevaluated_operand; 21557 int saved_inhibit_evaluation_warnings; 21558 21559 /* [temp.names] 21560 21561 When parsing a template-id, the first non-nested `>' is taken as 21562 the end of the template-argument-list rather than a greater-than 21563 operator. */ 21564 saved_greater_than_is_operator_p 21565 = parser->greater_than_is_operator_p; 21566 parser->greater_than_is_operator_p = false; 21567 /* Parsing the argument list may modify SCOPE, so we save it 21568 here. */ 21569 saved_scope = parser->scope; 21570 saved_qualifying_scope = parser->qualifying_scope; 21571 saved_object_scope = parser->object_scope; 21572 /* We need to evaluate the template arguments, even though this 21573 template-id may be nested within a "sizeof". */ 21574 saved_unevaluated_operand = cp_unevaluated_operand; 21575 cp_unevaluated_operand = 0; 21576 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; 21577 c_inhibit_evaluation_warnings = 0; 21578 /* Parse the template-argument-list itself. */ 21579 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER) 21580 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) 21581 arguments = NULL_TREE; 21582 else 21583 arguments = cp_parser_template_argument_list (parser); 21584 /* Look for the `>' that ends the template-argument-list. If we find 21585 a '>>' instead, it's probably just a typo. */ 21586 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)) 21587 { 21588 if (cxx_dialect != cxx98) 21589 { 21590 /* In C++0x, a `>>' in a template argument list or cast 21591 expression is considered to be two separate `>' 21592 tokens. So, change the current token to a `>', but don't 21593 consume it: it will be consumed later when the outer 21594 template argument list (or cast expression) is parsed. 21595 Note that this replacement of `>' for `>>' is necessary 21596 even if we are parsing tentatively: in the tentative 21597 case, after calling 21598 cp_parser_enclosed_template_argument_list we will always 21599 throw away all of the template arguments and the first 21600 closing `>', either because the template argument list 21601 was erroneous or because we are replacing those tokens 21602 with a CPP_TEMPLATE_ID token. The second `>' (which will 21603 not have been thrown away) is needed either to close an 21604 outer template argument list or to complete a new-style 21605 cast. */ 21606 cp_token *token = cp_lexer_peek_token (parser->lexer); 21607 token->type = CPP_GREATER; 21608 } 21609 else if (!saved_greater_than_is_operator_p) 21610 { 21611 /* If we're in a nested template argument list, the '>>' has 21612 to be a typo for '> >'. We emit the error message, but we 21613 continue parsing and we push a '>' as next token, so that 21614 the argument list will be parsed correctly. Note that the 21615 global source location is still on the token before the 21616 '>>', so we need to say explicitly where we want it. */ 21617 cp_token *token = cp_lexer_peek_token (parser->lexer); 21618 error_at (token->location, "%<>>%> should be %<> >%> " 21619 "within a nested template argument list"); 21620 21621 token->type = CPP_GREATER; 21622 } 21623 else 21624 { 21625 /* If this is not a nested template argument list, the '>>' 21626 is a typo for '>'. Emit an error message and continue. 21627 Same deal about the token location, but here we can get it 21628 right by consuming the '>>' before issuing the diagnostic. */ 21629 cp_token *token = cp_lexer_consume_token (parser->lexer); 21630 error_at (token->location, 21631 "spurious %<>>%>, use %<>%> to terminate " 21632 "a template argument list"); 21633 } 21634 } 21635 else 21636 cp_parser_skip_to_end_of_template_parameter_list (parser); 21637 /* The `>' token might be a greater-than operator again now. */ 21638 parser->greater_than_is_operator_p 21639 = saved_greater_than_is_operator_p; 21640 /* Restore the SAVED_SCOPE. */ 21641 parser->scope = saved_scope; 21642 parser->qualifying_scope = saved_qualifying_scope; 21643 parser->object_scope = saved_object_scope; 21644 cp_unevaluated_operand = saved_unevaluated_operand; 21645 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; 21646 21647 return arguments; 21648 } 21649 21650 /* MEMBER_FUNCTION is a member function, or a friend. If default 21651 arguments, or the body of the function have not yet been parsed, 21652 parse them now. */ 21653 21654 static void 21655 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function) 21656 { 21657 timevar_push (TV_PARSE_INMETH); 21658 /* If this member is a template, get the underlying 21659 FUNCTION_DECL. */ 21660 if (DECL_FUNCTION_TEMPLATE_P (member_function)) 21661 member_function = DECL_TEMPLATE_RESULT (member_function); 21662 21663 /* There should not be any class definitions in progress at this 21664 point; the bodies of members are only parsed outside of all class 21665 definitions. */ 21666 gcc_assert (parser->num_classes_being_defined == 0); 21667 /* While we're parsing the member functions we might encounter more 21668 classes. We want to handle them right away, but we don't want 21669 them getting mixed up with functions that are currently in the 21670 queue. */ 21671 push_unparsed_function_queues (parser); 21672 21673 /* Make sure that any template parameters are in scope. */ 21674 maybe_begin_member_template_processing (member_function); 21675 21676 /* If the body of the function has not yet been parsed, parse it 21677 now. */ 21678 if (DECL_PENDING_INLINE_P (member_function)) 21679 { 21680 tree function_scope; 21681 cp_token_cache *tokens; 21682 21683 /* The function is no longer pending; we are processing it. */ 21684 tokens = DECL_PENDING_INLINE_INFO (member_function); 21685 DECL_PENDING_INLINE_INFO (member_function) = NULL; 21686 DECL_PENDING_INLINE_P (member_function) = 0; 21687 21688 /* If this is a local class, enter the scope of the containing 21689 function. */ 21690 function_scope = current_function_decl; 21691 if (function_scope) 21692 push_function_context (); 21693 21694 /* Push the body of the function onto the lexer stack. */ 21695 cp_parser_push_lexer_for_tokens (parser, tokens); 21696 21697 /* Let the front end know that we going to be defining this 21698 function. */ 21699 start_preparsed_function (member_function, NULL_TREE, 21700 SF_PRE_PARSED | SF_INCLASS_INLINE); 21701 21702 /* Don't do access checking if it is a templated function. */ 21703 if (processing_template_decl) 21704 push_deferring_access_checks (dk_no_check); 21705 21706 /* Now, parse the body of the function. */ 21707 cp_parser_function_definition_after_declarator (parser, 21708 /*inline_p=*/true); 21709 21710 if (processing_template_decl) 21711 pop_deferring_access_checks (); 21712 21713 /* Leave the scope of the containing function. */ 21714 if (function_scope) 21715 pop_function_context (); 21716 cp_parser_pop_lexer (parser); 21717 } 21718 21719 /* Remove any template parameters from the symbol table. */ 21720 maybe_end_member_template_processing (); 21721 21722 /* Restore the queue. */ 21723 pop_unparsed_function_queues (parser); 21724 timevar_pop (TV_PARSE_INMETH); 21725 } 21726 21727 /* If DECL contains any default args, remember it on the unparsed 21728 functions queue. */ 21729 21730 static void 21731 cp_parser_save_default_args (cp_parser* parser, tree decl) 21732 { 21733 tree probe; 21734 21735 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl)); 21736 probe; 21737 probe = TREE_CHAIN (probe)) 21738 if (TREE_PURPOSE (probe)) 21739 { 21740 cp_default_arg_entry *entry 21741 = VEC_safe_push (cp_default_arg_entry, gc, 21742 unparsed_funs_with_default_args, NULL); 21743 entry->class_type = current_class_type; 21744 entry->decl = decl; 21745 break; 21746 } 21747 } 21748 21749 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL, 21750 which is either a FIELD_DECL or PARM_DECL. Parse it and return 21751 the result. For a PARM_DECL, PARMTYPE is the corresponding type 21752 from the parameter-type-list. */ 21753 21754 static tree 21755 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl, 21756 tree default_arg, tree parmtype) 21757 { 21758 cp_token_cache *tokens; 21759 tree parsed_arg; 21760 bool dummy; 21761 21762 if (default_arg == error_mark_node) 21763 return error_mark_node; 21764 21765 /* Push the saved tokens for the default argument onto the parser's 21766 lexer stack. */ 21767 tokens = DEFARG_TOKENS (default_arg); 21768 cp_parser_push_lexer_for_tokens (parser, tokens); 21769 21770 start_lambda_scope (decl); 21771 21772 /* Parse the default argument. */ 21773 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy); 21774 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)) 21775 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 21776 21777 finish_lambda_scope (); 21778 21779 if (!processing_template_decl) 21780 { 21781 /* In a non-template class, check conversions now. In a template, 21782 we'll wait and instantiate these as needed. */ 21783 if (TREE_CODE (decl) == PARM_DECL) 21784 parsed_arg = check_default_argument (parmtype, parsed_arg); 21785 else 21786 { 21787 int flags = LOOKUP_IMPLICIT; 21788 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg) 21789 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg)) 21790 flags = LOOKUP_NORMAL; 21791 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags); 21792 } 21793 } 21794 21795 /* If the token stream has not been completely used up, then 21796 there was extra junk after the end of the default 21797 argument. */ 21798 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF)) 21799 { 21800 if (TREE_CODE (decl) == PARM_DECL) 21801 cp_parser_error (parser, "expected %<,%>"); 21802 else 21803 cp_parser_error (parser, "expected %<;%>"); 21804 } 21805 21806 /* Revert to the main lexer. */ 21807 cp_parser_pop_lexer (parser); 21808 21809 return parsed_arg; 21810 } 21811 21812 /* FIELD is a non-static data member with an initializer which we saved for 21813 later; parse it now. */ 21814 21815 static void 21816 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field) 21817 { 21818 tree def; 21819 21820 push_unparsed_function_queues (parser); 21821 def = cp_parser_late_parse_one_default_arg (parser, field, 21822 DECL_INITIAL (field), 21823 NULL_TREE); 21824 pop_unparsed_function_queues (parser); 21825 21826 DECL_INITIAL (field) = def; 21827 } 21828 21829 /* FN is a FUNCTION_DECL which may contains a parameter with an 21830 unparsed DEFAULT_ARG. Parse the default args now. This function 21831 assumes that the current scope is the scope in which the default 21832 argument should be processed. */ 21833 21834 static void 21835 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn) 21836 { 21837 bool saved_local_variables_forbidden_p; 21838 tree parm, parmdecl; 21839 21840 /* While we're parsing the default args, we might (due to the 21841 statement expression extension) encounter more classes. We want 21842 to handle them right away, but we don't want them getting mixed 21843 up with default args that are currently in the queue. */ 21844 push_unparsed_function_queues (parser); 21845 21846 /* Local variable names (and the `this' keyword) may not appear 21847 in a default argument. */ 21848 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p; 21849 parser->local_variables_forbidden_p = true; 21850 21851 push_defarg_context (fn); 21852 21853 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)), 21854 parmdecl = DECL_ARGUMENTS (fn); 21855 parm && parm != void_list_node; 21856 parm = TREE_CHAIN (parm), 21857 parmdecl = DECL_CHAIN (parmdecl)) 21858 { 21859 tree default_arg = TREE_PURPOSE (parm); 21860 tree parsed_arg; 21861 VEC(tree,gc) *insts; 21862 tree copy; 21863 unsigned ix; 21864 21865 if (!default_arg) 21866 continue; 21867 21868 if (TREE_CODE (default_arg) != DEFAULT_ARG) 21869 /* This can happen for a friend declaration for a function 21870 already declared with default arguments. */ 21871 continue; 21872 21873 parsed_arg 21874 = cp_parser_late_parse_one_default_arg (parser, parmdecl, 21875 default_arg, 21876 TREE_VALUE (parm)); 21877 if (parsed_arg == error_mark_node) 21878 { 21879 continue; 21880 } 21881 21882 TREE_PURPOSE (parm) = parsed_arg; 21883 21884 /* Update any instantiations we've already created. */ 21885 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0; 21886 VEC_iterate (tree, insts, ix, copy); ix++) 21887 TREE_PURPOSE (copy) = parsed_arg; 21888 } 21889 21890 pop_defarg_context (); 21891 21892 /* Make sure no default arg is missing. */ 21893 check_default_args (fn); 21894 21895 /* Restore the state of local_variables_forbidden_p. */ 21896 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p; 21897 21898 /* Restore the queue. */ 21899 pop_unparsed_function_queues (parser); 21900 } 21901 21902 /* Parse the operand of `sizeof' (or a similar operator). Returns 21903 either a TYPE or an expression, depending on the form of the 21904 input. The KEYWORD indicates which kind of expression we have 21905 encountered. */ 21906 21907 static tree 21908 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword) 21909 { 21910 tree expr = NULL_TREE; 21911 const char *saved_message; 21912 char *tmp; 21913 bool saved_integral_constant_expression_p; 21914 bool saved_non_integral_constant_expression_p; 21915 bool pack_expansion_p = false; 21916 21917 /* Types cannot be defined in a `sizeof' expression. Save away the 21918 old message. */ 21919 saved_message = parser->type_definition_forbidden_message; 21920 /* And create the new one. */ 21921 tmp = concat ("types may not be defined in %<", 21922 IDENTIFIER_POINTER (ridpointers[keyword]), 21923 "%> expressions", NULL); 21924 parser->type_definition_forbidden_message = tmp; 21925 21926 /* The restrictions on constant-expressions do not apply inside 21927 sizeof expressions. */ 21928 saved_integral_constant_expression_p 21929 = parser->integral_constant_expression_p; 21930 saved_non_integral_constant_expression_p 21931 = parser->non_integral_constant_expression_p; 21932 parser->integral_constant_expression_p = false; 21933 21934 /* If it's a `...', then we are computing the length of a parameter 21935 pack. */ 21936 if (keyword == RID_SIZEOF 21937 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 21938 { 21939 /* Consume the `...'. */ 21940 cp_lexer_consume_token (parser->lexer); 21941 maybe_warn_variadic_templates (); 21942 21943 /* Note that this is an expansion. */ 21944 pack_expansion_p = true; 21945 } 21946 21947 /* Do not actually evaluate the expression. */ 21948 ++cp_unevaluated_operand; 21949 ++c_inhibit_evaluation_warnings; 21950 /* If it's a `(', then we might be looking at the type-id 21951 construction. */ 21952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 21953 { 21954 tree type; 21955 bool saved_in_type_id_in_expr_p; 21956 21957 /* We can't be sure yet whether we're looking at a type-id or an 21958 expression. */ 21959 cp_parser_parse_tentatively (parser); 21960 /* Consume the `('. */ 21961 cp_lexer_consume_token (parser->lexer); 21962 /* Parse the type-id. */ 21963 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p; 21964 parser->in_type_id_in_expr_p = true; 21965 type = cp_parser_type_id (parser); 21966 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p; 21967 /* Now, look for the trailing `)'. */ 21968 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 21969 /* If all went well, then we're done. */ 21970 if (cp_parser_parse_definitely (parser)) 21971 { 21972 cp_decl_specifier_seq decl_specs; 21973 21974 /* Build a trivial decl-specifier-seq. */ 21975 clear_decl_specs (&decl_specs); 21976 decl_specs.type = type; 21977 21978 /* Call grokdeclarator to figure out what type this is. */ 21979 expr = grokdeclarator (NULL, 21980 &decl_specs, 21981 TYPENAME, 21982 /*initialized=*/0, 21983 /*attrlist=*/NULL); 21984 } 21985 } 21986 21987 /* If the type-id production did not work out, then we must be 21988 looking at the unary-expression production. */ 21989 if (!expr) 21990 expr = cp_parser_unary_expression (parser, /*address_p=*/false, 21991 /*cast_p=*/false, NULL); 21992 21993 if (pack_expansion_p) 21994 /* Build a pack expansion. */ 21995 expr = make_pack_expansion (expr); 21996 21997 /* Go back to evaluating expressions. */ 21998 --cp_unevaluated_operand; 21999 --c_inhibit_evaluation_warnings; 22000 22001 /* Free the message we created. */ 22002 free (tmp); 22003 /* And restore the old one. */ 22004 parser->type_definition_forbidden_message = saved_message; 22005 parser->integral_constant_expression_p 22006 = saved_integral_constant_expression_p; 22007 parser->non_integral_constant_expression_p 22008 = saved_non_integral_constant_expression_p; 22009 22010 return expr; 22011 } 22012 22013 /* If the current declaration has no declarator, return true. */ 22014 22015 static bool 22016 cp_parser_declares_only_class_p (cp_parser *parser) 22017 { 22018 /* If the next token is a `;' or a `,' then there is no 22019 declarator. */ 22020 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 22021 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA)); 22022 } 22023 22024 /* Update the DECL_SPECS to reflect the storage class indicated by 22025 KEYWORD. */ 22026 22027 static void 22028 cp_parser_set_storage_class (cp_parser *parser, 22029 cp_decl_specifier_seq *decl_specs, 22030 enum rid keyword, 22031 location_t location) 22032 { 22033 cp_storage_class storage_class; 22034 22035 if (parser->in_unbraced_linkage_specification_p) 22036 { 22037 error_at (location, "invalid use of %qD in linkage specification", 22038 ridpointers[keyword]); 22039 return; 22040 } 22041 else if (decl_specs->storage_class != sc_none) 22042 { 22043 decl_specs->conflicting_specifiers_p = true; 22044 return; 22045 } 22046 22047 if ((keyword == RID_EXTERN || keyword == RID_STATIC) 22048 && decl_specs->specs[(int) ds_thread]) 22049 { 22050 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]); 22051 decl_specs->specs[(int) ds_thread] = 0; 22052 } 22053 22054 switch (keyword) 22055 { 22056 case RID_AUTO: 22057 storage_class = sc_auto; 22058 break; 22059 case RID_REGISTER: 22060 storage_class = sc_register; 22061 break; 22062 case RID_STATIC: 22063 storage_class = sc_static; 22064 break; 22065 case RID_EXTERN: 22066 storage_class = sc_extern; 22067 break; 22068 case RID_MUTABLE: 22069 storage_class = sc_mutable; 22070 break; 22071 default: 22072 gcc_unreachable (); 22073 } 22074 decl_specs->storage_class = storage_class; 22075 22076 /* A storage class specifier cannot be applied alongside a typedef 22077 specifier. If there is a typedef specifier present then set 22078 conflicting_specifiers_p which will trigger an error later 22079 on in grokdeclarator. */ 22080 if (decl_specs->specs[(int)ds_typedef]) 22081 decl_specs->conflicting_specifiers_p = true; 22082 } 22083 22084 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P 22085 is true, the type is a class or enum definition. */ 22086 22087 static void 22088 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs, 22089 tree type_spec, 22090 location_t location, 22091 bool type_definition_p) 22092 { 22093 decl_specs->any_specifiers_p = true; 22094 22095 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t 22096 (with, for example, in "typedef int wchar_t;") we remember that 22097 this is what happened. In system headers, we ignore these 22098 declarations so that G++ can work with system headers that are not 22099 C++-safe. */ 22100 if (decl_specs->specs[(int) ds_typedef] 22101 && !type_definition_p 22102 && (type_spec == boolean_type_node 22103 || type_spec == char16_type_node 22104 || type_spec == char32_type_node 22105 || type_spec == wchar_type_node) 22106 && (decl_specs->type 22107 || decl_specs->specs[(int) ds_long] 22108 || decl_specs->specs[(int) ds_short] 22109 || decl_specs->specs[(int) ds_unsigned] 22110 || decl_specs->specs[(int) ds_signed])) 22111 { 22112 decl_specs->redefined_builtin_type = type_spec; 22113 if (!decl_specs->type) 22114 { 22115 decl_specs->type = type_spec; 22116 decl_specs->type_definition_p = false; 22117 decl_specs->type_location = location; 22118 } 22119 } 22120 else if (decl_specs->type) 22121 decl_specs->multiple_types_p = true; 22122 else 22123 { 22124 decl_specs->type = type_spec; 22125 decl_specs->type_definition_p = type_definition_p; 22126 decl_specs->redefined_builtin_type = NULL_TREE; 22127 decl_specs->type_location = location; 22128 } 22129 } 22130 22131 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq. 22132 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */ 22133 22134 static bool 22135 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers) 22136 { 22137 return decl_specifiers->specs[(int) ds_friend] != 0; 22138 } 22139 22140 /* Issue an error message indicating that TOKEN_DESC was expected. 22141 If KEYWORD is true, it indicated this function is called by 22142 cp_parser_require_keword and the required token can only be 22143 a indicated keyword. */ 22144 22145 static void 22146 cp_parser_required_error (cp_parser *parser, 22147 required_token token_desc, 22148 bool keyword) 22149 { 22150 switch (token_desc) 22151 { 22152 case RT_NEW: 22153 cp_parser_error (parser, "expected %<new%>"); 22154 return; 22155 case RT_DELETE: 22156 cp_parser_error (parser, "expected %<delete%>"); 22157 return; 22158 case RT_RETURN: 22159 cp_parser_error (parser, "expected %<return%>"); 22160 return; 22161 case RT_WHILE: 22162 cp_parser_error (parser, "expected %<while%>"); 22163 return; 22164 case RT_EXTERN: 22165 cp_parser_error (parser, "expected %<extern%>"); 22166 return; 22167 case RT_STATIC_ASSERT: 22168 cp_parser_error (parser, "expected %<static_assert%>"); 22169 return; 22170 case RT_DECLTYPE: 22171 cp_parser_error (parser, "expected %<decltype%>"); 22172 return; 22173 case RT_OPERATOR: 22174 cp_parser_error (parser, "expected %<operator%>"); 22175 return; 22176 case RT_CLASS: 22177 cp_parser_error (parser, "expected %<class%>"); 22178 return; 22179 case RT_TEMPLATE: 22180 cp_parser_error (parser, "expected %<template%>"); 22181 return; 22182 case RT_NAMESPACE: 22183 cp_parser_error (parser, "expected %<namespace%>"); 22184 return; 22185 case RT_USING: 22186 cp_parser_error (parser, "expected %<using%>"); 22187 return; 22188 case RT_ASM: 22189 cp_parser_error (parser, "expected %<asm%>"); 22190 return; 22191 case RT_TRY: 22192 cp_parser_error (parser, "expected %<try%>"); 22193 return; 22194 case RT_CATCH: 22195 cp_parser_error (parser, "expected %<catch%>"); 22196 return; 22197 case RT_THROW: 22198 cp_parser_error (parser, "expected %<throw%>"); 22199 return; 22200 case RT_LABEL: 22201 cp_parser_error (parser, "expected %<__label__%>"); 22202 return; 22203 case RT_AT_TRY: 22204 cp_parser_error (parser, "expected %<@try%>"); 22205 return; 22206 case RT_AT_SYNCHRONIZED: 22207 cp_parser_error (parser, "expected %<@synchronized%>"); 22208 return; 22209 case RT_AT_THROW: 22210 cp_parser_error (parser, "expected %<@throw%>"); 22211 return; 22212 case RT_TRANSACTION_ATOMIC: 22213 cp_parser_error (parser, "expected %<__transaction_atomic%>"); 22214 return; 22215 case RT_TRANSACTION_RELAXED: 22216 cp_parser_error (parser, "expected %<__transaction_relaxed%>"); 22217 return; 22218 default: 22219 break; 22220 } 22221 if (!keyword) 22222 { 22223 switch (token_desc) 22224 { 22225 case RT_SEMICOLON: 22226 cp_parser_error (parser, "expected %<;%>"); 22227 return; 22228 case RT_OPEN_PAREN: 22229 cp_parser_error (parser, "expected %<(%>"); 22230 return; 22231 case RT_CLOSE_BRACE: 22232 cp_parser_error (parser, "expected %<}%>"); 22233 return; 22234 case RT_OPEN_BRACE: 22235 cp_parser_error (parser, "expected %<{%>"); 22236 return; 22237 case RT_CLOSE_SQUARE: 22238 cp_parser_error (parser, "expected %<]%>"); 22239 return; 22240 case RT_OPEN_SQUARE: 22241 cp_parser_error (parser, "expected %<[%>"); 22242 return; 22243 case RT_COMMA: 22244 cp_parser_error (parser, "expected %<,%>"); 22245 return; 22246 case RT_SCOPE: 22247 cp_parser_error (parser, "expected %<::%>"); 22248 return; 22249 case RT_LESS: 22250 cp_parser_error (parser, "expected %<<%>"); 22251 return; 22252 case RT_GREATER: 22253 cp_parser_error (parser, "expected %<>%>"); 22254 return; 22255 case RT_EQ: 22256 cp_parser_error (parser, "expected %<=%>"); 22257 return; 22258 case RT_ELLIPSIS: 22259 cp_parser_error (parser, "expected %<...%>"); 22260 return; 22261 case RT_MULT: 22262 cp_parser_error (parser, "expected %<*%>"); 22263 return; 22264 case RT_COMPL: 22265 cp_parser_error (parser, "expected %<~%>"); 22266 return; 22267 case RT_COLON: 22268 cp_parser_error (parser, "expected %<:%>"); 22269 return; 22270 case RT_COLON_SCOPE: 22271 cp_parser_error (parser, "expected %<:%> or %<::%>"); 22272 return; 22273 case RT_CLOSE_PAREN: 22274 cp_parser_error (parser, "expected %<)%>"); 22275 return; 22276 case RT_COMMA_CLOSE_PAREN: 22277 cp_parser_error (parser, "expected %<,%> or %<)%>"); 22278 return; 22279 case RT_PRAGMA_EOL: 22280 cp_parser_error (parser, "expected end of line"); 22281 return; 22282 case RT_NAME: 22283 cp_parser_error (parser, "expected identifier"); 22284 return; 22285 case RT_SELECT: 22286 cp_parser_error (parser, "expected selection-statement"); 22287 return; 22288 case RT_INTERATION: 22289 cp_parser_error (parser, "expected iteration-statement"); 22290 return; 22291 case RT_JUMP: 22292 cp_parser_error (parser, "expected jump-statement"); 22293 return; 22294 case RT_CLASS_KEY: 22295 cp_parser_error (parser, "expected class-key"); 22296 return; 22297 case RT_CLASS_TYPENAME_TEMPLATE: 22298 cp_parser_error (parser, 22299 "expected %<class%>, %<typename%>, or %<template%>"); 22300 return; 22301 default: 22302 gcc_unreachable (); 22303 } 22304 } 22305 else 22306 gcc_unreachable (); 22307 } 22308 22309 22310 22311 /* If the next token is of the indicated TYPE, consume it. Otherwise, 22312 issue an error message indicating that TOKEN_DESC was expected. 22313 22314 Returns the token consumed, if the token had the appropriate type. 22315 Otherwise, returns NULL. */ 22316 22317 static cp_token * 22318 cp_parser_require (cp_parser* parser, 22319 enum cpp_ttype type, 22320 required_token token_desc) 22321 { 22322 if (cp_lexer_next_token_is (parser->lexer, type)) 22323 return cp_lexer_consume_token (parser->lexer); 22324 else 22325 { 22326 /* Output the MESSAGE -- unless we're parsing tentatively. */ 22327 if (!cp_parser_simulate_error (parser)) 22328 cp_parser_required_error (parser, token_desc, /*keyword=*/false); 22329 return NULL; 22330 } 22331 } 22332 22333 /* An error message is produced if the next token is not '>'. 22334 All further tokens are skipped until the desired token is 22335 found or '{', '}', ';' or an unbalanced ')' or ']'. */ 22336 22337 static void 22338 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser) 22339 { 22340 /* Current level of '< ... >'. */ 22341 unsigned level = 0; 22342 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */ 22343 unsigned nesting_depth = 0; 22344 22345 /* Are we ready, yet? If not, issue error message. */ 22346 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER)) 22347 return; 22348 22349 /* Skip tokens until the desired token is found. */ 22350 while (true) 22351 { 22352 /* Peek at the next token. */ 22353 switch (cp_lexer_peek_token (parser->lexer)->type) 22354 { 22355 case CPP_LESS: 22356 if (!nesting_depth) 22357 ++level; 22358 break; 22359 22360 case CPP_RSHIFT: 22361 if (cxx_dialect == cxx98) 22362 /* C++0x views the `>>' operator as two `>' tokens, but 22363 C++98 does not. */ 22364 break; 22365 else if (!nesting_depth && level-- == 0) 22366 { 22367 /* We've hit a `>>' where the first `>' closes the 22368 template argument list, and the second `>' is 22369 spurious. Just consume the `>>' and stop; we've 22370 already produced at least one error. */ 22371 cp_lexer_consume_token (parser->lexer); 22372 return; 22373 } 22374 /* Fall through for C++0x, so we handle the second `>' in 22375 the `>>'. */ 22376 22377 case CPP_GREATER: 22378 if (!nesting_depth && level-- == 0) 22379 { 22380 /* We've reached the token we want, consume it and stop. */ 22381 cp_lexer_consume_token (parser->lexer); 22382 return; 22383 } 22384 break; 22385 22386 case CPP_OPEN_PAREN: 22387 case CPP_OPEN_SQUARE: 22388 ++nesting_depth; 22389 break; 22390 22391 case CPP_CLOSE_PAREN: 22392 case CPP_CLOSE_SQUARE: 22393 if (nesting_depth-- == 0) 22394 return; 22395 break; 22396 22397 case CPP_EOF: 22398 case CPP_PRAGMA_EOL: 22399 case CPP_SEMICOLON: 22400 case CPP_OPEN_BRACE: 22401 case CPP_CLOSE_BRACE: 22402 /* The '>' was probably forgotten, don't look further. */ 22403 return; 22404 22405 default: 22406 break; 22407 } 22408 22409 /* Consume this token. */ 22410 cp_lexer_consume_token (parser->lexer); 22411 } 22412 } 22413 22414 /* If the next token is the indicated keyword, consume it. Otherwise, 22415 issue an error message indicating that TOKEN_DESC was expected. 22416 22417 Returns the token consumed, if the token had the appropriate type. 22418 Otherwise, returns NULL. */ 22419 22420 static cp_token * 22421 cp_parser_require_keyword (cp_parser* parser, 22422 enum rid keyword, 22423 required_token token_desc) 22424 { 22425 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc); 22426 22427 if (token && token->keyword != keyword) 22428 { 22429 cp_parser_required_error (parser, token_desc, /*keyword=*/true); 22430 return NULL; 22431 } 22432 22433 return token; 22434 } 22435 22436 /* Returns TRUE iff TOKEN is a token that can begin the body of a 22437 function-definition. */ 22438 22439 static bool 22440 cp_parser_token_starts_function_definition_p (cp_token* token) 22441 { 22442 return (/* An ordinary function-body begins with an `{'. */ 22443 token->type == CPP_OPEN_BRACE 22444 /* A ctor-initializer begins with a `:'. */ 22445 || token->type == CPP_COLON 22446 /* A function-try-block begins with `try'. */ 22447 || token->keyword == RID_TRY 22448 /* A function-transaction-block begins with `__transaction_atomic' 22449 or `__transaction_relaxed'. */ 22450 || token->keyword == RID_TRANSACTION_ATOMIC 22451 || token->keyword == RID_TRANSACTION_RELAXED 22452 /* The named return value extension begins with `return'. */ 22453 || token->keyword == RID_RETURN); 22454 } 22455 22456 /* Returns TRUE iff the next token is the ":" or "{" beginning a class 22457 definition. */ 22458 22459 static bool 22460 cp_parser_next_token_starts_class_definition_p (cp_parser *parser) 22461 { 22462 cp_token *token; 22463 22464 token = cp_lexer_peek_token (parser->lexer); 22465 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON); 22466 } 22467 22468 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in 22469 C++0x) ending a template-argument. */ 22470 22471 static bool 22472 cp_parser_next_token_ends_template_argument_p (cp_parser *parser) 22473 { 22474 cp_token *token; 22475 22476 token = cp_lexer_peek_token (parser->lexer); 22477 return (token->type == CPP_COMMA 22478 || token->type == CPP_GREATER 22479 || token->type == CPP_ELLIPSIS 22480 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)); 22481 } 22482 22483 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the 22484 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */ 22485 22486 static bool 22487 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser, 22488 size_t n) 22489 { 22490 cp_token *token; 22491 22492 token = cp_lexer_peek_nth_token (parser->lexer, n); 22493 if (token->type == CPP_LESS) 22494 return true; 22495 /* Check for the sequence `<::' in the original code. It would be lexed as 22496 `[:', where `[' is a digraph, and there is no whitespace before 22497 `:'. */ 22498 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH) 22499 { 22500 cp_token *token2; 22501 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1); 22502 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE)) 22503 return true; 22504 } 22505 return false; 22506 } 22507 22508 /* Returns the kind of tag indicated by TOKEN, if it is a class-key, 22509 or none_type otherwise. */ 22510 22511 static enum tag_types 22512 cp_parser_token_is_class_key (cp_token* token) 22513 { 22514 switch (token->keyword) 22515 { 22516 case RID_CLASS: 22517 return class_type; 22518 case RID_STRUCT: 22519 return record_type; 22520 case RID_UNION: 22521 return union_type; 22522 22523 default: 22524 return none_type; 22525 } 22526 } 22527 22528 /* Issue an error message if the CLASS_KEY does not match the TYPE. */ 22529 22530 static void 22531 cp_parser_check_class_key (enum tag_types class_key, tree type) 22532 { 22533 if (type == error_mark_node) 22534 return; 22535 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type)) 22536 { 22537 permerror (input_location, "%qs tag used in naming %q#T", 22538 class_key == union_type ? "union" 22539 : class_key == record_type ? "struct" : "class", 22540 type); 22541 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)), 22542 "%q#T was previously declared here", type); 22543 } 22544 } 22545 22546 /* Issue an error message if DECL is redeclared with different 22547 access than its original declaration [class.access.spec/3]. 22548 This applies to nested classes and nested class templates. 22549 [class.mem/1]. */ 22550 22551 static void 22552 cp_parser_check_access_in_redeclaration (tree decl, location_t location) 22553 { 22554 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl))) 22555 return; 22556 22557 if ((TREE_PRIVATE (decl) 22558 != (current_access_specifier == access_private_node)) 22559 || (TREE_PROTECTED (decl) 22560 != (current_access_specifier == access_protected_node))) 22561 error_at (location, "%qD redeclared with different access", decl); 22562 } 22563 22564 /* Look for the `template' keyword, as a syntactic disambiguator. 22565 Return TRUE iff it is present, in which case it will be 22566 consumed. */ 22567 22568 static bool 22569 cp_parser_optional_template_keyword (cp_parser *parser) 22570 { 22571 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE)) 22572 { 22573 /* The `template' keyword can only be used within templates; 22574 outside templates the parser can always figure out what is a 22575 template and what is not. */ 22576 if (!processing_template_decl) 22577 { 22578 cp_token *token = cp_lexer_peek_token (parser->lexer); 22579 error_at (token->location, 22580 "%<template%> (as a disambiguator) is only allowed " 22581 "within templates"); 22582 /* If this part of the token stream is rescanned, the same 22583 error message would be generated. So, we purge the token 22584 from the stream. */ 22585 cp_lexer_purge_token (parser->lexer); 22586 return false; 22587 } 22588 else 22589 { 22590 /* Consume the `template' keyword. */ 22591 cp_lexer_consume_token (parser->lexer); 22592 return true; 22593 } 22594 } 22595 22596 return false; 22597 } 22598 22599 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token, 22600 set PARSER->SCOPE, and perform other related actions. */ 22601 22602 static void 22603 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser) 22604 { 22605 int i; 22606 struct tree_check *check_value; 22607 deferred_access_check *chk; 22608 VEC (deferred_access_check,gc) *checks; 22609 22610 /* Get the stored value. */ 22611 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value; 22612 /* Perform any access checks that were deferred. */ 22613 checks = check_value->checks; 22614 if (checks) 22615 { 22616 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk) 22617 perform_or_defer_access_check (chk->binfo, 22618 chk->decl, 22619 chk->diag_decl); 22620 } 22621 /* Set the scope from the stored value. */ 22622 parser->scope = check_value->value; 22623 parser->qualifying_scope = check_value->qualifying_scope; 22624 parser->object_scope = NULL_TREE; 22625 } 22626 22627 /* Consume tokens up through a non-nested END token. Returns TRUE if we 22628 encounter the end of a block before what we were looking for. */ 22629 22630 static bool 22631 cp_parser_cache_group (cp_parser *parser, 22632 enum cpp_ttype end, 22633 unsigned depth) 22634 { 22635 while (true) 22636 { 22637 cp_token *token = cp_lexer_peek_token (parser->lexer); 22638 22639 /* Abort a parenthesized expression if we encounter a semicolon. */ 22640 if ((end == CPP_CLOSE_PAREN || depth == 0) 22641 && token->type == CPP_SEMICOLON) 22642 return true; 22643 /* If we've reached the end of the file, stop. */ 22644 if (token->type == CPP_EOF 22645 || (end != CPP_PRAGMA_EOL 22646 && token->type == CPP_PRAGMA_EOL)) 22647 return true; 22648 if (token->type == CPP_CLOSE_BRACE && depth == 0) 22649 /* We've hit the end of an enclosing block, so there's been some 22650 kind of syntax error. */ 22651 return true; 22652 22653 /* Consume the token. */ 22654 cp_lexer_consume_token (parser->lexer); 22655 /* See if it starts a new group. */ 22656 if (token->type == CPP_OPEN_BRACE) 22657 { 22658 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1); 22659 /* In theory this should probably check end == '}', but 22660 cp_parser_save_member_function_body needs it to exit 22661 after either '}' or ')' when called with ')'. */ 22662 if (depth == 0) 22663 return false; 22664 } 22665 else if (token->type == CPP_OPEN_PAREN) 22666 { 22667 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1); 22668 if (depth == 0 && end == CPP_CLOSE_PAREN) 22669 return false; 22670 } 22671 else if (token->type == CPP_PRAGMA) 22672 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1); 22673 else if (token->type == end) 22674 return false; 22675 } 22676 } 22677 22678 /* Like above, for caching a default argument or NSDMI. Both of these are 22679 terminated by a non-nested comma, but it can be unclear whether or not a 22680 comma is nested in a template argument list unless we do more parsing. 22681 In order to handle this ambiguity, when we encounter a ',' after a '<' 22682 we try to parse what follows as a parameter-declaration-list (in the 22683 case of a default argument) or a member-declarator (in the case of an 22684 NSDMI). If that succeeds, then we stop caching. */ 22685 22686 static tree 22687 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi) 22688 { 22689 unsigned depth = 0; 22690 int maybe_template_id = 0; 22691 cp_token *first_token; 22692 cp_token *token; 22693 tree default_argument; 22694 22695 /* Add tokens until we have processed the entire default 22696 argument. We add the range [first_token, token). */ 22697 first_token = cp_lexer_peek_token (parser->lexer); 22698 if (first_token->type == CPP_OPEN_BRACE) 22699 { 22700 /* For list-initialization, this is straightforward. */ 22701 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0); 22702 token = cp_lexer_peek_token (parser->lexer); 22703 } 22704 else while (true) 22705 { 22706 bool done = false; 22707 22708 /* Peek at the next token. */ 22709 token = cp_lexer_peek_token (parser->lexer); 22710 /* What we do depends on what token we have. */ 22711 switch (token->type) 22712 { 22713 /* In valid code, a default argument must be 22714 immediately followed by a `,' `)', or `...'. */ 22715 case CPP_COMMA: 22716 if (depth == 0 && maybe_template_id) 22717 { 22718 /* If we've seen a '<', we might be in a 22719 template-argument-list. Until Core issue 325 is 22720 resolved, we don't know how this situation ought 22721 to be handled, so try to DTRT. We check whether 22722 what comes after the comma is a valid parameter 22723 declaration list. If it is, then the comma ends 22724 the default argument; otherwise the default 22725 argument continues. */ 22726 bool error = false; 22727 tree t; 22728 22729 /* Set ITALP so cp_parser_parameter_declaration_list 22730 doesn't decide to commit to this parse. */ 22731 bool saved_italp = parser->in_template_argument_list_p; 22732 parser->in_template_argument_list_p = true; 22733 22734 cp_parser_parse_tentatively (parser); 22735 cp_lexer_consume_token (parser->lexer); 22736 22737 if (nsdmi) 22738 { 22739 int ctor_dtor_or_conv_p; 22740 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 22741 &ctor_dtor_or_conv_p, 22742 /*parenthesized_p=*/NULL, 22743 /*member_p=*/true); 22744 } 22745 else 22746 { 22747 begin_scope (sk_function_parms, NULL_TREE); 22748 cp_parser_parameter_declaration_list (parser, &error); 22749 for (t = current_binding_level->names; t; t = DECL_CHAIN (t)) 22750 pop_binding (DECL_NAME (t), t); 22751 leave_scope (); 22752 } 22753 if (!cp_parser_error_occurred (parser) && !error) 22754 done = true; 22755 cp_parser_abort_tentative_parse (parser); 22756 22757 parser->in_template_argument_list_p = saved_italp; 22758 break; 22759 } 22760 case CPP_CLOSE_PAREN: 22761 case CPP_ELLIPSIS: 22762 /* If we run into a non-nested `;', `}', or `]', 22763 then the code is invalid -- but the default 22764 argument is certainly over. */ 22765 case CPP_SEMICOLON: 22766 case CPP_CLOSE_BRACE: 22767 case CPP_CLOSE_SQUARE: 22768 if (depth == 0) 22769 done = true; 22770 /* Update DEPTH, if necessary. */ 22771 else if (token->type == CPP_CLOSE_PAREN 22772 || token->type == CPP_CLOSE_BRACE 22773 || token->type == CPP_CLOSE_SQUARE) 22774 --depth; 22775 break; 22776 22777 case CPP_OPEN_PAREN: 22778 case CPP_OPEN_SQUARE: 22779 case CPP_OPEN_BRACE: 22780 ++depth; 22781 break; 22782 22783 case CPP_LESS: 22784 if (depth == 0) 22785 /* This might be the comparison operator, or it might 22786 start a template argument list. */ 22787 ++maybe_template_id; 22788 break; 22789 22790 case CPP_RSHIFT: 22791 if (cxx_dialect == cxx98) 22792 break; 22793 /* Fall through for C++0x, which treats the `>>' 22794 operator like two `>' tokens in certain 22795 cases. */ 22796 22797 case CPP_GREATER: 22798 if (depth == 0) 22799 { 22800 /* This might be an operator, or it might close a 22801 template argument list. But if a previous '<' 22802 started a template argument list, this will have 22803 closed it, so we can't be in one anymore. */ 22804 maybe_template_id -= 1 + (token->type == CPP_RSHIFT); 22805 if (maybe_template_id < 0) 22806 maybe_template_id = 0; 22807 } 22808 break; 22809 22810 /* If we run out of tokens, issue an error message. */ 22811 case CPP_EOF: 22812 case CPP_PRAGMA_EOL: 22813 error_at (token->location, "file ends in default argument"); 22814 done = true; 22815 break; 22816 22817 case CPP_NAME: 22818 case CPP_SCOPE: 22819 /* In these cases, we should look for template-ids. 22820 For example, if the default argument is 22821 `X<int, double>()', we need to do name lookup to 22822 figure out whether or not `X' is a template; if 22823 so, the `,' does not end the default argument. 22824 22825 That is not yet done. */ 22826 break; 22827 22828 default: 22829 break; 22830 } 22831 22832 /* If we've reached the end, stop. */ 22833 if (done) 22834 break; 22835 22836 /* Add the token to the token block. */ 22837 token = cp_lexer_consume_token (parser->lexer); 22838 } 22839 22840 /* Create a DEFAULT_ARG to represent the unparsed default 22841 argument. */ 22842 default_argument = make_node (DEFAULT_ARG); 22843 DEFARG_TOKENS (default_argument) 22844 = cp_token_cache_new (first_token, token); 22845 DEFARG_INSTANTIATIONS (default_argument) = NULL; 22846 22847 return default_argument; 22848 } 22849 22850 /* Begin parsing tentatively. We always save tokens while parsing 22851 tentatively so that if the tentative parsing fails we can restore the 22852 tokens. */ 22853 22854 static void 22855 cp_parser_parse_tentatively (cp_parser* parser) 22856 { 22857 /* Enter a new parsing context. */ 22858 parser->context = cp_parser_context_new (parser->context); 22859 /* Begin saving tokens. */ 22860 cp_lexer_save_tokens (parser->lexer); 22861 /* In order to avoid repetitive access control error messages, 22862 access checks are queued up until we are no longer parsing 22863 tentatively. */ 22864 push_deferring_access_checks (dk_deferred); 22865 } 22866 22867 /* Commit to the currently active tentative parse. */ 22868 22869 static void 22870 cp_parser_commit_to_tentative_parse (cp_parser* parser) 22871 { 22872 cp_parser_context *context; 22873 cp_lexer *lexer; 22874 22875 /* Mark all of the levels as committed. */ 22876 lexer = parser->lexer; 22877 for (context = parser->context; context->next; context = context->next) 22878 { 22879 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED) 22880 break; 22881 context->status = CP_PARSER_STATUS_KIND_COMMITTED; 22882 while (!cp_lexer_saving_tokens (lexer)) 22883 lexer = lexer->next; 22884 cp_lexer_commit_tokens (lexer); 22885 } 22886 } 22887 22888 /* Abort the currently active tentative parse. All consumed tokens 22889 will be rolled back, and no diagnostics will be issued. */ 22890 22891 static void 22892 cp_parser_abort_tentative_parse (cp_parser* parser) 22893 { 22894 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED 22895 || errorcount > 0); 22896 cp_parser_simulate_error (parser); 22897 /* Now, pretend that we want to see if the construct was 22898 successfully parsed. */ 22899 cp_parser_parse_definitely (parser); 22900 } 22901 22902 /* Stop parsing tentatively. If a parse error has occurred, restore the 22903 token stream. Otherwise, commit to the tokens we have consumed. 22904 Returns true if no error occurred; false otherwise. */ 22905 22906 static bool 22907 cp_parser_parse_definitely (cp_parser* parser) 22908 { 22909 bool error_occurred; 22910 cp_parser_context *context; 22911 22912 /* Remember whether or not an error occurred, since we are about to 22913 destroy that information. */ 22914 error_occurred = cp_parser_error_occurred (parser); 22915 /* Remove the topmost context from the stack. */ 22916 context = parser->context; 22917 parser->context = context->next; 22918 /* If no parse errors occurred, commit to the tentative parse. */ 22919 if (!error_occurred) 22920 { 22921 /* Commit to the tokens read tentatively, unless that was 22922 already done. */ 22923 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED) 22924 cp_lexer_commit_tokens (parser->lexer); 22925 22926 pop_to_parent_deferring_access_checks (); 22927 } 22928 /* Otherwise, if errors occurred, roll back our state so that things 22929 are just as they were before we began the tentative parse. */ 22930 else 22931 { 22932 cp_lexer_rollback_tokens (parser->lexer); 22933 pop_deferring_access_checks (); 22934 } 22935 /* Add the context to the front of the free list. */ 22936 context->next = cp_parser_context_free_list; 22937 cp_parser_context_free_list = context; 22938 22939 return !error_occurred; 22940 } 22941 22942 /* Returns true if we are parsing tentatively and are not committed to 22943 this tentative parse. */ 22944 22945 static bool 22946 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser) 22947 { 22948 return (cp_parser_parsing_tentatively (parser) 22949 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED); 22950 } 22951 22952 /* Returns nonzero iff an error has occurred during the most recent 22953 tentative parse. */ 22954 22955 static bool 22956 cp_parser_error_occurred (cp_parser* parser) 22957 { 22958 return (cp_parser_parsing_tentatively (parser) 22959 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR); 22960 } 22961 22962 /* Returns nonzero if GNU extensions are allowed. */ 22963 22964 static bool 22965 cp_parser_allow_gnu_extensions_p (cp_parser* parser) 22966 { 22967 return parser->allow_gnu_extensions_p; 22968 } 22969 22970 /* Objective-C++ Productions */ 22971 22972 22973 /* Parse an Objective-C expression, which feeds into a primary-expression 22974 above. 22975 22976 objc-expression: 22977 objc-message-expression 22978 objc-string-literal 22979 objc-encode-expression 22980 objc-protocol-expression 22981 objc-selector-expression 22982 22983 Returns a tree representation of the expression. */ 22984 22985 static tree 22986 cp_parser_objc_expression (cp_parser* parser) 22987 { 22988 /* Try to figure out what kind of declaration is present. */ 22989 cp_token *kwd = cp_lexer_peek_token (parser->lexer); 22990 22991 switch (kwd->type) 22992 { 22993 case CPP_OPEN_SQUARE: 22994 return cp_parser_objc_message_expression (parser); 22995 22996 case CPP_OBJC_STRING: 22997 kwd = cp_lexer_consume_token (parser->lexer); 22998 return objc_build_string_object (kwd->u.value); 22999 23000 case CPP_KEYWORD: 23001 switch (kwd->keyword) 23002 { 23003 case RID_AT_ENCODE: 23004 return cp_parser_objc_encode_expression (parser); 23005 23006 case RID_AT_PROTOCOL: 23007 return cp_parser_objc_protocol_expression (parser); 23008 23009 case RID_AT_SELECTOR: 23010 return cp_parser_objc_selector_expression (parser); 23011 23012 default: 23013 break; 23014 } 23015 default: 23016 error_at (kwd->location, 23017 "misplaced %<@%D%> Objective-C++ construct", 23018 kwd->u.value); 23019 cp_parser_skip_to_end_of_block_or_statement (parser); 23020 } 23021 23022 return error_mark_node; 23023 } 23024 23025 /* Parse an Objective-C message expression. 23026 23027 objc-message-expression: 23028 [ objc-message-receiver objc-message-args ] 23029 23030 Returns a representation of an Objective-C message. */ 23031 23032 static tree 23033 cp_parser_objc_message_expression (cp_parser* parser) 23034 { 23035 tree receiver, messageargs; 23036 23037 cp_lexer_consume_token (parser->lexer); /* Eat '['. */ 23038 receiver = cp_parser_objc_message_receiver (parser); 23039 messageargs = cp_parser_objc_message_args (parser); 23040 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 23041 23042 return objc_build_message_expr (receiver, messageargs); 23043 } 23044 23045 /* Parse an objc-message-receiver. 23046 23047 objc-message-receiver: 23048 expression 23049 simple-type-specifier 23050 23051 Returns a representation of the type or expression. */ 23052 23053 static tree 23054 cp_parser_objc_message_receiver (cp_parser* parser) 23055 { 23056 tree rcv; 23057 23058 /* An Objective-C message receiver may be either (1) a type 23059 or (2) an expression. */ 23060 cp_parser_parse_tentatively (parser); 23061 rcv = cp_parser_expression (parser, false, NULL); 23062 23063 if (cp_parser_parse_definitely (parser)) 23064 return rcv; 23065 23066 rcv = cp_parser_simple_type_specifier (parser, 23067 /*decl_specs=*/NULL, 23068 CP_PARSER_FLAGS_NONE); 23069 23070 return objc_get_class_reference (rcv); 23071 } 23072 23073 /* Parse the arguments and selectors comprising an Objective-C message. 23074 23075 objc-message-args: 23076 objc-selector 23077 objc-selector-args 23078 objc-selector-args , objc-comma-args 23079 23080 objc-selector-args: 23081 objc-selector [opt] : assignment-expression 23082 objc-selector-args objc-selector [opt] : assignment-expression 23083 23084 objc-comma-args: 23085 assignment-expression 23086 objc-comma-args , assignment-expression 23087 23088 Returns a TREE_LIST, with TREE_PURPOSE containing a list of 23089 selector arguments and TREE_VALUE containing a list of comma 23090 arguments. */ 23091 23092 static tree 23093 cp_parser_objc_message_args (cp_parser* parser) 23094 { 23095 tree sel_args = NULL_TREE, addl_args = NULL_TREE; 23096 bool maybe_unary_selector_p = true; 23097 cp_token *token = cp_lexer_peek_token (parser->lexer); 23098 23099 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) 23100 { 23101 tree selector = NULL_TREE, arg; 23102 23103 if (token->type != CPP_COLON) 23104 selector = cp_parser_objc_selector (parser); 23105 23106 /* Detect if we have a unary selector. */ 23107 if (maybe_unary_selector_p 23108 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) 23109 return build_tree_list (selector, NULL_TREE); 23110 23111 maybe_unary_selector_p = false; 23112 cp_parser_require (parser, CPP_COLON, RT_COLON); 23113 arg = cp_parser_assignment_expression (parser, false, NULL); 23114 23115 sel_args 23116 = chainon (sel_args, 23117 build_tree_list (selector, arg)); 23118 23119 token = cp_lexer_peek_token (parser->lexer); 23120 } 23121 23122 /* Handle non-selector arguments, if any. */ 23123 while (token->type == CPP_COMMA) 23124 { 23125 tree arg; 23126 23127 cp_lexer_consume_token (parser->lexer); 23128 arg = cp_parser_assignment_expression (parser, false, NULL); 23129 23130 addl_args 23131 = chainon (addl_args, 23132 build_tree_list (NULL_TREE, arg)); 23133 23134 token = cp_lexer_peek_token (parser->lexer); 23135 } 23136 23137 if (sel_args == NULL_TREE && addl_args == NULL_TREE) 23138 { 23139 cp_parser_error (parser, "objective-c++ message argument(s) are expected"); 23140 return build_tree_list (error_mark_node, error_mark_node); 23141 } 23142 23143 return build_tree_list (sel_args, addl_args); 23144 } 23145 23146 /* Parse an Objective-C encode expression. 23147 23148 objc-encode-expression: 23149 @encode objc-typename 23150 23151 Returns an encoded representation of the type argument. */ 23152 23153 static tree 23154 cp_parser_objc_encode_expression (cp_parser* parser) 23155 { 23156 tree type; 23157 cp_token *token; 23158 23159 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */ 23160 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 23161 token = cp_lexer_peek_token (parser->lexer); 23162 type = complete_type (cp_parser_type_id (parser)); 23163 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23164 23165 if (!type) 23166 { 23167 error_at (token->location, 23168 "%<@encode%> must specify a type as an argument"); 23169 return error_mark_node; 23170 } 23171 23172 /* This happens if we find @encode(T) (where T is a template 23173 typename or something dependent on a template typename) when 23174 parsing a template. In that case, we can't compile it 23175 immediately, but we rather create an AT_ENCODE_EXPR which will 23176 need to be instantiated when the template is used. 23177 */ 23178 if (dependent_type_p (type)) 23179 { 23180 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type); 23181 TREE_READONLY (value) = 1; 23182 return value; 23183 } 23184 23185 return objc_build_encode_expr (type); 23186 } 23187 23188 /* Parse an Objective-C @defs expression. */ 23189 23190 static tree 23191 cp_parser_objc_defs_expression (cp_parser *parser) 23192 { 23193 tree name; 23194 23195 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */ 23196 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 23197 name = cp_parser_identifier (parser); 23198 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23199 23200 return objc_get_class_ivars (name); 23201 } 23202 23203 /* Parse an Objective-C protocol expression. 23204 23205 objc-protocol-expression: 23206 @protocol ( identifier ) 23207 23208 Returns a representation of the protocol expression. */ 23209 23210 static tree 23211 cp_parser_objc_protocol_expression (cp_parser* parser) 23212 { 23213 tree proto; 23214 23215 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */ 23216 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 23217 proto = cp_parser_identifier (parser); 23218 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23219 23220 return objc_build_protocol_expr (proto); 23221 } 23222 23223 /* Parse an Objective-C selector expression. 23224 23225 objc-selector-expression: 23226 @selector ( objc-method-signature ) 23227 23228 objc-method-signature: 23229 objc-selector 23230 objc-selector-seq 23231 23232 objc-selector-seq: 23233 objc-selector : 23234 objc-selector-seq objc-selector : 23235 23236 Returns a representation of the method selector. */ 23237 23238 static tree 23239 cp_parser_objc_selector_expression (cp_parser* parser) 23240 { 23241 tree sel_seq = NULL_TREE; 23242 bool maybe_unary_selector_p = true; 23243 cp_token *token; 23244 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 23245 23246 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */ 23247 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 23248 token = cp_lexer_peek_token (parser->lexer); 23249 23250 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON 23251 || token->type == CPP_SCOPE) 23252 { 23253 tree selector = NULL_TREE; 23254 23255 if (token->type != CPP_COLON 23256 || token->type == CPP_SCOPE) 23257 selector = cp_parser_objc_selector (parser); 23258 23259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON) 23260 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)) 23261 { 23262 /* Detect if we have a unary selector. */ 23263 if (maybe_unary_selector_p) 23264 { 23265 sel_seq = selector; 23266 goto finish_selector; 23267 } 23268 else 23269 { 23270 cp_parser_error (parser, "expected %<:%>"); 23271 } 23272 } 23273 maybe_unary_selector_p = false; 23274 token = cp_lexer_consume_token (parser->lexer); 23275 23276 if (token->type == CPP_SCOPE) 23277 { 23278 sel_seq 23279 = chainon (sel_seq, 23280 build_tree_list (selector, NULL_TREE)); 23281 sel_seq 23282 = chainon (sel_seq, 23283 build_tree_list (NULL_TREE, NULL_TREE)); 23284 } 23285 else 23286 sel_seq 23287 = chainon (sel_seq, 23288 build_tree_list (selector, NULL_TREE)); 23289 23290 token = cp_lexer_peek_token (parser->lexer); 23291 } 23292 23293 finish_selector: 23294 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23295 23296 return objc_build_selector_expr (loc, sel_seq); 23297 } 23298 23299 /* Parse a list of identifiers. 23300 23301 objc-identifier-list: 23302 identifier 23303 objc-identifier-list , identifier 23304 23305 Returns a TREE_LIST of identifier nodes. */ 23306 23307 static tree 23308 cp_parser_objc_identifier_list (cp_parser* parser) 23309 { 23310 tree identifier; 23311 tree list; 23312 cp_token *sep; 23313 23314 identifier = cp_parser_identifier (parser); 23315 if (identifier == error_mark_node) 23316 return error_mark_node; 23317 23318 list = build_tree_list (NULL_TREE, identifier); 23319 sep = cp_lexer_peek_token (parser->lexer); 23320 23321 while (sep->type == CPP_COMMA) 23322 { 23323 cp_lexer_consume_token (parser->lexer); /* Eat ','. */ 23324 identifier = cp_parser_identifier (parser); 23325 if (identifier == error_mark_node) 23326 return list; 23327 23328 list = chainon (list, build_tree_list (NULL_TREE, 23329 identifier)); 23330 sep = cp_lexer_peek_token (parser->lexer); 23331 } 23332 23333 return list; 23334 } 23335 23336 /* Parse an Objective-C alias declaration. 23337 23338 objc-alias-declaration: 23339 @compatibility_alias identifier identifier ; 23340 23341 This function registers the alias mapping with the Objective-C front end. 23342 It returns nothing. */ 23343 23344 static void 23345 cp_parser_objc_alias_declaration (cp_parser* parser) 23346 { 23347 tree alias, orig; 23348 23349 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */ 23350 alias = cp_parser_identifier (parser); 23351 orig = cp_parser_identifier (parser); 23352 objc_declare_alias (alias, orig); 23353 cp_parser_consume_semicolon_at_end_of_statement (parser); 23354 } 23355 23356 /* Parse an Objective-C class forward-declaration. 23357 23358 objc-class-declaration: 23359 @class objc-identifier-list ; 23360 23361 The function registers the forward declarations with the Objective-C 23362 front end. It returns nothing. */ 23363 23364 static void 23365 cp_parser_objc_class_declaration (cp_parser* parser) 23366 { 23367 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */ 23368 while (true) 23369 { 23370 tree id; 23371 23372 id = cp_parser_identifier (parser); 23373 if (id == error_mark_node) 23374 break; 23375 23376 objc_declare_class (id); 23377 23378 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 23379 cp_lexer_consume_token (parser->lexer); 23380 else 23381 break; 23382 } 23383 cp_parser_consume_semicolon_at_end_of_statement (parser); 23384 } 23385 23386 /* Parse a list of Objective-C protocol references. 23387 23388 objc-protocol-refs-opt: 23389 objc-protocol-refs [opt] 23390 23391 objc-protocol-refs: 23392 < objc-identifier-list > 23393 23394 Returns a TREE_LIST of identifiers, if any. */ 23395 23396 static tree 23397 cp_parser_objc_protocol_refs_opt (cp_parser* parser) 23398 { 23399 tree protorefs = NULL_TREE; 23400 23401 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS)) 23402 { 23403 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */ 23404 protorefs = cp_parser_objc_identifier_list (parser); 23405 cp_parser_require (parser, CPP_GREATER, RT_GREATER); 23406 } 23407 23408 return protorefs; 23409 } 23410 23411 /* Parse a Objective-C visibility specification. */ 23412 23413 static void 23414 cp_parser_objc_visibility_spec (cp_parser* parser) 23415 { 23416 cp_token *vis = cp_lexer_peek_token (parser->lexer); 23417 23418 switch (vis->keyword) 23419 { 23420 case RID_AT_PRIVATE: 23421 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE); 23422 break; 23423 case RID_AT_PROTECTED: 23424 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED); 23425 break; 23426 case RID_AT_PUBLIC: 23427 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC); 23428 break; 23429 case RID_AT_PACKAGE: 23430 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE); 23431 break; 23432 default: 23433 return; 23434 } 23435 23436 /* Eat '@private'/'@protected'/'@public'. */ 23437 cp_lexer_consume_token (parser->lexer); 23438 } 23439 23440 /* Parse an Objective-C method type. Return 'true' if it is a class 23441 (+) method, and 'false' if it is an instance (-) method. */ 23442 23443 static inline bool 23444 cp_parser_objc_method_type (cp_parser* parser) 23445 { 23446 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS) 23447 return true; 23448 else 23449 return false; 23450 } 23451 23452 /* Parse an Objective-C protocol qualifier. */ 23453 23454 static tree 23455 cp_parser_objc_protocol_qualifiers (cp_parser* parser) 23456 { 23457 tree quals = NULL_TREE, node; 23458 cp_token *token = cp_lexer_peek_token (parser->lexer); 23459 23460 node = token->u.value; 23461 23462 while (node && TREE_CODE (node) == IDENTIFIER_NODE 23463 && (node == ridpointers [(int) RID_IN] 23464 || node == ridpointers [(int) RID_OUT] 23465 || node == ridpointers [(int) RID_INOUT] 23466 || node == ridpointers [(int) RID_BYCOPY] 23467 || node == ridpointers [(int) RID_BYREF] 23468 || node == ridpointers [(int) RID_ONEWAY])) 23469 { 23470 quals = tree_cons (NULL_TREE, node, quals); 23471 cp_lexer_consume_token (parser->lexer); 23472 token = cp_lexer_peek_token (parser->lexer); 23473 node = token->u.value; 23474 } 23475 23476 return quals; 23477 } 23478 23479 /* Parse an Objective-C typename. */ 23480 23481 static tree 23482 cp_parser_objc_typename (cp_parser* parser) 23483 { 23484 tree type_name = NULL_TREE; 23485 23486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 23487 { 23488 tree proto_quals, cp_type = NULL_TREE; 23489 23490 cp_lexer_consume_token (parser->lexer); /* Eat '('. */ 23491 proto_quals = cp_parser_objc_protocol_qualifiers (parser); 23492 23493 /* An ObjC type name may consist of just protocol qualifiers, in which 23494 case the type shall default to 'id'. */ 23495 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) 23496 { 23497 cp_type = cp_parser_type_id (parser); 23498 23499 /* If the type could not be parsed, an error has already 23500 been produced. For error recovery, behave as if it had 23501 not been specified, which will use the default type 23502 'id'. */ 23503 if (cp_type == error_mark_node) 23504 { 23505 cp_type = NULL_TREE; 23506 /* We need to skip to the closing parenthesis as 23507 cp_parser_type_id() does not seem to do it for 23508 us. */ 23509 cp_parser_skip_to_closing_parenthesis (parser, 23510 /*recovering=*/true, 23511 /*or_comma=*/false, 23512 /*consume_paren=*/false); 23513 } 23514 } 23515 23516 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 23517 type_name = build_tree_list (proto_quals, cp_type); 23518 } 23519 23520 return type_name; 23521 } 23522 23523 /* Check to see if TYPE refers to an Objective-C selector name. */ 23524 23525 static bool 23526 cp_parser_objc_selector_p (enum cpp_ttype type) 23527 { 23528 return (type == CPP_NAME || type == CPP_KEYWORD 23529 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND 23530 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT 23531 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ 23532 || type == CPP_XOR || type == CPP_XOR_EQ); 23533 } 23534 23535 /* Parse an Objective-C selector. */ 23536 23537 static tree 23538 cp_parser_objc_selector (cp_parser* parser) 23539 { 23540 cp_token *token = cp_lexer_consume_token (parser->lexer); 23541 23542 if (!cp_parser_objc_selector_p (token->type)) 23543 { 23544 error_at (token->location, "invalid Objective-C++ selector name"); 23545 return error_mark_node; 23546 } 23547 23548 /* C++ operator names are allowed to appear in ObjC selectors. */ 23549 switch (token->type) 23550 { 23551 case CPP_AND_AND: return get_identifier ("and"); 23552 case CPP_AND_EQ: return get_identifier ("and_eq"); 23553 case CPP_AND: return get_identifier ("bitand"); 23554 case CPP_OR: return get_identifier ("bitor"); 23555 case CPP_COMPL: return get_identifier ("compl"); 23556 case CPP_NOT: return get_identifier ("not"); 23557 case CPP_NOT_EQ: return get_identifier ("not_eq"); 23558 case CPP_OR_OR: return get_identifier ("or"); 23559 case CPP_OR_EQ: return get_identifier ("or_eq"); 23560 case CPP_XOR: return get_identifier ("xor"); 23561 case CPP_XOR_EQ: return get_identifier ("xor_eq"); 23562 default: return token->u.value; 23563 } 23564 } 23565 23566 /* Parse an Objective-C params list. */ 23567 23568 static tree 23569 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes) 23570 { 23571 tree params = NULL_TREE; 23572 bool maybe_unary_selector_p = true; 23573 cp_token *token = cp_lexer_peek_token (parser->lexer); 23574 23575 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON) 23576 { 23577 tree selector = NULL_TREE, type_name, identifier; 23578 tree parm_attr = NULL_TREE; 23579 23580 if (token->keyword == RID_ATTRIBUTE) 23581 break; 23582 23583 if (token->type != CPP_COLON) 23584 selector = cp_parser_objc_selector (parser); 23585 23586 /* Detect if we have a unary selector. */ 23587 if (maybe_unary_selector_p 23588 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) 23589 { 23590 params = selector; /* Might be followed by attributes. */ 23591 break; 23592 } 23593 23594 maybe_unary_selector_p = false; 23595 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)) 23596 { 23597 /* Something went quite wrong. There should be a colon 23598 here, but there is not. Stop parsing parameters. */ 23599 break; 23600 } 23601 type_name = cp_parser_objc_typename (parser); 23602 /* New ObjC allows attributes on parameters too. */ 23603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) 23604 parm_attr = cp_parser_attributes_opt (parser); 23605 identifier = cp_parser_identifier (parser); 23606 23607 params 23608 = chainon (params, 23609 objc_build_keyword_decl (selector, 23610 type_name, 23611 identifier, 23612 parm_attr)); 23613 23614 token = cp_lexer_peek_token (parser->lexer); 23615 } 23616 23617 if (params == NULL_TREE) 23618 { 23619 cp_parser_error (parser, "objective-c++ method declaration is expected"); 23620 return error_mark_node; 23621 } 23622 23623 /* We allow tail attributes for the method. */ 23624 if (token->keyword == RID_ATTRIBUTE) 23625 { 23626 *attributes = cp_parser_attributes_opt (parser); 23627 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 23628 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 23629 return params; 23630 cp_parser_error (parser, 23631 "method attributes must be specified at the end"); 23632 return error_mark_node; 23633 } 23634 23635 if (params == NULL_TREE) 23636 { 23637 cp_parser_error (parser, "objective-c++ method declaration is expected"); 23638 return error_mark_node; 23639 } 23640 return params; 23641 } 23642 23643 /* Parse the non-keyword Objective-C params. */ 23644 23645 static tree 23646 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 23647 tree* attributes) 23648 { 23649 tree params = make_node (TREE_LIST); 23650 cp_token *token = cp_lexer_peek_token (parser->lexer); 23651 *ellipsisp = false; /* Initially, assume no ellipsis. */ 23652 23653 while (token->type == CPP_COMMA) 23654 { 23655 cp_parameter_declarator *parmdecl; 23656 tree parm; 23657 23658 cp_lexer_consume_token (parser->lexer); /* Eat ','. */ 23659 token = cp_lexer_peek_token (parser->lexer); 23660 23661 if (token->type == CPP_ELLIPSIS) 23662 { 23663 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */ 23664 *ellipsisp = true; 23665 token = cp_lexer_peek_token (parser->lexer); 23666 break; 23667 } 23668 23669 /* TODO: parse attributes for tail parameters. */ 23670 parmdecl = cp_parser_parameter_declaration (parser, false, NULL); 23671 parm = grokdeclarator (parmdecl->declarator, 23672 &parmdecl->decl_specifiers, 23673 PARM, /*initialized=*/0, 23674 /*attrlist=*/NULL); 23675 23676 chainon (params, build_tree_list (NULL_TREE, parm)); 23677 token = cp_lexer_peek_token (parser->lexer); 23678 } 23679 23680 /* We allow tail attributes for the method. */ 23681 if (token->keyword == RID_ATTRIBUTE) 23682 { 23683 if (*attributes == NULL_TREE) 23684 { 23685 *attributes = cp_parser_attributes_opt (parser); 23686 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON) 23687 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 23688 return params; 23689 } 23690 else 23691 /* We have an error, but parse the attributes, so that we can 23692 carry on. */ 23693 *attributes = cp_parser_attributes_opt (parser); 23694 23695 cp_parser_error (parser, 23696 "method attributes must be specified at the end"); 23697 return error_mark_node; 23698 } 23699 23700 return params; 23701 } 23702 23703 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */ 23704 23705 static void 23706 cp_parser_objc_interstitial_code (cp_parser* parser) 23707 { 23708 cp_token *token = cp_lexer_peek_token (parser->lexer); 23709 23710 /* If the next token is `extern' and the following token is a string 23711 literal, then we have a linkage specification. */ 23712 if (token->keyword == RID_EXTERN 23713 && cp_parser_is_pure_string_literal 23714 (cp_lexer_peek_nth_token (parser->lexer, 2))) 23715 cp_parser_linkage_specification (parser); 23716 /* Handle #pragma, if any. */ 23717 else if (token->type == CPP_PRAGMA) 23718 cp_parser_pragma (parser, pragma_external); 23719 /* Allow stray semicolons. */ 23720 else if (token->type == CPP_SEMICOLON) 23721 cp_lexer_consume_token (parser->lexer); 23722 /* Mark methods as optional or required, when building protocols. */ 23723 else if (token->keyword == RID_AT_OPTIONAL) 23724 { 23725 cp_lexer_consume_token (parser->lexer); 23726 objc_set_method_opt (true); 23727 } 23728 else if (token->keyword == RID_AT_REQUIRED) 23729 { 23730 cp_lexer_consume_token (parser->lexer); 23731 objc_set_method_opt (false); 23732 } 23733 else if (token->keyword == RID_NAMESPACE) 23734 cp_parser_namespace_definition (parser); 23735 /* Other stray characters must generate errors. */ 23736 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE) 23737 { 23738 cp_lexer_consume_token (parser->lexer); 23739 error ("stray %qs between Objective-C++ methods", 23740 token->type == CPP_OPEN_BRACE ? "{" : "}"); 23741 } 23742 /* Finally, try to parse a block-declaration, or a function-definition. */ 23743 else 23744 cp_parser_block_declaration (parser, /*statement_p=*/false); 23745 } 23746 23747 /* Parse a method signature. */ 23748 23749 static tree 23750 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes) 23751 { 23752 tree rettype, kwdparms, optparms; 23753 bool ellipsis = false; 23754 bool is_class_method; 23755 23756 is_class_method = cp_parser_objc_method_type (parser); 23757 rettype = cp_parser_objc_typename (parser); 23758 *attributes = NULL_TREE; 23759 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes); 23760 if (kwdparms == error_mark_node) 23761 return error_mark_node; 23762 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes); 23763 if (optparms == error_mark_node) 23764 return error_mark_node; 23765 23766 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis); 23767 } 23768 23769 static bool 23770 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser) 23771 { 23772 tree tattr; 23773 cp_lexer_save_tokens (parser->lexer); 23774 tattr = cp_parser_attributes_opt (parser); 23775 gcc_assert (tattr) ; 23776 23777 /* If the attributes are followed by a method introducer, this is not allowed. 23778 Dump the attributes and flag the situation. */ 23779 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS) 23780 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS)) 23781 return true; 23782 23783 /* Otherwise, the attributes introduce some interstitial code, possibly so 23784 rewind to allow that check. */ 23785 cp_lexer_rollback_tokens (parser->lexer); 23786 return false; 23787 } 23788 23789 /* Parse an Objective-C method prototype list. */ 23790 23791 static void 23792 cp_parser_objc_method_prototype_list (cp_parser* parser) 23793 { 23794 cp_token *token = cp_lexer_peek_token (parser->lexer); 23795 23796 while (token->keyword != RID_AT_END && token->type != CPP_EOF) 23797 { 23798 if (token->type == CPP_PLUS || token->type == CPP_MINUS) 23799 { 23800 tree attributes, sig; 23801 bool is_class_method; 23802 if (token->type == CPP_PLUS) 23803 is_class_method = true; 23804 else 23805 is_class_method = false; 23806 sig = cp_parser_objc_method_signature (parser, &attributes); 23807 if (sig == error_mark_node) 23808 { 23809 cp_parser_skip_to_end_of_block_or_statement (parser); 23810 token = cp_lexer_peek_token (parser->lexer); 23811 continue; 23812 } 23813 objc_add_method_declaration (is_class_method, sig, attributes); 23814 cp_parser_consume_semicolon_at_end_of_statement (parser); 23815 } 23816 else if (token->keyword == RID_AT_PROPERTY) 23817 cp_parser_objc_at_property_declaration (parser); 23818 else if (token->keyword == RID_ATTRIBUTE 23819 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser)) 23820 warning_at (cp_lexer_peek_token (parser->lexer)->location, 23821 OPT_Wattributes, 23822 "prefix attributes are ignored for methods"); 23823 else 23824 /* Allow for interspersed non-ObjC++ code. */ 23825 cp_parser_objc_interstitial_code (parser); 23826 23827 token = cp_lexer_peek_token (parser->lexer); 23828 } 23829 23830 if (token->type != CPP_EOF) 23831 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ 23832 else 23833 cp_parser_error (parser, "expected %<@end%>"); 23834 23835 objc_finish_interface (); 23836 } 23837 23838 /* Parse an Objective-C method definition list. */ 23839 23840 static void 23841 cp_parser_objc_method_definition_list (cp_parser* parser) 23842 { 23843 cp_token *token = cp_lexer_peek_token (parser->lexer); 23844 23845 while (token->keyword != RID_AT_END && token->type != CPP_EOF) 23846 { 23847 tree meth; 23848 23849 if (token->type == CPP_PLUS || token->type == CPP_MINUS) 23850 { 23851 cp_token *ptk; 23852 tree sig, attribute; 23853 bool is_class_method; 23854 if (token->type == CPP_PLUS) 23855 is_class_method = true; 23856 else 23857 is_class_method = false; 23858 push_deferring_access_checks (dk_deferred); 23859 sig = cp_parser_objc_method_signature (parser, &attribute); 23860 if (sig == error_mark_node) 23861 { 23862 cp_parser_skip_to_end_of_block_or_statement (parser); 23863 token = cp_lexer_peek_token (parser->lexer); 23864 continue; 23865 } 23866 objc_start_method_definition (is_class_method, sig, attribute, 23867 NULL_TREE); 23868 23869 /* For historical reasons, we accept an optional semicolon. */ 23870 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 23871 cp_lexer_consume_token (parser->lexer); 23872 23873 ptk = cp_lexer_peek_token (parser->lexer); 23874 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 23875 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END)) 23876 { 23877 perform_deferred_access_checks (); 23878 stop_deferring_access_checks (); 23879 meth = cp_parser_function_definition_after_declarator (parser, 23880 false); 23881 pop_deferring_access_checks (); 23882 objc_finish_method_definition (meth); 23883 } 23884 } 23885 /* The following case will be removed once @synthesize is 23886 completely implemented. */ 23887 else if (token->keyword == RID_AT_PROPERTY) 23888 cp_parser_objc_at_property_declaration (parser); 23889 else if (token->keyword == RID_AT_SYNTHESIZE) 23890 cp_parser_objc_at_synthesize_declaration (parser); 23891 else if (token->keyword == RID_AT_DYNAMIC) 23892 cp_parser_objc_at_dynamic_declaration (parser); 23893 else if (token->keyword == RID_ATTRIBUTE 23894 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser)) 23895 warning_at (token->location, OPT_Wattributes, 23896 "prefix attributes are ignored for methods"); 23897 else 23898 /* Allow for interspersed non-ObjC++ code. */ 23899 cp_parser_objc_interstitial_code (parser); 23900 23901 token = cp_lexer_peek_token (parser->lexer); 23902 } 23903 23904 if (token->type != CPP_EOF) 23905 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ 23906 else 23907 cp_parser_error (parser, "expected %<@end%>"); 23908 23909 objc_finish_implementation (); 23910 } 23911 23912 /* Parse Objective-C ivars. */ 23913 23914 static void 23915 cp_parser_objc_class_ivars (cp_parser* parser) 23916 { 23917 cp_token *token = cp_lexer_peek_token (parser->lexer); 23918 23919 if (token->type != CPP_OPEN_BRACE) 23920 return; /* No ivars specified. */ 23921 23922 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */ 23923 token = cp_lexer_peek_token (parser->lexer); 23924 23925 while (token->type != CPP_CLOSE_BRACE 23926 && token->keyword != RID_AT_END && token->type != CPP_EOF) 23927 { 23928 cp_decl_specifier_seq declspecs; 23929 int decl_class_or_enum_p; 23930 tree prefix_attributes; 23931 23932 cp_parser_objc_visibility_spec (parser); 23933 23934 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 23935 break; 23936 23937 cp_parser_decl_specifier_seq (parser, 23938 CP_PARSER_FLAGS_OPTIONAL, 23939 &declspecs, 23940 &decl_class_or_enum_p); 23941 23942 /* auto, register, static, extern, mutable. */ 23943 if (declspecs.storage_class != sc_none) 23944 { 23945 cp_parser_error (parser, "invalid type for instance variable"); 23946 declspecs.storage_class = sc_none; 23947 } 23948 23949 /* __thread. */ 23950 if (declspecs.specs[(int) ds_thread]) 23951 { 23952 cp_parser_error (parser, "invalid type for instance variable"); 23953 declspecs.specs[(int) ds_thread] = 0; 23954 } 23955 23956 /* typedef. */ 23957 if (declspecs.specs[(int) ds_typedef]) 23958 { 23959 cp_parser_error (parser, "invalid type for instance variable"); 23960 declspecs.specs[(int) ds_typedef] = 0; 23961 } 23962 23963 prefix_attributes = declspecs.attributes; 23964 declspecs.attributes = NULL_TREE; 23965 23966 /* Keep going until we hit the `;' at the end of the 23967 declaration. */ 23968 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 23969 { 23970 tree width = NULL_TREE, attributes, first_attribute, decl; 23971 cp_declarator *declarator = NULL; 23972 int ctor_dtor_or_conv_p; 23973 23974 /* Check for a (possibly unnamed) bitfield declaration. */ 23975 token = cp_lexer_peek_token (parser->lexer); 23976 if (token->type == CPP_COLON) 23977 goto eat_colon; 23978 23979 if (token->type == CPP_NAME 23980 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type 23981 == CPP_COLON)) 23982 { 23983 /* Get the name of the bitfield. */ 23984 declarator = make_id_declarator (NULL_TREE, 23985 cp_parser_identifier (parser), 23986 sfk_none); 23987 23988 eat_colon: 23989 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */ 23990 /* Get the width of the bitfield. */ 23991 width 23992 = cp_parser_constant_expression (parser, 23993 /*allow_non_constant=*/false, 23994 NULL); 23995 } 23996 else 23997 { 23998 /* Parse the declarator. */ 23999 declarator 24000 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 24001 &ctor_dtor_or_conv_p, 24002 /*parenthesized_p=*/NULL, 24003 /*member_p=*/false); 24004 } 24005 24006 /* Look for attributes that apply to the ivar. */ 24007 attributes = cp_parser_attributes_opt (parser); 24008 /* Remember which attributes are prefix attributes and 24009 which are not. */ 24010 first_attribute = attributes; 24011 /* Combine the attributes. */ 24012 attributes = chainon (prefix_attributes, attributes); 24013 24014 if (width) 24015 /* Create the bitfield declaration. */ 24016 decl = grokbitfield (declarator, &declspecs, 24017 width, 24018 attributes); 24019 else 24020 decl = grokfield (declarator, &declspecs, 24021 NULL_TREE, /*init_const_expr_p=*/false, 24022 NULL_TREE, attributes); 24023 24024 /* Add the instance variable. */ 24025 if (decl != error_mark_node && decl != NULL_TREE) 24026 objc_add_instance_variable (decl); 24027 24028 /* Reset PREFIX_ATTRIBUTES. */ 24029 while (attributes && TREE_CHAIN (attributes) != first_attribute) 24030 attributes = TREE_CHAIN (attributes); 24031 if (attributes) 24032 TREE_CHAIN (attributes) = NULL_TREE; 24033 24034 token = cp_lexer_peek_token (parser->lexer); 24035 24036 if (token->type == CPP_COMMA) 24037 { 24038 cp_lexer_consume_token (parser->lexer); /* Eat ','. */ 24039 continue; 24040 } 24041 break; 24042 } 24043 24044 cp_parser_consume_semicolon_at_end_of_statement (parser); 24045 token = cp_lexer_peek_token (parser->lexer); 24046 } 24047 24048 if (token->keyword == RID_AT_END) 24049 cp_parser_error (parser, "expected %<}%>"); 24050 24051 /* Do not consume the RID_AT_END, so it will be read again as terminating 24052 the @interface of @implementation. */ 24053 if (token->keyword != RID_AT_END && token->type != CPP_EOF) 24054 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */ 24055 24056 /* For historical reasons, we accept an optional semicolon. */ 24057 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 24058 cp_lexer_consume_token (parser->lexer); 24059 } 24060 24061 /* Parse an Objective-C protocol declaration. */ 24062 24063 static void 24064 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes) 24065 { 24066 tree proto, protorefs; 24067 cp_token *tok; 24068 24069 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */ 24070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)) 24071 { 24072 tok = cp_lexer_peek_token (parser->lexer); 24073 error_at (tok->location, "identifier expected after %<@protocol%>"); 24074 cp_parser_consume_semicolon_at_end_of_statement (parser); 24075 return; 24076 } 24077 24078 /* See if we have a forward declaration or a definition. */ 24079 tok = cp_lexer_peek_nth_token (parser->lexer, 2); 24080 24081 /* Try a forward declaration first. */ 24082 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON) 24083 { 24084 while (true) 24085 { 24086 tree id; 24087 24088 id = cp_parser_identifier (parser); 24089 if (id == error_mark_node) 24090 break; 24091 24092 objc_declare_protocol (id, attributes); 24093 24094 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 24095 cp_lexer_consume_token (parser->lexer); 24096 else 24097 break; 24098 } 24099 cp_parser_consume_semicolon_at_end_of_statement (parser); 24100 } 24101 24102 /* Ok, we got a full-fledged definition (or at least should). */ 24103 else 24104 { 24105 proto = cp_parser_identifier (parser); 24106 protorefs = cp_parser_objc_protocol_refs_opt (parser); 24107 objc_start_protocol (proto, protorefs, attributes); 24108 cp_parser_objc_method_prototype_list (parser); 24109 } 24110 } 24111 24112 /* Parse an Objective-C superclass or category. */ 24113 24114 static void 24115 cp_parser_objc_superclass_or_category (cp_parser *parser, 24116 bool iface_p, 24117 tree *super, 24118 tree *categ, bool *is_class_extension) 24119 { 24120 cp_token *next = cp_lexer_peek_token (parser->lexer); 24121 24122 *super = *categ = NULL_TREE; 24123 *is_class_extension = false; 24124 if (next->type == CPP_COLON) 24125 { 24126 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */ 24127 *super = cp_parser_identifier (parser); 24128 } 24129 else if (next->type == CPP_OPEN_PAREN) 24130 { 24131 cp_lexer_consume_token (parser->lexer); /* Eat '('. */ 24132 24133 /* If there is no category name, and this is an @interface, we 24134 have a class extension. */ 24135 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) 24136 { 24137 *categ = NULL_TREE; 24138 *is_class_extension = true; 24139 } 24140 else 24141 *categ = cp_parser_identifier (parser); 24142 24143 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 24144 } 24145 } 24146 24147 /* Parse an Objective-C class interface. */ 24148 24149 static void 24150 cp_parser_objc_class_interface (cp_parser* parser, tree attributes) 24151 { 24152 tree name, super, categ, protos; 24153 bool is_class_extension; 24154 24155 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */ 24156 name = cp_parser_identifier (parser); 24157 if (name == error_mark_node) 24158 { 24159 /* It's hard to recover because even if valid @interface stuff 24160 is to follow, we can't compile it (or validate it) if we 24161 don't even know which class it refers to. Let's assume this 24162 was a stray '@interface' token in the stream and skip it. 24163 */ 24164 return; 24165 } 24166 cp_parser_objc_superclass_or_category (parser, true, &super, &categ, 24167 &is_class_extension); 24168 protos = cp_parser_objc_protocol_refs_opt (parser); 24169 24170 /* We have either a class or a category on our hands. */ 24171 if (categ || is_class_extension) 24172 objc_start_category_interface (name, categ, protos, attributes); 24173 else 24174 { 24175 objc_start_class_interface (name, super, protos, attributes); 24176 /* Handle instance variable declarations, if any. */ 24177 cp_parser_objc_class_ivars (parser); 24178 objc_continue_interface (); 24179 } 24180 24181 cp_parser_objc_method_prototype_list (parser); 24182 } 24183 24184 /* Parse an Objective-C class implementation. */ 24185 24186 static void 24187 cp_parser_objc_class_implementation (cp_parser* parser) 24188 { 24189 tree name, super, categ; 24190 bool is_class_extension; 24191 24192 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */ 24193 name = cp_parser_identifier (parser); 24194 if (name == error_mark_node) 24195 { 24196 /* It's hard to recover because even if valid @implementation 24197 stuff is to follow, we can't compile it (or validate it) if 24198 we don't even know which class it refers to. Let's assume 24199 this was a stray '@implementation' token in the stream and 24200 skip it. 24201 */ 24202 return; 24203 } 24204 cp_parser_objc_superclass_or_category (parser, false, &super, &categ, 24205 &is_class_extension); 24206 24207 /* We have either a class or a category on our hands. */ 24208 if (categ) 24209 objc_start_category_implementation (name, categ); 24210 else 24211 { 24212 objc_start_class_implementation (name, super); 24213 /* Handle instance variable declarations, if any. */ 24214 cp_parser_objc_class_ivars (parser); 24215 objc_continue_implementation (); 24216 } 24217 24218 cp_parser_objc_method_definition_list (parser); 24219 } 24220 24221 /* Consume the @end token and finish off the implementation. */ 24222 24223 static void 24224 cp_parser_objc_end_implementation (cp_parser* parser) 24225 { 24226 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */ 24227 objc_finish_implementation (); 24228 } 24229 24230 /* Parse an Objective-C declaration. */ 24231 24232 static void 24233 cp_parser_objc_declaration (cp_parser* parser, tree attributes) 24234 { 24235 /* Try to figure out what kind of declaration is present. */ 24236 cp_token *kwd = cp_lexer_peek_token (parser->lexer); 24237 24238 if (attributes) 24239 switch (kwd->keyword) 24240 { 24241 case RID_AT_ALIAS: 24242 case RID_AT_CLASS: 24243 case RID_AT_END: 24244 error_at (kwd->location, "attributes may not be specified before" 24245 " the %<@%D%> Objective-C++ keyword", 24246 kwd->u.value); 24247 attributes = NULL; 24248 break; 24249 case RID_AT_IMPLEMENTATION: 24250 warning_at (kwd->location, OPT_Wattributes, 24251 "prefix attributes are ignored before %<@%D%>", 24252 kwd->u.value); 24253 attributes = NULL; 24254 default: 24255 break; 24256 } 24257 24258 switch (kwd->keyword) 24259 { 24260 case RID_AT_ALIAS: 24261 cp_parser_objc_alias_declaration (parser); 24262 break; 24263 case RID_AT_CLASS: 24264 cp_parser_objc_class_declaration (parser); 24265 break; 24266 case RID_AT_PROTOCOL: 24267 cp_parser_objc_protocol_declaration (parser, attributes); 24268 break; 24269 case RID_AT_INTERFACE: 24270 cp_parser_objc_class_interface (parser, attributes); 24271 break; 24272 case RID_AT_IMPLEMENTATION: 24273 cp_parser_objc_class_implementation (parser); 24274 break; 24275 case RID_AT_END: 24276 cp_parser_objc_end_implementation (parser); 24277 break; 24278 default: 24279 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct", 24280 kwd->u.value); 24281 cp_parser_skip_to_end_of_block_or_statement (parser); 24282 } 24283 } 24284 24285 /* Parse an Objective-C try-catch-finally statement. 24286 24287 objc-try-catch-finally-stmt: 24288 @try compound-statement objc-catch-clause-seq [opt] 24289 objc-finally-clause [opt] 24290 24291 objc-catch-clause-seq: 24292 objc-catch-clause objc-catch-clause-seq [opt] 24293 24294 objc-catch-clause: 24295 @catch ( objc-exception-declaration ) compound-statement 24296 24297 objc-finally-clause: 24298 @finally compound-statement 24299 24300 objc-exception-declaration: 24301 parameter-declaration 24302 '...' 24303 24304 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS. 24305 24306 Returns NULL_TREE. 24307 24308 PS: This function is identical to c_parser_objc_try_catch_finally_statement 24309 for C. Keep them in sync. */ 24310 24311 static tree 24312 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) 24313 { 24314 location_t location; 24315 tree stmt; 24316 24317 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY); 24318 location = cp_lexer_peek_token (parser->lexer)->location; 24319 objc_maybe_warn_exceptions (location); 24320 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST 24321 node, lest it get absorbed into the surrounding block. */ 24322 stmt = push_stmt_list (); 24323 cp_parser_compound_statement (parser, NULL, false, false); 24324 objc_begin_try_stmt (location, pop_stmt_list (stmt)); 24325 24326 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH)) 24327 { 24328 cp_parameter_declarator *parm; 24329 tree parameter_declaration = error_mark_node; 24330 bool seen_open_paren = false; 24331 24332 cp_lexer_consume_token (parser->lexer); 24333 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 24334 seen_open_paren = true; 24335 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) 24336 { 24337 /* We have "@catch (...)" (where the '...' are literally 24338 what is in the code). Skip the '...'. 24339 parameter_declaration is set to NULL_TREE, and 24340 objc_being_catch_clauses() knows that that means 24341 '...'. */ 24342 cp_lexer_consume_token (parser->lexer); 24343 parameter_declaration = NULL_TREE; 24344 } 24345 else 24346 { 24347 /* We have "@catch (NSException *exception)" or something 24348 like that. Parse the parameter declaration. */ 24349 parm = cp_parser_parameter_declaration (parser, false, NULL); 24350 if (parm == NULL) 24351 parameter_declaration = error_mark_node; 24352 else 24353 parameter_declaration = grokdeclarator (parm->declarator, 24354 &parm->decl_specifiers, 24355 PARM, /*initialized=*/0, 24356 /*attrlist=*/NULL); 24357 } 24358 if (seen_open_paren) 24359 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 24360 else 24361 { 24362 /* If there was no open parenthesis, we are recovering from 24363 an error, and we are trying to figure out what mistake 24364 the user has made. */ 24365 24366 /* If there is an immediate closing parenthesis, the user 24367 probably forgot the opening one (ie, they typed "@catch 24368 NSException *e)". Parse the closing parenthesis and keep 24369 going. */ 24370 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)) 24371 cp_lexer_consume_token (parser->lexer); 24372 24373 /* If these is no immediate closing parenthesis, the user 24374 probably doesn't know that parenthesis are required at 24375 all (ie, they typed "@catch NSException *e"). So, just 24376 forget about the closing parenthesis and keep going. */ 24377 } 24378 objc_begin_catch_clause (parameter_declaration); 24379 cp_parser_compound_statement (parser, NULL, false, false); 24380 objc_finish_catch_clause (); 24381 } 24382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY)) 24383 { 24384 cp_lexer_consume_token (parser->lexer); 24385 location = cp_lexer_peek_token (parser->lexer)->location; 24386 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST 24387 node, lest it get absorbed into the surrounding block. */ 24388 stmt = push_stmt_list (); 24389 cp_parser_compound_statement (parser, NULL, false, false); 24390 objc_build_finally_clause (location, pop_stmt_list (stmt)); 24391 } 24392 24393 return objc_finish_try_stmt (); 24394 } 24395 24396 /* Parse an Objective-C synchronized statement. 24397 24398 objc-synchronized-stmt: 24399 @synchronized ( expression ) compound-statement 24400 24401 Returns NULL_TREE. */ 24402 24403 static tree 24404 cp_parser_objc_synchronized_statement (cp_parser *parser) 24405 { 24406 location_t location; 24407 tree lock, stmt; 24408 24409 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED); 24410 24411 location = cp_lexer_peek_token (parser->lexer)->location; 24412 objc_maybe_warn_exceptions (location); 24413 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 24414 lock = cp_parser_expression (parser, false, NULL); 24415 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 24416 24417 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST 24418 node, lest it get absorbed into the surrounding block. */ 24419 stmt = push_stmt_list (); 24420 cp_parser_compound_statement (parser, NULL, false, false); 24421 24422 return objc_build_synchronized (location, lock, pop_stmt_list (stmt)); 24423 } 24424 24425 /* Parse an Objective-C throw statement. 24426 24427 objc-throw-stmt: 24428 @throw assignment-expression [opt] ; 24429 24430 Returns a constructed '@throw' statement. */ 24431 24432 static tree 24433 cp_parser_objc_throw_statement (cp_parser *parser) 24434 { 24435 tree expr = NULL_TREE; 24436 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 24437 24438 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW); 24439 24440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 24441 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 24442 24443 cp_parser_consume_semicolon_at_end_of_statement (parser); 24444 24445 return objc_build_throw_stmt (loc, expr); 24446 } 24447 24448 /* Parse an Objective-C statement. */ 24449 24450 static tree 24451 cp_parser_objc_statement (cp_parser * parser) 24452 { 24453 /* Try to figure out what kind of declaration is present. */ 24454 cp_token *kwd = cp_lexer_peek_token (parser->lexer); 24455 24456 switch (kwd->keyword) 24457 { 24458 case RID_AT_TRY: 24459 return cp_parser_objc_try_catch_finally_statement (parser); 24460 case RID_AT_SYNCHRONIZED: 24461 return cp_parser_objc_synchronized_statement (parser); 24462 case RID_AT_THROW: 24463 return cp_parser_objc_throw_statement (parser); 24464 default: 24465 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct", 24466 kwd->u.value); 24467 cp_parser_skip_to_end_of_block_or_statement (parser); 24468 } 24469 24470 return error_mark_node; 24471 } 24472 24473 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 24474 look ahead to see if an objc keyword follows the attributes. This 24475 is to detect the use of prefix attributes on ObjC @interface and 24476 @protocol. */ 24477 24478 static bool 24479 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib) 24480 { 24481 cp_lexer_save_tokens (parser->lexer); 24482 *attrib = cp_parser_attributes_opt (parser); 24483 gcc_assert (*attrib); 24484 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword)) 24485 { 24486 cp_lexer_commit_tokens (parser->lexer); 24487 return true; 24488 } 24489 cp_lexer_rollback_tokens (parser->lexer); 24490 return false; 24491 } 24492 24493 /* This routine is a minimal replacement for 24494 c_parser_struct_declaration () used when parsing the list of 24495 types/names or ObjC++ properties. For example, when parsing the 24496 code 24497 24498 @property (readonly) int a, b, c; 24499 24500 this function is responsible for parsing "int a, int b, int c" and 24501 returning the declarations as CHAIN of DECLs. 24502 24503 TODO: Share this code with cp_parser_objc_class_ivars. It's very 24504 similar parsing. */ 24505 static tree 24506 cp_parser_objc_struct_declaration (cp_parser *parser) 24507 { 24508 tree decls = NULL_TREE; 24509 cp_decl_specifier_seq declspecs; 24510 int decl_class_or_enum_p; 24511 tree prefix_attributes; 24512 24513 cp_parser_decl_specifier_seq (parser, 24514 CP_PARSER_FLAGS_NONE, 24515 &declspecs, 24516 &decl_class_or_enum_p); 24517 24518 if (declspecs.type == error_mark_node) 24519 return error_mark_node; 24520 24521 /* auto, register, static, extern, mutable. */ 24522 if (declspecs.storage_class != sc_none) 24523 { 24524 cp_parser_error (parser, "invalid type for property"); 24525 declspecs.storage_class = sc_none; 24526 } 24527 24528 /* __thread. */ 24529 if (declspecs.specs[(int) ds_thread]) 24530 { 24531 cp_parser_error (parser, "invalid type for property"); 24532 declspecs.specs[(int) ds_thread] = 0; 24533 } 24534 24535 /* typedef. */ 24536 if (declspecs.specs[(int) ds_typedef]) 24537 { 24538 cp_parser_error (parser, "invalid type for property"); 24539 declspecs.specs[(int) ds_typedef] = 0; 24540 } 24541 24542 prefix_attributes = declspecs.attributes; 24543 declspecs.attributes = NULL_TREE; 24544 24545 /* Keep going until we hit the `;' at the end of the declaration. */ 24546 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 24547 { 24548 tree attributes, first_attribute, decl; 24549 cp_declarator *declarator; 24550 cp_token *token; 24551 24552 /* Parse the declarator. */ 24553 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED, 24554 NULL, NULL, false); 24555 24556 /* Look for attributes that apply to the ivar. */ 24557 attributes = cp_parser_attributes_opt (parser); 24558 /* Remember which attributes are prefix attributes and 24559 which are not. */ 24560 first_attribute = attributes; 24561 /* Combine the attributes. */ 24562 attributes = chainon (prefix_attributes, attributes); 24563 24564 decl = grokfield (declarator, &declspecs, 24565 NULL_TREE, /*init_const_expr_p=*/false, 24566 NULL_TREE, attributes); 24567 24568 if (decl == error_mark_node || decl == NULL_TREE) 24569 return error_mark_node; 24570 24571 /* Reset PREFIX_ATTRIBUTES. */ 24572 while (attributes && TREE_CHAIN (attributes) != first_attribute) 24573 attributes = TREE_CHAIN (attributes); 24574 if (attributes) 24575 TREE_CHAIN (attributes) = NULL_TREE; 24576 24577 DECL_CHAIN (decl) = decls; 24578 decls = decl; 24579 24580 token = cp_lexer_peek_token (parser->lexer); 24581 if (token->type == CPP_COMMA) 24582 { 24583 cp_lexer_consume_token (parser->lexer); /* Eat ','. */ 24584 continue; 24585 } 24586 else 24587 break; 24588 } 24589 return decls; 24590 } 24591 24592 /* Parse an Objective-C @property declaration. The syntax is: 24593 24594 objc-property-declaration: 24595 '@property' objc-property-attributes[opt] struct-declaration ; 24596 24597 objc-property-attributes: 24598 '(' objc-property-attribute-list ')' 24599 24600 objc-property-attribute-list: 24601 objc-property-attribute 24602 objc-property-attribute-list, objc-property-attribute 24603 24604 objc-property-attribute 24605 'getter' = identifier 24606 'setter' = identifier 24607 'readonly' 24608 'readwrite' 24609 'assign' 24610 'retain' 24611 'copy' 24612 'nonatomic' 24613 24614 For example: 24615 @property NSString *name; 24616 @property (readonly) id object; 24617 @property (retain, nonatomic, getter=getTheName) id name; 24618 @property int a, b, c; 24619 24620 PS: This function is identical to 24621 c_parser_objc_at_property_declaration for C. Keep them in sync. */ 24622 static void 24623 cp_parser_objc_at_property_declaration (cp_parser *parser) 24624 { 24625 /* The following variables hold the attributes of the properties as 24626 parsed. They are 'false' or 'NULL_TREE' if the attribute was not 24627 seen. When we see an attribute, we set them to 'true' (if they 24628 are boolean properties) or to the identifier (if they have an 24629 argument, ie, for getter and setter). Note that here we only 24630 parse the list of attributes, check the syntax and accumulate the 24631 attributes that we find. objc_add_property_declaration() will 24632 then process the information. */ 24633 bool property_assign = false; 24634 bool property_copy = false; 24635 tree property_getter_ident = NULL_TREE; 24636 bool property_nonatomic = false; 24637 bool property_readonly = false; 24638 bool property_readwrite = false; 24639 bool property_retain = false; 24640 tree property_setter_ident = NULL_TREE; 24641 24642 /* 'properties' is the list of properties that we read. Usually a 24643 single one, but maybe more (eg, in "@property int a, b, c;" there 24644 are three). */ 24645 tree properties; 24646 location_t loc; 24647 24648 loc = cp_lexer_peek_token (parser->lexer)->location; 24649 24650 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */ 24651 24652 /* Parse the optional attribute list... */ 24653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 24654 { 24655 /* Eat the '('. */ 24656 cp_lexer_consume_token (parser->lexer); 24657 24658 while (true) 24659 { 24660 bool syntax_error = false; 24661 cp_token *token = cp_lexer_peek_token (parser->lexer); 24662 enum rid keyword; 24663 24664 if (token->type != CPP_NAME) 24665 { 24666 cp_parser_error (parser, "expected identifier"); 24667 break; 24668 } 24669 keyword = C_RID_CODE (token->u.value); 24670 cp_lexer_consume_token (parser->lexer); 24671 switch (keyword) 24672 { 24673 case RID_ASSIGN: property_assign = true; break; 24674 case RID_COPY: property_copy = true; break; 24675 case RID_NONATOMIC: property_nonatomic = true; break; 24676 case RID_READONLY: property_readonly = true; break; 24677 case RID_READWRITE: property_readwrite = true; break; 24678 case RID_RETAIN: property_retain = true; break; 24679 24680 case RID_GETTER: 24681 case RID_SETTER: 24682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)) 24683 { 24684 if (keyword == RID_GETTER) 24685 cp_parser_error (parser, 24686 "missing %<=%> (after %<getter%> attribute)"); 24687 else 24688 cp_parser_error (parser, 24689 "missing %<=%> (after %<setter%> attribute)"); 24690 syntax_error = true; 24691 break; 24692 } 24693 cp_lexer_consume_token (parser->lexer); /* eat the = */ 24694 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type)) 24695 { 24696 cp_parser_error (parser, "expected identifier"); 24697 syntax_error = true; 24698 break; 24699 } 24700 if (keyword == RID_SETTER) 24701 { 24702 if (property_setter_ident != NULL_TREE) 24703 { 24704 cp_parser_error (parser, "the %<setter%> attribute may only be specified once"); 24705 cp_lexer_consume_token (parser->lexer); 24706 } 24707 else 24708 property_setter_ident = cp_parser_objc_selector (parser); 24709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)) 24710 cp_parser_error (parser, "setter name must terminate with %<:%>"); 24711 else 24712 cp_lexer_consume_token (parser->lexer); 24713 } 24714 else 24715 { 24716 if (property_getter_ident != NULL_TREE) 24717 { 24718 cp_parser_error (parser, "the %<getter%> attribute may only be specified once"); 24719 cp_lexer_consume_token (parser->lexer); 24720 } 24721 else 24722 property_getter_ident = cp_parser_objc_selector (parser); 24723 } 24724 break; 24725 default: 24726 cp_parser_error (parser, "unknown property attribute"); 24727 syntax_error = true; 24728 break; 24729 } 24730 24731 if (syntax_error) 24732 break; 24733 24734 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 24735 cp_lexer_consume_token (parser->lexer); 24736 else 24737 break; 24738 } 24739 24740 /* FIXME: "@property (setter, assign);" will generate a spurious 24741 "error: expected ‘)’ before ‘,’ token". This is because 24742 cp_parser_require, unlike the C counterpart, will produce an 24743 error even if we are in error recovery. */ 24744 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 24745 { 24746 cp_parser_skip_to_closing_parenthesis (parser, 24747 /*recovering=*/true, 24748 /*or_comma=*/false, 24749 /*consume_paren=*/true); 24750 } 24751 } 24752 24753 /* ... and the property declaration(s). */ 24754 properties = cp_parser_objc_struct_declaration (parser); 24755 24756 if (properties == error_mark_node) 24757 { 24758 cp_parser_skip_to_end_of_statement (parser); 24759 /* If the next token is now a `;', consume it. */ 24760 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 24761 cp_lexer_consume_token (parser->lexer); 24762 return; 24763 } 24764 24765 if (properties == NULL_TREE) 24766 cp_parser_error (parser, "expected identifier"); 24767 else 24768 { 24769 /* Comma-separated properties are chained together in 24770 reverse order; add them one by one. */ 24771 properties = nreverse (properties); 24772 24773 for (; properties; properties = TREE_CHAIN (properties)) 24774 objc_add_property_declaration (loc, copy_node (properties), 24775 property_readonly, property_readwrite, 24776 property_assign, property_retain, 24777 property_copy, property_nonatomic, 24778 property_getter_ident, property_setter_ident); 24779 } 24780 24781 cp_parser_consume_semicolon_at_end_of_statement (parser); 24782 } 24783 24784 /* Parse an Objective-C++ @synthesize declaration. The syntax is: 24785 24786 objc-synthesize-declaration: 24787 @synthesize objc-synthesize-identifier-list ; 24788 24789 objc-synthesize-identifier-list: 24790 objc-synthesize-identifier 24791 objc-synthesize-identifier-list, objc-synthesize-identifier 24792 24793 objc-synthesize-identifier 24794 identifier 24795 identifier = identifier 24796 24797 For example: 24798 @synthesize MyProperty; 24799 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty; 24800 24801 PS: This function is identical to c_parser_objc_at_synthesize_declaration 24802 for C. Keep them in sync. 24803 */ 24804 static void 24805 cp_parser_objc_at_synthesize_declaration (cp_parser *parser) 24806 { 24807 tree list = NULL_TREE; 24808 location_t loc; 24809 loc = cp_lexer_peek_token (parser->lexer)->location; 24810 24811 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */ 24812 while (true) 24813 { 24814 tree property, ivar; 24815 property = cp_parser_identifier (parser); 24816 if (property == error_mark_node) 24817 { 24818 cp_parser_consume_semicolon_at_end_of_statement (parser); 24819 return; 24820 } 24821 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) 24822 { 24823 cp_lexer_consume_token (parser->lexer); 24824 ivar = cp_parser_identifier (parser); 24825 if (ivar == error_mark_node) 24826 { 24827 cp_parser_consume_semicolon_at_end_of_statement (parser); 24828 return; 24829 } 24830 } 24831 else 24832 ivar = NULL_TREE; 24833 list = chainon (list, build_tree_list (ivar, property)); 24834 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 24835 cp_lexer_consume_token (parser->lexer); 24836 else 24837 break; 24838 } 24839 cp_parser_consume_semicolon_at_end_of_statement (parser); 24840 objc_add_synthesize_declaration (loc, list); 24841 } 24842 24843 /* Parse an Objective-C++ @dynamic declaration. The syntax is: 24844 24845 objc-dynamic-declaration: 24846 @dynamic identifier-list ; 24847 24848 For example: 24849 @dynamic MyProperty; 24850 @dynamic MyProperty, AnotherProperty; 24851 24852 PS: This function is identical to c_parser_objc_at_dynamic_declaration 24853 for C. Keep them in sync. 24854 */ 24855 static void 24856 cp_parser_objc_at_dynamic_declaration (cp_parser *parser) 24857 { 24858 tree list = NULL_TREE; 24859 location_t loc; 24860 loc = cp_lexer_peek_token (parser->lexer)->location; 24861 24862 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */ 24863 while (true) 24864 { 24865 tree property; 24866 property = cp_parser_identifier (parser); 24867 if (property == error_mark_node) 24868 { 24869 cp_parser_consume_semicolon_at_end_of_statement (parser); 24870 return; 24871 } 24872 list = chainon (list, build_tree_list (NULL, property)); 24873 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 24874 cp_lexer_consume_token (parser->lexer); 24875 else 24876 break; 24877 } 24878 cp_parser_consume_semicolon_at_end_of_statement (parser); 24879 objc_add_dynamic_declaration (loc, list); 24880 } 24881 24882 24883 /* OpenMP 2.5 parsing routines. */ 24884 24885 /* Returns name of the next clause. 24886 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and 24887 the token is not consumed. Otherwise appropriate pragma_omp_clause is 24888 returned and the token is consumed. */ 24889 24890 static pragma_omp_clause 24891 cp_parser_omp_clause_name (cp_parser *parser) 24892 { 24893 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE; 24894 24895 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF)) 24896 result = PRAGMA_OMP_CLAUSE_IF; 24897 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)) 24898 result = PRAGMA_OMP_CLAUSE_DEFAULT; 24899 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE)) 24900 result = PRAGMA_OMP_CLAUSE_PRIVATE; 24901 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 24902 { 24903 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 24904 const char *p = IDENTIFIER_POINTER (id); 24905 24906 switch (p[0]) 24907 { 24908 case 'c': 24909 if (!strcmp ("collapse", p)) 24910 result = PRAGMA_OMP_CLAUSE_COLLAPSE; 24911 else if (!strcmp ("copyin", p)) 24912 result = PRAGMA_OMP_CLAUSE_COPYIN; 24913 else if (!strcmp ("copyprivate", p)) 24914 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE; 24915 break; 24916 case 'f': 24917 if (!strcmp ("final", p)) 24918 result = PRAGMA_OMP_CLAUSE_FINAL; 24919 else if (!strcmp ("firstprivate", p)) 24920 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE; 24921 break; 24922 case 'l': 24923 if (!strcmp ("lastprivate", p)) 24924 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE; 24925 break; 24926 case 'm': 24927 if (!strcmp ("mergeable", p)) 24928 result = PRAGMA_OMP_CLAUSE_MERGEABLE; 24929 break; 24930 case 'n': 24931 if (!strcmp ("nowait", p)) 24932 result = PRAGMA_OMP_CLAUSE_NOWAIT; 24933 else if (!strcmp ("num_threads", p)) 24934 result = PRAGMA_OMP_CLAUSE_NUM_THREADS; 24935 break; 24936 case 'o': 24937 if (!strcmp ("ordered", p)) 24938 result = PRAGMA_OMP_CLAUSE_ORDERED; 24939 break; 24940 case 'r': 24941 if (!strcmp ("reduction", p)) 24942 result = PRAGMA_OMP_CLAUSE_REDUCTION; 24943 break; 24944 case 's': 24945 if (!strcmp ("schedule", p)) 24946 result = PRAGMA_OMP_CLAUSE_SCHEDULE; 24947 else if (!strcmp ("shared", p)) 24948 result = PRAGMA_OMP_CLAUSE_SHARED; 24949 break; 24950 case 'u': 24951 if (!strcmp ("untied", p)) 24952 result = PRAGMA_OMP_CLAUSE_UNTIED; 24953 break; 24954 } 24955 } 24956 24957 if (result != PRAGMA_OMP_CLAUSE_NONE) 24958 cp_lexer_consume_token (parser->lexer); 24959 24960 return result; 24961 } 24962 24963 /* Validate that a clause of the given type does not already exist. */ 24964 24965 static void 24966 check_no_duplicate_clause (tree clauses, enum omp_clause_code code, 24967 const char *name, location_t location) 24968 { 24969 tree c; 24970 24971 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) 24972 if (OMP_CLAUSE_CODE (c) == code) 24973 { 24974 error_at (location, "too many %qs clauses", name); 24975 break; 24976 } 24977 } 24978 24979 /* OpenMP 2.5: 24980 variable-list: 24981 identifier 24982 variable-list , identifier 24983 24984 In addition, we match a closing parenthesis. An opening parenthesis 24985 will have been consumed by the caller. 24986 24987 If KIND is nonzero, create the appropriate node and install the decl 24988 in OMP_CLAUSE_DECL and add the node to the head of the list. 24989 24990 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE; 24991 return the list created. */ 24992 24993 static tree 24994 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind, 24995 tree list) 24996 { 24997 cp_token *token; 24998 while (1) 24999 { 25000 tree name, decl; 25001 25002 token = cp_lexer_peek_token (parser->lexer); 25003 name = cp_parser_id_expression (parser, /*template_p=*/false, 25004 /*check_dependency_p=*/true, 25005 /*template_p=*/NULL, 25006 /*declarator_p=*/false, 25007 /*optional_p=*/false); 25008 if (name == error_mark_node) 25009 goto skip_comma; 25010 25011 decl = cp_parser_lookup_name_simple (parser, name, token->location); 25012 if (decl == error_mark_node) 25013 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, 25014 token->location); 25015 else if (kind != 0) 25016 { 25017 tree u = build_omp_clause (token->location, kind); 25018 OMP_CLAUSE_DECL (u) = decl; 25019 OMP_CLAUSE_CHAIN (u) = list; 25020 list = u; 25021 } 25022 else 25023 list = tree_cons (decl, NULL_TREE, list); 25024 25025 get_comma: 25026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) 25027 break; 25028 cp_lexer_consume_token (parser->lexer); 25029 } 25030 25031 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25032 { 25033 int ending; 25034 25035 /* Try to resync to an unnested comma. Copied from 25036 cp_parser_parenthesized_expression_list. */ 25037 skip_comma: 25038 ending = cp_parser_skip_to_closing_parenthesis (parser, 25039 /*recovering=*/true, 25040 /*or_comma=*/true, 25041 /*consume_paren=*/true); 25042 if (ending < 0) 25043 goto get_comma; 25044 } 25045 25046 return list; 25047 } 25048 25049 /* Similarly, but expect leading and trailing parenthesis. This is a very 25050 common case for omp clauses. */ 25051 25052 static tree 25053 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list) 25054 { 25055 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25056 return cp_parser_omp_var_list_no_open (parser, kind, list); 25057 return list; 25058 } 25059 25060 /* OpenMP 3.0: 25061 collapse ( constant-expression ) */ 25062 25063 static tree 25064 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location) 25065 { 25066 tree c, num; 25067 location_t loc; 25068 HOST_WIDE_INT n; 25069 25070 loc = cp_lexer_peek_token (parser->lexer)->location; 25071 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25072 return list; 25073 25074 num = cp_parser_constant_expression (parser, false, NULL); 25075 25076 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25077 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25078 /*or_comma=*/false, 25079 /*consume_paren=*/true); 25080 25081 if (num == error_mark_node) 25082 return list; 25083 num = fold_non_dependent_expr (num); 25084 if (!INTEGRAL_TYPE_P (TREE_TYPE (num)) 25085 || !host_integerp (num, 0) 25086 || (n = tree_low_cst (num, 0)) <= 0 25087 || (int) n != n) 25088 { 25089 error_at (loc, "collapse argument needs positive constant integer expression"); 25090 return list; 25091 } 25092 25093 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location); 25094 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE); 25095 OMP_CLAUSE_CHAIN (c) = list; 25096 OMP_CLAUSE_COLLAPSE_EXPR (c) = num; 25097 25098 return c; 25099 } 25100 25101 /* OpenMP 2.5: 25102 default ( shared | none ) */ 25103 25104 static tree 25105 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location) 25106 { 25107 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED; 25108 tree c; 25109 25110 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25111 return list; 25112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 25113 { 25114 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 25115 const char *p = IDENTIFIER_POINTER (id); 25116 25117 switch (p[0]) 25118 { 25119 case 'n': 25120 if (strcmp ("none", p) != 0) 25121 goto invalid_kind; 25122 kind = OMP_CLAUSE_DEFAULT_NONE; 25123 break; 25124 25125 case 's': 25126 if (strcmp ("shared", p) != 0) 25127 goto invalid_kind; 25128 kind = OMP_CLAUSE_DEFAULT_SHARED; 25129 break; 25130 25131 default: 25132 goto invalid_kind; 25133 } 25134 25135 cp_lexer_consume_token (parser->lexer); 25136 } 25137 else 25138 { 25139 invalid_kind: 25140 cp_parser_error (parser, "expected %<none%> or %<shared%>"); 25141 } 25142 25143 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25145 /*or_comma=*/false, 25146 /*consume_paren=*/true); 25147 25148 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED) 25149 return list; 25150 25151 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location); 25152 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT); 25153 OMP_CLAUSE_CHAIN (c) = list; 25154 OMP_CLAUSE_DEFAULT_KIND (c) = kind; 25155 25156 return c; 25157 } 25158 25159 /* OpenMP 3.1: 25160 final ( expression ) */ 25161 25162 static tree 25163 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location) 25164 { 25165 tree t, c; 25166 25167 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25168 return list; 25169 25170 t = cp_parser_condition (parser); 25171 25172 if (t == error_mark_node 25173 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25174 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25175 /*or_comma=*/false, 25176 /*consume_paren=*/true); 25177 25178 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location); 25179 25180 c = build_omp_clause (location, OMP_CLAUSE_FINAL); 25181 OMP_CLAUSE_FINAL_EXPR (c) = t; 25182 OMP_CLAUSE_CHAIN (c) = list; 25183 25184 return c; 25185 } 25186 25187 /* OpenMP 2.5: 25188 if ( expression ) */ 25189 25190 static tree 25191 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location) 25192 { 25193 tree t, c; 25194 25195 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25196 return list; 25197 25198 t = cp_parser_condition (parser); 25199 25200 if (t == error_mark_node 25201 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25202 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25203 /*or_comma=*/false, 25204 /*consume_paren=*/true); 25205 25206 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location); 25207 25208 c = build_omp_clause (location, OMP_CLAUSE_IF); 25209 OMP_CLAUSE_IF_EXPR (c) = t; 25210 OMP_CLAUSE_CHAIN (c) = list; 25211 25212 return c; 25213 } 25214 25215 /* OpenMP 3.1: 25216 mergeable */ 25217 25218 static tree 25219 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED, 25220 tree list, location_t location) 25221 { 25222 tree c; 25223 25224 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable", 25225 location); 25226 25227 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE); 25228 OMP_CLAUSE_CHAIN (c) = list; 25229 return c; 25230 } 25231 25232 /* OpenMP 2.5: 25233 nowait */ 25234 25235 static tree 25236 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, 25237 tree list, location_t location) 25238 { 25239 tree c; 25240 25241 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location); 25242 25243 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT); 25244 OMP_CLAUSE_CHAIN (c) = list; 25245 return c; 25246 } 25247 25248 /* OpenMP 2.5: 25249 num_threads ( expression ) */ 25250 25251 static tree 25252 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list, 25253 location_t location) 25254 { 25255 tree t, c; 25256 25257 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25258 return list; 25259 25260 t = cp_parser_expression (parser, false, NULL); 25261 25262 if (t == error_mark_node 25263 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25264 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25265 /*or_comma=*/false, 25266 /*consume_paren=*/true); 25267 25268 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, 25269 "num_threads", location); 25270 25271 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS); 25272 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t; 25273 OMP_CLAUSE_CHAIN (c) = list; 25274 25275 return c; 25276 } 25277 25278 /* OpenMP 2.5: 25279 ordered */ 25280 25281 static tree 25282 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, 25283 tree list, location_t location) 25284 { 25285 tree c; 25286 25287 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, 25288 "ordered", location); 25289 25290 c = build_omp_clause (location, OMP_CLAUSE_ORDERED); 25291 OMP_CLAUSE_CHAIN (c) = list; 25292 return c; 25293 } 25294 25295 /* OpenMP 2.5: 25296 reduction ( reduction-operator : variable-list ) 25297 25298 reduction-operator: 25299 One of: + * - & ^ | && || 25300 25301 OpenMP 3.1: 25302 25303 reduction-operator: 25304 One of: + * - & ^ | && || min max */ 25305 25306 static tree 25307 cp_parser_omp_clause_reduction (cp_parser *parser, tree list) 25308 { 25309 enum tree_code code; 25310 tree nlist, c; 25311 25312 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25313 return list; 25314 25315 switch (cp_lexer_peek_token (parser->lexer)->type) 25316 { 25317 case CPP_PLUS: 25318 code = PLUS_EXPR; 25319 break; 25320 case CPP_MULT: 25321 code = MULT_EXPR; 25322 break; 25323 case CPP_MINUS: 25324 code = MINUS_EXPR; 25325 break; 25326 case CPP_AND: 25327 code = BIT_AND_EXPR; 25328 break; 25329 case CPP_XOR: 25330 code = BIT_XOR_EXPR; 25331 break; 25332 case CPP_OR: 25333 code = BIT_IOR_EXPR; 25334 break; 25335 case CPP_AND_AND: 25336 code = TRUTH_ANDIF_EXPR; 25337 break; 25338 case CPP_OR_OR: 25339 code = TRUTH_ORIF_EXPR; 25340 break; 25341 case CPP_NAME: 25342 { 25343 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 25344 const char *p = IDENTIFIER_POINTER (id); 25345 25346 if (strcmp (p, "min") == 0) 25347 { 25348 code = MIN_EXPR; 25349 break; 25350 } 25351 if (strcmp (p, "max") == 0) 25352 { 25353 code = MAX_EXPR; 25354 break; 25355 } 25356 } 25357 /* FALLTHROUGH */ 25358 default: 25359 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, " 25360 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>"); 25361 resync_fail: 25362 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25363 /*or_comma=*/false, 25364 /*consume_paren=*/true); 25365 return list; 25366 } 25367 cp_lexer_consume_token (parser->lexer); 25368 25369 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)) 25370 goto resync_fail; 25371 25372 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list); 25373 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c)) 25374 OMP_CLAUSE_REDUCTION_CODE (c) = code; 25375 25376 return nlist; 25377 } 25378 25379 /* OpenMP 2.5: 25380 schedule ( schedule-kind ) 25381 schedule ( schedule-kind , expression ) 25382 25383 schedule-kind: 25384 static | dynamic | guided | runtime | auto */ 25385 25386 static tree 25387 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location) 25388 { 25389 tree c, t; 25390 25391 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 25392 return list; 25393 25394 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE); 25395 25396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 25397 { 25398 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 25399 const char *p = IDENTIFIER_POINTER (id); 25400 25401 switch (p[0]) 25402 { 25403 case 'd': 25404 if (strcmp ("dynamic", p) != 0) 25405 goto invalid_kind; 25406 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC; 25407 break; 25408 25409 case 'g': 25410 if (strcmp ("guided", p) != 0) 25411 goto invalid_kind; 25412 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED; 25413 break; 25414 25415 case 'r': 25416 if (strcmp ("runtime", p) != 0) 25417 goto invalid_kind; 25418 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME; 25419 break; 25420 25421 default: 25422 goto invalid_kind; 25423 } 25424 } 25425 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC)) 25426 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC; 25427 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)) 25428 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO; 25429 else 25430 goto invalid_kind; 25431 cp_lexer_consume_token (parser->lexer); 25432 25433 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 25434 { 25435 cp_token *token; 25436 cp_lexer_consume_token (parser->lexer); 25437 25438 token = cp_lexer_peek_token (parser->lexer); 25439 t = cp_parser_assignment_expression (parser, false, NULL); 25440 25441 if (t == error_mark_node) 25442 goto resync_fail; 25443 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME) 25444 error_at (token->location, "schedule %<runtime%> does not take " 25445 "a %<chunk_size%> parameter"); 25446 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO) 25447 error_at (token->location, "schedule %<auto%> does not take " 25448 "a %<chunk_size%> parameter"); 25449 else 25450 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t; 25451 25452 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 25453 goto resync_fail; 25454 } 25455 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN)) 25456 goto resync_fail; 25457 25458 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location); 25459 OMP_CLAUSE_CHAIN (c) = list; 25460 return c; 25461 25462 invalid_kind: 25463 cp_parser_error (parser, "invalid schedule kind"); 25464 resync_fail: 25465 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 25466 /*or_comma=*/false, 25467 /*consume_paren=*/true); 25468 return list; 25469 } 25470 25471 /* OpenMP 3.0: 25472 untied */ 25473 25474 static tree 25475 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED, 25476 tree list, location_t location) 25477 { 25478 tree c; 25479 25480 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location); 25481 25482 c = build_omp_clause (location, OMP_CLAUSE_UNTIED); 25483 OMP_CLAUSE_CHAIN (c) = list; 25484 return c; 25485 } 25486 25487 /* Parse all OpenMP clauses. The set clauses allowed by the directive 25488 is a bitmask in MASK. Return the list of clauses found; the result 25489 of clause default goes in *pdefault. */ 25490 25491 static tree 25492 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask, 25493 const char *where, cp_token *pragma_tok) 25494 { 25495 tree clauses = NULL; 25496 bool first = true; 25497 cp_token *token = NULL; 25498 25499 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)) 25500 { 25501 pragma_omp_clause c_kind; 25502 const char *c_name; 25503 tree prev = clauses; 25504 25505 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)) 25506 cp_lexer_consume_token (parser->lexer); 25507 25508 token = cp_lexer_peek_token (parser->lexer); 25509 c_kind = cp_parser_omp_clause_name (parser); 25510 first = false; 25511 25512 switch (c_kind) 25513 { 25514 case PRAGMA_OMP_CLAUSE_COLLAPSE: 25515 clauses = cp_parser_omp_clause_collapse (parser, clauses, 25516 token->location); 25517 c_name = "collapse"; 25518 break; 25519 case PRAGMA_OMP_CLAUSE_COPYIN: 25520 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses); 25521 c_name = "copyin"; 25522 break; 25523 case PRAGMA_OMP_CLAUSE_COPYPRIVATE: 25524 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE, 25525 clauses); 25526 c_name = "copyprivate"; 25527 break; 25528 case PRAGMA_OMP_CLAUSE_DEFAULT: 25529 clauses = cp_parser_omp_clause_default (parser, clauses, 25530 token->location); 25531 c_name = "default"; 25532 break; 25533 case PRAGMA_OMP_CLAUSE_FINAL: 25534 clauses = cp_parser_omp_clause_final (parser, clauses, token->location); 25535 c_name = "final"; 25536 break; 25537 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE: 25538 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE, 25539 clauses); 25540 c_name = "firstprivate"; 25541 break; 25542 case PRAGMA_OMP_CLAUSE_IF: 25543 clauses = cp_parser_omp_clause_if (parser, clauses, token->location); 25544 c_name = "if"; 25545 break; 25546 case PRAGMA_OMP_CLAUSE_LASTPRIVATE: 25547 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE, 25548 clauses); 25549 c_name = "lastprivate"; 25550 break; 25551 case PRAGMA_OMP_CLAUSE_MERGEABLE: 25552 clauses = cp_parser_omp_clause_mergeable (parser, clauses, 25553 token->location); 25554 c_name = "mergeable"; 25555 break; 25556 case PRAGMA_OMP_CLAUSE_NOWAIT: 25557 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location); 25558 c_name = "nowait"; 25559 break; 25560 case PRAGMA_OMP_CLAUSE_NUM_THREADS: 25561 clauses = cp_parser_omp_clause_num_threads (parser, clauses, 25562 token->location); 25563 c_name = "num_threads"; 25564 break; 25565 case PRAGMA_OMP_CLAUSE_ORDERED: 25566 clauses = cp_parser_omp_clause_ordered (parser, clauses, 25567 token->location); 25568 c_name = "ordered"; 25569 break; 25570 case PRAGMA_OMP_CLAUSE_PRIVATE: 25571 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, 25572 clauses); 25573 c_name = "private"; 25574 break; 25575 case PRAGMA_OMP_CLAUSE_REDUCTION: 25576 clauses = cp_parser_omp_clause_reduction (parser, clauses); 25577 c_name = "reduction"; 25578 break; 25579 case PRAGMA_OMP_CLAUSE_SCHEDULE: 25580 clauses = cp_parser_omp_clause_schedule (parser, clauses, 25581 token->location); 25582 c_name = "schedule"; 25583 break; 25584 case PRAGMA_OMP_CLAUSE_SHARED: 25585 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED, 25586 clauses); 25587 c_name = "shared"; 25588 break; 25589 case PRAGMA_OMP_CLAUSE_UNTIED: 25590 clauses = cp_parser_omp_clause_untied (parser, clauses, 25591 token->location); 25592 c_name = "nowait"; 25593 break; 25594 default: 25595 cp_parser_error (parser, "expected %<#pragma omp%> clause"); 25596 goto saw_error; 25597 } 25598 25599 if (((mask >> c_kind) & 1) == 0) 25600 { 25601 /* Remove the invalid clause(s) from the list to avoid 25602 confusing the rest of the compiler. */ 25603 clauses = prev; 25604 error_at (token->location, "%qs is not valid for %qs", c_name, where); 25605 } 25606 } 25607 saw_error: 25608 cp_parser_skip_to_pragma_eol (parser, pragma_tok); 25609 return finish_omp_clauses (clauses); 25610 } 25611 25612 /* OpenMP 2.5: 25613 structured-block: 25614 statement 25615 25616 In practice, we're also interested in adding the statement to an 25617 outer node. So it is convenient if we work around the fact that 25618 cp_parser_statement calls add_stmt. */ 25619 25620 static unsigned 25621 cp_parser_begin_omp_structured_block (cp_parser *parser) 25622 { 25623 unsigned save = parser->in_statement; 25624 25625 /* Only move the values to IN_OMP_BLOCK if they weren't false. 25626 This preserves the "not within loop or switch" style error messages 25627 for nonsense cases like 25628 void foo() { 25629 #pragma omp single 25630 break; 25631 } 25632 */ 25633 if (parser->in_statement) 25634 parser->in_statement = IN_OMP_BLOCK; 25635 25636 return save; 25637 } 25638 25639 static void 25640 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save) 25641 { 25642 parser->in_statement = save; 25643 } 25644 25645 static tree 25646 cp_parser_omp_structured_block (cp_parser *parser) 25647 { 25648 tree stmt = begin_omp_structured_block (); 25649 unsigned int save = cp_parser_begin_omp_structured_block (parser); 25650 25651 cp_parser_statement (parser, NULL_TREE, false, NULL); 25652 25653 cp_parser_end_omp_structured_block (parser, save); 25654 return finish_omp_structured_block (stmt); 25655 } 25656 25657 /* OpenMP 2.5: 25658 # pragma omp atomic new-line 25659 expression-stmt 25660 25661 expression-stmt: 25662 x binop= expr | x++ | ++x | x-- | --x 25663 binop: 25664 +, *, -, /, &, ^, |, <<, >> 25665 25666 where x is an lvalue expression with scalar type. 25667 25668 OpenMP 3.1: 25669 # pragma omp atomic new-line 25670 update-stmt 25671 25672 # pragma omp atomic read new-line 25673 read-stmt 25674 25675 # pragma omp atomic write new-line 25676 write-stmt 25677 25678 # pragma omp atomic update new-line 25679 update-stmt 25680 25681 # pragma omp atomic capture new-line 25682 capture-stmt 25683 25684 # pragma omp atomic capture new-line 25685 capture-block 25686 25687 read-stmt: 25688 v = x 25689 write-stmt: 25690 x = expr 25691 update-stmt: 25692 expression-stmt | x = x binop expr 25693 capture-stmt: 25694 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x 25695 capture-block: 25696 { v = x; update-stmt; } | { update-stmt; v = x; } 25697 25698 where x and v are lvalue expressions with scalar type. */ 25699 25700 static void 25701 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok) 25702 { 25703 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE; 25704 tree rhs1 = NULL_TREE, orig_lhs; 25705 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR; 25706 bool structured_block = false; 25707 25708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 25709 { 25710 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 25711 const char *p = IDENTIFIER_POINTER (id); 25712 25713 if (!strcmp (p, "read")) 25714 code = OMP_ATOMIC_READ; 25715 else if (!strcmp (p, "write")) 25716 code = NOP_EXPR; 25717 else if (!strcmp (p, "update")) 25718 code = OMP_ATOMIC; 25719 else if (!strcmp (p, "capture")) 25720 code = OMP_ATOMIC_CAPTURE_NEW; 25721 else 25722 p = NULL; 25723 if (p) 25724 cp_lexer_consume_token (parser->lexer); 25725 } 25726 cp_parser_require_pragma_eol (parser, pragma_tok); 25727 25728 switch (code) 25729 { 25730 case OMP_ATOMIC_READ: 25731 case NOP_EXPR: /* atomic write */ 25732 v = cp_parser_unary_expression (parser, /*address_p=*/false, 25733 /*cast_p=*/false, NULL); 25734 if (v == error_mark_node) 25735 goto saw_error; 25736 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 25737 goto saw_error; 25738 if (code == NOP_EXPR) 25739 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL); 25740 else 25741 lhs = cp_parser_unary_expression (parser, /*address_p=*/false, 25742 /*cast_p=*/false, NULL); 25743 if (lhs == error_mark_node) 25744 goto saw_error; 25745 if (code == NOP_EXPR) 25746 { 25747 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR 25748 opcode. */ 25749 code = OMP_ATOMIC; 25750 rhs = lhs; 25751 lhs = v; 25752 v = NULL_TREE; 25753 } 25754 goto done; 25755 case OMP_ATOMIC_CAPTURE_NEW: 25756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 25757 { 25758 cp_lexer_consume_token (parser->lexer); 25759 structured_block = true; 25760 } 25761 else 25762 { 25763 v = cp_parser_unary_expression (parser, /*address_p=*/false, 25764 /*cast_p=*/false, NULL); 25765 if (v == error_mark_node) 25766 goto saw_error; 25767 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 25768 goto saw_error; 25769 } 25770 default: 25771 break; 25772 } 25773 25774 restart: 25775 lhs = cp_parser_unary_expression (parser, /*address_p=*/false, 25776 /*cast_p=*/false, NULL); 25777 orig_lhs = lhs; 25778 switch (TREE_CODE (lhs)) 25779 { 25780 case ERROR_MARK: 25781 goto saw_error; 25782 25783 case POSTINCREMENT_EXPR: 25784 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block) 25785 code = OMP_ATOMIC_CAPTURE_OLD; 25786 /* FALLTHROUGH */ 25787 case PREINCREMENT_EXPR: 25788 lhs = TREE_OPERAND (lhs, 0); 25789 opcode = PLUS_EXPR; 25790 rhs = integer_one_node; 25791 break; 25792 25793 case POSTDECREMENT_EXPR: 25794 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block) 25795 code = OMP_ATOMIC_CAPTURE_OLD; 25796 /* FALLTHROUGH */ 25797 case PREDECREMENT_EXPR: 25798 lhs = TREE_OPERAND (lhs, 0); 25799 opcode = MINUS_EXPR; 25800 rhs = integer_one_node; 25801 break; 25802 25803 case COMPOUND_EXPR: 25804 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR 25805 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR 25806 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR 25807 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0) 25808 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND 25809 (TREE_OPERAND (lhs, 1), 0), 0))) 25810 == BOOLEAN_TYPE) 25811 /* Undo effects of boolean_increment for post {in,de}crement. */ 25812 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0); 25813 /* FALLTHRU */ 25814 case MODIFY_EXPR: 25815 if (TREE_CODE (lhs) == MODIFY_EXPR 25816 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE) 25817 { 25818 /* Undo effects of boolean_increment. */ 25819 if (integer_onep (TREE_OPERAND (lhs, 1))) 25820 { 25821 /* This is pre or post increment. */ 25822 rhs = TREE_OPERAND (lhs, 1); 25823 lhs = TREE_OPERAND (lhs, 0); 25824 opcode = NOP_EXPR; 25825 if (code == OMP_ATOMIC_CAPTURE_NEW 25826 && !structured_block 25827 && TREE_CODE (orig_lhs) == COMPOUND_EXPR) 25828 code = OMP_ATOMIC_CAPTURE_OLD; 25829 break; 25830 } 25831 } 25832 /* FALLTHRU */ 25833 default: 25834 switch (cp_lexer_peek_token (parser->lexer)->type) 25835 { 25836 case CPP_MULT_EQ: 25837 opcode = MULT_EXPR; 25838 break; 25839 case CPP_DIV_EQ: 25840 opcode = TRUNC_DIV_EXPR; 25841 break; 25842 case CPP_PLUS_EQ: 25843 opcode = PLUS_EXPR; 25844 break; 25845 case CPP_MINUS_EQ: 25846 opcode = MINUS_EXPR; 25847 break; 25848 case CPP_LSHIFT_EQ: 25849 opcode = LSHIFT_EXPR; 25850 break; 25851 case CPP_RSHIFT_EQ: 25852 opcode = RSHIFT_EXPR; 25853 break; 25854 case CPP_AND_EQ: 25855 opcode = BIT_AND_EXPR; 25856 break; 25857 case CPP_OR_EQ: 25858 opcode = BIT_IOR_EXPR; 25859 break; 25860 case CPP_XOR_EQ: 25861 opcode = BIT_XOR_EXPR; 25862 break; 25863 case CPP_EQ: 25864 if (structured_block || code == OMP_ATOMIC) 25865 { 25866 enum cp_parser_prec oprec; 25867 cp_token *token; 25868 cp_lexer_consume_token (parser->lexer); 25869 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false, 25870 /*cast_p=*/false, NULL); 25871 if (rhs1 == error_mark_node) 25872 goto saw_error; 25873 token = cp_lexer_peek_token (parser->lexer); 25874 switch (token->type) 25875 { 25876 case CPP_SEMICOLON: 25877 if (code == OMP_ATOMIC_CAPTURE_NEW) 25878 { 25879 code = OMP_ATOMIC_CAPTURE_OLD; 25880 v = lhs; 25881 lhs = NULL_TREE; 25882 lhs1 = rhs1; 25883 rhs1 = NULL_TREE; 25884 cp_lexer_consume_token (parser->lexer); 25885 goto restart; 25886 } 25887 cp_parser_error (parser, 25888 "invalid form of %<#pragma omp atomic%>"); 25889 goto saw_error; 25890 case CPP_MULT: 25891 opcode = MULT_EXPR; 25892 break; 25893 case CPP_DIV: 25894 opcode = TRUNC_DIV_EXPR; 25895 break; 25896 case CPP_PLUS: 25897 opcode = PLUS_EXPR; 25898 break; 25899 case CPP_MINUS: 25900 opcode = MINUS_EXPR; 25901 break; 25902 case CPP_LSHIFT: 25903 opcode = LSHIFT_EXPR; 25904 break; 25905 case CPP_RSHIFT: 25906 opcode = RSHIFT_EXPR; 25907 break; 25908 case CPP_AND: 25909 opcode = BIT_AND_EXPR; 25910 break; 25911 case CPP_OR: 25912 opcode = BIT_IOR_EXPR; 25913 break; 25914 case CPP_XOR: 25915 opcode = BIT_XOR_EXPR; 25916 break; 25917 default: 25918 cp_parser_error (parser, 25919 "invalid operator for %<#pragma omp atomic%>"); 25920 goto saw_error; 25921 } 25922 oprec = TOKEN_PRECEDENCE (token); 25923 gcc_assert (oprec != PREC_NOT_OPERATOR); 25924 if (commutative_tree_code (opcode)) 25925 oprec = (enum cp_parser_prec) (oprec - 1); 25926 cp_lexer_consume_token (parser->lexer); 25927 rhs = cp_parser_binary_expression (parser, false, false, 25928 oprec, NULL); 25929 if (rhs == error_mark_node) 25930 goto saw_error; 25931 goto stmt_done; 25932 } 25933 /* FALLTHROUGH */ 25934 default: 25935 cp_parser_error (parser, 25936 "invalid operator for %<#pragma omp atomic%>"); 25937 goto saw_error; 25938 } 25939 cp_lexer_consume_token (parser->lexer); 25940 25941 rhs = cp_parser_expression (parser, false, NULL); 25942 if (rhs == error_mark_node) 25943 goto saw_error; 25944 break; 25945 } 25946 stmt_done: 25947 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW) 25948 { 25949 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)) 25950 goto saw_error; 25951 v = cp_parser_unary_expression (parser, /*address_p=*/false, 25952 /*cast_p=*/false, NULL); 25953 if (v == error_mark_node) 25954 goto saw_error; 25955 if (!cp_parser_require (parser, CPP_EQ, RT_EQ)) 25956 goto saw_error; 25957 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false, 25958 /*cast_p=*/false, NULL); 25959 if (lhs1 == error_mark_node) 25960 goto saw_error; 25961 } 25962 if (structured_block) 25963 { 25964 cp_parser_consume_semicolon_at_end_of_statement (parser); 25965 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 25966 } 25967 done: 25968 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1); 25969 if (!structured_block) 25970 cp_parser_consume_semicolon_at_end_of_statement (parser); 25971 return; 25972 25973 saw_error: 25974 cp_parser_skip_to_end_of_block_or_statement (parser); 25975 if (structured_block) 25976 { 25977 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 25978 cp_lexer_consume_token (parser->lexer); 25979 else if (code == OMP_ATOMIC_CAPTURE_NEW) 25980 { 25981 cp_parser_skip_to_end_of_block_or_statement (parser); 25982 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 25983 cp_lexer_consume_token (parser->lexer); 25984 } 25985 } 25986 } 25987 25988 25989 /* OpenMP 2.5: 25990 # pragma omp barrier new-line */ 25991 25992 static void 25993 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok) 25994 { 25995 cp_parser_require_pragma_eol (parser, pragma_tok); 25996 finish_omp_barrier (); 25997 } 25998 25999 /* OpenMP 2.5: 26000 # pragma omp critical [(name)] new-line 26001 structured-block */ 26002 26003 static tree 26004 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok) 26005 { 26006 tree stmt, name = NULL; 26007 26008 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 26009 { 26010 cp_lexer_consume_token (parser->lexer); 26011 26012 name = cp_parser_identifier (parser); 26013 26014 if (name == error_mark_node 26015 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 26016 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 26017 /*or_comma=*/false, 26018 /*consume_paren=*/true); 26019 if (name == error_mark_node) 26020 name = NULL; 26021 } 26022 cp_parser_require_pragma_eol (parser, pragma_tok); 26023 26024 stmt = cp_parser_omp_structured_block (parser); 26025 return c_finish_omp_critical (input_location, stmt, name); 26026 } 26027 26028 /* OpenMP 2.5: 26029 # pragma omp flush flush-vars[opt] new-line 26030 26031 flush-vars: 26032 ( variable-list ) */ 26033 26034 static void 26035 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok) 26036 { 26037 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)) 26038 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL); 26039 cp_parser_require_pragma_eol (parser, pragma_tok); 26040 26041 finish_omp_flush (); 26042 } 26043 26044 /* Helper function, to parse omp for increment expression. */ 26045 26046 static tree 26047 cp_parser_omp_for_cond (cp_parser *parser, tree decl) 26048 { 26049 tree cond = cp_parser_binary_expression (parser, false, true, 26050 PREC_NOT_OPERATOR, NULL); 26051 if (cond == error_mark_node 26052 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 26053 { 26054 cp_parser_skip_to_end_of_statement (parser); 26055 return error_mark_node; 26056 } 26057 26058 switch (TREE_CODE (cond)) 26059 { 26060 case GT_EXPR: 26061 case GE_EXPR: 26062 case LT_EXPR: 26063 case LE_EXPR: 26064 break; 26065 default: 26066 return error_mark_node; 26067 } 26068 26069 /* If decl is an iterator, preserve LHS and RHS of the relational 26070 expr until finish_omp_for. */ 26071 if (decl 26072 && (type_dependent_expression_p (decl) 26073 || CLASS_TYPE_P (TREE_TYPE (decl)))) 26074 return cond; 26075 26076 return build_x_binary_op (TREE_CODE (cond), 26077 TREE_OPERAND (cond, 0), ERROR_MARK, 26078 TREE_OPERAND (cond, 1), ERROR_MARK, 26079 /*overload=*/NULL, tf_warning_or_error); 26080 } 26081 26082 /* Helper function, to parse omp for increment expression. */ 26083 26084 static tree 26085 cp_parser_omp_for_incr (cp_parser *parser, tree decl) 26086 { 26087 cp_token *token = cp_lexer_peek_token (parser->lexer); 26088 enum tree_code op; 26089 tree lhs, rhs; 26090 cp_id_kind idk; 26091 bool decl_first; 26092 26093 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS) 26094 { 26095 op = (token->type == CPP_PLUS_PLUS 26096 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR); 26097 cp_lexer_consume_token (parser->lexer); 26098 lhs = cp_parser_cast_expression (parser, false, false, NULL); 26099 if (lhs != decl) 26100 return error_mark_node; 26101 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE); 26102 } 26103 26104 lhs = cp_parser_primary_expression (parser, false, false, false, &idk); 26105 if (lhs != decl) 26106 return error_mark_node; 26107 26108 token = cp_lexer_peek_token (parser->lexer); 26109 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS) 26110 { 26111 op = (token->type == CPP_PLUS_PLUS 26112 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR); 26113 cp_lexer_consume_token (parser->lexer); 26114 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE); 26115 } 26116 26117 op = cp_parser_assignment_operator_opt (parser); 26118 if (op == ERROR_MARK) 26119 return error_mark_node; 26120 26121 if (op != NOP_EXPR) 26122 { 26123 rhs = cp_parser_assignment_expression (parser, false, NULL); 26124 rhs = build2 (op, TREE_TYPE (decl), decl, rhs); 26125 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs); 26126 } 26127 26128 lhs = cp_parser_binary_expression (parser, false, false, 26129 PREC_ADDITIVE_EXPRESSION, NULL); 26130 token = cp_lexer_peek_token (parser->lexer); 26131 decl_first = lhs == decl; 26132 if (decl_first) 26133 lhs = NULL_TREE; 26134 if (token->type != CPP_PLUS 26135 && token->type != CPP_MINUS) 26136 return error_mark_node; 26137 26138 do 26139 { 26140 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR; 26141 cp_lexer_consume_token (parser->lexer); 26142 rhs = cp_parser_binary_expression (parser, false, false, 26143 PREC_ADDITIVE_EXPRESSION, NULL); 26144 token = cp_lexer_peek_token (parser->lexer); 26145 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first) 26146 { 26147 if (lhs == NULL_TREE) 26148 { 26149 if (op == PLUS_EXPR) 26150 lhs = rhs; 26151 else 26152 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error); 26153 } 26154 else 26155 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK, 26156 NULL, tf_warning_or_error); 26157 } 26158 } 26159 while (token->type == CPP_PLUS || token->type == CPP_MINUS); 26160 26161 if (!decl_first) 26162 { 26163 if (rhs != decl || op == MINUS_EXPR) 26164 return error_mark_node; 26165 rhs = build2 (op, TREE_TYPE (decl), lhs, decl); 26166 } 26167 else 26168 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs); 26169 26170 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs); 26171 } 26172 26173 /* Parse the restricted form of the for statement allowed by OpenMP. */ 26174 26175 static tree 26176 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses) 26177 { 26178 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret; 26179 tree real_decl, initv, condv, incrv, declv; 26180 tree this_pre_body, cl; 26181 location_t loc_first; 26182 bool collapse_err = false; 26183 int i, collapse = 1, nbraces = 0; 26184 VEC(tree,gc) *for_block = make_tree_vector (); 26185 26186 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl)) 26187 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE) 26188 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0); 26189 26190 gcc_assert (collapse >= 1); 26191 26192 declv = make_tree_vec (collapse); 26193 initv = make_tree_vec (collapse); 26194 condv = make_tree_vec (collapse); 26195 incrv = make_tree_vec (collapse); 26196 26197 loc_first = cp_lexer_peek_token (parser->lexer)->location; 26198 26199 for (i = 0; i < collapse; i++) 26200 { 26201 int bracecount = 0; 26202 bool add_private_clause = false; 26203 location_t loc; 26204 26205 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR)) 26206 { 26207 cp_parser_error (parser, "for statement expected"); 26208 return NULL; 26209 } 26210 loc = cp_lexer_consume_token (parser->lexer)->location; 26211 26212 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN)) 26213 return NULL; 26214 26215 init = decl = real_decl = NULL; 26216 this_pre_body = push_stmt_list (); 26217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 26218 { 26219 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too): 26220 26221 init-expr: 26222 var = lb 26223 integer-type var = lb 26224 random-access-iterator-type var = lb 26225 pointer-type var = lb 26226 */ 26227 cp_decl_specifier_seq type_specifiers; 26228 26229 /* First, try to parse as an initialized declaration. See 26230 cp_parser_condition, from whence the bulk of this is copied. */ 26231 26232 cp_parser_parse_tentatively (parser); 26233 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true, 26234 /*is_trailing_return=*/false, 26235 &type_specifiers); 26236 if (cp_parser_parse_definitely (parser)) 26237 { 26238 /* If parsing a type specifier seq succeeded, then this 26239 MUST be a initialized declaration. */ 26240 tree asm_specification, attributes; 26241 cp_declarator *declarator; 26242 26243 declarator = cp_parser_declarator (parser, 26244 CP_PARSER_DECLARATOR_NAMED, 26245 /*ctor_dtor_or_conv_p=*/NULL, 26246 /*parenthesized_p=*/NULL, 26247 /*member_p=*/false); 26248 attributes = cp_parser_attributes_opt (parser); 26249 asm_specification = cp_parser_asm_specification_opt (parser); 26250 26251 if (declarator == cp_error_declarator) 26252 cp_parser_skip_to_end_of_statement (parser); 26253 26254 else 26255 { 26256 tree pushed_scope, auto_node; 26257 26258 decl = start_decl (declarator, &type_specifiers, 26259 SD_INITIALIZED, attributes, 26260 /*prefix_attributes=*/NULL_TREE, 26261 &pushed_scope); 26262 26263 auto_node = type_uses_auto (TREE_TYPE (decl)); 26264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)) 26265 { 26266 if (cp_lexer_next_token_is (parser->lexer, 26267 CPP_OPEN_PAREN)) 26268 error ("parenthesized initialization is not allowed in " 26269 "OpenMP %<for%> loop"); 26270 else 26271 /* Trigger an error. */ 26272 cp_parser_require (parser, CPP_EQ, RT_EQ); 26273 26274 init = error_mark_node; 26275 cp_parser_skip_to_end_of_statement (parser); 26276 } 26277 else if (CLASS_TYPE_P (TREE_TYPE (decl)) 26278 || type_dependent_expression_p (decl) 26279 || auto_node) 26280 { 26281 bool is_direct_init, is_non_constant_init; 26282 26283 init = cp_parser_initializer (parser, 26284 &is_direct_init, 26285 &is_non_constant_init); 26286 26287 if (auto_node) 26288 { 26289 TREE_TYPE (decl) 26290 = do_auto_deduction (TREE_TYPE (decl), init, 26291 auto_node); 26292 26293 if (!CLASS_TYPE_P (TREE_TYPE (decl)) 26294 && !type_dependent_expression_p (decl)) 26295 goto non_class; 26296 } 26297 26298 cp_finish_decl (decl, init, !is_non_constant_init, 26299 asm_specification, 26300 LOOKUP_ONLYCONVERTING); 26301 if (CLASS_TYPE_P (TREE_TYPE (decl))) 26302 { 26303 VEC_safe_push (tree, gc, for_block, this_pre_body); 26304 init = NULL_TREE; 26305 } 26306 else 26307 init = pop_stmt_list (this_pre_body); 26308 this_pre_body = NULL_TREE; 26309 } 26310 else 26311 { 26312 /* Consume '='. */ 26313 cp_lexer_consume_token (parser->lexer); 26314 init = cp_parser_assignment_expression (parser, false, NULL); 26315 26316 non_class: 26317 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE) 26318 init = error_mark_node; 26319 else 26320 cp_finish_decl (decl, NULL_TREE, 26321 /*init_const_expr_p=*/false, 26322 asm_specification, 26323 LOOKUP_ONLYCONVERTING); 26324 } 26325 26326 if (pushed_scope) 26327 pop_scope (pushed_scope); 26328 } 26329 } 26330 else 26331 { 26332 cp_id_kind idk; 26333 /* If parsing a type specifier sequence failed, then 26334 this MUST be a simple expression. */ 26335 cp_parser_parse_tentatively (parser); 26336 decl = cp_parser_primary_expression (parser, false, false, 26337 false, &idk); 26338 if (!cp_parser_error_occurred (parser) 26339 && decl 26340 && DECL_P (decl) 26341 && CLASS_TYPE_P (TREE_TYPE (decl))) 26342 { 26343 tree rhs; 26344 26345 cp_parser_parse_definitely (parser); 26346 cp_parser_require (parser, CPP_EQ, RT_EQ); 26347 rhs = cp_parser_assignment_expression (parser, false, NULL); 26348 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR, 26349 rhs, 26350 tf_warning_or_error)); 26351 add_private_clause = true; 26352 } 26353 else 26354 { 26355 decl = NULL; 26356 cp_parser_abort_tentative_parse (parser); 26357 init = cp_parser_expression (parser, false, NULL); 26358 if (init) 26359 { 26360 if (TREE_CODE (init) == MODIFY_EXPR 26361 || TREE_CODE (init) == MODOP_EXPR) 26362 real_decl = TREE_OPERAND (init, 0); 26363 } 26364 } 26365 } 26366 } 26367 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 26368 if (this_pre_body) 26369 { 26370 this_pre_body = pop_stmt_list (this_pre_body); 26371 if (pre_body) 26372 { 26373 tree t = pre_body; 26374 pre_body = push_stmt_list (); 26375 add_stmt (t); 26376 add_stmt (this_pre_body); 26377 pre_body = pop_stmt_list (pre_body); 26378 } 26379 else 26380 pre_body = this_pre_body; 26381 } 26382 26383 if (decl) 26384 real_decl = decl; 26385 if (par_clauses != NULL && real_decl != NULL_TREE) 26386 { 26387 tree *c; 26388 for (c = par_clauses; *c ; ) 26389 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE 26390 && OMP_CLAUSE_DECL (*c) == real_decl) 26391 { 26392 error_at (loc, "iteration variable %qD" 26393 " should not be firstprivate", real_decl); 26394 *c = OMP_CLAUSE_CHAIN (*c); 26395 } 26396 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE 26397 && OMP_CLAUSE_DECL (*c) == real_decl) 26398 { 26399 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES, 26400 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */ 26401 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE); 26402 OMP_CLAUSE_DECL (l) = real_decl; 26403 OMP_CLAUSE_CHAIN (l) = clauses; 26404 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c); 26405 clauses = l; 26406 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED); 26407 CP_OMP_CLAUSE_INFO (*c) = NULL; 26408 add_private_clause = false; 26409 } 26410 else 26411 { 26412 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE 26413 && OMP_CLAUSE_DECL (*c) == real_decl) 26414 add_private_clause = false; 26415 c = &OMP_CLAUSE_CHAIN (*c); 26416 } 26417 } 26418 26419 if (add_private_clause) 26420 { 26421 tree c; 26422 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c)) 26423 { 26424 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE 26425 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE) 26426 && OMP_CLAUSE_DECL (c) == decl) 26427 break; 26428 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE 26429 && OMP_CLAUSE_DECL (c) == decl) 26430 error_at (loc, "iteration variable %qD " 26431 "should not be firstprivate", 26432 decl); 26433 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION 26434 && OMP_CLAUSE_DECL (c) == decl) 26435 error_at (loc, "iteration variable %qD should not be reduction", 26436 decl); 26437 } 26438 if (c == NULL) 26439 { 26440 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE); 26441 OMP_CLAUSE_DECL (c) = decl; 26442 c = finish_omp_clauses (c); 26443 if (c) 26444 { 26445 OMP_CLAUSE_CHAIN (c) = clauses; 26446 clauses = c; 26447 } 26448 } 26449 } 26450 26451 cond = NULL; 26452 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)) 26453 cond = cp_parser_omp_for_cond (parser, decl); 26454 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 26455 26456 incr = NULL; 26457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)) 26458 { 26459 /* If decl is an iterator, preserve the operator on decl 26460 until finish_omp_for. */ 26461 if (real_decl 26462 && ((processing_template_decl 26463 && !POINTER_TYPE_P (TREE_TYPE (real_decl))) 26464 || CLASS_TYPE_P (TREE_TYPE (real_decl)))) 26465 incr = cp_parser_omp_for_incr (parser, real_decl); 26466 else 26467 incr = cp_parser_expression (parser, false, NULL); 26468 } 26469 26470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)) 26471 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true, 26472 /*or_comma=*/false, 26473 /*consume_paren=*/true); 26474 26475 TREE_VEC_ELT (declv, i) = decl; 26476 TREE_VEC_ELT (initv, i) = init; 26477 TREE_VEC_ELT (condv, i) = cond; 26478 TREE_VEC_ELT (incrv, i) = incr; 26479 26480 if (i == collapse - 1) 26481 break; 26482 26483 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed 26484 in between the collapsed for loops to be still considered perfectly 26485 nested. Hopefully the final version clarifies this. 26486 For now handle (multiple) {'s and empty statements. */ 26487 cp_parser_parse_tentatively (parser); 26488 do 26489 { 26490 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR)) 26491 break; 26492 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 26493 { 26494 cp_lexer_consume_token (parser->lexer); 26495 bracecount++; 26496 } 26497 else if (bracecount 26498 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 26499 cp_lexer_consume_token (parser->lexer); 26500 else 26501 { 26502 loc = cp_lexer_peek_token (parser->lexer)->location; 26503 error_at (loc, "not enough collapsed for loops"); 26504 collapse_err = true; 26505 cp_parser_abort_tentative_parse (parser); 26506 declv = NULL_TREE; 26507 break; 26508 } 26509 } 26510 while (1); 26511 26512 if (declv) 26513 { 26514 cp_parser_parse_definitely (parser); 26515 nbraces += bracecount; 26516 } 26517 } 26518 26519 /* Note that we saved the original contents of this flag when we entered 26520 the structured block, and so we don't need to re-save it here. */ 26521 parser->in_statement = IN_OMP_FOR; 26522 26523 /* Note that the grammar doesn't call for a structured block here, 26524 though the loop as a whole is a structured block. */ 26525 body = push_stmt_list (); 26526 cp_parser_statement (parser, NULL_TREE, false, NULL); 26527 body = pop_stmt_list (body); 26528 26529 if (declv == NULL_TREE) 26530 ret = NULL_TREE; 26531 else 26532 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body, 26533 pre_body, clauses); 26534 26535 while (nbraces) 26536 { 26537 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)) 26538 { 26539 cp_lexer_consume_token (parser->lexer); 26540 nbraces--; 26541 } 26542 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)) 26543 cp_lexer_consume_token (parser->lexer); 26544 else 26545 { 26546 if (!collapse_err) 26547 { 26548 error_at (cp_lexer_peek_token (parser->lexer)->location, 26549 "collapsed loops not perfectly nested"); 26550 } 26551 collapse_err = true; 26552 cp_parser_statement_seq_opt (parser, NULL); 26553 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)) 26554 break; 26555 } 26556 } 26557 26558 while (!VEC_empty (tree, for_block)) 26559 add_stmt (pop_stmt_list (VEC_pop (tree, for_block))); 26560 release_tree_vector (for_block); 26561 26562 return ret; 26563 } 26564 26565 /* OpenMP 2.5: 26566 #pragma omp for for-clause[optseq] new-line 26567 for-loop */ 26568 26569 #define OMP_FOR_CLAUSE_MASK \ 26570 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26571 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26572 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ 26573 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 26574 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \ 26575 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \ 26576 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \ 26577 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE)) 26578 26579 static tree 26580 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok) 26581 { 26582 tree clauses, sb, ret; 26583 unsigned int save; 26584 26585 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK, 26586 "#pragma omp for", pragma_tok); 26587 26588 sb = begin_omp_structured_block (); 26589 save = cp_parser_begin_omp_structured_block (parser); 26590 26591 ret = cp_parser_omp_for_loop (parser, clauses, NULL); 26592 26593 cp_parser_end_omp_structured_block (parser, save); 26594 add_stmt (finish_omp_structured_block (sb)); 26595 26596 return ret; 26597 } 26598 26599 /* OpenMP 2.5: 26600 # pragma omp master new-line 26601 structured-block */ 26602 26603 static tree 26604 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok) 26605 { 26606 cp_parser_require_pragma_eol (parser, pragma_tok); 26607 return c_finish_omp_master (input_location, 26608 cp_parser_omp_structured_block (parser)); 26609 } 26610 26611 /* OpenMP 2.5: 26612 # pragma omp ordered new-line 26613 structured-block */ 26614 26615 static tree 26616 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok) 26617 { 26618 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 26619 cp_parser_require_pragma_eol (parser, pragma_tok); 26620 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser)); 26621 } 26622 26623 /* OpenMP 2.5: 26624 26625 section-scope: 26626 { section-sequence } 26627 26628 section-sequence: 26629 section-directive[opt] structured-block 26630 section-sequence section-directive structured-block */ 26631 26632 static tree 26633 cp_parser_omp_sections_scope (cp_parser *parser) 26634 { 26635 tree stmt, substmt; 26636 bool error_suppress = false; 26637 cp_token *tok; 26638 26639 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE)) 26640 return NULL_TREE; 26641 26642 stmt = push_stmt_list (); 26643 26644 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION) 26645 { 26646 unsigned save; 26647 26648 substmt = begin_omp_structured_block (); 26649 save = cp_parser_begin_omp_structured_block (parser); 26650 26651 while (1) 26652 { 26653 cp_parser_statement (parser, NULL_TREE, false, NULL); 26654 26655 tok = cp_lexer_peek_token (parser->lexer); 26656 if (tok->pragma_kind == PRAGMA_OMP_SECTION) 26657 break; 26658 if (tok->type == CPP_CLOSE_BRACE) 26659 break; 26660 if (tok->type == CPP_EOF) 26661 break; 26662 } 26663 26664 cp_parser_end_omp_structured_block (parser, save); 26665 substmt = finish_omp_structured_block (substmt); 26666 substmt = build1 (OMP_SECTION, void_type_node, substmt); 26667 add_stmt (substmt); 26668 } 26669 26670 while (1) 26671 { 26672 tok = cp_lexer_peek_token (parser->lexer); 26673 if (tok->type == CPP_CLOSE_BRACE) 26674 break; 26675 if (tok->type == CPP_EOF) 26676 break; 26677 26678 if (tok->pragma_kind == PRAGMA_OMP_SECTION) 26679 { 26680 cp_lexer_consume_token (parser->lexer); 26681 cp_parser_require_pragma_eol (parser, tok); 26682 error_suppress = false; 26683 } 26684 else if (!error_suppress) 26685 { 26686 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>"); 26687 error_suppress = true; 26688 } 26689 26690 substmt = cp_parser_omp_structured_block (parser); 26691 substmt = build1 (OMP_SECTION, void_type_node, substmt); 26692 add_stmt (substmt); 26693 } 26694 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE); 26695 26696 substmt = pop_stmt_list (stmt); 26697 26698 stmt = make_node (OMP_SECTIONS); 26699 TREE_TYPE (stmt) = void_type_node; 26700 OMP_SECTIONS_BODY (stmt) = substmt; 26701 26702 add_stmt (stmt); 26703 return stmt; 26704 } 26705 26706 /* OpenMP 2.5: 26707 # pragma omp sections sections-clause[optseq] newline 26708 sections-scope */ 26709 26710 #define OMP_SECTIONS_CLAUSE_MASK \ 26711 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26712 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26713 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \ 26714 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 26715 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 26716 26717 static tree 26718 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok) 26719 { 26720 tree clauses, ret; 26721 26722 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK, 26723 "#pragma omp sections", pragma_tok); 26724 26725 ret = cp_parser_omp_sections_scope (parser); 26726 if (ret) 26727 OMP_SECTIONS_CLAUSES (ret) = clauses; 26728 26729 return ret; 26730 } 26731 26732 /* OpenMP 2.5: 26733 # pragma parallel parallel-clause new-line 26734 # pragma parallel for parallel-for-clause new-line 26735 # pragma parallel sections parallel-sections-clause new-line */ 26736 26737 #define OMP_PARALLEL_CLAUSE_MASK \ 26738 ( (1u << PRAGMA_OMP_CLAUSE_IF) \ 26739 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26740 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26741 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ 26742 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \ 26743 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \ 26744 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \ 26745 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS)) 26746 26747 static tree 26748 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok) 26749 { 26750 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL; 26751 const char *p_name = "#pragma omp parallel"; 26752 tree stmt, clauses, par_clause, ws_clause, block; 26753 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK; 26754 unsigned int save; 26755 location_t loc = cp_lexer_peek_token (parser->lexer)->location; 26756 26757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR)) 26758 { 26759 cp_lexer_consume_token (parser->lexer); 26760 p_kind = PRAGMA_OMP_PARALLEL_FOR; 26761 p_name = "#pragma omp parallel for"; 26762 mask |= OMP_FOR_CLAUSE_MASK; 26763 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); 26764 } 26765 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)) 26766 { 26767 tree id = cp_lexer_peek_token (parser->lexer)->u.value; 26768 const char *p = IDENTIFIER_POINTER (id); 26769 if (strcmp (p, "sections") == 0) 26770 { 26771 cp_lexer_consume_token (parser->lexer); 26772 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS; 26773 p_name = "#pragma omp parallel sections"; 26774 mask |= OMP_SECTIONS_CLAUSE_MASK; 26775 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT); 26776 } 26777 } 26778 26779 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok); 26780 block = begin_omp_parallel (); 26781 save = cp_parser_begin_omp_structured_block (parser); 26782 26783 switch (p_kind) 26784 { 26785 case PRAGMA_OMP_PARALLEL: 26786 cp_parser_statement (parser, NULL_TREE, false, NULL); 26787 par_clause = clauses; 26788 break; 26789 26790 case PRAGMA_OMP_PARALLEL_FOR: 26791 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause); 26792 cp_parser_omp_for_loop (parser, ws_clause, &par_clause); 26793 break; 26794 26795 case PRAGMA_OMP_PARALLEL_SECTIONS: 26796 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause); 26797 stmt = cp_parser_omp_sections_scope (parser); 26798 if (stmt) 26799 OMP_SECTIONS_CLAUSES (stmt) = ws_clause; 26800 break; 26801 26802 default: 26803 gcc_unreachable (); 26804 } 26805 26806 cp_parser_end_omp_structured_block (parser, save); 26807 stmt = finish_omp_parallel (par_clause, block); 26808 if (p_kind != PRAGMA_OMP_PARALLEL) 26809 OMP_PARALLEL_COMBINED (stmt) = 1; 26810 return stmt; 26811 } 26812 26813 /* OpenMP 2.5: 26814 # pragma omp single single-clause[optseq] new-line 26815 structured-block */ 26816 26817 #define OMP_SINGLE_CLAUSE_MASK \ 26818 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26819 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26820 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \ 26821 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)) 26822 26823 static tree 26824 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok) 26825 { 26826 tree stmt = make_node (OMP_SINGLE); 26827 TREE_TYPE (stmt) = void_type_node; 26828 26829 OMP_SINGLE_CLAUSES (stmt) 26830 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK, 26831 "#pragma omp single", pragma_tok); 26832 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser); 26833 26834 return add_stmt (stmt); 26835 } 26836 26837 /* OpenMP 3.0: 26838 # pragma omp task task-clause[optseq] new-line 26839 structured-block */ 26840 26841 #define OMP_TASK_CLAUSE_MASK \ 26842 ( (1u << PRAGMA_OMP_CLAUSE_IF) \ 26843 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \ 26844 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \ 26845 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \ 26846 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \ 26847 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \ 26848 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \ 26849 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE)) 26850 26851 static tree 26852 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok) 26853 { 26854 tree clauses, block; 26855 unsigned int save; 26856 26857 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK, 26858 "#pragma omp task", pragma_tok); 26859 block = begin_omp_task (); 26860 save = cp_parser_begin_omp_structured_block (parser); 26861 cp_parser_statement (parser, NULL_TREE, false, NULL); 26862 cp_parser_end_omp_structured_block (parser, save); 26863 return finish_omp_task (clauses, block); 26864 } 26865 26866 /* OpenMP 3.0: 26867 # pragma omp taskwait new-line */ 26868 26869 static void 26870 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok) 26871 { 26872 cp_parser_require_pragma_eol (parser, pragma_tok); 26873 finish_omp_taskwait (); 26874 } 26875 26876 /* OpenMP 3.1: 26877 # pragma omp taskyield new-line */ 26878 26879 static void 26880 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok) 26881 { 26882 cp_parser_require_pragma_eol (parser, pragma_tok); 26883 finish_omp_taskyield (); 26884 } 26885 26886 /* OpenMP 2.5: 26887 # pragma omp threadprivate (variable-list) */ 26888 26889 static void 26890 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok) 26891 { 26892 tree vars; 26893 26894 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL); 26895 cp_parser_require_pragma_eol (parser, pragma_tok); 26896 26897 finish_omp_threadprivate (vars); 26898 } 26899 26900 /* Main entry point to OpenMP statement pragmas. */ 26901 26902 static void 26903 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok) 26904 { 26905 tree stmt; 26906 26907 switch (pragma_tok->pragma_kind) 26908 { 26909 case PRAGMA_OMP_ATOMIC: 26910 cp_parser_omp_atomic (parser, pragma_tok); 26911 return; 26912 case PRAGMA_OMP_CRITICAL: 26913 stmt = cp_parser_omp_critical (parser, pragma_tok); 26914 break; 26915 case PRAGMA_OMP_FOR: 26916 stmt = cp_parser_omp_for (parser, pragma_tok); 26917 break; 26918 case PRAGMA_OMP_MASTER: 26919 stmt = cp_parser_omp_master (parser, pragma_tok); 26920 break; 26921 case PRAGMA_OMP_ORDERED: 26922 stmt = cp_parser_omp_ordered (parser, pragma_tok); 26923 break; 26924 case PRAGMA_OMP_PARALLEL: 26925 stmt = cp_parser_omp_parallel (parser, pragma_tok); 26926 break; 26927 case PRAGMA_OMP_SECTIONS: 26928 stmt = cp_parser_omp_sections (parser, pragma_tok); 26929 break; 26930 case PRAGMA_OMP_SINGLE: 26931 stmt = cp_parser_omp_single (parser, pragma_tok); 26932 break; 26933 case PRAGMA_OMP_TASK: 26934 stmt = cp_parser_omp_task (parser, pragma_tok); 26935 break; 26936 default: 26937 gcc_unreachable (); 26938 } 26939 26940 if (stmt) 26941 SET_EXPR_LOCATION (stmt, pragma_tok->location); 26942 } 26943 26944 /* Transactional Memory parsing routines. */ 26945 26946 /* Parse a transaction attribute. 26947 26948 txn-attribute: 26949 attribute 26950 [ [ identifier ] ] 26951 26952 ??? Simplify this when C++0x bracket attributes are 26953 implemented properly. */ 26954 26955 static tree 26956 cp_parser_txn_attribute_opt (cp_parser *parser) 26957 { 26958 cp_token *token; 26959 tree attr_name, attr = NULL; 26960 26961 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)) 26962 return cp_parser_attributes_opt (parser); 26963 26964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE)) 26965 return NULL_TREE; 26966 cp_lexer_consume_token (parser->lexer); 26967 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE)) 26968 goto error1; 26969 26970 token = cp_lexer_peek_token (parser->lexer); 26971 if (token->type == CPP_NAME || token->type == CPP_KEYWORD) 26972 { 26973 token = cp_lexer_consume_token (parser->lexer); 26974 26975 attr_name = (token->type == CPP_KEYWORD 26976 /* For keywords, use the canonical spelling, 26977 not the parsed identifier. */ 26978 ? ridpointers[(int) token->keyword] 26979 : token->u.value); 26980 attr = build_tree_list (attr_name, NULL_TREE); 26981 } 26982 else 26983 cp_parser_error (parser, "expected identifier"); 26984 26985 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 26986 error1: 26987 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE); 26988 return attr; 26989 } 26990 26991 /* Parse a __transaction_atomic or __transaction_relaxed statement. 26992 26993 transaction-statement: 26994 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt] 26995 compound-statement 26996 __transaction_relaxed txn-noexcept-spec[opt] compound-statement 26997 */ 26998 26999 static tree 27000 cp_parser_transaction (cp_parser *parser, enum rid keyword) 27001 { 27002 unsigned char old_in = parser->in_transaction; 27003 unsigned char this_in = 1, new_in; 27004 cp_token *token; 27005 tree stmt, attrs, noex; 27006 27007 gcc_assert (keyword == RID_TRANSACTION_ATOMIC 27008 || keyword == RID_TRANSACTION_RELAXED); 27009 token = cp_parser_require_keyword (parser, keyword, 27010 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC 27011 : RT_TRANSACTION_RELAXED)); 27012 gcc_assert (token != NULL); 27013 27014 if (keyword == RID_TRANSACTION_RELAXED) 27015 this_in |= TM_STMT_ATTR_RELAXED; 27016 else 27017 { 27018 attrs = cp_parser_txn_attribute_opt (parser); 27019 if (attrs) 27020 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER); 27021 } 27022 27023 /* Parse a noexcept specification. */ 27024 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true); 27025 27026 /* Keep track if we're in the lexical scope of an outer transaction. */ 27027 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER); 27028 27029 stmt = begin_transaction_stmt (token->location, NULL, this_in); 27030 27031 parser->in_transaction = new_in; 27032 cp_parser_compound_statement (parser, NULL, false, false); 27033 parser->in_transaction = old_in; 27034 27035 finish_transaction_stmt (stmt, NULL, this_in, noex); 27036 27037 return stmt; 27038 } 27039 27040 /* Parse a __transaction_atomic or __transaction_relaxed expression. 27041 27042 transaction-expression: 27043 __transaction_atomic txn-noexcept-spec[opt] ( expression ) 27044 __transaction_relaxed txn-noexcept-spec[opt] ( expression ) 27045 */ 27046 27047 static tree 27048 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword) 27049 { 27050 unsigned char old_in = parser->in_transaction; 27051 unsigned char this_in = 1; 27052 cp_token *token; 27053 tree expr, noex; 27054 bool noex_expr; 27055 27056 gcc_assert (keyword == RID_TRANSACTION_ATOMIC 27057 || keyword == RID_TRANSACTION_RELAXED); 27058 27059 if (!flag_tm) 27060 error (keyword == RID_TRANSACTION_RELAXED 27061 ? G_("%<__transaction_relaxed%> without transactional memory " 27062 "support enabled") 27063 : G_("%<__transaction_atomic%> without transactional memory " 27064 "support enabled")); 27065 27066 token = cp_parser_require_keyword (parser, keyword, 27067 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC 27068 : RT_TRANSACTION_RELAXED)); 27069 gcc_assert (token != NULL); 27070 27071 if (keyword == RID_TRANSACTION_RELAXED) 27072 this_in |= TM_STMT_ATTR_RELAXED; 27073 27074 /* Set this early. This might mean that we allow transaction_cancel in 27075 an expression that we find out later actually has to be a constexpr. 27076 However, we expect that cxx_constant_value will be able to deal with 27077 this; also, if the noexcept has no constexpr, then what we parse next 27078 really is a transaction's body. */ 27079 parser->in_transaction = this_in; 27080 27081 /* Parse a noexcept specification. */ 27082 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr, 27083 true); 27084 27085 if (!noex || !noex_expr 27086 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN) 27087 { 27088 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN); 27089 27090 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL); 27091 finish_parenthesized_expr (expr); 27092 27093 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN); 27094 } 27095 else 27096 { 27097 /* The only expression that is available got parsed for the noexcept 27098 already. noexcept is true then. */ 27099 expr = noex; 27100 noex = boolean_true_node; 27101 } 27102 27103 expr = build_transaction_expr (token->location, expr, this_in, noex); 27104 parser->in_transaction = old_in; 27105 27106 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION)) 27107 return error_mark_node; 27108 27109 return (flag_tm ? expr : error_mark_node); 27110 } 27111 27112 /* Parse a function-transaction-block. 27113 27114 function-transaction-block: 27115 __transaction_atomic txn-attribute[opt] ctor-initializer[opt] 27116 function-body 27117 __transaction_atomic txn-attribute[opt] function-try-block 27118 __transaction_relaxed ctor-initializer[opt] function-body 27119 __transaction_relaxed function-try-block 27120 */ 27121 27122 static bool 27123 cp_parser_function_transaction (cp_parser *parser, enum rid keyword) 27124 { 27125 unsigned char old_in = parser->in_transaction; 27126 unsigned char new_in = 1; 27127 tree compound_stmt, stmt, attrs; 27128 bool ctor_initializer_p; 27129 cp_token *token; 27130 27131 gcc_assert (keyword == RID_TRANSACTION_ATOMIC 27132 || keyword == RID_TRANSACTION_RELAXED); 27133 token = cp_parser_require_keyword (parser, keyword, 27134 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC 27135 : RT_TRANSACTION_RELAXED)); 27136 gcc_assert (token != NULL); 27137 27138 if (keyword == RID_TRANSACTION_RELAXED) 27139 new_in |= TM_STMT_ATTR_RELAXED; 27140 else 27141 { 27142 attrs = cp_parser_txn_attribute_opt (parser); 27143 if (attrs) 27144 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER); 27145 } 27146 27147 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in); 27148 27149 parser->in_transaction = new_in; 27150 27151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY)) 27152 ctor_initializer_p = cp_parser_function_try_block (parser); 27153 else 27154 ctor_initializer_p 27155 = cp_parser_ctor_initializer_opt_and_function_body (parser); 27156 27157 parser->in_transaction = old_in; 27158 27159 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE); 27160 27161 return ctor_initializer_p; 27162 } 27163 27164 /* Parse a __transaction_cancel statement. 27165 27166 cancel-statement: 27167 __transaction_cancel txn-attribute[opt] ; 27168 __transaction_cancel txn-attribute[opt] throw-expression ; 27169 27170 ??? Cancel and throw is not yet implemented. */ 27171 27172 static tree 27173 cp_parser_transaction_cancel (cp_parser *parser) 27174 { 27175 cp_token *token; 27176 bool is_outer = false; 27177 tree stmt, attrs; 27178 27179 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL, 27180 RT_TRANSACTION_CANCEL); 27181 gcc_assert (token != NULL); 27182 27183 attrs = cp_parser_txn_attribute_opt (parser); 27184 if (attrs) 27185 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0); 27186 27187 /* ??? Parse cancel-and-throw here. */ 27188 27189 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); 27190 27191 if (!flag_tm) 27192 { 27193 error_at (token->location, "%<__transaction_cancel%> without " 27194 "transactional memory support enabled"); 27195 return error_mark_node; 27196 } 27197 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED) 27198 { 27199 error_at (token->location, "%<__transaction_cancel%> within a " 27200 "%<__transaction_relaxed%>"); 27201 return error_mark_node; 27202 } 27203 else if (is_outer) 27204 { 27205 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0 27206 && !is_tm_may_cancel_outer (current_function_decl)) 27207 { 27208 error_at (token->location, "outer %<__transaction_cancel%> not " 27209 "within outer %<__transaction_atomic%>"); 27210 error_at (token->location, 27211 " or a %<transaction_may_cancel_outer%> function"); 27212 return error_mark_node; 27213 } 27214 } 27215 else if (parser->in_transaction == 0) 27216 { 27217 error_at (token->location, "%<__transaction_cancel%> not within " 27218 "%<__transaction_atomic%>"); 27219 return error_mark_node; 27220 } 27221 27222 stmt = build_tm_abort_call (token->location, is_outer); 27223 add_stmt (stmt); 27224 finish_stmt (); 27225 27226 return stmt; 27227 } 27228 27229 /* The parser. */ 27230 27231 static GTY (()) cp_parser *the_parser; 27232 27233 27234 /* Special handling for the first token or line in the file. The first 27235 thing in the file might be #pragma GCC pch_preprocess, which loads a 27236 PCH file, which is a GC collection point. So we need to handle this 27237 first pragma without benefit of an existing lexer structure. 27238 27239 Always returns one token to the caller in *FIRST_TOKEN. This is 27240 either the true first token of the file, or the first token after 27241 the initial pragma. */ 27242 27243 static void 27244 cp_parser_initial_pragma (cp_token *first_token) 27245 { 27246 tree name = NULL; 27247 27248 cp_lexer_get_preprocessor_token (NULL, first_token); 27249 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS) 27250 return; 27251 27252 cp_lexer_get_preprocessor_token (NULL, first_token); 27253 if (first_token->type == CPP_STRING) 27254 { 27255 name = first_token->u.value; 27256 27257 cp_lexer_get_preprocessor_token (NULL, first_token); 27258 if (first_token->type != CPP_PRAGMA_EOL) 27259 error_at (first_token->location, 27260 "junk at end of %<#pragma GCC pch_preprocess%>"); 27261 } 27262 else 27263 error_at (first_token->location, "expected string literal"); 27264 27265 /* Skip to the end of the pragma. */ 27266 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF) 27267 cp_lexer_get_preprocessor_token (NULL, first_token); 27268 27269 /* Now actually load the PCH file. */ 27270 if (name) 27271 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name)); 27272 27273 /* Read one more token to return to our caller. We have to do this 27274 after reading the PCH file in, since its pointers have to be 27275 live. */ 27276 cp_lexer_get_preprocessor_token (NULL, first_token); 27277 } 27278 27279 /* Normal parsing of a pragma token. Here we can (and must) use the 27280 regular lexer. */ 27281 27282 static bool 27283 cp_parser_pragma (cp_parser *parser, enum pragma_context context) 27284 { 27285 cp_token *pragma_tok; 27286 unsigned int id; 27287 27288 pragma_tok = cp_lexer_consume_token (parser->lexer); 27289 gcc_assert (pragma_tok->type == CPP_PRAGMA); 27290 parser->lexer->in_pragma = true; 27291 27292 id = pragma_tok->pragma_kind; 27293 switch (id) 27294 { 27295 case PRAGMA_GCC_PCH_PREPROCESS: 27296 error_at (pragma_tok->location, 27297 "%<#pragma GCC pch_preprocess%> must be first"); 27298 break; 27299 27300 case PRAGMA_OMP_BARRIER: 27301 switch (context) 27302 { 27303 case pragma_compound: 27304 cp_parser_omp_barrier (parser, pragma_tok); 27305 return false; 27306 case pragma_stmt: 27307 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be " 27308 "used in compound statements"); 27309 break; 27310 default: 27311 goto bad_stmt; 27312 } 27313 break; 27314 27315 case PRAGMA_OMP_FLUSH: 27316 switch (context) 27317 { 27318 case pragma_compound: 27319 cp_parser_omp_flush (parser, pragma_tok); 27320 return false; 27321 case pragma_stmt: 27322 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be " 27323 "used in compound statements"); 27324 break; 27325 default: 27326 goto bad_stmt; 27327 } 27328 break; 27329 27330 case PRAGMA_OMP_TASKWAIT: 27331 switch (context) 27332 { 27333 case pragma_compound: 27334 cp_parser_omp_taskwait (parser, pragma_tok); 27335 return false; 27336 case pragma_stmt: 27337 error_at (pragma_tok->location, 27338 "%<#pragma omp taskwait%> may only be " 27339 "used in compound statements"); 27340 break; 27341 default: 27342 goto bad_stmt; 27343 } 27344 break; 27345 27346 case PRAGMA_OMP_TASKYIELD: 27347 switch (context) 27348 { 27349 case pragma_compound: 27350 cp_parser_omp_taskyield (parser, pragma_tok); 27351 return false; 27352 case pragma_stmt: 27353 error_at (pragma_tok->location, 27354 "%<#pragma omp taskyield%> may only be " 27355 "used in compound statements"); 27356 break; 27357 default: 27358 goto bad_stmt; 27359 } 27360 break; 27361 27362 case PRAGMA_OMP_THREADPRIVATE: 27363 cp_parser_omp_threadprivate (parser, pragma_tok); 27364 return false; 27365 27366 case PRAGMA_OMP_ATOMIC: 27367 case PRAGMA_OMP_CRITICAL: 27368 case PRAGMA_OMP_FOR: 27369 case PRAGMA_OMP_MASTER: 27370 case PRAGMA_OMP_ORDERED: 27371 case PRAGMA_OMP_PARALLEL: 27372 case PRAGMA_OMP_SECTIONS: 27373 case PRAGMA_OMP_SINGLE: 27374 case PRAGMA_OMP_TASK: 27375 if (context == pragma_external) 27376 goto bad_stmt; 27377 cp_parser_omp_construct (parser, pragma_tok); 27378 return true; 27379 27380 case PRAGMA_OMP_SECTION: 27381 error_at (pragma_tok->location, 27382 "%<#pragma omp section%> may only be used in " 27383 "%<#pragma omp sections%> construct"); 27384 break; 27385 27386 default: 27387 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL); 27388 c_invoke_pragma_handler (id); 27389 break; 27390 27391 bad_stmt: 27392 cp_parser_error (parser, "expected declaration specifiers"); 27393 break; 27394 } 27395 27396 cp_parser_skip_to_pragma_eol (parser, pragma_tok); 27397 return false; 27398 } 27399 27400 /* The interface the pragma parsers have to the lexer. */ 27401 27402 enum cpp_ttype 27403 pragma_lex (tree *value) 27404 { 27405 cp_token *tok; 27406 enum cpp_ttype ret; 27407 27408 tok = cp_lexer_peek_token (the_parser->lexer); 27409 27410 ret = tok->type; 27411 *value = tok->u.value; 27412 27413 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF) 27414 ret = CPP_EOF; 27415 else if (ret == CPP_STRING) 27416 *value = cp_parser_string_literal (the_parser, false, false); 27417 else 27418 { 27419 cp_lexer_consume_token (the_parser->lexer); 27420 if (ret == CPP_KEYWORD) 27421 ret = CPP_NAME; 27422 } 27423 27424 return ret; 27425 } 27426 27427 27428 /* External interface. */ 27429 27430 /* Parse one entire translation unit. */ 27431 27432 void 27433 c_parse_file (void) 27434 { 27435 static bool already_called = false; 27436 27437 if (already_called) 27438 { 27439 sorry ("inter-module optimizations not implemented for C++"); 27440 return; 27441 } 27442 already_called = true; 27443 27444 the_parser = cp_parser_new (); 27445 push_deferring_access_checks (flag_access_control 27446 ? dk_no_deferred : dk_no_check); 27447 cp_parser_translation_unit (the_parser); 27448 the_parser = NULL; 27449 } 27450 27451 #include "gt-cp-parser.h" 27452