ParseDecl.cpp revision 91a2886d558ea6ca7a0bed73ab5acea5ae78eac2
1//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the Declaration portions of the Parser interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Parse/Parser.h" 15#include "clang/Parse/ParseDiagnostic.h" 16#include "clang/Parse/Scope.h" 17#include "ExtensionRAIIObject.h" 18#include "llvm/ADT/SmallSet.h" 19using namespace clang; 20 21//===----------------------------------------------------------------------===// 22// C99 6.7: Declarations. 23//===----------------------------------------------------------------------===// 24 25/// ParseTypeName 26/// type-name: [C99 6.7.6] 27/// specifier-qualifier-list abstract-declarator[opt] 28/// 29/// Called type-id in C++. 30Action::TypeResult Parser::ParseTypeName(SourceRange *Range) { 31 // Parse the common declaration-specifiers piece. 32 DeclSpec DS; 33 ParseSpecifierQualifierList(DS); 34 35 // Parse the abstract-declarator, if present. 36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); 37 ParseDeclarator(DeclaratorInfo); 38 if (Range) 39 *Range = DeclaratorInfo.getSourceRange(); 40 41 if (DeclaratorInfo.isInvalidType()) 42 return true; 43 44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo); 45} 46 47/// ParseAttributes - Parse a non-empty attributes list. 48/// 49/// [GNU] attributes: 50/// attribute 51/// attributes attribute 52/// 53/// [GNU] attribute: 54/// '__attribute__' '(' '(' attribute-list ')' ')' 55/// 56/// [GNU] attribute-list: 57/// attrib 58/// attribute_list ',' attrib 59/// 60/// [GNU] attrib: 61/// empty 62/// attrib-name 63/// attrib-name '(' identifier ')' 64/// attrib-name '(' identifier ',' nonempty-expr-list ')' 65/// attrib-name '(' argument-expression-list [C99 6.5.2] ')' 66/// 67/// [GNU] attrib-name: 68/// identifier 69/// typespec 70/// typequal 71/// storageclass 72/// 73/// FIXME: The GCC grammar/code for this construct implies we need two 74/// token lookahead. Comment from gcc: "If they start with an identifier 75/// which is followed by a comma or close parenthesis, then the arguments 76/// start with that identifier; otherwise they are an expression list." 77/// 78/// At the moment, I am not doing 2 token lookahead. I am also unaware of 79/// any attributes that don't work (based on my limited testing). Most 80/// attributes are very simple in practice. Until we find a bug, I don't see 81/// a pressing need to implement the 2 token lookahead. 82 83AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) { 84 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!"); 85 86 AttributeList *CurrAttr = 0; 87 88 while (Tok.is(tok::kw___attribute)) { 89 ConsumeToken(); 90 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, 91 "attribute")) { 92 SkipUntil(tok::r_paren, true); // skip until ) or ; 93 return CurrAttr; 94 } 95 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { 96 SkipUntil(tok::r_paren, true); // skip until ) or ; 97 return CurrAttr; 98 } 99 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) 100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() || 101 Tok.is(tok::comma)) { 102 103 if (Tok.is(tok::comma)) { 104 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) 105 ConsumeToken(); 106 continue; 107 } 108 // we have an identifier or declaration specifier (const, int, etc.) 109 IdentifierInfo *AttrName = Tok.getIdentifierInfo(); 110 SourceLocation AttrNameLoc = ConsumeToken(); 111 112 // check if we have a "paramterized" attribute 113 if (Tok.is(tok::l_paren)) { 114 ConsumeParen(); // ignore the left paren loc for now 115 116 if (Tok.is(tok::identifier)) { 117 IdentifierInfo *ParmName = Tok.getIdentifierInfo(); 118 SourceLocation ParmLoc = ConsumeToken(); 119 120 if (Tok.is(tok::r_paren)) { 121 // __attribute__(( mode(byte) )) 122 ConsumeParen(); // ignore the right paren loc for now 123 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 124 ParmName, ParmLoc, 0, 0, CurrAttr); 125 } else if (Tok.is(tok::comma)) { 126 ConsumeToken(); 127 // __attribute__(( format(printf, 1, 2) )) 128 ExprVector ArgExprs(Actions); 129 bool ArgExprsOk = true; 130 131 // now parse the non-empty comma separated list of expressions 132 while (1) { 133 OwningExprResult ArgExpr(ParseAssignmentExpression()); 134 if (ArgExpr.isInvalid()) { 135 ArgExprsOk = false; 136 SkipUntil(tok::r_paren); 137 break; 138 } else { 139 ArgExprs.push_back(ArgExpr.release()); 140 } 141 if (Tok.isNot(tok::comma)) 142 break; 143 ConsumeToken(); // Eat the comma, move to the next argument 144 } 145 if (ArgExprsOk && Tok.is(tok::r_paren)) { 146 ConsumeParen(); // ignore the right paren loc for now 147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, 148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr); 149 } 150 } 151 } else { // not an identifier 152 switch (Tok.getKind()) { 153 case tok::r_paren: 154 // parse a possibly empty comma separated list of expressions 155 // __attribute__(( nonnull() )) 156 ConsumeParen(); // ignore the right paren loc for now 157 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 158 0, SourceLocation(), 0, 0, CurrAttr); 159 break; 160 case tok::kw_char: 161 case tok::kw_wchar_t: 162 case tok::kw_char16_t: 163 case tok::kw_char32_t: 164 case tok::kw_bool: 165 case tok::kw_short: 166 case tok::kw_int: 167 case tok::kw_long: 168 case tok::kw_signed: 169 case tok::kw_unsigned: 170 case tok::kw_float: 171 case tok::kw_double: 172 case tok::kw_void: 173 case tok::kw_typeof: 174 // If it's a builtin type name, eat it and expect a rparen 175 // __attribute__(( vec_type_hint(char) )) 176 ConsumeToken(); 177 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 178 0, SourceLocation(), 0, 0, CurrAttr); 179 if (Tok.is(tok::r_paren)) 180 ConsumeParen(); 181 break; 182 default: 183 // __attribute__(( aligned(16) )) 184 ExprVector ArgExprs(Actions); 185 bool ArgExprsOk = true; 186 187 // now parse the list of expressions 188 while (1) { 189 OwningExprResult ArgExpr(ParseAssignmentExpression()); 190 if (ArgExpr.isInvalid()) { 191 ArgExprsOk = false; 192 SkipUntil(tok::r_paren); 193 break; 194 } else { 195 ArgExprs.push_back(ArgExpr.release()); 196 } 197 if (Tok.isNot(tok::comma)) 198 break; 199 ConsumeToken(); // Eat the comma, move to the next argument 200 } 201 // Match the ')'. 202 if (ArgExprsOk && Tok.is(tok::r_paren)) { 203 ConsumeParen(); // ignore the right paren loc for now 204 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, 205 SourceLocation(), ArgExprs.take(), ArgExprs.size(), 206 CurrAttr); 207 } 208 break; 209 } 210 } 211 } else { 212 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 213 0, SourceLocation(), 0, 0, CurrAttr); 214 } 215 } 216 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) 217 SkipUntil(tok::r_paren, false); 218 SourceLocation Loc = Tok.getLocation();; 219 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { 220 SkipUntil(tok::r_paren, false); 221 } 222 if (EndLoc) 223 *EndLoc = Loc; 224 } 225 return CurrAttr; 226} 227 228/// ParseMicrosoftDeclSpec - Parse an __declspec construct 229/// 230/// [MS] decl-specifier: 231/// __declspec ( extended-decl-modifier-seq ) 232/// 233/// [MS] extended-decl-modifier-seq: 234/// extended-decl-modifier[opt] 235/// extended-decl-modifier extended-decl-modifier-seq 236 237AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) { 238 assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); 239 240 ConsumeToken(); 241 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, 242 "declspec")) { 243 SkipUntil(tok::r_paren, true); // skip until ) or ; 244 return CurrAttr; 245 } 246 while (Tok.getIdentifierInfo()) { 247 IdentifierInfo *AttrName = Tok.getIdentifierInfo(); 248 SourceLocation AttrNameLoc = ConsumeToken(); 249 if (Tok.is(tok::l_paren)) { 250 ConsumeParen(); 251 // FIXME: This doesn't parse __declspec(property(get=get_func_name)) 252 // correctly. 253 OwningExprResult ArgExpr(ParseAssignmentExpression()); 254 if (!ArgExpr.isInvalid()) { 255 ExprTy* ExprList = ArgExpr.take(); 256 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, 257 SourceLocation(), &ExprList, 1, 258 CurrAttr, true); 259 } 260 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) 261 SkipUntil(tok::r_paren, false); 262 } else { 263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, SourceLocation(), 264 0, 0, CurrAttr, true); 265 } 266 } 267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) 268 SkipUntil(tok::r_paren, false); 269 return CurrAttr; 270} 271 272AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) { 273 // Treat these like attributes 274 // FIXME: Allow Sema to distinguish between these and real attributes! 275 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || 276 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) || 277 Tok.is(tok::kw___w64)) { 278 IdentifierInfo *AttrName = Tok.getIdentifierInfo(); 279 SourceLocation AttrNameLoc = ConsumeToken(); 280 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) 281 // FIXME: Support these properly! 282 continue; 283 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, 284 SourceLocation(), 0, 0, CurrAttr, true); 285 } 286 return CurrAttr; 287} 288 289/// ParseDeclaration - Parse a full 'declaration', which consists of 290/// declaration-specifiers, some number of declarators, and a semicolon. 291/// 'Context' should be a Declarator::TheContext value. This returns the 292/// location of the semicolon in DeclEnd. 293/// 294/// declaration: [C99 6.7] 295/// block-declaration -> 296/// simple-declaration 297/// others [FIXME] 298/// [C++] template-declaration 299/// [C++] namespace-definition 300/// [C++] using-directive 301/// [C++] using-declaration 302/// [C++0x] static_assert-declaration 303/// others... [FIXME] 304/// 305Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context, 306 SourceLocation &DeclEnd) { 307 DeclPtrTy SingleDecl; 308 switch (Tok.getKind()) { 309 case tok::kw_template: 310 case tok::kw_export: 311 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); 312 break; 313 case tok::kw_namespace: 314 SingleDecl = ParseNamespace(Context, DeclEnd); 315 break; 316 case tok::kw_using: 317 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd); 318 break; 319 case tok::kw_static_assert: 320 SingleDecl = ParseStaticAssertDeclaration(DeclEnd); 321 break; 322 default: 323 return ParseSimpleDeclaration(Context, DeclEnd); 324 } 325 326 // This routine returns a DeclGroup, if the thing we parsed only contains a 327 // single decl, convert it now. 328 return Actions.ConvertDeclToDeclGroup(SingleDecl); 329} 330 331/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] 332/// declaration-specifiers init-declarator-list[opt] ';' 333///[C90/C++]init-declarator-list ';' [TODO] 334/// [OMP] threadprivate-directive [TODO] 335/// 336/// If RequireSemi is false, this does not check for a ';' at the end of the 337/// declaration. 338Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context, 339 SourceLocation &DeclEnd, 340 bool RequireSemi) { 341 // Parse the common declaration-specifiers piece. 342 DeclSpec DS; 343 ParseDeclarationSpecifiers(DS); 344 345 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" 346 // declaration-specifiers init-declarator-list[opt] ';' 347 if (Tok.is(tok::semi)) { 348 ConsumeToken(); 349 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS); 350 return Actions.ConvertDeclToDeclGroup(TheDecl); 351 } 352 353 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); 354 ParseDeclarator(DeclaratorInfo); 355 356 DeclGroupPtrTy DG = 357 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); 358 359 DeclEnd = Tok.getLocation(); 360 361 // If the client wants to check what comes after the declaration, just return 362 // immediately without checking anything! 363 if (!RequireSemi) return DG; 364 365 if (Tok.is(tok::semi)) { 366 ConsumeToken(); 367 return DG; 368 } 369 370 Diag(Tok, diag::err_expected_semi_declaration); 371 // Skip to end of block or statement 372 SkipUntil(tok::r_brace, true, true); 373 if (Tok.is(tok::semi)) 374 ConsumeToken(); 375 return DG; 376} 377 378/// \brief Parse 'declaration' after parsing 'declaration-specifiers 379/// declarator'. This method parses the remainder of the declaration 380/// (including any attributes or initializer, among other things) and 381/// finalizes the declaration. 382/// 383/// init-declarator: [C99 6.7] 384/// declarator 385/// declarator '=' initializer 386/// [GNU] declarator simple-asm-expr[opt] attributes[opt] 387/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer 388/// [C++] declarator initializer[opt] 389/// 390/// [C++] initializer: 391/// [C++] '=' initializer-clause 392/// [C++] '(' expression-list ')' 393/// [C++0x] '=' 'default' [TODO] 394/// [C++0x] '=' 'delete' 395/// 396/// According to the standard grammar, =default and =delete are function 397/// definitions, but that definitely doesn't fit with the parser here. 398/// 399Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D, 400 const ParsedTemplateInfo &TemplateInfo) { 401 // If a simple-asm-expr is present, parse it. 402 if (Tok.is(tok::kw_asm)) { 403 SourceLocation Loc; 404 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc)); 405 if (AsmLabel.isInvalid()) { 406 SkipUntil(tok::semi, true, true); 407 return DeclPtrTy(); 408 } 409 410 D.setAsmLabel(AsmLabel.release()); 411 D.SetRangeEnd(Loc); 412 } 413 414 // If attributes are present, parse them. 415 if (Tok.is(tok::kw___attribute)) { 416 SourceLocation Loc; 417 AttributeList *AttrList = ParseAttributes(&Loc); 418 D.AddAttributes(AttrList, Loc); 419 } 420 421 // Inform the current actions module that we just parsed this declarator. 422 DeclPtrTy ThisDecl = TemplateInfo.TemplateParams? 423 Actions.ActOnTemplateDeclarator(CurScope, 424 Action::MultiTemplateParamsArg(Actions, 425 TemplateInfo.TemplateParams->data(), 426 TemplateInfo.TemplateParams->size()), 427 D) 428 : Actions.ActOnDeclarator(CurScope, D); 429 430 // Parse declarator '=' initializer. 431 if (Tok.is(tok::equal)) { 432 ConsumeToken(); 433 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) { 434 SourceLocation DelLoc = ConsumeToken(); 435 Actions.SetDeclDeleted(ThisDecl, DelLoc); 436 } else { 437 if (getLang().CPlusPlus) 438 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl); 439 440 OwningExprResult Init(ParseInitializer()); 441 442 if (getLang().CPlusPlus) 443 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl); 444 445 if (Init.isInvalid()) { 446 SkipUntil(tok::semi, true, true); 447 return DeclPtrTy(); 448 } 449 Actions.AddInitializerToDecl(ThisDecl, move(Init)); 450 } 451 } else if (Tok.is(tok::l_paren)) { 452 // Parse C++ direct initializer: '(' expression-list ')' 453 SourceLocation LParenLoc = ConsumeParen(); 454 ExprVector Exprs(Actions); 455 CommaLocsTy CommaLocs; 456 457 if (ParseExpressionList(Exprs, CommaLocs)) { 458 SkipUntil(tok::r_paren); 459 } else { 460 // Match the ')'. 461 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 462 463 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && 464 "Unexpected number of commas!"); 465 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc, 466 move_arg(Exprs), 467 CommaLocs.data(), RParenLoc); 468 } 469 } else { 470 bool TypeContainsUndeducedAuto = 471 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; 472 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto); 473 } 474 475 return ThisDecl; 476} 477 478/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after 479/// parsing 'declaration-specifiers declarator'. This method is split out this 480/// way to handle the ambiguity between top-level function-definitions and 481/// declarations. 482/// 483/// init-declarator-list: [C99 6.7] 484/// init-declarator 485/// init-declarator-list ',' init-declarator 486/// 487/// According to the standard grammar, =default and =delete are function 488/// definitions, but that definitely doesn't fit with the parser here. 489/// 490Parser::DeclGroupPtrTy Parser:: 491ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { 492 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls 493 // that we parse together here. 494 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup; 495 496 // At this point, we know that it is not a function definition. Parse the 497 // rest of the init-declarator-list. 498 while (1) { 499 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D); 500 if (ThisDecl.get()) 501 DeclsInGroup.push_back(ThisDecl); 502 503 // If we don't have a comma, it is either the end of the list (a ';') or an 504 // error, bail out. 505 if (Tok.isNot(tok::comma)) 506 break; 507 508 // Consume the comma. 509 ConsumeToken(); 510 511 // Parse the next declarator. 512 D.clear(); 513 514 // Accept attributes in an init-declarator. In the first declarator in a 515 // declaration, these would be part of the declspec. In subsequent 516 // declarators, they become part of the declarator itself, so that they 517 // don't apply to declarators after *this* one. Examples: 518 // short __attribute__((common)) var; -> declspec 519 // short var __attribute__((common)); -> declarator 520 // short x, __attribute__((common)) var; -> declarator 521 if (Tok.is(tok::kw___attribute)) { 522 SourceLocation Loc; 523 AttributeList *AttrList = ParseAttributes(&Loc); 524 D.AddAttributes(AttrList, Loc); 525 } 526 527 ParseDeclarator(D); 528 } 529 530 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(), 531 DeclsInGroup.data(), 532 DeclsInGroup.size()); 533} 534 535/// ParseSpecifierQualifierList 536/// specifier-qualifier-list: 537/// type-specifier specifier-qualifier-list[opt] 538/// type-qualifier specifier-qualifier-list[opt] 539/// [GNU] attributes specifier-qualifier-list[opt] 540/// 541void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { 542 /// specifier-qualifier-list is a subset of declaration-specifiers. Just 543 /// parse declaration-specifiers and complain about extra stuff. 544 ParseDeclarationSpecifiers(DS); 545 546 // Validate declspec for type-name. 547 unsigned Specs = DS.getParsedSpecifiers(); 548 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && 549 !DS.getAttributes()) 550 Diag(Tok, diag::err_typename_requires_specqual); 551 552 // Issue diagnostic and remove storage class if present. 553 if (Specs & DeclSpec::PQ_StorageClassSpecifier) { 554 if (DS.getStorageClassSpecLoc().isValid()) 555 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); 556 else 557 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); 558 DS.ClearStorageClassSpecs(); 559 } 560 561 // Issue diagnostic and remove function specfier if present. 562 if (Specs & DeclSpec::PQ_FunctionSpecifier) { 563 if (DS.isInlineSpecified()) 564 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); 565 if (DS.isVirtualSpecified()) 566 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); 567 if (DS.isExplicitSpecified()) 568 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); 569 DS.ClearFunctionSpecs(); 570 } 571} 572 573/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the 574/// specified token is valid after the identifier in a declarator which 575/// immediately follows the declspec. For example, these things are valid: 576/// 577/// int x [ 4]; // direct-declarator 578/// int x ( int y); // direct-declarator 579/// int(int x ) // direct-declarator 580/// int x ; // simple-declaration 581/// int x = 17; // init-declarator-list 582/// int x , y; // init-declarator-list 583/// int x __asm__ ("foo"); // init-declarator-list 584/// int x : 4; // struct-declarator 585/// int x { 5}; // C++'0x unified initializers 586/// 587/// This is not, because 'x' does not immediately follow the declspec (though 588/// ')' happens to be valid anyway). 589/// int (x) 590/// 591static bool isValidAfterIdentifierInDeclarator(const Token &T) { 592 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || 593 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || 594 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); 595} 596 597 598/// ParseImplicitInt - This method is called when we have an non-typename 599/// identifier in a declspec (which normally terminates the decl spec) when 600/// the declspec has no type specifier. In this case, the declspec is either 601/// malformed or is "implicit int" (in K&R and C89). 602/// 603/// This method handles diagnosing this prettily and returns false if the 604/// declspec is done being processed. If it recovers and thinks there may be 605/// other pieces of declspec after it, it returns true. 606/// 607bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, 608 const ParsedTemplateInfo &TemplateInfo, 609 AccessSpecifier AS) { 610 assert(Tok.is(tok::identifier) && "should have identifier"); 611 612 SourceLocation Loc = Tok.getLocation(); 613 // If we see an identifier that is not a type name, we normally would 614 // parse it as the identifer being declared. However, when a typename 615 // is typo'd or the definition is not included, this will incorrectly 616 // parse the typename as the identifier name and fall over misparsing 617 // later parts of the diagnostic. 618 // 619 // As such, we try to do some look-ahead in cases where this would 620 // otherwise be an "implicit-int" case to see if this is invalid. For 621 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as 622 // an identifier with implicit int, we'd get a parse error because the 623 // next token is obviously invalid for a type. Parse these as a case 624 // with an invalid type specifier. 625 assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); 626 627 // Since we know that this either implicit int (which is rare) or an 628 // error, we'd do lookahead to try to do better recovery. 629 if (isValidAfterIdentifierInDeclarator(NextToken())) { 630 // If this token is valid for implicit int, e.g. "static x = 4", then 631 // we just avoid eating the identifier, so it will be parsed as the 632 // identifier in the declarator. 633 return false; 634 } 635 636 // Otherwise, if we don't consume this token, we are going to emit an 637 // error anyway. Try to recover from various common problems. Check 638 // to see if this was a reference to a tag name without a tag specified. 639 // This is a common problem in C (saying 'foo' instead of 'struct foo'). 640 // 641 // C++ doesn't need this, and isTagName doesn't take SS. 642 if (SS == 0) { 643 const char *TagName = 0; 644 tok::TokenKind TagKind = tok::unknown; 645 646 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) { 647 default: break; 648 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break; 649 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break; 650 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break; 651 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break; 652 } 653 654 if (TagName) { 655 Diag(Loc, diag::err_use_of_tag_name_without_tag) 656 << Tok.getIdentifierInfo() << TagName 657 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName); 658 659 // Parse this as a tag as if the missing tag were present. 660 if (TagKind == tok::kw_enum) 661 ParseEnumSpecifier(Loc, DS, AS); 662 else 663 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS); 664 return true; 665 } 666 } 667 668 // Since this is almost certainly an invalid type name, emit a 669 // diagnostic that says it, eat the token, and mark the declspec as 670 // invalid. 671 SourceRange R; 672 if (SS) R = SS->getRange(); 673 674 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; 675 const char *PrevSpec; 676 unsigned DiagID; 677 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID); 678 DS.SetRangeEnd(Tok.getLocation()); 679 ConsumeToken(); 680 681 // TODO: Could inject an invalid typedef decl in an enclosing scope to 682 // avoid rippling error messages on subsequent uses of the same type, 683 // could be useful if #include was forgotten. 684 return false; 685} 686 687/// ParseDeclarationSpecifiers 688/// declaration-specifiers: [C99 6.7] 689/// storage-class-specifier declaration-specifiers[opt] 690/// type-specifier declaration-specifiers[opt] 691/// [C99] function-specifier declaration-specifiers[opt] 692/// [GNU] attributes declaration-specifiers[opt] 693/// 694/// storage-class-specifier: [C99 6.7.1] 695/// 'typedef' 696/// 'extern' 697/// 'static' 698/// 'auto' 699/// 'register' 700/// [C++] 'mutable' 701/// [GNU] '__thread' 702/// function-specifier: [C99 6.7.4] 703/// [C99] 'inline' 704/// [C++] 'virtual' 705/// [C++] 'explicit' 706/// 'friend': [C++ dcl.friend] 707 708/// 709void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, 710 const ParsedTemplateInfo &TemplateInfo, 711 AccessSpecifier AS, 712 DeclSpecContext DSContext) { 713 DS.SetRangeStart(Tok.getLocation()); 714 while (1) { 715 bool isInvalid = false; 716 const char *PrevSpec = 0; 717 unsigned DiagID = 0; 718 719 SourceLocation Loc = Tok.getLocation(); 720 721 switch (Tok.getKind()) { 722 default: 723 DoneWithDeclSpec: 724 // If this is not a declaration specifier token, we're done reading decl 725 // specifiers. First verify that DeclSpec's are consistent. 726 DS.Finish(Diags, PP); 727 return; 728 729 case tok::coloncolon: // ::foo::bar 730 // Annotate C++ scope specifiers. If we get one, loop. 731 if (TryAnnotateCXXScopeToken(true)) 732 continue; 733 goto DoneWithDeclSpec; 734 735 case tok::annot_cxxscope: { 736 if (DS.hasTypeSpecifier()) 737 goto DoneWithDeclSpec; 738 739 // We are looking for a qualified typename. 740 Token Next = NextToken(); 741 if (Next.is(tok::annot_template_id) && 742 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) 743 ->Kind == TNK_Type_template) { 744 // We have a qualified template-id, e.g., N::A<int> 745 CXXScopeSpec SS; 746 ParseOptionalCXXScopeSpecifier(SS, true); 747 assert(Tok.is(tok::annot_template_id) && 748 "ParseOptionalCXXScopeSpecifier not working"); 749 AnnotateTemplateIdTokenAsType(&SS); 750 continue; 751 } 752 753 if (Next.isNot(tok::identifier)) 754 goto DoneWithDeclSpec; 755 756 CXXScopeSpec SS; 757 SS.setScopeRep(Tok.getAnnotationValue()); 758 SS.setRange(Tok.getAnnotationRange()); 759 760 // If the next token is the name of the class type that the C++ scope 761 // denotes, followed by a '(', then this is a constructor declaration. 762 // We're done with the decl-specifiers. 763 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(), 764 CurScope, &SS) && 765 GetLookAheadToken(2).is(tok::l_paren)) 766 goto DoneWithDeclSpec; 767 768 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), 769 Next.getLocation(), CurScope, &SS); 770 771 // If the referenced identifier is not a type, then this declspec is 772 // erroneous: We already checked about that it has no type specifier, and 773 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the 774 // typename. 775 if (TypeRep == 0) { 776 ConsumeToken(); // Eat the scope spec so the identifier is current. 777 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue; 778 goto DoneWithDeclSpec; 779 } 780 781 ConsumeToken(); // The C++ scope. 782 783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, 784 DiagID, TypeRep); 785 if (isInvalid) 786 break; 787 788 DS.SetRangeEnd(Tok.getLocation()); 789 ConsumeToken(); // The typename. 790 791 continue; 792 } 793 794 case tok::annot_typename: { 795 if (Tok.getAnnotationValue()) 796 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, 797 DiagID, Tok.getAnnotationValue()); 798 else 799 DS.SetTypeSpecError(); 800 DS.SetRangeEnd(Tok.getAnnotationEndLoc()); 801 ConsumeToken(); // The typename 802 803 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' 804 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an 805 // Objective-C interface. If we don't have Objective-C or a '<', this is 806 // just a normal reference to a typedef name. 807 if (!Tok.is(tok::less) || !getLang().ObjC1) 808 continue; 809 810 SourceLocation EndProtoLoc; 811 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; 812 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); 813 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size()); 814 815 DS.SetRangeEnd(EndProtoLoc); 816 continue; 817 } 818 819 // typedef-name 820 case tok::identifier: { 821 // In C++, check to see if this is a scope specifier like foo::bar::, if 822 // so handle it as such. This is important for ctor parsing. 823 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true)) 824 continue; 825 826 // This identifier can only be a typedef name if we haven't already seen 827 // a type-specifier. Without this check we misparse: 828 // typedef int X; struct Y { short X; }; as 'short int'. 829 if (DS.hasTypeSpecifier()) 830 goto DoneWithDeclSpec; 831 832 // It has to be available as a typedef too! 833 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(), 834 Tok.getLocation(), CurScope); 835 836 // If this is not a typedef name, don't parse it as part of the declspec, 837 // it must be an implicit int or an error. 838 if (TypeRep == 0) { 839 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue; 840 goto DoneWithDeclSpec; 841 } 842 843 // C++: If the identifier is actually the name of the class type 844 // being defined and the next token is a '(', then this is a 845 // constructor declaration. We're done with the decl-specifiers 846 // and will treat this token as an identifier. 847 if (getLang().CPlusPlus && 848 (CurScope->isClassScope() || 849 (CurScope->isTemplateParamScope() && 850 CurScope->getParent()->isClassScope())) && 851 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) && 852 NextToken().getKind() == tok::l_paren) 853 goto DoneWithDeclSpec; 854 855 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, 856 DiagID, TypeRep); 857 if (isInvalid) 858 break; 859 860 DS.SetRangeEnd(Tok.getLocation()); 861 ConsumeToken(); // The identifier 862 863 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' 864 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an 865 // Objective-C interface. If we don't have Objective-C or a '<', this is 866 // just a normal reference to a typedef name. 867 if (!Tok.is(tok::less) || !getLang().ObjC1) 868 continue; 869 870 SourceLocation EndProtoLoc; 871 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; 872 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); 873 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size()); 874 875 DS.SetRangeEnd(EndProtoLoc); 876 877 // Need to support trailing type qualifiers (e.g. "id<p> const"). 878 // If a type specifier follows, it will be diagnosed elsewhere. 879 continue; 880 } 881 882 // type-name 883 case tok::annot_template_id: { 884 TemplateIdAnnotation *TemplateId 885 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 886 if (TemplateId->Kind != TNK_Type_template) { 887 // This template-id does not refer to a type name, so we're 888 // done with the type-specifiers. 889 goto DoneWithDeclSpec; 890 } 891 892 // Turn the template-id annotation token into a type annotation 893 // token, then try again to parse it as a type-specifier. 894 AnnotateTemplateIdTokenAsType(); 895 continue; 896 } 897 898 // GNU attributes support. 899 case tok::kw___attribute: 900 DS.AddAttributes(ParseAttributes()); 901 continue; 902 903 // Microsoft declspec support. 904 case tok::kw___declspec: 905 DS.AddAttributes(ParseMicrosoftDeclSpec()); 906 continue; 907 908 // Microsoft single token adornments. 909 case tok::kw___forceinline: 910 // FIXME: Add handling here! 911 break; 912 913 case tok::kw___ptr64: 914 case tok::kw___w64: 915 case tok::kw___cdecl: 916 case tok::kw___stdcall: 917 case tok::kw___fastcall: 918 DS.AddAttributes(ParseMicrosoftTypeAttributes()); 919 continue; 920 921 // storage-class-specifier 922 case tok::kw_typedef: 923 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec, 924 DiagID); 925 break; 926 case tok::kw_extern: 927 if (DS.isThreadSpecified()) 928 Diag(Tok, diag::ext_thread_before) << "extern"; 929 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec, 930 DiagID); 931 break; 932 case tok::kw___private_extern__: 933 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, 934 PrevSpec, DiagID); 935 break; 936 case tok::kw_static: 937 if (DS.isThreadSpecified()) 938 Diag(Tok, diag::ext_thread_before) << "static"; 939 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec, 940 DiagID); 941 break; 942 case tok::kw_auto: 943 if (getLang().CPlusPlus0x) 944 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, 945 DiagID); 946 else 947 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec, 948 DiagID); 949 break; 950 case tok::kw_register: 951 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec, 952 DiagID); 953 break; 954 case tok::kw_mutable: 955 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec, 956 DiagID); 957 break; 958 case tok::kw___thread: 959 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); 960 break; 961 962 // function-specifier 963 case tok::kw_inline: 964 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); 965 break; 966 case tok::kw_virtual: 967 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); 968 break; 969 case tok::kw_explicit: 970 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); 971 break; 972 973 // friend 974 case tok::kw_friend: 975 if (DSContext == DSC_class) 976 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); 977 else { 978 PrevSpec = ""; // not actually used by the diagnostic 979 DiagID = diag::err_friend_invalid_in_context; 980 isInvalid = true; 981 } 982 break; 983 984 // type-specifier 985 case tok::kw_short: 986 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, 987 DiagID); 988 break; 989 case tok::kw_long: 990 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) 991 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, 992 DiagID); 993 else 994 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, 995 DiagID); 996 break; 997 case tok::kw_signed: 998 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, 999 DiagID); 1000 break; 1001 case tok::kw_unsigned: 1002 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, 1003 DiagID); 1004 break; 1005 case tok::kw__Complex: 1006 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, 1007 DiagID); 1008 break; 1009 case tok::kw__Imaginary: 1010 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, 1011 DiagID); 1012 break; 1013 case tok::kw_void: 1014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, 1015 DiagID); 1016 break; 1017 case tok::kw_char: 1018 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, 1019 DiagID); 1020 break; 1021 case tok::kw_int: 1022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, 1023 DiagID); 1024 break; 1025 case tok::kw_float: 1026 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, 1027 DiagID); 1028 break; 1029 case tok::kw_double: 1030 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, 1031 DiagID); 1032 break; 1033 case tok::kw_wchar_t: 1034 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, 1035 DiagID); 1036 break; 1037 case tok::kw_char16_t: 1038 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, 1039 DiagID); 1040 break; 1041 case tok::kw_char32_t: 1042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, 1043 DiagID); 1044 break; 1045 case tok::kw_bool: 1046 case tok::kw__Bool: 1047 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, 1048 DiagID); 1049 break; 1050 case tok::kw__Decimal32: 1051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, 1052 DiagID); 1053 break; 1054 case tok::kw__Decimal64: 1055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, 1056 DiagID); 1057 break; 1058 case tok::kw__Decimal128: 1059 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, 1060 DiagID); 1061 break; 1062 1063 // class-specifier: 1064 case tok::kw_class: 1065 case tok::kw_struct: 1066 case tok::kw_union: { 1067 tok::TokenKind Kind = Tok.getKind(); 1068 ConsumeToken(); 1069 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS); 1070 continue; 1071 } 1072 1073 // enum-specifier: 1074 case tok::kw_enum: 1075 ConsumeToken(); 1076 ParseEnumSpecifier(Loc, DS, AS); 1077 continue; 1078 1079 // cv-qualifier: 1080 case tok::kw_const: 1081 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, 1082 getLang()); 1083 break; 1084 case tok::kw_volatile: 1085 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, 1086 getLang()); 1087 break; 1088 case tok::kw_restrict: 1089 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, 1090 getLang()); 1091 break; 1092 1093 // C++ typename-specifier: 1094 case tok::kw_typename: 1095 if (TryAnnotateTypeOrScopeToken()) 1096 continue; 1097 break; 1098 1099 // GNU typeof support. 1100 case tok::kw_typeof: 1101 ParseTypeofSpecifier(DS); 1102 continue; 1103 1104 case tok::kw_decltype: 1105 ParseDecltypeSpecifier(DS); 1106 continue; 1107 1108 case tok::less: 1109 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for 1110 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, 1111 // but we support it. 1112 if (DS.hasTypeSpecifier() || !getLang().ObjC1) 1113 goto DoneWithDeclSpec; 1114 1115 { 1116 SourceLocation EndProtoLoc; 1117 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; 1118 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); 1119 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size()); 1120 DS.SetRangeEnd(EndProtoLoc); 1121 1122 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) 1123 << CodeModificationHint::CreateInsertion(Loc, "id") 1124 << SourceRange(Loc, EndProtoLoc); 1125 // Need to support trailing type qualifiers (e.g. "id<p> const"). 1126 // If a type specifier follows, it will be diagnosed elsewhere. 1127 continue; 1128 } 1129 } 1130 // If the specifier wasn't legal, issue a diagnostic. 1131 if (isInvalid) { 1132 assert(PrevSpec && "Method did not return previous specifier!"); 1133 assert(DiagID); 1134 Diag(Tok, DiagID) << PrevSpec; 1135 } 1136 DS.SetRangeEnd(Tok.getLocation()); 1137 ConsumeToken(); 1138 } 1139} 1140 1141/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We 1142/// primarily follow the C++ grammar with additions for C99 and GNU, 1143/// which together subsume the C grammar. Note that the C++ 1144/// type-specifier also includes the C type-qualifier (for const, 1145/// volatile, and C99 restrict). Returns true if a type-specifier was 1146/// found (and parsed), false otherwise. 1147/// 1148/// type-specifier: [C++ 7.1.5] 1149/// simple-type-specifier 1150/// class-specifier 1151/// enum-specifier 1152/// elaborated-type-specifier [TODO] 1153/// cv-qualifier 1154/// 1155/// cv-qualifier: [C++ 7.1.5.1] 1156/// 'const' 1157/// 'volatile' 1158/// [C99] 'restrict' 1159/// 1160/// simple-type-specifier: [ C++ 7.1.5.2] 1161/// '::'[opt] nested-name-specifier[opt] type-name [TODO] 1162/// '::'[opt] nested-name-specifier 'template' template-id [TODO] 1163/// 'char' 1164/// 'wchar_t' 1165/// 'bool' 1166/// 'short' 1167/// 'int' 1168/// 'long' 1169/// 'signed' 1170/// 'unsigned' 1171/// 'float' 1172/// 'double' 1173/// 'void' 1174/// [C99] '_Bool' 1175/// [C99] '_Complex' 1176/// [C99] '_Imaginary' // Removed in TC2? 1177/// [GNU] '_Decimal32' 1178/// [GNU] '_Decimal64' 1179/// [GNU] '_Decimal128' 1180/// [GNU] typeof-specifier 1181/// [OBJC] class-name objc-protocol-refs[opt] [TODO] 1182/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] 1183/// [C++0x] 'decltype' ( expression ) 1184bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid, 1185 const char *&PrevSpec, 1186 unsigned &DiagID, 1187 const ParsedTemplateInfo &TemplateInfo) { 1188 SourceLocation Loc = Tok.getLocation(); 1189 1190 switch (Tok.getKind()) { 1191 case tok::identifier: // foo::bar 1192 case tok::kw_typename: // typename foo::bar 1193 // Annotate typenames and C++ scope specifiers. If we get one, just 1194 // recurse to handle whatever we get. 1195 if (TryAnnotateTypeOrScopeToken()) 1196 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, 1197 TemplateInfo); 1198 // Otherwise, not a type specifier. 1199 return false; 1200 case tok::coloncolon: // ::foo::bar 1201 if (NextToken().is(tok::kw_new) || // ::new 1202 NextToken().is(tok::kw_delete)) // ::delete 1203 return false; 1204 1205 // Annotate typenames and C++ scope specifiers. If we get one, just 1206 // recurse to handle whatever we get. 1207 if (TryAnnotateTypeOrScopeToken()) 1208 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, 1209 TemplateInfo); 1210 // Otherwise, not a type specifier. 1211 return false; 1212 1213 // simple-type-specifier: 1214 case tok::annot_typename: { 1215 if (Tok.getAnnotationValue()) 1216 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, 1217 DiagID, Tok.getAnnotationValue()); 1218 else 1219 DS.SetTypeSpecError(); 1220 DS.SetRangeEnd(Tok.getAnnotationEndLoc()); 1221 ConsumeToken(); // The typename 1222 1223 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' 1224 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an 1225 // Objective-C interface. If we don't have Objective-C or a '<', this is 1226 // just a normal reference to a typedef name. 1227 if (!Tok.is(tok::less) || !getLang().ObjC1) 1228 return true; 1229 1230 SourceLocation EndProtoLoc; 1231 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; 1232 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); 1233 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size()); 1234 1235 DS.SetRangeEnd(EndProtoLoc); 1236 return true; 1237 } 1238 1239 case tok::kw_short: 1240 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); 1241 break; 1242 case tok::kw_long: 1243 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) 1244 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, 1245 DiagID); 1246 else 1247 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, 1248 DiagID); 1249 break; 1250 case tok::kw_signed: 1251 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); 1252 break; 1253 case tok::kw_unsigned: 1254 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, 1255 DiagID); 1256 break; 1257 case tok::kw__Complex: 1258 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, 1259 DiagID); 1260 break; 1261 case tok::kw__Imaginary: 1262 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, 1263 DiagID); 1264 break; 1265 case tok::kw_void: 1266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); 1267 break; 1268 case tok::kw_char: 1269 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); 1270 break; 1271 case tok::kw_int: 1272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); 1273 break; 1274 case tok::kw_float: 1275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); 1276 break; 1277 case tok::kw_double: 1278 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); 1279 break; 1280 case tok::kw_wchar_t: 1281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); 1282 break; 1283 case tok::kw_char16_t: 1284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); 1285 break; 1286 case tok::kw_char32_t: 1287 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); 1288 break; 1289 case tok::kw_bool: 1290 case tok::kw__Bool: 1291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); 1292 break; 1293 case tok::kw__Decimal32: 1294 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, 1295 DiagID); 1296 break; 1297 case tok::kw__Decimal64: 1298 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, 1299 DiagID); 1300 break; 1301 case tok::kw__Decimal128: 1302 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, 1303 DiagID); 1304 break; 1305 1306 // class-specifier: 1307 case tok::kw_class: 1308 case tok::kw_struct: 1309 case tok::kw_union: { 1310 tok::TokenKind Kind = Tok.getKind(); 1311 ConsumeToken(); 1312 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo); 1313 return true; 1314 } 1315 1316 // enum-specifier: 1317 case tok::kw_enum: 1318 ConsumeToken(); 1319 ParseEnumSpecifier(Loc, DS); 1320 return true; 1321 1322 // cv-qualifier: 1323 case tok::kw_const: 1324 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, 1325 DiagID, getLang()); 1326 break; 1327 case tok::kw_volatile: 1328 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, 1329 DiagID, getLang()); 1330 break; 1331 case tok::kw_restrict: 1332 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, 1333 DiagID, getLang()); 1334 break; 1335 1336 // GNU typeof support. 1337 case tok::kw_typeof: 1338 ParseTypeofSpecifier(DS); 1339 return true; 1340 1341 // C++0x decltype support. 1342 case tok::kw_decltype: 1343 ParseDecltypeSpecifier(DS); 1344 return true; 1345 1346 // C++0x auto support. 1347 case tok::kw_auto: 1348 if (!getLang().CPlusPlus0x) 1349 return false; 1350 1351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID); 1352 break; 1353 case tok::kw___ptr64: 1354 case tok::kw___w64: 1355 case tok::kw___cdecl: 1356 case tok::kw___stdcall: 1357 case tok::kw___fastcall: 1358 DS.AddAttributes(ParseMicrosoftTypeAttributes()); 1359 return true; 1360 1361 default: 1362 // Not a type-specifier; do nothing. 1363 return false; 1364 } 1365 1366 // If the specifier combination wasn't legal, issue a diagnostic. 1367 if (isInvalid) { 1368 assert(PrevSpec && "Method did not return previous specifier!"); 1369 // Pick between error or extwarn. 1370 Diag(Tok, DiagID) << PrevSpec; 1371 } 1372 DS.SetRangeEnd(Tok.getLocation()); 1373 ConsumeToken(); // whatever we parsed above. 1374 return true; 1375} 1376 1377/// ParseStructDeclaration - Parse a struct declaration without the terminating 1378/// semicolon. 1379/// 1380/// struct-declaration: 1381/// specifier-qualifier-list struct-declarator-list 1382/// [GNU] __extension__ struct-declaration 1383/// [GNU] specifier-qualifier-list 1384/// struct-declarator-list: 1385/// struct-declarator 1386/// struct-declarator-list ',' struct-declarator 1387/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator 1388/// struct-declarator: 1389/// declarator 1390/// [GNU] declarator attributes[opt] 1391/// declarator[opt] ':' constant-expression 1392/// [GNU] declarator[opt] ':' constant-expression attributes[opt] 1393/// 1394void Parser:: 1395ParseStructDeclaration(DeclSpec &DS, 1396 llvm::SmallVectorImpl<FieldDeclarator> &Fields) { 1397 if (Tok.is(tok::kw___extension__)) { 1398 // __extension__ silences extension warnings in the subexpression. 1399 ExtensionRAIIObject O(Diags); // Use RAII to do this. 1400 ConsumeToken(); 1401 return ParseStructDeclaration(DS, Fields); 1402 } 1403 1404 // Parse the common specifier-qualifiers-list piece. 1405 SourceLocation DSStart = Tok.getLocation(); 1406 ParseSpecifierQualifierList(DS); 1407 1408 // If there are no declarators, this is a free-standing declaration 1409 // specifier. Let the actions module cope with it. 1410 if (Tok.is(tok::semi)) { 1411 Actions.ParsedFreeStandingDeclSpec(CurScope, DS); 1412 return; 1413 } 1414 1415 // Read struct-declarators until we find the semicolon. 1416 Fields.push_back(FieldDeclarator(DS)); 1417 while (1) { 1418 FieldDeclarator &DeclaratorInfo = Fields.back(); 1419 1420 /// struct-declarator: declarator 1421 /// struct-declarator: declarator[opt] ':' constant-expression 1422 if (Tok.isNot(tok::colon)) 1423 ParseDeclarator(DeclaratorInfo.D); 1424 1425 if (Tok.is(tok::colon)) { 1426 ConsumeToken(); 1427 OwningExprResult Res(ParseConstantExpression()); 1428 if (Res.isInvalid()) 1429 SkipUntil(tok::semi, true, true); 1430 else 1431 DeclaratorInfo.BitfieldSize = Res.release(); 1432 } 1433 1434 // If attributes exist after the declarator, parse them. 1435 if (Tok.is(tok::kw___attribute)) { 1436 SourceLocation Loc; 1437 AttributeList *AttrList = ParseAttributes(&Loc); 1438 DeclaratorInfo.D.AddAttributes(AttrList, Loc); 1439 } 1440 1441 // If we don't have a comma, it is either the end of the list (a ';') 1442 // or an error, bail out. 1443 if (Tok.isNot(tok::comma)) 1444 return; 1445 1446 // Consume the comma. 1447 ConsumeToken(); 1448 1449 // Parse the next declarator. 1450 Fields.push_back(FieldDeclarator(DS)); 1451 1452 // Attributes are only allowed on the second declarator. 1453 if (Tok.is(tok::kw___attribute)) { 1454 SourceLocation Loc; 1455 AttributeList *AttrList = ParseAttributes(&Loc); 1456 Fields.back().D.AddAttributes(AttrList, Loc); 1457 } 1458 } 1459} 1460 1461/// ParseStructUnionBody 1462/// struct-contents: 1463/// struct-declaration-list 1464/// [EXT] empty 1465/// [GNU] "struct-declaration-list" without terminatoring ';' 1466/// struct-declaration-list: 1467/// struct-declaration 1468/// struct-declaration-list struct-declaration 1469/// [OBC] '@' 'defs' '(' class-name ')' 1470/// 1471void Parser::ParseStructUnionBody(SourceLocation RecordLoc, 1472 unsigned TagType, DeclPtrTy TagDecl) { 1473 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions, 1474 PP.getSourceManager(), 1475 "parsing struct/union body"); 1476 1477 SourceLocation LBraceLoc = ConsumeBrace(); 1478 1479 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); 1480 Actions.ActOnTagStartDefinition(CurScope, TagDecl); 1481 1482 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in 1483 // C++. 1484 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) 1485 Diag(Tok, diag::ext_empty_struct_union_enum) 1486 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType); 1487 1488 llvm::SmallVector<DeclPtrTy, 32> FieldDecls; 1489 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; 1490 1491 // While we still have something to read, read the declarations in the struct. 1492 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 1493 // Each iteration of this loop reads one struct-declaration. 1494 1495 // Check for extraneous top-level semicolon. 1496 if (Tok.is(tok::semi)) { 1497 Diag(Tok, diag::ext_extra_struct_semi) 1498 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation())); 1499 ConsumeToken(); 1500 continue; 1501 } 1502 1503 // Parse all the comma separated declarators. 1504 DeclSpec DS; 1505 FieldDeclarators.clear(); 1506 if (!Tok.is(tok::at)) { 1507 ParseStructDeclaration(DS, FieldDeclarators); 1508 1509 // Convert them all to fields. 1510 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { 1511 FieldDeclarator &FD = FieldDeclarators[i]; 1512 DeclPtrTy Field; 1513 // Install the declarator into the current TagDecl. 1514 if (FD.D.getExtension()) { 1515 // Silences extension warnings 1516 ExtensionRAIIObject O(Diags); 1517 Field = Actions.ActOnField(CurScope, TagDecl, 1518 DS.getSourceRange().getBegin(), 1519 FD.D, FD.BitfieldSize); 1520 } else { 1521 Field = Actions.ActOnField(CurScope, TagDecl, 1522 DS.getSourceRange().getBegin(), 1523 FD.D, FD.BitfieldSize); 1524 } 1525 FieldDecls.push_back(Field); 1526 } 1527 } else { // Handle @defs 1528 ConsumeToken(); 1529 if (!Tok.isObjCAtKeyword(tok::objc_defs)) { 1530 Diag(Tok, diag::err_unexpected_at); 1531 SkipUntil(tok::semi, true, true); 1532 continue; 1533 } 1534 ConsumeToken(); 1535 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); 1536 if (!Tok.is(tok::identifier)) { 1537 Diag(Tok, diag::err_expected_ident); 1538 SkipUntil(tok::semi, true, true); 1539 continue; 1540 } 1541 llvm::SmallVector<DeclPtrTy, 16> Fields; 1542 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(), 1543 Tok.getIdentifierInfo(), Fields); 1544 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); 1545 ConsumeToken(); 1546 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); 1547 } 1548 1549 if (Tok.is(tok::semi)) { 1550 ConsumeToken(); 1551 } else if (Tok.is(tok::r_brace)) { 1552 Diag(Tok, diag::ext_expected_semi_decl_list); 1553 break; 1554 } else { 1555 Diag(Tok, diag::err_expected_semi_decl_list); 1556 // Skip to end of block or statement 1557 SkipUntil(tok::r_brace, true, true); 1558 } 1559 } 1560 1561 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 1562 1563 AttributeList *AttrList = 0; 1564 // If attributes exist after struct contents, parse them. 1565 if (Tok.is(tok::kw___attribute)) 1566 AttrList = ParseAttributes(); 1567 1568 Actions.ActOnFields(CurScope, 1569 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(), 1570 LBraceLoc, RBraceLoc, 1571 AttrList); 1572 StructScope.Exit(); 1573 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc); 1574} 1575 1576 1577/// ParseEnumSpecifier 1578/// enum-specifier: [C99 6.7.2.2] 1579/// 'enum' identifier[opt] '{' enumerator-list '}' 1580///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' 1581/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] 1582/// '}' attributes[opt] 1583/// 'enum' identifier 1584/// [GNU] 'enum' attributes[opt] identifier 1585/// 1586/// [C++] elaborated-type-specifier: 1587/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier 1588/// 1589void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, 1590 AccessSpecifier AS) { 1591 // Parse the tag portion of this. 1592 1593 AttributeList *Attr = 0; 1594 // If attributes exist after tag, parse them. 1595 if (Tok.is(tok::kw___attribute)) 1596 Attr = ParseAttributes(); 1597 1598 CXXScopeSpec SS; 1599 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) { 1600 if (Tok.isNot(tok::identifier)) { 1601 Diag(Tok, diag::err_expected_ident); 1602 if (Tok.isNot(tok::l_brace)) { 1603 // Has no name and is not a definition. 1604 // Skip the rest of this declarator, up until the comma or semicolon. 1605 SkipUntil(tok::comma, true); 1606 return; 1607 } 1608 } 1609 } 1610 1611 // Must have either 'enum name' or 'enum {...}'. 1612 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { 1613 Diag(Tok, diag::err_expected_ident_lbrace); 1614 1615 // Skip the rest of this declarator, up until the comma or semicolon. 1616 SkipUntil(tok::comma, true); 1617 return; 1618 } 1619 1620 // If an identifier is present, consume and remember it. 1621 IdentifierInfo *Name = 0; 1622 SourceLocation NameLoc; 1623 if (Tok.is(tok::identifier)) { 1624 Name = Tok.getIdentifierInfo(); 1625 NameLoc = ConsumeToken(); 1626 } 1627 1628 // There are three options here. If we have 'enum foo;', then this is a 1629 // forward declaration. If we have 'enum foo {...' then this is a 1630 // definition. Otherwise we have something like 'enum foo xyz', a reference. 1631 // 1632 // This is needed to handle stuff like this right (C99 6.7.2.3p11): 1633 // enum foo {..}; void bar() { enum foo; } <- new foo in bar. 1634 // enum foo {..}; void bar() { enum foo x; } <- use of old foo. 1635 // 1636 Action::TagUseKind TUK; 1637 if (Tok.is(tok::l_brace)) 1638 TUK = Action::TUK_Definition; 1639 else if (Tok.is(tok::semi)) 1640 TUK = Action::TUK_Declaration; 1641 else 1642 TUK = Action::TUK_Reference; 1643 bool Owned = false; 1644 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK, 1645 StartLoc, SS, Name, NameLoc, Attr, AS, 1646 Action::MultiTemplateParamsArg(Actions), 1647 Owned); 1648 1649 if (Tok.is(tok::l_brace)) 1650 ParseEnumBody(StartLoc, TagDecl); 1651 1652 // TODO: semantic analysis on the declspec for enums. 1653 const char *PrevSpec = 0; 1654 unsigned DiagID; 1655 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID, 1656 TagDecl.getAs<void>(), Owned)) 1657 Diag(StartLoc, DiagID) << PrevSpec; 1658} 1659 1660/// ParseEnumBody - Parse a {} enclosed enumerator-list. 1661/// enumerator-list: 1662/// enumerator 1663/// enumerator-list ',' enumerator 1664/// enumerator: 1665/// enumeration-constant 1666/// enumeration-constant '=' constant-expression 1667/// enumeration-constant: 1668/// identifier 1669/// 1670void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) { 1671 // Enter the scope of the enum body and start the definition. 1672 ParseScope EnumScope(this, Scope::DeclScope); 1673 Actions.ActOnTagStartDefinition(CurScope, EnumDecl); 1674 1675 SourceLocation LBraceLoc = ConsumeBrace(); 1676 1677 // C does not allow an empty enumerator-list, C++ does [dcl.enum]. 1678 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) 1679 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum"; 1680 1681 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls; 1682 1683 DeclPtrTy LastEnumConstDecl; 1684 1685 // Parse the enumerator-list. 1686 while (Tok.is(tok::identifier)) { 1687 IdentifierInfo *Ident = Tok.getIdentifierInfo(); 1688 SourceLocation IdentLoc = ConsumeToken(); 1689 1690 SourceLocation EqualLoc; 1691 OwningExprResult AssignedVal(Actions); 1692 if (Tok.is(tok::equal)) { 1693 EqualLoc = ConsumeToken(); 1694 AssignedVal = ParseConstantExpression(); 1695 if (AssignedVal.isInvalid()) 1696 SkipUntil(tok::comma, tok::r_brace, true, true); 1697 } 1698 1699 // Install the enumerator constant into EnumDecl. 1700 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, 1701 LastEnumConstDecl, 1702 IdentLoc, Ident, 1703 EqualLoc, 1704 AssignedVal.release()); 1705 EnumConstantDecls.push_back(EnumConstDecl); 1706 LastEnumConstDecl = EnumConstDecl; 1707 1708 if (Tok.isNot(tok::comma)) 1709 break; 1710 SourceLocation CommaLoc = ConsumeToken(); 1711 1712 if (Tok.isNot(tok::identifier) && 1713 !(getLang().C99 || getLang().CPlusPlus0x)) 1714 Diag(CommaLoc, diag::ext_enumerator_list_comma) 1715 << getLang().CPlusPlus 1716 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc))); 1717 } 1718 1719 // Eat the }. 1720 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 1721 1722 AttributeList *Attr = 0; 1723 // If attributes exist after the identifier list, parse them. 1724 if (Tok.is(tok::kw___attribute)) 1725 Attr = ParseAttributes(); 1726 1727 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl, 1728 EnumConstantDecls.data(), EnumConstantDecls.size(), 1729 CurScope, Attr); 1730 1731 EnumScope.Exit(); 1732 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc); 1733} 1734 1735/// isTypeSpecifierQualifier - Return true if the current token could be the 1736/// start of a type-qualifier-list. 1737bool Parser::isTypeQualifier() const { 1738 switch (Tok.getKind()) { 1739 default: return false; 1740 // type-qualifier 1741 case tok::kw_const: 1742 case tok::kw_volatile: 1743 case tok::kw_restrict: 1744 return true; 1745 } 1746} 1747 1748/// isTypeSpecifierQualifier - Return true if the current token could be the 1749/// start of a specifier-qualifier-list. 1750bool Parser::isTypeSpecifierQualifier() { 1751 switch (Tok.getKind()) { 1752 default: return false; 1753 1754 case tok::identifier: // foo::bar 1755 case tok::kw_typename: // typename T::type 1756 // Annotate typenames and C++ scope specifiers. If we get one, just 1757 // recurse to handle whatever we get. 1758 if (TryAnnotateTypeOrScopeToken()) 1759 return isTypeSpecifierQualifier(); 1760 // Otherwise, not a type specifier. 1761 return false; 1762 1763 case tok::coloncolon: // ::foo::bar 1764 if (NextToken().is(tok::kw_new) || // ::new 1765 NextToken().is(tok::kw_delete)) // ::delete 1766 return false; 1767 1768 // Annotate typenames and C++ scope specifiers. If we get one, just 1769 // recurse to handle whatever we get. 1770 if (TryAnnotateTypeOrScopeToken()) 1771 return isTypeSpecifierQualifier(); 1772 // Otherwise, not a type specifier. 1773 return false; 1774 1775 // GNU attributes support. 1776 case tok::kw___attribute: 1777 // GNU typeof support. 1778 case tok::kw_typeof: 1779 1780 // type-specifiers 1781 case tok::kw_short: 1782 case tok::kw_long: 1783 case tok::kw_signed: 1784 case tok::kw_unsigned: 1785 case tok::kw__Complex: 1786 case tok::kw__Imaginary: 1787 case tok::kw_void: 1788 case tok::kw_char: 1789 case tok::kw_wchar_t: 1790 case tok::kw_char16_t: 1791 case tok::kw_char32_t: 1792 case tok::kw_int: 1793 case tok::kw_float: 1794 case tok::kw_double: 1795 case tok::kw_bool: 1796 case tok::kw__Bool: 1797 case tok::kw__Decimal32: 1798 case tok::kw__Decimal64: 1799 case tok::kw__Decimal128: 1800 1801 // struct-or-union-specifier (C99) or class-specifier (C++) 1802 case tok::kw_class: 1803 case tok::kw_struct: 1804 case tok::kw_union: 1805 // enum-specifier 1806 case tok::kw_enum: 1807 1808 // type-qualifier 1809 case tok::kw_const: 1810 case tok::kw_volatile: 1811 case tok::kw_restrict: 1812 1813 // typedef-name 1814 case tok::annot_typename: 1815 return true; 1816 1817 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. 1818 case tok::less: 1819 return getLang().ObjC1; 1820 1821 case tok::kw___cdecl: 1822 case tok::kw___stdcall: 1823 case tok::kw___fastcall: 1824 case tok::kw___w64: 1825 case tok::kw___ptr64: 1826 return true; 1827 } 1828} 1829 1830/// isDeclarationSpecifier() - Return true if the current token is part of a 1831/// declaration specifier. 1832bool Parser::isDeclarationSpecifier() { 1833 switch (Tok.getKind()) { 1834 default: return false; 1835 1836 case tok::identifier: // foo::bar 1837 // Unfortunate hack to support "Class.factoryMethod" notation. 1838 if (getLang().ObjC1 && NextToken().is(tok::period)) 1839 return false; 1840 // Fall through 1841 1842 case tok::kw_typename: // typename T::type 1843 // Annotate typenames and C++ scope specifiers. If we get one, just 1844 // recurse to handle whatever we get. 1845 if (TryAnnotateTypeOrScopeToken()) 1846 return isDeclarationSpecifier(); 1847 // Otherwise, not a declaration specifier. 1848 return false; 1849 case tok::coloncolon: // ::foo::bar 1850 if (NextToken().is(tok::kw_new) || // ::new 1851 NextToken().is(tok::kw_delete)) // ::delete 1852 return false; 1853 1854 // Annotate typenames and C++ scope specifiers. If we get one, just 1855 // recurse to handle whatever we get. 1856 if (TryAnnotateTypeOrScopeToken()) 1857 return isDeclarationSpecifier(); 1858 // Otherwise, not a declaration specifier. 1859 return false; 1860 1861 // storage-class-specifier 1862 case tok::kw_typedef: 1863 case tok::kw_extern: 1864 case tok::kw___private_extern__: 1865 case tok::kw_static: 1866 case tok::kw_auto: 1867 case tok::kw_register: 1868 case tok::kw___thread: 1869 1870 // type-specifiers 1871 case tok::kw_short: 1872 case tok::kw_long: 1873 case tok::kw_signed: 1874 case tok::kw_unsigned: 1875 case tok::kw__Complex: 1876 case tok::kw__Imaginary: 1877 case tok::kw_void: 1878 case tok::kw_char: 1879 case tok::kw_wchar_t: 1880 case tok::kw_char16_t: 1881 case tok::kw_char32_t: 1882 1883 case tok::kw_int: 1884 case tok::kw_float: 1885 case tok::kw_double: 1886 case tok::kw_bool: 1887 case tok::kw__Bool: 1888 case tok::kw__Decimal32: 1889 case tok::kw__Decimal64: 1890 case tok::kw__Decimal128: 1891 1892 // struct-or-union-specifier (C99) or class-specifier (C++) 1893 case tok::kw_class: 1894 case tok::kw_struct: 1895 case tok::kw_union: 1896 // enum-specifier 1897 case tok::kw_enum: 1898 1899 // type-qualifier 1900 case tok::kw_const: 1901 case tok::kw_volatile: 1902 case tok::kw_restrict: 1903 1904 // function-specifier 1905 case tok::kw_inline: 1906 case tok::kw_virtual: 1907 case tok::kw_explicit: 1908 1909 // typedef-name 1910 case tok::annot_typename: 1911 1912 // GNU typeof support. 1913 case tok::kw_typeof: 1914 1915 // GNU attributes. 1916 case tok::kw___attribute: 1917 return true; 1918 1919 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. 1920 case tok::less: 1921 return getLang().ObjC1; 1922 1923 case tok::kw___declspec: 1924 case tok::kw___cdecl: 1925 case tok::kw___stdcall: 1926 case tok::kw___fastcall: 1927 case tok::kw___w64: 1928 case tok::kw___ptr64: 1929 case tok::kw___forceinline: 1930 return true; 1931 } 1932} 1933 1934 1935/// ParseTypeQualifierListOpt 1936/// type-qualifier-list: [C99 6.7.5] 1937/// type-qualifier 1938/// [GNU] attributes [ only if AttributesAllowed=true ] 1939/// type-qualifier-list type-qualifier 1940/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ] 1941/// 1942void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) { 1943 while (1) { 1944 bool isInvalid = false; 1945 const char *PrevSpec = 0; 1946 unsigned DiagID = 0; 1947 SourceLocation Loc = Tok.getLocation(); 1948 1949 switch (Tok.getKind()) { 1950 case tok::kw_const: 1951 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, 1952 getLang()); 1953 break; 1954 case tok::kw_volatile: 1955 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, 1956 getLang()); 1957 break; 1958 case tok::kw_restrict: 1959 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, 1960 getLang()); 1961 break; 1962 case tok::kw___w64: 1963 case tok::kw___ptr64: 1964 case tok::kw___cdecl: 1965 case tok::kw___stdcall: 1966 case tok::kw___fastcall: 1967 if (AttributesAllowed) { 1968 DS.AddAttributes(ParseMicrosoftTypeAttributes()); 1969 continue; 1970 } 1971 goto DoneWithTypeQuals; 1972 case tok::kw___attribute: 1973 if (AttributesAllowed) { 1974 DS.AddAttributes(ParseAttributes()); 1975 continue; // do *not* consume the next token! 1976 } 1977 // otherwise, FALL THROUGH! 1978 default: 1979 DoneWithTypeQuals: 1980 // If this is not a type-qualifier token, we're done reading type 1981 // qualifiers. First verify that DeclSpec's are consistent. 1982 DS.Finish(Diags, PP); 1983 return; 1984 } 1985 1986 // If the specifier combination wasn't legal, issue a diagnostic. 1987 if (isInvalid) { 1988 assert(PrevSpec && "Method did not return previous specifier!"); 1989 Diag(Tok, DiagID) << PrevSpec; 1990 } 1991 ConsumeToken(); 1992 } 1993} 1994 1995 1996/// ParseDeclarator - Parse and verify a newly-initialized declarator. 1997/// 1998void Parser::ParseDeclarator(Declarator &D) { 1999 /// This implements the 'declarator' production in the C grammar, then checks 2000 /// for well-formedness and issues diagnostics. 2001 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); 2002} 2003 2004/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator 2005/// is parsed by the function passed to it. Pass null, and the direct-declarator 2006/// isn't parsed at all, making this function effectively parse the C++ 2007/// ptr-operator production. 2008/// 2009/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] 2010/// [C] pointer[opt] direct-declarator 2011/// [C++] direct-declarator 2012/// [C++] ptr-operator declarator 2013/// 2014/// pointer: [C99 6.7.5] 2015/// '*' type-qualifier-list[opt] 2016/// '*' type-qualifier-list[opt] pointer 2017/// 2018/// ptr-operator: 2019/// '*' cv-qualifier-seq[opt] 2020/// '&' 2021/// [C++0x] '&&' 2022/// [GNU] '&' restrict[opt] attributes[opt] 2023/// [GNU?] '&&' restrict[opt] attributes[opt] 2024/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] 2025void Parser::ParseDeclaratorInternal(Declarator &D, 2026 DirectDeclParseFunction DirectDeclParser) { 2027 2028 if (Diags.hasAllExtensionsSilenced()) 2029 D.setExtension(); 2030 // C++ member pointers start with a '::' or a nested-name. 2031 // Member pointers get special handling, since there's no place for the 2032 // scope spec in the generic path below. 2033 if (getLang().CPlusPlus && 2034 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || 2035 Tok.is(tok::annot_cxxscope))) { 2036 CXXScopeSpec SS; 2037 if (ParseOptionalCXXScopeSpecifier(SS, true)) { 2038 if(Tok.isNot(tok::star)) { 2039 // The scope spec really belongs to the direct-declarator. 2040 D.getCXXScopeSpec() = SS; 2041 if (DirectDeclParser) 2042 (this->*DirectDeclParser)(D); 2043 return; 2044 } 2045 2046 SourceLocation Loc = ConsumeToken(); 2047 D.SetRangeEnd(Loc); 2048 DeclSpec DS; 2049 ParseTypeQualifierListOpt(DS); 2050 D.ExtendWithDeclSpec(DS); 2051 2052 // Recurse to parse whatever is left. 2053 ParseDeclaratorInternal(D, DirectDeclParser); 2054 2055 // Sema will have to catch (syntactically invalid) pointers into global 2056 // scope. It has to catch pointers into namespace scope anyway. 2057 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), 2058 Loc, DS.TakeAttributes()), 2059 /* Don't replace range end. */SourceLocation()); 2060 return; 2061 } 2062 } 2063 2064 tok::TokenKind Kind = Tok.getKind(); 2065 // Not a pointer, C++ reference, or block. 2066 if (Kind != tok::star && Kind != tok::caret && 2067 (Kind != tok::amp || !getLang().CPlusPlus) && 2068 // We parse rvalue refs in C++03, because otherwise the errors are scary. 2069 (Kind != tok::ampamp || !getLang().CPlusPlus)) { 2070 if (DirectDeclParser) 2071 (this->*DirectDeclParser)(D); 2072 return; 2073 } 2074 2075 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, 2076 // '&&' -> rvalue reference 2077 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. 2078 D.SetRangeEnd(Loc); 2079 2080 if (Kind == tok::star || Kind == tok::caret) { 2081 // Is a pointer. 2082 DeclSpec DS; 2083 2084 ParseTypeQualifierListOpt(DS); 2085 D.ExtendWithDeclSpec(DS); 2086 2087 // Recursively parse the declarator. 2088 ParseDeclaratorInternal(D, DirectDeclParser); 2089 if (Kind == tok::star) 2090 // Remember that we parsed a pointer type, and remember the type-quals. 2091 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, 2092 DS.TakeAttributes()), 2093 SourceLocation()); 2094 else 2095 // Remember that we parsed a Block type, and remember the type-quals. 2096 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), 2097 Loc, DS.TakeAttributes()), 2098 SourceLocation()); 2099 } else { 2100 // Is a reference 2101 DeclSpec DS; 2102 2103 // Complain about rvalue references in C++03, but then go on and build 2104 // the declarator. 2105 if (Kind == tok::ampamp && !getLang().CPlusPlus0x) 2106 Diag(Loc, diag::err_rvalue_reference); 2107 2108 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the 2109 // cv-qualifiers are introduced through the use of a typedef or of a 2110 // template type argument, in which case the cv-qualifiers are ignored. 2111 // 2112 // [GNU] Retricted references are allowed. 2113 // [GNU] Attributes on references are allowed. 2114 ParseTypeQualifierListOpt(DS); 2115 D.ExtendWithDeclSpec(DS); 2116 2117 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { 2118 if (DS.getTypeQualifiers() & DeclSpec::TQ_const) 2119 Diag(DS.getConstSpecLoc(), 2120 diag::err_invalid_reference_qualifier_application) << "const"; 2121 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) 2122 Diag(DS.getVolatileSpecLoc(), 2123 diag::err_invalid_reference_qualifier_application) << "volatile"; 2124 } 2125 2126 // Recursively parse the declarator. 2127 ParseDeclaratorInternal(D, DirectDeclParser); 2128 2129 if (D.getNumTypeObjects() > 0) { 2130 // C++ [dcl.ref]p4: There shall be no references to references. 2131 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); 2132 if (InnerChunk.Kind == DeclaratorChunk::Reference) { 2133 if (const IdentifierInfo *II = D.getIdentifier()) 2134 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) 2135 << II; 2136 else 2137 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) 2138 << "type name"; 2139 2140 // Once we've complained about the reference-to-reference, we 2141 // can go ahead and build the (technically ill-formed) 2142 // declarator: reference collapsing will take care of it. 2143 } 2144 } 2145 2146 // Remember that we parsed a reference type. It doesn't have type-quals. 2147 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, 2148 DS.TakeAttributes(), 2149 Kind == tok::amp), 2150 SourceLocation()); 2151 } 2152} 2153 2154/// ParseDirectDeclarator 2155/// direct-declarator: [C99 6.7.5] 2156/// [C99] identifier 2157/// '(' declarator ')' 2158/// [GNU] '(' attributes declarator ')' 2159/// [C90] direct-declarator '[' constant-expression[opt] ']' 2160/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' 2161/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' 2162/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' 2163/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' 2164/// direct-declarator '(' parameter-type-list ')' 2165/// direct-declarator '(' identifier-list[opt] ')' 2166/// [GNU] direct-declarator '(' parameter-forward-declarations 2167/// parameter-type-list[opt] ')' 2168/// [C++] direct-declarator '(' parameter-declaration-clause ')' 2169/// cv-qualifier-seq[opt] exception-specification[opt] 2170/// [C++] declarator-id 2171/// 2172/// declarator-id: [C++ 8] 2173/// id-expression 2174/// '::'[opt] nested-name-specifier[opt] type-name 2175/// 2176/// id-expression: [C++ 5.1] 2177/// unqualified-id 2178/// qualified-id [TODO] 2179/// 2180/// unqualified-id: [C++ 5.1] 2181/// identifier 2182/// operator-function-id 2183/// conversion-function-id [TODO] 2184/// '~' class-name 2185/// template-id 2186/// 2187void Parser::ParseDirectDeclarator(Declarator &D) { 2188 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); 2189 2190 if (getLang().CPlusPlus) { 2191 if (D.mayHaveIdentifier()) { 2192 // ParseDeclaratorInternal might already have parsed the scope. 2193 bool afterCXXScope = D.getCXXScopeSpec().isSet() || 2194 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), true); 2195 if (afterCXXScope) { 2196 // Change the declaration context for name lookup, until this function 2197 // is exited (and the declarator has been parsed). 2198 DeclScopeObj.EnterDeclaratorScope(); 2199 } 2200 2201 if (Tok.is(tok::identifier)) { 2202 assert(Tok.getIdentifierInfo() && "Not an identifier?"); 2203 2204 // If this identifier is the name of the current class, it's a 2205 // constructor name. 2206 if (!D.getDeclSpec().hasTypeSpecifier() && 2207 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) { 2208 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0; 2209 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(), 2210 Tok.getLocation(), CurScope, SS), 2211 Tok.getLocation()); 2212 // This is a normal identifier. 2213 } else 2214 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 2215 ConsumeToken(); 2216 goto PastIdentifier; 2217 } else if (Tok.is(tok::annot_template_id)) { 2218 TemplateIdAnnotation *TemplateId 2219 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 2220 2221 // FIXME: Could this template-id name a constructor? 2222 2223 // FIXME: This is an egregious hack, where we silently ignore 2224 // the specialization (which should be a function template 2225 // specialization name) and use the name instead. This hack 2226 // will go away when we have support for function 2227 // specializations. 2228 D.SetIdentifier(TemplateId->Name, Tok.getLocation()); 2229 TemplateId->Destroy(); 2230 ConsumeToken(); 2231 goto PastIdentifier; 2232 } else if (Tok.is(tok::kw_operator)) { 2233 SourceLocation OperatorLoc = Tok.getLocation(); 2234 SourceLocation EndLoc; 2235 2236 // First try the name of an overloaded operator 2237 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) { 2238 D.setOverloadedOperator(Op, OperatorLoc, EndLoc); 2239 } else { 2240 // This must be a conversion function (C++ [class.conv.fct]). 2241 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc)) 2242 D.setConversionFunction(ConvType, OperatorLoc, EndLoc); 2243 else { 2244 D.SetIdentifier(0, Tok.getLocation()); 2245 } 2246 } 2247 goto PastIdentifier; 2248 } else if (Tok.is(tok::tilde)) { 2249 // This should be a C++ destructor. 2250 SourceLocation TildeLoc = ConsumeToken(); 2251 if (Tok.is(tok::identifier)) { 2252 // FIXME: Inaccurate. 2253 SourceLocation NameLoc = Tok.getLocation(); 2254 SourceLocation EndLoc; 2255 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0; 2256 TypeResult Type = ParseClassName(EndLoc, SS, true); 2257 if (Type.isInvalid()) 2258 D.SetIdentifier(0, TildeLoc); 2259 else 2260 D.setDestructor(Type.get(), TildeLoc, NameLoc); 2261 } else { 2262 Diag(Tok, diag::err_destructor_class_name); 2263 D.SetIdentifier(0, TildeLoc); 2264 } 2265 goto PastIdentifier; 2266 } 2267 2268 // If we reached this point, token is not identifier and not '~'. 2269 2270 if (afterCXXScope) { 2271 Diag(Tok, diag::err_expected_unqualified_id); 2272 D.SetIdentifier(0, Tok.getLocation()); 2273 D.setInvalidType(true); 2274 goto PastIdentifier; 2275 } 2276 } 2277 } 2278 2279 // If we reached this point, we are either in C/ObjC or the token didn't 2280 // satisfy any of the C++-specific checks. 2281 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { 2282 assert(!getLang().CPlusPlus && 2283 "There's a C++-specific check for tok::identifier above"); 2284 assert(Tok.getIdentifierInfo() && "Not an identifier?"); 2285 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 2286 ConsumeToken(); 2287 } else if (Tok.is(tok::l_paren)) { 2288 // direct-declarator: '(' declarator ')' 2289 // direct-declarator: '(' attributes declarator ')' 2290 // Example: 'char (*X)' or 'int (*XX)(void)' 2291 ParseParenDeclarator(D); 2292 } else if (D.mayOmitIdentifier()) { 2293 // This could be something simple like "int" (in which case the declarator 2294 // portion is empty), if an abstract-declarator is allowed. 2295 D.SetIdentifier(0, Tok.getLocation()); 2296 } else { 2297 if (D.getContext() == Declarator::MemberContext) 2298 Diag(Tok, diag::err_expected_member_name_or_semi) 2299 << D.getDeclSpec().getSourceRange(); 2300 else if (getLang().CPlusPlus) 2301 Diag(Tok, diag::err_expected_unqualified_id); 2302 else 2303 Diag(Tok, diag::err_expected_ident_lparen); 2304 D.SetIdentifier(0, Tok.getLocation()); 2305 D.setInvalidType(true); 2306 } 2307 2308 PastIdentifier: 2309 assert(D.isPastIdentifier() && 2310 "Haven't past the location of the identifier yet?"); 2311 2312 while (1) { 2313 if (Tok.is(tok::l_paren)) { 2314 // The paren may be part of a C++ direct initializer, eg. "int x(1);". 2315 // In such a case, check if we actually have a function declarator; if it 2316 // is not, the declarator has been fully parsed. 2317 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { 2318 // When not in file scope, warn for ambiguous function declarators, just 2319 // in case the author intended it as a variable definition. 2320 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; 2321 if (!isCXXFunctionDeclarator(warnIfAmbiguous)) 2322 break; 2323 } 2324 ParseFunctionDeclarator(ConsumeParen(), D); 2325 } else if (Tok.is(tok::l_square)) { 2326 ParseBracketDeclarator(D); 2327 } else { 2328 break; 2329 } 2330 } 2331} 2332 2333/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is 2334/// only called before the identifier, so these are most likely just grouping 2335/// parens for precedence. If we find that these are actually function 2336/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. 2337/// 2338/// direct-declarator: 2339/// '(' declarator ')' 2340/// [GNU] '(' attributes declarator ')' 2341/// direct-declarator '(' parameter-type-list ')' 2342/// direct-declarator '(' identifier-list[opt] ')' 2343/// [GNU] direct-declarator '(' parameter-forward-declarations 2344/// parameter-type-list[opt] ')' 2345/// 2346void Parser::ParseParenDeclarator(Declarator &D) { 2347 SourceLocation StartLoc = ConsumeParen(); 2348 assert(!D.isPastIdentifier() && "Should be called before passing identifier"); 2349 2350 // Eat any attributes before we look at whether this is a grouping or function 2351 // declarator paren. If this is a grouping paren, the attribute applies to 2352 // the type being built up, for example: 2353 // int (__attribute__(()) *x)(long y) 2354 // If this ends up not being a grouping paren, the attribute applies to the 2355 // first argument, for example: 2356 // int (__attribute__(()) int x) 2357 // In either case, we need to eat any attributes to be able to determine what 2358 // sort of paren this is. 2359 // 2360 AttributeList *AttrList = 0; 2361 bool RequiresArg = false; 2362 if (Tok.is(tok::kw___attribute)) { 2363 AttrList = ParseAttributes(); 2364 2365 // We require that the argument list (if this is a non-grouping paren) be 2366 // present even if the attribute list was empty. 2367 RequiresArg = true; 2368 } 2369 // Eat any Microsoft extensions. 2370 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || 2371 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) || 2372 Tok.is(tok::kw___ptr64)) { 2373 AttrList = ParseMicrosoftTypeAttributes(AttrList); 2374 } 2375 2376 // If we haven't past the identifier yet (or where the identifier would be 2377 // stored, if this is an abstract declarator), then this is probably just 2378 // grouping parens. However, if this could be an abstract-declarator, then 2379 // this could also be the start of function arguments (consider 'void()'). 2380 bool isGrouping; 2381 2382 if (!D.mayOmitIdentifier()) { 2383 // If this can't be an abstract-declarator, this *must* be a grouping 2384 // paren, because we haven't seen the identifier yet. 2385 isGrouping = true; 2386 } else if (Tok.is(tok::r_paren) || // 'int()' is a function. 2387 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) 2388 isDeclarationSpecifier()) { // 'int(int)' is a function. 2389 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is 2390 // considered to be a type, not a K&R identifier-list. 2391 isGrouping = false; 2392 } else { 2393 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. 2394 isGrouping = true; 2395 } 2396 2397 // If this is a grouping paren, handle: 2398 // direct-declarator: '(' declarator ')' 2399 // direct-declarator: '(' attributes declarator ')' 2400 if (isGrouping) { 2401 bool hadGroupingParens = D.hasGroupingParens(); 2402 D.setGroupingParens(true); 2403 if (AttrList) 2404 D.AddAttributes(AttrList, SourceLocation()); 2405 2406 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); 2407 // Match the ')'. 2408 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc); 2409 2410 D.setGroupingParens(hadGroupingParens); 2411 D.SetRangeEnd(Loc); 2412 return; 2413 } 2414 2415 // Okay, if this wasn't a grouping paren, it must be the start of a function 2416 // argument list. Recognize that this declarator will never have an 2417 // identifier (and remember where it would have been), then call into 2418 // ParseFunctionDeclarator to handle of argument list. 2419 D.SetIdentifier(0, Tok.getLocation()); 2420 2421 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg); 2422} 2423 2424/// ParseFunctionDeclarator - We are after the identifier and have parsed the 2425/// declarator D up to a paren, which indicates that we are parsing function 2426/// arguments. 2427/// 2428/// If AttrList is non-null, then the caller parsed those arguments immediately 2429/// after the open paren - they should be considered to be the first argument of 2430/// a parameter. If RequiresArg is true, then the first argument of the 2431/// function is required to be present and required to not be an identifier 2432/// list. 2433/// 2434/// This method also handles this portion of the grammar: 2435/// parameter-type-list: [C99 6.7.5] 2436/// parameter-list 2437/// parameter-list ',' '...' 2438/// 2439/// parameter-list: [C99 6.7.5] 2440/// parameter-declaration 2441/// parameter-list ',' parameter-declaration 2442/// 2443/// parameter-declaration: [C99 6.7.5] 2444/// declaration-specifiers declarator 2445/// [C++] declaration-specifiers declarator '=' assignment-expression 2446/// [GNU] declaration-specifiers declarator attributes 2447/// declaration-specifiers abstract-declarator[opt] 2448/// [C++] declaration-specifiers abstract-declarator[opt] 2449/// '=' assignment-expression 2450/// [GNU] declaration-specifiers abstract-declarator[opt] attributes 2451/// 2452/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]" 2453/// and "exception-specification[opt]". 2454/// 2455void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, 2456 AttributeList *AttrList, 2457 bool RequiresArg) { 2458 // lparen is already consumed! 2459 assert(D.isPastIdentifier() && "Should not call before identifier!"); 2460 2461 // This parameter list may be empty. 2462 if (Tok.is(tok::r_paren)) { 2463 if (RequiresArg) { 2464 Diag(Tok, diag::err_argument_required_after_attribute); 2465 delete AttrList; 2466 } 2467 2468 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'. 2469 SourceLocation EndLoc = RParenLoc; 2470 2471 // cv-qualifier-seq[opt]. 2472 DeclSpec DS; 2473 bool hasExceptionSpec = false; 2474 SourceLocation ThrowLoc; 2475 bool hasAnyExceptionSpec = false; 2476 llvm::SmallVector<TypeTy*, 2> Exceptions; 2477 llvm::SmallVector<SourceRange, 2> ExceptionRanges; 2478 if (getLang().CPlusPlus) { 2479 ParseTypeQualifierListOpt(DS, false /*no attributes*/); 2480 if (!DS.getSourceRange().getEnd().isInvalid()) 2481 EndLoc = DS.getSourceRange().getEnd(); 2482 2483 // Parse exception-specification[opt]. 2484 if (Tok.is(tok::kw_throw)) { 2485 hasExceptionSpec = true; 2486 ThrowLoc = Tok.getLocation(); 2487 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges, 2488 hasAnyExceptionSpec); 2489 assert(Exceptions.size() == ExceptionRanges.size() && 2490 "Produced different number of exception types and ranges."); 2491 } 2492 } 2493 2494 // Remember that we parsed a function type, and remember the attributes. 2495 // int() -> no prototype, no '...'. 2496 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, 2497 /*variadic*/ false, 2498 SourceLocation(), 2499 /*arglist*/ 0, 0, 2500 DS.getTypeQualifiers(), 2501 hasExceptionSpec, ThrowLoc, 2502 hasAnyExceptionSpec, 2503 Exceptions.data(), 2504 ExceptionRanges.data(), 2505 Exceptions.size(), 2506 LParenLoc, RParenLoc, D), 2507 EndLoc); 2508 return; 2509 } 2510 2511 // Alternatively, this parameter list may be an identifier list form for a 2512 // K&R-style function: void foo(a,b,c) 2513 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) { 2514 if (!TryAnnotateTypeOrScopeToken()) { 2515 // K&R identifier lists can't have typedefs as identifiers, per 2516 // C99 6.7.5.3p11. 2517 if (RequiresArg) { 2518 Diag(Tok, diag::err_argument_required_after_attribute); 2519 delete AttrList; 2520 } 2521 // Identifier list. Note that '(' identifier-list ')' is only allowed for 2522 // normal declarators, not for abstract-declarators. 2523 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D); 2524 } 2525 } 2526 2527 // Finally, a normal, non-empty parameter type list. 2528 2529 // Build up an array of information about the parsed arguments. 2530 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; 2531 2532 // Enter function-declaration scope, limiting any declarators to the 2533 // function prototype scope, including parameter declarators. 2534 ParseScope PrototypeScope(this, 2535 Scope::FunctionPrototypeScope|Scope::DeclScope); 2536 2537 bool IsVariadic = false; 2538 SourceLocation EllipsisLoc; 2539 while (1) { 2540 if (Tok.is(tok::ellipsis)) { 2541 IsVariadic = true; 2542 EllipsisLoc = ConsumeToken(); // Consume the ellipsis. 2543 break; 2544 } 2545 2546 SourceLocation DSStart = Tok.getLocation(); 2547 2548 // Parse the declaration-specifiers. 2549 DeclSpec DS; 2550 2551 // If the caller parsed attributes for the first argument, add them now. 2552 if (AttrList) { 2553 DS.AddAttributes(AttrList); 2554 AttrList = 0; // Only apply the attributes to the first parameter. 2555 } 2556 ParseDeclarationSpecifiers(DS); 2557 2558 // Parse the declarator. This is "PrototypeContext", because we must 2559 // accept either 'declarator' or 'abstract-declarator' here. 2560 Declarator ParmDecl(DS, Declarator::PrototypeContext); 2561 ParseDeclarator(ParmDecl); 2562 2563 // Parse GNU attributes, if present. 2564 if (Tok.is(tok::kw___attribute)) { 2565 SourceLocation Loc; 2566 AttributeList *AttrList = ParseAttributes(&Loc); 2567 ParmDecl.AddAttributes(AttrList, Loc); 2568 } 2569 2570 // Remember this parsed parameter in ParamInfo. 2571 IdentifierInfo *ParmII = ParmDecl.getIdentifier(); 2572 2573 // DefArgToks is used when the parsing of default arguments needs 2574 // to be delayed. 2575 CachedTokens *DefArgToks = 0; 2576 2577 // If no parameter was specified, verify that *something* was specified, 2578 // otherwise we have a missing type and identifier. 2579 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && 2580 ParmDecl.getNumTypeObjects() == 0) { 2581 // Completely missing, emit error. 2582 Diag(DSStart, diag::err_missing_param); 2583 } else { 2584 // Otherwise, we have something. Add it and let semantic analysis try 2585 // to grok it and add the result to the ParamInfo we are building. 2586 2587 // Inform the actions module about the parameter declarator, so it gets 2588 // added to the current scope. 2589 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl); 2590 2591 // Parse the default argument, if any. We parse the default 2592 // arguments in all dialects; the semantic analysis in 2593 // ActOnParamDefaultArgument will reject the default argument in 2594 // C. 2595 if (Tok.is(tok::equal)) { 2596 SourceLocation EqualLoc = Tok.getLocation(); 2597 2598 // Parse the default argument 2599 if (D.getContext() == Declarator::MemberContext) { 2600 // If we're inside a class definition, cache the tokens 2601 // corresponding to the default argument. We'll actually parse 2602 // them when we see the end of the class definition. 2603 // FIXME: Templates will require something similar. 2604 // FIXME: Can we use a smart pointer for Toks? 2605 DefArgToks = new CachedTokens; 2606 2607 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, 2608 tok::semi, false)) { 2609 delete DefArgToks; 2610 DefArgToks = 0; 2611 Actions.ActOnParamDefaultArgumentError(Param); 2612 } else 2613 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, 2614 (*DefArgToks)[1].getLocation()); 2615 } else { 2616 // Consume the '='. 2617 ConsumeToken(); 2618 2619 OwningExprResult DefArgResult(ParseAssignmentExpression()); 2620 if (DefArgResult.isInvalid()) { 2621 Actions.ActOnParamDefaultArgumentError(Param); 2622 SkipUntil(tok::comma, tok::r_paren, true, true); 2623 } else { 2624 // Inform the actions module about the default argument 2625 Actions.ActOnParamDefaultArgument(Param, EqualLoc, 2626 move(DefArgResult)); 2627 } 2628 } 2629 } 2630 2631 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, 2632 ParmDecl.getIdentifierLoc(), Param, 2633 DefArgToks)); 2634 } 2635 2636 // If the next token is a comma, consume it and keep reading arguments. 2637 if (Tok.isNot(tok::comma)) break; 2638 2639 // Consume the comma. 2640 ConsumeToken(); 2641 } 2642 2643 // Leave prototype scope. 2644 PrototypeScope.Exit(); 2645 2646 // If we have the closing ')', eat it. 2647 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2648 SourceLocation EndLoc = RParenLoc; 2649 2650 DeclSpec DS; 2651 bool hasExceptionSpec = false; 2652 SourceLocation ThrowLoc; 2653 bool hasAnyExceptionSpec = false; 2654 llvm::SmallVector<TypeTy*, 2> Exceptions; 2655 llvm::SmallVector<SourceRange, 2> ExceptionRanges; 2656 if (getLang().CPlusPlus) { 2657 // Parse cv-qualifier-seq[opt]. 2658 ParseTypeQualifierListOpt(DS, false /*no attributes*/); 2659 if (!DS.getSourceRange().getEnd().isInvalid()) 2660 EndLoc = DS.getSourceRange().getEnd(); 2661 2662 // Parse exception-specification[opt]. 2663 if (Tok.is(tok::kw_throw)) { 2664 hasExceptionSpec = true; 2665 ThrowLoc = Tok.getLocation(); 2666 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges, 2667 hasAnyExceptionSpec); 2668 assert(Exceptions.size() == ExceptionRanges.size() && 2669 "Produced different number of exception types and ranges."); 2670 } 2671 } 2672 2673 // Remember that we parsed a function type, and remember the attributes. 2674 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, 2675 EllipsisLoc, 2676 ParamInfo.data(), ParamInfo.size(), 2677 DS.getTypeQualifiers(), 2678 hasExceptionSpec, ThrowLoc, 2679 hasAnyExceptionSpec, 2680 Exceptions.data(), 2681 ExceptionRanges.data(), 2682 Exceptions.size(), 2683 LParenLoc, RParenLoc, D), 2684 EndLoc); 2685} 2686 2687/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator 2688/// we found a K&R-style identifier list instead of a type argument list. The 2689/// current token is known to be the first identifier in the list. 2690/// 2691/// identifier-list: [C99 6.7.5] 2692/// identifier 2693/// identifier-list ',' identifier 2694/// 2695void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, 2696 Declarator &D) { 2697 // Build up an array of information about the parsed arguments. 2698 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; 2699 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; 2700 2701 // If there was no identifier specified for the declarator, either we are in 2702 // an abstract-declarator, or we are in a parameter declarator which was found 2703 // to be abstract. In abstract-declarators, identifier lists are not valid: 2704 // diagnose this. 2705 if (!D.getIdentifier()) 2706 Diag(Tok, diag::ext_ident_list_in_param); 2707 2708 // Tok is known to be the first identifier in the list. Remember this 2709 // identifier in ParamInfo. 2710 ParamsSoFar.insert(Tok.getIdentifierInfo()); 2711 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), 2712 Tok.getLocation(), 2713 DeclPtrTy())); 2714 2715 ConsumeToken(); // eat the first identifier. 2716 2717 while (Tok.is(tok::comma)) { 2718 // Eat the comma. 2719 ConsumeToken(); 2720 2721 // If this isn't an identifier, report the error and skip until ')'. 2722 if (Tok.isNot(tok::identifier)) { 2723 Diag(Tok, diag::err_expected_ident); 2724 SkipUntil(tok::r_paren); 2725 return; 2726 } 2727 2728 IdentifierInfo *ParmII = Tok.getIdentifierInfo(); 2729 2730 // Reject 'typedef int y; int test(x, y)', but continue parsing. 2731 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope)) 2732 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; 2733 2734 // Verify that the argument identifier has not already been mentioned. 2735 if (!ParamsSoFar.insert(ParmII)) { 2736 Diag(Tok, diag::err_param_redefinition) << ParmII; 2737 } else { 2738 // Remember this identifier in ParamInfo. 2739 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, 2740 Tok.getLocation(), 2741 DeclPtrTy())); 2742 } 2743 2744 // Eat the identifier. 2745 ConsumeToken(); 2746 } 2747 2748 // If we have the closing ')', eat it and we're done. 2749 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2750 2751 // Remember that we parsed a function type, and remember the attributes. This 2752 // function type is always a K&R style function type, which is not varargs and 2753 // has no prototype. 2754 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, 2755 SourceLocation(), 2756 &ParamInfo[0], ParamInfo.size(), 2757 /*TypeQuals*/0, 2758 /*exception*/false, 2759 SourceLocation(), false, 0, 0, 0, 2760 LParenLoc, RLoc, D), 2761 RLoc); 2762} 2763 2764/// [C90] direct-declarator '[' constant-expression[opt] ']' 2765/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' 2766/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' 2767/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' 2768/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' 2769void Parser::ParseBracketDeclarator(Declarator &D) { 2770 SourceLocation StartLoc = ConsumeBracket(); 2771 2772 // C array syntax has many features, but by-far the most common is [] and [4]. 2773 // This code does a fast path to handle some of the most obvious cases. 2774 if (Tok.getKind() == tok::r_square) { 2775 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); 2776 // Remember that we parsed the empty array type. 2777 OwningExprResult NumElements(Actions); 2778 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, 2779 StartLoc, EndLoc), 2780 EndLoc); 2781 return; 2782 } else if (Tok.getKind() == tok::numeric_constant && 2783 GetLookAheadToken(1).is(tok::r_square)) { 2784 // [4] is very common. Parse the numeric constant expression. 2785 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); 2786 ConsumeToken(); 2787 2788 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); 2789 2790 // If there was an error parsing the assignment-expression, recover. 2791 if (ExprRes.isInvalid()) 2792 ExprRes.release(); // Deallocate expr, just use []. 2793 2794 // Remember that we parsed a array type, and remember its features. 2795 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(), 2796 StartLoc, EndLoc), 2797 EndLoc); 2798 return; 2799 } 2800 2801 // If valid, this location is the position where we read the 'static' keyword. 2802 SourceLocation StaticLoc; 2803 if (Tok.is(tok::kw_static)) 2804 StaticLoc = ConsumeToken(); 2805 2806 // If there is a type-qualifier-list, read it now. 2807 // Type qualifiers in an array subscript are a C99 feature. 2808 DeclSpec DS; 2809 ParseTypeQualifierListOpt(DS, false /*no attributes*/); 2810 2811 // If we haven't already read 'static', check to see if there is one after the 2812 // type-qualifier-list. 2813 if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) 2814 StaticLoc = ConsumeToken(); 2815 2816 // Handle "direct-declarator [ type-qual-list[opt] * ]". 2817 bool isStar = false; 2818 OwningExprResult NumElements(Actions); 2819 2820 // Handle the case where we have '[*]' as the array size. However, a leading 2821 // star could be the start of an expression, for example 'X[*p + 4]'. Verify 2822 // the the token after the star is a ']'. Since stars in arrays are 2823 // infrequent, use of lookahead is not costly here. 2824 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { 2825 ConsumeToken(); // Eat the '*'. 2826 2827 if (StaticLoc.isValid()) { 2828 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); 2829 StaticLoc = SourceLocation(); // Drop the static. 2830 } 2831 isStar = true; 2832 } else if (Tok.isNot(tok::r_square)) { 2833 // Note, in C89, this production uses the constant-expr production instead 2834 // of assignment-expr. The only difference is that assignment-expr allows 2835 // things like '=' and '*='. Sema rejects these in C89 mode because they 2836 // are not i-c-e's, so we don't need to distinguish between the two here. 2837 2838 // Parse the constant-expression or assignment-expression now (depending 2839 // on dialect). 2840 if (getLang().CPlusPlus) 2841 NumElements = ParseConstantExpression(); 2842 else 2843 NumElements = ParseAssignmentExpression(); 2844 } 2845 2846 // If there was an error parsing the assignment-expression, recover. 2847 if (NumElements.isInvalid()) { 2848 D.setInvalidType(true); 2849 // If the expression was invalid, skip it. 2850 SkipUntil(tok::r_square); 2851 return; 2852 } 2853 2854 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); 2855 2856 // Remember that we parsed a array type, and remember its features. 2857 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), 2858 StaticLoc.isValid(), isStar, 2859 NumElements.release(), 2860 StartLoc, EndLoc), 2861 EndLoc); 2862} 2863 2864/// [GNU] typeof-specifier: 2865/// typeof ( expressions ) 2866/// typeof ( type-name ) 2867/// [GNU/C++] typeof unary-expression 2868/// 2869void Parser::ParseTypeofSpecifier(DeclSpec &DS) { 2870 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); 2871 Token OpTok = Tok; 2872 SourceLocation StartLoc = ConsumeToken(); 2873 2874 bool isCastExpr; 2875 TypeTy *CastTy; 2876 SourceRange CastRange; 2877 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok, 2878 isCastExpr, 2879 CastTy, 2880 CastRange); 2881 2882 if (CastRange.getEnd().isInvalid()) 2883 // FIXME: Not accurate, the range gets one token more than it should. 2884 DS.SetRangeEnd(Tok.getLocation()); 2885 else 2886 DS.SetRangeEnd(CastRange.getEnd()); 2887 2888 if (isCastExpr) { 2889 if (!CastTy) { 2890 DS.SetTypeSpecError(); 2891 return; 2892 } 2893 2894 const char *PrevSpec = 0; 2895 unsigned DiagID; 2896 // Check for duplicate type specifiers (e.g. "int typeof(int)"). 2897 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, 2898 DiagID, CastTy)) 2899 Diag(StartLoc, DiagID) << PrevSpec; 2900 return; 2901 } 2902 2903 // If we get here, the operand to the typeof was an expresion. 2904 if (Operand.isInvalid()) { 2905 DS.SetTypeSpecError(); 2906 return; 2907 } 2908 2909 const char *PrevSpec = 0; 2910 unsigned DiagID; 2911 // Check for duplicate type specifiers (e.g. "int typeof(int)"). 2912 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, 2913 DiagID, Operand.release())) 2914 Diag(StartLoc, DiagID) << PrevSpec; 2915} 2916