blob: 923a5587e6c31a5d34dcbff514775db2e35cfba4 [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.
John McCalleee1d542011-02-14 07:13:47 +000024Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, ParsingDeclarator &D,
Nico Weber48673472011-01-28 06:07:34 +000025 const ParsedTemplateInfo &TemplateInfo,
Francois Pichet6a247472011-05-11 02:14:46 +000026 const VirtSpecifiers& VS, ExprResult& Init) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000027 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sean Hunte4246a62011-05-12 06:15:49 +000028 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
29 Tok.is(tok::equal)) &&
30 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000031
John McCallf312b1e2010-08-26 23:41:50 +000032 MultiTemplateParamsArg TemplateParams(Actions,
Sean Hunt4cd84942010-04-14 23:07:37 +000033 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
34 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
35
John McCalld226f652010-08-21 09:40:31 +000036 Decl *FnD;
John McCall67d1a672009-08-06 02:15:43 +000037 if (D.getDeclSpec().isFriendSpecified())
Douglas Gregor37b372b2009-08-20 22:52:58 +000038 // FIXME: Friend templates
Douglas Gregor23c94db2010-07-02 17:43:08 +000039 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true,
Sean Hunt4cd84942010-04-14 23:07:37 +000040 move(TemplateParams));
Nico Weber48673472011-01-28 06:07:34 +000041 else { // FIXME: pass template information through
Douglas Gregor23c94db2010-07-02 17:43:08 +000042 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlsson69a87352011-01-20 03:57:25 +000043 move(TemplateParams), 0,
Francois Pichet6a247472011-05-11 02:14:46 +000044 VS, Init.release(),
Richard Smith7a614d82011-06-11 17:19:42 +000045 /*HasInit=*/false,
Francois Pichet6a247472011-05-11 02:14:46 +000046 /*IsDefinition*/true);
Nico Weber48673472011-01-28 06:07:34 +000047 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000048
Eli Friedmand33133c2009-07-22 21:45:50 +000049 HandleMemberFunctionDefaultArgs(D, FnD);
50
John McCalleee1d542011-02-14 07:13:47 +000051 D.complete(FnD);
52
Sean Hunte4246a62011-05-12 06:15:49 +000053 if (Tok.is(tok::equal)) {
54 ConsumeToken();
55
56 bool Delete = false;
57 SourceLocation KWLoc;
58 if (Tok.is(tok::kw_delete)) {
59 if (!getLang().CPlusPlus0x)
60 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
61
62 KWLoc = ConsumeToken();
63 Actions.SetDeclDeleted(FnD, KWLoc);
64 Delete = true;
65 } else if (Tok.is(tok::kw_default)) {
66 if (!getLang().CPlusPlus0x)
67 Diag(Tok, diag::warn_defaulted_function_accepted_as_extension);
68
69 KWLoc = ConsumeToken();
70 Actions.SetDeclDefaulted(FnD, KWLoc);
71 } else {
72 llvm_unreachable("function definition after = not 'delete' or 'default'");
73 }
74
75 if (Tok.is(tok::comma)) {
76 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
77 << Delete;
78 SkipUntil(tok::semi);
79 } else {
80 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
81 Delete ? "delete" : "default", tok::semi);
82 }
83
84 return FnD;
85 }
86
Francois Pichet8387e2a2011-04-22 22:18:13 +000087 // In delayed template parsing mode, if we are within a class template
88 // or if we are about to parse function member template then consume
89 // the tokens and store them for parsing at the end of the translation unit.
90 if (getLang().DelayedTemplateParsing &&
91 ((Actions.CurContext->isDependentContext() ||
92 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
93 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
94 !D.getDeclSpec().isFriendSpecified()) {
95
96 if (FnD) {
97 LateParsedTemplatedFunction *LPT =
98 new LateParsedTemplatedFunction(this, FnD);
99
100 FunctionDecl *FD = 0;
101 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
102 FD = FunTmpl->getTemplatedDecl();
103 else
104 FD = cast<FunctionDecl>(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +0000105 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000106
107 LateParsedTemplateMap[FD] = LPT;
108 Actions.MarkAsLateParsedTemplate(FD);
109 LexTemplateFunctionForLateParsing(LPT->Toks);
110 } else {
111 CachedTokens Toks;
112 LexTemplateFunctionForLateParsing(Toks);
113 }
114
115 return FnD;
116 }
117
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000118 // Consume the tokens and store them for later parsing.
119
Douglas Gregord54eb442010-10-12 16:25:54 +0000120 LexedMethod* LM = new LexedMethod(this, FnD);
121 getCurrentClass().LateParsedDeclarations.push_back(LM);
122 LM->TemplateScope = getCurScope()->isTemplateParamScope();
123 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000124
Sebastian Redld3a413d2009-04-26 20:35:05 +0000125 tok::TokenKind kind = Tok.getKind();
126 // We may have a constructor initializer or function-try-block here.
127 if (kind == tok::colon || kind == tok::kw_try) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000128 // Consume everything up to (and including) the left brace of the
129 // function body.
130 if (ConsumeAndStoreTryAndInitializers(Toks)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +0000131 // We didn't find the left-brace we expected after the
Mike Stump1eb44332009-09-09 15:08:12 +0000132 // constructor initializer.
Douglas Gregor3f08d182008-11-10 16:59:40 +0000133 if (Tok.is(tok::semi)) {
134 // We found a semicolon; complain, consume the semicolon, and
135 // don't try to parse this method later.
136 Diag(Tok.getLocation(), diag::err_expected_lbrace);
137 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000138 delete getCurrentClass().LateParsedDeclarations.back();
139 getCurrentClass().LateParsedDeclarations.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +0000140 return FnD;
141 }
142 }
143
Douglas Gregor7ad83902008-11-05 04:29:56 +0000144 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000145 // Begin by storing the '{' token.
Douglas Gregor7ad83902008-11-05 04:29:56 +0000146 Toks.push_back(Tok);
147 ConsumeBrace();
148 }
149 // Consume everything up to (and including) the matching right brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000150 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000151
Sebastian Redld3a413d2009-04-26 20:35:05 +0000152 // If we're in a function-try-block, we need to store all the catch blocks.
153 if (kind == tok::kw_try) {
154 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000155 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
156 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000157 }
158 }
159
Douglas Gregor87f10642011-04-14 23:19:27 +0000160
161 if (!FnD) {
162 // If semantic analysis could not build a function declaration,
163 // just throw away the late-parsed declaration.
164 delete getCurrentClass().LateParsedDeclarations.back();
165 getCurrentClass().LateParsedDeclarations.pop_back();
166 }
167
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000168 return FnD;
169}
170
Richard Smith7a614d82011-06-11 17:19:42 +0000171/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
172/// specified Declarator is a well formed C++ non-static data member
173/// declaration. Now lex its initializer and store its tokens for parsing
174/// after the class is complete.
175void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
176 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
177 "Current token not a '{' or '='!");
178
179 LateParsedMemberInitializer *MI =
180 new LateParsedMemberInitializer(this, VarD);
181 getCurrentClass().LateParsedDeclarations.push_back(MI);
182 CachedTokens &Toks = MI->Toks;
183
184 tok::TokenKind kind = Tok.getKind();
185 if (kind == tok::equal) {
186 Toks.push_back(Tok);
187 ConsumeAnyToken();
188 }
189
190 if (kind == tok::l_brace) {
191 // Begin by storing the '{' token.
192 Toks.push_back(Tok);
193 ConsumeBrace();
194
195 // Consume everything up to (and including) the matching right brace.
196 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
197 } else {
198 // Consume everything up to (but excluding) the comma or semicolon.
199 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
200 /*ConsumeFinalToken=*/false);
201 }
202
203 // Store an artificial EOF token to ensure that we don't run off the end of
204 // the initializer when we come to parse it.
205 Token Eof;
206 Eof.startToken();
207 Eof.setKind(tok::eof);
208 Eof.setLocation(Tok.getLocation());
209 Toks.push_back(Eof);
210}
211
Douglas Gregord54eb442010-10-12 16:25:54 +0000212Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
213void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000214void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000215void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
216
217Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
218 : Self(P), Class(C) {}
219
220Parser::LateParsedClass::~LateParsedClass() {
221 Self->DeallocateParsedClasses(Class);
222}
223
224void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
225 Self->ParseLexedMethodDeclarations(*Class);
226}
227
Richard Smith7a614d82011-06-11 17:19:42 +0000228void Parser::LateParsedClass::ParseLexedMemberInitializers() {
229 Self->ParseLexedMemberInitializers(*Class);
230}
231
Douglas Gregord54eb442010-10-12 16:25:54 +0000232void Parser::LateParsedClass::ParseLexedMethodDefs() {
233 Self->ParseLexedMethodDefs(*Class);
234}
235
236void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
237 Self->ParseLexedMethodDeclaration(*this);
238}
239
240void Parser::LexedMethod::ParseLexedMethodDefs() {
241 Self->ParseLexedMethodDef(*this);
242}
243
Richard Smith7a614d82011-06-11 17:19:42 +0000244void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
245 Self->ParseLexedMemberInitializer(*this);
246}
247
Douglas Gregor72b505b2008-12-16 21:30:33 +0000248/// ParseLexedMethodDeclarations - We finished parsing the member
249/// specification of a top (non-nested) C++ class. Now go over the
250/// stack of method declarations with some parts for which parsing was
251/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000252void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
253 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000254 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000255 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000256 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000257
John McCall7a1dc562009-12-19 10:49:29 +0000258 // The current scope is still active if we're the top-level class.
259 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000260 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000261 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
262 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000263 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000265
Douglas Gregord54eb442010-10-12 16:25:54 +0000266 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
267 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000268 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000269
John McCall7a1dc562009-12-19 10:49:29 +0000270 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000271 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000272}
273
Douglas Gregord54eb442010-10-12 16:25:54 +0000274void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
275 // If this is a member template, introduce the template parameter scope.
276 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
277 if (LM.TemplateScope)
278 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
279
280 // Start the delayed C++ method declaration
281 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
282
283 // Introduce the parameters into scope and parse their default
284 // arguments.
285 ParseScope PrototypeScope(this,
286 Scope::FunctionPrototypeScope|Scope::DeclScope);
287 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
288 // Introduce the parameter into scope.
289 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
290
291 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
292 // Save the current token position.
293 SourceLocation origLoc = Tok.getLocation();
294
295 // Parse the default argument from its saved token stream.
296 Toks->push_back(Tok); // So that the current token doesn't get lost
297 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
298
299 // Consume the previously-pushed token.
300 ConsumeAnyToken();
301
302 // Consume the '='.
303 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
304 SourceLocation EqualLoc = ConsumeToken();
305
306 // The argument isn't actually potentially evaluated unless it is
307 // used.
308 EnterExpressionEvaluationContext Eval(Actions,
309 Sema::PotentiallyEvaluatedIfUsed);
310
311 ExprResult DefArgResult(ParseAssignmentExpression());
312 if (DefArgResult.isInvalid())
313 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
314 else {
315 if (Tok.is(tok::cxx_defaultarg_end))
316 ConsumeToken();
317 else
318 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
319 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
320 DefArgResult.take());
321 }
322
323 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
324 Tok.getLocation()) &&
325 "ParseAssignmentExpression went over the default arg tokens!");
326 // There could be leftover tokens (e.g. because of an error).
327 // Skip through until we reach the original token position.
328 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
329 ConsumeAnyToken();
330
331 delete Toks;
332 LM.DefaultArgs[I].Toks = 0;
333 }
334 }
335 PrototypeScope.Exit();
336
337 // Finish the delayed C++ method declaration.
338 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
339}
340
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000341/// ParseLexedMethodDefs - We finished parsing the member specification of a top
342/// (non-nested) C++ class. Now go over the stack of lexed methods that were
343/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000344void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
345 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000346 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000347 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000348 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000349
350 bool HasClassScope = !Class.TopLevelClass;
351 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
352 HasClassScope);
353
Douglas Gregord54eb442010-10-12 16:25:54 +0000354 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
355 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
356 }
357}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000358
Douglas Gregord54eb442010-10-12 16:25:54 +0000359void Parser::ParseLexedMethodDef(LexedMethod &LM) {
360 // If this is a member template, introduce the template parameter scope.
361 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
362 if (LM.TemplateScope)
363 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Douglas Gregord54eb442010-10-12 16:25:54 +0000365 // Save the current token position.
366 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000367
Douglas Gregord54eb442010-10-12 16:25:54 +0000368 assert(!LM.Toks.empty() && "Empty body!");
369 // Append the current token at the end of the new token stream so that it
370 // doesn't get lost.
371 LM.Toks.push_back(Tok);
372 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000373
Douglas Gregord54eb442010-10-12 16:25:54 +0000374 // Consume the previously pushed token.
375 ConsumeAnyToken();
376 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
377 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000378
Douglas Gregord54eb442010-10-12 16:25:54 +0000379 // Parse the method body. Function body parsing code is similar enough
380 // to be re-used for method bodies as well.
381 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
382 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000383
Douglas Gregord54eb442010-10-12 16:25:54 +0000384 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000385 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000386 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
387 Tok.getLocation()) &&
388 "ParseFunctionTryBlock went over the cached tokens!");
389 // There could be leftover tokens (e.g. because of an error).
390 // Skip through until we reach the original token position.
391 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
392 ConsumeAnyToken();
393 return;
394 }
395 if (Tok.is(tok::colon)) {
396 ParseConstructorInitializer(LM.D);
397
398 // Error recovery.
399 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000400 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000401 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000402 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
403 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000404 return;
405 }
406 } else
407 Actions.ActOnDefaultCtorInitializers(LM.D);
408
Douglas Gregorc9977d02011-03-16 17:05:57 +0000409 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000410
411 if (Tok.getLocation() != origLoc) {
412 // Due to parsing error, we either went over the cached tokens or
413 // there are still cached tokens left. If it's the latter case skip the
414 // leftover tokens.
415 // Since this is an uncommon situation that should be avoided, use the
416 // expensive isBeforeInTranslationUnit call.
417 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
418 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000419 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000420 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000421 }
422}
423
Richard Smith7a614d82011-06-11 17:19:42 +0000424/// ParseLexedMemberInitializers - We finished parsing the member specification
425/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
426/// initializers that were collected during its parsing and parse them all.
427void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
428 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
429 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
430 HasTemplateScope);
431 if (HasTemplateScope)
432 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
433
434 // Set or update the scope flags to include Scope::ThisScope.
435 bool AlreadyHasClassScope = Class.TopLevelClass;
436 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
437 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
438 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
439
440 if (!AlreadyHasClassScope)
441 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
442 Class.TagOrTemplate);
443
444 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
445 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
446 }
447
448 if (!AlreadyHasClassScope)
449 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
450 Class.TagOrTemplate);
451
452 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
453}
454
455void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000456 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000457 return;
458
459 // Append the current token at the end of the new token stream so that it
460 // doesn't get lost.
461 MI.Toks.push_back(Tok);
462 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
463
464 // Consume the previously pushed token.
465 ConsumeAnyToken();
466
467 SourceLocation EqualLoc;
468 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
469
470 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
471
472 // The next token should be our artificial terminating EOF token.
473 if (Tok.isNot(tok::eof)) {
474 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
475 if (!EndLoc.isValid())
476 EndLoc = Tok.getLocation();
477 // No fixit; we can't recover as if there were a semicolon here.
478 Diag(EndLoc, diag::err_expected_semi_decl_list);
479
480 // Consume tokens until we hit the artificial EOF.
481 while (Tok.isNot(tok::eof))
482 ConsumeAnyToken();
483 }
484 ConsumeAnyToken();
485}
486
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000487/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000488/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000489/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000490/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000491/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000492/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000493bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
494 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000495 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000496 // We always want this function to consume at least one token if the first
497 // token isn't T and if not at EOF.
498 bool isFirstTokenConsumed = true;
499 while (1) {
500 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000501 if (Tok.is(T1) || Tok.is(T2)) {
502 if (ConsumeFinalToken) {
503 Toks.push_back(Tok);
504 ConsumeAnyToken();
505 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000506 return true;
507 }
508
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000509 switch (Tok.getKind()) {
510 case tok::eof:
511 // Ran out of tokens.
512 return false;
513
514 case tok::l_paren:
515 // Recursively consume properly-nested parens.
516 Toks.push_back(Tok);
517 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000518 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000519 break;
520 case tok::l_square:
521 // Recursively consume properly-nested square brackets.
522 Toks.push_back(Tok);
523 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000524 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000525 break;
526 case tok::l_brace:
527 // Recursively consume properly-nested braces.
528 Toks.push_back(Tok);
529 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000530 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000531 break;
532
533 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
534 // Since the user wasn't looking for this token (if they were, it would
535 // already be handled), this isn't balanced. If there is a LHS token at a
536 // higher level, we will assume that this matches the unbalanced token
537 // and return it. Otherwise, this is a spurious RHS token, which we skip.
538 case tok::r_paren:
539 if (ParenCount && !isFirstTokenConsumed)
540 return false; // Matches something.
541 Toks.push_back(Tok);
542 ConsumeParen();
543 break;
544 case tok::r_square:
545 if (BracketCount && !isFirstTokenConsumed)
546 return false; // Matches something.
547 Toks.push_back(Tok);
548 ConsumeBracket();
549 break;
550 case tok::r_brace:
551 if (BraceCount && !isFirstTokenConsumed)
552 return false; // Matches something.
553 Toks.push_back(Tok);
554 ConsumeBrace();
555 break;
556
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000557 case tok::code_completion:
558 Toks.push_back(Tok);
559 ConsumeCodeCompletionToken();
560 break;
561
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000562 case tok::string_literal:
563 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000564 case tok::utf8_string_literal:
565 case tok::utf16_string_literal:
566 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000567 Toks.push_back(Tok);
568 ConsumeStringToken();
569 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000570 case tok::semi:
571 if (StopAtSemi)
572 return false;
573 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000574 default:
575 // consume this token.
576 Toks.push_back(Tok);
577 ConsumeToken();
578 break;
579 }
580 isFirstTokenConsumed = false;
581 }
582}
Sebastian Redl6df65482011-09-24 17:48:25 +0000583
584/// \brief Consume tokens and store them in the passed token container until
585/// we've passed the try keyword and constructor initializers and have consumed
586/// the opening brace of the function body.
587///
588/// \return True on error.
589bool Parser::ConsumeAndStoreTryAndInitializers(CachedTokens &Toks) {
590 if (Tok.is(tok::kw_try)) {
591 Toks.push_back(Tok);
592 ConsumeToken();
593 }
594 if (Tok.is(tok::colon)) {
595 // Initializers can contain braces too.
596 Toks.push_back(Tok);
597 ConsumeToken();
598
599 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
600 if (Tok.is(tok::eof) || Tok.is(tok::semi))
601 return true;
602
603 // Grab the identifier.
604 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
605 /*StopAtSemi=*/true,
606 /*ConsumeFinalToken=*/false))
607 return true;
608
609 tok::TokenKind kind = Tok.getKind();
610 Toks.push_back(Tok);
611 if (kind == tok::l_paren)
612 ConsumeParen();
613 else {
614 assert(kind == tok::l_brace && "Must be left paren or brace here.");
615 ConsumeBrace();
616 }
617
618 // Grab the initializer
619 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
620 tok::r_brace,
621 Toks, /*StopAtSemi=*/true))
622 return true;
623 }
624 }
625
626 // Grab any remaining garbage to be diagnosed later, and the opening
627 // brace of the function body.
628 return !ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/true);
629}