blob: bc634b57d9cee4f6a460052e89601345783a6922 [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);
Douglas Gregor147545d2011-10-10 14:49:18 +000060 bool TypeSpecContainsAuto
61 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +000062 if (Init.isUsable())
Douglas Gregor147545d2011-10-10 14:49:18 +000063 Actions.AddInitializerToDecl(FnD, Init.get(), false,
64 TypeSpecContainsAuto);
65 else
66 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
67 }
Nico Weber48673472011-01-28 06:07:34 +000068 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000069
Douglas Gregor74e2fc32012-04-16 18:27:27 +000070 HandleMemberFunctionDeclDelays(D, FnD);
Eli Friedmand33133c2009-07-22 21:45:50 +000071
John McCalleee1d542011-02-14 07:13:47 +000072 D.complete(FnD);
73
Sean Hunte4246a62011-05-12 06:15:49 +000074 if (Tok.is(tok::equal)) {
75 ConsumeToken();
76
Richard Smithc430ef42011-11-10 09:08:44 +000077 if (!FnD) {
78 SkipUntil(tok::semi);
79 return 0;
80 }
81
Sean Hunte4246a62011-05-12 06:15:49 +000082 bool Delete = false;
83 SourceLocation KWLoc;
84 if (Tok.is(tok::kw_delete)) {
Richard Smith80ad52f2013-01-02 11:42:31 +000085 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +000086 diag::warn_cxx98_compat_deleted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000087 diag::ext_deleted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000088
89 KWLoc = ConsumeToken();
90 Actions.SetDeclDeleted(FnD, KWLoc);
91 Delete = true;
92 } else if (Tok.is(tok::kw_default)) {
Richard Smith80ad52f2013-01-02 11:42:31 +000093 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +000094 diag::warn_cxx98_compat_defaulted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000095 diag::ext_defaulted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000096
97 KWLoc = ConsumeToken();
98 Actions.SetDeclDefaulted(FnD, KWLoc);
99 } else {
100 llvm_unreachable("function definition after = not 'delete' or 'default'");
101 }
102
103 if (Tok.is(tok::comma)) {
104 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
105 << Delete;
106 SkipUntil(tok::semi);
107 } else {
108 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
109 Delete ? "delete" : "default", tok::semi);
110 }
111
112 return FnD;
113 }
114
Francois Pichet8387e2a2011-04-22 22:18:13 +0000115 // In delayed template parsing mode, if we are within a class template
116 // or if we are about to parse function member template then consume
117 // the tokens and store them for parsing at the end of the translation unit.
David Blaikie4e4d0842012-03-11 07:00:24 +0000118 if (getLangOpts().DelayedTemplateParsing &&
Douglas Gregor09630172012-06-28 21:43:01 +0000119 DefinitionKind == FDK_Definition &&
Francois Pichet8387e2a2011-04-22 22:18:13 +0000120 ((Actions.CurContext->isDependentContext() ||
121 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
Francois Pichet9d38dbc2011-11-18 23:47:17 +0000122 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
Francois Pichet8387e2a2011-04-22 22:18:13 +0000123
124 if (FnD) {
Francois Pichete1fca502011-12-08 09:11:52 +0000125 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000126
John McCalle34db6b2013-03-14 05:13:41 +0000127 FunctionDecl *FD = getFunctionDecl(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +0000128 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000129
130 LateParsedTemplateMap[FD] = LPT;
131 Actions.MarkAsLateParsedTemplate(FD);
132 LexTemplateFunctionForLateParsing(LPT->Toks);
133 } else {
134 CachedTokens Toks;
135 LexTemplateFunctionForLateParsing(Toks);
136 }
137
138 return FnD;
139 }
140
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000141 // Consume the tokens and store them for later parsing.
142
Douglas Gregord54eb442010-10-12 16:25:54 +0000143 LexedMethod* LM = new LexedMethod(this, FnD);
144 getCurrentClass().LateParsedDeclarations.push_back(LM);
145 LM->TemplateScope = getCurScope()->isTemplateParamScope();
146 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000147
Sebastian Redld3a413d2009-04-26 20:35:05 +0000148 tok::TokenKind kind = Tok.getKind();
Sebastian Redla891a322011-09-30 08:32:17 +0000149 // Consume everything up to (and including) the left brace of the
150 // function body.
151 if (ConsumeAndStoreFunctionPrologue(Toks)) {
152 // We didn't find the left-brace we expected after the
Eli Friedmane9ee3822012-02-22 04:49:04 +0000153 // constructor initializer; we already printed an error, and it's likely
154 // impossible to recover, so don't try to parse this method later.
155 // If we stopped at a semicolon, consume it to avoid an extra warning.
156 if (Tok.is(tok::semi))
157 ConsumeToken();
158 delete getCurrentClass().LateParsedDeclarations.back();
159 getCurrentClass().LateParsedDeclarations.pop_back();
160 return FnD;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000161 } else {
Sebastian Redla891a322011-09-30 08:32:17 +0000162 // Consume everything up to (and including) the matching right brace.
163 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000164 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000165
Sebastian Redld3a413d2009-04-26 20:35:05 +0000166 // If we're in a function-try-block, we need to store all the catch blocks.
167 if (kind == tok::kw_try) {
168 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000169 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
170 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000171 }
172 }
173
Douglas Gregor87f10642011-04-14 23:19:27 +0000174
175 if (!FnD) {
176 // If semantic analysis could not build a function declaration,
177 // just throw away the late-parsed declaration.
178 delete getCurrentClass().LateParsedDeclarations.back();
179 getCurrentClass().LateParsedDeclarations.pop_back();
180 }
181
John McCalle34db6b2013-03-14 05:13:41 +0000182 // If this is a friend function, mark that it's late-parsed so that
183 // it's still known to be a definition even before we attach the
184 // parsed body. Sema needs to treat friend function definitions
185 // differently during template instantiation, and it's possible for
186 // the containing class to be instantiated before all its member
187 // function definitions are parsed.
188 //
189 // If you remove this, you can remove the code that clears the flag
190 // after parsing the member.
191 if (D.getDeclSpec().isFriendSpecified()) {
192 getFunctionDecl(FnD)->setLateTemplateParsed(true);
193 }
194
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000195 return FnD;
196}
197
Richard Smith7a614d82011-06-11 17:19:42 +0000198/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
199/// specified Declarator is a well formed C++ non-static data member
200/// declaration. Now lex its initializer and store its tokens for parsing
201/// after the class is complete.
202void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
203 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
204 "Current token not a '{' or '='!");
205
206 LateParsedMemberInitializer *MI =
207 new LateParsedMemberInitializer(this, VarD);
208 getCurrentClass().LateParsedDeclarations.push_back(MI);
209 CachedTokens &Toks = MI->Toks;
210
211 tok::TokenKind kind = Tok.getKind();
212 if (kind == tok::equal) {
213 Toks.push_back(Tok);
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000214 ConsumeToken();
Richard Smith7a614d82011-06-11 17:19:42 +0000215 }
216
217 if (kind == tok::l_brace) {
218 // Begin by storing the '{' token.
219 Toks.push_back(Tok);
220 ConsumeBrace();
221
222 // Consume everything up to (and including) the matching right brace.
223 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
224 } else {
225 // Consume everything up to (but excluding) the comma or semicolon.
226 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
227 /*ConsumeFinalToken=*/false);
228 }
229
230 // Store an artificial EOF token to ensure that we don't run off the end of
231 // the initializer when we come to parse it.
232 Token Eof;
233 Eof.startToken();
234 Eof.setKind(tok::eof);
235 Eof.setLocation(Tok.getLocation());
236 Toks.push_back(Eof);
237}
238
Douglas Gregord54eb442010-10-12 16:25:54 +0000239Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
240void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000241void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000242void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
243
244Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
245 : Self(P), Class(C) {}
246
247Parser::LateParsedClass::~LateParsedClass() {
248 Self->DeallocateParsedClasses(Class);
249}
250
251void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
252 Self->ParseLexedMethodDeclarations(*Class);
253}
254
Richard Smith7a614d82011-06-11 17:19:42 +0000255void Parser::LateParsedClass::ParseLexedMemberInitializers() {
256 Self->ParseLexedMemberInitializers(*Class);
257}
258
Douglas Gregord54eb442010-10-12 16:25:54 +0000259void Parser::LateParsedClass::ParseLexedMethodDefs() {
260 Self->ParseLexedMethodDefs(*Class);
261}
262
263void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
264 Self->ParseLexedMethodDeclaration(*this);
265}
266
267void Parser::LexedMethod::ParseLexedMethodDefs() {
268 Self->ParseLexedMethodDef(*this);
269}
270
Richard Smith7a614d82011-06-11 17:19:42 +0000271void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
272 Self->ParseLexedMemberInitializer(*this);
273}
274
Douglas Gregor72b505b2008-12-16 21:30:33 +0000275/// ParseLexedMethodDeclarations - We finished parsing the member
276/// specification of a top (non-nested) C++ class. Now go over the
277/// stack of method declarations with some parts for which parsing was
278/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000279void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
280 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000281 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000282 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000283 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000284
John McCall7a1dc562009-12-19 10:49:29 +0000285 // The current scope is still active if we're the top-level class.
286 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000287 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000288 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
289 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000290 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000291 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000292
Douglas Gregord54eb442010-10-12 16:25:54 +0000293 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
294 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000295 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000296
John McCall7a1dc562009-12-19 10:49:29 +0000297 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000298 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000299}
300
Douglas Gregord54eb442010-10-12 16:25:54 +0000301void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
302 // If this is a member template, introduce the template parameter scope.
303 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
304 if (LM.TemplateScope)
305 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
306
307 // Start the delayed C++ method declaration
308 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
309
310 // Introduce the parameters into scope and parse their default
311 // arguments.
Richard Smith3a2b7a12013-01-28 22:42:45 +0000312 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
313 Scope::FunctionDeclarationScope | Scope::DeclScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000314 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
315 // Introduce the parameter into scope.
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000316 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
317 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000318
319 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
320 // Save the current token position.
321 SourceLocation origLoc = Tok.getLocation();
322
323 // Parse the default argument from its saved token stream.
324 Toks->push_back(Tok); // So that the current token doesn't get lost
325 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
326
327 // Consume the previously-pushed token.
328 ConsumeAnyToken();
329
330 // Consume the '='.
331 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
332 SourceLocation EqualLoc = ConsumeToken();
333
334 // The argument isn't actually potentially evaluated unless it is
335 // used.
336 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000337 Sema::PotentiallyEvaluatedIfUsed,
338 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000339
Sebastian Redl84407ba2012-03-14 15:54:00 +0000340 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +0000341 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redlca893712012-03-20 21:24:03 +0000342 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +0000343 DefArgResult = ParseBraceInitializer();
Sebastian Redlca893712012-03-20 21:24:03 +0000344 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +0000345 DefArgResult = ParseAssignmentExpression();
Douglas Gregord54eb442010-10-12 16:25:54 +0000346 if (DefArgResult.isInvalid())
347 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
348 else {
349 if (Tok.is(tok::cxx_defaultarg_end))
350 ConsumeToken();
351 else
352 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
353 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
354 DefArgResult.take());
355 }
356
357 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
358 Tok.getLocation()) &&
359 "ParseAssignmentExpression went over the default arg tokens!");
360 // There could be leftover tokens (e.g. because of an error).
361 // Skip through until we reach the original token position.
362 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
363 ConsumeAnyToken();
364
365 delete Toks;
366 LM.DefaultArgs[I].Toks = 0;
367 }
368 }
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000369
Douglas Gregord54eb442010-10-12 16:25:54 +0000370 PrototypeScope.Exit();
371
372 // Finish the delayed C++ method declaration.
373 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
374}
375
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000376/// ParseLexedMethodDefs - We finished parsing the member specification of a top
377/// (non-nested) C++ class. Now go over the stack of lexed methods that were
378/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000379void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
380 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000381 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000382 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000383 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000384
385 bool HasClassScope = !Class.TopLevelClass;
386 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
387 HasClassScope);
388
Douglas Gregord54eb442010-10-12 16:25:54 +0000389 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
390 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
391 }
392}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000393
Douglas Gregord54eb442010-10-12 16:25:54 +0000394void Parser::ParseLexedMethodDef(LexedMethod &LM) {
395 // If this is a member template, introduce the template parameter scope.
396 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
397 if (LM.TemplateScope)
398 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Douglas Gregord54eb442010-10-12 16:25:54 +0000400 // Save the current token position.
401 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000402
Douglas Gregord54eb442010-10-12 16:25:54 +0000403 assert(!LM.Toks.empty() && "Empty body!");
404 // Append the current token at the end of the new token stream so that it
405 // doesn't get lost.
406 LM.Toks.push_back(Tok);
407 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000408
Douglas Gregord54eb442010-10-12 16:25:54 +0000409 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +0000410 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Douglas Gregord54eb442010-10-12 16:25:54 +0000411 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
412 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000413
Douglas Gregord54eb442010-10-12 16:25:54 +0000414 // Parse the method body. Function body parsing code is similar enough
415 // to be re-used for method bodies as well.
416 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
417 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000418
Douglas Gregord54eb442010-10-12 16:25:54 +0000419 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000420 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000421 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
422 Tok.getLocation()) &&
423 "ParseFunctionTryBlock went over the cached tokens!");
424 // There could be leftover tokens (e.g. because of an error).
425 // Skip through until we reach the original token position.
426 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
427 ConsumeAnyToken();
428 return;
429 }
430 if (Tok.is(tok::colon)) {
431 ParseConstructorInitializer(LM.D);
432
433 // Error recovery.
434 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000435 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000436 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000437 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
438 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000439 return;
440 }
441 } else
442 Actions.ActOnDefaultCtorInitializers(LM.D);
443
Douglas Gregorc9977d02011-03-16 17:05:57 +0000444 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000445
John McCalle34db6b2013-03-14 05:13:41 +0000446 // Clear the late-template-parsed bit if we set it before.
447 if (LM.D) getFunctionDecl(LM.D)->setLateTemplateParsed(false);
448
Douglas Gregord54eb442010-10-12 16:25:54 +0000449 if (Tok.getLocation() != origLoc) {
450 // Due to parsing error, we either went over the cached tokens or
451 // there are still cached tokens left. If it's the latter case skip the
452 // leftover tokens.
453 // Since this is an uncommon situation that should be avoided, use the
454 // expensive isBeforeInTranslationUnit call.
455 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
456 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000457 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000458 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000459 }
460}
461
Richard Smith7a614d82011-06-11 17:19:42 +0000462/// ParseLexedMemberInitializers - We finished parsing the member specification
463/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
464/// initializers that were collected during its parsing and parse them all.
465void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
466 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
467 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
468 HasTemplateScope);
469 if (HasTemplateScope)
470 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
471
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000472 // Set or update the scope flags.
Richard Smith7a614d82011-06-11 17:19:42 +0000473 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000474 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Richard Smith7a614d82011-06-11 17:19:42 +0000475 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
476 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
477
478 if (!AlreadyHasClassScope)
479 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
480 Class.TagOrTemplate);
481
Benjamin Kramer268efba2012-05-17 12:01:52 +0000482 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000483 // C++11 [expr.prim.general]p4:
484 // Otherwise, if a member-declarator declares a non-static data member
485 // (9.2) of a class X, the expression this is a prvalue of type "pointer
486 // to X" within the optional brace-or-equal-initializer. It shall not
487 // appear elsewhere in the member-declarator.
488 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
489 /*TypeQuals=*/(unsigned)0);
Richard Smith7a614d82011-06-11 17:19:42 +0000490
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000491 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
492 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
493 }
494 }
495
Richard Smith7a614d82011-06-11 17:19:42 +0000496 if (!AlreadyHasClassScope)
497 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
498 Class.TagOrTemplate);
499
500 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
501}
502
503void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000504 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000505 return;
506
507 // Append the current token at the end of the new token stream so that it
508 // doesn't get lost.
509 MI.Toks.push_back(Tok);
510 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
511
512 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +0000513 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Richard Smith7a614d82011-06-11 17:19:42 +0000514
515 SourceLocation EqualLoc;
Richard Smithca523302012-06-10 03:12:00 +0000516
Douglas Gregor552e2992012-02-21 02:22:07 +0000517 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
518 EqualLoc);
Richard Smith7a614d82011-06-11 17:19:42 +0000519
520 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
521
522 // The next token should be our artificial terminating EOF token.
523 if (Tok.isNot(tok::eof)) {
524 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
525 if (!EndLoc.isValid())
526 EndLoc = Tok.getLocation();
527 // No fixit; we can't recover as if there were a semicolon here.
528 Diag(EndLoc, diag::err_expected_semi_decl_list);
529
530 // Consume tokens until we hit the artificial EOF.
531 while (Tok.isNot(tok::eof))
532 ConsumeAnyToken();
533 }
534 ConsumeAnyToken();
535}
536
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000537/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000538/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000539/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000540/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000541/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000542/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000543bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
544 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000545 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000546 // We always want this function to consume at least one token if the first
547 // token isn't T and if not at EOF.
548 bool isFirstTokenConsumed = true;
549 while (1) {
550 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000551 if (Tok.is(T1) || Tok.is(T2)) {
552 if (ConsumeFinalToken) {
553 Toks.push_back(Tok);
554 ConsumeAnyToken();
555 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000556 return true;
557 }
558
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000559 switch (Tok.getKind()) {
560 case tok::eof:
561 // Ran out of tokens.
562 return false;
563
564 case tok::l_paren:
565 // Recursively consume properly-nested parens.
566 Toks.push_back(Tok);
567 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000568 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000569 break;
570 case tok::l_square:
571 // Recursively consume properly-nested square brackets.
572 Toks.push_back(Tok);
573 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000574 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000575 break;
576 case tok::l_brace:
577 // Recursively consume properly-nested braces.
578 Toks.push_back(Tok);
579 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000580 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000581 break;
582
583 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
584 // Since the user wasn't looking for this token (if they were, it would
585 // already be handled), this isn't balanced. If there is a LHS token at a
586 // higher level, we will assume that this matches the unbalanced token
587 // and return it. Otherwise, this is a spurious RHS token, which we skip.
588 case tok::r_paren:
589 if (ParenCount && !isFirstTokenConsumed)
590 return false; // Matches something.
591 Toks.push_back(Tok);
592 ConsumeParen();
593 break;
594 case tok::r_square:
595 if (BracketCount && !isFirstTokenConsumed)
596 return false; // Matches something.
597 Toks.push_back(Tok);
598 ConsumeBracket();
599 break;
600 case tok::r_brace:
601 if (BraceCount && !isFirstTokenConsumed)
602 return false; // Matches something.
603 Toks.push_back(Tok);
604 ConsumeBrace();
605 break;
606
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000607 case tok::code_completion:
608 Toks.push_back(Tok);
609 ConsumeCodeCompletionToken();
610 break;
611
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000612 case tok::string_literal:
613 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000614 case tok::utf8_string_literal:
615 case tok::utf16_string_literal:
616 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000617 Toks.push_back(Tok);
618 ConsumeStringToken();
619 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000620 case tok::semi:
621 if (StopAtSemi)
622 return false;
623 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000624 default:
625 // consume this token.
626 Toks.push_back(Tok);
627 ConsumeToken();
628 break;
629 }
630 isFirstTokenConsumed = false;
631 }
632}
Sebastian Redl6df65482011-09-24 17:48:25 +0000633
634/// \brief Consume tokens and store them in the passed token container until
635/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redla891a322011-09-30 08:32:17 +0000636/// the opening brace of the function body. The opening brace will be consumed
637/// if and only if there was no error.
Sebastian Redl6df65482011-09-24 17:48:25 +0000638///
Sebastian Redla891a322011-09-30 08:32:17 +0000639/// \return True on error.
640bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000641 if (Tok.is(tok::kw_try)) {
642 Toks.push_back(Tok);
643 ConsumeToken();
644 }
Eli Friedmane9ee3822012-02-22 04:49:04 +0000645 bool ReadInitializer = false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000646 if (Tok.is(tok::colon)) {
647 // Initializers can contain braces too.
648 Toks.push_back(Tok);
649 ConsumeToken();
650
651 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
652 if (Tok.is(tok::eof) || Tok.is(tok::semi))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000653 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
Sebastian Redl6df65482011-09-24 17:48:25 +0000654
655 // Grab the identifier.
656 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
657 /*StopAtSemi=*/true,
658 /*ConsumeFinalToken=*/false))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000659 return Diag(Tok.getLocation(), diag::err_expected_lparen);
Sebastian Redl6df65482011-09-24 17:48:25 +0000660
661 tok::TokenKind kind = Tok.getKind();
662 Toks.push_back(Tok);
Eli Friedmane9ee3822012-02-22 04:49:04 +0000663 bool IsLParen = (kind == tok::l_paren);
664 SourceLocation LOpen = Tok.getLocation();
665
666 if (IsLParen) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000667 ConsumeParen();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000668 } else {
Sebastian Redl6df65482011-09-24 17:48:25 +0000669 assert(kind == tok::l_brace && "Must be left paren or brace here.");
670 ConsumeBrace();
Sebastian Redla891a322011-09-30 08:32:17 +0000671 // In C++03, this has to be the start of the function body, which
Eli Friedmane9ee3822012-02-22 04:49:04 +0000672 // means the initializer is malformed; we'll diagnose it later.
Richard Smith80ad52f2013-01-02 11:42:31 +0000673 if (!getLangOpts().CPlusPlus11)
Sebastian Redla891a322011-09-30 08:32:17 +0000674 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000675 }
676
677 // Grab the initializer
Eli Friedmane9ee3822012-02-22 04:49:04 +0000678 if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
679 Toks, /*StopAtSemi=*/true)) {
680 Diag(Tok, IsLParen ? diag::err_expected_rparen :
681 diag::err_expected_rbrace);
682 Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
Sebastian Redl6df65482011-09-24 17:48:25 +0000683 return true;
Eli Friedmane9ee3822012-02-22 04:49:04 +0000684 }
685
686 // Grab pack ellipsis, if present
687 if (Tok.is(tok::ellipsis)) {
688 Toks.push_back(Tok);
689 ConsumeToken();
690 }
Sebastian Redla891a322011-09-30 08:32:17 +0000691
692 // Grab the separating comma, if any.
693 if (Tok.is(tok::comma)) {
694 Toks.push_back(Tok);
695 ConsumeToken();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000696 } else if (Tok.isNot(tok::l_brace)) {
697 ReadInitializer = true;
698 break;
Sebastian Redla891a322011-09-30 08:32:17 +0000699 }
Sebastian Redl6df65482011-09-24 17:48:25 +0000700 }
701 }
702
Sebastian Redla891a322011-09-30 08:32:17 +0000703 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
704 // brace: an opening one is the function body, while a closing one probably
705 // means we've reached the end of the class.
Eli Friedmane9ee3822012-02-22 04:49:04 +0000706 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
707 /*StopAtSemi=*/true,
708 /*ConsumeFinalToken=*/false);
709 if (Tok.isNot(tok::l_brace)) {
710 if (ReadInitializer)
711 return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
712 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
713 }
Sebastian Redla891a322011-09-30 08:32:17 +0000714
715 Toks.push_back(Tok);
716 ConsumeBrace();
717 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000718}