Files
BlitzNext/compiler/type.hpp
T

129 lines
2.1 KiB
C++
Raw Normal View History

2014-01-31 08:23:00 +13:00
#ifndef TYPE_H
#define TYPE_H
2019-01-18 15:55:51 +01:00
#include "decl.hpp"
2014-01-31 08:23:00 +13:00
struct FuncType;
struct ArrayType;
struct StructType;
struct ConstType;
struct VectorType;
2019-01-18 17:04:57 +01:00
struct Type {
virtual ~Type() {}
2014-01-31 08:23:00 +13:00
2019-01-18 17:04:57 +01:00
virtual bool intType()
{
return 0;
}
virtual bool floatType()
{
return 0;
}
virtual bool stringType()
{
return 0;
}
2014-01-31 08:23:00 +13:00
//casts to inherited types
2019-01-18 17:04:57 +01:00
virtual FuncType* funcType()
{
return 0;
}
virtual ArrayType* arrayType()
{
return 0;
}
virtual StructType* structType()
{
return 0;
}
virtual ConstType* constType()
{
return 0;
}
virtual VectorType* vectorType()
{
return 0;
}
2014-01-31 08:23:00 +13:00
//operators
2019-01-18 17:04:57 +01:00
virtual bool canCastTo(Type* t)
{
return this == t;
}
2014-01-31 08:23:00 +13:00
//built in types
2019-01-18 17:04:57 +01:00
static Type *void_type, *int_type, *float_type, *string_type, *null_type;
2014-01-31 08:23:00 +13:00
};
2019-01-18 17:04:57 +01:00
struct FuncType : public Type {
Type* returnType;
DeclSeq* params;
bool userlib, cfunc;
FuncType(Type* t, DeclSeq* p, bool ulib, bool cfn) : returnType(t), params(p), userlib(ulib), cfunc(cfn) {}
~FuncType()
{
delete params;
}
FuncType* funcType()
{
return this;
}
2014-01-31 08:23:00 +13:00
};
2019-01-18 17:04:57 +01:00
struct ArrayType : public Type {
Type* elementType;
int dims;
ArrayType(Type* t, int n) : elementType(t), dims(n) {}
ArrayType* arrayType()
{
return this;
}
2014-01-31 08:23:00 +13:00
};
2019-01-18 17:04:57 +01:00
struct StructType : public Type {
string ident;
DeclSeq* fields;
StructType(const string& i) : ident(i), fields(0) {}
StructType(const string& i, DeclSeq* f) : ident(i), fields(f) {}
~StructType()
{
delete fields;
}
StructType* structType()
{
return this;
}
virtual bool canCastTo(Type* t);
2014-01-31 08:23:00 +13:00
};
2019-01-18 17:04:57 +01:00
struct ConstType : public Type {
Type* valueType;
int intValue;
float floatValue;
2014-01-31 08:23:00 +13:00
string stringValue;
2019-01-18 17:04:57 +01:00
ConstType(int n) : intValue(n), valueType(Type::int_type) {}
ConstType(float n) : floatValue(n), valueType(Type::float_type) {}
ConstType(const string& n) : stringValue(n), valueType(Type::string_type) {}
ConstType* constType()
{
return this;
}
2014-01-31 08:23:00 +13:00
};
2019-01-18 17:04:57 +01:00
struct VectorType : public Type {
string label;
Type* elementType;
2014-01-31 08:23:00 +13:00
vector<int> sizes;
2019-01-18 17:04:57 +01:00
VectorType(const string& l, Type* t, const vector<int>& szs) : label(l), elementType(t), sizes(szs) {}
VectorType* vectorType()
{
return this;
}
virtual bool canCastTo(Type* t);
2014-01-31 08:23:00 +13:00
};
#endif