1 //===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements parsing for C++ class inline methods.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Parse/Parser.h"
14 #include "clang/AST/DeclTemplate.h"
15 #include "clang/Parse/ParseDiagnostic.h"
16 #include "clang/Parse/RAIIObjectsForParser.h"
17 #include "clang/Sema/DeclSpec.h"
18 #include "clang/Sema/Scope.h"
19 using namespace clang;
21 /// ParseCXXInlineMethodDef - We parsed and verified that the specified
22 /// Declarator is a well formed C++ inline method definition. Now lex its body
23 /// and store its tokens for parsing after the C++ class is complete.
24 NamedDecl *Parser::ParseCXXInlineMethodDef(
25 AccessSpecifier AS, ParsedAttributes &AccessAttrs, ParsingDeclarator &D,
26 const ParsedTemplateInfo &TemplateInfo, const VirtSpecifiers &VS,
27 SourceLocation PureSpecLoc) {
28 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
29 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
30 "Current token not a '{', ':', '=', or 'try'!");
32 MultiTemplateParamsArg TemplateParams(
33 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
35 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
38 if (D.getDeclSpec().isFriendSpecified())
39 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
42 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
43 TemplateParams, nullptr,
46 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
47 if (PureSpecLoc.isValid())
48 Actions.ActOnPureSpecifier(FnD, PureSpecLoc);
53 HandleMemberFunctionDeclDelays(D, FnD);
57 if (TryConsumeToken(tok::equal)) {
65 SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
66 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
67 Diag(KWLoc, getLangOpts().CPlusPlus11
68 ? diag::warn_cxx98_compat_defaulted_deleted_function
69 : diag::ext_defaulted_deleted_function)
71 Actions.SetDeclDeleted(FnD, KWLoc);
73 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
74 DeclAsFunction->setRangeEnd(KWEndLoc);
76 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
77 Diag(KWLoc, getLangOpts().CPlusPlus11
78 ? diag::warn_cxx98_compat_defaulted_deleted_function
79 : diag::ext_defaulted_deleted_function)
81 Actions.SetDeclDefaulted(FnD, KWLoc);
82 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
83 DeclAsFunction->setRangeEnd(KWEndLoc);
86 llvm_unreachable("function definition after = not 'delete' or 'default'");
89 if (Tok.is(tok::comma)) {
90 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
93 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
94 Delete ? "delete" : "default")) {
101 if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) &&
102 trySkippingFunctionBody()) {
103 Actions.ActOnSkippedFunctionBody(FnD);
107 // In delayed template parsing mode, if we are within a class template
108 // or if we are about to parse function member template then consume
109 // the tokens and store them for parsing at the end of the translation unit.
110 if (getLangOpts().DelayedTemplateParsing &&
111 D.getFunctionDefinitionKind() == FDK_Definition &&
112 !D.getDeclSpec().hasConstexprSpecifier() &&
113 !(FnD && FnD->getAsFunction() &&
114 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
115 ((Actions.CurContext->isDependentContext() ||
116 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
117 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
118 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
121 LexTemplateFunctionForLateParsing(Toks);
124 FunctionDecl *FD = FnD->getAsFunction();
125 Actions.CheckForFunctionRedefinition(FD);
126 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
132 // Consume the tokens and store them for later parsing.
134 LexedMethod* LM = new LexedMethod(this, FnD);
135 getCurrentClass().LateParsedDeclarations.push_back(LM);
136 LM->TemplateScope = getCurScope()->isTemplateParamScope();
137 CachedTokens &Toks = LM->Toks;
139 tok::TokenKind kind = Tok.getKind();
140 // Consume everything up to (and including) the left brace of the
142 if (ConsumeAndStoreFunctionPrologue(Toks)) {
143 // We didn't find the left-brace we expected after the
144 // constructor initializer; we already printed an error, and it's likely
145 // impossible to recover, so don't try to parse this method later.
146 // Skip over the rest of the decl and back to somewhere that looks
149 delete getCurrentClass().LateParsedDeclarations.back();
150 getCurrentClass().LateParsedDeclarations.pop_back();
153 // Consume everything up to (and including) the matching right brace.
154 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
157 // If we're in a function-try-block, we need to store all the catch blocks.
158 if (kind == tok::kw_try) {
159 while (Tok.is(tok::kw_catch)) {
160 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
161 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
166 FunctionDecl *FD = FnD->getAsFunction();
167 // Track that this function will eventually have a body; Sema needs
169 Actions.CheckForFunctionRedefinition(FD);
170 FD->setWillHaveBody(true);
172 // If semantic analysis could not build a function declaration,
173 // just throw away the late-parsed declaration.
174 delete getCurrentClass().LateParsedDeclarations.back();
175 getCurrentClass().LateParsedDeclarations.pop_back();
181 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
182 /// specified Declarator is a well formed C++ non-static data member
183 /// declaration. Now lex its initializer and store its tokens for parsing
184 /// after the class is complete.
185 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
186 assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
187 "Current token not a '{' or '='!");
189 LateParsedMemberInitializer *MI =
190 new LateParsedMemberInitializer(this, VarD);
191 getCurrentClass().LateParsedDeclarations.push_back(MI);
192 CachedTokens &Toks = MI->Toks;
194 tok::TokenKind kind = Tok.getKind();
195 if (kind == tok::equal) {
200 if (kind == tok::l_brace) {
201 // Begin by storing the '{' token.
205 // Consume everything up to (and including) the matching right brace.
206 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
208 // Consume everything up to (but excluding) the comma or semicolon.
209 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
212 // Store an artificial EOF token to ensure that we don't run off the end of
213 // the initializer when we come to parse it.
216 Eof.setKind(tok::eof);
217 Eof.setLocation(Tok.getLocation());
218 Eof.setEofData(VarD);
222 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
223 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
224 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
225 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
227 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
228 : Self(P), Class(C) {}
230 Parser::LateParsedClass::~LateParsedClass() {
231 Self->DeallocateParsedClasses(Class);
234 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
235 Self->ParseLexedMethodDeclarations(*Class);
238 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
239 Self->ParseLexedMemberInitializers(*Class);
242 void Parser::LateParsedClass::ParseLexedMethodDefs() {
243 Self->ParseLexedMethodDefs(*Class);
246 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
247 Self->ParseLexedMethodDeclaration(*this);
250 void Parser::LexedMethod::ParseLexedMethodDefs() {
251 Self->ParseLexedMethodDef(*this);
254 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
255 Self->ParseLexedMemberInitializer(*this);
258 /// ParseLexedMethodDeclarations - We finished parsing the member
259 /// specification of a top (non-nested) C++ class. Now go over the
260 /// stack of method declarations with some parts for which parsing was
261 /// delayed (such as default arguments) and parse them.
262 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
263 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
264 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
266 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
267 if (HasTemplateScope) {
268 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
269 ++CurTemplateDepthTracker;
272 // The current scope is still active if we're the top-level class.
273 // Otherwise we'll need to push and enter a new scope.
274 bool HasClassScope = !Class.TopLevelClass;
275 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
278 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
279 Class.TagOrTemplate);
281 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
282 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
286 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
287 Class.TagOrTemplate);
290 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
291 // If this is a member template, introduce the template parameter scope.
292 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
293 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
294 if (LM.TemplateScope) {
295 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
296 ++CurTemplateDepthTracker;
298 // Start the delayed C++ method declaration
299 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
301 // Introduce the parameters into scope and parse their default
303 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
304 Scope::FunctionDeclarationScope | Scope::DeclScope);
305 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
306 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
307 // Introduce the parameter into scope.
308 bool HasUnparsed = Param->hasUnparsedDefaultArg();
309 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
310 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
312 ParenBraceBracketBalancer BalancerRAIIObj(*this);
314 // Mark the end of the default argument so that we know when to stop when
315 // we parse it later on.
316 Token LastDefaultArgToken = Toks->back();
318 DefArgEnd.startToken();
319 DefArgEnd.setKind(tok::eof);
320 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
321 DefArgEnd.setEofData(Param);
322 Toks->push_back(DefArgEnd);
324 // Parse the default argument from its saved token stream.
325 Toks->push_back(Tok); // So that the current token doesn't get lost
326 PP.EnterTokenStream(*Toks, true, /*IsReinject*/ true);
328 // Consume the previously-pushed token.
332 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
333 SourceLocation EqualLoc = ConsumeToken();
335 // The argument isn't actually potentially evaluated unless it is
337 EnterExpressionEvaluationContext Eval(
339 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param);
341 ExprResult DefArgResult;
342 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
343 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
344 DefArgResult = ParseBraceInitializer();
346 DefArgResult = ParseAssignmentExpression();
347 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
348 if (DefArgResult.isInvalid()) {
349 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
351 if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
352 // The last two tokens are the terminator and the saved value of
353 // Tok; the last token in the default argument is the one before
355 assert(Toks->size() >= 3 && "expected a token in default arg");
356 Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
357 << SourceRange(Tok.getLocation(),
358 (*Toks)[Toks->size() - 3].getLocation());
360 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
364 // There could be leftover tokens (e.g. because of an error).
365 // Skip through until we reach the 'end of default argument' token.
366 while (Tok.isNot(tok::eof))
369 if (Tok.is(tok::eof) && Tok.getEofData() == Param)
371 } else if (HasUnparsed) {
372 assert(Param->hasInheritedDefaultArg());
373 FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
374 ParmVarDecl *OldParam = Old->getParamDecl(I);
375 assert (!OldParam->hasUnparsedDefaultArg());
376 if (OldParam->hasUninstantiatedDefaultArg())
377 Param->setUninstantiatedDefaultArg(
378 OldParam->getUninstantiatedDefaultArg());
380 Param->setDefaultArg(OldParam->getInit());
384 // Parse a delayed exception-specification, if there is one.
385 if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
386 ParenBraceBracketBalancer BalancerRAIIObj(*this);
388 // Add the 'stop' token.
389 Token LastExceptionSpecToken = Toks->back();
390 Token ExceptionSpecEnd;
391 ExceptionSpecEnd.startToken();
392 ExceptionSpecEnd.setKind(tok::eof);
393 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
394 ExceptionSpecEnd.setEofData(LM.Method);
395 Toks->push_back(ExceptionSpecEnd);
397 // Parse the default argument from its saved token stream.
398 Toks->push_back(Tok); // So that the current token doesn't get lost
399 PP.EnterTokenStream(*Toks, true, /*IsReinject*/true);
401 // Consume the previously-pushed token.
404 // C++11 [expr.prim.general]p3:
405 // If a declaration declares a member function or member function
406 // template of a class X, the expression this is a prvalue of type
407 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
408 // and the end of the function-definition, member-declarator, or
410 CXXMethodDecl *Method;
411 if (FunctionTemplateDecl *FunTmpl
412 = dyn_cast<FunctionTemplateDecl>(LM.Method))
413 Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
415 Method = cast<CXXMethodDecl>(LM.Method);
417 Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
418 Method->getMethodQualifiers(),
419 getLangOpts().CPlusPlus11);
421 // Parse the exception-specification.
422 SourceRange SpecificationRange;
423 SmallVector<ParsedType, 4> DynamicExceptions;
424 SmallVector<SourceRange, 4> DynamicExceptionRanges;
425 ExprResult NoexceptExpr;
426 CachedTokens *ExceptionSpecTokens;
428 ExceptionSpecificationType EST
429 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
431 DynamicExceptionRanges, NoexceptExpr,
432 ExceptionSpecTokens);
434 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
435 Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
437 // Attach the exception-specification to the method.
438 Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
441 DynamicExceptionRanges,
442 NoexceptExpr.isUsable()?
443 NoexceptExpr.get() : nullptr);
445 // There could be leftover tokens (e.g. because of an error).
446 // Skip through until we reach the original token position.
447 while (Tok.isNot(tok::eof))
450 // Clean up the remaining EOF token.
451 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
455 LM.ExceptionSpecTokens = nullptr;
458 PrototypeScope.Exit();
460 // Finish the delayed C++ method declaration.
461 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
464 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
465 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
466 /// collected during its parsing and parse them all.
467 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
468 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
469 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
470 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
471 if (HasTemplateScope) {
472 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
473 ++CurTemplateDepthTracker;
475 bool HasClassScope = !Class.TopLevelClass;
476 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
479 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
480 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
484 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
485 // If this is a member template, introduce the template parameter scope.
486 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
487 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
488 if (LM.TemplateScope) {
489 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
490 ++CurTemplateDepthTracker;
493 ParenBraceBracketBalancer BalancerRAIIObj(*this);
495 assert(!LM.Toks.empty() && "Empty body!");
496 Token LastBodyToken = LM.Toks.back();
498 BodyEnd.startToken();
499 BodyEnd.setKind(tok::eof);
500 BodyEnd.setLocation(LastBodyToken.getEndLoc());
501 BodyEnd.setEofData(LM.D);
502 LM.Toks.push_back(BodyEnd);
503 // Append the current token at the end of the new token stream so that it
505 LM.Toks.push_back(Tok);
506 PP.EnterTokenStream(LM.Toks, true, /*IsReinject*/true);
508 // Consume the previously pushed token.
509 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
510 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
511 && "Inline method not starting with '{', ':' or 'try'");
513 // Parse the method body. Function body parsing code is similar enough
514 // to be re-used for method bodies as well.
515 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
516 Scope::CompoundStmtScope);
517 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
519 if (Tok.is(tok::kw_try)) {
520 ParseFunctionTryBlock(LM.D, FnScope);
522 while (Tok.isNot(tok::eof))
525 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
529 if (Tok.is(tok::colon)) {
530 ParseConstructorInitializer(LM.D);
533 if (!Tok.is(tok::l_brace)) {
535 Actions.ActOnFinishFunctionBody(LM.D, nullptr);
537 while (Tok.isNot(tok::eof))
540 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
545 Actions.ActOnDefaultCtorInitializers(LM.D);
547 assert((Actions.getDiagnostics().hasErrorOccurred() ||
548 !isa<FunctionTemplateDecl>(LM.D) ||
549 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
550 < TemplateParameterDepth) &&
551 "TemplateParameterDepth should be greater than the depth of "
552 "current template being instantiated!");
554 ParseFunctionStatementBody(LM.D, FnScope);
556 while (Tok.isNot(tok::eof))
559 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
562 if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
563 if (isa<CXXMethodDecl>(FD) ||
564 FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend))
565 Actions.ActOnFinishInlineFunctionDef(FD);
568 /// ParseLexedMemberInitializers - We finished parsing the member specification
569 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
570 /// initializers that were collected during its parsing and parse them all.
571 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
572 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
573 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
575 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
576 if (HasTemplateScope) {
577 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
578 ++CurTemplateDepthTracker;
580 // Set or update the scope flags.
581 bool AlreadyHasClassScope = Class.TopLevelClass;
582 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
583 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
584 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
586 if (!AlreadyHasClassScope)
587 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
588 Class.TagOrTemplate);
590 if (!Class.LateParsedDeclarations.empty()) {
591 // C++11 [expr.prim.general]p4:
592 // Otherwise, if a member-declarator declares a non-static data member
593 // (9.2) of a class X, the expression this is a prvalue of type "pointer
594 // to X" within the optional brace-or-equal-initializer. It shall not
595 // appear elsewhere in the member-declarator.
596 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
599 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
600 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
604 if (!AlreadyHasClassScope)
605 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
606 Class.TagOrTemplate);
608 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
611 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
612 if (!MI.Field || MI.Field->isInvalidDecl())
615 ParenBraceBracketBalancer BalancerRAIIObj(*this);
617 // Append the current token at the end of the new token stream so that it
619 MI.Toks.push_back(Tok);
620 PP.EnterTokenStream(MI.Toks, true, /*IsReinject*/true);
622 // Consume the previously pushed token.
623 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
625 SourceLocation EqualLoc;
627 Actions.ActOnStartCXXInClassMemberInitializer();
629 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
632 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
635 // The next token should be our artificial terminating EOF token.
636 if (Tok.isNot(tok::eof)) {
637 if (!Init.isInvalid()) {
638 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
639 if (!EndLoc.isValid())
640 EndLoc = Tok.getLocation();
641 // No fixit; we can't recover as if there were a semicolon here.
642 Diag(EndLoc, diag::err_expected_semi_decl_list);
645 // Consume tokens until we hit the artificial EOF.
646 while (Tok.isNot(tok::eof))
649 // Make sure this is *our* artificial EOF token.
650 if (Tok.getEofData() == MI.Field)
654 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
655 /// container until the token 'T' is reached (which gets
656 /// consumed/stored too, if ConsumeFinalToken).
657 /// If StopAtSemi is true, then we will stop early at a ';' character.
658 /// Returns true if token 'T1' or 'T2' was found.
659 /// NOTE: This is a specialized version of Parser::SkipUntil.
660 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
662 bool StopAtSemi, bool ConsumeFinalToken) {
663 // We always want this function to consume at least one token if the first
664 // token isn't T and if not at EOF.
665 bool isFirstTokenConsumed = true;
667 // If we found one of the tokens, stop and return true.
668 if (Tok.is(T1) || Tok.is(T2)) {
669 if (ConsumeFinalToken) {
676 switch (Tok.getKind()) {
678 case tok::annot_module_begin:
679 case tok::annot_module_end:
680 case tok::annot_module_include:
681 // Ran out of tokens.
685 // Recursively consume properly-nested parens.
688 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
691 // Recursively consume properly-nested square brackets.
694 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
697 // Recursively consume properly-nested braces.
700 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
703 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
704 // Since the user wasn't looking for this token (if they were, it would
705 // already be handled), this isn't balanced. If there is a LHS token at a
706 // higher level, we will assume that this matches the unbalanced token
707 // and return it. Otherwise, this is a spurious RHS token, which we skip.
709 if (ParenCount && !isFirstTokenConsumed)
710 return false; // Matches something.
715 if (BracketCount && !isFirstTokenConsumed)
716 return false; // Matches something.
721 if (BraceCount && !isFirstTokenConsumed)
722 return false; // Matches something.
732 // consume this token.
734 ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true);
737 isFirstTokenConsumed = false;
741 /// Consume tokens and store them in the passed token container until
742 /// we've passed the try keyword and constructor initializers and have consumed
743 /// the opening brace of the function body. The opening brace will be consumed
744 /// if and only if there was no error.
746 /// \return True on error.
747 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
748 if (Tok.is(tok::kw_try)) {
753 if (Tok.isNot(tok::colon)) {
754 // Easy case, just a function body.
756 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
757 // brace: an opening one is the function body, while a closing one probably
758 // means we've reached the end of the class.
759 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
761 /*ConsumeFinalToken=*/false);
762 if (Tok.isNot(tok::l_brace))
763 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
773 // We can't reliably skip over a mem-initializer-id, because it could be
774 // a template-id involving not-yet-declared names. Given:
776 // S ( ) : a < b < c > ( e )
778 // 'e' might be an initializer or part of a template argument, depending
779 // on whether 'b' is a template.
781 // Track whether we might be inside a template argument. We can give
782 // significantly better diagnostics if we know that we're not.
783 bool MightBeTemplateArgument = false;
786 // Skip over the mem-initializer-id, if possible.
787 if (Tok.is(tok::kw_decltype)) {
789 SourceLocation OpenLoc = ConsumeToken();
790 if (Tok.isNot(tok::l_paren))
791 return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
795 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
796 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
797 Diag(OpenLoc, diag::note_matching) << tok::l_paren;
802 // Walk over a component of a nested-name-specifier.
803 if (Tok.is(tok::coloncolon)) {
807 if (Tok.is(tok::kw_template)) {
813 if (Tok.is(tok::identifier)) {
819 } while (Tok.is(tok::coloncolon));
821 if (Tok.is(tok::code_completion)) {
823 ConsumeCodeCompletionToken();
824 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) {
825 // Could be the start of another member initializer (the ',' has not
831 if (Tok.is(tok::comma)) {
832 // The initialization is missing, we'll diagnose it later.
837 if (Tok.is(tok::less))
838 MightBeTemplateArgument = true;
840 if (MightBeTemplateArgument) {
841 // We may be inside a template argument list. Grab up to the start of the
842 // next parenthesized initializer or braced-init-list. This *might* be the
843 // initializer, or it might be a subexpression in the template argument
845 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
846 // if all angles are closed.
847 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
849 /*ConsumeFinalToken=*/false)) {
850 // We're not just missing the initializer, we're also missing the
852 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
854 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
855 // We found something weird in a mem-initializer-id.
856 if (getLangOpts().CPlusPlus11)
857 return Diag(Tok.getLocation(), diag::err_expected_either)
858 << tok::l_paren << tok::l_brace;
860 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
863 tok::TokenKind kind = Tok.getKind();
865 bool IsLParen = (kind == tok::l_paren);
866 SourceLocation OpenLoc = Tok.getLocation();
871 assert(kind == tok::l_brace && "Must be left paren or brace here.");
873 // In C++03, this has to be the start of the function body, which
874 // means the initializer is malformed; we'll diagnose it later.
875 if (!getLangOpts().CPlusPlus11)
878 const Token &PreviousToken = Toks[Toks.size() - 2];
879 if (!MightBeTemplateArgument &&
880 !PreviousToken.isOneOf(tok::identifier, tok::greater,
881 tok::greatergreater)) {
882 // If the opening brace is not preceded by one of these tokens, we are
883 // missing the mem-initializer-id. In order to recover better, we need
884 // to use heuristics to determine if this '{' is most likely the
885 // beginning of a brace-init-list or the function body.
886 // Check the token after the corresponding '}'.
887 TentativeParsingAction PA(*this);
888 if (SkipUntil(tok::r_brace) &&
889 !Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) {
890 // Consider there was a malformed initializer and this is the start
891 // of the function body. We'll diagnose it later.
899 // Grab the initializer (or the subexpression of the template argument).
900 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
901 // if we might be inside the braces of a lambda-expression.
902 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
903 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
904 Diag(Tok, diag::err_expected) << CloseKind;
905 Diag(OpenLoc, diag::note_matching) << kind;
909 // Grab pack ellipsis, if present.
910 if (Tok.is(tok::ellipsis)) {
915 // If we know we just consumed a mem-initializer, we must have ',' or '{'
917 if (Tok.is(tok::comma)) {
920 } else if (Tok.is(tok::l_brace)) {
921 // This is the function body if the ')' or '}' is immediately followed by
922 // a '{'. That cannot happen within a template argument, apart from the
923 // case where a template argument contains a compound literal:
925 // S ( ) : a < b < c > ( d ) { }
926 // // End of declaration, or still inside the template argument?
928 // ... and the case where the template argument contains a lambda:
930 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
933 // FIXME: Disambiguate these cases. Note that the latter case is probably
934 // going to be made ill-formed by core issue 1607.
938 } else if (!MightBeTemplateArgument) {
939 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
945 /// Consume and store tokens from the '?' to the ':' in a conditional
947 bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
949 assert(Tok.is(tok::question));
953 while (Tok.isNot(tok::colon)) {
954 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
956 /*ConsumeFinalToken=*/false))
959 // If we found a nested conditional, consume it.
960 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
970 /// A tentative parsing action that can also revert token annotations.
971 class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
973 explicit UnannotatedTentativeParsingAction(Parser &Self,
974 tok::TokenKind EndKind)
975 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
976 // Stash away the old token stream, so we can restore it once the
977 // tentative parse is complete.
978 TentativeParsingAction Inner(Self);
979 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
983 void RevertAnnotations() {
986 // Put back the original tokens.
987 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
989 auto Buffer = llvm::make_unique<Token[]>(Toks.size());
990 std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
991 Buffer[Toks.size() - 1] = Self.Tok;
992 Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true,
993 /*IsReinject*/ true);
995 Self.Tok = Toks.front();
1002 tok::TokenKind EndKind;
1005 /// ConsumeAndStoreInitializer - Consume and store the token at the passed token
1006 /// container until the end of the current initializer expression (either a
1007 /// default argument or an in-class initializer for a non-static data member).
1009 /// Returns \c true if we reached the end of something initializer-shaped,
1010 /// \c false if we bailed out.
1011 bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
1012 CachedInitKind CIK) {
1013 // We always want this function to consume at least one token if not at EOF.
1014 bool IsFirstToken = true;
1016 // Number of possible unclosed <s we've seen so far. These might be templates,
1017 // and might not, but if there were none of them (or we know for sure that
1018 // we're within a template), we can avoid a tentative parse.
1019 unsigned AngleCount = 0;
1020 unsigned KnownTemplateCount = 0;
1023 switch (Tok.getKind()) {
1025 // If we might be in a template, perform a tentative parse to check.
1027 // Not a template argument: this is the end of the initializer.
1029 if (KnownTemplateCount)
1032 // We hit a comma inside angle brackets. This is the hard case. The
1033 // rule we follow is:
1034 // * For a default argument, if the tokens after the comma form a
1035 // syntactically-valid parameter-declaration-clause, in which each
1036 // parameter has an initializer, then this comma ends the default
1038 // * For a default initializer, if the tokens after the comma form a
1039 // syntactically-valid init-declarator-list, then this comma ends
1040 // the default initializer.
1042 UnannotatedTentativeParsingAction PA(*this,
1043 CIK == CIK_DefaultInitializer
1044 ? tok::semi : tok::r_paren);
1045 Sema::TentativeAnalysisScope Scope(Actions);
1047 TPResult Result = TPResult::Error;
1050 case CIK_DefaultInitializer:
1051 Result = TryParseInitDeclaratorList();
1052 // If we parsed a complete, ambiguous init-declarator-list, this
1053 // is only syntactically-valid if it's followed by a semicolon.
1054 if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi))
1055 Result = TPResult::False;
1058 case CIK_DefaultArgument:
1059 bool InvalidAsDeclaration = false;
1060 Result = TryParseParameterDeclarationClause(
1061 &InvalidAsDeclaration, /*VersusTemplateArg=*/true);
1062 // If this is an expression or a declaration with a missing
1063 // 'typename', assume it's not a declaration.
1064 if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1065 Result = TPResult::False;
1069 // If what follows could be a declaration, it is a declaration.
1070 if (Result != TPResult::False && Result != TPResult::Error) {
1075 // In the uncommon case that we decide the following tokens are part
1076 // of a template argument, revert any annotations we've performed in
1077 // those tokens. We're not going to look them up until we've parsed
1078 // the rest of the class, and that might add more declarations.
1079 PA.RevertAnnotations();
1082 // Keep going. We know we're inside a template argument list now.
1083 ++KnownTemplateCount;
1087 case tok::annot_module_begin:
1088 case tok::annot_module_end:
1089 case tok::annot_module_include:
1090 // Ran out of tokens.
1094 // FIXME: A '<' can only start a template-id if it's preceded by an
1095 // identifier, an operator-function-id, or a literal-operator-id.
1100 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1101 // that is *never* the end of the initializer. Skip to the ':'.
1102 if (!ConsumeAndStoreConditional(Toks))
1106 case tok::greatergreatergreater:
1107 if (!getLangOpts().CPlusPlus11)
1109 if (AngleCount) --AngleCount;
1110 if (KnownTemplateCount) --KnownTemplateCount;
1112 case tok::greatergreater:
1113 if (!getLangOpts().CPlusPlus11)
1115 if (AngleCount) --AngleCount;
1116 if (KnownTemplateCount) --KnownTemplateCount;
1119 if (AngleCount) --AngleCount;
1120 if (KnownTemplateCount) --KnownTemplateCount;
1123 case tok::kw_template:
1124 // 'template' identifier '<' is known to start a template argument list,
1125 // and can be used to disambiguate the parse.
1126 // FIXME: Support all forms of 'template' unqualified-id '<'.
1127 Toks.push_back(Tok);
1129 if (Tok.is(tok::identifier)) {
1130 Toks.push_back(Tok);
1132 if (Tok.is(tok::less)) {
1134 ++KnownTemplateCount;
1135 Toks.push_back(Tok);
1141 case tok::kw_operator:
1142 // If 'operator' precedes other punctuation, that punctuation loses
1143 // its special behavior.
1144 Toks.push_back(Tok);
1146 switch (Tok.getKind()) {
1148 case tok::greatergreatergreater:
1149 case tok::greatergreater:
1152 Toks.push_back(Tok);
1161 // Recursively consume properly-nested parens.
1162 Toks.push_back(Tok);
1164 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1167 // Recursively consume properly-nested square brackets.
1168 Toks.push_back(Tok);
1170 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
1173 // Recursively consume properly-nested braces.
1174 Toks.push_back(Tok);
1176 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1179 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1180 // Since the user wasn't looking for this token (if they were, it would
1181 // already be handled), this isn't balanced. If there is a LHS token at a
1182 // higher level, we will assume that this matches the unbalanced token
1183 // and return it. Otherwise, this is a spurious RHS token, which we
1184 // consume and pass on to downstream code to diagnose.
1186 if (CIK == CIK_DefaultArgument)
1187 return true; // End of the default argument.
1188 if (ParenCount && !IsFirstToken)
1190 Toks.push_back(Tok);
1194 if (BracketCount && !IsFirstToken)
1196 Toks.push_back(Tok);
1200 if (BraceCount && !IsFirstToken)
1202 Toks.push_back(Tok);
1206 case tok::code_completion:
1207 Toks.push_back(Tok);
1208 ConsumeCodeCompletionToken();
1211 case tok::string_literal:
1212 case tok::wide_string_literal:
1213 case tok::utf8_string_literal:
1214 case tok::utf16_string_literal:
1215 case tok::utf32_string_literal:
1216 Toks.push_back(Tok);
1217 ConsumeStringToken();
1220 if (CIK == CIK_DefaultInitializer)
1221 return true; // End of the default initializer.
1225 Toks.push_back(Tok);
1229 IsFirstToken = false;