blob: 2877628f262f68c8a5cbf8adf275d6bcb76f78c6 [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,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +000027 const ParsedTemplateInfo &TemplateInfo,
28 const VirtSpecifiers& VS,
29 FunctionDefinitionKind DefinitionKind,
30 ExprResult& Init) {
Abramo Bagnara924a8f32010-12-10 16:29:40 +000031 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Alexis Hunt5a7fa252011-05-12 06:15:49 +000032 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
33 Tok.is(tok::equal)) &&
34 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000035
John McCallfaf5fb42010-08-26 23:41:50 +000036 MultiTemplateParamsArg TemplateParams(Actions,
Alexis Hunt1deb9722010-04-14 23:07:37 +000037 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
38 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
39
John McCall48871652010-08-21 09:40:31 +000040 Decl *FnD;
Douglas Gregor5d1b4e32011-11-07 20:56:01 +000041 D.setFunctionDefinitionKind(DefinitionKind);
John McCall07e91c02009-08-06 02:15:43 +000042 if (D.getDeclSpec().isFriendSpecified())
Kaelyn Uhrain4dc695d2011-10-11 00:28:45 +000043 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
Alexis Hunt1deb9722010-04-14 23:07:37 +000044 move(TemplateParams));
Douglas Gregor728d00b2011-10-10 14:49:18 +000045 else {
Douglas Gregor0be31a22010-07-02 17:43:08 +000046 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlssondb36b802011-01-20 03:57:25 +000047 move(TemplateParams), 0,
Douglas Gregor50cefbf2011-10-17 17:09:53 +000048 VS, /*HasDeferredInit=*/false);
Douglas Gregor728d00b2011-10-10 14:49:18 +000049 if (FnD) {
Erik Verbruggenca98f2a2011-10-13 09:41:32 +000050 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
51 false, true);
Douglas Gregor728d00b2011-10-10 14:49:18 +000052 bool TypeSpecContainsAuto
53 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
Douglas Gregor50cefbf2011-10-17 17:09:53 +000054 if (Init.isUsable())
Douglas Gregor728d00b2011-10-10 14:49:18 +000055 Actions.AddInitializerToDecl(FnD, Init.get(), false,
56 TypeSpecContainsAuto);
57 else
58 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
59 }
Nico Weber24b2a822011-01-28 06:07:34 +000060 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000061
Eli Friedman3af2a772009-07-22 21:45:50 +000062 HandleMemberFunctionDefaultArgs(D, FnD);
63
John McCallc1465822011-02-14 07:13:47 +000064 D.complete(FnD);
65
Alexis Hunt5a7fa252011-05-12 06:15:49 +000066 if (Tok.is(tok::equal)) {
67 ConsumeToken();
68
Richard Smith1c704732011-11-10 09:08:44 +000069 if (!FnD) {
70 SkipUntil(tok::semi);
71 return 0;
72 }
73
Alexis Hunt5a7fa252011-05-12 06:15:49 +000074 bool Delete = false;
75 SourceLocation KWLoc;
76 if (Tok.is(tok::kw_delete)) {
Richard Smith5d164bc2011-10-15 05:09:34 +000077 Diag(Tok, getLang().CPlusPlus0x ?
78 diag::warn_cxx98_compat_deleted_function :
79 diag::warn_deleted_function_accepted_as_extension);
Alexis Hunt5a7fa252011-05-12 06:15:49 +000080
81 KWLoc = ConsumeToken();
82 Actions.SetDeclDeleted(FnD, KWLoc);
83 Delete = true;
84 } else if (Tok.is(tok::kw_default)) {
Richard Smith5d164bc2011-10-15 05:09:34 +000085 Diag(Tok, getLang().CPlusPlus0x ?
86 diag::warn_cxx98_compat_defaulted_function :
87 diag::warn_defaulted_function_accepted_as_extension);
Alexis Hunt5a7fa252011-05-12 06:15:49 +000088
89 KWLoc = ConsumeToken();
90 Actions.SetDeclDefaulted(FnD, KWLoc);
91 } else {
92 llvm_unreachable("function definition after = not 'delete' or 'default'");
93 }
94
95 if (Tok.is(tok::comma)) {
96 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
97 << Delete;
98 SkipUntil(tok::semi);
99 } else {
100 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
101 Delete ? "delete" : "default", tok::semi);
102 }
103
104 return FnD;
105 }
106
Francois Pichet1c229c02011-04-22 22:18:13 +0000107 // In delayed template parsing mode, if we are within a class template
108 // or if we are about to parse function member template then consume
109 // the tokens and store them for parsing at the end of the translation unit.
110 if (getLang().DelayedTemplateParsing &&
111 ((Actions.CurContext->isDependentContext() ||
112 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
113 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
114 !D.getDeclSpec().isFriendSpecified()) {
115
116 if (FnD) {
117 LateParsedTemplatedFunction *LPT =
118 new LateParsedTemplatedFunction(this, FnD);
119
120 FunctionDecl *FD = 0;
121 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
122 FD = FunTmpl->getTemplatedDecl();
123 else
124 FD = cast<FunctionDecl>(FnD);
Chandler Carruthbc0f9ae2011-04-25 07:09:43 +0000125 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet1c229c02011-04-22 22:18:13 +0000126
127 LateParsedTemplateMap[FD] = LPT;
128 Actions.MarkAsLateParsedTemplate(FD);
129 LexTemplateFunctionForLateParsing(LPT->Toks);
130 } else {
131 CachedTokens Toks;
132 LexTemplateFunctionForLateParsing(Toks);
133 }
134
135 return FnD;
136 }
137
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000138 // Consume the tokens and store them for later parsing.
139
Douglas Gregorefc46952010-10-12 16:25:54 +0000140 LexedMethod* LM = new LexedMethod(this, FnD);
141 getCurrentClass().LateParsedDeclarations.push_back(LM);
142 LM->TemplateScope = getCurScope()->isTemplateParamScope();
143 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000144
Sebastian Redla7b98a72009-04-26 20:35:05 +0000145 tok::TokenKind kind = Tok.getKind();
Sebastian Redl0d164012011-09-30 08:32:17 +0000146 // Consume everything up to (and including) the left brace of the
147 // function body.
148 if (ConsumeAndStoreFunctionPrologue(Toks)) {
149 // We didn't find the left-brace we expected after the
150 // constructor initializer.
151 if (Tok.is(tok::semi)) {
152 // We found a semicolon; complain, consume the semicolon, and
153 // don't try to parse this method later.
154 Diag(Tok.getLocation(), diag::err_expected_lbrace);
155 ConsumeAnyToken();
156 delete getCurrentClass().LateParsedDeclarations.back();
157 getCurrentClass().LateParsedDeclarations.pop_back();
158 return FnD;
Douglas Gregor49de5342008-11-10 16:59:40 +0000159 }
Douglas Gregore8381c02008-11-05 04:29:56 +0000160 } else {
Sebastian Redl0d164012011-09-30 08:32:17 +0000161 // Consume everything up to (and including) the matching right brace.
162 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregore8381c02008-11-05 04:29:56 +0000163 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000164
Sebastian Redla7b98a72009-04-26 20:35:05 +0000165 // If we're in a function-try-block, we need to store all the catch blocks.
166 if (kind == tok::kw_try) {
167 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000168 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
169 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000170 }
171 }
172
Douglas Gregor6ca64102011-04-14 23:19:27 +0000173
174 if (!FnD) {
175 // If semantic analysis could not build a function declaration,
176 // just throw away the late-parsed declaration.
177 delete getCurrentClass().LateParsedDeclarations.back();
178 getCurrentClass().LateParsedDeclarations.pop_back();
179 }
180
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000181 return FnD;
182}
183
Richard Smith938f40b2011-06-11 17:19:42 +0000184/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
185/// specified Declarator is a well formed C++ non-static data member
186/// declaration. Now lex its initializer and store its tokens for parsing
187/// after the class is complete.
188void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
189 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
190 "Current token not a '{' or '='!");
191
192 LateParsedMemberInitializer *MI =
193 new LateParsedMemberInitializer(this, VarD);
194 getCurrentClass().LateParsedDeclarations.push_back(MI);
195 CachedTokens &Toks = MI->Toks;
196
197 tok::TokenKind kind = Tok.getKind();
198 if (kind == tok::equal) {
199 Toks.push_back(Tok);
200 ConsumeAnyToken();
201 }
202
203 if (kind == tok::l_brace) {
204 // Begin by storing the '{' token.
205 Toks.push_back(Tok);
206 ConsumeBrace();
207
208 // Consume everything up to (and including) the matching right brace.
209 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
210 } else {
211 // Consume everything up to (but excluding) the comma or semicolon.
212 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
213 /*ConsumeFinalToken=*/false);
214 }
215
216 // Store an artificial EOF token to ensure that we don't run off the end of
217 // the initializer when we come to parse it.
218 Token Eof;
219 Eof.startToken();
220 Eof.setKind(tok::eof);
221 Eof.setLocation(Tok.getLocation());
222 Toks.push_back(Eof);
223}
224
Douglas Gregorefc46952010-10-12 16:25:54 +0000225Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
226void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith938f40b2011-06-11 17:19:42 +0000227void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregorefc46952010-10-12 16:25:54 +0000228void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
229
230Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
231 : Self(P), Class(C) {}
232
233Parser::LateParsedClass::~LateParsedClass() {
234 Self->DeallocateParsedClasses(Class);
235}
236
237void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
238 Self->ParseLexedMethodDeclarations(*Class);
239}
240
Richard Smith938f40b2011-06-11 17:19:42 +0000241void Parser::LateParsedClass::ParseLexedMemberInitializers() {
242 Self->ParseLexedMemberInitializers(*Class);
243}
244
Douglas Gregorefc46952010-10-12 16:25:54 +0000245void Parser::LateParsedClass::ParseLexedMethodDefs() {
246 Self->ParseLexedMethodDefs(*Class);
247}
248
249void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
250 Self->ParseLexedMethodDeclaration(*this);
251}
252
253void Parser::LexedMethod::ParseLexedMethodDefs() {
254 Self->ParseLexedMethodDef(*this);
255}
256
Richard Smith938f40b2011-06-11 17:19:42 +0000257void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
258 Self->ParseLexedMemberInitializer(*this);
259}
260
Douglas Gregor4d87df52008-12-16 21:30:33 +0000261/// ParseLexedMethodDeclarations - We finished parsing the member
262/// specification of a top (non-nested) C++ class. Now go over the
263/// stack of method declarations with some parts for which parsing was
264/// delayed (such as default arguments) and parse them.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000265void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
266 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000267 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000268 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000269 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000270
John McCall6df5fef2009-12-19 10:49:29 +0000271 // The current scope is still active if we're the top-level class.
272 // Otherwise we'll need to push and enter a new scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000273 bool HasClassScope = !Class.TopLevelClass;
Alexis Hunt1deb9722010-04-14 23:07:37 +0000274 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
275 HasClassScope);
John McCall6df5fef2009-12-19 10:49:29 +0000276 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000277 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000278
Douglas Gregorefc46952010-10-12 16:25:54 +0000279 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
280 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor4d87df52008-12-16 21:30:33 +0000281 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000282
John McCall6df5fef2009-12-19 10:49:29 +0000283 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000284 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor4d87df52008-12-16 21:30:33 +0000285}
286
Douglas Gregorefc46952010-10-12 16:25:54 +0000287void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
288 // If this is a member template, introduce the template parameter scope.
289 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
290 if (LM.TemplateScope)
291 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
292
293 // Start the delayed C++ method declaration
294 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
295
296 // Introduce the parameters into scope and parse their default
297 // arguments.
298 ParseScope PrototypeScope(this,
299 Scope::FunctionPrototypeScope|Scope::DeclScope);
300 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
301 // Introduce the parameter into scope.
302 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
303
304 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
305 // Save the current token position.
306 SourceLocation origLoc = Tok.getLocation();
307
308 // Parse the default argument from its saved token stream.
309 Toks->push_back(Tok); // So that the current token doesn't get lost
310 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
311
312 // Consume the previously-pushed token.
313 ConsumeAnyToken();
314
315 // Consume the '='.
316 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
317 SourceLocation EqualLoc = ConsumeToken();
318
319 // The argument isn't actually potentially evaluated unless it is
320 // used.
321 EnterExpressionEvaluationContext Eval(Actions,
322 Sema::PotentiallyEvaluatedIfUsed);
323
324 ExprResult DefArgResult(ParseAssignmentExpression());
325 if (DefArgResult.isInvalid())
326 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
327 else {
328 if (Tok.is(tok::cxx_defaultarg_end))
329 ConsumeToken();
330 else
331 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
332 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
333 DefArgResult.take());
334 }
335
336 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
337 Tok.getLocation()) &&
338 "ParseAssignmentExpression went over the default arg tokens!");
339 // There could be leftover tokens (e.g. because of an error).
340 // Skip through until we reach the original token position.
341 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
342 ConsumeAnyToken();
343
344 delete Toks;
345 LM.DefaultArgs[I].Toks = 0;
346 }
347 }
348 PrototypeScope.Exit();
349
350 // Finish the delayed C++ method declaration.
351 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
352}
353
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000354/// ParseLexedMethodDefs - We finished parsing the member specification of a top
355/// (non-nested) C++ class. Now go over the stack of lexed methods that were
356/// collected during its parsing and parse them all.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000357void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
358 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000359 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000360 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000361 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000362
363 bool HasClassScope = !Class.TopLevelClass;
364 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
365 HasClassScope);
366
Douglas Gregorefc46952010-10-12 16:25:54 +0000367 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
368 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
369 }
370}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000371
Douglas Gregorefc46952010-10-12 16:25:54 +0000372void Parser::ParseLexedMethodDef(LexedMethod &LM) {
373 // If this is a member template, introduce the template parameter scope.
374 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
375 if (LM.TemplateScope)
376 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump11289f42009-09-09 15:08:12 +0000377
Douglas Gregorefc46952010-10-12 16:25:54 +0000378 // Save the current token position.
379 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidis02041972010-03-31 00:38:09 +0000380
Douglas Gregorefc46952010-10-12 16:25:54 +0000381 assert(!LM.Toks.empty() && "Empty body!");
382 // Append the current token at the end of the new token stream so that it
383 // doesn't get lost.
384 LM.Toks.push_back(Tok);
385 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000386
Douglas Gregorefc46952010-10-12 16:25:54 +0000387 // Consume the previously pushed token.
388 ConsumeAnyToken();
389 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
390 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000391
Douglas Gregorefc46952010-10-12 16:25:54 +0000392 // Parse the method body. Function body parsing code is similar enough
393 // to be re-used for method bodies as well.
394 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
395 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000396
Douglas Gregorefc46952010-10-12 16:25:54 +0000397 if (Tok.is(tok::kw_try)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000398 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000399 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
400 Tok.getLocation()) &&
401 "ParseFunctionTryBlock went over the cached tokens!");
402 // There could be leftover tokens (e.g. because of an error).
403 // Skip through until we reach the original token position.
404 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
405 ConsumeAnyToken();
406 return;
407 }
408 if (Tok.is(tok::colon)) {
409 ParseConstructorInitializer(LM.D);
410
411 // Error recovery.
412 if (!Tok.is(tok::l_brace)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000413 FnScope.Exit();
Douglas Gregorefc46952010-10-12 16:25:54 +0000414 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gayd0457922011-09-23 22:39:23 +0000415 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
416 ConsumeAnyToken();
Douglas Gregorefc46952010-10-12 16:25:54 +0000417 return;
418 }
419 } else
420 Actions.ActOnDefaultCtorInitializers(LM.D);
421
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000422 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000423
424 if (Tok.getLocation() != origLoc) {
425 // Due to parsing error, we either went over the cached tokens or
426 // there are still cached tokens left. If it's the latter case skip the
427 // leftover tokens.
428 // Since this is an uncommon situation that should be avoided, use the
429 // expensive isBeforeInTranslationUnit call.
430 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
431 origLoc))
Argyrios Kyrtzidise1224c82010-06-19 19:58:34 +0000432 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidise9b76af2010-06-17 10:52:22 +0000433 ConsumeAnyToken();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000434 }
435}
436
Richard Smith938f40b2011-06-11 17:19:42 +0000437/// ParseLexedMemberInitializers - We finished parsing the member specification
438/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
439/// initializers that were collected during its parsing and parse them all.
440void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
441 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
442 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
443 HasTemplateScope);
444 if (HasTemplateScope)
445 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
446
447 // Set or update the scope flags to include Scope::ThisScope.
448 bool AlreadyHasClassScope = Class.TopLevelClass;
449 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
450 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
451 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
452
453 if (!AlreadyHasClassScope)
454 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
455 Class.TagOrTemplate);
456
457 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
458 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
459 }
460
461 if (!AlreadyHasClassScope)
462 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
463 Class.TagOrTemplate);
464
465 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
466}
467
468void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1a526fd2011-09-29 19:42:27 +0000469 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith938f40b2011-06-11 17:19:42 +0000470 return;
471
472 // Append the current token at the end of the new token stream so that it
473 // doesn't get lost.
474 MI.Toks.push_back(Tok);
475 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
476
477 // Consume the previously pushed token.
478 ConsumeAnyToken();
479
480 SourceLocation EqualLoc;
481 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
482
483 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
484
485 // The next token should be our artificial terminating EOF token.
486 if (Tok.isNot(tok::eof)) {
487 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
488 if (!EndLoc.isValid())
489 EndLoc = Tok.getLocation();
490 // No fixit; we can't recover as if there were a semicolon here.
491 Diag(EndLoc, diag::err_expected_semi_decl_list);
492
493 // Consume tokens until we hit the artificial EOF.
494 while (Tok.isNot(tok::eof))
495 ConsumeAnyToken();
496 }
497 ConsumeAnyToken();
498}
499
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000500/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor4d87df52008-12-16 21:30:33 +0000501/// container until the token 'T' is reached (which gets
Mike Stump11289f42009-09-09 15:08:12 +0000502/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000503/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000504/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000505/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000506bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
507 CachedTokens &Toks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000508 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000509 // We always want this function to consume at least one token if the first
510 // token isn't T and if not at EOF.
511 bool isFirstTokenConsumed = true;
512 while (1) {
513 // If we found one of the tokens, stop and return true.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000514 if (Tok.is(T1) || Tok.is(T2)) {
515 if (ConsumeFinalToken) {
516 Toks.push_back(Tok);
517 ConsumeAnyToken();
518 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000519 return true;
520 }
521
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000522 switch (Tok.getKind()) {
523 case tok::eof:
524 // Ran out of tokens.
525 return false;
526
527 case tok::l_paren:
528 // Recursively consume properly-nested parens.
529 Toks.push_back(Tok);
530 ConsumeParen();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000531 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000532 break;
533 case tok::l_square:
534 // Recursively consume properly-nested square brackets.
535 Toks.push_back(Tok);
536 ConsumeBracket();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000537 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000538 break;
539 case tok::l_brace:
540 // Recursively consume properly-nested braces.
541 Toks.push_back(Tok);
542 ConsumeBrace();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000543 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000544 break;
545
546 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
547 // Since the user wasn't looking for this token (if they were, it would
548 // already be handled), this isn't balanced. If there is a LHS token at a
549 // higher level, we will assume that this matches the unbalanced token
550 // and return it. Otherwise, this is a spurious RHS token, which we skip.
551 case tok::r_paren:
552 if (ParenCount && !isFirstTokenConsumed)
553 return false; // Matches something.
554 Toks.push_back(Tok);
555 ConsumeParen();
556 break;
557 case tok::r_square:
558 if (BracketCount && !isFirstTokenConsumed)
559 return false; // Matches something.
560 Toks.push_back(Tok);
561 ConsumeBracket();
562 break;
563 case tok::r_brace:
564 if (BraceCount && !isFirstTokenConsumed)
565 return false; // Matches something.
566 Toks.push_back(Tok);
567 ConsumeBrace();
568 break;
569
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000570 case tok::code_completion:
571 Toks.push_back(Tok);
572 ConsumeCodeCompletionToken();
573 break;
574
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000575 case tok::string_literal:
576 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000577 case tok::utf8_string_literal:
578 case tok::utf16_string_literal:
579 case tok::utf32_string_literal:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000580 Toks.push_back(Tok);
581 ConsumeStringToken();
582 break;
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000583 case tok::semi:
584 if (StopAtSemi)
585 return false;
586 // FALL THROUGH.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000587 default:
588 // consume this token.
589 Toks.push_back(Tok);
590 ConsumeToken();
591 break;
592 }
593 isFirstTokenConsumed = false;
594 }
595}
Sebastian Redla74948d2011-09-24 17:48:25 +0000596
597/// \brief Consume tokens and store them in the passed token container until
598/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redl0d164012011-09-30 08:32:17 +0000599/// the opening brace of the function body. The opening brace will be consumed
600/// if and only if there was no error.
Sebastian Redla74948d2011-09-24 17:48:25 +0000601///
Sebastian Redl0d164012011-09-30 08:32:17 +0000602/// \return True on error.
603bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redla74948d2011-09-24 17:48:25 +0000604 if (Tok.is(tok::kw_try)) {
605 Toks.push_back(Tok);
606 ConsumeToken();
607 }
608 if (Tok.is(tok::colon)) {
609 // Initializers can contain braces too.
610 Toks.push_back(Tok);
611 ConsumeToken();
612
613 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
614 if (Tok.is(tok::eof) || Tok.is(tok::semi))
615 return true;
616
617 // Grab the identifier.
618 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
619 /*StopAtSemi=*/true,
620 /*ConsumeFinalToken=*/false))
621 return true;
622
623 tok::TokenKind kind = Tok.getKind();
624 Toks.push_back(Tok);
625 if (kind == tok::l_paren)
626 ConsumeParen();
627 else {
628 assert(kind == tok::l_brace && "Must be left paren or brace here.");
629 ConsumeBrace();
Sebastian Redl0d164012011-09-30 08:32:17 +0000630 // In C++03, this has to be the start of the function body, which
631 // means the initializer is malformed.
632 if (!getLang().CPlusPlus0x)
633 return false;
Sebastian Redla74948d2011-09-24 17:48:25 +0000634 }
635
636 // Grab the initializer
637 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
638 tok::r_brace,
639 Toks, /*StopAtSemi=*/true))
640 return true;
Sebastian Redl0d164012011-09-30 08:32:17 +0000641
642 // Grab the separating comma, if any.
643 if (Tok.is(tok::comma)) {
644 Toks.push_back(Tok);
645 ConsumeToken();
646 }
Sebastian Redla74948d2011-09-24 17:48:25 +0000647 }
648 }
649
Sebastian Redl0d164012011-09-30 08:32:17 +0000650 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
651 // brace: an opening one is the function body, while a closing one probably
652 // means we've reached the end of the class.
653 if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
654 /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false))
655 return true;
656 if(Tok.isNot(tok::l_brace))
657 return true;
658
659 Toks.push_back(Tok);
660 ConsumeBrace();
661 return false;
Sebastian Redla74948d2011-09-24 17:48:25 +0000662}