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 "RAIIObjectsForParser.h"
16 #include "clang/AST/DeclTemplate.h"
17 #include "clang/Parse/ParseDiagnostic.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(AccessSpecifier AS,
26 AttributeList *AccessAttrs,
28 const ParsedTemplateInfo &TemplateInfo,
29 const VirtSpecifiers& VS,
30 SourceLocation PureSpecLoc) {
31 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
32 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
33 "Current token not a '{', ':', '=', or 'try'!");
35 MultiTemplateParamsArg TemplateParams(
36 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
38 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
41 if (D.getDeclSpec().isFriendSpecified())
42 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
45 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
46 TemplateParams, nullptr,
49 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
50 if (PureSpecLoc.isValid())
51 Actions.ActOnPureSpecifier(FnD, PureSpecLoc);
56 HandleMemberFunctionDeclDelays(D, FnD);
60 if (TryConsumeToken(tok::equal)) {
68 SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
69 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
70 Diag(KWLoc, getLangOpts().CPlusPlus11
71 ? diag::warn_cxx98_compat_defaulted_deleted_function
72 : diag::ext_defaulted_deleted_function)
74 Actions.SetDeclDeleted(FnD, KWLoc);
76 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
77 DeclAsFunction->setRangeEnd(KWEndLoc);
79 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
80 Diag(KWLoc, getLangOpts().CPlusPlus11
81 ? diag::warn_cxx98_compat_defaulted_deleted_function
82 : diag::ext_defaulted_deleted_function)
84 Actions.SetDeclDefaulted(FnD, KWLoc);
85 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
86 DeclAsFunction->setRangeEnd(KWEndLoc);
89 llvm_unreachable("function definition after = not 'delete' or 'default'");
92 if (Tok.is(tok::comma)) {
93 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
96 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
97 Delete ? "delete" : "default")) {
104 if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(FnD)) &&
105 trySkippingFunctionBody()) {
106 Actions.ActOnSkippedFunctionBody(FnD);
110 // In delayed template parsing mode, if we are within a class template
111 // or if we are about to parse function member template then consume
112 // the tokens and store them for parsing at the end of the translation unit.
113 if (getLangOpts().DelayedTemplateParsing &&
114 D.getFunctionDefinitionKind() == FDK_Definition &&
115 !D.getDeclSpec().isConstexprSpecified() &&
116 !(FnD && FnD->getAsFunction() &&
117 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
118 ((Actions.CurContext->isDependentContext() ||
119 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
120 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
121 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
124 LexTemplateFunctionForLateParsing(Toks);
127 FunctionDecl *FD = FnD->getAsFunction();
128 Actions.CheckForFunctionRedefinition(FD);
129 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
135 // Consume the tokens and store them for later parsing.
137 LexedMethod* LM = new LexedMethod(this, FnD);
138 getCurrentClass().LateParsedDeclarations.push_back(LM);
139 LM->TemplateScope = getCurScope()->isTemplateParamScope();
140 CachedTokens &Toks = LM->Toks;
142 tok::TokenKind kind = Tok.getKind();
143 // Consume everything up to (and including) the left brace of the
145 if (ConsumeAndStoreFunctionPrologue(Toks)) {
146 // We didn't find the left-brace we expected after the
147 // constructor initializer; we already printed an error, and it's likely
148 // impossible to recover, so don't try to parse this method later.
149 // Skip over the rest of the decl and back to somewhere that looks
152 delete getCurrentClass().LateParsedDeclarations.back();
153 getCurrentClass().LateParsedDeclarations.pop_back();
156 // Consume everything up to (and including) the matching right brace.
157 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
160 // If we're in a function-try-block, we need to store all the catch blocks.
161 if (kind == tok::kw_try) {
162 while (Tok.is(tok::kw_catch)) {
163 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
164 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
169 // If this is a friend function, mark that it's late-parsed so that
170 // it's still known to be a definition even before we attach the
171 // parsed body. Sema needs to treat friend function definitions
172 // differently during template instantiation, and it's possible for
173 // the containing class to be instantiated before all its member
174 // function definitions are parsed.
176 // If you remove this, you can remove the code that clears the flag
177 // after parsing the member.
178 if (D.getDeclSpec().isFriendSpecified()) {
179 FunctionDecl *FD = FnD->getAsFunction();
180 Actions.CheckForFunctionRedefinition(FD);
181 FD->setLateTemplateParsed(true);
184 // If semantic analysis could not build a function declaration,
185 // just throw away the late-parsed declaration.
186 delete getCurrentClass().LateParsedDeclarations.back();
187 getCurrentClass().LateParsedDeclarations.pop_back();
193 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
194 /// specified Declarator is a well formed C++ non-static data member
195 /// declaration. Now lex its initializer and store its tokens for parsing
196 /// after the class is complete.
197 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
198 assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
199 "Current token not a '{' or '='!");
201 LateParsedMemberInitializer *MI =
202 new LateParsedMemberInitializer(this, VarD);
203 getCurrentClass().LateParsedDeclarations.push_back(MI);
204 CachedTokens &Toks = MI->Toks;
206 tok::TokenKind kind = Tok.getKind();
207 if (kind == tok::equal) {
212 if (kind == tok::l_brace) {
213 // Begin by storing the '{' token.
217 // Consume everything up to (and including) the matching right brace.
218 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
220 // Consume everything up to (but excluding) the comma or semicolon.
221 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
224 // Store an artificial EOF token to ensure that we don't run off the end of
225 // the initializer when we come to parse it.
228 Eof.setKind(tok::eof);
229 Eof.setLocation(Tok.getLocation());
230 Eof.setEofData(VarD);
234 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
235 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
236 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
237 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
239 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
240 : Self(P), Class(C) {}
242 Parser::LateParsedClass::~LateParsedClass() {
243 Self->DeallocateParsedClasses(Class);
246 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
247 Self->ParseLexedMethodDeclarations(*Class);
250 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
251 Self->ParseLexedMemberInitializers(*Class);
254 void Parser::LateParsedClass::ParseLexedMethodDefs() {
255 Self->ParseLexedMethodDefs(*Class);
258 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
259 Self->ParseLexedMethodDeclaration(*this);
262 void Parser::LexedMethod::ParseLexedMethodDefs() {
263 Self->ParseLexedMethodDef(*this);
266 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
267 Self->ParseLexedMemberInitializer(*this);
270 /// ParseLexedMethodDeclarations - We finished parsing the member
271 /// specification of a top (non-nested) C++ class. Now go over the
272 /// stack of method declarations with some parts for which parsing was
273 /// delayed (such as default arguments) and parse them.
274 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
275 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
276 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
278 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
279 if (HasTemplateScope) {
280 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
281 ++CurTemplateDepthTracker;
284 // The current scope is still active if we're the top-level class.
285 // Otherwise we'll need to push and enter a new scope.
286 bool HasClassScope = !Class.TopLevelClass;
287 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
290 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
291 Class.TagOrTemplate);
293 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
294 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
298 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
299 Class.TagOrTemplate);
302 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
303 // If this is a member template, introduce the template parameter scope.
304 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
305 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
306 if (LM.TemplateScope) {
307 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
308 ++CurTemplateDepthTracker;
310 // Start the delayed C++ method declaration
311 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
313 // Introduce the parameters into scope and parse their default
315 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
316 Scope::FunctionDeclarationScope | Scope::DeclScope);
317 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
318 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
319 // Introduce the parameter into scope.
320 bool HasUnparsed = Param->hasUnparsedDefaultArg();
321 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
322 std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
324 // Mark the end of the default argument so that we know when to stop when
325 // we parse it later on.
326 Token LastDefaultArgToken = Toks->back();
328 DefArgEnd.startToken();
329 DefArgEnd.setKind(tok::eof);
330 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
331 DefArgEnd.setEofData(Param);
332 Toks->push_back(DefArgEnd);
334 // Parse the default argument from its saved token stream.
335 Toks->push_back(Tok); // So that the current token doesn't get lost
336 PP.EnterTokenStream(*Toks, true);
338 // Consume the previously-pushed token.
342 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
343 SourceLocation EqualLoc = ConsumeToken();
345 // The argument isn't actually potentially evaluated unless it is
347 EnterExpressionEvaluationContext Eval(Actions,
348 Sema::PotentiallyEvaluatedIfUsed,
351 ExprResult DefArgResult;
352 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
353 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
354 DefArgResult = ParseBraceInitializer();
356 DefArgResult = ParseAssignmentExpression();
357 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
358 if (DefArgResult.isInvalid()) {
359 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
361 if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
362 // The last two tokens are the terminator and the saved value of
363 // Tok; the last token in the default argument is the one before
365 assert(Toks->size() >= 3 && "expected a token in default arg");
366 Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
367 << SourceRange(Tok.getLocation(),
368 (*Toks)[Toks->size() - 3].getLocation());
370 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
374 // There could be leftover tokens (e.g. because of an error).
375 // Skip through until we reach the 'end of default argument' token.
376 while (Tok.isNot(tok::eof))
379 if (Tok.is(tok::eof) && Tok.getEofData() == Param)
381 } else if (HasUnparsed) {
382 assert(Param->hasInheritedDefaultArg());
383 FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
384 ParmVarDecl *OldParam = Old->getParamDecl(I);
385 assert (!OldParam->hasUnparsedDefaultArg());
386 if (OldParam->hasUninstantiatedDefaultArg())
387 Param->setUninstantiatedDefaultArg(
388 OldParam->getUninstantiatedDefaultArg());
390 Param->setDefaultArg(OldParam->getInit());
394 // Parse a delayed exception-specification, if there is one.
395 if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
396 // Add the 'stop' token.
397 Token LastExceptionSpecToken = Toks->back();
398 Token ExceptionSpecEnd;
399 ExceptionSpecEnd.startToken();
400 ExceptionSpecEnd.setKind(tok::eof);
401 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
402 ExceptionSpecEnd.setEofData(LM.Method);
403 Toks->push_back(ExceptionSpecEnd);
405 // Parse the default argument from its saved token stream.
406 Toks->push_back(Tok); // So that the current token doesn't get lost
407 PP.EnterTokenStream(*Toks, true);
409 // Consume the previously-pushed token.
412 // C++11 [expr.prim.general]p3:
413 // If a declaration declares a member function or member function
414 // template of a class X, the expression this is a prvalue of type
415 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
416 // and the end of the function-definition, member-declarator, or
418 CXXMethodDecl *Method;
419 if (FunctionTemplateDecl *FunTmpl
420 = dyn_cast<FunctionTemplateDecl>(LM.Method))
421 Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
423 Method = cast<CXXMethodDecl>(LM.Method);
425 Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
426 Method->getTypeQualifiers(),
427 getLangOpts().CPlusPlus11);
429 // Parse the exception-specification.
430 SourceRange SpecificationRange;
431 SmallVector<ParsedType, 4> DynamicExceptions;
432 SmallVector<SourceRange, 4> DynamicExceptionRanges;
433 ExprResult NoexceptExpr;
434 CachedTokens *ExceptionSpecTokens;
436 ExceptionSpecificationType EST
437 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
439 DynamicExceptionRanges, NoexceptExpr,
440 ExceptionSpecTokens);
442 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
443 Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
445 // Attach the exception-specification to the method.
446 Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
449 DynamicExceptionRanges,
450 NoexceptExpr.isUsable()?
451 NoexceptExpr.get() : nullptr);
453 // There could be leftover tokens (e.g. because of an error).
454 // Skip through until we reach the original token position.
455 while (Tok.isNot(tok::eof))
458 // Clean up the remaining EOF token.
459 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
463 LM.ExceptionSpecTokens = nullptr;
466 PrototypeScope.Exit();
468 // Finish the delayed C++ method declaration.
469 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
472 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
473 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
474 /// collected during its parsing and parse them all.
475 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
476 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
477 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
478 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
479 if (HasTemplateScope) {
480 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
481 ++CurTemplateDepthTracker;
483 bool HasClassScope = !Class.TopLevelClass;
484 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
487 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
488 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
492 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
493 // If this is a member template, introduce the template parameter scope.
494 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
495 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
496 if (LM.TemplateScope) {
497 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
498 ++CurTemplateDepthTracker;
501 assert(!LM.Toks.empty() && "Empty body!");
502 Token LastBodyToken = LM.Toks.back();
504 BodyEnd.startToken();
505 BodyEnd.setKind(tok::eof);
506 BodyEnd.setLocation(LastBodyToken.getEndLoc());
507 BodyEnd.setEofData(LM.D);
508 LM.Toks.push_back(BodyEnd);
509 // Append the current token at the end of the new token stream so that it
511 LM.Toks.push_back(Tok);
512 PP.EnterTokenStream(LM.Toks, true);
514 // Consume the previously pushed token.
515 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
516 assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
517 && "Inline method not starting with '{', ':' or 'try'");
519 // Parse the method body. Function body parsing code is similar enough
520 // to be re-used for method bodies as well.
521 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
522 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
524 if (Tok.is(tok::kw_try)) {
525 ParseFunctionTryBlock(LM.D, FnScope);
527 while (Tok.isNot(tok::eof))
530 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
534 if (Tok.is(tok::colon)) {
535 ParseConstructorInitializer(LM.D);
538 if (!Tok.is(tok::l_brace)) {
540 Actions.ActOnFinishFunctionBody(LM.D, nullptr);
542 while (Tok.isNot(tok::eof))
545 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
550 Actions.ActOnDefaultCtorInitializers(LM.D);
552 assert((Actions.getDiagnostics().hasErrorOccurred() ||
553 !isa<FunctionTemplateDecl>(LM.D) ||
554 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
555 < TemplateParameterDepth) &&
556 "TemplateParameterDepth should be greater than the depth of "
557 "current template being instantiated!");
559 ParseFunctionStatementBody(LM.D, FnScope);
561 // Clear the late-template-parsed bit if we set it before.
563 LM.D->getAsFunction()->setLateTemplateParsed(false);
565 while (Tok.isNot(tok::eof))
568 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
571 if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
572 if (isa<CXXMethodDecl>(FD) ||
573 FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend))
574 Actions.ActOnFinishInlineFunctionDef(FD);
577 /// ParseLexedMemberInitializers - We finished parsing the member specification
578 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
579 /// initializers that were collected during its parsing and parse them all.
580 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
581 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
582 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
584 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
585 if (HasTemplateScope) {
586 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
587 ++CurTemplateDepthTracker;
589 // Set or update the scope flags.
590 bool AlreadyHasClassScope = Class.TopLevelClass;
591 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
592 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
593 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
595 if (!AlreadyHasClassScope)
596 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
597 Class.TagOrTemplate);
599 if (!Class.LateParsedDeclarations.empty()) {
600 // C++11 [expr.prim.general]p4:
601 // Otherwise, if a member-declarator declares a non-static data member
602 // (9.2) of a class X, the expression this is a prvalue of type "pointer
603 // to X" within the optional brace-or-equal-initializer. It shall not
604 // appear elsewhere in the member-declarator.
605 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
606 /*TypeQuals=*/(unsigned)0);
608 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
609 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
613 if (!AlreadyHasClassScope)
614 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
615 Class.TagOrTemplate);
617 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
620 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
621 if (!MI.Field || MI.Field->isInvalidDecl())
624 // Append the current token at the end of the new token stream so that it
626 MI.Toks.push_back(Tok);
627 PP.EnterTokenStream(MI.Toks, true);
629 // Consume the previously pushed token.
630 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
632 SourceLocation EqualLoc;
634 Actions.ActOnStartCXXInClassMemberInitializer();
636 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
639 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
642 // The next token should be our artificial terminating EOF token.
643 if (Tok.isNot(tok::eof)) {
644 if (!Init.isInvalid()) {
645 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
646 if (!EndLoc.isValid())
647 EndLoc = Tok.getLocation();
648 // No fixit; we can't recover as if there were a semicolon here.
649 Diag(EndLoc, diag::err_expected_semi_decl_list);
652 // Consume tokens until we hit the artificial EOF.
653 while (Tok.isNot(tok::eof))
656 // Make sure this is *our* artificial EOF token.
657 if (Tok.getEofData() == MI.Field)
661 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
662 /// container until the token 'T' is reached (which gets
663 /// consumed/stored too, if ConsumeFinalToken).
664 /// If StopAtSemi is true, then we will stop early at a ';' character.
665 /// Returns true if token 'T1' or 'T2' was found.
666 /// NOTE: This is a specialized version of Parser::SkipUntil.
667 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
669 bool StopAtSemi, bool ConsumeFinalToken) {
670 // We always want this function to consume at least one token if the first
671 // token isn't T and if not at EOF.
672 bool isFirstTokenConsumed = true;
674 // If we found one of the tokens, stop and return true.
675 if (Tok.is(T1) || Tok.is(T2)) {
676 if (ConsumeFinalToken) {
683 switch (Tok.getKind()) {
685 case tok::annot_module_begin:
686 case tok::annot_module_end:
687 case tok::annot_module_include:
688 // Ran out of tokens.
692 // Recursively consume properly-nested parens.
695 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
698 // Recursively consume properly-nested square brackets.
701 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
704 // Recursively consume properly-nested braces.
707 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
710 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
711 // Since the user wasn't looking for this token (if they were, it would
712 // already be handled), this isn't balanced. If there is a LHS token at a
713 // higher level, we will assume that this matches the unbalanced token
714 // and return it. Otherwise, this is a spurious RHS token, which we skip.
716 if (ParenCount && !isFirstTokenConsumed)
717 return false; // Matches something.
722 if (BracketCount && !isFirstTokenConsumed)
723 return false; // Matches something.
728 if (BraceCount && !isFirstTokenConsumed)
729 return false; // Matches something.
734 case tok::code_completion:
736 ConsumeCodeCompletionToken();
739 case tok::string_literal:
740 case tok::wide_string_literal:
741 case tok::utf8_string_literal:
742 case tok::utf16_string_literal:
743 case tok::utf32_string_literal:
745 ConsumeStringToken();
752 // consume this token.
757 isFirstTokenConsumed = false;
761 /// \brief Consume tokens and store them in the passed token container until
762 /// we've passed the try keyword and constructor initializers and have consumed
763 /// the opening brace of the function body. The opening brace will be consumed
764 /// if and only if there was no error.
766 /// \return True on error.
767 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
768 if (Tok.is(tok::kw_try)) {
773 if (Tok.isNot(tok::colon)) {
774 // Easy case, just a function body.
776 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
777 // brace: an opening one is the function body, while a closing one probably
778 // means we've reached the end of the class.
779 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
781 /*ConsumeFinalToken=*/false);
782 if (Tok.isNot(tok::l_brace))
783 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
793 // We can't reliably skip over a mem-initializer-id, because it could be
794 // a template-id involving not-yet-declared names. Given:
796 // S ( ) : a < b < c > ( e )
798 // 'e' might be an initializer or part of a template argument, depending
799 // on whether 'b' is a template.
801 // Track whether we might be inside a template argument. We can give
802 // significantly better diagnostics if we know that we're not.
803 bool MightBeTemplateArgument = false;
806 // Skip over the mem-initializer-id, if possible.
807 if (Tok.is(tok::kw_decltype)) {
809 SourceLocation OpenLoc = ConsumeToken();
810 if (Tok.isNot(tok::l_paren))
811 return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
815 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
816 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
817 Diag(OpenLoc, diag::note_matching) << tok::l_paren;
822 // Walk over a component of a nested-name-specifier.
823 if (Tok.is(tok::coloncolon)) {
827 if (Tok.is(tok::kw_template)) {
833 if (Tok.is(tok::identifier)) {
839 } while (Tok.is(tok::coloncolon));
841 if (Tok.is(tok::code_completion)) {
843 ConsumeCodeCompletionToken();
844 if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype)) {
845 // Could be the start of another member initializer (the ',' has not
851 if (Tok.is(tok::comma)) {
852 // The initialization is missing, we'll diagnose it later.
857 if (Tok.is(tok::less))
858 MightBeTemplateArgument = true;
860 if (MightBeTemplateArgument) {
861 // We may be inside a template argument list. Grab up to the start of the
862 // next parenthesized initializer or braced-init-list. This *might* be the
863 // initializer, or it might be a subexpression in the template argument
865 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
866 // if all angles are closed.
867 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
869 /*ConsumeFinalToken=*/false)) {
870 // We're not just missing the initializer, we're also missing the
872 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
874 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
875 // We found something weird in a mem-initializer-id.
876 if (getLangOpts().CPlusPlus11)
877 return Diag(Tok.getLocation(), diag::err_expected_either)
878 << tok::l_paren << tok::l_brace;
880 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
883 tok::TokenKind kind = Tok.getKind();
885 bool IsLParen = (kind == tok::l_paren);
886 SourceLocation OpenLoc = Tok.getLocation();
891 assert(kind == tok::l_brace && "Must be left paren or brace here.");
893 // In C++03, this has to be the start of the function body, which
894 // means the initializer is malformed; we'll diagnose it later.
895 if (!getLangOpts().CPlusPlus11)
898 const Token &PreviousToken = Toks[Toks.size() - 2];
899 if (!MightBeTemplateArgument &&
900 !PreviousToken.isOneOf(tok::identifier, tok::greater,
901 tok::greatergreater)) {
902 // If the opening brace is not preceded by one of these tokens, we are
903 // missing the mem-initializer-id. In order to recover better, we need
904 // to use heuristics to determine if this '{' is most likely the
905 // begining of a brace-init-list or the function body.
906 // Check the token after the corresponding '}'.
907 TentativeParsingAction PA(*this);
908 if (SkipUntil(tok::r_brace) &&
909 !Tok.isOneOf(tok::comma, tok::ellipsis, tok::l_brace)) {
910 // Consider there was a malformed initializer and this is the start
911 // of the function body. We'll diagnose it later.
919 // Grab the initializer (or the subexpression of the template argument).
920 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
921 // if we might be inside the braces of a lambda-expression.
922 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
923 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
924 Diag(Tok, diag::err_expected) << CloseKind;
925 Diag(OpenLoc, diag::note_matching) << kind;
929 // Grab pack ellipsis, if present.
930 if (Tok.is(tok::ellipsis)) {
935 // If we know we just consumed a mem-initializer, we must have ',' or '{'
937 if (Tok.is(tok::comma)) {
940 } else if (Tok.is(tok::l_brace)) {
941 // This is the function body if the ')' or '}' is immediately followed by
942 // a '{'. That cannot happen within a template argument, apart from the
943 // case where a template argument contains a compound literal:
945 // S ( ) : a < b < c > ( d ) { }
946 // // End of declaration, or still inside the template argument?
948 // ... and the case where the template argument contains a lambda:
950 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
953 // FIXME: Disambiguate these cases. Note that the latter case is probably
954 // going to be made ill-formed by core issue 1607.
958 } else if (!MightBeTemplateArgument) {
959 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
965 /// \brief Consume and store tokens from the '?' to the ':' in a conditional
967 bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
969 assert(Tok.is(tok::question));
973 while (Tok.isNot(tok::colon)) {
974 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
976 /*ConsumeFinalToken=*/false))
979 // If we found a nested conditional, consume it.
980 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
990 /// \brief A tentative parsing action that can also revert token annotations.
991 class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
993 explicit UnannotatedTentativeParsingAction(Parser &Self,
994 tok::TokenKind EndKind)
995 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
996 // Stash away the old token stream, so we can restore it once the
997 // tentative parse is complete.
998 TentativeParsingAction Inner(Self);
999 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
1003 void RevertAnnotations() {
1006 // Put back the original tokens.
1007 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
1009 auto Buffer = llvm::make_unique<Token[]>(Toks.size());
1010 std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
1011 Buffer[Toks.size() - 1] = Self.Tok;
1012 Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true);
1014 Self.Tok = Toks.front();
1021 tok::TokenKind EndKind;
1024 /// ConsumeAndStoreInitializer - Consume and store the token at the passed token
1025 /// container until the end of the current initializer expression (either a
1026 /// default argument or an in-class initializer for a non-static data member).
1028 /// Returns \c true if we reached the end of something initializer-shaped,
1029 /// \c false if we bailed out.
1030 bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
1031 CachedInitKind CIK) {
1032 // We always want this function to consume at least one token if not at EOF.
1033 bool IsFirstToken = true;
1035 // Number of possible unclosed <s we've seen so far. These might be templates,
1036 // and might not, but if there were none of them (or we know for sure that
1037 // we're within a template), we can avoid a tentative parse.
1038 unsigned AngleCount = 0;
1039 unsigned KnownTemplateCount = 0;
1042 switch (Tok.getKind()) {
1044 // If we might be in a template, perform a tentative parse to check.
1046 // Not a template argument: this is the end of the initializer.
1048 if (KnownTemplateCount)
1051 // We hit a comma inside angle brackets. This is the hard case. The
1052 // rule we follow is:
1053 // * For a default argument, if the tokens after the comma form a
1054 // syntactically-valid parameter-declaration-clause, in which each
1055 // parameter has an initializer, then this comma ends the default
1057 // * For a default initializer, if the tokens after the comma form a
1058 // syntactically-valid init-declarator-list, then this comma ends
1059 // the default initializer.
1061 UnannotatedTentativeParsingAction PA(*this,
1062 CIK == CIK_DefaultInitializer
1063 ? tok::semi : tok::r_paren);
1064 Sema::TentativeAnalysisScope Scope(Actions);
1066 TPResult Result = TPResult::Error;
1069 case CIK_DefaultInitializer:
1070 Result = TryParseInitDeclaratorList();
1071 // If we parsed a complete, ambiguous init-declarator-list, this
1072 // is only syntactically-valid if it's followed by a semicolon.
1073 if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi))
1074 Result = TPResult::False;
1077 case CIK_DefaultArgument:
1078 bool InvalidAsDeclaration = false;
1079 Result = TryParseParameterDeclarationClause(
1080 &InvalidAsDeclaration, /*VersusTemplateArgument=*/true);
1081 // If this is an expression or a declaration with a missing
1082 // 'typename', assume it's not a declaration.
1083 if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1084 Result = TPResult::False;
1088 // If what follows could be a declaration, it is a declaration.
1089 if (Result != TPResult::False && Result != TPResult::Error) {
1094 // In the uncommon case that we decide the following tokens are part
1095 // of a template argument, revert any annotations we've performed in
1096 // those tokens. We're not going to look them up until we've parsed
1097 // the rest of the class, and that might add more declarations.
1098 PA.RevertAnnotations();
1101 // Keep going. We know we're inside a template argument list now.
1102 ++KnownTemplateCount;
1106 case tok::annot_module_begin:
1107 case tok::annot_module_end:
1108 case tok::annot_module_include:
1109 // Ran out of tokens.
1113 // FIXME: A '<' can only start a template-id if it's preceded by an
1114 // identifier, an operator-function-id, or a literal-operator-id.
1119 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1120 // that is *never* the end of the initializer. Skip to the ':'.
1121 if (!ConsumeAndStoreConditional(Toks))
1125 case tok::greatergreatergreater:
1126 if (!getLangOpts().CPlusPlus11)
1128 if (AngleCount) --AngleCount;
1129 if (KnownTemplateCount) --KnownTemplateCount;
1131 case tok::greatergreater:
1132 if (!getLangOpts().CPlusPlus11)
1134 if (AngleCount) --AngleCount;
1135 if (KnownTemplateCount) --KnownTemplateCount;
1138 if (AngleCount) --AngleCount;
1139 if (KnownTemplateCount) --KnownTemplateCount;
1142 case tok::kw_template:
1143 // 'template' identifier '<' is known to start a template argument list,
1144 // and can be used to disambiguate the parse.
1145 // FIXME: Support all forms of 'template' unqualified-id '<'.
1146 Toks.push_back(Tok);
1148 if (Tok.is(tok::identifier)) {
1149 Toks.push_back(Tok);
1151 if (Tok.is(tok::less)) {
1153 ++KnownTemplateCount;
1154 Toks.push_back(Tok);
1160 case tok::kw_operator:
1161 // If 'operator' precedes other punctuation, that punctuation loses
1162 // its special behavior.
1163 Toks.push_back(Tok);
1165 switch (Tok.getKind()) {
1167 case tok::greatergreatergreater:
1168 case tok::greatergreater:
1171 Toks.push_back(Tok);
1180 // Recursively consume properly-nested parens.
1181 Toks.push_back(Tok);
1183 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1186 // Recursively consume properly-nested square brackets.
1187 Toks.push_back(Tok);
1189 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
1192 // Recursively consume properly-nested braces.
1193 Toks.push_back(Tok);
1195 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1198 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1199 // Since the user wasn't looking for this token (if they were, it would
1200 // already be handled), this isn't balanced. If there is a LHS token at a
1201 // higher level, we will assume that this matches the unbalanced token
1202 // and return it. Otherwise, this is a spurious RHS token, which we
1203 // consume and pass on to downstream code to diagnose.
1205 if (CIK == CIK_DefaultArgument)
1206 return true; // End of the default argument.
1207 if (ParenCount && !IsFirstToken)
1209 Toks.push_back(Tok);
1213 if (BracketCount && !IsFirstToken)
1215 Toks.push_back(Tok);
1219 if (BraceCount && !IsFirstToken)
1221 Toks.push_back(Tok);
1225 case tok::code_completion:
1226 Toks.push_back(Tok);
1227 ConsumeCodeCompletionToken();
1230 case tok::string_literal:
1231 case tok::wide_string_literal:
1232 case tok::utf8_string_literal:
1233 case tok::utf16_string_literal:
1234 case tok::utf32_string_literal:
1235 Toks.push_back(Tok);
1236 ConsumeStringToken();
1239 if (CIK == CIK_DefaultInitializer)
1240 return true; // End of the default initializer.
1244 Toks.push_back(Tok);
1248 IsFirstToken = false;