decl.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "std.h"
  2. #include "decl.h"
  3. #include "exp.h"
  4. #include "../codegen/cgdebug.h"
  5. static vector<ConstDecl*> _constDecls;
  6. static vector<FunDecl*> _funDecls;
  7. //********************* Decl **********************
  8. Decl::Decl( string id,Val *v ):ident(id),val(v){
  9. }
  10. Decl::Decl( string id,Type *ty,CGExp *cg ):ident(id),val(new Val(ty,cg)){
  11. }
  12. void Decl::setMetaData( string meta ){
  13. this->meta=meta;
  14. }
  15. string Decl::debugEncoding(){
  16. Type *t=val->type;
  17. string e=t->encoding();
  18. if( e.size() && e[0]==':' && t->exObjectType() ) e='?'+e.substr(1);
  19. if( meta.size() ){
  20. e+="{"+meta+"}";
  21. }
  22. return e;
  23. }
  24. void Decl::resolveDecls(){
  25. int k;
  26. if( opt_verbose ) cout<<"Resolving const decls..."<<endl;
  27. for( k=0;k<_constDecls.size();++k ) _constDecls[k]->resolve();
  28. if( opt_verbose ) cout<<"Resolving fun decls..."<<endl;
  29. for( k=0;k<_funDecls.size();++k ) _funDecls[k]->resolve();
  30. }
  31. void Decl::debugDecl( CGDat *d,int blockKind ){
  32. CGExp *e=val->cg_exp;
  33. if( !e ) return;
  34. if( val->constant() ){
  35. if( val->type->numericType() || val->type->stringType() ){
  36. bstring t=val->stringValue();
  37. d->push_back( CG::lit(1) );
  38. d->push_back( genCString(ident) );
  39. d->push_back( genCString(debugEncoding()) );
  40. d->push_back( genBBString2( t ) );
  41. /*
  42. CGDat *dt=CG::dat();
  43. dt->push_back( CG::sym("bbStringClass",CG_IMPORT) );
  44. dt->push_back( CG::lit(0x7ffffffe) ); //normally 0x7fffffff - 0x..fe indicates 'ignore for GC'.
  45. dt->push_back( CG::lit(t,CG_BSTRING) );
  46. d->push_back( dt );
  47. */
  48. /*
  49. d->push_back( CG::lit(1) );
  50. d->push_back( genCString(ident) );
  51. d->push_back( genCString(debugEncoding()) );
  52. d->push_back( val->cast(Type::stringObject)->cg_exp );
  53. */
  54. return;
  55. }
  56. }
  57. if( !val->type->refType() ){
  58. if( blockKind==2 && val->type->funType() ){
  59. //type method/function
  60. int dt=7;
  61. if( CGVfn *t=e->vfn() ){
  62. e=t->exp;
  63. dt=6;
  64. }
  65. if( CGMem *t=e->mem() ){
  66. d->push_back( CG::lit( dt ) );
  67. d->push_back( genCString(ident) );
  68. d->push_back( genCString(debugEncoding()) );
  69. d->push_back( CG::lit( t->offset ) );
  70. }else if( CGSym *t=e->sym() ){
  71. d->push_back( CG::lit( dt ) );
  72. d->push_back( genCString(ident) );
  73. d->push_back( genCString(debugEncoding()) );
  74. d->push_back( t );
  75. }
  76. }
  77. return;
  78. }
  79. if( CGTmp *t=e->tmp() ){
  80. d->push_back( CG::lit(2) );
  81. d->push_back( genCString(ident) );
  82. d->push_back( genCString(debugEncoding()) );
  83. d->push_back( CG::lea(t) );
  84. return;
  85. }
  86. if( CGMem *t=e->mem() ){
  87. if( t->exp->tmp() ){
  88. if( blockKind==2 ){
  89. //field
  90. d->push_back( CG::lit(3) );
  91. d->push_back( genCString(ident) );
  92. d->push_back( genCString(debugEncoding()) );
  93. d->push_back( CG::lit(t->offset) );
  94. }else{
  95. d->push_back( CG::lit(5) );
  96. d->push_back( genCString(ident) );
  97. d->push_back( genCString(debugEncoding()) );
  98. d->push_back( CG::lea(t->exp->tmp()) );
  99. }
  100. return;
  101. }else if( t->exp->sym() && !t->offset ){
  102. d->push_back( CG::lit(4) );
  103. d->push_back( genCString(ident) );
  104. d->push_back( genCString(debugEncoding()) );
  105. d->push_back( t->exp );
  106. return;
  107. }
  108. }
  109. return;
  110. }
  111. //***************** fun Decl ********************
  112. FunDecl::FunDecl( string id,FunType *ty,CGExp *cg,Scope *sc,ExpSeq *defs ):Decl( id,ty,cg ),scope(sc),defaults(defs){
  113. sourceinfo=source_info;
  114. _funDecls.push_back( this );
  115. }
  116. void FunDecl::resolve(){
  117. source_info=sourceinfo;
  118. FunType *fun=val->type->funType();
  119. ClassBlock *class_block=dynamic_cast<ClassBlock*>(scope);
  120. if( class_block ){
  121. if( Val *v=class_block->type->findSuperMethod( ident ) ){
  122. FunType *t=v->type->funType();
  123. if( t->attrs & FunType::FINAL ){
  124. fail( "Final methods cannot be overridden" );
  125. }
  126. if( !fun->extends(t) ){
  127. fail( "Overriding method differs by type" );
  128. }
  129. }
  130. }
  131. if( defaults ){
  132. int k;
  133. for( k=0;k<defaults->size();++k ){
  134. Exp *e=(*defaults)[k];
  135. if( !e ) continue;
  136. Val *v=e->eval(scope,fun->args[k]->val->type);
  137. if( !v->constant() ) fail( "Function defaults must be constant" );
  138. fun->args[k]->val->cg_exp=v->cg_exp;
  139. }
  140. }
  141. if( !val->cg_exp ){
  142. string id=ident;
  143. if( fun->call_conv==CG_STDCALL ){
  144. int sz=0;
  145. for( int k=0;k<fun->args.size();++k ){
  146. int n=fun->args[k]->val->type->size();
  147. sz+=n>4 ? n : 4;
  148. }
  149. id+="@"+fromint(sz);
  150. }
  151. val->cg_exp=CG::sym( id,CG_IMPORT );
  152. }
  153. }
  154. //****************** ConstDecl *********************
  155. ConstDecl::ConstDecl( string id,Type *ty,Scope *sc,Exp *e ):Decl(id,ty,0),scope(sc),exp(e){
  156. sourceinfo=source_info;
  157. _constDecls.push_back(this);
  158. }
  159. void ConstDecl::resolve(){
  160. source_info=sourceinfo;
  161. Val *v=exp->eval(scope,val->type);
  162. if( !v->constant() ) fail( "Constant initializers must be constant" );
  163. val->cg_exp=v->cg_exp;
  164. }