blob: d2a85355b6f55b4593ea1a32b6d69688f2c06eb9 [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
Chris Lattner500d3292009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000015#include "clang/Parse/Parser.h"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/Scope.h"
Francois Pichet8387e2a2011-04-22 22:18:13 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000019using namespace clang;
20
Sebastian Redld3a413d2009-04-26 20:35:05 +000021/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000022/// Declarator is a well formed C++ inline method definition. Now lex its body
23/// and store its tokens for parsing after the C++ class is complete.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000024Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
25 AttributeList *AccessAttrs,
26 ParsingDeclarator &D,
Douglas Gregor45fa5602011-11-07 20:56:01 +000027 const ParsedTemplateInfo &TemplateInfo,
28 const VirtSpecifiers& VS,
29 FunctionDefinitionKind DefinitionKind,
30 ExprResult& Init) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000031 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sean Hunte4246a62011-05-12 06:15:49 +000032 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
33 Tok.is(tok::equal)) &&
34 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000035
John McCallf312b1e2010-08-26 23:41:50 +000036 MultiTemplateParamsArg TemplateParams(Actions,
Sean Hunt4cd84942010-04-14 23:07:37 +000037 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
38 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
39
John McCalld226f652010-08-21 09:40:31 +000040 Decl *FnD;
Douglas Gregor45fa5602011-11-07 20:56:01 +000041 D.setFunctionDefinitionKind(DefinitionKind);
John McCall67d1a672009-08-06 02:15:43 +000042 if (D.getDeclSpec().isFriendSpecified())
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +000043 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
Sean Hunt4cd84942010-04-14 23:07:37 +000044 move(TemplateParams));
Douglas Gregor147545d2011-10-10 14:49:18 +000045 else {
Douglas Gregor23c94db2010-07-02 17:43:08 +000046 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlsson69a87352011-01-20 03:57:25 +000047 move(TemplateParams), 0,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +000048 VS, /*HasDeferredInit=*/false);
Douglas Gregor147545d2011-10-10 14:49:18 +000049 if (FnD) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000050 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
51 false, true);
Douglas Gregor147545d2011-10-10 14:49:18 +000052 bool TypeSpecContainsAuto
53 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +000054 if (Init.isUsable())
Douglas Gregor147545d2011-10-10 14:49:18 +000055 Actions.AddInitializerToDecl(FnD, Init.get(), false,
56 TypeSpecContainsAuto);
57 else
58 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
59 }
Nico Weber48673472011-01-28 06:07:34 +000060 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000061
Eli Friedmand33133c2009-07-22 21:45:50 +000062 HandleMemberFunctionDefaultArgs(D, FnD);
63
John McCalleee1d542011-02-14 07:13:47 +000064 D.complete(FnD);
65
Sean Hunte4246a62011-05-12 06:15:49 +000066 if (Tok.is(tok::equal)) {
67 ConsumeToken();
68
Richard Smithc430ef42011-11-10 09:08:44 +000069 if (!FnD) {
70 SkipUntil(tok::semi);
71 return 0;
72 }
73
Sean Hunte4246a62011-05-12 06:15:49 +000074 bool Delete = false;
75 SourceLocation KWLoc;
76 if (Tok.is(tok::kw_delete)) {
David Blaikie4e4d0842012-03-11 07:00:24 +000077 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +000078 diag::warn_cxx98_compat_deleted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000079 diag::ext_deleted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000080
81 KWLoc = ConsumeToken();
82 Actions.SetDeclDeleted(FnD, KWLoc);
83 Delete = true;
84 } else if (Tok.is(tok::kw_default)) {
David Blaikie4e4d0842012-03-11 07:00:24 +000085 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +000086 diag::warn_cxx98_compat_defaulted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000087 diag::ext_defaulted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000088
89 KWLoc = ConsumeToken();
90 Actions.SetDeclDefaulted(FnD, KWLoc);
91 } else {
92 llvm_unreachable("function definition after = not 'delete' or 'default'");
93 }
94
95 if (Tok.is(tok::comma)) {
96 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
97 << Delete;
98 SkipUntil(tok::semi);
99 } else {
100 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
101 Delete ? "delete" : "default", tok::semi);
102 }
103
104 return FnD;
105 }
106
Francois Pichet8387e2a2011-04-22 22:18:13 +0000107 // In delayed template parsing mode, if we are within a class template
108 // or if we are about to parse function member template then consume
109 // the tokens and store them for parsing at the end of the translation unit.
David Blaikie4e4d0842012-03-11 07:00:24 +0000110 if (getLangOpts().DelayedTemplateParsing &&
Francois Pichet8387e2a2011-04-22 22:18:13 +0000111 ((Actions.CurContext->isDependentContext() ||
112 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
Francois Pichet9d38dbc2011-11-18 23:47:17 +0000113 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
Francois Pichet8387e2a2011-04-22 22:18:13 +0000114
115 if (FnD) {
Francois Pichete1fca502011-12-08 09:11:52 +0000116 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000117
118 FunctionDecl *FD = 0;
119 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
120 FD = FunTmpl->getTemplatedDecl();
121 else
122 FD = cast<FunctionDecl>(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +0000123 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000124
125 LateParsedTemplateMap[FD] = LPT;
126 Actions.MarkAsLateParsedTemplate(FD);
127 LexTemplateFunctionForLateParsing(LPT->Toks);
128 } else {
129 CachedTokens Toks;
130 LexTemplateFunctionForLateParsing(Toks);
131 }
132
133 return FnD;
134 }
135
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000136 // Consume the tokens and store them for later parsing.
137
Douglas Gregord54eb442010-10-12 16:25:54 +0000138 LexedMethod* LM = new LexedMethod(this, FnD);
139 getCurrentClass().LateParsedDeclarations.push_back(LM);
140 LM->TemplateScope = getCurScope()->isTemplateParamScope();
141 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000142
Sebastian Redld3a413d2009-04-26 20:35:05 +0000143 tok::TokenKind kind = Tok.getKind();
Sebastian Redla891a322011-09-30 08:32:17 +0000144 // Consume everything up to (and including) the left brace of the
145 // function body.
146 if (ConsumeAndStoreFunctionPrologue(Toks)) {
147 // We didn't find the left-brace we expected after the
Eli Friedmane9ee3822012-02-22 04:49:04 +0000148 // constructor initializer; we already printed an error, and it's likely
149 // impossible to recover, so don't try to parse this method later.
150 // If we stopped at a semicolon, consume it to avoid an extra warning.
151 if (Tok.is(tok::semi))
152 ConsumeToken();
153 delete getCurrentClass().LateParsedDeclarations.back();
154 getCurrentClass().LateParsedDeclarations.pop_back();
155 return FnD;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000156 } else {
Sebastian Redla891a322011-09-30 08:32:17 +0000157 // Consume everything up to (and including) the matching right brace.
158 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000159 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000160
Sebastian Redld3a413d2009-04-26 20:35:05 +0000161 // If we're in a function-try-block, we need to store all the catch blocks.
162 if (kind == tok::kw_try) {
163 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000164 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
165 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000166 }
167 }
168
Douglas Gregor87f10642011-04-14 23:19:27 +0000169
170 if (!FnD) {
171 // If semantic analysis could not build a function declaration,
172 // just throw away the late-parsed declaration.
173 delete getCurrentClass().LateParsedDeclarations.back();
174 getCurrentClass().LateParsedDeclarations.pop_back();
175 }
176
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000177 return FnD;
178}
179
Richard Smith7a614d82011-06-11 17:19:42 +0000180/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
181/// specified Declarator is a well formed C++ non-static data member
182/// declaration. Now lex its initializer and store its tokens for parsing
183/// after the class is complete.
184void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
185 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
186 "Current token not a '{' or '='!");
187
188 LateParsedMemberInitializer *MI =
189 new LateParsedMemberInitializer(this, VarD);
190 getCurrentClass().LateParsedDeclarations.push_back(MI);
191 CachedTokens &Toks = MI->Toks;
192
193 tok::TokenKind kind = Tok.getKind();
194 if (kind == tok::equal) {
195 Toks.push_back(Tok);
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000196 ConsumeToken();
Richard Smith7a614d82011-06-11 17:19:42 +0000197 }
198
199 if (kind == tok::l_brace) {
200 // Begin by storing the '{' token.
201 Toks.push_back(Tok);
202 ConsumeBrace();
203
204 // Consume everything up to (and including) the matching right brace.
205 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
206 } else {
207 // Consume everything up to (but excluding) the comma or semicolon.
208 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
209 /*ConsumeFinalToken=*/false);
210 }
211
212 // Store an artificial EOF token to ensure that we don't run off the end of
213 // the initializer when we come to parse it.
214 Token Eof;
215 Eof.startToken();
216 Eof.setKind(tok::eof);
217 Eof.setLocation(Tok.getLocation());
218 Toks.push_back(Eof);
219}
220
Douglas Gregord54eb442010-10-12 16:25:54 +0000221Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
222void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000223void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000224void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
225
226Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
227 : Self(P), Class(C) {}
228
229Parser::LateParsedClass::~LateParsedClass() {
230 Self->DeallocateParsedClasses(Class);
231}
232
233void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
234 Self->ParseLexedMethodDeclarations(*Class);
235}
236
Richard Smith7a614d82011-06-11 17:19:42 +0000237void Parser::LateParsedClass::ParseLexedMemberInitializers() {
238 Self->ParseLexedMemberInitializers(*Class);
239}
240
Douglas Gregord54eb442010-10-12 16:25:54 +0000241void Parser::LateParsedClass::ParseLexedMethodDefs() {
242 Self->ParseLexedMethodDefs(*Class);
243}
244
245void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
246 Self->ParseLexedMethodDeclaration(*this);
247}
248
249void Parser::LexedMethod::ParseLexedMethodDefs() {
250 Self->ParseLexedMethodDef(*this);
251}
252
Richard Smith7a614d82011-06-11 17:19:42 +0000253void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
254 Self->ParseLexedMemberInitializer(*this);
255}
256
Douglas Gregor72b505b2008-12-16 21:30:33 +0000257/// ParseLexedMethodDeclarations - We finished parsing the member
258/// specification of a top (non-nested) C++ class. Now go over the
259/// stack of method declarations with some parts for which parsing was
260/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000261void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
262 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000263 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000264 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000265 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000266
John McCall7a1dc562009-12-19 10:49:29 +0000267 // The current scope is still active if we're the top-level class.
268 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000269 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000270 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
271 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000272 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000273 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000274
Douglas Gregord54eb442010-10-12 16:25:54 +0000275 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
276 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000277 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000278
John McCall7a1dc562009-12-19 10:49:29 +0000279 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000280 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000281}
282
Douglas Gregord54eb442010-10-12 16:25:54 +0000283void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
284 // If this is a member template, introduce the template parameter scope.
285 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
286 if (LM.TemplateScope)
287 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
288
289 // Start the delayed C++ method declaration
290 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
291
292 // Introduce the parameters into scope and parse their default
293 // arguments.
294 ParseScope PrototypeScope(this,
295 Scope::FunctionPrototypeScope|Scope::DeclScope);
296 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
297 // Introduce the parameter into scope.
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000298 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
299 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000300
301 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
302 // Save the current token position.
303 SourceLocation origLoc = Tok.getLocation();
304
305 // Parse the default argument from its saved token stream.
306 Toks->push_back(Tok); // So that the current token doesn't get lost
307 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
308
309 // Consume the previously-pushed token.
310 ConsumeAnyToken();
311
312 // Consume the '='.
313 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
314 SourceLocation EqualLoc = ConsumeToken();
315
316 // The argument isn't actually potentially evaluated unless it is
317 // used.
318 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000319 Sema::PotentiallyEvaluatedIfUsed,
320 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000321
Sebastian Redl84407ba2012-03-14 15:54:00 +0000322 ExprResult DefArgResult;
Sebastian Redlca893712012-03-20 21:24:03 +0000323 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
324 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +0000325 DefArgResult = ParseBraceInitializer();
Sebastian Redlca893712012-03-20 21:24:03 +0000326 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +0000327 DefArgResult = ParseAssignmentExpression();
Douglas Gregord54eb442010-10-12 16:25:54 +0000328 if (DefArgResult.isInvalid())
329 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
330 else {
331 if (Tok.is(tok::cxx_defaultarg_end))
332 ConsumeToken();
333 else
334 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
335 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
336 DefArgResult.take());
337 }
338
339 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
340 Tok.getLocation()) &&
341 "ParseAssignmentExpression went over the default arg tokens!");
342 // There could be leftover tokens (e.g. because of an error).
343 // Skip through until we reach the original token position.
344 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
345 ConsumeAnyToken();
346
347 delete Toks;
348 LM.DefaultArgs[I].Toks = 0;
349 }
350 }
351 PrototypeScope.Exit();
352
353 // Finish the delayed C++ method declaration.
354 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
355}
356
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000357/// ParseLexedMethodDefs - We finished parsing the member specification of a top
358/// (non-nested) C++ class. Now go over the stack of lexed methods that were
359/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000360void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
361 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000362 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000363 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000364 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000365
366 bool HasClassScope = !Class.TopLevelClass;
367 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
368 HasClassScope);
369
Douglas Gregord54eb442010-10-12 16:25:54 +0000370 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
371 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
372 }
373}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000374
Douglas Gregord54eb442010-10-12 16:25:54 +0000375void Parser::ParseLexedMethodDef(LexedMethod &LM) {
376 // If this is a member template, introduce the template parameter scope.
377 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
378 if (LM.TemplateScope)
379 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Douglas Gregord54eb442010-10-12 16:25:54 +0000381 // Save the current token position.
382 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000383
Douglas Gregord54eb442010-10-12 16:25:54 +0000384 assert(!LM.Toks.empty() && "Empty body!");
385 // Append the current token at the end of the new token stream so that it
386 // doesn't get lost.
387 LM.Toks.push_back(Tok);
388 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000389
Douglas Gregord54eb442010-10-12 16:25:54 +0000390 // Consume the previously pushed token.
391 ConsumeAnyToken();
392 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
393 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000394
Douglas Gregord54eb442010-10-12 16:25:54 +0000395 // Parse the method body. Function body parsing code is similar enough
396 // to be re-used for method bodies as well.
397 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
398 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000399
Douglas Gregord54eb442010-10-12 16:25:54 +0000400 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000401 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000402 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
403 Tok.getLocation()) &&
404 "ParseFunctionTryBlock went over the cached tokens!");
405 // There could be leftover tokens (e.g. because of an error).
406 // Skip through until we reach the original token position.
407 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
408 ConsumeAnyToken();
409 return;
410 }
411 if (Tok.is(tok::colon)) {
412 ParseConstructorInitializer(LM.D);
413
414 // Error recovery.
415 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000416 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000417 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000418 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
419 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000420 return;
421 }
422 } else
423 Actions.ActOnDefaultCtorInitializers(LM.D);
424
Douglas Gregorc9977d02011-03-16 17:05:57 +0000425 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000426
427 if (Tok.getLocation() != origLoc) {
428 // Due to parsing error, we either went over the cached tokens or
429 // there are still cached tokens left. If it's the latter case skip the
430 // leftover tokens.
431 // Since this is an uncommon situation that should be avoided, use the
432 // expensive isBeforeInTranslationUnit call.
433 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
434 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000435 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000436 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000437 }
438}
439
Richard Smith7a614d82011-06-11 17:19:42 +0000440/// ParseLexedMemberInitializers - We finished parsing the member specification
441/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
442/// initializers that were collected during its parsing and parse them all.
443void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
444 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
445 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
446 HasTemplateScope);
447 if (HasTemplateScope)
448 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
449
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000450 // Set or update the scope flags.
Richard Smith7a614d82011-06-11 17:19:42 +0000451 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000452 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Richard Smith7a614d82011-06-11 17:19:42 +0000453 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
454 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
455
456 if (!AlreadyHasClassScope)
457 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
458 Class.TagOrTemplate);
459
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000460 {
461 // C++11 [expr.prim.general]p4:
462 // Otherwise, if a member-declarator declares a non-static data member
463 // (9.2) of a class X, the expression this is a prvalue of type "pointer
464 // to X" within the optional brace-or-equal-initializer. It shall not
465 // appear elsewhere in the member-declarator.
466 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
467 /*TypeQuals=*/(unsigned)0);
Richard Smith7a614d82011-06-11 17:19:42 +0000468
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000469 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
470 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
471 }
472 }
473
Richard Smith7a614d82011-06-11 17:19:42 +0000474 if (!AlreadyHasClassScope)
475 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
476 Class.TagOrTemplate);
477
478 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
479}
480
481void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000482 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000483 return;
484
485 // Append the current token at the end of the new token stream so that it
486 // doesn't get lost.
487 MI.Toks.push_back(Tok);
488 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
489
490 // Consume the previously pushed token.
491 ConsumeAnyToken();
492
493 SourceLocation EqualLoc;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000494
Douglas Gregor552e2992012-02-21 02:22:07 +0000495 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
496 EqualLoc);
Richard Smith7a614d82011-06-11 17:19:42 +0000497
498 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
499
500 // The next token should be our artificial terminating EOF token.
501 if (Tok.isNot(tok::eof)) {
502 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
503 if (!EndLoc.isValid())
504 EndLoc = Tok.getLocation();
505 // No fixit; we can't recover as if there were a semicolon here.
506 Diag(EndLoc, diag::err_expected_semi_decl_list);
507
508 // Consume tokens until we hit the artificial EOF.
509 while (Tok.isNot(tok::eof))
510 ConsumeAnyToken();
511 }
512 ConsumeAnyToken();
513}
514
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000515/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000516/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000517/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000518/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000519/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000520/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000521bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
522 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000523 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000524 // We always want this function to consume at least one token if the first
525 // token isn't T and if not at EOF.
526 bool isFirstTokenConsumed = true;
527 while (1) {
528 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000529 if (Tok.is(T1) || Tok.is(T2)) {
530 if (ConsumeFinalToken) {
531 Toks.push_back(Tok);
532 ConsumeAnyToken();
533 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000534 return true;
535 }
536
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000537 switch (Tok.getKind()) {
538 case tok::eof:
539 // Ran out of tokens.
540 return false;
541
542 case tok::l_paren:
543 // Recursively consume properly-nested parens.
544 Toks.push_back(Tok);
545 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000546 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000547 break;
548 case tok::l_square:
549 // Recursively consume properly-nested square brackets.
550 Toks.push_back(Tok);
551 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000552 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000553 break;
554 case tok::l_brace:
555 // Recursively consume properly-nested braces.
556 Toks.push_back(Tok);
557 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000558 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000559 break;
560
561 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
562 // Since the user wasn't looking for this token (if they were, it would
563 // already be handled), this isn't balanced. If there is a LHS token at a
564 // higher level, we will assume that this matches the unbalanced token
565 // and return it. Otherwise, this is a spurious RHS token, which we skip.
566 case tok::r_paren:
567 if (ParenCount && !isFirstTokenConsumed)
568 return false; // Matches something.
569 Toks.push_back(Tok);
570 ConsumeParen();
571 break;
572 case tok::r_square:
573 if (BracketCount && !isFirstTokenConsumed)
574 return false; // Matches something.
575 Toks.push_back(Tok);
576 ConsumeBracket();
577 break;
578 case tok::r_brace:
579 if (BraceCount && !isFirstTokenConsumed)
580 return false; // Matches something.
581 Toks.push_back(Tok);
582 ConsumeBrace();
583 break;
584
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000585 case tok::code_completion:
586 Toks.push_back(Tok);
587 ConsumeCodeCompletionToken();
588 break;
589
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000590 case tok::string_literal:
591 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000592 case tok::utf8_string_literal:
593 case tok::utf16_string_literal:
594 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000595 Toks.push_back(Tok);
596 ConsumeStringToken();
597 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000598 case tok::semi:
599 if (StopAtSemi)
600 return false;
601 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000602 default:
603 // consume this token.
604 Toks.push_back(Tok);
605 ConsumeToken();
606 break;
607 }
608 isFirstTokenConsumed = false;
609 }
610}
Sebastian Redl6df65482011-09-24 17:48:25 +0000611
612/// \brief Consume tokens and store them in the passed token container until
613/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redla891a322011-09-30 08:32:17 +0000614/// the opening brace of the function body. The opening brace will be consumed
615/// if and only if there was no error.
Sebastian Redl6df65482011-09-24 17:48:25 +0000616///
Sebastian Redla891a322011-09-30 08:32:17 +0000617/// \return True on error.
618bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000619 if (Tok.is(tok::kw_try)) {
620 Toks.push_back(Tok);
621 ConsumeToken();
622 }
Eli Friedmane9ee3822012-02-22 04:49:04 +0000623 bool ReadInitializer = false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000624 if (Tok.is(tok::colon)) {
625 // Initializers can contain braces too.
626 Toks.push_back(Tok);
627 ConsumeToken();
628
629 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
630 if (Tok.is(tok::eof) || Tok.is(tok::semi))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000631 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
Sebastian Redl6df65482011-09-24 17:48:25 +0000632
633 // Grab the identifier.
634 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
635 /*StopAtSemi=*/true,
636 /*ConsumeFinalToken=*/false))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000637 return Diag(Tok.getLocation(), diag::err_expected_lparen);
Sebastian Redl6df65482011-09-24 17:48:25 +0000638
639 tok::TokenKind kind = Tok.getKind();
640 Toks.push_back(Tok);
Eli Friedmane9ee3822012-02-22 04:49:04 +0000641 bool IsLParen = (kind == tok::l_paren);
642 SourceLocation LOpen = Tok.getLocation();
643
644 if (IsLParen) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000645 ConsumeParen();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000646 } else {
Sebastian Redl6df65482011-09-24 17:48:25 +0000647 assert(kind == tok::l_brace && "Must be left paren or brace here.");
648 ConsumeBrace();
Sebastian Redla891a322011-09-30 08:32:17 +0000649 // In C++03, this has to be the start of the function body, which
Eli Friedmane9ee3822012-02-22 04:49:04 +0000650 // means the initializer is malformed; we'll diagnose it later.
David Blaikie4e4d0842012-03-11 07:00:24 +0000651 if (!getLangOpts().CPlusPlus0x)
Sebastian Redla891a322011-09-30 08:32:17 +0000652 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000653 }
654
655 // Grab the initializer
Eli Friedmane9ee3822012-02-22 04:49:04 +0000656 if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
657 Toks, /*StopAtSemi=*/true)) {
658 Diag(Tok, IsLParen ? diag::err_expected_rparen :
659 diag::err_expected_rbrace);
660 Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
Sebastian Redl6df65482011-09-24 17:48:25 +0000661 return true;
Eli Friedmane9ee3822012-02-22 04:49:04 +0000662 }
663
664 // Grab pack ellipsis, if present
665 if (Tok.is(tok::ellipsis)) {
666 Toks.push_back(Tok);
667 ConsumeToken();
668 }
Sebastian Redla891a322011-09-30 08:32:17 +0000669
670 // Grab the separating comma, if any.
671 if (Tok.is(tok::comma)) {
672 Toks.push_back(Tok);
673 ConsumeToken();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000674 } else if (Tok.isNot(tok::l_brace)) {
675 ReadInitializer = true;
676 break;
Sebastian Redla891a322011-09-30 08:32:17 +0000677 }
Sebastian Redl6df65482011-09-24 17:48:25 +0000678 }
679 }
680
Sebastian Redla891a322011-09-30 08:32:17 +0000681 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
682 // brace: an opening one is the function body, while a closing one probably
683 // means we've reached the end of the class.
Eli Friedmane9ee3822012-02-22 04:49:04 +0000684 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
685 /*StopAtSemi=*/true,
686 /*ConsumeFinalToken=*/false);
687 if (Tok.isNot(tok::l_brace)) {
688 if (ReadInitializer)
689 return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
690 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
691 }
Sebastian Redla891a322011-09-30 08:32:17 +0000692
693 Toks.push_back(Tok);
694 ConsumeBrace();
695 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000696}