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