bbtypeinfo.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "bbtypeinfo.h"
  2. #include "bbdeclinfo.h"
  3. namespace{
  4. bbClassTypeInfo *_classes;
  5. }
  6. #define BB_PRIM_GETTYPE( TYPE,ID ) bbTypeInfo *bbGetType( TYPE const& ){ \
  7. static bbPrimTypeInfo info( ID ); \
  8. return &info; \
  9. }
  10. BB_PRIM_GETTYPE( bbBool,"Bool" )
  11. BB_PRIM_GETTYPE( bbByte,"Byte" )
  12. BB_PRIM_GETTYPE( bbUByte,"UShort" )
  13. BB_PRIM_GETTYPE( bbShort,"Short" )
  14. BB_PRIM_GETTYPE( bbUShort,"UShort" )
  15. BB_PRIM_GETTYPE( bbInt,"Int" )
  16. BB_PRIM_GETTYPE( bbUInt,"UInt" )
  17. BB_PRIM_GETTYPE( bbLong,"Long" )
  18. BB_PRIM_GETTYPE( bbULong,"ULong" )
  19. BB_PRIM_GETTYPE( bbFloat,"Float" )
  20. BB_PRIM_GETTYPE( bbDouble,"Double" )
  21. BB_PRIM_GETTYPE( bbString,"String" )
  22. BB_PRIM_GETTYPE( bbCString,"CString" )
  23. BB_PRIM_GETTYPE( bbVariant,"Variant" )
  24. // ***** bbTypeInfo *****
  25. bbString bbTypeInfo::toString(){
  26. return name;
  27. }
  28. bbTypeInfo *bbTypeInfo::pointeeType(){
  29. bbRuntimeError( "Type '"+name+"' is not a pointer type" );
  30. return 0;
  31. }
  32. bbTypeInfo *bbTypeInfo::elementType(){
  33. bbRuntimeError( "Type '"+name+"' is not an array type" );
  34. return 0;
  35. }
  36. int bbTypeInfo::arrayRank(){
  37. bbRuntimeError( "Type '"+name+"' is not an array type" );
  38. return 0;
  39. }
  40. bbTypeInfo *bbTypeInfo::returnType(){
  41. bbRuntimeError( "Type '"+name+"' is not a function type" );
  42. return 0;
  43. }
  44. bbArray<bbTypeInfo*> bbTypeInfo::paramTypes(){
  45. bbRuntimeError( "Type '"+name+"' is not a function type" );
  46. return {};
  47. }
  48. bbTypeInfo *bbTypeInfo::superType(){
  49. bbRuntimeError( "Type '"+name+"' is not a class type" );
  50. return 0;
  51. }
  52. bbArray<bbTypeInfo*> bbTypeInfo::interfaceTypes(){
  53. bbRuntimeError( "Type '"+name+"' is not a class or interface type" );
  54. return {};
  55. }
  56. bbBool bbTypeInfo::extendsType( bbTypeInfo *type ){
  57. bbRuntimeError( "Type '"+name+"' is not a class or interface type" );
  58. return false;
  59. }
  60. bbArray<bbDeclInfo*> bbTypeInfo::getDecls(){
  61. bbRuntimeError( "Type '"+name+"' is not a class or interface type" );
  62. return {};
  63. }
  64. bbDeclInfo *bbTypeInfo::getDecl( bbString name ){
  65. bbArray<bbDeclInfo*> decls=getDecls();
  66. bbDeclInfo *found=0;
  67. for( int i=0;i<decls.length();++i ){
  68. bbDeclInfo *decl=decls[i];
  69. if( decl->name!=name ) continue;
  70. if( found ) return 0;
  71. found=decl;
  72. }
  73. return found;
  74. }
  75. bbArray<bbDeclInfo*> bbTypeInfo::getDecls( bbString name ){
  76. bbArray<bbDeclInfo*> decls=getDecls();
  77. int n=0;
  78. for( int i=0;i<decls.length();++i ){
  79. if( decls[i]->name==name ) ++n;
  80. }
  81. if( !n ) return {};
  82. bbArray<bbDeclInfo*> rdecls;
  83. int j=0;
  84. for( int i=0;i<decls.length();++i ){
  85. if( decls[i]->name==name) rdecls[j++]=decls[i];
  86. }
  87. return rdecls;
  88. }
  89. bbDeclInfo *bbTypeInfo::getDecl( bbString name,bbTypeInfo *type ){
  90. bbArray<bbDeclInfo*> decls=getDecls();
  91. for( int i=0;i<decls.length();++i ){
  92. bbDeclInfo *decl=decls[i];
  93. if( decl->name==name && decl->type==type ) return decl;
  94. }
  95. return 0;
  96. }
  97. bbTypeInfo *bbTypeInfo::getType( bbString cname ){
  98. for( bbClassTypeInfo *c=_classes;c;c=c->_succ ){
  99. if( c->name==cname ) return c;
  100. }
  101. return 0;
  102. }
  103. bbArray<bbTypeInfo*> bbTypeInfo::getTypes(){
  104. int n=0;
  105. for( bbClassTypeInfo *c=_classes;c;c=c->_succ ) ++n;
  106. bbArray<bbTypeInfo*> types( n );
  107. int i=0;
  108. for( bbClassTypeInfo *c=_classes;c;c=c->_succ ) types[i++]=c;
  109. return types;
  110. }
  111. // ***** bbUnknownTypeInfo *****
  112. bbUnknownTypeInfo::bbUnknownTypeInfo(){
  113. this->name=BB_T("Unknown@")+bbString( bbLong( this ) );
  114. this->kind="Unknown";
  115. }
  116. // ***** bbVoidTypeInfo *****
  117. bbVoidTypeInfo bbVoidTypeInfo::instance;
  118. bbVoidTypeInfo::bbVoidTypeInfo(){
  119. this->name="Void";
  120. this->kind="Void";
  121. }
  122. // ***** bbObjectTypeInfo *****
  123. bbObjectTypeInfo bbObjectTypeInfo::instance;
  124. bbObjectTypeInfo::bbObjectTypeInfo(){
  125. this->name="Object";
  126. this->kind="Class";
  127. }
  128. bbTypeInfo *bbObjectTypeInfo::superType(){
  129. return 0;
  130. }
  131. bbBool bbObjectTypeInfo::extendsType( bbTypeInfo *type ){
  132. return type==&instance;
  133. }
  134. bbArray<bbDeclInfo*> bbObjectTypeInfo::getDecls(){
  135. return {};
  136. }
  137. bbTypeInfo *bbObject::typeof()const{
  138. return &bbObjectTypeInfo::instance;
  139. }
  140. // ***** bbPrimTypeInfo *****
  141. bbPrimTypeInfo::bbPrimTypeInfo( bbString name ){
  142. this->name=name;
  143. this->kind="Primitive";
  144. }
  145. // ***** bbClassDecls *****
  146. bbClassDecls::bbClassDecls( bbClassTypeInfo *classType ){
  147. _succ=classType->_decls;
  148. classType->_decls=this;
  149. }
  150. bbDeclInfo **bbClassDecls::decls(){
  151. if( !_decls ){
  152. _decls=initDecls();
  153. bbDeclInfo **p=_decls;
  154. while( *p ) ++p;
  155. _numDecls=p-_decls;
  156. }
  157. return _decls;
  158. }
  159. int bbClassDecls::numDecls(){
  160. if( !_decls ) decls();
  161. return _numDecls;
  162. }
  163. // ***** bbClassTypeInfo *****
  164. bbClassTypeInfo::bbClassTypeInfo( bbString name,bbString kind ){
  165. // printf( "ClassTypeInfo:%s\n",name.c_str() );
  166. this->name=name;
  167. this->kind=kind;
  168. _succ=_classes;
  169. _classes=this;
  170. }
  171. bbTypeInfo *bbClassTypeInfo::superType(){
  172. return 0;
  173. }
  174. bbArray<bbTypeInfo*> bbClassTypeInfo::interfaceTypes(){
  175. return {};
  176. }
  177. bbBool bbClassTypeInfo::extendsType( bbTypeInfo *type ){
  178. if( type==this ) return true;
  179. bbArray<bbTypeInfo*> ifaces=interfaceTypes();
  180. for( int i=0;i<ifaces.length();++i ){
  181. if( ifaces[i]->extendsType( type ) ) return true;
  182. }
  183. if( bbTypeInfo *super=superType() ) return super->extendsType( type );
  184. return false;
  185. }
  186. bbArray<bbDeclInfo*> bbClassTypeInfo::getDecls(){
  187. int n=0;
  188. for( bbClassDecls *m=_decls;m;m=m->_succ ) n+=m->numDecls();
  189. bbArray<bbDeclInfo*> rdecls( n );
  190. int i=0;
  191. for( bbClassDecls *m=_decls;m;m=m->_succ ){
  192. bbDeclInfo **decls=m->decls();
  193. int n=m->numDecls();
  194. for( int j=0;j<n;++j ) rdecls[i++]=decls[j];
  195. }
  196. return rdecls;
  197. }
  198. bbClassTypeInfo *bbClassTypeInfo::getNamespace( bbString name ){
  199. for( bbClassTypeInfo *nmspace=_classes;nmspace;nmspace=nmspace->_succ ){
  200. if( nmspace->name==name ) return nmspace;
  201. }
  202. bbClassTypeInfo *nmspace=new bbClassTypeInfo( name,"Namespace" );
  203. return nmspace;
  204. }