blob: 8ed058787e469a20f61636edce280cccddc805bd [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 :
Richard Smithe4345902011-12-29 21:57:33 +000079 diag::ext_deleted_function);
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 :
Richard Smithe4345902011-12-29 21:57:33 +000087 diag::ext_defaulted_function);
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) &&
Francois Pichet6dc4c162011-11-18 23:47:17 +0000113 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
Francois Pichet1c229c02011-04-22 22:18:13 +0000114
115 if (FnD) {
Francois Pichet33786cb2011-12-08 09:11:52 +0000116 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
Francois Pichet1c229c02011-04-22 22:18:13 +0000117
118 FunctionDecl *FD = 0;
119 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
120 FD = FunTmpl->getTemplatedDecl();
121 else
122 FD = cast<FunctionDecl>(FnD);
Chandler Carruthbc0f9ae2011-04-25 07:09:43 +0000123 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet1c229c02011-04-22 22:18:13 +0000124
125 LateParsedTemplateMap[FD] = LPT;
126 Actions.MarkAsLateParsedTemplate(FD);
127 LexTemplateFunctionForLateParsing(LPT->Toks);
128 } else {
129 CachedTokens Toks;
130 LexTemplateFunctionForLateParsing(Toks);
131 }
132
133 return FnD;
134 }
135
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000136 // Consume the tokens and store them for later parsing.
137
Douglas Gregorefc46952010-10-12 16:25:54 +0000138 LexedMethod* LM = new LexedMethod(this, FnD);
139 getCurrentClass().LateParsedDeclarations.push_back(LM);
140 LM->TemplateScope = getCurScope()->isTemplateParamScope();
141 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000142
Sebastian Redla7b98a72009-04-26 20:35:05 +0000143 tok::TokenKind kind = Tok.getKind();
Sebastian Redl0d164012011-09-30 08:32:17 +0000144 // Consume everything up to (and including) the left brace of the
145 // function body.
146 if (ConsumeAndStoreFunctionPrologue(Toks)) {
147 // We didn't find the left-brace we expected after the
148 // constructor initializer.
149 if (Tok.is(tok::semi)) {
150 // We found a semicolon; complain, consume the semicolon, and
151 // don't try to parse this method later.
152 Diag(Tok.getLocation(), diag::err_expected_lbrace);
153 ConsumeAnyToken();
154 delete getCurrentClass().LateParsedDeclarations.back();
155 getCurrentClass().LateParsedDeclarations.pop_back();
156 return FnD;
Douglas Gregor49de5342008-11-10 16:59:40 +0000157 }
Douglas Gregore8381c02008-11-05 04:29:56 +0000158 } else {
Sebastian Redl0d164012011-09-30 08:32:17 +0000159 // Consume everything up to (and including) the matching right brace.
160 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregore8381c02008-11-05 04:29:56 +0000161 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000162
Sebastian Redla7b98a72009-04-26 20:35:05 +0000163 // If we're in a function-try-block, we need to store all the catch blocks.
164 if (kind == tok::kw_try) {
165 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000166 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
167 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000168 }
169 }
170
Douglas Gregor6ca64102011-04-14 23:19:27 +0000171
172 if (!FnD) {
173 // If semantic analysis could not build a function declaration,
174 // just throw away the late-parsed declaration.
175 delete getCurrentClass().LateParsedDeclarations.back();
176 getCurrentClass().LateParsedDeclarations.pop_back();
177 }
178
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000179 return FnD;
180}
181
Richard Smith938f40b2011-06-11 17:19:42 +0000182/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
183/// specified Declarator is a well formed C++ non-static data member
184/// declaration. Now lex its initializer and store its tokens for parsing
185/// after the class is complete.
186void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
187 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
188 "Current token not a '{' or '='!");
189
190 LateParsedMemberInitializer *MI =
191 new LateParsedMemberInitializer(this, VarD);
192 getCurrentClass().LateParsedDeclarations.push_back(MI);
193 CachedTokens &Toks = MI->Toks;
194
195 tok::TokenKind kind = Tok.getKind();
196 if (kind == tok::equal) {
197 Toks.push_back(Tok);
198 ConsumeAnyToken();
199 }
200
201 if (kind == tok::l_brace) {
202 // Begin by storing the '{' token.
203 Toks.push_back(Tok);
204 ConsumeBrace();
205
206 // Consume everything up to (and including) the matching right brace.
207 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
208 } else {
209 // Consume everything up to (but excluding) the comma or semicolon.
210 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
211 /*ConsumeFinalToken=*/false);
212 }
213
214 // Store an artificial EOF token to ensure that we don't run off the end of
215 // the initializer when we come to parse it.
216 Token Eof;
217 Eof.startToken();
218 Eof.setKind(tok::eof);
219 Eof.setLocation(Tok.getLocation());
220 Toks.push_back(Eof);
221}
222
Douglas Gregorefc46952010-10-12 16:25:54 +0000223Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
224void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith938f40b2011-06-11 17:19:42 +0000225void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregorefc46952010-10-12 16:25:54 +0000226void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
227
228Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
229 : Self(P), Class(C) {}
230
231Parser::LateParsedClass::~LateParsedClass() {
232 Self->DeallocateParsedClasses(Class);
233}
234
235void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
236 Self->ParseLexedMethodDeclarations(*Class);
237}
238
Richard Smith938f40b2011-06-11 17:19:42 +0000239void Parser::LateParsedClass::ParseLexedMemberInitializers() {
240 Self->ParseLexedMemberInitializers(*Class);
241}
242
Douglas Gregorefc46952010-10-12 16:25:54 +0000243void Parser::LateParsedClass::ParseLexedMethodDefs() {
244 Self->ParseLexedMethodDefs(*Class);
245}
246
247void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
248 Self->ParseLexedMethodDeclaration(*this);
249}
250
251void Parser::LexedMethod::ParseLexedMethodDefs() {
252 Self->ParseLexedMethodDef(*this);
253}
254
Richard Smith938f40b2011-06-11 17:19:42 +0000255void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
256 Self->ParseLexedMemberInitializer(*this);
257}
258
Douglas Gregor4d87df52008-12-16 21:30:33 +0000259/// ParseLexedMethodDeclarations - We finished parsing the member
260/// specification of a top (non-nested) C++ class. Now go over the
261/// stack of method declarations with some parts for which parsing was
262/// delayed (such as default arguments) and parse them.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000263void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
264 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000265 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000266 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000267 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000268
John McCall6df5fef2009-12-19 10:49:29 +0000269 // The current scope is still active if we're the top-level class.
270 // Otherwise we'll need to push and enter a new scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000271 bool HasClassScope = !Class.TopLevelClass;
Alexis Hunt1deb9722010-04-14 23:07:37 +0000272 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
273 HasClassScope);
John McCall6df5fef2009-12-19 10:49:29 +0000274 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000275 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000276
Douglas Gregorefc46952010-10-12 16:25:54 +0000277 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
278 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor4d87df52008-12-16 21:30:33 +0000279 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000280
John McCall6df5fef2009-12-19 10:49:29 +0000281 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000282 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor4d87df52008-12-16 21:30:33 +0000283}
284
Douglas Gregorefc46952010-10-12 16:25:54 +0000285void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
286 // If this is a member template, introduce the template parameter scope.
287 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
288 if (LM.TemplateScope)
289 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
290
291 // Start the delayed C++ method declaration
292 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
293
294 // Introduce the parameters into scope and parse their default
295 // arguments.
296 ParseScope PrototypeScope(this,
297 Scope::FunctionPrototypeScope|Scope::DeclScope);
298 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
299 // Introduce the parameter into scope.
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000300 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
301 LM.DefaultArgs[I].Param);
Douglas Gregorefc46952010-10-12 16:25:54 +0000302
303 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
304 // Save the current token position.
305 SourceLocation origLoc = Tok.getLocation();
306
307 // Parse the default argument from its saved token stream.
308 Toks->push_back(Tok); // So that the current token doesn't get lost
309 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
310
311 // Consume the previously-pushed token.
312 ConsumeAnyToken();
313
314 // Consume the '='.
315 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
316 SourceLocation EqualLoc = ConsumeToken();
317
318 // The argument isn't actually potentially evaluated unless it is
319 // used.
320 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000321 Sema::PotentiallyEvaluatedIfUsed,
322 LM.DefaultArgs[I].Param);
Douglas Gregorefc46952010-10-12 16:25:54 +0000323
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}