blob: f5c69981ca358f02f82114f56550970bebffae23 [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) {
Douglas Gregor7ad83902008-11-05 04:29:56 +0000128 // Consume everything up to (and including) the left brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000129 if (!ConsumeAndStoreUntil(tok::l_brace, Toks)) {
Douglas Gregor3f08d182008-11-10 16:59:40 +0000130 // We didn't find the left-brace we expected after the
Mike Stump1eb44332009-09-09 15:08:12 +0000131 // constructor initializer.
Douglas Gregor3f08d182008-11-10 16:59:40 +0000132 if (Tok.is(tok::semi)) {
133 // We found a semicolon; complain, consume the semicolon, and
134 // don't try to parse this method later.
135 Diag(Tok.getLocation(), diag::err_expected_lbrace);
136 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000137 delete getCurrentClass().LateParsedDeclarations.back();
138 getCurrentClass().LateParsedDeclarations.pop_back();
Douglas Gregor3f08d182008-11-10 16:59:40 +0000139 return FnD;
140 }
141 }
142
Douglas Gregor7ad83902008-11-05 04:29:56 +0000143 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000144 // Begin by storing the '{' token.
Douglas Gregor7ad83902008-11-05 04:29:56 +0000145 Toks.push_back(Tok);
146 ConsumeBrace();
147 }
148 // Consume everything up to (and including) the matching right brace.
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000149 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000150
Sebastian Redld3a413d2009-04-26 20:35:05 +0000151 // If we're in a function-try-block, we need to store all the catch blocks.
152 if (kind == tok::kw_try) {
153 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000154 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
155 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000156 }
157 }
158
Douglas Gregor87f10642011-04-14 23:19:27 +0000159
160 if (!FnD) {
161 // If semantic analysis could not build a function declaration,
162 // just throw away the late-parsed declaration.
163 delete getCurrentClass().LateParsedDeclarations.back();
164 getCurrentClass().LateParsedDeclarations.pop_back();
165 }
166
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000167 return FnD;
168}
169
Richard Smith7a614d82011-06-11 17:19:42 +0000170/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
171/// specified Declarator is a well formed C++ non-static data member
172/// declaration. Now lex its initializer and store its tokens for parsing
173/// after the class is complete.
174void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
175 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
176 "Current token not a '{' or '='!");
177
178 LateParsedMemberInitializer *MI =
179 new LateParsedMemberInitializer(this, VarD);
180 getCurrentClass().LateParsedDeclarations.push_back(MI);
181 CachedTokens &Toks = MI->Toks;
182
183 tok::TokenKind kind = Tok.getKind();
184 if (kind == tok::equal) {
185 Toks.push_back(Tok);
186 ConsumeAnyToken();
187 }
188
189 if (kind == tok::l_brace) {
190 // Begin by storing the '{' token.
191 Toks.push_back(Tok);
192 ConsumeBrace();
193
194 // Consume everything up to (and including) the matching right brace.
195 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
196 } else {
197 // Consume everything up to (but excluding) the comma or semicolon.
198 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
199 /*ConsumeFinalToken=*/false);
200 }
201
202 // Store an artificial EOF token to ensure that we don't run off the end of
203 // the initializer when we come to parse it.
204 Token Eof;
205 Eof.startToken();
206 Eof.setKind(tok::eof);
207 Eof.setLocation(Tok.getLocation());
208 Toks.push_back(Eof);
209}
210
Douglas Gregord54eb442010-10-12 16:25:54 +0000211Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
212void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000213void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000214void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
215
216Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
217 : Self(P), Class(C) {}
218
219Parser::LateParsedClass::~LateParsedClass() {
220 Self->DeallocateParsedClasses(Class);
221}
222
223void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
224 Self->ParseLexedMethodDeclarations(*Class);
225}
226
Richard Smith7a614d82011-06-11 17:19:42 +0000227void Parser::LateParsedClass::ParseLexedMemberInitializers() {
228 Self->ParseLexedMemberInitializers(*Class);
229}
230
Douglas Gregord54eb442010-10-12 16:25:54 +0000231void Parser::LateParsedClass::ParseLexedMethodDefs() {
232 Self->ParseLexedMethodDefs(*Class);
233}
234
235void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
236 Self->ParseLexedMethodDeclaration(*this);
237}
238
239void Parser::LexedMethod::ParseLexedMethodDefs() {
240 Self->ParseLexedMethodDef(*this);
241}
242
Richard Smith7a614d82011-06-11 17:19:42 +0000243void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
244 Self->ParseLexedMemberInitializer(*this);
245}
246
Douglas Gregor72b505b2008-12-16 21:30:33 +0000247/// ParseLexedMethodDeclarations - We finished parsing the member
248/// specification of a top (non-nested) C++ class. Now go over the
249/// stack of method declarations with some parts for which parsing was
250/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000251void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
252 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000253 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000254 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000255 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000256
John McCall7a1dc562009-12-19 10:49:29 +0000257 // The current scope is still active if we're the top-level class.
258 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000259 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000260 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
261 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000262 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000263 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000264
Douglas Gregord54eb442010-10-12 16:25:54 +0000265 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
266 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000267 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000268
John McCall7a1dc562009-12-19 10:49:29 +0000269 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000270 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000271}
272
Douglas Gregord54eb442010-10-12 16:25:54 +0000273void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
274 // If this is a member template, introduce the template parameter scope.
275 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
276 if (LM.TemplateScope)
277 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
278
279 // Start the delayed C++ method declaration
280 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
281
282 // Introduce the parameters into scope and parse their default
283 // arguments.
284 ParseScope PrototypeScope(this,
285 Scope::FunctionPrototypeScope|Scope::DeclScope);
286 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
287 // Introduce the parameter into scope.
288 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
289
290 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
291 // Save the current token position.
292 SourceLocation origLoc = Tok.getLocation();
293
294 // Parse the default argument from its saved token stream.
295 Toks->push_back(Tok); // So that the current token doesn't get lost
296 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
297
298 // Consume the previously-pushed token.
299 ConsumeAnyToken();
300
301 // Consume the '='.
302 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
303 SourceLocation EqualLoc = ConsumeToken();
304
305 // The argument isn't actually potentially evaluated unless it is
306 // used.
307 EnterExpressionEvaluationContext Eval(Actions,
308 Sema::PotentiallyEvaluatedIfUsed);
309
310 ExprResult DefArgResult(ParseAssignmentExpression());
311 if (DefArgResult.isInvalid())
312 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
313 else {
314 if (Tok.is(tok::cxx_defaultarg_end))
315 ConsumeToken();
316 else
317 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
318 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
319 DefArgResult.take());
320 }
321
322 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
323 Tok.getLocation()) &&
324 "ParseAssignmentExpression went over the default arg tokens!");
325 // There could be leftover tokens (e.g. because of an error).
326 // Skip through until we reach the original token position.
327 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
328 ConsumeAnyToken();
329
330 delete Toks;
331 LM.DefaultArgs[I].Toks = 0;
332 }
333 }
334 PrototypeScope.Exit();
335
336 // Finish the delayed C++ method declaration.
337 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
338}
339
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000340/// ParseLexedMethodDefs - We finished parsing the member specification of a top
341/// (non-nested) C++ class. Now go over the stack of lexed methods that were
342/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000343void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
344 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000345 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000346 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000347 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000348
349 bool HasClassScope = !Class.TopLevelClass;
350 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
351 HasClassScope);
352
Douglas Gregord54eb442010-10-12 16:25:54 +0000353 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
354 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
355 }
356}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000357
Douglas Gregord54eb442010-10-12 16:25:54 +0000358void Parser::ParseLexedMethodDef(LexedMethod &LM) {
359 // If this is a member template, introduce the template parameter scope.
360 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
361 if (LM.TemplateScope)
362 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Douglas Gregord54eb442010-10-12 16:25:54 +0000364 // Save the current token position.
365 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000366
Douglas Gregord54eb442010-10-12 16:25:54 +0000367 assert(!LM.Toks.empty() && "Empty body!");
368 // Append the current token at the end of the new token stream so that it
369 // doesn't get lost.
370 LM.Toks.push_back(Tok);
371 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000372
Douglas Gregord54eb442010-10-12 16:25:54 +0000373 // Consume the previously pushed token.
374 ConsumeAnyToken();
375 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
376 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000377
Douglas Gregord54eb442010-10-12 16:25:54 +0000378 // Parse the method body. Function body parsing code is similar enough
379 // to be re-used for method bodies as well.
380 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
381 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000382
Douglas Gregord54eb442010-10-12 16:25:54 +0000383 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000384 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000385 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
386 Tok.getLocation()) &&
387 "ParseFunctionTryBlock went over the cached tokens!");
388 // There could be leftover tokens (e.g. because of an error).
389 // Skip through until we reach the original token position.
390 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
391 ConsumeAnyToken();
392 return;
393 }
394 if (Tok.is(tok::colon)) {
395 ParseConstructorInitializer(LM.D);
396
397 // Error recovery.
398 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000399 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000400 Actions.ActOnFinishFunctionBody(LM.D, 0);
401 return;
402 }
403 } else
404 Actions.ActOnDefaultCtorInitializers(LM.D);
405
Douglas Gregorc9977d02011-03-16 17:05:57 +0000406 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000407
408 if (Tok.getLocation() != origLoc) {
409 // Due to parsing error, we either went over the cached tokens or
410 // there are still cached tokens left. If it's the latter case skip the
411 // leftover tokens.
412 // Since this is an uncommon situation that should be avoided, use the
413 // expensive isBeforeInTranslationUnit call.
414 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
415 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000416 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000417 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000418 }
419}
420
Richard Smith7a614d82011-06-11 17:19:42 +0000421/// ParseLexedMemberInitializers - We finished parsing the member specification
422/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
423/// initializers that were collected during its parsing and parse them all.
424void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
425 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
426 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
427 HasTemplateScope);
428 if (HasTemplateScope)
429 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
430
431 // Set or update the scope flags to include Scope::ThisScope.
432 bool AlreadyHasClassScope = Class.TopLevelClass;
433 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
434 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
435 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
436
437 if (!AlreadyHasClassScope)
438 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
439 Class.TagOrTemplate);
440
441 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
442 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
443 }
444
445 if (!AlreadyHasClassScope)
446 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
447 Class.TagOrTemplate);
448
449 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
450}
451
452void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
453 if (MI.Field->isInvalidDecl())
454 return;
455
456 // Append the current token at the end of the new token stream so that it
457 // doesn't get lost.
458 MI.Toks.push_back(Tok);
459 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
460
461 // Consume the previously pushed token.
462 ConsumeAnyToken();
463
464 SourceLocation EqualLoc;
465 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
466
467 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
468
469 // The next token should be our artificial terminating EOF token.
470 if (Tok.isNot(tok::eof)) {
471 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
472 if (!EndLoc.isValid())
473 EndLoc = Tok.getLocation();
474 // No fixit; we can't recover as if there were a semicolon here.
475 Diag(EndLoc, diag::err_expected_semi_decl_list);
476
477 // Consume tokens until we hit the artificial EOF.
478 while (Tok.isNot(tok::eof))
479 ConsumeAnyToken();
480 }
481 ConsumeAnyToken();
482}
483
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000484/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000485/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000486/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000487/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000488/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000489/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000490bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
491 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000492 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000493 // We always want this function to consume at least one token if the first
494 // token isn't T and if not at EOF.
495 bool isFirstTokenConsumed = true;
496 while (1) {
497 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000498 if (Tok.is(T1) || Tok.is(T2)) {
499 if (ConsumeFinalToken) {
500 Toks.push_back(Tok);
501 ConsumeAnyToken();
502 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000503 return true;
504 }
505
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000506 switch (Tok.getKind()) {
507 case tok::eof:
508 // Ran out of tokens.
509 return false;
510
511 case tok::l_paren:
512 // Recursively consume properly-nested parens.
513 Toks.push_back(Tok);
514 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000515 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000516 break;
517 case tok::l_square:
518 // Recursively consume properly-nested square brackets.
519 Toks.push_back(Tok);
520 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000521 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000522 break;
523 case tok::l_brace:
524 // Recursively consume properly-nested braces.
525 Toks.push_back(Tok);
526 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000527 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000528 break;
529
530 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
531 // Since the user wasn't looking for this token (if they were, it would
532 // already be handled), this isn't balanced. If there is a LHS token at a
533 // higher level, we will assume that this matches the unbalanced token
534 // and return it. Otherwise, this is a spurious RHS token, which we skip.
535 case tok::r_paren:
536 if (ParenCount && !isFirstTokenConsumed)
537 return false; // Matches something.
538 Toks.push_back(Tok);
539 ConsumeParen();
540 break;
541 case tok::r_square:
542 if (BracketCount && !isFirstTokenConsumed)
543 return false; // Matches something.
544 Toks.push_back(Tok);
545 ConsumeBracket();
546 break;
547 case tok::r_brace:
548 if (BraceCount && !isFirstTokenConsumed)
549 return false; // Matches something.
550 Toks.push_back(Tok);
551 ConsumeBrace();
552 break;
553
554 case tok::string_literal:
555 case tok::wide_string_literal:
556 Toks.push_back(Tok);
557 ConsumeStringToken();
558 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000559 case tok::semi:
560 if (StopAtSemi)
561 return false;
562 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000563 default:
564 // consume this token.
565 Toks.push_back(Tok);
566 ConsumeToken();
567 break;
568 }
569 isFirstTokenConsumed = false;
570 }
571}