blob: 04c05d0cc38446968a61119123d93c1604121000 [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,
Nico Weber48673472011-01-28 06:07:34 +000027 const ParsedTemplateInfo &TemplateInfo,
Francois Pichet6a247472011-05-11 02:14:46 +000028 const VirtSpecifiers& VS, ExprResult& Init) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000029 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sean Hunte4246a62011-05-12 06:15:49 +000030 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
31 Tok.is(tok::equal)) &&
32 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000033
John McCallf312b1e2010-08-26 23:41:50 +000034 MultiTemplateParamsArg TemplateParams(Actions,
Sean Hunt4cd84942010-04-14 23:07:37 +000035 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
36 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
37
John McCalld226f652010-08-21 09:40:31 +000038 Decl *FnD;
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +000039 D.setFunctionDefinition(true);
John McCall67d1a672009-08-06 02:15:43 +000040 if (D.getDeclSpec().isFriendSpecified())
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +000041 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
Sean Hunt4cd84942010-04-14 23:07:37 +000042 move(TemplateParams));
Douglas Gregor147545d2011-10-10 14:49:18 +000043 else {
Douglas Gregor23c94db2010-07-02 17:43:08 +000044 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlsson69a87352011-01-20 03:57:25 +000045 move(TemplateParams), 0,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +000046 VS, /*HasDeferredInit=*/false);
Douglas Gregor147545d2011-10-10 14:49:18 +000047 if (FnD) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000048 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
49 false, true);
Douglas Gregor147545d2011-10-10 14:49:18 +000050 bool TypeSpecContainsAuto
51 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +000052 if (Init.isUsable())
Douglas Gregor147545d2011-10-10 14:49:18 +000053 Actions.AddInitializerToDecl(FnD, Init.get(), false,
54 TypeSpecContainsAuto);
55 else
56 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
57 }
Nico Weber48673472011-01-28 06:07:34 +000058 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000059
Eli Friedmand33133c2009-07-22 21:45:50 +000060 HandleMemberFunctionDefaultArgs(D, FnD);
61
John McCalleee1d542011-02-14 07:13:47 +000062 D.complete(FnD);
63
Sean Hunte4246a62011-05-12 06:15:49 +000064 if (Tok.is(tok::equal)) {
65 ConsumeToken();
66
67 bool Delete = false;
68 SourceLocation KWLoc;
69 if (Tok.is(tok::kw_delete)) {
Richard Smith7fe62082011-10-15 05:09:34 +000070 Diag(Tok, getLang().CPlusPlus0x ?
71 diag::warn_cxx98_compat_deleted_function :
72 diag::warn_deleted_function_accepted_as_extension);
Sean Hunte4246a62011-05-12 06:15:49 +000073
74 KWLoc = ConsumeToken();
75 Actions.SetDeclDeleted(FnD, KWLoc);
76 Delete = true;
77 } else if (Tok.is(tok::kw_default)) {
Richard Smith7fe62082011-10-15 05:09:34 +000078 Diag(Tok, getLang().CPlusPlus0x ?
79 diag::warn_cxx98_compat_defaulted_function :
80 diag::warn_defaulted_function_accepted_as_extension);
Sean Hunte4246a62011-05-12 06:15:49 +000081
82 KWLoc = ConsumeToken();
83 Actions.SetDeclDefaulted(FnD, KWLoc);
84 } else {
85 llvm_unreachable("function definition after = not 'delete' or 'default'");
86 }
87
88 if (Tok.is(tok::comma)) {
89 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
90 << Delete;
91 SkipUntil(tok::semi);
92 } else {
93 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
94 Delete ? "delete" : "default", tok::semi);
95 }
96
97 return FnD;
98 }
99
Francois Pichet8387e2a2011-04-22 22:18:13 +0000100 // In delayed template parsing mode, if we are within a class template
101 // or if we are about to parse function member template then consume
102 // the tokens and store them for parsing at the end of the translation unit.
103 if (getLang().DelayedTemplateParsing &&
104 ((Actions.CurContext->isDependentContext() ||
105 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
106 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
107 !D.getDeclSpec().isFriendSpecified()) {
108
109 if (FnD) {
110 LateParsedTemplatedFunction *LPT =
111 new LateParsedTemplatedFunction(this, FnD);
112
113 FunctionDecl *FD = 0;
114 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
115 FD = FunTmpl->getTemplatedDecl();
116 else
117 FD = cast<FunctionDecl>(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +0000118 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000119
120 LateParsedTemplateMap[FD] = LPT;
121 Actions.MarkAsLateParsedTemplate(FD);
122 LexTemplateFunctionForLateParsing(LPT->Toks);
123 } else {
124 CachedTokens Toks;
125 LexTemplateFunctionForLateParsing(Toks);
126 }
127
128 return FnD;
129 }
130
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000131 // Consume the tokens and store them for later parsing.
132
Douglas Gregord54eb442010-10-12 16:25:54 +0000133 LexedMethod* LM = new LexedMethod(this, FnD);
134 getCurrentClass().LateParsedDeclarations.push_back(LM);
135 LM->TemplateScope = getCurScope()->isTemplateParamScope();
136 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000137
Sebastian Redld3a413d2009-04-26 20:35:05 +0000138 tok::TokenKind kind = Tok.getKind();
Sebastian Redla891a322011-09-30 08:32:17 +0000139 // Consume everything up to (and including) the left brace of the
140 // function body.
141 if (ConsumeAndStoreFunctionPrologue(Toks)) {
142 // We didn't find the left-brace we expected after the
143 // constructor initializer.
144 if (Tok.is(tok::semi)) {
145 // We found a semicolon; complain, consume the semicolon, and
146 // don't try to parse this method later.
147 Diag(Tok.getLocation(), diag::err_expected_lbrace);
148 ConsumeAnyToken();
149 delete getCurrentClass().LateParsedDeclarations.back();
150 getCurrentClass().LateParsedDeclarations.pop_back();
151 return FnD;
Douglas Gregor3f08d182008-11-10 16:59:40 +0000152 }
Douglas Gregor7ad83902008-11-05 04:29:56 +0000153 } else {
Sebastian Redla891a322011-09-30 08:32:17 +0000154 // Consume everything up to (and including) the matching right brace.
155 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000156 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000157
Sebastian Redld3a413d2009-04-26 20:35:05 +0000158 // If we're in a function-try-block, we need to store all the catch blocks.
159 if (kind == tok::kw_try) {
160 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000161 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
162 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000163 }
164 }
165
Douglas Gregor87f10642011-04-14 23:19:27 +0000166
167 if (!FnD) {
168 // If semantic analysis could not build a function declaration,
169 // just throw away the late-parsed declaration.
170 delete getCurrentClass().LateParsedDeclarations.back();
171 getCurrentClass().LateParsedDeclarations.pop_back();
172 }
173
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000174 return FnD;
175}
176
Richard Smith7a614d82011-06-11 17:19:42 +0000177/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
178/// specified Declarator is a well formed C++ non-static data member
179/// declaration. Now lex its initializer and store its tokens for parsing
180/// after the class is complete.
181void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
182 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
183 "Current token not a '{' or '='!");
184
185 LateParsedMemberInitializer *MI =
186 new LateParsedMemberInitializer(this, VarD);
187 getCurrentClass().LateParsedDeclarations.push_back(MI);
188 CachedTokens &Toks = MI->Toks;
189
190 tok::TokenKind kind = Tok.getKind();
191 if (kind == tok::equal) {
192 Toks.push_back(Tok);
193 ConsumeAnyToken();
194 }
195
196 if (kind == tok::l_brace) {
197 // Begin by storing the '{' token.
198 Toks.push_back(Tok);
199 ConsumeBrace();
200
201 // Consume everything up to (and including) the matching right brace.
202 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
203 } else {
204 // Consume everything up to (but excluding) the comma or semicolon.
205 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
206 /*ConsumeFinalToken=*/false);
207 }
208
209 // Store an artificial EOF token to ensure that we don't run off the end of
210 // the initializer when we come to parse it.
211 Token Eof;
212 Eof.startToken();
213 Eof.setKind(tok::eof);
214 Eof.setLocation(Tok.getLocation());
215 Toks.push_back(Eof);
216}
217
Douglas Gregord54eb442010-10-12 16:25:54 +0000218Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
219void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000220void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000221void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
222
223Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
224 : Self(P), Class(C) {}
225
226Parser::LateParsedClass::~LateParsedClass() {
227 Self->DeallocateParsedClasses(Class);
228}
229
230void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
231 Self->ParseLexedMethodDeclarations(*Class);
232}
233
Richard Smith7a614d82011-06-11 17:19:42 +0000234void Parser::LateParsedClass::ParseLexedMemberInitializers() {
235 Self->ParseLexedMemberInitializers(*Class);
236}
237
Douglas Gregord54eb442010-10-12 16:25:54 +0000238void Parser::LateParsedClass::ParseLexedMethodDefs() {
239 Self->ParseLexedMethodDefs(*Class);
240}
241
242void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
243 Self->ParseLexedMethodDeclaration(*this);
244}
245
246void Parser::LexedMethod::ParseLexedMethodDefs() {
247 Self->ParseLexedMethodDef(*this);
248}
249
Richard Smith7a614d82011-06-11 17:19:42 +0000250void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
251 Self->ParseLexedMemberInitializer(*this);
252}
253
Douglas Gregor72b505b2008-12-16 21:30:33 +0000254/// ParseLexedMethodDeclarations - We finished parsing the member
255/// specification of a top (non-nested) C++ class. Now go over the
256/// stack of method declarations with some parts for which parsing was
257/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000258void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
259 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000260 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000261 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000262 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000263
John McCall7a1dc562009-12-19 10:49:29 +0000264 // The current scope is still active if we're the top-level class.
265 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000266 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000267 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
268 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000269 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000270 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000271
Douglas Gregord54eb442010-10-12 16:25:54 +0000272 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
273 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000274 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000275
John McCall7a1dc562009-12-19 10:49:29 +0000276 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000277 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000278}
279
Douglas Gregord54eb442010-10-12 16:25:54 +0000280void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
281 // If this is a member template, introduce the template parameter scope.
282 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
283 if (LM.TemplateScope)
284 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
285
286 // Start the delayed C++ method declaration
287 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
288
289 // Introduce the parameters into scope and parse their default
290 // arguments.
291 ParseScope PrototypeScope(this,
292 Scope::FunctionPrototypeScope|Scope::DeclScope);
293 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
294 // Introduce the parameter into scope.
295 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
296
297 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
298 // Save the current token position.
299 SourceLocation origLoc = Tok.getLocation();
300
301 // Parse the default argument from its saved token stream.
302 Toks->push_back(Tok); // So that the current token doesn't get lost
303 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
304
305 // Consume the previously-pushed token.
306 ConsumeAnyToken();
307
308 // Consume the '='.
309 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
310 SourceLocation EqualLoc = ConsumeToken();
311
312 // The argument isn't actually potentially evaluated unless it is
313 // used.
314 EnterExpressionEvaluationContext Eval(Actions,
315 Sema::PotentiallyEvaluatedIfUsed);
316
317 ExprResult DefArgResult(ParseAssignmentExpression());
318 if (DefArgResult.isInvalid())
319 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
320 else {
321 if (Tok.is(tok::cxx_defaultarg_end))
322 ConsumeToken();
323 else
324 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
325 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
326 DefArgResult.take());
327 }
328
329 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
330 Tok.getLocation()) &&
331 "ParseAssignmentExpression went over the default arg tokens!");
332 // There could be leftover tokens (e.g. because of an error).
333 // Skip through until we reach the original token position.
334 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
335 ConsumeAnyToken();
336
337 delete Toks;
338 LM.DefaultArgs[I].Toks = 0;
339 }
340 }
341 PrototypeScope.Exit();
342
343 // Finish the delayed C++ method declaration.
344 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
345}
346
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000347/// ParseLexedMethodDefs - We finished parsing the member specification of a top
348/// (non-nested) C++ class. Now go over the stack of lexed methods that were
349/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000350void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
351 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000352 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000353 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000354 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000355
356 bool HasClassScope = !Class.TopLevelClass;
357 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
358 HasClassScope);
359
Douglas Gregord54eb442010-10-12 16:25:54 +0000360 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
361 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
362 }
363}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000364
Douglas Gregord54eb442010-10-12 16:25:54 +0000365void Parser::ParseLexedMethodDef(LexedMethod &LM) {
366 // If this is a member template, introduce the template parameter scope.
367 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
368 if (LM.TemplateScope)
369 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Douglas Gregord54eb442010-10-12 16:25:54 +0000371 // Save the current token position.
372 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000373
Douglas Gregord54eb442010-10-12 16:25:54 +0000374 assert(!LM.Toks.empty() && "Empty body!");
375 // Append the current token at the end of the new token stream so that it
376 // doesn't get lost.
377 LM.Toks.push_back(Tok);
378 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000379
Douglas Gregord54eb442010-10-12 16:25:54 +0000380 // Consume the previously pushed token.
381 ConsumeAnyToken();
382 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
383 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000384
Douglas Gregord54eb442010-10-12 16:25:54 +0000385 // Parse the method body. Function body parsing code is similar enough
386 // to be re-used for method bodies as well.
387 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
388 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000389
Douglas Gregord54eb442010-10-12 16:25:54 +0000390 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000391 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000392 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
393 Tok.getLocation()) &&
394 "ParseFunctionTryBlock went over the cached tokens!");
395 // There could be leftover tokens (e.g. because of an error).
396 // Skip through until we reach the original token position.
397 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
398 ConsumeAnyToken();
399 return;
400 }
401 if (Tok.is(tok::colon)) {
402 ParseConstructorInitializer(LM.D);
403
404 // Error recovery.
405 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000406 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000407 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000408 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
409 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000410 return;
411 }
412 } else
413 Actions.ActOnDefaultCtorInitializers(LM.D);
414
Douglas Gregorc9977d02011-03-16 17:05:57 +0000415 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000416
417 if (Tok.getLocation() != origLoc) {
418 // Due to parsing error, we either went over the cached tokens or
419 // there are still cached tokens left. If it's the latter case skip the
420 // leftover tokens.
421 // Since this is an uncommon situation that should be avoided, use the
422 // expensive isBeforeInTranslationUnit call.
423 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
424 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000425 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000426 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000427 }
428}
429
Richard Smith7a614d82011-06-11 17:19:42 +0000430/// ParseLexedMemberInitializers - We finished parsing the member specification
431/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
432/// initializers that were collected during its parsing and parse them all.
433void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
434 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
435 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
436 HasTemplateScope);
437 if (HasTemplateScope)
438 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
439
440 // Set or update the scope flags to include Scope::ThisScope.
441 bool AlreadyHasClassScope = Class.TopLevelClass;
442 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
443 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
444 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
445
446 if (!AlreadyHasClassScope)
447 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
448 Class.TagOrTemplate);
449
450 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
451 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
452 }
453
454 if (!AlreadyHasClassScope)
455 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
456 Class.TagOrTemplate);
457
458 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
459}
460
461void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000462 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000463 return;
464
465 // Append the current token at the end of the new token stream so that it
466 // doesn't get lost.
467 MI.Toks.push_back(Tok);
468 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
469
470 // Consume the previously pushed token.
471 ConsumeAnyToken();
472
473 SourceLocation EqualLoc;
474 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
475
476 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
477
478 // The next token should be our artificial terminating EOF token.
479 if (Tok.isNot(tok::eof)) {
480 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
481 if (!EndLoc.isValid())
482 EndLoc = Tok.getLocation();
483 // No fixit; we can't recover as if there were a semicolon here.
484 Diag(EndLoc, diag::err_expected_semi_decl_list);
485
486 // Consume tokens until we hit the artificial EOF.
487 while (Tok.isNot(tok::eof))
488 ConsumeAnyToken();
489 }
490 ConsumeAnyToken();
491}
492
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000493/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000494/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000495/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000496/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000497/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000498/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000499bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
500 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000501 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000502 // We always want this function to consume at least one token if the first
503 // token isn't T and if not at EOF.
504 bool isFirstTokenConsumed = true;
505 while (1) {
506 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000507 if (Tok.is(T1) || Tok.is(T2)) {
508 if (ConsumeFinalToken) {
509 Toks.push_back(Tok);
510 ConsumeAnyToken();
511 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000512 return true;
513 }
514
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000515 switch (Tok.getKind()) {
516 case tok::eof:
517 // Ran out of tokens.
518 return false;
519
520 case tok::l_paren:
521 // Recursively consume properly-nested parens.
522 Toks.push_back(Tok);
523 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000524 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000525 break;
526 case tok::l_square:
527 // Recursively consume properly-nested square brackets.
528 Toks.push_back(Tok);
529 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000530 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000531 break;
532 case tok::l_brace:
533 // Recursively consume properly-nested braces.
534 Toks.push_back(Tok);
535 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000536 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000537 break;
538
539 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
540 // Since the user wasn't looking for this token (if they were, it would
541 // already be handled), this isn't balanced. If there is a LHS token at a
542 // higher level, we will assume that this matches the unbalanced token
543 // and return it. Otherwise, this is a spurious RHS token, which we skip.
544 case tok::r_paren:
545 if (ParenCount && !isFirstTokenConsumed)
546 return false; // Matches something.
547 Toks.push_back(Tok);
548 ConsumeParen();
549 break;
550 case tok::r_square:
551 if (BracketCount && !isFirstTokenConsumed)
552 return false; // Matches something.
553 Toks.push_back(Tok);
554 ConsumeBracket();
555 break;
556 case tok::r_brace:
557 if (BraceCount && !isFirstTokenConsumed)
558 return false; // Matches something.
559 Toks.push_back(Tok);
560 ConsumeBrace();
561 break;
562
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000563 case tok::code_completion:
564 Toks.push_back(Tok);
565 ConsumeCodeCompletionToken();
566 break;
567
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000568 case tok::string_literal:
569 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000570 case tok::utf8_string_literal:
571 case tok::utf16_string_literal:
572 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000573 Toks.push_back(Tok);
574 ConsumeStringToken();
575 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000576 case tok::semi:
577 if (StopAtSemi)
578 return false;
579 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000580 default:
581 // consume this token.
582 Toks.push_back(Tok);
583 ConsumeToken();
584 break;
585 }
586 isFirstTokenConsumed = false;
587 }
588}
Sebastian Redl6df65482011-09-24 17:48:25 +0000589
590/// \brief Consume tokens and store them in the passed token container until
591/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redla891a322011-09-30 08:32:17 +0000592/// the opening brace of the function body. The opening brace will be consumed
593/// if and only if there was no error.
Sebastian Redl6df65482011-09-24 17:48:25 +0000594///
Sebastian Redla891a322011-09-30 08:32:17 +0000595/// \return True on error.
596bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000597 if (Tok.is(tok::kw_try)) {
598 Toks.push_back(Tok);
599 ConsumeToken();
600 }
601 if (Tok.is(tok::colon)) {
602 // Initializers can contain braces too.
603 Toks.push_back(Tok);
604 ConsumeToken();
605
606 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
607 if (Tok.is(tok::eof) || Tok.is(tok::semi))
608 return true;
609
610 // Grab the identifier.
611 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
612 /*StopAtSemi=*/true,
613 /*ConsumeFinalToken=*/false))
614 return true;
615
616 tok::TokenKind kind = Tok.getKind();
617 Toks.push_back(Tok);
618 if (kind == tok::l_paren)
619 ConsumeParen();
620 else {
621 assert(kind == tok::l_brace && "Must be left paren or brace here.");
622 ConsumeBrace();
Sebastian Redla891a322011-09-30 08:32:17 +0000623 // In C++03, this has to be the start of the function body, which
624 // means the initializer is malformed.
625 if (!getLang().CPlusPlus0x)
626 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000627 }
628
629 // Grab the initializer
630 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
631 tok::r_brace,
632 Toks, /*StopAtSemi=*/true))
633 return true;
Sebastian Redla891a322011-09-30 08:32:17 +0000634
635 // Grab the separating comma, if any.
636 if (Tok.is(tok::comma)) {
637 Toks.push_back(Tok);
638 ConsumeToken();
639 }
Sebastian Redl6df65482011-09-24 17:48:25 +0000640 }
641 }
642
Sebastian Redla891a322011-09-30 08:32:17 +0000643 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
644 // brace: an opening one is the function body, while a closing one probably
645 // means we've reached the end of the class.
646 if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
647 /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false))
648 return true;
649 if(Tok.isNot(tok::l_brace))
650 return true;
651
652 Toks.push_back(Tok);
653 ConsumeBrace();
654 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000655}