ASResult.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  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. #include "ASResult.h"
  23. #include "Tuning.h"
  24. #include "Utils.h"
  25. #include <fstream>
  26. namespace ASBindingGenerator
  27. {
  28. ASGeneratedFile_WithRegistrationFunction::ASGeneratedFile_WithRegistrationFunction(const string& outputFilePath, const string& functionName)
  29. {
  30. outputFilePath_ = outputFilePath;
  31. functionName_ = functionName;
  32. }
  33. // ============================================================================
  34. void ASGeneratedFile_Members::Save()
  35. {
  36. ofstream out(outputFilePath_);
  37. out <<
  38. "// DO NOT EDIT. This file is generated\n"
  39. "\n"
  40. "#include \"../Precompiled.h\"\n"
  41. "#include \"../AngelScript/APITemplates.h\"\n"
  42. "\n"
  43. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  44. "#include \"../AngelScript/Manual.h\"\n"
  45. "\n"
  46. "namespace Urho3D\n"
  47. "{\n"
  48. "\n"
  49. "void FakeAddRef(void* ptr);\n"
  50. "void FakeReleaseRef(void* ptr);\n"
  51. "\n"
  52. << glue_.str() <<
  53. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  54. "{\n"
  55. << reg_.str() <<
  56. "}\n"
  57. "\n"
  58. "}\n";
  59. }
  60. // ============================================================================
  61. ASGeneratedFile_Templates::ASGeneratedFile_Templates(const string& outputFilePath)
  62. {
  63. outputFilePath_ = outputFilePath;
  64. }
  65. void ASGeneratedFile_Templates::Save()
  66. {
  67. ofstream out(outputFilePath_);
  68. out <<
  69. "// DO NOT EDIT. This file is generated\n"
  70. "\n"
  71. "#pragma once\n"
  72. "\n"
  73. "#include \"../Precompiled.h\"\n"
  74. "#include \"../AngelScript/APITemplates.h\"\n"
  75. "\n"
  76. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  77. "\n"
  78. "#include \"../AngelScript/Manual.h\"\n"
  79. "\n"
  80. "namespace Urho3D\n"
  81. "{\n"
  82. "\n"
  83. "void FakeAddRef(void* ptr);\n"
  84. "void FakeReleaseRef(void* ptr);\n"
  85. "\n"
  86. << glue_.str()
  87. << reg_.str() <<
  88. "}\n";
  89. }
  90. bool ProcessedEnum::operator <(const ProcessedEnum& rhs) const
  91. {
  92. if (insideDefine_ != rhs.insideDefine_)
  93. return insideDefine_ < rhs.insideDefine_;
  94. return name_ < rhs.name_;
  95. }
  96. bool ProcessedGlobalFunction::operator <(const ProcessedGlobalFunction& rhs) const
  97. {
  98. if (insideDefine_ != rhs.insideDefine_)
  99. return insideDefine_ < rhs.insideDefine_;
  100. if (name_ != rhs.name_)
  101. return name_ < rhs.name_;
  102. // Overloads with the same name may exist
  103. if (comment_ != rhs.comment_)
  104. return comment_ < rhs.comment_;
  105. // Different specializations of the same template and aliases have the same comment
  106. return registration_ < rhs.registration_;
  107. }
  108. bool ProcessedGlobalVariable::operator <(const ProcessedGlobalVariable& rhs) const
  109. {
  110. if (insideDefine_ != rhs.insideDefine_)
  111. return insideDefine_ < rhs.insideDefine_;
  112. return name_ < rhs.name_;
  113. }
  114. bool ProcessedClass::operator <(const ProcessedClass& rhs) const
  115. {
  116. if (insideDefine_ != rhs.insideDefine_)
  117. return insideDefine_ < rhs.insideDefine_;
  118. return name_ < rhs.name_;
  119. }
  120. namespace Result
  121. {
  122. vector<ProcessedEnum> enums_;
  123. // Write result to GeneratedEnums.cpp
  124. static void SaveEnums(const string& outputBasePath)
  125. {
  126. sort(enums_.begin(), enums_.end());
  127. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedEnums.cpp");
  128. ofs <<
  129. "// DO NOT EDIT. This file is generated\n"
  130. "\n"
  131. "// We need register all enums before registration of any functions because functions can use any enums\n"
  132. "\n"
  133. "#include \"../Precompiled.h\"\n"
  134. "#include \"../AngelScript/APITemplates.h\"\n"
  135. "\n"
  136. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  137. "\n"
  138. "namespace Urho3D\n"
  139. "{\n"
  140. "\n";
  141. for (const ProcessedEnum& processedEnum : enums_)
  142. {
  143. if (!processedEnum.glue_.size())
  144. continue;
  145. if (!processedEnum.insideDefine_.empty())
  146. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  147. ofs << "// " << processedEnum.comment_ << "\n";
  148. for (const string& glue : processedEnum.glue_)
  149. ofs << glue << "\n";
  150. if (!processedEnum.insideDefine_.empty())
  151. ofs << "#endif\n";
  152. ofs << "\n";
  153. }
  154. ofs <<
  155. "void ASRegisterGeneratedEnums(asIScriptEngine* engine)\n"
  156. "{\n";
  157. bool isFirst = true;
  158. string openedDefine;
  159. for (const ProcessedEnum& processedEnum : enums_)
  160. {
  161. if (processedEnum.insideDefine_ != openedDefine && !openedDefine.empty())
  162. {
  163. ofs << "#endif\n";
  164. openedDefine.clear();
  165. }
  166. if (!isFirst)
  167. ofs << "\n";
  168. if (processedEnum.insideDefine_ != openedDefine && !processedEnum.insideDefine_.empty())
  169. {
  170. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  171. openedDefine = processedEnum.insideDefine_;
  172. }
  173. ofs << " // " << processedEnum.comment_ << "\n";
  174. for (const string& registration : processedEnum.registration_)
  175. ofs << " " << registration << "\n";
  176. isFirst = false;
  177. }
  178. if (!openedDefine.empty())
  179. ofs << "#endif\n";
  180. ofs <<
  181. "}\n"
  182. "\n"
  183. "}\n";
  184. }
  185. // ============================================================================
  186. vector<ProcessedGlobalFunction> globalFunctions_;
  187. // Write result to GlobalFunctions.cpp
  188. static void SaveGlobalFunctions(const string& outputBasePath)
  189. {
  190. sort(globalFunctions_.begin(), globalFunctions_.end());
  191. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedGlobalFunctions.cpp");
  192. ofs <<
  193. "// DO NOT EDIT. This file is generated\n"
  194. "\n"
  195. "#include \"../Precompiled.h\"\n"
  196. "#include \"../AngelScript/APITemplates.h\"\n"
  197. "\n"
  198. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  199. "\n"
  200. "namespace Urho3D\n"
  201. "{\n"
  202. "\n";
  203. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  204. {
  205. if (globalFunction.glue_.empty())
  206. continue;
  207. if (!globalFunction.insideDefine_.empty())
  208. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  209. ofs << "// " << globalFunction.comment_ << "\n";
  210. ofs << globalFunction.glue_ << "\n";
  211. if (!globalFunction.insideDefine_.empty())
  212. ofs << "#endif\n";
  213. ofs << "\n";
  214. }
  215. ofs <<
  216. "void ASRegisterGeneratedGlobalFunctions(asIScriptEngine* engine)\n"
  217. "{\n";
  218. bool isFirst = true;
  219. string openedDefine;
  220. string lastComment;
  221. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  222. {
  223. if (globalFunction.insideDefine_ != openedDefine && !openedDefine.empty())
  224. {
  225. ofs << "#endif\n";
  226. openedDefine.clear();
  227. }
  228. if (!isFirst && lastComment != globalFunction.comment_)
  229. ofs << "\n";
  230. if (globalFunction.insideDefine_ != openedDefine && !globalFunction.insideDefine_.empty())
  231. {
  232. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  233. openedDefine = globalFunction.insideDefine_;
  234. }
  235. if (lastComment != globalFunction.comment_)
  236. ofs << " // " << globalFunction.comment_ << "\n";
  237. ofs << " " << globalFunction.registration_ << "\n";
  238. isFirst = false;
  239. lastComment = globalFunction.comment_;
  240. }
  241. if (!openedDefine.empty())
  242. ofs << "#endif\n";
  243. ofs <<
  244. "}\n"
  245. "\n"
  246. "}\n";
  247. }
  248. // ============================================================================
  249. vector<ProcessedGlobalVariable> globalVariables_;
  250. // Write result to GlobalVariables.cpp
  251. static void SaveGlobalVariables(const string& outputBasePath)
  252. {
  253. sort(globalVariables_.begin(), globalVariables_.end());
  254. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedGlobalVariables.cpp");
  255. ofs <<
  256. "// DO NOT EDIT. This file is generated\n"
  257. "\n"
  258. "#include \"../Precompiled.h\"\n"
  259. "#include \"../AngelScript/APITemplates.h\"\n"
  260. "\n"
  261. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  262. "\n"
  263. "// Some headers could re-define M_PI, ensure that it's undefined\n"
  264. "#undef M_PI\n"
  265. "\n"
  266. "namespace Urho3D\n"
  267. "{\n"
  268. "\n"
  269. "void ASRegisterGeneratedGlobalVariables(asIScriptEngine* engine)\n"
  270. "{\n";
  271. bool isFirst = true;
  272. string openedDefine;
  273. string lastComment;
  274. for (const ProcessedGlobalVariable& globalVariable : globalVariables_)
  275. {
  276. if (globalVariable.insideDefine_ != openedDefine && !openedDefine.empty())
  277. {
  278. ofs << "#endif\n";
  279. openedDefine.clear();
  280. }
  281. if (!isFirst && lastComment != globalVariable.comment_)
  282. ofs << "\n";
  283. if (globalVariable.insideDefine_ != openedDefine && !globalVariable.insideDefine_.empty())
  284. {
  285. ofs << "#ifdef " << globalVariable.insideDefine_ << "\n";
  286. openedDefine = globalVariable.insideDefine_;
  287. }
  288. if (lastComment != globalVariable.comment_)
  289. ofs << " // " << globalVariable.comment_ << "\n";
  290. ofs << " " << globalVariable.registration_ << "\n";
  291. isFirst = false;
  292. lastComment = globalVariable.comment_;
  293. }
  294. if (!openedDefine.empty())
  295. ofs << "#endif\n";
  296. ofs <<
  297. "}\n"
  298. "\n"
  299. "}\n";
  300. }
  301. // ============================================================================
  302. vector<ProcessedClass> classes_;
  303. // Write result to GeneratedObjectTypes.cpp
  304. static void SaveObjectTypes(const string& outputBasePath)
  305. {
  306. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedObjectTypes.cpp");
  307. ofs <<
  308. "// DO NOT EDIT. This file is generated\n"
  309. "\n"
  310. "// We need register all types before registration of any functions because functions can use any types\n"
  311. "\n"
  312. "#include \"../Precompiled.h\"\n"
  313. "#include \"../AngelScript/APITemplates.h\"\n"
  314. "\n"
  315. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  316. "\n"
  317. "namespace Urho3D\n"
  318. "{\n"
  319. "\n"
  320. "void ASRegisterGeneratedObjectTypes(asIScriptEngine* engine)\n"
  321. "{\n";
  322. string openedDefine;
  323. bool isFirst = true;
  324. for (const ProcessedClass& processedClass : classes_)
  325. {
  326. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  327. {
  328. ofs << "#endif\n";
  329. openedDefine.clear();
  330. }
  331. if (!isFirst)
  332. ofs << "\n";
  333. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  334. {
  335. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  336. openedDefine = processedClass.insideDefine_;
  337. }
  338. ofs
  339. << " // " << processedClass.comment_ << "\n"
  340. << " " << processedClass.objectTypeRegistration_ << "\n";
  341. isFirst = false;
  342. }
  343. if (!openedDefine.empty())
  344. ofs << "#endif\n";
  345. ofs <<
  346. "}\n"
  347. "\n"
  348. "}\n";
  349. }
  350. // Write result to GeneratedDefaultConstructors.cpp
  351. static void SaveDefaultConstructors(const string& outputBasePath)
  352. {
  353. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedDefaultConstructors.cpp");
  354. ofs <<
  355. "// DO NOT EDIT. This file is generated\n"
  356. "\n"
  357. "// We need register default constructors before any members to allow using in Array<type>\n"
  358. "\n"
  359. "#include \"../Precompiled.h\"\n"
  360. "#include \"../AngelScript/APITemplates.h\"\n"
  361. "\n"
  362. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  363. "\n"
  364. "namespace Urho3D\n"
  365. "{\n";
  366. string openedDefine;
  367. for (const ProcessedClass& processedClass : classes_)
  368. {
  369. if (!processedClass.defaultConstructor_)
  370. continue;
  371. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  372. {
  373. ofs << "\n#endif\n";
  374. openedDefine.clear();
  375. }
  376. ofs << "\n";
  377. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  378. {
  379. ofs << "#ifdef " << processedClass.insideDefine_ << "\n\n";
  380. openedDefine = processedClass.insideDefine_;
  381. }
  382. ofs <<
  383. "// " << processedClass.defaultConstructor_->comment_ << "\n" <<
  384. processedClass.defaultConstructor_->glue_;
  385. }
  386. if (!openedDefine.empty())
  387. {
  388. ofs << "\n#endif\n";
  389. openedDefine.clear();
  390. }
  391. ofs <<
  392. "\n"
  393. "void ASRegisterGeneratedDefaultConstructors(asIScriptEngine* engine)\n"
  394. "{\n";
  395. bool isFirst = true;
  396. for (const ProcessedClass& processedClass : classes_)
  397. {
  398. if (!processedClass.defaultConstructor_)
  399. continue;
  400. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  401. {
  402. ofs << "#endif\n";
  403. openedDefine.clear();
  404. }
  405. if (!isFirst)
  406. ofs << "\n";
  407. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  408. {
  409. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  410. openedDefine = processedClass.insideDefine_;
  411. }
  412. ofs <<
  413. " // " << processedClass.defaultConstructor_->comment_ << "\n" <<
  414. " " << processedClass.defaultConstructor_->registration_ << "\n";
  415. isFirst = false;
  416. }
  417. if (!openedDefine.empty())
  418. ofs << "#endif\n";
  419. ofs <<
  420. "}\n"
  421. "\n"
  422. "}\n";
  423. }
  424. // Write result to GeneratedClasses.cpp
  425. static void SaveGeneratedClasses(const string& outputBasePath)
  426. {
  427. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedClasses.cpp");
  428. ofs <<
  429. "// DO NOT EDIT. This file is generated\n"
  430. "\n"
  431. "#include \"../Precompiled.h\"\n"
  432. "#include \"../AngelScript/APITemplates.h\"\n"
  433. "\n"
  434. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  435. "#include \"../AngelScript/GeneratedClassMembers.h\"\n"
  436. "#include \"../AngelScript/Manual.h\"\n"
  437. "\n"
  438. "namespace Urho3D\n"
  439. "{\n"
  440. "\n"
  441. "void FakeAddRef(void* ptr);\n"
  442. "void FakeReleaseRef(void* ptr);\n";
  443. string openedDefine;
  444. for (const ProcessedClass& processedClass : classes_)
  445. {
  446. if (processedClass.noBind_)
  447. continue;
  448. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  449. {
  450. ofs << "\n#endif // def " << openedDefine << "\n";
  451. openedDefine.clear();
  452. }
  453. ofs << "\n";
  454. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  455. {
  456. ofs << "#ifdef " << processedClass.insideDefine_ << "\n\n";
  457. openedDefine = processedClass.insideDefine_;
  458. }
  459. if (processedClass.destructor_)
  460. {
  461. ofs <<
  462. "// " << processedClass.destructor_->comment_ << "\n"
  463. << processedClass.destructor_->glue_ <<
  464. "\n";
  465. }
  466. ofs <<
  467. "// " << processedClass.comment_ << "\n"
  468. "static void Register_" << processedClass.name_ << "(asIScriptEngine* engine)\n"
  469. "{\n";
  470. bool needGap = false;
  471. /*
  472. for (string nonDefaultConstructor : processedClass.nonDefaultConstructors_)
  473. ofs << " // " << nonDefaultConstructor << "\n";
  474. */
  475. if (processedClass.destructor_)
  476. {
  477. ofs <<
  478. " // " << processedClass.destructor_->comment_ << "\n"
  479. " " << processedClass.destructor_->registration_ << "\n";
  480. needGap = true;
  481. }
  482. if (needGap)
  483. ofs << '\n';
  484. ofs <<
  485. " Vector<RegisterObjectMethodArgs> methods;\n"
  486. " CollectMembers_" << processedClass.name_ << "(methods);\n"
  487. " const char* asClassName = \"" << processedClass.name_ << "\";\n"
  488. " //for (const RegisterObjectMethodArgs& method : methods)\n"
  489. " // engine->RegisterObjectMethod(asClassName, method.declaration_.CString(), method.funcPointer_, method.callConv_);\n";
  490. ofs << "}\n";
  491. }
  492. if (!openedDefine.empty())
  493. {
  494. ofs << "\n#endif // def " << openedDefine << "\n";
  495. openedDefine.clear();
  496. }
  497. ofs <<
  498. "\n"
  499. "void ASRegisterGeneratedClasses(asIScriptEngine* engine)\n"
  500. "{\n";
  501. bool isFirst = true;
  502. for (const ProcessedClass& processedClass : classes_)
  503. {
  504. if (processedClass.noBind_)
  505. continue;
  506. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  507. {
  508. ofs << "#endif\n";
  509. openedDefine.clear();
  510. }
  511. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  512. {
  513. if (!isFirst)
  514. ofs << "\n";
  515. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  516. openedDefine = processedClass.insideDefine_;
  517. }
  518. ofs << " Register_" << processedClass.name_ << "(engine);\n";
  519. isFirst = false;
  520. }
  521. if (!openedDefine.empty())
  522. ofs << "#endif\n";
  523. ofs <<
  524. "}\n"
  525. "\n"
  526. "}\n";
  527. }
  528. // Write result to GeneratedClassMembers.cpp and GeneratedClassMembers.h
  529. static void SaveClassMembers(const string& outputBasePath)
  530. {
  531. ofstream ofsCpp(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedClassMembers.cpp");
  532. ofsCpp <<
  533. "// DO NOT EDIT. This file is generated\n"
  534. "\n"
  535. "#include \"../Precompiled.h\"\n"
  536. "#include \"../AngelScript/APITemplates.h\"\n"
  537. "\n"
  538. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  539. "#include \"../AngelScript/GeneratedClassMembers.h\"\n"
  540. "#include \"../AngelScript/Manual.h\"\n"
  541. "\n"
  542. "namespace Urho3D\n"
  543. "{\n"
  544. "\n"
  545. "void FakeAddRef(void* ptr);\n"
  546. "void FakeReleaseRef(void* ptr);\n";
  547. string openedDefine;
  548. for (const ProcessedClass& processedClass : classes_)
  549. {
  550. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  551. {
  552. ofsCpp <<
  553. "\n"
  554. "#endif // def " << openedDefine << "\n";
  555. openedDefine.clear();
  556. }
  557. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  558. {
  559. ofsCpp <<
  560. "\n"
  561. "#ifdef " << processedClass.insideDefine_ << "\n";
  562. openedDefine = processedClass.insideDefine_;
  563. }
  564. /*for (const ClassMethodRegistration& method : processedClass.methods_)
  565. {
  566. if (!method.glue_.empty())
  567. {
  568. ofsCpp <<
  569. "\n"
  570. "// " << method.cppDeclaration_ << "\n"
  571. << method.glue_;
  572. }
  573. }*/
  574. ofsCpp <<
  575. "\n"
  576. "// " << processedClass.comment_ << "\n"
  577. "void CollectMembers_" << processedClass.name_ << "(Vector<RegisterObjectMethodArgs>& methods)\n"
  578. "{\n";
  579. bool needGap = false;
  580. /*for (const RegistrationError& unregisteredMethod : processedClass.unregisteredMethods_)
  581. {
  582. if (needGap)
  583. ofsCpp << '\n';
  584. ofsCpp <<
  585. " // " << unregisteredMethod.cppDeclaration_ << "\n"
  586. " // " << unregisteredMethod.message_ << "\n";
  587. needGap = true;
  588. }*/
  589. if (processedClass.baseClassNames_.size())
  590. {
  591. if (needGap)
  592. ofsCpp << '\n';
  593. for (const string& baseClassName : processedClass.baseClassNames_)
  594. ofsCpp << " CollectMembers_" << baseClassName << "(methods);\n";
  595. needGap = true;
  596. }
  597. /*for (const ClassMethodRegistration& method : processedClass.methods_)
  598. {
  599. if (needGap)
  600. ofsCpp << '\n';
  601. const RegisterObjectMethodArgs& args = method.registration_;
  602. ofsCpp <<
  603. " // " << method.cppDeclaration_ << "\n"
  604. " methods.Push(RegisterObjectMethodArgs(\"" << args.declaration_ << "\", " << args.funcPointer_ << ", " << args.callConv_ << "));\n";
  605. needGap = true;
  606. }*/
  607. ofsCpp << "}\n";
  608. }
  609. if (!openedDefine.empty())
  610. {
  611. ofsCpp <<
  612. "\n"
  613. "#endif // def " << openedDefine << "\n";
  614. openedDefine.clear();
  615. }
  616. ofsCpp <<
  617. "\n"
  618. "} // namespace Urho3D\n";
  619. ofstream ofsH(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedClassMembers.h");
  620. ofsH <<
  621. "// DO NOT EDIT. This file is generated\n"
  622. "\n"
  623. "#pragma once\n"
  624. "\n"
  625. "#include \"../AngelScript/APITemplates.h\"\n"
  626. "\n"
  627. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  628. "\n"
  629. "namespace Urho3D\n"
  630. "{\n";
  631. for (const ProcessedClass& processedClass : classes_)
  632. {
  633. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  634. {
  635. ofsH <<
  636. "\n"
  637. "#endif // def " << openedDefine << "\n";
  638. openedDefine.clear();
  639. }
  640. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  641. {
  642. ofsH <<
  643. "\n"
  644. "#ifdef " << processedClass.insideDefine_ << "\n";
  645. openedDefine = processedClass.insideDefine_;
  646. }
  647. ofsH <<
  648. "\n"
  649. "// " << processedClass.comment_ << "\n"
  650. "void CollectMembers_" << processedClass.name_ << "(Vector<RegisterObjectMethodArgs>& methods);\n";
  651. }
  652. if (!openedDefine.empty())
  653. {
  654. ofsH <<
  655. "\n"
  656. "#endif // def " << openedDefine << "\n";
  657. openedDefine.clear();
  658. }
  659. ofsH <<
  660. "\n"
  661. "} // namespace Urho3D\n";
  662. }
  663. static void SaveClasses(const string& outputBasePath)
  664. {
  665. sort(classes_.begin(), classes_.end());
  666. SaveObjectTypes(outputBasePath);
  667. SaveDefaultConstructors(outputBasePath);
  668. SaveGeneratedClasses(outputBasePath);
  669. SaveClassMembers(outputBasePath);
  670. }
  671. // ============================================================================
  672. // List of all required header files
  673. static vector<string> headers_;
  674. // Discarded header files for statistic
  675. static vector<string> ignoredHeaders_;
  676. // Add header to lists if not added yet
  677. void AddHeader(const string& headerFile)
  678. {
  679. if (IsIgnoredHeader(headerFile))
  680. {
  681. if (!CONTAINS(ignoredHeaders_, headerFile))
  682. ignoredHeaders_.push_back(headerFile);
  683. }
  684. else
  685. {
  686. if (!CONTAINS(headers_, headerFile))
  687. headers_.push_back(headerFile);
  688. }
  689. }
  690. // Write result to GeneratedIncludes.h
  691. static void SaveIncludes(const string& outputBasePath)
  692. {
  693. sort(headers_.begin(), headers_.end());
  694. sort(ignoredHeaders_.begin(), ignoredHeaders_.end());
  695. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedIncludes.h");
  696. ofs <<
  697. "// DO NOT EDIT. This file is generated\n"
  698. "\n"
  699. "#pragma once\n"
  700. "\n";
  701. string openedDefine;
  702. bool isFirst = true;
  703. for (const string& header : headers_)
  704. {
  705. string insideDefine = InsideDefine(header);
  706. if (insideDefine != openedDefine)
  707. {
  708. if (!openedDefine.empty())
  709. {
  710. ofs << "#endif\n";
  711. openedDefine.clear();
  712. }
  713. if (!isFirst) // First include can be guarded. Avoid print \n before it
  714. ofs << "\n";
  715. if (!insideDefine.empty())
  716. {
  717. ofs << "#ifdef " << insideDefine << "\n";
  718. openedDefine = insideDefine;
  719. }
  720. }
  721. ofs << "#include \"" << header << "\"\n";
  722. isFirst = false;
  723. }
  724. if (!openedDefine.empty())
  725. {
  726. ofs << "#endif\n";
  727. openedDefine.clear();
  728. }
  729. if (headers_.size() > 0)
  730. ofs << "\n";
  731. if (ignoredHeaders_.size() > 0)
  732. ofs << "// Ignored headers\n\n";
  733. isFirst = true;
  734. for (const string& header : ignoredHeaders_)
  735. {
  736. string insideDefine = InsideDefine(header);
  737. if (insideDefine != openedDefine)
  738. {
  739. if (!openedDefine.empty())
  740. {
  741. ofs << "//#endif\n";
  742. openedDefine.clear();
  743. }
  744. if (!isFirst) // First include can be guarded. Avoid print \n before it
  745. ofs << "\n";
  746. if (!insideDefine.empty())
  747. {
  748. ofs << "//#ifdef " << insideDefine << "\n";
  749. openedDefine = insideDefine;
  750. }
  751. }
  752. ofs << "//#include \"" << header << "\"\n";
  753. isFirst = false;
  754. }
  755. if (!openedDefine.empty())
  756. ofs << "//#endif\n";
  757. }
  758. }
  759. void SaveResult(const string& outputBasePath)
  760. {
  761. Result::SaveEnums(outputBasePath);
  762. Result::SaveGlobalFunctions(outputBasePath);
  763. Result::SaveGlobalVariables(outputBasePath);
  764. Result::SaveClasses(outputBasePath);
  765. Result::SaveIncludes(outputBasePath);
  766. }
  767. }