ParseDeclCXX.cpp revision 201361
1193323Sed//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file implements the C++ Declaration portions of the Parser interfaces. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#include "clang/Basic/OperatorKinds.h" 15193323Sed#include "clang/Parse/Parser.h" 16205218Srdivacky#include "clang/Parse/ParseDiagnostic.h" 17193323Sed#include "clang/Parse/DeclSpec.h" 18198090Srdivacky#include "clang/Parse/Scope.h" 19193323Sed#include "clang/Parse/Template.h" 20193323Sed#include "RAIIObjectsForParser.h" 21193323Sedusing namespace clang; 22193323Sed 23198090Srdivacky/// ParseNamespace - We know that the current token is a namespace keyword. This 24202878Srdivacky/// may either be a top level namespace or a block-level namespace alias. 25202878Srdivacky/// 26206083Srdivacky/// namespace-definition: [C++ 7.3: basic.namespace] 27202878Srdivacky/// named-namespace-definition 28202878Srdivacky/// unnamed-namespace-definition 29193323Sed/// 30193323Sed/// unnamed-namespace-definition: 31193323Sed/// 'namespace' attributes[opt] '{' namespace-body '}' 32193323Sed/// 33202878Srdivacky/// named-namespace-definition: 34202878Srdivacky/// original-namespace-definition 35193323Sed/// extension-namespace-definition 36193323Sed/// 37193323Sed/// original-namespace-definition: 38193323Sed/// 'namespace' identifier attributes[opt] '{' namespace-body '}' 39193323Sed/// 40193323Sed/// extension-namespace-definition: 41193323Sed/// 'namespace' original-namespace-name '{' namespace-body '}' 42193323Sed/// 43193323Sed/// namespace-alias-definition: [C++ 7.3.2: namespace.alias] 44193323Sed/// 'namespace' identifier '=' qualified-namespace-specifier ';' 45193323Sed/// 46193323SedParser::DeclPtrTy Parser::ParseNamespace(unsigned Context, 47193323Sed SourceLocation &DeclEnd) { 48193323Sed assert(Tok.is(tok::kw_namespace) && "Not a namespace!"); 49193323Sed SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'. 50193323Sed 51193323Sed if (Tok.is(tok::code_completion)) { 52193323Sed Actions.CodeCompleteNamespaceDecl(CurScope); 53193323Sed ConsumeToken(); 54193323Sed } 55193323Sed 56193323Sed SourceLocation IdentLoc; 57193323Sed IdentifierInfo *Ident = 0; 58193323Sed 59193323Sed Token attrTok; 60193323Sed 61193323Sed if (Tok.is(tok::identifier)) { 62193323Sed Ident = Tok.getIdentifierInfo(); 63193323Sed IdentLoc = ConsumeToken(); // eat the identifier. 64193323Sed } 65193323Sed 66193323Sed // Read label attributes, if present. 67193323Sed Action::AttrTy *AttrList = 0; 68193323Sed if (Tok.is(tok::kw___attribute)) { 69193323Sed attrTok = Tok; 70193323Sed 71193323Sed // FIXME: save these somewhere. 72193323Sed AttrList = ParseGNUAttributes(); 73193323Sed } 74193323Sed 75193323Sed if (Tok.is(tok::equal)) { 76193323Sed if (AttrList) 77193323Sed Diag(attrTok, diag::err_unexpected_namespace_attributes_alias); 78193323Sed 79193323Sed return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd); 80193323Sed } 81193323Sed 82193323Sed if (Tok.isNot(tok::l_brace)) { 83193323Sed Diag(Tok, Ident ? diag::err_expected_lbrace : 84193323Sed diag::err_expected_ident_lbrace); 85193323Sed return DeclPtrTy(); 86202878Srdivacky } 87202878Srdivacky 88202878Srdivacky SourceLocation LBrace = ConsumeBrace(); 89202878Srdivacky 90202878Srdivacky // Enter a scope for the namespace. 91202878Srdivacky ParseScope NamespaceScope(this, Scope::DeclScope); 92202878Srdivacky 93202878Srdivacky DeclPtrTy NamespcDecl = 94202878Srdivacky Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace); 95202878Srdivacky 96202878Srdivacky PrettyStackTraceActionsDecl CrashInfo(NamespcDecl, NamespaceLoc, Actions, 97202878Srdivacky PP.getSourceManager(), 98202878Srdivacky "parsing namespace"); 99202878Srdivacky 100202878Srdivacky while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 101202878Srdivacky CXX0XAttributeList Attr; 102202878Srdivacky if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 103202878Srdivacky Attr = ParseCXX0XAttributes(); 104202878Srdivacky ParseExternalDeclaration(Attr); 105202878Srdivacky } 106202878Srdivacky 107202878Srdivacky // Leave the namespace scope. 108202878Srdivacky NamespaceScope.Exit(); 109202878Srdivacky 110202878Srdivacky SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBrace); 111202878Srdivacky Actions.ActOnFinishNamespaceDef(NamespcDecl, RBraceLoc); 112202878Srdivacky 113202878Srdivacky DeclEnd = RBraceLoc; 114202878Srdivacky return NamespcDecl; 115202878Srdivacky} 116202878Srdivacky 117202878Srdivacky/// ParseNamespaceAlias - Parse the part after the '=' in a namespace 118202878Srdivacky/// alias definition. 119202878Srdivacky/// 120202878SrdivackyParser::DeclPtrTy Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc, 121202878Srdivacky SourceLocation AliasLoc, 122202878Srdivacky IdentifierInfo *Alias, 123202878Srdivacky SourceLocation &DeclEnd) { 124202878Srdivacky assert(Tok.is(tok::equal) && "Not equal token"); 125202878Srdivacky 126202878Srdivacky ConsumeToken(); // eat the '='. 127202878Srdivacky 128202878Srdivacky if (Tok.is(tok::code_completion)) { 129202878Srdivacky Actions.CodeCompleteNamespaceAliasDecl(CurScope); 130202878Srdivacky ConsumeToken(); 131202878Srdivacky } 132202878Srdivacky 133202878Srdivacky CXXScopeSpec SS; 134202878Srdivacky // Parse (optional) nested-name-specifier. 135202878Srdivacky ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); 136202878Srdivacky 137202878Srdivacky if (SS.isInvalid() || Tok.isNot(tok::identifier)) { 138202878Srdivacky Diag(Tok, diag::err_expected_namespace_name); 139202878Srdivacky // Skip to end of the definition and eat the ';'. 140202878Srdivacky SkipUntil(tok::semi); 141202878Srdivacky return DeclPtrTy(); 142202878Srdivacky } 143202878Srdivacky 144202878Srdivacky // Parse identifier. 145202878Srdivacky IdentifierInfo *Ident = Tok.getIdentifierInfo(); 146202878Srdivacky SourceLocation IdentLoc = ConsumeToken(); 147202878Srdivacky 148202878Srdivacky // Eat the ';'. 149202878Srdivacky DeclEnd = Tok.getLocation(); 150202878Srdivacky ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name, 151202878Srdivacky "", tok::semi); 152202878Srdivacky 153202878Srdivacky return Actions.ActOnNamespaceAliasDef(CurScope, NamespaceLoc, AliasLoc, Alias, 154202878Srdivacky SS, IdentLoc, Ident); 155202878Srdivacky} 156202878Srdivacky 157202878Srdivacky/// ParseLinkage - We know that the current token is a string_literal 158202878Srdivacky/// and just before that, that extern was seen. 159202878Srdivacky/// 160202878Srdivacky/// linkage-specification: [C++ 7.5p2: dcl.link] 161202878Srdivacky/// 'extern' string-literal '{' declaration-seq[opt] '}' 162202878Srdivacky/// 'extern' string-literal declaration 163202878Srdivacky/// 164202878SrdivackyParser::DeclPtrTy Parser::ParseLinkage(ParsingDeclSpec &DS, 165202878Srdivacky unsigned Context) { 166202878Srdivacky assert(Tok.is(tok::string_literal) && "Not a string literal!"); 167202878Srdivacky llvm::SmallVector<char, 8> LangBuffer; 168202878Srdivacky // LangBuffer is guaranteed to be big enough. 169202878Srdivacky LangBuffer.resize(Tok.getLength()); 170202878Srdivacky const char *LangBufPtr = &LangBuffer[0]; 171202878Srdivacky unsigned StrSize = PP.getSpelling(Tok, LangBufPtr); 172202878Srdivacky 173202878Srdivacky SourceLocation Loc = ConsumeStringToken(); 174202878Srdivacky 175202878Srdivacky ParseScope LinkageScope(this, Scope::DeclScope); 176202878Srdivacky DeclPtrTy LinkageSpec 177202878Srdivacky = Actions.ActOnStartLinkageSpecification(CurScope, 178202878Srdivacky /*FIXME: */SourceLocation(), 179202878Srdivacky Loc, LangBufPtr, StrSize, 180202878Srdivacky Tok.is(tok::l_brace)? Tok.getLocation() 181202878Srdivacky : SourceLocation()); 182202878Srdivacky 183202878Srdivacky CXX0XAttributeList Attr; 184202878Srdivacky if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { 185202878Srdivacky Attr = ParseCXX0XAttributes(); 186202878Srdivacky } 187202878Srdivacky 188202878Srdivacky if (Tok.isNot(tok::l_brace)) { 189202878Srdivacky ParseDeclarationOrFunctionDefinition(DS, Attr.AttrList); 190202878Srdivacky return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, 191202878Srdivacky SourceLocation()); 192202878Srdivacky } 193202878Srdivacky 194202878Srdivacky if (Attr.HasAttr) 195202878Srdivacky Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) 196202878Srdivacky << Attr.Range; 197202878Srdivacky 198202878Srdivacky SourceLocation LBrace = ConsumeBrace(); 199202878Srdivacky while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { 200202878Srdivacky CXX0XAttributeList Attr; 201202878Srdivacky if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) 202193323Sed Attr = ParseCXX0XAttributes(); 203193323Sed ParseExternalDeclaration(Attr); 204193323Sed } 205193323Sed 206193323Sed SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace); 207193323Sed return Actions.ActOnFinishLinkageSpecification(CurScope, LinkageSpec, RBrace); 208193323Sed} 209193323Sed 210193323Sed/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or 211193323Sed/// using-directive. Assumes that current token is 'using'. 212193323SedParser::DeclPtrTy Parser::ParseUsingDirectiveOrDeclaration(unsigned Context, 213193323Sed SourceLocation &DeclEnd, 214193323Sed CXX0XAttributeList Attr) { 215193323Sed assert(Tok.is(tok::kw_using) && "Not using token"); 216193323Sed 217193323Sed // Eat 'using'. 218193323Sed SourceLocation UsingLoc = ConsumeToken(); 219193323Sed 220193323Sed if (Tok.is(tok::code_completion)) { 221193323Sed Actions.CodeCompleteUsing(CurScope); 222193323Sed ConsumeToken(); 223204642Srdivacky } 224204642Srdivacky 225204642Srdivacky if (Tok.is(tok::kw_namespace)) 226204642Srdivacky // Next token after 'using' is 'namespace' so it must be using-directive 227204642Srdivacky return ParseUsingDirective(Context, UsingLoc, DeclEnd, Attr.AttrList); 228204642Srdivacky 229204642Srdivacky if (Attr.HasAttr) 230204642Srdivacky Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed) 231204642Srdivacky << Attr.Range; 232204642Srdivacky 233204642Srdivacky // Otherwise, it must be using-declaration. 234204642Srdivacky // Ignore illegal attributes (the caller should already have issued an error. 235204642Srdivacky return ParseUsingDeclaration(Context, UsingLoc, DeclEnd); 236204642Srdivacky} 237193323Sed 238193323Sed/// ParseUsingDirective - Parse C++ using-directive, assumes 239193323Sed/// that current token is 'namespace' and 'using' was already parsed. 240193323Sed/// 241193323Sed/// using-directive: [C++ 7.3.p4: namespace.udir] 242193323Sed/// 'using' 'namespace' ::[opt] nested-name-specifier[opt] 243193323Sed/// namespace-name ; 244193323Sed/// [GNU] using-directive: 245193323Sed/// 'using' 'namespace' ::[opt] nested-name-specifier[opt] 246193323Sed/// namespace-name attributes[opt] ; 247193323Sed/// 248193323SedParser::DeclPtrTy Parser::ParseUsingDirective(unsigned Context, 249193323Sed SourceLocation UsingLoc, 250193323Sed SourceLocation &DeclEnd, 251193323Sed AttributeList *Attr) { 252193323Sed assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token"); 253193323Sed 254193323Sed // Eat 'namespace'. 255193323Sed SourceLocation NamespcLoc = ConsumeToken(); 256193323Sed 257193323Sed if (Tok.is(tok::code_completion)) { 258193323Sed Actions.CodeCompleteUsingDirective(CurScope); 259193323Sed ConsumeToken(); 260193323Sed } 261193323Sed 262193323Sed CXXScopeSpec SS; 263193323Sed // Parse (optional) nested-name-specifier. 264193323Sed ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); 265193323Sed 266193323Sed IdentifierInfo *NamespcName = 0; 267193323Sed SourceLocation IdentLoc = SourceLocation(); 268193323Sed 269193323Sed // Parse namespace-name. 270193323Sed if (SS.isInvalid() || Tok.isNot(tok::identifier)) { 271193323Sed Diag(Tok, diag::err_expected_namespace_name); 272193323Sed // If there was invalid namespace name, skip to end of decl, and eat ';'. 273193323Sed SkipUntil(tok::semi); 274193323Sed // FIXME: Are there cases, when we would like to call ActOnUsingDirective? 275193323Sed return DeclPtrTy(); 276193323Sed } 277193323Sed 278193323Sed // Parse identifier. 279193323Sed NamespcName = Tok.getIdentifierInfo(); 280193323Sed IdentLoc = ConsumeToken(); 281193323Sed 282193323Sed // Parse (optional) attributes (most likely GNU strong-using extension). 283193323Sed bool GNUAttr = false; 284193323Sed if (Tok.is(tok::kw___attribute)) { 285193323Sed GNUAttr = true; 286193323Sed Attr = addAttributeLists(Attr, ParseGNUAttributes()); 287193323Sed } 288193323Sed 289193323Sed // Eat ';'. 290193323Sed DeclEnd = Tok.getLocation(); 291193323Sed ExpectAndConsume(tok::semi, 292193323Sed GNUAttr ? diag::err_expected_semi_after_attribute_list : 293198090Srdivacky diag::err_expected_semi_after_namespace_name, "", tok::semi); 294198090Srdivacky 295198090Srdivacky return Actions.ActOnUsingDirective(CurScope, UsingLoc, NamespcLoc, SS, 296198090Srdivacky IdentLoc, NamespcName, Attr); 297198090Srdivacky} 298193323Sed 299193323Sed/// ParseUsingDeclaration - Parse C++ using-declaration. Assumes that 300193323Sed/// 'using' was already seen. 301193323Sed/// 302193323Sed/// using-declaration: [C++ 7.3.p3: namespace.udecl] 303193323Sed/// 'using' 'typename'[opt] ::[opt] nested-name-specifier 304193323Sed/// unqualified-id 305193323Sed/// 'using' :: unqualified-id 306193323Sed/// 307193323SedParser::DeclPtrTy Parser::ParseUsingDeclaration(unsigned Context, 308193323Sed SourceLocation UsingLoc, 309193323Sed SourceLocation &DeclEnd, 310193323Sed AccessSpecifier AS) { 311193323Sed CXXScopeSpec SS; 312193323Sed SourceLocation TypenameLoc; 313193323Sed bool IsTypeName; 314193323Sed 315193323Sed // Ignore optional 'typename'. 316193323Sed // FIXME: This is wrong; we should parse this as a typename-specifier. 317193323Sed if (Tok.is(tok::kw_typename)) { 318193323Sed TypenameLoc = Tok.getLocation(); 319193323Sed ConsumeToken(); 320193323Sed IsTypeName = true; 321198090Srdivacky } 322193323Sed else 323193323Sed IsTypeName = false; 324193323Sed 325193323Sed // Parse nested-name-specifier. 326193323Sed ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); 327193323Sed 328193323Sed AttributeList *AttrList = 0; 329193323Sed 330193323Sed // Check nested-name specifier. 331193323Sed if (SS.isInvalid()) { 332193323Sed SkipUntil(tok::semi); 333193323Sed return DeclPtrTy(); 334193323Sed } 335198090Srdivacky 336193323Sed // Parse the unqualified-id. We allow parsing of both constructor and 337193323Sed // destructor names and allow the action module to diagnose any semantic 338193323Sed // errors. 339193323Sed UnqualifiedId Name; 340193323Sed if (ParseUnqualifiedId(SS, 341193323Sed /*EnteringContext=*/false, 342193323Sed /*AllowDestructorName=*/true, 343193323Sed /*AllowConstructorName=*/true, 344193323Sed /*ObjectType=*/0, 345193323Sed Name)) { 346193323Sed SkipUntil(tok::semi); 347193323Sed return DeclPtrTy(); 348193323Sed } 349193323Sed 350193323Sed // Parse (optional) attributes (most likely GNU strong-using extension). 351193323Sed if (Tok.is(tok::kw___attribute)) 352198090Srdivacky AttrList = ParseGNUAttributes(); 353198090Srdivacky 354198090Srdivacky // Eat ';'. 355198090Srdivacky DeclEnd = Tok.getLocation(); 356198090Srdivacky ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 357198090Srdivacky AttrList ? "attributes list" : "using declaration", 358198090Srdivacky tok::semi); 359198090Srdivacky 360198090Srdivacky return Actions.ActOnUsingDeclaration(CurScope, AS, true, UsingLoc, SS, Name, 361193323Sed AttrList, IsTypeName, TypenameLoc); 362193323Sed} 363193323Sed 364193323Sed/// ParseStaticAssertDeclaration - Parse C++0x static_assert-declaratoion. 365193323Sed/// 366193323Sed/// static_assert-declaration: 367193323Sed/// static_assert ( constant-expression , string-literal ) ; 368193323Sed/// 369193323SedParser::DeclPtrTy Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){ 370198090Srdivacky assert(Tok.is(tok::kw_static_assert) && "Not a static_assert declaration"); 371202878Srdivacky SourceLocation StaticAssertLoc = ConsumeToken(); 372202878Srdivacky 373193323Sed if (Tok.isNot(tok::l_paren)) { 374193323Sed Diag(Tok, diag::err_expected_lparen); 375193323Sed return DeclPtrTy(); 376193323Sed } 377193323Sed 378193323Sed SourceLocation LParenLoc = ConsumeParen(); 379193323Sed 380193323Sed OwningExprResult AssertExpr(ParseConstantExpression()); 381193323Sed if (AssertExpr.isInvalid()) { 382193323Sed SkipUntil(tok::semi); 383193323Sed return DeclPtrTy(); 384193323Sed } 385193323Sed 386193323Sed if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi)) 387198090Srdivacky return DeclPtrTy(); 388198090Srdivacky 389193323Sed if (Tok.isNot(tok::string_literal)) { 390193323Sed Diag(Tok, diag::err_expected_string_literal); 391193323Sed SkipUntil(tok::semi); 392193323Sed return DeclPtrTy(); 393193323Sed } 394202375Srdivacky 395193323Sed OwningExprResult AssertMessage(ParseStringLiteralExpression()); 396193323Sed if (AssertMessage.isInvalid()) 397193323Sed return DeclPtrTy(); 398193323Sed 399202375Srdivacky MatchRHSPunctuation(tok::r_paren, LParenLoc); 400193323Sed 401193323Sed DeclEnd = Tok.getLocation(); 402193323Sed ExpectAndConsume(tok::semi, diag::err_expected_semi_after_static_assert); 403193323Sed 404202375Srdivacky return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, move(AssertExpr), 405193323Sed move(AssertMessage)); 406202375Srdivacky} 407193323Sed 408193323Sed/// ParseDecltypeSpecifier - Parse a C++0x decltype specifier. 409193323Sed/// 410198090Srdivacky/// 'decltype' ( expression ) 411206083Srdivacky/// 412206083Srdivackyvoid Parser::ParseDecltypeSpecifier(DeclSpec &DS) { 413206083Srdivacky assert(Tok.is(tok::kw_decltype) && "Not a decltype specifier"); 414206083Srdivacky 415206083Srdivacky SourceLocation StartLoc = ConsumeToken(); 416206083Srdivacky SourceLocation LParenLoc = Tok.getLocation(); 417206083Srdivacky 418206083Srdivacky if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, 419206083Srdivacky "decltype")) { 420206083Srdivacky SkipUntil(tok::r_paren); 421206083Srdivacky return; 422206083Srdivacky } 423206083Srdivacky 424206083Srdivacky // Parse the expression 425206083Srdivacky 426206083Srdivacky // C++0x [dcl.type.simple]p4: 427206083Srdivacky // The operand of the decltype specifier is an unevaluated operand. 428206083Srdivacky EnterExpressionEvaluationContext Unevaluated(Actions, 429206083Srdivacky Action::Unevaluated); 430206083Srdivacky OwningExprResult Result = ParseExpression(); 431206083Srdivacky if (Result.isInvalid()) { 432206083Srdivacky SkipUntil(tok::r_paren); 433206083Srdivacky return; 434206083Srdivacky } 435206083Srdivacky 436206083Srdivacky // Match the ')' 437206083Srdivacky SourceLocation RParenLoc; 438206083Srdivacky if (Tok.is(tok::r_paren)) 439206083Srdivacky RParenLoc = ConsumeParen(); 440206083Srdivacky else 441206083Srdivacky MatchRHSPunctuation(tok::r_paren, LParenLoc); 442206083Srdivacky 443206083Srdivacky if (RParenLoc.isInvalid()) 444206083Srdivacky return; 445206083Srdivacky 446206083Srdivacky const char *PrevSpec = 0; 447206083Srdivacky unsigned DiagID; 448206083Srdivacky // Check for duplicate type specifiers (e.g. "int decltype(a)"). 449206083Srdivacky if (DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec, 450206083Srdivacky DiagID, Result.release())) 451206083Srdivacky Diag(StartLoc, DiagID) << PrevSpec; 452206083Srdivacky} 453206083Srdivacky 454206083Srdivacky/// ParseClassName - Parse a C++ class-name, which names a class. Note 455206083Srdivacky/// that we only check that the result names a type; semantic analysis 456206083Srdivacky/// will need to verify that the type names a class. The result is 457206083Srdivacky/// either a type or NULL, depending on whether a type name was 458206083Srdivacky/// found. 459206083Srdivacky/// 460206083Srdivacky/// class-name: [C++ 9.1] 461198090Srdivacky/// identifier 462198090Srdivacky/// simple-template-id 463198090Srdivacky/// 464198090SrdivackyParser::TypeResult Parser::ParseClassName(SourceLocation &EndLocation, 465198090Srdivacky const CXXScopeSpec *SS, 466198090Srdivacky bool DestrExpected) { 467206083Srdivacky // Check whether we have a template-id that names a type. 468206083Srdivacky if (Tok.is(tok::annot_template_id)) { 469206083Srdivacky TemplateIdAnnotation *TemplateId 470205218Srdivacky = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); 471198090Srdivacky if (TemplateId->Kind == TNK_Type_template) { 472198090Srdivacky AnnotateTemplateIdTokenAsType(SS); 473198090Srdivacky 474198090Srdivacky assert(Tok.is(tok::annot_typename) && "template-id -> type failed"); 475198090Srdivacky TypeTy *Type = Tok.getAnnotationValue(); 476198090Srdivacky EndLocation = Tok.getAnnotationEndLoc(); 477198090Srdivacky ConsumeToken(); 478198090Srdivacky 479198090Srdivacky if (Type) 480198090Srdivacky return Type; 481198090Srdivacky return true; 482198090Srdivacky } 483198090Srdivacky 484198090Srdivacky // Fall through to produce an error below. 485198090Srdivacky } 486198090Srdivacky 487198090Srdivacky if (Tok.isNot(tok::identifier)) { 488198090Srdivacky Diag(Tok, diag::err_expected_class_name); 489198090Srdivacky return true; 490198090Srdivacky } 491198090Srdivacky 492206083Srdivacky // We have an identifier; check whether it is actually a type. 493198090Srdivacky TypeTy *Type = Actions.getTypeName(*Tok.getIdentifierInfo(), 494198090Srdivacky Tok.getLocation(), CurScope, SS, 495206083Srdivacky true); 496206083Srdivacky if (!Type) { 497206083Srdivacky Diag(Tok, DestrExpected ? diag::err_destructor_class_name 498198090Srdivacky : diag::err_expected_class_name); 499198090Srdivacky return true; 500198090Srdivacky } 501198090Srdivacky 502206083Srdivacky // Consume the identifier. 503206083Srdivacky EndLocation = ConsumeToken(); 504206083Srdivacky return Type; 505206083Srdivacky} 506206083Srdivacky 507206083Srdivacky/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or 508206083Srdivacky/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which 509206083Srdivacky/// until we reach the start of a definition or see a token that 510206083Srdivacky/// cannot start a definition. 511206083Srdivacky/// 512206083Srdivacky/// class-specifier: [C++ class] 513206083Srdivacky/// class-head '{' member-specification[opt] '}' 514206083Srdivacky/// class-head '{' member-specification[opt] '}' attributes[opt] 515206083Srdivacky/// class-head: 516206083Srdivacky/// class-key identifier[opt] base-clause[opt] 517206083Srdivacky/// class-key nested-name-specifier identifier base-clause[opt] 518206083Srdivacky/// class-key nested-name-specifier[opt] simple-template-id 519206083Srdivacky/// base-clause[opt] 520206083Srdivacky/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt] 521206083Srdivacky/// [GNU] class-key attributes[opt] nested-name-specifier 522206083Srdivacky/// identifier base-clause[opt] 523206083Srdivacky/// [GNU] class-key attributes[opt] nested-name-specifier[opt] 524206083Srdivacky/// simple-template-id base-clause[opt] 525206083Srdivacky/// class-key: 526206083Srdivacky/// 'class' 527206083Srdivacky/// 'struct' 528206083Srdivacky/// 'union' 529206083Srdivacky/// 530206083Srdivacky/// elaborated-type-specifier: [C++ dcl.type.elab] 531206083Srdivacky/// class-key ::[opt] nested-name-specifier[opt] identifier 532206083Srdivacky/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt] 533206083Srdivacky/// simple-template-id 534206083Srdivacky/// 535206083Srdivacky/// Note that the C++ class-specifier and elaborated-type-specifier, 536206083Srdivacky/// together, subsume the C99 struct-or-union-specifier: 537206083Srdivacky/// 538206083Srdivacky/// struct-or-union-specifier: [C99 6.7.2.1] 539206083Srdivacky/// struct-or-union identifier[opt] '{' struct-contents '}' 540206083Srdivacky/// struct-or-union identifier 541206083Srdivacky/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents 542206083Srdivacky/// '}' attributes[opt] 543206083Srdivacky/// [GNU] struct-or-union attributes[opt] identifier 544206083Srdivacky/// struct-or-union: 545206083Srdivacky/// 'struct' 546206083Srdivacky/// 'union' 547206083Srdivackyvoid Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, 548206083Srdivacky SourceLocation StartLoc, DeclSpec &DS, 549206083Srdivacky const ParsedTemplateInfo &TemplateInfo, 550206083Srdivacky AccessSpecifier AS) { 551205218Srdivacky DeclSpec::TST TagType; 552206083Srdivacky if (TagTokKind == tok::kw_struct) 553206083Srdivacky TagType = DeclSpec::TST_struct; 554206083Srdivacky else if (TagTokKind == tok::kw_class) 555206083Srdivacky TagType = DeclSpec::TST_class; 556206083Srdivacky else { 557206083Srdivacky assert(TagTokKind == tok::kw_union && "Not a class specifier"); 558206083Srdivacky TagType = DeclSpec::TST_union; 559206083Srdivacky } 560206083Srdivacky 561206083Srdivacky if (Tok.is(tok::code_completion)) { 562206083Srdivacky // Code completion for a struct, class, or union name. 563206083Srdivacky Actions.CodeCompleteTag(CurScope, TagType); 564206083Srdivacky ConsumeToken(); 565206083Srdivacky } 566198090Srdivacky 567198090Srdivacky AttributeList *AttrList = 0; 568198090Srdivacky // If attributes exist after tag, parse them. 569198090Srdivacky if (Tok.is(tok::kw___attribute)) 570198090Srdivacky AttrList = ParseGNUAttributes(); 571198090Srdivacky 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