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