JSBHeaderVisitor.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/Core/ProcessUtils.h>
  25. using namespace Atomic;
  26. #include "JSBHeader.h"
  27. #include "JSBModule.h"
  28. #include "JSBClass.h"
  29. #include "JSBPackage.h"
  30. #include "JSBFunction.h"
  31. #include "JSBNameVisitor.h"
  32. namespace ToolCore
  33. {
  34. class JSBHeader;
  35. class JSBPrimitiveType;
  36. class JSBHeaderVisitor : public SymbolVisitor
  37. {
  38. JSBHeader* header_;
  39. JSBModule* module_;
  40. TranslationUnit* unit_;
  41. Namespace* globalNamespace_;
  42. public:
  43. JSBHeaderVisitor(JSBHeader* header, TranslationUnit *unit, Namespace* globalNamespace) :
  44. header_(header),
  45. unit_(unit),
  46. globalNamespace_(globalNamespace)
  47. {
  48. module_ = header_->GetModule();
  49. accept(globalNamespace_);
  50. }
  51. String getNameString(const Name* name)
  52. {
  53. JSBNameVisitor nvisitor;
  54. return nvisitor(name);
  55. }
  56. JSBType* processTypeConversion(Type* type)
  57. {
  58. JSBType* jtype = NULL;
  59. if (type->isIntegerType())
  60. {
  61. IntegerType* itype = type->asIntegerType();
  62. jtype = new JSBPrimitiveType(itype->kind());
  63. }
  64. else if (type->isFloatType())
  65. {
  66. jtype = new JSBPrimitiveType(JSBPrimitiveType::Float);
  67. }
  68. else if (type->isNamedType())
  69. {
  70. NamedType* ntype = type->asNamedType();
  71. String classname = getNameString(ntype->name());
  72. if (classname.StartsWith("Atomic::"))
  73. classname.Replace("Atomic::", "");
  74. if (classname == "Vector")
  75. {
  76. if (ntype->name()->asTemplateNameId())
  77. {
  78. const TemplateNameId* tnid = ntype->name()->asTemplateNameId();
  79. FullySpecifiedType pfst = tnid->templateArgumentAt(0);
  80. JSBType* vtype = processTypeConversion(pfst.type());
  81. if (vtype)
  82. {
  83. jtype = new JSBVectorType(vtype);
  84. }
  85. }
  86. }
  87. else if (classname == "String")
  88. {
  89. jtype = new JSBStringType();
  90. }
  91. else if (classname == "StringHash")
  92. {
  93. jtype = new JSBStringHashType();
  94. }
  95. else if (classname == "JS_HEAP_PTR")
  96. {
  97. jtype = new JSBHeapPtrType();
  98. }
  99. else
  100. {
  101. JSBClass* jclass = JSBPackage::GetClassAllPackages(classname);
  102. if (jclass)
  103. jtype = new JSBClassType(jclass);
  104. else
  105. {
  106. // this might be an enum
  107. JSBEnum* jenum = JSBPackage::GetEnumAllPackages(classname);
  108. if (jenum)
  109. jtype = new JSBEnumType(jenum);
  110. }
  111. }
  112. }
  113. else if (type->asUndefinedType())
  114. {
  115. UndefinedType* utype = type->asUndefinedType();
  116. //ErrorExit("Undefined type");
  117. }
  118. return jtype;
  119. }
  120. JSBFunctionType* processFunctionType(FullySpecifiedType fst, bool retType = false)
  121. {
  122. JSBType* jtype = NULL;
  123. Type* type = fst.type();
  124. bool isPointer = false;
  125. bool isSharedPtr = false;
  126. bool isReference = false;
  127. bool isTemplate = false;
  128. bool isConst = false;
  129. if (type->isPointerType())
  130. {
  131. isPointer=true;
  132. FullySpecifiedType pfst = type->asPointerType()->elementType();
  133. type = pfst.type();
  134. }
  135. if (type->isReferenceType())
  136. {
  137. isReference=true;
  138. FullySpecifiedType pfst = type->asReferenceType()->elementType();
  139. type = pfst.type();
  140. isConst = pfst.isConst();
  141. }
  142. if (!isPointer && retType)
  143. {
  144. if (type->isNamedType())
  145. {
  146. NamedType* ntype = type->asNamedType();
  147. if (ntype->name()->asTemplateNameId())
  148. {
  149. const TemplateNameId* tnid = ntype->name()->asTemplateNameId();
  150. String classname = getNameString(tnid->identifier()->asNameId());
  151. if (classname == "SharedPtr")
  152. {
  153. FullySpecifiedType pfst = tnid->templateArgumentAt(0);
  154. type = pfst.type();
  155. isTemplate = true;
  156. isSharedPtr = true;
  157. }
  158. }
  159. }
  160. }
  161. if (fst.isUnsigned())
  162. {
  163. if (type->isUndefinedType())
  164. {
  165. // this happens when just using "unsigned" in code
  166. jtype = new JSBPrimitiveType(JSBPrimitiveType::Int, true);
  167. }
  168. }
  169. if (!jtype)
  170. {
  171. jtype = processTypeConversion(type);
  172. if (fst.isUnsigned() && jtype->asPrimitiveType())
  173. jtype->asPrimitiveType()->isUnsigned_ = true;
  174. }
  175. if (!jtype)
  176. return NULL;
  177. // read only vectors atm
  178. if (!isConst && jtype->asVectorType())
  179. return NULL;
  180. bool skip = false;
  181. // no pointers to prim atm
  182. if (isPointer || isReference)
  183. {
  184. if (jtype->asPrimitiveType())
  185. skip = true;
  186. else if (!retType && !isConst && (jtype->asStringType() || jtype->asStringHashType()))
  187. {
  188. skip = true;
  189. }
  190. if (skip)
  191. return NULL;
  192. }
  193. JSBFunctionType* ftype = new JSBFunctionType(jtype);
  194. ftype->isPointer_ = isPointer;
  195. ftype->isSharedPtr_ = isSharedPtr;
  196. ftype->isReference_ = isReference;
  197. ftype->isTemplate_ = isTemplate;
  198. ftype->isConst_ = isConst;
  199. return ftype;
  200. }
  201. JSBFunctionType* processFunctionArgType(Argument* arg)
  202. {
  203. JSBFunctionType* jtype = processFunctionType(arg->type());
  204. if (!jtype)
  205. return NULL;
  206. jtype->name_ = getNameString(arg->name());
  207. return jtype;
  208. }
  209. JSBFunctionType* processFunctionReturnType(Function* function)
  210. {
  211. if (!function->hasReturnType())
  212. {
  213. return NULL;
  214. }
  215. JSBFunctionType* jtype = processFunctionType(function->returnType(), true);
  216. if (!jtype)
  217. return NULL;
  218. return jtype;
  219. }
  220. JSBFunction* processFunction(JSBClass* klass, Function* function)
  221. {
  222. JSBFunction* jfunction = new JSBFunction(klass);
  223. // don't ... atm
  224. if (function->isVariadic())
  225. return NULL;
  226. String name = getNameString(function->name());
  227. jfunction->SetName(name);
  228. // don't support operators atm
  229. if (name.StartsWith("operator "))
  230. return NULL;
  231. if (name == klass->GetNativeName())
  232. jfunction->SetConstructor();
  233. if (name.StartsWith("~"))
  234. jfunction->SetDestructor();
  235. if (function->isVirtual())
  236. jfunction->SetVirtual(true);
  237. if (function->isStatic())
  238. jfunction->SetStatic(true);
  239. // see if we support return type
  240. if (function->hasReturnType() && !function->returnType().type()->isVoidType())
  241. {
  242. JSBFunctionType* returnType = processFunctionReturnType(function);
  243. if (!returnType)
  244. return NULL;
  245. jfunction->SetReturnType(returnType);
  246. }
  247. if (function->hasArguments())
  248. {
  249. for (unsigned i = 0; i < function->argumentCount(); i++)
  250. {
  251. Symbol* symbol = function->argumentAt(i);
  252. if (symbol->isArgument())
  253. {
  254. Argument* arg = symbol->asArgument();
  255. JSBFunctionType* ftype = processFunctionArgType(arg);
  256. if (!ftype)
  257. return NULL;
  258. if (arg->hasInitializer())
  259. {
  260. ftype->initializer_ = arg->initializer()->chars();
  261. if (arg->initializer()->_quotedString)
  262. ftype->initializer_ = "\"" + ftype->initializer_ + "\"";
  263. }
  264. jfunction->AddParameter(ftype);
  265. }
  266. else
  267. {
  268. return NULL;
  269. }
  270. }
  271. }
  272. jfunction->sourceLocation_ = function->sourceLocation();
  273. jfunction->fileName_ = function->fileName();
  274. jfunction->sourceLine_ = function->line();
  275. jfunction->sourceColumn_ = function->column();
  276. //const Token &token = unit_->tokenAt(function->sourceLocation());
  277. //const char* source = unit_->firstSourceChar() + token.byteOffset;
  278. const char* comment = NULL;
  279. for (unsigned i = 0; i < unit_->commentCount(); i++)
  280. {
  281. const Token &tcomment = unit_->commentAt(i);
  282. unsigned line;
  283. unit_->getPosition(tcomment.utf16charOffset, &line);
  284. if (line == function->line() - 1)
  285. {
  286. comment = unit_->firstSourceChar() + tcomment.byteOffset;
  287. break;
  288. }
  289. }
  290. if (comment && strlen(comment) > 3)
  291. {
  292. if (comment[0] == '/' && comment[1] == '/' && comment[2] == '/')
  293. {
  294. int index = 3;
  295. while(comment[index] && comment[index] != '\n' && comment[index] != '\r')
  296. {
  297. String docString = jfunction->GetDocString();
  298. docString += comment[index++];
  299. jfunction->SetDocString(docString);
  300. }
  301. }
  302. }
  303. return jfunction;
  304. }
  305. virtual bool visit(Namespace *nspace)
  306. {
  307. String name = getNameString(nspace->name());
  308. // LOGINFOF("Namespace: %s", name.CString());
  309. return true;
  310. }
  311. // reject template types
  312. virtual bool visit(Template *t)
  313. {
  314. return false;
  315. }
  316. // enums visited in preprocessor visitor
  317. virtual bool visit(Enum *penum)
  318. {
  319. return false;
  320. }
  321. // global var decl or function
  322. virtual bool visit(Declaration* decl)
  323. {
  324. if (decl->isTypedef())
  325. return true;
  326. FullySpecifiedType dtype = decl->type();
  327. Type* type = dtype.type();
  328. if (type->isPointerType() || type->isReferenceType())
  329. return true;
  330. if (type->asEnumType())
  331. return true;
  332. bool _unsigned = false;
  333. if (dtype.isUnsigned())
  334. _unsigned = true;
  335. if (!type->asFloatType() && !type->asIntegerType() && !_unsigned)
  336. {
  337. return true;
  338. }
  339. String value;
  340. const StringLiteral* init = decl->getInitializer();
  341. if (init)
  342. {
  343. if (init->chars())
  344. value = init->chars();
  345. }
  346. if (type->isIntegerType() || _unsigned)
  347. {
  348. module_->RegisterConstant(getNameString(decl->name()).CString(), value, JSBPrimitiveType::Int, _unsigned);
  349. }
  350. else
  351. {
  352. module_->RegisterConstant(getNameString(decl->name()).CString(), value, JSBPrimitiveType::Float);
  353. }
  354. return true;
  355. }
  356. virtual bool visit(Class *klass)
  357. {
  358. String name = getNameString(klass->name());
  359. JSBClass* jclass = module_->GetClass(name);
  360. if (!jclass)
  361. {
  362. return false;
  363. }
  364. jclass->SetHeader(header_);
  365. for (unsigned i = 0; i < klass->baseClassCount(); i++)
  366. {
  367. BaseClass* baseclass = klass->baseClassAt(i);
  368. String baseclassname = getNameString(baseclass->name());
  369. JSBClass* base = JSBPackage::GetClassAllPackages(baseclassname);
  370. if (!base)
  371. {
  372. LOGINFOF("Warning: %s baseclass %s not in bindings", name.CString(), baseclassname.CString());
  373. }
  374. else
  375. {
  376. jclass->SetBaseClass(base);
  377. }
  378. }
  379. for (unsigned i = 0; i < klass->memberCount(); i++)
  380. {
  381. Symbol* symbol = klass->memberAt(i);
  382. Declaration* decl = symbol->asDeclaration();
  383. // if the function describes the body in the header
  384. Function* function = symbol->asFunction();
  385. // otherwise it could be a decl
  386. if (!function && decl)
  387. function = decl->type()->asFunctionType();
  388. if (function)
  389. {
  390. if (function->isPureVirtual())
  391. jclass->SetAbstract();
  392. // only want public functions
  393. if (!symbol->isPublic())
  394. continue;
  395. JSBFunction* jfunction = processFunction(jclass, function);
  396. if (jfunction)
  397. jclass->AddFunction(jfunction);
  398. }
  399. }
  400. // return false so we don't traverse the class members
  401. return false;
  402. }
  403. };
  404. }