blob: b387e9e551d4b6df129630b717351e081864acf7 [file] [log] [blame]
Argyrios Kyrtzidis7bbb20e2008-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 Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000015#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/DeclSpec.h"
17#include "clang/Sema/Scope.h"
Francois Pichet1c229c02011-04-22 22:18:13 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000019using namespace clang;
20
Sebastian Redla7b98a72009-04-26 20:35:05 +000021/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis7bbb20e2008-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 Verbruggenca98f2a2011-10-13 09:41:32 +000024Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
25 AttributeList *AccessAttrs,
26 ParsingDeclarator &D,
Nico Weber24b2a822011-01-28 06:07:34 +000027 const ParsedTemplateInfo &TemplateInfo,
Francois Pichet3abc9b82011-05-11 02:14:46 +000028 const VirtSpecifiers& VS, ExprResult& Init) {
Abramo Bagnara924a8f32010-12-10 16:29:40 +000029 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Alexis Hunt5a7fa252011-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 Kyrtzidis7bbb20e2008-06-24 22:12:16 +000033
John McCallfaf5fb42010-08-26 23:41:50 +000034 MultiTemplateParamsArg TemplateParams(Actions,
Alexis Hunt1deb9722010-04-14 23:07:37 +000035 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
36 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
37
John McCall48871652010-08-21 09:40:31 +000038 Decl *FnD;
Kaelyn Uhrain4dc695d2011-10-11 00:28:45 +000039 D.setFunctionDefinition(true);
John McCall07e91c02009-08-06 02:15:43 +000040 if (D.getDeclSpec().isFriendSpecified())
Kaelyn Uhrain4dc695d2011-10-11 00:28:45 +000041 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
Alexis Hunt1deb9722010-04-14 23:07:37 +000042 move(TemplateParams));
Douglas Gregor728d00b2011-10-10 14:49:18 +000043 else {
Douglas Gregor0be31a22010-07-02 17:43:08 +000044 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlssondb36b802011-01-20 03:57:25 +000045 move(TemplateParams), 0,
Kaelyn Uhrain4dc695d2011-10-11 00:28:45 +000046 VS, /*HasInit=*/false);
Douglas Gregor728d00b2011-10-10 14:49:18 +000047 if (FnD) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +000048 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
49 false, true);
Douglas Gregor728d00b2011-10-10 14:49:18 +000050 bool TypeSpecContainsAuto
51 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
52 if (Init.get())
53 Actions.AddInitializerToDecl(FnD, Init.get(), false,
54 TypeSpecContainsAuto);
55 else
56 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
57 }
Nico Weber24b2a822011-01-28 06:07:34 +000058 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000059
Eli Friedman3af2a772009-07-22 21:45:50 +000060 HandleMemberFunctionDefaultArgs(D, FnD);
61
John McCallc1465822011-02-14 07:13:47 +000062 D.complete(FnD);
63
Alexis Hunt5a7fa252011-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)) {
70 if (!getLang().CPlusPlus0x)
71 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
72
73 KWLoc = ConsumeToken();
74 Actions.SetDeclDeleted(FnD, KWLoc);
75 Delete = true;
76 } else if (Tok.is(tok::kw_default)) {
77 if (!getLang().CPlusPlus0x)
78 Diag(Tok, diag::warn_defaulted_function_accepted_as_extension);
79
80 KWLoc = ConsumeToken();
81 Actions.SetDeclDefaulted(FnD, KWLoc);
82 } else {
83 llvm_unreachable("function definition after = not 'delete' or 'default'");
84 }
85
86 if (Tok.is(tok::comma)) {
87 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
88 << Delete;
89 SkipUntil(tok::semi);
90 } else {
91 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
92 Delete ? "delete" : "default", tok::semi);
93 }
94
95 return FnD;
96 }
97
Francois Pichet1c229c02011-04-22 22:18:13 +000098 // In delayed template parsing mode, if we are within a class template
99 // or if we are about to parse function member template then consume
100 // the tokens and store them for parsing at the end of the translation unit.
101 if (getLang().DelayedTemplateParsing &&
102 ((Actions.CurContext->isDependentContext() ||
103 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
104 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
105 !D.getDeclSpec().isFriendSpecified()) {
106
107 if (FnD) {
108 LateParsedTemplatedFunction *LPT =
109 new LateParsedTemplatedFunction(this, FnD);
110
111 FunctionDecl *FD = 0;
112 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
113 FD = FunTmpl->getTemplatedDecl();
114 else
115 FD = cast<FunctionDecl>(FnD);
Chandler Carruthbc0f9ae2011-04-25 07:09:43 +0000116 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet1c229c02011-04-22 22:18:13 +0000117
118 LateParsedTemplateMap[FD] = LPT;
119 Actions.MarkAsLateParsedTemplate(FD);
120 LexTemplateFunctionForLateParsing(LPT->Toks);
121 } else {
122 CachedTokens Toks;
123 LexTemplateFunctionForLateParsing(Toks);
124 }
125
126 return FnD;
127 }
128
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000129 // Consume the tokens and store them for later parsing.
130
Douglas Gregorefc46952010-10-12 16:25:54 +0000131 LexedMethod* LM = new LexedMethod(this, FnD);
132 getCurrentClass().LateParsedDeclarations.push_back(LM);
133 LM->TemplateScope = getCurScope()->isTemplateParamScope();
134 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000135
Sebastian Redla7b98a72009-04-26 20:35:05 +0000136 tok::TokenKind kind = Tok.getKind();
Sebastian Redl0d164012011-09-30 08:32:17 +0000137 // Consume everything up to (and including) the left brace of the
138 // function body.
139 if (ConsumeAndStoreFunctionPrologue(Toks)) {
140 // We didn't find the left-brace we expected after the
141 // constructor initializer.
142 if (Tok.is(tok::semi)) {
143 // We found a semicolon; complain, consume the semicolon, and
144 // don't try to parse this method later.
145 Diag(Tok.getLocation(), diag::err_expected_lbrace);
146 ConsumeAnyToken();
147 delete getCurrentClass().LateParsedDeclarations.back();
148 getCurrentClass().LateParsedDeclarations.pop_back();
149 return FnD;
Douglas Gregor49de5342008-11-10 16:59:40 +0000150 }
Douglas Gregore8381c02008-11-05 04:29:56 +0000151 } else {
Sebastian Redl0d164012011-09-30 08:32:17 +0000152 // Consume everything up to (and including) the matching right brace.
153 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregore8381c02008-11-05 04:29:56 +0000154 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000155
Sebastian Redla7b98a72009-04-26 20:35:05 +0000156 // If we're in a function-try-block, we need to store all the catch blocks.
157 if (kind == tok::kw_try) {
158 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000159 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
160 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000161 }
162 }
163
Douglas Gregor6ca64102011-04-14 23:19:27 +0000164
165 if (!FnD) {
166 // If semantic analysis could not build a function declaration,
167 // just throw away the late-parsed declaration.
168 delete getCurrentClass().LateParsedDeclarations.back();
169 getCurrentClass().LateParsedDeclarations.pop_back();
170 }
171
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000172 return FnD;
173}
174
Richard Smith938f40b2011-06-11 17:19:42 +0000175/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
176/// specified Declarator is a well formed C++ non-static data member
177/// declaration. Now lex its initializer and store its tokens for parsing
178/// after the class is complete.
179void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
180 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
181 "Current token not a '{' or '='!");
182
183 LateParsedMemberInitializer *MI =
184 new LateParsedMemberInitializer(this, VarD);
185 getCurrentClass().LateParsedDeclarations.push_back(MI);
186 CachedTokens &Toks = MI->Toks;
187
188 tok::TokenKind kind = Tok.getKind();
189 if (kind == tok::equal) {
190 Toks.push_back(Tok);
191 ConsumeAnyToken();
192 }
193
194 if (kind == tok::l_brace) {
195 // Begin by storing the '{' token.
196 Toks.push_back(Tok);
197 ConsumeBrace();
198
199 // Consume everything up to (and including) the matching right brace.
200 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
201 } else {
202 // Consume everything up to (but excluding) the comma or semicolon.
203 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
204 /*ConsumeFinalToken=*/false);
205 }
206
207 // Store an artificial EOF token to ensure that we don't run off the end of
208 // the initializer when we come to parse it.
209 Token Eof;
210 Eof.startToken();
211 Eof.setKind(tok::eof);
212 Eof.setLocation(Tok.getLocation());
213 Toks.push_back(Eof);
214}
215
Douglas Gregorefc46952010-10-12 16:25:54 +0000216Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
217void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith938f40b2011-06-11 17:19:42 +0000218void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregorefc46952010-10-12 16:25:54 +0000219void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
220
221Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
222 : Self(P), Class(C) {}
223
224Parser::LateParsedClass::~LateParsedClass() {
225 Self->DeallocateParsedClasses(Class);
226}
227
228void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
229 Self->ParseLexedMethodDeclarations(*Class);
230}
231
Richard Smith938f40b2011-06-11 17:19:42 +0000232void Parser::LateParsedClass::ParseLexedMemberInitializers() {
233 Self->ParseLexedMemberInitializers(*Class);
234}
235
Douglas Gregorefc46952010-10-12 16:25:54 +0000236void Parser::LateParsedClass::ParseLexedMethodDefs() {
237 Self->ParseLexedMethodDefs(*Class);
238}
239
240void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
241 Self->ParseLexedMethodDeclaration(*this);
242}
243
244void Parser::LexedMethod::ParseLexedMethodDefs() {
245 Self->ParseLexedMethodDef(*this);
246}
247
Richard Smith938f40b2011-06-11 17:19:42 +0000248void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
249 Self->ParseLexedMemberInitializer(*this);
250}
251
Douglas Gregor4d87df52008-12-16 21:30:33 +0000252/// ParseLexedMethodDeclarations - We finished parsing the member
253/// specification of a top (non-nested) C++ class. Now go over the
254/// stack of method declarations with some parts for which parsing was
255/// delayed (such as default arguments) and parse them.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000256void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
257 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000258 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000259 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000260 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000261
John McCall6df5fef2009-12-19 10:49:29 +0000262 // The current scope is still active if we're the top-level class.
263 // Otherwise we'll need to push and enter a new scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000264 bool HasClassScope = !Class.TopLevelClass;
Alexis Hunt1deb9722010-04-14 23:07:37 +0000265 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
266 HasClassScope);
John McCall6df5fef2009-12-19 10:49:29 +0000267 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000268 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000269
Douglas Gregorefc46952010-10-12 16:25:54 +0000270 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
271 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor4d87df52008-12-16 21:30:33 +0000272 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000273
John McCall6df5fef2009-12-19 10:49:29 +0000274 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000275 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor4d87df52008-12-16 21:30:33 +0000276}
277
Douglas Gregorefc46952010-10-12 16:25:54 +0000278void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
279 // If this is a member template, introduce the template parameter scope.
280 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
281 if (LM.TemplateScope)
282 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
283
284 // Start the delayed C++ method declaration
285 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
286
287 // Introduce the parameters into scope and parse their default
288 // arguments.
289 ParseScope PrototypeScope(this,
290 Scope::FunctionPrototypeScope|Scope::DeclScope);
291 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
292 // Introduce the parameter into scope.
293 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
294
295 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
296 // Save the current token position.
297 SourceLocation origLoc = Tok.getLocation();
298
299 // Parse the default argument from its saved token stream.
300 Toks->push_back(Tok); // So that the current token doesn't get lost
301 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
302
303 // Consume the previously-pushed token.
304 ConsumeAnyToken();
305
306 // Consume the '='.
307 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
308 SourceLocation EqualLoc = ConsumeToken();
309
310 // The argument isn't actually potentially evaluated unless it is
311 // used.
312 EnterExpressionEvaluationContext Eval(Actions,
313 Sema::PotentiallyEvaluatedIfUsed);
314
315 ExprResult DefArgResult(ParseAssignmentExpression());
316 if (DefArgResult.isInvalid())
317 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
318 else {
319 if (Tok.is(tok::cxx_defaultarg_end))
320 ConsumeToken();
321 else
322 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
323 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
324 DefArgResult.take());
325 }
326
327 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
328 Tok.getLocation()) &&
329 "ParseAssignmentExpression went over the default arg tokens!");
330 // There could be leftover tokens (e.g. because of an error).
331 // Skip through until we reach the original token position.
332 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
333 ConsumeAnyToken();
334
335 delete Toks;
336 LM.DefaultArgs[I].Toks = 0;
337 }
338 }
339 PrototypeScope.Exit();
340
341 // Finish the delayed C++ method declaration.
342 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
343}
344
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000345/// ParseLexedMethodDefs - We finished parsing the member specification of a top
346/// (non-nested) C++ class. Now go over the stack of lexed methods that were
347/// collected during its parsing and parse them all.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000348void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
349 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000350 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000351 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000352 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000353
354 bool HasClassScope = !Class.TopLevelClass;
355 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
356 HasClassScope);
357
Douglas Gregorefc46952010-10-12 16:25:54 +0000358 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
359 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
360 }
361}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000362
Douglas Gregorefc46952010-10-12 16:25:54 +0000363void Parser::ParseLexedMethodDef(LexedMethod &LM) {
364 // If this is a member template, introduce the template parameter scope.
365 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
366 if (LM.TemplateScope)
367 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump11289f42009-09-09 15:08:12 +0000368
Douglas Gregorefc46952010-10-12 16:25:54 +0000369 // Save the current token position.
370 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidis02041972010-03-31 00:38:09 +0000371
Douglas Gregorefc46952010-10-12 16:25:54 +0000372 assert(!LM.Toks.empty() && "Empty body!");
373 // Append the current token at the end of the new token stream so that it
374 // doesn't get lost.
375 LM.Toks.push_back(Tok);
376 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000377
Douglas Gregorefc46952010-10-12 16:25:54 +0000378 // Consume the previously pushed token.
379 ConsumeAnyToken();
380 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
381 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000382
Douglas Gregorefc46952010-10-12 16:25:54 +0000383 // Parse the method body. Function body parsing code is similar enough
384 // to be re-used for method bodies as well.
385 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
386 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000387
Douglas Gregorefc46952010-10-12 16:25:54 +0000388 if (Tok.is(tok::kw_try)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000389 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000390 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
391 Tok.getLocation()) &&
392 "ParseFunctionTryBlock went over the cached tokens!");
393 // There could be leftover tokens (e.g. because of an error).
394 // Skip through until we reach the original token position.
395 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
396 ConsumeAnyToken();
397 return;
398 }
399 if (Tok.is(tok::colon)) {
400 ParseConstructorInitializer(LM.D);
401
402 // Error recovery.
403 if (!Tok.is(tok::l_brace)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000404 FnScope.Exit();
Douglas Gregorefc46952010-10-12 16:25:54 +0000405 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gayd0457922011-09-23 22:39:23 +0000406 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
407 ConsumeAnyToken();
Douglas Gregorefc46952010-10-12 16:25:54 +0000408 return;
409 }
410 } else
411 Actions.ActOnDefaultCtorInitializers(LM.D);
412
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000413 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000414
415 if (Tok.getLocation() != origLoc) {
416 // Due to parsing error, we either went over the cached tokens or
417 // there are still cached tokens left. If it's the latter case skip the
418 // leftover tokens.
419 // Since this is an uncommon situation that should be avoided, use the
420 // expensive isBeforeInTranslationUnit call.
421 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
422 origLoc))
Argyrios Kyrtzidise1224c82010-06-19 19:58:34 +0000423 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidise9b76af2010-06-17 10:52:22 +0000424 ConsumeAnyToken();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000425 }
426}
427
Richard Smith938f40b2011-06-11 17:19:42 +0000428/// ParseLexedMemberInitializers - We finished parsing the member specification
429/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
430/// initializers that were collected during its parsing and parse them all.
431void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
432 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
433 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
434 HasTemplateScope);
435 if (HasTemplateScope)
436 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
437
438 // Set or update the scope flags to include Scope::ThisScope.
439 bool AlreadyHasClassScope = Class.TopLevelClass;
440 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
441 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
442 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
443
444 if (!AlreadyHasClassScope)
445 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
446 Class.TagOrTemplate);
447
448 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
449 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
450 }
451
452 if (!AlreadyHasClassScope)
453 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
454 Class.TagOrTemplate);
455
456 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
457}
458
459void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1a526fd2011-09-29 19:42:27 +0000460 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith938f40b2011-06-11 17:19:42 +0000461 return;
462
463 // Append the current token at the end of the new token stream so that it
464 // doesn't get lost.
465 MI.Toks.push_back(Tok);
466 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
467
468 // Consume the previously pushed token.
469 ConsumeAnyToken();
470
471 SourceLocation EqualLoc;
472 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
473
474 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
475
476 // The next token should be our artificial terminating EOF token.
477 if (Tok.isNot(tok::eof)) {
478 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
479 if (!EndLoc.isValid())
480 EndLoc = Tok.getLocation();
481 // No fixit; we can't recover as if there were a semicolon here.
482 Diag(EndLoc, diag::err_expected_semi_decl_list);
483
484 // Consume tokens until we hit the artificial EOF.
485 while (Tok.isNot(tok::eof))
486 ConsumeAnyToken();
487 }
488 ConsumeAnyToken();
489}
490
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000491/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor4d87df52008-12-16 21:30:33 +0000492/// container until the token 'T' is reached (which gets
Mike Stump11289f42009-09-09 15:08:12 +0000493/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000494/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000495/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000496/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000497bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
498 CachedTokens &Toks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000499 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000500 // We always want this function to consume at least one token if the first
501 // token isn't T and if not at EOF.
502 bool isFirstTokenConsumed = true;
503 while (1) {
504 // If we found one of the tokens, stop and return true.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000505 if (Tok.is(T1) || Tok.is(T2)) {
506 if (ConsumeFinalToken) {
507 Toks.push_back(Tok);
508 ConsumeAnyToken();
509 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000510 return true;
511 }
512
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000513 switch (Tok.getKind()) {
514 case tok::eof:
515 // Ran out of tokens.
516 return false;
517
518 case tok::l_paren:
519 // Recursively consume properly-nested parens.
520 Toks.push_back(Tok);
521 ConsumeParen();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000522 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000523 break;
524 case tok::l_square:
525 // Recursively consume properly-nested square brackets.
526 Toks.push_back(Tok);
527 ConsumeBracket();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000528 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000529 break;
530 case tok::l_brace:
531 // Recursively consume properly-nested braces.
532 Toks.push_back(Tok);
533 ConsumeBrace();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000534 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000535 break;
536
537 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
538 // Since the user wasn't looking for this token (if they were, it would
539 // already be handled), this isn't balanced. If there is a LHS token at a
540 // higher level, we will assume that this matches the unbalanced token
541 // and return it. Otherwise, this is a spurious RHS token, which we skip.
542 case tok::r_paren:
543 if (ParenCount && !isFirstTokenConsumed)
544 return false; // Matches something.
545 Toks.push_back(Tok);
546 ConsumeParen();
547 break;
548 case tok::r_square:
549 if (BracketCount && !isFirstTokenConsumed)
550 return false; // Matches something.
551 Toks.push_back(Tok);
552 ConsumeBracket();
553 break;
554 case tok::r_brace:
555 if (BraceCount && !isFirstTokenConsumed)
556 return false; // Matches something.
557 Toks.push_back(Tok);
558 ConsumeBrace();
559 break;
560
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000561 case tok::code_completion:
562 Toks.push_back(Tok);
563 ConsumeCodeCompletionToken();
564 break;
565
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000566 case tok::string_literal:
567 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000568 case tok::utf8_string_literal:
569 case tok::utf16_string_literal:
570 case tok::utf32_string_literal:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000571 Toks.push_back(Tok);
572 ConsumeStringToken();
573 break;
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000574 case tok::semi:
575 if (StopAtSemi)
576 return false;
577 // FALL THROUGH.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000578 default:
579 // consume this token.
580 Toks.push_back(Tok);
581 ConsumeToken();
582 break;
583 }
584 isFirstTokenConsumed = false;
585 }
586}
Sebastian Redla74948d2011-09-24 17:48:25 +0000587
588/// \brief Consume tokens and store them in the passed token container until
589/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redl0d164012011-09-30 08:32:17 +0000590/// the opening brace of the function body. The opening brace will be consumed
591/// if and only if there was no error.
Sebastian Redla74948d2011-09-24 17:48:25 +0000592///
Sebastian Redl0d164012011-09-30 08:32:17 +0000593/// \return True on error.
594bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redla74948d2011-09-24 17:48:25 +0000595 if (Tok.is(tok::kw_try)) {
596 Toks.push_back(Tok);
597 ConsumeToken();
598 }
599 if (Tok.is(tok::colon)) {
600 // Initializers can contain braces too.
601 Toks.push_back(Tok);
602 ConsumeToken();
603
604 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
605 if (Tok.is(tok::eof) || Tok.is(tok::semi))
606 return true;
607
608 // Grab the identifier.
609 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
610 /*StopAtSemi=*/true,
611 /*ConsumeFinalToken=*/false))
612 return true;
613
614 tok::TokenKind kind = Tok.getKind();
615 Toks.push_back(Tok);
616 if (kind == tok::l_paren)
617 ConsumeParen();
618 else {
619 assert(kind == tok::l_brace && "Must be left paren or brace here.");
620 ConsumeBrace();
Sebastian Redl0d164012011-09-30 08:32:17 +0000621 // In C++03, this has to be the start of the function body, which
622 // means the initializer is malformed.
623 if (!getLang().CPlusPlus0x)
624 return false;
Sebastian Redla74948d2011-09-24 17:48:25 +0000625 }
626
627 // Grab the initializer
628 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
629 tok::r_brace,
630 Toks, /*StopAtSemi=*/true))
631 return true;
Sebastian Redl0d164012011-09-30 08:32:17 +0000632
633 // Grab the separating comma, if any.
634 if (Tok.is(tok::comma)) {
635 Toks.push_back(Tok);
636 ConsumeToken();
637 }
Sebastian Redla74948d2011-09-24 17:48:25 +0000638 }
639 }
640
Sebastian Redl0d164012011-09-30 08:32:17 +0000641 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
642 // brace: an opening one is the function body, while a closing one probably
643 // means we've reached the end of the class.
644 if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
645 /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false))
646 return true;
647 if(Tok.isNot(tok::l_brace))
648 return true;
649
650 Toks.push_back(Tok);
651 ConsumeBrace();
652 return false;
Sebastian Redla74948d2011-09-24 17:48:25 +0000653}