Refactored CPlusPlus::Function.

This commit is contained in:
Roberto Raggi
2010-08-05 17:02:25 +02:00
committed by Oswald Buddenhagen
parent 5f44036424
commit 9aa991d636
11 changed files with 46 additions and 45 deletions

View File

@@ -175,7 +175,7 @@ bool CheckDeclarator::visit(FunctionDeclaratorAST *ast)
if (ast->parameters) {
DeclarationListAST *parameter_declarations = ast->parameters->parameter_declaration_list;
for (DeclarationListAST *decl = parameter_declarations; decl; decl = decl->next) {
semantic()->check(decl->value, fun->arguments());
semantic()->check(decl->value, fun->members());
}
if (ast->parameters->dot_dot_dot_token)

View File

@@ -58,9 +58,9 @@ using namespace CPlusPlus;
Scope::Scope(ScopedSymbol *owner)
: _owner(owner),
_symbols(0),
_hash(0),
_allocatedSymbols(0),
_symbolCount(-1),
_hash(0),
_hashSize(0),
_startOffset(0),
_endOffset(0)
@@ -168,10 +168,7 @@ bool Scope::isBlockScope() const
bool Scope::isPrototypeScope() const
{
Function *f = 0;
if (_owner && 0 != (f = _owner->asFunction()))
return f->arguments() == this;
return false;
return isFunctionScope();
}
bool Scope::isObjCClassScope() const
@@ -190,9 +187,8 @@ bool Scope::isObjCProtocolScope() const
bool Scope::isFunctionScope() const
{
Function *f = 0;
if (_owner && 0 != (f = _owner->asFunction()))
return f->arguments() != this;
if (_owner)
return _owner->isFunction();
return false;
}

View File

@@ -161,14 +161,11 @@ private:
enum { DefaultInitialSize = 11 };
ScopedSymbol *_owner;
Symbol **_symbols;
Symbol **_hash;
int _allocatedSymbols;
int _symbolCount;
Symbol **_hash;
int _hashSize;
unsigned _startOffset;
unsigned _endOffset;
};

View File

@@ -199,7 +199,13 @@ void Semantic::finishFunctionDefinition(FunctionDefinitionAST *ast)
Function *fun = ast->symbol;
d->checkDeclaration->check(ast->ctor_initializer, fun->scope());
check(ast->function_body, fun->members());
if (ast->function_body) {
check(ast->function_body, fun->members());
if (CompoundStatementAST *c = ast->function_body->asCompoundStatement())
fun->setBlock(c->symbol);
}
switchMethodKey(previousMethodKey);
switchVisibility(previousVisibility);

View File

@@ -191,14 +191,14 @@ void TypenameArgument::visitSymbol0(SymbolVisitor *visitor)
Function::Function(TranslationUnit *translationUnit, unsigned sourceLocation, const Name *name)
: ScopedSymbol(translationUnit, sourceLocation, name),
_templateParameters(0),
_flags(0)
{ _arguments = new Scope(this); }
_templateParameters(0),
_block(0),
_flags(0)
{ }
Function::~Function()
{
delete _templateParameters;
delete _arguments;
}
bool Function::isNormal() const
@@ -219,6 +219,12 @@ int Function::methodKey() const
void Function::setMethodKey(int key)
{ f._methodKey = key; }
Block *Function::block() const
{ return _block; }
void Function::setBlock(Block *block)
{ _block = block; }
unsigned Function::templateParameterCount() const
{
if (! _templateParameters)
@@ -255,13 +261,13 @@ bool Function::isEqualTo(const Type *other) const
const Name *l = identity();
const Name *r = o->identity();
if (l == r || (l && l->isEqualTo(r))) {
if (_arguments->symbolCount() != o->_arguments->symbolCount())
if (argumentCount() != o->argumentCount())
return false;
else if (! _returnType.isEqualTo(o->_returnType))
return false;
for (unsigned i = 0; i < _arguments->symbolCount(); ++i) {
Symbol *l = _arguments->symbolAt(i);
Symbol *r = o->_arguments->symbolAt(i);
for (unsigned i = 0; i < argumentCount(); ++i) {
Symbol *l = argumentAt(i);
Symbol *r = o->argumentAt(i);
if (! l->type().isEqualTo(r->type()))
return false;
}
@@ -334,17 +340,14 @@ bool Function::hasReturnType() const
unsigned Function::argumentCount() const
{
if (! _arguments)
return 0;
if (_block)
return memberCount() - 1;
return _arguments->symbolCount();
return memberCount();
}
Symbol *Function::argumentAt(unsigned index) const
{ return _arguments->symbolAt(index); }
Scope *Function::arguments() const
{ return _arguments; }
{ return memberAt(index); }
bool Function::hasArguments() const
{
@@ -356,8 +359,8 @@ unsigned Function::minimumArgumentCount() const
{
unsigned index = 0;
for (; index < _arguments->symbolCount(); ++index) {
if (Argument *arg = _arguments->symbolAt(index)->asArgument()) {
for (; index < argumentCount(); ++index) {
if (Argument *arg = argumentAt(index)->asArgument()) {
if (arg->hasInitializer())
break;
}
@@ -405,9 +408,6 @@ void Function::setAmbiguous(bool isAmbiguous)
void Function::visitSymbol0(SymbolVisitor *visitor)
{
if (visitor->visit(this)) {
for (unsigned i = 0; i < _arguments->symbolCount(); ++i) {
visitSymbol(_arguments->symbolAt(i), visitor);
}
for (unsigned i = 0; i < memberCount(); ++i) {
visitSymbol(memberAt(i), visitor);
}

View File

@@ -342,6 +342,9 @@ public:
int methodKey() const;
void setMethodKey(int key);
Block *block() const;
void setBlock(Block *block);
unsigned templateParameterCount() const; // ### remove me
Symbol *templateParameterAt(unsigned index) const; // ### remove me
@@ -356,7 +359,6 @@ public:
unsigned argumentCount() const;
Symbol *argumentAt(unsigned index) const;
Scope *arguments() const;
/** Convenience function that returns whether the function receives any arguments. */
bool hasArguments() const;
@@ -408,8 +410,9 @@ protected:
virtual bool matchType0(const Type *otherType, TypeMatcher *matcher) const;
private:
TemplateParameters *_templateParameters;
FullySpecifiedType _returnType;
TemplateParameters *_templateParameters;
Block *_block;
struct Flags {
unsigned _isVirtual: 1;
unsigned _isVariadic: 1;
@@ -423,7 +426,6 @@ private:
unsigned _flags;
Flags f;
};
Scope *_arguments;
};
class CPLUSPLUS_EXPORT Namespace: public ScopedSymbol, public Type