JSBHeaderVisitor.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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, FullySpecifiedType fst)
  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" || classname == "PODVector")
  75. {
  76. PODVector<TemplateType> types;
  77. unwrapTemplateType(fst, types);
  78. if (types.Size() == 2)
  79. {
  80. JSBType* vtype = processTypeConversion((Type*) types[1].type_, types[1].fstype_);
  81. if (vtype)
  82. {
  83. jtype = new JSBVectorType(vtype, classname == "PODVector");
  84. }
  85. }
  86. else if (types.Size() == 3 && ( getNameString(types[1].name_) == "SharedPtr" || getNameString(types[1].name_) == "WeakPtr"))
  87. {
  88. JSBType* vtype = processTypeConversion((Type*) types[2].type_, types[2].fstype_);
  89. if (vtype)
  90. {
  91. JSBVectorType* jvtype = new JSBVectorType(vtype, classname == "PODVector");
  92. if (getNameString(types[1].name_) == "SharedPtr")
  93. {
  94. jvtype->vectorTypeIsSharedPtr_ = true;
  95. }
  96. else if (getNameString(types[1].name_) == "WeakPtr")
  97. {
  98. jvtype->vectorTypeIsWeakPtr_ = true;
  99. }
  100. jtype = jvtype;
  101. }
  102. }
  103. }
  104. else if (classname == "String")
  105. {
  106. jtype = new JSBStringType();
  107. }
  108. else if (classname == "StringHash")
  109. {
  110. jtype = new JSBStringHashType();
  111. }
  112. else if (classname == "JS_HEAP_PTR")
  113. {
  114. jtype = new JSBHeapPtrType();
  115. }
  116. else
  117. {
  118. JSBClass* jclass = JSBPackage::GetClassAllPackages(classname);
  119. if (jclass)
  120. jtype = new JSBClassType(jclass);
  121. else
  122. {
  123. // this might be an enum
  124. JSBEnum* jenum = JSBPackage::GetEnumAllPackages(classname);
  125. if (jenum)
  126. jtype = new JSBEnumType(jenum);
  127. }
  128. }
  129. }
  130. else if (type->asUndefinedType())
  131. {
  132. UndefinedType* utype = type->asUndefinedType();
  133. //ErrorExit("Undefined type");
  134. }
  135. return jtype;
  136. }
  137. struct TemplateType
  138. {
  139. FullySpecifiedType fstype_;
  140. bool isPointer_;
  141. bool isReference_;
  142. const Name* name_;
  143. const Type* type_;
  144. static void Init(TemplateType& ttype)
  145. {
  146. ttype.isPointer_ = false;
  147. ttype.isReference_ = false;
  148. ttype.name_ = 0;
  149. ttype.type_ = 0;
  150. }
  151. };
  152. bool unwrapTemplateType(const FullySpecifiedType& fstype, PODVector<TemplateType>& types)
  153. {
  154. TemplateType ttype;
  155. TemplateType::Init(ttype);
  156. ttype.fstype_ = fstype;
  157. ttype.type_ = fstype.type();
  158. if (ttype.type_->isPointerType())
  159. {
  160. ttype.isPointer_=true;
  161. FullySpecifiedType pfst = ttype.type_->asPointerType()->elementType();
  162. ttype.type_ = pfst.type();
  163. }
  164. if (ttype.type_->isReferenceType())
  165. {
  166. ttype.isReference_=true;
  167. FullySpecifiedType pfst = ttype.type_->asReferenceType()->elementType();
  168. ttype.type_ = pfst.type();
  169. }
  170. const NamedType* ntype = ttype.type_->asNamedType();
  171. if (!ntype)
  172. return false;
  173. if (ntype->name()->asTemplateNameId())
  174. {
  175. const TemplateNameId* tnid = ntype->name()->asTemplateNameId();
  176. ttype.name_ = tnid->identifier()->asNameId();
  177. types.Push(ttype);
  178. unwrapTemplateType(tnid->templateArgumentAt(0), types);
  179. return true;
  180. }
  181. ttype.name_ = ntype->name();
  182. types.Push(ttype);
  183. return false;
  184. }
  185. JSBFunctionType* processFunctionType(FullySpecifiedType fst, bool retType = false)
  186. {
  187. JSBType* jtype = NULL;
  188. Type* type = fst.type();
  189. bool isPointer = false;
  190. bool isSharedPtr = false;
  191. bool isReference = false;
  192. bool isTemplate = false;
  193. bool isConst = false;
  194. if (type->isPointerType())
  195. {
  196. isPointer=true;
  197. FullySpecifiedType pfst = type->asPointerType()->elementType();
  198. type = pfst.type();
  199. }
  200. if (type->isReferenceType())
  201. {
  202. isReference=true;
  203. FullySpecifiedType pfst = type->asReferenceType()->elementType();
  204. type = pfst.type();
  205. isConst = pfst.isConst();
  206. }
  207. if (!isPointer && retType)
  208. {
  209. if (type->isNamedType())
  210. {
  211. NamedType* ntype = type->asNamedType();
  212. if (ntype->name()->asTemplateNameId())
  213. {
  214. PODVector<TemplateType> types;
  215. unwrapTemplateType(fst, types);
  216. String classname = getNameString(types[0].name_);
  217. // SharedPtr
  218. if ( classname == "SharedPtr" && types.Size() == 2 )
  219. {
  220. type = (Type*) types[1].type_;
  221. isTemplate = true;
  222. isSharedPtr = true;
  223. }
  224. }
  225. }
  226. }
  227. if (fst.isUnsigned())
  228. {
  229. if (type->isUndefinedType())
  230. {
  231. // this happens when just using "unsigned" in code
  232. jtype = new JSBPrimitiveType(JSBPrimitiveType::Int, true);
  233. }
  234. }
  235. if (!jtype)
  236. {
  237. jtype = processTypeConversion(type, fst);
  238. // explicit script string -> StringHash required
  239. if (jtype && jtype->asStringHashType())
  240. isReference = false;
  241. if (fst.isUnsigned() && jtype->asPrimitiveType())
  242. jtype->asPrimitiveType()->isUnsigned_ = true;
  243. }
  244. if (!jtype)
  245. return NULL;
  246. bool skip = false;
  247. // no pointers to prim atm
  248. if (isPointer || isReference)
  249. {
  250. if (jtype->asPrimitiveType())
  251. skip = true;
  252. else if (!retType && !isConst && (jtype->asStringType() || jtype->asStringHashType()))
  253. {
  254. skip = true;
  255. }
  256. if (skip)
  257. return NULL;
  258. }
  259. // We don't support pointers to Vector2/Vector3/etc which are generally out values
  260. if (isPointer && jtype->asClassType() && jtype->asClassType()->class_->IsNumberArray())
  261. return NULL;
  262. JSBFunctionType* ftype = new JSBFunctionType(jtype);
  263. ftype->isPointer_ = isPointer;
  264. ftype->isSharedPtr_ = isSharedPtr;
  265. ftype->isReference_ = isReference;
  266. ftype->isTemplate_ = isTemplate;
  267. ftype->isConst_ = isConst;
  268. return ftype;
  269. }
  270. JSBFunctionType* processFunctionArgType(Argument* arg)
  271. {
  272. JSBFunctionType* jtype = processFunctionType(arg->type());
  273. if (!jtype)
  274. return NULL;
  275. jtype->name_ = getNameString(arg->name());
  276. return jtype;
  277. }
  278. JSBFunctionType* processFunctionReturnType(Function* function)
  279. {
  280. if (!function->hasReturnType())
  281. {
  282. return NULL;
  283. }
  284. JSBFunctionType* jtype = processFunctionType(function->returnType(), true);
  285. if (!jtype)
  286. return NULL;
  287. return jtype;
  288. }
  289. String parseDocString(int commentLine) const
  290. {
  291. const char* comment = NULL;
  292. for (unsigned i = 0; i < unit_->commentCount(); i++)
  293. {
  294. const Token &tcomment = unit_->commentAt(i);
  295. unsigned line;
  296. unit_->getPosition(tcomment.utf16charsEnd(), &line);
  297. if (line == commentLine)
  298. {
  299. comment = unit_->firstSourceChar() + tcomment.byteOffset;
  300. break;
  301. }
  302. }
  303. String docString;
  304. if (comment && strlen(comment) > 3)
  305. {
  306. if (comment[0] == '/' && comment[1] == '/' && comment[2] == '/')
  307. {
  308. int index = 3;
  309. while(comment[index] && comment[index] != '\n' && comment[index] != '\r')
  310. {
  311. docString += comment[index++];
  312. }
  313. }
  314. if (comment[0] == '/' && comment[1] == '*' && comment[2] == '*')
  315. {
  316. int index = 3;
  317. bool foundStar = false;
  318. while(comment[index])
  319. {
  320. // did we find a star in the last loop?
  321. if (foundStar)
  322. {
  323. // We have a an end of block indicator, let's break
  324. if (comment[index] == '/' && foundStar)
  325. break;
  326. // This is just a star in the comment, not an end of comment indicator. Let's keep it
  327. docString += '*';
  328. }
  329. foundStar = comment[index] == '*';
  330. if (!foundStar)
  331. docString += comment[index];
  332. index++;
  333. }
  334. }
  335. }
  336. return docString;
  337. }
  338. JSBFunction* processFunction(JSBClass* klass, Function* function)
  339. {
  340. JSBFunction* jfunction = new JSBFunction(klass);
  341. // don't ... atm
  342. if (function->isVariadic())
  343. return NULL;
  344. String name = getNameString(function->name());
  345. jfunction->SetName(name);
  346. // don't support operators atm
  347. if (name.StartsWith("operator "))
  348. return NULL;
  349. if (name == klass->GetNativeName())
  350. jfunction->SetConstructor();
  351. if (name.StartsWith("~"))
  352. jfunction->SetDestructor();
  353. if (function->isVirtual())
  354. jfunction->SetVirtual(true);
  355. if (function->isStatic())
  356. jfunction->SetStatic(true);
  357. // see if we support return type
  358. if (function->hasReturnType() && !function->returnType().type()->isVoidType())
  359. {
  360. JSBFunctionType* returnType = processFunctionReturnType(function);
  361. if (!returnType)
  362. return NULL;
  363. jfunction->SetReturnType(returnType);
  364. }
  365. if (function->hasArguments())
  366. {
  367. for (unsigned i = 0; i < function->argumentCount(); i++)
  368. {
  369. Symbol* symbol = function->argumentAt(i);
  370. if (symbol->isArgument())
  371. {
  372. Argument* arg = symbol->asArgument();
  373. JSBFunctionType* ftype = processFunctionArgType(arg);
  374. if (!ftype)
  375. {
  376. // if we don't have an initializer, the function cannot be bound
  377. // as unscriptable type
  378. if (!arg->hasInitializer())
  379. return NULL;
  380. // otherwise, break and the optional args will be ignored
  381. break;
  382. }
  383. if (arg->hasInitializer())
  384. {
  385. ftype->initializer_ = arg->initializer()->chars();
  386. if (arg->initializer()->_quotedString)
  387. ftype->initializer_ = "\"" + ftype->initializer_ + "\"";
  388. }
  389. jfunction->AddParameter(ftype);
  390. }
  391. else
  392. {
  393. return NULL;
  394. }
  395. }
  396. }
  397. jfunction->sourceLocation_ = function->sourceLocation();
  398. jfunction->fileName_ = function->fileName();
  399. jfunction->sourceLine_ = function->line();
  400. jfunction->sourceColumn_ = function->column();
  401. //const Token &token = unit_->tokenAt(function->sourceLocation());
  402. //const char* source = unit_->firstSourceChar() + token.byteOffset;
  403. String docString = parseDocString(function->line() - 1);
  404. if (docString.Length())
  405. jfunction->SetDocString(docString);
  406. return jfunction;
  407. }
  408. virtual bool visit(Namespace *nspace)
  409. {
  410. String name = getNameString(nspace->name());
  411. // LOGINFOF("Namespace: %s", name.CString());
  412. return true;
  413. }
  414. // reject template types
  415. virtual bool visit(Template *t)
  416. {
  417. return false;
  418. }
  419. // enums visited in preprocessor visitor
  420. virtual bool visit(Enum *penum)
  421. {
  422. return false;
  423. }
  424. // global var decl or function
  425. virtual bool visit(Declaration* decl)
  426. {
  427. if (decl->isTypedef())
  428. return true;
  429. FullySpecifiedType dtype = decl->type();
  430. Type* type = dtype.type();
  431. if (type->isPointerType() || type->isReferenceType())
  432. return true;
  433. if (type->asEnumType())
  434. return true;
  435. bool _unsigned = false;
  436. if (dtype.isUnsigned())
  437. _unsigned = true;
  438. if (!type->asFloatType() && !type->asIntegerType() && !_unsigned)
  439. {
  440. return true;
  441. }
  442. String value;
  443. const StringLiteral* init = decl->getInitializer();
  444. if (init)
  445. {
  446. if (init->chars())
  447. value = init->chars();
  448. }
  449. if (dtype.isConst())
  450. {
  451. if (type->isIntegerType() || _unsigned)
  452. {
  453. module_->RegisterConstant(getNameString(decl->name()).CString(), value, JSBPrimitiveType::Int, _unsigned);
  454. }
  455. else
  456. {
  457. module_->RegisterConstant(getNameString(decl->name()).CString(), value, JSBPrimitiveType::Float);
  458. }
  459. }
  460. return true;
  461. }
  462. virtual bool visit(Class *klass)
  463. {
  464. String name = getNameString(klass->name());
  465. JSBClass* jclass = module_->GetClass(name);
  466. if (!jclass)
  467. {
  468. return false;
  469. }
  470. jclass->SetHeader(header_);
  471. String docString = parseDocString(klass->line() - 1);
  472. if (docString.Length())
  473. jclass->SetDocString(docString);
  474. for (unsigned i = 0; i < klass->baseClassCount(); i++)
  475. {
  476. BaseClass* baseclass = klass->baseClassAt(i);
  477. String baseclassname = getNameString(baseclass->name());
  478. JSBClass* base = JSBPackage::GetClassAllPackages(baseclassname);
  479. if (!base)
  480. {
  481. ATOMIC_LOGINFOF("Warning: %s baseclass %s not in bindings", name.CString(), baseclassname.CString());
  482. }
  483. else
  484. {
  485. jclass->SetBaseClass(base);
  486. }
  487. }
  488. for (unsigned i = 0; i < klass->memberCount(); i++)
  489. {
  490. Symbol* symbol = klass->memberAt(i);
  491. Declaration* decl = symbol->asDeclaration();
  492. // if the function describes the body in the header
  493. Function* function = symbol->asFunction();
  494. // otherwise it could be a decl
  495. if (!function && decl)
  496. function = decl->type()->asFunctionType();
  497. if (function)
  498. {
  499. if (function->isPureVirtual())
  500. jclass->SetAbstract();
  501. // only want public functions
  502. if (!symbol->isPublic())
  503. continue;
  504. JSBFunction* jfunction = processFunction(jclass, function);
  505. if (jfunction)
  506. jclass->AddFunction(jfunction);
  507. }
  508. }
  509. // return false so we don't traverse the class members
  510. return false;
  511. }
  512. };
  513. }