JSBHeaderVisitor.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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.utf16charsEnd(), &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. if (comment[0] == '/' && comment[1] == '*' && comment[2] == '*')
  303. {
  304. int index = 3;
  305. bool foundStar = false;
  306. String docString = jfunction->GetDocString();
  307. while(comment[index])
  308. {
  309. // did we find a star in the last loop?
  310. if (foundStar)
  311. {
  312. // We have a an end of block indicator, let's break
  313. if (comment[index] == '/' && foundStar)
  314. break;
  315. // This is just a star in the comment, not an end of comment indicator. Let's keep it
  316. docString += '*';
  317. }
  318. foundStar = comment[index] == '*';
  319. if (!foundStar)
  320. docString += comment[index];
  321. index++;
  322. }
  323. jfunction->SetDocString(docString);
  324. }
  325. }
  326. return jfunction;
  327. }
  328. virtual bool visit(Namespace *nspace)
  329. {
  330. String name = getNameString(nspace->name());
  331. // LOGINFOF("Namespace: %s", name.CString());
  332. return true;
  333. }
  334. // reject template types
  335. virtual bool visit(Template *t)
  336. {
  337. return false;
  338. }
  339. // enums visited in preprocessor visitor
  340. virtual bool visit(Enum *penum)
  341. {
  342. return false;
  343. }
  344. // global var decl or function
  345. virtual bool visit(Declaration* decl)
  346. {
  347. if (decl->isTypedef())
  348. return true;
  349. FullySpecifiedType dtype = decl->type();
  350. Type* type = dtype.type();
  351. if (type->isPointerType() || type->isReferenceType())
  352. return true;
  353. if (type->asEnumType())
  354. return true;
  355. bool _unsigned = false;
  356. if (dtype.isUnsigned())
  357. _unsigned = true;
  358. if (!type->asFloatType() && !type->asIntegerType() && !_unsigned)
  359. {
  360. return true;
  361. }
  362. String value;
  363. const StringLiteral* init = decl->getInitializer();
  364. if (init)
  365. {
  366. if (init->chars())
  367. value = init->chars();
  368. }
  369. if (type->isIntegerType() || _unsigned)
  370. {
  371. module_->RegisterConstant(getNameString(decl->name()).CString(), value, JSBPrimitiveType::Int, _unsigned);
  372. }
  373. else
  374. {
  375. module_->RegisterConstant(getNameString(decl->name()).CString(), value, JSBPrimitiveType::Float);
  376. }
  377. return true;
  378. }
  379. virtual bool visit(Class *klass)
  380. {
  381. String name = getNameString(klass->name());
  382. JSBClass* jclass = module_->GetClass(name);
  383. if (!jclass)
  384. {
  385. return false;
  386. }
  387. jclass->SetHeader(header_);
  388. for (unsigned i = 0; i < klass->baseClassCount(); i++)
  389. {
  390. BaseClass* baseclass = klass->baseClassAt(i);
  391. String baseclassname = getNameString(baseclass->name());
  392. JSBClass* base = JSBPackage::GetClassAllPackages(baseclassname);
  393. if (!base)
  394. {
  395. LOGINFOF("Warning: %s baseclass %s not in bindings", name.CString(), baseclassname.CString());
  396. }
  397. else
  398. {
  399. jclass->SetBaseClass(base);
  400. }
  401. }
  402. for (unsigned i = 0; i < klass->memberCount(); i++)
  403. {
  404. Symbol* symbol = klass->memberAt(i);
  405. Declaration* decl = symbol->asDeclaration();
  406. // if the function describes the body in the header
  407. Function* function = symbol->asFunction();
  408. // otherwise it could be a decl
  409. if (!function && decl)
  410. function = decl->type()->asFunctionType();
  411. if (function)
  412. {
  413. if (function->isPureVirtual())
  414. jclass->SetAbstract();
  415. // only want public functions
  416. if (!symbol->isPublic())
  417. continue;
  418. JSBFunction* jfunction = processFunction(jclass, function);
  419. if (jfunction)
  420. jclass->AddFunction(jfunction);
  421. }
  422. }
  423. // return false so we don't traverse the class members
  424. return false;
  425. }
  426. };
  427. }