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