blob: 40e36f0a0b10e16e7c2566ac78586d254b9f3f6f [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();
Sebastian Redla891a322011-09-30 08:32:17 +0000126 // Consume everything up to (and including) the left brace of the
127 // function body.
128 if (ConsumeAndStoreFunctionPrologue(Toks)) {
129 // We didn't find the left-brace we expected after the
130 // constructor initializer.
131 if (Tok.is(tok::semi)) {
132 // We found a semicolon; complain, consume the semicolon, and
133 // don't try to parse this method later.
134 Diag(Tok.getLocation(), diag::err_expected_lbrace);
135 ConsumeAnyToken();
136 delete getCurrentClass().LateParsedDeclarations.back();
137 getCurrentClass().LateParsedDeclarations.pop_back();
138 return FnD;
Douglas Gregor3f08d182008-11-10 16:59:40 +0000139 }
Douglas Gregor7ad83902008-11-05 04:29:56 +0000140 } else {
Sebastian Redla891a322011-09-30 08:32:17 +0000141 // Consume everything up to (and including) the matching right brace.
142 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000143 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000144
Sebastian Redld3a413d2009-04-26 20:35:05 +0000145 // If we're in a function-try-block, we need to store all the catch blocks.
146 if (kind == tok::kw_try) {
147 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000148 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
149 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000150 }
151 }
152
Douglas Gregor87f10642011-04-14 23:19:27 +0000153
154 if (!FnD) {
155 // If semantic analysis could not build a function declaration,
156 // just throw away the late-parsed declaration.
157 delete getCurrentClass().LateParsedDeclarations.back();
158 getCurrentClass().LateParsedDeclarations.pop_back();
159 }
160
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000161 return FnD;
162}
163
Richard Smith7a614d82011-06-11 17:19:42 +0000164/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
165/// specified Declarator is a well formed C++ non-static data member
166/// declaration. Now lex its initializer and store its tokens for parsing
167/// after the class is complete.
168void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
169 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
170 "Current token not a '{' or '='!");
171
172 LateParsedMemberInitializer *MI =
173 new LateParsedMemberInitializer(this, VarD);
174 getCurrentClass().LateParsedDeclarations.push_back(MI);
175 CachedTokens &Toks = MI->Toks;
176
177 tok::TokenKind kind = Tok.getKind();
178 if (kind == tok::equal) {
179 Toks.push_back(Tok);
180 ConsumeAnyToken();
181 }
182
183 if (kind == tok::l_brace) {
184 // Begin by storing the '{' token.
185 Toks.push_back(Tok);
186 ConsumeBrace();
187
188 // Consume everything up to (and including) the matching right brace.
189 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
190 } else {
191 // Consume everything up to (but excluding) the comma or semicolon.
192 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
193 /*ConsumeFinalToken=*/false);
194 }
195
196 // Store an artificial EOF token to ensure that we don't run off the end of
197 // the initializer when we come to parse it.
198 Token Eof;
199 Eof.startToken();
200 Eof.setKind(tok::eof);
201 Eof.setLocation(Tok.getLocation());
202 Toks.push_back(Eof);
203}
204
Douglas Gregord54eb442010-10-12 16:25:54 +0000205Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
206void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000207void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000208void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
209
210Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
211 : Self(P), Class(C) {}
212
213Parser::LateParsedClass::~LateParsedClass() {
214 Self->DeallocateParsedClasses(Class);
215}
216
217void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
218 Self->ParseLexedMethodDeclarations(*Class);
219}
220
Richard Smith7a614d82011-06-11 17:19:42 +0000221void Parser::LateParsedClass::ParseLexedMemberInitializers() {
222 Self->ParseLexedMemberInitializers(*Class);
223}
224
Douglas Gregord54eb442010-10-12 16:25:54 +0000225void Parser::LateParsedClass::ParseLexedMethodDefs() {
226 Self->ParseLexedMethodDefs(*Class);
227}
228
229void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
230 Self->ParseLexedMethodDeclaration(*this);
231}
232
233void Parser::LexedMethod::ParseLexedMethodDefs() {
234 Self->ParseLexedMethodDef(*this);
235}
236
Richard Smith7a614d82011-06-11 17:19:42 +0000237void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
238 Self->ParseLexedMemberInitializer(*this);
239}
240
Douglas Gregor72b505b2008-12-16 21:30:33 +0000241/// ParseLexedMethodDeclarations - We finished parsing the member
242/// specification of a top (non-nested) C++ class. Now go over the
243/// stack of method declarations with some parts for which parsing was
244/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000245void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
246 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000247 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000248 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000249 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000250
John McCall7a1dc562009-12-19 10:49:29 +0000251 // The current scope is still active if we're the top-level class.
252 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000253 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000254 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
255 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000256 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000257 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000258
Douglas Gregord54eb442010-10-12 16:25:54 +0000259 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
260 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000261 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000262
John McCall7a1dc562009-12-19 10:49:29 +0000263 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000265}
266
Douglas Gregord54eb442010-10-12 16:25:54 +0000267void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
268 // If this is a member template, introduce the template parameter scope.
269 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
270 if (LM.TemplateScope)
271 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
272
273 // Start the delayed C++ method declaration
274 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
275
276 // Introduce the parameters into scope and parse their default
277 // arguments.
278 ParseScope PrototypeScope(this,
279 Scope::FunctionPrototypeScope|Scope::DeclScope);
280 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
281 // Introduce the parameter into scope.
282 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
283
284 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
285 // Save the current token position.
286 SourceLocation origLoc = Tok.getLocation();
287
288 // Parse the default argument from its saved token stream.
289 Toks->push_back(Tok); // So that the current token doesn't get lost
290 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
291
292 // Consume the previously-pushed token.
293 ConsumeAnyToken();
294
295 // Consume the '='.
296 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
297 SourceLocation EqualLoc = ConsumeToken();
298
299 // The argument isn't actually potentially evaluated unless it is
300 // used.
301 EnterExpressionEvaluationContext Eval(Actions,
302 Sema::PotentiallyEvaluatedIfUsed);
303
304 ExprResult DefArgResult(ParseAssignmentExpression());
305 if (DefArgResult.isInvalid())
306 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
307 else {
308 if (Tok.is(tok::cxx_defaultarg_end))
309 ConsumeToken();
310 else
311 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
312 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
313 DefArgResult.take());
314 }
315
316 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
317 Tok.getLocation()) &&
318 "ParseAssignmentExpression went over the default arg tokens!");
319 // There could be leftover tokens (e.g. because of an error).
320 // Skip through until we reach the original token position.
321 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
322 ConsumeAnyToken();
323
324 delete Toks;
325 LM.DefaultArgs[I].Toks = 0;
326 }
327 }
328 PrototypeScope.Exit();
329
330 // Finish the delayed C++ method declaration.
331 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
332}
333
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000334/// ParseLexedMethodDefs - We finished parsing the member specification of a top
335/// (non-nested) C++ class. Now go over the stack of lexed methods that were
336/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000337void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
338 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000339 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000340 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000341 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000342
343 bool HasClassScope = !Class.TopLevelClass;
344 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
345 HasClassScope);
346
Douglas Gregord54eb442010-10-12 16:25:54 +0000347 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
348 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
349 }
350}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000351
Douglas Gregord54eb442010-10-12 16:25:54 +0000352void Parser::ParseLexedMethodDef(LexedMethod &LM) {
353 // If this is a member template, introduce the template parameter scope.
354 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
355 if (LM.TemplateScope)
356 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Douglas Gregord54eb442010-10-12 16:25:54 +0000358 // Save the current token position.
359 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000360
Douglas Gregord54eb442010-10-12 16:25:54 +0000361 assert(!LM.Toks.empty() && "Empty body!");
362 // Append the current token at the end of the new token stream so that it
363 // doesn't get lost.
364 LM.Toks.push_back(Tok);
365 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000366
Douglas Gregord54eb442010-10-12 16:25:54 +0000367 // Consume the previously pushed token.
368 ConsumeAnyToken();
369 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
370 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000371
Douglas Gregord54eb442010-10-12 16:25:54 +0000372 // Parse the method body. Function body parsing code is similar enough
373 // to be re-used for method bodies as well.
374 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
375 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000376
Douglas Gregord54eb442010-10-12 16:25:54 +0000377 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000378 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000379 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
380 Tok.getLocation()) &&
381 "ParseFunctionTryBlock went over the cached tokens!");
382 // There could be leftover tokens (e.g. because of an error).
383 // Skip through until we reach the original token position.
384 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
385 ConsumeAnyToken();
386 return;
387 }
388 if (Tok.is(tok::colon)) {
389 ParseConstructorInitializer(LM.D);
390
391 // Error recovery.
392 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000393 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000394 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000395 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
396 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000397 return;
398 }
399 } else
400 Actions.ActOnDefaultCtorInitializers(LM.D);
401
Douglas Gregorc9977d02011-03-16 17:05:57 +0000402 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000403
404 if (Tok.getLocation() != origLoc) {
405 // Due to parsing error, we either went over the cached tokens or
406 // there are still cached tokens left. If it's the latter case skip the
407 // leftover tokens.
408 // Since this is an uncommon situation that should be avoided, use the
409 // expensive isBeforeInTranslationUnit call.
410 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
411 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000412 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000413 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000414 }
415}
416
Richard Smith7a614d82011-06-11 17:19:42 +0000417/// ParseLexedMemberInitializers - We finished parsing the member specification
418/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
419/// initializers that were collected during its parsing and parse them all.
420void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
421 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
422 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
423 HasTemplateScope);
424 if (HasTemplateScope)
425 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
426
427 // Set or update the scope flags to include Scope::ThisScope.
428 bool AlreadyHasClassScope = Class.TopLevelClass;
429 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
430 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
431 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
432
433 if (!AlreadyHasClassScope)
434 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
435 Class.TagOrTemplate);
436
437 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
438 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
439 }
440
441 if (!AlreadyHasClassScope)
442 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
443 Class.TagOrTemplate);
444
445 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
446}
447
448void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000449 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000450 return;
451
452 // Append the current token at the end of the new token stream so that it
453 // doesn't get lost.
454 MI.Toks.push_back(Tok);
455 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
456
457 // Consume the previously pushed token.
458 ConsumeAnyToken();
459
460 SourceLocation EqualLoc;
461 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
462
463 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
464
465 // The next token should be our artificial terminating EOF token.
466 if (Tok.isNot(tok::eof)) {
467 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
468 if (!EndLoc.isValid())
469 EndLoc = Tok.getLocation();
470 // No fixit; we can't recover as if there were a semicolon here.
471 Diag(EndLoc, diag::err_expected_semi_decl_list);
472
473 // Consume tokens until we hit the artificial EOF.
474 while (Tok.isNot(tok::eof))
475 ConsumeAnyToken();
476 }
477 ConsumeAnyToken();
478}
479
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000480/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000481/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000482/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000483/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000484/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000485/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000486bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
487 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000488 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000489 // We always want this function to consume at least one token if the first
490 // token isn't T and if not at EOF.
491 bool isFirstTokenConsumed = true;
492 while (1) {
493 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000494 if (Tok.is(T1) || Tok.is(T2)) {
495 if (ConsumeFinalToken) {
496 Toks.push_back(Tok);
497 ConsumeAnyToken();
498 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000499 return true;
500 }
501
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000502 switch (Tok.getKind()) {
503 case tok::eof:
504 // Ran out of tokens.
505 return false;
506
507 case tok::l_paren:
508 // Recursively consume properly-nested parens.
509 Toks.push_back(Tok);
510 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000511 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000512 break;
513 case tok::l_square:
514 // Recursively consume properly-nested square brackets.
515 Toks.push_back(Tok);
516 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000517 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000518 break;
519 case tok::l_brace:
520 // Recursively consume properly-nested braces.
521 Toks.push_back(Tok);
522 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000523 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000524 break;
525
526 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
527 // Since the user wasn't looking for this token (if they were, it would
528 // already be handled), this isn't balanced. If there is a LHS token at a
529 // higher level, we will assume that this matches the unbalanced token
530 // and return it. Otherwise, this is a spurious RHS token, which we skip.
531 case tok::r_paren:
532 if (ParenCount && !isFirstTokenConsumed)
533 return false; // Matches something.
534 Toks.push_back(Tok);
535 ConsumeParen();
536 break;
537 case tok::r_square:
538 if (BracketCount && !isFirstTokenConsumed)
539 return false; // Matches something.
540 Toks.push_back(Tok);
541 ConsumeBracket();
542 break;
543 case tok::r_brace:
544 if (BraceCount && !isFirstTokenConsumed)
545 return false; // Matches something.
546 Toks.push_back(Tok);
547 ConsumeBrace();
548 break;
549
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000550 case tok::code_completion:
551 Toks.push_back(Tok);
552 ConsumeCodeCompletionToken();
553 break;
554
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000555 case tok::string_literal:
556 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000557 case tok::utf8_string_literal:
558 case tok::utf16_string_literal:
559 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000560 Toks.push_back(Tok);
561 ConsumeStringToken();
562 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000563 case tok::semi:
564 if (StopAtSemi)
565 return false;
566 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000567 default:
568 // consume this token.
569 Toks.push_back(Tok);
570 ConsumeToken();
571 break;
572 }
573 isFirstTokenConsumed = false;
574 }
575}
Sebastian Redl6df65482011-09-24 17:48:25 +0000576
577/// \brief Consume tokens and store them in the passed token container until
578/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redla891a322011-09-30 08:32:17 +0000579/// the opening brace of the function body. The opening brace will be consumed
580/// if and only if there was no error.
Sebastian Redl6df65482011-09-24 17:48:25 +0000581///
Sebastian Redla891a322011-09-30 08:32:17 +0000582/// \return True on error.
583bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000584 if (Tok.is(tok::kw_try)) {
585 Toks.push_back(Tok);
586 ConsumeToken();
587 }
588 if (Tok.is(tok::colon)) {
589 // Initializers can contain braces too.
590 Toks.push_back(Tok);
591 ConsumeToken();
592
593 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
594 if (Tok.is(tok::eof) || Tok.is(tok::semi))
595 return true;
596
597 // Grab the identifier.
598 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
599 /*StopAtSemi=*/true,
600 /*ConsumeFinalToken=*/false))
601 return true;
602
603 tok::TokenKind kind = Tok.getKind();
604 Toks.push_back(Tok);
605 if (kind == tok::l_paren)
606 ConsumeParen();
607 else {
608 assert(kind == tok::l_brace && "Must be left paren or brace here.");
609 ConsumeBrace();
Sebastian Redla891a322011-09-30 08:32:17 +0000610 // In C++03, this has to be the start of the function body, which
611 // means the initializer is malformed.
612 if (!getLang().CPlusPlus0x)
613 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000614 }
615
616 // Grab the initializer
617 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
618 tok::r_brace,
619 Toks, /*StopAtSemi=*/true))
620 return true;
Sebastian Redla891a322011-09-30 08:32:17 +0000621
622 // Grab the separating comma, if any.
623 if (Tok.is(tok::comma)) {
624 Toks.push_back(Tok);
625 ConsumeToken();
626 }
Sebastian Redl6df65482011-09-24 17:48:25 +0000627 }
628 }
629
Sebastian Redla891a322011-09-30 08:32:17 +0000630 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
631 // brace: an opening one is the function body, while a closing one probably
632 // means we've reached the end of the class.
633 if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
634 /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false))
635 return true;
636 if(Tok.isNot(tok::l_brace))
637 return true;
638
639 Toks.push_back(Tok);
640 ConsumeBrace();
641 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000642}