2008-06-25 06:12:16 +08:00
|
|
|
//===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements parsing for C++ class inline methods.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/Parse/ParseDiagnostic.h"
|
2008-06-25 06:12:16 +08:00
|
|
|
#include "clang/Parse/Parser.h"
|
|
|
|
#include "clang/Parse/DeclSpec.h"
|
|
|
|
#include "clang/Parse/Scope.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
/// ParseCXXInlineMethodDef - We parsed and verified that the specified
|
2008-06-25 06:12:16 +08:00
|
|
|
/// Declarator is a well formed C++ inline method definition. Now lex its body
|
|
|
|
/// and store its tokens for parsing after the C++ class is complete.
|
2009-03-29 03:18:32 +08:00
|
|
|
Parser::DeclPtrTy
|
2009-08-21 06:52:58 +08:00
|
|
|
Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D,
|
|
|
|
const ParsedTemplateInfo &TemplateInfo) {
|
2008-06-25 06:12:16 +08:00
|
|
|
assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
|
|
|
|
"This isn't a function declarator!");
|
2009-04-27 04:35:05 +08:00
|
|
|
assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) &&
|
|
|
|
"Current token not a '{', ':' or 'try'!");
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2009-08-21 06:52:58 +08:00
|
|
|
Action::MultiTemplateParamsArg TemplateParams(Actions,
|
|
|
|
TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
|
|
|
|
TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
|
2009-08-06 10:15:43 +08:00
|
|
|
DeclPtrTy FnD;
|
|
|
|
if (D.getDeclSpec().isFriendSpecified())
|
2009-08-21 06:52:58 +08:00
|
|
|
// FIXME: Friend templates
|
2009-09-12 05:02:39 +08:00
|
|
|
FnD = Actions.ActOnFriendFunctionDecl(CurScope, D, true, move(TemplateParams));
|
2009-08-21 06:52:58 +08:00
|
|
|
else // FIXME: pass template information through
|
2009-09-09 23:08:12 +08:00
|
|
|
FnD = Actions.ActOnCXXMemberDeclarator(CurScope, AS, D,
|
2009-11-25 07:38:44 +08:00
|
|
|
move(TemplateParams), 0, 0,
|
|
|
|
/*IsDefinition*/true);
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2009-07-23 05:45:50 +08:00
|
|
|
HandleMemberFunctionDefaultArgs(D, FnD);
|
|
|
|
|
2008-06-25 06:12:16 +08:00
|
|
|
// Consume the tokens and store them for later parsing.
|
|
|
|
|
2009-05-28 07:11:45 +08:00
|
|
|
getCurrentClass().MethodDefs.push_back(LexedMethod(FnD));
|
2009-09-09 23:08:12 +08:00
|
|
|
getCurrentClass().MethodDefs.back().TemplateScope
|
2009-08-22 08:34:47 +08:00
|
|
|
= CurScope->isTemplateParamScope();
|
2009-05-28 07:11:45 +08:00
|
|
|
CachedTokens &Toks = getCurrentClass().MethodDefs.back().Toks;
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
tok::TokenKind kind = Tok.getKind();
|
|
|
|
// We may have a constructor initializer or function-try-block here.
|
|
|
|
if (kind == tok::colon || kind == tok::kw_try) {
|
2008-11-05 12:29:56 +08:00
|
|
|
// Consume everything up to (and including) the left brace.
|
2008-12-17 05:30:33 +08:00
|
|
|
if (!ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks, tok::semi)) {
|
2008-11-11 00:59:40 +08:00
|
|
|
// We didn't find the left-brace we expected after the
|
2009-09-09 23:08:12 +08:00
|
|
|
// constructor initializer.
|
2008-11-11 00:59:40 +08:00
|
|
|
if (Tok.is(tok::semi)) {
|
|
|
|
// We found a semicolon; complain, consume the semicolon, and
|
|
|
|
// don't try to parse this method later.
|
|
|
|
Diag(Tok.getLocation(), diag::err_expected_lbrace);
|
|
|
|
ConsumeAnyToken();
|
2009-05-28 07:11:45 +08:00
|
|
|
getCurrentClass().MethodDefs.pop_back();
|
2008-11-11 00:59:40 +08:00
|
|
|
return FnD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-05 12:29:56 +08:00
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
// Begin by storing the '{' token.
|
2008-11-05 12:29:56 +08:00
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
|
|
|
}
|
|
|
|
// Consume everything up to (and including) the matching right brace.
|
2008-12-17 05:30:33 +08:00
|
|
|
ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
// If we're in a function-try-block, we need to store all the catch blocks.
|
|
|
|
if (kind == tok::kw_try) {
|
|
|
|
while (Tok.is(tok::kw_catch)) {
|
|
|
|
ConsumeAndStoreUntil(tok::l_brace, tok::unknown, Toks);
|
|
|
|
ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-25 06:12:16 +08:00
|
|
|
return FnD;
|
|
|
|
}
|
|
|
|
|
2008-12-17 05:30:33 +08:00
|
|
|
/// ParseLexedMethodDeclarations - We finished parsing the member
|
|
|
|
/// specification of a top (non-nested) C++ class. Now go over the
|
|
|
|
/// stack of method declarations with some parts for which parsing was
|
|
|
|
/// delayed (such as default arguments) and parse them.
|
2009-05-28 07:11:45 +08:00
|
|
|
void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
|
|
|
|
bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
|
|
|
|
ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
|
|
|
|
if (HasTemplateScope)
|
|
|
|
Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
|
|
|
|
|
2009-12-19 18:49:29 +08:00
|
|
|
// The current scope is still active if we're the top-level class.
|
|
|
|
// Otherwise we'll need to push and enter a new scope.
|
2009-05-28 07:11:45 +08:00
|
|
|
bool HasClassScope = !Class.TopLevelClass;
|
2009-12-19 18:49:29 +08:00
|
|
|
ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope, HasClassScope);
|
|
|
|
if (HasClassScope)
|
|
|
|
Actions.ActOnStartDelayedMemberDeclarations(CurScope, Class.TagOrTemplate);
|
2009-05-28 07:11:45 +08:00
|
|
|
|
|
|
|
for (; !Class.MethodDecls.empty(); Class.MethodDecls.pop_front()) {
|
|
|
|
LateParsedMethodDeclaration &LM = Class.MethodDecls.front();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-22 08:34:47 +08:00
|
|
|
// If this is a member template, introduce the template parameter scope.
|
|
|
|
ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
|
|
|
|
if (LM.TemplateScope)
|
|
|
|
Actions.ActOnReenterTemplateScope(CurScope, LM.Method);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 05:30:33 +08:00
|
|
|
// Start the delayed C++ method declaration
|
|
|
|
Actions.ActOnStartDelayedCXXMethodDeclaration(CurScope, LM.Method);
|
|
|
|
|
|
|
|
// Introduce the parameters into scope and parse their default
|
|
|
|
// arguments.
|
2009-09-09 23:08:12 +08:00
|
|
|
ParseScope PrototypeScope(this,
|
2009-01-10 06:42:13 +08:00
|
|
|
Scope::FunctionPrototypeScope|Scope::DeclScope);
|
2008-12-17 05:30:33 +08:00
|
|
|
for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) {
|
|
|
|
// Introduce the parameter into scope.
|
|
|
|
Actions.ActOnDelayedCXXMethodParameter(CurScope, LM.DefaultArgs[I].Param);
|
|
|
|
|
|
|
|
if (CachedTokens *Toks = LM.DefaultArgs[I].Toks) {
|
|
|
|
// Parse the default argument from its saved token stream.
|
|
|
|
Toks->push_back(Tok); // So that the current token doesn't get lost
|
|
|
|
PP.EnterTokenStream(&Toks->front(), Toks->size(), true, false);
|
|
|
|
|
|
|
|
// Consume the previously-pushed token.
|
|
|
|
ConsumeAnyToken();
|
|
|
|
|
|
|
|
// Consume the '='.
|
|
|
|
assert(Tok.is(tok::equal) && "Default argument not starting with '='");
|
|
|
|
SourceLocation EqualLoc = ConsumeToken();
|
|
|
|
|
|
|
|
OwningExprResult DefArgResult(ParseAssignmentExpression());
|
|
|
|
if (DefArgResult.isInvalid())
|
|
|
|
Actions.ActOnParamDefaultArgumentError(LM.DefaultArgs[I].Param);
|
|
|
|
else
|
|
|
|
Actions.ActOnParamDefaultArgument(LM.DefaultArgs[I].Param, EqualLoc,
|
2009-03-16 01:47:39 +08:00
|
|
|
move(DefArgResult));
|
2008-12-17 05:30:33 +08:00
|
|
|
delete Toks;
|
|
|
|
LM.DefaultArgs[I].Toks = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PrototypeScope.Exit();
|
|
|
|
|
|
|
|
// Finish the delayed C++ method declaration.
|
|
|
|
Actions.ActOnFinishDelayedCXXMethodDeclaration(CurScope, LM.Method);
|
|
|
|
}
|
2009-05-28 07:11:45 +08:00
|
|
|
|
|
|
|
for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
|
|
|
|
ParseLexedMethodDeclarations(*Class.NestedClasses[I]);
|
2009-12-19 18:49:29 +08:00
|
|
|
|
|
|
|
if (HasClassScope)
|
|
|
|
Actions.ActOnFinishDelayedMemberDeclarations(CurScope, Class.TagOrTemplate);
|
2008-12-17 05:30:33 +08:00
|
|
|
}
|
|
|
|
|
2008-06-25 06:12:16 +08:00
|
|
|
/// ParseLexedMethodDefs - We finished parsing the member specification of a top
|
|
|
|
/// (non-nested) C++ class. Now go over the stack of lexed methods that were
|
|
|
|
/// collected during its parsing and parse them all.
|
2009-05-28 07:11:45 +08:00
|
|
|
void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
|
|
|
|
bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
|
|
|
|
ParseScope TemplateScope(this, Scope::TemplateParamScope, HasTemplateScope);
|
|
|
|
if (HasTemplateScope)
|
|
|
|
Actions.ActOnReenterTemplateScope(CurScope, Class.TagOrTemplate);
|
|
|
|
|
|
|
|
bool HasClassScope = !Class.TopLevelClass;
|
|
|
|
ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope,
|
|
|
|
HasClassScope);
|
|
|
|
|
|
|
|
for (; !Class.MethodDefs.empty(); Class.MethodDefs.pop_front()) {
|
|
|
|
LexedMethod &LM = Class.MethodDefs.front();
|
2008-06-25 06:12:16 +08:00
|
|
|
|
2009-08-22 08:34:47 +08:00
|
|
|
// If this is a member template, introduce the template parameter scope.
|
|
|
|
ParseScope TemplateScope(this, Scope::TemplateParamScope, LM.TemplateScope);
|
|
|
|
if (LM.TemplateScope)
|
|
|
|
Actions.ActOnReenterTemplateScope(CurScope, LM.D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-06-25 06:12:16 +08:00
|
|
|
assert(!LM.Toks.empty() && "Empty body!");
|
|
|
|
// Append the current token at the end of the new token stream so that it
|
|
|
|
// doesn't get lost.
|
|
|
|
LM.Toks.push_back(Tok);
|
2009-08-24 19:57:43 +08:00
|
|
|
PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
|
2008-06-25 06:12:16 +08:00
|
|
|
|
|
|
|
// Consume the previously pushed token.
|
|
|
|
ConsumeAnyToken();
|
2009-04-27 04:35:05 +08:00
|
|
|
assert((Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try))
|
|
|
|
&& "Inline method not starting with '{', ':' or 'try'");
|
2008-06-25 06:12:16 +08:00
|
|
|
|
|
|
|
// Parse the method body. Function body parsing code is similar enough
|
|
|
|
// to be re-used for method bodies as well.
|
2008-12-10 14:34:36 +08:00
|
|
|
ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope);
|
2008-06-25 06:12:16 +08:00
|
|
|
Actions.ActOnStartOfFunctionDef(CurScope, LM.D);
|
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
if (Tok.is(tok::kw_try)) {
|
|
|
|
ParseFunctionTryBlock(LM.D);
|
2009-04-27 05:08:36 +08:00
|
|
|
continue;
|
2009-04-27 04:35:05 +08:00
|
|
|
}
|
2008-11-05 12:29:56 +08:00
|
|
|
if (Tok.is(tok::colon))
|
|
|
|
ParseConstructorInitializer(LM.D);
|
2009-07-15 04:06:22 +08:00
|
|
|
else
|
2009-07-22 06:36:06 +08:00
|
|
|
Actions.ActOnDefaultCtorInitializers(LM.D);
|
2009-07-15 02:24:21 +08:00
|
|
|
|
2009-03-05 08:49:17 +08:00
|
|
|
// FIXME: What if ParseConstructorInitializer doesn't leave us with a '{'??
|
|
|
|
ParseFunctionStatementBody(LM.D);
|
2008-06-25 06:12:16 +08:00
|
|
|
}
|
2009-05-28 07:11:45 +08:00
|
|
|
|
|
|
|
for (unsigned I = 0, N = Class.NestedClasses.size(); I != N; ++I)
|
|
|
|
ParseLexedMethodDefs(*Class.NestedClasses[I]);
|
2008-06-25 06:12:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ConsumeAndStoreUntil - Consume and store the token at the passed token
|
2008-12-17 05:30:33 +08:00
|
|
|
/// container until the token 'T' is reached (which gets
|
2009-09-09 23:08:12 +08:00
|
|
|
/// consumed/stored too, if ConsumeFinalToken).
|
2008-11-11 00:59:40 +08:00
|
|
|
/// If EarlyAbortIf is specified, then we will stop early if we find that
|
|
|
|
/// token at the top level.
|
2008-12-17 05:30:33 +08:00
|
|
|
/// Returns true if token 'T1' or 'T2' was found.
|
2008-06-25 06:12:16 +08:00
|
|
|
/// NOTE: This is a specialized version of Parser::SkipUntil.
|
2008-12-17 05:30:33 +08:00
|
|
|
bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2,
|
|
|
|
CachedTokens &Toks,
|
|
|
|
tok::TokenKind EarlyAbortIf,
|
|
|
|
bool ConsumeFinalToken) {
|
2008-06-25 06:12:16 +08:00
|
|
|
// We always want this function to consume at least one token if the first
|
|
|
|
// token isn't T and if not at EOF.
|
|
|
|
bool isFirstTokenConsumed = true;
|
|
|
|
while (1) {
|
|
|
|
// If we found one of the tokens, stop and return true.
|
2008-12-17 05:30:33 +08:00
|
|
|
if (Tok.is(T1) || Tok.is(T2)) {
|
|
|
|
if (ConsumeFinalToken) {
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeAnyToken();
|
|
|
|
}
|
2008-06-25 06:12:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-11 00:59:40 +08:00
|
|
|
// If we found the early-abort token, return.
|
|
|
|
if (Tok.is(EarlyAbortIf))
|
|
|
|
return false;
|
|
|
|
|
2008-06-25 06:12:16 +08:00
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::eof:
|
|
|
|
// Ran out of tokens.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case tok::l_paren:
|
|
|
|
// Recursively consume properly-nested parens.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeParen();
|
2008-12-17 05:30:33 +08:00
|
|
|
ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks);
|
2008-06-25 06:12:16 +08:00
|
|
|
break;
|
|
|
|
case tok::l_square:
|
|
|
|
// Recursively consume properly-nested square brackets.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBracket();
|
2008-12-17 05:30:33 +08:00
|
|
|
ConsumeAndStoreUntil(tok::r_square, tok::unknown, Toks);
|
2008-06-25 06:12:16 +08:00
|
|
|
break;
|
|
|
|
case tok::l_brace:
|
|
|
|
// Recursively consume properly-nested braces.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
2008-12-17 05:30:33 +08:00
|
|
|
ConsumeAndStoreUntil(tok::r_brace, tok::unknown, Toks);
|
2008-06-25 06:12:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Okay, we found a ']' or '}' or ')', which we think should be balanced.
|
|
|
|
// Since the user wasn't looking for this token (if they were, it would
|
|
|
|
// already be handled), this isn't balanced. If there is a LHS token at a
|
|
|
|
// higher level, we will assume that this matches the unbalanced token
|
|
|
|
// and return it. Otherwise, this is a spurious RHS token, which we skip.
|
|
|
|
case tok::r_paren:
|
|
|
|
if (ParenCount && !isFirstTokenConsumed)
|
|
|
|
return false; // Matches something.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeParen();
|
|
|
|
break;
|
|
|
|
case tok::r_square:
|
|
|
|
if (BracketCount && !isFirstTokenConsumed)
|
|
|
|
return false; // Matches something.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBracket();
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
|
|
|
if (BraceCount && !isFirstTokenConsumed)
|
|
|
|
return false; // Matches something.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeBrace();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::string_literal:
|
|
|
|
case tok::wide_string_literal:
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeStringToken();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// consume this token.
|
|
|
|
Toks.push_back(Tok);
|
|
|
|
ConsumeToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
isFirstTokenConsumed = false;
|
|
|
|
}
|
|
|
|
}
|