blob: ef7ad91cbc5db7dd5e80b1fc6d78ceb2ef04cff9 [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"
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000019using namespace clang;
20
Sebastian Redld3a413d2009-04-26 20:35:05 +000021/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis4cc18a42008-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.
John McCalleee1d542011-02-14 07:13:47 +000024Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, ParsingDeclarator &D,
Nico Weber48673472011-01-28 06:07:34 +000025 const ParsedTemplateInfo &TemplateInfo,
Francois Pichet6a247472011-05-11 02:14:46 +000026 const VirtSpecifiers& VS, ExprResult& Init) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +000027 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Sean Hunte4246a62011-05-12 06:15:49 +000028 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
29 Tok.is(tok::equal)) &&
30 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000031
John McCallf312b1e2010-08-26 23:41:50 +000032 MultiTemplateParamsArg TemplateParams(Actions,
Sean Hunt4cd84942010-04-14 23:07:37 +000033 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
34 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
35
John McCalld226f652010-08-21 09:40:31 +000036 Decl *FnD;
John McCall67d1a672009-08-06 02:15:43 +000037 if (D.getDeclSpec().isFriendSpecified())
Douglas Gregor23c94db2010-07-02 17:43:08 +000038 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D, true,
Sean Hunt4cd84942010-04-14 23:07:37 +000039 move(TemplateParams));
Douglas Gregor147545d2011-10-10 14:49:18 +000040 else {
Douglas Gregor23c94db2010-07-02 17:43:08 +000041 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Anders Carlsson69a87352011-01-20 03:57:25 +000042 move(TemplateParams), 0,
Douglas Gregor147545d2011-10-10 14:49:18 +000043 VS, /*HasInit=*/false,
Francois Pichet6a247472011-05-11 02:14:46 +000044 /*IsDefinition*/true);
Douglas Gregor147545d2011-10-10 14:49:18 +000045 if (FnD) {
46 bool TypeSpecContainsAuto
47 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
48 if (Init.get())
49 Actions.AddInitializerToDecl(FnD, Init.get(), false,
50 TypeSpecContainsAuto);
51 else
52 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
53 }
Nico Weber48673472011-01-28 06:07:34 +000054 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +000055
Eli Friedmand33133c2009-07-22 21:45:50 +000056 HandleMemberFunctionDefaultArgs(D, FnD);
57
John McCalleee1d542011-02-14 07:13:47 +000058 D.complete(FnD);
59
Sean Hunte4246a62011-05-12 06:15:49 +000060 if (Tok.is(tok::equal)) {
61 ConsumeToken();
62
63 bool Delete = false;
64 SourceLocation KWLoc;
65 if (Tok.is(tok::kw_delete)) {
66 if (!getLang().CPlusPlus0x)
67 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
68
69 KWLoc = ConsumeToken();
70 Actions.SetDeclDeleted(FnD, KWLoc);
71 Delete = true;
72 } else if (Tok.is(tok::kw_default)) {
73 if (!getLang().CPlusPlus0x)
74 Diag(Tok, diag::warn_defaulted_function_accepted_as_extension);
75
76 KWLoc = ConsumeToken();
77 Actions.SetDeclDefaulted(FnD, KWLoc);
78 } else {
79 llvm_unreachable("function definition after = not 'delete' or 'default'");
80 }
81
82 if (Tok.is(tok::comma)) {
83 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
84 << Delete;
85 SkipUntil(tok::semi);
86 } else {
87 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
88 Delete ? "delete" : "default", tok::semi);
89 }
90
91 return FnD;
92 }
93
Francois Pichet8387e2a2011-04-22 22:18:13 +000094 // In delayed template parsing mode, if we are within a class template
95 // or if we are about to parse function member template then consume
96 // the tokens and store them for parsing at the end of the translation unit.
97 if (getLang().DelayedTemplateParsing &&
98 ((Actions.CurContext->isDependentContext() ||
99 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
100 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
101 !D.getDeclSpec().isFriendSpecified()) {
102
103 if (FnD) {
104 LateParsedTemplatedFunction *LPT =
105 new LateParsedTemplatedFunction(this, FnD);
106
107 FunctionDecl *FD = 0;
108 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
109 FD = FunTmpl->getTemplatedDecl();
110 else
111 FD = cast<FunctionDecl>(FnD);
Chandler Carruth81542fd2011-04-25 07:09:43 +0000112 Actions.CheckForFunctionRedefinition(FD);
Francois Pichet8387e2a2011-04-22 22:18:13 +0000113
114 LateParsedTemplateMap[FD] = LPT;
115 Actions.MarkAsLateParsedTemplate(FD);
116 LexTemplateFunctionForLateParsing(LPT->Toks);
117 } else {
118 CachedTokens Toks;
119 LexTemplateFunctionForLateParsing(Toks);
120 }
121
122 return FnD;
123 }
124
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000125 // Consume the tokens and store them for later parsing.
126
Douglas Gregord54eb442010-10-12 16:25:54 +0000127 LexedMethod* LM = new LexedMethod(this, FnD);
128 getCurrentClass().LateParsedDeclarations.push_back(LM);
129 LM->TemplateScope = getCurScope()->isTemplateParamScope();
130 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000131
Sebastian Redld3a413d2009-04-26 20:35:05 +0000132 tok::TokenKind kind = Tok.getKind();
Sebastian Redla891a322011-09-30 08:32:17 +0000133 // Consume everything up to (and including) the left brace of the
134 // function body.
135 if (ConsumeAndStoreFunctionPrologue(Toks)) {
136 // We didn't find the left-brace we expected after the
137 // constructor initializer.
138 if (Tok.is(tok::semi)) {
139 // We found a semicolon; complain, consume the semicolon, and
140 // don't try to parse this method later.
141 Diag(Tok.getLocation(), diag::err_expected_lbrace);
142 ConsumeAnyToken();
143 delete getCurrentClass().LateParsedDeclarations.back();
144 getCurrentClass().LateParsedDeclarations.pop_back();
145 return FnD;
Douglas Gregor3f08d182008-11-10 16:59:40 +0000146 }
Douglas Gregor7ad83902008-11-05 04:29:56 +0000147 } else {
Sebastian Redla891a322011-09-30 08:32:17 +0000148 // Consume everything up to (and including) the matching right brace.
149 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregor7ad83902008-11-05 04:29:56 +0000150 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000151
Sebastian Redld3a413d2009-04-26 20:35:05 +0000152 // If we're in a function-try-block, we need to store all the catch blocks.
153 if (kind == tok::kw_try) {
154 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000155 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
156 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redld3a413d2009-04-26 20:35:05 +0000157 }
158 }
159
Douglas Gregor87f10642011-04-14 23:19:27 +0000160
161 if (!FnD) {
162 // If semantic analysis could not build a function declaration,
163 // just throw away the late-parsed declaration.
164 delete getCurrentClass().LateParsedDeclarations.back();
165 getCurrentClass().LateParsedDeclarations.pop_back();
166 }
167
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000168 return FnD;
169}
170
Richard Smith7a614d82011-06-11 17:19:42 +0000171/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
172/// specified Declarator is a well formed C++ non-static data member
173/// declaration. Now lex its initializer and store its tokens for parsing
174/// after the class is complete.
175void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
176 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
177 "Current token not a '{' or '='!");
178
179 LateParsedMemberInitializer *MI =
180 new LateParsedMemberInitializer(this, VarD);
181 getCurrentClass().LateParsedDeclarations.push_back(MI);
182 CachedTokens &Toks = MI->Toks;
183
184 tok::TokenKind kind = Tok.getKind();
185 if (kind == tok::equal) {
186 Toks.push_back(Tok);
187 ConsumeAnyToken();
188 }
189
190 if (kind == tok::l_brace) {
191 // Begin by storing the '{' token.
192 Toks.push_back(Tok);
193 ConsumeBrace();
194
195 // Consume everything up to (and including) the matching right brace.
196 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
197 } else {
198 // Consume everything up to (but excluding) the comma or semicolon.
199 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
200 /*ConsumeFinalToken=*/false);
201 }
202
203 // Store an artificial EOF token to ensure that we don't run off the end of
204 // the initializer when we come to parse it.
205 Token Eof;
206 Eof.startToken();
207 Eof.setKind(tok::eof);
208 Eof.setLocation(Tok.getLocation());
209 Toks.push_back(Eof);
210}
211
Douglas Gregord54eb442010-10-12 16:25:54 +0000212Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
213void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith7a614d82011-06-11 17:19:42 +0000214void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregord54eb442010-10-12 16:25:54 +0000215void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
216
217Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
218 : Self(P), Class(C) {}
219
220Parser::LateParsedClass::~LateParsedClass() {
221 Self->DeallocateParsedClasses(Class);
222}
223
224void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
225 Self->ParseLexedMethodDeclarations(*Class);
226}
227
Richard Smith7a614d82011-06-11 17:19:42 +0000228void Parser::LateParsedClass::ParseLexedMemberInitializers() {
229 Self->ParseLexedMemberInitializers(*Class);
230}
231
Douglas Gregord54eb442010-10-12 16:25:54 +0000232void Parser::LateParsedClass::ParseLexedMethodDefs() {
233 Self->ParseLexedMethodDefs(*Class);
234}
235
236void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
237 Self->ParseLexedMethodDeclaration(*this);
238}
239
240void Parser::LexedMethod::ParseLexedMethodDefs() {
241 Self->ParseLexedMethodDef(*this);
242}
243
Richard Smith7a614d82011-06-11 17:19:42 +0000244void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
245 Self->ParseLexedMemberInitializer(*this);
246}
247
Douglas Gregor72b505b2008-12-16 21:30:33 +0000248/// ParseLexedMethodDeclarations - We finished parsing the member
249/// specification of a top (non-nested) C++ class. Now go over the
250/// stack of method declarations with some parts for which parsing was
251/// delayed (such as default arguments) and parse them.
Douglas Gregor6569d682009-05-27 23:11:45 +0000252void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
253 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000254 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000255 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000256 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000257
John McCall7a1dc562009-12-19 10:49:29 +0000258 // The current scope is still active if we're the top-level class.
259 // Otherwise we'll need to push and enter a new scope.
Douglas Gregor6569d682009-05-27 23:11:45 +0000260 bool HasClassScope = !Class.TopLevelClass;
Sean Hunt4cd84942010-04-14 23:07:37 +0000261 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
262 HasClassScope);
John McCall7a1dc562009-12-19 10:49:29 +0000263 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000264 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000265
Douglas Gregord54eb442010-10-12 16:25:54 +0000266 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
267 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor72b505b2008-12-16 21:30:33 +0000268 }
Douglas Gregor6569d682009-05-27 23:11:45 +0000269
John McCall7a1dc562009-12-19 10:49:29 +0000270 if (HasClassScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000271 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
Douglas Gregor72b505b2008-12-16 21:30:33 +0000272}
273
Douglas Gregord54eb442010-10-12 16:25:54 +0000274void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
275 // If this is a member template, introduce the template parameter scope.
276 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
277 if (LM.TemplateScope)
278 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
279
280 // Start the delayed C++ method declaration
281 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
282
283 // Introduce the parameters into scope and parse their default
284 // arguments.
285 ParseScope PrototypeScope(this,
286 Scope::FunctionPrototypeScope|Scope::DeclScope);
287 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
288 // Introduce the parameter into scope.
289 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
290
291 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
292 // Save the current token position.
293 SourceLocation origLoc = Tok.getLocation();
294
295 // Parse the default argument from its saved token stream.
296 Toks->push_back(Tok); // So that the current token doesn't get lost
297 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
298
299 // Consume the previously-pushed token.
300 ConsumeAnyToken();
301
302 // Consume the '='.
303 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
304 SourceLocation EqualLoc = ConsumeToken();
305
306 // The argument isn't actually potentially evaluated unless it is
307 // used.
308 EnterExpressionEvaluationContext Eval(Actions,
309 Sema::PotentiallyEvaluatedIfUsed);
310
311 ExprResult DefArgResult(ParseAssignmentExpression());
312 if (DefArgResult.isInvalid())
313 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
314 else {
315 if (Tok.is(tok::cxx_defaultarg_end))
316 ConsumeToken();
317 else
318 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
319 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
320 DefArgResult.take());
321 }
322
323 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
324 Tok.getLocation()) &&
325 "ParseAssignmentExpression went over the default arg tokens!");
326 // There could be leftover tokens (e.g. because of an error).
327 // Skip through until we reach the original token position.
328 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
329 ConsumeAnyToken();
330
331 delete Toks;
332 LM.DefaultArgs[I].Toks = 0;
333 }
334 }
335 PrototypeScope.Exit();
336
337 // Finish the delayed C++ method declaration.
338 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
339}
340
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000341/// ParseLexedMethodDefs - We finished parsing the member specification of a top
342/// (non-nested) C++ class. Now go over the stack of lexed methods that were
343/// collected during its parsing and parse them all.
Douglas Gregor6569d682009-05-27 23:11:45 +0000344void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
345 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregord54eb442010-10-12 16:25:54 +0000346 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Douglas Gregor6569d682009-05-27 23:11:45 +0000347 if (HasTemplateScope)
Douglas Gregor23c94db2010-07-02 17:43:08 +0000348 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Douglas Gregor6569d682009-05-27 23:11:45 +0000349
350 bool HasClassScope = !Class.TopLevelClass;
351 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
352 HasClassScope);
353
Douglas Gregord54eb442010-10-12 16:25:54 +0000354 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
355 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
356 }
357}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000358
Douglas Gregord54eb442010-10-12 16:25:54 +0000359void Parser::ParseLexedMethodDef(LexedMethod &LM) {
360 // If this is a member template, introduce the template parameter scope.
361 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
362 if (LM.TemplateScope)
363 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Douglas Gregord54eb442010-10-12 16:25:54 +0000365 // Save the current token position.
366 SourceLocation origLoc = Tok.getLocation();
Argyrios Kyrtzidisc50a5e02010-03-31 00:38:09 +0000367
Douglas Gregord54eb442010-10-12 16:25:54 +0000368 assert(!LM.Toks.empty() && "Empty body!");
369 // Append the current token at the end of the new token stream so that it
370 // doesn't get lost.
371 LM.Toks.push_back(Tok);
372 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000373
Douglas Gregord54eb442010-10-12 16:25:54 +0000374 // Consume the previously pushed token.
375 ConsumeAnyToken();
376 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
377 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000378
Douglas Gregord54eb442010-10-12 16:25:54 +0000379 // Parse the method body. Function body parsing code is similar enough
380 // to be re-used for method bodies as well.
381 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
382 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000383
Douglas Gregord54eb442010-10-12 16:25:54 +0000384 if (Tok.is(tok::kw_try)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000385 ParseFunctionTryBlock(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000386 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
387 Tok.getLocation()) &&
388 "ParseFunctionTryBlock went over the cached tokens!");
389 // There could be leftover tokens (e.g. because of an error).
390 // Skip through until we reach the original token position.
391 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
392 ConsumeAnyToken();
393 return;
394 }
395 if (Tok.is(tok::colon)) {
396 ParseConstructorInitializer(LM.D);
397
398 // Error recovery.
399 if (!Tok.is(tok::l_brace)) {
Douglas Gregorc9977d02011-03-16 17:05:57 +0000400 FnScope.Exit();
Douglas Gregord54eb442010-10-12 16:25:54 +0000401 Actions.ActOnFinishFunctionBody(LM.D, 0);
Matt Beaumont-Gay10904522011-09-23 22:39:23 +0000402 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
403 ConsumeAnyToken();
Douglas Gregord54eb442010-10-12 16:25:54 +0000404 return;
405 }
406 } else
407 Actions.ActOnDefaultCtorInitializers(LM.D);
408
Douglas Gregorc9977d02011-03-16 17:05:57 +0000409 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregord54eb442010-10-12 16:25:54 +0000410
411 if (Tok.getLocation() != origLoc) {
412 // Due to parsing error, we either went over the cached tokens or
413 // there are still cached tokens left. If it's the latter case skip the
414 // leftover tokens.
415 // Since this is an uncommon situation that should be avoided, use the
416 // expensive isBeforeInTranslationUnit call.
417 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
418 origLoc))
Argyrios Kyrtzidis8f9359f2010-06-19 19:58:34 +0000419 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
Argyrios Kyrtzidis7558cd02010-06-17 10:52:22 +0000420 ConsumeAnyToken();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000421 }
422}
423
Richard Smith7a614d82011-06-11 17:19:42 +0000424/// ParseLexedMemberInitializers - We finished parsing the member specification
425/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
426/// initializers that were collected during its parsing and parse them all.
427void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
428 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
429 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
430 HasTemplateScope);
431 if (HasTemplateScope)
432 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
433
434 // Set or update the scope flags to include Scope::ThisScope.
435 bool AlreadyHasClassScope = Class.TopLevelClass;
436 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
437 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
438 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
439
440 if (!AlreadyHasClassScope)
441 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
442 Class.TagOrTemplate);
443
444 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
445 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
446 }
447
448 if (!AlreadyHasClassScope)
449 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
450 Class.TagOrTemplate);
451
452 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
453}
454
455void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1991b712011-09-29 19:42:27 +0000456 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith7a614d82011-06-11 17:19:42 +0000457 return;
458
459 // Append the current token at the end of the new token stream so that it
460 // doesn't get lost.
461 MI.Toks.push_back(Tok);
462 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
463
464 // Consume the previously pushed token.
465 ConsumeAnyToken();
466
467 SourceLocation EqualLoc;
468 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
469
470 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
471
472 // The next token should be our artificial terminating EOF token.
473 if (Tok.isNot(tok::eof)) {
474 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
475 if (!EndLoc.isValid())
476 EndLoc = Tok.getLocation();
477 // No fixit; we can't recover as if there were a semicolon here.
478 Diag(EndLoc, diag::err_expected_semi_decl_list);
479
480 // Consume tokens until we hit the artificial EOF.
481 while (Tok.isNot(tok::eof))
482 ConsumeAnyToken();
483 }
484 ConsumeAnyToken();
485}
486
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000487/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor72b505b2008-12-16 21:30:33 +0000488/// container until the token 'T' is reached (which gets
Mike Stump1eb44332009-09-09 15:08:12 +0000489/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000490/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000491/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000492/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000493bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
494 CachedTokens &Toks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000495 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000496 // We always want this function to consume at least one token if the first
497 // token isn't T and if not at EOF.
498 bool isFirstTokenConsumed = true;
499 while (1) {
500 // If we found one of the tokens, stop and return true.
Douglas Gregor72b505b2008-12-16 21:30:33 +0000501 if (Tok.is(T1) || Tok.is(T2)) {
502 if (ConsumeFinalToken) {
503 Toks.push_back(Tok);
504 ConsumeAnyToken();
505 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000506 return true;
507 }
508
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000509 switch (Tok.getKind()) {
510 case tok::eof:
511 // Ran out of tokens.
512 return false;
513
514 case tok::l_paren:
515 // Recursively consume properly-nested parens.
516 Toks.push_back(Tok);
517 ConsumeParen();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000518 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000519 break;
520 case tok::l_square:
521 // Recursively consume properly-nested square brackets.
522 Toks.push_back(Tok);
523 ConsumeBracket();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000524 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000525 break;
526 case tok::l_brace:
527 // Recursively consume properly-nested braces.
528 Toks.push_back(Tok);
529 ConsumeBrace();
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000530 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000531 break;
532
533 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
534 // Since the user wasn't looking for this token (if they were, it would
535 // already be handled), this isn't balanced. If there is a LHS token at a
536 // higher level, we will assume that this matches the unbalanced token
537 // and return it. Otherwise, this is a spurious RHS token, which we skip.
538 case tok::r_paren:
539 if (ParenCount && !isFirstTokenConsumed)
540 return false; // Matches something.
541 Toks.push_back(Tok);
542 ConsumeParen();
543 break;
544 case tok::r_square:
545 if (BracketCount && !isFirstTokenConsumed)
546 return false; // Matches something.
547 Toks.push_back(Tok);
548 ConsumeBracket();
549 break;
550 case tok::r_brace:
551 if (BraceCount && !isFirstTokenConsumed)
552 return false; // Matches something.
553 Toks.push_back(Tok);
554 ConsumeBrace();
555 break;
556
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000557 case tok::code_completion:
558 Toks.push_back(Tok);
559 ConsumeCodeCompletionToken();
560 break;
561
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000562 case tok::string_literal:
563 case tok::wide_string_literal:
Douglas Gregor5cee1192011-07-27 05:40:30 +0000564 case tok::utf8_string_literal:
565 case tok::utf16_string_literal:
566 case tok::utf32_string_literal:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000567 Toks.push_back(Tok);
568 ConsumeStringToken();
569 break;
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +0000570 case tok::semi:
571 if (StopAtSemi)
572 return false;
573 // FALL THROUGH.
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +0000574 default:
575 // consume this token.
576 Toks.push_back(Tok);
577 ConsumeToken();
578 break;
579 }
580 isFirstTokenConsumed = false;
581 }
582}
Sebastian Redl6df65482011-09-24 17:48:25 +0000583
584/// \brief Consume tokens and store them in the passed token container until
585/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redla891a322011-09-30 08:32:17 +0000586/// the opening brace of the function body. The opening brace will be consumed
587/// if and only if there was no error.
Sebastian Redl6df65482011-09-24 17:48:25 +0000588///
Sebastian Redla891a322011-09-30 08:32:17 +0000589/// \return True on error.
590bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redl6df65482011-09-24 17:48:25 +0000591 if (Tok.is(tok::kw_try)) {
592 Toks.push_back(Tok);
593 ConsumeToken();
594 }
595 if (Tok.is(tok::colon)) {
596 // Initializers can contain braces too.
597 Toks.push_back(Tok);
598 ConsumeToken();
599
600 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
601 if (Tok.is(tok::eof) || Tok.is(tok::semi))
602 return true;
603
604 // Grab the identifier.
605 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
606 /*StopAtSemi=*/true,
607 /*ConsumeFinalToken=*/false))
608 return true;
609
610 tok::TokenKind kind = Tok.getKind();
611 Toks.push_back(Tok);
612 if (kind == tok::l_paren)
613 ConsumeParen();
614 else {
615 assert(kind == tok::l_brace && "Must be left paren or brace here.");
616 ConsumeBrace();
Sebastian Redla891a322011-09-30 08:32:17 +0000617 // In C++03, this has to be the start of the function body, which
618 // means the initializer is malformed.
619 if (!getLang().CPlusPlus0x)
620 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000621 }
622
623 // Grab the initializer
624 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
625 tok::r_brace,
626 Toks, /*StopAtSemi=*/true))
627 return true;
Sebastian Redla891a322011-09-30 08:32:17 +0000628
629 // Grab the separating comma, if any.
630 if (Tok.is(tok::comma)) {
631 Toks.push_back(Tok);
632 ConsumeToken();
633 }
Sebastian Redl6df65482011-09-24 17:48:25 +0000634 }
635 }
636
Sebastian Redla891a322011-09-30 08:32:17 +0000637 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
638 // brace: an opening one is the function body, while a closing one probably
639 // means we've reached the end of the class.
640 if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
641 /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false))
642 return true;
643 if(Tok.isNot(tok::l_brace))
644 return true;
645
646 Toks.push_back(Tok);
647 ConsumeBrace();
648 return false;
Sebastian Redl6df65482011-09-24 17:48:25 +0000649}