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