1 //===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements parsing for C++ class inline methods.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Parse/Parser.h"
15 #include "clang/AST/DeclTemplate.h"
16 #include "clang/Parse/ParseDiagnostic.h"
17 #include "clang/Parse/RAIIObjectsForParser.h"
18 #include "clang/Sema/DeclSpec.h"
19 #include "clang/Sema/Scope.h"
20 using namespace clang;
22 /// ParseCXXInlineMethodDef - We parsed and verified that the specified
23 /// Declarator is a well formed C++ inline method definition. Now lex its body
24 /// and store its tokens for parsing after the C++ class is complete.
25 NamedDecl *Parser::ParseCXXInlineMethodDef(
26 AccessSpecifier AS, ParsedAttributes &AccessAttrs, ParsingDeclarator &D,
27 const ParsedTemplateInfo &TemplateInfo, const VirtSpecifiers &VS,
28 SourceLocation PureSpecLoc) {
29 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
30 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
31 "Current token not a '{', ':', '=', or 'try'!");
33 MultiTemplateParamsArg TemplateParams(
34 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
36 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
39 if (D.getDeclSpec().isFriendSpecified())
40 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
43 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
44 TemplateParams, nullptr,
47 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
48 if (PureSpecLoc.isValid())
49 Actions.ActOnPureSpecifier(FnD, PureSpecLoc);
54 HandleMemberFunctionDeclDelays(D, FnD);
58 if (TryConsumeToken(tok::equal)) {
66 SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
67 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
68 Diag(KWLoc, getLangOpts().CPlusPlus11
69 ? diag::warn_cxx98_compat_defaulted_deleted_function
70 : diag::ext_defaulted_deleted_function)
72 Actions.SetDeclDeleted(FnD, KWLoc);
74 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
75 DeclAsFunction->setRangeEnd(KWEndLoc);
77 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
78 Diag(KWLoc, getLangOpts().CPlusPlus11
79 ? diag::warn_cxx98_compat_defaulted_deleted_function
80 : diag::ext_defaulted_deleted_function)
82 Actions.SetDeclDefaulted(FnD, KWLoc);
83 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
84 DeclAsFunction->setRangeEnd(KWEndLoc);
87 llvm_unreachable("function definition after = not 'delete' or 'default'");
90 if (Tok.is(tok::comma)) {
91 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
94 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
95 Delete ? "delete" : "default")) {
102 if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) &&
103 trySkippingFunctionBody()) {
104 Actions.ActOnSkippedFunctionBody(FnD);
108 // In delayed template parsing mode, if we are within a class template
109 // or if we are about to parse function member template then consume
110 // the tokens and store them for parsing at the end of the translation unit.
111 if (getLangOpts().DelayedTemplateParsing &&
112 D.getFunctionDefinitionKind() == FDK_Definition &&
113 !D.getDeclSpec().isConstexprSpecified() &&
114 !(FnD && FnD->getAsFunction() &&
115 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
116 ((Actions.CurContext->isDependentContext() ||
117 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
118 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
119 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
122 LexTemplateFunctionForLateParsing(Toks);
125 FunctionDecl *FD = FnD->getAsFunction();
126 Actions.CheckForFunctionRedefinition(FD);
127 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
133 // Consume the tokens and store them for later parsing.
135 LexedMethod* LM = new LexedMethod(this, FnD);
136 getCurrentClass().LateParsedDeclarations.push_back(LM);
137 LM->TemplateScope = getCurScope()->isTemplateParamScope();
138 CachedTokens &Toks = LM->Toks;
140 tok::TokenKind kind = Tok.getKind();
141 // Consume everything up to (and including) the left brace of the
143 if (ConsumeAndStoreFunctionPrologue(Toks)) {
144 // We didn't find the left-brace we expected after the
145 // constructor initializer; we already printed an error, and it's likely
146 // impossible to recover, so don't try to parse this method later.
147 // Skip over the rest of the decl and back to somewhere that looks
150 delete getCurrentClass().LateParsedDeclarations.back();
151 getCurrentClass().LateParsedDeclarations.pop_back();
154 // Consume everything up to (and including) the matching right brace.
155 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
158 // If we're in a function-try-block, we need to store all the catch blocks.
159 if (kind == tok::kw_try) {
160 while (Tok.is(tok::kw_catch)) {
161 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
162 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
167 FunctionDecl *FD = FnD->getAsFunction();
168 // Track that this function will eventually have a body; Sema needs
170 Actions.CheckForFunctionRedefinition(FD);
171 FD->setWillHaveBody(true);
173 // If semantic analysis could not build a function declaration,
174 // just throw away the late-parsed declaration.
175 delete getCurrentClass().LateParsedDeclarations.back();
176 getCurrentClass().LateParsedDeclarations.pop_back();
182 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
183 /// specified Declarator is a well formed C++ non-static data member
184 /// declaration. Now lex its initializer and store its tokens for parsing
185 /// after the class is complete.
186 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
187 assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
188 "Current token not a '{' or '='!");
190 LateParsedMemberInitializer *MI =
191 new LateParsedMemberInitializer(this, VarD);
192 getCurrentClass().LateParsedDeclarations.push_back(MI);
193 CachedTokens &Toks = MI->Toks;
195 tok::TokenKind kind = Tok.getKind();
196 if (kind == tok::equal) {
201 if (kind == tok::l_brace) {
202 // Begin by storing the '{' token.
206 // Consume everything up to (and including) the matching right brace.
207 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
209 // Consume everything up to (but excluding) the comma or semicolon.
210 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
213 // Store an artificial EOF token to ensure that we don't run off the end of
214 // the initializer when we come to parse it.
217 Eof.setKind(tok::eof);
218 Eof.setLocation(Tok.getLocation());
219 Eof.setEofData(VarD);
223 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
224 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
225 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
226 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
228 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
229 : Self(P), Class(C) {}
231 Parser::LateParsedClass::~LateParsedClass() {
232 Self->DeallocateParsedClasses(Class);
235 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
236 Self->ParseLexedMethodDeclarations(*Class);
239 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
240 Self->ParseLexedMemberInitializers(*Class);
243 void Parser::LateParsedClass::ParseLexedMethodDefs() {
244 Self->ParseLexedMethodDefs(*Class);
247 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
248 Self->ParseLexedMethodDeclaration(*this);
251 void Parser::LexedMethod::ParseLexedMethodDefs() {
252 Self->ParseLexedMethodDef(*this);
255 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
256 Self->ParseLexedMemberInitializer(*this);
259 /// ParseLexedMethodDeclarations - We finished parsing the member
260 /// specification of a top (non-nested) C++ class. Now go over the
261 /// stack of method declarations with some parts for which parsing was
262 /// delayed (such as default arguments) and parse them.
263 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
264 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
265 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
267 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
268 if (HasTemplateScope) {
269 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
270 ++CurTemplateDepthTracker;
273 // The current scope is still active if we're the top-level class.
274 // Otherwise we'll need to push and enter a new scope.
275 bool HasClassScope = !Class.TopLevelClass;
276 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
279 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
280 Class.TagOrTemplate);
282 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
283 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
287 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
288 Class.TagOrTemplate);
291 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
292 // If this is a member template, introduce the template parameter scope.
293 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
294 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
295 if (LM.TemplateScope) {
296 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
297 ++CurTemplateDepthTracker;
299 // Start the delayed C++ method declaration
300 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
302 // Introduce the parameters into scope and parse their default
304 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
305 Scope::FunctionDeclarationScope | Scope::DeclScope);
306 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
307 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
308 // Introduce the parameter into scope.
309 bool HasUnparsed = Param->hasUnparsedDefaultArg();
310 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
311 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
313 ParenBraceBracketBalancer BalancerRAIIObj(*this);
315 // Mark the end of the default argument so that we know when to stop when
316 // we parse it later on.
317 Token LastDefaultArgToken = Toks->back();
319 DefArgEnd.startToken();
320 DefArgEnd.setKind(tok::eof);
321 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
322 DefArgEnd.setEofData(Param);
323 Toks->push_back(DefArgEnd);
325 // Parse the default argument from its saved token stream.
326 Toks->push_back(Tok); // So that the current token doesn't get lost
327 PP.EnterTokenStream(*Toks, true);
329 // Consume the previously-pushed token.
333 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
334 SourceLocation EqualLoc = ConsumeToken();
336 // The argument isn't actually potentially evaluated unless it is
338 EnterExpressionEvaluationContext Eval(
340 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param);
342 ExprResult DefArgResult;
343 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
344 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
345 DefArgResult = ParseBraceInitializer();
347 DefArgResult = ParseAssignmentExpression();
348 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
349 if (DefArgResult.isInvalid()) {
350 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
352 if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
353 // The last two tokens are the terminator and the saved value of
354 // Tok; the last token in the default argument is the one before
356 assert(Toks->size() >= 3 && "expected a token in default arg");
357 Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
358 << SourceRange(Tok.getLocation(),
359 (*Toks)[Toks->size() - 3].getLocation());
361 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
365 // There could be leftover tokens (e.g. because of an error).
366 // Skip through until we reach the 'end of default argument' token.
367 while (Tok.isNot(tok::eof))
370 if (Tok.is(tok::eof) && Tok.getEofData() == Param)
372 } else if (HasUnparsed) {
373 assert(Param->hasInheritedDefaultArg());
374 FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
375 ParmVarDecl *OldParam = Old->getParamDecl(I);
376 assert (!OldParam->hasUnparsedDefaultArg());
377 if (OldParam->hasUninstantiatedDefaultArg())
378 Param->setUninstantiatedDefaultArg(
379 OldParam->getUninstantiatedDefaultArg());
381 Param->setDefaultArg(OldParam->getInit());
385 // Parse a delayed exception-specification, if there is one.
386 if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
387 ParenBraceBracketBalancer BalancerRAIIObj(*this);
389 // Add the 'stop' token.
390 Token LastExceptionSpecToken = Toks->back();
391 Token ExceptionSpecEnd;
392 ExceptionSpecEnd.startToken();
393 ExceptionSpecEnd.setKind(tok::eof);
394 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
395 ExceptionSpecEnd.setEofData(LM.Method);
396 Toks->push_back(ExceptionSpecEnd);
398 // Parse the default argument from its saved token stream.
399 Toks->push_back(Tok); // So that the current token doesn't get lost
400 PP.EnterTokenStream(*Toks, true);
402 // Consume the previously-pushed token.
405 // C++11 [expr.prim.general]p3:
406 // If a declaration declares a member function or member function
407 // template of a class X, the expression this is a prvalue of type
408 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
409 // and the end of the function-definition, member-declarator, or
411 CXXMethodDecl *Method;
412 if (FunctionTemplateDecl *FunTmpl
413 = dyn_cast<FunctionTemplateDecl>(LM.Method))
414 Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
416 Method = cast<CXXMethodDecl>(LM.Method);
418 Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
419 Method->getTypeQualifiers(),
420 getLangOpts().CPlusPlus11);
422 // Parse the exception-specification.
423 SourceRange SpecificationRange;
424 SmallVector<ParsedType, 4> DynamicExceptions;
425 SmallVector<SourceRange, 4> DynamicExceptionRanges;
426 ExprResult NoexceptExpr;
427 CachedTokens *ExceptionSpecTokens;
429 ExceptionSpecificationType EST
430 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
432 DynamicExceptionRanges, NoexceptExpr,
433 ExceptionSpecTokens);
435 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
436 Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
438 // Attach the exception-specification to the method.
439 Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
442 DynamicExceptionRanges,
443 NoexceptExpr.isUsable()?
444 NoexceptExpr.get() : nullptr);
446 // There could be leftover tokens (e.g. because of an error).
447 // Skip through until we reach the original token position.
448 while (Tok.isNot(tok::eof))
451 // Clean up the remaining EOF token.
452 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
456 LM.ExceptionSpecTokens = nullptr;
459 PrototypeScope.Exit();
461 // Finish the delayed C++ method declaration.
462 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
465 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
466 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
467 /// collected during its parsing and parse them all.
468 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
469 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
470 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
471 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
472 if (HasTemplateScope) {
473 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
474 ++CurTemplateDepthTracker;
476 bool HasClassScope = !Class.TopLevelClass;
477 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
480 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
481 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
485 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
486 // If this is a member template, introduce the template parameter scope.
487 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
488 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
489 if (LM.TemplateScope) {
490 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
491 ++CurTemplateDepthTracker;
494 ParenBraceBracketBalancer BalancerRAIIObj(*this);
496 assert(!LM.Toks.empty() && "Empty body!");
497 Token LastBodyToken = LM.Toks.back();
499 BodyEnd.startToken();
500 BodyEnd.setKind(tok::eof);
501 BodyEnd.setLocation(LastBodyToken.getEndLoc());
502 BodyEnd.setEofData(LM.D);
503 LM.Toks.push_back(BodyEnd);
504 // Append the current token at the end of the new token stream so that it
506 LM.Toks.push_back(Tok);
507 PP.EnterTokenStream(LM.Toks, true);
509 // Consume the previously pushed token.
510 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
511 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
512 && "Inline method not starting with '{', ':' or 'try'");
514 // Parse the method body. Function body parsing code is similar enough
515 // to be re-used for method bodies as well.
516 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
517 Scope::CompoundStmtScope);
518 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
520 if (Tok.is(tok::kw_try)) {
521 ParseFunctionTryBlock(LM.D, FnScope);
523 while (Tok.isNot(tok::eof))
526 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
530 if (Tok.is(tok::colon)) {
531 ParseConstructorInitializer(LM.D);
534 if (!Tok.is(tok::l_brace)) {
536 Actions.ActOnFinishFunctionBody(LM.D, nullptr);
538 while (Tok.isNot(tok::eof))
541 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
546 Actions.ActOnDefaultCtorInitializers(LM.D);
548 assert((Actions.getDiagnostics().hasErrorOccurred() ||
549 !isa<FunctionTemplateDecl>(LM.D) ||
550 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
551 < TemplateParameterDepth) &&
552 "TemplateParameterDepth should be greater than the depth of "
553 "current template being instantiated!");
555 ParseFunctionStatementBody(LM.D, FnScope);
557 while (Tok.isNot(tok::eof))
560 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
563 if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
564 if (isa<CXXMethodDecl>(FD) ||
565 FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend))
566 Actions.ActOnFinishInlineFunctionDef(FD);
569 /// ParseLexedMemberInitializers - We finished parsing the member specification
570 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
571 /// initializers that were collected during its parsing and parse them all.
572 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
573 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
574 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
576 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
577 if (HasTemplateScope) {
578 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
579 ++CurTemplateDepthTracker;
581 // Set or update the scope flags.
582 bool AlreadyHasClassScope = Class.TopLevelClass;
583 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
584 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
585 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
587 if (!AlreadyHasClassScope)
588 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
589 Class.TagOrTemplate);
591 if (!Class.LateParsedDeclarations.empty()) {
592 // C++11 [expr.prim.general]p4:
593 // Otherwise, if a member-declarator declares a non-static data member
594 // (9.2) of a class X, the expression this is a prvalue of type "pointer
595 // to X" within the optional brace-or-equal-initializer. It shall not
596 // appear elsewhere in the member-declarator.
597 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
600 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
601 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
605 if (!AlreadyHasClassScope)
606 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
607 Class.TagOrTemplate);
609 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
612 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
613 if (!MI.Field || MI.Field->isInvalidDecl())
616 ParenBraceBracketBalancer BalancerRAIIObj(*this);
618 // Append the current token at the end of the new token stream so that it
620 MI.Toks.push_back(Tok);
621 PP.EnterTokenStream(MI.Toks, true);
623 // Consume the previously pushed token.
624 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
626 SourceLocation EqualLoc;
628 Actions.ActOnStartCXXInClassMemberInitializer();
630 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
633 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
636 // The next token should be our artificial terminating EOF token.
637 if (Tok.isNot(tok::eof)) {
638 if (!Init.isInvalid()) {
639 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
640 if (!EndLoc.isValid())
641 EndLoc = Tok.getLocation();
642 // No fixit; we can't recover as if there were a semicolon here.
643 Diag(EndLoc, diag::err_expected_semi_decl_list);
646 // Consume tokens until we hit the artificial EOF.
647 while (Tok.isNot(tok::eof))
650 // Make sure this is *our* artificial EOF token.
651 if (Tok.getEofData() == MI.Field)
655 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
656 /// container until the token 'T' is reached (which gets
657 /// consumed/stored too, if ConsumeFinalToken).
658 /// If StopAtSemi is true, then we will stop early at a ';' character.
659 /// Returns true if token 'T1' or 'T2' was found.
660 /// NOTE: This is a specialized version of Parser::SkipUntil.
661 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
663 bool StopAtSemi, bool ConsumeFinalToken) {
664 // We always want this function to consume at least one token if the first
665 // token isn't T and if not at EOF.
666 bool isFirstTokenConsumed = true;
668 // If we found one of the tokens, stop and return true.
669 if (Tok.is(T1) || Tok.is(T2)) {
670 if (ConsumeFinalToken) {
677 switch (Tok.getKind()) {
679 case tok::annot_module_begin:
680 case tok::annot_module_end:
681 case tok::annot_module_include:
682 // Ran out of tokens.
686 // Recursively consume properly-nested parens.
689 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
692 // Recursively consume properly-nested square brackets.
695 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
698 // Recursively consume properly-nested braces.
701 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
704 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
705 // Since the user wasn't looking for this token (if they were, it would
706 // already be handled), this isn't balanced. If there is a LHS token at a
707 // higher level, we will assume that this matches the unbalanced token
708 // and return it. Otherwise, this is a spurious RHS token, which we skip.
710 if (ParenCount && !isFirstTokenConsumed)
711 return false; // Matches something.
716 if (BracketCount && !isFirstTokenConsumed)
717 return false; // Matches something.
722 if (BraceCount && !isFirstTokenConsumed)
723 return false; // Matches something.
733 // consume this token.
735 ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true);
738 isFirstTokenConsumed = false;
742 /// Consume tokens and store them in the passed token container until
743 /// we've passed the try keyword and constructor initializers and have consumed
744 /// the opening brace of the function body. The opening brace will be consumed
745 /// if and only if there was no error.
747 /// \return True on error.
748 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
749 if (Tok.is(tok::kw_try)) {
754 if (Tok.isNot(tok::colon)) {
755 // Easy case, just a function body.
757 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
758 // brace: an opening one is the function body, while a closing one probably
759 // means we've reached the end of the class.
760 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
762 /*ConsumeFinalToken=*/false);
763 if (Tok.isNot(tok::l_brace))
764 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
774 // We can't reliably skip over a mem-initializer-id, because it could be
775 // a template-id involving not-yet-declared names. Given:
777 // S ( ) : a < b < c > ( e )
779 // 'e' might be an initializer or part of a template argument, depending
780 // on whether 'b' is a template.
782 // Track whether we might be inside a template argument. We can give
783 // significantly better diagnostics if we know that we're not.
784 bool MightBeTemplateArgument = false;
787 // Skip over the mem-initializer-id, if possible.
788 if (Tok.is(tok::kw_decltype)) {
790 SourceLocation OpenLoc = ConsumeToken();
791 if (Tok.isNot(tok::l_paren))
792 return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
796 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
797 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
798 Diag(OpenLoc, diag::note_matching) << tok::l_paren;
803 // Walk over a component of a nested-name-specifier.
804 if (Tok.is(tok::coloncolon)) {
808 if (Tok.is(tok::kw_template)) {
814 if (Tok.is(tok::identifier)) {
820 } while (Tok.is(tok::coloncolon));
822 if (Tok.is(tok::code_completion)) {
824 ConsumeCodeCompletionToken();
825 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) {
826 // Could be the start of another member initializer (the ',' has not
832 if (Tok.is(tok::comma)) {
833 // The initialization is missing, we'll diagnose it later.
838 if (Tok.is(tok::less))
839 MightBeTemplateArgument = true;
841 if (MightBeTemplateArgument) {
842 // We may be inside a template argument list. Grab up to the start of the
843 // next parenthesized initializer or braced-init-list. This *might* be the
844 // initializer, or it might be a subexpression in the template argument
846 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
847 // if all angles are closed.
848 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
850 /*ConsumeFinalToken=*/false)) {
851 // We're not just missing the initializer, we're also missing the
853 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
855 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
856 // We found something weird in a mem-initializer-id.
857 if (getLangOpts().CPlusPlus11)
858 return Diag(Tok.getLocation(), diag::err_expected_either)
859 << tok::l_paren << tok::l_brace;
861 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
864 tok::TokenKind kind = Tok.getKind();
866 bool IsLParen = (kind == tok::l_paren);
867 SourceLocation OpenLoc = Tok.getLocation();
872 assert(kind == tok::l_brace && "Must be left paren or brace here.");
874 // In C++03, this has to be the start of the function body, which
875 // means the initializer is malformed; we'll diagnose it later.
876 if (!getLangOpts().CPlusPlus11)
879 const Token &PreviousToken = Toks[Toks.size() - 2];
880 if (!MightBeTemplateArgument &&
881 !PreviousToken.isOneOf(tok::identifier, tok::greater,
882 tok::greatergreater)) {
883 // If the opening brace is not preceded by one of these tokens, we are
884 // missing the mem-initializer-id. In order to recover better, we need
885 // to use heuristics to determine if this '{' is most likely the
886 // beginning of a brace-init-list or the function body.
887 // Check the token after the corresponding '}'.
888 TentativeParsingAction PA(*this);
889 if (SkipUntil(tok::r_brace) &&
890 !Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) {
891 // Consider there was a malformed initializer and this is the start
892 // of the function body. We'll diagnose it later.
900 // Grab the initializer (or the subexpression of the template argument).
901 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
902 // if we might be inside the braces of a lambda-expression.
903 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
904 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
905 Diag(Tok, diag::err_expected) << CloseKind;
906 Diag(OpenLoc, diag::note_matching) << kind;
910 // Grab pack ellipsis, if present.
911 if (Tok.is(tok::ellipsis)) {
916 // If we know we just consumed a mem-initializer, we must have ',' or '{'
918 if (Tok.is(tok::comma)) {
921 } else if (Tok.is(tok::l_brace)) {
922 // This is the function body if the ')' or '}' is immediately followed by
923 // a '{'. That cannot happen within a template argument, apart from the
924 // case where a template argument contains a compound literal:
926 // S ( ) : a < b < c > ( d ) { }
927 // // End of declaration, or still inside the template argument?
929 // ... and the case where the template argument contains a lambda:
931 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
934 // FIXME: Disambiguate these cases. Note that the latter case is probably
935 // going to be made ill-formed by core issue 1607.
939 } else if (!MightBeTemplateArgument) {
940 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
946 /// Consume and store tokens from the '?' to the ':' in a conditional
948 bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
950 assert(Tok.is(tok::question));
954 while (Tok.isNot(tok::colon)) {
955 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
957 /*ConsumeFinalToken=*/false))
960 // If we found a nested conditional, consume it.
961 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
971 /// A tentative parsing action that can also revert token annotations.
972 class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
974 explicit UnannotatedTentativeParsingAction(Parser &Self,
975 tok::TokenKind EndKind)
976 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
977 // Stash away the old token stream, so we can restore it once the
978 // tentative parse is complete.
979 TentativeParsingAction Inner(Self);
980 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
984 void RevertAnnotations() {
987 // Put back the original tokens.
988 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
990 auto Buffer = llvm::make_unique<Token[]>(Toks.size());
991 std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
992 Buffer[Toks.size() - 1] = Self.Tok;
993 Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), 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, /*VersusTemplateArgument=*/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;