type.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #ifndef TYPE_H
  2. #define TYPE_H
  3. #include "scope.h"
  4. #include "declseq.h"
  5. struct Val;
  6. /*
  7. Runtime type Encoding:
  8. b=byte
  9. s=short
  10. i=int
  11. f=float
  12. d=double
  13. z=cstring
  14. $=string
  15. []<type>=array
  16. ^<ident>.<ident>=class
  17. :<ident>.<ident>=object
  18. (<type>,...)<type>=function
  19. *<type>=pointer
  20. */
  21. struct NumericType;
  22. struct IntType;
  23. struct FloatType;
  24. struct StringType;
  25. struct CStringType;
  26. struct WStringType;
  27. struct ArrayType;
  28. struct ClassType;
  29. struct ObjectType;
  30. struct FunType;
  31. struct PtrType;
  32. struct VarType;
  33. struct RefType;
  34. struct NullType;
  35. struct ModuleType;
  36. struct Type : public Scope{
  37. virtual ~Type();
  38. virtual NumericType*numericType();
  39. virtual IntType* intType();
  40. virtual FloatType* floatType();
  41. virtual StringType* stringType();
  42. virtual CStringType*cstringType();
  43. virtual WStringType*wstringType();
  44. virtual ClassType* classType();
  45. virtual ObjectType* objectType();
  46. virtual ObjectType* exObjectType();
  47. virtual ArrayType* arrayType();
  48. virtual FunType* funType();
  49. virtual PtrType* ptrType();
  50. virtual VarType* varType();
  51. virtual RefType* refType();
  52. virtual NullType* nullType();
  53. virtual ModuleType* moduleType();
  54. virtual string encoding();
  55. virtual string toString();
  56. virtual bool equals( Type *ty );
  57. virtual bool extends( Type *ty );
  58. int size();
  59. int cgType();
  60. PtrType* ptrType( string valEncoding );
  61. static void createTypes();
  62. static void resolveTypes();
  63. static IntType *int8,*int16,*int32,*int64;
  64. static FloatType *float32,*float64;
  65. static CStringType *c_string;
  66. static WStringType *w_string;
  67. static PtrType *bytePtr;
  68. static NullType *null;
  69. static ModuleType *blitzModule;
  70. static ObjectType *objectObject;
  71. static StringType *stringObject;
  72. static Val *objectClass,*stringClass,*arrayClass;
  73. };
  74. struct NumericType : public Type{
  75. string _encoding;
  76. NumericType( int sz,bool fp );
  77. NumericType *numericType();
  78. string encoding();
  79. string toString();
  80. bool equals( Type *ty );
  81. };
  82. struct IntType : public NumericType{
  83. IntType( int sz ):NumericType( sz,false ){}
  84. IntType *intType();
  85. };
  86. struct FloatType : public NumericType{
  87. FloatType( int sz ):NumericType( sz,true ){}
  88. FloatType *floatType();
  89. };
  90. struct CStringType : public Type{
  91. CStringType *cstringType();
  92. string encoding();
  93. string toString();
  94. bool equals( Type *ty );
  95. };
  96. struct WStringType : public Type{
  97. WStringType *wstringType();
  98. string encoding();
  99. string toString();
  100. bool equals( Type *ty );
  101. };
  102. struct ClassType : public Type{
  103. enum{
  104. ABSTRACT=1,FINAL=2,EXTERN=4,PRIVATE=8
  105. };
  106. string super_name;
  107. Scope* scope;
  108. DeclSeq decls,fields,methods;
  109. int attrs,sizeof_fields,sizeof_vtable;
  110. ClassType( string supername,Scope *sc,int attrs=0 );
  111. ClassType*classType();
  112. string encoding();
  113. string toString();
  114. bool equals( Type *ty );
  115. bool extends( Type *ty );
  116. Val* find( string id );
  117. Val* findMethod( string id );
  118. Val* findSuperMethod( string id );
  119. void resolve();
  120. Val* superVal();
  121. ClassType*superClass();
  122. private:
  123. int resolved;
  124. Val* super_val;
  125. ClassType*super_class;
  126. string sourceinfo;
  127. };
  128. struct ObjectType : public Type{
  129. string ident;
  130. Scope *scope;
  131. Val *class_val;
  132. string sourceinfo;
  133. ObjectType( Val *clas );
  134. ObjectType( string id,Scope *sc );
  135. ObjectType *objectType();
  136. ObjectType *exObjectType();
  137. void resolve();
  138. string encoding();
  139. string toString();
  140. bool equals( Type *ty );
  141. bool extends( Type *ty );
  142. Val* find( string id );
  143. Val* classVal();
  144. ClassType *objectClass();
  145. };
  146. struct StringType : public ObjectType{
  147. StringType( Val *clas );
  148. StringType *stringType();
  149. string encoding();
  150. string toString();
  151. bool equals( Type *ty );
  152. };
  153. struct ArrayType : public ObjectType{
  154. Type *element_type;
  155. int dims;
  156. ArrayType( Type *ty,int n );
  157. ArrayType *arrayType();
  158. string encoding();
  159. string toString();
  160. bool equals( Type *ty );
  161. bool extends( Type *ty );
  162. };
  163. struct FunType : public Type{
  164. enum{
  165. ABSTRACT=1,FINAL=2,METHOD=4,VOIDFUN=8
  166. };
  167. DeclSeq args;
  168. Type *return_type;
  169. int attrs,call_conv;
  170. FunType( Type *ty,int at=0 );
  171. FunType *funType();
  172. string encoding();
  173. string toString();
  174. bool equals( Type *ty );
  175. bool extends( Type *ty );
  176. bool method();
  177. };
  178. struct PtrType : public Type{
  179. Type *val_type;
  180. string sourceinfo;
  181. PtrType( Type *ty );
  182. PtrType *ptrType();
  183. void resolve();
  184. string encoding();
  185. string toString();
  186. bool equals( Type *ty );
  187. };
  188. struct VarType : public Type{
  189. Type *val_type;
  190. VarType( Type *ty ):val_type(ty){}
  191. VarType *varType();
  192. string encoding();
  193. string toString();
  194. bool equals( Type *ty );
  195. };
  196. struct AliasType : public Type{
  197. Type *val_type;
  198. AliasType( Type *ty );
  199. NumericType*numericType();
  200. IntType* intType();
  201. FloatType* floatType();
  202. StringType* stringType();
  203. ClassType* classType();
  204. ObjectType* objectType();
  205. ObjectType* exObjectType();
  206. ArrayType* arrayType();
  207. FunType* funType();
  208. PtrType* ptrType();
  209. ModuleType* moduleType();
  210. string encoding();
  211. string toString();
  212. bool equals( Type *ty );
  213. bool extends( Type *ty );
  214. Val* find( string id );
  215. };
  216. struct RefType : public AliasType{
  217. enum{
  218. VARPARAM=1
  219. };
  220. int attrs;
  221. RefType( Type *ty,int at=0 ):AliasType(ty),attrs(at){}
  222. RefType* refType();
  223. };
  224. struct NullType : public Type{
  225. NullType *nullType();
  226. string toString();
  227. bool equals( Type *ty );
  228. };
  229. struct ModuleType : public Type{
  230. DeclSeq decls;
  231. ModuleType* moduleType();
  232. string toString();
  233. bool equals( Type *ty );
  234. Val* find( string id );
  235. };
  236. #endif