1//===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements parsing for C++ class inline methods.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/DeclTemplate.h"
14#include "clang/Basic/DiagnosticParse.h"
15#include "clang/Parse/Parser.h"
16#include "clang/Parse/RAIIObjectsForParser.h"
17#include "clang/Sema/DeclSpec.h"
18#include "clang/Sema/EnterExpressionEvaluationContext.h"
19#include "clang/Sema/Scope.h"
20#include "llvm/ADT/ScopeExit.h"
21
22using namespace clang;
23
24StringLiteral *Parser::ParseCXXDeletedFunctionMessage() {
25 if (!Tok.is(K: tok::l_paren))
26 return nullptr;
27 StringLiteral *Message = nullptr;
28 BalancedDelimiterTracker BT{*this, tok::l_paren};
29 BT.consumeOpen();
30
31 if (isTokenStringLiteral()) {
32 ExprResult Res = ParseUnevaluatedStringLiteralExpression();
33 if (Res.isUsable()) {
34 Message = Res.getAs<StringLiteral>();
35 Diag(Loc: Message->getBeginLoc(), DiagID: getLangOpts().CPlusPlus26
36 ? diag::warn_cxx23_delete_with_message
37 : diag::ext_delete_with_message)
38 << Message->getSourceRange();
39 }
40 } else {
41 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_string_literal)
42 << /*Source='in'*/ 0 << "'delete'";
43 SkipUntil(T: tok::r_paren, Flags: StopAtSemi | StopBeforeMatch);
44 }
45
46 BT.consumeClose();
47 return Message;
48}
49
50void Parser::SkipDeletedFunctionBody() {
51 if (!Tok.is(K: tok::l_paren))
52 return;
53
54 BalancedDelimiterTracker BT{*this, tok::l_paren};
55 BT.consumeOpen();
56
57 // Just skip to the end of the current declaration.
58 SkipUntil(T1: tok::r_paren, T2: tok::comma, Flags: StopAtSemi | StopBeforeMatch);
59 if (Tok.is(K: tok::r_paren))
60 BT.consumeClose();
61}
62
63NamedDecl *Parser::ParseCXXInlineMethodDef(
64 AccessSpecifier AS, const ParsedAttributesView &AccessAttrs,
65 ParsingDeclarator &D, const ParsedTemplateInfo &TemplateInfo,
66 const VirtSpecifiers &VS, SourceLocation PureSpecLoc) {
67 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
68 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
69 "Current token not a '{', ':', '=', or 'try'!");
70
71 MultiTemplateParamsArg TemplateParams(
72 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
73 : nullptr,
74 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
75
76 NamedDecl *FnD;
77 if (D.getDeclSpec().isFriendSpecified())
78 FnD = Actions.ActOnFriendFunctionDecl(S: getCurScope(), D,
79 TemplateParams);
80 else {
81 FnD = Actions.ActOnCXXMemberDeclarator(S: getCurScope(), AS, D,
82 TemplateParameterLists: TemplateParams, BitfieldWidth: nullptr,
83 VS, InitStyle: ICIS_NoInit);
84 if (FnD) {
85 Actions.ProcessDeclAttributeList(S: getCurScope(), D: FnD, AttrList: AccessAttrs);
86 if (PureSpecLoc.isValid())
87 Actions.ActOnPureSpecifier(D: FnD, PureSpecLoc);
88 }
89 }
90
91 if (FnD)
92 HandleMemberFunctionDeclDelays(DeclaratorInfo&: D, ThisDecl: FnD);
93
94 D.complete(D: FnD);
95
96 if (TryConsumeToken(Expected: tok::equal)) {
97 if (!FnD) {
98 SkipUntil(T: tok::semi);
99 return nullptr;
100 }
101
102 bool Delete = false;
103 SourceLocation KWLoc;
104 if (TryConsumeToken(Expected: tok::kw_delete, Loc&: KWLoc)) {
105 Diag(Loc: KWLoc, DiagID: getLangOpts().CPlusPlus11
106 ? diag::warn_cxx98_compat_defaulted_deleted_function
107 : diag::ext_defaulted_deleted_function)
108 << 1 /* deleted */;
109 StringLiteral *Message = ParseCXXDeletedFunctionMessage();
110 Actions.SetDeclDeleted(dcl: FnD, DelLoc: KWLoc, Message);
111 Delete = true;
112 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(Val: FnD)) {
113 DeclAsFunction->setRangeEnd(PrevTokLocation);
114 }
115 } else if (TryConsumeToken(Expected: tok::kw_default, Loc&: KWLoc)) {
116 Diag(Loc: KWLoc, DiagID: getLangOpts().CPlusPlus11
117 ? diag::warn_cxx98_compat_defaulted_deleted_function
118 : diag::ext_defaulted_deleted_function)
119 << 0 /* defaulted */;
120 Actions.SetDeclDefaulted(dcl: FnD, DefaultLoc: KWLoc);
121 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(Val: FnD)) {
122 DeclAsFunction->setRangeEnd(PrevTokLocation);
123 }
124 } else {
125 llvm_unreachable("function definition after = not 'delete' or 'default'");
126 }
127
128 if (Tok.is(K: tok::comma)) {
129 Diag(Loc: KWLoc, DiagID: diag::err_default_delete_in_multiple_declaration)
130 << Delete;
131 SkipUntil(T: tok::semi);
132 } else if (ExpectAndConsume(ExpectedTok: tok::semi, Diag: diag::err_expected_after,
133 DiagMsg: Delete ? "delete" : "default") &&
134 !isLikelyAtStartOfNewDeclaration()) {
135 SkipUntil(T: tok::semi);
136 }
137
138 return FnD;
139 }
140
141 if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(D: FnD)) &&
142 trySkippingFunctionBody()) {
143 Actions.ActOnSkippedFunctionBody(Decl: FnD);
144 return FnD;
145 }
146
147 // In delayed template parsing mode, if we are within a class template
148 // or if we are about to parse function member template then consume
149 // the tokens and store them for parsing at the end of the translation unit.
150 if (getLangOpts().DelayedTemplateParsing &&
151 D.getFunctionDefinitionKind() == FunctionDefinitionKind::Definition &&
152 !D.getDeclSpec().hasConstexprSpecifier() &&
153 !(FnD && FnD->getAsFunction() &&
154 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
155 ((Actions.CurContext->isDependentContext() ||
156 (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate &&
157 TemplateInfo.Kind != ParsedTemplateKind::ExplicitSpecialization)) &&
158 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
159
160 CachedTokens Toks;
161 LexTemplateFunctionForLateParsing(Toks);
162
163 if (FnD) {
164 FunctionDecl *FD = FnD->getAsFunction();
165 Actions.CheckForFunctionRedefinition(FD);
166 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
167 }
168
169 return FnD;
170 }
171
172 // Consume the tokens and store them for later parsing.
173
174 LexedMethod* LM = new LexedMethod(this, FnD);
175 getCurrentClass().LateParsedDeclarations.push_back(Elt: LM);
176 CachedTokens &Toks = LM->Toks;
177
178 tok::TokenKind kind = Tok.getKind();
179 // Consume everything up to (and including) the left brace of the
180 // function body.
181 if (ConsumeAndStoreFunctionPrologue(Toks)) {
182 // We didn't find the left-brace we expected after the
183 // constructor initializer.
184
185 // If we're code-completing and the completion point was in the broken
186 // initializer, we want to parse it even though that will fail.
187 if (PP.isCodeCompletionEnabled() &&
188 llvm::any_of(Range&: Toks, P: [](const Token &Tok) {
189 return Tok.is(K: tok::code_completion);
190 })) {
191 // If we gave up at the completion point, the initializer list was
192 // likely truncated, so don't eat more tokens. We'll hit some extra
193 // errors, but they should be ignored in code completion.
194 return FnD;
195 }
196
197 // We already printed an error, and it's likely impossible to recover,
198 // so don't try to parse this method later.
199 // Skip over the rest of the decl and back to somewhere that looks
200 // reasonable.
201 SkipMalformedDecl();
202 delete getCurrentClass().LateParsedDeclarations.back();
203 getCurrentClass().LateParsedDeclarations.pop_back();
204 return FnD;
205 } else {
206 // Consume everything up to (and including) the matching right brace.
207 ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false);
208 }
209
210 // If we're in a function-try-block, we need to store all the catch blocks.
211 if (kind == tok::kw_try) {
212 while (Tok.is(K: tok::kw_catch)) {
213 ConsumeAndStoreUntil(T1: tok::l_brace, Toks, /*StopAtSemi=*/false);
214 ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false);
215 }
216 }
217
218 if (FnD) {
219 FunctionDecl *FD = FnD->getAsFunction();
220 // Track that this function will eventually have a body; Sema needs
221 // to know this.
222 Actions.CheckForFunctionRedefinition(FD);
223 FD->setWillHaveBody(true);
224 } else {
225 // If semantic analysis could not build a function declaration,
226 // just throw away the late-parsed declaration.
227 delete getCurrentClass().LateParsedDeclarations.back();
228 getCurrentClass().LateParsedDeclarations.pop_back();
229 }
230
231 return FnD;
232}
233
234void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
235 assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
236 "Current token not a '{' or '='!");
237
238 LateParsedMemberInitializer *MI =
239 new LateParsedMemberInitializer(this, VarD);
240 getCurrentClass().LateParsedDeclarations.push_back(Elt: MI);
241 CachedTokens &Toks = MI->Toks;
242
243 tok::TokenKind kind = Tok.getKind();
244 if (kind == tok::equal) {
245 Toks.push_back(Elt: Tok);
246 ConsumeToken();
247 }
248
249 if (kind == tok::l_brace) {
250 // Begin by storing the '{' token.
251 Toks.push_back(Elt: Tok);
252 ConsumeBrace();
253
254 // Consume everything up to (and including) the matching right brace.
255 ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/true);
256 } else {
257 // Consume everything up to (but excluding) the comma or semicolon.
258 ConsumeAndStoreInitializer(Toks, CIK: CachedInitKind::DefaultInitializer);
259 }
260
261 // Store an artificial EOF token to ensure that we don't run off the end of
262 // the initializer when we come to parse it.
263 Token Eof;
264 Eof.startToken();
265 Eof.setKind(tok::eof);
266 Eof.setLocation(Tok.getLocation());
267 Eof.setEofData(VarD);
268 Toks.push_back(Elt: Eof);
269}
270
271LateParsedDeclaration::~LateParsedDeclaration() {}
272void LateParsedDeclaration::ParseLexedMethodDeclarations() {}
273void LateParsedDeclaration::ParseLexedMemberInitializers() {}
274void LateParsedDeclaration::ParseLexedMethodDefs() {}
275void LateParsedDeclaration::ParseLexedAttributes() {}
276void LateParsedDeclaration::ParseLexedPragmas() {}
277
278Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
279 : Self(P), Class(C) {}
280
281Parser::LateParsedClass::~LateParsedClass() {
282 Self->DeallocateParsedClasses(Class);
283}
284
285void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
286 Self->ParseLexedMethodDeclarations(Class&: *Class);
287}
288
289void Parser::LateParsedClass::ParseLexedMemberInitializers() {
290 Self->ParseLexedMemberInitializers(Class&: *Class);
291}
292
293void Parser::LateParsedClass::ParseLexedMethodDefs() {
294 Self->ParseLexedMethodDefs(Class&: *Class);
295}
296
297void Parser::LateParsedClass::ParseLexedAttributes() {
298 Self->ParseLexedAttributes(Class&: *Class);
299}
300
301void Parser::LateParsedClass::ParseLexedPragmas() {
302 Self->ParseLexedPragmas(Class&: *Class);
303}
304
305void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
306 Self->ParseLexedMethodDeclaration(LM&: *this);
307}
308
309void Parser::LexedMethod::ParseLexedMethodDefs() {
310 Self->ParseLexedMethodDef(LM&: *this);
311}
312
313void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
314 Self->ParseLexedMemberInitializer(MI&: *this);
315}
316
317void LateParsedAttribute::ParseLexedAttributes() {
318 Self->ParseLexedAttribute(LPA&: *this, EnterScope: true, OnDefinition: false);
319}
320
321void LateParsedTypeAttribute::ParseLexedAttributes() {}
322
323void Parser::LateParsedPragma::ParseLexedPragmas() {
324 Self->ParseLexedPragma(LP&: *this);
325}
326
327struct Parser::ReenterTemplateScopeRAII {
328 Parser &P;
329 MultiParseScope Scopes;
330 TemplateParameterDepthRAII CurTemplateDepthTracker;
331
332 ReenterTemplateScopeRAII(Parser &P, Decl *MaybeTemplated, bool Enter = true)
333 : P(P), Scopes(P), CurTemplateDepthTracker(P.TemplateParameterDepth) {
334 if (Enter) {
335 CurTemplateDepthTracker.addDepth(
336 D: P.ReenterTemplateScopes(S&: Scopes, D: MaybeTemplated));
337 }
338 }
339};
340
341struct Parser::ReenterClassScopeRAII : ReenterTemplateScopeRAII {
342 ParsingClass &Class;
343
344 ReenterClassScopeRAII(Parser &P, ParsingClass &Class)
345 : ReenterTemplateScopeRAII(P, Class.TagOrTemplate,
346 /*Enter=*/!Class.TopLevelClass),
347 Class(Class) {
348 // If this is the top-level class, we're still within its scope.
349 if (Class.TopLevelClass)
350 return;
351
352 // Re-enter the class scope itself.
353 Scopes.Enter(ScopeFlags: Scope::ClassScope|Scope::DeclScope);
354 P.Actions.ActOnStartDelayedMemberDeclarations(S: P.getCurScope(),
355 Record: Class.TagOrTemplate);
356 }
357 ~ReenterClassScopeRAII() {
358 if (Class.TopLevelClass)
359 return;
360
361 P.Actions.ActOnFinishDelayedMemberDeclarations(S: P.getCurScope(),
362 Record: Class.TagOrTemplate);
363 }
364};
365
366void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
367 ReenterClassScopeRAII InClassScope(*this, Class);
368
369 for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations)
370 LateD->ParseLexedMethodDeclarations();
371}
372
373void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
374 // If this is a member template, introduce the template parameter scope.
375 ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.Method);
376
377 // Start the delayed C++ method declaration
378 Actions.ActOnStartDelayedCXXMethodDeclaration(S: getCurScope(), Method: LM.Method);
379
380 // Introduce the parameters into scope and parse their default
381 // arguments.
382 InFunctionTemplateScope.Scopes.Enter(ScopeFlags: Scope::FunctionPrototypeScope |
383 Scope::FunctionDeclarationScope |
384 Scope::DeclScope);
385
386 // Delayed default arguments or exception specifications may contain lambdas,
387 // struct S {
388 // void ICE(int x, int = sizeof([x] { return x; }()));
389 // }
390 //
391 // struct X {
392 // void ICE(int val) noexcept(noexcept([val]{}));
393 // };
394 // Lambda capture handling in tryCaptureVariable() expects an enclosing
395 // function scope in Sema's FunctionScopes stack.
396 Sema::FunctionScopeRAII PopFnContext(Actions);
397 Actions.PushFunctionScope();
398
399 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
400 auto Param = cast<ParmVarDecl>(Val: LM.DefaultArgs[I].Param);
401 // Introduce the parameter into scope.
402 bool HasUnparsed = Param->hasUnparsedDefaultArg();
403 Actions.ActOnDelayedCXXMethodParameter(S: getCurScope(), Param);
404 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
405 if (Toks) {
406 ParenBraceBracketBalancer BalancerRAIIObj(*this);
407
408 // Mark the end of the default argument so that we know when to stop when
409 // we parse it later on.
410 Token LastDefaultArgToken = Toks->back();
411 Token DefArgEnd;
412 DefArgEnd.startToken();
413 DefArgEnd.setKind(tok::eof);
414 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
415 DefArgEnd.setEofData(Param);
416 Toks->push_back(Elt: DefArgEnd);
417
418 // Parse the default argument from its saved token stream.
419 Toks->push_back(Elt: Tok); // So that the current token doesn't get lost
420 PP.EnterTokenStream(Toks: *Toks, DisableMacroExpansion: true, /*IsReinject*/ true);
421
422 // Consume the previously-pushed token.
423 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
424
425 // Consume the '='.
426 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
427 SourceLocation EqualLoc = ConsumeToken();
428
429 // The argument isn't actually potentially evaluated unless it is
430 // used.
431 EnterExpressionEvaluationContext Eval(
432 Actions,
433 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param);
434
435 ExprResult DefArgResult;
436 if (getLangOpts().CPlusPlus11 && Tok.is(K: tok::l_brace)) {
437 Diag(Tok, DiagID: diag::warn_cxx98_compat_generalized_initializer_lists);
438 DefArgResult = ParseBraceInitializer();
439 } else
440 DefArgResult = ParseAssignmentExpression();
441 if (DefArgResult.isInvalid()) {
442 Actions.ActOnParamDefaultArgumentError(param: Param, EqualLoc,
443 /*DefaultArg=*/nullptr);
444 } else {
445 if (Tok.isNot(K: tok::eof) || Tok.getEofData() != Param) {
446 // The last two tokens are the terminator and the saved value of
447 // Tok; the last token in the default argument is the one before
448 // those.
449 assert(Toks->size() >= 3 && "expected a token in default arg");
450 Diag(Loc: Tok.getLocation(), DiagID: diag::err_default_arg_unparsed)
451 << SourceRange(Tok.getLocation(),
452 (*Toks)[Toks->size() - 3].getLocation());
453 }
454 Actions.ActOnParamDefaultArgument(param: Param, EqualLoc,
455 defarg: DefArgResult.get());
456 }
457
458 // There could be leftover tokens (e.g. because of an error).
459 // Skip through until we reach the 'end of default argument' token.
460 while (Tok.isNot(K: tok::eof))
461 ConsumeAnyToken();
462
463 if (Tok.is(K: tok::eof) && Tok.getEofData() == Param)
464 ConsumeAnyToken();
465 } else if (HasUnparsed) {
466 assert(Param->hasInheritedDefaultArg());
467 FunctionDecl *Old;
468 if (const auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(Val: LM.Method))
469 Old =
470 cast<FunctionDecl>(Val: FunTmpl->getTemplatedDecl())->getPreviousDecl();
471 else
472 Old = cast<FunctionDecl>(Val: LM.Method)->getPreviousDecl();
473 if (Old) {
474 ParmVarDecl *OldParam = Old->getParamDecl(i: I);
475 assert(!OldParam->hasUnparsedDefaultArg());
476 if (OldParam->hasUninstantiatedDefaultArg())
477 Param->setUninstantiatedDefaultArg(
478 OldParam->getUninstantiatedDefaultArg());
479 else
480 Param->setDefaultArg(OldParam->getInit());
481 }
482 }
483 }
484
485 // Parse a delayed exception-specification, if there is one.
486 if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
487 ParenBraceBracketBalancer BalancerRAIIObj(*this);
488
489 // Add the 'stop' token.
490 Token LastExceptionSpecToken = Toks->back();
491 Token ExceptionSpecEnd;
492 ExceptionSpecEnd.startToken();
493 ExceptionSpecEnd.setKind(tok::eof);
494 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
495 ExceptionSpecEnd.setEofData(LM.Method);
496 Toks->push_back(Elt: ExceptionSpecEnd);
497
498 // Parse the default argument from its saved token stream.
499 Toks->push_back(Elt: Tok); // So that the current token doesn't get lost
500 PP.EnterTokenStream(Toks: *Toks, DisableMacroExpansion: true, /*IsReinject*/true);
501
502 // Consume the previously-pushed token.
503 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
504
505 // C++11 [expr.prim.general]p3:
506 // If a declaration declares a member function or member function
507 // template of a class X, the expression this is a prvalue of type
508 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
509 // and the end of the function-definition, member-declarator, or
510 // declarator.
511 CXXMethodDecl *Method;
512 FunctionDecl *FunctionToPush;
513 if (FunctionTemplateDecl *FunTmpl
514 = dyn_cast<FunctionTemplateDecl>(Val: LM.Method))
515 FunctionToPush = FunTmpl->getTemplatedDecl();
516 else
517 FunctionToPush = cast<FunctionDecl>(Val: LM.Method);
518 Method = dyn_cast<CXXMethodDecl>(Val: FunctionToPush);
519
520 // Setup the CurScope to match the function DeclContext - we have such
521 // assumption in IsInFnTryBlockHandler().
522 ParseScope FnScope(this, Scope::FnScope);
523 Sema::ContextRAII FnContext(Actions, FunctionToPush,
524 /*NewThisContext=*/false);
525
526 Sema::CXXThisScopeRAII ThisScope(
527 Actions, Method ? Method->getParent() : nullptr,
528 Method ? Method->getMethodQualifiers() : Qualifiers{},
529 Method && getLangOpts().CPlusPlus11);
530
531 // Parse the exception-specification.
532 SourceRange SpecificationRange;
533 SmallVector<ParsedType, 4> DynamicExceptions;
534 SmallVector<SourceRange, 4> DynamicExceptionRanges;
535 ExprResult NoexceptExpr;
536 CachedTokens *ExceptionSpecTokens;
537
538 ExceptionSpecificationType EST
539 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
540 DynamicExceptions,
541 DynamicExceptionRanges, NoexceptExpr,
542 ExceptionSpecTokens);
543
544 if (Tok.isNot(K: tok::eof) || Tok.getEofData() != LM.Method)
545 Diag(Loc: Tok.getLocation(), DiagID: diag::err_except_spec_unparsed);
546
547 // Attach the exception-specification to the method.
548 Actions.actOnDelayedExceptionSpecification(D: LM.Method, EST,
549 SpecificationRange,
550 DynamicExceptions,
551 DynamicExceptionRanges,
552 NoexceptExpr: NoexceptExpr.isUsable()?
553 NoexceptExpr.get() : nullptr);
554
555 // There could be leftover tokens (e.g. because of an error).
556 // Skip through until we reach the original token position.
557 while (Tok.isNot(K: tok::eof))
558 ConsumeAnyToken();
559
560 // Clean up the remaining EOF token.
561 if (Tok.is(K: tok::eof) && Tok.getEofData() == LM.Method)
562 ConsumeAnyToken();
563
564 delete Toks;
565 LM.ExceptionSpecTokens = nullptr;
566 }
567
568 InFunctionTemplateScope.Scopes.Exit();
569
570 // Finish the delayed C++ method declaration.
571 Actions.ActOnFinishDelayedCXXMethodDeclaration(S: getCurScope(), Method: LM.Method);
572}
573
574void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
575 ReenterClassScopeRAII InClassScope(*this, Class);
576
577 for (LateParsedDeclaration *D : Class.LateParsedDeclarations)
578 D->ParseLexedMethodDefs();
579}
580
581void Parser::ParseLexedMethodDef(LexedMethod &LM) {
582 // If this is a member template, introduce the template parameter scope.
583 ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.D);
584
585 ParenBraceBracketBalancer BalancerRAIIObj(*this);
586
587 assert(!LM.Toks.empty() && "Empty body!");
588 Token LastBodyToken = LM.Toks.back();
589 Token BodyEnd;
590 BodyEnd.startToken();
591 BodyEnd.setKind(tok::eof);
592 BodyEnd.setLocation(LastBodyToken.getEndLoc());
593 BodyEnd.setEofData(LM.D);
594 LM.Toks.push_back(Elt: BodyEnd);
595 // Append the current token at the end of the new token stream so that it
596 // doesn't get lost.
597 LM.Toks.push_back(Elt: Tok);
598 PP.EnterTokenStream(Toks: LM.Toks, DisableMacroExpansion: true, /*IsReinject*/true);
599
600 // Consume the previously pushed token.
601 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
602 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
603 && "Inline method not starting with '{', ':' or 'try'");
604
605 // Parse the method body. Function body parsing code is similar enough
606 // to be re-used for method bodies as well.
607 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
608 Scope::CompoundStmtScope);
609 Sema::FPFeaturesStateRAII SaveFPFeatures(Actions);
610
611 Actions.ActOnStartOfFunctionDef(S: getCurScope(), D: LM.D);
612
613 llvm::scope_exit _([&]() {
614 while (Tok.isNot(K: tok::eof))
615 ConsumeAnyToken();
616
617 if (Tok.is(K: tok::eof) && Tok.getEofData() == LM.D)
618 ConsumeAnyToken();
619
620 if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: LM.D))
621 if (isa<CXXMethodDecl>(Val: FD) ||
622 FD->isInIdentifierNamespace(NS: Decl::IDNS_OrdinaryFriend))
623 Actions.ActOnFinishInlineFunctionDef(D: FD);
624 });
625
626 if (Tok.is(K: tok::kw_try)) {
627 ParseFunctionTryBlock(Decl: LM.D, BodyScope&: FnScope);
628 return;
629 }
630 if (Tok.is(K: tok::colon)) {
631 ParseConstructorInitializer(ConstructorDecl: LM.D);
632
633 // Error recovery.
634 if (!Tok.is(K: tok::l_brace)) {
635 FnScope.Exit();
636 Actions.ActOnFinishFunctionBody(Decl: LM.D, Body: nullptr);
637 return;
638 }
639 } else
640 Actions.ActOnDefaultCtorInitializers(CDtorDecl: LM.D);
641
642 assert((Actions.getDiagnostics().hasErrorOccurred() ||
643 !isa<FunctionTemplateDecl>(LM.D) ||
644 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
645 < TemplateParameterDepth) &&
646 "TemplateParameterDepth should be greater than the depth of "
647 "current template being instantiated!");
648
649 ParseFunctionStatementBody(Decl: LM.D, BodyScope&: FnScope);
650}
651
652void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
653 ReenterClassScopeRAII InClassScope(*this, Class);
654
655 if (!Class.LateParsedDeclarations.empty()) {
656 // C++11 [expr.prim.general]p4:
657 // Otherwise, if a member-declarator declares a non-static data member
658 // (9.2) of a class X, the expression this is a prvalue of type "pointer
659 // to X" within the optional brace-or-equal-initializer. It shall not
660 // appear elsewhere in the member-declarator.
661 // FIXME: This should be done in ParseLexedMemberInitializer, not here.
662 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
663 Qualifiers());
664
665 for (LateParsedDeclaration *D : Class.LateParsedDeclarations)
666 D->ParseLexedMemberInitializers();
667 }
668
669 Actions.ActOnFinishDelayedMemberInitializers(Record: Class.TagOrTemplate);
670}
671
672void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
673 if (!MI.Field || MI.Field->isInvalidDecl())
674 return;
675
676 ParenBraceBracketBalancer BalancerRAIIObj(*this);
677
678 // Append the current token at the end of the new token stream so that it
679 // doesn't get lost.
680 MI.Toks.push_back(Elt: Tok);
681 PP.EnterTokenStream(Toks: MI.Toks, DisableMacroExpansion: true, /*IsReinject*/true);
682
683 // Consume the previously pushed token.
684 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
685
686 SourceLocation EqualLoc;
687
688 Actions.ActOnStartCXXInClassMemberInitializer();
689
690 // The initializer isn't actually potentially evaluated unless it is
691 // used.
692 EnterExpressionEvaluationContext Eval(
693 Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed);
694
695 ExprResult Init = ParseCXXMemberInitializer(D: MI.Field, /*IsFunction=*/false,
696 EqualLoc);
697
698 Actions.ActOnFinishCXXInClassMemberInitializer(VarDecl: MI.Field, EqualLoc, Init);
699
700 // The next token should be our artificial terminating EOF token.
701 if (Tok.isNot(K: tok::eof)) {
702 if (!Init.isInvalid()) {
703 SourceLocation EndLoc = PP.getLocForEndOfToken(Loc: PrevTokLocation);
704 if (!EndLoc.isValid())
705 EndLoc = Tok.getLocation();
706 // No fixit; we can't recover as if there were a semicolon here.
707 Diag(Loc: EndLoc, DiagID: diag::err_expected_semi_decl_list);
708 }
709
710 // Consume tokens until we hit the artificial EOF.
711 while (Tok.isNot(K: tok::eof))
712 ConsumeAnyToken();
713 }
714 // Make sure this is *our* artificial EOF token.
715 if (Tok.getEofData() == MI.Field)
716 ConsumeAnyToken();
717}
718
719void Parser::ParseLexedAttributes(ParsingClass &Class) {
720 ReenterClassScopeRAII InClassScope(*this, Class);
721
722 for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations)
723 LateD->ParseLexedAttributes();
724}
725
726void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
727 bool EnterScope, bool OnDefinition,
728 ParsedAttributes *OutAttrs) {
729 assert(LAs.parseSoon() &&
730 "Attribute list should be marked for immediate parsing.");
731 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
732 if (D)
733 LAs[i]->addDecl(D);
734 ParseLexedAttribute(LPA&: *LAs[i], EnterScope, OnDefinition, OutAttrs);
735 delete LAs[i];
736 }
737 LAs.clear();
738}
739
740void Parser::ParseLexedAttribute(LateParsedAttribute &LPA, bool EnterScope,
741 bool OnDefinition,
742 ParsedAttributes *OutAttrs) {
743 // Create a fake EOF so that attribute parsing won't go off the end of the
744 // attribute.
745 Token AttrEnd;
746 AttrEnd.startToken();
747 AttrEnd.setKind(tok::eof);
748 AttrEnd.setLocation(Tok.getLocation());
749 AttrEnd.setEofData(LPA.Toks.data());
750 LPA.Toks.push_back(Elt: AttrEnd);
751
752 // Append the current token at the end of the new token stream so that it
753 // doesn't get lost.
754 LPA.Toks.push_back(Elt: Tok);
755 PP.EnterTokenStream(Toks: LPA.Toks, DisableMacroExpansion: true, /*IsReinject=*/true);
756 // Consume the previously pushed token.
757 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
758
759 ParsedAttributes Attrs(AttrFactory);
760
761 if (LPA.Decls.size() > 0) {
762 Decl *D = LPA.Decls[0];
763 bool HasFuncScope = EnterScope && LPA.Decls.size() == 1 &&
764 D->isFunctionOrFunctionTemplate();
765 bool IsCPlusPlus = getLangOpts().CPlusPlus;
766
767 NamedDecl *ND = dyn_cast<NamedDecl>(Val: D);
768 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(Val: D->getDeclContext());
769
770 // Allow 'this' within late-parsed attributes.
771 Sema::CXXThisScopeRAII ThisScope(Actions, RD, Qualifiers(),
772 IsCPlusPlus && ND &&
773 ND->isCXXInstanceMember());
774
775 // If the Decl is templatized, add template parameters to the scope.
776 ReenterTemplateScopeRAII InDeclScope(*this, D, IsCPlusPlus && EnterScope);
777
778 // If the Decl is on a function, add function parameters to the scope.
779 if (HasFuncScope) {
780 InDeclScope.Scopes.Enter(ScopeFlags: Scope::FnScope | Scope::DeclScope |
781 Scope::CompoundStmtScope);
782 Actions.ActOnReenterFunctionContext(S: Actions.CurScope, D);
783 }
784
785 ParseGNUAttributeArgs(AttrName: &LPA.AttrName, AttrNameLoc: LPA.AttrNameLoc, Attrs,
786 /*EndLoc=*/nullptr, /*ScopeName=*/nullptr,
787 ScopeLoc: SourceLocation(), Form: ParsedAttr::Form::GNU(),
788 /*D=*/nullptr);
789
790 if (HasFuncScope)
791 Actions.ActOnExitFunctionContext();
792 } else if (OutAttrs) {
793 ParseGNUAttributeArgs(AttrName: &LPA.AttrName, AttrNameLoc: LPA.AttrNameLoc, Attrs,
794 /*EndLoc=*/nullptr, /*ScopeName=*/nullptr,
795 ScopeLoc: SourceLocation(), Form: ParsedAttr::Form::GNU(),
796 /*D=*/nullptr);
797 } else {
798 Diag(Tok, DiagID: diag::warn_attribute_no_decl) << LPA.AttrName.getName();
799 }
800
801 if (OnDefinition && !Attrs.empty() && !Attrs.begin()->isCXX11Attribute() &&
802 Attrs.begin()->isKnownToGCC())
803 Diag(Tok, DiagID: diag::warn_attribute_on_function_definition) << &LPA.AttrName;
804
805 for (auto *D : LPA.Decls)
806 Actions.ActOnFinishDelayedAttribute(S: getCurScope(), D, Attrs);
807
808 // Due to a parsing error, we either went over the cached tokens or
809 // there are still cached tokens left, so we skip the leftover tokens.
810 while (Tok.isNot(K: tok::eof))
811 ConsumeAnyToken();
812
813 if (Tok.is(K: tok::eof) && Tok.getEofData() == AttrEnd.getEofData())
814 ConsumeAnyToken();
815
816 if (OutAttrs)
817 OutAttrs->takeAllAppendingFrom(Other&: Attrs);
818}
819
820void Parser::ParseLexedPragmas(ParsingClass &Class) {
821 ReenterClassScopeRAII InClassScope(*this, Class);
822
823 for (LateParsedDeclaration *D : Class.LateParsedDeclarations)
824 D->ParseLexedPragmas();
825}
826
827void Parser::ParseLexedPragma(LateParsedPragma &LP) {
828 PP.EnterToken(Tok, /*IsReinject=*/true);
829 PP.EnterTokenStream(Toks: LP.toks(), /*DisableMacroExpansion=*/true,
830 /*IsReinject=*/true);
831
832 // Consume the previously pushed token.
833 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
834 assert(Tok.isAnnotation() && "Expected annotation token.");
835 switch (Tok.getKind()) {
836 case tok::annot_attr_openmp:
837 case tok::annot_pragma_openmp: {
838 AccessSpecifier AS = LP.getAccessSpecifier();
839 ParsedAttributes Attrs(AttrFactory);
840 (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
841 break;
842 }
843 default:
844 llvm_unreachable("Unexpected token.");
845 }
846}
847
848bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
849 CachedTokens &Toks,
850 bool StopAtSemi, bool ConsumeFinalToken) {
851 // We always want this function to consume at least one token if the first
852 // token isn't T and if not at EOF.
853 bool isFirstTokenConsumed = true;
854 while (true) {
855 // If we found one of the tokens, stop and return true.
856 if (Tok.is(K: T1) || Tok.is(K: T2)) {
857 if (ConsumeFinalToken) {
858 Toks.push_back(Elt: Tok);
859 ConsumeAnyToken();
860 }
861 return true;
862 }
863
864 switch (Tok.getKind()) {
865 case tok::eof:
866 case tok::annot_module_begin:
867 case tok::annot_module_end:
868 case tok::annot_module_include:
869 case tok::annot_repl_input_end:
870 // Ran out of tokens.
871 return false;
872
873 case tok::annot_pragma_openacc:
874 case tok::annot_pragma_openmp:
875 case tok::annot_attr_openmp: {
876 // Ignore any tokens inside of a OMP/OpenACC pragma, as these should just
877 // be taken as 1.
878 tok::TokenKind EndKind = Tok.is(K: tok::annot_pragma_openacc)
879 ? tok::annot_pragma_openacc_end
880 : tok::annot_pragma_openmp_end;
881 Toks.push_back(Elt: Tok);
882 ConsumeAnnotationToken();
883 while (Tok.isNot(K: EndKind) && Tok.isNot(K: tok::eof)) {
884 Toks.push_back(Elt: Tok);
885 ConsumeAnyToken();
886 }
887 if (Tok.is(K: EndKind)) {
888 Toks.push_back(Elt: Tok);
889 ConsumeAnnotationToken();
890 }
891 break;
892 }
893
894 case tok::l_paren:
895 // Recursively consume properly-nested parens.
896 Toks.push_back(Elt: Tok);
897 ConsumeParen();
898 ConsumeAndStoreUntil(T1: tok::r_paren, Toks, /*StopAtSemi=*/false);
899 break;
900 case tok::l_square:
901 // Recursively consume properly-nested square brackets.
902 Toks.push_back(Elt: Tok);
903 ConsumeBracket();
904 ConsumeAndStoreUntil(T1: tok::r_square, Toks, /*StopAtSemi=*/false);
905 break;
906 case tok::l_brace:
907 // Recursively consume properly-nested braces.
908 Toks.push_back(Elt: Tok);
909 ConsumeBrace();
910 ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false);
911 break;
912
913 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
914 // Since the user wasn't looking for this token (if they were, it would
915 // already be handled), this isn't balanced. If there is a LHS token at a
916 // higher level, we will assume that this matches the unbalanced token
917 // and return it. Otherwise, this is a spurious RHS token, which we skip.
918 case tok::r_paren:
919 if (ParenCount && !isFirstTokenConsumed)
920 return false; // Matches something.
921 Toks.push_back(Elt: Tok);
922 ConsumeParen();
923 break;
924 case tok::r_square:
925 if (BracketCount && !isFirstTokenConsumed)
926 return false; // Matches something.
927 Toks.push_back(Elt: Tok);
928 ConsumeBracket();
929 break;
930 case tok::r_brace:
931 if (BraceCount && !isFirstTokenConsumed)
932 return false; // Matches something.
933 Toks.push_back(Elt: Tok);
934 ConsumeBrace();
935 break;
936
937 case tok::semi:
938 if (StopAtSemi)
939 return false;
940 [[fallthrough]];
941 default:
942 // consume this token.
943 Toks.push_back(Elt: Tok);
944 ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true);
945 break;
946 }
947 isFirstTokenConsumed = false;
948 }
949}
950
951bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
952 if (Tok.is(K: tok::kw_try)) {
953 Toks.push_back(Elt: Tok);
954 ConsumeToken();
955 }
956
957 if (Tok.isNot(K: tok::colon)) {
958 // Easy case, just a function body.
959
960 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
961 // brace: an opening one is the function body, while a closing one probably
962 // means we've reached the end of the class.
963 ConsumeAndStoreUntil(T1: tok::l_brace, T2: tok::r_brace, Toks,
964 /*StopAtSemi=*/true,
965 /*ConsumeFinalToken=*/false);
966 if (Tok.isNot(K: tok::l_brace))
967 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::l_brace;
968
969 Toks.push_back(Elt: Tok);
970 ConsumeBrace();
971 return false;
972 }
973
974 Toks.push_back(Elt: Tok);
975 ConsumeToken();
976
977 // We can't reliably skip over a mem-initializer-id, because it could be
978 // a template-id involving not-yet-declared names. Given:
979 //
980 // S ( ) : a < b < c > ( e )
981 //
982 // 'e' might be an initializer or part of a template argument, depending
983 // on whether 'b' is a template.
984
985 // Track whether we might be inside a template argument. We can give
986 // significantly better diagnostics if we know that we're not.
987 bool MightBeTemplateArgument = false;
988
989 while (true) {
990 // Skip over the mem-initializer-id, if possible.
991 if (Tok.is(K: tok::kw_decltype)) {
992 Toks.push_back(Elt: Tok);
993 SourceLocation OpenLoc = ConsumeToken();
994 if (Tok.isNot(K: tok::l_paren))
995 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_lparen_after)
996 << "decltype";
997 Toks.push_back(Elt: Tok);
998 ConsumeParen();
999 if (!ConsumeAndStoreUntil(T1: tok::r_paren, Toks, /*StopAtSemi=*/true)) {
1000 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::r_paren;
1001 Diag(Loc: OpenLoc, DiagID: diag::note_matching) << tok::l_paren;
1002 return true;
1003 }
1004 }
1005 do {
1006 // Walk over a component of a nested-name-specifier.
1007 if (Tok.is(K: tok::coloncolon)) {
1008 Toks.push_back(Elt: Tok);
1009 ConsumeToken();
1010
1011 if (Tok.is(K: tok::kw_template)) {
1012 Toks.push_back(Elt: Tok);
1013 ConsumeToken();
1014 }
1015 }
1016
1017 if (Tok.is(K: tok::identifier)) {
1018 Toks.push_back(Elt: Tok);
1019 ConsumeToken();
1020 } else {
1021 break;
1022 }
1023 // Pack indexing
1024 if (Tok.is(K: tok::ellipsis) && NextToken().is(K: tok::l_square)) {
1025 Toks.push_back(Elt: Tok);
1026 SourceLocation OpenLoc = ConsumeToken();
1027 Toks.push_back(Elt: Tok);
1028 ConsumeBracket();
1029 if (!ConsumeAndStoreUntil(T1: tok::r_square, Toks, /*StopAtSemi=*/true)) {
1030 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::r_square;
1031 Diag(Loc: OpenLoc, DiagID: diag::note_matching) << tok::l_square;
1032 return true;
1033 }
1034 }
1035
1036 } while (Tok.is(K: tok::coloncolon));
1037
1038 if (Tok.is(K: tok::code_completion)) {
1039 Toks.push_back(Elt: Tok);
1040 ConsumeCodeCompletionToken();
1041 if (Tok.isOneOf(Ks: tok::identifier, Ks: tok::coloncolon, Ks: tok::kw_decltype)) {
1042 // Could be the start of another member initializer (the ',' has not
1043 // been written yet)
1044 continue;
1045 }
1046 }
1047
1048 if (Tok.is(K: tok::comma)) {
1049 // The initialization is missing, we'll diagnose it later.
1050 Toks.push_back(Elt: Tok);
1051 ConsumeToken();
1052 continue;
1053 }
1054 if (Tok.is(K: tok::less))
1055 MightBeTemplateArgument = true;
1056
1057 if (MightBeTemplateArgument) {
1058 // We may be inside a template argument list. Grab up to the start of the
1059 // next parenthesized initializer or braced-init-list. This *might* be the
1060 // initializer, or it might be a subexpression in the template argument
1061 // list.
1062 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
1063 // if all angles are closed.
1064 if (!ConsumeAndStoreUntil(T1: tok::l_paren, T2: tok::l_brace, Toks,
1065 /*StopAtSemi=*/true,
1066 /*ConsumeFinalToken=*/false)) {
1067 // We're not just missing the initializer, we're also missing the
1068 // function body!
1069 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::l_brace;
1070 }
1071 } else if (Tok.isNot(K: tok::l_paren) && Tok.isNot(K: tok::l_brace)) {
1072 // We found something weird in a mem-initializer-id.
1073 if (getLangOpts().CPlusPlus11)
1074 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_either)
1075 << tok::l_paren << tok::l_brace;
1076 else
1077 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::l_paren;
1078 }
1079
1080 tok::TokenKind kind = Tok.getKind();
1081 Toks.push_back(Elt: Tok);
1082 bool IsLParen = (kind == tok::l_paren);
1083 SourceLocation OpenLoc = Tok.getLocation();
1084
1085 if (IsLParen) {
1086 ConsumeParen();
1087 } else {
1088 assert(kind == tok::l_brace && "Must be left paren or brace here.");
1089 ConsumeBrace();
1090 // In C++03, this has to be the start of the function body, which
1091 // means the initializer is malformed; we'll diagnose it later.
1092 if (!getLangOpts().CPlusPlus11)
1093 return false;
1094
1095 const Token &PreviousToken = Toks[Toks.size() - 2];
1096 if (!MightBeTemplateArgument &&
1097 !PreviousToken.isOneOf(Ks: tok::identifier, Ks: tok::greater,
1098 Ks: tok::greatergreater)) {
1099 // If the opening brace is not preceded by one of these tokens, we are
1100 // missing the mem-initializer-id. In order to recover better, we need
1101 // to use heuristics to determine if this '{' is most likely the
1102 // beginning of a brace-init-list or the function body.
1103 // Check the token after the corresponding '}'.
1104 TentativeParsingAction PA(*this);
1105 if (SkipUntil(T: tok::r_brace) &&
1106 !Tok.isOneOf(Ks: tok::comma, Ks: tok::ellipsis, Ks: tok::l_brace)) {
1107 // Consider there was a malformed initializer and this is the start
1108 // of the function body. We'll diagnose it later.
1109 PA.Revert();
1110 return false;
1111 }
1112 PA.Revert();
1113 }
1114 }
1115
1116 // Grab the initializer (or the subexpression of the template argument).
1117 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
1118 // if we might be inside the braces of a lambda-expression.
1119 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
1120 if (!ConsumeAndStoreUntil(T1: CloseKind, Toks, /*StopAtSemi=*/true)) {
1121 Diag(Tok, DiagID: diag::err_expected) << CloseKind;
1122 Diag(Loc: OpenLoc, DiagID: diag::note_matching) << kind;
1123 return true;
1124 }
1125
1126 // Grab pack ellipsis, if present.
1127 if (Tok.is(K: tok::ellipsis)) {
1128 Toks.push_back(Elt: Tok);
1129 ConsumeToken();
1130 }
1131
1132 // If we know we just consumed a mem-initializer, we must have ',' or '{'
1133 // next.
1134 if (Tok.is(K: tok::comma)) {
1135 Toks.push_back(Elt: Tok);
1136 ConsumeToken();
1137 } else if (Tok.is(K: tok::l_brace)) {
1138 // This is the function body if the ')' or '}' is immediately followed by
1139 // a '{'. That cannot happen within a template argument, apart from the
1140 // case where a template argument contains a compound literal:
1141 //
1142 // S ( ) : a < b < c > ( d ) { }
1143 // // End of declaration, or still inside the template argument?
1144 //
1145 // ... and the case where the template argument contains a lambda:
1146 //
1147 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
1148 // ( ) > ( ) { }
1149 //
1150 // FIXME: Disambiguate these cases. Note that the latter case is probably
1151 // going to be made ill-formed by core issue 1607.
1152 Toks.push_back(Elt: Tok);
1153 ConsumeBrace();
1154 return false;
1155 } else if (!MightBeTemplateArgument) {
1156 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_either) << tok::l_brace
1157 << tok::comma;
1158 }
1159 }
1160}
1161
1162bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
1163 // Consume '?'.
1164 assert(Tok.is(tok::question));
1165 Toks.push_back(Elt: Tok);
1166 ConsumeToken();
1167
1168 while (Tok.isNot(K: tok::colon)) {
1169 if (!ConsumeAndStoreUntil(T1: tok::question, T2: tok::colon, Toks,
1170 /*StopAtSemi=*/true,
1171 /*ConsumeFinalToken=*/false))
1172 return false;
1173
1174 // If we found a nested conditional, consume it.
1175 if (Tok.is(K: tok::question) && !ConsumeAndStoreConditional(Toks))
1176 return false;
1177 }
1178
1179 // Consume ':'.
1180 Toks.push_back(Elt: Tok);
1181 ConsumeToken();
1182 return true;
1183}
1184
1185bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
1186 CachedInitKind CIK) {
1187 // We always want this function to consume at least one token if not at EOF.
1188 bool IsFirstToken = true;
1189
1190 // Number of possible unclosed <s we've seen so far. These might be templates,
1191 // and might not, but if there were none of them (or we know for sure that
1192 // we're within a template), we can avoid a tentative parse.
1193 unsigned AngleCount = 0;
1194 unsigned KnownTemplateCount = 0;
1195
1196 while (true) {
1197 switch (Tok.getKind()) {
1198 case tok::ellipsis:
1199 // We found an elipsis at the end of the parameter list;
1200 // it is not part of a parameter declaration.
1201 if (ParenCount == 1 && NextToken().is(K: tok::r_paren))
1202 return true;
1203 goto consume_token;
1204 case tok::comma:
1205 // If we might be in a template, perform a tentative parse to check.
1206 if (!AngleCount)
1207 // Not a template argument: this is the end of the initializer.
1208 return true;
1209 if (KnownTemplateCount)
1210 goto consume_token;
1211
1212 // We hit a comma inside angle brackets. This is the hard case. The
1213 // rule we follow is:
1214 // * For a default argument, if the tokens after the comma form a
1215 // syntactically-valid parameter-declaration-clause, in which each
1216 // parameter has an initializer, then this comma ends the default
1217 // argument.
1218 // * For a default initializer, if the tokens after the comma form a
1219 // syntactically-valid init-declarator-list, then this comma ends
1220 // the default initializer.
1221 {
1222 TentativeParsingAction TPA(*this, /*Unannotated=*/true);
1223 Sema::TentativeAnalysisScope Scope(Actions);
1224
1225 TPResult Result = TPResult::Error;
1226 ConsumeToken();
1227 switch (CIK) {
1228 case CachedInitKind::DefaultInitializer:
1229 Result = TryParseInitDeclaratorList();
1230 // If we parsed a complete, ambiguous init-declarator-list, this
1231 // is only syntactically-valid if it's followed by a semicolon.
1232 if (Result == TPResult::Ambiguous && Tok.isNot(K: tok::semi))
1233 Result = TPResult::False;
1234 break;
1235
1236 case CachedInitKind::DefaultArgument:
1237 bool InvalidAsDeclaration = false;
1238 Result = TryParseParameterDeclarationClause(
1239 InvalidAsDeclaration: &InvalidAsDeclaration, /*VersusTemplateArg=*/true);
1240 // If this is an expression or a declaration with a missing
1241 // 'typename', assume it's not a declaration.
1242 if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1243 Result = TPResult::False;
1244 break;
1245 }
1246
1247 // Put the token stream back and undo any annotations we performed
1248 // after the comma. They may reflect a different parse than the one
1249 // we will actually perform at the end of the class.
1250 TPA.Revert();
1251
1252 // If what follows could be a declaration, it is a declaration.
1253 if (Result != TPResult::False && Result != TPResult::Error)
1254 return true;
1255 }
1256
1257 // Keep going. We know we're inside a template argument list now.
1258 ++KnownTemplateCount;
1259 goto consume_token;
1260
1261 case tok::eof:
1262 // Ran out of tokens.
1263 return false;
1264
1265 case tok::less:
1266 // FIXME: A '<' can only start a template-id if it's preceded by an
1267 // identifier, an operator-function-id, or a literal-operator-id.
1268 ++AngleCount;
1269 goto consume_token;
1270
1271 case tok::question:
1272 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1273 // that is *never* the end of the initializer. Skip to the ':'.
1274 if (!ConsumeAndStoreConditional(Toks))
1275 return false;
1276 break;
1277
1278 case tok::greatergreatergreater:
1279 if (!getLangOpts().CPlusPlus11)
1280 goto consume_token;
1281 if (AngleCount) --AngleCount;
1282 if (KnownTemplateCount) --KnownTemplateCount;
1283 [[fallthrough]];
1284 case tok::greatergreater:
1285 if (!getLangOpts().CPlusPlus11)
1286 goto consume_token;
1287 if (AngleCount) --AngleCount;
1288 if (KnownTemplateCount) --KnownTemplateCount;
1289 [[fallthrough]];
1290 case tok::greater:
1291 if (AngleCount) --AngleCount;
1292 if (KnownTemplateCount) --KnownTemplateCount;
1293 goto consume_token;
1294
1295 case tok::kw_template:
1296 // 'template' identifier '<' is known to start a template argument list,
1297 // and can be used to disambiguate the parse.
1298 // FIXME: Support all forms of 'template' unqualified-id '<'.
1299 Toks.push_back(Elt: Tok);
1300 ConsumeToken();
1301 if (Tok.is(K: tok::identifier)) {
1302 Toks.push_back(Elt: Tok);
1303 ConsumeToken();
1304 if (Tok.is(K: tok::less)) {
1305 ++AngleCount;
1306 ++KnownTemplateCount;
1307 Toks.push_back(Elt: Tok);
1308 ConsumeToken();
1309 }
1310 }
1311 break;
1312
1313 case tok::kw_operator:
1314 // If 'operator' precedes other punctuation, that punctuation loses
1315 // its special behavior.
1316 Toks.push_back(Elt: Tok);
1317 ConsumeToken();
1318 switch (Tok.getKind()) {
1319 case tok::comma:
1320 case tok::greatergreatergreater:
1321 case tok::greatergreater:
1322 case tok::greater:
1323 case tok::less:
1324 Toks.push_back(Elt: Tok);
1325 ConsumeToken();
1326 break;
1327 default:
1328 break;
1329 }
1330 break;
1331
1332 case tok::l_paren:
1333 // Recursively consume properly-nested parens.
1334 Toks.push_back(Elt: Tok);
1335 ConsumeParen();
1336 ConsumeAndStoreUntil(T1: tok::r_paren, Toks, /*StopAtSemi=*/false);
1337 break;
1338 case tok::l_square:
1339 // Recursively consume properly-nested square brackets.
1340 Toks.push_back(Elt: Tok);
1341 ConsumeBracket();
1342 ConsumeAndStoreUntil(T1: tok::r_square, Toks, /*StopAtSemi=*/false);
1343 break;
1344 case tok::l_brace:
1345 // Recursively consume properly-nested braces.
1346 Toks.push_back(Elt: Tok);
1347 ConsumeBrace();
1348 ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false);
1349 break;
1350
1351 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1352 // Since the user wasn't looking for this token (if they were, it would
1353 // already be handled), this isn't balanced. If there is a LHS token at a
1354 // higher level, we will assume that this matches the unbalanced token
1355 // and return it. Otherwise, this is a spurious RHS token, which we
1356 // consume and pass on to downstream code to diagnose.
1357 case tok::r_paren:
1358 if (CIK == CachedInitKind::DefaultArgument)
1359 return true; // End of the default argument.
1360 if (ParenCount && !IsFirstToken)
1361 return false;
1362 Toks.push_back(Elt: Tok);
1363 ConsumeParen();
1364 continue;
1365 case tok::r_square:
1366 if (BracketCount && !IsFirstToken)
1367 return false;
1368 Toks.push_back(Elt: Tok);
1369 ConsumeBracket();
1370 continue;
1371 case tok::r_brace:
1372 if (BraceCount && !IsFirstToken)
1373 return false;
1374 Toks.push_back(Elt: Tok);
1375 ConsumeBrace();
1376 continue;
1377
1378 case tok::code_completion:
1379 Toks.push_back(Elt: Tok);
1380 ConsumeCodeCompletionToken();
1381 break;
1382
1383 case tok::string_literal:
1384 case tok::wide_string_literal:
1385 case tok::utf8_string_literal:
1386 case tok::utf16_string_literal:
1387 case tok::utf32_string_literal:
1388 Toks.push_back(Elt: Tok);
1389 ConsumeStringToken();
1390 break;
1391 case tok::semi:
1392 if (CIK == CachedInitKind::DefaultInitializer)
1393 return true; // End of the default initializer.
1394 [[fallthrough]];
1395 default:
1396 consume_token:
1397 // If it's an annotation token, then we've run out of tokens and should
1398 // bail out. Otherwise, cache the token and consume it.
1399 if (Tok.isAnnotation())
1400 return false;
1401
1402 Toks.push_back(Elt: Tok);
1403 ConsumeToken();
1404 break;
1405 }
1406 IsFirstToken = false;
1407 }
1408}
1409