bbtypeinfo.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #ifndef BB_TYPEINFO_H
  2. #define BB_TYPEINFO_H
  3. #include "bbassert.h"
  4. #include "bbobject.h"
  5. #include "bbarray.h"
  6. #include "bbfunction.h"
  7. struct bbClassTypeInfo;
  8. struct bbTypeInfo{
  9. bbString name;
  10. bbString kind;
  11. bbString getName(){
  12. return name;
  13. }
  14. bbString getKind(){
  15. return kind;
  16. }
  17. virtual bbString toString();
  18. virtual bbTypeInfo *pointeeType();
  19. virtual bbTypeInfo *elementType();
  20. virtual int arrayRank();
  21. virtual bbTypeInfo *returnType();
  22. virtual bbArray<bbTypeInfo*> paramTypes();
  23. virtual bbTypeInfo *superType();
  24. virtual bbArray<bbTypeInfo*> interfaceTypes();
  25. virtual bbBool extendsType( bbTypeInfo *type );
  26. virtual bbArray<bbDeclInfo*> getDecls();
  27. bbDeclInfo *getDecl( bbString name );
  28. bbDeclInfo *getDecl( bbString name,bbTypeInfo *type );
  29. bbArray<bbDeclInfo*> getDecls( bbString name );
  30. static bbTypeInfo *getType( bbString cname );
  31. static bbArray<bbTypeInfo*> getTypes();
  32. };
  33. template<class T> bbTypeInfo *bbGetType();
  34. struct bbUnknownTypeInfo : public bbTypeInfo{
  35. bbUnknownTypeInfo();
  36. };
  37. struct bbVoidTypeInfo : public bbTypeInfo{
  38. static bbVoidTypeInfo instance;
  39. bbVoidTypeInfo();
  40. };
  41. struct bbObjectTypeInfo : public bbTypeInfo{
  42. static bbObjectTypeInfo instance;
  43. bbObjectTypeInfo();
  44. bbTypeInfo *superType();
  45. bbBool extendsType( bbTypeInfo *type );
  46. bbArray<bbDeclInfo*> getDecls();
  47. };
  48. struct bbPrimTypeInfo : public bbTypeInfo{
  49. bbPrimTypeInfo( bbString name );
  50. };
  51. template<class T> struct bbPointerTypeInfo : public bbTypeInfo{
  52. bbPointerTypeInfo(){
  53. this->name=bbGetType<T>()->name+" Ptr";
  54. this->kind="Pointer";
  55. }
  56. bbTypeInfo *pointeeType(){
  57. return bbGetType<T>();
  58. }
  59. };
  60. template<class T,int D> struct bbArrayTypeInfo : public bbTypeInfo{
  61. bbArrayTypeInfo(){
  62. this->name=bbGetType<T>()->name+"["+BB_T(",").dup(D-1)+"]";
  63. this->kind="Array";
  64. }
  65. bbTypeInfo *elementType(){
  66. return bbGetType<T>();
  67. }
  68. int arrayRank(){
  69. return D;
  70. }
  71. };
  72. template<class R,class...A> struct bbFunctionTypeInfo : public bbTypeInfo{
  73. bbFunctionTypeInfo(){
  74. this->name=bbGetType<R>()->name+"("+BB_T(",").join( bbArray<bbString>( { bbGetType<A>()->name... },int(sizeof...(A)) ) )+")";
  75. this->kind="Function";
  76. }
  77. bbTypeInfo *returnType(){
  78. return bbGetType<R>();
  79. }
  80. bbArray<bbTypeInfo*> paramTypes(){
  81. return bbArray<bbTypeInfo*>( { bbGetType<A>()... },int(sizeof...(A)) );
  82. }
  83. };
  84. template<class...A> struct bbFunctionTypeInfo<void,A...> : public bbTypeInfo{
  85. bbFunctionTypeInfo(){
  86. this->name=BB_T("Void(")+BB_T(",").join( bbArray<bbString>( { bbGetType<A>()->name... },int(sizeof...(A)) ) )+")";
  87. this->kind="Function";
  88. }
  89. bbTypeInfo *returnType(){
  90. return &bbVoidTypeInfo::instance;
  91. }
  92. bbArray<bbTypeInfo*> paramTypes(){
  93. return bbArray<bbTypeInfo*>( { bbGetType<A>()... },int(sizeof...(A)) );
  94. }
  95. };
  96. struct bbClassDecls{
  97. bbClassDecls *_succ;
  98. bbDeclInfo **_decls=0;
  99. int _numDecls=0;
  100. bbClassDecls( bbClassTypeInfo *classType );
  101. bbDeclInfo **decls();
  102. int numDecls();
  103. virtual bbDeclInfo **initDecls(){
  104. return 0;
  105. }
  106. };
  107. struct bbClassTypeInfo : public bbTypeInfo{
  108. bbClassTypeInfo *_succ=0;
  109. bbClassDecls *_decls=0;
  110. bbClassTypeInfo( bbString name,bbString kind );
  111. bbTypeInfo *superType();
  112. bbArray<bbTypeInfo*> interfaceTypes();
  113. bbBool extendsType( bbTypeInfo *type );
  114. bbArray<bbDeclInfo*> getDecls();
  115. bbString toString(){
  116. return kind+" "+name;
  117. }
  118. static bbClassTypeInfo *getNamespace( bbString name );
  119. };
  120. #define BB_GETTYPE_DECL( TYPE ) bbTypeInfo *bbGetType( TYPE const& );
  121. BB_GETTYPE_DECL( bbBool )
  122. BB_GETTYPE_DECL( bbByte )
  123. BB_GETTYPE_DECL( bbUByte )
  124. BB_GETTYPE_DECL( bbShort )
  125. BB_GETTYPE_DECL( bbUShort )
  126. BB_GETTYPE_DECL( bbInt )
  127. BB_GETTYPE_DECL( bbUInt )
  128. BB_GETTYPE_DECL( bbLong )
  129. BB_GETTYPE_DECL( bbULong )
  130. BB_GETTYPE_DECL( bbFloat )
  131. BB_GETTYPE_DECL( bbDouble )
  132. BB_GETTYPE_DECL( bbString )
  133. BB_GETTYPE_DECL( bbCString )
  134. BB_GETTYPE_DECL( bbVariant )
  135. inline bbTypeInfo *bbGetType( bbObject* const& ){
  136. return &bbObjectTypeInfo::instance;
  137. }
  138. template<class T> bbTypeInfo *bbGetType( T const& ){
  139. static bbUnknownTypeInfo info;
  140. return &info;
  141. }
  142. template<class T> bbTypeInfo *bbGetType( T* const& ){
  143. static bbPointerTypeInfo<T> info;
  144. return &info;
  145. }
  146. template<class T,int D> bbTypeInfo *bbGetType( bbArray<T,D> const& ){
  147. static bbArrayTypeInfo<T,D> info;
  148. return &info;
  149. }
  150. template<class R,class...A> bbTypeInfo *bbGetFuncType(){
  151. static bbFunctionTypeInfo<R,A...> info;
  152. return &info;
  153. }
  154. template<class R,class...A> bbTypeInfo *bbGetType( R(*)(A...) ){
  155. return bbGetFuncType<R,A...>();
  156. }
  157. template<class R,class...A> bbTypeInfo *bbGetType( bbFunction<R(A...)> const& ){
  158. return bbGetFuncType<R,A...>();
  159. }
  160. template<class T> bbTypeInfo *bbGetType( bbGCVar<T> const& ){
  161. return bbGetType<T*>();
  162. }
  163. template<> inline bbTypeInfo *bbGetType<void>(){
  164. return &bbVoidTypeInfo::instance;
  165. }
  166. template<class T> bbTypeInfo *bbGetType(){
  167. return bbGetType( *(T*)0 );
  168. }
  169. #endif