blob: e15ab0ae98948264e788a26b978ffe3b1fb4ea70 [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
Sebastian Redld3a413d2009-04-26 20:35:05 +000022/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000023/// Declarator is a well formed C++ inline method definition. Now lex its body
24/// and store its tokens for parsing after the C++ class is complete.
Rafael Espindola0777cf52013-01-08 21:00:12 +000025NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000026 AttributeList *AccessAttrs,
27 ParsingDeclarator &D,
Douglas Gregor45fa5602011-11-07 20:56:01 +000028 const ParsedTemplateInfo &TemplateInfo,
29 const VirtSpecifiers& VS,
30 FunctionDefinitionKind DefinitionKind,
31 ExprResult& Init) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000032 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sean Hunte4246a62011-05-12 06:15:49 +000033 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
34 Tok.is(tok::equal)) &&
35 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000036
Benjamin Kramer5354e772012-08-23 23:38:35 +000037 MultiTemplateParamsArg TemplateParams(
Sean Hunt4cd84942010-04-14 23:07:37 +000038 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
39 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
40
Rafael Espindola0777cf52013-01-08 21:00:12 +000041 NamedDecl *FnD;
Douglas Gregor45fa5602011-11-07 20:56:01 +000042 D.setFunctionDefinitionKind(DefinitionKind);
John McCall67d1a672009-08-06 02:15:43 +000043 if (D.getDeclSpec().isFriendSpecified())
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +000044 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +000045 TemplateParams);
Douglas Gregor147545d2011-10-10 14:49:18 +000046 else {
Douglas Gregor23c94db2010-07-02 17:43:08 +000047 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +000048 TemplateParams, 0,
Richard Smithca523302012-06-10 03:12:00 +000049 VS, ICIS_NoInit);
Douglas Gregor147545d2011-10-10 14:49:18 +000050 if (FnD) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000051 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
52 false, true);
Douglas Gregor147545d2011-10-10 14:49:18 +000053 bool TypeSpecContainsAuto
54 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +000055 if (Init.isUsable())
Douglas Gregor147545d2011-10-10 14:49:18 +000056 Actions.AddInitializerToDecl(FnD, Init.get(), false,
57 TypeSpecContainsAuto);
58 else
59 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
60 }
Nico Weber48673472011-01-28 06:07:34 +000061 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000062
Douglas Gregor74e2fc32012-04-16 18:27:27 +000063 HandleMemberFunctionDeclDelays(D, FnD);
Eli Friedmand33133c2009-07-22 21:45:50 +000064
John McCalleee1d542011-02-14 07:13:47 +000065 D.complete(FnD);
66
Sean Hunte4246a62011-05-12 06:15:49 +000067 if (Tok.is(tok::equal)) {
68 ConsumeToken();
69
Richard Smithc430ef42011-11-10 09:08:44 +000070 if (!FnD) {
71 SkipUntil(tok::semi);
72 return 0;
73 }
74
Sean Hunte4246a62011-05-12 06:15:49 +000075 bool Delete = false;
76 SourceLocation KWLoc;
77 if (Tok.is(tok::kw_delete)) {
Richard Smith80ad52f2013-01-02 11:42:31 +000078 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +000079 diag::warn_cxx98_compat_deleted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000080 diag::ext_deleted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000081
82 KWLoc = ConsumeToken();
83 Actions.SetDeclDeleted(FnD, KWLoc);
84 Delete = true;
85 } else if (Tok.is(tok::kw_default)) {
Richard Smith80ad52f2013-01-02 11:42:31 +000086 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +000087 diag::warn_cxx98_compat_defaulted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000088 diag::ext_defaulted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000089
90 KWLoc = ConsumeToken();
91 Actions.SetDeclDefaulted(FnD, KWLoc);
92 } else {
93 llvm_unreachable("function definition after = not 'delete' or 'default'");
94 }
95
96 if (Tok.is(tok::comma)) {
97 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
98 << Delete;
99 SkipUntil(tok::semi);
100 } else {
101 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
102 Delete ? "delete" : "default", tok::semi);
103 }
104
105 return FnD;
106 }
107
Francois Pichet8387e2a2011-04-22 22:18:13 +0000108 // In delayed template parsing mode, if we are within a class template
109 // or if we are about to parse function member template then consume
110 // the tokens and store them for parsing at the end of the translation unit.
David Blaikie4e4d0842012-03-11 07:00:24 +0000111 if (getLangOpts().DelayedTemplateParsing &&
Douglas Gregor09630172012-06-28 21:43:01 +0000112 DefinitionKind == FDK_Definition &&
Francois Pichet8387e2a2011-04-22 22:18:13 +0000113 ((Actions.CurContext->isDependentContext() ||
114 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
Francois Pichet9d38dbc2011-11-18 23:47:17 +0000115 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
Francois Pichet8387e2a2011-04-22 22:18:13 +0000116
117 if (FnD) {
Francois Pichete1fca502011-12-08 09:11:52 +0000118 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000119
120 FunctionDecl *FD = 0;
121 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
122 FD = FunTmpl->getTemplatedDecl();
123 else
124 FD = cast<FunctionDecl>(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +0000125 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000126
127 LateParsedTemplateMap[FD] = LPT;
128 Actions.MarkAsLateParsedTemplate(FD);
129 LexTemplateFunctionForLateParsing(LPT->Toks);
130 } else {
131 CachedTokens Toks;
132 LexTemplateFunctionForLateParsing(Toks);
133 }
134
135 return FnD;
136 }
137
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000138 // Consume the tokens and store them for later parsing.
139
Douglas Gregord54eb442010-10-12 16:25:54 +0000140 LexedMethod* LM = new LexedMethod(this, FnD);
141 getCurrentClass().LateParsedDeclarations.push_back(LM);
142 LM->TemplateScope = getCurScope()->isTemplateParamScope();
143 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000144
Sebastian Redld3a413d2009-04-26 20:35:05 +0000145 tok::TokenKind kind = Tok.getKind();
Sebastian Redla891a322011-09-30 08:32:17 +0000146 // Consume everything up to (and including) the left brace of the
147 // function body.
148 if (ConsumeAndStoreFunctionPrologue(Toks)) {
149 // We didn't find the left-brace we expected after the
Eli Friedmane9ee3822012-02-22 04:49:04 +0000150 // constructor initializer; we already printed an error, and it's likely
151 // impossible to recover, so don't try to parse this method later.
152 // If we stopped at a semicolon, consume it to avoid an extra warning.
153 if (Tok.is(tok::semi))
154 ConsumeToken();
155 delete getCurrentClass().LateParsedDeclarations.back();
156 getCurrentClass().LateParsedDeclarations.pop_back();
157 return FnD;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000158 } else {
Sebastian Redla891a322011-09-30 08:32:17 +0000159 // Consume everything up to (and including) the matching right brace.
160 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000161 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000162
Sebastian Redld3a413d2009-04-26 20:35:05 +0000163 // If we're in a function-try-block, we need to store all the catch blocks.
164 if (kind == tok::kw_try) {
165 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000166 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
167 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000168 }
169 }
170
Douglas Gregor87f10642011-04-14 23:19:27 +0000171
172 if (!FnD) {
173 // If semantic analysis could not build a function declaration,
174 // just throw away the late-parsed declaration.
175 delete getCurrentClass().LateParsedDeclarations.back();
176 getCurrentClass().LateParsedDeclarations.pop_back();
177 }
178
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000179 return FnD;
180}
181
Richard Smith7a614d82011-06-11 17:19:42 +0000182/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
183/// specified Declarator is a well formed C++ non-static data member
184/// declaration. Now lex its initializer and store its tokens for parsing
185/// after the class is complete.
186void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
187 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
188 "Current token not a '{' or '='!");
189
190 LateParsedMemberInitializer *MI =
191 new LateParsedMemberInitializer(this, VarD);
192 getCurrentClass().LateParsedDeclarations.push_back(MI);
193 CachedTokens &Toks = MI->Toks;
194
195 tok::TokenKind kind = Tok.getKind();
196 if (kind == tok::equal) {
197 Toks.push_back(Tok);
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000198 ConsumeToken();
Richard Smith7a614d82011-06-11 17:19:42 +0000199 }
200
201 if (kind == tok::l_brace) {
202 // Begin by storing the '{' token.
203 Toks.push_back(Tok);
204 ConsumeBrace();
205
206 // Consume everything up to (and including) the matching right brace.
207 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
208 } else {
209 // Consume everything up to (but excluding) the comma or semicolon.
210 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
211 /*ConsumeFinalToken=*/false);
212 }
213
214 // Store an artificial EOF token to ensure that we don't run off the end of
215 // the initializer when we come to parse it.
216 Token Eof;
217 Eof.startToken();
218 Eof.setKind(tok::eof);
219 Eof.setLocation(Tok.getLocation());
220 Toks.push_back(Eof);
221}
222
Douglas Gregord54eb442010-10-12 16:25:54 +0000223Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
224void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000225void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000226void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
227
228Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
229 : Self(P), Class(C) {}
230
231Parser::LateParsedClass::~LateParsedClass() {
232 Self->DeallocateParsedClasses(Class);
233}
234
235void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
236 Self->ParseLexedMethodDeclarations(*Class);
237}
238
Richard Smith7a614d82011-06-11 17:19:42 +0000239void Parser::LateParsedClass::ParseLexedMemberInitializers() {
240 Self->ParseLexedMemberInitializers(*Class);
241}
242
Douglas Gregord54eb442010-10-12 16:25:54 +0000243void Parser::LateParsedClass::ParseLexedMethodDefs() {
244 Self->ParseLexedMethodDefs(*Class);
245}
246
247void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
248 Self->ParseLexedMethodDeclaration(*this);
249}
250
251void Parser::LexedMethod::ParseLexedMethodDefs() {
252 Self->ParseLexedMethodDef(*this);
253}
254
Richard Smith7a614d82011-06-11 17:19:42 +0000255void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
256 Self->ParseLexedMemberInitializer(*this);
257}
258
Douglas Gregor72b505b2008-12-16 21:30:33 +0000259/// ParseLexedMethodDeclarations - We finished parsing the member
260/// specification of a top (non-nested) C++ class. Now go over the
261/// stack of method declarations with some parts for which parsing was
262/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000263void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
264 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000265 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000266 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000267 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000268
John McCall7a1dc562009-12-19 10:49:29 +0000269 // The current scope is still active if we're the top-level class.
270 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000271 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000272 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
273 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000274 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000275 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000276
Douglas Gregord54eb442010-10-12 16:25:54 +0000277 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
278 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000279 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000280
John McCall7a1dc562009-12-19 10:49:29 +0000281 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000282 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000283}
284
Douglas Gregord54eb442010-10-12 16:25:54 +0000285void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
286 // If this is a member template, introduce the template parameter scope.
287 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
288 if (LM.TemplateScope)
289 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
290
291 // Start the delayed C++ method declaration
292 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
293
294 // Introduce the parameters into scope and parse their default
295 // arguments.
Richard Smith3a2b7a12013-01-28 22:42:45 +0000296 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
297 Scope::FunctionDeclarationScope | Scope::DeclScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000298 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
299 // Introduce the parameter into scope.
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000300 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
301 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000302
303 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
304 // Save the current token position.
305 SourceLocation origLoc = Tok.getLocation();
306
307 // Parse the default argument from its saved token stream.
308 Toks->push_back(Tok); // So that the current token doesn't get lost
309 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
310
311 // Consume the previously-pushed token.
312 ConsumeAnyToken();
313
314 // Consume the '='.
315 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
316 SourceLocation EqualLoc = ConsumeToken();
317
318 // The argument isn't actually potentially evaluated unless it is
319 // used.
320 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000321 Sema::PotentiallyEvaluatedIfUsed,
322 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000323
Sebastian Redl84407ba2012-03-14 15:54:00 +0000324 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +0000325 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redlca893712012-03-20 21:24:03 +0000326 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +0000327 DefArgResult = ParseBraceInitializer();
Sebastian Redlca893712012-03-20 21:24:03 +0000328 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +0000329 DefArgResult = ParseAssignmentExpression();
Douglas Gregord54eb442010-10-12 16:25:54 +0000330 if (DefArgResult.isInvalid())
331 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
332 else {
333 if (Tok.is(tok::cxx_defaultarg_end))
334 ConsumeToken();
335 else
336 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
337 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
338 DefArgResult.take());
339 }
340
341 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
342 Tok.getLocation()) &&
343 "ParseAssignmentExpression went over the default arg tokens!");
344 // There could be leftover tokens (e.g. because of an error).
345 // Skip through until we reach the original token position.
346 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
347 ConsumeAnyToken();
348
349 delete Toks;
350 LM.DefaultArgs[I].Toks = 0;
351 }
352 }
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000353
Douglas Gregord54eb442010-10-12 16:25:54 +0000354 PrototypeScope.Exit();
355
356 // Finish the delayed C++ method declaration.
357 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
358}
359
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000360/// ParseLexedMethodDefs - We finished parsing the member specification of a top
361/// (non-nested) C++ class. Now go over the stack of lexed methods that were
362/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000363void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
364 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000365 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000366 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000367 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000368
369 bool HasClassScope = !Class.TopLevelClass;
370 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
371 HasClassScope);
372
Douglas Gregord54eb442010-10-12 16:25:54 +0000373 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
374 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
375 }
376}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000377
Douglas Gregord54eb442010-10-12 16:25:54 +0000378void Parser::ParseLexedMethodDef(LexedMethod &LM) {
379 // If this is a member template, introduce the template parameter scope.
380 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
381 if (LM.TemplateScope)
382 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Douglas Gregord54eb442010-10-12 16:25:54 +0000384 // Save the current token position.
385 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000386
Douglas Gregord54eb442010-10-12 16:25:54 +0000387 assert(!LM.Toks.empty() && "Empty body!");
388 // Append the current token at the end of the new token stream so that it
389 // doesn't get lost.
390 LM.Toks.push_back(Tok);
391 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000392
Douglas Gregord54eb442010-10-12 16:25:54 +0000393 // Consume the previously pushed token.
394 ConsumeAnyToken();
395 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
396 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000397
Douglas Gregord54eb442010-10-12 16:25:54 +0000398 // Parse the method body. Function body parsing code is similar enough
399 // to be re-used for method bodies as well.
400 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
401 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000402
Douglas Gregord54eb442010-10-12 16:25:54 +0000403 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000404 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000405 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
406 Tok.getLocation()) &&
407 "ParseFunctionTryBlock went over the cached tokens!");
408 // There could be leftover tokens (e.g. because of an error).
409 // Skip through until we reach the original token position.
410 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
411 ConsumeAnyToken();
412 return;
413 }
414 if (Tok.is(tok::colon)) {
415 ParseConstructorInitializer(LM.D);
416
417 // Error recovery.
418 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000419 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000420 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000421 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
422 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000423 return;
424 }
425 } else
426 Actions.ActOnDefaultCtorInitializers(LM.D);
427
Douglas Gregorc9977d02011-03-16 17:05:57 +0000428 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000429
430 if (Tok.getLocation() != origLoc) {
431 // Due to parsing error, we either went over the cached tokens or
432 // there are still cached tokens left. If it's the latter case skip the
433 // leftover tokens.
434 // Since this is an uncommon situation that should be avoided, use the
435 // expensive isBeforeInTranslationUnit call.
436 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
437 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000438 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000439 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000440 }
441}
442
Richard Smith7a614d82011-06-11 17:19:42 +0000443/// ParseLexedMemberInitializers - We finished parsing the member specification
444/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
445/// initializers that were collected during its parsing and parse them all.
446void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
447 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
448 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
449 HasTemplateScope);
450 if (HasTemplateScope)
451 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
452
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000453 // Set or update the scope flags.
Richard Smith7a614d82011-06-11 17:19:42 +0000454 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000455 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Richard Smith7a614d82011-06-11 17:19:42 +0000456 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
457 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
458
459 if (!AlreadyHasClassScope)
460 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
461 Class.TagOrTemplate);
462
Benjamin Kramer268efba2012-05-17 12:01:52 +0000463 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000464 // C++11 [expr.prim.general]p4:
465 // Otherwise, if a member-declarator declares a non-static data member
466 // (9.2) of a class X, the expression this is a prvalue of type "pointer
467 // to X" within the optional brace-or-equal-initializer. It shall not
468 // appear elsewhere in the member-declarator.
469 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
470 /*TypeQuals=*/(unsigned)0);
Richard Smith7a614d82011-06-11 17:19:42 +0000471
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000472 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
473 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
474 }
475 }
476
Richard Smith7a614d82011-06-11 17:19:42 +0000477 if (!AlreadyHasClassScope)
478 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
479 Class.TagOrTemplate);
480
481 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
482}
483
484void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000485 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000486 return;
487
488 // Append the current token at the end of the new token stream so that it
489 // doesn't get lost.
490 MI.Toks.push_back(Tok);
491 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
492
493 // Consume the previously pushed token.
494 ConsumeAnyToken();
495
496 SourceLocation EqualLoc;
Richard Smithca523302012-06-10 03:12:00 +0000497
Douglas Gregor552e2992012-02-21 02:22:07 +0000498 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
499 EqualLoc);
Richard Smith7a614d82011-06-11 17:19:42 +0000500
501 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
502
503 // The next token should be our artificial terminating EOF token.
504 if (Tok.isNot(tok::eof)) {
505 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
506 if (!EndLoc.isValid())
507 EndLoc = Tok.getLocation();
508 // No fixit; we can't recover as if there were a semicolon here.
509 Diag(EndLoc, diag::err_expected_semi_decl_list);
510
511 // Consume tokens until we hit the artificial EOF.
512 while (Tok.isNot(tok::eof))
513 ConsumeAnyToken();
514 }
515 ConsumeAnyToken();
516}
517
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000518/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000519/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000520/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000521/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000522/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000523/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000524bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
525 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000526 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000527 // We always want this function to consume at least one token if the first
528 // token isn't T and if not at EOF.
529 bool isFirstTokenConsumed = true;
530 while (1) {
531 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000532 if (Tok.is(T1) || Tok.is(T2)) {
533 if (ConsumeFinalToken) {
534 Toks.push_back(Tok);
535 ConsumeAnyToken();
536 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000537 return true;
538 }
539
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000540 switch (Tok.getKind()) {
541 case tok::eof:
542 // Ran out of tokens.
543 return false;
544
545 case tok::l_paren:
546 // Recursively consume properly-nested parens.
547 Toks.push_back(Tok);
548 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000549 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000550 break;
551 case tok::l_square:
552 // Recursively consume properly-nested square brackets.
553 Toks.push_back(Tok);
554 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000555 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000556 break;
557 case tok::l_brace:
558 // Recursively consume properly-nested braces.
559 Toks.push_back(Tok);
560 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000561 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000562 break;
563
564 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
565 // Since the user wasn't looking for this token (if they were, it would
566 // already be handled), this isn't balanced. If there is a LHS token at a
567 // higher level, we will assume that this matches the unbalanced token
568 // and return it. Otherwise, this is a spurious RHS token, which we skip.
569 case tok::r_paren:
570 if (ParenCount && !isFirstTokenConsumed)
571 return false; // Matches something.
572 Toks.push_back(Tok);
573 ConsumeParen();
574 break;
575 case tok::r_square:
576 if (BracketCount && !isFirstTokenConsumed)
577 return false; // Matches something.
578 Toks.push_back(Tok);
579 ConsumeBracket();
580 break;
581 case tok::r_brace:
582 if (BraceCount && !isFirstTokenConsumed)
583 return false; // Matches something.
584 Toks.push_back(Tok);
585 ConsumeBrace();
586 break;
587
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000588 case tok::code_completion:
589 Toks.push_back(Tok);
590 ConsumeCodeCompletionToken();
591 break;
592
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000593 case tok::string_literal:
594 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000595 case tok::utf8_string_literal:
596 case tok::utf16_string_literal:
597 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000598 Toks.push_back(Tok);
599 ConsumeStringToken();
600 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000601 case tok::semi:
602 if (StopAtSemi)
603 return false;
604 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000605 default:
606 // consume this token.
607 Toks.push_back(Tok);
608 ConsumeToken();
609 break;
610 }
611 isFirstTokenConsumed = false;
612 }
613}
Sebastian Redl6df65482011-09-24 17:48:25 +0000614
615/// \brief Consume tokens and store them in the passed token container until
616/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redla891a322011-09-30 08:32:17 +0000617/// the opening brace of the function body. The opening brace will be consumed
618/// if and only if there was no error.
Sebastian Redl6df65482011-09-24 17:48:25 +0000619///
Sebastian Redla891a322011-09-30 08:32:17 +0000620/// \return True on error.
621bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000622 if (Tok.is(tok::kw_try)) {
623 Toks.push_back(Tok);
624 ConsumeToken();
625 }
Eli Friedmane9ee3822012-02-22 04:49:04 +0000626 bool ReadInitializer = false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000627 if (Tok.is(tok::colon)) {
628 // Initializers can contain braces too.
629 Toks.push_back(Tok);
630 ConsumeToken();
631
632 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
633 if (Tok.is(tok::eof) || Tok.is(tok::semi))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000634 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
Sebastian Redl6df65482011-09-24 17:48:25 +0000635
636 // Grab the identifier.
637 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
638 /*StopAtSemi=*/true,
639 /*ConsumeFinalToken=*/false))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000640 return Diag(Tok.getLocation(), diag::err_expected_lparen);
Sebastian Redl6df65482011-09-24 17:48:25 +0000641
642 tok::TokenKind kind = Tok.getKind();
643 Toks.push_back(Tok);
Eli Friedmane9ee3822012-02-22 04:49:04 +0000644 bool IsLParen = (kind == tok::l_paren);
645 SourceLocation LOpen = Tok.getLocation();
646
647 if (IsLParen) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000648 ConsumeParen();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000649 } else {
Sebastian Redl6df65482011-09-24 17:48:25 +0000650 assert(kind == tok::l_brace && "Must be left paren or brace here.");
651 ConsumeBrace();
Sebastian Redla891a322011-09-30 08:32:17 +0000652 // In C++03, this has to be the start of the function body, which
Eli Friedmane9ee3822012-02-22 04:49:04 +0000653 // means the initializer is malformed; we'll diagnose it later.
Richard Smith80ad52f2013-01-02 11:42:31 +0000654 if (!getLangOpts().CPlusPlus11)
Sebastian Redla891a322011-09-30 08:32:17 +0000655 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000656 }
657
658 // Grab the initializer
Eli Friedmane9ee3822012-02-22 04:49:04 +0000659 if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
660 Toks, /*StopAtSemi=*/true)) {
661 Diag(Tok, IsLParen ? diag::err_expected_rparen :
662 diag::err_expected_rbrace);
663 Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
Sebastian Redl6df65482011-09-24 17:48:25 +0000664 return true;
Eli Friedmane9ee3822012-02-22 04:49:04 +0000665 }
666
667 // Grab pack ellipsis, if present
668 if (Tok.is(tok::ellipsis)) {
669 Toks.push_back(Tok);
670 ConsumeToken();
671 }
Sebastian Redla891a322011-09-30 08:32:17 +0000672
673 // Grab the separating comma, if any.
674 if (Tok.is(tok::comma)) {
675 Toks.push_back(Tok);
676 ConsumeToken();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000677 } else if (Tok.isNot(tok::l_brace)) {
678 ReadInitializer = true;
679 break;
Sebastian Redla891a322011-09-30 08:32:17 +0000680 }
Sebastian Redl6df65482011-09-24 17:48:25 +0000681 }
682 }
683
Sebastian Redla891a322011-09-30 08:32:17 +0000684 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
685 // brace: an opening one is the function body, while a closing one probably
686 // means we've reached the end of the class.
Eli Friedmane9ee3822012-02-22 04:49:04 +0000687 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
688 /*StopAtSemi=*/true,
689 /*ConsumeFinalToken=*/false);
690 if (Tok.isNot(tok::l_brace)) {
691 if (ReadInitializer)
692 return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
693 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
694 }
Sebastian Redla891a322011-09-30 08:32:17 +0000695
696 Toks.push_back(Tok);
697 ConsumeBrace();
698 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000699}