[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / lib / Parse / ParseCXXInlineMethods.cpp
blob3a7f5426d4a704330f83294e8153c862c4c8d6c5
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.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Parse/Parser.h"
14 #include "clang/AST/DeclTemplate.h"
15 #include "clang/Parse/ParseDiagnostic.h"
16 #include "clang/Parse/RAIIObjectsForParser.h"
17 #include "clang/Sema/DeclSpec.h"
18 #include "clang/Sema/Scope.h"
19 using namespace clang;
21 /// ParseCXXInlineMethodDef - We parsed and verified that the specified
22 /// Declarator is a well formed C++ inline method definition. Now lex its body
23 /// and store its tokens for parsing after the C++ class is complete.
24 NamedDecl *Parser::ParseCXXInlineMethodDef(
25 AccessSpecifier AS, const ParsedAttributesView &AccessAttrs,
26 ParsingDeclarator &D, const ParsedTemplateInfo &TemplateInfo,
27 const VirtSpecifiers &VS, SourceLocation PureSpecLoc) {
28 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
29 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
30 "Current token not a '{', ':', '=', or 'try'!");
32 MultiTemplateParamsArg TemplateParams(
33 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
34 : nullptr,
35 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
37 NamedDecl *FnD;
38 if (D.getDeclSpec().isFriendSpecified())
39 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
40 TemplateParams);
41 else {
42 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
43 TemplateParams, nullptr,
44 VS, ICIS_NoInit);
45 if (FnD) {
46 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
47 if (PureSpecLoc.isValid())
48 Actions.ActOnPureSpecifier(FnD, PureSpecLoc);
52 if (FnD)
53 HandleMemberFunctionDeclDelays(D, FnD);
55 D.complete(FnD);
57 if (TryConsumeToken(tok::equal)) {
58 if (!FnD) {
59 SkipUntil(tok::semi);
60 return nullptr;
63 bool Delete = false;
64 SourceLocation KWLoc;
65 SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
66 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
67 Diag(KWLoc, getLangOpts().CPlusPlus11
68 ? diag::warn_cxx98_compat_defaulted_deleted_function
69 : diag::ext_defaulted_deleted_function)
70 << 1 /* deleted */;
71 Actions.SetDeclDeleted(FnD, KWLoc);
72 Delete = true;
73 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
74 DeclAsFunction->setRangeEnd(KWEndLoc);
76 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
77 Diag(KWLoc, getLangOpts().CPlusPlus11
78 ? diag::warn_cxx98_compat_defaulted_deleted_function
79 : diag::ext_defaulted_deleted_function)
80 << 0 /* defaulted */;
81 Actions.SetDeclDefaulted(FnD, KWLoc);
82 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
83 DeclAsFunction->setRangeEnd(KWEndLoc);
85 } else {
86 llvm_unreachable("function definition after = not 'delete' or 'default'");
89 if (Tok.is(tok::comma)) {
90 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
91 << Delete;
92 SkipUntil(tok::semi);
93 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
94 Delete ? "delete" : "default")) {
95 SkipUntil(tok::semi);
98 return FnD;
101 if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) &&
102 trySkippingFunctionBody()) {
103 Actions.ActOnSkippedFunctionBody(FnD);
104 return FnD;
107 // In delayed template parsing mode, if we are within a class template
108 // or if we are about to parse function member template then consume
109 // the tokens and store them for parsing at the end of the translation unit.
110 if (getLangOpts().DelayedTemplateParsing &&
111 D.getFunctionDefinitionKind() == FunctionDefinitionKind::Definition &&
112 !D.getDeclSpec().hasConstexprSpecifier() &&
113 !(FnD && FnD->getAsFunction() &&
114 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
115 ((Actions.CurContext->isDependentContext() ||
116 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
117 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
118 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
120 CachedTokens Toks;
121 LexTemplateFunctionForLateParsing(Toks);
123 if (FnD) {
124 FunctionDecl *FD = FnD->getAsFunction();
125 Actions.CheckForFunctionRedefinition(FD);
126 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
129 return FnD;
132 // Consume the tokens and store them for later parsing.
134 LexedMethod* LM = new LexedMethod(this, FnD);
135 getCurrentClass().LateParsedDeclarations.push_back(LM);
136 CachedTokens &Toks = LM->Toks;
138 tok::TokenKind kind = Tok.getKind();
139 // Consume everything up to (and including) the left brace of the
140 // function body.
141 if (ConsumeAndStoreFunctionPrologue(Toks)) {
142 // We didn't find the left-brace we expected after the
143 // constructor initializer.
145 // If we're code-completing and the completion point was in the broken
146 // initializer, we want to parse it even though that will fail.
147 if (PP.isCodeCompletionEnabled() &&
148 llvm::any_of(Toks, [](const Token &Tok) {
149 return Tok.is(tok::code_completion);
150 })) {
151 // If we gave up at the completion point, the initializer list was
152 // likely truncated, so don't eat more tokens. We'll hit some extra
153 // errors, but they should be ignored in code completion.
154 return FnD;
157 // We already printed an error, and it's likely impossible to recover,
158 // so don't try to parse this method later.
159 // Skip over the rest of the decl and back to somewhere that looks
160 // reasonable.
161 SkipMalformedDecl();
162 delete getCurrentClass().LateParsedDeclarations.back();
163 getCurrentClass().LateParsedDeclarations.pop_back();
164 return FnD;
165 } else {
166 // Consume everything up to (and including) the matching right brace.
167 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
170 // If we're in a function-try-block, we need to store all the catch blocks.
171 if (kind == tok::kw_try) {
172 while (Tok.is(tok::kw_catch)) {
173 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
174 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
178 if (FnD) {
179 FunctionDecl *FD = FnD->getAsFunction();
180 // Track that this function will eventually have a body; Sema needs
181 // to know this.
182 Actions.CheckForFunctionRedefinition(FD);
183 FD->setWillHaveBody(true);
184 } else {
185 // If semantic analysis could not build a function declaration,
186 // just throw away the late-parsed declaration.
187 delete getCurrentClass().LateParsedDeclarations.back();
188 getCurrentClass().LateParsedDeclarations.pop_back();
191 return FnD;
194 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
195 /// specified Declarator is a well formed C++ non-static data member
196 /// declaration. Now lex its initializer and store its tokens for parsing
197 /// after the class is complete.
198 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
199 assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
200 "Current token not a '{' or '='!");
202 LateParsedMemberInitializer *MI =
203 new LateParsedMemberInitializer(this, VarD);
204 getCurrentClass().LateParsedDeclarations.push_back(MI);
205 CachedTokens &Toks = MI->Toks;
207 tok::TokenKind kind = Tok.getKind();
208 if (kind == tok::equal) {
209 Toks.push_back(Tok);
210 ConsumeToken();
213 if (kind == tok::l_brace) {
214 // Begin by storing the '{' token.
215 Toks.push_back(Tok);
216 ConsumeBrace();
218 // Consume everything up to (and including) the matching right brace.
219 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
220 } else {
221 // Consume everything up to (but excluding) the comma or semicolon.
222 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
225 // Store an artificial EOF token to ensure that we don't run off the end of
226 // the initializer when we come to parse it.
227 Token Eof;
228 Eof.startToken();
229 Eof.setKind(tok::eof);
230 Eof.setLocation(Tok.getLocation());
231 Eof.setEofData(VarD);
232 Toks.push_back(Eof);
235 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
236 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
237 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
238 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
239 void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
240 void Parser::LateParsedDeclaration::ParseLexedPragmas() {}
242 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
243 : Self(P), Class(C) {}
245 Parser::LateParsedClass::~LateParsedClass() {
246 Self->DeallocateParsedClasses(Class);
249 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
250 Self->ParseLexedMethodDeclarations(*Class);
253 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
254 Self->ParseLexedMemberInitializers(*Class);
257 void Parser::LateParsedClass::ParseLexedMethodDefs() {
258 Self->ParseLexedMethodDefs(*Class);
261 void Parser::LateParsedClass::ParseLexedAttributes() {
262 Self->ParseLexedAttributes(*Class);
265 void Parser::LateParsedClass::ParseLexedPragmas() {
266 Self->ParseLexedPragmas(*Class);
269 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
270 Self->ParseLexedMethodDeclaration(*this);
273 void Parser::LexedMethod::ParseLexedMethodDefs() {
274 Self->ParseLexedMethodDef(*this);
277 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
278 Self->ParseLexedMemberInitializer(*this);
281 void Parser::LateParsedAttribute::ParseLexedAttributes() {
282 Self->ParseLexedAttribute(*this, true, false);
285 void Parser::LateParsedPragma::ParseLexedPragmas() {
286 Self->ParseLexedPragma(*this);
289 /// Utility to re-enter a possibly-templated scope while parsing its
290 /// late-parsed components.
291 struct Parser::ReenterTemplateScopeRAII {
292 Parser &P;
293 MultiParseScope Scopes;
294 TemplateParameterDepthRAII CurTemplateDepthTracker;
296 ReenterTemplateScopeRAII(Parser &P, Decl *MaybeTemplated, bool Enter = true)
297 : P(P), Scopes(P), CurTemplateDepthTracker(P.TemplateParameterDepth) {
298 if (Enter) {
299 CurTemplateDepthTracker.addDepth(
300 P.ReenterTemplateScopes(Scopes, MaybeTemplated));
305 /// Utility to re-enter a class scope while parsing its late-parsed components.
306 struct Parser::ReenterClassScopeRAII : ReenterTemplateScopeRAII {
307 ParsingClass &Class;
309 ReenterClassScopeRAII(Parser &P, ParsingClass &Class)
310 : ReenterTemplateScopeRAII(P, Class.TagOrTemplate,
311 /*Enter=*/!Class.TopLevelClass),
312 Class(Class) {
313 // If this is the top-level class, we're still within its scope.
314 if (Class.TopLevelClass)
315 return;
317 // Re-enter the class scope itself.
318 Scopes.Enter(Scope::ClassScope|Scope::DeclScope);
319 P.Actions.ActOnStartDelayedMemberDeclarations(P.getCurScope(),
320 Class.TagOrTemplate);
322 ~ReenterClassScopeRAII() {
323 if (Class.TopLevelClass)
324 return;
326 P.Actions.ActOnFinishDelayedMemberDeclarations(P.getCurScope(),
327 Class.TagOrTemplate);
331 /// ParseLexedMethodDeclarations - We finished parsing the member
332 /// specification of a top (non-nested) C++ class. Now go over the
333 /// stack of method declarations with some parts for which parsing was
334 /// delayed (such as default arguments) and parse them.
335 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
336 ReenterClassScopeRAII InClassScope(*this, Class);
338 for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations)
339 LateD->ParseLexedMethodDeclarations();
342 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
343 // If this is a member template, introduce the template parameter scope.
344 ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.Method);
346 // Start the delayed C++ method declaration
347 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
349 // Introduce the parameters into scope and parse their default
350 // arguments.
351 InFunctionTemplateScope.Scopes.Enter(Scope::FunctionPrototypeScope |
352 Scope::FunctionDeclarationScope |
353 Scope::DeclScope);
354 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
355 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
356 // Introduce the parameter into scope.
357 bool HasUnparsed = Param->hasUnparsedDefaultArg();
358 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
359 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
360 if (Toks) {
361 ParenBraceBracketBalancer BalancerRAIIObj(*this);
363 // Mark the end of the default argument so that we know when to stop when
364 // we parse it later on.
365 Token LastDefaultArgToken = Toks->back();
366 Token DefArgEnd;
367 DefArgEnd.startToken();
368 DefArgEnd.setKind(tok::eof);
369 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
370 DefArgEnd.setEofData(Param);
371 Toks->push_back(DefArgEnd);
373 // Parse the default argument from its saved token stream.
374 Toks->push_back(Tok); // So that the current token doesn't get lost
375 PP.EnterTokenStream(*Toks, true, /*IsReinject*/ true);
377 // Consume the previously-pushed token.
378 ConsumeAnyToken();
380 // Consume the '='.
381 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
382 SourceLocation EqualLoc = ConsumeToken();
384 // The argument isn't actually potentially evaluated unless it is
385 // used.
386 EnterExpressionEvaluationContext Eval(
387 Actions,
388 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param);
390 ExprResult DefArgResult;
391 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
392 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
393 DefArgResult = ParseBraceInitializer();
394 } else
395 DefArgResult = ParseAssignmentExpression();
396 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
397 if (DefArgResult.isInvalid()) {
398 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
399 } else {
400 if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
401 // The last two tokens are the terminator and the saved value of
402 // Tok; the last token in the default argument is the one before
403 // those.
404 assert(Toks->size() >= 3 && "expected a token in default arg");
405 Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
406 << SourceRange(Tok.getLocation(),
407 (*Toks)[Toks->size() - 3].getLocation());
409 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
410 DefArgResult.get());
413 // There could be leftover tokens (e.g. because of an error).
414 // Skip through until we reach the 'end of default argument' token.
415 while (Tok.isNot(tok::eof))
416 ConsumeAnyToken();
418 if (Tok.is(tok::eof) && Tok.getEofData() == Param)
419 ConsumeAnyToken();
420 } else if (HasUnparsed) {
421 assert(Param->hasInheritedDefaultArg());
422 const FunctionDecl *Old;
423 if (const auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(LM.Method))
424 Old =
425 cast<FunctionDecl>(FunTmpl->getTemplatedDecl())->getPreviousDecl();
426 else
427 Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
428 if (Old) {
429 ParmVarDecl *OldParam = const_cast<ParmVarDecl*>(Old->getParamDecl(I));
430 assert(!OldParam->hasUnparsedDefaultArg());
431 if (OldParam->hasUninstantiatedDefaultArg())
432 Param->setUninstantiatedDefaultArg(
433 OldParam->getUninstantiatedDefaultArg());
434 else
435 Param->setDefaultArg(OldParam->getInit());
440 // Parse a delayed exception-specification, if there is one.
441 if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
442 ParenBraceBracketBalancer BalancerRAIIObj(*this);
444 // Add the 'stop' token.
445 Token LastExceptionSpecToken = Toks->back();
446 Token ExceptionSpecEnd;
447 ExceptionSpecEnd.startToken();
448 ExceptionSpecEnd.setKind(tok::eof);
449 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
450 ExceptionSpecEnd.setEofData(LM.Method);
451 Toks->push_back(ExceptionSpecEnd);
453 // Parse the default argument from its saved token stream.
454 Toks->push_back(Tok); // So that the current token doesn't get lost
455 PP.EnterTokenStream(*Toks, true, /*IsReinject*/true);
457 // Consume the previously-pushed token.
458 ConsumeAnyToken();
460 // C++11 [expr.prim.general]p3:
461 // If a declaration declares a member function or member function
462 // template of a class X, the expression this is a prvalue of type
463 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
464 // and the end of the function-definition, member-declarator, or
465 // declarator.
466 CXXMethodDecl *Method;
467 if (FunctionTemplateDecl *FunTmpl
468 = dyn_cast<FunctionTemplateDecl>(LM.Method))
469 Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
470 else
471 Method = dyn_cast<CXXMethodDecl>(LM.Method);
473 Sema::CXXThisScopeRAII ThisScope(
474 Actions, Method ? Method->getParent() : nullptr,
475 Method ? Method->getMethodQualifiers() : Qualifiers{},
476 Method && getLangOpts().CPlusPlus11);
478 // Parse the exception-specification.
479 SourceRange SpecificationRange;
480 SmallVector<ParsedType, 4> DynamicExceptions;
481 SmallVector<SourceRange, 4> DynamicExceptionRanges;
482 ExprResult NoexceptExpr;
483 CachedTokens *ExceptionSpecTokens;
485 ExceptionSpecificationType EST
486 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
487 DynamicExceptions,
488 DynamicExceptionRanges, NoexceptExpr,
489 ExceptionSpecTokens);
491 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
492 Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
494 // Attach the exception-specification to the method.
495 Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
496 SpecificationRange,
497 DynamicExceptions,
498 DynamicExceptionRanges,
499 NoexceptExpr.isUsable()?
500 NoexceptExpr.get() : nullptr);
502 // There could be leftover tokens (e.g. because of an error).
503 // Skip through until we reach the original token position.
504 while (Tok.isNot(tok::eof))
505 ConsumeAnyToken();
507 // Clean up the remaining EOF token.
508 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
509 ConsumeAnyToken();
511 delete Toks;
512 LM.ExceptionSpecTokens = nullptr;
515 InFunctionTemplateScope.Scopes.Exit();
517 // Finish the delayed C++ method declaration.
518 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
521 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
522 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
523 /// collected during its parsing and parse them all.
524 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
525 ReenterClassScopeRAII InClassScope(*this, Class);
527 for (LateParsedDeclaration *D : Class.LateParsedDeclarations)
528 D->ParseLexedMethodDefs();
531 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
532 // If this is a member template, introduce the template parameter scope.
533 ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.D);
535 ParenBraceBracketBalancer BalancerRAIIObj(*this);
537 assert(!LM.Toks.empty() && "Empty body!");
538 Token LastBodyToken = LM.Toks.back();
539 Token BodyEnd;
540 BodyEnd.startToken();
541 BodyEnd.setKind(tok::eof);
542 BodyEnd.setLocation(LastBodyToken.getEndLoc());
543 BodyEnd.setEofData(LM.D);
544 LM.Toks.push_back(BodyEnd);
545 // Append the current token at the end of the new token stream so that it
546 // doesn't get lost.
547 LM.Toks.push_back(Tok);
548 PP.EnterTokenStream(LM.Toks, true, /*IsReinject*/true);
550 // Consume the previously pushed token.
551 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
552 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
553 && "Inline method not starting with '{', ':' or 'try'");
555 // Parse the method body. Function body parsing code is similar enough
556 // to be re-used for method bodies as well.
557 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
558 Scope::CompoundStmtScope);
559 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
561 if (Tok.is(tok::kw_try)) {
562 ParseFunctionTryBlock(LM.D, FnScope);
564 while (Tok.isNot(tok::eof))
565 ConsumeAnyToken();
567 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
568 ConsumeAnyToken();
569 return;
571 if (Tok.is(tok::colon)) {
572 ParseConstructorInitializer(LM.D);
574 // Error recovery.
575 if (!Tok.is(tok::l_brace)) {
576 FnScope.Exit();
577 Actions.ActOnFinishFunctionBody(LM.D, nullptr);
579 while (Tok.isNot(tok::eof))
580 ConsumeAnyToken();
582 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
583 ConsumeAnyToken();
584 return;
586 } else
587 Actions.ActOnDefaultCtorInitializers(LM.D);
589 assert((Actions.getDiagnostics().hasErrorOccurred() ||
590 !isa<FunctionTemplateDecl>(LM.D) ||
591 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
592 < TemplateParameterDepth) &&
593 "TemplateParameterDepth should be greater than the depth of "
594 "current template being instantiated!");
596 ParseFunctionStatementBody(LM.D, FnScope);
598 while (Tok.isNot(tok::eof))
599 ConsumeAnyToken();
601 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
602 ConsumeAnyToken();
604 if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
605 if (isa<CXXMethodDecl>(FD) ||
606 FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend))
607 Actions.ActOnFinishInlineFunctionDef(FD);
610 /// ParseLexedMemberInitializers - We finished parsing the member specification
611 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
612 /// initializers that were collected during its parsing and parse them all.
613 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
614 ReenterClassScopeRAII InClassScope(*this, Class);
616 if (!Class.LateParsedDeclarations.empty()) {
617 // C++11 [expr.prim.general]p4:
618 // Otherwise, if a member-declarator declares a non-static data member
619 // (9.2) of a class X, the expression this is a prvalue of type "pointer
620 // to X" within the optional brace-or-equal-initializer. It shall not
621 // appear elsewhere in the member-declarator.
622 // FIXME: This should be done in ParseLexedMemberInitializer, not here.
623 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
624 Qualifiers());
626 for (LateParsedDeclaration *D : Class.LateParsedDeclarations)
627 D->ParseLexedMemberInitializers();
630 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
633 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
634 if (!MI.Field || MI.Field->isInvalidDecl())
635 return;
637 ParenBraceBracketBalancer BalancerRAIIObj(*this);
639 // Append the current token at the end of the new token stream so that it
640 // doesn't get lost.
641 MI.Toks.push_back(Tok);
642 PP.EnterTokenStream(MI.Toks, true, /*IsReinject*/true);
644 // Consume the previously pushed token.
645 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
647 SourceLocation EqualLoc;
649 Actions.ActOnStartCXXInClassMemberInitializer();
651 // The initializer isn't actually potentially evaluated unless it is
652 // used.
653 EnterExpressionEvaluationContext Eval(
654 Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed);
656 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
657 EqualLoc);
659 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
660 Init.get());
662 // The next token should be our artificial terminating EOF token.
663 if (Tok.isNot(tok::eof)) {
664 if (!Init.isInvalid()) {
665 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
666 if (!EndLoc.isValid())
667 EndLoc = Tok.getLocation();
668 // No fixit; we can't recover as if there were a semicolon here.
669 Diag(EndLoc, diag::err_expected_semi_decl_list);
672 // Consume tokens until we hit the artificial EOF.
673 while (Tok.isNot(tok::eof))
674 ConsumeAnyToken();
676 // Make sure this is *our* artificial EOF token.
677 if (Tok.getEofData() == MI.Field)
678 ConsumeAnyToken();
681 /// Wrapper class which calls ParseLexedAttribute, after setting up the
682 /// scope appropriately.
683 void Parser::ParseLexedAttributes(ParsingClass &Class) {
684 ReenterClassScopeRAII InClassScope(*this, Class);
686 for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations)
687 LateD->ParseLexedAttributes();
690 /// Parse all attributes in LAs, and attach them to Decl D.
691 void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
692 bool EnterScope, bool OnDefinition) {
693 assert(LAs.parseSoon() &&
694 "Attribute list should be marked for immediate parsing.");
695 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
696 if (D)
697 LAs[i]->addDecl(D);
698 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
699 delete LAs[i];
701 LAs.clear();
704 /// Finish parsing an attribute for which parsing was delayed.
705 /// This will be called at the end of parsing a class declaration
706 /// for each LateParsedAttribute. We consume the saved tokens and
707 /// create an attribute with the arguments filled in. We add this
708 /// to the Attribute list for the decl.
709 void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
710 bool EnterScope, bool OnDefinition) {
711 // Create a fake EOF so that attribute parsing won't go off the end of the
712 // attribute.
713 Token AttrEnd;
714 AttrEnd.startToken();
715 AttrEnd.setKind(tok::eof);
716 AttrEnd.setLocation(Tok.getLocation());
717 AttrEnd.setEofData(LA.Toks.data());
718 LA.Toks.push_back(AttrEnd);
720 // Append the current token at the end of the new token stream so that it
721 // doesn't get lost.
722 LA.Toks.push_back(Tok);
723 PP.EnterTokenStream(LA.Toks, true, /*IsReinject=*/true);
724 // Consume the previously pushed token.
725 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
727 ParsedAttributes Attrs(AttrFactory);
729 if (LA.Decls.size() > 0) {
730 Decl *D = LA.Decls[0];
731 NamedDecl *ND = dyn_cast<NamedDecl>(D);
732 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
734 // Allow 'this' within late-parsed attributes.
735 Sema::CXXThisScopeRAII ThisScope(Actions, RD, Qualifiers(),
736 ND && ND->isCXXInstanceMember());
738 if (LA.Decls.size() == 1) {
739 // If the Decl is templatized, add template parameters to scope.
740 ReenterTemplateScopeRAII InDeclScope(*this, D, EnterScope);
742 // If the Decl is on a function, add function parameters to the scope.
743 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
744 if (HasFunScope) {
745 InDeclScope.Scopes.Enter(Scope::FnScope | Scope::DeclScope |
746 Scope::CompoundStmtScope);
747 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
750 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr,
751 nullptr, SourceLocation(), ParsedAttr::AS_GNU,
752 nullptr);
754 if (HasFunScope)
755 Actions.ActOnExitFunctionContext();
756 } else {
757 // If there are multiple decls, then the decl cannot be within the
758 // function scope.
759 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, nullptr,
760 nullptr, SourceLocation(), ParsedAttr::AS_GNU,
761 nullptr);
763 } else {
764 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
767 if (OnDefinition && !Attrs.empty() && !Attrs.begin()->isCXX11Attribute() &&
768 Attrs.begin()->isKnownToGCC())
769 Diag(Tok, diag::warn_attribute_on_function_definition)
770 << &LA.AttrName;
772 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i)
773 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
775 // Due to a parsing error, we either went over the cached tokens or
776 // there are still cached tokens left, so we skip the leftover tokens.
777 while (Tok.isNot(tok::eof))
778 ConsumeAnyToken();
780 if (Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData())
781 ConsumeAnyToken();
784 void Parser::ParseLexedPragmas(ParsingClass &Class) {
785 ReenterClassScopeRAII InClassScope(*this, Class);
787 for (LateParsedDeclaration *D : Class.LateParsedDeclarations)
788 D->ParseLexedPragmas();
791 void Parser::ParseLexedPragma(LateParsedPragma &LP) {
792 PP.EnterToken(Tok, /*IsReinject=*/true);
793 PP.EnterTokenStream(LP.toks(), /*DisableMacroExpansion=*/true,
794 /*IsReinject=*/true);
796 // Consume the previously pushed token.
797 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
798 assert(Tok.isAnnotation() && "Expected annotation token.");
799 switch (Tok.getKind()) {
800 case tok::annot_attr_openmp:
801 case tok::annot_pragma_openmp: {
802 AccessSpecifier AS = LP.getAccessSpecifier();
803 ParsedAttributes Attrs(AttrFactory);
804 (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
805 break;
807 default:
808 llvm_unreachable("Unexpected token.");
812 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
813 /// container until the token 'T' is reached (which gets
814 /// consumed/stored too, if ConsumeFinalToken).
815 /// If StopAtSemi is true, then we will stop early at a ';' character.
816 /// Returns true if token 'T1' or 'T2' was found.
817 /// NOTE: This is a specialized version of Parser::SkipUntil.
818 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
819 CachedTokens &Toks,
820 bool StopAtSemi, bool ConsumeFinalToken) {
821 // We always want this function to consume at least one token if the first
822 // token isn't T and if not at EOF.
823 bool isFirstTokenConsumed = true;
824 while (true) {
825 // If we found one of the tokens, stop and return true.
826 if (Tok.is(T1) || Tok.is(T2)) {
827 if (ConsumeFinalToken) {
828 Toks.push_back(Tok);
829 ConsumeAnyToken();
831 return true;
834 switch (Tok.getKind()) {
835 case tok::eof:
836 case tok::annot_module_begin:
837 case tok::annot_module_end:
838 case tok::annot_module_include:
839 // Ran out of tokens.
840 return false;
842 case tok::l_paren:
843 // Recursively consume properly-nested parens.
844 Toks.push_back(Tok);
845 ConsumeParen();
846 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
847 break;
848 case tok::l_square:
849 // Recursively consume properly-nested square brackets.
850 Toks.push_back(Tok);
851 ConsumeBracket();
852 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
853 break;
854 case tok::l_brace:
855 // Recursively consume properly-nested braces.
856 Toks.push_back(Tok);
857 ConsumeBrace();
858 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
859 break;
861 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
862 // Since the user wasn't looking for this token (if they were, it would
863 // already be handled), this isn't balanced. If there is a LHS token at a
864 // higher level, we will assume that this matches the unbalanced token
865 // and return it. Otherwise, this is a spurious RHS token, which we skip.
866 case tok::r_paren:
867 if (ParenCount && !isFirstTokenConsumed)
868 return false; // Matches something.
869 Toks.push_back(Tok);
870 ConsumeParen();
871 break;
872 case tok::r_square:
873 if (BracketCount && !isFirstTokenConsumed)
874 return false; // Matches something.
875 Toks.push_back(Tok);
876 ConsumeBracket();
877 break;
878 case tok::r_brace:
879 if (BraceCount && !isFirstTokenConsumed)
880 return false; // Matches something.
881 Toks.push_back(Tok);
882 ConsumeBrace();
883 break;
885 case tok::semi:
886 if (StopAtSemi)
887 return false;
888 [[fallthrough]];
889 default:
890 // consume this token.
891 Toks.push_back(Tok);
892 ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true);
893 break;
895 isFirstTokenConsumed = false;
899 /// Consume tokens and store them in the passed token container until
900 /// we've passed the try keyword and constructor initializers and have consumed
901 /// the opening brace of the function body. The opening brace will be consumed
902 /// if and only if there was no error.
904 /// \return True on error.
905 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
906 if (Tok.is(tok::kw_try)) {
907 Toks.push_back(Tok);
908 ConsumeToken();
911 if (Tok.isNot(tok::colon)) {
912 // Easy case, just a function body.
914 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
915 // brace: an opening one is the function body, while a closing one probably
916 // means we've reached the end of the class.
917 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
918 /*StopAtSemi=*/true,
919 /*ConsumeFinalToken=*/false);
920 if (Tok.isNot(tok::l_brace))
921 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
923 Toks.push_back(Tok);
924 ConsumeBrace();
925 return false;
928 Toks.push_back(Tok);
929 ConsumeToken();
931 // We can't reliably skip over a mem-initializer-id, because it could be
932 // a template-id involving not-yet-declared names. Given:
934 // S ( ) : a < b < c > ( e )
936 // 'e' might be an initializer or part of a template argument, depending
937 // on whether 'b' is a template.
939 // Track whether we might be inside a template argument. We can give
940 // significantly better diagnostics if we know that we're not.
941 bool MightBeTemplateArgument = false;
943 while (true) {
944 // Skip over the mem-initializer-id, if possible.
945 if (Tok.is(tok::kw_decltype)) {
946 Toks.push_back(Tok);
947 SourceLocation OpenLoc = ConsumeToken();
948 if (Tok.isNot(tok::l_paren))
949 return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
950 << "decltype";
951 Toks.push_back(Tok);
952 ConsumeParen();
953 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
954 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
955 Diag(OpenLoc, diag::note_matching) << tok::l_paren;
956 return true;
959 do {
960 // Walk over a component of a nested-name-specifier.
961 if (Tok.is(tok::coloncolon)) {
962 Toks.push_back(Tok);
963 ConsumeToken();
965 if (Tok.is(tok::kw_template)) {
966 Toks.push_back(Tok);
967 ConsumeToken();
971 if (Tok.is(tok::identifier)) {
972 Toks.push_back(Tok);
973 ConsumeToken();
974 } else {
975 break;
977 } while (Tok.is(tok::coloncolon));
979 if (Tok.is(tok::code_completion)) {
980 Toks.push_back(Tok);
981 ConsumeCodeCompletionToken();
982 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) {
983 // Could be the start of another member initializer (the ',' has not
984 // been written yet)
985 continue;
989 if (Tok.is(tok::comma)) {
990 // The initialization is missing, we'll diagnose it later.
991 Toks.push_back(Tok);
992 ConsumeToken();
993 continue;
995 if (Tok.is(tok::less))
996 MightBeTemplateArgument = true;
998 if (MightBeTemplateArgument) {
999 // We may be inside a template argument list. Grab up to the start of the
1000 // next parenthesized initializer or braced-init-list. This *might* be the
1001 // initializer, or it might be a subexpression in the template argument
1002 // list.
1003 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
1004 // if all angles are closed.
1005 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
1006 /*StopAtSemi=*/true,
1007 /*ConsumeFinalToken=*/false)) {
1008 // We're not just missing the initializer, we're also missing the
1009 // function body!
1010 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
1012 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
1013 // We found something weird in a mem-initializer-id.
1014 if (getLangOpts().CPlusPlus11)
1015 return Diag(Tok.getLocation(), diag::err_expected_either)
1016 << tok::l_paren << tok::l_brace;
1017 else
1018 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
1021 tok::TokenKind kind = Tok.getKind();
1022 Toks.push_back(Tok);
1023 bool IsLParen = (kind == tok::l_paren);
1024 SourceLocation OpenLoc = Tok.getLocation();
1026 if (IsLParen) {
1027 ConsumeParen();
1028 } else {
1029 assert(kind == tok::l_brace && "Must be left paren or brace here.");
1030 ConsumeBrace();
1031 // In C++03, this has to be the start of the function body, which
1032 // means the initializer is malformed; we'll diagnose it later.
1033 if (!getLangOpts().CPlusPlus11)
1034 return false;
1036 const Token &PreviousToken = Toks[Toks.size() - 2];
1037 if (!MightBeTemplateArgument &&
1038 !PreviousToken.isOneOf(tok::identifier, tok::greater,
1039 tok::greatergreater)) {
1040 // If the opening brace is not preceded by one of these tokens, we are
1041 // missing the mem-initializer-id. In order to recover better, we need
1042 // to use heuristics to determine if this '{' is most likely the
1043 // beginning of a brace-init-list or the function body.
1044 // Check the token after the corresponding '}'.
1045 TentativeParsingAction PA(*this);
1046 if (SkipUntil(tok::r_brace) &&
1047 !Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) {
1048 // Consider there was a malformed initializer and this is the start
1049 // of the function body. We'll diagnose it later.
1050 PA.Revert();
1051 return false;
1053 PA.Revert();
1057 // Grab the initializer (or the subexpression of the template argument).
1058 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
1059 // if we might be inside the braces of a lambda-expression.
1060 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
1061 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
1062 Diag(Tok, diag::err_expected) << CloseKind;
1063 Diag(OpenLoc, diag::note_matching) << kind;
1064 return true;
1067 // Grab pack ellipsis, if present.
1068 if (Tok.is(tok::ellipsis)) {
1069 Toks.push_back(Tok);
1070 ConsumeToken();
1073 // If we know we just consumed a mem-initializer, we must have ',' or '{'
1074 // next.
1075 if (Tok.is(tok::comma)) {
1076 Toks.push_back(Tok);
1077 ConsumeToken();
1078 } else if (Tok.is(tok::l_brace)) {
1079 // This is the function body if the ')' or '}' is immediately followed by
1080 // a '{'. That cannot happen within a template argument, apart from the
1081 // case where a template argument contains a compound literal:
1083 // S ( ) : a < b < c > ( d ) { }
1084 // // End of declaration, or still inside the template argument?
1086 // ... and the case where the template argument contains a lambda:
1088 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
1089 // ( ) > ( ) { }
1091 // FIXME: Disambiguate these cases. Note that the latter case is probably
1092 // going to be made ill-formed by core issue 1607.
1093 Toks.push_back(Tok);
1094 ConsumeBrace();
1095 return false;
1096 } else if (!MightBeTemplateArgument) {
1097 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
1098 << tok::comma;
1103 /// Consume and store tokens from the '?' to the ':' in a conditional
1104 /// expression.
1105 bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
1106 // Consume '?'.
1107 assert(Tok.is(tok::question));
1108 Toks.push_back(Tok);
1109 ConsumeToken();
1111 while (Tok.isNot(tok::colon)) {
1112 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
1113 /*StopAtSemi=*/true,
1114 /*ConsumeFinalToken=*/false))
1115 return false;
1117 // If we found a nested conditional, consume it.
1118 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
1119 return false;
1122 // Consume ':'.
1123 Toks.push_back(Tok);
1124 ConsumeToken();
1125 return true;
1128 /// A tentative parsing action that can also revert token annotations.
1129 class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
1130 public:
1131 explicit UnannotatedTentativeParsingAction(Parser &Self,
1132 tok::TokenKind EndKind)
1133 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
1134 // Stash away the old token stream, so we can restore it once the
1135 // tentative parse is complete.
1136 TentativeParsingAction Inner(Self);
1137 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
1138 Inner.Revert();
1141 void RevertAnnotations() {
1142 Revert();
1144 // Put back the original tokens.
1145 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
1146 if (Toks.size()) {
1147 auto Buffer = std::make_unique<Token[]>(Toks.size());
1148 std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
1149 Buffer[Toks.size() - 1] = Self.Tok;
1150 Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true,
1151 /*IsReinject*/ true);
1153 Self.Tok = Toks.front();
1157 private:
1158 Parser &Self;
1159 CachedTokens Toks;
1160 tok::TokenKind EndKind;
1163 /// ConsumeAndStoreInitializer - Consume and store the token at the passed token
1164 /// container until the end of the current initializer expression (either a
1165 /// default argument or an in-class initializer for a non-static data member).
1167 /// Returns \c true if we reached the end of something initializer-shaped,
1168 /// \c false if we bailed out.
1169 bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
1170 CachedInitKind CIK) {
1171 // We always want this function to consume at least one token if not at EOF.
1172 bool IsFirstToken = true;
1174 // Number of possible unclosed <s we've seen so far. These might be templates,
1175 // and might not, but if there were none of them (or we know for sure that
1176 // we're within a template), we can avoid a tentative parse.
1177 unsigned AngleCount = 0;
1178 unsigned KnownTemplateCount = 0;
1180 while (true) {
1181 switch (Tok.getKind()) {
1182 case tok::comma:
1183 // If we might be in a template, perform a tentative parse to check.
1184 if (!AngleCount)
1185 // Not a template argument: this is the end of the initializer.
1186 return true;
1187 if (KnownTemplateCount)
1188 goto consume_token;
1190 // We hit a comma inside angle brackets. This is the hard case. The
1191 // rule we follow is:
1192 // * For a default argument, if the tokens after the comma form a
1193 // syntactically-valid parameter-declaration-clause, in which each
1194 // parameter has an initializer, then this comma ends the default
1195 // argument.
1196 // * For a default initializer, if the tokens after the comma form a
1197 // syntactically-valid init-declarator-list, then this comma ends
1198 // the default initializer.
1200 UnannotatedTentativeParsingAction PA(*this,
1201 CIK == CIK_DefaultInitializer
1202 ? tok::semi : tok::r_paren);
1203 Sema::TentativeAnalysisScope Scope(Actions);
1205 TPResult Result = TPResult::Error;
1206 ConsumeToken();
1207 switch (CIK) {
1208 case CIK_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(tok::semi))
1213 Result = TPResult::False;
1214 break;
1216 case CIK_DefaultArgument:
1217 bool InvalidAsDeclaration = false;
1218 Result = TryParseParameterDeclarationClause(
1219 &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;
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 PA.RevertAnnotations();
1232 // If what follows could be a declaration, it is a declaration.
1233 if (Result != TPResult::False && Result != TPResult::Error)
1234 return true;
1237 // Keep going. We know we're inside a template argument list now.
1238 ++KnownTemplateCount;
1239 goto consume_token;
1241 case tok::eof:
1242 case tok::annot_module_begin:
1243 case tok::annot_module_end:
1244 case tok::annot_module_include:
1245 // Ran out of tokens.
1246 return false;
1248 case tok::less:
1249 // FIXME: A '<' can only start a template-id if it's preceded by an
1250 // identifier, an operator-function-id, or a literal-operator-id.
1251 ++AngleCount;
1252 goto consume_token;
1254 case tok::question:
1255 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1256 // that is *never* the end of the initializer. Skip to the ':'.
1257 if (!ConsumeAndStoreConditional(Toks))
1258 return false;
1259 break;
1261 case tok::greatergreatergreater:
1262 if (!getLangOpts().CPlusPlus11)
1263 goto consume_token;
1264 if (AngleCount) --AngleCount;
1265 if (KnownTemplateCount) --KnownTemplateCount;
1266 [[fallthrough]];
1267 case tok::greatergreater:
1268 if (!getLangOpts().CPlusPlus11)
1269 goto consume_token;
1270 if (AngleCount) --AngleCount;
1271 if (KnownTemplateCount) --KnownTemplateCount;
1272 [[fallthrough]];
1273 case tok::greater:
1274 if (AngleCount) --AngleCount;
1275 if (KnownTemplateCount) --KnownTemplateCount;
1276 goto consume_token;
1278 case tok::kw_template:
1279 // 'template' identifier '<' is known to start a template argument list,
1280 // and can be used to disambiguate the parse.
1281 // FIXME: Support all forms of 'template' unqualified-id '<'.
1282 Toks.push_back(Tok);
1283 ConsumeToken();
1284 if (Tok.is(tok::identifier)) {
1285 Toks.push_back(Tok);
1286 ConsumeToken();
1287 if (Tok.is(tok::less)) {
1288 ++AngleCount;
1289 ++KnownTemplateCount;
1290 Toks.push_back(Tok);
1291 ConsumeToken();
1294 break;
1296 case tok::kw_operator:
1297 // If 'operator' precedes other punctuation, that punctuation loses
1298 // its special behavior.
1299 Toks.push_back(Tok);
1300 ConsumeToken();
1301 switch (Tok.getKind()) {
1302 case tok::comma:
1303 case tok::greatergreatergreater:
1304 case tok::greatergreater:
1305 case tok::greater:
1306 case tok::less:
1307 Toks.push_back(Tok);
1308 ConsumeToken();
1309 break;
1310 default:
1311 break;
1313 break;
1315 case tok::l_paren:
1316 // Recursively consume properly-nested parens.
1317 Toks.push_back(Tok);
1318 ConsumeParen();
1319 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1320 break;
1321 case tok::l_square:
1322 // Recursively consume properly-nested square brackets.
1323 Toks.push_back(Tok);
1324 ConsumeBracket();
1325 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
1326 break;
1327 case tok::l_brace:
1328 // Recursively consume properly-nested braces.
1329 Toks.push_back(Tok);
1330 ConsumeBrace();
1331 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1332 break;
1334 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1335 // Since the user wasn't looking for this token (if they were, it would
1336 // already be handled), this isn't balanced. If there is a LHS token at a
1337 // higher level, we will assume that this matches the unbalanced token
1338 // and return it. Otherwise, this is a spurious RHS token, which we
1339 // consume and pass on to downstream code to diagnose.
1340 case tok::r_paren:
1341 if (CIK == CIK_DefaultArgument)
1342 return true; // End of the default argument.
1343 if (ParenCount && !IsFirstToken)
1344 return false;
1345 Toks.push_back(Tok);
1346 ConsumeParen();
1347 continue;
1348 case tok::r_square:
1349 if (BracketCount && !IsFirstToken)
1350 return false;
1351 Toks.push_back(Tok);
1352 ConsumeBracket();
1353 continue;
1354 case tok::r_brace:
1355 if (BraceCount && !IsFirstToken)
1356 return false;
1357 Toks.push_back(Tok);
1358 ConsumeBrace();
1359 continue;
1361 case tok::code_completion:
1362 Toks.push_back(Tok);
1363 ConsumeCodeCompletionToken();
1364 break;
1366 case tok::string_literal:
1367 case tok::wide_string_literal:
1368 case tok::utf8_string_literal:
1369 case tok::utf16_string_literal:
1370 case tok::utf32_string_literal:
1371 Toks.push_back(Tok);
1372 ConsumeStringToken();
1373 break;
1374 case tok::semi:
1375 if (CIK == CIK_DefaultInitializer)
1376 return true; // End of the default initializer.
1377 [[fallthrough]];
1378 default:
1379 consume_token:
1380 Toks.push_back(Tok);
1381 ConsumeToken();
1382 break;
1384 IsFirstToken = false;