blob: c52b61e7e9832ac1a4ae81129d0882e750369e71 [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
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
16#include "clang/AST/DeclTemplate.h"
17#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000018#include "clang/Sema/DeclSpec.h"
19#include "clang/Sema/Scope.h"
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000020using namespace clang;
21
Sebastian Redla7b98a72009-04-26 20:35:05 +000022/// ParseCXXInlineMethodDef - We parsed and verified that the specified
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000023/// Declarator is a well formed C++ inline method definition. Now lex its body
24/// and store its tokens for parsing after the C++ class is complete.
Rafael Espindolac2453dd2013-01-08 21:00:12 +000025NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +000026 AttributeList *AccessAttrs,
27 ParsingDeclarator &D,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +000028 const ParsedTemplateInfo &TemplateInfo,
Eli Bendersky41842222015-03-23 23:49:41 +000029 const VirtSpecifiers& VS,
Richard Smith9ba0fec2015-06-30 01:28:56 +000030 SourceLocation PureSpecLoc) {
Abramo Bagnara924a8f32010-12-10 16:29:40 +000031 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Daniel Marjamakie59f8d72015-06-18 10:59:26 +000032 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
Alexis Hunt5a7fa252011-05-12 06:15:49 +000033 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000034
Benjamin Kramercc4c49d2012-08-23 23:38:35 +000035 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +000036 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
37 : nullptr,
Nico Weber77c76c52014-05-10 17:43:15 +000038 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
Alexis Hunt1deb9722010-04-14 23:07:37 +000039
Rafael Espindolac2453dd2013-01-08 21:00:12 +000040 NamedDecl *FnD;
John McCall07e91c02009-08-06 02:15:43 +000041 if (D.getDeclSpec().isFriendSpecified())
Kaelyn Uhrain4dc695d2011-10-11 00:28:45 +000042 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
Benjamin Kramer62b95d82012-08-23 21:35:17 +000043 TemplateParams);
Douglas Gregor728d00b2011-10-10 14:49:18 +000044 else {
Douglas Gregor0be31a22010-07-02 17:43:08 +000045 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Craig Topper161e4db2014-05-21 06:02:52 +000046 TemplateParams, nullptr,
Richard Smith2b013182012-06-10 03:12:00 +000047 VS, ICIS_NoInit);
Douglas Gregor728d00b2011-10-10 14:49:18 +000048 if (FnD) {
Richard Smithf8a75c32013-08-29 00:47:48 +000049 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
Richard Smith9ba0fec2015-06-30 01:28:56 +000050 if (PureSpecLoc.isValid())
51 Actions.ActOnPureSpecifier(FnD, PureSpecLoc);
Douglas Gregor728d00b2011-10-10 14:49:18 +000052 }
Nico Weber24b2a822011-01-28 06:07:34 +000053 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000054
Reid Klecknerbde5ede2016-02-19 01:15:08 +000055 if (FnD)
56 HandleMemberFunctionDeclDelays(D, FnD);
Eli Friedman3af2a772009-07-22 21:45:50 +000057
John McCallc1465822011-02-14 07:13:47 +000058 D.complete(FnD);
59
Alp Tokera3ebe6e2013-12-17 14:12:37 +000060 if (TryConsumeToken(tok::equal)) {
Richard Smith1c704732011-11-10 09:08:44 +000061 if (!FnD) {
62 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000063 return nullptr;
Richard Smith1c704732011-11-10 09:08:44 +000064 }
65
Alexis Hunt5a7fa252011-05-12 06:15:49 +000066 bool Delete = false;
67 SourceLocation KWLoc;
Eli Bendersky5f4d76e2015-03-23 21:43:28 +000068 SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
Alp Tokera3ebe6e2013-12-17 14:12:37 +000069 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
70 Diag(KWLoc, getLangOpts().CPlusPlus11
Craig Topper54a6a682015-11-14 18:16:08 +000071 ? diag::warn_cxx98_compat_defaulted_deleted_function
72 : diag::ext_defaulted_deleted_function)
73 << 1 /* deleted */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +000074 Actions.SetDeclDeleted(FnD, KWLoc);
75 Delete = true;
Eli Bendersky5f4d76e2015-03-23 21:43:28 +000076 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
77 DeclAsFunction->setRangeEnd(KWEndLoc);
78 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +000079 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
80 Diag(KWLoc, getLangOpts().CPlusPlus11
Craig Topper54a6a682015-11-14 18:16:08 +000081 ? diag::warn_cxx98_compat_defaulted_deleted_function
82 : diag::ext_defaulted_deleted_function)
83 << 0 /* defaulted */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +000084 Actions.SetDeclDefaulted(FnD, KWLoc);
Eli Bendersky5f4d76e2015-03-23 21:43:28 +000085 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
86 DeclAsFunction->setRangeEnd(KWEndLoc);
87 }
Alexis Hunt5a7fa252011-05-12 06:15:49 +000088 } else {
89 llvm_unreachable("function definition after = not 'delete' or 'default'");
90 }
91
92 if (Tok.is(tok::comma)) {
93 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
94 << Delete;
95 SkipUntil(tok::semi);
Alp Toker383d2c42014-01-01 03:08:43 +000096 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
97 Delete ? "delete" : "default")) {
98 SkipUntil(tok::semi);
Alexis Hunt5a7fa252011-05-12 06:15:49 +000099 }
100
101 return FnD;
102 }
Eli Bendersky41842222015-03-23 23:49:41 +0000103
Olivier Goffartf9e890c2016-06-16 21:40:06 +0000104 if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) &&
105 trySkippingFunctionBody()) {
106 Actions.ActOnSkippedFunctionBody(FnD);
107 return FnD;
108 }
109
Francois Pichet1c229c02011-04-22 22:18:13 +0000110 // In delayed template parsing mode, if we are within a class template
111 // or if we are about to parse function member template then consume
112 // the tokens and store them for parsing at the end of the translation unit.
David Majnemer90b17292013-09-14 05:46:42 +0000113 if (getLangOpts().DelayedTemplateParsing &&
Eli Bendersky41842222015-03-23 23:49:41 +0000114 D.getFunctionDefinitionKind() == FDK_Definition &&
Alp Tokera2794f92014-01-22 07:29:52 +0000115 !D.getDeclSpec().isConstexprSpecified() &&
116 !(FnD && FnD->getAsFunction() &&
Alp Toker314cc812014-01-25 16:55:45 +0000117 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
Francois Pichet1c229c02011-04-22 22:18:13 +0000118 ((Actions.CurContext->isDependentContext() ||
David Majnemer90b17292013-09-14 05:46:42 +0000119 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
120 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
121 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
Francois Pichet1c229c02011-04-22 22:18:13 +0000122
Richard Smithe40f2ba2013-08-07 21:41:30 +0000123 CachedTokens Toks;
124 LexTemplateFunctionForLateParsing(Toks);
Francois Pichet1c229c02011-04-22 22:18:13 +0000125
Richard Smithe40f2ba2013-08-07 21:41:30 +0000126 if (FnD) {
Alp Tokera2794f92014-01-22 07:29:52 +0000127 FunctionDecl *FD = FnD->getAsFunction();
Chandler Carruthbc0f9ae2011-04-25 07:09:43 +0000128 Actions.CheckForFunctionRedefinition(FD);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000129 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
Francois Pichet1c229c02011-04-22 22:18:13 +0000130 }
131
132 return FnD;
133 }
134
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000135 // Consume the tokens and store them for later parsing.
136
Douglas Gregorefc46952010-10-12 16:25:54 +0000137 LexedMethod* LM = new LexedMethod(this, FnD);
138 getCurrentClass().LateParsedDeclarations.push_back(LM);
139 LM->TemplateScope = getCurScope()->isTemplateParamScope();
140 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000141
Sebastian Redla7b98a72009-04-26 20:35:05 +0000142 tok::TokenKind kind = Tok.getKind();
Sebastian Redl0d164012011-09-30 08:32:17 +0000143 // Consume everything up to (and including) the left brace of the
144 // function body.
145 if (ConsumeAndStoreFunctionPrologue(Toks)) {
146 // We didn't find the left-brace we expected after the
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000147 // constructor initializer; we already printed an error, and it's likely
148 // impossible to recover, so don't try to parse this method later.
Richard Smithcde3fd82013-07-04 00:13:48 +0000149 // Skip over the rest of the decl and back to somewhere that looks
150 // reasonable.
151 SkipMalformedDecl();
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000152 delete getCurrentClass().LateParsedDeclarations.back();
153 getCurrentClass().LateParsedDeclarations.pop_back();
154 return FnD;
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
Stephen Lin9354fc52013-06-23 07:37:13 +0000168 if (FnD) {
169 // If this is a friend function, mark that it's late-parsed so that
170 // it's still known to be a definition even before we attach the
171 // parsed body. Sema needs to treat friend function definitions
172 // differently during template instantiation, and it's possible for
173 // the containing class to be instantiated before all its member
174 // function definitions are parsed.
175 //
176 // If you remove this, you can remove the code that clears the flag
177 // after parsing the member.
178 if (D.getDeclSpec().isFriendSpecified()) {
Alp Tokera2794f92014-01-22 07:29:52 +0000179 FunctionDecl *FD = FnD->getAsFunction();
Alp Toker19bff322013-10-18 05:54:24 +0000180 Actions.CheckForFunctionRedefinition(FD);
181 FD->setLateTemplateParsed(true);
Stephen Lin9354fc52013-06-23 07:37:13 +0000182 }
183 } else {
Douglas Gregor6ca64102011-04-14 23:19:27 +0000184 // If semantic analysis could not build a function declaration,
185 // just throw away the late-parsed declaration.
186 delete getCurrentClass().LateParsedDeclarations.back();
187 getCurrentClass().LateParsedDeclarations.pop_back();
188 }
189
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000190 return FnD;
191}
192
Richard Smith938f40b2011-06-11 17:19:42 +0000193/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
194/// specified Declarator is a well formed C++ non-static data member
195/// declaration. Now lex its initializer and store its tokens for parsing
196/// after the class is complete.
197void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000198 assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000199 "Current token not a '{' or '='!");
200
201 LateParsedMemberInitializer *MI =
202 new LateParsedMemberInitializer(this, VarD);
203 getCurrentClass().LateParsedDeclarations.push_back(MI);
204 CachedTokens &Toks = MI->Toks;
205
206 tok::TokenKind kind = Tok.getKind();
207 if (kind == tok::equal) {
208 Toks.push_back(Tok);
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000209 ConsumeToken();
Richard Smith938f40b2011-06-11 17:19:42 +0000210 }
211
212 if (kind == tok::l_brace) {
213 // Begin by storing the '{' token.
214 Toks.push_back(Tok);
215 ConsumeBrace();
216
217 // Consume everything up to (and including) the matching right brace.
218 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
219 } else {
220 // Consume everything up to (but excluding) the comma or semicolon.
Richard Smith1fff95c2013-09-12 23:28:08 +0000221 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
Richard Smith938f40b2011-06-11 17:19:42 +0000222 }
223
224 // Store an artificial EOF token to ensure that we don't run off the end of
225 // the initializer when we come to parse it.
226 Token Eof;
227 Eof.startToken();
228 Eof.setKind(tok::eof);
229 Eof.setLocation(Tok.getLocation());
David Majnemerce1d3012014-12-19 02:13:56 +0000230 Eof.setEofData(VarD);
Richard Smith938f40b2011-06-11 17:19:42 +0000231 Toks.push_back(Eof);
232}
233
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000234Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
Douglas Gregorefc46952010-10-12 16:25:54 +0000235void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith938f40b2011-06-11 17:19:42 +0000236void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregorefc46952010-10-12 16:25:54 +0000237void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
238
239Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
240 : Self(P), Class(C) {}
241
242Parser::LateParsedClass::~LateParsedClass() {
243 Self->DeallocateParsedClasses(Class);
244}
245
246void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
247 Self->ParseLexedMethodDeclarations(*Class);
248}
249
Richard Smith938f40b2011-06-11 17:19:42 +0000250void Parser::LateParsedClass::ParseLexedMemberInitializers() {
251 Self->ParseLexedMemberInitializers(*Class);
252}
253
Douglas Gregorefc46952010-10-12 16:25:54 +0000254void Parser::LateParsedClass::ParseLexedMethodDefs() {
255 Self->ParseLexedMethodDefs(*Class);
256}
257
258void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
259 Self->ParseLexedMethodDeclaration(*this);
260}
261
262void Parser::LexedMethod::ParseLexedMethodDefs() {
263 Self->ParseLexedMethodDef(*this);
264}
265
Richard Smith938f40b2011-06-11 17:19:42 +0000266void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
267 Self->ParseLexedMemberInitializer(*this);
268}
269
Douglas Gregor4d87df52008-12-16 21:30:33 +0000270/// ParseLexedMethodDeclarations - We finished parsing the member
271/// specification of a top (non-nested) C++ class. Now go over the
272/// stack of method declarations with some parts for which parsing was
273/// delayed (such as default arguments) and parse them.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000274void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
275 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Nico Weber77c76c52014-05-10 17:43:15 +0000276 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
277 HasTemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000278 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
279 if (HasTemplateScope) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000280 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Richard Smithc8378952013-04-29 11:55:38 +0000281 ++CurTemplateDepthTracker;
282 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000283
John McCall6df5fef2009-12-19 10:49:29 +0000284 // The current scope is still active if we're the top-level class.
285 // Otherwise we'll need to push and enter a new scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000286 bool HasClassScope = !Class.TopLevelClass;
Alexis Hunt1deb9722010-04-14 23:07:37 +0000287 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
288 HasClassScope);
John McCall6df5fef2009-12-19 10:49:29 +0000289 if (HasClassScope)
Nico Weber77c76c52014-05-10 17:43:15 +0000290 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
291 Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000292
Douglas Gregorefc46952010-10-12 16:25:54 +0000293 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
294 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor4d87df52008-12-16 21:30:33 +0000295 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000296
John McCall6df5fef2009-12-19 10:49:29 +0000297 if (HasClassScope)
Nico Weber77c76c52014-05-10 17:43:15 +0000298 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
299 Class.TagOrTemplate);
Douglas Gregor4d87df52008-12-16 21:30:33 +0000300}
301
Douglas Gregorefc46952010-10-12 16:25:54 +0000302void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
303 // If this is a member template, introduce the template parameter scope.
304 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000305 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
306 if (LM.TemplateScope) {
Douglas Gregorefc46952010-10-12 16:25:54 +0000307 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
Richard Smithc8378952013-04-29 11:55:38 +0000308 ++CurTemplateDepthTracker;
309 }
Douglas Gregorefc46952010-10-12 16:25:54 +0000310 // Start the delayed C++ method declaration
311 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
312
313 // Introduce the parameters into scope and parse their default
314 // arguments.
Richard Smithe233fbf2013-01-28 22:42:45 +0000315 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
316 Scope::FunctionDeclarationScope | Scope::DeclScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000317 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
Nathan Sidwell5bb231c2015-02-19 14:03:22 +0000318 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
Douglas Gregorefc46952010-10-12 16:25:54 +0000319 // Introduce the parameter into scope.
Nathan Sidwell5bb231c2015-02-19 14:03:22 +0000320 bool HasUnparsed = Param->hasUnparsedDefaultArg();
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000321 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
Malcolm Parsonsff0382c2016-11-17 17:52:58 +0000322 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
323 if (Toks) {
David Majnemer7cceba52015-01-13 04:20:57 +0000324 // Mark the end of the default argument so that we know when to stop when
325 // we parse it later on.
326 Token LastDefaultArgToken = Toks->back();
327 Token DefArgEnd;
328 DefArgEnd.startToken();
329 DefArgEnd.setKind(tok::eof);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000330 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000331 DefArgEnd.setEofData(Param);
David Majnemer7cceba52015-01-13 04:20:57 +0000332 Toks->push_back(DefArgEnd);
Douglas Gregorefc46952010-10-12 16:25:54 +0000333
334 // Parse the default argument from its saved token stream.
335 Toks->push_back(Tok); // So that the current token doesn't get lost
David Blaikie2eabcc92016-02-09 18:52:09 +0000336 PP.EnterTokenStream(*Toks, true);
Douglas Gregorefc46952010-10-12 16:25:54 +0000337
338 // Consume the previously-pushed token.
339 ConsumeAnyToken();
340
341 // Consume the '='.
342 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
343 SourceLocation EqualLoc = ConsumeToken();
344
345 // The argument isn't actually potentially evaluated unless it is
346 // used.
347 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000348 Sema::PotentiallyEvaluatedIfUsed,
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000349 Param);
Douglas Gregorefc46952010-10-12 16:25:54 +0000350
Sebastian Redldb63af22012-03-14 15:54:00 +0000351 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000352 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl6db0b1b2012-03-20 21:24:03 +0000353 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +0000354 DefArgResult = ParseBraceInitializer();
Sebastian Redl6db0b1b2012-03-20 21:24:03 +0000355 } else
Sebastian Redldb63af22012-03-14 15:54:00 +0000356 DefArgResult = ParseAssignmentExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000357 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
David Majnemerbba12342015-01-12 06:51:15 +0000358 if (DefArgResult.isInvalid()) {
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000359 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
David Majnemerbba12342015-01-12 06:51:15 +0000360 } else {
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000361 if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000362 // The last two tokens are the terminator and the saved value of
363 // Tok; the last token in the default argument is the one before
364 // those.
365 assert(Toks->size() >= 3 && "expected a token in default arg");
366 Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
367 << SourceRange(Tok.getLocation(),
368 (*Toks)[Toks->size() - 3].getLocation());
369 }
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000370 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000371 DefArgResult.get());
Douglas Gregorefc46952010-10-12 16:25:54 +0000372 }
373
Douglas Gregorefc46952010-10-12 16:25:54 +0000374 // There could be leftover tokens (e.g. because of an error).
David Majnemer7cceba52015-01-13 04:20:57 +0000375 // Skip through until we reach the 'end of default argument' token.
376 while (Tok.isNot(tok::eof))
Douglas Gregorefc46952010-10-12 16:25:54 +0000377 ConsumeAnyToken();
David Majnemer7cceba52015-01-13 04:20:57 +0000378
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000379 if (Tok.is(tok::eof) && Tok.getEofData() == Param)
David Majnemer7cceba52015-01-13 04:20:57 +0000380 ConsumeAnyToken();
Nathan Sidwell5bb231c2015-02-19 14:03:22 +0000381 } else if (HasUnparsed) {
382 assert(Param->hasInheritedDefaultArg());
383 FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
384 ParmVarDecl *OldParam = Old->getParamDecl(I);
385 assert (!OldParam->hasUnparsedDefaultArg());
386 if (OldParam->hasUninstantiatedDefaultArg())
387 Param->setUninstantiatedDefaultArg(
David Majnemercef7d372016-06-10 20:21:15 +0000388 OldParam->getUninstantiatedDefaultArg());
Nathan Sidwell5bb231c2015-02-19 14:03:22 +0000389 else
390 Param->setDefaultArg(OldParam->getInit());
Douglas Gregorefc46952010-10-12 16:25:54 +0000391 }
392 }
Douglas Gregor433e0532012-04-16 18:27:27 +0000393
Richard Smith0b3a4622014-11-13 20:01:57 +0000394 // Parse a delayed exception-specification, if there is one.
395 if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
David Majnemer7cceba52015-01-13 04:20:57 +0000396 // Add the 'stop' token.
397 Token LastExceptionSpecToken = Toks->back();
398 Token ExceptionSpecEnd;
399 ExceptionSpecEnd.startToken();
400 ExceptionSpecEnd.setKind(tok::eof);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000401 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
David Majnemer7cceba52015-01-13 04:20:57 +0000402 ExceptionSpecEnd.setEofData(LM.Method);
403 Toks->push_back(ExceptionSpecEnd);
Richard Smith0b3a4622014-11-13 20:01:57 +0000404
405 // Parse the default argument from its saved token stream.
406 Toks->push_back(Tok); // So that the current token doesn't get lost
David Blaikie2eabcc92016-02-09 18:52:09 +0000407 PP.EnterTokenStream(*Toks, true);
Richard Smith0b3a4622014-11-13 20:01:57 +0000408
409 // Consume the previously-pushed token.
410 ConsumeAnyToken();
411
412 // C++11 [expr.prim.general]p3:
413 // If a declaration declares a member function or member function
414 // template of a class X, the expression this is a prvalue of type
415 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
416 // and the end of the function-definition, member-declarator, or
417 // declarator.
418 CXXMethodDecl *Method;
419 if (FunctionTemplateDecl *FunTmpl
420 = dyn_cast<FunctionTemplateDecl>(LM.Method))
421 Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
422 else
423 Method = cast<CXXMethodDecl>(LM.Method);
424
425 Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
426 Method->getTypeQualifiers(),
427 getLangOpts().CPlusPlus11);
428
429 // Parse the exception-specification.
430 SourceRange SpecificationRange;
431 SmallVector<ParsedType, 4> DynamicExceptions;
432 SmallVector<SourceRange, 4> DynamicExceptionRanges;
433 ExprResult NoexceptExpr;
434 CachedTokens *ExceptionSpecTokens;
435
436 ExceptionSpecificationType EST
437 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
438 DynamicExceptions,
439 DynamicExceptionRanges, NoexceptExpr,
440 ExceptionSpecTokens);
441
David Majnemer7cceba52015-01-13 04:20:57 +0000442 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
Richard Smith0b3a4622014-11-13 20:01:57 +0000443 Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
444
445 // Attach the exception-specification to the method.
Richard Smith3ef3e892014-11-20 22:32:11 +0000446 Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
447 SpecificationRange,
448 DynamicExceptions,
449 DynamicExceptionRanges,
450 NoexceptExpr.isUsable()?
451 NoexceptExpr.get() : nullptr);
Richard Smith0b3a4622014-11-13 20:01:57 +0000452
Richard Smith0b3a4622014-11-13 20:01:57 +0000453 // There could be leftover tokens (e.g. because of an error).
454 // Skip through until we reach the original token position.
David Majnemer7cceba52015-01-13 04:20:57 +0000455 while (Tok.isNot(tok::eof))
Richard Smith0b3a4622014-11-13 20:01:57 +0000456 ConsumeAnyToken();
David Majnemer7cceba52015-01-13 04:20:57 +0000457
458 // Clean up the remaining EOF token.
459 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
460 ConsumeAnyToken();
Richard Smith0b3a4622014-11-13 20:01:57 +0000461
462 delete Toks;
463 LM.ExceptionSpecTokens = nullptr;
464 }
465
Douglas Gregorefc46952010-10-12 16:25:54 +0000466 PrototypeScope.Exit();
467
468 // Finish the delayed C++ method declaration.
469 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
470}
471
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000472/// ParseLexedMethodDefs - We finished parsing the member specification of a top
473/// (non-nested) C++ class. Now go over the stack of lexed methods that were
474/// collected during its parsing and parse them all.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000475void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
476 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000477 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000478 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
479 if (HasTemplateScope) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000480 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Richard Smithc8378952013-04-29 11:55:38 +0000481 ++CurTemplateDepthTracker;
482 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000483 bool HasClassScope = !Class.TopLevelClass;
484 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
485 HasClassScope);
486
Douglas Gregorefc46952010-10-12 16:25:54 +0000487 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
488 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
489 }
490}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000491
Douglas Gregorefc46952010-10-12 16:25:54 +0000492void Parser::ParseLexedMethodDef(LexedMethod &LM) {
493 // If this is a member template, introduce the template parameter scope.
494 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000495 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
496 if (LM.TemplateScope) {
Douglas Gregorefc46952010-10-12 16:25:54 +0000497 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Richard Smithc8378952013-04-29 11:55:38 +0000498 ++CurTemplateDepthTracker;
499 }
Argyrios Kyrtzidis02041972010-03-31 00:38:09 +0000500
Douglas Gregorefc46952010-10-12 16:25:54 +0000501 assert(!LM.Toks.empty() && "Empty body!");
David Majnemera7ea1b12015-01-13 05:06:20 +0000502 Token LastBodyToken = LM.Toks.back();
503 Token BodyEnd;
504 BodyEnd.startToken();
505 BodyEnd.setKind(tok::eof);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000506 BodyEnd.setLocation(LastBodyToken.getEndLoc());
David Majnemera7ea1b12015-01-13 05:06:20 +0000507 BodyEnd.setEofData(LM.D);
508 LM.Toks.push_back(BodyEnd);
Douglas Gregorefc46952010-10-12 16:25:54 +0000509 // Append the current token at the end of the new token stream so that it
510 // doesn't get lost.
511 LM.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +0000512 PP.EnterTokenStream(LM.Toks, true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000513
Douglas Gregorefc46952010-10-12 16:25:54 +0000514 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +0000515 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000516 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
Douglas Gregorefc46952010-10-12 16:25:54 +0000517 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000518
Douglas Gregorefc46952010-10-12 16:25:54 +0000519 // Parse the method body. Function body parsing code is similar enough
520 // to be re-used for method bodies as well.
521 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
522 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000523
Douglas Gregorefc46952010-10-12 16:25:54 +0000524 if (Tok.is(tok::kw_try)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000525 ParseFunctionTryBlock(LM.D, FnScope);
David Majnemera7ea1b12015-01-13 05:06:20 +0000526
527 while (Tok.isNot(tok::eof))
528 ConsumeAnyToken();
529
530 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
Douglas Gregorefc46952010-10-12 16:25:54 +0000531 ConsumeAnyToken();
532 return;
533 }
534 if (Tok.is(tok::colon)) {
535 ParseConstructorInitializer(LM.D);
536
537 // Error recovery.
538 if (!Tok.is(tok::l_brace)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000539 FnScope.Exit();
Craig Topper161e4db2014-05-21 06:02:52 +0000540 Actions.ActOnFinishFunctionBody(LM.D, nullptr);
David Majnemera7ea1b12015-01-13 05:06:20 +0000541
542 while (Tok.isNot(tok::eof))
543 ConsumeAnyToken();
544
545 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
Matt Beaumont-Gayd0457922011-09-23 22:39:23 +0000546 ConsumeAnyToken();
Douglas Gregorefc46952010-10-12 16:25:54 +0000547 return;
548 }
549 } else
550 Actions.ActOnDefaultCtorInitializers(LM.D);
551
Richard Smithc8378952013-04-29 11:55:38 +0000552 assert((Actions.getDiagnostics().hasErrorOccurred() ||
553 !isa<FunctionTemplateDecl>(LM.D) ||
554 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
555 < TemplateParameterDepth) &&
556 "TemplateParameterDepth should be greater than the depth of "
557 "current template being instantiated!");
558
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000559 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000560
John McCalle68672f2013-03-14 05:13:41 +0000561 // Clear the late-template-parsed bit if we set it before.
Alp Tokera2794f92014-01-22 07:29:52 +0000562 if (LM.D)
563 LM.D->getAsFunction()->setLateTemplateParsed(false);
John McCalle68672f2013-03-14 05:13:41 +0000564
David Majnemera7ea1b12015-01-13 05:06:20 +0000565 while (Tok.isNot(tok::eof))
566 ConsumeAnyToken();
567
568 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
569 ConsumeAnyToken();
Hans Wennborga926d842014-05-23 20:37:38 +0000570
Stephan Bergmann17d7d142016-03-30 06:27:31 +0000571 if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
572 if (isa<CXXMethodDecl>(FD) ||
573 FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend))
574 Actions.ActOnFinishInlineFunctionDef(FD);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000575}
576
Richard Smith938f40b2011-06-11 17:19:42 +0000577/// ParseLexedMemberInitializers - We finished parsing the member specification
578/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
579/// initializers that were collected during its parsing and parse them all.
580void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
581 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
582 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
583 HasTemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000584 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
585 if (HasTemplateScope) {
Richard Smith938f40b2011-06-11 17:19:42 +0000586 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Richard Smithc8378952013-04-29 11:55:38 +0000587 ++CurTemplateDepthTracker;
588 }
Douglas Gregor3024f072012-04-16 07:05:22 +0000589 // Set or update the scope flags.
Richard Smith938f40b2011-06-11 17:19:42 +0000590 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000591 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Richard Smith938f40b2011-06-11 17:19:42 +0000592 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
593 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
594
595 if (!AlreadyHasClassScope)
596 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
597 Class.TagOrTemplate);
598
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000599 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000600 // C++11 [expr.prim.general]p4:
601 // Otherwise, if a member-declarator declares a non-static data member
602 // (9.2) of a class X, the expression this is a prvalue of type "pointer
603 // to X" within the optional brace-or-equal-initializer. It shall not
604 // appear elsewhere in the member-declarator.
605 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
606 /*TypeQuals=*/(unsigned)0);
Richard Smith938f40b2011-06-11 17:19:42 +0000607
Douglas Gregor3024f072012-04-16 07:05:22 +0000608 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
609 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
610 }
611 }
612
Richard Smith938f40b2011-06-11 17:19:42 +0000613 if (!AlreadyHasClassScope)
614 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
615 Class.TagOrTemplate);
616
617 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
618}
619
620void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1a526fd2011-09-29 19:42:27 +0000621 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith938f40b2011-06-11 17:19:42 +0000622 return;
623
624 // Append the current token at the end of the new token stream so that it
625 // doesn't get lost.
626 MI.Toks.push_back(Tok);
David Blaikie2eabcc92016-02-09 18:52:09 +0000627 PP.EnterTokenStream(MI.Toks, true);
Richard Smith938f40b2011-06-11 17:19:42 +0000628
629 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +0000630 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Richard Smith938f40b2011-06-11 17:19:42 +0000631
632 SourceLocation EqualLoc;
Richard Smith2b013182012-06-10 03:12:00 +0000633
Richard Smith74108172014-01-17 03:11:34 +0000634 Actions.ActOnStartCXXInClassMemberInitializer();
635
Douglas Gregor926410d2012-02-21 02:22:07 +0000636 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
637 EqualLoc);
Richard Smith938f40b2011-06-11 17:19:42 +0000638
Richard Smith74108172014-01-17 03:11:34 +0000639 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000640 Init.get());
Richard Smith938f40b2011-06-11 17:19:42 +0000641
642 // The next token should be our artificial terminating EOF token.
643 if (Tok.isNot(tok::eof)) {
Richard Smith95e1fb02014-08-27 03:23:12 +0000644 if (!Init.isInvalid()) {
645 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
646 if (!EndLoc.isValid())
647 EndLoc = Tok.getLocation();
648 // No fixit; we can't recover as if there were a semicolon here.
649 Diag(EndLoc, diag::err_expected_semi_decl_list);
650 }
Richard Smith938f40b2011-06-11 17:19:42 +0000651
652 // Consume tokens until we hit the artificial EOF.
653 while (Tok.isNot(tok::eof))
654 ConsumeAnyToken();
655 }
David Majnemer2d3663e2014-12-18 09:57:31 +0000656 // Make sure this is *our* artificial EOF token.
David Majnemerce1d3012014-12-19 02:13:56 +0000657 if (Tok.getEofData() == MI.Field)
David Majnemer2d3663e2014-12-18 09:57:31 +0000658 ConsumeAnyToken();
Richard Smith938f40b2011-06-11 17:19:42 +0000659}
660
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000661/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor4d87df52008-12-16 21:30:33 +0000662/// container until the token 'T' is reached (which gets
Mike Stump11289f42009-09-09 15:08:12 +0000663/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000664/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000665/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000666/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000667bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
668 CachedTokens &Toks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000669 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000670 // We always want this function to consume at least one token if the first
671 // token isn't T and if not at EOF.
672 bool isFirstTokenConsumed = true;
673 while (1) {
674 // If we found one of the tokens, stop and return true.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000675 if (Tok.is(T1) || Tok.is(T2)) {
676 if (ConsumeFinalToken) {
677 Toks.push_back(Tok);
678 ConsumeAnyToken();
679 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000680 return true;
681 }
682
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000683 switch (Tok.getKind()) {
684 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +0000685 case tok::annot_module_begin:
686 case tok::annot_module_end:
687 case tok::annot_module_include:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000688 // Ran out of tokens.
689 return false;
690
691 case tok::l_paren:
692 // Recursively consume properly-nested parens.
693 Toks.push_back(Tok);
694 ConsumeParen();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000695 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000696 break;
697 case tok::l_square:
698 // Recursively consume properly-nested square brackets.
699 Toks.push_back(Tok);
700 ConsumeBracket();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000701 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000702 break;
703 case tok::l_brace:
704 // Recursively consume properly-nested braces.
705 Toks.push_back(Tok);
706 ConsumeBrace();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000707 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000708 break;
709
710 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
711 // Since the user wasn't looking for this token (if they were, it would
712 // already be handled), this isn't balanced. If there is a LHS token at a
713 // higher level, we will assume that this matches the unbalanced token
714 // and return it. Otherwise, this is a spurious RHS token, which we skip.
715 case tok::r_paren:
716 if (ParenCount && !isFirstTokenConsumed)
717 return false; // Matches something.
718 Toks.push_back(Tok);
719 ConsumeParen();
720 break;
721 case tok::r_square:
722 if (BracketCount && !isFirstTokenConsumed)
723 return false; // Matches something.
724 Toks.push_back(Tok);
725 ConsumeBracket();
726 break;
727 case tok::r_brace:
728 if (BraceCount && !isFirstTokenConsumed)
729 return false; // Matches something.
730 Toks.push_back(Tok);
731 ConsumeBrace();
732 break;
733
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000734 case tok::code_completion:
735 Toks.push_back(Tok);
736 ConsumeCodeCompletionToken();
737 break;
738
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000739 case tok::string_literal:
740 case tok::wide_string_literal:
Douglas Gregorfb65e592011-07-27 05:40:30 +0000741 case tok::utf8_string_literal:
742 case tok::utf16_string_literal:
743 case tok::utf32_string_literal:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000744 Toks.push_back(Tok);
745 ConsumeStringToken();
746 break;
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000747 case tok::semi:
748 if (StopAtSemi)
749 return false;
750 // FALL THROUGH.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000751 default:
752 // consume this token.
753 Toks.push_back(Tok);
754 ConsumeToken();
755 break;
756 }
757 isFirstTokenConsumed = false;
758 }
759}
Sebastian Redla74948d2011-09-24 17:48:25 +0000760
761/// \brief Consume tokens and store them in the passed token container until
762/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redl0d164012011-09-30 08:32:17 +0000763/// the opening brace of the function body. The opening brace will be consumed
764/// if and only if there was no error.
Sebastian Redla74948d2011-09-24 17:48:25 +0000765///
Richard Smithcde3fd82013-07-04 00:13:48 +0000766/// \return True on error.
Sebastian Redl0d164012011-09-30 08:32:17 +0000767bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redla74948d2011-09-24 17:48:25 +0000768 if (Tok.is(tok::kw_try)) {
769 Toks.push_back(Tok);
770 ConsumeToken();
771 }
Richard Smithcde3fd82013-07-04 00:13:48 +0000772
773 if (Tok.isNot(tok::colon)) {
774 // Easy case, just a function body.
775
776 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
777 // brace: an opening one is the function body, while a closing one probably
778 // means we've reached the end of the class.
779 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
780 /*StopAtSemi=*/true,
781 /*ConsumeFinalToken=*/false);
782 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000783 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
Richard Smithcde3fd82013-07-04 00:13:48 +0000784
Sebastian Redla74948d2011-09-24 17:48:25 +0000785 Toks.push_back(Tok);
Richard Smithcde3fd82013-07-04 00:13:48 +0000786 ConsumeBrace();
787 return false;
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000788 }
Sebastian Redl0d164012011-09-30 08:32:17 +0000789
790 Toks.push_back(Tok);
Richard Smithcde3fd82013-07-04 00:13:48 +0000791 ConsumeToken();
792
793 // We can't reliably skip over a mem-initializer-id, because it could be
794 // a template-id involving not-yet-declared names. Given:
795 //
796 // S ( ) : a < b < c > ( e )
797 //
798 // 'e' might be an initializer or part of a template argument, depending
799 // on whether 'b' is a template.
800
801 // Track whether we might be inside a template argument. We can give
802 // significantly better diagnostics if we know that we're not.
803 bool MightBeTemplateArgument = false;
804
805 while (true) {
806 // Skip over the mem-initializer-id, if possible.
807 if (Tok.is(tok::kw_decltype)) {
808 Toks.push_back(Tok);
809 SourceLocation OpenLoc = ConsumeToken();
810 if (Tok.isNot(tok::l_paren))
811 return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
812 << "decltype";
813 Toks.push_back(Tok);
814 ConsumeParen();
815 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
Alp Tokerec543272013-12-24 09:48:30 +0000816 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
817 Diag(OpenLoc, diag::note_matching) << tok::l_paren;
Richard Smithcde3fd82013-07-04 00:13:48 +0000818 return true;
819 }
820 }
821 do {
822 // Walk over a component of a nested-name-specifier.
823 if (Tok.is(tok::coloncolon)) {
824 Toks.push_back(Tok);
825 ConsumeToken();
826
827 if (Tok.is(tok::kw_template)) {
828 Toks.push_back(Tok);
829 ConsumeToken();
830 }
831 }
832
Olivier Goffart3cd10132016-11-03 07:36:17 +0000833 if (Tok.is(tok::identifier)) {
Richard Smithcde3fd82013-07-04 00:13:48 +0000834 Toks.push_back(Tok);
835 ConsumeToken();
Richard Smithcde3fd82013-07-04 00:13:48 +0000836 } else {
837 break;
838 }
839 } while (Tok.is(tok::coloncolon));
840
Olivier Goffart3cd10132016-11-03 07:36:17 +0000841 if (Tok.is(tok::code_completion)) {
842 Toks.push_back(Tok);
843 ConsumeCodeCompletionToken();
844 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) {
845 // Could be the start of another member initializer (the ',' has not
846 // been written yet)
847 continue;
848 }
849 }
850
851 if (Tok.is(tok::comma)) {
852 // The initialization is missing, we'll diagnose it later.
853 Toks.push_back(Tok);
854 ConsumeToken();
855 continue;
856 }
Richard Smithcde3fd82013-07-04 00:13:48 +0000857 if (Tok.is(tok::less))
858 MightBeTemplateArgument = true;
859
860 if (MightBeTemplateArgument) {
861 // We may be inside a template argument list. Grab up to the start of the
862 // next parenthesized initializer or braced-init-list. This *might* be the
863 // initializer, or it might be a subexpression in the template argument
864 // list.
865 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
866 // if all angles are closed.
867 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
868 /*StopAtSemi=*/true,
869 /*ConsumeFinalToken=*/false)) {
870 // We're not just missing the initializer, we're also missing the
871 // function body!
Alp Tokerec543272013-12-24 09:48:30 +0000872 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
Richard Smithcde3fd82013-07-04 00:13:48 +0000873 }
874 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
875 // We found something weird in a mem-initializer-id.
Alp Tokerec543272013-12-24 09:48:30 +0000876 if (getLangOpts().CPlusPlus11)
877 return Diag(Tok.getLocation(), diag::err_expected_either)
878 << tok::l_paren << tok::l_brace;
879 else
880 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
Richard Smithcde3fd82013-07-04 00:13:48 +0000881 }
882
883 tok::TokenKind kind = Tok.getKind();
884 Toks.push_back(Tok);
885 bool IsLParen = (kind == tok::l_paren);
886 SourceLocation OpenLoc = Tok.getLocation();
887
888 if (IsLParen) {
889 ConsumeParen();
890 } else {
891 assert(kind == tok::l_brace && "Must be left paren or brace here.");
892 ConsumeBrace();
893 // In C++03, this has to be the start of the function body, which
894 // means the initializer is malformed; we'll diagnose it later.
895 if (!getLangOpts().CPlusPlus11)
896 return false;
Olivier Goffart3cd10132016-11-03 07:36:17 +0000897
898 const Token &PreviousToken = Toks[Toks.size() - 2];
899 if (!MightBeTemplateArgument &&
900 !PreviousToken.isOneOf(tok::identifier, tok::greater,
901 tok::greatergreater)) {
902 // If the opening brace is not preceded by one of these tokens, we are
903 // missing the mem-initializer-id. In order to recover better, we need
904 // to use heuristics to determine if this '{' is most likely the
905 // begining of a brace-init-list or the function body.
906 // Check the token after the corresponding '}'.
907 TentativeParsingAction PA(*this);
908 if (SkipUntil(tok::r_brace) &&
909 !Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) {
910 // Consider there was a malformed initializer and this is the start
911 // of the function body. We'll diagnose it later.
912 PA.Revert();
913 return false;
914 }
915 PA.Revert();
916 }
Richard Smithcde3fd82013-07-04 00:13:48 +0000917 }
918
919 // Grab the initializer (or the subexpression of the template argument).
920 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
921 // if we might be inside the braces of a lambda-expression.
Alp Tokerec543272013-12-24 09:48:30 +0000922 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
923 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
924 Diag(Tok, diag::err_expected) << CloseKind;
925 Diag(OpenLoc, diag::note_matching) << kind;
Richard Smithcde3fd82013-07-04 00:13:48 +0000926 return true;
927 }
928
929 // Grab pack ellipsis, if present.
930 if (Tok.is(tok::ellipsis)) {
931 Toks.push_back(Tok);
932 ConsumeToken();
933 }
934
935 // If we know we just consumed a mem-initializer, we must have ',' or '{'
936 // next.
937 if (Tok.is(tok::comma)) {
938 Toks.push_back(Tok);
939 ConsumeToken();
940 } else if (Tok.is(tok::l_brace)) {
941 // This is the function body if the ')' or '}' is immediately followed by
942 // a '{'. That cannot happen within a template argument, apart from the
943 // case where a template argument contains a compound literal:
944 //
945 // S ( ) : a < b < c > ( d ) { }
946 // // End of declaration, or still inside the template argument?
947 //
948 // ... and the case where the template argument contains a lambda:
949 //
950 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
951 // ( ) > ( ) { }
952 //
953 // FIXME: Disambiguate these cases. Note that the latter case is probably
954 // going to be made ill-formed by core issue 1607.
955 Toks.push_back(Tok);
956 ConsumeBrace();
957 return false;
958 } else if (!MightBeTemplateArgument) {
Alp Tokerec543272013-12-24 09:48:30 +0000959 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
960 << tok::comma;
Richard Smithcde3fd82013-07-04 00:13:48 +0000961 }
962 }
Sebastian Redla74948d2011-09-24 17:48:25 +0000963}
Richard Smith1fff95c2013-09-12 23:28:08 +0000964
965/// \brief Consume and store tokens from the '?' to the ':' in a conditional
966/// expression.
967bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
968 // Consume '?'.
969 assert(Tok.is(tok::question));
970 Toks.push_back(Tok);
971 ConsumeToken();
972
973 while (Tok.isNot(tok::colon)) {
Nico Weber77c76c52014-05-10 17:43:15 +0000974 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
975 /*StopAtSemi=*/true,
976 /*ConsumeFinalToken=*/false))
Richard Smith1fff95c2013-09-12 23:28:08 +0000977 return false;
978
979 // If we found a nested conditional, consume it.
980 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
981 return false;
982 }
983
984 // Consume ':'.
985 Toks.push_back(Tok);
986 ConsumeToken();
987 return true;
988}
989
990/// \brief A tentative parsing action that can also revert token annotations.
991class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
992public:
993 explicit UnannotatedTentativeParsingAction(Parser &Self,
994 tok::TokenKind EndKind)
995 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
996 // Stash away the old token stream, so we can restore it once the
997 // tentative parse is complete.
998 TentativeParsingAction Inner(Self);
999 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
1000 Inner.Revert();
1001 }
1002
1003 void RevertAnnotations() {
1004 Revert();
1005
1006 // Put back the original tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001007 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
Richard Smith1fff95c2013-09-12 23:28:08 +00001008 if (Toks.size()) {
David Blaikie2eabcc92016-02-09 18:52:09 +00001009 auto Buffer = llvm::make_unique<Token[]>(Toks.size());
1010 std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
Richard Smith1fff95c2013-09-12 23:28:08 +00001011 Buffer[Toks.size() - 1] = Self.Tok;
David Blaikie2eabcc92016-02-09 18:52:09 +00001012 Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true);
Richard Smith1fff95c2013-09-12 23:28:08 +00001013
1014 Self.Tok = Toks.front();
1015 }
1016 }
1017
1018private:
1019 Parser &Self;
1020 CachedTokens Toks;
1021 tok::TokenKind EndKind;
1022};
1023
1024/// ConsumeAndStoreInitializer - Consume and store the token at the passed token
1025/// container until the end of the current initializer expression (either a
1026/// default argument or an in-class initializer for a non-static data member).
Richard Smith95e1fb02014-08-27 03:23:12 +00001027///
1028/// Returns \c true if we reached the end of something initializer-shaped,
1029/// \c false if we bailed out.
Richard Smith1fff95c2013-09-12 23:28:08 +00001030bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
1031 CachedInitKind CIK) {
1032 // We always want this function to consume at least one token if not at EOF.
Richard Smith95e1fb02014-08-27 03:23:12 +00001033 bool IsFirstToken = true;
Richard Smith1fff95c2013-09-12 23:28:08 +00001034
1035 // Number of possible unclosed <s we've seen so far. These might be templates,
1036 // and might not, but if there were none of them (or we know for sure that
1037 // we're within a template), we can avoid a tentative parse.
1038 unsigned AngleCount = 0;
1039 unsigned KnownTemplateCount = 0;
1040
1041 while (1) {
1042 switch (Tok.getKind()) {
1043 case tok::comma:
1044 // If we might be in a template, perform a tentative parse to check.
1045 if (!AngleCount)
1046 // Not a template argument: this is the end of the initializer.
1047 return true;
1048 if (KnownTemplateCount)
1049 goto consume_token;
1050
1051 // We hit a comma inside angle brackets. This is the hard case. The
1052 // rule we follow is:
1053 // * For a default argument, if the tokens after the comma form a
1054 // syntactically-valid parameter-declaration-clause, in which each
1055 // parameter has an initializer, then this comma ends the default
1056 // argument.
1057 // * For a default initializer, if the tokens after the comma form a
1058 // syntactically-valid init-declarator-list, then this comma ends
1059 // the default initializer.
1060 {
1061 UnannotatedTentativeParsingAction PA(*this,
1062 CIK == CIK_DefaultInitializer
1063 ? tok::semi : tok::r_paren);
1064 Sema::TentativeAnalysisScope Scope(Actions);
1065
Richard Smithee390432014-05-16 01:56:53 +00001066 TPResult Result = TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001067 ConsumeToken();
1068 switch (CIK) {
1069 case CIK_DefaultInitializer:
1070 Result = TryParseInitDeclaratorList();
1071 // If we parsed a complete, ambiguous init-declarator-list, this
1072 // is only syntactically-valid if it's followed by a semicolon.
Richard Smithee390432014-05-16 01:56:53 +00001073 if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi))
1074 Result = TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001075 break;
1076
1077 case CIK_DefaultArgument:
1078 bool InvalidAsDeclaration = false;
1079 Result = TryParseParameterDeclarationClause(
Nico Weberc29c4832014-12-28 23:24:02 +00001080 &InvalidAsDeclaration, /*VersusTemplateArgument=*/true);
Richard Smith1fff95c2013-09-12 23:28:08 +00001081 // If this is an expression or a declaration with a missing
1082 // 'typename', assume it's not a declaration.
Richard Smithee390432014-05-16 01:56:53 +00001083 if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1084 Result = TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001085 break;
1086 }
1087
1088 // If what follows could be a declaration, it is a declaration.
Richard Smithee390432014-05-16 01:56:53 +00001089 if (Result != TPResult::False && Result != TPResult::Error) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001090 PA.Revert();
1091 return true;
1092 }
1093
1094 // In the uncommon case that we decide the following tokens are part
1095 // of a template argument, revert any annotations we've performed in
1096 // those tokens. We're not going to look them up until we've parsed
1097 // the rest of the class, and that might add more declarations.
1098 PA.RevertAnnotations();
1099 }
1100
1101 // Keep going. We know we're inside a template argument list now.
1102 ++KnownTemplateCount;
1103 goto consume_token;
1104
1105 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001106 case tok::annot_module_begin:
1107 case tok::annot_module_end:
1108 case tok::annot_module_include:
Richard Smith1fff95c2013-09-12 23:28:08 +00001109 // Ran out of tokens.
1110 return false;
1111
1112 case tok::less:
1113 // FIXME: A '<' can only start a template-id if it's preceded by an
1114 // identifier, an operator-function-id, or a literal-operator-id.
1115 ++AngleCount;
1116 goto consume_token;
1117
1118 case tok::question:
1119 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1120 // that is *never* the end of the initializer. Skip to the ':'.
1121 if (!ConsumeAndStoreConditional(Toks))
1122 return false;
1123 break;
1124
1125 case tok::greatergreatergreater:
1126 if (!getLangOpts().CPlusPlus11)
1127 goto consume_token;
1128 if (AngleCount) --AngleCount;
1129 if (KnownTemplateCount) --KnownTemplateCount;
1130 // Fall through.
1131 case tok::greatergreater:
1132 if (!getLangOpts().CPlusPlus11)
1133 goto consume_token;
1134 if (AngleCount) --AngleCount;
1135 if (KnownTemplateCount) --KnownTemplateCount;
1136 // Fall through.
1137 case tok::greater:
1138 if (AngleCount) --AngleCount;
1139 if (KnownTemplateCount) --KnownTemplateCount;
1140 goto consume_token;
1141
1142 case tok::kw_template:
1143 // 'template' identifier '<' is known to start a template argument list,
1144 // and can be used to disambiguate the parse.
1145 // FIXME: Support all forms of 'template' unqualified-id '<'.
1146 Toks.push_back(Tok);
1147 ConsumeToken();
1148 if (Tok.is(tok::identifier)) {
1149 Toks.push_back(Tok);
1150 ConsumeToken();
1151 if (Tok.is(tok::less)) {
Richard Smith93033572014-07-27 05:38:12 +00001152 ++AngleCount;
Richard Smith1fff95c2013-09-12 23:28:08 +00001153 ++KnownTemplateCount;
1154 Toks.push_back(Tok);
1155 ConsumeToken();
1156 }
1157 }
1158 break;
1159
1160 case tok::kw_operator:
1161 // If 'operator' precedes other punctuation, that punctuation loses
1162 // its special behavior.
1163 Toks.push_back(Tok);
1164 ConsumeToken();
1165 switch (Tok.getKind()) {
1166 case tok::comma:
1167 case tok::greatergreatergreater:
1168 case tok::greatergreater:
1169 case tok::greater:
1170 case tok::less:
1171 Toks.push_back(Tok);
1172 ConsumeToken();
1173 break;
1174 default:
1175 break;
1176 }
1177 break;
1178
1179 case tok::l_paren:
1180 // Recursively consume properly-nested parens.
1181 Toks.push_back(Tok);
1182 ConsumeParen();
1183 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1184 break;
1185 case tok::l_square:
1186 // Recursively consume properly-nested square brackets.
1187 Toks.push_back(Tok);
1188 ConsumeBracket();
1189 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
1190 break;
1191 case tok::l_brace:
1192 // Recursively consume properly-nested braces.
1193 Toks.push_back(Tok);
1194 ConsumeBrace();
1195 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1196 break;
1197
1198 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1199 // Since the user wasn't looking for this token (if they were, it would
1200 // already be handled), this isn't balanced. If there is a LHS token at a
1201 // higher level, we will assume that this matches the unbalanced token
Richard Smith95e1fb02014-08-27 03:23:12 +00001202 // and return it. Otherwise, this is a spurious RHS token, which we
1203 // consume and pass on to downstream code to diagnose.
Richard Smith1fff95c2013-09-12 23:28:08 +00001204 case tok::r_paren:
1205 if (CIK == CIK_DefaultArgument)
1206 return true; // End of the default argument.
Richard Smith95e1fb02014-08-27 03:23:12 +00001207 if (ParenCount && !IsFirstToken)
1208 return false;
1209 Toks.push_back(Tok);
1210 ConsumeParen();
1211 continue;
Richard Smith1fff95c2013-09-12 23:28:08 +00001212 case tok::r_square:
Richard Smith95e1fb02014-08-27 03:23:12 +00001213 if (BracketCount && !IsFirstToken)
1214 return false;
1215 Toks.push_back(Tok);
1216 ConsumeBracket();
1217 continue;
Richard Smith1fff95c2013-09-12 23:28:08 +00001218 case tok::r_brace:
Richard Smith95e1fb02014-08-27 03:23:12 +00001219 if (BraceCount && !IsFirstToken)
1220 return false;
1221 Toks.push_back(Tok);
1222 ConsumeBrace();
1223 continue;
Richard Smith1fff95c2013-09-12 23:28:08 +00001224
1225 case tok::code_completion:
1226 Toks.push_back(Tok);
1227 ConsumeCodeCompletionToken();
1228 break;
1229
1230 case tok::string_literal:
1231 case tok::wide_string_literal:
1232 case tok::utf8_string_literal:
1233 case tok::utf16_string_literal:
1234 case tok::utf32_string_literal:
1235 Toks.push_back(Tok);
1236 ConsumeStringToken();
1237 break;
1238 case tok::semi:
1239 if (CIK == CIK_DefaultInitializer)
1240 return true; // End of the default initializer.
1241 // FALL THROUGH.
1242 default:
1243 consume_token:
1244 Toks.push_back(Tok);
1245 ConsumeToken();
1246 break;
1247 }
Richard Smith95e1fb02014-08-27 03:23:12 +00001248 IsFirstToken = false;
Richard Smith1fff95c2013-09-12 23:28:08 +00001249 }
1250}