blob: f2e552a3ce1462212212060aaeefb639826f7e04 [file] [log] [blame]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001//===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements parsing for C++ class inline methods.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/AST/DeclTemplate.h"
15#include "clang/Parse/ParseDiagnostic.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000016#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/Scope.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.
Erich Keanec480f302018-07-12 21:09:05 +000024NamedDecl *Parser::ParseCXXInlineMethodDef(
25 AccessSpecifier AS, ParsedAttributes &AccessAttrs, ParsingDeclarator &D,
26 const ParsedTemplateInfo &TemplateInfo, const VirtSpecifiers &VS,
27 SourceLocation PureSpecLoc) {
Abramo Bagnara924a8f32010-12-10 16:29:40 +000028 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
Daniel Marjamakie59f8d72015-06-18 10:59:26 +000029 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
Alexis Hunt5a7fa252011-05-12 06:15:49 +000030 "Current token not a '{', ':', '=', or 'try'!");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000031
Benjamin Kramercc4c49d2012-08-23 23:38:35 +000032 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +000033 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
34 : nullptr,
Nico Weber77c76c52014-05-10 17:43:15 +000035 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
Alexis Hunt1deb9722010-04-14 23:07:37 +000036
Rafael Espindolac2453dd2013-01-08 21:00:12 +000037 NamedDecl *FnD;
John McCall07e91c02009-08-06 02:15:43 +000038 if (D.getDeclSpec().isFriendSpecified())
Kaelyn Uhrain4dc695d2011-10-11 00:28:45 +000039 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
Benjamin Kramer62b95d82012-08-23 21:35:17 +000040 TemplateParams);
Douglas Gregor728d00b2011-10-10 14:49:18 +000041 else {
Douglas Gregor0be31a22010-07-02 17:43:08 +000042 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
Craig Topper161e4db2014-05-21 06:02:52 +000043 TemplateParams, nullptr,
Richard Smith2b013182012-06-10 03:12:00 +000044 VS, ICIS_NoInit);
Douglas Gregor728d00b2011-10-10 14:49:18 +000045 if (FnD) {
Richard Smithf8a75c32013-08-29 00:47:48 +000046 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
Richard Smith9ba0fec2015-06-30 01:28:56 +000047 if (PureSpecLoc.isValid())
48 Actions.ActOnPureSpecifier(FnD, PureSpecLoc);
Douglas Gregor728d00b2011-10-10 14:49:18 +000049 }
Nico Weber24b2a822011-01-28 06:07:34 +000050 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +000051
Reid Klecknerbde5ede2016-02-19 01:15:08 +000052 if (FnD)
53 HandleMemberFunctionDeclDelays(D, FnD);
Eli Friedman3af2a772009-07-22 21:45:50 +000054
John McCallc1465822011-02-14 07:13:47 +000055 D.complete(FnD);
56
Alp Tokera3ebe6e2013-12-17 14:12:37 +000057 if (TryConsumeToken(tok::equal)) {
Richard Smith1c704732011-11-10 09:08:44 +000058 if (!FnD) {
59 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +000060 return nullptr;
Richard Smith1c704732011-11-10 09:08:44 +000061 }
62
Alexis Hunt5a7fa252011-05-12 06:15:49 +000063 bool Delete = false;
64 SourceLocation KWLoc;
Eli Bendersky5f4d76e2015-03-23 21:43:28 +000065 SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
Alp Tokera3ebe6e2013-12-17 14:12:37 +000066 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
67 Diag(KWLoc, getLangOpts().CPlusPlus11
Craig Topper54a6a682015-11-14 18:16:08 +000068 ? diag::warn_cxx98_compat_defaulted_deleted_function
69 : diag::ext_defaulted_deleted_function)
70 << 1 /* deleted */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +000071 Actions.SetDeclDeleted(FnD, KWLoc);
72 Delete = true;
Eli Bendersky5f4d76e2015-03-23 21:43:28 +000073 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
74 DeclAsFunction->setRangeEnd(KWEndLoc);
75 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +000076 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
77 Diag(KWLoc, getLangOpts().CPlusPlus11
Craig Topper54a6a682015-11-14 18:16:08 +000078 ? diag::warn_cxx98_compat_defaulted_deleted_function
79 : diag::ext_defaulted_deleted_function)
80 << 0 /* defaulted */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +000081 Actions.SetDeclDefaulted(FnD, KWLoc);
Eli Bendersky5f4d76e2015-03-23 21:43:28 +000082 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
83 DeclAsFunction->setRangeEnd(KWEndLoc);
84 }
Alexis Hunt5a7fa252011-05-12 06:15:49 +000085 } else {
86 llvm_unreachable("function definition after = not 'delete' or 'default'");
87 }
88
89 if (Tok.is(tok::comma)) {
90 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
91 << Delete;
92 SkipUntil(tok::semi);
Alp Toker383d2c42014-01-01 03:08:43 +000093 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
94 Delete ? "delete" : "default")) {
95 SkipUntil(tok::semi);
Alexis Hunt5a7fa252011-05-12 06:15:49 +000096 }
97
98 return FnD;
99 }
Eli Bendersky41842222015-03-23 23:49:41 +0000100
Olivier Goffartf9e890c2016-06-16 21:40:06 +0000101 if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) &&
102 trySkippingFunctionBody()) {
103 Actions.ActOnSkippedFunctionBody(FnD);
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.
David Majnemer90b17292013-09-14 05:46:42 +0000110 if (getLangOpts().DelayedTemplateParsing &&
Eli Bendersky41842222015-03-23 23:49:41 +0000111 D.getFunctionDefinitionKind() == FDK_Definition &&
Alp Tokera2794f92014-01-22 07:29:52 +0000112 !D.getDeclSpec().isConstexprSpecified() &&
113 !(FnD && FnD->getAsFunction() &&
Alp Toker314cc812014-01-25 16:55:45 +0000114 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
Francois Pichet1c229c02011-04-22 22:18:13 +0000115 ((Actions.CurContext->isDependentContext() ||
David Majnemer90b17292013-09-14 05:46:42 +0000116 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
117 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
118 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
Francois Pichet1c229c02011-04-22 22:18:13 +0000119
Richard Smithe40f2ba2013-08-07 21:41:30 +0000120 CachedTokens Toks;
121 LexTemplateFunctionForLateParsing(Toks);
Francois Pichet1c229c02011-04-22 22:18:13 +0000122
Richard Smithe40f2ba2013-08-07 21:41:30 +0000123 if (FnD) {
Alp Tokera2794f92014-01-22 07:29:52 +0000124 FunctionDecl *FD = FnD->getAsFunction();
Chandler Carruthbc0f9ae2011-04-25 07:09:43 +0000125 Actions.CheckForFunctionRedefinition(FD);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000126 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
Francois Pichet1c229c02011-04-22 22:18:13 +0000127 }
128
129 return FnD;
130 }
131
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000132 // Consume the tokens and store them for later parsing.
133
Douglas Gregorefc46952010-10-12 16:25:54 +0000134 LexedMethod* LM = new LexedMethod(this, FnD);
135 getCurrentClass().LateParsedDeclarations.push_back(LM);
136 LM->TemplateScope = getCurScope()->isTemplateParamScope();
137 CachedTokens &Toks = LM->Toks;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000138
Sebastian Redla7b98a72009-04-26 20:35:05 +0000139 tok::TokenKind kind = Tok.getKind();
Sebastian Redl0d164012011-09-30 08:32:17 +0000140 // Consume everything up to (and including) the left brace of the
141 // function body.
142 if (ConsumeAndStoreFunctionPrologue(Toks)) {
143 // We didn't find the left-brace we expected after the
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000144 // constructor initializer; we already printed an error, and it's likely
145 // impossible to recover, so don't try to parse this method later.
Richard Smithcde3fd82013-07-04 00:13:48 +0000146 // Skip over the rest of the decl and back to somewhere that looks
147 // reasonable.
148 SkipMalformedDecl();
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000149 delete getCurrentClass().LateParsedDeclarations.back();
150 getCurrentClass().LateParsedDeclarations.pop_back();
151 return FnD;
Douglas Gregore8381c02008-11-05 04:29:56 +0000152 } else {
Sebastian Redl0d164012011-09-30 08:32:17 +0000153 // Consume everything up to (and including) the matching right brace.
154 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Douglas Gregore8381c02008-11-05 04:29:56 +0000155 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000156
Sebastian Redla7b98a72009-04-26 20:35:05 +0000157 // If we're in a function-try-block, we need to store all the catch blocks.
158 if (kind == tok::kw_try) {
159 while (Tok.is(tok::kw_catch)) {
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000160 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
161 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Sebastian Redla7b98a72009-04-26 20:35:05 +0000162 }
163 }
164
Stephen Lin9354fc52013-06-23 07:37:13 +0000165 if (FnD) {
Richard Smith6c7161162017-08-12 01:46:03 +0000166 FunctionDecl *FD = FnD->getAsFunction();
167 // Track that this function will eventually have a body; Sema needs
168 // to know this.
169 Actions.CheckForFunctionRedefinition(FD);
170 FD->setWillHaveBody(true);
Stephen Lin9354fc52013-06-23 07:37:13 +0000171 } else {
Douglas Gregor6ca64102011-04-14 23:19:27 +0000172 // If semantic analysis could not build a function declaration,
173 // just throw away the late-parsed declaration.
174 delete getCurrentClass().LateParsedDeclarations.back();
175 getCurrentClass().LateParsedDeclarations.pop_back();
176 }
177
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000178 return FnD;
179}
180
Richard Smith938f40b2011-06-11 17:19:42 +0000181/// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
182/// specified Declarator is a well formed C++ non-static data member
183/// declaration. Now lex its initializer and store its tokens for parsing
184/// after the class is complete.
185void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000186 assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
Richard Smith938f40b2011-06-11 17:19:42 +0000187 "Current token not a '{' or '='!");
188
189 LateParsedMemberInitializer *MI =
190 new LateParsedMemberInitializer(this, VarD);
191 getCurrentClass().LateParsedDeclarations.push_back(MI);
192 CachedTokens &Toks = MI->Toks;
193
194 tok::TokenKind kind = Tok.getKind();
195 if (kind == tok::equal) {
196 Toks.push_back(Tok);
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000197 ConsumeToken();
Richard Smith938f40b2011-06-11 17:19:42 +0000198 }
199
200 if (kind == tok::l_brace) {
201 // Begin by storing the '{' token.
202 Toks.push_back(Tok);
203 ConsumeBrace();
204
205 // Consume everything up to (and including) the matching right brace.
206 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
207 } else {
208 // Consume everything up to (but excluding) the comma or semicolon.
Richard Smith1fff95c2013-09-12 23:28:08 +0000209 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
Richard Smith938f40b2011-06-11 17:19:42 +0000210 }
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());
David Majnemerce1d3012014-12-19 02:13:56 +0000218 Eof.setEofData(VarD);
Richard Smith938f40b2011-06-11 17:19:42 +0000219 Toks.push_back(Eof);
220}
221
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000222Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
Douglas Gregorefc46952010-10-12 16:25:54 +0000223void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
Richard Smith938f40b2011-06-11 17:19:42 +0000224void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
Douglas Gregorefc46952010-10-12 16:25:54 +0000225void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
226
227Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
228 : Self(P), Class(C) {}
229
230Parser::LateParsedClass::~LateParsedClass() {
231 Self->DeallocateParsedClasses(Class);
232}
233
234void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
235 Self->ParseLexedMethodDeclarations(*Class);
236}
237
Richard Smith938f40b2011-06-11 17:19:42 +0000238void Parser::LateParsedClass::ParseLexedMemberInitializers() {
239 Self->ParseLexedMemberInitializers(*Class);
240}
241
Douglas Gregorefc46952010-10-12 16:25:54 +0000242void Parser::LateParsedClass::ParseLexedMethodDefs() {
243 Self->ParseLexedMethodDefs(*Class);
244}
245
246void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
247 Self->ParseLexedMethodDeclaration(*this);
248}
249
250void Parser::LexedMethod::ParseLexedMethodDefs() {
251 Self->ParseLexedMethodDef(*this);
252}
253
Richard Smith938f40b2011-06-11 17:19:42 +0000254void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
255 Self->ParseLexedMemberInitializer(*this);
256}
257
Douglas Gregor4d87df52008-12-16 21:30:33 +0000258/// ParseLexedMethodDeclarations - We finished parsing the member
259/// specification of a top (non-nested) C++ class. Now go over the
260/// stack of method declarations with some parts for which parsing was
261/// delayed (such as default arguments) and parse them.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000262void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
263 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Nico Weber77c76c52014-05-10 17:43:15 +0000264 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
265 HasTemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000266 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
267 if (HasTemplateScope) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000268 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Richard Smithc8378952013-04-29 11:55:38 +0000269 ++CurTemplateDepthTracker;
270 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000271
John McCall6df5fef2009-12-19 10:49:29 +0000272 // The current scope is still active if we're the top-level class.
273 // Otherwise we'll need to push and enter a new scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000274 bool HasClassScope = !Class.TopLevelClass;
Alexis Hunt1deb9722010-04-14 23:07:37 +0000275 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
276 HasClassScope);
John McCall6df5fef2009-12-19 10:49:29 +0000277 if (HasClassScope)
Nico Weber77c76c52014-05-10 17:43:15 +0000278 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
279 Class.TagOrTemplate);
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000280
Douglas Gregorefc46952010-10-12 16:25:54 +0000281 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
282 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
Douglas Gregor4d87df52008-12-16 21:30:33 +0000283 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000284
John McCall6df5fef2009-12-19 10:49:29 +0000285 if (HasClassScope)
Nico Weber77c76c52014-05-10 17:43:15 +0000286 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
287 Class.TagOrTemplate);
Douglas Gregor4d87df52008-12-16 21:30:33 +0000288}
289
Douglas Gregorefc46952010-10-12 16:25:54 +0000290void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
291 // If this is a member template, introduce the template parameter scope.
292 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000293 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
294 if (LM.TemplateScope) {
Douglas Gregorefc46952010-10-12 16:25:54 +0000295 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
Richard Smithc8378952013-04-29 11:55:38 +0000296 ++CurTemplateDepthTracker;
297 }
Douglas Gregorefc46952010-10-12 16:25:54 +0000298 // Start the delayed C++ method declaration
299 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
300
301 // Introduce the parameters into scope and parse their default
302 // arguments.
Richard Smithe233fbf2013-01-28 22:42:45 +0000303 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
304 Scope::FunctionDeclarationScope | Scope::DeclScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000305 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
Nathan Sidwell5bb231c2015-02-19 14:03:22 +0000306 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
Douglas Gregorefc46952010-10-12 16:25:54 +0000307 // Introduce the parameter into scope.
Nathan Sidwell5bb231c2015-02-19 14:03:22 +0000308 bool HasUnparsed = Param->hasUnparsedDefaultArg();
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000309 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
Malcolm Parsonsff0382c2016-11-17 17:52:58 +0000310 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
311 if (Toks) {
Richard Smithbf5bcf22018-06-26 23:20:26 +0000312 ParenBraceBracketBalancer BalancerRAIIObj(*this);
313
David Majnemer7cceba52015-01-13 04:20:57 +0000314 // Mark the end of the default argument so that we know when to stop when
315 // we parse it later on.
316 Token LastDefaultArgToken = Toks->back();
317 Token DefArgEnd;
318 DefArgEnd.startToken();
319 DefArgEnd.setKind(tok::eof);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000320 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000321 DefArgEnd.setEofData(Param);
David Majnemer7cceba52015-01-13 04:20:57 +0000322 Toks->push_back(DefArgEnd);
Douglas Gregorefc46952010-10-12 16:25:54 +0000323
324 // Parse the default argument from its saved token stream.
325 Toks->push_back(Tok); // So that the current token doesn't get lost
Ilya Biryukov929af672019-05-17 09:32:05 +0000326 PP.EnterTokenStream(*Toks, true, /*IsReinject*/ true);
Douglas Gregorefc46952010-10-12 16:25:54 +0000327
328 // Consume the previously-pushed token.
329 ConsumeAnyToken();
330
331 // Consume the '='.
332 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
333 SourceLocation EqualLoc = ConsumeToken();
334
335 // The argument isn't actually potentially evaluated unless it is
336 // used.
Faisal Valid143a0c2017-04-01 21:30:49 +0000337 EnterExpressionEvaluationContext Eval(
338 Actions,
339 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param);
Douglas Gregorefc46952010-10-12 16:25:54 +0000340
Sebastian Redldb63af22012-03-14 15:54:00 +0000341 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000342 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl6db0b1b2012-03-20 21:24:03 +0000343 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +0000344 DefArgResult = ParseBraceInitializer();
Sebastian Redl6db0b1b2012-03-20 21:24:03 +0000345 } else
Sebastian Redldb63af22012-03-14 15:54:00 +0000346 DefArgResult = ParseAssignmentExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000347 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
David Majnemerbba12342015-01-12 06:51:15 +0000348 if (DefArgResult.isInvalid()) {
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000349 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
David Majnemerbba12342015-01-12 06:51:15 +0000350 } else {
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000351 if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
Richard Smith1fff95c2013-09-12 23:28:08 +0000352 // The last two tokens are the terminator and the saved value of
353 // Tok; the last token in the default argument is the one before
354 // those.
355 assert(Toks->size() >= 3 && "expected a token in default arg");
356 Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
357 << SourceRange(Tok.getLocation(),
358 (*Toks)[Toks->size() - 3].getLocation());
359 }
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000360 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000361 DefArgResult.get());
Douglas Gregorefc46952010-10-12 16:25:54 +0000362 }
363
Douglas Gregorefc46952010-10-12 16:25:54 +0000364 // There could be leftover tokens (e.g. because of an error).
David Majnemer7cceba52015-01-13 04:20:57 +0000365 // Skip through until we reach the 'end of default argument' token.
366 while (Tok.isNot(tok::eof))
Douglas Gregorefc46952010-10-12 16:25:54 +0000367 ConsumeAnyToken();
David Majnemer7cceba52015-01-13 04:20:57 +0000368
Nathan Sidwell55d53fe2015-01-30 14:21:35 +0000369 if (Tok.is(tok::eof) && Tok.getEofData() == Param)
David Majnemer7cceba52015-01-13 04:20:57 +0000370 ConsumeAnyToken();
Nathan Sidwell5bb231c2015-02-19 14:03:22 +0000371 } else if (HasUnparsed) {
372 assert(Param->hasInheritedDefaultArg());
373 FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
374 ParmVarDecl *OldParam = Old->getParamDecl(I);
375 assert (!OldParam->hasUnparsedDefaultArg());
376 if (OldParam->hasUninstantiatedDefaultArg())
377 Param->setUninstantiatedDefaultArg(
David Majnemercef7d372016-06-10 20:21:15 +0000378 OldParam->getUninstantiatedDefaultArg());
Nathan Sidwell5bb231c2015-02-19 14:03:22 +0000379 else
380 Param->setDefaultArg(OldParam->getInit());
Douglas Gregorefc46952010-10-12 16:25:54 +0000381 }
382 }
Douglas Gregor433e0532012-04-16 18:27:27 +0000383
Richard Smith0b3a4622014-11-13 20:01:57 +0000384 // Parse a delayed exception-specification, if there is one.
385 if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
Richard Smithbf5bcf22018-06-26 23:20:26 +0000386 ParenBraceBracketBalancer BalancerRAIIObj(*this);
387
David Majnemer7cceba52015-01-13 04:20:57 +0000388 // Add the 'stop' token.
389 Token LastExceptionSpecToken = Toks->back();
390 Token ExceptionSpecEnd;
391 ExceptionSpecEnd.startToken();
392 ExceptionSpecEnd.setKind(tok::eof);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000393 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
David Majnemer7cceba52015-01-13 04:20:57 +0000394 ExceptionSpecEnd.setEofData(LM.Method);
395 Toks->push_back(ExceptionSpecEnd);
Richard Smith0b3a4622014-11-13 20:01:57 +0000396
397 // Parse the default argument from its saved token stream.
398 Toks->push_back(Tok); // So that the current token doesn't get lost
Ilya Biryukov929af672019-05-17 09:32:05 +0000399 PP.EnterTokenStream(*Toks, true, /*IsReinject*/true);
Richard Smith0b3a4622014-11-13 20:01:57 +0000400
401 // Consume the previously-pushed token.
402 ConsumeAnyToken();
403
404 // C++11 [expr.prim.general]p3:
405 // If a declaration declares a member function or member function
406 // template of a class X, the expression this is a prvalue of type
407 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
408 // and the end of the function-definition, member-declarator, or
409 // declarator.
410 CXXMethodDecl *Method;
411 if (FunctionTemplateDecl *FunTmpl
412 = dyn_cast<FunctionTemplateDecl>(LM.Method))
413 Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
414 else
415 Method = cast<CXXMethodDecl>(LM.Method);
416
417 Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
Anastasia Stulovac61eaa52019-01-28 11:37:49 +0000418 Method->getMethodQualifiers(),
Richard Smith0b3a4622014-11-13 20:01:57 +0000419 getLangOpts().CPlusPlus11);
420
421 // Parse the exception-specification.
422 SourceRange SpecificationRange;
423 SmallVector<ParsedType, 4> DynamicExceptions;
424 SmallVector<SourceRange, 4> DynamicExceptionRanges;
425 ExprResult NoexceptExpr;
426 CachedTokens *ExceptionSpecTokens;
427
428 ExceptionSpecificationType EST
429 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
430 DynamicExceptions,
431 DynamicExceptionRanges, NoexceptExpr,
432 ExceptionSpecTokens);
433
David Majnemer7cceba52015-01-13 04:20:57 +0000434 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
Richard Smith0b3a4622014-11-13 20:01:57 +0000435 Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
436
437 // Attach the exception-specification to the method.
Richard Smith3ef3e892014-11-20 22:32:11 +0000438 Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
439 SpecificationRange,
440 DynamicExceptions,
441 DynamicExceptionRanges,
442 NoexceptExpr.isUsable()?
443 NoexceptExpr.get() : nullptr);
Richard Smith0b3a4622014-11-13 20:01:57 +0000444
Richard Smith0b3a4622014-11-13 20:01:57 +0000445 // There could be leftover tokens (e.g. because of an error).
446 // Skip through until we reach the original token position.
David Majnemer7cceba52015-01-13 04:20:57 +0000447 while (Tok.isNot(tok::eof))
Richard Smith0b3a4622014-11-13 20:01:57 +0000448 ConsumeAnyToken();
David Majnemer7cceba52015-01-13 04:20:57 +0000449
450 // Clean up the remaining EOF token.
451 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
452 ConsumeAnyToken();
Richard Smith0b3a4622014-11-13 20:01:57 +0000453
454 delete Toks;
455 LM.ExceptionSpecTokens = nullptr;
456 }
457
Douglas Gregorefc46952010-10-12 16:25:54 +0000458 PrototypeScope.Exit();
459
460 // Finish the delayed C++ method declaration.
461 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
462}
463
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000464/// ParseLexedMethodDefs - We finished parsing the member specification of a top
465/// (non-nested) C++ class. Now go over the stack of lexed methods that were
466/// collected during its parsing and parse them all.
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000467void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
468 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
Douglas Gregorefc46952010-10-12 16:25:54 +0000469 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000470 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
471 if (HasTemplateScope) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000472 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Richard Smithc8378952013-04-29 11:55:38 +0000473 ++CurTemplateDepthTracker;
474 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +0000475 bool HasClassScope = !Class.TopLevelClass;
476 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
477 HasClassScope);
478
Douglas Gregorefc46952010-10-12 16:25:54 +0000479 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
480 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
481 }
482}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000483
Douglas Gregorefc46952010-10-12 16:25:54 +0000484void Parser::ParseLexedMethodDef(LexedMethod &LM) {
485 // If this is a member template, introduce the template parameter scope.
486 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000487 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
488 if (LM.TemplateScope) {
Douglas Gregorefc46952010-10-12 16:25:54 +0000489 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
Richard Smithc8378952013-04-29 11:55:38 +0000490 ++CurTemplateDepthTracker;
491 }
Argyrios Kyrtzidis02041972010-03-31 00:38:09 +0000492
Richard Smithbf5bcf22018-06-26 23:20:26 +0000493 ParenBraceBracketBalancer BalancerRAIIObj(*this);
494
Douglas Gregorefc46952010-10-12 16:25:54 +0000495 assert(!LM.Toks.empty() && "Empty body!");
David Majnemera7ea1b12015-01-13 05:06:20 +0000496 Token LastBodyToken = LM.Toks.back();
497 Token BodyEnd;
498 BodyEnd.startToken();
499 BodyEnd.setKind(tok::eof);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000500 BodyEnd.setLocation(LastBodyToken.getEndLoc());
David Majnemera7ea1b12015-01-13 05:06:20 +0000501 BodyEnd.setEofData(LM.D);
502 LM.Toks.push_back(BodyEnd);
Douglas Gregorefc46952010-10-12 16:25:54 +0000503 // Append the current token at the end of the new token stream so that it
504 // doesn't get lost.
505 LM.Toks.push_back(Tok);
Ilya Biryukov929af672019-05-17 09:32:05 +0000506 PP.EnterTokenStream(LM.Toks, true, /*IsReinject*/true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000507
Douglas Gregorefc46952010-10-12 16:25:54 +0000508 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +0000509 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000510 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
Douglas Gregorefc46952010-10-12 16:25:54 +0000511 && "Inline method not starting with '{', ':' or 'try'");
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000512
Douglas Gregorefc46952010-10-12 16:25:54 +0000513 // Parse the method body. Function body parsing code is similar enough
514 // to be re-used for method bodies as well.
Momchil Velikov57c681f2017-08-10 15:43:06 +0000515 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
516 Scope::CompoundStmtScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000517 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000518
Douglas Gregorefc46952010-10-12 16:25:54 +0000519 if (Tok.is(tok::kw_try)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000520 ParseFunctionTryBlock(LM.D, FnScope);
David Majnemera7ea1b12015-01-13 05:06:20 +0000521
522 while (Tok.isNot(tok::eof))
523 ConsumeAnyToken();
524
525 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
Douglas Gregorefc46952010-10-12 16:25:54 +0000526 ConsumeAnyToken();
527 return;
528 }
529 if (Tok.is(tok::colon)) {
530 ParseConstructorInitializer(LM.D);
531
532 // Error recovery.
533 if (!Tok.is(tok::l_brace)) {
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000534 FnScope.Exit();
Craig Topper161e4db2014-05-21 06:02:52 +0000535 Actions.ActOnFinishFunctionBody(LM.D, nullptr);
David Majnemera7ea1b12015-01-13 05:06:20 +0000536
537 while (Tok.isNot(tok::eof))
538 ConsumeAnyToken();
539
540 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
Matt Beaumont-Gayd0457922011-09-23 22:39:23 +0000541 ConsumeAnyToken();
Douglas Gregorefc46952010-10-12 16:25:54 +0000542 return;
543 }
544 } else
545 Actions.ActOnDefaultCtorInitializers(LM.D);
546
Richard Smithc8378952013-04-29 11:55:38 +0000547 assert((Actions.getDiagnostics().hasErrorOccurred() ||
548 !isa<FunctionTemplateDecl>(LM.D) ||
549 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
550 < TemplateParameterDepth) &&
551 "TemplateParameterDepth should be greater than the depth of "
552 "current template being instantiated!");
553
Douglas Gregora0ff0c32011-03-16 17:05:57 +0000554 ParseFunctionStatementBody(LM.D, FnScope);
Douglas Gregorefc46952010-10-12 16:25:54 +0000555
David Majnemera7ea1b12015-01-13 05:06:20 +0000556 while (Tok.isNot(tok::eof))
557 ConsumeAnyToken();
558
559 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
560 ConsumeAnyToken();
Hans Wennborga926d842014-05-23 20:37:38 +0000561
Stephan Bergmann17d7d142016-03-30 06:27:31 +0000562 if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
563 if (isa<CXXMethodDecl>(FD) ||
564 FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend))
565 Actions.ActOnFinishInlineFunctionDef(FD);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000566}
567
Richard Smith938f40b2011-06-11 17:19:42 +0000568/// ParseLexedMemberInitializers - We finished parsing the member specification
569/// of a top (non-nested) C++ class. Now go over the stack of lexed data member
570/// initializers that were collected during its parsing and parse them all.
571void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
572 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
573 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
574 HasTemplateScope);
Richard Smithc8378952013-04-29 11:55:38 +0000575 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
576 if (HasTemplateScope) {
Richard Smith938f40b2011-06-11 17:19:42 +0000577 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
Richard Smithc8378952013-04-29 11:55:38 +0000578 ++CurTemplateDepthTracker;
579 }
Douglas Gregor3024f072012-04-16 07:05:22 +0000580 // Set or update the scope flags.
Richard Smith938f40b2011-06-11 17:19:42 +0000581 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000582 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Richard Smith938f40b2011-06-11 17:19:42 +0000583 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
584 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
585
586 if (!AlreadyHasClassScope)
587 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
588 Class.TagOrTemplate);
589
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000590 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000591 // C++11 [expr.prim.general]p4:
Fangrui Song6907ce22018-07-30 19:24:48 +0000592 // Otherwise, if a member-declarator declares a non-static data member
Douglas Gregor3024f072012-04-16 07:05:22 +0000593 // (9.2) of a class X, the expression this is a prvalue of type "pointer
Fangrui Song6907ce22018-07-30 19:24:48 +0000594 // to X" within the optional brace-or-equal-initializer. It shall not
Douglas Gregor3024f072012-04-16 07:05:22 +0000595 // appear elsewhere in the member-declarator.
596 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000597 Qualifiers());
Richard Smith938f40b2011-06-11 17:19:42 +0000598
Douglas Gregor3024f072012-04-16 07:05:22 +0000599 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
600 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
601 }
602 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000603
Richard Smith938f40b2011-06-11 17:19:42 +0000604 if (!AlreadyHasClassScope)
605 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
606 Class.TagOrTemplate);
607
608 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
609}
610
611void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
Richard Smith1a526fd2011-09-29 19:42:27 +0000612 if (!MI.Field || MI.Field->isInvalidDecl())
Richard Smith938f40b2011-06-11 17:19:42 +0000613 return;
614
Richard Smithbf5bcf22018-06-26 23:20:26 +0000615 ParenBraceBracketBalancer BalancerRAIIObj(*this);
616
Richard Smith938f40b2011-06-11 17:19:42 +0000617 // Append the current token at the end of the new token stream so that it
618 // doesn't get lost.
619 MI.Toks.push_back(Tok);
Ilya Biryukov929af672019-05-17 09:32:05 +0000620 PP.EnterTokenStream(MI.Toks, true, /*IsReinject*/true);
Richard Smith938f40b2011-06-11 17:19:42 +0000621
622 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +0000623 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Richard Smith938f40b2011-06-11 17:19:42 +0000624
625 SourceLocation EqualLoc;
Richard Smith2b013182012-06-10 03:12:00 +0000626
Richard Smith74108172014-01-17 03:11:34 +0000627 Actions.ActOnStartCXXInClassMemberInitializer();
628
Fangrui Song6907ce22018-07-30 19:24:48 +0000629 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
Douglas Gregor926410d2012-02-21 02:22:07 +0000630 EqualLoc);
Richard Smith938f40b2011-06-11 17:19:42 +0000631
Richard Smith74108172014-01-17 03:11:34 +0000632 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000633 Init.get());
Richard Smith938f40b2011-06-11 17:19:42 +0000634
635 // The next token should be our artificial terminating EOF token.
636 if (Tok.isNot(tok::eof)) {
Richard Smith95e1fb02014-08-27 03:23:12 +0000637 if (!Init.isInvalid()) {
638 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
639 if (!EndLoc.isValid())
640 EndLoc = Tok.getLocation();
641 // No fixit; we can't recover as if there were a semicolon here.
642 Diag(EndLoc, diag::err_expected_semi_decl_list);
643 }
Richard Smith938f40b2011-06-11 17:19:42 +0000644
645 // Consume tokens until we hit the artificial EOF.
646 while (Tok.isNot(tok::eof))
647 ConsumeAnyToken();
648 }
David Majnemer2d3663e2014-12-18 09:57:31 +0000649 // Make sure this is *our* artificial EOF token.
David Majnemerce1d3012014-12-19 02:13:56 +0000650 if (Tok.getEofData() == MI.Field)
David Majnemer2d3663e2014-12-18 09:57:31 +0000651 ConsumeAnyToken();
Richard Smith938f40b2011-06-11 17:19:42 +0000652}
653
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000654/// ConsumeAndStoreUntil - Consume and store the token at the passed token
Douglas Gregor4d87df52008-12-16 21:30:33 +0000655/// container until the token 'T' is reached (which gets
Mike Stump11289f42009-09-09 15:08:12 +0000656/// consumed/stored too, if ConsumeFinalToken).
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000657/// If StopAtSemi is true, then we will stop early at a ';' character.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000658/// Returns true if token 'T1' or 'T2' was found.
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000659/// NOTE: This is a specialized version of Parser::SkipUntil.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000660bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
661 CachedTokens &Toks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000662 bool StopAtSemi, bool ConsumeFinalToken) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000663 // We always want this function to consume at least one token if the first
664 // token isn't T and if not at EOF.
665 bool isFirstTokenConsumed = true;
666 while (1) {
667 // If we found one of the tokens, stop and return true.
Douglas Gregor4d87df52008-12-16 21:30:33 +0000668 if (Tok.is(T1) || Tok.is(T2)) {
669 if (ConsumeFinalToken) {
670 Toks.push_back(Tok);
671 ConsumeAnyToken();
672 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000673 return true;
674 }
675
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000676 switch (Tok.getKind()) {
677 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +0000678 case tok::annot_module_begin:
679 case tok::annot_module_end:
680 case tok::annot_module_include:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000681 // Ran out of tokens.
682 return false;
683
684 case tok::l_paren:
685 // Recursively consume properly-nested parens.
686 Toks.push_back(Tok);
687 ConsumeParen();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000688 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000689 break;
690 case tok::l_square:
691 // Recursively consume properly-nested square brackets.
692 Toks.push_back(Tok);
693 ConsumeBracket();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000694 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000695 break;
696 case tok::l_brace:
697 // Recursively consume properly-nested braces.
698 Toks.push_back(Tok);
699 ConsumeBrace();
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000700 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000701 break;
702
703 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
704 // Since the user wasn't looking for this token (if they were, it would
705 // already be handled), this isn't balanced. If there is a LHS token at a
706 // higher level, we will assume that this matches the unbalanced token
707 // and return it. Otherwise, this is a spurious RHS token, which we skip.
708 case tok::r_paren:
709 if (ParenCount && !isFirstTokenConsumed)
710 return false; // Matches something.
711 Toks.push_back(Tok);
712 ConsumeParen();
713 break;
714 case tok::r_square:
715 if (BracketCount && !isFirstTokenConsumed)
716 return false; // Matches something.
717 Toks.push_back(Tok);
718 ConsumeBracket();
719 break;
720 case tok::r_brace:
721 if (BraceCount && !isFirstTokenConsumed)
722 return false; // Matches something.
723 Toks.push_back(Tok);
724 ConsumeBrace();
725 break;
726
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +0000727 case tok::semi:
728 if (StopAtSemi)
729 return false;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000730 LLVM_FALLTHROUGH;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000731 default:
732 // consume this token.
733 Toks.push_back(Tok);
Richard Smithaf3b3252017-05-18 19:21:48 +0000734 ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +0000735 break;
736 }
737 isFirstTokenConsumed = false;
738 }
739}
Sebastian Redla74948d2011-09-24 17:48:25 +0000740
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000741/// Consume tokens and store them in the passed token container until
Sebastian Redla74948d2011-09-24 17:48:25 +0000742/// we've passed the try keyword and constructor initializers and have consumed
Sebastian Redl0d164012011-09-30 08:32:17 +0000743/// the opening brace of the function body. The opening brace will be consumed
744/// if and only if there was no error.
Sebastian Redla74948d2011-09-24 17:48:25 +0000745///
Richard Smithcde3fd82013-07-04 00:13:48 +0000746/// \return True on error.
Sebastian Redl0d164012011-09-30 08:32:17 +0000747bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
Sebastian Redla74948d2011-09-24 17:48:25 +0000748 if (Tok.is(tok::kw_try)) {
749 Toks.push_back(Tok);
750 ConsumeToken();
751 }
Richard Smithcde3fd82013-07-04 00:13:48 +0000752
753 if (Tok.isNot(tok::colon)) {
754 // Easy case, just a function body.
755
756 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
757 // brace: an opening one is the function body, while a closing one probably
758 // means we've reached the end of the class.
759 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
760 /*StopAtSemi=*/true,
761 /*ConsumeFinalToken=*/false);
762 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000763 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
Richard Smithcde3fd82013-07-04 00:13:48 +0000764
Sebastian Redla74948d2011-09-24 17:48:25 +0000765 Toks.push_back(Tok);
Richard Smithcde3fd82013-07-04 00:13:48 +0000766 ConsumeBrace();
767 return false;
Eli Friedman7cd4a9b2012-02-22 04:49:04 +0000768 }
Sebastian Redl0d164012011-09-30 08:32:17 +0000769
770 Toks.push_back(Tok);
Richard Smithcde3fd82013-07-04 00:13:48 +0000771 ConsumeToken();
772
773 // We can't reliably skip over a mem-initializer-id, because it could be
774 // a template-id involving not-yet-declared names. Given:
775 //
776 // S ( ) : a < b < c > ( e )
777 //
778 // 'e' might be an initializer or part of a template argument, depending
779 // on whether 'b' is a template.
780
781 // Track whether we might be inside a template argument. We can give
782 // significantly better diagnostics if we know that we're not.
783 bool MightBeTemplateArgument = false;
784
785 while (true) {
786 // Skip over the mem-initializer-id, if possible.
787 if (Tok.is(tok::kw_decltype)) {
788 Toks.push_back(Tok);
789 SourceLocation OpenLoc = ConsumeToken();
790 if (Tok.isNot(tok::l_paren))
791 return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
792 << "decltype";
793 Toks.push_back(Tok);
794 ConsumeParen();
795 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
Alp Tokerec543272013-12-24 09:48:30 +0000796 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
797 Diag(OpenLoc, diag::note_matching) << tok::l_paren;
Richard Smithcde3fd82013-07-04 00:13:48 +0000798 return true;
799 }
800 }
801 do {
802 // Walk over a component of a nested-name-specifier.
803 if (Tok.is(tok::coloncolon)) {
804 Toks.push_back(Tok);
805 ConsumeToken();
806
807 if (Tok.is(tok::kw_template)) {
808 Toks.push_back(Tok);
809 ConsumeToken();
810 }
811 }
812
Olivier Goffart3cd10132016-11-03 07:36:17 +0000813 if (Tok.is(tok::identifier)) {
Richard Smithcde3fd82013-07-04 00:13:48 +0000814 Toks.push_back(Tok);
815 ConsumeToken();
Richard Smithcde3fd82013-07-04 00:13:48 +0000816 } else {
817 break;
818 }
819 } while (Tok.is(tok::coloncolon));
820
Olivier Goffart3cd10132016-11-03 07:36:17 +0000821 if (Tok.is(tok::code_completion)) {
822 Toks.push_back(Tok);
823 ConsumeCodeCompletionToken();
824 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) {
825 // Could be the start of another member initializer (the ',' has not
826 // been written yet)
827 continue;
828 }
829 }
830
831 if (Tok.is(tok::comma)) {
832 // The initialization is missing, we'll diagnose it later.
833 Toks.push_back(Tok);
834 ConsumeToken();
835 continue;
836 }
Richard Smithcde3fd82013-07-04 00:13:48 +0000837 if (Tok.is(tok::less))
838 MightBeTemplateArgument = true;
839
840 if (MightBeTemplateArgument) {
841 // We may be inside a template argument list. Grab up to the start of the
842 // next parenthesized initializer or braced-init-list. This *might* be the
843 // initializer, or it might be a subexpression in the template argument
844 // list.
845 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
846 // if all angles are closed.
847 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
848 /*StopAtSemi=*/true,
849 /*ConsumeFinalToken=*/false)) {
850 // We're not just missing the initializer, we're also missing the
851 // function body!
Alp Tokerec543272013-12-24 09:48:30 +0000852 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
Richard Smithcde3fd82013-07-04 00:13:48 +0000853 }
854 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
855 // We found something weird in a mem-initializer-id.
Alp Tokerec543272013-12-24 09:48:30 +0000856 if (getLangOpts().CPlusPlus11)
857 return Diag(Tok.getLocation(), diag::err_expected_either)
858 << tok::l_paren << tok::l_brace;
859 else
860 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
Richard Smithcde3fd82013-07-04 00:13:48 +0000861 }
862
863 tok::TokenKind kind = Tok.getKind();
864 Toks.push_back(Tok);
865 bool IsLParen = (kind == tok::l_paren);
866 SourceLocation OpenLoc = Tok.getLocation();
867
868 if (IsLParen) {
869 ConsumeParen();
870 } else {
871 assert(kind == tok::l_brace && "Must be left paren or brace here.");
872 ConsumeBrace();
873 // In C++03, this has to be the start of the function body, which
874 // means the initializer is malformed; we'll diagnose it later.
875 if (!getLangOpts().CPlusPlus11)
876 return false;
Olivier Goffart3cd10132016-11-03 07:36:17 +0000877
878 const Token &PreviousToken = Toks[Toks.size() - 2];
879 if (!MightBeTemplateArgument &&
880 !PreviousToken.isOneOf(tok::identifier, tok::greater,
881 tok::greatergreater)) {
882 // If the opening brace is not preceded by one of these tokens, we are
883 // missing the mem-initializer-id. In order to recover better, we need
884 // to use heuristics to determine if this '{' is most likely the
Hiroshi Inouea5141c82017-07-13 06:51:20 +0000885 // beginning of a brace-init-list or the function body.
Olivier Goffart3cd10132016-11-03 07:36:17 +0000886 // Check the token after the corresponding '}'.
887 TentativeParsingAction PA(*this);
888 if (SkipUntil(tok::r_brace) &&
889 !Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) {
890 // Consider there was a malformed initializer and this is the start
891 // of the function body. We'll diagnose it later.
892 PA.Revert();
893 return false;
894 }
895 PA.Revert();
896 }
Richard Smithcde3fd82013-07-04 00:13:48 +0000897 }
898
899 // Grab the initializer (or the subexpression of the template argument).
900 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
901 // if we might be inside the braces of a lambda-expression.
Alp Tokerec543272013-12-24 09:48:30 +0000902 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
903 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
904 Diag(Tok, diag::err_expected) << CloseKind;
905 Diag(OpenLoc, diag::note_matching) << kind;
Richard Smithcde3fd82013-07-04 00:13:48 +0000906 return true;
907 }
908
909 // Grab pack ellipsis, if present.
910 if (Tok.is(tok::ellipsis)) {
911 Toks.push_back(Tok);
912 ConsumeToken();
913 }
914
915 // If we know we just consumed a mem-initializer, we must have ',' or '{'
916 // next.
917 if (Tok.is(tok::comma)) {
918 Toks.push_back(Tok);
919 ConsumeToken();
920 } else if (Tok.is(tok::l_brace)) {
921 // This is the function body if the ')' or '}' is immediately followed by
922 // a '{'. That cannot happen within a template argument, apart from the
923 // case where a template argument contains a compound literal:
924 //
925 // S ( ) : a < b < c > ( d ) { }
926 // // End of declaration, or still inside the template argument?
927 //
928 // ... and the case where the template argument contains a lambda:
929 //
930 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
931 // ( ) > ( ) { }
932 //
933 // FIXME: Disambiguate these cases. Note that the latter case is probably
934 // going to be made ill-formed by core issue 1607.
935 Toks.push_back(Tok);
936 ConsumeBrace();
937 return false;
938 } else if (!MightBeTemplateArgument) {
Alp Tokerec543272013-12-24 09:48:30 +0000939 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
940 << tok::comma;
Richard Smithcde3fd82013-07-04 00:13:48 +0000941 }
942 }
Sebastian Redla74948d2011-09-24 17:48:25 +0000943}
Richard Smith1fff95c2013-09-12 23:28:08 +0000944
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000945/// Consume and store tokens from the '?' to the ':' in a conditional
Richard Smith1fff95c2013-09-12 23:28:08 +0000946/// expression.
947bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
948 // Consume '?'.
949 assert(Tok.is(tok::question));
950 Toks.push_back(Tok);
951 ConsumeToken();
952
953 while (Tok.isNot(tok::colon)) {
Nico Weber77c76c52014-05-10 17:43:15 +0000954 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
955 /*StopAtSemi=*/true,
956 /*ConsumeFinalToken=*/false))
Richard Smith1fff95c2013-09-12 23:28:08 +0000957 return false;
958
959 // If we found a nested conditional, consume it.
960 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
961 return false;
962 }
963
964 // Consume ':'.
965 Toks.push_back(Tok);
966 ConsumeToken();
967 return true;
968}
969
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000970/// A tentative parsing action that can also revert token annotations.
Richard Smith1fff95c2013-09-12 23:28:08 +0000971class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
972public:
973 explicit UnannotatedTentativeParsingAction(Parser &Self,
974 tok::TokenKind EndKind)
975 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
976 // Stash away the old token stream, so we can restore it once the
977 // tentative parse is complete.
978 TentativeParsingAction Inner(Self);
979 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
980 Inner.Revert();
981 }
982
983 void RevertAnnotations() {
984 Revert();
985
986 // Put back the original tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000987 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
Richard Smith1fff95c2013-09-12 23:28:08 +0000988 if (Toks.size()) {
David Blaikie2eabcc92016-02-09 18:52:09 +0000989 auto Buffer = llvm::make_unique<Token[]>(Toks.size());
990 std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
Richard Smith1fff95c2013-09-12 23:28:08 +0000991 Buffer[Toks.size() - 1] = Self.Tok;
Ilya Biryukov929af672019-05-17 09:32:05 +0000992 Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true,
993 /*IsReinject*/ true);
Richard Smith1fff95c2013-09-12 23:28:08 +0000994
995 Self.Tok = Toks.front();
996 }
997 }
998
999private:
1000 Parser &Self;
1001 CachedTokens Toks;
1002 tok::TokenKind EndKind;
1003};
1004
1005/// ConsumeAndStoreInitializer - Consume and store the token at the passed token
1006/// container until the end of the current initializer expression (either a
1007/// default argument or an in-class initializer for a non-static data member).
Richard Smith95e1fb02014-08-27 03:23:12 +00001008///
1009/// Returns \c true if we reached the end of something initializer-shaped,
1010/// \c false if we bailed out.
Richard Smith1fff95c2013-09-12 23:28:08 +00001011bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
1012 CachedInitKind CIK) {
1013 // We always want this function to consume at least one token if not at EOF.
Richard Smith95e1fb02014-08-27 03:23:12 +00001014 bool IsFirstToken = true;
Richard Smith1fff95c2013-09-12 23:28:08 +00001015
1016 // Number of possible unclosed <s we've seen so far. These might be templates,
1017 // and might not, but if there were none of them (or we know for sure that
1018 // we're within a template), we can avoid a tentative parse.
1019 unsigned AngleCount = 0;
1020 unsigned KnownTemplateCount = 0;
1021
1022 while (1) {
1023 switch (Tok.getKind()) {
1024 case tok::comma:
1025 // If we might be in a template, perform a tentative parse to check.
1026 if (!AngleCount)
1027 // Not a template argument: this is the end of the initializer.
1028 return true;
1029 if (KnownTemplateCount)
1030 goto consume_token;
1031
1032 // We hit a comma inside angle brackets. This is the hard case. The
1033 // rule we follow is:
1034 // * For a default argument, if the tokens after the comma form a
1035 // syntactically-valid parameter-declaration-clause, in which each
1036 // parameter has an initializer, then this comma ends the default
1037 // argument.
1038 // * For a default initializer, if the tokens after the comma form a
1039 // syntactically-valid init-declarator-list, then this comma ends
1040 // the default initializer.
1041 {
1042 UnannotatedTentativeParsingAction PA(*this,
1043 CIK == CIK_DefaultInitializer
1044 ? tok::semi : tok::r_paren);
1045 Sema::TentativeAnalysisScope Scope(Actions);
1046
Richard Smithee390432014-05-16 01:56:53 +00001047 TPResult Result = TPResult::Error;
Richard Smith1fff95c2013-09-12 23:28:08 +00001048 ConsumeToken();
1049 switch (CIK) {
1050 case CIK_DefaultInitializer:
1051 Result = TryParseInitDeclaratorList();
1052 // If we parsed a complete, ambiguous init-declarator-list, this
1053 // is only syntactically-valid if it's followed by a semicolon.
Richard Smithee390432014-05-16 01:56:53 +00001054 if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi))
1055 Result = TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001056 break;
1057
1058 case CIK_DefaultArgument:
1059 bool InvalidAsDeclaration = false;
1060 Result = TryParseParameterDeclarationClause(
Nico Weberc29c4832014-12-28 23:24:02 +00001061 &InvalidAsDeclaration, /*VersusTemplateArgument=*/true);
Richard Smith1fff95c2013-09-12 23:28:08 +00001062 // If this is an expression or a declaration with a missing
1063 // 'typename', assume it's not a declaration.
Richard Smithee390432014-05-16 01:56:53 +00001064 if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1065 Result = TPResult::False;
Richard Smith1fff95c2013-09-12 23:28:08 +00001066 break;
1067 }
1068
1069 // If what follows could be a declaration, it is a declaration.
Richard Smithee390432014-05-16 01:56:53 +00001070 if (Result != TPResult::False && Result != TPResult::Error) {
Richard Smith1fff95c2013-09-12 23:28:08 +00001071 PA.Revert();
1072 return true;
1073 }
1074
1075 // In the uncommon case that we decide the following tokens are part
1076 // of a template argument, revert any annotations we've performed in
1077 // those tokens. We're not going to look them up until we've parsed
1078 // the rest of the class, and that might add more declarations.
1079 PA.RevertAnnotations();
1080 }
1081
1082 // Keep going. We know we're inside a template argument list now.
1083 ++KnownTemplateCount;
1084 goto consume_token;
1085
1086 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001087 case tok::annot_module_begin:
1088 case tok::annot_module_end:
1089 case tok::annot_module_include:
Richard Smith1fff95c2013-09-12 23:28:08 +00001090 // Ran out of tokens.
1091 return false;
1092
1093 case tok::less:
1094 // FIXME: A '<' can only start a template-id if it's preceded by an
1095 // identifier, an operator-function-id, or a literal-operator-id.
1096 ++AngleCount;
1097 goto consume_token;
1098
1099 case tok::question:
1100 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1101 // that is *never* the end of the initializer. Skip to the ':'.
1102 if (!ConsumeAndStoreConditional(Toks))
1103 return false;
1104 break;
1105
1106 case tok::greatergreatergreater:
1107 if (!getLangOpts().CPlusPlus11)
1108 goto consume_token;
1109 if (AngleCount) --AngleCount;
1110 if (KnownTemplateCount) --KnownTemplateCount;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00001111 LLVM_FALLTHROUGH;
Richard Smith1fff95c2013-09-12 23:28:08 +00001112 case tok::greatergreater:
1113 if (!getLangOpts().CPlusPlus11)
1114 goto consume_token;
1115 if (AngleCount) --AngleCount;
1116 if (KnownTemplateCount) --KnownTemplateCount;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00001117 LLVM_FALLTHROUGH;
Richard Smith1fff95c2013-09-12 23:28:08 +00001118 case tok::greater:
1119 if (AngleCount) --AngleCount;
1120 if (KnownTemplateCount) --KnownTemplateCount;
1121 goto consume_token;
1122
1123 case tok::kw_template:
1124 // 'template' identifier '<' is known to start a template argument list,
1125 // and can be used to disambiguate the parse.
1126 // FIXME: Support all forms of 'template' unqualified-id '<'.
1127 Toks.push_back(Tok);
1128 ConsumeToken();
1129 if (Tok.is(tok::identifier)) {
1130 Toks.push_back(Tok);
1131 ConsumeToken();
1132 if (Tok.is(tok::less)) {
Richard Smith93033572014-07-27 05:38:12 +00001133 ++AngleCount;
Richard Smith1fff95c2013-09-12 23:28:08 +00001134 ++KnownTemplateCount;
1135 Toks.push_back(Tok);
1136 ConsumeToken();
1137 }
1138 }
1139 break;
1140
1141 case tok::kw_operator:
1142 // If 'operator' precedes other punctuation, that punctuation loses
1143 // its special behavior.
1144 Toks.push_back(Tok);
1145 ConsumeToken();
1146 switch (Tok.getKind()) {
1147 case tok::comma:
1148 case tok::greatergreatergreater:
1149 case tok::greatergreater:
1150 case tok::greater:
1151 case tok::less:
1152 Toks.push_back(Tok);
1153 ConsumeToken();
1154 break;
1155 default:
1156 break;
1157 }
1158 break;
1159
1160 case tok::l_paren:
1161 // Recursively consume properly-nested parens.
1162 Toks.push_back(Tok);
1163 ConsumeParen();
1164 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1165 break;
1166 case tok::l_square:
1167 // Recursively consume properly-nested square brackets.
1168 Toks.push_back(Tok);
1169 ConsumeBracket();
1170 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
1171 break;
1172 case tok::l_brace:
1173 // Recursively consume properly-nested braces.
1174 Toks.push_back(Tok);
1175 ConsumeBrace();
1176 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1177 break;
1178
1179 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1180 // Since the user wasn't looking for this token (if they were, it would
1181 // already be handled), this isn't balanced. If there is a LHS token at a
1182 // higher level, we will assume that this matches the unbalanced token
Richard Smith95e1fb02014-08-27 03:23:12 +00001183 // and return it. Otherwise, this is a spurious RHS token, which we
1184 // consume and pass on to downstream code to diagnose.
Richard Smith1fff95c2013-09-12 23:28:08 +00001185 case tok::r_paren:
1186 if (CIK == CIK_DefaultArgument)
1187 return true; // End of the default argument.
Richard Smith95e1fb02014-08-27 03:23:12 +00001188 if (ParenCount && !IsFirstToken)
1189 return false;
1190 Toks.push_back(Tok);
1191 ConsumeParen();
1192 continue;
Richard Smith1fff95c2013-09-12 23:28:08 +00001193 case tok::r_square:
Richard Smith95e1fb02014-08-27 03:23:12 +00001194 if (BracketCount && !IsFirstToken)
1195 return false;
1196 Toks.push_back(Tok);
1197 ConsumeBracket();
1198 continue;
Richard Smith1fff95c2013-09-12 23:28:08 +00001199 case tok::r_brace:
Richard Smith95e1fb02014-08-27 03:23:12 +00001200 if (BraceCount && !IsFirstToken)
1201 return false;
1202 Toks.push_back(Tok);
1203 ConsumeBrace();
1204 continue;
Richard Smith1fff95c2013-09-12 23:28:08 +00001205
1206 case tok::code_completion:
1207 Toks.push_back(Tok);
1208 ConsumeCodeCompletionToken();
1209 break;
1210
1211 case tok::string_literal:
1212 case tok::wide_string_literal:
1213 case tok::utf8_string_literal:
1214 case tok::utf16_string_literal:
1215 case tok::utf32_string_literal:
1216 Toks.push_back(Tok);
1217 ConsumeStringToken();
1218 break;
1219 case tok::semi:
1220 if (CIK == CIK_DefaultInitializer)
1221 return true; // End of the default initializer.
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00001222 LLVM_FALLTHROUGH;
Richard Smith1fff95c2013-09-12 23:28:08 +00001223 default:
1224 consume_token:
1225 Toks.push_back(Tok);
1226 ConsumeToken();
1227 break;
1228 }
Richard Smith95e1fb02014-08-27 03:23:12 +00001229 IsFirstToken = false;
Richard Smith1fff95c2013-09-12 23:28:08 +00001230 }
1231}