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

source code of clang/lib/Parse/ParseCXXInlineMethods.cpp