type.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef TYPE_H
  2. #define TYPE_H
  3. #include "decl.h"
  4. struct FuncType;
  5. struct ArrayType;
  6. struct StructType;
  7. struct ConstType;
  8. struct VectorType;
  9. struct Type{
  10. virtual ~Type(){}
  11. virtual bool intType(){ return 0;}
  12. virtual bool floatType(){ return 0; }
  13. virtual bool stringType(){ return 0; }
  14. //casts to inherited types
  15. virtual FuncType *funcType(){ return 0; }
  16. virtual ArrayType *arrayType(){ return 0; }
  17. virtual StructType *structType(){ return 0; }
  18. virtual ConstType *constType(){ return 0; }
  19. virtual VectorType *vectorType(){ return 0; }
  20. //operators
  21. virtual bool canCastTo( Type *t ){ return this==t; }
  22. //built in types
  23. static Type *void_type,*int_type,*float_type,*string_type,*null_type;
  24. };
  25. struct FuncType : public Type{
  26. Type *returnType;
  27. DeclSeq *params;
  28. bool userlib,cfunc;
  29. FuncType( Type *t,DeclSeq *p,bool ulib,bool cfn ):returnType(t),params(p),userlib(ulib),cfunc(cfn){}
  30. ~FuncType(){ delete params; }
  31. FuncType *funcType(){ return this; }
  32. };
  33. struct ArrayType : public Type{
  34. Type *elementType;int dims;
  35. ArrayType( Type *t,int n ):elementType(t),dims(n){}
  36. ArrayType *arrayType(){ return this; }
  37. };
  38. struct StructType : public Type{
  39. string ident;
  40. DeclSeq *fields;
  41. StructType( const string &i ):ident(i),fields(0){}
  42. StructType( const string &i,DeclSeq *f ):ident(i),fields( f ){}
  43. ~StructType(){ delete fields; }
  44. StructType *structType(){ return this; }
  45. virtual bool canCastTo( Type *t );
  46. };
  47. struct ConstType : public Type{
  48. Type *valueType;
  49. int intValue;
  50. float floatValue;
  51. string stringValue;
  52. ConstType( int n ):intValue(n),valueType(Type::int_type){}
  53. ConstType( float n ):floatValue(n),valueType(Type::float_type){}
  54. ConstType( const string &n ):stringValue(n),valueType(Type::string_type){}
  55. ConstType *constType(){ return this; }
  56. };
  57. struct VectorType : public Type{
  58. string label;
  59. Type *elementType;
  60. vector<int> sizes;
  61. VectorType( const string &l,Type *t,const vector<int> &szs ):label(l),elementType(t),sizes(szs){}
  62. VectorType *vectorType(){ return this; }
  63. virtual bool canCastTo( Type *t );
  64. };
  65. #endif