type.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. #include "std.h"
  2. #include "decl.h"
  3. using namespace CG;
  4. static vector<ClassType*> _classTypes;
  5. static vector<ObjectType*> _objectTypes;
  6. static vector<PtrType*> _ptrTypes;
  7. IntType *Type::int8;
  8. IntType *Type::int16;
  9. IntType *Type::int32;
  10. IntType *Type::int64;
  11. FloatType *Type::float32;
  12. FloatType *Type::float64;
  13. CStringType *Type::c_string;
  14. WStringType *Type::w_string;
  15. PtrType *Type::bytePtr;
  16. NullType *Type::null;
  17. ModuleType *Type::blitzModule;
  18. ObjectType *Type::objectObject;
  19. StringType *Type::stringObject;
  20. Val *Type::objectClass,*Type::stringClass,*Type::arrayClass;
  21. void Type::createTypes(){
  22. int8=new IntType(1);
  23. int16=new IntType(2);
  24. int32=new IntType(4);
  25. int64=new IntType(8);
  26. float32=new FloatType(4);
  27. float64=new FloatType(8);
  28. c_string=new CStringType();
  29. w_string=new WStringType();
  30. bytePtr=new PtrType(new IntType(1));
  31. null=new NullType();
  32. objectClass=new Val((Type*)0,(CGExp*)0);
  33. objectObject=new ObjectType(0);
  34. objectObject->ident="Object";
  35. objectObject->class_val=objectClass;
  36. stringClass=new Val((Type*)0,(CGExp*)0);
  37. stringObject=new StringType(0);
  38. stringObject->ident="String";
  39. stringObject->class_val=stringClass;
  40. arrayClass=new Val((Type*)0,(CGExp*)0);
  41. blitzModule=new ModuleType();
  42. }
  43. void Type::resolveTypes(){
  44. int k;
  45. for( k=0;k<_classTypes.size();++k ) _classTypes[k]->resolve();
  46. for( k=0;k<_objectTypes.size();++k ) _objectTypes[k]->resolve();
  47. for( k=0;k<_ptrTypes.size();++k ) _ptrTypes[k]->resolve();
  48. }
  49. //********************* Type **********************
  50. Type::~Type(){
  51. }
  52. NumericType *Type::numericType(){
  53. return 0;
  54. }
  55. IntType *Type::intType(){
  56. return 0;
  57. }
  58. FloatType *Type::floatType(){
  59. return 0;
  60. }
  61. StringType *Type::stringType(){
  62. return 0;
  63. }
  64. CStringType *Type::cstringType(){
  65. return 0;
  66. }
  67. WStringType *Type::wstringType(){
  68. return 0;
  69. }
  70. ArrayType *Type::arrayType(){
  71. return 0;
  72. }
  73. ClassType *Type::classType(){
  74. return 0;
  75. }
  76. ObjectType *Type::objectType(){
  77. return 0;
  78. }
  79. ObjectType *Type::exObjectType(){
  80. return 0;
  81. }
  82. FunType *Type::funType(){
  83. return 0;
  84. }
  85. PtrType *Type::ptrType(){
  86. return 0;
  87. }
  88. VarType *Type::varType(){
  89. return 0;
  90. }
  91. RefType *Type::refType(){
  92. return 0;
  93. }
  94. NullType *Type::nullType(){
  95. return 0;
  96. }
  97. ModuleType *Type::moduleType(){
  98. return 0;
  99. }
  100. string Type::encoding(){
  101. return "?";
  102. }
  103. string Type::toString(){
  104. return "<void>";
  105. }
  106. bool Type::equals( Type *ty ){
  107. return false;
  108. }
  109. bool Type::extends( Type *ty ){
  110. return equals(ty);
  111. }
  112. int Type::size(){
  113. switch( cgType() ){
  114. case CG_VOID:return 0;
  115. case CG_INT8:return 1;
  116. case CG_INT16:return 2;
  117. case CG_INT32:return 4;
  118. case CG_INT64:return 8;
  119. case CG_FLOAT32:return 4;
  120. case CG_FLOAT64:return 8;
  121. case CG_PTR:return 4;
  122. }
  123. assert(0);
  124. return 0;
  125. }
  126. int Type::cgType(){
  127. switch( encoding()[0] ){
  128. case '?':return CG_VOID;
  129. case 'b':return CG_INT8;
  130. case 's':return CG_INT16;
  131. case 'i':return CG_INT32;
  132. case 'l':return CG_INT64;
  133. case 'f':return CG_FLOAT32;
  134. case 'd':return CG_FLOAT64;
  135. }
  136. return CG_PTR;
  137. }
  138. PtrType *Type::ptrType( string valEncoding ){
  139. PtrType *p=ptrType();
  140. return (p && p->val_type->encoding()==valEncoding) ? p : 0;
  141. }
  142. //****************** NumericType ******************
  143. NumericType::NumericType( int sz,bool fp ){
  144. switch( sz ){
  145. case 1:assert(!fp);_encoding="b";break;
  146. case 2:assert(!fp);_encoding="s";break;
  147. case 4:_encoding=fp ? "f" : "i";break;
  148. case 8:_encoding=fp ? "d" : "l";break;
  149. default:assert(0);
  150. }
  151. }
  152. NumericType *NumericType::numericType(){
  153. return this;
  154. }
  155. string NumericType::encoding(){
  156. return _encoding;
  157. }
  158. string NumericType::toString(){
  159. switch( encoding()[0] ){
  160. case 'b':return "Byte";
  161. case 's':return "Short";
  162. case 'i':return "Int";
  163. case 'l':return "Long";
  164. case 'f':return "Float";
  165. case 'd':return "Double";
  166. default:assert(0);
  167. }
  168. return "";
  169. }
  170. bool NumericType::equals( Type *ty ){
  171. NumericType *t=ty->numericType();
  172. return t && encoding()==t->encoding();
  173. }
  174. //******************** IntType ********************
  175. IntType *IntType::intType(){
  176. return this;
  177. }
  178. //******************* FloatType *******************
  179. FloatType *FloatType::floatType(){
  180. return this;
  181. }
  182. //***************** CStringType *******************
  183. CStringType *CStringType::cstringType(){
  184. return this;
  185. }
  186. string CStringType::encoding(){
  187. return "z";
  188. }
  189. string CStringType::toString(){
  190. return "CString";
  191. }
  192. bool CStringType::equals( Type *ty ){
  193. return ty->cstringType() ? true : false;
  194. }
  195. //***************** WStringType *******************
  196. WStringType *WStringType::wstringType(){
  197. return this;
  198. }
  199. string WStringType::encoding(){
  200. return "w";
  201. }
  202. string WStringType::toString(){
  203. return "WString";
  204. }
  205. bool WStringType::equals( Type *ty ){
  206. return ty->wstringType() ? true : false;
  207. }
  208. //******************* StringType ******************
  209. StringType::StringType( Val *clas ):ObjectType(clas){
  210. }
  211. StringType *StringType::stringType(){
  212. return this;
  213. }
  214. string StringType::encoding(){
  215. return "$";
  216. }
  217. string StringType::toString(){
  218. return "String";
  219. }
  220. bool StringType::equals( Type *ty ){
  221. return ty->stringType() ? true : false;
  222. }
  223. //****************** ArrayType ********************
  224. ArrayType::ArrayType( Type *ty,int n ):ObjectType(arrayClass),element_type(ty),dims(n){
  225. }
  226. ArrayType *ArrayType::arrayType(){
  227. return this;
  228. }
  229. string ArrayType::encoding(){
  230. return "["+string(dims-1,',')+"]"+element_type->encoding();
  231. }
  232. string ArrayType::toString(){
  233. return element_type->toString()+" Array";
  234. }
  235. bool ArrayType::equals( Type *ty ){
  236. ArrayType *t=ty->arrayType();
  237. return t && dims==t->dims && element_type->equals(t->element_type);
  238. }
  239. bool ArrayType::extends( Type *ty ){
  240. ArrayType *t=ty->arrayType();
  241. if( !t ) return ObjectType::extends( ty );
  242. return t && dims==t->dims && element_type->extends(t->element_type);
  243. }
  244. //****************** ClassType ********************
  245. ClassType::ClassType( string supername,Scope *sc,int attrs ):
  246. super_name(supername),scope(sc),attrs(attrs),
  247. sizeof_fields(0),sizeof_vtable(0),super_val(0),super_class(0),resolved(0){
  248. sourceinfo=source_info;
  249. _classTypes.push_back(this);
  250. }
  251. void ClassType::resolve(){
  252. if( resolved==1 ) return;
  253. source_info=sourceinfo;
  254. if( resolved==-1 ) fail( "Cyclic type dependancy" );
  255. resolved=-1;
  256. //resolve super
  257. if( super_name.size() ){
  258. if( super_val=scope->findTypeIdent( super_name ) ){
  259. super_class=super_val->type->classType();
  260. }
  261. if( !super_class ) badty( super_name );
  262. super_class->resolve();
  263. if( super_class->attrs & ClassType::FINAL ) fail( "Final types cannot be extended" );
  264. if( (attrs & ClassType::EXTERN) && !(super_class->attrs & ClassType::EXTERN) ) fail( "Extern types can only extends other extern types" );
  265. if( (super_class->attrs & ClassType::EXTERN) && !(attrs & ClassType::EXTERN) ) fail( "Extern types can only be extended by other extern types" );
  266. sizeof_fields=super_class->sizeof_fields;
  267. sizeof_vtable=super_class->sizeof_vtable;
  268. }else if( attrs & EXTERN ){
  269. sizeof_fields=4;
  270. sizeof_vtable=0;
  271. }else{
  272. sizeof_fields=8;
  273. sizeof_vtable=16;
  274. }
  275. //resolve fields
  276. int k;
  277. for( k=0;k<fields.size();++k ){
  278. Decl *d=fields[k];
  279. Type *type=d->val->type;
  280. int sz=type->size();
  281. sizeof_fields=(sizeof_fields+sz-1)/sz*sz;
  282. CGExp *e=mem( type->cgType(),tmp(CG_PTR,"@self"),sizeof_fields );
  283. Decl *t=new Decl( d->ident,type,e );
  284. t->setMetaData( d->meta );
  285. decls.push_back( t );
  286. sizeof_fields+=sz;
  287. }
  288. //resolve methods
  289. for( k=0;k<methods.size();++k ){
  290. Decl *d=methods[k];
  291. FunType *type=d->val->type->funType();
  292. CGExp *e;
  293. Val *v=findSuperMethod(d->ident);
  294. if( (attrs & FINAL) || (type->attrs & FunType::FINAL) ){
  295. e=d->val->cg_exp;
  296. if( type->method() ) e=vfn( e,tmp(CG_PTR,"@self") );
  297. if( !v ) sizeof_vtable+=4;
  298. }else if( v ){
  299. e=v->cg_exp;
  300. }else{
  301. e=mem( CG_PTR,tmp(CG_PTR,"@type"),sizeof_vtable );
  302. if( type->method() ) e=vfn( e,tmp(CG_PTR,"@self") );
  303. sizeof_vtable+=4;
  304. }
  305. Decl *t=new Decl( d->ident,type,e );
  306. t->setMetaData( d->meta );
  307. decls.push_back( t );
  308. }
  309. resolved=1;
  310. }
  311. ClassType *ClassType::classType(){
  312. return this;
  313. }
  314. Val *ClassType::superVal(){
  315. return super_val;
  316. }
  317. ClassType *ClassType::superClass(){
  318. return super_class;
  319. }
  320. string ClassType::encoding(){
  321. return "^";
  322. }
  323. string ClassType::toString(){
  324. return "Type";
  325. }
  326. bool ClassType::equals( Type *ty ){
  327. return this==ty->classType();
  328. }
  329. bool ClassType::extends( Type *ty ){
  330. ClassType *c=ty->classType();
  331. if( !c ) return false;
  332. ClassType *t;
  333. for( t=this;t;t=t->super_class ){
  334. if( t==ty ) return true;
  335. }
  336. return false;
  337. }
  338. Val *ClassType::find( string id ){
  339. ClassType *t;
  340. for( t=this;t;t=t->superClass() ){
  341. Val *v=t->decls.find(id);
  342. if( v && !v->countTmps( "@self" ) ) return v;
  343. }
  344. return 0;
  345. }
  346. Val *ClassType::findMethod( string id ){
  347. ClassType *t;
  348. for( t=this;t;t=t->super_class ){
  349. if( t->methods.find(id) ) return t->decls.find(id);
  350. }
  351. return 0;
  352. }
  353. Val *ClassType::findSuperMethod( string id ){
  354. return super_class ? super_class->findMethod( id ) : 0;
  355. }
  356. //****************** ObjectType *******************
  357. ObjectType::ObjectType( Val *clas ):ident("<unknown>"),scope(0),class_val(clas){
  358. }
  359. ObjectType::ObjectType( string id,Scope *sc ):ident(id),scope(sc),class_val(0){
  360. sourceinfo=source_info;
  361. _objectTypes.push_back( this );
  362. }
  363. ObjectType *ObjectType::objectType(){
  364. if( objectClass()->attrs & ClassType::EXTERN ) return 0;
  365. return this;
  366. }
  367. ObjectType *ObjectType::exObjectType(){
  368. if( objectClass()->attrs & ClassType::EXTERN ) return this;
  369. return 0;
  370. }
  371. void ObjectType::resolve(){
  372. if( class_val ) return;
  373. source_info=sourceinfo;
  374. class_val=scope->findTypeIdent( ident );
  375. if( !class_val ) badid( ident );
  376. if( !class_val->type->classType() ) fail( "expecting type name" );
  377. }
  378. string ObjectType::encoding(){
  379. // if( objectClass()->attrs & ClassType::EXTERN ) return "?"+ident;
  380. return ":"+ident;
  381. }
  382. string ObjectType::toString(){
  383. return ident;
  384. }
  385. bool ObjectType::equals( Type *ty ){
  386. ObjectType *o=objectType() ? ty->objectType() : ty->exObjectType();
  387. return o ? objectClass()==o->objectClass() : false;
  388. }
  389. bool ObjectType::extends( Type *ty ){
  390. ObjectType *o=objectType() ? ty->objectType() : ty->exObjectType();
  391. return o && objectClass()->extends(o->objectClass());
  392. }
  393. Val *ObjectType::find( string id ){
  394. ClassType *t;
  395. for( t=objectClass();t;t=t->superClass() ){
  396. if( Val *v=t->decls.find(id) ) return v;
  397. }
  398. return 0;
  399. }
  400. ClassType *ObjectType::objectClass(){
  401. return class_val->type->classType();
  402. }
  403. //******************** FunType ********************
  404. FunType::FunType( Type *ty,int attrs ):return_type(ty),attrs(attrs),call_conv(CG_CDECL){
  405. }
  406. FunType *FunType::funType(){
  407. return this;
  408. }
  409. string FunType::encoding(){
  410. string t="(";
  411. for( int k=0;k<args.size();++k ){
  412. if( k ) t+=",";
  413. t+=args[k]->val->type->encoding();
  414. }
  415. return t+")"+return_type->encoding();
  416. }
  417. string FunType::toString(){
  418. string t=return_type->toString()+"(";
  419. for( int k=0;k<args.size();++k ){
  420. if( k ) t+=",";
  421. t+=args[k]->val->type->toString();
  422. }
  423. return t+")";
  424. }
  425. bool FunType::equals( Type *ty ){
  426. FunType *f=ty->funType();
  427. if( !f ) return false;
  428. if( method()!=f->method() || args.size()!=f->args.size() ) return false;
  429. if( !return_type->equals(f->return_type) ) return false;
  430. for( int k=0;k<args.size();++k ){
  431. if( !args[k]->val->type->equals(f->args[k]->val->type) ) return false;
  432. }
  433. return true;
  434. }
  435. bool FunType::extends( Type *ty ){
  436. FunType *f=ty->funType();
  437. if( !f) return false;
  438. if( method()!=f->method() || args.size()!=f->args.size() ) return false;
  439. if( ObjectType *t=return_type->objectType() ){
  440. ObjectType *p=f->return_type->objectType();
  441. if( !p || !t->objectClass()->extends(p->objectClass()) ) return false;
  442. }else{
  443. if( !return_type->equals(f->return_type) ) return false;
  444. }
  445. for( int k=0;k<args.size();++k ){
  446. if( !args[k]->val->type->equals(f->args[k]->val->type) ) return false;
  447. }
  448. return true;
  449. }
  450. bool FunType::method(){
  451. return !!(attrs & METHOD);
  452. }
  453. //******************** PtrType ********************
  454. PtrType::PtrType( Type *ty ):val_type(ty){
  455. _ptrTypes.push_back( this );
  456. sourceinfo=source_info;
  457. }
  458. PtrType *PtrType::ptrType(){
  459. return this;
  460. }
  461. void PtrType::resolve(){
  462. source_info=sourceinfo;
  463. if( val_type->ptrType() || val_type->numericType() || val_type->exObjectType() ) return;
  464. fail( "Illegal pointer type" );
  465. }
  466. string PtrType::encoding(){
  467. return "*"+val_type->encoding();
  468. }
  469. string PtrType::toString(){
  470. return val_type->toString()+ " Ptr";
  471. }
  472. bool PtrType::equals( Type *ty ){
  473. PtrType *t=ty->ptrType();
  474. return t && val_type->equals(t->val_type);
  475. }
  476. //******************** VarType ********************
  477. VarType *VarType::varType(){
  478. return this;
  479. }
  480. string VarType::encoding(){
  481. return "*"+val_type->encoding();
  482. }
  483. string VarType::toString(){
  484. return val_type->toString()+" Var";
  485. }
  486. bool VarType::equals( Type *ty ){
  487. VarType *t=ty->varType();
  488. return t && val_type->equals(t->val_type);
  489. }
  490. //******************** AliasType ********************
  491. AliasType::AliasType( Type *ty ):val_type(ty){
  492. }
  493. NumericType *AliasType::numericType(){
  494. return val_type->numericType();
  495. }
  496. IntType *AliasType::intType(){
  497. return val_type->intType();
  498. }
  499. FloatType *AliasType::floatType(){
  500. return val_type->floatType();
  501. }
  502. ClassType *AliasType::classType(){
  503. return val_type->classType();
  504. }
  505. ObjectType *AliasType::objectType(){
  506. return val_type->objectType();
  507. }
  508. ObjectType *AliasType::exObjectType(){
  509. return val_type->exObjectType();
  510. }
  511. StringType *AliasType::stringType(){
  512. return val_type->stringType();
  513. }
  514. ArrayType *AliasType::arrayType(){
  515. return val_type->arrayType();
  516. }
  517. FunType *AliasType::funType(){
  518. return val_type->funType();
  519. }
  520. PtrType *AliasType::ptrType(){
  521. return val_type->ptrType();
  522. }
  523. ModuleType *AliasType::moduleType(){
  524. return val_type->moduleType();
  525. }
  526. string AliasType::encoding(){
  527. return val_type->encoding();
  528. }
  529. string AliasType::toString(){
  530. return val_type->toString();
  531. }
  532. bool AliasType::equals( Type *ty ){
  533. return val_type->equals(ty);
  534. }
  535. bool AliasType::extends( Type *ty ){
  536. return val_type->extends(ty);
  537. }
  538. Val *AliasType::find( string id ){
  539. return val_type->find(id);
  540. }
  541. //***************** Reference Type ****************
  542. RefType *RefType::refType(){
  543. return this;
  544. }
  545. //***************** Null Type *********************
  546. NullType *NullType::nullType(){
  547. return this;
  548. }
  549. string NullType::toString(){
  550. return "null";
  551. }
  552. bool NullType::equals( Type *ty ){
  553. return !!ty->nullType();
  554. }
  555. //***************** Module Type *******************
  556. ModuleType *ModuleType::moduleType(){
  557. return this;
  558. }
  559. string ModuleType::toString(){
  560. return "module";
  561. }
  562. bool ModuleType::equals( Type *ty ){
  563. return this==ty;
  564. }
  565. Val *ModuleType::find( string id ){
  566. return decls.find(id);
  567. }