blob: 2a64be53f77957ad1523aa7ee09f3162e9623496 [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"
John McCall92576642012-05-07 06:16:41 +000019#include "RAIIObjectsForParser.h"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000020using namespace clang;
21
Sebastian Redld3a413d2009-04-26 20:35:05 +000022/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000023/// Declarator is a well formed C++ inline method definition. Now lex its body
24/// and store its tokens for parsing after the C++ class is complete.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000025Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
26 AttributeList *AccessAttrs,
27 ParsingDeclarator &D,
Douglas Gregor45fa5602011-11-07 20:56:01 +000028 const ParsedTemplateInfo &TemplateInfo,
29 const VirtSpecifiers& VS,
30 FunctionDefinitionKind DefinitionKind,
31 ExprResult& Init) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000032 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sean Hunte4246a62011-05-12 06:15:49 +000033 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
34 Tok.is(tok::equal)) &&
35 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000036
John McCallf312b1e2010-08-26 23:41:50 +000037 MultiTemplateParamsArg TemplateParams(Actions,
Sean Hunt4cd84942010-04-14 23:07:37 +000038 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
39 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
40
John McCalld226f652010-08-21 09:40:31 +000041 Decl *FnD;
Douglas Gregor45fa5602011-11-07 20:56:01 +000042 D.setFunctionDefinitionKind(DefinitionKind);
John McCall67d1a672009-08-06 02:15:43 +000043 if (D.getDeclSpec().isFriendSpecified())
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +000044 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
Sean Hunt4cd84942010-04-14 23:07:37 +000045 move(TemplateParams));
Douglas Gregor147545d2011-10-10 14:49:18 +000046 else {
Douglas Gregor23c94db2010-07-02 17:43:08 +000047 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlsson69a87352011-01-20 03:57:25 +000048 move(TemplateParams), 0,
Douglas Gregora2b4e5d2011-10-17 17:09:53 +000049 VS, /*HasDeferredInit=*/false);
Douglas Gregor147545d2011-10-10 14:49:18 +000050 if (FnD) {
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000051 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
52 false, true);
Douglas Gregor147545d2011-10-10 14:49:18 +000053 bool TypeSpecContainsAuto
54 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +000055 if (Init.isUsable())
Douglas Gregor147545d2011-10-10 14:49:18 +000056 Actions.AddInitializerToDecl(FnD, Init.get(), false,
57 TypeSpecContainsAuto);
58 else
59 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
60 }
Nico Weber48673472011-01-28 06:07:34 +000061 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000062
Douglas Gregor74e2fc32012-04-16 18:27:27 +000063 HandleMemberFunctionDeclDelays(D, FnD);
Eli Friedmand33133c2009-07-22 21:45:50 +000064
John McCalleee1d542011-02-14 07:13:47 +000065 D.complete(FnD);
66
Sean Hunte4246a62011-05-12 06:15:49 +000067 if (Tok.is(tok::equal)) {
68 ConsumeToken();
69
Richard Smithc430ef42011-11-10 09:08:44 +000070 if (!FnD) {
71 SkipUntil(tok::semi);
72 return 0;
73 }
74
Sean Hunte4246a62011-05-12 06:15:49 +000075 bool Delete = false;
76 SourceLocation KWLoc;
77 if (Tok.is(tok::kw_delete)) {
David Blaikie4e4d0842012-03-11 07:00:24 +000078 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +000079 diag::warn_cxx98_compat_deleted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000080 diag::ext_deleted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000081
82 KWLoc = ConsumeToken();
83 Actions.SetDeclDeleted(FnD, KWLoc);
84 Delete = true;
85 } else if (Tok.is(tok::kw_default)) {
David Blaikie4e4d0842012-03-11 07:00:24 +000086 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +000087 diag::warn_cxx98_compat_defaulted_function :
Richard Smithd7c56e12011-12-29 21:57:33 +000088 diag::ext_defaulted_function);
Sean Hunte4246a62011-05-12 06:15:49 +000089
90 KWLoc = ConsumeToken();
91 Actions.SetDeclDefaulted(FnD, KWLoc);
92 } else {
93 llvm_unreachable("function definition after = not 'delete' or 'default'");
94 }
95
96 if (Tok.is(tok::comma)) {
97 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
98 << Delete;
99 SkipUntil(tok::semi);
100 } else {
101 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
102 Delete ? "delete" : "default", tok::semi);
103 }
104
105 return FnD;
106 }
107
Francois Pichet8387e2a2011-04-22 22:18:13 +0000108 // In delayed template parsing mode, if we are within a class template
109 // or if we are about to parse function member template then consume
110 // the tokens and store them for parsing at the end of the translation unit.
David Blaikie4e4d0842012-03-11 07:00:24 +0000111 if (getLangOpts().DelayedTemplateParsing &&
Francois Pichet8387e2a2011-04-22 22:18:13 +0000112 ((Actions.CurContext->isDependentContext() ||
113 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
Francois Pichet9d38dbc2011-11-18 23:47:17 +0000114 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
Francois Pichet8387e2a2011-04-22 22:18:13 +0000115
116 if (FnD) {
Francois Pichete1fca502011-12-08 09:11:52 +0000117 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000118
119 FunctionDecl *FD = 0;
120 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
121 FD = FunTmpl->getTemplatedDecl();
122 else
123 FD = cast<FunctionDecl>(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +0000124 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000125
126 LateParsedTemplateMap[FD] = LPT;
127 Actions.MarkAsLateParsedTemplate(FD);
128 LexTemplateFunctionForLateParsing(LPT->Toks);
129 } else {
130 CachedTokens Toks;
131 LexTemplateFunctionForLateParsing(Toks);
132 }
133
134 return FnD;
135 }
136
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000137 // Consume the tokens and store them for later parsing.
138
Douglas Gregord54eb442010-10-12 16:25:54 +0000139 LexedMethod* LM = new LexedMethod(this, FnD);
140 getCurrentClass().LateParsedDeclarations.push_back(LM);
141 LM->TemplateScope = getCurScope()->isTemplateParamScope();
142 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000143
Sebastian Redld3a413d2009-04-26 20:35:05 +0000144 tok::TokenKind kind = Tok.getKind();
Sebastian Redla891a322011-09-30 08:32:17 +0000145 // Consume everything up to (and including) the left brace of the
146 // function body.
147 if (ConsumeAndStoreFunctionPrologue(Toks)) {
148 // We didn't find the left-brace we expected after the
Eli Friedmane9ee3822012-02-22 04:49:04 +0000149 // constructor initializer; we already printed an error, and it's likely
150 // impossible to recover, so don't try to parse this method later.
151 // If we stopped at a semicolon, consume it to avoid an extra warning.
152 if (Tok.is(tok::semi))
153 ConsumeToken();
154 delete getCurrentClass().LateParsedDeclarations.back();
155 getCurrentClass().LateParsedDeclarations.pop_back();
156 return FnD;
Douglas Gregor7ad83902008-11-05 04:29:56 +0000157 } else {
Sebastian Redla891a322011-09-30 08:32:17 +0000158 // Consume everything up to (and including) the matching right brace.
159 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000160 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000161
Sebastian Redld3a413d2009-04-26 20:35:05 +0000162 // If we're in a function-try-block, we need to store all the catch blocks.
163 if (kind == tok::kw_try) {
164 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000165 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
166 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000167 }
168 }
169
Douglas Gregor87f10642011-04-14 23:19:27 +0000170
171 if (!FnD) {
172 // If semantic analysis could not build a function declaration,
173 // just throw away the late-parsed declaration.
174 delete getCurrentClass().LateParsedDeclarations.back();
175 getCurrentClass().LateParsedDeclarations.pop_back();
176 }
177
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000178 return FnD;
179}
180
Richard Smith7a614d82011-06-11 17:19:42 +0000181/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
182/// specified Declarator is a well formed C++ non-static data member
183/// declaration. Now lex its initializer and store its tokens for parsing
184/// after the class is complete.
185void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
186 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
187 "Current token not a '{' or '='!");
188
189 LateParsedMemberInitializer *MI =
190 new LateParsedMemberInitializer(this, VarD);
191 getCurrentClass().LateParsedDeclarations.push_back(MI);
192 CachedTokens &Toks = MI->Toks;
193
194 tok::TokenKind kind = Tok.getKind();
195 if (kind == tok::equal) {
196 Toks.push_back(Tok);
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000197 ConsumeToken();
Richard Smith7a614d82011-06-11 17:19:42 +0000198 }
199
200 if (kind == tok::l_brace) {
201 // Begin by storing the '{' token.
202 Toks.push_back(Tok);
203 ConsumeBrace();
204
205 // Consume everything up to (and including) the matching right brace.
206 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
207 } else {
208 // Consume everything up to (but excluding) the comma or semicolon.
209 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
210 /*ConsumeFinalToken=*/false);
211 }
212
213 // Store an artificial EOF token to ensure that we don't run off the end of
214 // the initializer when we come to parse it.
215 Token Eof;
216 Eof.startToken();
217 Eof.setKind(tok::eof);
218 Eof.setLocation(Tok.getLocation());
219 Toks.push_back(Eof);
220}
221
Douglas Gregord54eb442010-10-12 16:25:54 +0000222Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
223void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000224void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000225void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
226
227Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
228 : Self(P), Class(C) {}
229
230Parser::LateParsedClass::~LateParsedClass() {
231 Self->DeallocateParsedClasses(Class);
232}
233
234void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
235 Self->ParseLexedMethodDeclarations(*Class);
236}
237
Richard Smith7a614d82011-06-11 17:19:42 +0000238void Parser::LateParsedClass::ParseLexedMemberInitializers() {
239 Self->ParseLexedMemberInitializers(*Class);
240}
241
Douglas Gregord54eb442010-10-12 16:25:54 +0000242void Parser::LateParsedClass::ParseLexedMethodDefs() {
243 Self->ParseLexedMethodDefs(*Class);
244}
245
246void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
247 Self->ParseLexedMethodDeclaration(*this);
248}
249
250void Parser::LexedMethod::ParseLexedMethodDefs() {
251 Self->ParseLexedMethodDef(*this);
252}
253
Richard Smith7a614d82011-06-11 17:19:42 +0000254void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
255 Self->ParseLexedMemberInitializer(*this);
256}
257
Douglas Gregor72b505b2008-12-16 21:30:33 +0000258/// ParseLexedMethodDeclarations - We finished parsing the member
259/// specification of a top (non-nested) C++ class. Now go over the
260/// stack of method declarations with some parts for which parsing was
261/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000262void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
263 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000264 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000265 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000266 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000267
John McCall7a1dc562009-12-19 10:49:29 +0000268 // The current scope is still active if we're the top-level class.
269 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000270 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000271 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
272 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000273 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000274 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000275
Douglas Gregord54eb442010-10-12 16:25:54 +0000276 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
277 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000278 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000279
John McCall7a1dc562009-12-19 10:49:29 +0000280 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000281 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000282}
283
Douglas Gregord54eb442010-10-12 16:25:54 +0000284void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
285 // If this is a member template, introduce the template parameter scope.
286 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
287 if (LM.TemplateScope)
288 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
289
290 // Start the delayed C++ method declaration
291 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
292
293 // Introduce the parameters into scope and parse their default
294 // arguments.
295 ParseScope PrototypeScope(this,
296 Scope::FunctionPrototypeScope|Scope::DeclScope);
297 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
298 // Introduce the parameter into scope.
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000299 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
300 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000301
302 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
303 // Save the current token position.
304 SourceLocation origLoc = Tok.getLocation();
305
306 // Parse the default argument from its saved token stream.
307 Toks->push_back(Tok); // So that the current token doesn't get lost
308 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
309
310 // Consume the previously-pushed token.
311 ConsumeAnyToken();
312
313 // Consume the '='.
314 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
315 SourceLocation EqualLoc = ConsumeToken();
316
317 // The argument isn't actually potentially evaluated unless it is
318 // used.
319 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000320 Sema::PotentiallyEvaluatedIfUsed,
321 LM.DefaultArgs[I].Param);
Douglas Gregord54eb442010-10-12 16:25:54 +0000322
Sebastian Redl84407ba2012-03-14 15:54:00 +0000323 ExprResult DefArgResult;
Sebastian Redlca893712012-03-20 21:24:03 +0000324 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
325 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +0000326 DefArgResult = ParseBraceInitializer();
Sebastian Redlca893712012-03-20 21:24:03 +0000327 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +0000328 DefArgResult = ParseAssignmentExpression();
Douglas Gregord54eb442010-10-12 16:25:54 +0000329 if (DefArgResult.isInvalid())
330 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
331 else {
332 if (Tok.is(tok::cxx_defaultarg_end))
333 ConsumeToken();
334 else
335 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
336 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
337 DefArgResult.take());
338 }
339
340 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
341 Tok.getLocation()) &&
342 "ParseAssignmentExpression went over the default arg tokens!");
343 // There could be leftover tokens (e.g. because of an error).
344 // Skip through until we reach the original token position.
345 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
346 ConsumeAnyToken();
347
348 delete Toks;
349 LM.DefaultArgs[I].Toks = 0;
350 }
351 }
Douglas Gregor74e2fc32012-04-16 18:27:27 +0000352
Douglas Gregord54eb442010-10-12 16:25:54 +0000353 PrototypeScope.Exit();
354
355 // Finish the delayed C++ method declaration.
356 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
357}
358
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000359/// ParseLexedMethodDefs - We finished parsing the member specification of a top
360/// (non-nested) C++ class. Now go over the stack of lexed methods that were
361/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000362void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
363 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000364 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000365 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000366 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000367
368 bool HasClassScope = !Class.TopLevelClass;
369 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
370 HasClassScope);
371
Douglas Gregord54eb442010-10-12 16:25:54 +0000372 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
373 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
374 }
375}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000376
Douglas Gregord54eb442010-10-12 16:25:54 +0000377void Parser::ParseLexedMethodDef(LexedMethod &LM) {
378 // If this is a member template, introduce the template parameter scope.
379 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
380 if (LM.TemplateScope)
381 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Douglas Gregord54eb442010-10-12 16:25:54 +0000383 // Save the current token position.
384 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000385
Douglas Gregord54eb442010-10-12 16:25:54 +0000386 assert(!LM.Toks.empty() && "Empty body!");
387 // Append the current token at the end of the new token stream so that it
388 // doesn't get lost.
389 LM.Toks.push_back(Tok);
390 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000391
Douglas Gregord54eb442010-10-12 16:25:54 +0000392 // Consume the previously pushed token.
393 ConsumeAnyToken();
394 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
395 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000396
Douglas Gregord54eb442010-10-12 16:25:54 +0000397 // Parse the method body. Function body parsing code is similar enough
398 // to be re-used for method bodies as well.
399 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
400 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000401
Douglas Gregord54eb442010-10-12 16:25:54 +0000402 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000403 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000404 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
405 Tok.getLocation()) &&
406 "ParseFunctionTryBlock went over the cached tokens!");
407 // There could be leftover tokens (e.g. because of an error).
408 // Skip through until we reach the original token position.
409 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
410 ConsumeAnyToken();
411 return;
412 }
413 if (Tok.is(tok::colon)) {
414 ParseConstructorInitializer(LM.D);
415
416 // Error recovery.
417 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000418 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000419 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000420 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
421 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000422 return;
423 }
424 } else
425 Actions.ActOnDefaultCtorInitializers(LM.D);
426
Douglas Gregorc9977d02011-03-16 17:05:57 +0000427 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000428
429 if (Tok.getLocation() != origLoc) {
430 // Due to parsing error, we either went over the cached tokens or
431 // there are still cached tokens left. If it's the latter case skip the
432 // leftover tokens.
433 // Since this is an uncommon situation that should be avoided, use the
434 // expensive isBeforeInTranslationUnit call.
435 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
436 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000437 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000438 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000439 }
440}
441
Richard Smith7a614d82011-06-11 17:19:42 +0000442/// ParseLexedMemberInitializers - We finished parsing the member specification
443/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
444/// initializers that were collected during its parsing and parse them all.
445void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
446 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
447 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
448 HasTemplateScope);
449 if (HasTemplateScope)
450 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
451
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000452 // Set or update the scope flags.
Richard Smith7a614d82011-06-11 17:19:42 +0000453 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000454 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Richard Smith7a614d82011-06-11 17:19:42 +0000455 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
456 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
457
458 if (!AlreadyHasClassScope)
459 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
460 Class.TagOrTemplate);
461
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000462 {
463 // C++11 [expr.prim.general]p4:
464 // Otherwise, if a member-declarator declares a non-static data member
465 // (9.2) of a class X, the expression this is a prvalue of type "pointer
466 // to X" within the optional brace-or-equal-initializer. It shall not
467 // appear elsewhere in the member-declarator.
468 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
469 /*TypeQuals=*/(unsigned)0);
Richard Smith7a614d82011-06-11 17:19:42 +0000470
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000471 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
472 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
473 }
474 }
475
Richard Smith7a614d82011-06-11 17:19:42 +0000476 if (!AlreadyHasClassScope)
477 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
478 Class.TagOrTemplate);
479
480 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
481}
482
483void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000484 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000485 return;
486
487 // Append the current token at the end of the new token stream so that it
488 // doesn't get lost.
489 MI.Toks.push_back(Tok);
490 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
491
492 // Consume the previously pushed token.
493 ConsumeAnyToken();
494
495 SourceLocation EqualLoc;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000496
Douglas Gregor552e2992012-02-21 02:22:07 +0000497 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
498 EqualLoc);
Richard Smith7a614d82011-06-11 17:19:42 +0000499
500 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
501
502 // The next token should be our artificial terminating EOF token.
503 if (Tok.isNot(tok::eof)) {
504 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
505 if (!EndLoc.isValid())
506 EndLoc = Tok.getLocation();
507 // No fixit; we can't recover as if there were a semicolon here.
508 Diag(EndLoc, diag::err_expected_semi_decl_list);
509
510 // Consume tokens until we hit the artificial EOF.
511 while (Tok.isNot(tok::eof))
512 ConsumeAnyToken();
513 }
514 ConsumeAnyToken();
515}
516
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000517/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000518/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000519/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000520/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000521/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000522/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000523bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
524 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000525 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000526 // We always want this function to consume at least one token if the first
527 // token isn't T and if not at EOF.
528 bool isFirstTokenConsumed = true;
529 while (1) {
530 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000531 if (Tok.is(T1) || Tok.is(T2)) {
532 if (ConsumeFinalToken) {
533 Toks.push_back(Tok);
534 ConsumeAnyToken();
535 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000536 return true;
537 }
538
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000539 switch (Tok.getKind()) {
540 case tok::eof:
541 // Ran out of tokens.
542 return false;
543
544 case tok::l_paren:
545 // Recursively consume properly-nested parens.
546 Toks.push_back(Tok);
547 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000548 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000549 break;
550 case tok::l_square:
551 // Recursively consume properly-nested square brackets.
552 Toks.push_back(Tok);
553 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000554 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000555 break;
556 case tok::l_brace:
557 // Recursively consume properly-nested braces.
558 Toks.push_back(Tok);
559 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000560 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000561 break;
562
563 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
564 // Since the user wasn't looking for this token (if they were, it would
565 // already be handled), this isn't balanced. If there is a LHS token at a
566 // higher level, we will assume that this matches the unbalanced token
567 // and return it. Otherwise, this is a spurious RHS token, which we skip.
568 case tok::r_paren:
569 if (ParenCount && !isFirstTokenConsumed)
570 return false; // Matches something.
571 Toks.push_back(Tok);
572 ConsumeParen();
573 break;
574 case tok::r_square:
575 if (BracketCount && !isFirstTokenConsumed)
576 return false; // Matches something.
577 Toks.push_back(Tok);
578 ConsumeBracket();
579 break;
580 case tok::r_brace:
581 if (BraceCount && !isFirstTokenConsumed)
582 return false; // Matches something.
583 Toks.push_back(Tok);
584 ConsumeBrace();
585 break;
586
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000587 case tok::code_completion:
588 Toks.push_back(Tok);
589 ConsumeCodeCompletionToken();
590 break;
591
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000592 case tok::string_literal:
593 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000594 case tok::utf8_string_literal:
595 case tok::utf16_string_literal:
596 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000597 Toks.push_back(Tok);
598 ConsumeStringToken();
599 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000600 case tok::semi:
601 if (StopAtSemi)
602 return false;
603 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000604 default:
605 // consume this token.
606 Toks.push_back(Tok);
607 ConsumeToken();
608 break;
609 }
610 isFirstTokenConsumed = false;
611 }
612}
Sebastian Redl6df65482011-09-24 17:48:25 +0000613
614/// \brief Consume tokens and store them in the passed token container until
615/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redla891a322011-09-30 08:32:17 +0000616/// the opening brace of the function body. The opening brace will be consumed
617/// if and only if there was no error.
Sebastian Redl6df65482011-09-24 17:48:25 +0000618///
Sebastian Redla891a322011-09-30 08:32:17 +0000619/// \return True on error.
620bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000621 if (Tok.is(tok::kw_try)) {
622 Toks.push_back(Tok);
623 ConsumeToken();
624 }
Eli Friedmane9ee3822012-02-22 04:49:04 +0000625 bool ReadInitializer = false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000626 if (Tok.is(tok::colon)) {
627 // Initializers can contain braces too.
628 Toks.push_back(Tok);
629 ConsumeToken();
630
631 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
632 if (Tok.is(tok::eof) || Tok.is(tok::semi))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000633 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
Sebastian Redl6df65482011-09-24 17:48:25 +0000634
635 // Grab the identifier.
636 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
637 /*StopAtSemi=*/true,
638 /*ConsumeFinalToken=*/false))
Eli Friedmane9ee3822012-02-22 04:49:04 +0000639 return Diag(Tok.getLocation(), diag::err_expected_lparen);
Sebastian Redl6df65482011-09-24 17:48:25 +0000640
641 tok::TokenKind kind = Tok.getKind();
642 Toks.push_back(Tok);
Eli Friedmane9ee3822012-02-22 04:49:04 +0000643 bool IsLParen = (kind == tok::l_paren);
644 SourceLocation LOpen = Tok.getLocation();
645
646 if (IsLParen) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000647 ConsumeParen();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000648 } else {
Sebastian Redl6df65482011-09-24 17:48:25 +0000649 assert(kind == tok::l_brace && "Must be left paren or brace here.");
650 ConsumeBrace();
Sebastian Redla891a322011-09-30 08:32:17 +0000651 // In C++03, this has to be the start of the function body, which
Eli Friedmane9ee3822012-02-22 04:49:04 +0000652 // means the initializer is malformed; we'll diagnose it later.
David Blaikie4e4d0842012-03-11 07:00:24 +0000653 if (!getLangOpts().CPlusPlus0x)
Sebastian Redla891a322011-09-30 08:32:17 +0000654 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000655 }
656
657 // Grab the initializer
Eli Friedmane9ee3822012-02-22 04:49:04 +0000658 if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
659 Toks, /*StopAtSemi=*/true)) {
660 Diag(Tok, IsLParen ? diag::err_expected_rparen :
661 diag::err_expected_rbrace);
662 Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
Sebastian Redl6df65482011-09-24 17:48:25 +0000663 return true;
Eli Friedmane9ee3822012-02-22 04:49:04 +0000664 }
665
666 // Grab pack ellipsis, if present
667 if (Tok.is(tok::ellipsis)) {
668 Toks.push_back(Tok);
669 ConsumeToken();
670 }
Sebastian Redla891a322011-09-30 08:32:17 +0000671
672 // Grab the separating comma, if any.
673 if (Tok.is(tok::comma)) {
674 Toks.push_back(Tok);
675 ConsumeToken();
Eli Friedmane9ee3822012-02-22 04:49:04 +0000676 } else if (Tok.isNot(tok::l_brace)) {
677 ReadInitializer = true;
678 break;
Sebastian Redla891a322011-09-30 08:32:17 +0000679 }
Sebastian Redl6df65482011-09-24 17:48:25 +0000680 }
681 }
682
Sebastian Redla891a322011-09-30 08:32:17 +0000683 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
684 // brace: an opening one is the function body, while a closing one probably
685 // means we've reached the end of the class.
Eli Friedmane9ee3822012-02-22 04:49:04 +0000686 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
687 /*StopAtSemi=*/true,
688 /*ConsumeFinalToken=*/false);
689 if (Tok.isNot(tok::l_brace)) {
690 if (ReadInitializer)
691 return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
692 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
693 }
Sebastian Redla891a322011-09-30 08:32:17 +0000694
695 Toks.push_back(Tok);
696 ConsumeBrace();
697 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000698}