1*f4a2713aSLionel Sambuc //===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This file implements parsing for C++ class inline methods. 11*f4a2713aSLionel Sambuc // 12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13*f4a2713aSLionel Sambuc 14*f4a2713aSLionel Sambuc #include "clang/Parse/Parser.h" 15*f4a2713aSLionel Sambuc #include "RAIIObjectsForParser.h" 16*f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h" 17*f4a2713aSLionel Sambuc #include "clang/Parse/ParseDiagnostic.h" 18*f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h" 19*f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h" 20*f4a2713aSLionel Sambuc using namespace clang; 21*f4a2713aSLionel Sambuc 22*f4a2713aSLionel Sambuc /// Get the FunctionDecl for a function or function template decl. 23*f4a2713aSLionel Sambuc static FunctionDecl *getFunctionDecl(Decl *D) { 24*f4a2713aSLionel Sambuc if (FunctionDecl *fn = dyn_cast<FunctionDecl>(D)) 25*f4a2713aSLionel Sambuc return fn; 26*f4a2713aSLionel Sambuc return cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); 27*f4a2713aSLionel Sambuc } 28*f4a2713aSLionel Sambuc 29*f4a2713aSLionel Sambuc /// ParseCXXInlineMethodDef - We parsed and verified that the specified 30*f4a2713aSLionel Sambuc /// Declarator is a well formed C++ inline method definition. Now lex its body 31*f4a2713aSLionel Sambuc /// and store its tokens for parsing after the C++ class is complete. 32*f4a2713aSLionel Sambuc NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, 33*f4a2713aSLionel Sambuc AttributeList *AccessAttrs, 34*f4a2713aSLionel Sambuc ParsingDeclarator &D, 35*f4a2713aSLionel Sambuc const ParsedTemplateInfo &TemplateInfo, 36*f4a2713aSLionel Sambuc const VirtSpecifiers& VS, 37*f4a2713aSLionel Sambuc FunctionDefinitionKind DefinitionKind, 38*f4a2713aSLionel Sambuc ExprResult& Init) { 39*f4a2713aSLionel Sambuc assert(D.isFunctionDeclarator() && "This isn't a function declarator!"); 40*f4a2713aSLionel Sambuc assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) || 41*f4a2713aSLionel Sambuc Tok.is(tok::equal)) && 42*f4a2713aSLionel Sambuc "Current token not a '{', ':', '=', or 'try'!"); 43*f4a2713aSLionel Sambuc 44*f4a2713aSLionel Sambuc MultiTemplateParamsArg TemplateParams( 45*f4a2713aSLionel Sambuc TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0, 46*f4a2713aSLionel Sambuc TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); 47*f4a2713aSLionel Sambuc 48*f4a2713aSLionel Sambuc NamedDecl *FnD; 49*f4a2713aSLionel Sambuc D.setFunctionDefinitionKind(DefinitionKind); 50*f4a2713aSLionel Sambuc if (D.getDeclSpec().isFriendSpecified()) 51*f4a2713aSLionel Sambuc FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, 52*f4a2713aSLionel Sambuc TemplateParams); 53*f4a2713aSLionel Sambuc else { 54*f4a2713aSLionel Sambuc FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D, 55*f4a2713aSLionel Sambuc TemplateParams, 0, 56*f4a2713aSLionel Sambuc VS, ICIS_NoInit); 57*f4a2713aSLionel Sambuc if (FnD) { 58*f4a2713aSLionel Sambuc Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs); 59*f4a2713aSLionel Sambuc bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType(); 60*f4a2713aSLionel Sambuc if (Init.isUsable()) 61*f4a2713aSLionel Sambuc Actions.AddInitializerToDecl(FnD, Init.get(), false, 62*f4a2713aSLionel Sambuc TypeSpecContainsAuto); 63*f4a2713aSLionel Sambuc else 64*f4a2713aSLionel Sambuc Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto); 65*f4a2713aSLionel Sambuc } 66*f4a2713aSLionel Sambuc } 67*f4a2713aSLionel Sambuc 68*f4a2713aSLionel Sambuc HandleMemberFunctionDeclDelays(D, FnD); 69*f4a2713aSLionel Sambuc 70*f4a2713aSLionel Sambuc D.complete(FnD); 71*f4a2713aSLionel Sambuc 72*f4a2713aSLionel Sambuc if (Tok.is(tok::equal)) { 73*f4a2713aSLionel Sambuc ConsumeToken(); 74*f4a2713aSLionel Sambuc 75*f4a2713aSLionel Sambuc if (!FnD) { 76*f4a2713aSLionel Sambuc SkipUntil(tok::semi); 77*f4a2713aSLionel Sambuc return 0; 78*f4a2713aSLionel Sambuc } 79*f4a2713aSLionel Sambuc 80*f4a2713aSLionel Sambuc bool Delete = false; 81*f4a2713aSLionel Sambuc SourceLocation KWLoc; 82*f4a2713aSLionel Sambuc if (Tok.is(tok::kw_delete)) { 83*f4a2713aSLionel Sambuc Diag(Tok, getLangOpts().CPlusPlus11 ? 84*f4a2713aSLionel Sambuc diag::warn_cxx98_compat_deleted_function : 85*f4a2713aSLionel Sambuc diag::ext_deleted_function); 86*f4a2713aSLionel Sambuc 87*f4a2713aSLionel Sambuc KWLoc = ConsumeToken(); 88*f4a2713aSLionel Sambuc Actions.SetDeclDeleted(FnD, KWLoc); 89*f4a2713aSLionel Sambuc Delete = true; 90*f4a2713aSLionel Sambuc } else if (Tok.is(tok::kw_default)) { 91*f4a2713aSLionel Sambuc Diag(Tok, getLangOpts().CPlusPlus11 ? 92*f4a2713aSLionel Sambuc diag::warn_cxx98_compat_defaulted_function : 93*f4a2713aSLionel Sambuc diag::ext_defaulted_function); 94*f4a2713aSLionel Sambuc 95*f4a2713aSLionel Sambuc KWLoc = ConsumeToken(); 96*f4a2713aSLionel Sambuc Actions.SetDeclDefaulted(FnD, KWLoc); 97*f4a2713aSLionel Sambuc } else { 98*f4a2713aSLionel Sambuc llvm_unreachable("function definition after = not 'delete' or 'default'"); 99*f4a2713aSLionel Sambuc } 100*f4a2713aSLionel Sambuc 101*f4a2713aSLionel Sambuc if (Tok.is(tok::comma)) { 102*f4a2713aSLionel Sambuc Diag(KWLoc, diag::err_default_delete_in_multiple_declaration) 103*f4a2713aSLionel Sambuc << Delete; 104*f4a2713aSLionel Sambuc SkipUntil(tok::semi); 105*f4a2713aSLionel Sambuc } else { 106*f4a2713aSLionel Sambuc ExpectAndConsume(tok::semi, diag::err_expected_semi_after, 107*f4a2713aSLionel Sambuc Delete ? "delete" : "default", tok::semi); 108*f4a2713aSLionel Sambuc } 109*f4a2713aSLionel Sambuc 110*f4a2713aSLionel Sambuc return FnD; 111*f4a2713aSLionel Sambuc } 112*f4a2713aSLionel Sambuc 113*f4a2713aSLionel Sambuc // In delayed template parsing mode, if we are within a class template 114*f4a2713aSLionel Sambuc // or if we are about to parse function member template then consume 115*f4a2713aSLionel Sambuc // the tokens and store them for parsing at the end of the translation unit. 116*f4a2713aSLionel Sambuc if (getLangOpts().DelayedTemplateParsing && 117*f4a2713aSLionel Sambuc DefinitionKind == FDK_Definition && 118*f4a2713aSLionel Sambuc !D.getDeclSpec().isConstexprSpecified() && 119*f4a2713aSLionel Sambuc !(FnD && getFunctionDecl(FnD) && 120*f4a2713aSLionel Sambuc getFunctionDecl(FnD)->getResultType()->getContainedAutoType()) && 121*f4a2713aSLionel Sambuc ((Actions.CurContext->isDependentContext() || 122*f4a2713aSLionel Sambuc (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && 123*f4a2713aSLionel Sambuc TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) && 124*f4a2713aSLionel Sambuc !Actions.IsInsideALocalClassWithinATemplateFunction())) { 125*f4a2713aSLionel Sambuc 126*f4a2713aSLionel Sambuc CachedTokens Toks; 127*f4a2713aSLionel Sambuc LexTemplateFunctionForLateParsing(Toks); 128*f4a2713aSLionel Sambuc 129*f4a2713aSLionel Sambuc if (FnD) { 130*f4a2713aSLionel Sambuc FunctionDecl *FD = getFunctionDecl(FnD); 131*f4a2713aSLionel Sambuc Actions.CheckForFunctionRedefinition(FD); 132*f4a2713aSLionel Sambuc Actions.MarkAsLateParsedTemplate(FD, FnD, Toks); 133*f4a2713aSLionel Sambuc } 134*f4a2713aSLionel Sambuc 135*f4a2713aSLionel Sambuc return FnD; 136*f4a2713aSLionel Sambuc } 137*f4a2713aSLionel Sambuc 138*f4a2713aSLionel Sambuc // Consume the tokens and store them for later parsing. 139*f4a2713aSLionel Sambuc 140*f4a2713aSLionel Sambuc LexedMethod* LM = new LexedMethod(this, FnD); 141*f4a2713aSLionel Sambuc getCurrentClass().LateParsedDeclarations.push_back(LM); 142*f4a2713aSLionel Sambuc LM->TemplateScope = getCurScope()->isTemplateParamScope(); 143*f4a2713aSLionel Sambuc CachedTokens &Toks = LM->Toks; 144*f4a2713aSLionel Sambuc 145*f4a2713aSLionel Sambuc tok::TokenKind kind = Tok.getKind(); 146*f4a2713aSLionel Sambuc // Consume everything up to (and including) the left brace of the 147*f4a2713aSLionel Sambuc // function body. 148*f4a2713aSLionel Sambuc if (ConsumeAndStoreFunctionPrologue(Toks)) { 149*f4a2713aSLionel Sambuc // We didn't find the left-brace we expected after the 150*f4a2713aSLionel Sambuc // constructor initializer; we already printed an error, and it's likely 151*f4a2713aSLionel Sambuc // impossible to recover, so don't try to parse this method later. 152*f4a2713aSLionel Sambuc // Skip over the rest of the decl and back to somewhere that looks 153*f4a2713aSLionel Sambuc // reasonable. 154*f4a2713aSLionel Sambuc SkipMalformedDecl(); 155*f4a2713aSLionel Sambuc delete getCurrentClass().LateParsedDeclarations.back(); 156*f4a2713aSLionel Sambuc getCurrentClass().LateParsedDeclarations.pop_back(); 157*f4a2713aSLionel Sambuc return FnD; 158*f4a2713aSLionel Sambuc } else { 159*f4a2713aSLionel Sambuc // Consume everything up to (and including) the matching right brace. 160*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 161*f4a2713aSLionel Sambuc } 162*f4a2713aSLionel Sambuc 163*f4a2713aSLionel Sambuc // If we're in a function-try-block, we need to store all the catch blocks. 164*f4a2713aSLionel Sambuc if (kind == tok::kw_try) { 165*f4a2713aSLionel Sambuc while (Tok.is(tok::kw_catch)) { 166*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false); 167*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 168*f4a2713aSLionel Sambuc } 169*f4a2713aSLionel Sambuc } 170*f4a2713aSLionel Sambuc 171*f4a2713aSLionel Sambuc if (FnD) { 172*f4a2713aSLionel Sambuc // If this is a friend function, mark that it's late-parsed so that 173*f4a2713aSLionel Sambuc // it's still known to be a definition even before we attach the 174*f4a2713aSLionel Sambuc // parsed body. Sema needs to treat friend function definitions 175*f4a2713aSLionel Sambuc // differently during template instantiation, and it's possible for 176*f4a2713aSLionel Sambuc // the containing class to be instantiated before all its member 177*f4a2713aSLionel Sambuc // function definitions are parsed. 178*f4a2713aSLionel Sambuc // 179*f4a2713aSLionel Sambuc // If you remove this, you can remove the code that clears the flag 180*f4a2713aSLionel Sambuc // after parsing the member. 181*f4a2713aSLionel Sambuc if (D.getDeclSpec().isFriendSpecified()) { 182*f4a2713aSLionel Sambuc FunctionDecl *FD = getFunctionDecl(FnD); 183*f4a2713aSLionel Sambuc Actions.CheckForFunctionRedefinition(FD); 184*f4a2713aSLionel Sambuc FD->setLateTemplateParsed(true); 185*f4a2713aSLionel Sambuc } 186*f4a2713aSLionel Sambuc } else { 187*f4a2713aSLionel Sambuc // If semantic analysis could not build a function declaration, 188*f4a2713aSLionel Sambuc // just throw away the late-parsed declaration. 189*f4a2713aSLionel Sambuc delete getCurrentClass().LateParsedDeclarations.back(); 190*f4a2713aSLionel Sambuc getCurrentClass().LateParsedDeclarations.pop_back(); 191*f4a2713aSLionel Sambuc } 192*f4a2713aSLionel Sambuc 193*f4a2713aSLionel Sambuc return FnD; 194*f4a2713aSLionel Sambuc } 195*f4a2713aSLionel Sambuc 196*f4a2713aSLionel Sambuc /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the 197*f4a2713aSLionel Sambuc /// specified Declarator is a well formed C++ non-static data member 198*f4a2713aSLionel Sambuc /// declaration. Now lex its initializer and store its tokens for parsing 199*f4a2713aSLionel Sambuc /// after the class is complete. 200*f4a2713aSLionel Sambuc void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) { 201*f4a2713aSLionel Sambuc assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) && 202*f4a2713aSLionel Sambuc "Current token not a '{' or '='!"); 203*f4a2713aSLionel Sambuc 204*f4a2713aSLionel Sambuc LateParsedMemberInitializer *MI = 205*f4a2713aSLionel Sambuc new LateParsedMemberInitializer(this, VarD); 206*f4a2713aSLionel Sambuc getCurrentClass().LateParsedDeclarations.push_back(MI); 207*f4a2713aSLionel Sambuc CachedTokens &Toks = MI->Toks; 208*f4a2713aSLionel Sambuc 209*f4a2713aSLionel Sambuc tok::TokenKind kind = Tok.getKind(); 210*f4a2713aSLionel Sambuc if (kind == tok::equal) { 211*f4a2713aSLionel Sambuc Toks.push_back(Tok); 212*f4a2713aSLionel Sambuc ConsumeToken(); 213*f4a2713aSLionel Sambuc } 214*f4a2713aSLionel Sambuc 215*f4a2713aSLionel Sambuc if (kind == tok::l_brace) { 216*f4a2713aSLionel Sambuc // Begin by storing the '{' token. 217*f4a2713aSLionel Sambuc Toks.push_back(Tok); 218*f4a2713aSLionel Sambuc ConsumeBrace(); 219*f4a2713aSLionel Sambuc 220*f4a2713aSLionel Sambuc // Consume everything up to (and including) the matching right brace. 221*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true); 222*f4a2713aSLionel Sambuc } else { 223*f4a2713aSLionel Sambuc // Consume everything up to (but excluding) the comma or semicolon. 224*f4a2713aSLionel Sambuc ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer); 225*f4a2713aSLionel Sambuc } 226*f4a2713aSLionel Sambuc 227*f4a2713aSLionel Sambuc // Store an artificial EOF token to ensure that we don't run off the end of 228*f4a2713aSLionel Sambuc // the initializer when we come to parse it. 229*f4a2713aSLionel Sambuc Token Eof; 230*f4a2713aSLionel Sambuc Eof.startToken(); 231*f4a2713aSLionel Sambuc Eof.setKind(tok::eof); 232*f4a2713aSLionel Sambuc Eof.setLocation(Tok.getLocation()); 233*f4a2713aSLionel Sambuc Toks.push_back(Eof); 234*f4a2713aSLionel Sambuc } 235*f4a2713aSLionel Sambuc 236*f4a2713aSLionel Sambuc Parser::LateParsedDeclaration::~LateParsedDeclaration() {} 237*f4a2713aSLionel Sambuc void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {} 238*f4a2713aSLionel Sambuc void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {} 239*f4a2713aSLionel Sambuc void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {} 240*f4a2713aSLionel Sambuc 241*f4a2713aSLionel Sambuc Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) 242*f4a2713aSLionel Sambuc : Self(P), Class(C) {} 243*f4a2713aSLionel Sambuc 244*f4a2713aSLionel Sambuc Parser::LateParsedClass::~LateParsedClass() { 245*f4a2713aSLionel Sambuc Self->DeallocateParsedClasses(Class); 246*f4a2713aSLionel Sambuc } 247*f4a2713aSLionel Sambuc 248*f4a2713aSLionel Sambuc void Parser::LateParsedClass::ParseLexedMethodDeclarations() { 249*f4a2713aSLionel Sambuc Self->ParseLexedMethodDeclarations(*Class); 250*f4a2713aSLionel Sambuc } 251*f4a2713aSLionel Sambuc 252*f4a2713aSLionel Sambuc void Parser::LateParsedClass::ParseLexedMemberInitializers() { 253*f4a2713aSLionel Sambuc Self->ParseLexedMemberInitializers(*Class); 254*f4a2713aSLionel Sambuc } 255*f4a2713aSLionel Sambuc 256*f4a2713aSLionel Sambuc void Parser::LateParsedClass::ParseLexedMethodDefs() { 257*f4a2713aSLionel Sambuc Self->ParseLexedMethodDefs(*Class); 258*f4a2713aSLionel Sambuc } 259*f4a2713aSLionel Sambuc 260*f4a2713aSLionel Sambuc void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { 261*f4a2713aSLionel Sambuc Self->ParseLexedMethodDeclaration(*this); 262*f4a2713aSLionel Sambuc } 263*f4a2713aSLionel Sambuc 264*f4a2713aSLionel Sambuc void Parser::LexedMethod::ParseLexedMethodDefs() { 265*f4a2713aSLionel Sambuc Self->ParseLexedMethodDef(*this); 266*f4a2713aSLionel Sambuc } 267*f4a2713aSLionel Sambuc 268*f4a2713aSLionel Sambuc void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() { 269*f4a2713aSLionel Sambuc Self->ParseLexedMemberInitializer(*this); 270*f4a2713aSLionel Sambuc } 271*f4a2713aSLionel Sambuc 272*f4a2713aSLionel Sambuc /// ParseLexedMethodDeclarations - We finished parsing the member 273*f4a2713aSLionel Sambuc /// specification of a top (non-nested) C++ class. Now go over the 274*f4a2713aSLionel Sambuc /// stack of method declarations with some parts for which parsing was 275*f4a2713aSLionel Sambuc /// delayed (such as default arguments) and parse them. 276*f4a2713aSLionel Sambuc void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { 277*f4a2713aSLionel Sambuc bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 278*f4a2713aSLionel Sambuc ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); 279*f4a2713aSLionel Sambuc TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 280*f4a2713aSLionel Sambuc if (HasTemplateScope) { 281*f4a2713aSLionel Sambuc Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 282*f4a2713aSLionel Sambuc ++CurTemplateDepthTracker; 283*f4a2713aSLionel Sambuc } 284*f4a2713aSLionel Sambuc 285*f4a2713aSLionel Sambuc // The current scope is still active if we're the top-level class. 286*f4a2713aSLionel Sambuc // Otherwise we'll need to push and enter a new scope. 287*f4a2713aSLionel Sambuc bool HasClassScope = !Class.TopLevelClass; 288*f4a2713aSLionel Sambuc ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, 289*f4a2713aSLionel Sambuc HasClassScope); 290*f4a2713aSLionel Sambuc if (HasClassScope) 291*f4a2713aSLionel Sambuc Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); 292*f4a2713aSLionel Sambuc 293*f4a2713aSLionel Sambuc for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 294*f4a2713aSLionel Sambuc Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations(); 295*f4a2713aSLionel Sambuc } 296*f4a2713aSLionel Sambuc 297*f4a2713aSLionel Sambuc if (HasClassScope) 298*f4a2713aSLionel Sambuc Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate); 299*f4a2713aSLionel Sambuc } 300*f4a2713aSLionel Sambuc 301*f4a2713aSLionel Sambuc void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { 302*f4a2713aSLionel Sambuc // If this is a member template, introduce the template parameter scope. 303*f4a2713aSLionel Sambuc ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); 304*f4a2713aSLionel Sambuc TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 305*f4a2713aSLionel Sambuc if (LM.TemplateScope) { 306*f4a2713aSLionel Sambuc Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method); 307*f4a2713aSLionel Sambuc ++CurTemplateDepthTracker; 308*f4a2713aSLionel Sambuc } 309*f4a2713aSLionel Sambuc // Start the delayed C++ method declaration 310*f4a2713aSLionel Sambuc Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 311*f4a2713aSLionel Sambuc 312*f4a2713aSLionel Sambuc // Introduce the parameters into scope and parse their default 313*f4a2713aSLionel Sambuc // arguments. 314*f4a2713aSLionel Sambuc ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope | 315*f4a2713aSLionel Sambuc Scope::FunctionDeclarationScope | Scope::DeclScope); 316*f4a2713aSLionel Sambuc for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { 317*f4a2713aSLionel Sambuc // Introduce the parameter into scope. 318*f4a2713aSLionel Sambuc Actions.ActOnDelayedCXXMethodParameter(getCurScope(), 319*f4a2713aSLionel Sambuc LM.DefaultArgs[I].Param); 320*f4a2713aSLionel Sambuc 321*f4a2713aSLionel Sambuc if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) { 322*f4a2713aSLionel Sambuc // Save the current token position. 323*f4a2713aSLionel Sambuc SourceLocation origLoc = Tok.getLocation(); 324*f4a2713aSLionel Sambuc 325*f4a2713aSLionel Sambuc // Parse the default argument from its saved token stream. 326*f4a2713aSLionel Sambuc Toks->push_back(Tok); // So that the current token doesn't get lost 327*f4a2713aSLionel Sambuc PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false); 328*f4a2713aSLionel Sambuc 329*f4a2713aSLionel Sambuc // Consume the previously-pushed token. 330*f4a2713aSLionel Sambuc ConsumeAnyToken(); 331*f4a2713aSLionel Sambuc 332*f4a2713aSLionel Sambuc // Consume the '='. 333*f4a2713aSLionel Sambuc assert(Tok.is(tok::equal) && "Default argument not starting with '='"); 334*f4a2713aSLionel Sambuc SourceLocation EqualLoc = ConsumeToken(); 335*f4a2713aSLionel Sambuc 336*f4a2713aSLionel Sambuc // The argument isn't actually potentially evaluated unless it is 337*f4a2713aSLionel Sambuc // used. 338*f4a2713aSLionel Sambuc EnterExpressionEvaluationContext Eval(Actions, 339*f4a2713aSLionel Sambuc Sema::PotentiallyEvaluatedIfUsed, 340*f4a2713aSLionel Sambuc LM.DefaultArgs[I].Param); 341*f4a2713aSLionel Sambuc 342*f4a2713aSLionel Sambuc ExprResult DefArgResult; 343*f4a2713aSLionel Sambuc if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { 344*f4a2713aSLionel Sambuc Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); 345*f4a2713aSLionel Sambuc DefArgResult = ParseBraceInitializer(); 346*f4a2713aSLionel Sambuc } else 347*f4a2713aSLionel Sambuc DefArgResult = ParseAssignmentExpression(); 348*f4a2713aSLionel Sambuc if (DefArgResult.isInvalid()) 349*f4a2713aSLionel Sambuc Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param); 350*f4a2713aSLionel Sambuc else { 351*f4a2713aSLionel Sambuc if (Tok.is(tok::cxx_defaultarg_end)) 352*f4a2713aSLionel Sambuc ConsumeToken(); 353*f4a2713aSLionel Sambuc else { 354*f4a2713aSLionel Sambuc // The last two tokens are the terminator and the saved value of 355*f4a2713aSLionel Sambuc // Tok; the last token in the default argument is the one before 356*f4a2713aSLionel Sambuc // those. 357*f4a2713aSLionel Sambuc assert(Toks->size() >= 3 && "expected a token in default arg"); 358*f4a2713aSLionel Sambuc Diag(Tok.getLocation(), diag::err_default_arg_unparsed) 359*f4a2713aSLionel Sambuc << SourceRange(Tok.getLocation(), 360*f4a2713aSLionel Sambuc (*Toks)[Toks->size() - 3].getLocation()); 361*f4a2713aSLionel Sambuc } 362*f4a2713aSLionel Sambuc Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc, 363*f4a2713aSLionel Sambuc DefArgResult.take()); 364*f4a2713aSLionel Sambuc } 365*f4a2713aSLionel Sambuc 366*f4a2713aSLionel Sambuc assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, 367*f4a2713aSLionel Sambuc Tok.getLocation()) && 368*f4a2713aSLionel Sambuc "ParseAssignmentExpression went over the default arg tokens!"); 369*f4a2713aSLionel Sambuc // There could be leftover tokens (e.g. because of an error). 370*f4a2713aSLionel Sambuc // Skip through until we reach the original token position. 371*f4a2713aSLionel Sambuc while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 372*f4a2713aSLionel Sambuc ConsumeAnyToken(); 373*f4a2713aSLionel Sambuc 374*f4a2713aSLionel Sambuc delete Toks; 375*f4a2713aSLionel Sambuc LM.DefaultArgs[I].Toks = 0; 376*f4a2713aSLionel Sambuc } 377*f4a2713aSLionel Sambuc } 378*f4a2713aSLionel Sambuc 379*f4a2713aSLionel Sambuc PrototypeScope.Exit(); 380*f4a2713aSLionel Sambuc 381*f4a2713aSLionel Sambuc // Finish the delayed C++ method declaration. 382*f4a2713aSLionel Sambuc Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method); 383*f4a2713aSLionel Sambuc } 384*f4a2713aSLionel Sambuc 385*f4a2713aSLionel Sambuc /// ParseLexedMethodDefs - We finished parsing the member specification of a top 386*f4a2713aSLionel Sambuc /// (non-nested) C++ class. Now go over the stack of lexed methods that were 387*f4a2713aSLionel Sambuc /// collected during its parsing and parse them all. 388*f4a2713aSLionel Sambuc void Parser::ParseLexedMethodDefs(ParsingClass &Class) { 389*f4a2713aSLionel Sambuc bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 390*f4a2713aSLionel Sambuc ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope); 391*f4a2713aSLionel Sambuc TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 392*f4a2713aSLionel Sambuc if (HasTemplateScope) { 393*f4a2713aSLionel Sambuc Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 394*f4a2713aSLionel Sambuc ++CurTemplateDepthTracker; 395*f4a2713aSLionel Sambuc } 396*f4a2713aSLionel Sambuc bool HasClassScope = !Class.TopLevelClass; 397*f4a2713aSLionel Sambuc ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, 398*f4a2713aSLionel Sambuc HasClassScope); 399*f4a2713aSLionel Sambuc 400*f4a2713aSLionel Sambuc for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 401*f4a2713aSLionel Sambuc Class.LateParsedDeclarations[i]->ParseLexedMethodDefs(); 402*f4a2713aSLionel Sambuc } 403*f4a2713aSLionel Sambuc } 404*f4a2713aSLionel Sambuc 405*f4a2713aSLionel Sambuc void Parser::ParseLexedMethodDef(LexedMethod &LM) { 406*f4a2713aSLionel Sambuc // If this is a member template, introduce the template parameter scope. 407*f4a2713aSLionel Sambuc ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope); 408*f4a2713aSLionel Sambuc TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 409*f4a2713aSLionel Sambuc if (LM.TemplateScope) { 410*f4a2713aSLionel Sambuc Actions.ActOnReenterTemplateScope(getCurScope(), LM.D); 411*f4a2713aSLionel Sambuc ++CurTemplateDepthTracker; 412*f4a2713aSLionel Sambuc } 413*f4a2713aSLionel Sambuc // Save the current token position. 414*f4a2713aSLionel Sambuc SourceLocation origLoc = Tok.getLocation(); 415*f4a2713aSLionel Sambuc 416*f4a2713aSLionel Sambuc assert(!LM.Toks.empty() && "Empty body!"); 417*f4a2713aSLionel Sambuc // Append the current token at the end of the new token stream so that it 418*f4a2713aSLionel Sambuc // doesn't get lost. 419*f4a2713aSLionel Sambuc LM.Toks.push_back(Tok); 420*f4a2713aSLionel Sambuc PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false); 421*f4a2713aSLionel Sambuc 422*f4a2713aSLionel Sambuc // Consume the previously pushed token. 423*f4a2713aSLionel Sambuc ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 424*f4a2713aSLionel Sambuc assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) 425*f4a2713aSLionel Sambuc && "Inline method not starting with '{', ':' or 'try'"); 426*f4a2713aSLionel Sambuc 427*f4a2713aSLionel Sambuc // Parse the method body. Function body parsing code is similar enough 428*f4a2713aSLionel Sambuc // to be re-used for method bodies as well. 429*f4a2713aSLionel Sambuc ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope); 430*f4a2713aSLionel Sambuc Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D); 431*f4a2713aSLionel Sambuc 432*f4a2713aSLionel Sambuc if (Tok.is(tok::kw_try)) { 433*f4a2713aSLionel Sambuc ParseFunctionTryBlock(LM.D, FnScope); 434*f4a2713aSLionel Sambuc assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc, 435*f4a2713aSLionel Sambuc Tok.getLocation()) && 436*f4a2713aSLionel Sambuc "ParseFunctionTryBlock went over the cached tokens!"); 437*f4a2713aSLionel Sambuc // There could be leftover tokens (e.g. because of an error). 438*f4a2713aSLionel Sambuc // Skip through until we reach the original token position. 439*f4a2713aSLionel Sambuc while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 440*f4a2713aSLionel Sambuc ConsumeAnyToken(); 441*f4a2713aSLionel Sambuc return; 442*f4a2713aSLionel Sambuc } 443*f4a2713aSLionel Sambuc if (Tok.is(tok::colon)) { 444*f4a2713aSLionel Sambuc ParseConstructorInitializer(LM.D); 445*f4a2713aSLionel Sambuc 446*f4a2713aSLionel Sambuc // Error recovery. 447*f4a2713aSLionel Sambuc if (!Tok.is(tok::l_brace)) { 448*f4a2713aSLionel Sambuc FnScope.Exit(); 449*f4a2713aSLionel Sambuc Actions.ActOnFinishFunctionBody(LM.D, 0); 450*f4a2713aSLionel Sambuc while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 451*f4a2713aSLionel Sambuc ConsumeAnyToken(); 452*f4a2713aSLionel Sambuc return; 453*f4a2713aSLionel Sambuc } 454*f4a2713aSLionel Sambuc } else 455*f4a2713aSLionel Sambuc Actions.ActOnDefaultCtorInitializers(LM.D); 456*f4a2713aSLionel Sambuc 457*f4a2713aSLionel Sambuc assert((Actions.getDiagnostics().hasErrorOccurred() || 458*f4a2713aSLionel Sambuc !isa<FunctionTemplateDecl>(LM.D) || 459*f4a2713aSLionel Sambuc cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth() 460*f4a2713aSLionel Sambuc < TemplateParameterDepth) && 461*f4a2713aSLionel Sambuc "TemplateParameterDepth should be greater than the depth of " 462*f4a2713aSLionel Sambuc "current template being instantiated!"); 463*f4a2713aSLionel Sambuc 464*f4a2713aSLionel Sambuc ParseFunctionStatementBody(LM.D, FnScope); 465*f4a2713aSLionel Sambuc 466*f4a2713aSLionel Sambuc // Clear the late-template-parsed bit if we set it before. 467*f4a2713aSLionel Sambuc if (LM.D) getFunctionDecl(LM.D)->setLateTemplateParsed(false); 468*f4a2713aSLionel Sambuc 469*f4a2713aSLionel Sambuc if (Tok.getLocation() != origLoc) { 470*f4a2713aSLionel Sambuc // Due to parsing error, we either went over the cached tokens or 471*f4a2713aSLionel Sambuc // there are still cached tokens left. If it's the latter case skip the 472*f4a2713aSLionel Sambuc // leftover tokens. 473*f4a2713aSLionel Sambuc // Since this is an uncommon situation that should be avoided, use the 474*f4a2713aSLionel Sambuc // expensive isBeforeInTranslationUnit call. 475*f4a2713aSLionel Sambuc if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), 476*f4a2713aSLionel Sambuc origLoc)) 477*f4a2713aSLionel Sambuc while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof)) 478*f4a2713aSLionel Sambuc ConsumeAnyToken(); 479*f4a2713aSLionel Sambuc } 480*f4a2713aSLionel Sambuc } 481*f4a2713aSLionel Sambuc 482*f4a2713aSLionel Sambuc /// ParseLexedMemberInitializers - We finished parsing the member specification 483*f4a2713aSLionel Sambuc /// of a top (non-nested) C++ class. Now go over the stack of lexed data member 484*f4a2713aSLionel Sambuc /// initializers that were collected during its parsing and parse them all. 485*f4a2713aSLionel Sambuc void Parser::ParseLexedMemberInitializers(ParsingClass &Class) { 486*f4a2713aSLionel Sambuc bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; 487*f4a2713aSLionel Sambuc ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, 488*f4a2713aSLionel Sambuc HasTemplateScope); 489*f4a2713aSLionel Sambuc TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); 490*f4a2713aSLionel Sambuc if (HasTemplateScope) { 491*f4a2713aSLionel Sambuc Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); 492*f4a2713aSLionel Sambuc ++CurTemplateDepthTracker; 493*f4a2713aSLionel Sambuc } 494*f4a2713aSLionel Sambuc // Set or update the scope flags. 495*f4a2713aSLionel Sambuc bool AlreadyHasClassScope = Class.TopLevelClass; 496*f4a2713aSLionel Sambuc unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; 497*f4a2713aSLionel Sambuc ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); 498*f4a2713aSLionel Sambuc ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); 499*f4a2713aSLionel Sambuc 500*f4a2713aSLionel Sambuc if (!AlreadyHasClassScope) 501*f4a2713aSLionel Sambuc Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), 502*f4a2713aSLionel Sambuc Class.TagOrTemplate); 503*f4a2713aSLionel Sambuc 504*f4a2713aSLionel Sambuc if (!Class.LateParsedDeclarations.empty()) { 505*f4a2713aSLionel Sambuc // C++11 [expr.prim.general]p4: 506*f4a2713aSLionel Sambuc // Otherwise, if a member-declarator declares a non-static data member 507*f4a2713aSLionel Sambuc // (9.2) of a class X, the expression this is a prvalue of type "pointer 508*f4a2713aSLionel Sambuc // to X" within the optional brace-or-equal-initializer. It shall not 509*f4a2713aSLionel Sambuc // appear elsewhere in the member-declarator. 510*f4a2713aSLionel Sambuc Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, 511*f4a2713aSLionel Sambuc /*TypeQuals=*/(unsigned)0); 512*f4a2713aSLionel Sambuc 513*f4a2713aSLionel Sambuc for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) { 514*f4a2713aSLionel Sambuc Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers(); 515*f4a2713aSLionel Sambuc } 516*f4a2713aSLionel Sambuc } 517*f4a2713aSLionel Sambuc 518*f4a2713aSLionel Sambuc if (!AlreadyHasClassScope) 519*f4a2713aSLionel Sambuc Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), 520*f4a2713aSLionel Sambuc Class.TagOrTemplate); 521*f4a2713aSLionel Sambuc 522*f4a2713aSLionel Sambuc Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate); 523*f4a2713aSLionel Sambuc } 524*f4a2713aSLionel Sambuc 525*f4a2713aSLionel Sambuc void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { 526*f4a2713aSLionel Sambuc if (!MI.Field || MI.Field->isInvalidDecl()) 527*f4a2713aSLionel Sambuc return; 528*f4a2713aSLionel Sambuc 529*f4a2713aSLionel Sambuc // Append the current token at the end of the new token stream so that it 530*f4a2713aSLionel Sambuc // doesn't get lost. 531*f4a2713aSLionel Sambuc MI.Toks.push_back(Tok); 532*f4a2713aSLionel Sambuc PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false); 533*f4a2713aSLionel Sambuc 534*f4a2713aSLionel Sambuc // Consume the previously pushed token. 535*f4a2713aSLionel Sambuc ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); 536*f4a2713aSLionel Sambuc 537*f4a2713aSLionel Sambuc SourceLocation EqualLoc; 538*f4a2713aSLionel Sambuc 539*f4a2713aSLionel Sambuc ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false, 540*f4a2713aSLionel Sambuc EqualLoc); 541*f4a2713aSLionel Sambuc 542*f4a2713aSLionel Sambuc Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release()); 543*f4a2713aSLionel Sambuc 544*f4a2713aSLionel Sambuc // The next token should be our artificial terminating EOF token. 545*f4a2713aSLionel Sambuc if (Tok.isNot(tok::eof)) { 546*f4a2713aSLionel Sambuc SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation); 547*f4a2713aSLionel Sambuc if (!EndLoc.isValid()) 548*f4a2713aSLionel Sambuc EndLoc = Tok.getLocation(); 549*f4a2713aSLionel Sambuc // No fixit; we can't recover as if there were a semicolon here. 550*f4a2713aSLionel Sambuc Diag(EndLoc, diag::err_expected_semi_decl_list); 551*f4a2713aSLionel Sambuc 552*f4a2713aSLionel Sambuc // Consume tokens until we hit the artificial EOF. 553*f4a2713aSLionel Sambuc while (Tok.isNot(tok::eof)) 554*f4a2713aSLionel Sambuc ConsumeAnyToken(); 555*f4a2713aSLionel Sambuc } 556*f4a2713aSLionel Sambuc ConsumeAnyToken(); 557*f4a2713aSLionel Sambuc } 558*f4a2713aSLionel Sambuc 559*f4a2713aSLionel Sambuc /// ConsumeAndStoreUntil - Consume and store the token at the passed token 560*f4a2713aSLionel Sambuc /// container until the token 'T' is reached (which gets 561*f4a2713aSLionel Sambuc /// consumed/stored too, if ConsumeFinalToken). 562*f4a2713aSLionel Sambuc /// If StopAtSemi is true, then we will stop early at a ';' character. 563*f4a2713aSLionel Sambuc /// Returns true if token 'T1' or 'T2' was found. 564*f4a2713aSLionel Sambuc /// NOTE: This is a specialized version of Parser::SkipUntil. 565*f4a2713aSLionel Sambuc bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, 566*f4a2713aSLionel Sambuc CachedTokens &Toks, 567*f4a2713aSLionel Sambuc bool StopAtSemi, bool ConsumeFinalToken) { 568*f4a2713aSLionel Sambuc // We always want this function to consume at least one token if the first 569*f4a2713aSLionel Sambuc // token isn't T and if not at EOF. 570*f4a2713aSLionel Sambuc bool isFirstTokenConsumed = true; 571*f4a2713aSLionel Sambuc while (1) { 572*f4a2713aSLionel Sambuc // If we found one of the tokens, stop and return true. 573*f4a2713aSLionel Sambuc if (Tok.is(T1) || Tok.is(T2)) { 574*f4a2713aSLionel Sambuc if (ConsumeFinalToken) { 575*f4a2713aSLionel Sambuc Toks.push_back(Tok); 576*f4a2713aSLionel Sambuc ConsumeAnyToken(); 577*f4a2713aSLionel Sambuc } 578*f4a2713aSLionel Sambuc return true; 579*f4a2713aSLionel Sambuc } 580*f4a2713aSLionel Sambuc 581*f4a2713aSLionel Sambuc switch (Tok.getKind()) { 582*f4a2713aSLionel Sambuc case tok::eof: 583*f4a2713aSLionel Sambuc // Ran out of tokens. 584*f4a2713aSLionel Sambuc return false; 585*f4a2713aSLionel Sambuc 586*f4a2713aSLionel Sambuc case tok::l_paren: 587*f4a2713aSLionel Sambuc // Recursively consume properly-nested parens. 588*f4a2713aSLionel Sambuc Toks.push_back(Tok); 589*f4a2713aSLionel Sambuc ConsumeParen(); 590*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); 591*f4a2713aSLionel Sambuc break; 592*f4a2713aSLionel Sambuc case tok::l_square: 593*f4a2713aSLionel Sambuc // Recursively consume properly-nested square brackets. 594*f4a2713aSLionel Sambuc Toks.push_back(Tok); 595*f4a2713aSLionel Sambuc ConsumeBracket(); 596*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); 597*f4a2713aSLionel Sambuc break; 598*f4a2713aSLionel Sambuc case tok::l_brace: 599*f4a2713aSLionel Sambuc // Recursively consume properly-nested braces. 600*f4a2713aSLionel Sambuc Toks.push_back(Tok); 601*f4a2713aSLionel Sambuc ConsumeBrace(); 602*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 603*f4a2713aSLionel Sambuc break; 604*f4a2713aSLionel Sambuc 605*f4a2713aSLionel Sambuc // Okay, we found a ']' or '}' or ')', which we think should be balanced. 606*f4a2713aSLionel Sambuc // Since the user wasn't looking for this token (if they were, it would 607*f4a2713aSLionel Sambuc // already be handled), this isn't balanced. If there is a LHS token at a 608*f4a2713aSLionel Sambuc // higher level, we will assume that this matches the unbalanced token 609*f4a2713aSLionel Sambuc // and return it. Otherwise, this is a spurious RHS token, which we skip. 610*f4a2713aSLionel Sambuc case tok::r_paren: 611*f4a2713aSLionel Sambuc if (ParenCount && !isFirstTokenConsumed) 612*f4a2713aSLionel Sambuc return false; // Matches something. 613*f4a2713aSLionel Sambuc Toks.push_back(Tok); 614*f4a2713aSLionel Sambuc ConsumeParen(); 615*f4a2713aSLionel Sambuc break; 616*f4a2713aSLionel Sambuc case tok::r_square: 617*f4a2713aSLionel Sambuc if (BracketCount && !isFirstTokenConsumed) 618*f4a2713aSLionel Sambuc return false; // Matches something. 619*f4a2713aSLionel Sambuc Toks.push_back(Tok); 620*f4a2713aSLionel Sambuc ConsumeBracket(); 621*f4a2713aSLionel Sambuc break; 622*f4a2713aSLionel Sambuc case tok::r_brace: 623*f4a2713aSLionel Sambuc if (BraceCount && !isFirstTokenConsumed) 624*f4a2713aSLionel Sambuc return false; // Matches something. 625*f4a2713aSLionel Sambuc Toks.push_back(Tok); 626*f4a2713aSLionel Sambuc ConsumeBrace(); 627*f4a2713aSLionel Sambuc break; 628*f4a2713aSLionel Sambuc 629*f4a2713aSLionel Sambuc case tok::code_completion: 630*f4a2713aSLionel Sambuc Toks.push_back(Tok); 631*f4a2713aSLionel Sambuc ConsumeCodeCompletionToken(); 632*f4a2713aSLionel Sambuc break; 633*f4a2713aSLionel Sambuc 634*f4a2713aSLionel Sambuc case tok::string_literal: 635*f4a2713aSLionel Sambuc case tok::wide_string_literal: 636*f4a2713aSLionel Sambuc case tok::utf8_string_literal: 637*f4a2713aSLionel Sambuc case tok::utf16_string_literal: 638*f4a2713aSLionel Sambuc case tok::utf32_string_literal: 639*f4a2713aSLionel Sambuc Toks.push_back(Tok); 640*f4a2713aSLionel Sambuc ConsumeStringToken(); 641*f4a2713aSLionel Sambuc break; 642*f4a2713aSLionel Sambuc case tok::semi: 643*f4a2713aSLionel Sambuc if (StopAtSemi) 644*f4a2713aSLionel Sambuc return false; 645*f4a2713aSLionel Sambuc // FALL THROUGH. 646*f4a2713aSLionel Sambuc default: 647*f4a2713aSLionel Sambuc // consume this token. 648*f4a2713aSLionel Sambuc Toks.push_back(Tok); 649*f4a2713aSLionel Sambuc ConsumeToken(); 650*f4a2713aSLionel Sambuc break; 651*f4a2713aSLionel Sambuc } 652*f4a2713aSLionel Sambuc isFirstTokenConsumed = false; 653*f4a2713aSLionel Sambuc } 654*f4a2713aSLionel Sambuc } 655*f4a2713aSLionel Sambuc 656*f4a2713aSLionel Sambuc /// \brief Consume tokens and store them in the passed token container until 657*f4a2713aSLionel Sambuc /// we've passed the try keyword and constructor initializers and have consumed 658*f4a2713aSLionel Sambuc /// the opening brace of the function body. The opening brace will be consumed 659*f4a2713aSLionel Sambuc /// if and only if there was no error. 660*f4a2713aSLionel Sambuc /// 661*f4a2713aSLionel Sambuc /// \return True on error. 662*f4a2713aSLionel Sambuc bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) { 663*f4a2713aSLionel Sambuc if (Tok.is(tok::kw_try)) { 664*f4a2713aSLionel Sambuc Toks.push_back(Tok); 665*f4a2713aSLionel Sambuc ConsumeToken(); 666*f4a2713aSLionel Sambuc } 667*f4a2713aSLionel Sambuc 668*f4a2713aSLionel Sambuc if (Tok.isNot(tok::colon)) { 669*f4a2713aSLionel Sambuc // Easy case, just a function body. 670*f4a2713aSLionel Sambuc 671*f4a2713aSLionel Sambuc // Grab any remaining garbage to be diagnosed later. We stop when we reach a 672*f4a2713aSLionel Sambuc // brace: an opening one is the function body, while a closing one probably 673*f4a2713aSLionel Sambuc // means we've reached the end of the class. 674*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks, 675*f4a2713aSLionel Sambuc /*StopAtSemi=*/true, 676*f4a2713aSLionel Sambuc /*ConsumeFinalToken=*/false); 677*f4a2713aSLionel Sambuc if (Tok.isNot(tok::l_brace)) 678*f4a2713aSLionel Sambuc return Diag(Tok.getLocation(), diag::err_expected_lbrace); 679*f4a2713aSLionel Sambuc 680*f4a2713aSLionel Sambuc Toks.push_back(Tok); 681*f4a2713aSLionel Sambuc ConsumeBrace(); 682*f4a2713aSLionel Sambuc return false; 683*f4a2713aSLionel Sambuc } 684*f4a2713aSLionel Sambuc 685*f4a2713aSLionel Sambuc Toks.push_back(Tok); 686*f4a2713aSLionel Sambuc ConsumeToken(); 687*f4a2713aSLionel Sambuc 688*f4a2713aSLionel Sambuc // We can't reliably skip over a mem-initializer-id, because it could be 689*f4a2713aSLionel Sambuc // a template-id involving not-yet-declared names. Given: 690*f4a2713aSLionel Sambuc // 691*f4a2713aSLionel Sambuc // S ( ) : a < b < c > ( e ) 692*f4a2713aSLionel Sambuc // 693*f4a2713aSLionel Sambuc // 'e' might be an initializer or part of a template argument, depending 694*f4a2713aSLionel Sambuc // on whether 'b' is a template. 695*f4a2713aSLionel Sambuc 696*f4a2713aSLionel Sambuc // Track whether we might be inside a template argument. We can give 697*f4a2713aSLionel Sambuc // significantly better diagnostics if we know that we're not. 698*f4a2713aSLionel Sambuc bool MightBeTemplateArgument = false; 699*f4a2713aSLionel Sambuc 700*f4a2713aSLionel Sambuc while (true) { 701*f4a2713aSLionel Sambuc // Skip over the mem-initializer-id, if possible. 702*f4a2713aSLionel Sambuc if (Tok.is(tok::kw_decltype)) { 703*f4a2713aSLionel Sambuc Toks.push_back(Tok); 704*f4a2713aSLionel Sambuc SourceLocation OpenLoc = ConsumeToken(); 705*f4a2713aSLionel Sambuc if (Tok.isNot(tok::l_paren)) 706*f4a2713aSLionel Sambuc return Diag(Tok.getLocation(), diag::err_expected_lparen_after) 707*f4a2713aSLionel Sambuc << "decltype"; 708*f4a2713aSLionel Sambuc Toks.push_back(Tok); 709*f4a2713aSLionel Sambuc ConsumeParen(); 710*f4a2713aSLionel Sambuc if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) { 711*f4a2713aSLionel Sambuc Diag(Tok.getLocation(), diag::err_expected_rparen); 712*f4a2713aSLionel Sambuc Diag(OpenLoc, diag::note_matching) << "("; 713*f4a2713aSLionel Sambuc return true; 714*f4a2713aSLionel Sambuc } 715*f4a2713aSLionel Sambuc } 716*f4a2713aSLionel Sambuc do { 717*f4a2713aSLionel Sambuc // Walk over a component of a nested-name-specifier. 718*f4a2713aSLionel Sambuc if (Tok.is(tok::coloncolon)) { 719*f4a2713aSLionel Sambuc Toks.push_back(Tok); 720*f4a2713aSLionel Sambuc ConsumeToken(); 721*f4a2713aSLionel Sambuc 722*f4a2713aSLionel Sambuc if (Tok.is(tok::kw_template)) { 723*f4a2713aSLionel Sambuc Toks.push_back(Tok); 724*f4a2713aSLionel Sambuc ConsumeToken(); 725*f4a2713aSLionel Sambuc } 726*f4a2713aSLionel Sambuc } 727*f4a2713aSLionel Sambuc 728*f4a2713aSLionel Sambuc if (Tok.is(tok::identifier) || Tok.is(tok::kw_template)) { 729*f4a2713aSLionel Sambuc Toks.push_back(Tok); 730*f4a2713aSLionel Sambuc ConsumeToken(); 731*f4a2713aSLionel Sambuc } else if (Tok.is(tok::code_completion)) { 732*f4a2713aSLionel Sambuc Toks.push_back(Tok); 733*f4a2713aSLionel Sambuc ConsumeCodeCompletionToken(); 734*f4a2713aSLionel Sambuc // Consume the rest of the initializers permissively. 735*f4a2713aSLionel Sambuc // FIXME: We should be able to perform code-completion here even if 736*f4a2713aSLionel Sambuc // there isn't a subsequent '{' token. 737*f4a2713aSLionel Sambuc MightBeTemplateArgument = true; 738*f4a2713aSLionel Sambuc break; 739*f4a2713aSLionel Sambuc } else { 740*f4a2713aSLionel Sambuc break; 741*f4a2713aSLionel Sambuc } 742*f4a2713aSLionel Sambuc } while (Tok.is(tok::coloncolon)); 743*f4a2713aSLionel Sambuc 744*f4a2713aSLionel Sambuc if (Tok.is(tok::less)) 745*f4a2713aSLionel Sambuc MightBeTemplateArgument = true; 746*f4a2713aSLionel Sambuc 747*f4a2713aSLionel Sambuc if (MightBeTemplateArgument) { 748*f4a2713aSLionel Sambuc // We may be inside a template argument list. Grab up to the start of the 749*f4a2713aSLionel Sambuc // next parenthesized initializer or braced-init-list. This *might* be the 750*f4a2713aSLionel Sambuc // initializer, or it might be a subexpression in the template argument 751*f4a2713aSLionel Sambuc // list. 752*f4a2713aSLionel Sambuc // FIXME: Count angle brackets, and clear MightBeTemplateArgument 753*f4a2713aSLionel Sambuc // if all angles are closed. 754*f4a2713aSLionel Sambuc if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks, 755*f4a2713aSLionel Sambuc /*StopAtSemi=*/true, 756*f4a2713aSLionel Sambuc /*ConsumeFinalToken=*/false)) { 757*f4a2713aSLionel Sambuc // We're not just missing the initializer, we're also missing the 758*f4a2713aSLionel Sambuc // function body! 759*f4a2713aSLionel Sambuc return Diag(Tok.getLocation(), diag::err_expected_lbrace); 760*f4a2713aSLionel Sambuc } 761*f4a2713aSLionel Sambuc } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) { 762*f4a2713aSLionel Sambuc // We found something weird in a mem-initializer-id. 763*f4a2713aSLionel Sambuc return Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 764*f4a2713aSLionel Sambuc ? diag::err_expected_lparen_or_lbrace 765*f4a2713aSLionel Sambuc : diag::err_expected_lparen); 766*f4a2713aSLionel Sambuc } 767*f4a2713aSLionel Sambuc 768*f4a2713aSLionel Sambuc tok::TokenKind kind = Tok.getKind(); 769*f4a2713aSLionel Sambuc Toks.push_back(Tok); 770*f4a2713aSLionel Sambuc bool IsLParen = (kind == tok::l_paren); 771*f4a2713aSLionel Sambuc SourceLocation OpenLoc = Tok.getLocation(); 772*f4a2713aSLionel Sambuc 773*f4a2713aSLionel Sambuc if (IsLParen) { 774*f4a2713aSLionel Sambuc ConsumeParen(); 775*f4a2713aSLionel Sambuc } else { 776*f4a2713aSLionel Sambuc assert(kind == tok::l_brace && "Must be left paren or brace here."); 777*f4a2713aSLionel Sambuc ConsumeBrace(); 778*f4a2713aSLionel Sambuc // In C++03, this has to be the start of the function body, which 779*f4a2713aSLionel Sambuc // means the initializer is malformed; we'll diagnose it later. 780*f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus11) 781*f4a2713aSLionel Sambuc return false; 782*f4a2713aSLionel Sambuc } 783*f4a2713aSLionel Sambuc 784*f4a2713aSLionel Sambuc // Grab the initializer (or the subexpression of the template argument). 785*f4a2713aSLionel Sambuc // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false 786*f4a2713aSLionel Sambuc // if we might be inside the braces of a lambda-expression. 787*f4a2713aSLionel Sambuc if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace, 788*f4a2713aSLionel Sambuc Toks, /*StopAtSemi=*/true)) { 789*f4a2713aSLionel Sambuc Diag(Tok, IsLParen ? diag::err_expected_rparen : 790*f4a2713aSLionel Sambuc diag::err_expected_rbrace); 791*f4a2713aSLionel Sambuc Diag(OpenLoc, diag::note_matching) << (IsLParen ? "(" : "{"); 792*f4a2713aSLionel Sambuc return true; 793*f4a2713aSLionel Sambuc } 794*f4a2713aSLionel Sambuc 795*f4a2713aSLionel Sambuc // Grab pack ellipsis, if present. 796*f4a2713aSLionel Sambuc if (Tok.is(tok::ellipsis)) { 797*f4a2713aSLionel Sambuc Toks.push_back(Tok); 798*f4a2713aSLionel Sambuc ConsumeToken(); 799*f4a2713aSLionel Sambuc } 800*f4a2713aSLionel Sambuc 801*f4a2713aSLionel Sambuc // If we know we just consumed a mem-initializer, we must have ',' or '{' 802*f4a2713aSLionel Sambuc // next. 803*f4a2713aSLionel Sambuc if (Tok.is(tok::comma)) { 804*f4a2713aSLionel Sambuc Toks.push_back(Tok); 805*f4a2713aSLionel Sambuc ConsumeToken(); 806*f4a2713aSLionel Sambuc } else if (Tok.is(tok::l_brace)) { 807*f4a2713aSLionel Sambuc // This is the function body if the ')' or '}' is immediately followed by 808*f4a2713aSLionel Sambuc // a '{'. That cannot happen within a template argument, apart from the 809*f4a2713aSLionel Sambuc // case where a template argument contains a compound literal: 810*f4a2713aSLionel Sambuc // 811*f4a2713aSLionel Sambuc // S ( ) : a < b < c > ( d ) { } 812*f4a2713aSLionel Sambuc // // End of declaration, or still inside the template argument? 813*f4a2713aSLionel Sambuc // 814*f4a2713aSLionel Sambuc // ... and the case where the template argument contains a lambda: 815*f4a2713aSLionel Sambuc // 816*f4a2713aSLionel Sambuc // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; } 817*f4a2713aSLionel Sambuc // ( ) > ( ) { } 818*f4a2713aSLionel Sambuc // 819*f4a2713aSLionel Sambuc // FIXME: Disambiguate these cases. Note that the latter case is probably 820*f4a2713aSLionel Sambuc // going to be made ill-formed by core issue 1607. 821*f4a2713aSLionel Sambuc Toks.push_back(Tok); 822*f4a2713aSLionel Sambuc ConsumeBrace(); 823*f4a2713aSLionel Sambuc return false; 824*f4a2713aSLionel Sambuc } else if (!MightBeTemplateArgument) { 825*f4a2713aSLionel Sambuc return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma); 826*f4a2713aSLionel Sambuc } 827*f4a2713aSLionel Sambuc } 828*f4a2713aSLionel Sambuc } 829*f4a2713aSLionel Sambuc 830*f4a2713aSLionel Sambuc /// \brief Consume and store tokens from the '?' to the ':' in a conditional 831*f4a2713aSLionel Sambuc /// expression. 832*f4a2713aSLionel Sambuc bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) { 833*f4a2713aSLionel Sambuc // Consume '?'. 834*f4a2713aSLionel Sambuc assert(Tok.is(tok::question)); 835*f4a2713aSLionel Sambuc Toks.push_back(Tok); 836*f4a2713aSLionel Sambuc ConsumeToken(); 837*f4a2713aSLionel Sambuc 838*f4a2713aSLionel Sambuc while (Tok.isNot(tok::colon)) { 839*f4a2713aSLionel Sambuc if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks, /*StopAtSemi*/true, 840*f4a2713aSLionel Sambuc /*ConsumeFinalToken*/false)) 841*f4a2713aSLionel Sambuc return false; 842*f4a2713aSLionel Sambuc 843*f4a2713aSLionel Sambuc // If we found a nested conditional, consume it. 844*f4a2713aSLionel Sambuc if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks)) 845*f4a2713aSLionel Sambuc return false; 846*f4a2713aSLionel Sambuc } 847*f4a2713aSLionel Sambuc 848*f4a2713aSLionel Sambuc // Consume ':'. 849*f4a2713aSLionel Sambuc Toks.push_back(Tok); 850*f4a2713aSLionel Sambuc ConsumeToken(); 851*f4a2713aSLionel Sambuc return true; 852*f4a2713aSLionel Sambuc } 853*f4a2713aSLionel Sambuc 854*f4a2713aSLionel Sambuc /// \brief A tentative parsing action that can also revert token annotations. 855*f4a2713aSLionel Sambuc class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction { 856*f4a2713aSLionel Sambuc public: 857*f4a2713aSLionel Sambuc explicit UnannotatedTentativeParsingAction(Parser &Self, 858*f4a2713aSLionel Sambuc tok::TokenKind EndKind) 859*f4a2713aSLionel Sambuc : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) { 860*f4a2713aSLionel Sambuc // Stash away the old token stream, so we can restore it once the 861*f4a2713aSLionel Sambuc // tentative parse is complete. 862*f4a2713aSLionel Sambuc TentativeParsingAction Inner(Self); 863*f4a2713aSLionel Sambuc Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false); 864*f4a2713aSLionel Sambuc Inner.Revert(); 865*f4a2713aSLionel Sambuc } 866*f4a2713aSLionel Sambuc 867*f4a2713aSLionel Sambuc void RevertAnnotations() { 868*f4a2713aSLionel Sambuc Revert(); 869*f4a2713aSLionel Sambuc 870*f4a2713aSLionel Sambuc // Put back the original tokens. 871*f4a2713aSLionel Sambuc Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch); 872*f4a2713aSLionel Sambuc if (Toks.size()) { 873*f4a2713aSLionel Sambuc Token *Buffer = new Token[Toks.size()]; 874*f4a2713aSLionel Sambuc std::copy(Toks.begin() + 1, Toks.end(), Buffer); 875*f4a2713aSLionel Sambuc Buffer[Toks.size() - 1] = Self.Tok; 876*f4a2713aSLionel Sambuc Self.PP.EnterTokenStream(Buffer, Toks.size(), true, /*Owned*/true); 877*f4a2713aSLionel Sambuc 878*f4a2713aSLionel Sambuc Self.Tok = Toks.front(); 879*f4a2713aSLionel Sambuc } 880*f4a2713aSLionel Sambuc } 881*f4a2713aSLionel Sambuc 882*f4a2713aSLionel Sambuc private: 883*f4a2713aSLionel Sambuc Parser &Self; 884*f4a2713aSLionel Sambuc CachedTokens Toks; 885*f4a2713aSLionel Sambuc tok::TokenKind EndKind; 886*f4a2713aSLionel Sambuc }; 887*f4a2713aSLionel Sambuc 888*f4a2713aSLionel Sambuc /// ConsumeAndStoreInitializer - Consume and store the token at the passed token 889*f4a2713aSLionel Sambuc /// container until the end of the current initializer expression (either a 890*f4a2713aSLionel Sambuc /// default argument or an in-class initializer for a non-static data member). 891*f4a2713aSLionel Sambuc /// The final token is not consumed. 892*f4a2713aSLionel Sambuc bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks, 893*f4a2713aSLionel Sambuc CachedInitKind CIK) { 894*f4a2713aSLionel Sambuc // We always want this function to consume at least one token if not at EOF. 895*f4a2713aSLionel Sambuc bool IsFirstTokenConsumed = true; 896*f4a2713aSLionel Sambuc 897*f4a2713aSLionel Sambuc // Number of possible unclosed <s we've seen so far. These might be templates, 898*f4a2713aSLionel Sambuc // and might not, but if there were none of them (or we know for sure that 899*f4a2713aSLionel Sambuc // we're within a template), we can avoid a tentative parse. 900*f4a2713aSLionel Sambuc unsigned AngleCount = 0; 901*f4a2713aSLionel Sambuc unsigned KnownTemplateCount = 0; 902*f4a2713aSLionel Sambuc 903*f4a2713aSLionel Sambuc while (1) { 904*f4a2713aSLionel Sambuc switch (Tok.getKind()) { 905*f4a2713aSLionel Sambuc case tok::comma: 906*f4a2713aSLionel Sambuc // If we might be in a template, perform a tentative parse to check. 907*f4a2713aSLionel Sambuc if (!AngleCount) 908*f4a2713aSLionel Sambuc // Not a template argument: this is the end of the initializer. 909*f4a2713aSLionel Sambuc return true; 910*f4a2713aSLionel Sambuc if (KnownTemplateCount) 911*f4a2713aSLionel Sambuc goto consume_token; 912*f4a2713aSLionel Sambuc 913*f4a2713aSLionel Sambuc // We hit a comma inside angle brackets. This is the hard case. The 914*f4a2713aSLionel Sambuc // rule we follow is: 915*f4a2713aSLionel Sambuc // * For a default argument, if the tokens after the comma form a 916*f4a2713aSLionel Sambuc // syntactically-valid parameter-declaration-clause, in which each 917*f4a2713aSLionel Sambuc // parameter has an initializer, then this comma ends the default 918*f4a2713aSLionel Sambuc // argument. 919*f4a2713aSLionel Sambuc // * For a default initializer, if the tokens after the comma form a 920*f4a2713aSLionel Sambuc // syntactically-valid init-declarator-list, then this comma ends 921*f4a2713aSLionel Sambuc // the default initializer. 922*f4a2713aSLionel Sambuc { 923*f4a2713aSLionel Sambuc UnannotatedTentativeParsingAction PA(*this, 924*f4a2713aSLionel Sambuc CIK == CIK_DefaultInitializer 925*f4a2713aSLionel Sambuc ? tok::semi : tok::r_paren); 926*f4a2713aSLionel Sambuc Sema::TentativeAnalysisScope Scope(Actions); 927*f4a2713aSLionel Sambuc 928*f4a2713aSLionel Sambuc TPResult Result = TPResult::Error(); 929*f4a2713aSLionel Sambuc ConsumeToken(); 930*f4a2713aSLionel Sambuc switch (CIK) { 931*f4a2713aSLionel Sambuc case CIK_DefaultInitializer: 932*f4a2713aSLionel Sambuc Result = TryParseInitDeclaratorList(); 933*f4a2713aSLionel Sambuc // If we parsed a complete, ambiguous init-declarator-list, this 934*f4a2713aSLionel Sambuc // is only syntactically-valid if it's followed by a semicolon. 935*f4a2713aSLionel Sambuc if (Result == TPResult::Ambiguous() && Tok.isNot(tok::semi)) 936*f4a2713aSLionel Sambuc Result = TPResult::False(); 937*f4a2713aSLionel Sambuc break; 938*f4a2713aSLionel Sambuc 939*f4a2713aSLionel Sambuc case CIK_DefaultArgument: 940*f4a2713aSLionel Sambuc bool InvalidAsDeclaration = false; 941*f4a2713aSLionel Sambuc Result = TryParseParameterDeclarationClause( 942*f4a2713aSLionel Sambuc &InvalidAsDeclaration, /*VersusTemplateArgument*/true); 943*f4a2713aSLionel Sambuc // If this is an expression or a declaration with a missing 944*f4a2713aSLionel Sambuc // 'typename', assume it's not a declaration. 945*f4a2713aSLionel Sambuc if (Result == TPResult::Ambiguous() && InvalidAsDeclaration) 946*f4a2713aSLionel Sambuc Result = TPResult::False(); 947*f4a2713aSLionel Sambuc break; 948*f4a2713aSLionel Sambuc } 949*f4a2713aSLionel Sambuc 950*f4a2713aSLionel Sambuc // If what follows could be a declaration, it is a declaration. 951*f4a2713aSLionel Sambuc if (Result != TPResult::False() && Result != TPResult::Error()) { 952*f4a2713aSLionel Sambuc PA.Revert(); 953*f4a2713aSLionel Sambuc return true; 954*f4a2713aSLionel Sambuc } 955*f4a2713aSLionel Sambuc 956*f4a2713aSLionel Sambuc // In the uncommon case that we decide the following tokens are part 957*f4a2713aSLionel Sambuc // of a template argument, revert any annotations we've performed in 958*f4a2713aSLionel Sambuc // those tokens. We're not going to look them up until we've parsed 959*f4a2713aSLionel Sambuc // the rest of the class, and that might add more declarations. 960*f4a2713aSLionel Sambuc PA.RevertAnnotations(); 961*f4a2713aSLionel Sambuc } 962*f4a2713aSLionel Sambuc 963*f4a2713aSLionel Sambuc // Keep going. We know we're inside a template argument list now. 964*f4a2713aSLionel Sambuc ++KnownTemplateCount; 965*f4a2713aSLionel Sambuc goto consume_token; 966*f4a2713aSLionel Sambuc 967*f4a2713aSLionel Sambuc case tok::eof: 968*f4a2713aSLionel Sambuc // Ran out of tokens. 969*f4a2713aSLionel Sambuc return false; 970*f4a2713aSLionel Sambuc 971*f4a2713aSLionel Sambuc case tok::less: 972*f4a2713aSLionel Sambuc // FIXME: A '<' can only start a template-id if it's preceded by an 973*f4a2713aSLionel Sambuc // identifier, an operator-function-id, or a literal-operator-id. 974*f4a2713aSLionel Sambuc ++AngleCount; 975*f4a2713aSLionel Sambuc goto consume_token; 976*f4a2713aSLionel Sambuc 977*f4a2713aSLionel Sambuc case tok::question: 978*f4a2713aSLionel Sambuc // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does, 979*f4a2713aSLionel Sambuc // that is *never* the end of the initializer. Skip to the ':'. 980*f4a2713aSLionel Sambuc if (!ConsumeAndStoreConditional(Toks)) 981*f4a2713aSLionel Sambuc return false; 982*f4a2713aSLionel Sambuc break; 983*f4a2713aSLionel Sambuc 984*f4a2713aSLionel Sambuc case tok::greatergreatergreater: 985*f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus11) 986*f4a2713aSLionel Sambuc goto consume_token; 987*f4a2713aSLionel Sambuc if (AngleCount) --AngleCount; 988*f4a2713aSLionel Sambuc if (KnownTemplateCount) --KnownTemplateCount; 989*f4a2713aSLionel Sambuc // Fall through. 990*f4a2713aSLionel Sambuc case tok::greatergreater: 991*f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus11) 992*f4a2713aSLionel Sambuc goto consume_token; 993*f4a2713aSLionel Sambuc if (AngleCount) --AngleCount; 994*f4a2713aSLionel Sambuc if (KnownTemplateCount) --KnownTemplateCount; 995*f4a2713aSLionel Sambuc // Fall through. 996*f4a2713aSLionel Sambuc case tok::greater: 997*f4a2713aSLionel Sambuc if (AngleCount) --AngleCount; 998*f4a2713aSLionel Sambuc if (KnownTemplateCount) --KnownTemplateCount; 999*f4a2713aSLionel Sambuc goto consume_token; 1000*f4a2713aSLionel Sambuc 1001*f4a2713aSLionel Sambuc case tok::kw_template: 1002*f4a2713aSLionel Sambuc // 'template' identifier '<' is known to start a template argument list, 1003*f4a2713aSLionel Sambuc // and can be used to disambiguate the parse. 1004*f4a2713aSLionel Sambuc // FIXME: Support all forms of 'template' unqualified-id '<'. 1005*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1006*f4a2713aSLionel Sambuc ConsumeToken(); 1007*f4a2713aSLionel Sambuc if (Tok.is(tok::identifier)) { 1008*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1009*f4a2713aSLionel Sambuc ConsumeToken(); 1010*f4a2713aSLionel Sambuc if (Tok.is(tok::less)) { 1011*f4a2713aSLionel Sambuc ++KnownTemplateCount; 1012*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1013*f4a2713aSLionel Sambuc ConsumeToken(); 1014*f4a2713aSLionel Sambuc } 1015*f4a2713aSLionel Sambuc } 1016*f4a2713aSLionel Sambuc break; 1017*f4a2713aSLionel Sambuc 1018*f4a2713aSLionel Sambuc case tok::kw_operator: 1019*f4a2713aSLionel Sambuc // If 'operator' precedes other punctuation, that punctuation loses 1020*f4a2713aSLionel Sambuc // its special behavior. 1021*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1022*f4a2713aSLionel Sambuc ConsumeToken(); 1023*f4a2713aSLionel Sambuc switch (Tok.getKind()) { 1024*f4a2713aSLionel Sambuc case tok::comma: 1025*f4a2713aSLionel Sambuc case tok::greatergreatergreater: 1026*f4a2713aSLionel Sambuc case tok::greatergreater: 1027*f4a2713aSLionel Sambuc case tok::greater: 1028*f4a2713aSLionel Sambuc case tok::less: 1029*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1030*f4a2713aSLionel Sambuc ConsumeToken(); 1031*f4a2713aSLionel Sambuc break; 1032*f4a2713aSLionel Sambuc default: 1033*f4a2713aSLionel Sambuc break; 1034*f4a2713aSLionel Sambuc } 1035*f4a2713aSLionel Sambuc break; 1036*f4a2713aSLionel Sambuc 1037*f4a2713aSLionel Sambuc case tok::l_paren: 1038*f4a2713aSLionel Sambuc // Recursively consume properly-nested parens. 1039*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1040*f4a2713aSLionel Sambuc ConsumeParen(); 1041*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false); 1042*f4a2713aSLionel Sambuc break; 1043*f4a2713aSLionel Sambuc case tok::l_square: 1044*f4a2713aSLionel Sambuc // Recursively consume properly-nested square brackets. 1045*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1046*f4a2713aSLionel Sambuc ConsumeBracket(); 1047*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false); 1048*f4a2713aSLionel Sambuc break; 1049*f4a2713aSLionel Sambuc case tok::l_brace: 1050*f4a2713aSLionel Sambuc // Recursively consume properly-nested braces. 1051*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1052*f4a2713aSLionel Sambuc ConsumeBrace(); 1053*f4a2713aSLionel Sambuc ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false); 1054*f4a2713aSLionel Sambuc break; 1055*f4a2713aSLionel Sambuc 1056*f4a2713aSLionel Sambuc // Okay, we found a ']' or '}' or ')', which we think should be balanced. 1057*f4a2713aSLionel Sambuc // Since the user wasn't looking for this token (if they were, it would 1058*f4a2713aSLionel Sambuc // already be handled), this isn't balanced. If there is a LHS token at a 1059*f4a2713aSLionel Sambuc // higher level, we will assume that this matches the unbalanced token 1060*f4a2713aSLionel Sambuc // and return it. Otherwise, this is a spurious RHS token, which we skip. 1061*f4a2713aSLionel Sambuc case tok::r_paren: 1062*f4a2713aSLionel Sambuc if (CIK == CIK_DefaultArgument) 1063*f4a2713aSLionel Sambuc return true; // End of the default argument. 1064*f4a2713aSLionel Sambuc if (ParenCount && !IsFirstTokenConsumed) 1065*f4a2713aSLionel Sambuc return false; // Matches something. 1066*f4a2713aSLionel Sambuc goto consume_token; 1067*f4a2713aSLionel Sambuc case tok::r_square: 1068*f4a2713aSLionel Sambuc if (BracketCount && !IsFirstTokenConsumed) 1069*f4a2713aSLionel Sambuc return false; // Matches something. 1070*f4a2713aSLionel Sambuc goto consume_token; 1071*f4a2713aSLionel Sambuc case tok::r_brace: 1072*f4a2713aSLionel Sambuc if (BraceCount && !IsFirstTokenConsumed) 1073*f4a2713aSLionel Sambuc return false; // Matches something. 1074*f4a2713aSLionel Sambuc goto consume_token; 1075*f4a2713aSLionel Sambuc 1076*f4a2713aSLionel Sambuc case tok::code_completion: 1077*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1078*f4a2713aSLionel Sambuc ConsumeCodeCompletionToken(); 1079*f4a2713aSLionel Sambuc break; 1080*f4a2713aSLionel Sambuc 1081*f4a2713aSLionel Sambuc case tok::string_literal: 1082*f4a2713aSLionel Sambuc case tok::wide_string_literal: 1083*f4a2713aSLionel Sambuc case tok::utf8_string_literal: 1084*f4a2713aSLionel Sambuc case tok::utf16_string_literal: 1085*f4a2713aSLionel Sambuc case tok::utf32_string_literal: 1086*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1087*f4a2713aSLionel Sambuc ConsumeStringToken(); 1088*f4a2713aSLionel Sambuc break; 1089*f4a2713aSLionel Sambuc case tok::semi: 1090*f4a2713aSLionel Sambuc if (CIK == CIK_DefaultInitializer) 1091*f4a2713aSLionel Sambuc return true; // End of the default initializer. 1092*f4a2713aSLionel Sambuc // FALL THROUGH. 1093*f4a2713aSLionel Sambuc default: 1094*f4a2713aSLionel Sambuc consume_token: 1095*f4a2713aSLionel Sambuc Toks.push_back(Tok); 1096*f4a2713aSLionel Sambuc ConsumeToken(); 1097*f4a2713aSLionel Sambuc break; 1098*f4a2713aSLionel Sambuc } 1099*f4a2713aSLionel Sambuc IsFirstTokenConsumed = false; 1100*f4a2713aSLionel Sambuc } 1101*f4a2713aSLionel Sambuc } 1102