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