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