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,
31 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
32 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
33 Tok.is(tok::equal)) &&
34 "Current token not a '{', ':', '=', or 'try'!");
36 MultiTemplateParamsArg TemplateParams(
37 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
39 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
42 if (D.getDeclSpec().isFriendSpecified())
43 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
46 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
47 TemplateParams, nullptr,
50 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
51 bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType();
53 Actions.AddInitializerToDecl(FnD, Init.get(), false,
54 TypeSpecContainsAuto);
56 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
60 HandleMemberFunctionDeclDelays(D, FnD);
64 if (TryConsumeToken(tok::equal)) {
72 SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
73 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
74 Diag(KWLoc, getLangOpts().CPlusPlus11
75 ? diag::warn_cxx98_compat_deleted_function
76 : diag::ext_deleted_function);
77 Actions.SetDeclDeleted(FnD, KWLoc);
79 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
80 DeclAsFunction->setRangeEnd(KWEndLoc);
82 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
83 Diag(KWLoc, getLangOpts().CPlusPlus11
84 ? diag::warn_cxx98_compat_defaulted_function
85 : diag::ext_defaulted_function);
86 Actions.SetDeclDefaulted(FnD, KWLoc);
87 if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
88 DeclAsFunction->setRangeEnd(KWEndLoc);
91 llvm_unreachable("function definition after = not 'delete' or 'default'");
94 if (Tok.is(tok::comma)) {
95 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
98 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
99 Delete ? "delete" : "default")) {
100 SkipUntil(tok::semi);
106 // In delayed template parsing mode, if we are within a class template
107 // or if we are about to parse function member template then consume
108 // the tokens and store them for parsing at the end of the translation unit.
109 if (getLangOpts().DelayedTemplateParsing &&
110 D.getFunctionDefinitionKind() == FDK_Definition &&
111 !D.getDeclSpec().isConstexprSpecified() &&
112 !(FnD && FnD->getAsFunction() &&
113 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
114 ((Actions.CurContext->isDependentContext() ||
115 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
116 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
117 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
120 LexTemplateFunctionForLateParsing(Toks);
123 FunctionDecl *FD = FnD->getAsFunction();
124 Actions.CheckForFunctionRedefinition(FD);
125 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
131 // Consume the tokens and store them for later parsing.
133 LexedMethod* LM = new LexedMethod(this, FnD);
134 getCurrentClass().LateParsedDeclarations.push_back(LM);
135 LM->TemplateScope = getCurScope()->isTemplateParamScope();
136 CachedTokens &Toks = LM->Toks;
138 tok::TokenKind kind = Tok.getKind();
139 // Consume everything up to (and including) the left brace of the
141 if (ConsumeAndStoreFunctionPrologue(Toks)) {
142 // We didn't find the left-brace we expected after the
143 // constructor initializer; we already printed an error, and it's likely
144 // impossible to recover, so don't try to parse this method later.
145 // Skip over the rest of the decl and back to somewhere that looks
148 delete getCurrentClass().LateParsedDeclarations.back();
149 getCurrentClass().LateParsedDeclarations.pop_back();
152 // Consume everything up to (and including) the matching right brace.
153 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
156 // If we're in a function-try-block, we need to store all the catch blocks.
157 if (kind == tok::kw_try) {
158 while (Tok.is(tok::kw_catch)) {
159 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
160 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
165 // If this is a friend function, mark that it's late-parsed so that
166 // it's still known to be a definition even before we attach the
167 // parsed body. Sema needs to treat friend function definitions
168 // differently during template instantiation, and it's possible for
169 // the containing class to be instantiated before all its member
170 // function definitions are parsed.
172 // If you remove this, you can remove the code that clears the flag
173 // after parsing the member.
174 if (D.getDeclSpec().isFriendSpecified()) {
175 FunctionDecl *FD = FnD->getAsFunction();
176 Actions.CheckForFunctionRedefinition(FD);
177 FD->setLateTemplateParsed(true);
180 // If semantic analysis could not build a function declaration,
181 // just throw away the late-parsed declaration.
182 delete getCurrentClass().LateParsedDeclarations.back();
183 getCurrentClass().LateParsedDeclarations.pop_back();
189 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
190 /// specified Declarator is a well formed C++ non-static data member
191 /// declaration. Now lex its initializer and store its tokens for parsing
192 /// after the class is complete.
193 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
194 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
195 "Current token not a '{' or '='!");
197 LateParsedMemberInitializer *MI =
198 new LateParsedMemberInitializer(this, VarD);
199 getCurrentClass().LateParsedDeclarations.push_back(MI);
200 CachedTokens &Toks = MI->Toks;
202 tok::TokenKind kind = Tok.getKind();
203 if (kind == tok::equal) {
208 if (kind == tok::l_brace) {
209 // Begin by storing the '{' token.
213 // Consume everything up to (and including) the matching right brace.
214 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
216 // Consume everything up to (but excluding) the comma or semicolon.
217 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
220 // Store an artificial EOF token to ensure that we don't run off the end of
221 // the initializer when we come to parse it.
224 Eof.setKind(tok::eof);
225 Eof.setLocation(Tok.getLocation());
226 Eof.setEofData(VarD);
230 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
231 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
232 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
233 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
235 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
236 : Self(P), Class(C) {}
238 Parser::LateParsedClass::~LateParsedClass() {
239 Self->DeallocateParsedClasses(Class);
242 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
243 Self->ParseLexedMethodDeclarations(*Class);
246 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
247 Self->ParseLexedMemberInitializers(*Class);
250 void Parser::LateParsedClass::ParseLexedMethodDefs() {
251 Self->ParseLexedMethodDefs(*Class);
254 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
255 Self->ParseLexedMethodDeclaration(*this);
258 void Parser::LexedMethod::ParseLexedMethodDefs() {
259 Self->ParseLexedMethodDef(*this);
262 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
263 Self->ParseLexedMemberInitializer(*this);
266 /// ParseLexedMethodDeclarations - We finished parsing the member
267 /// specification of a top (non-nested) C++ class. Now go over the
268 /// stack of method declarations with some parts for which parsing was
269 /// delayed (such as default arguments) and parse them.
270 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
271 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
272 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
274 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
275 if (HasTemplateScope) {
276 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
277 ++CurTemplateDepthTracker;
280 // The current scope is still active if we're the top-level class.
281 // Otherwise we'll need to push and enter a new scope.
282 bool HasClassScope = !Class.TopLevelClass;
283 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
286 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
287 Class.TagOrTemplate);
289 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
290 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
294 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
295 Class.TagOrTemplate);
298 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
299 // If this is a member template, introduce the template parameter scope.
300 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
301 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
302 if (LM.TemplateScope) {
303 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
304 ++CurTemplateDepthTracker;
306 // Start the delayed C++ method declaration
307 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
309 // Introduce the parameters into scope and parse their default
311 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
312 Scope::FunctionDeclarationScope | Scope::DeclScope);
313 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
314 auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
315 // Introduce the parameter into scope.
316 bool HasUnparsed = Param->hasUnparsedDefaultArg();
317 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
318 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
319 // Mark the end of the default argument so that we know when to stop when
320 // we parse it later on.
321 Token LastDefaultArgToken = Toks->back();
323 DefArgEnd.startToken();
324 DefArgEnd.setKind(tok::eof);
325 DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
326 DefArgEnd.setEofData(Param);
327 Toks->push_back(DefArgEnd);
329 // Parse the default argument from its saved token stream.
330 Toks->push_back(Tok); // So that the current token doesn't get lost
331 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
333 // Consume the previously-pushed token.
337 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
338 SourceLocation EqualLoc = ConsumeToken();
340 // The argument isn't actually potentially evaluated unless it is
342 EnterExpressionEvaluationContext Eval(Actions,
343 Sema::PotentiallyEvaluatedIfUsed,
346 ExprResult DefArgResult;
347 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
348 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
349 DefArgResult = ParseBraceInitializer();
351 DefArgResult = ParseAssignmentExpression();
352 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
353 if (DefArgResult.isInvalid()) {
354 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
356 if (Tok.isNot(tok::eof) || Tok.getEofData() != Param) {
357 // The last two tokens are the terminator and the saved value of
358 // Tok; the last token in the default argument is the one before
360 assert(Toks->size() >= 3 && "expected a token in default arg");
361 Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
362 << SourceRange(Tok.getLocation(),
363 (*Toks)[Toks->size() - 3].getLocation());
365 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
369 // There could be leftover tokens (e.g. because of an error).
370 // Skip through until we reach the 'end of default argument' token.
371 while (Tok.isNot(tok::eof))
374 if (Tok.is(tok::eof) && Tok.getEofData() == Param)
378 LM.DefaultArgs[I].Toks = nullptr;
379 } else if (HasUnparsed) {
380 assert(Param->hasInheritedDefaultArg());
381 FunctionDecl *Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
382 ParmVarDecl *OldParam = Old->getParamDecl(I);
383 assert (!OldParam->hasUnparsedDefaultArg());
384 if (OldParam->hasUninstantiatedDefaultArg())
385 Param->setUninstantiatedDefaultArg(
386 Param->getUninstantiatedDefaultArg());
388 Param->setDefaultArg(OldParam->getInit());
392 // Parse a delayed exception-specification, if there is one.
393 if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
394 // Add the 'stop' token.
395 Token LastExceptionSpecToken = Toks->back();
396 Token ExceptionSpecEnd;
397 ExceptionSpecEnd.startToken();
398 ExceptionSpecEnd.setKind(tok::eof);
399 ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
400 ExceptionSpecEnd.setEofData(LM.Method);
401 Toks->push_back(ExceptionSpecEnd);
403 // Parse the default argument from its saved token stream.
404 Toks->push_back(Tok); // So that the current token doesn't get lost
405 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
407 // Consume the previously-pushed token.
410 // C++11 [expr.prim.general]p3:
411 // If a declaration declares a member function or member function
412 // template of a class X, the expression this is a prvalue of type
413 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
414 // and the end of the function-definition, member-declarator, or
416 CXXMethodDecl *Method;
417 if (FunctionTemplateDecl *FunTmpl
418 = dyn_cast<FunctionTemplateDecl>(LM.Method))
419 Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
421 Method = cast<CXXMethodDecl>(LM.Method);
423 Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
424 Method->getTypeQualifiers(),
425 getLangOpts().CPlusPlus11);
427 // Parse the exception-specification.
428 SourceRange SpecificationRange;
429 SmallVector<ParsedType, 4> DynamicExceptions;
430 SmallVector<SourceRange, 4> DynamicExceptionRanges;
431 ExprResult NoexceptExpr;
432 CachedTokens *ExceptionSpecTokens;
434 ExceptionSpecificationType EST
435 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
437 DynamicExceptionRanges, NoexceptExpr,
438 ExceptionSpecTokens);
440 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
441 Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
443 // Attach the exception-specification to the method.
444 Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
447 DynamicExceptionRanges,
448 NoexceptExpr.isUsable()?
449 NoexceptExpr.get() : nullptr);
451 // There could be leftover tokens (e.g. because of an error).
452 // Skip through until we reach the original token position.
453 while (Tok.isNot(tok::eof))
456 // Clean up the remaining EOF token.
457 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
461 LM.ExceptionSpecTokens = nullptr;
464 PrototypeScope.Exit();
466 // Finish the delayed C++ method declaration.
467 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
470 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
471 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
472 /// collected during its parsing and parse them all.
473 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
474 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
475 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
476 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
477 if (HasTemplateScope) {
478 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
479 ++CurTemplateDepthTracker;
481 bool HasClassScope = !Class.TopLevelClass;
482 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
485 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
486 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
490 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
491 // If this is a member template, introduce the template parameter scope.
492 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
493 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
494 if (LM.TemplateScope) {
495 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
496 ++CurTemplateDepthTracker;
499 assert(!LM.Toks.empty() && "Empty body!");
500 Token LastBodyToken = LM.Toks.back();
502 BodyEnd.startToken();
503 BodyEnd.setKind(tok::eof);
504 BodyEnd.setLocation(LastBodyToken.getEndLoc());
505 BodyEnd.setEofData(LM.D);
506 LM.Toks.push_back(BodyEnd);
507 // Append the current token at the end of the new token stream so that it
509 LM.Toks.push_back(Tok);
510 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
512 // Consume the previously pushed token.
513 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
514 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
515 && "Inline method not starting with '{', ':' or 'try'");
517 // Parse the method body. Function body parsing code is similar enough
518 // to be re-used for method bodies as well.
519 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
520 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
522 if (Tok.is(tok::kw_try)) {
523 ParseFunctionTryBlock(LM.D, FnScope);
525 while (Tok.isNot(tok::eof))
528 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
532 if (Tok.is(tok::colon)) {
533 ParseConstructorInitializer(LM.D);
536 if (!Tok.is(tok::l_brace)) {
538 Actions.ActOnFinishFunctionBody(LM.D, nullptr);
540 while (Tok.isNot(tok::eof))
543 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
548 Actions.ActOnDefaultCtorInitializers(LM.D);
550 assert((Actions.getDiagnostics().hasErrorOccurred() ||
551 !isa<FunctionTemplateDecl>(LM.D) ||
552 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
553 < TemplateParameterDepth) &&
554 "TemplateParameterDepth should be greater than the depth of "
555 "current template being instantiated!");
557 ParseFunctionStatementBody(LM.D, FnScope);
559 // Clear the late-template-parsed bit if we set it before.
561 LM.D->getAsFunction()->setLateTemplateParsed(false);
563 while (Tok.isNot(tok::eof))
566 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
569 if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(LM.D))
570 Actions.ActOnFinishInlineMethodDef(MD);
573 /// ParseLexedMemberInitializers - We finished parsing the member specification
574 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
575 /// initializers that were collected during its parsing and parse them all.
576 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
577 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
578 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
580 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
581 if (HasTemplateScope) {
582 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
583 ++CurTemplateDepthTracker;
585 // Set or update the scope flags.
586 bool AlreadyHasClassScope = Class.TopLevelClass;
587 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
588 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
589 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
591 if (!AlreadyHasClassScope)
592 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
593 Class.TagOrTemplate);
595 if (!Class.LateParsedDeclarations.empty()) {
596 // C++11 [expr.prim.general]p4:
597 // Otherwise, if a member-declarator declares a non-static data member
598 // (9.2) of a class X, the expression this is a prvalue of type "pointer
599 // to X" within the optional brace-or-equal-initializer. It shall not
600 // appear elsewhere in the member-declarator.
601 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
602 /*TypeQuals=*/(unsigned)0);
604 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
605 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
609 if (!AlreadyHasClassScope)
610 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
611 Class.TagOrTemplate);
613 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
616 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
617 if (!MI.Field || MI.Field->isInvalidDecl())
620 // Append the current token at the end of the new token stream so that it
622 MI.Toks.push_back(Tok);
623 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
625 // Consume the previously pushed token.
626 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
628 SourceLocation EqualLoc;
630 Actions.ActOnStartCXXInClassMemberInitializer();
632 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
635 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
638 // The next token should be our artificial terminating EOF token.
639 if (Tok.isNot(tok::eof)) {
640 if (!Init.isInvalid()) {
641 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
642 if (!EndLoc.isValid())
643 EndLoc = Tok.getLocation();
644 // No fixit; we can't recover as if there were a semicolon here.
645 Diag(EndLoc, diag::err_expected_semi_decl_list);
648 // Consume tokens until we hit the artificial EOF.
649 while (Tok.isNot(tok::eof))
652 // Make sure this is *our* artificial EOF token.
653 if (Tok.getEofData() == MI.Field)
657 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
658 /// container until the token 'T' is reached (which gets
659 /// consumed/stored too, if ConsumeFinalToken).
660 /// If StopAtSemi is true, then we will stop early at a ';' character.
661 /// Returns true if token 'T1' or 'T2' was found.
662 /// NOTE: This is a specialized version of Parser::SkipUntil.
663 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
665 bool StopAtSemi, bool ConsumeFinalToken) {
666 // We always want this function to consume at least one token if the first
667 // token isn't T and if not at EOF.
668 bool isFirstTokenConsumed = true;
670 // If we found one of the tokens, stop and return true.
671 if (Tok.is(T1) || Tok.is(T2)) {
672 if (ConsumeFinalToken) {
679 switch (Tok.getKind()) {
681 case tok::annot_module_begin:
682 case tok::annot_module_end:
683 case tok::annot_module_include:
684 // Ran out of tokens.
688 // Recursively consume properly-nested parens.
691 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
694 // Recursively consume properly-nested square brackets.
697 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
700 // Recursively consume properly-nested braces.
703 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
706 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
707 // Since the user wasn't looking for this token (if they were, it would
708 // already be handled), this isn't balanced. If there is a LHS token at a
709 // higher level, we will assume that this matches the unbalanced token
710 // and return it. Otherwise, this is a spurious RHS token, which we skip.
712 if (ParenCount && !isFirstTokenConsumed)
713 return false; // Matches something.
718 if (BracketCount && !isFirstTokenConsumed)
719 return false; // Matches something.
724 if (BraceCount && !isFirstTokenConsumed)
725 return false; // Matches something.
730 case tok::code_completion:
732 ConsumeCodeCompletionToken();
735 case tok::string_literal:
736 case tok::wide_string_literal:
737 case tok::utf8_string_literal:
738 case tok::utf16_string_literal:
739 case tok::utf32_string_literal:
741 ConsumeStringToken();
748 // consume this token.
753 isFirstTokenConsumed = false;
757 /// \brief Consume tokens and store them in the passed token container until
758 /// we've passed the try keyword and constructor initializers and have consumed
759 /// the opening brace of the function body. The opening brace will be consumed
760 /// if and only if there was no error.
762 /// \return True on error.
763 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
764 if (Tok.is(tok::kw_try)) {
769 if (Tok.isNot(tok::colon)) {
770 // Easy case, just a function body.
772 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
773 // brace: an opening one is the function body, while a closing one probably
774 // means we've reached the end of the class.
775 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
777 /*ConsumeFinalToken=*/false);
778 if (Tok.isNot(tok::l_brace))
779 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
789 // We can't reliably skip over a mem-initializer-id, because it could be
790 // a template-id involving not-yet-declared names. Given:
792 // S ( ) : a < b < c > ( e )
794 // 'e' might be an initializer or part of a template argument, depending
795 // on whether 'b' is a template.
797 // Track whether we might be inside a template argument. We can give
798 // significantly better diagnostics if we know that we're not.
799 bool MightBeTemplateArgument = false;
802 // Skip over the mem-initializer-id, if possible.
803 if (Tok.is(tok::kw_decltype)) {
805 SourceLocation OpenLoc = ConsumeToken();
806 if (Tok.isNot(tok::l_paren))
807 return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
811 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
812 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
813 Diag(OpenLoc, diag::note_matching) << tok::l_paren;
818 // Walk over a component of a nested-name-specifier.
819 if (Tok.is(tok::coloncolon)) {
823 if (Tok.is(tok::kw_template)) {
829 if (Tok.is(tok::identifier) || Tok.is(tok::kw_template)) {
832 } else if (Tok.is(tok::code_completion)) {
834 ConsumeCodeCompletionToken();
835 // Consume the rest of the initializers permissively.
836 // FIXME: We should be able to perform code-completion here even if
837 // there isn't a subsequent '{' token.
838 MightBeTemplateArgument = true;
843 } while (Tok.is(tok::coloncolon));
845 if (Tok.is(tok::less))
846 MightBeTemplateArgument = true;
848 if (MightBeTemplateArgument) {
849 // We may be inside a template argument list. Grab up to the start of the
850 // next parenthesized initializer or braced-init-list. This *might* be the
851 // initializer, or it might be a subexpression in the template argument
853 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
854 // if all angles are closed.
855 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
857 /*ConsumeFinalToken=*/false)) {
858 // We're not just missing the initializer, we're also missing the
860 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
862 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
863 // We found something weird in a mem-initializer-id.
864 if (getLangOpts().CPlusPlus11)
865 return Diag(Tok.getLocation(), diag::err_expected_either)
866 << tok::l_paren << tok::l_brace;
868 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
871 tok::TokenKind kind = Tok.getKind();
873 bool IsLParen = (kind == tok::l_paren);
874 SourceLocation OpenLoc = Tok.getLocation();
879 assert(kind == tok::l_brace && "Must be left paren or brace here.");
881 // In C++03, this has to be the start of the function body, which
882 // means the initializer is malformed; we'll diagnose it later.
883 if (!getLangOpts().CPlusPlus11)
887 // Grab the initializer (or the subexpression of the template argument).
888 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
889 // if we might be inside the braces of a lambda-expression.
890 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
891 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
892 Diag(Tok, diag::err_expected) << CloseKind;
893 Diag(OpenLoc, diag::note_matching) << kind;
897 // Grab pack ellipsis, if present.
898 if (Tok.is(tok::ellipsis)) {
903 // If we know we just consumed a mem-initializer, we must have ',' or '{'
905 if (Tok.is(tok::comma)) {
908 } else if (Tok.is(tok::l_brace)) {
909 // This is the function body if the ')' or '}' is immediately followed by
910 // a '{'. That cannot happen within a template argument, apart from the
911 // case where a template argument contains a compound literal:
913 // S ( ) : a < b < c > ( d ) { }
914 // // End of declaration, or still inside the template argument?
916 // ... and the case where the template argument contains a lambda:
918 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
921 // FIXME: Disambiguate these cases. Note that the latter case is probably
922 // going to be made ill-formed by core issue 1607.
926 } else if (!MightBeTemplateArgument) {
927 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
933 /// \brief Consume and store tokens from the '?' to the ':' in a conditional
935 bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
937 assert(Tok.is(tok::question));
941 while (Tok.isNot(tok::colon)) {
942 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
944 /*ConsumeFinalToken=*/false))
947 // If we found a nested conditional, consume it.
948 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
958 /// \brief A tentative parsing action that can also revert token annotations.
959 class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
961 explicit UnannotatedTentativeParsingAction(Parser &Self,
962 tok::TokenKind EndKind)
963 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
964 // Stash away the old token stream, so we can restore it once the
965 // tentative parse is complete.
966 TentativeParsingAction Inner(Self);
967 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
971 void RevertAnnotations() {
974 // Put back the original tokens.
975 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
977 Token *Buffer = new Token[Toks.size()];
978 std::copy(Toks.begin() + 1, Toks.end(), Buffer);
979 Buffer[Toks.size() - 1] = Self.Tok;
980 Self.PP.EnterTokenStream(Buffer, Toks.size(), true, /*Owned*/true);
982 Self.Tok = Toks.front();
989 tok::TokenKind EndKind;
992 /// ConsumeAndStoreInitializer - Consume and store the token at the passed token
993 /// container until the end of the current initializer expression (either a
994 /// default argument or an in-class initializer for a non-static data member).
996 /// Returns \c true if we reached the end of something initializer-shaped,
997 /// \c false if we bailed out.
998 bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
999 CachedInitKind CIK) {
1000 // We always want this function to consume at least one token if not at EOF.
1001 bool IsFirstToken = true;
1003 // Number of possible unclosed <s we've seen so far. These might be templates,
1004 // and might not, but if there were none of them (or we know for sure that
1005 // we're within a template), we can avoid a tentative parse.
1006 unsigned AngleCount = 0;
1007 unsigned KnownTemplateCount = 0;
1010 switch (Tok.getKind()) {
1012 // If we might be in a template, perform a tentative parse to check.
1014 // Not a template argument: this is the end of the initializer.
1016 if (KnownTemplateCount)
1019 // We hit a comma inside angle brackets. This is the hard case. The
1020 // rule we follow is:
1021 // * For a default argument, if the tokens after the comma form a
1022 // syntactically-valid parameter-declaration-clause, in which each
1023 // parameter has an initializer, then this comma ends the default
1025 // * For a default initializer, if the tokens after the comma form a
1026 // syntactically-valid init-declarator-list, then this comma ends
1027 // the default initializer.
1029 UnannotatedTentativeParsingAction PA(*this,
1030 CIK == CIK_DefaultInitializer
1031 ? tok::semi : tok::r_paren);
1032 Sema::TentativeAnalysisScope Scope(Actions);
1034 TPResult Result = TPResult::Error;
1037 case CIK_DefaultInitializer:
1038 Result = TryParseInitDeclaratorList();
1039 // If we parsed a complete, ambiguous init-declarator-list, this
1040 // is only syntactically-valid if it's followed by a semicolon.
1041 if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi))
1042 Result = TPResult::False;
1045 case CIK_DefaultArgument:
1046 bool InvalidAsDeclaration = false;
1047 Result = TryParseParameterDeclarationClause(
1048 &InvalidAsDeclaration, /*VersusTemplateArgument=*/true);
1049 // If this is an expression or a declaration with a missing
1050 // 'typename', assume it's not a declaration.
1051 if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1052 Result = TPResult::False;
1056 // If what follows could be a declaration, it is a declaration.
1057 if (Result != TPResult::False && Result != TPResult::Error) {
1062 // In the uncommon case that we decide the following tokens are part
1063 // of a template argument, revert any annotations we've performed in
1064 // those tokens. We're not going to look them up until we've parsed
1065 // the rest of the class, and that might add more declarations.
1066 PA.RevertAnnotations();
1069 // Keep going. We know we're inside a template argument list now.
1070 ++KnownTemplateCount;
1074 case tok::annot_module_begin:
1075 case tok::annot_module_end:
1076 case tok::annot_module_include:
1077 // Ran out of tokens.
1081 // FIXME: A '<' can only start a template-id if it's preceded by an
1082 // identifier, an operator-function-id, or a literal-operator-id.
1087 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1088 // that is *never* the end of the initializer. Skip to the ':'.
1089 if (!ConsumeAndStoreConditional(Toks))
1093 case tok::greatergreatergreater:
1094 if (!getLangOpts().CPlusPlus11)
1096 if (AngleCount) --AngleCount;
1097 if (KnownTemplateCount) --KnownTemplateCount;
1099 case tok::greatergreater:
1100 if (!getLangOpts().CPlusPlus11)
1102 if (AngleCount) --AngleCount;
1103 if (KnownTemplateCount) --KnownTemplateCount;
1106 if (AngleCount) --AngleCount;
1107 if (KnownTemplateCount) --KnownTemplateCount;
1110 case tok::kw_template:
1111 // 'template' identifier '<' is known to start a template argument list,
1112 // and can be used to disambiguate the parse.
1113 // FIXME: Support all forms of 'template' unqualified-id '<'.
1114 Toks.push_back(Tok);
1116 if (Tok.is(tok::identifier)) {
1117 Toks.push_back(Tok);
1119 if (Tok.is(tok::less)) {
1121 ++KnownTemplateCount;
1122 Toks.push_back(Tok);
1128 case tok::kw_operator:
1129 // If 'operator' precedes other punctuation, that punctuation loses
1130 // its special behavior.
1131 Toks.push_back(Tok);
1133 switch (Tok.getKind()) {
1135 case tok::greatergreatergreater:
1136 case tok::greatergreater:
1139 Toks.push_back(Tok);
1148 // Recursively consume properly-nested parens.
1149 Toks.push_back(Tok);
1151 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1154 // Recursively consume properly-nested square brackets.
1155 Toks.push_back(Tok);
1157 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
1160 // Recursively consume properly-nested braces.
1161 Toks.push_back(Tok);
1163 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1166 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1167 // Since the user wasn't looking for this token (if they were, it would
1168 // already be handled), this isn't balanced. If there is a LHS token at a
1169 // higher level, we will assume that this matches the unbalanced token
1170 // and return it. Otherwise, this is a spurious RHS token, which we
1171 // consume and pass on to downstream code to diagnose.
1173 if (CIK == CIK_DefaultArgument)
1174 return true; // End of the default argument.
1175 if (ParenCount && !IsFirstToken)
1177 Toks.push_back(Tok);
1181 if (BracketCount && !IsFirstToken)
1183 Toks.push_back(Tok);
1187 if (BraceCount && !IsFirstToken)
1189 Toks.push_back(Tok);
1193 case tok::code_completion:
1194 Toks.push_back(Tok);
1195 ConsumeCodeCompletionToken();
1198 case tok::string_literal:
1199 case tok::wide_string_literal:
1200 case tok::utf8_string_literal:
1201 case tok::utf16_string_literal:
1202 case tok::utf32_string_literal:
1203 Toks.push_back(Tok);
1204 ConsumeStringToken();
1207 if (CIK == CIK_DefaultInitializer)
1208 return true; // End of the default initializer.
1212 Toks.push_back(Tok);
1216 IsFirstToken = false;