ParseDeclCXX.cpp revision 201361
1//===--- ParseDeclCXX.cpp - C++ 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 C++ Declaration portions of the Parser interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Basic/OperatorKinds.h" 15#include "clang/Parse/Parser.h" 16#include "clang/Parse/ParseDiagnostic.h" 17#include "clang/Parse/DeclSpec.h" 18#include "clang/Parse/Scope.h" 19#include "clang/Parse/Template.h" 20#include "RAIIObjectsForParser.h" 21using namespace clang; 22 23/// ParseNamespace - We know that the current token is a namespace keyword. This 24/// may either be a top level namespace or a block-level namespace alias. 25/// 26/// namespace-definition: [C++ 7.3: basic.namespace] 27/// named-namespace-definition 28/// unnamed-namespace-definition 29/// 30/// unnamed-namespace-definition: 31/// 'namespace' attributes[opt] '{' namespace-body '}' 32/// 33/// named-namespace-definition: 34/// original-namespace-definition 35/// extension-namespace-definition 36/// 37/// original-namespace-definition: 38/// 'namespace' identifier attributes[opt] '{' namespace-body '}' 39/// 40/// extension-namespace-definition: 41/// 'namespace' original-namespace-name '{' namespace-body '}' 42/// 43/// namespace-alias-definition: [C++ 7.3.2: namespace.alias] 44/// 'namespace' identifier '=' qualified-namespace-specifier ';' 45/// 46Parser::DeclPtrTy Parser::ParseNamespace(unsigned Context, 47 SourceLocation &DeclEnd) { 48 assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); 49 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. 50 51 if (Tok.is(tok::code_completion)) { 52 Actions.CodeCompleteNamespaceDecl(CurScope); 53 ConsumeToken(); 54 } 55 56 SourceLocation IdentLoc; 57 IdentifierInfo *Ident = 0; 58 59 Token attrTok; 60 61 if (Tok.is(tok::identifier)) { 62 Ident = Tok.getIdentifierInfo(); 63 IdentLoc = ConsumeToken(); // eat the identifier. 64 } 65 66 // Read label attributes, if present. 67 Action::AttrTy *AttrList = 0; 68 if (Tok.is(tok::kw___attribute)) { 69 attrTok = Tok; 70 71 // FIXME: save these somewhere. 72 AttrList = ParseGNUAttributes(); 73 } 74 75 if (Tok.is(tok::equal)) { 76 if (AttrList) 77 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); 78 79 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); 80 } 81 82 if (Tok.isNot(tok::l_brace)) { 83 Diag(Tok, Ident ? diag::err_expected_lbrace : 84 diag::err_expected_ident_lbrace); 85 return DeclPtrTy(); 86 } 87 88 SourceLocation LBrace = ConsumeBrace(); 89 90 // Enter a scope for the namespace. 91 ParseScope NamespaceScope(this, Scope::DeclScope); 92 93 DeclPtrTy NamespcDecl = 94 Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace); 95 96 PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions, 97 PP.getSourceManager(), 98 "parsing namespace"); 99 100 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 101 CXX0XAttributeList Attr; 102 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 103 Attr = ParseCXX0XAttributes(); 104 ParseExternalDeclaration(Attr); 105 } 106 107 // Leave the namespace scope. 108 NamespaceScope.Exit(); 109 110 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace); 111 Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc); 112 113 DeclEnd = RBraceLoc; 114 return NamespcDecl; 115} 116 117/// ParseNamespaceAlias - Parse the part after the '=' in a namespace 118/// alias definition. 119/// 120Parser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, 121 SourceLocation AliasLoc, 122 IdentifierInfo *Alias, 123 SourceLocation &DeclEnd) { 124 assert(Tok.is(tok::equal) && "Not equal token"); 125 126 ConsumeToken(); // eat the '='. 127 128 if (Tok.is(tok::code_completion)) { 129 Actions.CodeCompleteNamespaceAliasDecl(CurScope); 130 ConsumeToken(); 131 } 132 133 CXXScopeSpec SS; 134 // Parse (optional) nested-name-specifier. 135 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); 136 137 if (SS.isInvalid() || Tok.isNot(tok::identifier)) { 138 Diag(Tok, diag::err_expected_namespace_name); 139 // Skip to end of the definition and eat the ';'. 140 SkipUntil(tok::semi); 141 return DeclPtrTy(); 142 } 143 144 // Parse identifier. 145 IdentifierInfo *Ident = Tok.getIdentifierInfo(); 146 SourceLocation IdentLoc = ConsumeToken(); 147 148 // Eat the ';'. 149 DeclEnd = Tok.getLocation(); 150 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name, 151 "", tok::semi); 152 153 return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias, 154 SS, IdentLoc, Ident); 155} 156 157/// ParseLinkage - We know that the current token is a string_literal 158/// and just before that, that extern was seen. 159/// 160/// linkage-specification: [C++ 7.5p2: dcl.link] 161/// 'extern' string-literal '{' declaration-seq[opt] '}' 162/// 'extern' string-literal declaration 163/// 164Parser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS, 165 unsigned Context) { 166 assert(Tok.is(tok::string_literal) && "Not a string literal!"); 167 llvm::SmallVector<char, 8> LangBuffer; 168 // LangBuffer is guaranteed to be big enough. 169 LangBuffer.resize(Tok.getLength()); 170 const char *LangBufPtr = &LangBuffer[0]; 171 unsigned StrSize = PP.getSpelling(Tok, LangBufPtr); 172 173 SourceLocation Loc = ConsumeStringToken(); 174 175 ParseScope LinkageScope(this, Scope::DeclScope); 176 DeclPtrTy LinkageSpec 177 = Actions.ActOnStartLinkageSpecification(CurScope, 178 /*FIXME: */SourceLocation(), 179 Loc, LangBufPtr, StrSize, 180 Tok.is(tok::l_brace)? Tok.getLocation() 181 : SourceLocation()); 182 183 CXX0XAttributeList Attr; 184 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { 185 Attr = ParseCXX0XAttributes(); 186 } 187 188 if (Tok.isNot(tok::l_brace)) { 189 ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList); 190 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, 191 SourceLocation()); 192 } 193 194 if (Attr.HasAttr) 195 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) 196 << Attr.Range; 197 198 SourceLocation LBrace = ConsumeBrace(); 199 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 200 CXX0XAttributeList Attr; 201 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 202 Attr = ParseCXX0XAttributes(); 203 ParseExternalDeclaration(Attr); 204 } 205 206 SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); 207 return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace); 208} 209 210/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or 211/// using-directive. Assumes that current token is 'using'. 212Parser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, 213 SourceLocation &DeclEnd, 214 CXX0XAttributeList Attr) { 215 assert(Tok.is(tok::kw_using) && "Not using token"); 216 217 // Eat 'using'. 218 SourceLocation UsingLoc = ConsumeToken(); 219 220 if (Tok.is(tok::code_completion)) { 221 Actions.CodeCompleteUsing(CurScope); 222 ConsumeToken(); 223 } 224 225 if (Tok.is(tok::kw_namespace)) 226 // Next token after 'using' is 'namespace' so it must be using-directive 227 return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList); 228 229 if (Attr.HasAttr) 230 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) 231 << Attr.Range; 232 233 // Otherwise, it must be using-declaration. 234 // Ignore illegal attributes (the caller should already have issued an error. 235 return ParseUsingDeclaration(Context, UsingLoc, DeclEnd); 236} 237 238/// ParseUsingDirective - Parse C++ using-directive, assumes 239/// that current token is 'namespace' and 'using' was already parsed. 240/// 241/// using-directive: [C++ 7.3.p4: namespace.udir] 242/// 'using' 'namespace' ::[opt] nested-name-specifier[opt] 243/// namespace-name ; 244/// [GNU] using-directive: 245/// 'using' 'namespace' ::[opt] nested-name-specifier[opt] 246/// namespace-name attributes[opt] ; 247/// 248Parser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context, 249 SourceLocation UsingLoc, 250 SourceLocation &DeclEnd, 251 AttributeList *Attr) { 252 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); 253 254 // Eat 'namespace'. 255 SourceLocation NamespcLoc = ConsumeToken(); 256 257 if (Tok.is(tok::code_completion)) { 258 Actions.CodeCompleteUsingDirective(CurScope); 259 ConsumeToken(); 260 } 261 262 CXXScopeSpec SS; 263 // Parse (optional) nested-name-specifier. 264 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); 265 266 IdentifierInfo *NamespcName = 0; 267 SourceLocation IdentLoc = SourceLocation(); 268 269 // Parse namespace-name. 270 if (SS.isInvalid() || Tok.isNot(tok::identifier)) { 271 Diag(Tok, diag::err_expected_namespace_name); 272 // If there was invalid namespace name, skip to end of decl, and eat ';'. 273 SkipUntil(tok::semi); 274 // FIXME: Are there cases, when we would like to call ActOnUsingDirective? 275 return DeclPtrTy(); 276 } 277 278 // Parse identifier. 279 NamespcName = Tok.getIdentifierInfo(); 280 IdentLoc = ConsumeToken(); 281 282 // Parse (optional) attributes (most likely GNU strong-using extension). 283 bool GNUAttr = false; 284 if (Tok.is(tok::kw___attribute)) { 285 GNUAttr = true; 286 Attr = addAttributeLists(Attr, ParseGNUAttributes()); 287 } 288 289 // Eat ';'. 290 DeclEnd = Tok.getLocation(); 291 ExpectAndConsume(tok::semi, 292 GNUAttr ? diag::err_expected_semi_after_attribute_list : 293 diag::err_expected_semi_after_namespace_name, "", tok::semi); 294 295 return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS, 296 IdentLoc, NamespcName, Attr); 297} 298 299/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that 300/// 'using' was already seen. 301/// 302/// using-declaration: [C++ 7.3.p3: namespace.udecl] 303/// 'using' 'typename'[opt] ::[opt] nested-name-specifier 304/// unqualified-id 305/// 'using' :: unqualified-id 306/// 307Parser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context, 308 SourceLocation UsingLoc, 309 SourceLocation &DeclEnd, 310 AccessSpecifier AS) { 311 CXXScopeSpec SS; 312 SourceLocation TypenameLoc; 313 bool IsTypeName; 314 315 // Ignore optional 'typename'. 316 // FIXME: This is wrong; we should parse this as a typename-specifier. 317 if (Tok.is(tok::kw_typename)) { 318 TypenameLoc = Tok.getLocation(); 319 ConsumeToken(); 320 IsTypeName = true; 321 } 322 else 323 IsTypeName = false; 324 325 // Parse nested-name-specifier. 326 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); 327 328 AttributeList *AttrList = 0; 329 330 // Check nested-name specifier. 331 if (SS.isInvalid()) { 332 SkipUntil(tok::semi); 333 return DeclPtrTy(); 334 } 335 336 // Parse the unqualified-id. We allow parsing of both constructor and 337 // destructor names and allow the action module to diagnose any semantic 338 // errors. 339 UnqualifiedId Name; 340 if (ParseUnqualifiedId(SS, 341 /*EnteringContext=*/false, 342 /*AllowDestructorName=*/true, 343 /*AllowConstructorName=*/true, 344 /*ObjectType=*/0, 345 Name)) { 346 SkipUntil(tok::semi); 347 return DeclPtrTy(); 348 } 349 350 // Parse (optional) attributes (most likely GNU strong-using extension). 351 if (Tok.is(tok::kw___attribute)) 352 AttrList = ParseGNUAttributes(); 353 354 // Eat ';'. 355 DeclEnd = Tok.getLocation(); 356 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 357 AttrList ? "attributes list" : "using declaration", 358 tok::semi); 359 360 return Actions.ActOnUsingDeclaration(CurScope, AS, true, UsingLoc, SS, Name, 361 AttrList, IsTypeName, TypenameLoc); 362} 363 364/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion. 365/// 366/// static_assert-declaration: 367/// static_assert ( constant-expression , string-literal ) ; 368/// 369Parser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ 370 assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration"); 371 SourceLocation StaticAssertLoc = ConsumeToken(); 372 373 if (Tok.isNot(tok::l_paren)) { 374 Diag(Tok, diag::err_expected_lparen); 375 return DeclPtrTy(); 376 } 377 378 SourceLocation LParenLoc = ConsumeParen(); 379 380 OwningExprResult AssertExpr(ParseConstantExpression()); 381 if (AssertExpr.isInvalid()) { 382 SkipUntil(tok::semi); 383 return DeclPtrTy(); 384 } 385 386 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) 387 return DeclPtrTy(); 388 389 if (Tok.isNot(tok::string_literal)) { 390 Diag(Tok, diag::err_expected_string_literal); 391 SkipUntil(tok::semi); 392 return DeclPtrTy(); 393 } 394 395 OwningExprResult AssertMessage(ParseStringLiteralExpression()); 396 if (AssertMessage.isInvalid()) 397 return DeclPtrTy(); 398 399 MatchRHSPunctuation(tok::r_paren, LParenLoc); 400 401 DeclEnd = Tok.getLocation(); 402 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert); 403 404 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr), 405 move(AssertMessage)); 406} 407 408/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier. 409/// 410/// 'decltype' ( expression ) 411/// 412void Parser::ParseDecltypeSpecifier(DeclSpec &DS) { 413 assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier"); 414 415 SourceLocation StartLoc = ConsumeToken(); 416 SourceLocation LParenLoc = Tok.getLocation(); 417 418 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, 419 "decltype")) { 420 SkipUntil(tok::r_paren); 421 return; 422 } 423 424 // Parse the expression 425 426 // C++0x [dcl.type.simple]p4: 427 // The operand of the decltype specifier is an unevaluated operand. 428 EnterExpressionEvaluationContext Unevaluated(Actions, 429 Action::Unevaluated); 430 OwningExprResult Result = ParseExpression(); 431 if (Result.isInvalid()) { 432 SkipUntil(tok::r_paren); 433 return; 434 } 435 436 // Match the ')' 437 SourceLocation RParenLoc; 438 if (Tok.is(tok::r_paren)) 439 RParenLoc = ConsumeParen(); 440 else 441 MatchRHSPunctuation(tok::r_paren, LParenLoc); 442 443 if (RParenLoc.isInvalid()) 444 return; 445 446 const char *PrevSpec = 0; 447 unsigned DiagID; 448 // Check for duplicate type specifiers (e.g. "int decltype(a)"). 449 if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, 450 DiagID, Result.release())) 451 Diag(StartLoc, DiagID) << PrevSpec; 452} 453 454/// ParseClassName - Parse a C++ class-name, which names a class. Note 455/// that we only check that the result names a type; semantic analysis 456/// will need to verify that the type names a class. The result is 457/// either a type or NULL, depending on whether a type name was 458/// found. 459/// 460/// class-name: [C++ 9.1] 461/// identifier 462/// simple-template-id 463/// 464Parser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation, 465 const CXXScopeSpec *SS, 466 bool DestrExpected) { 467 // Check whether we have a template-id that names a type. 468 if (Tok.is(tok::annot_template_id)) { 469 TemplateIdAnnotation *TemplateId 470 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 471 if (TemplateId->Kind == TNK_Type_template) { 472 AnnotateTemplateIdTokenAsType(SS); 473 474 assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); 475 TypeTy *Type = Tok.getAnnotationValue(); 476 EndLocation = Tok.getAnnotationEndLoc(); 477 ConsumeToken(); 478 479 if (Type) 480 return Type; 481 return true; 482 } 483 484 // Fall through to produce an error below. 485 } 486 487 if (Tok.isNot(tok::identifier)) { 488 Diag(Tok, diag::err_expected_class_name); 489 return true; 490 } 491 492 // We have an identifier; check whether it is actually a type. 493 TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(), 494 Tok.getLocation(), CurScope, SS, 495 true); 496 if (!Type) { 497 Diag(Tok, DestrExpected ? diag::err_destructor_class_name 498 : diag::err_expected_class_name); 499 return true; 500 } 501 502 // Consume the identifier. 503 EndLocation = ConsumeToken(); 504 return Type; 505} 506 507/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or 508/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which 509/// until we reach the start of a definition or see a token that 510/// cannot start a definition. 511/// 512/// class-specifier: [C++ class] 513/// class-head '{' member-specification[opt] '}' 514/// class-head '{' member-specification[opt] '}' attributes[opt] 515/// class-head: 516/// class-key identifier[opt] base-clause[opt] 517/// class-key nested-name-specifier identifier base-clause[opt] 518/// class-key nested-name-specifier[opt] simple-template-id 519/// base-clause[opt] 520/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] 521/// [GNU] class-key attributes[opt] nested-name-specifier 522/// identifier base-clause[opt] 523/// [GNU] class-key attributes[opt] nested-name-specifier[opt] 524/// simple-template-id base-clause[opt] 525/// class-key: 526/// 'class' 527/// 'struct' 528/// 'union' 529/// 530/// elaborated-type-specifier: [C++ dcl.type.elab] 531/// class-key ::[opt] nested-name-specifier[opt] identifier 532/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] 533/// simple-template-id 534/// 535/// Note that the C++ class-specifier and elaborated-type-specifier, 536/// together, subsume the C99 struct-or-union-specifier: 537/// 538/// struct-or-union-specifier: [C99 6.7.2.1] 539/// struct-or-union identifier[opt] '{' struct-contents '}' 540/// struct-or-union identifier 541/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents 542/// '}' attributes[opt] 543/// [GNU] struct-or-union attributes[opt] identifier 544/// struct-or-union: 545/// 'struct' 546/// 'union' 547void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, 548 SourceLocation StartLoc, DeclSpec &DS, 549 const ParsedTemplateInfo &TemplateInfo, 550 AccessSpecifier AS) { 551 DeclSpec::TST TagType; 552 if (TagTokKind == tok::kw_struct) 553 TagType = DeclSpec::TST_struct; 554 else if (TagTokKind == tok::kw_class) 555 TagType = DeclSpec::TST_class; 556 else { 557 assert(TagTokKind == tok::kw_union && "Not a class specifier"); 558 TagType = DeclSpec::TST_union; 559 } 560 561 if (Tok.is(tok::code_completion)) { 562 // Code completion for a struct, class, or union name. 563 Actions.CodeCompleteTag(CurScope, TagType); 564 ConsumeToken(); 565 } 566 567 AttributeList *AttrList = 0; 568 // If attributes exist after tag, parse them. 569 if (Tok.is(tok::kw___attribute)) 570 AttrList = ParseGNUAttributes(); 571 572 // If declspecs exist after tag, parse them. 573 if (Tok.is(tok::kw___declspec)) 574 AttrList = ParseMicrosoftDeclSpec(AttrList); 575 576 // If C++0x attributes exist here, parse them. 577 // FIXME: Are we consistent with the ordering of parsing of different 578 // styles of attributes? 579 if (isCXX0XAttributeSpecifier()) 580 AttrList = addAttributeLists(AttrList, ParseCXX0XAttributes().AttrList); 581 582 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_pod)) { 583 // GNU libstdc++ 4.2 uses __is_pod as the name of a struct template, but 584 // __is_pod is a keyword in GCC >= 4.3. Therefore, when we see the 585 // token sequence "struct __is_pod", make __is_pod into a normal 586 // identifier rather than a keyword, to allow libstdc++ 4.2 to work 587 // properly. 588 Tok.getIdentifierInfo()->setTokenID(tok::identifier); 589 Tok.setKind(tok::identifier); 590 } 591 592 if (TagType == DeclSpec::TST_struct && Tok.is(tok::kw___is_empty)) { 593 // GNU libstdc++ 4.2 uses __is_empty as the name of a struct template, but 594 // __is_empty is a keyword in GCC >= 4.3. Therefore, when we see the 595 // token sequence "struct __is_empty", make __is_empty into a normal 596 // identifier rather than a keyword, to allow libstdc++ 4.2 to work 597 // properly. 598 Tok.getIdentifierInfo()->setTokenID(tok::identifier); 599 Tok.setKind(tok::identifier); 600 } 601 602 // Parse the (optional) nested-name-specifier. 603 CXXScopeSpec &SS = DS.getTypeSpecScope(); 604 if (getLang().CPlusPlus) { 605 // "FOO : BAR" is not a potential typo for "FOO::BAR". 606 ColonProtectionRAIIObject X(*this); 607 608 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) 609 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) 610 Diag(Tok, diag::err_expected_ident); 611 } 612 613 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams; 614 615 // Parse the (optional) class name or simple-template-id. 616 IdentifierInfo *Name = 0; 617 SourceLocation NameLoc; 618 TemplateIdAnnotation *TemplateId = 0; 619 if (Tok.is(tok::identifier)) { 620 Name = Tok.getIdentifierInfo(); 621 NameLoc = ConsumeToken(); 622 623 if (Tok.is(tok::less)) { 624 // The name was supposed to refer to a template, but didn't. 625 // Eat the template argument list and try to continue parsing this as 626 // a class (or template thereof). 627 TemplateArgList TemplateArgs; 628 SourceLocation LAngleLoc, RAngleLoc; 629 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, &SS, 630 true, LAngleLoc, 631 TemplateArgs, RAngleLoc)) { 632 // We couldn't parse the template argument list at all, so don't 633 // try to give any location information for the list. 634 LAngleLoc = RAngleLoc = SourceLocation(); 635 } 636 637 Diag(NameLoc, diag::err_explicit_spec_non_template) 638 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) 639 << (TagType == DeclSpec::TST_class? 0 640 : TagType == DeclSpec::TST_struct? 1 641 : 2) 642 << Name 643 << SourceRange(LAngleLoc, RAngleLoc); 644 645 // Strip off the last template parameter list if it was empty, since 646 // we've removed its template argument list. 647 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) { 648 if (TemplateParams && TemplateParams->size() > 1) { 649 TemplateParams->pop_back(); 650 } else { 651 TemplateParams = 0; 652 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind 653 = ParsedTemplateInfo::NonTemplate; 654 } 655 } else if (TemplateInfo.Kind 656 == ParsedTemplateInfo::ExplicitInstantiation) { 657 // Pretend this is just a forward declaration. 658 TemplateParams = 0; 659 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind 660 = ParsedTemplateInfo::NonTemplate; 661 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc 662 = SourceLocation(); 663 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc 664 = SourceLocation(); 665 } 666 667 668 } 669 } else if (Tok.is(tok::annot_template_id)) { 670 TemplateId = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 671 NameLoc = ConsumeToken(); 672 673 if (TemplateId->Kind != TNK_Type_template) { 674 // The template-name in the simple-template-id refers to 675 // something other than a class template. Give an appropriate 676 // error message and skip to the ';'. 677 SourceRange Range(NameLoc); 678 if (SS.isNotEmpty()) 679 Range.setBegin(SS.getBeginLoc()); 680 681 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template) 682 << Name << static_cast<int>(TemplateId->Kind) << Range; 683 684 DS.SetTypeSpecError(); 685 SkipUntil(tok::semi, false, true); 686 TemplateId->Destroy(); 687 return; 688 } 689 } 690 691 // There are four options here. If we have 'struct foo;', then this 692 // is either a forward declaration or a friend declaration, which 693 // have to be treated differently. If we have 'struct foo {...' or 694 // 'struct foo :...' then this is a definition. Otherwise we have 695 // something like 'struct foo xyz', a reference. 696 Action::TagUseKind TUK; 697 if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon))) { 698 if (DS.isFriendSpecified()) { 699 // C++ [class.friend]p2: 700 // A class shall not be defined in a friend declaration. 701 Diag(Tok.getLocation(), diag::err_friend_decl_defines_class) 702 << SourceRange(DS.getFriendSpecLoc()); 703 704 // Skip everything up to the semicolon, so that this looks like a proper 705 // friend class (or template thereof) declaration. 706 SkipUntil(tok::semi, true, true); 707 TUK = Action::TUK_Friend; 708 } else { 709 // Okay, this is a class definition. 710 TUK = Action::TUK_Definition; 711 } 712 } else if (Tok.is(tok::semi)) 713 TUK = DS.isFriendSpecified() ? Action::TUK_Friend : Action::TUK_Declaration; 714 else 715 TUK = Action::TUK_Reference; 716 717 if (!Name && !TemplateId && TUK != Action::TUK_Definition) { 718 // We have a declaration or reference to an anonymous class. 719 Diag(StartLoc, diag::err_anon_type_definition) 720 << DeclSpec::getSpecifierName(TagType); 721 722 SkipUntil(tok::comma, true); 723 724 if (TemplateId) 725 TemplateId->Destroy(); 726 return; 727 } 728 729 // Create the tag portion of the class or class template. 730 Action::DeclResult TagOrTempResult = true; // invalid 731 Action::TypeResult TypeResult = true; // invalid 732 733 // FIXME: When TUK == TUK_Reference and we have a template-id, we need 734 // to turn that template-id into a type. 735 736 bool Owned = false; 737 if (TemplateId) { 738 // Explicit specialization, class template partial specialization, 739 // or explicit instantiation. 740 ASTTemplateArgsPtr TemplateArgsPtr(Actions, 741 TemplateId->getTemplateArgs(), 742 TemplateId->NumArgs); 743 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && 744 TUK == Action::TUK_Declaration) { 745 // This is an explicit instantiation of a class template. 746 TagOrTempResult 747 = Actions.ActOnExplicitInstantiation(CurScope, 748 TemplateInfo.ExternLoc, 749 TemplateInfo.TemplateLoc, 750 TagType, 751 StartLoc, 752 SS, 753 TemplateTy::make(TemplateId->Template), 754 TemplateId->TemplateNameLoc, 755 TemplateId->LAngleLoc, 756 TemplateArgsPtr, 757 TemplateId->RAngleLoc, 758 AttrList); 759 } else if (TUK == Action::TUK_Reference) { 760 TypeResult 761 = Actions.ActOnTemplateIdType(TemplateTy::make(TemplateId->Template), 762 TemplateId->TemplateNameLoc, 763 TemplateId->LAngleLoc, 764 TemplateArgsPtr, 765 TemplateId->RAngleLoc); 766 767 TypeResult = Actions.ActOnTagTemplateIdType(TypeResult, TUK, 768 TagType, StartLoc); 769 } else { 770 // This is an explicit specialization or a class template 771 // partial specialization. 772 TemplateParameterLists FakedParamLists; 773 774 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { 775 // This looks like an explicit instantiation, because we have 776 // something like 777 // 778 // template class Foo<X> 779 // 780 // but it actually has a definition. Most likely, this was 781 // meant to be an explicit specialization, but the user forgot 782 // the '<>' after 'template'. 783 assert(TUK == Action::TUK_Definition && "Expected a definition here"); 784 785 SourceLocation LAngleLoc 786 = PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); 787 Diag(TemplateId->TemplateNameLoc, 788 diag::err_explicit_instantiation_with_definition) 789 << SourceRange(TemplateInfo.TemplateLoc) 790 << CodeModificationHint::CreateInsertion(LAngleLoc, "<>"); 791 792 // Create a fake template parameter list that contains only 793 // "template<>", so that we treat this construct as a class 794 // template specialization. 795 FakedParamLists.push_back( 796 Actions.ActOnTemplateParameterList(0, SourceLocation(), 797 TemplateInfo.TemplateLoc, 798 LAngleLoc, 799 0, 0, 800 LAngleLoc)); 801 TemplateParams = &FakedParamLists; 802 } 803 804 // Build the class template specialization. 805 TagOrTempResult 806 = Actions.ActOnClassTemplateSpecialization(CurScope, TagType, TUK, 807 StartLoc, SS, 808 TemplateTy::make(TemplateId->Template), 809 TemplateId->TemplateNameLoc, 810 TemplateId->LAngleLoc, 811 TemplateArgsPtr, 812 TemplateId->RAngleLoc, 813 AttrList, 814 Action::MultiTemplateParamsArg(Actions, 815 TemplateParams? &(*TemplateParams)[0] : 0, 816 TemplateParams? TemplateParams->size() : 0)); 817 } 818 TemplateId->Destroy(); 819 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && 820 TUK == Action::TUK_Declaration) { 821 // Explicit instantiation of a member of a class template 822 // specialization, e.g., 823 // 824 // template struct Outer<int>::Inner; 825 // 826 TagOrTempResult 827 = Actions.ActOnExplicitInstantiation(CurScope, 828 TemplateInfo.ExternLoc, 829 TemplateInfo.TemplateLoc, 830 TagType, StartLoc, SS, Name, 831 NameLoc, AttrList); 832 } else { 833 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation && 834 TUK == Action::TUK_Definition) { 835 // FIXME: Diagnose this particular error. 836 } 837 838 bool IsDependent = false; 839 840 // Declaration or definition of a class type 841 TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TUK, StartLoc, SS, 842 Name, NameLoc, AttrList, AS, 843 Action::MultiTemplateParamsArg(Actions, 844 TemplateParams? &(*TemplateParams)[0] : 0, 845 TemplateParams? TemplateParams->size() : 0), 846 Owned, IsDependent); 847 848 // If ActOnTag said the type was dependent, try again with the 849 // less common call. 850 if (IsDependent) 851 TypeResult = Actions.ActOnDependentTag(CurScope, TagType, TUK, 852 SS, Name, StartLoc, NameLoc); 853 } 854 855 // If there is a body, parse it and inform the actions module. 856 if (TUK == Action::TUK_Definition) { 857 assert(Tok.is(tok::l_brace) || 858 (getLang().CPlusPlus && Tok.is(tok::colon))); 859 if (getLang().CPlusPlus) 860 ParseCXXMemberSpecification(StartLoc, TagType, TagOrTempResult.get()); 861 else 862 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get()); 863 } 864 865 void *Result; 866 if (!TypeResult.isInvalid()) { 867 TagType = DeclSpec::TST_typename; 868 Result = TypeResult.get(); 869 Owned = false; 870 } else if (!TagOrTempResult.isInvalid()) { 871 Result = TagOrTempResult.get().getAs<void>(); 872 } else { 873 DS.SetTypeSpecError(); 874 return; 875 } 876 877 const char *PrevSpec = 0; 878 unsigned DiagID; 879 880 if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, DiagID, 881 Result, Owned)) 882 Diag(StartLoc, DiagID) << PrevSpec; 883} 884 885/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived]. 886/// 887/// base-clause : [C++ class.derived] 888/// ':' base-specifier-list 889/// base-specifier-list: 890/// base-specifier '...'[opt] 891/// base-specifier-list ',' base-specifier '...'[opt] 892void Parser::ParseBaseClause(DeclPtrTy ClassDecl) { 893 assert(Tok.is(tok::colon) && "Not a base clause"); 894 ConsumeToken(); 895 896 // Build up an array of parsed base specifiers. 897 llvm::SmallVector<BaseTy *, 8> BaseInfo; 898 899 while (true) { 900 // Parse a base-specifier. 901 BaseResult Result = ParseBaseSpecifier(ClassDecl); 902 if (Result.isInvalid()) { 903 // Skip the rest of this base specifier, up until the comma or 904 // opening brace. 905 SkipUntil(tok::comma, tok::l_brace, true, true); 906 } else { 907 // Add this to our array of base specifiers. 908 BaseInfo.push_back(Result.get()); 909 } 910 911 // If the next token is a comma, consume it and keep reading 912 // base-specifiers. 913 if (Tok.isNot(tok::comma)) break; 914 915 // Consume the comma. 916 ConsumeToken(); 917 } 918 919 // Attach the base specifiers 920 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size()); 921} 922 923/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is 924/// one entry in the base class list of a class specifier, for example: 925/// class foo : public bar, virtual private baz { 926/// 'public bar' and 'virtual private baz' are each base-specifiers. 927/// 928/// base-specifier: [C++ class.derived] 929/// ::[opt] nested-name-specifier[opt] class-name 930/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt] 931/// class-name 932/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt] 933/// class-name 934Parser::BaseResult Parser::ParseBaseSpecifier(DeclPtrTy ClassDecl) { 935 bool IsVirtual = false; 936 SourceLocation StartLoc = Tok.getLocation(); 937 938 // Parse the 'virtual' keyword. 939 if (Tok.is(tok::kw_virtual)) { 940 ConsumeToken(); 941 IsVirtual = true; 942 } 943 944 // Parse an (optional) access specifier. 945 AccessSpecifier Access = getAccessSpecifierIfPresent(); 946 if (Access) 947 ConsumeToken(); 948 949 // Parse the 'virtual' keyword (again!), in case it came after the 950 // access specifier. 951 if (Tok.is(tok::kw_virtual)) { 952 SourceLocation VirtualLoc = ConsumeToken(); 953 if (IsVirtual) { 954 // Complain about duplicate 'virtual' 955 Diag(VirtualLoc, diag::err_dup_virtual) 956 << CodeModificationHint::CreateRemoval(VirtualLoc); 957 } 958 959 IsVirtual = true; 960 } 961 962 // Parse optional '::' and optional nested-name-specifier. 963 CXXScopeSpec SS; 964 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); 965 966 // The location of the base class itself. 967 SourceLocation BaseLoc = Tok.getLocation(); 968 969 // Parse the class-name. 970 SourceLocation EndLocation; 971 TypeResult BaseType = ParseClassName(EndLocation, &SS); 972 if (BaseType.isInvalid()) 973 return true; 974 975 // Find the complete source range for the base-specifier. 976 SourceRange Range(StartLoc, EndLocation); 977 978 // Notify semantic analysis that we have parsed a complete 979 // base-specifier. 980 return Actions.ActOnBaseSpecifier(ClassDecl, Range, IsVirtual, Access, 981 BaseType.get(), BaseLoc); 982} 983 984/// getAccessSpecifierIfPresent - Determine whether the next token is 985/// a C++ access-specifier. 986/// 987/// access-specifier: [C++ class.derived] 988/// 'private' 989/// 'protected' 990/// 'public' 991AccessSpecifier Parser::getAccessSpecifierIfPresent() const { 992 switch (Tok.getKind()) { 993 default: return AS_none; 994 case tok::kw_private: return AS_private; 995 case tok::kw_protected: return AS_protected; 996 case tok::kw_public: return AS_public; 997 } 998} 999 1000void Parser::HandleMemberFunctionDefaultArgs(Declarator& DeclaratorInfo, 1001 DeclPtrTy ThisDecl) { 1002 // We just declared a member function. If this member function 1003 // has any default arguments, we'll need to parse them later. 1004 LateParsedMethodDeclaration *LateMethod = 0; 1005 DeclaratorChunk::FunctionTypeInfo &FTI 1006 = DeclaratorInfo.getTypeObject(0).Fun; 1007 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) { 1008 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) { 1009 if (!LateMethod) { 1010 // Push this method onto the stack of late-parsed method 1011 // declarations. 1012 getCurrentClass().MethodDecls.push_back( 1013 LateParsedMethodDeclaration(ThisDecl)); 1014 LateMethod = &getCurrentClass().MethodDecls.back(); 1015 LateMethod->TemplateScope = CurScope->isTemplateParamScope(); 1016 1017 // Add all of the parameters prior to this one (they don't 1018 // have default arguments). 1019 LateMethod->DefaultArgs.reserve(FTI.NumArgs); 1020 for (unsigned I = 0; I < ParamIdx; ++I) 1021 LateMethod->DefaultArgs.push_back( 1022 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param)); 1023 } 1024 1025 // Add this parameter to the list of parameters (it or may 1026 // not have a default argument). 1027 LateMethod->DefaultArgs.push_back( 1028 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param, 1029 FTI.ArgInfo[ParamIdx].DefaultArgTokens)); 1030 } 1031 } 1032} 1033 1034/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration. 1035/// 1036/// member-declaration: 1037/// decl-specifier-seq[opt] member-declarator-list[opt] ';' 1038/// function-definition ';'[opt] 1039/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO] 1040/// using-declaration [TODO] 1041/// [C++0x] static_assert-declaration 1042/// template-declaration 1043/// [GNU] '__extension__' member-declaration 1044/// 1045/// member-declarator-list: 1046/// member-declarator 1047/// member-declarator-list ',' member-declarator 1048/// 1049/// member-declarator: 1050/// declarator pure-specifier[opt] 1051/// declarator constant-initializer[opt] 1052/// identifier[opt] ':' constant-expression 1053/// 1054/// pure-specifier: 1055/// '= 0' 1056/// 1057/// constant-initializer: 1058/// '=' constant-expression 1059/// 1060void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS, 1061 const ParsedTemplateInfo &TemplateInfo) { 1062 // Access declarations. 1063 if (!TemplateInfo.Kind && 1064 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) && 1065 TryAnnotateCXXScopeToken() && 1066 Tok.is(tok::annot_cxxscope)) { 1067 bool isAccessDecl = false; 1068 if (NextToken().is(tok::identifier)) 1069 isAccessDecl = GetLookAheadToken(2).is(tok::semi); 1070 else 1071 isAccessDecl = NextToken().is(tok::kw_operator); 1072 1073 if (isAccessDecl) { 1074 // Collect the scope specifier token we annotated earlier. 1075 CXXScopeSpec SS; 1076 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType*/ 0, false); 1077 1078 // Try to parse an unqualified-id. 1079 UnqualifiedId Name; 1080 if (ParseUnqualifiedId(SS, false, true, true, /*ObjectType*/ 0, Name)) { 1081 SkipUntil(tok::semi); 1082 return; 1083 } 1084 1085 // TODO: recover from mistakenly-qualified operator declarations. 1086 if (ExpectAndConsume(tok::semi, 1087 diag::err_expected_semi_after, 1088 "access declaration", 1089 tok::semi)) 1090 return; 1091 1092 Actions.ActOnUsingDeclaration(CurScope, AS, 1093 false, SourceLocation(), 1094 SS, Name, 1095 /* AttrList */ 0, 1096 /* IsTypeName */ false, 1097 SourceLocation()); 1098 return; 1099 } 1100 } 1101 1102 // static_assert-declaration 1103 if (Tok.is(tok::kw_static_assert)) { 1104 // FIXME: Check for templates 1105 SourceLocation DeclEnd; 1106 ParseStaticAssertDeclaration(DeclEnd); 1107 return; 1108 } 1109 1110 if (Tok.is(tok::kw_template)) { 1111 assert(!TemplateInfo.TemplateParams && 1112 "Nested template improperly parsed?"); 1113 SourceLocation DeclEnd; 1114 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd, 1115 AS); 1116 return; 1117 } 1118 1119 // Handle: member-declaration ::= '__extension__' member-declaration 1120 if (Tok.is(tok::kw___extension__)) { 1121 // __extension__ silences extension warnings in the subexpression. 1122 ExtensionRAIIObject O(Diags); // Use RAII to do this. 1123 ConsumeToken(); 1124 return ParseCXXClassMemberDeclaration(AS, TemplateInfo); 1125 } 1126 1127 // Don't parse FOO:BAR as if it were a typo for FOO::BAR. 1128 ColonProtectionRAIIObject X(*this); 1129 1130 CXX0XAttributeList AttrList; 1131 // Optional C++0x attribute-specifier 1132 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 1133 AttrList = ParseCXX0XAttributes(); 1134 1135 if (Tok.is(tok::kw_using)) { 1136 // FIXME: Check for template aliases 1137 1138 if (AttrList.HasAttr) 1139 Diag(AttrList.Range.getBegin(), diag::err_attributes_not_allowed) 1140 << AttrList.Range; 1141 1142 // Eat 'using'. 1143 SourceLocation UsingLoc = ConsumeToken(); 1144 1145 if (Tok.is(tok::kw_namespace)) { 1146 Diag(UsingLoc, diag::err_using_namespace_in_class); 1147 SkipUntil(tok::semi, true, true); 1148 } 1149 else { 1150 SourceLocation DeclEnd; 1151 // Otherwise, it must be using-declaration. 1152 ParseUsingDeclaration(Declarator::MemberContext, UsingLoc, DeclEnd, AS); 1153 } 1154 return; 1155 } 1156 1157 SourceLocation DSStart = Tok.getLocation(); 1158 // decl-specifier-seq: 1159 // Parse the common declaration-specifiers piece. 1160 ParsingDeclSpec DS(*this); 1161 DS.AddAttributes(AttrList.AttrList); 1162 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class); 1163 1164 Action::MultiTemplateParamsArg TemplateParams(Actions, 1165 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0, 1166 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0); 1167 1168 if (Tok.is(tok::semi)) { 1169 ConsumeToken(); 1170 Actions.ParsedFreeStandingDeclSpec(CurScope, DS); 1171 return; 1172 } 1173 1174 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext); 1175 1176 if (Tok.isNot(tok::colon)) { 1177 // Don't parse FOO:BAR as if it were a typo for FOO::BAR. 1178 ColonProtectionRAIIObject X(*this); 1179 1180 // Parse the first declarator. 1181 ParseDeclarator(DeclaratorInfo); 1182 // Error parsing the declarator? 1183 if (!DeclaratorInfo.hasName()) { 1184 // If so, skip until the semi-colon or a }. 1185 SkipUntil(tok::r_brace, true); 1186 if (Tok.is(tok::semi)) 1187 ConsumeToken(); 1188 return; 1189 } 1190 1191 // If attributes exist after the declarator, but before an '{', parse them. 1192 if (Tok.is(tok::kw___attribute)) { 1193 SourceLocation Loc; 1194 AttributeList *AttrList = ParseGNUAttributes(&Loc); 1195 DeclaratorInfo.AddAttributes(AttrList, Loc); 1196 } 1197 1198 // function-definition: 1199 if (Tok.is(tok::l_brace) 1200 || (DeclaratorInfo.isFunctionDeclarator() && 1201 (Tok.is(tok::colon) || Tok.is(tok::kw_try)))) { 1202 if (!DeclaratorInfo.isFunctionDeclarator()) { 1203 Diag(Tok, diag::err_func_def_no_params); 1204 ConsumeBrace(); 1205 SkipUntil(tok::r_brace, true); 1206 return; 1207 } 1208 1209 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { 1210 Diag(Tok, diag::err_function_declared_typedef); 1211 // This recovery skips the entire function body. It would be nice 1212 // to simply call ParseCXXInlineMethodDef() below, however Sema 1213 // assumes the declarator represents a function, not a typedef. 1214 ConsumeBrace(); 1215 SkipUntil(tok::r_brace, true); 1216 return; 1217 } 1218 1219 ParseCXXInlineMethodDef(AS, DeclaratorInfo, TemplateInfo); 1220 return; 1221 } 1222 } 1223 1224 // member-declarator-list: 1225 // member-declarator 1226 // member-declarator-list ',' member-declarator 1227 1228 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup; 1229 OwningExprResult BitfieldSize(Actions); 1230 OwningExprResult Init(Actions); 1231 bool Deleted = false; 1232 1233 while (1) { 1234 // member-declarator: 1235 // declarator pure-specifier[opt] 1236 // declarator constant-initializer[opt] 1237 // identifier[opt] ':' constant-expression 1238 1239 if (Tok.is(tok::colon)) { 1240 ConsumeToken(); 1241 BitfieldSize = ParseConstantExpression(); 1242 if (BitfieldSize.isInvalid()) 1243 SkipUntil(tok::comma, true, true); 1244 } 1245 1246 // pure-specifier: 1247 // '= 0' 1248 // 1249 // constant-initializer: 1250 // '=' constant-expression 1251 // 1252 // defaulted/deleted function-definition: 1253 // '=' 'default' [TODO] 1254 // '=' 'delete' 1255 1256 if (Tok.is(tok::equal)) { 1257 ConsumeToken(); 1258 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) { 1259 ConsumeToken(); 1260 Deleted = true; 1261 } else { 1262 Init = ParseInitializer(); 1263 if (Init.isInvalid()) 1264 SkipUntil(tok::comma, true, true); 1265 } 1266 } 1267 1268 // If attributes exist after the declarator, parse them. 1269 if (Tok.is(tok::kw___attribute)) { 1270 SourceLocation Loc; 1271 AttributeList *AttrList = ParseGNUAttributes(&Loc); 1272 DeclaratorInfo.AddAttributes(AttrList, Loc); 1273 } 1274 1275 // NOTE: If Sema is the Action module and declarator is an instance field, 1276 // this call will *not* return the created decl; It will return null. 1277 // See Sema::ActOnCXXMemberDeclarator for details. 1278 1279 DeclPtrTy ThisDecl; 1280 if (DS.isFriendSpecified()) { 1281 // TODO: handle initializers, bitfields, 'delete' 1282 ThisDecl = Actions.ActOnFriendFunctionDecl(CurScope, DeclaratorInfo, 1283 /*IsDefinition*/ false, 1284 move(TemplateParams)); 1285 } else { 1286 ThisDecl = Actions.ActOnCXXMemberDeclarator(CurScope, AS, 1287 DeclaratorInfo, 1288 move(TemplateParams), 1289 BitfieldSize.release(), 1290 Init.release(), 1291 /*IsDefinition*/Deleted, 1292 Deleted); 1293 } 1294 if (ThisDecl) 1295 DeclsInGroup.push_back(ThisDecl); 1296 1297 if (DeclaratorInfo.isFunctionDeclarator() && 1298 DeclaratorInfo.getDeclSpec().getStorageClassSpec() 1299 != DeclSpec::SCS_typedef) { 1300 HandleMemberFunctionDefaultArgs(DeclaratorInfo, ThisDecl); 1301 } 1302 1303 DeclaratorInfo.complete(ThisDecl); 1304 1305 // If we don't have a comma, it is either the end of the list (a ';') 1306 // or an error, bail out. 1307 if (Tok.isNot(tok::comma)) 1308 break; 1309 1310 // Consume the comma. 1311 ConsumeToken(); 1312 1313 // Parse the next declarator. 1314 DeclaratorInfo.clear(); 1315 BitfieldSize = 0; 1316 Init = 0; 1317 Deleted = false; 1318 1319 // Attributes are only allowed on the second declarator. 1320 if (Tok.is(tok::kw___attribute)) { 1321 SourceLocation Loc; 1322 AttributeList *AttrList = ParseGNUAttributes(&Loc); 1323 DeclaratorInfo.AddAttributes(AttrList, Loc); 1324 } 1325 1326 if (Tok.isNot(tok::colon)) 1327 ParseDeclarator(DeclaratorInfo); 1328 } 1329 1330 if (Tok.is(tok::semi)) { 1331 ConsumeToken(); 1332 Actions.FinalizeDeclaratorGroup(CurScope, DS, DeclsInGroup.data(), 1333 DeclsInGroup.size()); 1334 return; 1335 } 1336 1337 Diag(Tok, diag::err_expected_semi_decl_list); 1338 // Skip to end of block or statement 1339 SkipUntil(tok::r_brace, true, true); 1340 if (Tok.is(tok::semi)) 1341 ConsumeToken(); 1342 return; 1343} 1344 1345/// ParseCXXMemberSpecification - Parse the class definition. 1346/// 1347/// member-specification: 1348/// member-declaration member-specification[opt] 1349/// access-specifier ':' member-specification[opt] 1350/// 1351void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc, 1352 unsigned TagType, DeclPtrTy TagDecl) { 1353 assert((TagType == DeclSpec::TST_struct || 1354 TagType == DeclSpec::TST_union || 1355 TagType == DeclSpec::TST_class) && "Invalid TagType!"); 1356 1357 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions, 1358 PP.getSourceManager(), 1359 "parsing struct/union/class body"); 1360 1361 // Determine whether this is a top-level (non-nested) class. 1362 bool TopLevelClass = ClassStack.empty() || 1363 CurScope->isInCXXInlineMethodScope(); 1364 1365 // Enter a scope for the class. 1366 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope); 1367 1368 // Note that we are parsing a new (potentially-nested) class definition. 1369 ParsingClassDefinition ParsingDef(*this, TagDecl, TopLevelClass); 1370 1371 if (TagDecl) 1372 Actions.ActOnTagStartDefinition(CurScope, TagDecl); 1373 1374 if (Tok.is(tok::colon)) { 1375 ParseBaseClause(TagDecl); 1376 1377 if (!Tok.is(tok::l_brace)) { 1378 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers); 1379 return; 1380 } 1381 } 1382 1383 assert(Tok.is(tok::l_brace)); 1384 1385 SourceLocation LBraceLoc = ConsumeBrace(); 1386 1387 if (!TagDecl) { 1388 SkipUntil(tok::r_brace, false, false); 1389 return; 1390 } 1391 1392 Actions.ActOnStartCXXMemberDeclarations(CurScope, TagDecl, LBraceLoc); 1393 1394 // C++ 11p3: Members of a class defined with the keyword class are private 1395 // by default. Members of a class defined with the keywords struct or union 1396 // are public by default. 1397 AccessSpecifier CurAS; 1398 if (TagType == DeclSpec::TST_class) 1399 CurAS = AS_private; 1400 else 1401 CurAS = AS_public; 1402 1403 // While we still have something to read, read the member-declarations. 1404 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 1405 // Each iteration of this loop reads one member-declaration. 1406 1407 // Check for extraneous top-level semicolon. 1408 if (Tok.is(tok::semi)) { 1409 Diag(Tok, diag::ext_extra_struct_semi) 1410 << CodeModificationHint::CreateRemoval(Tok.getLocation()); 1411 ConsumeToken(); 1412 continue; 1413 } 1414 1415 AccessSpecifier AS = getAccessSpecifierIfPresent(); 1416 if (AS != AS_none) { 1417 // Current token is a C++ access specifier. 1418 CurAS = AS; 1419 ConsumeToken(); 1420 ExpectAndConsume(tok::colon, diag::err_expected_colon); 1421 continue; 1422 } 1423 1424 // FIXME: Make sure we don't have a template here. 1425 1426 // Parse all the comma separated declarators. 1427 ParseCXXClassMemberDeclaration(CurAS); 1428 } 1429 1430 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); 1431 1432 AttributeList *AttrList = 0; 1433 // If attributes exist after class contents, parse them. 1434 if (Tok.is(tok::kw___attribute)) 1435 AttrList = ParseGNUAttributes(); // FIXME: where should I put them? 1436 1437 Actions.ActOnFinishCXXMemberSpecification(CurScope, RecordLoc, TagDecl, 1438 LBraceLoc, RBraceLoc); 1439 1440 // C++ 9.2p2: Within the class member-specification, the class is regarded as 1441 // complete within function bodies, default arguments, 1442 // exception-specifications, and constructor ctor-initializers (including 1443 // such things in nested classes). 1444 // 1445 // FIXME: Only function bodies and constructor ctor-initializers are 1446 // parsed correctly, fix the rest. 1447 if (TopLevelClass) { 1448 // We are not inside a nested class. This class and its nested classes 1449 // are complete and we can parse the delayed portions of method 1450 // declarations and the lexed inline method definitions. 1451 ParseLexedMethodDeclarations(getCurrentClass()); 1452 ParseLexedMethodDefs(getCurrentClass()); 1453 } 1454 1455 // Leave the class scope. 1456 ParsingDef.Pop(); 1457 ClassScope.Exit(); 1458 1459 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc); 1460} 1461 1462/// ParseConstructorInitializer - Parse a C++ constructor initializer, 1463/// which explicitly initializes the members or base classes of a 1464/// class (C++ [class.base.init]). For example, the three initializers 1465/// after the ':' in the Derived constructor below: 1466/// 1467/// @code 1468/// class Base { }; 1469/// class Derived : Base { 1470/// int x; 1471/// float f; 1472/// public: 1473/// Derived(float f) : Base(), x(17), f(f) { } 1474/// }; 1475/// @endcode 1476/// 1477/// [C++] ctor-initializer: 1478/// ':' mem-initializer-list 1479/// 1480/// [C++] mem-initializer-list: 1481/// mem-initializer 1482/// mem-initializer , mem-initializer-list 1483void Parser::ParseConstructorInitializer(DeclPtrTy ConstructorDecl) { 1484 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'"); 1485 1486 SourceLocation ColonLoc = ConsumeToken(); 1487 1488 llvm::SmallVector<MemInitTy*, 4> MemInitializers; 1489 1490 do { 1491 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl); 1492 if (!MemInit.isInvalid()) 1493 MemInitializers.push_back(MemInit.get()); 1494 1495 if (Tok.is(tok::comma)) 1496 ConsumeToken(); 1497 else if (Tok.is(tok::l_brace)) 1498 break; 1499 else { 1500 // Skip over garbage, until we get to '{'. Don't eat the '{'. 1501 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); 1502 SkipUntil(tok::l_brace, true, true); 1503 break; 1504 } 1505 } while (true); 1506 1507 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, 1508 MemInitializers.data(), MemInitializers.size()); 1509} 1510 1511/// ParseMemInitializer - Parse a C++ member initializer, which is 1512/// part of a constructor initializer that explicitly initializes one 1513/// member or base class (C++ [class.base.init]). See 1514/// ParseConstructorInitializer for an example. 1515/// 1516/// [C++] mem-initializer: 1517/// mem-initializer-id '(' expression-list[opt] ')' 1518/// 1519/// [C++] mem-initializer-id: 1520/// '::'[opt] nested-name-specifier[opt] class-name 1521/// identifier 1522Parser::MemInitResult Parser::ParseMemInitializer(DeclPtrTy ConstructorDecl) { 1523 // parse '::'[opt] nested-name-specifier[opt] 1524 CXXScopeSpec SS; 1525 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); 1526 TypeTy *TemplateTypeTy = 0; 1527 if (Tok.is(tok::annot_template_id)) { 1528 TemplateIdAnnotation *TemplateId 1529 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 1530 if (TemplateId->Kind == TNK_Type_template) { 1531 AnnotateTemplateIdTokenAsType(&SS); 1532 assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); 1533 TemplateTypeTy = Tok.getAnnotationValue(); 1534 } 1535 // FIXME. May need to check for TNK_Dependent_template as well. 1536 } 1537 if (!TemplateTypeTy && Tok.isNot(tok::identifier)) { 1538 Diag(Tok, diag::err_expected_member_or_base_name); 1539 return true; 1540 } 1541 1542 // Get the identifier. This may be a member name or a class name, 1543 // but we'll let the semantic analysis determine which it is. 1544 IdentifierInfo *II = Tok.is(tok::identifier) ? Tok.getIdentifierInfo() : 0; 1545 SourceLocation IdLoc = ConsumeToken(); 1546 1547 // Parse the '('. 1548 if (Tok.isNot(tok::l_paren)) { 1549 Diag(Tok, diag::err_expected_lparen); 1550 return true; 1551 } 1552 SourceLocation LParenLoc = ConsumeParen(); 1553 1554 // Parse the optional expression-list. 1555 ExprVector ArgExprs(Actions); 1556 CommaLocsTy CommaLocs; 1557 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) { 1558 SkipUntil(tok::r_paren); 1559 return true; 1560 } 1561 1562 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 1563 1564 return Actions.ActOnMemInitializer(ConstructorDecl, CurScope, SS, II, 1565 TemplateTypeTy, IdLoc, 1566 LParenLoc, ArgExprs.take(), 1567 ArgExprs.size(), CommaLocs.data(), 1568 RParenLoc); 1569} 1570 1571/// ParseExceptionSpecification - Parse a C++ exception-specification 1572/// (C++ [except.spec]). 1573/// 1574/// exception-specification: 1575/// 'throw' '(' type-id-list [opt] ')' 1576/// [MS] 'throw' '(' '...' ')' 1577/// 1578/// type-id-list: 1579/// type-id 1580/// type-id-list ',' type-id 1581/// 1582bool Parser::ParseExceptionSpecification(SourceLocation &EndLoc, 1583 llvm::SmallVector<TypeTy*, 2> 1584 &Exceptions, 1585 llvm::SmallVector<SourceRange, 2> 1586 &Ranges, 1587 bool &hasAnyExceptionSpec) { 1588 assert(Tok.is(tok::kw_throw) && "expected throw"); 1589 1590 SourceLocation ThrowLoc = ConsumeToken(); 1591 1592 if (!Tok.is(tok::l_paren)) { 1593 return Diag(Tok, diag::err_expected_lparen_after) << "throw"; 1594 } 1595 SourceLocation LParenLoc = ConsumeParen(); 1596 1597 // Parse throw(...), a Microsoft extension that means "this function 1598 // can throw anything". 1599 if (Tok.is(tok::ellipsis)) { 1600 hasAnyExceptionSpec = true; 1601 SourceLocation EllipsisLoc = ConsumeToken(); 1602 if (!getLang().Microsoft) 1603 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec); 1604 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 1605 return false; 1606 } 1607 1608 // Parse the sequence of type-ids. 1609 SourceRange Range; 1610 while (Tok.isNot(tok::r_paren)) { 1611 TypeResult Res(ParseTypeName(&Range)); 1612 if (!Res.isInvalid()) { 1613 Exceptions.push_back(Res.get()); 1614 Ranges.push_back(Range); 1615 } 1616 if (Tok.is(tok::comma)) 1617 ConsumeToken(); 1618 else 1619 break; 1620 } 1621 1622 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); 1623 return false; 1624} 1625 1626/// \brief We have just started parsing the definition of a new class, 1627/// so push that class onto our stack of classes that is currently 1628/// being parsed. 1629void Parser::PushParsingClass(DeclPtrTy ClassDecl, bool TopLevelClass) { 1630 assert((TopLevelClass || !ClassStack.empty()) && 1631 "Nested class without outer class"); 1632 ClassStack.push(new ParsingClass(ClassDecl, TopLevelClass)); 1633} 1634 1635/// \brief Deallocate the given parsed class and all of its nested 1636/// classes. 1637void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) { 1638 for (unsigned I = 0, N = Class->NestedClasses.size(); I != N; ++I) 1639 DeallocateParsedClasses(Class->NestedClasses[I]); 1640 delete Class; 1641} 1642 1643/// \brief Pop the top class of the stack of classes that are 1644/// currently being parsed. 1645/// 1646/// This routine should be called when we have finished parsing the 1647/// definition of a class, but have not yet popped the Scope 1648/// associated with the class's definition. 1649/// 1650/// \returns true if the class we've popped is a top-level class, 1651/// false otherwise. 1652void Parser::PopParsingClass() { 1653 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing"); 1654 1655 ParsingClass *Victim = ClassStack.top(); 1656 ClassStack.pop(); 1657 if (Victim->TopLevelClass) { 1658 // Deallocate all of the nested classes of this class, 1659 // recursively: we don't need to keep any of this information. 1660 DeallocateParsedClasses(Victim); 1661 return; 1662 } 1663 assert(!ClassStack.empty() && "Missing top-level class?"); 1664 1665 if (Victim->MethodDecls.empty() && Victim->MethodDefs.empty() && 1666 Victim->NestedClasses.empty()) { 1667 // The victim is a nested class, but we will not need to perform 1668 // any processing after the definition of this class since it has 1669 // no members whose handling was delayed. Therefore, we can just 1670 // remove this nested class. 1671 delete Victim; 1672 return; 1673 } 1674 1675 // This nested class has some members that will need to be processed 1676 // after the top-level class is completely defined. Therefore, add 1677 // it to the list of nested classes within its parent. 1678 assert(CurScope->isClassScope() && "Nested class outside of class scope?"); 1679 ClassStack.top()->NestedClasses.push_back(Victim); 1680 Victim->TemplateScope = CurScope->getParent()->isTemplateParamScope(); 1681} 1682 1683/// ParseCXX0XAttributes - Parse a C++0x attribute-specifier. Currently only 1684/// parses standard attributes. 1685/// 1686/// [C++0x] attribute-specifier: 1687/// '[' '[' attribute-list ']' ']' 1688/// 1689/// [C++0x] attribute-list: 1690/// attribute[opt] 1691/// attribute-list ',' attribute[opt] 1692/// 1693/// [C++0x] attribute: 1694/// attribute-token attribute-argument-clause[opt] 1695/// 1696/// [C++0x] attribute-token: 1697/// identifier 1698/// attribute-scoped-token 1699/// 1700/// [C++0x] attribute-scoped-token: 1701/// attribute-namespace '::' identifier 1702/// 1703/// [C++0x] attribute-namespace: 1704/// identifier 1705/// 1706/// [C++0x] attribute-argument-clause: 1707/// '(' balanced-token-seq ')' 1708/// 1709/// [C++0x] balanced-token-seq: 1710/// balanced-token 1711/// balanced-token-seq balanced-token 1712/// 1713/// [C++0x] balanced-token: 1714/// '(' balanced-token-seq ')' 1715/// '[' balanced-token-seq ']' 1716/// '{' balanced-token-seq '}' 1717/// any token but '(', ')', '[', ']', '{', or '}' 1718CXX0XAttributeList Parser::ParseCXX0XAttributes(SourceLocation *EndLoc) { 1719 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) 1720 && "Not a C++0x attribute list"); 1721 1722 SourceLocation StartLoc = Tok.getLocation(), Loc; 1723 AttributeList *CurrAttr = 0; 1724 1725 ConsumeBracket(); 1726 ConsumeBracket(); 1727 1728 if (Tok.is(tok::comma)) { 1729 Diag(Tok.getLocation(), diag::err_expected_ident); 1730 ConsumeToken(); 1731 } 1732 1733 while (Tok.is(tok::identifier) || Tok.is(tok::comma)) { 1734 // attribute not present 1735 if (Tok.is(tok::comma)) { 1736 ConsumeToken(); 1737 continue; 1738 } 1739 1740 IdentifierInfo *ScopeName = 0, *AttrName = Tok.getIdentifierInfo(); 1741 SourceLocation ScopeLoc, AttrLoc = ConsumeToken(); 1742 1743 // scoped attribute 1744 if (Tok.is(tok::coloncolon)) { 1745 ConsumeToken(); 1746 1747 if (!Tok.is(tok::identifier)) { 1748 Diag(Tok.getLocation(), diag::err_expected_ident); 1749 SkipUntil(tok::r_square, tok::comma, true, true); 1750 continue; 1751 } 1752 1753 ScopeName = AttrName; 1754 ScopeLoc = AttrLoc; 1755 1756 AttrName = Tok.getIdentifierInfo(); 1757 AttrLoc = ConsumeToken(); 1758 } 1759 1760 bool AttrParsed = false; 1761 // No scoped names are supported; ideally we could put all non-standard 1762 // attributes into namespaces. 1763 if (!ScopeName) { 1764 switch(AttributeList::getKind(AttrName)) 1765 { 1766 // No arguments 1767 case AttributeList::AT_base_check: 1768 case AttributeList::AT_carries_dependency: 1769 case AttributeList::AT_final: 1770 case AttributeList::AT_hiding: 1771 case AttributeList::AT_noreturn: 1772 case AttributeList::AT_override: { 1773 if (Tok.is(tok::l_paren)) { 1774 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_forbids_arguments) 1775 << AttrName->getName(); 1776 break; 1777 } 1778 1779 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 0, 1780 SourceLocation(), 0, 0, CurrAttr, false, 1781 true); 1782 AttrParsed = true; 1783 break; 1784 } 1785 1786 // One argument; must be a type-id or assignment-expression 1787 case AttributeList::AT_aligned: { 1788 if (Tok.isNot(tok::l_paren)) { 1789 Diag(Tok.getLocation(), diag::err_cxx0x_attribute_requires_arguments) 1790 << AttrName->getName(); 1791 break; 1792 } 1793 SourceLocation ParamLoc = ConsumeParen(); 1794 1795 OwningExprResult ArgExpr = ParseCXX0XAlignArgument(ParamLoc); 1796 1797 MatchRHSPunctuation(tok::r_paren, ParamLoc); 1798 1799 ExprVector ArgExprs(Actions); 1800 ArgExprs.push_back(ArgExpr.release()); 1801 CurrAttr = new AttributeList(AttrName, AttrLoc, 0, AttrLoc, 1802 0, ParamLoc, ArgExprs.take(), 1, CurrAttr, 1803 false, true); 1804 1805 AttrParsed = true; 1806 break; 1807 } 1808 1809 // Silence warnings 1810 default: break; 1811 } 1812 } 1813 1814 // Skip the entire parameter clause, if any 1815 if (!AttrParsed && Tok.is(tok::l_paren)) { 1816 ConsumeParen(); 1817 // SkipUntil maintains the balancedness of tokens. 1818 SkipUntil(tok::r_paren, false); 1819 } 1820 } 1821 1822 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) 1823 SkipUntil(tok::r_square, false); 1824 Loc = Tok.getLocation(); 1825 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare)) 1826 SkipUntil(tok::r_square, false); 1827 1828 CXX0XAttributeList Attr (CurrAttr, SourceRange(StartLoc, Loc), true); 1829 return Attr; 1830} 1831 1832/// ParseCXX0XAlignArgument - Parse the argument to C++0x's [[align]] 1833/// attribute. 1834/// 1835/// FIXME: Simply returns an alignof() expression if the argument is a 1836/// type. Ideally, the type should be propagated directly into Sema. 1837/// 1838/// [C++0x] 'align' '(' type-id ')' 1839/// [C++0x] 'align' '(' assignment-expression ')' 1840Parser::OwningExprResult Parser::ParseCXX0XAlignArgument(SourceLocation Start) { 1841 if (isTypeIdInParens()) { 1842 EnterExpressionEvaluationContext Unevaluated(Actions, 1843 Action::Unevaluated); 1844 SourceLocation TypeLoc = Tok.getLocation(); 1845 TypeTy *Ty = ParseTypeName().get(); 1846 SourceRange TypeRange(Start, Tok.getLocation()); 1847 return Actions.ActOnSizeOfAlignOfExpr(TypeLoc, false, true, Ty, 1848 TypeRange); 1849 } else 1850 return ParseConstantExpression(); 1851} 1852