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