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::l_paren:
874 // Recursively consume properly-nested parens.
875 Toks.push_back(Elt: Tok);
876 ConsumeParen();
877 ConsumeAndStoreUntil(T1: tok::r_paren, Toks, /*StopAtSemi=*/false);
878 break;
879 case tok::l_square:
880 // Recursively consume properly-nested square brackets.
881 Toks.push_back(Elt: Tok);
882 ConsumeBracket();
883 ConsumeAndStoreUntil(T1: tok::r_square, Toks, /*StopAtSemi=*/false);
884 break;
885 case tok::l_brace:
886 // Recursively consume properly-nested braces.
887 Toks.push_back(Elt: Tok);
888 ConsumeBrace();
889 ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false);
890 break;
891
892 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
893 // Since the user wasn't looking for this token (if they were, it would
894 // already be handled), this isn't balanced. If there is a LHS token at a
895 // higher level, we will assume that this matches the unbalanced token
896 // and return it. Otherwise, this is a spurious RHS token, which we skip.
897 case tok::r_paren:
898 if (ParenCount && !isFirstTokenConsumed)
899 return false; // Matches something.
900 Toks.push_back(Elt: Tok);
901 ConsumeParen();
902 break;
903 case tok::r_square:
904 if (BracketCount && !isFirstTokenConsumed)
905 return false; // Matches something.
906 Toks.push_back(Elt: Tok);
907 ConsumeBracket();
908 break;
909 case tok::r_brace:
910 if (BraceCount && !isFirstTokenConsumed)
911 return false; // Matches something.
912 Toks.push_back(Elt: Tok);
913 ConsumeBrace();
914 break;
915
916 case tok::semi:
917 if (StopAtSemi)
918 return false;
919 [[fallthrough]];
920 default:
921 // consume this token.
922 Toks.push_back(Elt: Tok);
923 ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true);
924 break;
925 }
926 isFirstTokenConsumed = false;
927 }
928}
929
930bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
931 if (Tok.is(K: tok::kw_try)) {
932 Toks.push_back(Elt: Tok);
933 ConsumeToken();
934 }
935
936 if (Tok.isNot(K: tok::colon)) {
937 // Easy case, just a function body.
938
939 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
940 // brace: an opening one is the function body, while a closing one probably
941 // means we've reached the end of the class.
942 ConsumeAndStoreUntil(T1: tok::l_brace, T2: tok::r_brace, Toks,
943 /*StopAtSemi=*/true,
944 /*ConsumeFinalToken=*/false);
945 if (Tok.isNot(K: tok::l_brace))
946 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::l_brace;
947
948 Toks.push_back(Elt: Tok);
949 ConsumeBrace();
950 return false;
951 }
952
953 Toks.push_back(Elt: Tok);
954 ConsumeToken();
955
956 // We can't reliably skip over a mem-initializer-id, because it could be
957 // a template-id involving not-yet-declared names. Given:
958 //
959 // S ( ) : a < b < c > ( e )
960 //
961 // 'e' might be an initializer or part of a template argument, depending
962 // on whether 'b' is a template.
963
964 // Track whether we might be inside a template argument. We can give
965 // significantly better diagnostics if we know that we're not.
966 bool MightBeTemplateArgument = false;
967
968 while (true) {
969 // Skip over the mem-initializer-id, if possible.
970 if (Tok.is(K: tok::kw_decltype)) {
971 Toks.push_back(Elt: Tok);
972 SourceLocation OpenLoc = ConsumeToken();
973 if (Tok.isNot(K: tok::l_paren))
974 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_lparen_after)
975 << "decltype";
976 Toks.push_back(Elt: Tok);
977 ConsumeParen();
978 if (!ConsumeAndStoreUntil(T1: tok::r_paren, Toks, /*StopAtSemi=*/true)) {
979 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::r_paren;
980 Diag(Loc: OpenLoc, DiagID: diag::note_matching) << tok::l_paren;
981 return true;
982 }
983 }
984 do {
985 // Walk over a component of a nested-name-specifier.
986 if (Tok.is(K: tok::coloncolon)) {
987 Toks.push_back(Elt: Tok);
988 ConsumeToken();
989
990 if (Tok.is(K: tok::kw_template)) {
991 Toks.push_back(Elt: Tok);
992 ConsumeToken();
993 }
994 }
995
996 if (Tok.is(K: tok::identifier)) {
997 Toks.push_back(Elt: Tok);
998 ConsumeToken();
999 } else {
1000 break;
1001 }
1002 // Pack indexing
1003 if (Tok.is(K: tok::ellipsis) && NextToken().is(K: tok::l_square)) {
1004 Toks.push_back(Elt: Tok);
1005 SourceLocation OpenLoc = ConsumeToken();
1006 Toks.push_back(Elt: Tok);
1007 ConsumeBracket();
1008 if (!ConsumeAndStoreUntil(T1: tok::r_square, Toks, /*StopAtSemi=*/true)) {
1009 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::r_square;
1010 Diag(Loc: OpenLoc, DiagID: diag::note_matching) << tok::l_square;
1011 return true;
1012 }
1013 }
1014
1015 } while (Tok.is(K: tok::coloncolon));
1016
1017 if (Tok.is(K: tok::code_completion)) {
1018 Toks.push_back(Elt: Tok);
1019 ConsumeCodeCompletionToken();
1020 if (Tok.isOneOf(Ks: tok::identifier, Ks: tok::coloncolon, Ks: tok::kw_decltype)) {
1021 // Could be the start of another member initializer (the ',' has not
1022 // been written yet)
1023 continue;
1024 }
1025 }
1026
1027 if (Tok.is(K: tok::comma)) {
1028 // The initialization is missing, we'll diagnose it later.
1029 Toks.push_back(Elt: Tok);
1030 ConsumeToken();
1031 continue;
1032 }
1033 if (Tok.is(K: tok::less))
1034 MightBeTemplateArgument = true;
1035
1036 if (MightBeTemplateArgument) {
1037 // We may be inside a template argument list. Grab up to the start of the
1038 // next parenthesized initializer or braced-init-list. This *might* be the
1039 // initializer, or it might be a subexpression in the template argument
1040 // list.
1041 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
1042 // if all angles are closed.
1043 if (!ConsumeAndStoreUntil(T1: tok::l_paren, T2: tok::l_brace, Toks,
1044 /*StopAtSemi=*/true,
1045 /*ConsumeFinalToken=*/false)) {
1046 // We're not just missing the initializer, we're also missing the
1047 // function body!
1048 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::l_brace;
1049 }
1050 } else if (Tok.isNot(K: tok::l_paren) && Tok.isNot(K: tok::l_brace)) {
1051 // We found something weird in a mem-initializer-id.
1052 if (getLangOpts().CPlusPlus11)
1053 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_either)
1054 << tok::l_paren << tok::l_brace;
1055 else
1056 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::l_paren;
1057 }
1058
1059 tok::TokenKind kind = Tok.getKind();
1060 Toks.push_back(Elt: Tok);
1061 bool IsLParen = (kind == tok::l_paren);
1062 SourceLocation OpenLoc = Tok.getLocation();
1063
1064 if (IsLParen) {
1065 ConsumeParen();
1066 } else {
1067 assert(kind == tok::l_brace && "Must be left paren or brace here.");
1068 ConsumeBrace();
1069 // In C++03, this has to be the start of the function body, which
1070 // means the initializer is malformed; we'll diagnose it later.
1071 if (!getLangOpts().CPlusPlus11)
1072 return false;
1073
1074 const Token &PreviousToken = Toks[Toks.size() - 2];
1075 if (!MightBeTemplateArgument &&
1076 !PreviousToken.isOneOf(Ks: tok::identifier, Ks: tok::greater,
1077 Ks: tok::greatergreater)) {
1078 // If the opening brace is not preceded by one of these tokens, we are
1079 // missing the mem-initializer-id. In order to recover better, we need
1080 // to use heuristics to determine if this '{' is most likely the
1081 // beginning of a brace-init-list or the function body.
1082 // Check the token after the corresponding '}'.
1083 TentativeParsingAction PA(*this);
1084 if (SkipUntil(T: tok::r_brace) &&
1085 !Tok.isOneOf(Ks: tok::comma, Ks: tok::ellipsis, Ks: tok::l_brace)) {
1086 // Consider there was a malformed initializer and this is the start
1087 // of the function body. We'll diagnose it later.
1088 PA.Revert();
1089 return false;
1090 }
1091 PA.Revert();
1092 }
1093 }
1094
1095 // Grab the initializer (or the subexpression of the template argument).
1096 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
1097 // if we might be inside the braces of a lambda-expression.
1098 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
1099 if (!ConsumeAndStoreUntil(T1: CloseKind, Toks, /*StopAtSemi=*/true)) {
1100 Diag(Tok, DiagID: diag::err_expected) << CloseKind;
1101 Diag(Loc: OpenLoc, DiagID: diag::note_matching) << kind;
1102 return true;
1103 }
1104
1105 // Grab pack ellipsis, if present.
1106 if (Tok.is(K: tok::ellipsis)) {
1107 Toks.push_back(Elt: Tok);
1108 ConsumeToken();
1109 }
1110
1111 // If we know we just consumed a mem-initializer, we must have ',' or '{'
1112 // next.
1113 if (Tok.is(K: tok::comma)) {
1114 Toks.push_back(Elt: Tok);
1115 ConsumeToken();
1116 } else if (Tok.is(K: tok::l_brace)) {
1117 // This is the function body if the ')' or '}' is immediately followed by
1118 // a '{'. That cannot happen within a template argument, apart from the
1119 // case where a template argument contains a compound literal:
1120 //
1121 // S ( ) : a < b < c > ( d ) { }
1122 // // End of declaration, or still inside the template argument?
1123 //
1124 // ... and the case where the template argument contains a lambda:
1125 //
1126 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
1127 // ( ) > ( ) { }
1128 //
1129 // FIXME: Disambiguate these cases. Note that the latter case is probably
1130 // going to be made ill-formed by core issue 1607.
1131 Toks.push_back(Elt: Tok);
1132 ConsumeBrace();
1133 return false;
1134 } else if (!MightBeTemplateArgument) {
1135 return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_either) << tok::l_brace
1136 << tok::comma;
1137 }
1138 }
1139}
1140
1141bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
1142 // Consume '?'.
1143 assert(Tok.is(tok::question));
1144 Toks.push_back(Elt: Tok);
1145 ConsumeToken();
1146
1147 while (Tok.isNot(K: tok::colon)) {
1148 if (!ConsumeAndStoreUntil(T1: tok::question, T2: tok::colon, Toks,
1149 /*StopAtSemi=*/true,
1150 /*ConsumeFinalToken=*/false))
1151 return false;
1152
1153 // If we found a nested conditional, consume it.
1154 if (Tok.is(K: tok::question) && !ConsumeAndStoreConditional(Toks))
1155 return false;
1156 }
1157
1158 // Consume ':'.
1159 Toks.push_back(Elt: Tok);
1160 ConsumeToken();
1161 return true;
1162}
1163
1164bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
1165 CachedInitKind CIK) {
1166 // We always want this function to consume at least one token if not at EOF.
1167 bool IsFirstToken = true;
1168
1169 // Number of possible unclosed <s we've seen so far. These might be templates,
1170 // and might not, but if there were none of them (or we know for sure that
1171 // we're within a template), we can avoid a tentative parse.
1172 unsigned AngleCount = 0;
1173 unsigned KnownTemplateCount = 0;
1174
1175 while (true) {
1176 switch (Tok.getKind()) {
1177 case tok::ellipsis:
1178 // We found an elipsis at the end of the parameter list;
1179 // it is not part of a parameter declaration.
1180 if (ParenCount == 1 && NextToken().is(K: tok::r_paren))
1181 return true;
1182 goto consume_token;
1183 case tok::comma:
1184 // If we might be in a template, perform a tentative parse to check.
1185 if (!AngleCount)
1186 // Not a template argument: this is the end of the initializer.
1187 return true;
1188 if (KnownTemplateCount)
1189 goto consume_token;
1190
1191 // We hit a comma inside angle brackets. This is the hard case. The
1192 // rule we follow is:
1193 // * For a default argument, if the tokens after the comma form a
1194 // syntactically-valid parameter-declaration-clause, in which each
1195 // parameter has an initializer, then this comma ends the default
1196 // argument.
1197 // * For a default initializer, if the tokens after the comma form a
1198 // syntactically-valid init-declarator-list, then this comma ends
1199 // the default initializer.
1200 {
1201 TentativeParsingAction TPA(*this, /*Unannotated=*/true);
1202 Sema::TentativeAnalysisScope Scope(Actions);
1203
1204 TPResult Result = TPResult::Error;
1205 ConsumeToken();
1206 switch (CIK) {
1207 case CachedInitKind::DefaultInitializer:
1208 Result = TryParseInitDeclaratorList();
1209 // If we parsed a complete, ambiguous init-declarator-list, this
1210 // is only syntactically-valid if it's followed by a semicolon.
1211 if (Result == TPResult::Ambiguous && Tok.isNot(K: tok::semi))
1212 Result = TPResult::False;
1213 break;
1214
1215 case CachedInitKind::DefaultArgument:
1216 bool InvalidAsDeclaration = false;
1217 Result = TryParseParameterDeclarationClause(
1218 InvalidAsDeclaration: &InvalidAsDeclaration, /*VersusTemplateArg=*/true);
1219 // If this is an expression or a declaration with a missing
1220 // 'typename', assume it's not a declaration.
1221 if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1222 Result = TPResult::False;
1223 break;
1224 }
1225
1226 // Put the token stream back and undo any annotations we performed
1227 // after the comma. They may reflect a different parse than the one
1228 // we will actually perform at the end of the class.
1229 TPA.Revert();
1230
1231 // If what follows could be a declaration, it is a declaration.
1232 if (Result != TPResult::False && Result != TPResult::Error)
1233 return true;
1234 }
1235
1236 // Keep going. We know we're inside a template argument list now.
1237 ++KnownTemplateCount;
1238 goto consume_token;
1239
1240 case tok::eof:
1241 // Ran out of tokens.
1242 return false;
1243
1244 case tok::less:
1245 // FIXME: A '<' can only start a template-id if it's preceded by an
1246 // identifier, an operator-function-id, or a literal-operator-id.
1247 ++AngleCount;
1248 goto consume_token;
1249
1250 case tok::question:
1251 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1252 // that is *never* the end of the initializer. Skip to the ':'.
1253 if (!ConsumeAndStoreConditional(Toks))
1254 return false;
1255 break;
1256
1257 case tok::greatergreatergreater:
1258 if (!getLangOpts().CPlusPlus11)
1259 goto consume_token;
1260 if (AngleCount) --AngleCount;
1261 if (KnownTemplateCount) --KnownTemplateCount;
1262 [[fallthrough]];
1263 case tok::greatergreater:
1264 if (!getLangOpts().CPlusPlus11)
1265 goto consume_token;
1266 if (AngleCount) --AngleCount;
1267 if (KnownTemplateCount) --KnownTemplateCount;
1268 [[fallthrough]];
1269 case tok::greater:
1270 if (AngleCount) --AngleCount;
1271 if (KnownTemplateCount) --KnownTemplateCount;
1272 goto consume_token;
1273
1274 case tok::kw_template:
1275 // 'template' identifier '<' is known to start a template argument list,
1276 // and can be used to disambiguate the parse.
1277 // FIXME: Support all forms of 'template' unqualified-id '<'.
1278 Toks.push_back(Elt: Tok);
1279 ConsumeToken();
1280 if (Tok.is(K: tok::identifier)) {
1281 Toks.push_back(Elt: Tok);
1282 ConsumeToken();
1283 if (Tok.is(K: tok::less)) {
1284 ++AngleCount;
1285 ++KnownTemplateCount;
1286 Toks.push_back(Elt: Tok);
1287 ConsumeToken();
1288 }
1289 }
1290 break;
1291
1292 case tok::kw_operator:
1293 // If 'operator' precedes other punctuation, that punctuation loses
1294 // its special behavior.
1295 Toks.push_back(Elt: Tok);
1296 ConsumeToken();
1297 switch (Tok.getKind()) {
1298 case tok::comma:
1299 case tok::greatergreatergreater:
1300 case tok::greatergreater:
1301 case tok::greater:
1302 case tok::less:
1303 Toks.push_back(Elt: Tok);
1304 ConsumeToken();
1305 break;
1306 default:
1307 break;
1308 }
1309 break;
1310
1311 case tok::l_paren:
1312 // Recursively consume properly-nested parens.
1313 Toks.push_back(Elt: Tok);
1314 ConsumeParen();
1315 ConsumeAndStoreUntil(T1: tok::r_paren, Toks, /*StopAtSemi=*/false);
1316 break;
1317 case tok::l_square:
1318 // Recursively consume properly-nested square brackets.
1319 Toks.push_back(Elt: Tok);
1320 ConsumeBracket();
1321 ConsumeAndStoreUntil(T1: tok::r_square, Toks, /*StopAtSemi=*/false);
1322 break;
1323 case tok::l_brace:
1324 // Recursively consume properly-nested braces.
1325 Toks.push_back(Elt: Tok);
1326 ConsumeBrace();
1327 ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false);
1328 break;
1329
1330 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1331 // Since the user wasn't looking for this token (if they were, it would
1332 // already be handled), this isn't balanced. If there is a LHS token at a
1333 // higher level, we will assume that this matches the unbalanced token
1334 // and return it. Otherwise, this is a spurious RHS token, which we
1335 // consume and pass on to downstream code to diagnose.
1336 case tok::r_paren:
1337 if (CIK == CachedInitKind::DefaultArgument)
1338 return true; // End of the default argument.
1339 if (ParenCount && !IsFirstToken)
1340 return false;
1341 Toks.push_back(Elt: Tok);
1342 ConsumeParen();
1343 continue;
1344 case tok::r_square:
1345 if (BracketCount && !IsFirstToken)
1346 return false;
1347 Toks.push_back(Elt: Tok);
1348 ConsumeBracket();
1349 continue;
1350 case tok::r_brace:
1351 if (BraceCount && !IsFirstToken)
1352 return false;
1353 Toks.push_back(Elt: Tok);
1354 ConsumeBrace();
1355 continue;
1356
1357 case tok::code_completion:
1358 Toks.push_back(Elt: Tok);
1359 ConsumeCodeCompletionToken();
1360 break;
1361
1362 case tok::string_literal:
1363 case tok::wide_string_literal:
1364 case tok::utf8_string_literal:
1365 case tok::utf16_string_literal:
1366 case tok::utf32_string_literal:
1367 Toks.push_back(Elt: Tok);
1368 ConsumeStringToken();
1369 break;
1370 case tok::semi:
1371 if (CIK == CachedInitKind::DefaultInitializer)
1372 return true; // End of the default initializer.
1373 [[fallthrough]];
1374 default:
1375 consume_token:
1376 // If it's an annotation token, then we've run out of tokens and should
1377 // bail out. Otherwise, cache the token and consume it.
1378 if (Tok.isAnnotation())
1379 return false;
1380
1381 Toks.push_back(Elt: Tok);
1382 ConsumeToken();
1383 break;
1384 }
1385 IsFirstToken = false;
1386 }
1387}
1388