Reimplemented Type::as*Type() using virtual methods.

This commit is contained in:
Roberto Raggi
2009-02-09 17:44:06 +01:00
parent ce22a96041
commit d01795d933
15 changed files with 175 additions and 181 deletions

View File

@@ -130,8 +130,8 @@ bool CheckDeclaration::visit(SimpleDeclarationAST *ast)
FullySpecifiedType ty = semantic()->check(ast->decl_specifier_seq, _scope);
FullySpecifiedType qualTy = ty.qualifiedType();
if (_templateParameters) {
if (Class *klass = ty->asClass()) {
if (_templateParameters && ty) {
if (Class *klass = ty->asClassType()) {
klass->setTemplateParameters(_templateParameters);
}
}
@@ -142,7 +142,8 @@ bool CheckDeclaration::visit(SimpleDeclarationAST *ast)
FullySpecifiedType declTy = semantic()->check(it->declarator, qualTy,
_scope, &name);
if (Function *fun = declTy->asFunction()) {
Function *fun = 0;
if (declTy && 0 != (fun = declTy->asFunctionType())) {
fun->setScope(_scope);
fun->setName(name);
fun->setMethodKey(semantic()->currentMethodKey());
@@ -162,7 +163,7 @@ bool CheckDeclaration::visit(SimpleDeclarationAST *ast)
symbol->setType(control()->integerType(IntegerType::Int));
symbol->setType(declTy);
if (_templateParameters && it == ast->declarators && ! ty->asClass())
if (_templateParameters && it == ast->declarators && ty && ! ty->isClassType())
symbol->setTemplateParameters(_templateParameters);
symbol->setVisibility(semantic()->currentVisibility());
@@ -225,13 +226,13 @@ bool CheckDeclaration::visit(FunctionDefinitionAST *ast)
Name *name = 0;
FullySpecifiedType funTy = semantic()->check(ast->declarator, qualTy,
_scope, &name);
Function *fun = funTy->asFunction();
if (! fun) {
if (! (funTy && funTy->isFunctionType())) {
translationUnit()->error(ast->firstToken(),
"expected a function prototype");
return false;
}
Function *fun = funTy->asFunctionType();
fun->setName(name);
fun->setTemplateParameters(_templateParameters);
fun->setVisibility(semantic()->currentVisibility());

View File

@@ -135,9 +135,12 @@ bool CheckDeclarator::visit(DeclaratorAST *ast)
// ### check the initializer
// FullySpecifiedType exprTy = semantic()->check(ast->initializer, _scope);
if (ast->initializer && _fullySpecifiedType->isFunction()) {
_fullySpecifiedType->asFunction()->setPureVirtual(true);
if (ast->initializer && _fullySpecifiedType) {
if (Function *funTy = _fullySpecifiedType->asFunctionType()) {
funTy->setPureVirtual(true);
}
}
return false;
}

View File

@@ -319,7 +319,7 @@ bool CheckExpression::visit(QtMethodAST *ast)
Scope dummy;
FullySpecifiedType methTy = semantic()->check(ast->declarator, FullySpecifiedType(),
&dummy, &name);
Function *fty = methTy->asFunction();
Function *fty = methTy->asFunctionType();
if (! fty)
translationUnit()->warning(ast->firstToken(), "expected a function declarator");
else {

View File

@@ -66,6 +66,12 @@ class CPLUSPLUS_EXPORT VoidType: public Type
public:
virtual bool isEqualTo(const Type *other) const;
virtual const VoidType *asVoidType() const
{ return this; }
virtual VoidType *asVoidType()
{ return this; }
protected:
virtual void accept0(TypeVisitor *visitor);
};
@@ -91,6 +97,12 @@ public:
virtual bool isEqualTo(const Type *other) const;
virtual IntegerType *asIntegerType()
{ return this; }
virtual const IntegerType *asIntegerType() const
{ return this; }
protected:
virtual void accept0(TypeVisitor *visitor);
@@ -115,6 +127,12 @@ public:
virtual bool isEqualTo(const Type *other) const;
virtual const FloatType *asFloatType() const
{ return this; }
virtual FloatType *asFloatType()
{ return this; }
protected:
virtual void accept0(TypeVisitor *visitor);
@@ -132,6 +150,12 @@ public:
virtual bool isEqualTo(const Type *other) const;
virtual const PointerType *asPointerType() const
{ return this; }
virtual PointerType *asPointerType()
{ return this; }
protected:
virtual void accept0(TypeVisitor *visitor);
@@ -150,6 +174,12 @@ public:
virtual bool isEqualTo(const Type *other) const;
virtual const PointerToMemberType *asPointerToMemberType() const
{ return this; }
virtual PointerToMemberType *asPointerToMemberType()
{ return this; }
protected:
virtual void accept0(TypeVisitor *visitor);
@@ -168,6 +198,12 @@ public:
virtual bool isEqualTo(const Type *other) const;
virtual const ReferenceType *asReferenceType() const
{ return this; }
virtual ReferenceType *asReferenceType()
{ return this; }
protected:
virtual void accept0(TypeVisitor *visitor);
@@ -186,6 +222,12 @@ public:
virtual bool isEqualTo(const Type *other) const;
virtual const ArrayType *asArrayType() const
{ return this; }
virtual ArrayType *asArrayType()
{ return this; }
protected:
virtual void accept0(TypeVisitor *visitor);
@@ -204,6 +246,12 @@ public:
virtual bool isEqualTo(const Type *other) const;
virtual const NamedType *asNamedType() const
{ return this; }
virtual NamedType *asNamedType()
{ return this; }
protected:
virtual void accept0(TypeVisitor *visitor);

View File

@@ -188,7 +188,7 @@ void Function::setTemplateParameters(Scope *templateParameters)
bool Function::isEqualTo(const Type *other) const
{
const Function *o = other->asFunction();
const Function *o = other->asFunctionType();
if (! o)
return false;
Name *l = identity();
@@ -323,7 +323,7 @@ FullySpecifiedType Enum::type() const
bool Enum::isEqualTo(const Type *other) const
{
const Enum *o = other->asEnum();
const Enum *o = other->asEnumType();
if (! o)
return false;
Name *l = identity();
@@ -356,7 +356,7 @@ Namespace::~Namespace()
bool Namespace::isEqualTo(const Type *other) const
{
const Namespace *o = other->asNamespace();
const Namespace *o = other->asNamespaceType();
if (! o)
return false;
Name *l = identity();
@@ -458,7 +458,7 @@ FullySpecifiedType Class::type() const
bool Class::isEqualTo(const Type *other) const
{
const Class *o = other->asClass();
const Class *o = other->asClassType();
if (! o)
return false;
Name *l = identity();

View File

@@ -217,6 +217,12 @@ public:
virtual Enum *asEnum()
{ return this; }
virtual const Enum *asEnumType() const
{ return this; }
virtual Enum *asEnumType()
{ return this; }
protected:
virtual void visitSymbol0(SymbolVisitor *visitor);
virtual void accept0(TypeVisitor *visitor);
@@ -278,6 +284,12 @@ public:
virtual Function *asFunction()
{ return this; }
virtual const Function *asFunctionType() const
{ return this; }
virtual Function *asFunctionType()
{ return this; }
protected:
virtual void visitSymbol0(SymbolVisitor *visitor);
virtual void accept0(TypeVisitor *visitor);
@@ -318,6 +330,12 @@ public:
virtual Namespace *asNamespace()
{ return this; }
virtual const Namespace *asNamespaceType() const
{ return this; }
virtual Namespace *asNamespaceType()
{ return this; }
protected:
virtual void visitSymbol0(SymbolVisitor *visitor);
virtual void accept0(TypeVisitor *visitor);
@@ -388,6 +406,12 @@ public:
virtual Class *asClass()
{ return this; }
virtual const Class *asClassType() const
{ return this; }
virtual Class *asClassType()
{ return this; }
protected:
virtual void visitSymbol0(SymbolVisitor *visitor);
virtual void accept0(TypeVisitor *visitor);

View File

@@ -64,121 +64,40 @@ Type::~Type()
{ }
bool Type::isVoidType() const
{ return dynamic_cast<const VoidType *>(this) != 0; }
{ return asVoidType() != 0; }
bool Type::isIntegerType() const
{ return dynamic_cast<const IntegerType *>(this) != 0; }
{ return asIntegerType() != 0; }
bool Type::isFloatType() const
{ return dynamic_cast<const FloatType *>(this) != 0; }
{ return asFloatType() != 0; }
bool Type::isPointerType() const
{ return dynamic_cast<const PointerType *>(this) != 0; }
{ return asPointerType() != 0; }
bool Type::isPointerToMemberType() const
{ return dynamic_cast<const PointerToMemberType *>(this) != 0; }
{ return asPointerToMemberType() != 0; }
bool Type::isReferenceType() const
{ return dynamic_cast<const ReferenceType *>(this) != 0; }
{ return asReferenceType() != 0; }
bool Type::isArrayType() const
{ return dynamic_cast<const ArrayType *>(this) != 0; }
{ return asArrayType() != 0; }
bool Type::isNamedType() const
{ return dynamic_cast<const NamedType *>(this) != 0; }
{ return asNamedType() != 0; }
bool Type::isFunction() const
{ return dynamic_cast<const Function *>(this) != 0; }
bool Type::isFunctionType() const
{ return asFunctionType() != 0; }
bool Type::isNamespace() const
{ return dynamic_cast<const Namespace *>(this) != 0; }
bool Type::isNamespaceType() const
{ return asNamespaceType() != 0; }
bool Type::isClass() const
{ return dynamic_cast<const Class *>(this) != 0; }
bool Type::isClassType() const
{ return asClassType() != 0; }
bool Type::isEnum() const
{ return dynamic_cast<const Enum *>(this) != 0; }
bool Type::isScopedSymbol() const
{ return dynamic_cast<const ScopedSymbol *>(this) != 0; }
const VoidType *Type::asVoidType() const
{ return dynamic_cast<const VoidType *>(this); }
const IntegerType *Type::asIntegerType() const
{ return dynamic_cast<const IntegerType *>(this); }
const FloatType *Type::asFloatType() const
{ return dynamic_cast<const FloatType *>(this); }
const PointerType *Type::asPointerType() const
{ return dynamic_cast<const PointerType *>(this); }
const PointerToMemberType *Type::asPointerToMemberType() const
{ return dynamic_cast<const PointerToMemberType *>(this); }
const ReferenceType *Type::asReferenceType() const
{ return dynamic_cast<const ReferenceType *>(this); }
const ArrayType *Type::asArrayType() const
{ return dynamic_cast<const ArrayType *>(this); }
const NamedType *Type::asNamedType() const
{ return dynamic_cast<const NamedType *>(this); }
const Function *Type::asFunction() const
{ return dynamic_cast<const Function *>(this); }
const Namespace *Type::asNamespace() const
{ return dynamic_cast<const Namespace *>(this); }
const Class *Type::asClass() const
{ return dynamic_cast<const Class *>(this); }
const Enum *Type::asEnum() const
{ return dynamic_cast<const Enum *>(this); }
const ScopedSymbol *Type::asScopedSymbol() const
{ return dynamic_cast<const ScopedSymbol *>(this); }
VoidType *Type::asVoidType()
{ return dynamic_cast<VoidType *>(this); }
IntegerType *Type::asIntegerType()
{ return dynamic_cast<IntegerType *>(this); }
FloatType *Type::asFloatType()
{ return dynamic_cast<FloatType *>(this); }
PointerType *Type::asPointerType()
{ return dynamic_cast<PointerType *>(this); }
PointerToMemberType *Type::asPointerToMemberType()
{ return dynamic_cast<PointerToMemberType *>(this); }
ReferenceType *Type::asReferenceType()
{ return dynamic_cast<ReferenceType *>(this); }
ArrayType *Type::asArrayType()
{ return dynamic_cast<ArrayType *>(this); }
NamedType *Type::asNamedType()
{ return dynamic_cast<NamedType *>(this); }
Function *Type::asFunction()
{ return dynamic_cast<Function *>(this); }
Namespace *Type::asNamespace()
{ return dynamic_cast<Namespace *>(this); }
Class *Type::asClass()
{ return dynamic_cast<Class *>(this); }
Enum *Type::asEnum()
{ return dynamic_cast<Enum *>(this); }
ScopedSymbol *Type::asScopedSymbol()
{ return dynamic_cast<ScopedSymbol *>(this); }
bool Type::isEnumType() const
{ return asEnumType() != 0; }
void Type::accept(TypeVisitor *visitor)
{

View File

@@ -75,39 +75,36 @@ public:
bool isReferenceType() const;
bool isArrayType() const;
bool isNamedType() const;
bool isFunction() const;
bool isNamespace() const;
bool isClass() const;
bool isEnum() const;
bool isScopedSymbol() const;
bool isFunctionType() const;
bool isNamespaceType() const;
bool isClassType() const;
bool isEnumType() const;
const VoidType *asVoidType() const;
const IntegerType *asIntegerType() const;
const FloatType *asFloatType() const;
const PointerType *asPointerType() const;
const PointerToMemberType *asPointerToMemberType() const;
const ReferenceType *asReferenceType() const;
const ArrayType *asArrayType() const;
const NamedType *asNamedType() const;
const Function *asFunction() const;
const Namespace *asNamespace() const;
const Class *asClass() const;
const Enum *asEnum() const;
const ScopedSymbol *asScopedSymbol() const;
virtual const VoidType *asVoidType() const { return 0; }
virtual const IntegerType *asIntegerType() const { return 0; }
virtual const FloatType *asFloatType() const { return 0; }
virtual const PointerType *asPointerType() const { return 0; }
virtual const PointerToMemberType *asPointerToMemberType() const { return 0; }
virtual const ReferenceType *asReferenceType() const { return 0; }
virtual const ArrayType *asArrayType() const { return 0; }
virtual const NamedType *asNamedType() const { return 0; }
virtual const Function *asFunctionType() const { return 0; }
virtual const Namespace *asNamespaceType() const { return 0; }
virtual const Class *asClassType() const { return 0; }
virtual const Enum *asEnumType() const { return 0; }
VoidType *asVoidType();
IntegerType *asIntegerType();
FloatType *asFloatType();
PointerType *asPointerType();
PointerToMemberType *asPointerToMemberType();
ReferenceType *asReferenceType();
ArrayType *asArrayType();
NamedType *asNamedType();
Function *asFunction();
Namespace *asNamespace();
Class *asClass();
Enum *asEnum();
ScopedSymbol *asScopedSymbol();
virtual VoidType *asVoidType() { return 0; }
virtual IntegerType *asIntegerType() { return 0; }
virtual FloatType *asFloatType() { return 0; }
virtual PointerType *asPointerType() { return 0; }
virtual PointerToMemberType *asPointerToMemberType() { return 0; }
virtual ReferenceType *asReferenceType() { return 0; }
virtual ArrayType *asArrayType() { return 0; }
virtual NamedType *asNamedType() { return 0; }
virtual Function *asFunctionType() { return 0; }
virtual Namespace *asNamespaceType() { return 0; }
virtual Class *asClassType() { return 0; }
virtual Enum *asEnumType() { return 0; }
void accept(TypeVisitor *visitor);
static void accept(Type *type, TypeVisitor *visitor);