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, ExprResult& Init) {
29 assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
30 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try) ||
31 Tok.is(tok::equal)) &&
32 "Current token not a '{', ':', '=', or 'try'!");
34 MultiTemplateParamsArg TemplateParams(Actions,
35 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() : 0,
36 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
39 D.setFunctionDefinition(true);
40 if (D.getDeclSpec().isFriendSpecified())
41 FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
42 move(TemplateParams));
44 FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
45 move(TemplateParams), 0,
46 VS, /*HasInit=*/false);
48 Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs,
50 bool TypeSpecContainsAuto
51 = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
53 Actions.AddInitializerToDecl(FnD, Init.get(), false,
54 TypeSpecContainsAuto);
56 Actions.ActOnUninitializedDecl(FnD, TypeSpecContainsAuto);
60 HandleMemberFunctionDefaultArgs(D, FnD);
64 if (Tok.is(tok::equal)) {
69 if (Tok.is(tok::kw_delete)) {
70 if (!getLang().CPlusPlus0x)
71 Diag(Tok, diag::warn_deleted_function_accepted_as_extension);
73 KWLoc = ConsumeToken();
74 Actions.SetDeclDeleted(FnD, KWLoc);
76 } else if (Tok.is(tok::kw_default)) {
77 if (!getLang().CPlusPlus0x)
78 Diag(Tok, diag::warn_defaulted_function_accepted_as_extension);
80 KWLoc = ConsumeToken();
81 Actions.SetDeclDefaulted(FnD, KWLoc);
83 llvm_unreachable("function definition after = not 'delete' or 'default'");
86 if (Tok.is(tok::comma)) {
87 Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
91 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
92 Delete ? "delete" : "default", tok::semi);
98 // In delayed template parsing mode, if we are within a class template
99 // or if we are about to parse function member template then consume
100 // the tokens and store them for parsing at the end of the translation unit.
101 if (getLang().DelayedTemplateParsing &&
102 ((Actions.CurContext->isDependentContext() ||
103 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) &&
104 !Actions.IsInsideALocalClassWithinATemplateFunction()) &&
105 !D.getDeclSpec().isFriendSpecified()) {
108 LateParsedTemplatedFunction *LPT =
109 new LateParsedTemplatedFunction(this, FnD);
111 FunctionDecl *FD = 0;
112 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(FnD))
113 FD = FunTmpl->getTemplatedDecl();
115 FD = cast<FunctionDecl>(FnD);
116 Actions.CheckForFunctionRedefinition(FD);
118 LateParsedTemplateMap[FD] = LPT;
119 Actions.MarkAsLateParsedTemplate(FD);
120 LexTemplateFunctionForLateParsing(LPT->Toks);
123 LexTemplateFunctionForLateParsing(Toks);
129 // Consume the tokens and store them for later parsing.
131 LexedMethod* LM = new LexedMethod(this, FnD);
132 getCurrentClass().LateParsedDeclarations.push_back(LM);
133 LM->TemplateScope = getCurScope()->isTemplateParamScope();
134 CachedTokens &Toks = LM->Toks;
136 tok::TokenKind kind = Tok.getKind();
137 // Consume everything up to (and including) the left brace of the
139 if (ConsumeAndStoreFunctionPrologue(Toks)) {
140 // We didn't find the left-brace we expected after the
141 // constructor initializer.
142 if (Tok.is(tok::semi)) {
143 // We found a semicolon; complain, consume the semicolon, and
144 // don't try to parse this method later.
145 Diag(Tok.getLocation(), diag::err_expected_lbrace);
147 delete getCurrentClass().LateParsedDeclarations.back();
148 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);
166 // If semantic analysis could not build a function declaration,
167 // just throw away the late-parsed declaration.
168 delete getCurrentClass().LateParsedDeclarations.back();
169 getCurrentClass().LateParsedDeclarations.pop_back();
175 /// ParseCXXNonStaticMemberInitializer - We parsed and verified that the
176 /// specified Declarator is a well formed C++ non-static data member
177 /// declaration. Now lex its initializer and store its tokens for parsing
178 /// after the class is complete.
179 void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
180 assert((Tok.is(tok::l_brace) || Tok.is(tok::equal)) &&
181 "Current token not a '{' or '='!");
183 LateParsedMemberInitializer *MI =
184 new LateParsedMemberInitializer(this, VarD);
185 getCurrentClass().LateParsedDeclarations.push_back(MI);
186 CachedTokens &Toks = MI->Toks;
188 tok::TokenKind kind = Tok.getKind();
189 if (kind == tok::equal) {
194 if (kind == tok::l_brace) {
195 // Begin by storing the '{' token.
199 // Consume everything up to (and including) the matching right brace.
200 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
202 // Consume everything up to (but excluding) the comma or semicolon.
203 ConsumeAndStoreUntil(tok::comma, Toks, /*StopAtSemi=*/true,
204 /*ConsumeFinalToken=*/false);
207 // Store an artificial EOF token to ensure that we don't run off the end of
208 // the initializer when we come to parse it.
211 Eof.setKind(tok::eof);
212 Eof.setLocation(Tok.getLocation());
216 Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
217 void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
218 void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
219 void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
221 Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
222 : Self(P), Class(C) {}
224 Parser::LateParsedClass::~LateParsedClass() {
225 Self->DeallocateParsedClasses(Class);
228 void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
229 Self->ParseLexedMethodDeclarations(*Class);
232 void Parser::LateParsedClass::ParseLexedMemberInitializers() {
233 Self->ParseLexedMemberInitializers(*Class);
236 void Parser::LateParsedClass::ParseLexedMethodDefs() {
237 Self->ParseLexedMethodDefs(*Class);
240 void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
241 Self->ParseLexedMethodDeclaration(*this);
244 void Parser::LexedMethod::ParseLexedMethodDefs() {
245 Self->ParseLexedMethodDef(*this);
248 void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
249 Self->ParseLexedMemberInitializer(*this);
252 /// ParseLexedMethodDeclarations - We finished parsing the member
253 /// specification of a top (non-nested) C++ class. Now go over the
254 /// stack of method declarations with some parts for which parsing was
255 /// delayed (such as default arguments) and parse them.
256 void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
257 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
258 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
259 if (HasTemplateScope)
260 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
262 // The current scope is still active if we're the top-level class.
263 // Otherwise we'll need to push and enter a new scope.
264 bool HasClassScope = !Class.TopLevelClass;
265 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
268 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
270 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
271 Class.LateParsedDeclarations[i]->ParseLexedMethodDeclarations();
275 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), Class.TagOrTemplate);
278 void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
279 // If this is a member template, introduce the template parameter scope.
280 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
281 if (LM.TemplateScope)
282 Actions.ActOnReenterTemplateScope(getCurScope(), LM.Method);
284 // Start the delayed C++ method declaration
285 Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
287 // Introduce the parameters into scope and parse their default
289 ParseScope PrototypeScope(this,
290 Scope::FunctionPrototypeScope|Scope::DeclScope);
291 for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
292 // Introduce the parameter into scope.
293 Actions.ActOnDelayedCXXMethodParameter(getCurScope(), LM.DefaultArgs[I].Param);
295 if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
296 // Save the current token position.
297 SourceLocation origLoc = Tok.getLocation();
299 // Parse the default argument from its saved token stream.
300 Toks->push_back(Tok); // So that the current token doesn't get lost
301 PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
303 // Consume the previously-pushed token.
307 assert(Tok.is(tok::equal) && "Default argument not starting with '='");
308 SourceLocation EqualLoc = ConsumeToken();
310 // The argument isn't actually potentially evaluated unless it is
312 EnterExpressionEvaluationContext Eval(Actions,
313 Sema::PotentiallyEvaluatedIfUsed);
315 ExprResult DefArgResult(ParseAssignmentExpression());
316 if (DefArgResult.isInvalid())
317 Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
319 if (Tok.is(tok::cxx_defaultarg_end))
322 Diag(Tok.getLocation(), diag::err_default_arg_unparsed);
323 Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
324 DefArgResult.take());
327 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
328 Tok.getLocation()) &&
329 "ParseAssignmentExpression went over the default arg tokens!");
330 // There could be leftover tokens (e.g. because of an error).
331 // Skip through until we reach the original token position.
332 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
336 LM.DefaultArgs[I].Toks = 0;
339 PrototypeScope.Exit();
341 // Finish the delayed C++ method declaration.
342 Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
345 /// ParseLexedMethodDefs - We finished parsing the member specification of a top
346 /// (non-nested) C++ class. Now go over the stack of lexed methods that were
347 /// collected during its parsing and parse them all.
348 void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
349 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
350 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
351 if (HasTemplateScope)
352 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
354 bool HasClassScope = !Class.TopLevelClass;
355 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
358 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
359 Class.LateParsedDeclarations[i]->ParseLexedMethodDefs();
363 void Parser::ParseLexedMethodDef(LexedMethod &LM) {
364 // If this is a member template, introduce the template parameter scope.
365 ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
366 if (LM.TemplateScope)
367 Actions.ActOnReenterTemplateScope(getCurScope(), LM.D);
369 // Save the current token position.
370 SourceLocation origLoc = Tok.getLocation();
372 assert(!LM.Toks.empty() && "Empty body!");
373 // Append the current token at the end of the new token stream so that it
375 LM.Toks.push_back(Tok);
376 PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
378 // Consume the previously pushed token.
380 assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
381 && "Inline method not starting with '{', ':' or 'try'");
383 // Parse the method body. Function body parsing code is similar enough
384 // to be re-used for method bodies as well.
385 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
386 Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
388 if (Tok.is(tok::kw_try)) {
389 ParseFunctionTryBlock(LM.D, FnScope);
390 assert(!PP.getSourceManager().isBeforeInTranslationUnit(origLoc,
391 Tok.getLocation()) &&
392 "ParseFunctionTryBlock went over the cached tokens!");
393 // There could be leftover tokens (e.g. because of an error).
394 // Skip through until we reach the original token position.
395 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
399 if (Tok.is(tok::colon)) {
400 ParseConstructorInitializer(LM.D);
403 if (!Tok.is(tok::l_brace)) {
405 Actions.ActOnFinishFunctionBody(LM.D, 0);
406 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
411 Actions.ActOnDefaultCtorInitializers(LM.D);
413 ParseFunctionStatementBody(LM.D, FnScope);
415 if (Tok.getLocation() != origLoc) {
416 // Due to parsing error, we either went over the cached tokens or
417 // there are still cached tokens left. If it's the latter case skip the
419 // Since this is an uncommon situation that should be avoided, use the
420 // expensive isBeforeInTranslationUnit call.
421 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
423 while (Tok.getLocation() != origLoc && Tok.isNot(tok::eof))
428 /// ParseLexedMemberInitializers - We finished parsing the member specification
429 /// of a top (non-nested) C++ class. Now go over the stack of lexed data member
430 /// initializers that were collected during its parsing and parse them all.
431 void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
432 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
433 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
435 if (HasTemplateScope)
436 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
438 // Set or update the scope flags to include Scope::ThisScope.
439 bool AlreadyHasClassScope = Class.TopLevelClass;
440 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
441 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
442 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
444 if (!AlreadyHasClassScope)
445 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
446 Class.TagOrTemplate);
448 for (size_t i = 0; i < Class.LateParsedDeclarations.size(); ++i) {
449 Class.LateParsedDeclarations[i]->ParseLexedMemberInitializers();
452 if (!AlreadyHasClassScope)
453 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
454 Class.TagOrTemplate);
456 Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
459 void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
460 if (!MI.Field || MI.Field->isInvalidDecl())
463 // Append the current token at the end of the new token stream so that it
465 MI.Toks.push_back(Tok);
466 PP.EnterTokenStream(MI.Toks.data(), MI.Toks.size(), true, false);
468 // Consume the previously pushed token.
471 SourceLocation EqualLoc;
472 ExprResult Init = ParseCXXMemberInitializer(/*IsFunction=*/false, EqualLoc);
474 Actions.ActOnCXXInClassMemberInitializer(MI.Field, EqualLoc, Init.release());
476 // The next token should be our artificial terminating EOF token.
477 if (Tok.isNot(tok::eof)) {
478 SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
479 if (!EndLoc.isValid())
480 EndLoc = Tok.getLocation();
481 // No fixit; we can't recover as if there were a semicolon here.
482 Diag(EndLoc, diag::err_expected_semi_decl_list);
484 // Consume tokens until we hit the artificial EOF.
485 while (Tok.isNot(tok::eof))
491 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
492 /// container until the token 'T' is reached (which gets
493 /// consumed/stored too, if ConsumeFinalToken).
494 /// If StopAtSemi is true, then we will stop early at a ';' character.
495 /// Returns true if token 'T1' or 'T2' was found.
496 /// NOTE: This is a specialized version of Parser::SkipUntil.
497 bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
499 bool StopAtSemi, bool ConsumeFinalToken) {
500 // We always want this function to consume at least one token if the first
501 // token isn't T and if not at EOF.
502 bool isFirstTokenConsumed = true;
504 // If we found one of the tokens, stop and return true.
505 if (Tok.is(T1) || Tok.is(T2)) {
506 if (ConsumeFinalToken) {
513 switch (Tok.getKind()) {
515 // Ran out of tokens.
519 // Recursively consume properly-nested parens.
522 ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
525 // Recursively consume properly-nested square brackets.
528 ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
531 // Recursively consume properly-nested braces.
534 ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
537 // Okay, we found a ']' or '}' or ')', which we think should be balanced.
538 // Since the user wasn't looking for this token (if they were, it would
539 // already be handled), this isn't balanced. If there is a LHS token at a
540 // higher level, we will assume that this matches the unbalanced token
541 // and return it. Otherwise, this is a spurious RHS token, which we skip.
543 if (ParenCount && !isFirstTokenConsumed)
544 return false; // Matches something.
549 if (BracketCount && !isFirstTokenConsumed)
550 return false; // Matches something.
555 if (BraceCount && !isFirstTokenConsumed)
556 return false; // Matches something.
561 case tok::code_completion:
563 ConsumeCodeCompletionToken();
566 case tok::string_literal:
567 case tok::wide_string_literal:
568 case tok::utf8_string_literal:
569 case tok::utf16_string_literal:
570 case tok::utf32_string_literal:
572 ConsumeStringToken();
579 // consume this token.
584 isFirstTokenConsumed = false;
588 /// \brief Consume tokens and store them in the passed token container until
589 /// we've passed the try keyword and constructor initializers and have consumed
590 /// the opening brace of the function body. The opening brace will be consumed
591 /// if and only if there was no error.
593 /// \return True on error.
594 bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
595 if (Tok.is(tok::kw_try)) {
599 if (Tok.is(tok::colon)) {
600 // Initializers can contain braces too.
604 while (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
605 if (Tok.is(tok::eof) || Tok.is(tok::semi))
608 // Grab the identifier.
609 if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
611 /*ConsumeFinalToken=*/false))
614 tok::TokenKind kind = Tok.getKind();
616 if (kind == tok::l_paren)
619 assert(kind == tok::l_brace && "Must be left paren or brace here.");
621 // In C++03, this has to be the start of the function body, which
622 // means the initializer is malformed.
623 if (!getLang().CPlusPlus0x)
627 // Grab the initializer
628 if (!ConsumeAndStoreUntil(kind == tok::l_paren ? tok::r_paren :
630 Toks, /*StopAtSemi=*/true))
633 // Grab the separating comma, if any.
634 if (Tok.is(tok::comma)) {
641 // Grab any remaining garbage to be diagnosed later. We stop when we reach a
642 // brace: an opening one is the function body, while a closing one probably
643 // means we've reached the end of the class.
644 if (!ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
645 /*StopAtSemi=*/true, /*ConsumeFinalToken=*/false))
647 if(Tok.isNot(tok::l_brace))