blob: 51dc5c88f8c7079a665d20a4fd2c0aa0f5d64b95 [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)) {
Richard Smith7fe62082011-10-15 05:09:34 +000077 Diag(Tok, getLang().CPlusPlus0x ?
78 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)) {
Richard Smith7fe62082011-10-15 05:09:34 +000085 Diag(Tok, getLang().CPlusPlus0x ?
86 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.
110 if (getLang().DelayedTemplateParsing &&
111 ((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
148 // constructor initializer.
149 if (Tok.is(tok::semi)) {
150 // We found a semicolon; complain, consume the semicolon, and
151 // don't try to parse this method later.
152 Diag(Tok.getLocation(), diag::err_expected_lbrace);
153 ConsumeAnyToken();
154 delete getCurrentClass().LateParsedDeclarations.back();
155 getCurrentClass().LateParsedDeclarations.pop_back();
156 return FnD;
Douglas Gregor3f08d182008-11-10 16:59:40 +0000157 }
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);
198 ConsumeAnyToken();
199 }
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.
296 ParseScope PrototypeScope(this,
297 Scope::FunctionPrototypeScope|Scope::DeclScope);
298 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
299 // Introduce the parameter into scope.
300 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
301
302 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
303 // Save the current token position.
304 SourceLocation origLoc = Tok.getLocation();
305
306 // Parse the default argument from its saved token stream.
307 Toks->push_back(Tok); // So that the current token doesn't get lost
308 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
309
310 // Consume the previously-pushed token.
311 ConsumeAnyToken();
312
313 // Consume the '='.
314 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
315 SourceLocation EqualLoc = ConsumeToken();
316
317 // The argument isn't actually potentially evaluated unless it is
318 // used.
319 EnterExpressionEvaluationContext Eval(Actions,
320 Sema::PotentiallyEvaluatedIfUsed);
321
322 ExprResult DefArgResult(ParseAssignmentExpression());
323 if (DefArgResult.isInvalid())
324 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
325 else {
326 if (Tok.is(tok::cxx_defaultarg_end))
327 ConsumeToken();
328 else
329 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
330 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
331 DefArgResult.take());
332 }
333
334 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
335 Tok.getLocation()) &&
336 "ParseAssignmentExpression went over the default arg tokens!");
337 // There could be leftover tokens (e.g. because of an error).
338 // Skip through until we reach the original token position.
339 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
340 ConsumeAnyToken();
341
342 delete Toks;
343 LM.DefaultArgs[I].Toks = 0;
344 }
345 }
346 PrototypeScope.Exit();
347
348 // Finish the delayed C++ method declaration.
349 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
350}
351
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000352/// ParseLexedMethodDefs - We finished parsing the member specification of a top
353/// (non-nested) C++ class. Now go over the stack of lexed methods that were
354/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000355void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
356 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000357 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000358 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000359 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000360
361 bool HasClassScope = !Class.TopLevelClass;
362 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
363 HasClassScope);
364
Douglas Gregord54eb442010-10-12 16:25:54 +0000365 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
366 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
367 }
368}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000369
Douglas Gregord54eb442010-10-12 16:25:54 +0000370void Parser::ParseLexedMethodDef(LexedMethod &LM) {
371 // If this is a member template, introduce the template parameter scope.
372 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
373 if (LM.TemplateScope)
374 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregord54eb442010-10-12 16:25:54 +0000376 // Save the current token position.
377 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000378
Douglas Gregord54eb442010-10-12 16:25:54 +0000379 assert(!LM.Toks.empty() && "Empty body!");
380 // Append the current token at the end of the new token stream so that it
381 // doesn't get lost.
382 LM.Toks.push_back(Tok);
383 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000384
Douglas Gregord54eb442010-10-12 16:25:54 +0000385 // Consume the previously pushed token.
386 ConsumeAnyToken();
387 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
388 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000389
Douglas Gregord54eb442010-10-12 16:25:54 +0000390 // Parse the method body. Function body parsing code is similar enough
391 // to be re-used for method bodies as well.
392 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
393 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000394
Douglas Gregord54eb442010-10-12 16:25:54 +0000395 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000396 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000397 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
398 Tok.getLocation()) &&
399 "ParseFunctionTryBlock went over the cached tokens!");
400 // There could be leftover tokens (e.g. because of an error).
401 // Skip through until we reach the original token position.
402 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
403 ConsumeAnyToken();
404 return;
405 }
406 if (Tok.is(tok::colon)) {
407 ParseConstructorInitializer(LM.D);
408
409 // Error recovery.
410 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000411 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000412 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000413 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
414 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000415 return;
416 }
417 } else
418 Actions.ActOnDefaultCtorInitializers(LM.D);
419
Douglas Gregorc9977d02011-03-16 17:05:57 +0000420 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000421
422 if (Tok.getLocation() != origLoc) {
423 // Due to parsing error, we either went over the cached tokens or
424 // there are still cached tokens left. If it's the latter case skip the
425 // leftover tokens.
426 // Since this is an uncommon situation that should be avoided, use the
427 // expensive isBeforeInTranslationUnit call.
428 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
429 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000430 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000431 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000432 }
433}
434
Richard Smith7a614d82011-06-11 17:19:42 +0000435/// ParseLexedMemberInitializers - We finished parsing the member specification
436/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
437/// initializers that were collected during its parsing and parse them all.
438void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
439 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
440 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
441 HasTemplateScope);
442 if (HasTemplateScope)
443 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
444
445 // Set or update the scope flags to include Scope::ThisScope.
446 bool AlreadyHasClassScope = Class.TopLevelClass;
447 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
448 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
449 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
450
451 if (!AlreadyHasClassScope)
452 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
453 Class.TagOrTemplate);
454
455 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
456 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
457 }
458
459 if (!AlreadyHasClassScope)
460 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
461 Class.TagOrTemplate);
462
463 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
464}
465
466void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000467 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000468 return;
469
470 // Append the current token at the end of the new token stream so that it
471 // doesn't get lost.
472 MI.Toks.push_back(Tok);
473 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
474
475 // Consume the previously pushed token.
476 ConsumeAnyToken();
477
478 SourceLocation EqualLoc;
479 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
480
481 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
482
483 // The next token should be our artificial terminating EOF token.
484 if (Tok.isNot(tok::eof)) {
485 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
486 if (!EndLoc.isValid())
487 EndLoc = Tok.getLocation();
488 // No fixit; we can't recover as if there were a semicolon here.
489 Diag(EndLoc, diag::err_expected_semi_decl_list);
490
491 // Consume tokens until we hit the artificial EOF.
492 while (Tok.isNot(tok::eof))
493 ConsumeAnyToken();
494 }
495 ConsumeAnyToken();
496}
497
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000498/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000499/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000500/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000501/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000502/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000503/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000504bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
505 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000506 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000507 // We always want this function to consume at least one token if the first
508 // token isn't T and if not at EOF.
509 bool isFirstTokenConsumed = true;
510 while (1) {
511 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000512 if (Tok.is(T1) || Tok.is(T2)) {
513 if (ConsumeFinalToken) {
514 Toks.push_back(Tok);
515 ConsumeAnyToken();
516 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000517 return true;
518 }
519
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000520 switch (Tok.getKind()) {
521 case tok::eof:
522 // Ran out of tokens.
523 return false;
524
525 case tok::l_paren:
526 // Recursively consume properly-nested parens.
527 Toks.push_back(Tok);
528 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000529 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000530 break;
531 case tok::l_square:
532 // Recursively consume properly-nested square brackets.
533 Toks.push_back(Tok);
534 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000535 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000536 break;
537 case tok::l_brace:
538 // Recursively consume properly-nested braces.
539 Toks.push_back(Tok);
540 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000541 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000542 break;
543
544 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
545 // Since the user wasn't looking for this token (if they were, it would
546 // already be handled), this isn't balanced. If there is a LHS token at a
547 // higher level, we will assume that this matches the unbalanced token
548 // and return it. Otherwise, this is a spurious RHS token, which we skip.
549 case tok::r_paren:
550 if (ParenCount && !isFirstTokenConsumed)
551 return false; // Matches something.
552 Toks.push_back(Tok);
553 ConsumeParen();
554 break;
555 case tok::r_square:
556 if (BracketCount && !isFirstTokenConsumed)
557 return false; // Matches something.
558 Toks.push_back(Tok);
559 ConsumeBracket();
560 break;
561 case tok::r_brace:
562 if (BraceCount && !isFirstTokenConsumed)
563 return false; // Matches something.
564 Toks.push_back(Tok);
565 ConsumeBrace();
566 break;
567
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000568 case tok::code_completion:
569 Toks.push_back(Tok);
570 ConsumeCodeCompletionToken();
571 break;
572
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000573 case tok::string_literal:
574 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000575 case tok::utf8_string_literal:
576 case tok::utf16_string_literal:
577 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000578 Toks.push_back(Tok);
579 ConsumeStringToken();
580 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000581 case tok::semi:
582 if (StopAtSemi)
583 return false;
584 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000585 default:
586 // consume this token.
587 Toks.push_back(Tok);
588 ConsumeToken();
589 break;
590 }
591 isFirstTokenConsumed = false;
592 }
593}
Sebastian Redl6df65482011-09-24 17:48:25 +0000594
595/// \brief Consume tokens and store them in the passed token container until
596/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redla891a322011-09-30 08:32:17 +0000597/// the opening brace of the function body. The opening brace will be consumed
598/// if and only if there was no error.
Sebastian Redl6df65482011-09-24 17:48:25 +0000599///
Sebastian Redla891a322011-09-30 08:32:17 +0000600/// \return True on error.
601bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000602 if (Tok.is(tok::kw_try)) {
603 Toks.push_back(Tok);
604 ConsumeToken();
605 }
606 if (Tok.is(tok::colon)) {
607 // Initializers can contain braces too.
608 Toks.push_back(Tok);
609 ConsumeToken();
610
611 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
612 if (Tok.is(tok::eof) || Tok.is(tok::semi))
613 return true;
614
615 // Grab the identifier.
616 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
617 /*StopAtSemi=*/true,
618 /*ConsumeFinalToken=*/false))
619 return true;
620
621 tok::TokenKind kind = Tok.getKind();
622 Toks.push_back(Tok);
623 if (kind == tok::l_paren)
624 ConsumeParen();
625 else {
626 assert(kind == tok::l_brace && "Must be left paren or brace here.");
627 ConsumeBrace();
Sebastian Redla891a322011-09-30 08:32:17 +0000628 // In C++03, this has to be the start of the function body, which
629 // means the initializer is malformed.
630 if (!getLang().CPlusPlus0x)
631 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000632 }
633
634 // Grab the initializer
635 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
636 tok::r_brace,
637 Toks, /*StopAtSemi=*/true))
638 return true;
Sebastian Redla891a322011-09-30 08:32:17 +0000639
640 // Grab the separating comma, if any.
641 if (Tok.is(tok::comma)) {
642 Toks.push_back(Tok);
643 ConsumeToken();
644 }
Sebastian Redl6df65482011-09-24 17:48:25 +0000645 }
646 }
647
Sebastian Redla891a322011-09-30 08:32:17 +0000648 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
649 // brace: an opening one is the function body, while a closing one probably
650 // means we've reached the end of the class.
651 if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
652 /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false))
653 return true;
654 if(Tok.isNot(tok::l_brace))
655 return true;
656
657 Toks.push_back(Tok);
658 ConsumeBrace();
659 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000660}