blob: 8cec31ca0d742d52dcfd01af811873b9070931ec [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
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000148 // constructor initializer; we already printed an error, and it's likely
149 // impossible to recover, so don't try to parse this method later.
150 // If we stopped at a semicolon, consume it to avoid an extra warning.
151 if (Tok.is(tok::semi))
152 ConsumeToken();
153 delete getCurrentClass().LateParsedDeclarations.back();
154 getCurrentClass().LateParsedDeclarations.pop_back();
155 return FnD;
Douglas Gregore8381c02008-11-05 04:29:56 +0000156 } else {
Sebastian Redl0d164012011-09-30 08:32:17 +0000157 // Consume everything up to (and including) the matching right brace.
158 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregore8381c02008-11-05 04:29:56 +0000159 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000160
Sebastian Redla7b98a72009-04-26 20:35:05 +0000161 // If we're in a function-try-block, we need to store all the catch blocks.
162 if (kind == tok::kw_try) {
163 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000164 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
165 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000166 }
167 }
168
Douglas Gregor6ca64102011-04-14 23:19:27 +0000169
170 if (!FnD) {
171 // If semantic analysis could not build a function declaration,
172 // just throw away the late-parsed declaration.
173 delete getCurrentClass().LateParsedDeclarations.back();
174 getCurrentClass().LateParsedDeclarations.pop_back();
175 }
176
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000177 return FnD;
178}
179
Richard Smith938f40b2011-06-11 17:19:42 +0000180/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
181/// specified Declarator is a well formed C++ non-static data member
182/// declaration. Now lex its initializer and store its tokens for parsing
183/// after the class is complete.
184void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
185 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
186 "Current token not a '{' or '='!");
187
188 LateParsedMemberInitializer *MI =
189 new LateParsedMemberInitializer(this, VarD);
190 getCurrentClass().LateParsedDeclarations.push_back(MI);
191 CachedTokens &Toks = MI->Toks;
192
193 tok::TokenKind kind = Tok.getKind();
194 if (kind == tok::equal) {
195 Toks.push_back(Tok);
196 ConsumeAnyToken();
197 }
198
199 if (kind == tok::l_brace) {
200 // Begin by storing the '{' token.
201 Toks.push_back(Tok);
202 ConsumeBrace();
203
204 // Consume everything up to (and including) the matching right brace.
205 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
206 } else {
207 // Consume everything up to (but excluding) the comma or semicolon.
208 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
209 /*ConsumeFinalToken=*/false);
210 }
211
212 // Store an artificial EOF token to ensure that we don't run off the end of
213 // the initializer when we come to parse it.
214 Token Eof;
215 Eof.startToken();
216 Eof.setKind(tok::eof);
217 Eof.setLocation(Tok.getLocation());
218 Toks.push_back(Eof);
219}
220
Douglas Gregorefc46952010-10-12 16:25:54 +0000221Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
222void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith938f40b2011-06-11 17:19:42 +0000223void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregorefc46952010-10-12 16:25:54 +0000224void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
225
226Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
227 : Self(P), Class(C) {}
228
229Parser::LateParsedClass::~LateParsedClass() {
230 Self->DeallocateParsedClasses(Class);
231}
232
233void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
234 Self->ParseLexedMethodDeclarations(*Class);
235}
236
Richard Smith938f40b2011-06-11 17:19:42 +0000237void Parser::LateParsedClass::ParseLexedMemberInitializers() {
238 Self->ParseLexedMemberInitializers(*Class);
239}
240
Douglas Gregorefc46952010-10-12 16:25:54 +0000241void Parser::LateParsedClass::ParseLexedMethodDefs() {
242 Self->ParseLexedMethodDefs(*Class);
243}
244
245void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
246 Self->ParseLexedMethodDeclaration(*this);
247}
248
249void Parser::LexedMethod::ParseLexedMethodDefs() {
250 Self->ParseLexedMethodDef(*this);
251}
252
Richard Smith938f40b2011-06-11 17:19:42 +0000253void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
254 Self->ParseLexedMemberInitializer(*this);
255}
256
Douglas Gregor4d87df52008-12-16 21:30:33 +0000257/// ParseLexedMethodDeclarations - We finished parsing the member
258/// specification of a top (non-nested) C++ class. Now go over the
259/// stack of method declarations with some parts for which parsing was
260/// delayed (such as default arguments) and parse them.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000261void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
262 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000263 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000264 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000265 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000266
John McCall6df5fef2009-12-19 10:49:29 +0000267 // The current scope is still active if we're the top-level class.
268 // Otherwise we'll need to push and enter a new scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000269 bool HasClassScope = !Class.TopLevelClass;
Alexis Hunt1deb9722010-04-14 23:07:37 +0000270 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
271 HasClassScope);
John McCall6df5fef2009-12-19 10:49:29 +0000272 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000273 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000274
Douglas Gregorefc46952010-10-12 16:25:54 +0000275 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
276 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor4d87df52008-12-16 21:30:33 +0000277 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000278
John McCall6df5fef2009-12-19 10:49:29 +0000279 if (HasClassScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000280 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor4d87df52008-12-16 21:30:33 +0000281}
282
Douglas Gregorefc46952010-10-12 16:25:54 +0000283void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
284 // If this is a member template, introduce the template parameter scope.
285 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
286 if (LM.TemplateScope)
287 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
288
289 // Start the delayed C++ method declaration
290 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
291
292 // Introduce the parameters into scope and parse their default
293 // arguments.
294 ParseScope PrototypeScope(this,
295 Scope::FunctionPrototypeScope|Scope::DeclScope);
296 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
297 // Introduce the parameter into scope.
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000298 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
299 LM.DefaultArgs[I].Param);
Douglas Gregorefc46952010-10-12 16:25:54 +0000300
301 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
302 // Save the current token position.
303 SourceLocation origLoc = Tok.getLocation();
304
305 // Parse the default argument from its saved token stream.
306 Toks->push_back(Tok); // So that the current token doesn't get lost
307 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
308
309 // Consume the previously-pushed token.
310 ConsumeAnyToken();
311
312 // Consume the '='.
313 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
314 SourceLocation EqualLoc = ConsumeToken();
315
316 // The argument isn't actually potentially evaluated unless it is
317 // used.
318 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000319 Sema::PotentiallyEvaluatedIfUsed,
320 LM.DefaultArgs[I].Param);
Douglas Gregorefc46952010-10-12 16:25:54 +0000321
322 ExprResult DefArgResult(ParseAssignmentExpression());
323 if (DefArgResult.isInvalid())
324 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
325 else {
326 if (Tok.is(tok::cxx_defaultarg_end))
327 ConsumeToken();
328 else
329 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
330 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
331 DefArgResult.take());
332 }
333
334 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
335 Tok.getLocation()) &&
336 "ParseAssignmentExpression went over the default arg tokens!");
337 // There could be leftover tokens (e.g. because of an error).
338 // Skip through until we reach the original token position.
339 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
340 ConsumeAnyToken();
341
342 delete Toks;
343 LM.DefaultArgs[I].Toks = 0;
344 }
345 }
346 PrototypeScope.Exit();
347
348 // Finish the delayed C++ method declaration.
349 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
350}
351
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000352/// ParseLexedMethodDefs - We finished parsing the member specification of a top
353/// (non-nested) C++ class. Now go over the stack of lexed methods that were
354/// collected during its parsing and parse them all.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000355void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
356 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000357 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000358 if (HasTemplateScope)
Douglas Gregor0be31a22010-07-02 17:43:08 +0000359 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000360
361 bool HasClassScope = !Class.TopLevelClass;
362 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
363 HasClassScope);
364
Douglas Gregorefc46952010-10-12 16:25:54 +0000365 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
366 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
367 }
368}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000369
Douglas Gregorefc46952010-10-12 16:25:54 +0000370void Parser::ParseLexedMethodDef(LexedMethod &LM) {
371 // If this is a member template, introduce the template parameter scope.
372 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
373 if (LM.TemplateScope)
374 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump11289f42009-09-09 15:08:12 +0000375
Douglas Gregorefc46952010-10-12 16:25:54 +0000376 // Save the current token position.
377 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidis02041972010-03-31 00:38:09 +0000378
Douglas Gregorefc46952010-10-12 16:25:54 +0000379 assert(!LM.Toks.empty() && "Empty body!");
380 // Append the current token at the end of the new token stream so that it
381 // doesn't get lost.
382 LM.Toks.push_back(Tok);
383 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000384
Douglas Gregorefc46952010-10-12 16:25:54 +0000385 // Consume the previously pushed token.
386 ConsumeAnyToken();
387 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
388 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000389
Douglas Gregorefc46952010-10-12 16:25:54 +0000390 // Parse the method body. Function body parsing code is similar enough
391 // to be re-used for method bodies as well.
392 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
393 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000394
Douglas Gregorefc46952010-10-12 16:25:54 +0000395 if (Tok.is(tok::kw_try)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000396 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000397 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
398 Tok.getLocation()) &&
399 "ParseFunctionTryBlock went over the cached tokens!");
400 // There could be leftover tokens (e.g. because of an error).
401 // Skip through until we reach the original token position.
402 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
403 ConsumeAnyToken();
404 return;
405 }
406 if (Tok.is(tok::colon)) {
407 ParseConstructorInitializer(LM.D);
408
409 // Error recovery.
410 if (!Tok.is(tok::l_brace)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000411 FnScope.Exit();
Douglas Gregorefc46952010-10-12 16:25:54 +0000412 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gayd0457922011-09-23 22:39:23 +0000413 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
414 ConsumeAnyToken();
Douglas Gregorefc46952010-10-12 16:25:54 +0000415 return;
416 }
417 } else
418 Actions.ActOnDefaultCtorInitializers(LM.D);
419
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000420 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000421
422 if (Tok.getLocation() != origLoc) {
423 // Due to parsing error, we either went over the cached tokens or
424 // there are still cached tokens left. If it's the latter case skip the
425 // leftover tokens.
426 // Since this is an uncommon situation that should be avoided, use the
427 // expensive isBeforeInTranslationUnit call.
428 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
429 origLoc))
Argyrios Kyrtzidise1224c82010-06-19 19:58:34 +0000430 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidise9b76af2010-06-17 10:52:22 +0000431 ConsumeAnyToken();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000432 }
433}
434
Richard Smith938f40b2011-06-11 17:19:42 +0000435/// ParseLexedMemberInitializers - We finished parsing the member specification
436/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
437/// initializers that were collected during its parsing and parse them all.
438void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
439 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
440 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
441 HasTemplateScope);
442 if (HasTemplateScope)
443 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
444
445 // Set or update the scope flags to include Scope::ThisScope.
446 bool AlreadyHasClassScope = Class.TopLevelClass;
447 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
448 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
449 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
450
451 if (!AlreadyHasClassScope)
452 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
453 Class.TagOrTemplate);
454
455 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
456 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
457 }
458
459 if (!AlreadyHasClassScope)
460 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
461 Class.TagOrTemplate);
462
463 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
464}
465
466void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1a526fd2011-09-29 19:42:27 +0000467 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith938f40b2011-06-11 17:19:42 +0000468 return;
469
470 // Append the current token at the end of the new token stream so that it
471 // doesn't get lost.
472 MI.Toks.push_back(Tok);
473 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
474
475 // Consume the previously pushed token.
476 ConsumeAnyToken();
477
478 SourceLocation EqualLoc;
Douglas Gregor926410d2012-02-21 02:22:07 +0000479 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
480 EqualLoc);
Richard Smith938f40b2011-06-11 17:19:42 +0000481
482 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
483
484 // The next token should be our artificial terminating EOF token.
485 if (Tok.isNot(tok::eof)) {
486 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
487 if (!EndLoc.isValid())
488 EndLoc = Tok.getLocation();
489 // No fixit; we can't recover as if there were a semicolon here.
490 Diag(EndLoc, diag::err_expected_semi_decl_list);
491
492 // Consume tokens until we hit the artificial EOF.
493 while (Tok.isNot(tok::eof))
494 ConsumeAnyToken();
495 }
496 ConsumeAnyToken();
497}
498
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000499/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor4d87df52008-12-16 21:30:33 +0000500/// container until the token 'T' is reached (which gets
Mike Stump11289f42009-09-09 15:08:12 +0000501/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000502/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000503/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000504/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000505bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
506 CachedTokens &Toks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000507 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000508 // We always want this function to consume at least one token if the first
509 // token isn't T and if not at EOF.
510 bool isFirstTokenConsumed = true;
511 while (1) {
512 // If we found one of the tokens, stop and return true.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000513 if (Tok.is(T1) || Tok.is(T2)) {
514 if (ConsumeFinalToken) {
515 Toks.push_back(Tok);
516 ConsumeAnyToken();
517 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000518 return true;
519 }
520
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000521 switch (Tok.getKind()) {
522 case tok::eof:
523 // Ran out of tokens.
524 return false;
525
526 case tok::l_paren:
527 // Recursively consume properly-nested parens.
528 Toks.push_back(Tok);
529 ConsumeParen();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000530 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000531 break;
532 case tok::l_square:
533 // Recursively consume properly-nested square brackets.
534 Toks.push_back(Tok);
535 ConsumeBracket();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000536 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000537 break;
538 case tok::l_brace:
539 // Recursively consume properly-nested braces.
540 Toks.push_back(Tok);
541 ConsumeBrace();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000542 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000543 break;
544
545 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
546 // Since the user wasn't looking for this token (if they were, it would
547 // already be handled), this isn't balanced. If there is a LHS token at a
548 // higher level, we will assume that this matches the unbalanced token
549 // and return it. Otherwise, this is a spurious RHS token, which we skip.
550 case tok::r_paren:
551 if (ParenCount && !isFirstTokenConsumed)
552 return false; // Matches something.
553 Toks.push_back(Tok);
554 ConsumeParen();
555 break;
556 case tok::r_square:
557 if (BracketCount && !isFirstTokenConsumed)
558 return false; // Matches something.
559 Toks.push_back(Tok);
560 ConsumeBracket();
561 break;
562 case tok::r_brace:
563 if (BraceCount && !isFirstTokenConsumed)
564 return false; // Matches something.
565 Toks.push_back(Tok);
566 ConsumeBrace();
567 break;
568
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000569 case tok::code_completion:
570 Toks.push_back(Tok);
571 ConsumeCodeCompletionToken();
572 break;
573
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000574 case tok::string_literal:
575 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000576 case tok::utf8_string_literal:
577 case tok::utf16_string_literal:
578 case tok::utf32_string_literal:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000579 Toks.push_back(Tok);
580 ConsumeStringToken();
581 break;
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000582 case tok::semi:
583 if (StopAtSemi)
584 return false;
585 // FALL THROUGH.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000586 default:
587 // consume this token.
588 Toks.push_back(Tok);
589 ConsumeToken();
590 break;
591 }
592 isFirstTokenConsumed = false;
593 }
594}
Sebastian Redla74948d2011-09-24 17:48:25 +0000595
596/// \brief Consume tokens and store them in the passed token container until
597/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redl0d164012011-09-30 08:32:17 +0000598/// the opening brace of the function body. The opening brace will be consumed
599/// if and only if there was no error.
Sebastian Redla74948d2011-09-24 17:48:25 +0000600///
Sebastian Redl0d164012011-09-30 08:32:17 +0000601/// \return True on error.
602bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redla74948d2011-09-24 17:48:25 +0000603 if (Tok.is(tok::kw_try)) {
604 Toks.push_back(Tok);
605 ConsumeToken();
606 }
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000607 bool ReadInitializer = false;
Sebastian Redla74948d2011-09-24 17:48:25 +0000608 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))
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000615 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
Sebastian Redla74948d2011-09-24 17:48:25 +0000616
617 // Grab the identifier.
618 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
619 /*StopAtSemi=*/true,
620 /*ConsumeFinalToken=*/false))
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000621 return Diag(Tok.getLocation(), diag::err_expected_lparen);
Sebastian Redla74948d2011-09-24 17:48:25 +0000622
623 tok::TokenKind kind = Tok.getKind();
624 Toks.push_back(Tok);
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000625 bool IsLParen = (kind == tok::l_paren);
626 SourceLocation LOpen = Tok.getLocation();
627
628 if (IsLParen) {
Sebastian Redla74948d2011-09-24 17:48:25 +0000629 ConsumeParen();
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000630 } else {
Sebastian Redla74948d2011-09-24 17:48:25 +0000631 assert(kind == tok::l_brace && "Must be left paren or brace here.");
632 ConsumeBrace();
Sebastian Redl0d164012011-09-30 08:32:17 +0000633 // In C++03, this has to be the start of the function body, which
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000634 // means the initializer is malformed; we'll diagnose it later.
Sebastian Redl0d164012011-09-30 08:32:17 +0000635 if (!getLang().CPlusPlus0x)
636 return false;
Sebastian Redla74948d2011-09-24 17:48:25 +0000637 }
638
639 // Grab the initializer
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000640 if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
641 Toks, /*StopAtSemi=*/true)) {
642 Diag(Tok, IsLParen ? diag::err_expected_rparen :
643 diag::err_expected_rbrace);
644 Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
Sebastian Redla74948d2011-09-24 17:48:25 +0000645 return true;
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000646 }
647
648 // Grab pack ellipsis, if present
649 if (Tok.is(tok::ellipsis)) {
650 Toks.push_back(Tok);
651 ConsumeToken();
652 }
Sebastian Redl0d164012011-09-30 08:32:17 +0000653
654 // Grab the separating comma, if any.
655 if (Tok.is(tok::comma)) {
656 Toks.push_back(Tok);
657 ConsumeToken();
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000658 } else if (Tok.isNot(tok::l_brace)) {
659 ReadInitializer = true;
660 break;
Sebastian Redl0d164012011-09-30 08:32:17 +0000661 }
Sebastian Redla74948d2011-09-24 17:48:25 +0000662 }
663 }
664
Sebastian Redl0d164012011-09-30 08:32:17 +0000665 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
666 // brace: an opening one is the function body, while a closing one probably
667 // means we've reached the end of the class.
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000668 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
669 /*StopAtSemi=*/true,
670 /*ConsumeFinalToken=*/false);
671 if (Tok.isNot(tok::l_brace)) {
672 if (ReadInitializer)
673 return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
674 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
675 }
Sebastian Redl0d164012011-09-30 08:32:17 +0000676
677 Toks.push_back(Tok);
678 ConsumeBrace();
679 return false;
Sebastian Redla74948d2011-09-24 17:48:25 +0000680}