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 FunctionDefinitionKind DefinitionKind,
32 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
33 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
34 Tok.is(tok::equal)) &&
35 "Current token not a '{', ':', '=', or 'try'!");
37 MultiTemplateParamsArg TemplateParams(
38 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
40 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
43 D.setFunctionDefinitionKind(DefinitionKind);
44 if (D.getDeclSpec().isFriendSpecified())
45 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
48 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
49 TemplateParams, nullptr,
52 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
53 bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType();
55 Actions.AddInitializerToDecl(FnD, Init.get(), false,
56 TypeSpecContainsAuto);
58 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
62 HandleMemberFunctionDeclDelays(D, FnD);
66 if (TryConsumeToken(tok::equal)) {
74 if (TryConsumeToken(tok::kw_delete, KWLoc)) {
75 Diag(KWLoc, getLangOpts().CPlusPlus11
76 ? diag::warn_cxx98_compat_deleted_function
77 : diag::ext_deleted_function);
78 Actions.SetDeclDeleted(FnD, KWLoc);
80 } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
81 Diag(KWLoc, getLangOpts().CPlusPlus11
82 ? diag::warn_cxx98_compat_defaulted_function
83 : diag::ext_defaulted_function);
84 Actions.SetDeclDefaulted(FnD, KWLoc);
86 llvm_unreachable("function definition after = not 'delete' or 'default'");
89 if (Tok.is(tok::comma)) {
90 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
93 } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
94 Delete ? "delete" : "default")) {
101 // In delayed template parsing mode, if we are within a class template
102 // or if we are about to parse function member template then consume
103 // the tokens and store them for parsing at the end of the translation unit.
104 if (getLangOpts().DelayedTemplateParsing &&
105 DefinitionKind == FDK_Definition &&
106 !D.getDeclSpec().isConstexprSpecified() &&
107 !(FnD && FnD->getAsFunction() &&
108 FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
109 ((Actions.CurContext->isDependentContext() ||
110 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
111 TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
112 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
115 LexTemplateFunctionForLateParsing(Toks);
118 FunctionDecl *FD = FnD->getAsFunction();
119 Actions.CheckForFunctionRedefinition(FD);
120 Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
126 // Consume the tokens and store them for later parsing.
128 LexedMethod* LM = new LexedMethod(this, FnD);
129 getCurrentClass().LateParsedDeclarations.push_back(LM);
130 LM->TemplateScope = getCurScope()->isTemplateParamScope();
131 CachedTokens &Toks = LM->Toks;
133 tok::TokenKind kind = Tok.getKind();
134 // Consume everything up to (and including) the left brace of the
136 if (ConsumeAndStoreFunctionPrologue(Toks)) {
137 // We didn't find the left-brace we expected after the
138 // constructor initializer; we already printed an error, and it's likely
139 // impossible to recover, so don't try to parse this method later.
140 // Skip over the rest of the decl and back to somewhere that looks
143 delete getCurrentClass().LateParsedDeclarations.back();
144 getCurrentClass().LateParsedDeclarations.pop_back();
147 // Consume everything up to (and including) the matching right brace.
148 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
151 // If we're in a function-try-block, we need to store all the catch blocks.
152 if (kind == tok::kw_try) {
153 while (Tok.is(tok::kw_catch)) {
154 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
155 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
160 // If this is a friend function, mark that it's late-parsed so that
161 // it's still known to be a definition even before we attach the
162 // parsed body. Sema needs to treat friend function definitions
163 // differently during template instantiation, and it's possible for
164 // the containing class to be instantiated before all its member
165 // function definitions are parsed.
167 // If you remove this, you can remove the code that clears the flag
168 // after parsing the member.
169 if (D.getDeclSpec().isFriendSpecified()) {
170 FunctionDecl *FD = FnD->getAsFunction();
171 Actions.CheckForFunctionRedefinition(FD);
172 FD->setLateTemplateParsed(true);
175 // If semantic analysis could not build a function declaration,
176 // just throw away the late-parsed declaration.
177 delete getCurrentClass().LateParsedDeclarations.back();
178 getCurrentClass().LateParsedDeclarations.pop_back();
184 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
185 /// specified Declarator is a well formed C++ non-static data member
186 /// declaration. Now lex its initializer and store its tokens for parsing
187 /// after the class is complete.
188 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
189 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
190 "Current token not a '{' or '='!");
192 LateParsedMemberInitializer *MI =
193 new LateParsedMemberInitializer(this, VarD);
194 getCurrentClass().LateParsedDeclarations.push_back(MI);
195 CachedTokens &Toks = MI->Toks;
197 tok::TokenKind kind = Tok.getKind();
198 if (kind == tok::equal) {
203 if (kind == tok::l_brace) {
204 // Begin by storing the '{' token.
208 // Consume everything up to (and including) the matching right brace.
209 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
211 // Consume everything up to (but excluding) the comma or semicolon.
212 ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
215 // Store an artificial EOF token to ensure that we don't run off the end of
216 // the initializer when we come to parse it.
219 Eof.setKind(tok::eof);
220 Eof.setLocation(Tok.getLocation());
221 Eof.setEofData(VarD);
225 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
226 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
227 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
228 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
230 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
231 : Self(P), Class(C) {}
233 Parser::LateParsedClass::~LateParsedClass() {
234 Self->DeallocateParsedClasses(Class);
237 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
238 Self->ParseLexedMethodDeclarations(*Class);
241 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
242 Self->ParseLexedMemberInitializers(*Class);
245 void Parser::LateParsedClass::ParseLexedMethodDefs() {
246 Self->ParseLexedMethodDefs(*Class);
249 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
250 Self->ParseLexedMethodDeclaration(*this);
253 void Parser::LexedMethod::ParseLexedMethodDefs() {
254 Self->ParseLexedMethodDef(*this);
257 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
258 Self->ParseLexedMemberInitializer(*this);
261 /// ParseLexedMethodDeclarations - We finished parsing the member
262 /// specification of a top (non-nested) C++ class. Now go over the
263 /// stack of method declarations with some parts for which parsing was
264 /// delayed (such as default arguments) and parse them.
265 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
266 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
267 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
269 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
270 if (HasTemplateScope) {
271 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
272 ++CurTemplateDepthTracker;
275 // The current scope is still active if we're the top-level class.
276 // Otherwise we'll need to push and enter a new scope.
277 bool HasClassScope = !Class.TopLevelClass;
278 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
281 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
282 Class.TagOrTemplate);
284 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
285 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
289 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
290 Class.TagOrTemplate);
293 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
294 // If this is a member template, introduce the template parameter scope.
295 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
296 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
297 if (LM.TemplateScope) {
298 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
299 ++CurTemplateDepthTracker;
301 // Start the delayed C++ method declaration
302 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
304 // Introduce the parameters into scope and parse their default
306 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
307 Scope::FunctionDeclarationScope | Scope::DeclScope);
308 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
309 // Introduce the parameter into scope.
310 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
311 LM.DefaultArgs[I].Param);
312 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
313 // Mark the end of the default argument so that we know when to stop when
314 // we parse it later on.
315 Token LastDefaultArgToken = Toks->back();
317 DefArgEnd.startToken();
318 DefArgEnd.setKind(tok::eof);
319 DefArgEnd.setLocation(LastDefaultArgToken.getLocation().getLocWithOffset(
320 LastDefaultArgToken.getLength()));
321 DefArgEnd.setEofData(LM.DefaultArgs[I].Param);
322 Toks->push_back(DefArgEnd);
324 // Parse the default argument from its saved token stream.
325 Toks->push_back(Tok); // So that the current token doesn't get lost
326 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
328 // Consume the previously-pushed token.
332 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
333 SourceLocation EqualLoc = ConsumeToken();
335 // The argument isn't actually potentially evaluated unless it is
337 EnterExpressionEvaluationContext Eval(Actions,
338 Sema::PotentiallyEvaluatedIfUsed,
339 LM.DefaultArgs[I].Param);
341 ExprResult DefArgResult;
342 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
343 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
344 DefArgResult = ParseBraceInitializer();
346 DefArgResult = ParseAssignmentExpression();
347 DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
348 if (DefArgResult.isInvalid()) {
349 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param,
352 if (Tok.isNot(tok::eof) ||
353 Tok.getEofData() != LM.DefaultArgs[I].Param) {
354 // The last two tokens are the terminator and the saved value of
355 // Tok; the last token in the default argument is the one before
357 assert(Toks->size() >= 3 && "expected a token in default arg");
358 Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
359 << SourceRange(Tok.getLocation(),
360 (*Toks)[Toks->size() - 3].getLocation());
362 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
366 // There could be leftover tokens (e.g. because of an error).
367 // Skip through until we reach the 'end of default argument' token.
368 while (Tok.isNot(tok::eof))
371 if (Tok.is(tok::eof) && Tok.getEofData() == LM.DefaultArgs[I].Param)
375 LM.DefaultArgs[I].Toks = nullptr;
379 // Parse a delayed exception-specification, if there is one.
380 if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
381 // Add the 'stop' token.
382 Token LastExceptionSpecToken = Toks->back();
383 Token ExceptionSpecEnd;
384 ExceptionSpecEnd.startToken();
385 ExceptionSpecEnd.setKind(tok::eof);
386 ExceptionSpecEnd.setLocation(
387 LastExceptionSpecToken.getLocation().getLocWithOffset(
388 LastExceptionSpecToken.getLength()));
389 ExceptionSpecEnd.setEofData(LM.Method);
390 Toks->push_back(ExceptionSpecEnd);
392 // Parse the default argument from its saved token stream.
393 Toks->push_back(Tok); // So that the current token doesn't get lost
394 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
396 // Consume the previously-pushed token.
399 // C++11 [expr.prim.general]p3:
400 // If a declaration declares a member function or member function
401 // template of a class X, the expression this is a prvalue of type
402 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
403 // and the end of the function-definition, member-declarator, or
405 CXXMethodDecl *Method;
406 if (FunctionTemplateDecl *FunTmpl
407 = dyn_cast<FunctionTemplateDecl>(LM.Method))
408 Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
410 Method = cast<CXXMethodDecl>(LM.Method);
412 Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
413 Method->getTypeQualifiers(),
414 getLangOpts().CPlusPlus11);
416 // Parse the exception-specification.
417 SourceRange SpecificationRange;
418 SmallVector<ParsedType, 4> DynamicExceptions;
419 SmallVector<SourceRange, 4> DynamicExceptionRanges;
420 ExprResult NoexceptExpr;
421 CachedTokens *ExceptionSpecTokens;
423 ExceptionSpecificationType EST
424 = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
426 DynamicExceptionRanges, NoexceptExpr,
427 ExceptionSpecTokens);
429 if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
430 Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
432 // Attach the exception-specification to the method.
433 Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
436 DynamicExceptionRanges,
437 NoexceptExpr.isUsable()?
438 NoexceptExpr.get() : nullptr);
440 // There could be leftover tokens (e.g. because of an error).
441 // Skip through until we reach the original token position.
442 while (Tok.isNot(tok::eof))
445 // Clean up the remaining EOF token.
446 if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
450 LM.ExceptionSpecTokens = nullptr;
453 PrototypeScope.Exit();
455 // Finish the delayed C++ method declaration.
456 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
459 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
460 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
461 /// collected during its parsing and parse them all.
462 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
463 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
464 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
465 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
466 if (HasTemplateScope) {
467 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
468 ++CurTemplateDepthTracker;
470 bool HasClassScope = !Class.TopLevelClass;
471 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
474 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
475 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
479 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
480 // If this is a member template, introduce the template parameter scope.
481 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
482 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
483 if (LM.TemplateScope) {
484 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
485 ++CurTemplateDepthTracker;
488 assert(!LM.Toks.empty() && "Empty body!");
489 Token LastBodyToken = LM.Toks.back();
491 BodyEnd.startToken();
492 BodyEnd.setKind(tok::eof);
494 LastBodyToken.getLocation().getLocWithOffset(LastBodyToken.getLength()));
495 BodyEnd.setEofData(LM.D);
496 LM.Toks.push_back(BodyEnd);
497 // Append the current token at the end of the new token stream so that it
499 LM.Toks.push_back(Tok);
500 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
502 // Consume the previously pushed token.
503 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
504 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
505 && "Inline method not starting with '{', ':' or 'try'");
507 // Parse the method body. Function body parsing code is similar enough
508 // to be re-used for method bodies as well.
509 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
510 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
512 if (Tok.is(tok::kw_try)) {
513 ParseFunctionTryBlock(LM.D, FnScope);
515 while (Tok.isNot(tok::eof))
518 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
522 if (Tok.is(tok::colon)) {
523 ParseConstructorInitializer(LM.D);
526 if (!Tok.is(tok::l_brace)) {
528 Actions.ActOnFinishFunctionBody(LM.D, nullptr);
530 while (Tok.isNot(tok::eof))
533 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
538 Actions.ActOnDefaultCtorInitializers(LM.D);
540 assert((Actions.getDiagnostics().hasErrorOccurred() ||
541 !isa<FunctionTemplateDecl>(LM.D) ||
542 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
543 < TemplateParameterDepth) &&
544 "TemplateParameterDepth should be greater than the depth of "
545 "current template being instantiated!");
547 ParseFunctionStatementBody(LM.D, FnScope);
549 // Clear the late-template-parsed bit if we set it before.
551 LM.D->getAsFunction()->setLateTemplateParsed(false);
553 while (Tok.isNot(tok::eof))
556 if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
559 if (CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(LM.D))
560 Actions.ActOnFinishInlineMethodDef(MD);
563 /// ParseLexedMemberInitializers - We finished parsing the member specification
564 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
565 /// initializers that were collected during its parsing and parse them all.
566 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
567 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
568 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
570 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
571 if (HasTemplateScope) {
572 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
573 ++CurTemplateDepthTracker;
575 // Set or update the scope flags.
576 bool AlreadyHasClassScope = Class.TopLevelClass;
577 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
578 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
579 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
581 if (!AlreadyHasClassScope)
582 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
583 Class.TagOrTemplate);
585 if (!Class.LateParsedDeclarations.empty()) {
586 // C++11 [expr.prim.general]p4:
587 // Otherwise, if a member-declarator declares a non-static data member
588 // (9.2) of a class X, the expression this is a prvalue of type "pointer
589 // to X" within the optional brace-or-equal-initializer. It shall not
590 // appear elsewhere in the member-declarator.
591 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
592 /*TypeQuals=*/(unsigned)0);
594 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
595 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
599 if (!AlreadyHasClassScope)
600 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
601 Class.TagOrTemplate);
603 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
606 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
607 if (!MI.Field || MI.Field->isInvalidDecl())
610 // Append the current token at the end of the new token stream so that it
612 MI.Toks.push_back(Tok);
613 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
615 // Consume the previously pushed token.
616 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
618 SourceLocation EqualLoc;
620 Actions.ActOnStartCXXInClassMemberInitializer();
622 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
625 Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
628 // The next token should be our artificial terminating EOF token.
629 if (Tok.isNot(tok::eof)) {
630 if (!Init.isInvalid()) {
631 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
632 if (!EndLoc.isValid())
633 EndLoc = Tok.getLocation();
634 // No fixit; we can't recover as if there were a semicolon here.
635 Diag(EndLoc, diag::err_expected_semi_decl_list);
638 // Consume tokens until we hit the artificial EOF.
639 while (Tok.isNot(tok::eof))
642 // Make sure this is *our* artificial EOF token.
643 if (Tok.getEofData() == MI.Field)
647 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
648 /// container until the token 'T' is reached (which gets
649 /// consumed/stored too, if ConsumeFinalToken).
650 /// If StopAtSemi is true, then we will stop early at a ';' character.
651 /// Returns true if token 'T1' or 'T2' was found.
652 /// NOTE: This is a specialized version of Parser::SkipUntil.
653 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
655 bool StopAtSemi, bool ConsumeFinalToken) {
656 // We always want this function to consume at least one token if the first
657 // token isn't T and if not at EOF.
658 bool isFirstTokenConsumed = true;
660 // If we found one of the tokens, stop and return true.
661 if (Tok.is(T1) || Tok.is(T2)) {
662 if (ConsumeFinalToken) {
669 switch (Tok.getKind()) {
671 case tok::annot_module_begin:
672 case tok::annot_module_end:
673 case tok::annot_module_include:
674 // Ran out of tokens.
678 // Recursively consume properly-nested parens.
681 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
684 // Recursively consume properly-nested square brackets.
687 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
690 // Recursively consume properly-nested braces.
693 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
696 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
697 // Since the user wasn't looking for this token (if they were, it would
698 // already be handled), this isn't balanced. If there is a LHS token at a
699 // higher level, we will assume that this matches the unbalanced token
700 // and return it. Otherwise, this is a spurious RHS token, which we skip.
702 if (ParenCount && !isFirstTokenConsumed)
703 return false; // Matches something.
708 if (BracketCount && !isFirstTokenConsumed)
709 return false; // Matches something.
714 if (BraceCount && !isFirstTokenConsumed)
715 return false; // Matches something.
720 case tok::code_completion:
722 ConsumeCodeCompletionToken();
725 case tok::string_literal:
726 case tok::wide_string_literal:
727 case tok::utf8_string_literal:
728 case tok::utf16_string_literal:
729 case tok::utf32_string_literal:
731 ConsumeStringToken();
738 // consume this token.
743 isFirstTokenConsumed = false;
747 /// \brief Consume tokens and store them in the passed token container until
748 /// we've passed the try keyword and constructor initializers and have consumed
749 /// the opening brace of the function body. The opening brace will be consumed
750 /// if and only if there was no error.
752 /// \return True on error.
753 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
754 if (Tok.is(tok::kw_try)) {
759 if (Tok.isNot(tok::colon)) {
760 // Easy case, just a function body.
762 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
763 // brace: an opening one is the function body, while a closing one probably
764 // means we've reached the end of the class.
765 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
767 /*ConsumeFinalToken=*/false);
768 if (Tok.isNot(tok::l_brace))
769 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
779 // We can't reliably skip over a mem-initializer-id, because it could be
780 // a template-id involving not-yet-declared names. Given:
782 // S ( ) : a < b < c > ( e )
784 // 'e' might be an initializer or part of a template argument, depending
785 // on whether 'b' is a template.
787 // Track whether we might be inside a template argument. We can give
788 // significantly better diagnostics if we know that we're not.
789 bool MightBeTemplateArgument = false;
792 // Skip over the mem-initializer-id, if possible.
793 if (Tok.is(tok::kw_decltype)) {
795 SourceLocation OpenLoc = ConsumeToken();
796 if (Tok.isNot(tok::l_paren))
797 return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
801 if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
802 Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
803 Diag(OpenLoc, diag::note_matching) << tok::l_paren;
808 // Walk over a component of a nested-name-specifier.
809 if (Tok.is(tok::coloncolon)) {
813 if (Tok.is(tok::kw_template)) {
819 if (Tok.is(tok::identifier) || Tok.is(tok::kw_template)) {
822 } else if (Tok.is(tok::code_completion)) {
824 ConsumeCodeCompletionToken();
825 // Consume the rest of the initializers permissively.
826 // FIXME: We should be able to perform code-completion here even if
827 // there isn't a subsequent '{' token.
828 MightBeTemplateArgument = true;
833 } while (Tok.is(tok::coloncolon));
835 if (Tok.is(tok::less))
836 MightBeTemplateArgument = true;
838 if (MightBeTemplateArgument) {
839 // We may be inside a template argument list. Grab up to the start of the
840 // next parenthesized initializer or braced-init-list. This *might* be the
841 // initializer, or it might be a subexpression in the template argument
843 // FIXME: Count angle brackets, and clear MightBeTemplateArgument
844 // if all angles are closed.
845 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
847 /*ConsumeFinalToken=*/false)) {
848 // We're not just missing the initializer, we're also missing the
850 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
852 } else if (Tok.isNot(tok::l_paren) && Tok.isNot(tok::l_brace)) {
853 // We found something weird in a mem-initializer-id.
854 if (getLangOpts().CPlusPlus11)
855 return Diag(Tok.getLocation(), diag::err_expected_either)
856 << tok::l_paren << tok::l_brace;
858 return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
861 tok::TokenKind kind = Tok.getKind();
863 bool IsLParen = (kind == tok::l_paren);
864 SourceLocation OpenLoc = Tok.getLocation();
869 assert(kind == tok::l_brace && "Must be left paren or brace here.");
871 // In C++03, this has to be the start of the function body, which
872 // means the initializer is malformed; we'll diagnose it later.
873 if (!getLangOpts().CPlusPlus11)
877 // Grab the initializer (or the subexpression of the template argument).
878 // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false
879 // if we might be inside the braces of a lambda-expression.
880 tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace;
881 if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
882 Diag(Tok, diag::err_expected) << CloseKind;
883 Diag(OpenLoc, diag::note_matching) << kind;
887 // Grab pack ellipsis, if present.
888 if (Tok.is(tok::ellipsis)) {
893 // If we know we just consumed a mem-initializer, we must have ',' or '{'
895 if (Tok.is(tok::comma)) {
898 } else if (Tok.is(tok::l_brace)) {
899 // This is the function body if the ')' or '}' is immediately followed by
900 // a '{'. That cannot happen within a template argument, apart from the
901 // case where a template argument contains a compound literal:
903 // S ( ) : a < b < c > ( d ) { }
904 // // End of declaration, or still inside the template argument?
906 // ... and the case where the template argument contains a lambda:
908 // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; }
911 // FIXME: Disambiguate these cases. Note that the latter case is probably
912 // going to be made ill-formed by core issue 1607.
916 } else if (!MightBeTemplateArgument) {
917 return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
923 /// \brief Consume and store tokens from the '?' to the ':' in a conditional
925 bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
927 assert(Tok.is(tok::question));
931 while (Tok.isNot(tok::colon)) {
932 if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
934 /*ConsumeFinalToken=*/false))
937 // If we found a nested conditional, consume it.
938 if (Tok.is(tok::question) && !ConsumeAndStoreConditional(Toks))
948 /// \brief A tentative parsing action that can also revert token annotations.
949 class Parser::UnannotatedTentativeParsingAction : public TentativeParsingAction {
951 explicit UnannotatedTentativeParsingAction(Parser &Self,
952 tok::TokenKind EndKind)
953 : TentativeParsingAction(Self), Self(Self), EndKind(EndKind) {
954 // Stash away the old token stream, so we can restore it once the
955 // tentative parse is complete.
956 TentativeParsingAction Inner(Self);
957 Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
961 void RevertAnnotations() {
964 // Put back the original tokens.
965 Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
967 Token *Buffer = new Token[Toks.size()];
968 std::copy(Toks.begin() + 1, Toks.end(), Buffer);
969 Buffer[Toks.size() - 1] = Self.Tok;
970 Self.PP.EnterTokenStream(Buffer, Toks.size(), true, /*Owned*/true);
972 Self.Tok = Toks.front();
979 tok::TokenKind EndKind;
982 /// ConsumeAndStoreInitializer - Consume and store the token at the passed token
983 /// container until the end of the current initializer expression (either a
984 /// default argument or an in-class initializer for a non-static data member).
986 /// Returns \c true if we reached the end of something initializer-shaped,
987 /// \c false if we bailed out.
988 bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks,
989 CachedInitKind CIK) {
990 // We always want this function to consume at least one token if not at EOF.
991 bool IsFirstToken = true;
993 // Number of possible unclosed <s we've seen so far. These might be templates,
994 // and might not, but if there were none of them (or we know for sure that
995 // we're within a template), we can avoid a tentative parse.
996 unsigned AngleCount = 0;
997 unsigned KnownTemplateCount = 0;
1000 switch (Tok.getKind()) {
1002 // If we might be in a template, perform a tentative parse to check.
1004 // Not a template argument: this is the end of the initializer.
1006 if (KnownTemplateCount)
1009 // We hit a comma inside angle brackets. This is the hard case. The
1010 // rule we follow is:
1011 // * For a default argument, if the tokens after the comma form a
1012 // syntactically-valid parameter-declaration-clause, in which each
1013 // parameter has an initializer, then this comma ends the default
1015 // * For a default initializer, if the tokens after the comma form a
1016 // syntactically-valid init-declarator-list, then this comma ends
1017 // the default initializer.
1019 UnannotatedTentativeParsingAction PA(*this,
1020 CIK == CIK_DefaultInitializer
1021 ? tok::semi : tok::r_paren);
1022 Sema::TentativeAnalysisScope Scope(Actions);
1024 TPResult Result = TPResult::Error;
1027 case CIK_DefaultInitializer:
1028 Result = TryParseInitDeclaratorList();
1029 // If we parsed a complete, ambiguous init-declarator-list, this
1030 // is only syntactically-valid if it's followed by a semicolon.
1031 if (Result == TPResult::Ambiguous && Tok.isNot(tok::semi))
1032 Result = TPResult::False;
1035 case CIK_DefaultArgument:
1036 bool InvalidAsDeclaration = false;
1037 Result = TryParseParameterDeclarationClause(
1038 &InvalidAsDeclaration, /*VersusTemplateArgument=*/true);
1039 // If this is an expression or a declaration with a missing
1040 // 'typename', assume it's not a declaration.
1041 if (Result == TPResult::Ambiguous && InvalidAsDeclaration)
1042 Result = TPResult::False;
1046 // If what follows could be a declaration, it is a declaration.
1047 if (Result != TPResult::False && Result != TPResult::Error) {
1052 // In the uncommon case that we decide the following tokens are part
1053 // of a template argument, revert any annotations we've performed in
1054 // those tokens. We're not going to look them up until we've parsed
1055 // the rest of the class, and that might add more declarations.
1056 PA.RevertAnnotations();
1059 // Keep going. We know we're inside a template argument list now.
1060 ++KnownTemplateCount;
1064 case tok::annot_module_begin:
1065 case tok::annot_module_end:
1066 case tok::annot_module_include:
1067 // Ran out of tokens.
1071 // FIXME: A '<' can only start a template-id if it's preceded by an
1072 // identifier, an operator-function-id, or a literal-operator-id.
1077 // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does,
1078 // that is *never* the end of the initializer. Skip to the ':'.
1079 if (!ConsumeAndStoreConditional(Toks))
1083 case tok::greatergreatergreater:
1084 if (!getLangOpts().CPlusPlus11)
1086 if (AngleCount) --AngleCount;
1087 if (KnownTemplateCount) --KnownTemplateCount;
1089 case tok::greatergreater:
1090 if (!getLangOpts().CPlusPlus11)
1092 if (AngleCount) --AngleCount;
1093 if (KnownTemplateCount) --KnownTemplateCount;
1096 if (AngleCount) --AngleCount;
1097 if (KnownTemplateCount) --KnownTemplateCount;
1100 case tok::kw_template:
1101 // 'template' identifier '<' is known to start a template argument list,
1102 // and can be used to disambiguate the parse.
1103 // FIXME: Support all forms of 'template' unqualified-id '<'.
1104 Toks.push_back(Tok);
1106 if (Tok.is(tok::identifier)) {
1107 Toks.push_back(Tok);
1109 if (Tok.is(tok::less)) {
1111 ++KnownTemplateCount;
1112 Toks.push_back(Tok);
1118 case tok::kw_operator:
1119 // If 'operator' precedes other punctuation, that punctuation loses
1120 // its special behavior.
1121 Toks.push_back(Tok);
1123 switch (Tok.getKind()) {
1125 case tok::greatergreatergreater:
1126 case tok::greatergreater:
1129 Toks.push_back(Tok);
1138 // Recursively consume properly-nested parens.
1139 Toks.push_back(Tok);
1141 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1144 // Recursively consume properly-nested square brackets.
1145 Toks.push_back(Tok);
1147 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
1150 // Recursively consume properly-nested braces.
1151 Toks.push_back(Tok);
1153 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1156 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
1157 // Since the user wasn't looking for this token (if they were, it would
1158 // already be handled), this isn't balanced. If there is a LHS token at a
1159 // higher level, we will assume that this matches the unbalanced token
1160 // and return it. Otherwise, this is a spurious RHS token, which we
1161 // consume and pass on to downstream code to diagnose.
1163 if (CIK == CIK_DefaultArgument)
1164 return true; // End of the default argument.
1165 if (ParenCount && !IsFirstToken)
1167 Toks.push_back(Tok);
1171 if (BracketCount && !IsFirstToken)
1173 Toks.push_back(Tok);
1177 if (BraceCount && !IsFirstToken)
1179 Toks.push_back(Tok);
1183 case tok::code_completion:
1184 Toks.push_back(Tok);
1185 ConsumeCodeCompletionToken();
1188 case tok::string_literal:
1189 case tok::wide_string_literal:
1190 case tok::utf8_string_literal:
1191 case tok::utf16_string_literal:
1192 case tok::utf32_string_literal:
1193 Toks.push_back(Tok);
1194 ConsumeStringToken();
1197 if (CIK == CIK_DefaultInitializer)
1198 return true; // End of the default initializer.
1202 Toks.push_back(Tok);
1206 IsFirstToken = false;