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/ParseDiagnostic.h"
15 #include "clang/Parse/Parser.h"
16 #include "clang/Sema/DeclSpec.h"
17 #include "clang/Sema/Scope.h"
18 #include "clang/AST/DeclTemplate.h"
19 using namespace clang;
21 /// ParseCXXInlineMethodDef - We parsed and verified that the specified
22 /// Declarator is a well formed C++ inline method definition. Now lex its body
23 /// and store its tokens for parsing after the C++ class is complete.
24 Decl *Parser::ParseCXXInlineMethodDef(AccessSpecifier AS,
25 AttributeList *AccessAttrs,
27 const ParsedTemplateInfo &TemplateInfo,
28 const VirtSpecifiers& VS,
29 FunctionDefinitionKind DefinitionKind,
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(Actions,
37 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
38 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
41 D.setFunctionDefinitionKind(DefinitionKind);
42 if (D.getDeclSpec().isFriendSpecified())
43 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
44 move(TemplateParams));
46 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
47 move(TemplateParams), 0,
48 VS, /*HasDeferredInit=*/false);
50 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
52 bool TypeSpecContainsAuto
53 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
55 Actions.AddInitializerToDecl(FnD, Init.get(), false,
56 TypeSpecContainsAuto);
58 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
62 HandleMemberFunctionDefaultArgs(D, FnD);
66 if (Tok.is(tok::equal)) {
76 if (Tok.is(tok::kw_delete)) {
77 Diag(Tok, getLangOpts().CPlusPlus0x ?
78 diag::warn_cxx98_compat_deleted_function :
79 diag::ext_deleted_function);
81 KWLoc = ConsumeToken();
82 Actions.SetDeclDeleted(FnD, KWLoc);
84 } else if (Tok.is(tok::kw_default)) {
85 Diag(Tok, getLangOpts().CPlusPlus0x ?
86 diag::warn_cxx98_compat_defaulted_function :
87 diag::ext_defaulted_function);
89 KWLoc = ConsumeToken();
90 Actions.SetDeclDefaulted(FnD, KWLoc);
92 llvm_unreachable("function definition after = not 'delete' or 'default'");
95 if (Tok.is(tok::comma)) {
96 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
100 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
101 Delete ? "delete" : "default", tok::semi);
107 // In delayed template parsing mode, if we are within a class template
108 // or if we are about to parse function member template then consume
109 // the tokens and store them for parsing at the end of the translation unit.
110 if (getLangOpts().DelayedTemplateParsing &&
111 ((Actions.CurContext->isDependentContext() ||
112 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
113 !Actions.IsInsideALocalClassWithinATemplateFunction())) {
116 LateParsedTemplatedFunction *LPT = new LateParsedTemplatedFunction(FnD);
118 FunctionDecl *FD = 0;
119 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
120 FD = FunTmpl->getTemplatedDecl();
122 FD = cast<FunctionDecl>(FnD);
123 Actions.CheckForFunctionRedefinition(FD);
125 LateParsedTemplateMap[FD] = LPT;
126 Actions.MarkAsLateParsedTemplate(FD);
127 LexTemplateFunctionForLateParsing(LPT->Toks);
130 LexTemplateFunctionForLateParsing(Toks);
136 // Consume the tokens and store them for later parsing.
138 LexedMethod* LM = new LexedMethod(this, FnD);
139 getCurrentClass().LateParsedDeclarations.push_back(LM);
140 LM->TemplateScope = getCurScope()->isTemplateParamScope();
141 CachedTokens &Toks = LM->Toks;
143 tok::TokenKind kind = Tok.getKind();
144 // Consume everything up to (and including) the left brace of the
146 if (ConsumeAndStoreFunctionPrologue(Toks)) {
147 // We didn't find the left-brace we expected after the
148 // constructor initializer; we already printed an error, and it's likely
149 // impossible to recover, so don't try to parse this method later.
150 // If we stopped at a semicolon, consume it to avoid an extra warning.
151 if (Tok.is(tok::semi))
153 delete getCurrentClass().LateParsedDeclarations.back();
154 getCurrentClass().LateParsedDeclarations.pop_back();
157 // Consume everything up to (and including) the matching right brace.
158 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
161 // If we're in a function-try-block, we need to store all the catch blocks.
162 if (kind == tok::kw_try) {
163 while (Tok.is(tok::kw_catch)) {
164 ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
165 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
171 // If semantic analysis could not build a function declaration,
172 // just throw away the late-parsed declaration.
173 delete getCurrentClass().LateParsedDeclarations.back();
174 getCurrentClass().LateParsedDeclarations.pop_back();
180 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
181 /// specified Declarator is a well formed C++ non-static data member
182 /// declaration. Now lex its initializer and store its tokens for parsing
183 /// after the class is complete.
184 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
185 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
186 "Current token not a '{' or '='!");
188 LateParsedMemberInitializer *MI =
189 new LateParsedMemberInitializer(this, VarD);
190 getCurrentClass().LateParsedDeclarations.push_back(MI);
191 CachedTokens &Toks = MI->Toks;
193 tok::TokenKind kind = Tok.getKind();
194 if (kind == tok::equal) {
199 if (kind == tok::l_brace) {
200 // Begin by storing the '{' token.
204 // Consume everything up to (and including) the matching right brace.
205 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
207 // Consume everything up to (but excluding) the comma or semicolon.
208 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
209 /*ConsumeFinalToken=*/false);
212 // Store an artificial EOF token to ensure that we don't run off the end of
213 // the initializer when we come to parse it.
216 Eof.setKind(tok::eof);
217 Eof.setLocation(Tok.getLocation());
221 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
222 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
223 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
224 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
226 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
227 : Self(P), Class(C) {}
229 Parser::LateParsedClass::~LateParsedClass() {
230 Self->DeallocateParsedClasses(Class);
233 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
234 Self->ParseLexedMethodDeclarations(*Class);
237 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
238 Self->ParseLexedMemberInitializers(*Class);
241 void Parser::LateParsedClass::ParseLexedMethodDefs() {
242 Self->ParseLexedMethodDefs(*Class);
245 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
246 Self->ParseLexedMethodDeclaration(*this);
249 void Parser::LexedMethod::ParseLexedMethodDefs() {
250 Self->ParseLexedMethodDef(*this);
253 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
254 Self->ParseLexedMemberInitializer(*this);
257 /// ParseLexedMethodDeclarations - We finished parsing the member
258 /// specification of a top (non-nested) C++ class. Now go over the
259 /// stack of method declarations with some parts for which parsing was
260 /// delayed (such as default arguments) and parse them.
261 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
262 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
263 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
264 if (HasTemplateScope)
265 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
267 // The current scope is still active if we're the top-level class.
268 // Otherwise we'll need to push and enter a new scope.
269 bool HasClassScope = !Class.TopLevelClass;
270 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
273 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
275 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
276 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
280 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
283 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
284 // If this is a member template, introduce the template parameter scope.
285 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
286 if (LM.TemplateScope)
287 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
289 // Start the delayed C++ method declaration
290 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
292 // Introduce the parameters into scope and parse their default
294 ParseScope PrototypeScope(this,
295 Scope::FunctionPrototypeScope|Scope::DeclScope);
296 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
297 // Introduce the parameter into scope.
298 Actions.ActOnDelayedCXXMethodParameter(getCurScope(),
299 LM.DefaultArgs[I].Param);
301 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
302 // Save the current token position.
303 SourceLocation origLoc = Tok.getLocation();
305 // Parse the default argument from its saved token stream.
306 Toks->push_back(Tok); // So that the current token doesn't get lost
307 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
309 // Consume the previously-pushed token.
313 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
314 SourceLocation EqualLoc = ConsumeToken();
316 // The argument isn't actually potentially evaluated unless it is
318 EnterExpressionEvaluationContext Eval(Actions,
319 Sema::PotentiallyEvaluatedIfUsed,
320 LM.DefaultArgs[I].Param);
322 ExprResult DefArgResult;
323 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
324 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
325 DefArgResult = ParseBraceInitializer();
327 DefArgResult = ParseAssignmentExpression();
328 if (DefArgResult.isInvalid())
329 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
331 if (Tok.is(tok::cxx_defaultarg_end))
334 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
335 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
336 DefArgResult.take());
339 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
340 Tok.getLocation()) &&
341 "ParseAssignmentExpression went over the default arg tokens!");
342 // There could be leftover tokens (e.g. because of an error).
343 // Skip through until we reach the original token position.
344 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
348 LM.DefaultArgs[I].Toks = 0;
351 PrototypeScope.Exit();
353 // Finish the delayed C++ method declaration.
354 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
357 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
358 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
359 /// collected during its parsing and parse them all.
360 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
361 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
362 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
363 if (HasTemplateScope)
364 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
366 bool HasClassScope = !Class.TopLevelClass;
367 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
370 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
371 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
375 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
376 // If this is a member template, introduce the template parameter scope.
377 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
378 if (LM.TemplateScope)
379 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
381 // Save the current token position.
382 SourceLocation origLoc = Tok.getLocation();
384 assert(!LM.Toks.empty() && "Empty body!");
385 // Append the current token at the end of the new token stream so that it
387 LM.Toks.push_back(Tok);
388 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
390 // Consume the previously pushed token.
392 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
393 && "Inline method not starting with '{', ':' or 'try'");
395 // Parse the method body. Function body parsing code is similar enough
396 // to be re-used for method bodies as well.
397 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
398 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
400 if (Tok.is(tok::kw_try)) {
401 ParseFunctionTryBlock(LM.D, FnScope);
402 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
403 Tok.getLocation()) &&
404 "ParseFunctionTryBlock went over the cached tokens!");
405 // There could be leftover tokens (e.g. because of an error).
406 // Skip through until we reach the original token position.
407 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
411 if (Tok.is(tok::colon)) {
412 ParseConstructorInitializer(LM.D);
415 if (!Tok.is(tok::l_brace)) {
417 Actions.ActOnFinishFunctionBody(LM.D, 0);
418 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
423 Actions.ActOnDefaultCtorInitializers(LM.D);
425 ParseFunctionStatementBody(LM.D, FnScope);
427 if (Tok.getLocation() != origLoc) {
428 // Due to parsing error, we either went over the cached tokens or
429 // there are still cached tokens left. If it's the latter case skip the
431 // Since this is an uncommon situation that should be avoided, use the
432 // expensive isBeforeInTranslationUnit call.
433 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
435 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
440 /// ParseLexedMemberInitializers - We finished parsing the member specification
441 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
442 /// initializers that were collected during its parsing and parse them all.
443 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
444 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
445 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
447 if (HasTemplateScope)
448 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
450 // Set or update the scope flags to include Scope::ThisScope.
451 bool AlreadyHasClassScope = Class.TopLevelClass;
452 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
453 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
454 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
456 if (!AlreadyHasClassScope)
457 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
458 Class.TagOrTemplate);
460 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
461 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
464 if (!AlreadyHasClassScope)
465 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
466 Class.TagOrTemplate);
468 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
471 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
472 if (!MI.Field || MI.Field->isInvalidDecl())
475 // Append the current token at the end of the new token stream so that it
477 MI.Toks.push_back(Tok);
478 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
480 // Consume the previously pushed token.
483 SourceLocation EqualLoc;
484 ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
487 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
489 // The next token should be our artificial terminating EOF token.
490 if (Tok.isNot(tok::eof)) {
491 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
492 if (!EndLoc.isValid())
493 EndLoc = Tok.getLocation();
494 // No fixit; we can't recover as if there were a semicolon here.
495 Diag(EndLoc, diag::err_expected_semi_decl_list);
497 // Consume tokens until we hit the artificial EOF.
498 while (Tok.isNot(tok::eof))
504 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
505 /// container until the token 'T' is reached (which gets
506 /// consumed/stored too, if ConsumeFinalToken).
507 /// If StopAtSemi is true, then we will stop early at a ';' character.
508 /// Returns true if token 'T1' or 'T2' was found.
509 /// NOTE: This is a specialized version of Parser::SkipUntil.
510 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
512 bool StopAtSemi, bool ConsumeFinalToken) {
513 // We always want this function to consume at least one token if the first
514 // token isn't T and if not at EOF.
515 bool isFirstTokenConsumed = true;
517 // If we found one of the tokens, stop and return true.
518 if (Tok.is(T1) || Tok.is(T2)) {
519 if (ConsumeFinalToken) {
526 switch (Tok.getKind()) {
528 // Ran out of tokens.
532 // Recursively consume properly-nested parens.
535 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
538 // Recursively consume properly-nested square brackets.
541 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
544 // Recursively consume properly-nested braces.
547 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
550 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
551 // Since the user wasn't looking for this token (if they were, it would
552 // already be handled), this isn't balanced. If there is a LHS token at a
553 // higher level, we will assume that this matches the unbalanced token
554 // and return it. Otherwise, this is a spurious RHS token, which we skip.
556 if (ParenCount && !isFirstTokenConsumed)
557 return false; // Matches something.
562 if (BracketCount && !isFirstTokenConsumed)
563 return false; // Matches something.
568 if (BraceCount && !isFirstTokenConsumed)
569 return false; // Matches something.
574 case tok::code_completion:
576 ConsumeCodeCompletionToken();
579 case tok::string_literal:
580 case tok::wide_string_literal:
581 case tok::utf8_string_literal:
582 case tok::utf16_string_literal:
583 case tok::utf32_string_literal:
585 ConsumeStringToken();
592 // consume this token.
597 isFirstTokenConsumed = false;
601 /// \brief Consume tokens and store them in the passed token container until
602 /// we've passed the try keyword and constructor initializers and have consumed
603 /// the opening brace of the function body. The opening brace will be consumed
604 /// if and only if there was no error.
606 /// \return True on error.
607 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
608 if (Tok.is(tok::kw_try)) {
612 bool ReadInitializer = false;
613 if (Tok.is(tok::colon)) {
614 // Initializers can contain braces too.
618 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
619 if (Tok.is(tok::eof) || Tok.is(tok::semi))
620 return Diag(Tok.getLocation(), diag::err_expected_lbrace);
622 // Grab the identifier.
623 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
625 /*ConsumeFinalToken=*/false))
626 return Diag(Tok.getLocation(), diag::err_expected_lparen);
628 tok::TokenKind kind = Tok.getKind();
630 bool IsLParen = (kind == tok::l_paren);
631 SourceLocation LOpen = Tok.getLocation();
636 assert(kind == tok::l_brace && "Must be left paren or brace here.");
638 // In C++03, this has to be the start of the function body, which
639 // means the initializer is malformed; we'll diagnose it later.
640 if (!getLangOpts().CPlusPlus0x)
644 // Grab the initializer
645 if (!ConsumeAndStoreUntil(IsLParen ? tok::r_paren : tok::r_brace,
646 Toks, /*StopAtSemi=*/true)) {
647 Diag(Tok, IsLParen ? diag::err_expected_rparen :
648 diag::err_expected_rbrace);
649 Diag(LOpen, diag::note_matching) << (IsLParen ? "(" : "{");
653 // Grab pack ellipsis, if present
654 if (Tok.is(tok::ellipsis)) {
659 // Grab the separating comma, if any.
660 if (Tok.is(tok::comma)) {
663 } else if (Tok.isNot(tok::l_brace)) {
664 ReadInitializer = true;
670 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
671 // brace: an opening one is the function body, while a closing one probably
672 // means we've reached the end of the class.
673 ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
675 /*ConsumeFinalToken=*/false);
676 if (Tok.isNot(tok::l_brace)) {
678 return Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
679 return Diag(Tok.getLocation(), diag::err_expected_lbrace);