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