ParseDecl.cpp revision 82bf01061b97404fed8c422fc0eda0a380689cc9
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()) 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); 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()) 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 && CurScope->isClassScope() && 848 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) && 849 NextToken().getKind() == tok::l_paren) 850 goto DoneWithDeclSpec; 851 852 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, 853 DiagID, TypeRep); 854 if (isInvalid) 855 break; 856 857 DS.SetRangeEnd(Tok.getLocation()); 858 ConsumeToken(); // The identifier 859 860 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' 861 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an 862 // Objective-C interface. If we don't have Objective-C or a '<', this is 863 // just a normal reference to a typedef name. 864 if (!Tok.is(tok::less) || !getLang().ObjC1) 865 continue; 866 867 SourceLocation EndProtoLoc; 868 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; 869 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); 870 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size()); 871 872 DS.SetRangeEnd(EndProtoLoc); 873 874 // Need to support trailing type qualifiers (e.g. "id<p> const"). 875 // If a type specifier follows, it will be diagnosed elsewhere. 876 continue; 877 } 878 879 // type-name 880 case tok::annot_template_id: { 881 TemplateIdAnnotation *TemplateId 882 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 883 if (TemplateId->Kind != TNK_Type_template) { 884 // This template-id does not refer to a type name, so we're 885 // done with the type-specifiers. 886 goto DoneWithDeclSpec; 887 } 888 889 // Turn the template-id annotation token into a type annotation 890 // token, then try again to parse it as a type-specifier. 891 AnnotateTemplateIdTokenAsType(); 892 continue; 893 } 894 895 // GNU attributes support. 896 case tok::kw___attribute: 897 DS.AddAttributes(ParseAttributes()); 898 continue; 899 900 // Microsoft declspec support. 901 case tok::kw___declspec: 902 DS.AddAttributes(ParseMicrosoftDeclSpec()); 903 continue; 904 905 // Microsoft single token adornments. 906 case tok::kw___forceinline: 907 // FIXME: Add handling here! 908 break; 909 910 case tok::kw___ptr64: 911 case tok::kw___w64: 912 case tok::kw___cdecl: 913 case tok::kw___stdcall: 914 case tok::kw___fastcall: 915 DS.AddAttributes(ParseMicrosoftTypeAttributes()); 916 continue; 917 918 // storage-class-specifier 919 case tok::kw_typedef: 920 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec, 921 DiagID); 922 break; 923 case tok::kw_extern: 924 if (DS.isThreadSpecified()) 925 Diag(Tok, diag::ext_thread_before) << "extern"; 926 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec, 927 DiagID); 928 break; 929 case tok::kw___private_extern__: 930 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, 931 PrevSpec, DiagID); 932 break; 933 case tok::kw_static: 934 if (DS.isThreadSpecified()) 935 Diag(Tok, diag::ext_thread_before) << "static"; 936 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec, 937 DiagID); 938 break; 939 case tok::kw_auto: 940 if (getLang().CPlusPlus0x) 941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, 942 DiagID); 943 else 944 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec, 945 DiagID); 946 break; 947 case tok::kw_register: 948 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec, 949 DiagID); 950 break; 951 case tok::kw_mutable: 952 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec, 953 DiagID); 954 break; 955 case tok::kw___thread: 956 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); 957 break; 958 959 // function-specifier 960 case tok::kw_inline: 961 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); 962 break; 963 case tok::kw_virtual: 964 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); 965 break; 966 case tok::kw_explicit: 967 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); 968 break; 969 970 // friend 971 case tok::kw_friend: 972 if (DSContext == DSC_class) 973 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); 974 else { 975 PrevSpec = ""; // not actually used by the diagnostic 976 DiagID = diag::err_friend_invalid_in_context; 977 isInvalid = true; 978 } 979 break; 980 981 // type-specifier 982 case tok::kw_short: 983 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, 984 DiagID); 985 break; 986 case tok::kw_long: 987 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) 988 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, 989 DiagID); 990 else 991 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, 992 DiagID); 993 break; 994 case tok::kw_signed: 995 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, 996 DiagID); 997 break; 998 case tok::kw_unsigned: 999 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, 1000 DiagID); 1001 break; 1002 case tok::kw__Complex: 1003 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, 1004 DiagID); 1005 break; 1006 case tok::kw__Imaginary: 1007 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, 1008 DiagID); 1009 break; 1010 case tok::kw_void: 1011 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, 1012 DiagID); 1013 break; 1014 case tok::kw_char: 1015 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, 1016 DiagID); 1017 break; 1018 case tok::kw_int: 1019 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, 1020 DiagID); 1021 break; 1022 case tok::kw_float: 1023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, 1024 DiagID); 1025 break; 1026 case tok::kw_double: 1027 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, 1028 DiagID); 1029 break; 1030 case tok::kw_wchar_t: 1031 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, 1032 DiagID); 1033 break; 1034 case tok::kw_char16_t: 1035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, 1036 DiagID); 1037 break; 1038 case tok::kw_char32_t: 1039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, 1040 DiagID); 1041 break; 1042 case tok::kw_bool: 1043 case tok::kw__Bool: 1044 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, 1045 DiagID); 1046 break; 1047 case tok::kw__Decimal32: 1048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, 1049 DiagID); 1050 break; 1051 case tok::kw__Decimal64: 1052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, 1053 DiagID); 1054 break; 1055 case tok::kw__Decimal128: 1056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, 1057 DiagID); 1058 break; 1059 1060 // class-specifier: 1061 case tok::kw_class: 1062 case tok::kw_struct: 1063 case tok::kw_union: { 1064 tok::TokenKind Kind = Tok.getKind(); 1065 ConsumeToken(); 1066 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS); 1067 continue; 1068 } 1069 1070 // enum-specifier: 1071 case tok::kw_enum: 1072 ConsumeToken(); 1073 ParseEnumSpecifier(Loc, DS, AS); 1074 continue; 1075 1076 // cv-qualifier: 1077 case tok::kw_const: 1078 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, 1079 getLang()); 1080 break; 1081 case tok::kw_volatile: 1082 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, 1083 getLang()); 1084 break; 1085 case tok::kw_restrict: 1086 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, 1087 getLang()); 1088 break; 1089 1090 // C++ typename-specifier: 1091 case tok::kw_typename: 1092 if (TryAnnotateTypeOrScopeToken()) 1093 continue; 1094 break; 1095 1096 // GNU typeof support. 1097 case tok::kw_typeof: 1098 ParseTypeofSpecifier(DS); 1099 continue; 1100 1101 case tok::kw_decltype: 1102 ParseDecltypeSpecifier(DS); 1103 continue; 1104 1105 case tok::less: 1106 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for 1107 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, 1108 // but we support it. 1109 if (DS.hasTypeSpecifier() || !getLang().ObjC1) 1110 goto DoneWithDeclSpec; 1111 1112 { 1113 SourceLocation EndProtoLoc; 1114 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; 1115 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); 1116 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size()); 1117 DS.SetRangeEnd(EndProtoLoc); 1118 1119 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) 1120 << CodeModificationHint::CreateInsertion(Loc, "id") 1121 << SourceRange(Loc, EndProtoLoc); 1122 // Need to support trailing type qualifiers (e.g. "id<p> const"). 1123 // If a type specifier follows, it will be diagnosed elsewhere. 1124 continue; 1125 } 1126 } 1127 // If the specifier wasn't legal, issue a diagnostic. 1128 if (isInvalid) { 1129 assert(PrevSpec && "Method did not return previous specifier!"); 1130 assert(DiagID); 1131 Diag(Tok, DiagID) << PrevSpec; 1132 } 1133 DS.SetRangeEnd(Tok.getLocation()); 1134 ConsumeToken(); 1135 } 1136} 1137 1138/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We 1139/// primarily follow the C++ grammar with additions for C99 and GNU, 1140/// which together subsume the C grammar. Note that the C++ 1141/// type-specifier also includes the C type-qualifier (for const, 1142/// volatile, and C99 restrict). Returns true if a type-specifier was 1143/// found (and parsed), false otherwise. 1144/// 1145/// type-specifier: [C++ 7.1.5] 1146/// simple-type-specifier 1147/// class-specifier 1148/// enum-specifier 1149/// elaborated-type-specifier [TODO] 1150/// cv-qualifier 1151/// 1152/// cv-qualifier: [C++ 7.1.5.1] 1153/// 'const' 1154/// 'volatile' 1155/// [C99] 'restrict' 1156/// 1157/// simple-type-specifier: [ C++ 7.1.5.2] 1158/// '::'[opt] nested-name-specifier[opt] type-name [TODO] 1159/// '::'[opt] nested-name-specifier 'template' template-id [TODO] 1160/// 'char' 1161/// 'wchar_t' 1162/// 'bool' 1163/// 'short' 1164/// 'int' 1165/// 'long' 1166/// 'signed' 1167/// 'unsigned' 1168/// 'float' 1169/// 'double' 1170/// 'void' 1171/// [C99] '_Bool' 1172/// [C99] '_Complex' 1173/// [C99] '_Imaginary' // Removed in TC2? 1174/// [GNU] '_Decimal32' 1175/// [GNU] '_Decimal64' 1176/// [GNU] '_Decimal128' 1177/// [GNU] typeof-specifier 1178/// [OBJC] class-name objc-protocol-refs[opt] [TODO] 1179/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] 1180/// [C++0x] 'decltype' ( expression ) 1181bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid, 1182 const char *&PrevSpec, 1183 unsigned &DiagID, 1184 const ParsedTemplateInfo &TemplateInfo) { 1185 SourceLocation Loc = Tok.getLocation(); 1186 1187 switch (Tok.getKind()) { 1188 case tok::identifier: // foo::bar 1189 case tok::kw_typename: // typename foo::bar 1190 // Annotate typenames and C++ scope specifiers. If we get one, just 1191 // recurse to handle whatever we get. 1192 if (TryAnnotateTypeOrScopeToken()) 1193 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, 1194 TemplateInfo); 1195 // Otherwise, not a type specifier. 1196 return false; 1197 case tok::coloncolon: // ::foo::bar 1198 if (NextToken().is(tok::kw_new) || // ::new 1199 NextToken().is(tok::kw_delete)) // ::delete 1200 return false; 1201 1202 // Annotate typenames and C++ scope specifiers. If we get one, just 1203 // recurse to handle whatever we get. 1204 if (TryAnnotateTypeOrScopeToken()) 1205 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, 1206 TemplateInfo); 1207 // Otherwise, not a type specifier. 1208 return false; 1209 1210 // simple-type-specifier: 1211 case tok::annot_typename: { 1212 if (Tok.getAnnotationValue()) 1213 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, 1214 DiagID, Tok.getAnnotationValue()); 1215 else 1216 DS.SetTypeSpecError(); 1217 DS.SetRangeEnd(Tok.getAnnotationEndLoc()); 1218 ConsumeToken(); // The typename 1219 1220 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' 1221 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an 1222 // Objective-C interface. If we don't have Objective-C or a '<', this is 1223 // just a normal reference to a typedef name. 1224 if (!Tok.is(tok::less) || !getLang().ObjC1) 1225 return true; 1226 1227 SourceLocation EndProtoLoc; 1228 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl; 1229 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); 1230 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size()); 1231 1232 DS.SetRangeEnd(EndProtoLoc); 1233 return true; 1234 } 1235 1236 case tok::kw_short: 1237 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); 1238 break; 1239 case tok::kw_long: 1240 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) 1241 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, 1242 DiagID); 1243 else 1244 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, 1245 DiagID); 1246 break; 1247 case tok::kw_signed: 1248 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); 1249 break; 1250 case tok::kw_unsigned: 1251 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, 1252 DiagID); 1253 break; 1254 case tok::kw__Complex: 1255 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, 1256 DiagID); 1257 break; 1258 case tok::kw__Imaginary: 1259 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, 1260 DiagID); 1261 break; 1262 case tok::kw_void: 1263 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); 1264 break; 1265 case tok::kw_char: 1266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); 1267 break; 1268 case tok::kw_int: 1269 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); 1270 break; 1271 case tok::kw_float: 1272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); 1273 break; 1274 case tok::kw_double: 1275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); 1276 break; 1277 case tok::kw_wchar_t: 1278 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); 1279 break; 1280 case tok::kw_char16_t: 1281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); 1282 break; 1283 case tok::kw_char32_t: 1284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); 1285 break; 1286 case tok::kw_bool: 1287 case tok::kw__Bool: 1288 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); 1289 break; 1290 case tok::kw__Decimal32: 1291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, 1292 DiagID); 1293 break; 1294 case tok::kw__Decimal64: 1295 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, 1296 DiagID); 1297 break; 1298 case tok::kw__Decimal128: 1299 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, 1300 DiagID); 1301 break; 1302 1303 // class-specifier: 1304 case tok::kw_class: 1305 case tok::kw_struct: 1306 case tok::kw_union: { 1307 tok::TokenKind Kind = Tok.getKind(); 1308 ConsumeToken(); 1309 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo); 1310 return true; 1311 } 1312 1313 // enum-specifier: 1314 case tok::kw_enum: 1315 ConsumeToken(); 1316 ParseEnumSpecifier(Loc, DS); 1317 return true; 1318 1319 // cv-qualifier: 1320 case tok::kw_const: 1321 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, 1322 DiagID, getLang()); 1323 break; 1324 case tok::kw_volatile: 1325 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, 1326 DiagID, getLang()); 1327 break; 1328 case tok::kw_restrict: 1329 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, 1330 DiagID, getLang()); 1331 break; 1332 1333 // GNU typeof support. 1334 case tok::kw_typeof: 1335 ParseTypeofSpecifier(DS); 1336 return true; 1337 1338 // C++0x decltype support. 1339 case tok::kw_decltype: 1340 ParseDecltypeSpecifier(DS); 1341 return true; 1342 1343 // C++0x auto support. 1344 case tok::kw_auto: 1345 if (!getLang().CPlusPlus0x) 1346 return false; 1347 1348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID); 1349 break; 1350 case tok::kw___ptr64: 1351 case tok::kw___w64: 1352 case tok::kw___cdecl: 1353 case tok::kw___stdcall: 1354 case tok::kw___fastcall: 1355 DS.AddAttributes(ParseMicrosoftTypeAttributes()); 1356 return true; 1357 1358 default: 1359 // Not a type-specifier; do nothing. 1360 return false; 1361 } 1362 1363 // If the specifier combination wasn't legal, issue a diagnostic. 1364 if (isInvalid) { 1365 assert(PrevSpec && "Method did not return previous specifier!"); 1366 // Pick between error or extwarn. 1367 Diag(Tok, DiagID) << PrevSpec; 1368 } 1369 DS.SetRangeEnd(Tok.getLocation()); 1370 ConsumeToken(); // whatever we parsed above. 1371 return true; 1372} 1373 1374/// ParseStructDeclaration - Parse a struct declaration without the terminating 1375/// semicolon. 1376/// 1377/// struct-declaration: 1378/// specifier-qualifier-list struct-declarator-list 1379/// [GNU] __extension__ struct-declaration 1380/// [GNU] specifier-qualifier-list 1381/// struct-declarator-list: 1382/// struct-declarator 1383/// struct-declarator-list ',' struct-declarator 1384/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator 1385/// struct-declarator: 1386/// declarator 1387/// [GNU] declarator attributes[opt] 1388/// declarator[opt] ':' constant-expression 1389/// [GNU] declarator[opt] ':' constant-expression attributes[opt] 1390/// 1391void Parser:: 1392ParseStructDeclaration(DeclSpec &DS, 1393 llvm::SmallVectorImpl<FieldDeclarator> &Fields) { 1394 if (Tok.is(tok::kw___extension__)) { 1395 // __extension__ silences extension warnings in the subexpression. 1396 ExtensionRAIIObject O(Diags); // Use RAII to do this. 1397 ConsumeToken(); 1398 return ParseStructDeclaration(DS, Fields); 1399 } 1400 1401 // Parse the common specifier-qualifiers-list piece. 1402 SourceLocation DSStart = Tok.getLocation(); 1403 ParseSpecifierQualifierList(DS); 1404 1405 // If there are no declarators, this is a free-standing declaration 1406 // specifier. Let the actions module cope with it. 1407 if (Tok.is(tok::semi)) { 1408 Actions.ParsedFreeStandingDeclSpec(CurScope, DS); 1409 return; 1410 } 1411 1412 // Read struct-declarators until we find the semicolon. 1413 Fields.push_back(FieldDeclarator(DS)); 1414 while (1) { 1415 FieldDeclarator &DeclaratorInfo = Fields.back(); 1416 1417 /// struct-declarator: declarator 1418 /// struct-declarator: declarator[opt] ':' constant-expression 1419 if (Tok.isNot(tok::colon)) 1420 ParseDeclarator(DeclaratorInfo.D); 1421 1422 if (Tok.is(tok::colon)) { 1423 ConsumeToken(); 1424 OwningExprResult Res(ParseConstantExpression()); 1425 if (Res.isInvalid()) 1426 SkipUntil(tok::semi, true, true); 1427 else 1428 DeclaratorInfo.BitfieldSize = Res.release(); 1429 } 1430 1431 // If attributes exist after the declarator, parse them. 1432 if (Tok.is(tok::kw___attribute)) { 1433 SourceLocation Loc; 1434 AttributeList *AttrList = ParseAttributes(&Loc); 1435 DeclaratorInfo.D.AddAttributes(AttrList, Loc); 1436 } 1437 1438 // If we don't have a comma, it is either the end of the list (a ';') 1439 // or an error, bail out. 1440 if (Tok.isNot(tok::comma)) 1441 return; 1442 1443 // Consume the comma. 1444 ConsumeToken(); 1445 1446 // Parse the next declarator. 1447 Fields.push_back(FieldDeclarator(DS)); 1448 1449 // Attributes are only allowed on the second declarator. 1450 if (Tok.is(tok::kw___attribute)) { 1451 SourceLocation Loc; 1452 AttributeList *AttrList = ParseAttributes(&Loc); 1453 Fields.back().D.AddAttributes(AttrList, Loc); 1454 } 1455 } 1456} 1457 1458/// ParseStructUnionBody 1459/// struct-contents: 1460/// struct-declaration-list 1461/// [EXT] empty 1462/// [GNU] "struct-declaration-list" without terminatoring ';' 1463/// struct-declaration-list: 1464/// struct-declaration 1465/// struct-declaration-list struct-declaration 1466/// [OBC] '@' 'defs' '(' class-name ')' 1467/// 1468void Parser::ParseStructUnionBody(SourceLocation RecordLoc, 1469 unsigned TagType, DeclPtrTy TagDecl) { 1470 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions, 1471 PP.getSourceManager(), 1472 "parsing struct/union body"); 1473 1474 SourceLocation LBraceLoc = ConsumeBrace(); 1475 1476 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); 1477 Actions.ActOnTagStartDefinition(CurScope, TagDecl); 1478 1479 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in 1480 // C++. 1481 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) 1482 Diag(Tok, diag::ext_empty_struct_union_enum) 1483 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType); 1484 1485 llvm::SmallVector<DeclPtrTy, 32> FieldDecls; 1486 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; 1487 1488 // While we still have something to read, read the declarations in the struct. 1489 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 1490 // Each iteration of this loop reads one struct-declaration. 1491 1492 // Check for extraneous top-level semicolon. 1493 if (Tok.is(tok::semi)) { 1494 Diag(Tok, diag::ext_extra_struct_semi) 1495 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation())); 1496 ConsumeToken(); 1497 continue; 1498 } 1499 1500 // Parse all the comma separated declarators. 1501 DeclSpec DS; 1502 FieldDeclarators.clear(); 1503 if (!Tok.is(tok::at)) { 1504 ParseStructDeclaration(DS, FieldDeclarators); 1505 1506 // Convert them all to fields. 1507 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { 1508 FieldDeclarator &FD = FieldDeclarators[i]; 1509 // Install the declarator into the current TagDecl. 1510 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl, 1511 DS.getSourceRange().getBegin(), 1512 FD.D, FD.BitfieldSize); 1513 FieldDecls.push_back(Field); 1514 } 1515 } else { // Handle @defs 1516 ConsumeToken(); 1517 if (!Tok.isObjCAtKeyword(tok::objc_defs)) { 1518 Diag(Tok, diag::err_unexpected_at); 1519 SkipUntil(tok::semi, true, true); 1520 continue; 1521 } 1522 ConsumeToken(); 1523 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); 1524 if (!Tok.is(tok::identifier)) { 1525 Diag(Tok, diag::err_expected_ident); 1526 SkipUntil(tok::semi, true, true); 1527 continue; 1528 } 1529 llvm::SmallVector<DeclPtrTy, 16> Fields; 1530 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(), 1531 Tok.getIdentifierInfo(), Fields); 1532 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); 1533 ConsumeToken(); 1534 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); 1535 } 1536 1537 if (Tok.is(tok::semi)) { 1538 ConsumeToken(); 1539 } else if (Tok.is(tok::r_brace)) { 1540 Diag(Tok, diag::ext_expected_semi_decl_list); 1541 break; 1542 } else { 1543 Diag(Tok, diag::err_expected_semi_decl_list); 1544 // Skip to end of block or statement 1545 SkipUntil(tok::r_brace, true, true); 1546 } 1547 } 1548 1549 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 1550 1551 AttributeList *AttrList = 0; 1552 // If attributes exist after struct contents, parse them. 1553 if (Tok.is(tok::kw___attribute)) 1554 AttrList = ParseAttributes(); 1555 1556 Actions.ActOnFields(CurScope, 1557 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(), 1558 LBraceLoc, RBraceLoc, 1559 AttrList); 1560 StructScope.Exit(); 1561 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc); 1562} 1563 1564 1565/// ParseEnumSpecifier 1566/// enum-specifier: [C99 6.7.2.2] 1567/// 'enum' identifier[opt] '{' enumerator-list '}' 1568///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' 1569/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] 1570/// '}' attributes[opt] 1571/// 'enum' identifier 1572/// [GNU] 'enum' attributes[opt] identifier 1573/// 1574/// [C++] elaborated-type-specifier: 1575/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier 1576/// 1577void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, 1578 AccessSpecifier AS) { 1579 // Parse the tag portion of this. 1580 1581 AttributeList *Attr = 0; 1582 // If attributes exist after tag, parse them. 1583 if (Tok.is(tok::kw___attribute)) 1584 Attr = ParseAttributes(); 1585 1586 CXXScopeSpec SS; 1587 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) { 1588 if (Tok.isNot(tok::identifier)) { 1589 Diag(Tok, diag::err_expected_ident); 1590 if (Tok.isNot(tok::l_brace)) { 1591 // Has no name and is not a definition. 1592 // Skip the rest of this declarator, up until the comma or semicolon. 1593 SkipUntil(tok::comma, true); 1594 return; 1595 } 1596 } 1597 } 1598 1599 // Must have either 'enum name' or 'enum {...}'. 1600 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { 1601 Diag(Tok, diag::err_expected_ident_lbrace); 1602 1603 // Skip the rest of this declarator, up until the comma or semicolon. 1604 SkipUntil(tok::comma, true); 1605 return; 1606 } 1607 1608 // If an identifier is present, consume and remember it. 1609 IdentifierInfo *Name = 0; 1610 SourceLocation NameLoc; 1611 if (Tok.is(tok::identifier)) { 1612 Name = Tok.getIdentifierInfo(); 1613 NameLoc = ConsumeToken(); 1614 } 1615 1616 // There are three options here. If we have 'enum foo;', then this is a 1617 // forward declaration. If we have 'enum foo {...' then this is a 1618 // definition. Otherwise we have something like 'enum foo xyz', a reference. 1619 // 1620 // This is needed to handle stuff like this right (C99 6.7.2.3p11): 1621 // enum foo {..}; void bar() { enum foo; } <- new foo in bar. 1622 // enum foo {..}; void bar() { enum foo x; } <- use of old foo. 1623 // 1624 Action::TagUseKind TUK; 1625 if (Tok.is(tok::l_brace)) 1626 TUK = Action::TUK_Definition; 1627 else if (Tok.is(tok::semi)) 1628 TUK = Action::TUK_Declaration; 1629 else 1630 TUK = Action::TUK_Reference; 1631 bool Owned = false; 1632 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK, 1633 StartLoc, SS, Name, NameLoc, Attr, AS, 1634 Action::MultiTemplateParamsArg(Actions), 1635 Owned); 1636 1637 if (Tok.is(tok::l_brace)) 1638 ParseEnumBody(StartLoc, TagDecl); 1639 1640 // TODO: semantic analysis on the declspec for enums. 1641 const char *PrevSpec = 0; 1642 unsigned DiagID; 1643 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID, 1644 TagDecl.getAs<void>(), Owned)) 1645 Diag(StartLoc, DiagID) << PrevSpec; 1646} 1647 1648/// ParseEnumBody - Parse a {} enclosed enumerator-list. 1649/// enumerator-list: 1650/// enumerator 1651/// enumerator-list ',' enumerator 1652/// enumerator: 1653/// enumeration-constant 1654/// enumeration-constant '=' constant-expression 1655/// enumeration-constant: 1656/// identifier 1657/// 1658void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) { 1659 // Enter the scope of the enum body and start the definition. 1660 ParseScope EnumScope(this, Scope::DeclScope); 1661 Actions.ActOnTagStartDefinition(CurScope, EnumDecl); 1662 1663 SourceLocation LBraceLoc = ConsumeBrace(); 1664 1665 // C does not allow an empty enumerator-list, C++ does [dcl.enum]. 1666 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) 1667 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum"; 1668 1669 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls; 1670 1671 DeclPtrTy LastEnumConstDecl; 1672 1673 // Parse the enumerator-list. 1674 while (Tok.is(tok::identifier)) { 1675 IdentifierInfo *Ident = Tok.getIdentifierInfo(); 1676 SourceLocation IdentLoc = ConsumeToken(); 1677 1678 SourceLocation EqualLoc; 1679 OwningExprResult AssignedVal(Actions); 1680 if (Tok.is(tok::equal)) { 1681 EqualLoc = ConsumeToken(); 1682 AssignedVal = ParseConstantExpression(); 1683 if (AssignedVal.isInvalid()) 1684 SkipUntil(tok::comma, tok::r_brace, true, true); 1685 } 1686 1687 // Install the enumerator constant into EnumDecl. 1688 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, 1689 LastEnumConstDecl, 1690 IdentLoc, Ident, 1691 EqualLoc, 1692 AssignedVal.release()); 1693 EnumConstantDecls.push_back(EnumConstDecl); 1694 LastEnumConstDecl = EnumConstDecl; 1695 1696 if (Tok.isNot(tok::comma)) 1697 break; 1698 SourceLocation CommaLoc = ConsumeToken(); 1699 1700 if (Tok.isNot(tok::identifier) && 1701 !(getLang().C99 || getLang().CPlusPlus0x)) 1702 Diag(CommaLoc, diag::ext_enumerator_list_comma) 1703 << getLang().CPlusPlus 1704 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc))); 1705 } 1706 1707 // Eat the }. 1708 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 1709 1710 AttributeList *Attr = 0; 1711 // If attributes exist after the identifier list, parse them. 1712 if (Tok.is(tok::kw___attribute)) 1713 Attr = ParseAttributes(); 1714 1715 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl, 1716 EnumConstantDecls.data(), EnumConstantDecls.size(), 1717 CurScope, Attr); 1718 1719 EnumScope.Exit(); 1720 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc); 1721} 1722 1723/// isTypeSpecifierQualifier - Return true if the current token could be the 1724/// start of a type-qualifier-list. 1725bool Parser::isTypeQualifier() const { 1726 switch (Tok.getKind()) { 1727 default: return false; 1728 // type-qualifier 1729 case tok::kw_const: 1730 case tok::kw_volatile: 1731 case tok::kw_restrict: 1732 return true; 1733 } 1734} 1735 1736/// isTypeSpecifierQualifier - Return true if the current token could be the 1737/// start of a specifier-qualifier-list. 1738bool Parser::isTypeSpecifierQualifier() { 1739 switch (Tok.getKind()) { 1740 default: return false; 1741 1742 case tok::identifier: // foo::bar 1743 case tok::kw_typename: // typename T::type 1744 // Annotate typenames and C++ scope specifiers. If we get one, just 1745 // recurse to handle whatever we get. 1746 if (TryAnnotateTypeOrScopeToken()) 1747 return isTypeSpecifierQualifier(); 1748 // Otherwise, not a type specifier. 1749 return false; 1750 1751 case tok::coloncolon: // ::foo::bar 1752 if (NextToken().is(tok::kw_new) || // ::new 1753 NextToken().is(tok::kw_delete)) // ::delete 1754 return false; 1755 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 // GNU attributes support. 1764 case tok::kw___attribute: 1765 // GNU typeof support. 1766 case tok::kw_typeof: 1767 1768 // type-specifiers 1769 case tok::kw_short: 1770 case tok::kw_long: 1771 case tok::kw_signed: 1772 case tok::kw_unsigned: 1773 case tok::kw__Complex: 1774 case tok::kw__Imaginary: 1775 case tok::kw_void: 1776 case tok::kw_char: 1777 case tok::kw_wchar_t: 1778 case tok::kw_char16_t: 1779 case tok::kw_char32_t: 1780 case tok::kw_int: 1781 case tok::kw_float: 1782 case tok::kw_double: 1783 case tok::kw_bool: 1784 case tok::kw__Bool: 1785 case tok::kw__Decimal32: 1786 case tok::kw__Decimal64: 1787 case tok::kw__Decimal128: 1788 1789 // struct-or-union-specifier (C99) or class-specifier (C++) 1790 case tok::kw_class: 1791 case tok::kw_struct: 1792 case tok::kw_union: 1793 // enum-specifier 1794 case tok::kw_enum: 1795 1796 // type-qualifier 1797 case tok::kw_const: 1798 case tok::kw_volatile: 1799 case tok::kw_restrict: 1800 1801 // typedef-name 1802 case tok::annot_typename: 1803 return true; 1804 1805 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. 1806 case tok::less: 1807 return getLang().ObjC1; 1808 1809 case tok::kw___cdecl: 1810 case tok::kw___stdcall: 1811 case tok::kw___fastcall: 1812 case tok::kw___w64: 1813 case tok::kw___ptr64: 1814 return true; 1815 } 1816} 1817 1818/// isDeclarationSpecifier() - Return true if the current token is part of a 1819/// declaration specifier. 1820bool Parser::isDeclarationSpecifier() { 1821 switch (Tok.getKind()) { 1822 default: return false; 1823 1824 case tok::identifier: // foo::bar 1825 // Unfortunate hack to support "Class.factoryMethod" notation. 1826 if (getLang().ObjC1 && NextToken().is(tok::period)) 1827 return false; 1828 // Fall through 1829 1830 case tok::kw_typename: // typename T::type 1831 // Annotate typenames and C++ scope specifiers. If we get one, just 1832 // recurse to handle whatever we get. 1833 if (TryAnnotateTypeOrScopeToken()) 1834 return isDeclarationSpecifier(); 1835 // Otherwise, not a declaration specifier. 1836 return false; 1837 case tok::coloncolon: // ::foo::bar 1838 if (NextToken().is(tok::kw_new) || // ::new 1839 NextToken().is(tok::kw_delete)) // ::delete 1840 return false; 1841 1842 // Annotate typenames and C++ scope specifiers. If we get one, just 1843 // recurse to handle whatever we get. 1844 if (TryAnnotateTypeOrScopeToken()) 1845 return isDeclarationSpecifier(); 1846 // Otherwise, not a declaration specifier. 1847 return false; 1848 1849 // storage-class-specifier 1850 case tok::kw_typedef: 1851 case tok::kw_extern: 1852 case tok::kw___private_extern__: 1853 case tok::kw_static: 1854 case tok::kw_auto: 1855 case tok::kw_register: 1856 case tok::kw___thread: 1857 1858 // type-specifiers 1859 case tok::kw_short: 1860 case tok::kw_long: 1861 case tok::kw_signed: 1862 case tok::kw_unsigned: 1863 case tok::kw__Complex: 1864 case tok::kw__Imaginary: 1865 case tok::kw_void: 1866 case tok::kw_char: 1867 case tok::kw_wchar_t: 1868 case tok::kw_char16_t: 1869 case tok::kw_char32_t: 1870 1871 case tok::kw_int: 1872 case tok::kw_float: 1873 case tok::kw_double: 1874 case tok::kw_bool: 1875 case tok::kw__Bool: 1876 case tok::kw__Decimal32: 1877 case tok::kw__Decimal64: 1878 case tok::kw__Decimal128: 1879 1880 // struct-or-union-specifier (C99) or class-specifier (C++) 1881 case tok::kw_class: 1882 case tok::kw_struct: 1883 case tok::kw_union: 1884 // enum-specifier 1885 case tok::kw_enum: 1886 1887 // type-qualifier 1888 case tok::kw_const: 1889 case tok::kw_volatile: 1890 case tok::kw_restrict: 1891 1892 // function-specifier 1893 case tok::kw_inline: 1894 case tok::kw_virtual: 1895 case tok::kw_explicit: 1896 1897 // typedef-name 1898 case tok::annot_typename: 1899 1900 // GNU typeof support. 1901 case tok::kw_typeof: 1902 1903 // GNU attributes. 1904 case tok::kw___attribute: 1905 return true; 1906 1907 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. 1908 case tok::less: 1909 return getLang().ObjC1; 1910 1911 case tok::kw___declspec: 1912 case tok::kw___cdecl: 1913 case tok::kw___stdcall: 1914 case tok::kw___fastcall: 1915 case tok::kw___w64: 1916 case tok::kw___ptr64: 1917 case tok::kw___forceinline: 1918 return true; 1919 } 1920} 1921 1922 1923/// ParseTypeQualifierListOpt 1924/// type-qualifier-list: [C99 6.7.5] 1925/// type-qualifier 1926/// [GNU] attributes [ only if AttributesAllowed=true ] 1927/// type-qualifier-list type-qualifier 1928/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ] 1929/// 1930void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) { 1931 while (1) { 1932 bool isInvalid = false; 1933 const char *PrevSpec = 0; 1934 unsigned DiagID = 0; 1935 SourceLocation Loc = Tok.getLocation(); 1936 1937 switch (Tok.getKind()) { 1938 case tok::kw_const: 1939 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, 1940 getLang()); 1941 break; 1942 case tok::kw_volatile: 1943 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, 1944 getLang()); 1945 break; 1946 case tok::kw_restrict: 1947 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, 1948 getLang()); 1949 break; 1950 case tok::kw___w64: 1951 case tok::kw___ptr64: 1952 case tok::kw___cdecl: 1953 case tok::kw___stdcall: 1954 case tok::kw___fastcall: 1955 if (AttributesAllowed) { 1956 DS.AddAttributes(ParseMicrosoftTypeAttributes()); 1957 continue; 1958 } 1959 goto DoneWithTypeQuals; 1960 case tok::kw___attribute: 1961 if (AttributesAllowed) { 1962 DS.AddAttributes(ParseAttributes()); 1963 continue; // do *not* consume the next token! 1964 } 1965 // otherwise, FALL THROUGH! 1966 default: 1967 DoneWithTypeQuals: 1968 // If this is not a type-qualifier token, we're done reading type 1969 // qualifiers. First verify that DeclSpec's are consistent. 1970 DS.Finish(Diags, PP); 1971 return; 1972 } 1973 1974 // If the specifier combination wasn't legal, issue a diagnostic. 1975 if (isInvalid) { 1976 assert(PrevSpec && "Method did not return previous specifier!"); 1977 Diag(Tok, DiagID) << PrevSpec; 1978 } 1979 ConsumeToken(); 1980 } 1981} 1982 1983 1984/// ParseDeclarator - Parse and verify a newly-initialized declarator. 1985/// 1986void Parser::ParseDeclarator(Declarator &D) { 1987 /// This implements the 'declarator' production in the C grammar, then checks 1988 /// for well-formedness and issues diagnostics. 1989 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); 1990} 1991 1992/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator 1993/// is parsed by the function passed to it. Pass null, and the direct-declarator 1994/// isn't parsed at all, making this function effectively parse the C++ 1995/// ptr-operator production. 1996/// 1997/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] 1998/// [C] pointer[opt] direct-declarator 1999/// [C++] direct-declarator 2000/// [C++] ptr-operator declarator 2001/// 2002/// pointer: [C99 6.7.5] 2003/// '*' type-qualifier-list[opt] 2004/// '*' type-qualifier-list[opt] pointer 2005/// 2006/// ptr-operator: 2007/// '*' cv-qualifier-seq[opt] 2008/// '&' 2009/// [C++0x] '&&' 2010/// [GNU] '&' restrict[opt] attributes[opt] 2011/// [GNU?] '&&' restrict[opt] attributes[opt] 2012/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] 2013void Parser::ParseDeclaratorInternal(Declarator &D, 2014 DirectDeclParseFunction DirectDeclParser) { 2015 2016 // C++ member pointers start with a '::' or a nested-name. 2017 // Member pointers get special handling, since there's no place for the 2018 // scope spec in the generic path below. 2019 if (getLang().CPlusPlus && 2020 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || 2021 Tok.is(tok::annot_cxxscope))) { 2022 CXXScopeSpec SS; 2023 if (ParseOptionalCXXScopeSpecifier(SS)) { 2024 if(Tok.isNot(tok::star)) { 2025 // The scope spec really belongs to the direct-declarator. 2026 D.getCXXScopeSpec() = SS; 2027 if (DirectDeclParser) 2028 (this->*DirectDeclParser)(D); 2029 return; 2030 } 2031 2032 SourceLocation Loc = ConsumeToken(); 2033 D.SetRangeEnd(Loc); 2034 DeclSpec DS; 2035 ParseTypeQualifierListOpt(DS); 2036 D.ExtendWithDeclSpec(DS); 2037 2038 // Recurse to parse whatever is left. 2039 ParseDeclaratorInternal(D, DirectDeclParser); 2040 2041 // Sema will have to catch (syntactically invalid) pointers into global 2042 // scope. It has to catch pointers into namespace scope anyway. 2043 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), 2044 Loc, DS.TakeAttributes()), 2045 /* Don't replace range end. */SourceLocation()); 2046 return; 2047 } 2048 } 2049 2050 tok::TokenKind Kind = Tok.getKind(); 2051 // Not a pointer, C++ reference, or block. 2052 if (Kind != tok::star && Kind != tok::caret && 2053 (Kind != tok::amp || !getLang().CPlusPlus) && 2054 // We parse rvalue refs in C++03, because otherwise the errors are scary. 2055 (Kind != tok::ampamp || !getLang().CPlusPlus)) { 2056 if (DirectDeclParser) 2057 (this->*DirectDeclParser)(D); 2058 return; 2059 } 2060 2061 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, 2062 // '&&' -> rvalue reference 2063 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. 2064 D.SetRangeEnd(Loc); 2065 2066 if (Kind == tok::star || Kind == tok::caret) { 2067 // Is a pointer. 2068 DeclSpec DS; 2069 2070 ParseTypeQualifierListOpt(DS); 2071 D.ExtendWithDeclSpec(DS); 2072 2073 // Recursively parse the declarator. 2074 ParseDeclaratorInternal(D, DirectDeclParser); 2075 if (Kind == tok::star) 2076 // Remember that we parsed a pointer type, and remember the type-quals. 2077 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, 2078 DS.TakeAttributes()), 2079 SourceLocation()); 2080 else 2081 // Remember that we parsed a Block type, and remember the type-quals. 2082 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), 2083 Loc, DS.TakeAttributes()), 2084 SourceLocation()); 2085 } else { 2086 // Is a reference 2087 DeclSpec DS; 2088 2089 // Complain about rvalue references in C++03, but then go on and build 2090 // the declarator. 2091 if (Kind == tok::ampamp && !getLang().CPlusPlus0x) 2092 Diag(Loc, diag::err_rvalue_reference); 2093 2094 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the 2095 // cv-qualifiers are introduced through the use of a typedef or of a 2096 // template type argument, in which case the cv-qualifiers are ignored. 2097 // 2098 // [GNU] Retricted references are allowed. 2099 // [GNU] Attributes on references are allowed. 2100 ParseTypeQualifierListOpt(DS); 2101 D.ExtendWithDeclSpec(DS); 2102 2103 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { 2104 if (DS.getTypeQualifiers() & DeclSpec::TQ_const) 2105 Diag(DS.getConstSpecLoc(), 2106 diag::err_invalid_reference_qualifier_application) << "const"; 2107 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) 2108 Diag(DS.getVolatileSpecLoc(), 2109 diag::err_invalid_reference_qualifier_application) << "volatile"; 2110 } 2111 2112 // Recursively parse the declarator. 2113 ParseDeclaratorInternal(D, DirectDeclParser); 2114 2115 if (D.getNumTypeObjects() > 0) { 2116 // C++ [dcl.ref]p4: There shall be no references to references. 2117 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); 2118 if (InnerChunk.Kind == DeclaratorChunk::Reference) { 2119 if (const IdentifierInfo *II = D.getIdentifier()) 2120 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) 2121 << II; 2122 else 2123 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) 2124 << "type name"; 2125 2126 // Once we've complained about the reference-to-reference, we 2127 // can go ahead and build the (technically ill-formed) 2128 // declarator: reference collapsing will take care of it. 2129 } 2130 } 2131 2132 // Remember that we parsed a reference type. It doesn't have type-quals. 2133 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, 2134 DS.TakeAttributes(), 2135 Kind == tok::amp), 2136 SourceLocation()); 2137 } 2138} 2139 2140/// ParseDirectDeclarator 2141/// direct-declarator: [C99 6.7.5] 2142/// [C99] identifier 2143/// '(' declarator ')' 2144/// [GNU] '(' attributes declarator ')' 2145/// [C90] direct-declarator '[' constant-expression[opt] ']' 2146/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' 2147/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' 2148/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' 2149/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' 2150/// direct-declarator '(' parameter-type-list ')' 2151/// direct-declarator '(' identifier-list[opt] ')' 2152/// [GNU] direct-declarator '(' parameter-forward-declarations 2153/// parameter-type-list[opt] ')' 2154/// [C++] direct-declarator '(' parameter-declaration-clause ')' 2155/// cv-qualifier-seq[opt] exception-specification[opt] 2156/// [C++] declarator-id 2157/// 2158/// declarator-id: [C++ 8] 2159/// id-expression 2160/// '::'[opt] nested-name-specifier[opt] type-name 2161/// 2162/// id-expression: [C++ 5.1] 2163/// unqualified-id 2164/// qualified-id [TODO] 2165/// 2166/// unqualified-id: [C++ 5.1] 2167/// identifier 2168/// operator-function-id 2169/// conversion-function-id [TODO] 2170/// '~' class-name 2171/// template-id 2172/// 2173void Parser::ParseDirectDeclarator(Declarator &D) { 2174 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); 2175 2176 if (getLang().CPlusPlus) { 2177 if (D.mayHaveIdentifier()) { 2178 // ParseDeclaratorInternal might already have parsed the scope. 2179 bool afterCXXScope = D.getCXXScopeSpec().isSet() || 2180 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec()); 2181 if (afterCXXScope) { 2182 // Change the declaration context for name lookup, until this function 2183 // is exited (and the declarator has been parsed). 2184 DeclScopeObj.EnterDeclaratorScope(); 2185 } 2186 2187 if (Tok.is(tok::identifier)) { 2188 assert(Tok.getIdentifierInfo() && "Not an identifier?"); 2189 2190 // If this identifier is the name of the current class, it's a 2191 // constructor name. 2192 if (!D.getDeclSpec().hasTypeSpecifier() && 2193 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) { 2194 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0; 2195 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(), 2196 Tok.getLocation(), CurScope, SS), 2197 Tok.getLocation()); 2198 // This is a normal identifier. 2199 } else 2200 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 2201 ConsumeToken(); 2202 goto PastIdentifier; 2203 } else if (Tok.is(tok::annot_template_id)) { 2204 TemplateIdAnnotation *TemplateId 2205 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 2206 2207 // FIXME: Could this template-id name a constructor? 2208 2209 // FIXME: This is an egregious hack, where we silently ignore 2210 // the specialization (which should be a function template 2211 // specialization name) and use the name instead. This hack 2212 // will go away when we have support for function 2213 // specializations. 2214 D.SetIdentifier(TemplateId->Name, Tok.getLocation()); 2215 TemplateId->Destroy(); 2216 ConsumeToken(); 2217 goto PastIdentifier; 2218 } else if (Tok.is(tok::kw_operator)) { 2219 SourceLocation OperatorLoc = Tok.getLocation(); 2220 SourceLocation EndLoc; 2221 2222 // First try the name of an overloaded operator 2223 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) { 2224 D.setOverloadedOperator(Op, OperatorLoc, EndLoc); 2225 } else { 2226 // This must be a conversion function (C++ [class.conv.fct]). 2227 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc)) 2228 D.setConversionFunction(ConvType, OperatorLoc, EndLoc); 2229 else { 2230 D.SetIdentifier(0, Tok.getLocation()); 2231 } 2232 } 2233 goto PastIdentifier; 2234 } else if (Tok.is(tok::tilde)) { 2235 // This should be a C++ destructor. 2236 SourceLocation TildeLoc = ConsumeToken(); 2237 if (Tok.is(tok::identifier)) { 2238 // FIXME: Inaccurate. 2239 SourceLocation NameLoc = Tok.getLocation(); 2240 SourceLocation EndLoc; 2241 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0; 2242 TypeResult Type = ParseClassName(EndLoc, SS, true); 2243 if (Type.isInvalid()) 2244 D.SetIdentifier(0, TildeLoc); 2245 else 2246 D.setDestructor(Type.get(), TildeLoc, NameLoc); 2247 } else { 2248 Diag(Tok, diag::err_destructor_class_name); 2249 D.SetIdentifier(0, TildeLoc); 2250 } 2251 goto PastIdentifier; 2252 } 2253 2254 // If we reached this point, token is not identifier and not '~'. 2255 2256 if (afterCXXScope) { 2257 Diag(Tok, diag::err_expected_unqualified_id); 2258 D.SetIdentifier(0, Tok.getLocation()); 2259 D.setInvalidType(true); 2260 goto PastIdentifier; 2261 } 2262 } 2263 } 2264 2265 // If we reached this point, we are either in C/ObjC or the token didn't 2266 // satisfy any of the C++-specific checks. 2267 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { 2268 assert(!getLang().CPlusPlus && 2269 "There's a C++-specific check for tok::identifier above"); 2270 assert(Tok.getIdentifierInfo() && "Not an identifier?"); 2271 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); 2272 ConsumeToken(); 2273 } else if (Tok.is(tok::l_paren)) { 2274 // direct-declarator: '(' declarator ')' 2275 // direct-declarator: '(' attributes declarator ')' 2276 // Example: 'char (*X)' or 'int (*XX)(void)' 2277 ParseParenDeclarator(D); 2278 } else if (D.mayOmitIdentifier()) { 2279 // This could be something simple like "int" (in which case the declarator 2280 // portion is empty), if an abstract-declarator is allowed. 2281 D.SetIdentifier(0, Tok.getLocation()); 2282 } else { 2283 if (D.getContext() == Declarator::MemberContext) 2284 Diag(Tok, diag::err_expected_member_name_or_semi) 2285 << D.getDeclSpec().getSourceRange(); 2286 else if (getLang().CPlusPlus) 2287 Diag(Tok, diag::err_expected_unqualified_id); 2288 else 2289 Diag(Tok, diag::err_expected_ident_lparen); 2290 D.SetIdentifier(0, Tok.getLocation()); 2291 D.setInvalidType(true); 2292 } 2293 2294 PastIdentifier: 2295 assert(D.isPastIdentifier() && 2296 "Haven't past the location of the identifier yet?"); 2297 2298 while (1) { 2299 if (Tok.is(tok::l_paren)) { 2300 // The paren may be part of a C++ direct initializer, eg. "int x(1);". 2301 // In such a case, check if we actually have a function declarator; if it 2302 // is not, the declarator has been fully parsed. 2303 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { 2304 // When not in file scope, warn for ambiguous function declarators, just 2305 // in case the author intended it as a variable definition. 2306 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; 2307 if (!isCXXFunctionDeclarator(warnIfAmbiguous)) 2308 break; 2309 } 2310 ParseFunctionDeclarator(ConsumeParen(), D); 2311 } else if (Tok.is(tok::l_square)) { 2312 ParseBracketDeclarator(D); 2313 } else { 2314 break; 2315 } 2316 } 2317} 2318 2319/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is 2320/// only called before the identifier, so these are most likely just grouping 2321/// parens for precedence. If we find that these are actually function 2322/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. 2323/// 2324/// direct-declarator: 2325/// '(' declarator ')' 2326/// [GNU] '(' attributes declarator ')' 2327/// direct-declarator '(' parameter-type-list ')' 2328/// direct-declarator '(' identifier-list[opt] ')' 2329/// [GNU] direct-declarator '(' parameter-forward-declarations 2330/// parameter-type-list[opt] ')' 2331/// 2332void Parser::ParseParenDeclarator(Declarator &D) { 2333 SourceLocation StartLoc = ConsumeParen(); 2334 assert(!D.isPastIdentifier() && "Should be called before passing identifier"); 2335 2336 // Eat any attributes before we look at whether this is a grouping or function 2337 // declarator paren. If this is a grouping paren, the attribute applies to 2338 // the type being built up, for example: 2339 // int (__attribute__(()) *x)(long y) 2340 // If this ends up not being a grouping paren, the attribute applies to the 2341 // first argument, for example: 2342 // int (__attribute__(()) int x) 2343 // In either case, we need to eat any attributes to be able to determine what 2344 // sort of paren this is. 2345 // 2346 AttributeList *AttrList = 0; 2347 bool RequiresArg = false; 2348 if (Tok.is(tok::kw___attribute)) { 2349 AttrList = ParseAttributes(); 2350 2351 // We require that the argument list (if this is a non-grouping paren) be 2352 // present even if the attribute list was empty. 2353 RequiresArg = true; 2354 } 2355 // Eat any Microsoft extensions. 2356 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || 2357 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) || 2358 Tok.is(tok::kw___ptr64)) { 2359 AttrList = ParseMicrosoftTypeAttributes(AttrList); 2360 } 2361 2362 // If we haven't past the identifier yet (or where the identifier would be 2363 // stored, if this is an abstract declarator), then this is probably just 2364 // grouping parens. However, if this could be an abstract-declarator, then 2365 // this could also be the start of function arguments (consider 'void()'). 2366 bool isGrouping; 2367 2368 if (!D.mayOmitIdentifier()) { 2369 // If this can't be an abstract-declarator, this *must* be a grouping 2370 // paren, because we haven't seen the identifier yet. 2371 isGrouping = true; 2372 } else if (Tok.is(tok::r_paren) || // 'int()' is a function. 2373 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) 2374 isDeclarationSpecifier()) { // 'int(int)' is a function. 2375 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is 2376 // considered to be a type, not a K&R identifier-list. 2377 isGrouping = false; 2378 } else { 2379 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. 2380 isGrouping = true; 2381 } 2382 2383 // If this is a grouping paren, handle: 2384 // direct-declarator: '(' declarator ')' 2385 // direct-declarator: '(' attributes declarator ')' 2386 if (isGrouping) { 2387 bool hadGroupingParens = D.hasGroupingParens(); 2388 D.setGroupingParens(true); 2389 if (AttrList) 2390 D.AddAttributes(AttrList, SourceLocation()); 2391 2392 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); 2393 // Match the ')'. 2394 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc); 2395 2396 D.setGroupingParens(hadGroupingParens); 2397 D.SetRangeEnd(Loc); 2398 return; 2399 } 2400 2401 // Okay, if this wasn't a grouping paren, it must be the start of a function 2402 // argument list. Recognize that this declarator will never have an 2403 // identifier (and remember where it would have been), then call into 2404 // ParseFunctionDeclarator to handle of argument list. 2405 D.SetIdentifier(0, Tok.getLocation()); 2406 2407 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg); 2408} 2409 2410/// ParseFunctionDeclarator - We are after the identifier and have parsed the 2411/// declarator D up to a paren, which indicates that we are parsing function 2412/// arguments. 2413/// 2414/// If AttrList is non-null, then the caller parsed those arguments immediately 2415/// after the open paren - they should be considered to be the first argument of 2416/// a parameter. If RequiresArg is true, then the first argument of the 2417/// function is required to be present and required to not be an identifier 2418/// list. 2419/// 2420/// This method also handles this portion of the grammar: 2421/// parameter-type-list: [C99 6.7.5] 2422/// parameter-list 2423/// parameter-list ',' '...' 2424/// 2425/// parameter-list: [C99 6.7.5] 2426/// parameter-declaration 2427/// parameter-list ',' parameter-declaration 2428/// 2429/// parameter-declaration: [C99 6.7.5] 2430/// declaration-specifiers declarator 2431/// [C++] declaration-specifiers declarator '=' assignment-expression 2432/// [GNU] declaration-specifiers declarator attributes 2433/// declaration-specifiers abstract-declarator[opt] 2434/// [C++] declaration-specifiers abstract-declarator[opt] 2435/// '=' assignment-expression 2436/// [GNU] declaration-specifiers abstract-declarator[opt] attributes 2437/// 2438/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]" 2439/// and "exception-specification[opt]". 2440/// 2441void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, 2442 AttributeList *AttrList, 2443 bool RequiresArg) { 2444 // lparen is already consumed! 2445 assert(D.isPastIdentifier() && "Should not call before identifier!"); 2446 2447 // This parameter list may be empty. 2448 if (Tok.is(tok::r_paren)) { 2449 if (RequiresArg) { 2450 Diag(Tok, diag::err_argument_required_after_attribute); 2451 delete AttrList; 2452 } 2453 2454 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'. 2455 SourceLocation EndLoc = RParenLoc; 2456 2457 // cv-qualifier-seq[opt]. 2458 DeclSpec DS; 2459 bool hasExceptionSpec = false; 2460 SourceLocation ThrowLoc; 2461 bool hasAnyExceptionSpec = false; 2462 llvm::SmallVector<TypeTy*, 2> Exceptions; 2463 llvm::SmallVector<SourceRange, 2> ExceptionRanges; 2464 if (getLang().CPlusPlus) { 2465 ParseTypeQualifierListOpt(DS, false /*no attributes*/); 2466 if (!DS.getSourceRange().getEnd().isInvalid()) 2467 EndLoc = DS.getSourceRange().getEnd(); 2468 2469 // Parse exception-specification[opt]. 2470 if (Tok.is(tok::kw_throw)) { 2471 hasExceptionSpec = true; 2472 ThrowLoc = Tok.getLocation(); 2473 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges, 2474 hasAnyExceptionSpec); 2475 assert(Exceptions.size() == ExceptionRanges.size() && 2476 "Produced different number of exception types and ranges."); 2477 } 2478 } 2479 2480 // Remember that we parsed a function type, and remember the attributes. 2481 // int() -> no prototype, no '...'. 2482 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, 2483 /*variadic*/ false, 2484 SourceLocation(), 2485 /*arglist*/ 0, 0, 2486 DS.getTypeQualifiers(), 2487 hasExceptionSpec, ThrowLoc, 2488 hasAnyExceptionSpec, 2489 Exceptions.data(), 2490 ExceptionRanges.data(), 2491 Exceptions.size(), 2492 LParenLoc, RParenLoc, D), 2493 EndLoc); 2494 return; 2495 } 2496 2497 // Alternatively, this parameter list may be an identifier list form for a 2498 // K&R-style function: void foo(a,b,c) 2499 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) { 2500 if (!TryAnnotateTypeOrScopeToken()) { 2501 // K&R identifier lists can't have typedefs as identifiers, per 2502 // C99 6.7.5.3p11. 2503 if (RequiresArg) { 2504 Diag(Tok, diag::err_argument_required_after_attribute); 2505 delete AttrList; 2506 } 2507 // Identifier list. Note that '(' identifier-list ')' is only allowed for 2508 // normal declarators, not for abstract-declarators. 2509 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D); 2510 } 2511 } 2512 2513 // Finally, a normal, non-empty parameter type list. 2514 2515 // Build up an array of information about the parsed arguments. 2516 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; 2517 2518 // Enter function-declaration scope, limiting any declarators to the 2519 // function prototype scope, including parameter declarators. 2520 ParseScope PrototypeScope(this, 2521 Scope::FunctionPrototypeScope|Scope::DeclScope); 2522 2523 bool IsVariadic = false; 2524 SourceLocation EllipsisLoc; 2525 while (1) { 2526 if (Tok.is(tok::ellipsis)) { 2527 IsVariadic = true; 2528 EllipsisLoc = ConsumeToken(); // Consume the ellipsis. 2529 break; 2530 } 2531 2532 SourceLocation DSStart = Tok.getLocation(); 2533 2534 // Parse the declaration-specifiers. 2535 DeclSpec DS; 2536 2537 // If the caller parsed attributes for the first argument, add them now. 2538 if (AttrList) { 2539 DS.AddAttributes(AttrList); 2540 AttrList = 0; // Only apply the attributes to the first parameter. 2541 } 2542 ParseDeclarationSpecifiers(DS); 2543 2544 // Parse the declarator. This is "PrototypeContext", because we must 2545 // accept either 'declarator' or 'abstract-declarator' here. 2546 Declarator ParmDecl(DS, Declarator::PrototypeContext); 2547 ParseDeclarator(ParmDecl); 2548 2549 // Parse GNU attributes, if present. 2550 if (Tok.is(tok::kw___attribute)) { 2551 SourceLocation Loc; 2552 AttributeList *AttrList = ParseAttributes(&Loc); 2553 ParmDecl.AddAttributes(AttrList, Loc); 2554 } 2555 2556 // Remember this parsed parameter in ParamInfo. 2557 IdentifierInfo *ParmII = ParmDecl.getIdentifier(); 2558 2559 // DefArgToks is used when the parsing of default arguments needs 2560 // to be delayed. 2561 CachedTokens *DefArgToks = 0; 2562 2563 // If no parameter was specified, verify that *something* was specified, 2564 // otherwise we have a missing type and identifier. 2565 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && 2566 ParmDecl.getNumTypeObjects() == 0) { 2567 // Completely missing, emit error. 2568 Diag(DSStart, diag::err_missing_param); 2569 } else { 2570 // Otherwise, we have something. Add it and let semantic analysis try 2571 // to grok it and add the result to the ParamInfo we are building. 2572 2573 // Inform the actions module about the parameter declarator, so it gets 2574 // added to the current scope. 2575 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl); 2576 2577 // Parse the default argument, if any. We parse the default 2578 // arguments in all dialects; the semantic analysis in 2579 // ActOnParamDefaultArgument will reject the default argument in 2580 // C. 2581 if (Tok.is(tok::equal)) { 2582 SourceLocation EqualLoc = Tok.getLocation(); 2583 2584 // Parse the default argument 2585 if (D.getContext() == Declarator::MemberContext) { 2586 // If we're inside a class definition, cache the tokens 2587 // corresponding to the default argument. We'll actually parse 2588 // them when we see the end of the class definition. 2589 // FIXME: Templates will require something similar. 2590 // FIXME: Can we use a smart pointer for Toks? 2591 DefArgToks = new CachedTokens; 2592 2593 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, 2594 tok::semi, false)) { 2595 delete DefArgToks; 2596 DefArgToks = 0; 2597 Actions.ActOnParamDefaultArgumentError(Param); 2598 } else 2599 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, 2600 (*DefArgToks)[1].getLocation()); 2601 } else { 2602 // Consume the '='. 2603 ConsumeToken(); 2604 2605 OwningExprResult DefArgResult(ParseAssignmentExpression()); 2606 if (DefArgResult.isInvalid()) { 2607 Actions.ActOnParamDefaultArgumentError(Param); 2608 SkipUntil(tok::comma, tok::r_paren, true, true); 2609 } else { 2610 // Inform the actions module about the default argument 2611 Actions.ActOnParamDefaultArgument(Param, EqualLoc, 2612 move(DefArgResult)); 2613 } 2614 } 2615 } 2616 2617 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, 2618 ParmDecl.getIdentifierLoc(), Param, 2619 DefArgToks)); 2620 } 2621 2622 // If the next token is a comma, consume it and keep reading arguments. 2623 if (Tok.isNot(tok::comma)) break; 2624 2625 // Consume the comma. 2626 ConsumeToken(); 2627 } 2628 2629 // Leave prototype scope. 2630 PrototypeScope.Exit(); 2631 2632 // If we have the closing ')', eat it. 2633 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2634 SourceLocation EndLoc = RParenLoc; 2635 2636 DeclSpec DS; 2637 bool hasExceptionSpec = false; 2638 SourceLocation ThrowLoc; 2639 bool hasAnyExceptionSpec = false; 2640 llvm::SmallVector<TypeTy*, 2> Exceptions; 2641 llvm::SmallVector<SourceRange, 2> ExceptionRanges; 2642 if (getLang().CPlusPlus) { 2643 // Parse cv-qualifier-seq[opt]. 2644 ParseTypeQualifierListOpt(DS, false /*no attributes*/); 2645 if (!DS.getSourceRange().getEnd().isInvalid()) 2646 EndLoc = DS.getSourceRange().getEnd(); 2647 2648 // Parse exception-specification[opt]. 2649 if (Tok.is(tok::kw_throw)) { 2650 hasExceptionSpec = true; 2651 ThrowLoc = Tok.getLocation(); 2652 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges, 2653 hasAnyExceptionSpec); 2654 assert(Exceptions.size() == ExceptionRanges.size() && 2655 "Produced different number of exception types and ranges."); 2656 } 2657 } 2658 2659 // Remember that we parsed a function type, and remember the attributes. 2660 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, 2661 EllipsisLoc, 2662 ParamInfo.data(), ParamInfo.size(), 2663 DS.getTypeQualifiers(), 2664 hasExceptionSpec, ThrowLoc, 2665 hasAnyExceptionSpec, 2666 Exceptions.data(), 2667 ExceptionRanges.data(), 2668 Exceptions.size(), 2669 LParenLoc, RParenLoc, D), 2670 EndLoc); 2671} 2672 2673/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator 2674/// we found a K&R-style identifier list instead of a type argument list. The 2675/// current token is known to be the first identifier in the list. 2676/// 2677/// identifier-list: [C99 6.7.5] 2678/// identifier 2679/// identifier-list ',' identifier 2680/// 2681void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, 2682 Declarator &D) { 2683 // Build up an array of information about the parsed arguments. 2684 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; 2685 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; 2686 2687 // If there was no identifier specified for the declarator, either we are in 2688 // an abstract-declarator, or we are in a parameter declarator which was found 2689 // to be abstract. In abstract-declarators, identifier lists are not valid: 2690 // diagnose this. 2691 if (!D.getIdentifier()) 2692 Diag(Tok, diag::ext_ident_list_in_param); 2693 2694 // Tok is known to be the first identifier in the list. Remember this 2695 // identifier in ParamInfo. 2696 ParamsSoFar.insert(Tok.getIdentifierInfo()); 2697 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), 2698 Tok.getLocation(), 2699 DeclPtrTy())); 2700 2701 ConsumeToken(); // eat the first identifier. 2702 2703 while (Tok.is(tok::comma)) { 2704 // Eat the comma. 2705 ConsumeToken(); 2706 2707 // If this isn't an identifier, report the error and skip until ')'. 2708 if (Tok.isNot(tok::identifier)) { 2709 Diag(Tok, diag::err_expected_ident); 2710 SkipUntil(tok::r_paren); 2711 return; 2712 } 2713 2714 IdentifierInfo *ParmII = Tok.getIdentifierInfo(); 2715 2716 // Reject 'typedef int y; int test(x, y)', but continue parsing. 2717 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope)) 2718 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; 2719 2720 // Verify that the argument identifier has not already been mentioned. 2721 if (!ParamsSoFar.insert(ParmII)) { 2722 Diag(Tok, diag::err_param_redefinition) << ParmII; 2723 } else { 2724 // Remember this identifier in ParamInfo. 2725 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, 2726 Tok.getLocation(), 2727 DeclPtrTy())); 2728 } 2729 2730 // Eat the identifier. 2731 ConsumeToken(); 2732 } 2733 2734 // If we have the closing ')', eat it and we're done. 2735 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 2736 2737 // Remember that we parsed a function type, and remember the attributes. This 2738 // function type is always a K&R style function type, which is not varargs and 2739 // has no prototype. 2740 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, 2741 SourceLocation(), 2742 &ParamInfo[0], ParamInfo.size(), 2743 /*TypeQuals*/0, 2744 /*exception*/false, 2745 SourceLocation(), false, 0, 0, 0, 2746 LParenLoc, RLoc, D), 2747 RLoc); 2748} 2749 2750/// [C90] direct-declarator '[' constant-expression[opt] ']' 2751/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' 2752/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' 2753/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' 2754/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' 2755void Parser::ParseBracketDeclarator(Declarator &D) { 2756 SourceLocation StartLoc = ConsumeBracket(); 2757 2758 // C array syntax has many features, but by-far the most common is [] and [4]. 2759 // This code does a fast path to handle some of the most obvious cases. 2760 if (Tok.getKind() == tok::r_square) { 2761 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); 2762 // Remember that we parsed the empty array type. 2763 OwningExprResult NumElements(Actions); 2764 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, 2765 StartLoc, EndLoc), 2766 EndLoc); 2767 return; 2768 } else if (Tok.getKind() == tok::numeric_constant && 2769 GetLookAheadToken(1).is(tok::r_square)) { 2770 // [4] is very common. Parse the numeric constant expression. 2771 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); 2772 ConsumeToken(); 2773 2774 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); 2775 2776 // If there was an error parsing the assignment-expression, recover. 2777 if (ExprRes.isInvalid()) 2778 ExprRes.release(); // Deallocate expr, just use []. 2779 2780 // Remember that we parsed a array type, and remember its features. 2781 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(), 2782 StartLoc, EndLoc), 2783 EndLoc); 2784 return; 2785 } 2786 2787 // If valid, this location is the position where we read the 'static' keyword. 2788 SourceLocation StaticLoc; 2789 if (Tok.is(tok::kw_static)) 2790 StaticLoc = ConsumeToken(); 2791 2792 // If there is a type-qualifier-list, read it now. 2793 // Type qualifiers in an array subscript are a C99 feature. 2794 DeclSpec DS; 2795 ParseTypeQualifierListOpt(DS, false /*no attributes*/); 2796 2797 // If we haven't already read 'static', check to see if there is one after the 2798 // type-qualifier-list. 2799 if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) 2800 StaticLoc = ConsumeToken(); 2801 2802 // Handle "direct-declarator [ type-qual-list[opt] * ]". 2803 bool isStar = false; 2804 OwningExprResult NumElements(Actions); 2805 2806 // Handle the case where we have '[*]' as the array size. However, a leading 2807 // star could be the start of an expression, for example 'X[*p + 4]'. Verify 2808 // the the token after the star is a ']'. Since stars in arrays are 2809 // infrequent, use of lookahead is not costly here. 2810 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { 2811 ConsumeToken(); // Eat the '*'. 2812 2813 if (StaticLoc.isValid()) { 2814 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); 2815 StaticLoc = SourceLocation(); // Drop the static. 2816 } 2817 isStar = true; 2818 } else if (Tok.isNot(tok::r_square)) { 2819 // Note, in C89, this production uses the constant-expr production instead 2820 // of assignment-expr. The only difference is that assignment-expr allows 2821 // things like '=' and '*='. Sema rejects these in C89 mode because they 2822 // are not i-c-e's, so we don't need to distinguish between the two here. 2823 2824 // Parse the constant-expression or assignment-expression now (depending 2825 // on dialect). 2826 if (getLang().CPlusPlus) 2827 NumElements = ParseConstantExpression(); 2828 else 2829 NumElements = ParseAssignmentExpression(); 2830 } 2831 2832 // If there was an error parsing the assignment-expression, recover. 2833 if (NumElements.isInvalid()) { 2834 D.setInvalidType(true); 2835 // If the expression was invalid, skip it. 2836 SkipUntil(tok::r_square); 2837 return; 2838 } 2839 2840 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); 2841 2842 // Remember that we parsed a array type, and remember its features. 2843 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), 2844 StaticLoc.isValid(), isStar, 2845 NumElements.release(), 2846 StartLoc, EndLoc), 2847 EndLoc); 2848} 2849 2850/// [GNU] typeof-specifier: 2851/// typeof ( expressions ) 2852/// typeof ( type-name ) 2853/// [GNU/C++] typeof unary-expression 2854/// 2855void Parser::ParseTypeofSpecifier(DeclSpec &DS) { 2856 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); 2857 Token OpTok = Tok; 2858 SourceLocation StartLoc = ConsumeToken(); 2859 2860 bool isCastExpr; 2861 TypeTy *CastTy; 2862 SourceRange CastRange; 2863 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok, 2864 isCastExpr, 2865 CastTy, 2866 CastRange); 2867 2868 if (CastRange.getEnd().isInvalid()) 2869 // FIXME: Not accurate, the range gets one token more than it should. 2870 DS.SetRangeEnd(Tok.getLocation()); 2871 else 2872 DS.SetRangeEnd(CastRange.getEnd()); 2873 2874 if (isCastExpr) { 2875 if (!CastTy) { 2876 DS.SetTypeSpecError(); 2877 return; 2878 } 2879 2880 const char *PrevSpec = 0; 2881 unsigned DiagID; 2882 // Check for duplicate type specifiers (e.g. "int typeof(int)"). 2883 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, 2884 DiagID, CastTy)) 2885 Diag(StartLoc, DiagID) << PrevSpec; 2886 return; 2887 } 2888 2889 // If we get here, the operand to the typeof was an expresion. 2890 if (Operand.isInvalid()) { 2891 DS.SetTypeSpecError(); 2892 return; 2893 } 2894 2895 const char *PrevSpec = 0; 2896 unsigned DiagID; 2897 // Check for duplicate type specifiers (e.g. "int typeof(int)"). 2898 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, 2899 DiagID, Operand.release())) 2900 Diag(StartLoc, DiagID) << PrevSpec; 2901} 2902