JSBHeaderVisitor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #pragma once
  5. #include <Atomic/IO/Log.h>
  6. #include <Atomic/Core/ProcessUtils.h>
  7. #include "JSBHeader.h"
  8. #include "JSBModule.h"
  9. #include "JSBindings.h"
  10. #include "JSBClass.h"
  11. #include "JSBFunction.h"
  12. #include "JSBNameVisitor.h"
  13. class JSBHeader;
  14. class JSBHeaderVisitor : public SymbolVisitor
  15. {
  16. JSBHeader* header_;
  17. JSBModule* module_;
  18. JSBindings* bindings_;
  19. TranslationUnit* unit_;
  20. Namespace* globalNamespace_;
  21. public:
  22. JSBHeaderVisitor(JSBHeader* header, TranslationUnit *unit, Namespace* globalNamespace) :
  23. header_(header),
  24. unit_(unit),
  25. globalNamespace_(globalNamespace)
  26. {
  27. module_ = header_->module_;
  28. bindings_ = module_->bindings_;
  29. accept(globalNamespace_);
  30. }
  31. String getNameString(const Name* name)
  32. {
  33. JSBNameVisitor nvisitor;
  34. return nvisitor(name);
  35. }
  36. JSBType* processTypeConversion(Type* type)
  37. {
  38. JSBType* jtype = NULL;
  39. if (type->isIntegerType())
  40. {
  41. IntegerType* itype = type->asIntegerType();
  42. jtype = new JSBPrimitiveType(itype->kind());
  43. }
  44. if (type->isFloatType())
  45. {
  46. jtype = new JSBPrimitiveType(JSBPrimitiveType::Float);
  47. }
  48. else if (type->isNamedType())
  49. {
  50. NamedType* ntype = type->asNamedType();
  51. String classname = getNameString(ntype->name());
  52. if (classname == "String")
  53. {
  54. jtype = new JSBStringType();
  55. }
  56. else if (classname == "StringHash")
  57. {
  58. jtype = new JSBStringHashType();
  59. }
  60. else if (classname == "JS_HEAP_PTR")
  61. {
  62. jtype = new JSBHeapPtrType();
  63. }
  64. else
  65. {
  66. JSBClass* jclass = bindings_->GetClass(classname);
  67. if (jclass)
  68. jtype = new JSBClassType(jclass);
  69. else
  70. {
  71. // this might be an enum
  72. JSBEnum* jenum = bindings_->GetEnum(classname);
  73. if (jenum)
  74. jtype = new JSBEnumType(jenum);
  75. }
  76. }
  77. }
  78. else if (type->asUndefinedType())
  79. {
  80. UndefinedType* utype = type->asUndefinedType();
  81. //ErrorExit("Undefined type");
  82. }
  83. return jtype;
  84. }
  85. JSBFunctionType* processFunctionType(FullySpecifiedType fst)
  86. {
  87. JSBType* jtype = NULL;
  88. Type* type = fst.type();
  89. bool isPointer = false;
  90. bool isSharedPtr = false;
  91. bool isReference = false;
  92. if (type->isPointerType())
  93. {
  94. isPointer=true;
  95. FullySpecifiedType pfst = type->asPointerType()->elementType();
  96. type = pfst.type();
  97. }
  98. if (type->isReferenceType())
  99. {
  100. isReference=true;
  101. FullySpecifiedType pfst = type->asReferenceType()->elementType();
  102. type = pfst.type();
  103. }
  104. if (fst.isUnsigned())
  105. {
  106. if (type->isUndefinedType())
  107. {
  108. // this happens when just using "unsigned" in code
  109. jtype = new JSBPrimitiveType(JSBPrimitiveType::Int, true);
  110. }
  111. }
  112. if (!jtype)
  113. jtype = processTypeConversion(type);
  114. if (!jtype)
  115. return NULL;
  116. // no pointers to prim atm
  117. if ((isPointer || isReference) && jtype->asPrimitiveType())
  118. return NULL;
  119. JSBFunctionType* ftype = new JSBFunctionType(jtype);
  120. ftype->isPointer_ = isPointer;
  121. ftype->isSharedPtr_ = isSharedPtr;
  122. ftype->isReference_ = isReference;
  123. return ftype;
  124. }
  125. JSBFunctionType* processFunctionArgType(Argument* arg)
  126. {
  127. JSBFunctionType* jtype = processFunctionType(arg->type());
  128. if (!jtype)
  129. return NULL;
  130. jtype->name_ = getNameString(arg->name());
  131. return jtype;
  132. }
  133. JSBFunctionType* processFunctionReturnType(Function* function)
  134. {
  135. if (!function->hasReturnType())
  136. {
  137. return NULL;
  138. }
  139. JSBFunctionType* jtype = processFunctionType(function->returnType());
  140. if (!jtype)
  141. return NULL;
  142. return jtype;
  143. }
  144. JSBFunction* processFunction(JSBClass* klass, Function* function)
  145. {
  146. JSBFunction* jfunction = new JSBFunction(klass);
  147. // don't ... atm
  148. if (function->isVariadic())
  149. return NULL;
  150. jfunction->name_ = getNameString(function->name());
  151. // don't support operators atm
  152. if (jfunction->name_.StartsWith("operator "))
  153. return NULL;
  154. if (jfunction->name_ == klass->GetClassName())
  155. jfunction->isConstructor_ = true;
  156. if (jfunction->name_.StartsWith("~"))
  157. jfunction->isDestructor_ = true;
  158. // see if we support return type
  159. if (function->hasReturnType() && !function->returnType().type()->isVoidType())
  160. {
  161. jfunction->returnType_ = processFunctionReturnType(function);
  162. if (!jfunction->returnType_)
  163. return NULL;
  164. }
  165. if (function->hasArguments())
  166. {
  167. for (unsigned i = 0; i < function->argumentCount(); i++)
  168. {
  169. Symbol* symbol = function->argumentAt(i);
  170. if (symbol->isArgument())
  171. {
  172. Argument* arg = symbol->asArgument();
  173. JSBFunctionType* ftype = processFunctionArgType(arg);
  174. if (!ftype)
  175. return NULL;
  176. if (arg->hasInitializer())
  177. {
  178. ftype->initializer_ = arg->initializer()->chars();
  179. if (ftype->initializer_.StartsWith("\\"))
  180. ftype->initializer_ = "\"" + ftype->initializer_ + "\"";
  181. }
  182. jfunction->AddParameter(ftype);
  183. }
  184. else
  185. {
  186. return NULL;
  187. }
  188. }
  189. }
  190. jfunction->sourceLocation_ = function->sourceLocation();
  191. jfunction->fileName_ = function->fileName();
  192. jfunction->sourceLine_ = function->line();
  193. jfunction->sourceColumn_ = function->column();
  194. //const Token &token = unit_->tokenAt(function->sourceLocation());
  195. //const char* source = unit_->firstSourceChar() + token.byteOffset;
  196. const char* comment = NULL;
  197. for (unsigned i = 0; i < unit_->commentCount(); i++)
  198. {
  199. const Token &tcomment = unit_->commentAt(i);
  200. unsigned line;
  201. unit_->getPosition(tcomment.utf16charOffset, &line);
  202. if (line == function->line() - 1)
  203. {
  204. comment = unit_->firstSourceChar() + tcomment.byteOffset;
  205. break;
  206. }
  207. }
  208. if (comment && strlen(comment) > 3)
  209. {
  210. if (comment[0] == '/' && comment[1] == '/' && comment[2] == '/')
  211. {
  212. int index = 3;
  213. while(comment[index] && comment[index] != '\n' && comment[index] != '\r')
  214. jfunction->docString_ += comment[index++];
  215. }
  216. }
  217. return jfunction;
  218. }
  219. virtual bool visit(Namespace *nspace)
  220. {
  221. String name = getNameString(nspace->name());
  222. // LOGINFOF("Namespace: %s", name.CString());
  223. return true;
  224. }
  225. // reject template types
  226. virtual bool visit(Template *t)
  227. {
  228. return false;
  229. }
  230. // enums visited in preprocessor visitor
  231. virtual bool visit(Enum *penum)
  232. {
  233. return false;
  234. }
  235. // global var decl or function
  236. virtual bool visit(Declaration* decl)
  237. {
  238. FullySpecifiedType dtype = decl->type();
  239. Type* type = dtype.type();
  240. if (type->isPointerType() || type->isReferenceType())
  241. return true;
  242. if (type->asEnumType())
  243. return true;
  244. if (!type->asFloatType() && !type->asIntegerType())
  245. return true;
  246. module_->RegisterConstant(getNameString(decl->name()).CString());
  247. return true;
  248. }
  249. virtual bool visit(Class *klass)
  250. {
  251. String name = getNameString(klass->name());
  252. JSBClass* jclass = bindings_->GetClass(name);
  253. if (!jclass)
  254. {
  255. return false;
  256. }
  257. jclass->SetHeader(header_);
  258. //LOGINFOF("Adding Class: %s to Module: %s", name.CString(), module_->name_.CString());
  259. jclass->SetModule(module_);
  260. module_->AddClass(jclass);
  261. for (unsigned i = 0; i < klass->baseClassCount(); i++)
  262. {
  263. BaseClass* baseclass = klass->baseClassAt(i);
  264. String baseclassname = getNameString(baseclass->name());
  265. JSBClass* base = bindings_->GetClass(baseclassname);
  266. if (!base)
  267. {
  268. LOGINFOF("Warning: %s baseclass %s not in bindings", name.CString(), baseclassname.CString());
  269. }
  270. else
  271. {
  272. jclass->AddBaseClass(base);
  273. }
  274. }
  275. for (unsigned i = 0; i < klass->memberCount(); i++)
  276. {
  277. Symbol* symbol = klass->memberAt(i);
  278. Declaration* decl = symbol->asDeclaration();
  279. // if the function describes the body in the header
  280. Function* function = symbol->asFunction();
  281. // otherwise it could be a decl
  282. if (!function && decl)
  283. function = decl->type()->asFunctionType();
  284. if (function)
  285. {
  286. if (function->isPureVirtual())
  287. jclass->setAbstract(true);
  288. // only want public functions
  289. if (!symbol->isPublic())
  290. continue;
  291. JSBFunction* jfunction = processFunction(jclass, function);
  292. if (jfunction)
  293. jclass->AddFunction(jfunction);
  294. }
  295. }
  296. // return false so we don't traverse the class members
  297. return false;
  298. }
  299. };