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 /// Get the FunctionDecl for a function or function template decl.
23 static FunctionDecl *getFunctionDecl(Decl *D) {
24 if (FunctionDecl *fn = dyn_cast<FunctionDecl>(D))
26 return cast<FunctionTemplateDecl>(D)->getTemplatedDecl();
29 /// ParseCXXInlineMethodDef - We parsed and verified that the specified
30 /// Declarator is a well formed C++ inline method definition. Now lex its body
31 /// and store its tokens for parsing after the C++ class is complete.
32 NamedDecl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
33 AttributeList *AccessAttrs,
35 const ParsedTemplateInfo &TemplateInfo,
36 const VirtSpecifiers& VS,
37 FunctionDefinitionKind DefinitionKind,
39 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
40 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
41 Tok.is(tok::equal)) &&
42 "Current token not a '{', ':', '=', or 'try'!");
44 MultiTemplateParamsArg TemplateParams(
45 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
46 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
49 D.setFunctionDefinitionKind(DefinitionKind);
50 if (D.getDeclSpec().isFriendSpecified())
51 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
54 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
58 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
60 bool TypeSpecContainsAuto = D.getDeclSpec().containsPlaceholderType();
62 Actions.AddInitializerToDecl(FnD, Init.get(), false,
63 TypeSpecContainsAuto);
65 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
69 HandleMemberFunctionDeclDelays(D, FnD);
73 if (Tok.is(tok::equal)) {
83 if (Tok.is(tok::kw_delete)) {
84 Diag(Tok, getLangOpts().CPlusPlus11 ?
85 diag::warn_cxx98_compat_deleted_function :
86 diag::ext_deleted_function);
88 KWLoc = ConsumeToken();
89 Actions.SetDeclDeleted(FnD, KWLoc);
91 } else if (Tok.is(tok::kw_default)) {
92 Diag(Tok, getLangOpts().CPlusPlus11 ?
93 diag::warn_cxx98_compat_defaulted_function :
94 diag::ext_defaulted_function);
96 KWLoc = ConsumeToken();
97 Actions.SetDeclDefaulted(FnD, KWLoc);
99 llvm_unreachable("function definition after = not 'delete' or 'default'");
102 if (Tok.is(tok::comma)) {
103 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
105 SkipUntil(tok::semi);
107 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
108 Delete ? "delete" : "default", tok::semi);
114 // In delayed template parsing mode, if we are within a class template
115 // or if we are about to parse function member template then consume
116 // the tokens and store them for parsing at the end of the translation unit.
117 if (getLangOpts().DelayedTemplateParsing &&
118 DefinitionKind == FDK_Definition &&
119 ((Actions.CurContext->isDependentContext() ||
120 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
121 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
124 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
126 FunctionDecl *FD = getFunctionDecl(FnD);
127 Actions.CheckForFunctionRedefinition(FD);
129 LateParsedTemplateMap[FD] = LPT;
130 Actions.MarkAsLateParsedTemplate(FD);
131 LexTemplateFunctionForLateParsing(LPT->Toks);
134 LexTemplateFunctionForLateParsing(Toks);
140 // Consume the tokens and store them for later parsing.
142 LexedMethod* LM = new LexedMethod(this, FnD);
143 getCurrentClass().LateParsedDeclarations.push_back(LM);
144 LM->TemplateScope = getCurScope()->isTemplateParamScope();
145 CachedTokens &Toks = LM->Toks;
147 tok::TokenKind kind = Tok.getKind();
148 // Consume everything up to (and including) the left brace of the
150 if (ConsumeAndStoreFunctionPrologue(Toks)) {
151 // We didn't find the left-brace we expected after the
152 // constructor initializer; we already printed an error, and it's likely
153 // impossible to recover, so don't try to parse this method later.
154 // If we stopped at a semicolon, consume it to avoid an extra warning.
155 if (Tok.is(tok::semi))
157 delete getCurrentClass().LateParsedDeclarations.back();
158 getCurrentClass().LateParsedDeclarations.pop_back();
161 // Consume everything up to (and including) the matching right brace.
162 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
165 // If we're in a function-try-block, we need to store all the catch blocks.
166 if (kind == tok::kw_try) {
167 while (Tok.is(tok::kw_catch)) {
168 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
169 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
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();
181 // If this is a friend function, mark that it's late-parsed so that
182 // it's still known to be a definition even before we attach the
183 // parsed body. Sema needs to treat friend function definitions
184 // differently during template instantiation, and it's possible for
185 // the containing class to be instantiated before all its member
186 // function definitions are parsed.
188 // If you remove this, you can remove the code that clears the flag
189 // after parsing the member.
190 if (D.getDeclSpec().isFriendSpecified()) {
191 getFunctionDecl(FnD)->setLateTemplateParsed(true);
197 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
198 /// specified Declarator is a well formed C++ non-static data member
199 /// declaration. Now lex its initializer and store its tokens for parsing
200 /// after the class is complete.
201 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
202 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
203 "Current token not a '{' or '='!");
205 LateParsedMemberInitializer *MI =
206 new LateParsedMemberInitializer(this, VarD);
207 getCurrentClass().LateParsedDeclarations.push_back(MI);
208 CachedTokens &Toks = MI->Toks;
210 tok::TokenKind kind = Tok.getKind();
211 if (kind == tok::equal) {
216 if (kind == tok::l_brace) {
217 // Begin by storing the '{' token.
221 // Consume everything up to (and including) the matching right brace.
222 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
224 // Consume everything up to (but excluding) the comma or semicolon.
225 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
226 /*ConsumeFinalToken=*/false);
229 // Store an artificial EOF token to ensure that we don't run off the end of
230 // the initializer when we come to parse it.
233 Eof.setKind(tok::eof);
234 Eof.setLocation(Tok.getLocation());
238 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
239 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
240 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
241 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
243 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
244 : Self(P), Class(C) {}
246 Parser::LateParsedClass::~LateParsedClass() {
247 Self->DeallocateParsedClasses(Class);
250 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
251 Self->ParseLexedMethodDeclarations(*Class);
254 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
255 Self->ParseLexedMemberInitializers(*Class);
258 void Parser::LateParsedClass::ParseLexedMethodDefs() {
259 Self->ParseLexedMethodDefs(*Class);
262 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
263 Self->ParseLexedMethodDeclaration(*this);
266 void Parser::LexedMethod::ParseLexedMethodDefs() {
267 Self->ParseLexedMethodDef(*this);
270 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
271 Self->ParseLexedMemberInitializer(*this);
274 /// ParseLexedMethodDeclarations - We finished parsing the member
275 /// specification of a top (non-nested) C++ class. Now go over the
276 /// stack of method declarations with some parts for which parsing was
277 /// delayed (such as default arguments) and parse them.
278 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
279 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
280 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
281 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
282 if (HasTemplateScope) {
283 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
284 ++CurTemplateDepthTracker;
287 // The current scope is still active if we're the top-level class.
288 // Otherwise we'll need to push and enter a new scope.
289 bool HasClassScope = !Class.TopLevelClass;
290 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
293 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
295 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
296 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
300 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
303 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
304 // If this is a member template, introduce the template parameter scope.
305 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
306 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
307 if (LM.TemplateScope) {
308 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
309 ++CurTemplateDepthTracker;
311 // Start the delayed C++ method declaration
312 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
314 // Introduce the parameters into scope and parse their default
316 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
317 Scope::FunctionDeclarationScope | Scope::DeclScope);
318 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
319 // Introduce the parameter into scope.
320 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
321 LM.DefaultArgs[I].Param);
323 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
324 // Save the current token position.
325 SourceLocation origLoc = Tok.getLocation();
327 // Parse the default argument from its saved token stream.
328 Toks->push_back(Tok); // So that the current token doesn't get lost
329 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
331 // Consume the previously-pushed token.
335 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
336 SourceLocation EqualLoc = ConsumeToken();
338 // The argument isn't actually potentially evaluated unless it is
340 EnterExpressionEvaluationContext Eval(Actions,
341 Sema::PotentiallyEvaluatedIfUsed,
342 LM.DefaultArgs[I].Param);
344 ExprResult DefArgResult;
345 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
346 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
347 DefArgResult = ParseBraceInitializer();
349 DefArgResult = ParseAssignmentExpression();
350 if (DefArgResult.isInvalid())
351 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
353 if (Tok.is(tok::cxx_defaultarg_end))
356 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
357 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
358 DefArgResult.take());
361 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
362 Tok.getLocation()) &&
363 "ParseAssignmentExpression went over the default arg tokens!");
364 // There could be leftover tokens (e.g. because of an error).
365 // Skip through until we reach the original token position.
366 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
370 LM.DefaultArgs[I].Toks = 0;
374 PrototypeScope.Exit();
376 // Finish the delayed C++ method declaration.
377 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
380 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
381 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
382 /// collected during its parsing and parse them all.
383 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
384 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
385 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
386 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
387 if (HasTemplateScope) {
388 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
389 ++CurTemplateDepthTracker;
391 bool HasClassScope = !Class.TopLevelClass;
392 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
395 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
396 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
400 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
401 // If this is a member template, introduce the template parameter scope.
402 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
403 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
404 if (LM.TemplateScope) {
405 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
406 ++CurTemplateDepthTracker;
408 // Save the current token position.
409 SourceLocation origLoc = Tok.getLocation();
411 assert(!LM.Toks.empty() && "Empty body!");
412 // Append the current token at the end of the new token stream so that it
414 LM.Toks.push_back(Tok);
415 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
417 // Consume the previously pushed token.
418 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
419 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
420 && "Inline method not starting with '{', ':' or 'try'");
422 // Parse the method body. Function body parsing code is similar enough
423 // to be re-used for method bodies as well.
424 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
425 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
427 if (Tok.is(tok::kw_try)) {
428 ParseFunctionTryBlock(LM.D, FnScope);
429 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
430 Tok.getLocation()) &&
431 "ParseFunctionTryBlock went over the cached tokens!");
432 // There could be leftover tokens (e.g. because of an error).
433 // Skip through until we reach the original token position.
434 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
438 if (Tok.is(tok::colon)) {
439 ParseConstructorInitializer(LM.D);
442 if (!Tok.is(tok::l_brace)) {
444 Actions.ActOnFinishFunctionBody(LM.D, 0);
445 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
450 Actions.ActOnDefaultCtorInitializers(LM.D);
452 assert((Actions.getDiagnostics().hasErrorOccurred() ||
453 !isa<FunctionTemplateDecl>(LM.D) ||
454 cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
455 < TemplateParameterDepth) &&
456 "TemplateParameterDepth should be greater than the depth of "
457 "current template being instantiated!");
459 ParseFunctionStatementBody(LM.D, FnScope);
461 // Clear the late-template-parsed bit if we set it before.
462 if (LM.D) getFunctionDecl(LM.D)->setLateTemplateParsed(false);
464 if (Tok.getLocation() != origLoc) {
465 // Due to parsing error, we either went over the cached tokens or
466 // there are still cached tokens left. If it's the latter case skip the
468 // Since this is an uncommon situation that should be avoided, use the
469 // expensive isBeforeInTranslationUnit call.
470 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
472 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
477 /// ParseLexedMemberInitializers - We finished parsing the member specification
478 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
479 /// initializers that were collected during its parsing and parse them all.
480 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
481 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
482 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
484 TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
485 if (HasTemplateScope) {
486 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
487 ++CurTemplateDepthTracker;
489 // Set or update the scope flags.
490 bool AlreadyHasClassScope = Class.TopLevelClass;
491 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
492 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
493 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
495 if (!AlreadyHasClassScope)
496 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
497 Class.TagOrTemplate);
499 if (!Class.LateParsedDeclarations.empty()) {
500 // C++11 [expr.prim.general]p4:
501 // Otherwise, if a member-declarator declares a non-static data member
502 // (9.2) of a class X, the expression this is a prvalue of type "pointer
503 // to X" within the optional brace-or-equal-initializer. It shall not
504 // appear elsewhere in the member-declarator.
505 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
506 /*TypeQuals=*/(unsigned)0);
508 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
509 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
513 if (!AlreadyHasClassScope)
514 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
515 Class.TagOrTemplate);
517 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
520 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
521 if (!MI.Field || MI.Field->isInvalidDecl())
524 // Append the current token at the end of the new token stream so that it
526 MI.Toks.push_back(Tok);
527 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
529 // Consume the previously pushed token.
530 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
532 SourceLocation EqualLoc;
534 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
537 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
539 // The next token should be our artificial terminating EOF token.
540 if (Tok.isNot(tok::eof)) {
541 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
542 if (!EndLoc.isValid())
543 EndLoc = Tok.getLocation();
544 // No fixit; we can't recover as if there were a semicolon here.
545 Diag(EndLoc, diag::err_expected_semi_decl_list);
547 // Consume tokens until we hit the artificial EOF.
548 while (Tok.isNot(tok::eof))
554 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
555 /// container until the token 'T' is reached (which gets
556 /// consumed/stored too, if ConsumeFinalToken).
557 /// If StopAtSemi is true, then we will stop early at a ';' character.
558 /// Returns true if token 'T1' or 'T2' was found.
559 /// NOTE: This is a specialized version of Parser::SkipUntil.
560 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
562 bool StopAtSemi, bool ConsumeFinalToken) {
563 // We always want this function to consume at least one token if the first
564 // token isn't T and if not at EOF.
565 bool isFirstTokenConsumed = true;
567 // If we found one of the tokens, stop and return true.
568 if (Tok.is(T1) || Tok.is(T2)) {
569 if (ConsumeFinalToken) {
576 switch (Tok.getKind()) {
578 // Ran out of tokens.
582 // Recursively consume properly-nested parens.
585 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
588 // Recursively consume properly-nested square brackets.
591 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
594 // Recursively consume properly-nested braces.
597 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
600 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
601 // Since the user wasn't looking for this token (if they were, it would
602 // already be handled), this isn't balanced. If there is a LHS token at a
603 // higher level, we will assume that this matches the unbalanced token
604 // and return it. Otherwise, this is a spurious RHS token, which we skip.
606 if (ParenCount && !isFirstTokenConsumed)
607 return false; // Matches something.
612 if (BracketCount && !isFirstTokenConsumed)
613 return false; // Matches something.
618 if (BraceCount && !isFirstTokenConsumed)
619 return false; // Matches something.
624 case tok::code_completion:
626 ConsumeCodeCompletionToken();
629 case tok::string_literal:
630 case tok::wide_string_literal:
631 case tok::utf8_string_literal:
632 case tok::utf16_string_literal:
633 case tok::utf32_string_literal:
635 ConsumeStringToken();
642 // consume this token.
647 isFirstTokenConsumed = false;
651 /// \brief Consume tokens and store them in the passed token container until
652 /// we've passed the try keyword and constructor initializers and have consumed
653 /// the opening brace of the function body. The opening brace will be consumed
654 /// if and only if there was no error.
656 /// \return True on error.
657 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
658 if (Tok.is(tok::kw_try)) {
662 bool ReadInitializer = false;
663 if (Tok.is(tok::colon)) {
664 // Initializers can contain braces too.
668 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
669 if (Tok.is(tok::eof) || Tok.is(tok::semi))
670 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
672 // Grab the identifier.
673 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
675 /*ConsumeFinalToken=*/false))
676 return Diag(Tok.getLocation(), diag::err_expected_lparen);
678 tok::TokenKind kind = Tok.getKind();
680 bool IsLParen = (kind == tok::l_paren);
681 SourceLocation LOpen = Tok.getLocation();
686 assert(kind == tok::l_brace && "Must be left paren or brace here.");
688 // In C++03, this has to be the start of the function body, which
689 // means the initializer is malformed; we'll diagnose it later.
690 if (!getLangOpts().CPlusPlus11)
694 // Grab the initializer
695 if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
696 Toks, /*StopAtSemi=*/true)) {
697 Diag(Tok, IsLParen ? diag::err_expected_rparen :
698 diag::err_expected_rbrace);
699 Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
703 // Grab pack ellipsis, if present
704 if (Tok.is(tok::ellipsis)) {
709 // Grab the separating comma, if any.
710 if (Tok.is(tok::comma)) {
713 } else if (Tok.isNot(tok::l_brace)) {
714 ReadInitializer = true;
720 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
721 // brace: an opening one is the function body, while a closing one probably
722 // means we've reached the end of the class.
723 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
725 /*ConsumeFinalToken=*/false);
726 if (Tok.isNot(tok::l_brace)) {
728 return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
729 return Diag(Tok.getLocation(), diag::err_expected_lbrace);