blob: 65f7f56fe93d0566cc7f2d46177fe8ac1ccaaee5 [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
69 bool Delete = false;
70 SourceLocation KWLoc;
71 if (Tok.is(tok::kw_delete)) {
Richard Smith5d164bc2011-10-15 05:09:34 +000072 Diag(Tok, getLang().CPlusPlus0x ?
73 diag::warn_cxx98_compat_deleted_function :
74 diag::warn_deleted_function_accepted_as_extension);
Alexis Hunt5a7fa252011-05-12 06:15:49 +000075
76 KWLoc = ConsumeToken();
77 Actions.SetDeclDeleted(FnD, KWLoc);
78 Delete = true;
79 } else if (Tok.is(tok::kw_default)) {
Richard Smith5d164bc2011-10-15 05:09:34 +000080 Diag(Tok, getLang().CPlusPlus0x ?
81 diag::warn_cxx98_compat_defaulted_function :
82 diag::warn_defaulted_function_accepted_as_extension);
Alexis Hunt5a7fa252011-05-12 06:15:49 +000083
84 KWLoc = ConsumeToken();
85 Actions.SetDeclDefaulted(FnD, KWLoc);
86 } else {
87 llvm_unreachable("function definition after = not 'delete' or 'default'");
88 }
89
90 if (Tok.is(tok::comma)) {
91 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
92 << Delete;
93 SkipUntil(tok::semi);
94 } else {
95 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
96 Delete ? "delete" : "default", tok::semi);
97 }
98
99 return FnD;
100 }
101
Francois Pichet1c229c02011-04-22 22:18:13 +0000102 // In delayed template parsing mode, if we are within a class template
103 // or if we are about to parse function member template then consume
104 // the tokens and store them for parsing at the end of the translation unit.
105 if (getLang().DelayedTemplateParsing &&
106 ((Actions.CurContext->isDependentContext() ||
107 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
108 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
109 !D.getDeclSpec().isFriendSpecified()) {
110
111 if (FnD) {
112 LateParsedTemplatedFunction *LPT =
113 new LateParsedTemplatedFunction(this, FnD);
114
115 FunctionDecl *FD = 0;
116 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
117 FD = FunTmpl->getTemplatedDecl();
118 else
119 FD = cast<FunctionDecl>(FnD);
Chandler Carruthbc0f9ae2011-04-25 07:09:43 +0000120 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet1c229c02011-04-22 22:18:13 +0000121
122 LateParsedTemplateMap[FD] = LPT;
123 Actions.MarkAsLateParsedTemplate(FD);
124 LexTemplateFunctionForLateParsing(LPT->Toks);
125 } else {
126 CachedTokens Toks;
127 LexTemplateFunctionForLateParsing(Toks);
128 }
129
130 return FnD;
131 }
132
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000133 // Consume the tokens and store them for later parsing.
134
Douglas Gregorefc46952010-10-12 16:25:54 +0000135 LexedMethod* LM = new LexedMethod(this, FnD);
136 getCurrentClass().LateParsedDeclarations.push_back(LM);
137 LM->TemplateScope = getCurScope()->isTemplateParamScope();
138 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000139
Sebastian Redla7b98a72009-04-26 20:35:05 +0000140 tok::TokenKind kind = Tok.getKind();
Sebastian Redl0d164012011-09-30 08:32:17 +0000141 // Consume everything up to (and including) the left brace of the
142 // function body.
143 if (ConsumeAndStoreFunctionPrologue(Toks)) {
144 // We didn't find the left-brace we expected after the
145 // constructor initializer.
146 if (Tok.is(tok::semi)) {
147 // We found a semicolon; complain, consume the semicolon, and
148 // don't try to parse this method later.
149 Diag(Tok.getLocation(), diag::err_expected_lbrace);
150 ConsumeAnyToken();
151 delete getCurrentClass().LateParsedDeclarations.back();
152 getCurrentClass().LateParsedDeclarations.pop_back();
153 return FnD;
Douglas Gregor49de5342008-11-10 16:59:40 +0000154 }
Douglas Gregore8381c02008-11-05 04:29:56 +0000155 } else {
Sebastian Redl0d164012011-09-30 08:32:17 +0000156 // Consume everything up to (and including) the matching right brace.
157 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregore8381c02008-11-05 04:29:56 +0000158 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000159
Sebastian Redla7b98a72009-04-26 20:35:05 +0000160 // If we're in a function-try-block, we need to store all the catch blocks.
161 if (kind == tok::kw_try) {
162 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000163 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
164 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000165 }
166 }
167
Douglas Gregor6ca64102011-04-14 23:19:27 +0000168
169 if (!FnD) {
170 // If semantic analysis could not build a function declaration,
171 // just throw away the late-parsed declaration.
172 delete getCurrentClass().LateParsedDeclarations.back();
173 getCurrentClass().LateParsedDeclarations.pop_back();
174 }
175
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000176 return FnD;
177}
178
Richard Smith938f40b2011-06-11 17:19:42 +0000179/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
180/// specified Declarator is a well formed C++ non-static data member
181/// declaration. Now lex its initializer and store its tokens for parsing
182/// after the class is complete.
183void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
184 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
185 "Current token not a '{' or '='!");
186
187 LateParsedMemberInitializer *MI =
188 new LateParsedMemberInitializer(this, VarD);
189 getCurrentClass().LateParsedDeclarations.push_back(MI);
190 CachedTokens &Toks = MI->Toks;
191
192 tok::TokenKind kind = Tok.getKind();
193 if (kind == tok::equal) {
194 Toks.push_back(Tok);
195 ConsumeAnyToken();
196 }
197
198 if (kind == tok::l_brace) {
199 // Begin by storing the '{' token.
200 Toks.push_back(Tok);
201 ConsumeBrace();
202
203 // Consume everything up to (and including) the matching right brace.
204 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
205 } else {
206 // Consume everything up to (but excluding) the comma or semicolon.
207 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
208 /*ConsumeFinalToken=*/false);
209 }
210
211 // Store an artificial EOF token to ensure that we don't run off the end of
212 // the initializer when we come to parse it.
213 Token Eof;
214 Eof.startToken();
215 Eof.setKind(tok::eof);
216 Eof.setLocation(Tok.getLocation());
217 Toks.push_back(Eof);
218}
219
Douglas Gregorefc46952010-10-12 16:25:54 +0000220Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
221void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith938f40b2011-06-11 17:19:42 +0000222void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregorefc46952010-10-12 16:25:54 +0000223void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
224
225Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
226 : Self(P), Class(C) {}
227
228Parser::LateParsedClass::~LateParsedClass() {
229 Self->DeallocateParsedClasses(Class);
230}
231
232void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
233 Self->ParseLexedMethodDeclarations(*Class);
234}
235
Richard Smith938f40b2011-06-11 17:19:42 +0000236void Parser::LateParsedClass::ParseLexedMemberInitializers() {
237 Self->ParseLexedMemberInitializers(*Class);
238}
239
Douglas Gregorefc46952010-10-12 16:25:54 +0000240void Parser::LateParsedClass::ParseLexedMethodDefs() {
241 Self->ParseLexedMethodDefs(*Class);
242}
243
244void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
245 Self->ParseLexedMethodDeclaration(*this);
246}
247
248void Parser::LexedMethod::ParseLexedMethodDefs() {
249 Self->ParseLexedMethodDef(*this);
250}
251
Richard Smith938f40b2011-06-11 17:19:42 +0000252void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
253 Self->ParseLexedMemberInitializer(*this);
254}
255
Douglas Gregor4d87df52008-12-16 21:30:33 +0000256/// ParseLexedMethodDeclarations - We finished parsing the member
257/// specification of a top (non-nested) C++ class. Now go over the
258/// stack of method declarations with some parts for which parsing was
259/// delayed (such as default arguments) and parse them.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000260void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
261 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000262 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000263 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000264 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000265
John McCall6df5fef2009-12-19 10:49:29 +0000266 // The current scope is still active if we're the top-level class.
267 // Otherwise we'll need to push and enter a new scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000268 bool HasClassScope = !Class.TopLevelClass;
Alexis Hunt1deb9722010-04-14 23:07:37 +0000269 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
270 HasClassScope);
John McCall6df5fef2009-12-19 10:49:29 +0000271 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000272 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000273
Douglas Gregorefc46952010-10-12 16:25:54 +0000274 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
275 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor4d87df52008-12-16 21:30:33 +0000276 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000277
John McCall6df5fef2009-12-19 10:49:29 +0000278 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000279 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor4d87df52008-12-16 21:30:33 +0000280}
281
Douglas Gregorefc46952010-10-12 16:25:54 +0000282void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
283 // If this is a member template, introduce the template parameter scope.
284 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
285 if (LM.TemplateScope)
286 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
287
288 // Start the delayed C++ method declaration
289 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
290
291 // Introduce the parameters into scope and parse their default
292 // arguments.
293 ParseScope PrototypeScope(this,
294 Scope::FunctionPrototypeScope|Scope::DeclScope);
295 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
296 // Introduce the parameter into scope.
297 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
298
299 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
300 // Save the current token position.
301 SourceLocation origLoc = Tok.getLocation();
302
303 // Parse the default argument from its saved token stream.
304 Toks->push_back(Tok); // So that the current token doesn't get lost
305 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
306
307 // Consume the previously-pushed token.
308 ConsumeAnyToken();
309
310 // Consume the '='.
311 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
312 SourceLocation EqualLoc = ConsumeToken();
313
314 // The argument isn't actually potentially evaluated unless it is
315 // used.
316 EnterExpressionEvaluationContext Eval(Actions,
317 Sema::PotentiallyEvaluatedIfUsed);
318
319 ExprResult DefArgResult(ParseAssignmentExpression());
320 if (DefArgResult.isInvalid())
321 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
322 else {
323 if (Tok.is(tok::cxx_defaultarg_end))
324 ConsumeToken();
325 else
326 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
327 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
328 DefArgResult.take());
329 }
330
331 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
332 Tok.getLocation()) &&
333 "ParseAssignmentExpression went over the default arg tokens!");
334 // There could be leftover tokens (e.g. because of an error).
335 // Skip through until we reach the original token position.
336 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
337 ConsumeAnyToken();
338
339 delete Toks;
340 LM.DefaultArgs[I].Toks = 0;
341 }
342 }
343 PrototypeScope.Exit();
344
345 // Finish the delayed C++ method declaration.
346 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
347}
348
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000349/// ParseLexedMethodDefs - We finished parsing the member specification of a top
350/// (non-nested) C++ class. Now go over the stack of lexed methods that were
351/// collected during its parsing and parse them all.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000352void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
353 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000354 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000355 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000356 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000357
358 bool HasClassScope = !Class.TopLevelClass;
359 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
360 HasClassScope);
361
Douglas Gregorefc46952010-10-12 16:25:54 +0000362 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
363 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
364 }
365}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000366
Douglas Gregorefc46952010-10-12 16:25:54 +0000367void Parser::ParseLexedMethodDef(LexedMethod &LM) {
368 // If this is a member template, introduce the template parameter scope.
369 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
370 if (LM.TemplateScope)
371 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump11289f42009-09-09 15:08:12 +0000372
Douglas Gregorefc46952010-10-12 16:25:54 +0000373 // Save the current token position.
374 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidis02041972010-03-31 00:38:09 +0000375
Douglas Gregorefc46952010-10-12 16:25:54 +0000376 assert(!LM.Toks.empty() && "Empty body!");
377 // Append the current token at the end of the new token stream so that it
378 // doesn't get lost.
379 LM.Toks.push_back(Tok);
380 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000381
Douglas Gregorefc46952010-10-12 16:25:54 +0000382 // Consume the previously pushed token.
383 ConsumeAnyToken();
384 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
385 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000386
Douglas Gregorefc46952010-10-12 16:25:54 +0000387 // Parse the method body. Function body parsing code is similar enough
388 // to be re-used for method bodies as well.
389 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
390 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000391
Douglas Gregorefc46952010-10-12 16:25:54 +0000392 if (Tok.is(tok::kw_try)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000393 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000394 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
395 Tok.getLocation()) &&
396 "ParseFunctionTryBlock went over the cached tokens!");
397 // There could be leftover tokens (e.g. because of an error).
398 // Skip through until we reach the original token position.
399 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
400 ConsumeAnyToken();
401 return;
402 }
403 if (Tok.is(tok::colon)) {
404 ParseConstructorInitializer(LM.D);
405
406 // Error recovery.
407 if (!Tok.is(tok::l_brace)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000408 FnScope.Exit();
Douglas Gregorefc46952010-10-12 16:25:54 +0000409 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gayd0457922011-09-23 22:39:23 +0000410 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
411 ConsumeAnyToken();
Douglas Gregorefc46952010-10-12 16:25:54 +0000412 return;
413 }
414 } else
415 Actions.ActOnDefaultCtorInitializers(LM.D);
416
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000417 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000418
419 if (Tok.getLocation() != origLoc) {
420 // Due to parsing error, we either went over the cached tokens or
421 // there are still cached tokens left. If it's the latter case skip the
422 // leftover tokens.
423 // Since this is an uncommon situation that should be avoided, use the
424 // expensive isBeforeInTranslationUnit call.
425 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
426 origLoc))
Argyrios Kyrtzidise1224c82010-06-19 19:58:34 +0000427 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidise9b76af2010-06-17 10:52:22 +0000428 ConsumeAnyToken();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000429 }
430}
431
Richard Smith938f40b2011-06-11 17:19:42 +0000432/// ParseLexedMemberInitializers - We finished parsing the member specification
433/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
434/// initializers that were collected during its parsing and parse them all.
435void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
436 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
437 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
438 HasTemplateScope);
439 if (HasTemplateScope)
440 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
441
442 // Set or update the scope flags to include Scope::ThisScope.
443 bool AlreadyHasClassScope = Class.TopLevelClass;
444 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
445 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
446 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
447
448 if (!AlreadyHasClassScope)
449 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
450 Class.TagOrTemplate);
451
452 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
453 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
454 }
455
456 if (!AlreadyHasClassScope)
457 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
458 Class.TagOrTemplate);
459
460 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
461}
462
463void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1a526fd2011-09-29 19:42:27 +0000464 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith938f40b2011-06-11 17:19:42 +0000465 return;
466
467 // Append the current token at the end of the new token stream so that it
468 // doesn't get lost.
469 MI.Toks.push_back(Tok);
470 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
471
472 // Consume the previously pushed token.
473 ConsumeAnyToken();
474
475 SourceLocation EqualLoc;
476 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
477
478 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
479
480 // The next token should be our artificial terminating EOF token.
481 if (Tok.isNot(tok::eof)) {
482 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
483 if (!EndLoc.isValid())
484 EndLoc = Tok.getLocation();
485 // No fixit; we can't recover as if there were a semicolon here.
486 Diag(EndLoc, diag::err_expected_semi_decl_list);
487
488 // Consume tokens until we hit the artificial EOF.
489 while (Tok.isNot(tok::eof))
490 ConsumeAnyToken();
491 }
492 ConsumeAnyToken();
493}
494
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000495/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor4d87df52008-12-16 21:30:33 +0000496/// container until the token 'T' is reached (which gets
Mike Stump11289f42009-09-09 15:08:12 +0000497/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000498/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000499/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000500/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000501bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
502 CachedTokens &Toks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000503 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000504 // We always want this function to consume at least one token if the first
505 // token isn't T and if not at EOF.
506 bool isFirstTokenConsumed = true;
507 while (1) {
508 // If we found one of the tokens, stop and return true.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000509 if (Tok.is(T1) || Tok.is(T2)) {
510 if (ConsumeFinalToken) {
511 Toks.push_back(Tok);
512 ConsumeAnyToken();
513 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000514 return true;
515 }
516
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000517 switch (Tok.getKind()) {
518 case tok::eof:
519 // Ran out of tokens.
520 return false;
521
522 case tok::l_paren:
523 // Recursively consume properly-nested parens.
524 Toks.push_back(Tok);
525 ConsumeParen();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000526 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000527 break;
528 case tok::l_square:
529 // Recursively consume properly-nested square brackets.
530 Toks.push_back(Tok);
531 ConsumeBracket();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000532 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000533 break;
534 case tok::l_brace:
535 // Recursively consume properly-nested braces.
536 Toks.push_back(Tok);
537 ConsumeBrace();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000538 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000539 break;
540
541 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
542 // Since the user wasn't looking for this token (if they were, it would
543 // already be handled), this isn't balanced. If there is a LHS token at a
544 // higher level, we will assume that this matches the unbalanced token
545 // and return it. Otherwise, this is a spurious RHS token, which we skip.
546 case tok::r_paren:
547 if (ParenCount && !isFirstTokenConsumed)
548 return false; // Matches something.
549 Toks.push_back(Tok);
550 ConsumeParen();
551 break;
552 case tok::r_square:
553 if (BracketCount && !isFirstTokenConsumed)
554 return false; // Matches something.
555 Toks.push_back(Tok);
556 ConsumeBracket();
557 break;
558 case tok::r_brace:
559 if (BraceCount && !isFirstTokenConsumed)
560 return false; // Matches something.
561 Toks.push_back(Tok);
562 ConsumeBrace();
563 break;
564
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000565 case tok::code_completion:
566 Toks.push_back(Tok);
567 ConsumeCodeCompletionToken();
568 break;
569
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000570 case tok::string_literal:
571 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000572 case tok::utf8_string_literal:
573 case tok::utf16_string_literal:
574 case tok::utf32_string_literal:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000575 Toks.push_back(Tok);
576 ConsumeStringToken();
577 break;
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000578 case tok::semi:
579 if (StopAtSemi)
580 return false;
581 // FALL THROUGH.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000582 default:
583 // consume this token.
584 Toks.push_back(Tok);
585 ConsumeToken();
586 break;
587 }
588 isFirstTokenConsumed = false;
589 }
590}
Sebastian Redla74948d2011-09-24 17:48:25 +0000591
592/// \brief Consume tokens and store them in the passed token container until
593/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redl0d164012011-09-30 08:32:17 +0000594/// the opening brace of the function body. The opening brace will be consumed
595/// if and only if there was no error.
Sebastian Redla74948d2011-09-24 17:48:25 +0000596///
Sebastian Redl0d164012011-09-30 08:32:17 +0000597/// \return True on error.
598bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redla74948d2011-09-24 17:48:25 +0000599 if (Tok.is(tok::kw_try)) {
600 Toks.push_back(Tok);
601 ConsumeToken();
602 }
603 if (Tok.is(tok::colon)) {
604 // Initializers can contain braces too.
605 Toks.push_back(Tok);
606 ConsumeToken();
607
608 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
609 if (Tok.is(tok::eof) || Tok.is(tok::semi))
610 return true;
611
612 // Grab the identifier.
613 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
614 /*StopAtSemi=*/true,
615 /*ConsumeFinalToken=*/false))
616 return true;
617
618 tok::TokenKind kind = Tok.getKind();
619 Toks.push_back(Tok);
620 if (kind == tok::l_paren)
621 ConsumeParen();
622 else {
623 assert(kind == tok::l_brace && "Must be left paren or brace here.");
624 ConsumeBrace();
Sebastian Redl0d164012011-09-30 08:32:17 +0000625 // In C++03, this has to be the start of the function body, which
626 // means the initializer is malformed.
627 if (!getLang().CPlusPlus0x)
628 return false;
Sebastian Redla74948d2011-09-24 17:48:25 +0000629 }
630
631 // Grab the initializer
632 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
633 tok::r_brace,
634 Toks, /*StopAtSemi=*/true))
635 return true;
Sebastian Redl0d164012011-09-30 08:32:17 +0000636
637 // Grab the separating comma, if any.
638 if (Tok.is(tok::comma)) {
639 Toks.push_back(Tok);
640 ConsumeToken();
641 }
Sebastian Redla74948d2011-09-24 17:48:25 +0000642 }
643 }
644
Sebastian Redl0d164012011-09-30 08:32:17 +0000645 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
646 // brace: an opening one is the function body, while a closing one probably
647 // means we've reached the end of the class.
648 if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
649 /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false))
650 return true;
651 if(Tok.isNot(tok::l_brace))
652 return true;
653
654 Toks.push_back(Tok);
655 ConsumeBrace();
656 return false;
Sebastian Redla74948d2011-09-24 17:48:25 +0000657}