blob: 5fc4189742cf1f3460fe66e6224f16d99f9fabb5 [file] [log] [blame]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001//===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements parsing for C++ class inline methods.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
16#include "clang/AST/DeclTemplate.h"
17#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
19#include "clang/Sema/Scope.h"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000020using namespace clang;
21
John McCalle34db6b2013-03-14 05:13:41 +000022/// Get the FunctionDecl for a function or function template decl.
23static FunctionDecl *getFunctionDecl(Decl *D) {
24 if (FunctionDecl *fn = dyn_cast<FunctionDecl>(D))
25 return fn;
26 return cast<FunctionTemplateDecl>(D)->getTemplatedDecl();
27}
28
Sebastian Redld3a413d2009-04-26 20:35:05 +000029/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000030/// Declarator is a well formed C++ inline method definition. Now lex its body
31/// and store its tokens for parsing after the C++ class is complete.
Rafael Espindola0777cf52013-01-08 21:00:12 +000032NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000033 AttributeList *AccessAttrs,
34 ParsingDeclarator &D,
Douglas Gregor45fa5602011-11-07 20:56:01 +000035 const ParsedTemplateInfo &TemplateInfo,
36 const VirtSpecifiers& VS,
37 FunctionDefinitionKind DefinitionKind,
38 ExprResult& Init) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000039 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sean Hunte4246a62011-05-12 06:15:49 +000040 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
41 Tok.is(tok::equal)) &&
42 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000043
Benjamin Kramer5354e772012-08-23 23:38:35 +000044 MultiTemplateParamsArg TemplateParams(
Sean Hunt4cd84942010-04-14 23:07:37 +000045 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
46 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
47
Rafael Espindola0777cf52013-01-08 21:00:12 +000048 NamedDecl *FnD;
Douglas Gregor45fa5602011-11-07 20:56:01 +000049 D.setFunctionDefinitionKind(DefinitionKind);
John McCall67d1a672009-08-06 02:15:43 +000050 if (D.getDeclSpec().isFriendSpecified())
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +000051 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +000052 TemplateParams);
Douglas Gregor147545d2011-10-10 14:49:18 +000053 else {
Douglas Gregor23c94db2010-07-02 17:43:08 +000054 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +000055 TemplateParams, 0,
Richard Smithca523302012-06-10 03:12:00 +000056 VS, ICIS_NoInit);
Douglas Gregor147545d2011-10-10 14:49:18 +000057 if (FnD) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000058 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
59 false, true);
Richard Smitha2c36462013-04-26 16:15:35 +000060 bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType();
Douglas Gregora2b4e5d2011-10-17 17:09:53 +000061 if (Init.isUsable())
Douglas Gregor147545d2011-10-10 14:49:18 +000062 Actions.AddInitializerToDecl(FnD, Init.get(), false,
63 TypeSpecContainsAuto);
64 else
65 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
66 }
Nico Weber48673472011-01-28 06:07:34 +000067 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000068
Douglas Gregor74e2fc32012-04-16 18:27:27 +000069 HandleMemberFunctionDeclDelays(D, FnD);
Eli Friedmand33133c2009-07-22 21:45:50 +000070
John McCalleee1d542011-02-14 07:13:47 +000071 D.complete(FnD);
72
Sean Hunte4246a62011-05-12 06:15:49 +000073 if (Tok.is(tok::equal)) {
74 ConsumeToken();
75
Richard Smithc430ef42011-11-10 09:08:44 +000076 if (!FnD) {
77 SkipUntil(tok::semi);
78 return 0;
79 }
80
Sean Hunte4246a62011-05-12 06:15:49 +000081 bool Delete = false;
82 SourceLocation KWLoc;
83 if (Tok.is(tok::kw_delete)) {
Richard Smith80ad52f2013-01-02 11:42:31 +000084 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +000085 diag::warn_cxx98_compat_deleted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000086 diag::ext_deleted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000087
88 KWLoc = ConsumeToken();
89 Actions.SetDeclDeleted(FnD, KWLoc);
90 Delete = true;
91 } else if (Tok.is(tok::kw_default)) {
Richard Smith80ad52f2013-01-02 11:42:31 +000092 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +000093 diag::warn_cxx98_compat_defaulted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000094 diag::ext_defaulted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000095
96 KWLoc = ConsumeToken();
97 Actions.SetDeclDefaulted(FnD, KWLoc);
98 } else {
99 llvm_unreachable("function definition after = not 'delete' or 'default'");
100 }
101
102 if (Tok.is(tok::comma)) {
103 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
104 << Delete;
105 SkipUntil(tok::semi);
106 } else {
107 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
108 Delete ? "delete" : "default", tok::semi);
109 }
110
111 return FnD;
112 }
113
Francois Pichet8387e2a2011-04-22 22:18:13 +0000114 // In delayed template parsing mode, if we are within a class template
115 // or if we are about to parse function member template then consume
116 // the tokens and store them for parsing at the end of the translation unit.
David Blaikie4e4d0842012-03-11 07:00:24 +0000117 if (getLangOpts().DelayedTemplateParsing &&
Douglas Gregor09630172012-06-28 21:43:01 +0000118 DefinitionKind == FDK_Definition &&
Francois Pichet8387e2a2011-04-22 22:18:13 +0000119 ((Actions.CurContext->isDependentContext() ||
120 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
Francois Pichet9d38dbc2011-11-18 23:47:17 +0000121 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
Francois Pichet8387e2a2011-04-22 22:18:13 +0000122
123 if (FnD) {
Francois Pichete1fca502011-12-08 09:11:52 +0000124 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000125
John McCalle34db6b2013-03-14 05:13:41 +0000126 FunctionDecl *FD = getFunctionDecl(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +0000127 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000128
129 LateParsedTemplateMap[FD] = LPT;
130 Actions.MarkAsLateParsedTemplate(FD);
131 LexTemplateFunctionForLateParsing(LPT->Toks);
132 } else {
133 CachedTokens Toks;
134 LexTemplateFunctionForLateParsing(Toks);
135 }
136
137 return FnD;
138 }
139
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000140 // Consume the tokens and store them for later parsing.
141
Douglas Gregord54eb442010-10-12 16:25:54 +0000142 LexedMethod* LM = new LexedMethod(this, FnD);
143 getCurrentClass().LateParsedDeclarations.push_back(LM);
144 LM->TemplateScope = getCurScope()->isTemplateParamScope();
145 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000146
Sebastian Redld3a413d2009-04-26 20:35:05 +0000147 tok::TokenKind kind = Tok.getKind();
Sebastian Redla891a322011-09-30 08:32:17 +0000148 // Consume everything up to (and including) the left brace of the
149 // function body.
150 if (ConsumeAndStoreFunctionPrologue(Toks)) {
151 // We didn't find the left-brace we expected after the
Eli Friedmane9ee3822012-02-22 04:49:04 +0000152 // constructor initializer; we already printed an error, and it's likely
153 // impossible to recover, so don't try to parse this method later.
154 // If we stopped at a semicolon, consume it to avoid an extra warning.
155 if (Tok.is(tok::semi))
156 ConsumeToken();
157 delete getCurrentClass().LateParsedDeclarations.back();
158 getCurrentClass().LateParsedDeclarations.pop_back();
159 return FnD;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000160 } else {
Sebastian Redla891a322011-09-30 08:32:17 +0000161 // Consume everything up to (and including) the matching right brace.
162 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000163 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000164
Sebastian Redld3a413d2009-04-26 20:35:05 +0000165 // If we're in a function-try-block, we need to store all the catch blocks.
166 if (kind == tok::kw_try) {
167 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000168 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
169 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000170 }
171 }
172
Douglas Gregor87f10642011-04-14 23:19:27 +0000173
174 if (!FnD) {
175 // If semantic analysis could not build a function declaration,
176 // just throw away the late-parsed declaration.
177 delete getCurrentClass().LateParsedDeclarations.back();
178 getCurrentClass().LateParsedDeclarations.pop_back();
179 }
180
John McCalle34db6b2013-03-14 05:13:41 +0000181 // If this is a friend function, mark that it's late-parsed so that
182 // it's still known to be a definition even before we attach the
183 // parsed body. Sema needs to treat friend function definitions
184 // differently during template instantiation, and it's possible for
185 // the containing class to be instantiated before all its member
186 // function definitions are parsed.
187 //
188 // If you remove this, you can remove the code that clears the flag
189 // after parsing the member.
190 if (D.getDeclSpec().isFriendSpecified()) {
191 getFunctionDecl(FnD)->setLateTemplateParsed(true);
192 }
193
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000194 return FnD;
195}
196
Richard Smith7a614d82011-06-11 17:19:42 +0000197/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
198/// specified Declarator is a well formed C++ non-static data member
199/// declaration. Now lex its initializer and store its tokens for parsing
200/// after the class is complete.
201void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
202 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
203 "Current token not a '{' or '='!");
204
205 LateParsedMemberInitializer *MI =
206 new LateParsedMemberInitializer(this, VarD);
207 getCurrentClass().LateParsedDeclarations.push_back(MI);
208 CachedTokens &Toks = MI->Toks;
209
210 tok::TokenKind kind = Tok.getKind();
211 if (kind == tok::equal) {
212 Toks.push_back(Tok);
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000213 ConsumeToken();
Richard Smith7a614d82011-06-11 17:19:42 +0000214 }
215
216 if (kind == tok::l_brace) {
217 // Begin by storing the '{' token.
218 Toks.push_back(Tok);
219 ConsumeBrace();
220
221 // Consume everything up to (and including) the matching right brace.
222 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
223 } else {
224 // Consume everything up to (but excluding) the comma or semicolon.
225 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
226 /*ConsumeFinalToken=*/false);
227 }
228
229 // Store an artificial EOF token to ensure that we don't run off the end of
230 // the initializer when we come to parse it.
231 Token Eof;
232 Eof.startToken();
233 Eof.setKind(tok::eof);
234 Eof.setLocation(Tok.getLocation());
235 Toks.push_back(Eof);
236}
237
Douglas Gregord54eb442010-10-12 16:25:54 +0000238Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
239void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000240void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000241void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
242
243Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
244 : Self(P), Class(C) {}
245
246Parser::LateParsedClass::~LateParsedClass() {
247 Self->DeallocateParsedClasses(Class);
248}
249
250void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
251 Self->ParseLexedMethodDeclarations(*Class);
252}
253
Richard Smith7a614d82011-06-11 17:19:42 +0000254void Parser::LateParsedClass::ParseLexedMemberInitializers() {
255 Self->ParseLexedMemberInitializers(*Class);
256}
257
Douglas Gregord54eb442010-10-12 16:25:54 +0000258void Parser::LateParsedClass::ParseLexedMethodDefs() {
259 Self->ParseLexedMethodDefs(*Class);
260}
261
262void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
263 Self->ParseLexedMethodDeclaration(*this);
264}
265
266void Parser::LexedMethod::ParseLexedMethodDefs() {
267 Self->ParseLexedMethodDef(*this);
268}
269
Richard Smith7a614d82011-06-11 17:19:42 +0000270void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
271 Self->ParseLexedMemberInitializer(*this);
272}
273
Douglas Gregor72b505b2008-12-16 21:30:33 +0000274/// ParseLexedMethodDeclarations - We finished parsing the member
275/// specification of a top (non-nested) C++ class. Now go over the
276/// stack of method declarations with some parts for which parsing was
277/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000278void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
279 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000280 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Richard Smith098b8142013-04-29 11:55:38 +0000281 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
282 if (HasTemplateScope) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000283 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Richard Smith098b8142013-04-29 11:55:38 +0000284 ++CurTemplateDepthTracker;
285 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000286
John McCall7a1dc562009-12-19 10:49:29 +0000287 // The current scope is still active if we're the top-level class.
288 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000289 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000290 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
291 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000292 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000293 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000294
Douglas Gregord54eb442010-10-12 16:25:54 +0000295 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
296 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000297 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000298
John McCall7a1dc562009-12-19 10:49:29 +0000299 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000300 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000301}
302
Douglas Gregord54eb442010-10-12 16:25:54 +0000303void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
304 // If this is a member template, introduce the template parameter scope.
305 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
Richard Smith098b8142013-04-29 11:55:38 +0000306 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
307 if (LM.TemplateScope) {
Douglas Gregord54eb442010-10-12 16:25:54 +0000308 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
Richard Smith098b8142013-04-29 11:55:38 +0000309 ++CurTemplateDepthTracker;
310 }
Douglas Gregord54eb442010-10-12 16:25:54 +0000311 // Start the delayed C++ method declaration
312 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
313
314 // Introduce the parameters into scope and parse their default
315 // arguments.
Richard Smith3a2b7a12013-01-28 22:42:45 +0000316 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
317 Scope::FunctionDeclarationScope | Scope::DeclScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000318 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
319 // Introduce the parameter into scope.
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000320 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
321 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000322
323 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
324 // Save the current token position.
325 SourceLocation origLoc = Tok.getLocation();
326
327 // Parse the default argument from its saved token stream.
328 Toks->push_back(Tok); // So that the current token doesn't get lost
329 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
330
331 // Consume the previously-pushed token.
332 ConsumeAnyToken();
333
334 // Consume the '='.
335 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
336 SourceLocation EqualLoc = ConsumeToken();
337
338 // The argument isn't actually potentially evaluated unless it is
339 // used.
340 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000341 Sema::PotentiallyEvaluatedIfUsed,
342 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000343
Sebastian Redl84407ba2012-03-14 15:54:00 +0000344 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +0000345 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redlca893712012-03-20 21:24:03 +0000346 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +0000347 DefArgResult = ParseBraceInitializer();
Sebastian Redlca893712012-03-20 21:24:03 +0000348 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +0000349 DefArgResult = ParseAssignmentExpression();
Douglas Gregord54eb442010-10-12 16:25:54 +0000350 if (DefArgResult.isInvalid())
351 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
352 else {
353 if (Tok.is(tok::cxx_defaultarg_end))
354 ConsumeToken();
355 else
356 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
357 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
358 DefArgResult.take());
359 }
360
361 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
362 Tok.getLocation()) &&
363 "ParseAssignmentExpression went over the default arg tokens!");
364 // There could be leftover tokens (e.g. because of an error).
365 // Skip through until we reach the original token position.
366 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
367 ConsumeAnyToken();
368
369 delete Toks;
370 LM.DefaultArgs[I].Toks = 0;
371 }
372 }
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000373
Douglas Gregord54eb442010-10-12 16:25:54 +0000374 PrototypeScope.Exit();
375
376 // Finish the delayed C++ method declaration.
377 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
378}
379
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000380/// ParseLexedMethodDefs - We finished parsing the member specification of a top
381/// (non-nested) C++ class. Now go over the stack of lexed methods that were
382/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000383void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
384 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000385 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Richard Smith098b8142013-04-29 11:55:38 +0000386 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
387 if (HasTemplateScope) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000388 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Richard Smith098b8142013-04-29 11:55:38 +0000389 ++CurTemplateDepthTracker;
390 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000391 bool HasClassScope = !Class.TopLevelClass;
392 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
393 HasClassScope);
394
Douglas Gregord54eb442010-10-12 16:25:54 +0000395 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
396 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
397 }
398}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000399
Douglas Gregord54eb442010-10-12 16:25:54 +0000400void Parser::ParseLexedMethodDef(LexedMethod &LM) {
401 // If this is a member template, introduce the template parameter scope.
402 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
Richard Smith098b8142013-04-29 11:55:38 +0000403 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
404 if (LM.TemplateScope) {
Douglas Gregord54eb442010-10-12 16:25:54 +0000405 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Richard Smith098b8142013-04-29 11:55:38 +0000406 ++CurTemplateDepthTracker;
407 }
Douglas Gregord54eb442010-10-12 16:25:54 +0000408 // Save the current token position.
409 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000410
Douglas Gregord54eb442010-10-12 16:25:54 +0000411 assert(!LM.Toks.empty() && "Empty body!");
412 // Append the current token at the end of the new token stream so that it
413 // doesn't get lost.
414 LM.Toks.push_back(Tok);
415 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000416
Douglas Gregord54eb442010-10-12 16:25:54 +0000417 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +0000418 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Douglas Gregord54eb442010-10-12 16:25:54 +0000419 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
420 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000421
Douglas Gregord54eb442010-10-12 16:25:54 +0000422 // Parse the method body. Function body parsing code is similar enough
423 // to be re-used for method bodies as well.
424 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
425 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000426
Douglas Gregord54eb442010-10-12 16:25:54 +0000427 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000428 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000429 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
430 Tok.getLocation()) &&
431 "ParseFunctionTryBlock went over the cached tokens!");
432 // There could be leftover tokens (e.g. because of an error).
433 // Skip through until we reach the original token position.
434 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
435 ConsumeAnyToken();
436 return;
437 }
438 if (Tok.is(tok::colon)) {
439 ParseConstructorInitializer(LM.D);
440
441 // Error recovery.
442 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000443 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000444 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000445 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
446 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000447 return;
448 }
449 } else
450 Actions.ActOnDefaultCtorInitializers(LM.D);
451
Richard Smith098b8142013-04-29 11:55:38 +0000452 assert((Actions.getDiagnostics().hasErrorOccurred() ||
453 !isa<FunctionTemplateDecl>(LM.D) ||
454 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
455 < TemplateParameterDepth) &&
456 "TemplateParameterDepth should be greater than the depth of "
457 "current template being instantiated!");
458
Douglas Gregorc9977d02011-03-16 17:05:57 +0000459 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000460
John McCalle34db6b2013-03-14 05:13:41 +0000461 // Clear the late-template-parsed bit if we set it before.
462 if (LM.D) getFunctionDecl(LM.D)->setLateTemplateParsed(false);
463
Douglas Gregord54eb442010-10-12 16:25:54 +0000464 if (Tok.getLocation() != origLoc) {
465 // Due to parsing error, we either went over the cached tokens or
466 // there are still cached tokens left. If it's the latter case skip the
467 // leftover tokens.
468 // Since this is an uncommon situation that should be avoided, use the
469 // expensive isBeforeInTranslationUnit call.
470 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
471 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000472 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000473 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000474 }
475}
476
Richard Smith7a614d82011-06-11 17:19:42 +0000477/// ParseLexedMemberInitializers - We finished parsing the member specification
478/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
479/// initializers that were collected during its parsing and parse them all.
480void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
481 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
482 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
483 HasTemplateScope);
Richard Smith098b8142013-04-29 11:55:38 +0000484 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
485 if (HasTemplateScope) {
Richard Smith7a614d82011-06-11 17:19:42 +0000486 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Richard Smith098b8142013-04-29 11:55:38 +0000487 ++CurTemplateDepthTracker;
488 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000489 // Set or update the scope flags.
Richard Smith7a614d82011-06-11 17:19:42 +0000490 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000491 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Richard Smith7a614d82011-06-11 17:19:42 +0000492 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
493 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
494
495 if (!AlreadyHasClassScope)
496 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
497 Class.TagOrTemplate);
498
Benjamin Kramer268efba2012-05-17 12:01:52 +0000499 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000500 // C++11 [expr.prim.general]p4:
501 // Otherwise, if a member-declarator declares a non-static data member
502 // (9.2) of a class X, the expression this is a prvalue of type "pointer
503 // to X" within the optional brace-or-equal-initializer. It shall not
504 // appear elsewhere in the member-declarator.
505 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
506 /*TypeQuals=*/(unsigned)0);
Richard Smith7a614d82011-06-11 17:19:42 +0000507
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000508 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
509 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
510 }
511 }
512
Richard Smith7a614d82011-06-11 17:19:42 +0000513 if (!AlreadyHasClassScope)
514 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
515 Class.TagOrTemplate);
516
517 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
518}
519
520void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000521 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000522 return;
523
524 // Append the current token at the end of the new token stream so that it
525 // doesn't get lost.
526 MI.Toks.push_back(Tok);
527 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
528
529 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +0000530 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Richard Smith7a614d82011-06-11 17:19:42 +0000531
532 SourceLocation EqualLoc;
Richard Smithca523302012-06-10 03:12:00 +0000533
Douglas Gregor552e2992012-02-21 02:22:07 +0000534 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
535 EqualLoc);
Richard Smith7a614d82011-06-11 17:19:42 +0000536
537 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
538
539 // The next token should be our artificial terminating EOF token.
540 if (Tok.isNot(tok::eof)) {
541 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
542 if (!EndLoc.isValid())
543 EndLoc = Tok.getLocation();
544 // No fixit; we can't recover as if there were a semicolon here.
545 Diag(EndLoc, diag::err_expected_semi_decl_list);
546
547 // Consume tokens until we hit the artificial EOF.
548 while (Tok.isNot(tok::eof))
549 ConsumeAnyToken();
550 }
551 ConsumeAnyToken();
552}
553
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000554/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000555/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000556/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000557/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000558/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000559/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000560bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
561 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000562 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000563 // We always want this function to consume at least one token if the first
564 // token isn't T and if not at EOF.
565 bool isFirstTokenConsumed = true;
566 while (1) {
567 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000568 if (Tok.is(T1) || Tok.is(T2)) {
569 if (ConsumeFinalToken) {
570 Toks.push_back(Tok);
571 ConsumeAnyToken();
572 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000573 return true;
574 }
575
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000576 switch (Tok.getKind()) {
577 case tok::eof:
578 // Ran out of tokens.
579 return false;
580
581 case tok::l_paren:
582 // Recursively consume properly-nested parens.
583 Toks.push_back(Tok);
584 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000585 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000586 break;
587 case tok::l_square:
588 // Recursively consume properly-nested square brackets.
589 Toks.push_back(Tok);
590 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000591 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000592 break;
593 case tok::l_brace:
594 // Recursively consume properly-nested braces.
595 Toks.push_back(Tok);
596 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000597 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000598 break;
599
600 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
601 // Since the user wasn't looking for this token (if they were, it would
602 // already be handled), this isn't balanced. If there is a LHS token at a
603 // higher level, we will assume that this matches the unbalanced token
604 // and return it. Otherwise, this is a spurious RHS token, which we skip.
605 case tok::r_paren:
606 if (ParenCount && !isFirstTokenConsumed)
607 return false; // Matches something.
608 Toks.push_back(Tok);
609 ConsumeParen();
610 break;
611 case tok::r_square:
612 if (BracketCount && !isFirstTokenConsumed)
613 return false; // Matches something.
614 Toks.push_back(Tok);
615 ConsumeBracket();
616 break;
617 case tok::r_brace:
618 if (BraceCount && !isFirstTokenConsumed)
619 return false; // Matches something.
620 Toks.push_back(Tok);
621 ConsumeBrace();
622 break;
623
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000624 case tok::code_completion:
625 Toks.push_back(Tok);
626 ConsumeCodeCompletionToken();
627 break;
628
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000629 case tok::string_literal:
630 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000631 case tok::utf8_string_literal:
632 case tok::utf16_string_literal:
633 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000634 Toks.push_back(Tok);
635 ConsumeStringToken();
636 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000637 case tok::semi:
638 if (StopAtSemi)
639 return false;
640 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000641 default:
642 // consume this token.
643 Toks.push_back(Tok);
644 ConsumeToken();
645 break;
646 }
647 isFirstTokenConsumed = false;
648 }
649}
Sebastian Redl6df65482011-09-24 17:48:25 +0000650
651/// \brief Consume tokens and store them in the passed token container until
652/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redla891a322011-09-30 08:32:17 +0000653/// the opening brace of the function body. The opening brace will be consumed
654/// if and only if there was no error.
Sebastian Redl6df65482011-09-24 17:48:25 +0000655///
Sebastian Redla891a322011-09-30 08:32:17 +0000656/// \return True on error.
657bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000658 if (Tok.is(tok::kw_try)) {
659 Toks.push_back(Tok);
660 ConsumeToken();
661 }
Eli Friedmane9ee3822012-02-22 04:49:04 +0000662 bool ReadInitializer = false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000663 if (Tok.is(tok::colon)) {
664 // Initializers can contain braces too.
665 Toks.push_back(Tok);
666 ConsumeToken();
667
668 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
669 if (Tok.is(tok::eof) || Tok.is(tok::semi))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000670 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
Sebastian Redl6df65482011-09-24 17:48:25 +0000671
672 // Grab the identifier.
673 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
674 /*StopAtSemi=*/true,
675 /*ConsumeFinalToken=*/false))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000676 return Diag(Tok.getLocation(), diag::err_expected_lparen);
Sebastian Redl6df65482011-09-24 17:48:25 +0000677
678 tok::TokenKind kind = Tok.getKind();
679 Toks.push_back(Tok);
Eli Friedmane9ee3822012-02-22 04:49:04 +0000680 bool IsLParen = (kind == tok::l_paren);
681 SourceLocation LOpen = Tok.getLocation();
682
683 if (IsLParen) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000684 ConsumeParen();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000685 } else {
Sebastian Redl6df65482011-09-24 17:48:25 +0000686 assert(kind == tok::l_brace && "Must be left paren or brace here.");
687 ConsumeBrace();
Sebastian Redla891a322011-09-30 08:32:17 +0000688 // In C++03, this has to be the start of the function body, which
Eli Friedmane9ee3822012-02-22 04:49:04 +0000689 // means the initializer is malformed; we'll diagnose it later.
Richard Smith80ad52f2013-01-02 11:42:31 +0000690 if (!getLangOpts().CPlusPlus11)
Sebastian Redla891a322011-09-30 08:32:17 +0000691 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000692 }
693
694 // Grab the initializer
Eli Friedmane9ee3822012-02-22 04:49:04 +0000695 if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
696 Toks, /*StopAtSemi=*/true)) {
697 Diag(Tok, IsLParen ? diag::err_expected_rparen :
698 diag::err_expected_rbrace);
699 Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
Sebastian Redl6df65482011-09-24 17:48:25 +0000700 return true;
Eli Friedmane9ee3822012-02-22 04:49:04 +0000701 }
702
703 // Grab pack ellipsis, if present
704 if (Tok.is(tok::ellipsis)) {
705 Toks.push_back(Tok);
706 ConsumeToken();
707 }
Sebastian Redla891a322011-09-30 08:32:17 +0000708
709 // Grab the separating comma, if any.
710 if (Tok.is(tok::comma)) {
711 Toks.push_back(Tok);
712 ConsumeToken();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000713 } else if (Tok.isNot(tok::l_brace)) {
714 ReadInitializer = true;
715 break;
Sebastian Redla891a322011-09-30 08:32:17 +0000716 }
Sebastian Redl6df65482011-09-24 17:48:25 +0000717 }
718 }
719
Sebastian Redla891a322011-09-30 08:32:17 +0000720 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
721 // brace: an opening one is the function body, while a closing one probably
722 // means we've reached the end of the class.
Eli Friedmane9ee3822012-02-22 04:49:04 +0000723 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
724 /*StopAtSemi=*/true,
725 /*ConsumeFinalToken=*/false);
726 if (Tok.isNot(tok::l_brace)) {
727 if (ReadInitializer)
728 return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
729 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
730 }
Sebastian Redla891a322011-09-30 08:32:17 +0000731
732 Toks.push_back(Tok);
733 ConsumeBrace();
734 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000735}