ASResult.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. //
  2. // Copyright (c) 2008-2022 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. #include <cassert>
  27. #include <unordered_map>
  28. #include <memory>
  29. using namespace std;
  30. extern string _sourceDir;
  31. namespace ASBindingGenerator
  32. {
  33. ASGeneratedFile_WithRegistrationFunction::ASGeneratedFile_WithRegistrationFunction(const string& outputFilePath, const string& functionName)
  34. {
  35. outputFilePath_ = outputFilePath;
  36. functionName_ = functionName;
  37. }
  38. // ============================================================================
  39. void ASGeneratedFile_Members::Save()
  40. {
  41. ofstream out(outputFilePath_);
  42. out <<
  43. "// DO NOT EDIT. This file is generated\n"
  44. "\n"
  45. "#include \"../Precompiled.h\"\n"
  46. "#include \"../AngelScript/APITemplates.h\"\n"
  47. "\n"
  48. "#include \"../AngelScript/Generated_Includes.h\"\n"
  49. "#include \"../AngelScript/Manual.h\"\n"
  50. "\n"
  51. "namespace Urho3D\n"
  52. "{\n"
  53. "\n"
  54. << glue_.str() <<
  55. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  56. "{\n"
  57. << reg_.str() <<
  58. "}\n"
  59. "\n"
  60. "}\n";
  61. }
  62. // ============================================================================
  63. ASGeneratedFile_Templates::ASGeneratedFile_Templates(const string& outputFilePath)
  64. {
  65. outputFilePath_ = outputFilePath;
  66. }
  67. void ASGeneratedFile_Templates::Save()
  68. {
  69. ofstream out(outputFilePath_);
  70. out <<
  71. "// DO NOT EDIT. This file is generated\n"
  72. "\n"
  73. "#pragma once\n"
  74. "\n"
  75. "#include \"../Precompiled.h\"\n"
  76. "#include \"../AngelScript/APITemplates.h\"\n"
  77. "\n"
  78. "#include \"../AngelScript/Generated_Includes.h\"\n"
  79. "\n"
  80. "#include \"../AngelScript/Manual.h\"\n"
  81. "\n"
  82. "namespace Urho3D\n"
  83. "{\n"
  84. "\n"
  85. << glue_.str()
  86. << reg_.str() <<
  87. "}\n";
  88. }
  89. bool ProcessedEnum::operator <(const ProcessedEnum& rhs) const
  90. {
  91. if (insideDefine_ != rhs.insideDefine_)
  92. return insideDefine_ < rhs.insideDefine_;
  93. return name_ < rhs.name_;
  94. }
  95. bool ProcessedGlobalFunction::operator <(const ProcessedGlobalFunction& rhs) const
  96. {
  97. if (insideDefine_ != rhs.insideDefine_)
  98. return insideDefine_ < rhs.insideDefine_;
  99. if (name_ != rhs.name_)
  100. return name_ < rhs.name_;
  101. // Overloads with the same name may exist
  102. if (comment_ != rhs.comment_)
  103. return comment_ < rhs.comment_;
  104. // Different specializations of the same template and aliases have the same comment
  105. return registration_ < rhs.registration_;
  106. }
  107. bool ProcessedGlobalVariable::operator <(const ProcessedGlobalVariable& rhs) const
  108. {
  109. if (insideDefine_ != rhs.insideDefine_)
  110. return insideDefine_ < rhs.insideDefine_;
  111. return name_ < rhs.name_;
  112. }
  113. bool ProcessedClass::operator <(const ProcessedClass& rhs) const
  114. {
  115. if (inherianceDeep_ != rhs.inherianceDeep_)
  116. return inherianceDeep_ < rhs.inherianceDeep_;
  117. if (insideDefine_ != rhs.insideDefine_)
  118. return insideDefine_ < rhs.insideDefine_;
  119. return name_ < rhs.name_;
  120. }
  121. bool MemberRegistrationError::operator <(const MemberRegistrationError& rhs) const
  122. {
  123. if (name_ != rhs.name_)
  124. return name_ < rhs.name_;
  125. return comment_ < rhs.comment_;
  126. }
  127. namespace Result
  128. {
  129. vector<ProcessedEnum> enums_;
  130. // Write result to Generated_Enums.cpp
  131. static void SaveEnums()
  132. {
  133. sort(enums_.begin(), enums_.end());
  134. ofstream ofs(_sourceDir + "/Source/Urho3D/AngelScript/Generated_Enums.cpp");
  135. ofs <<
  136. "// DO NOT EDIT. This file is generated\n"
  137. "\n"
  138. "// We need register all enums before registration of any functions because functions can use any enums\n"
  139. "\n"
  140. "#include \"../Precompiled.h\"\n"
  141. "#include \"../AngelScript/APITemplates.h\"\n"
  142. "\n"
  143. "#include \"../AngelScript/Generated_Includes.h\"\n"
  144. "\n"
  145. "namespace Urho3D\n"
  146. "{\n"
  147. "\n";
  148. for (const ProcessedEnum& processedEnum : enums_)
  149. {
  150. if (!processedEnum.glue_.size())
  151. continue;
  152. if (!processedEnum.insideDefine_.empty())
  153. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  154. ofs << "// " << processedEnum.comment_ << "\n";
  155. for (const string& glue : processedEnum.glue_)
  156. ofs << glue << "\n";
  157. if (!processedEnum.insideDefine_.empty())
  158. ofs << "#endif\n";
  159. ofs << "\n";
  160. }
  161. ofs <<
  162. "void ASRegisterGeneratedEnums(asIScriptEngine* engine)\n"
  163. "{\n";
  164. bool isFirst = true;
  165. string openedDefine;
  166. for (const ProcessedEnum& processedEnum : enums_)
  167. {
  168. if (processedEnum.insideDefine_ != openedDefine && !openedDefine.empty())
  169. {
  170. ofs << "#endif\n";
  171. openedDefine.clear();
  172. }
  173. if (!isFirst)
  174. ofs << "\n";
  175. if (processedEnum.insideDefine_ != openedDefine && !processedEnum.insideDefine_.empty())
  176. {
  177. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  178. openedDefine = processedEnum.insideDefine_;
  179. }
  180. ofs << " // " << processedEnum.comment_ << "\n";
  181. for (const string& registration : processedEnum.registration_)
  182. ofs << " " << registration << "\n";
  183. isFirst = false;
  184. }
  185. if (!openedDefine.empty())
  186. ofs << "#endif\n";
  187. ofs <<
  188. "}\n"
  189. "\n"
  190. "}\n";
  191. }
  192. // ============================================================================
  193. vector<ProcessedGlobalFunction> globalFunctions_;
  194. // Write result to Generated_GlobalFunctions.cpp
  195. static void SaveGlobalFunctions()
  196. {
  197. sort(globalFunctions_.begin(), globalFunctions_.end());
  198. ofstream ofs(_sourceDir + "/Source/Urho3D/AngelScript/Generated_GlobalFunctions.cpp");
  199. ofs <<
  200. "// DO NOT EDIT. This file is generated\n"
  201. "\n"
  202. "#include \"../Precompiled.h\"\n"
  203. "#include \"../AngelScript/APITemplates.h\"\n"
  204. "\n"
  205. "#include \"../AngelScript/Generated_Includes.h\"\n"
  206. "\n"
  207. "namespace Urho3D\n"
  208. "{\n"
  209. "\n";
  210. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  211. {
  212. if (globalFunction.glue_.empty())
  213. continue;
  214. if (!globalFunction.insideDefine_.empty())
  215. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  216. ofs << "// " << globalFunction.comment_ << "\n";
  217. ofs << globalFunction.glue_ << "\n";
  218. if (!globalFunction.insideDefine_.empty())
  219. ofs << "#endif\n";
  220. ofs << "\n";
  221. }
  222. ofs <<
  223. "void ASRegisterGeneratedGlobalFunctions(asIScriptEngine* engine)\n"
  224. "{\n";
  225. bool isFirst = true;
  226. string openedDefine;
  227. string lastComment;
  228. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  229. {
  230. if (globalFunction.insideDefine_ != openedDefine && !openedDefine.empty())
  231. {
  232. ofs << "#endif\n";
  233. openedDefine.clear();
  234. }
  235. if (!isFirst && lastComment != globalFunction.comment_)
  236. ofs << "\n";
  237. if (globalFunction.insideDefine_ != openedDefine && !globalFunction.insideDefine_.empty())
  238. {
  239. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  240. openedDefine = globalFunction.insideDefine_;
  241. }
  242. if (lastComment != globalFunction.comment_)
  243. ofs << " // " << globalFunction.comment_ << "\n";
  244. ofs << " " << globalFunction.registration_ << "\n";
  245. isFirst = false;
  246. lastComment = globalFunction.comment_;
  247. }
  248. if (!openedDefine.empty())
  249. ofs << "#endif\n";
  250. ofs <<
  251. "}\n"
  252. "\n"
  253. "}\n";
  254. }
  255. // ============================================================================
  256. vector<ProcessedGlobalVariable> globalVariables_;
  257. // Write result to Generated_GlobalVariables.cpp
  258. static void SaveGlobalVariables()
  259. {
  260. sort(globalVariables_.begin(), globalVariables_.end());
  261. ofstream ofs(_sourceDir + "/Source/Urho3D/AngelScript/Generated_GlobalVariables.cpp");
  262. ofs <<
  263. "// DO NOT EDIT. This file is generated\n"
  264. "\n"
  265. "#include \"../Precompiled.h\"\n"
  266. "#include \"../AngelScript/APITemplates.h\"\n"
  267. "\n"
  268. "#include \"../AngelScript/Generated_Includes.h\"\n"
  269. "\n"
  270. "// Some headers could re-define M_PI, ensure that it's undefined\n"
  271. "#undef M_PI\n"
  272. "\n"
  273. "namespace Urho3D\n"
  274. "{\n"
  275. "\n"
  276. "void ASRegisterGeneratedGlobalVariables(asIScriptEngine* engine)\n"
  277. "{\n";
  278. bool isFirst = true;
  279. string openedDefine;
  280. string lastComment;
  281. for (const ProcessedGlobalVariable& globalVariable : globalVariables_)
  282. {
  283. if (globalVariable.insideDefine_ != openedDefine && !openedDefine.empty())
  284. {
  285. ofs << "#endif\n";
  286. openedDefine.clear();
  287. }
  288. if (!isFirst && lastComment != globalVariable.comment_)
  289. ofs << "\n";
  290. if (globalVariable.insideDefine_ != openedDefine && !globalVariable.insideDefine_.empty())
  291. {
  292. ofs << "#ifdef " << globalVariable.insideDefine_ << "\n";
  293. openedDefine = globalVariable.insideDefine_;
  294. }
  295. if (lastComment != globalVariable.comment_)
  296. ofs << " // " << globalVariable.comment_ << "\n";
  297. ofs << " " << globalVariable.registration_ << "\n";
  298. isFirst = false;
  299. lastComment = globalVariable.comment_;
  300. }
  301. if (!openedDefine.empty())
  302. ofs << "#endif\n";
  303. ofs <<
  304. "}\n"
  305. "\n"
  306. "}\n";
  307. }
  308. // ============================================================================
  309. vector<ProcessedClass> classes_;
  310. // Write result to Generated_ObjectTypes.cpp
  311. static void SaveObjectTypes()
  312. {
  313. ofstream ofs(_sourceDir + "/Source/Urho3D/AngelScript/Generated_ObjectTypes.cpp");
  314. ofs <<
  315. "// DO NOT EDIT. This file is generated\n"
  316. "\n"
  317. "// We need register all types before registration of any functions because functions can use any types\n"
  318. "\n"
  319. "#include \"../Precompiled.h\"\n"
  320. "#include \"../AngelScript/APITemplates.h\"\n"
  321. "\n"
  322. "#include \"../AngelScript/Generated_Includes.h\"\n"
  323. "\n"
  324. "namespace Urho3D\n"
  325. "{\n"
  326. "\n"
  327. "void ASRegisterGeneratedObjectTypes(asIScriptEngine* engine)\n"
  328. "{\n";
  329. string openedDefine;
  330. bool isFirst = true;
  331. for (const ProcessedClass& processedClass : classes_)
  332. {
  333. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  334. {
  335. ofs << "#endif\n";
  336. openedDefine.clear();
  337. }
  338. if (!isFirst)
  339. ofs << "\n";
  340. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  341. {
  342. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  343. openedDefine = processedClass.insideDefine_;
  344. }
  345. ofs
  346. << " // " << processedClass.comment_ << "\n"
  347. << " " << processedClass.objectTypeRegistration_ << "\n";
  348. isFirst = false;
  349. }
  350. if (!openedDefine.empty())
  351. ofs << "#endif\n";
  352. ofs <<
  353. "}\n"
  354. "\n"
  355. "}\n";
  356. }
  357. // Write result to Generated_DefaultConstructors.cpp
  358. static void SaveDefaultConstructors()
  359. {
  360. ofstream ofs(_sourceDir + "/Source/Urho3D/AngelScript/Generated_DefaultConstructors.cpp");
  361. ofs <<
  362. "// DO NOT EDIT. This file is generated\n"
  363. "\n"
  364. "// We need register default constructors before any members to allow using in Array<type>\n"
  365. "\n"
  366. "#include \"../Precompiled.h\"\n"
  367. "#include \"../AngelScript/APITemplates.h\"\n"
  368. "\n"
  369. "#include \"../AngelScript/Generated_Includes.h\"\n"
  370. "\n"
  371. "namespace Urho3D\n"
  372. "{\n";
  373. string openedDefine;
  374. for (const ProcessedClass& processedClass : classes_)
  375. {
  376. if (!processedClass.defaultConstructor_)
  377. continue;
  378. if (processedClass.defaultConstructor_->glue_.empty())
  379. continue;
  380. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  381. {
  382. ofs << "\n#endif\n";
  383. openedDefine.clear();
  384. }
  385. ofs << "\n";
  386. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  387. {
  388. ofs << "#ifdef " << processedClass.insideDefine_ << "\n\n";
  389. openedDefine = processedClass.insideDefine_;
  390. }
  391. ofs <<
  392. "// " << processedClass.defaultConstructor_->comment_ << "\n" <<
  393. processedClass.defaultConstructor_->glue_;
  394. }
  395. if (!openedDefine.empty())
  396. {
  397. ofs << "\n#endif\n";
  398. openedDefine.clear();
  399. }
  400. ofs <<
  401. "\n"
  402. "void ASRegisterGeneratedDefaultConstructors(asIScriptEngine* engine)\n"
  403. "{\n";
  404. bool isFirst = true;
  405. for (const ProcessedClass& processedClass : classes_)
  406. {
  407. if (!processedClass.defaultConstructor_)
  408. continue;
  409. if (processedClass.noBind_)
  410. continue;
  411. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  412. {
  413. ofs << "#endif\n";
  414. openedDefine.clear();
  415. }
  416. if (!isFirst)
  417. ofs << "\n";
  418. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  419. {
  420. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  421. openedDefine = processedClass.insideDefine_;
  422. }
  423. ofs <<
  424. " // " << processedClass.defaultConstructor_->comment_ << "\n" <<
  425. " " << processedClass.defaultConstructor_->registration_ << "\n";
  426. isFirst = false;
  427. }
  428. if (!openedDefine.empty())
  429. ofs << "#endif\n";
  430. ofs <<
  431. "}\n"
  432. "\n"
  433. "}\n";
  434. }
  435. // Write result to Generated_Classes.cpp
  436. static void SaveGeneratedClasses()
  437. {
  438. ofstream ofs(_sourceDir + "/Source/Urho3D/AngelScript/Generated_Classes.cpp");
  439. ofs <<
  440. "// DO NOT EDIT. This file is generated\n"
  441. "\n"
  442. "#include \"../Precompiled.h\"\n"
  443. "#include \"../AngelScript/APITemplates.h\"\n"
  444. "\n"
  445. "#include \"../AngelScript/Generated_Includes.h\"\n"
  446. "#include \"../AngelScript/Generated_Members.h\"\n"
  447. "#include \"../AngelScript/Manual.h\"\n"
  448. "\n"
  449. "namespace Urho3D\n"
  450. "{\n";
  451. string openedDefine;
  452. for (ProcessedClass& processedClass : classes_)
  453. {
  454. if (processedClass.noBind_)
  455. continue;
  456. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  457. {
  458. ofs << "\n#endif // def " << openedDefine << "\n";
  459. openedDefine.clear();
  460. }
  461. ofs << "\n";
  462. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  463. {
  464. ofs << "#ifdef " << processedClass.insideDefine_ << "\n\n";
  465. openedDefine = processedClass.insideDefine_;
  466. }
  467. if (processedClass.destructor_ && !processedClass.destructor_->glue_.empty())
  468. {
  469. ofs <<
  470. "// " << processedClass.destructor_->comment_ << "\n"
  471. << processedClass.destructor_->glue_ <<
  472. "\n";
  473. }
  474. for (const SpecialMethodRegistration& nonDefaultConstructor : processedClass.nonDefaultConstructors_)
  475. {
  476. ofs <<
  477. "// " << nonDefaultConstructor.comment_ << "\n"
  478. << nonDefaultConstructor.glue_ <<
  479. "\n";
  480. }
  481. for (const Registration& personalMethod : processedClass.personalMethods_)
  482. {
  483. if (personalMethod.glue_.size())
  484. {
  485. ofs <<
  486. "// " << personalMethod.comment_ << "\n"
  487. << personalMethod.glue_ <<
  488. "\n";
  489. }
  490. }
  491. for (const Registration& personalStaticMethod : processedClass.personalStaticMethods_)
  492. {
  493. if (personalStaticMethod.glue_.size())
  494. {
  495. ofs <<
  496. "// " << personalStaticMethod.comment_ << "\n"
  497. << personalStaticMethod.glue_ <<
  498. "\n";
  499. }
  500. }
  501. ofs <<
  502. "// " << processedClass.comment_ << "\n"
  503. "static void Register_" << processedClass.name_ << "(asIScriptEngine* engine)\n"
  504. "{\n";
  505. bool needGap = false;
  506. sort(processedClass.unregisteredSpecialMethods_.begin(), processedClass.unregisteredSpecialMethods_.end());
  507. for (const MemberRegistrationError& regError : processedClass.unregisteredSpecialMethods_)
  508. {
  509. ofs <<
  510. " // " << regError.comment_ << "\n"
  511. " // " << regError.message_ << "\n";
  512. needGap = true;
  513. }
  514. if (needGap && processedClass.nonDefaultConstructors_.size())
  515. ofs << '\n';
  516. for (const SpecialMethodRegistration& nonDefaultConstructor : processedClass.nonDefaultConstructors_)
  517. {
  518. ofs <<
  519. " // " << nonDefaultConstructor.comment_ << "\n"
  520. " " << nonDefaultConstructor.registration_ << "\n";
  521. needGap = true;
  522. }
  523. if (processedClass.destructor_)
  524. {
  525. if (needGap)
  526. ofs << '\n';
  527. ofs <<
  528. " // " << processedClass.destructor_->comment_ << "\n"
  529. " " << processedClass.destructor_->registration_ << "\n";
  530. needGap = true;
  531. }
  532. if (needGap && processedClass.subclassRegistrations_.size())
  533. ofs << '\n';
  534. for (const string& subclassRegistration : processedClass.subclassRegistrations_)
  535. {
  536. ofs << " " << subclassRegistration << '\n';
  537. needGap = true;
  538. }
  539. if (needGap)
  540. ofs << '\n';
  541. ofs <<
  542. " RegisterMembers_" << processedClass.name_ << "<" << processedClass.name_ << ">(engine, \"" << processedClass.name_ << "\");\n"
  543. "\n"
  544. " #ifdef REGISTER_CLASS_MANUAL_PART_" << processedClass.name_ << "\n"
  545. " REGISTER_CLASS_MANUAL_PART_" << processedClass.name_ << "();\n"
  546. " #endif\n";
  547. for (const MemberRegistrationError& unregisteredPersonalMethod : processedClass.unregisteredPersonalMethods_)
  548. {
  549. ofs <<
  550. "\n"
  551. " // " << unregisteredPersonalMethod.comment_ << "\n"
  552. " // " << unregisteredPersonalMethod.message_ << "\n";
  553. needGap = true;
  554. }
  555. for (const Registration& perosnalMethod : processedClass.personalMethods_)
  556. {
  557. ofs <<
  558. "\n"
  559. " // " << perosnalMethod.comment_ << "\n";
  560. for (const string& reg : perosnalMethod.registration_)
  561. ofs << " " << reg << '\n';
  562. }
  563. for (const MemberRegistrationError& unregisteredPersonalStaticMethod : processedClass.unregisteredPersonalStaticMethods_)
  564. {
  565. ofs <<
  566. "\n"
  567. " // " << unregisteredPersonalStaticMethod.comment_ << "\n"
  568. " // " << unregisteredPersonalStaticMethod.message_ << "\n";
  569. needGap = true;
  570. }
  571. for (const Registration& perosnalStaticMethod : processedClass.personalStaticMethods_)
  572. {
  573. ofs <<
  574. "\n"
  575. " // " << perosnalStaticMethod.comment_ << "\n";
  576. for (const string& reg : perosnalStaticMethod.registration_)
  577. ofs << " " << reg << '\n';
  578. }
  579. if (processedClass.additionalLines_.size())
  580. ofs << '\n';
  581. for (string str : processedClass.additionalLines_)
  582. ofs << str << '\n';
  583. ofs << "}\n";
  584. }
  585. if (!openedDefine.empty())
  586. {
  587. ofs << "\n#endif // def " << openedDefine << "\n";
  588. openedDefine.clear();
  589. }
  590. ofs <<
  591. "\n"
  592. "void ASRegisterGeneratedClasses(asIScriptEngine* engine)\n"
  593. "{\n";
  594. bool isFirst = true;
  595. for (const ProcessedClass& processedClass : classes_)
  596. {
  597. if (processedClass.noBind_)
  598. continue;
  599. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  600. {
  601. ofs << "#endif\n";
  602. openedDefine.clear();
  603. }
  604. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  605. {
  606. if (!isFirst)
  607. ofs << "\n";
  608. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  609. openedDefine = processedClass.insideDefine_;
  610. }
  611. ofs << " Register_" << processedClass.name_ << "(engine);\n";
  612. isFirst = false;
  613. }
  614. if (!openedDefine.empty())
  615. ofs << "#endif\n";
  616. ofs <<
  617. "}\n"
  618. "\n"
  619. "}\n";
  620. }
  621. struct MemberCppFile
  622. {
  623. ofstream ofs_;
  624. string openedDefine_;
  625. bool needGap_ = false;
  626. MemberCppFile(const string& dirName)
  627. {
  628. string fileName = _sourceDir + "/Source/Urho3D/AngelScript/Generated_Members_" + dirName + ".cpp";
  629. ofs_ = ofstream(fileName);
  630. ofs_ <<
  631. "// DO NOT EDIT. This file is generated\n"
  632. "\n"
  633. "#include \"../Precompiled.h\"\n"
  634. "#include \"../AngelScript/APITemplates.h\"\n"
  635. "\n"
  636. "#include \"../AngelScript/Generated_Includes.h\"\n"
  637. "#include \"../AngelScript/Generated_ClassMembers.h\"\n"
  638. "#include \"../AngelScript/Manual.h\"\n"
  639. "\n"
  640. "namespace Urho3D\n"
  641. "{\n";
  642. }
  643. };
  644. unordered_map<string, shared_ptr<MemberCppFile>> memberCppFiles_;
  645. shared_ptr<MemberCppFile> GetMemberCppFile(const string& dirName)
  646. {
  647. auto it = memberCppFiles_.find(dirName);
  648. if (it == memberCppFiles_.end())
  649. {
  650. auto newElement = memberCppFiles_.emplace(dirName, make_shared<MemberCppFile>(dirName));
  651. it = newElement.first;
  652. }
  653. return (*it).second;
  654. }
  655. // Write result to GeneratedClassMembers.cpp and GeneratedClassMembers.h // TODO change comment
  656. static void SaveClassMembers()
  657. {
  658. ofstream ofsH = ofstream(_sourceDir + "/Source/Urho3D/AngelScript/Generated_Members.h");
  659. ofsH <<
  660. "// DO NOT EDIT. This file is generated\n"
  661. "\n"
  662. "#pragma once\n"
  663. "\n"
  664. "#include \"../AngelScript/APITemplates.h\"\n"
  665. "\n"
  666. "#include \"../AngelScript/Generated_Includes.h\"\n"
  667. "#include \"../AngelScript/Manual.h\"\n"
  668. "\n"
  669. "namespace Urho3D\n"
  670. "{\n";
  671. string openedDefine;
  672. for (const ProcessedClass& processedClass : classes_)
  673. {
  674. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  675. {
  676. ofsH <<
  677. "\n"
  678. "#endif // def " << openedDefine << "\n";
  679. openedDefine.clear();
  680. }
  681. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  682. {
  683. ofsH <<
  684. "\n"
  685. "#ifdef " << processedClass.insideDefine_ << "\n";
  686. openedDefine = processedClass.insideDefine_;
  687. }
  688. for (const Registration& method : processedClass.templateMethods_)
  689. {
  690. if (method.glue_.size())
  691. {
  692. ofsH <<
  693. "\n"
  694. "// " << method.comment_ << "\n"
  695. << method.glue_;
  696. }
  697. }
  698. for (const Registration& staticMethod : processedClass.templateStaticMethods_)
  699. {
  700. if (staticMethod.glue_.size())
  701. {
  702. ofsH <<
  703. "\n"
  704. "// " << staticMethod.comment_ << "\n"
  705. << staticMethod.glue_;
  706. }
  707. }
  708. ofsH <<
  709. "\n"
  710. "// " << processedClass.comment_ << "\n"
  711. "template <class T> void RegisterMembers_" << processedClass.name_ << "(asIScriptEngine* engine, const char* className)\n"
  712. "{\n";
  713. bool needGap = false;
  714. if (processedClass.baseClassNames_.size())
  715. {
  716. for (const string& baseClassName : processedClass.baseClassNames_)
  717. ofsH << " RegisterMembers_" << baseClassName << "<T>(engine, className);\n";
  718. needGap = true;
  719. }
  720. if (needGap && processedClass.unregisteredTemplateMethods_.size())
  721. ofsH << '\n';
  722. for (const MemberRegistrationError& unregisteredTemplateMethod : processedClass.unregisteredTemplateMethods_)
  723. {
  724. ofsH <<
  725. " // " << unregisteredTemplateMethod.comment_ << "\n"
  726. " // " << unregisteredTemplateMethod.message_ << "\n";
  727. needGap = true;
  728. }
  729. for (const Registration& method : processedClass.templateMethods_)
  730. {
  731. if (needGap)
  732. ofsH << '\n';
  733. ofsH << " // " << method.comment_ << '\n';
  734. for (const string& registration : method.registration_)
  735. ofsH << " " << registration << '\n';
  736. needGap = true;
  737. }
  738. if (needGap && processedClass.unregisteredTemplateStaticMethods_.size())
  739. ofsH << '\n';
  740. for (const MemberRegistrationError& unregisteredTemplateStaticMethod : processedClass.unregisteredTemplateStaticMethods_)
  741. {
  742. ofsH <<
  743. " // " << unregisteredTemplateStaticMethod.comment_ << "\n"
  744. " // " << unregisteredTemplateStaticMethod.message_ << "\n";
  745. needGap = true;
  746. }
  747. for (const Registration& staticMethod : processedClass.templateStaticMethods_)
  748. {
  749. if (needGap)
  750. ofsH << '\n';
  751. ofsH << " // " << staticMethod.comment_ << '\n';
  752. for (const string& registration : staticMethod.registration_)
  753. ofsH << " " << registration << '\n';
  754. needGap = true;
  755. }
  756. if (needGap && processedClass.unregisteredTemplateFields_.size())
  757. ofsH << '\n';
  758. for (const MemberRegistrationError& unregisteredTemplateField : processedClass.unregisteredTemplateFields_)
  759. {
  760. ofsH <<
  761. " // " << unregisteredTemplateField.comment_ << "\n"
  762. " // " << unregisteredTemplateField.message_ << "\n";
  763. needGap = true;
  764. }
  765. for (const Registration& field : processedClass.templateFields_)
  766. {
  767. if (needGap)
  768. ofsH << '\n';
  769. ofsH << " // " << field.comment_ << '\n';
  770. for (const string& registration : field.registration_)
  771. ofsH << " " << registration << '\n';
  772. needGap = true;
  773. }
  774. if (needGap && processedClass.unregisteredTemplateStaticFields_.size())
  775. ofsH << '\n';
  776. for (const MemberRegistrationError& unregisteredTemplateStaticField : processedClass.unregisteredTemplateStaticFields_)
  777. {
  778. ofsH <<
  779. " // " << unregisteredTemplateStaticField.comment_ << "\n"
  780. " // " << unregisteredTemplateStaticField.message_ << "\n";
  781. needGap = true;
  782. }
  783. for (const Registration& field : processedClass.templateStaticFields_)
  784. {
  785. if (needGap)
  786. ofsH << '\n';
  787. ofsH << " // " << field.comment_ << '\n';
  788. for (const string& registration : field.registration_)
  789. ofsH << " " << registration << '\n';
  790. needGap = true;
  791. }
  792. if (needGap)
  793. ofsH << '\n';
  794. ofsH <<
  795. " #ifdef REGISTER_MEMBERS_MANUAL_PART_" << processedClass.name_ << "\n"
  796. " REGISTER_MEMBERS_MANUAL_PART_" << processedClass.name_ << "();\n"
  797. " #endif\n";
  798. ofsH << "}\n";
  799. }
  800. if (!openedDefine.empty())
  801. {
  802. ofsH <<
  803. "\n"
  804. "#endif // def " << openedDefine << "\n";
  805. openedDefine.clear();
  806. }
  807. ofsH <<
  808. "\n"
  809. "} // namespace Urho3D\n";
  810. }
  811. static void SaveClasses()
  812. {
  813. sort(classes_.begin(), classes_.end());
  814. SaveObjectTypes();
  815. SaveDefaultConstructors();
  816. SaveGeneratedClasses();
  817. SaveClassMembers();
  818. }
  819. // ============================================================================
  820. // List of all required header files
  821. static vector<string> headers_;
  822. // Discarded header files for statistic
  823. static vector<string> ignoredHeaders_;
  824. // Add header to lists if not added yet
  825. void AddHeader(const string& headerFile)
  826. {
  827. if (IsIgnoredHeader(headerFile))
  828. {
  829. if (!CONTAINS(ignoredHeaders_, headerFile))
  830. ignoredHeaders_.push_back(headerFile);
  831. }
  832. else
  833. {
  834. if (!CONTAINS(headers_, headerFile))
  835. headers_.push_back(headerFile);
  836. }
  837. }
  838. // Write result to Generated_Includes.h
  839. static void SaveIncludes()
  840. {
  841. sort(headers_.begin(), headers_.end());
  842. sort(ignoredHeaders_.begin(), ignoredHeaders_.end());
  843. ofstream ofs(_sourceDir + "/Source/Urho3D/AngelScript/Generated_Includes.h");
  844. ofs <<
  845. "// DO NOT EDIT. This file is generated\n"
  846. "\n"
  847. "#pragma once\n"
  848. "\n";
  849. string openedDefine;
  850. bool isFirst = true;
  851. for (const string& header : headers_)
  852. {
  853. string insideDefine = InsideDefine(header);
  854. if (insideDefine != openedDefine)
  855. {
  856. if (!openedDefine.empty())
  857. {
  858. ofs << "#endif\n";
  859. openedDefine.clear();
  860. }
  861. if (!isFirst) // First include can be guarded. Avoid print \n before it
  862. ofs << "\n";
  863. if (!insideDefine.empty())
  864. {
  865. ofs << "#ifdef " << insideDefine << "\n";
  866. openedDefine = insideDefine;
  867. }
  868. }
  869. ofs << "#include \"" << header << "\"\n";
  870. isFirst = false;
  871. }
  872. if (!openedDefine.empty())
  873. {
  874. ofs << "#endif\n";
  875. openedDefine.clear();
  876. }
  877. if (headers_.size() > 0)
  878. ofs << "\n";
  879. if (ignoredHeaders_.size() > 0)
  880. ofs << "// Ignored headers\n\n";
  881. isFirst = true;
  882. for (const string& header : ignoredHeaders_)
  883. {
  884. string insideDefine = InsideDefine(header);
  885. if (insideDefine != openedDefine)
  886. {
  887. if (!openedDefine.empty())
  888. {
  889. ofs << "//#endif\n";
  890. openedDefine.clear();
  891. }
  892. if (!isFirst) // First include can be guarded. Avoid print \n before it
  893. ofs << "\n";
  894. if (!insideDefine.empty())
  895. {
  896. ofs << "//#ifdef " << insideDefine << "\n";
  897. openedDefine = insideDefine;
  898. }
  899. }
  900. ofs << "//#include \"" << header << "\"\n";
  901. isFirst = false;
  902. }
  903. if (!openedDefine.empty())
  904. ofs << "//#endif\n";
  905. }
  906. }
  907. void SaveResult()
  908. {
  909. Result::SaveEnums();
  910. Result::SaveGlobalFunctions();
  911. Result::SaveGlobalVariables();
  912. Result::SaveClasses();
  913. Result::SaveIncludes();
  914. }
  915. }