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