ASResult.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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_HighPriority::Save()
  35. {
  36. ofstream out(outputFilePath_);
  37. out <<
  38. "// DO NOT EDIT. This file is generated\n"
  39. "\n"
  40. "// We need register default constructors before any members to allow using in Array<type>\n"
  41. "\n"
  42. "#include \"../Precompiled.h\"\n"
  43. "#include \"../AngelScript/APITemplates.h\"\n"
  44. "\n"
  45. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  46. "\n"
  47. "namespace Urho3D\n"
  48. "{\n"
  49. "\n"
  50. "void FakeAddRef(void* ptr);\n"
  51. "void FakeReleaseRef(void* ptr);\n"
  52. "\n"
  53. << glue_.str() <<
  54. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  55. "{\n"
  56. << reg_.str() <<
  57. "}\n"
  58. "\n"
  59. "}\n";
  60. }
  61. // ============================================================================
  62. void ASGeneratedFile_Members::Save()
  63. {
  64. ofstream out(outputFilePath_);
  65. out <<
  66. "// DO NOT EDIT. This file is generated\n"
  67. "\n"
  68. "#include \"../Precompiled.h\"\n"
  69. "#include \"../AngelScript/APITemplates.h\"\n"
  70. "\n"
  71. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  72. "#include \"../AngelScript/Manual.h\"\n"
  73. "\n"
  74. "namespace Urho3D\n"
  75. "{\n"
  76. "\n"
  77. "void FakeAddRef(void* ptr);\n"
  78. "void FakeReleaseRef(void* ptr);\n"
  79. "\n"
  80. << glue_.str() <<
  81. "void " << functionName_ << "(asIScriptEngine* engine)\n"
  82. "{\n"
  83. << reg_.str() <<
  84. "}\n"
  85. "\n"
  86. "}\n";
  87. }
  88. // ============================================================================
  89. ASGeneratedFile_Templates::ASGeneratedFile_Templates(const string& outputFilePath)
  90. {
  91. outputFilePath_ = outputFilePath;
  92. }
  93. void ASGeneratedFile_Templates::Save()
  94. {
  95. ofstream out(outputFilePath_);
  96. out <<
  97. "// DO NOT EDIT. This file is generated\n"
  98. "\n"
  99. "#pragma once\n"
  100. "\n"
  101. "#include \"../Precompiled.h\"\n"
  102. "#include \"../AngelScript/APITemplates.h\"\n"
  103. "\n"
  104. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  105. "\n"
  106. "#include \"../AngelScript/Manual.h\"\n"
  107. "\n"
  108. "namespace Urho3D\n"
  109. "{\n"
  110. "\n"
  111. "void FakeAddRef(void* ptr);\n"
  112. "void FakeReleaseRef(void* ptr);\n"
  113. "\n"
  114. << glue_.str()
  115. << reg_.str() <<
  116. "}\n";
  117. }
  118. bool ProcessedEnum::operator <(const ProcessedEnum& rhs) const
  119. {
  120. if (insideDefine_ != rhs.insideDefine_)
  121. return insideDefine_ < rhs.insideDefine_;
  122. return name_ < rhs.name_;
  123. }
  124. bool ProcessedGlobalFunction::operator <(const ProcessedGlobalFunction& rhs) const
  125. {
  126. if (insideDefine_ != rhs.insideDefine_)
  127. return insideDefine_ < rhs.insideDefine_;
  128. if (name_ != rhs.name_)
  129. return name_ < rhs.name_;
  130. // Overloads with the same name may exist
  131. if (comment_ != rhs.comment_)
  132. return comment_ < rhs.comment_;
  133. // Different specializations of the same template and aliases have the same comment
  134. return registration_ < rhs.registration_;
  135. }
  136. bool ProcessedGlobalVariable::operator <(const ProcessedGlobalVariable& rhs) const
  137. {
  138. if (insideDefine_ != rhs.insideDefine_)
  139. return insideDefine_ < rhs.insideDefine_;
  140. return name_ < rhs.name_;
  141. }
  142. bool ProcessedClass::operator <(const ProcessedClass& rhs) const
  143. {
  144. if (insideDefine_ != rhs.insideDefine_)
  145. return insideDefine_ < rhs.insideDefine_;
  146. return name_ < rhs.name_;
  147. }
  148. namespace Result
  149. {
  150. vector<ProcessedEnum> enums_;
  151. // Write result to GeneratedEnums.cpp
  152. static void SaveEnums(const string& outputBasePath)
  153. {
  154. sort(enums_.begin(), enums_.end());
  155. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedEnums.cpp");
  156. ofs <<
  157. "// DO NOT EDIT. This file is generated\n"
  158. "\n"
  159. "// We need register all enums before registration of any functions because functions can use any enums\n"
  160. "\n"
  161. "#include \"../Precompiled.h\"\n"
  162. "#include \"../AngelScript/APITemplates.h\"\n"
  163. "\n"
  164. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  165. "\n"
  166. "namespace Urho3D\n"
  167. "{\n"
  168. "\n";
  169. for (const ProcessedEnum& processedEnum : enums_)
  170. {
  171. if (!processedEnum.glue_.size())
  172. continue;
  173. if (!processedEnum.insideDefine_.empty())
  174. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  175. ofs << "// " << processedEnum.comment_ << "\n";
  176. for (const string& glue : processedEnum.glue_)
  177. ofs << glue << "\n";
  178. if (!processedEnum.insideDefine_.empty())
  179. ofs << "#endif\n";
  180. ofs << "\n";
  181. }
  182. ofs <<
  183. "void ASRegisterGeneratedEnums(asIScriptEngine* engine)\n"
  184. "{\n";
  185. bool isFirst = true;
  186. string openedDefine;
  187. for (const ProcessedEnum& processedEnum : enums_)
  188. {
  189. if (processedEnum.insideDefine_ != openedDefine && !openedDefine.empty())
  190. {
  191. ofs << "#endif\n";
  192. openedDefine.clear();
  193. }
  194. if (!isFirst)
  195. ofs << "\n";
  196. if (processedEnum.insideDefine_ != openedDefine && !processedEnum.insideDefine_.empty())
  197. {
  198. ofs << "#ifdef " << processedEnum.insideDefine_ << "\n";
  199. openedDefine = processedEnum.insideDefine_;
  200. }
  201. ofs << " // " << processedEnum.comment_ << "\n";
  202. for (const string& registration : processedEnum.registration_)
  203. ofs << " " << registration << "\n";
  204. isFirst = false;
  205. }
  206. if (!openedDefine.empty())
  207. ofs << "#endif\n";
  208. ofs <<
  209. "}\n"
  210. "\n"
  211. "}\n";
  212. }
  213. // ============================================================================
  214. vector<ProcessedGlobalFunction> globalFunctions_;
  215. // Write result to GlobalFunctions.cpp
  216. static void SaveGlobalFunctions(const string& outputBasePath)
  217. {
  218. sort(globalFunctions_.begin(), globalFunctions_.end());
  219. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedGlobalFunctions.cpp");
  220. ofs <<
  221. "// DO NOT EDIT. This file is generated\n"
  222. "\n"
  223. "#include \"../Precompiled.h\"\n"
  224. "#include \"../AngelScript/APITemplates.h\"\n"
  225. "\n"
  226. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  227. "\n"
  228. "namespace Urho3D\n"
  229. "{\n"
  230. "\n";
  231. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  232. {
  233. if (globalFunction.glue_.empty())
  234. continue;
  235. if (!globalFunction.insideDefine_.empty())
  236. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  237. ofs << "// " << globalFunction.comment_ << "\n";
  238. ofs << globalFunction.glue_ << "\n";
  239. if (!globalFunction.insideDefine_.empty())
  240. ofs << "#endif\n";
  241. ofs << "\n";
  242. }
  243. ofs <<
  244. "void ASRegisterGeneratedGlobalFunctions(asIScriptEngine* engine)\n"
  245. "{\n";
  246. bool isFirst = true;
  247. string openedDefine;
  248. string lastComment;
  249. for (const ProcessedGlobalFunction& globalFunction : globalFunctions_)
  250. {
  251. if (globalFunction.insideDefine_ != openedDefine && !openedDefine.empty())
  252. {
  253. ofs << "#endif\n";
  254. openedDefine.clear();
  255. }
  256. if (!isFirst && lastComment != globalFunction.comment_)
  257. ofs << "\n";
  258. if (globalFunction.insideDefine_ != openedDefine && !globalFunction.insideDefine_.empty())
  259. {
  260. ofs << "#ifdef " << globalFunction.insideDefine_ << "\n";
  261. openedDefine = globalFunction.insideDefine_;
  262. }
  263. if (lastComment != globalFunction.comment_)
  264. ofs << " // " << globalFunction.comment_ << "\n";
  265. ofs << " " << globalFunction.registration_ << "\n";
  266. isFirst = false;
  267. lastComment = globalFunction.comment_;
  268. }
  269. if (!openedDefine.empty())
  270. ofs << "#endif\n";
  271. ofs <<
  272. "}\n"
  273. "\n"
  274. "}\n";
  275. }
  276. // ============================================================================
  277. vector<ProcessedGlobalVariable> globalVariables_;
  278. // Write result to GlobalVariables.cpp
  279. static void SaveGlobalVariables(const string& outputBasePath)
  280. {
  281. sort(globalVariables_.begin(), globalVariables_.end());
  282. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedGlobalVariables.cpp");
  283. ofs <<
  284. "// DO NOT EDIT. This file is generated\n"
  285. "\n"
  286. "#include \"../Precompiled.h\"\n"
  287. "#include \"../AngelScript/APITemplates.h\"\n"
  288. "\n"
  289. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  290. "\n"
  291. "// Some headers could re-define M_PI, ensure that it's undefined\n"
  292. "#undef M_PI\n"
  293. "\n"
  294. "namespace Urho3D\n"
  295. "{\n"
  296. "\n";
  297. ofs <<
  298. "void ASRegisterGeneratedGlobalVariables(asIScriptEngine* engine)\n"
  299. "{\n";
  300. bool isFirst = true;
  301. string openedDefine;
  302. string lastComment;
  303. for (const ProcessedGlobalVariable& globalVariable : globalVariables_)
  304. {
  305. if (globalVariable.insideDefine_ != openedDefine && !openedDefine.empty())
  306. {
  307. ofs << "#endif\n";
  308. openedDefine.clear();
  309. }
  310. if (!isFirst && lastComment != globalVariable.comment_)
  311. ofs << "\n";
  312. if (globalVariable.insideDefine_ != openedDefine && !globalVariable.insideDefine_.empty())
  313. {
  314. ofs << "#ifdef " << globalVariable.insideDefine_ << "\n";
  315. openedDefine = globalVariable.insideDefine_;
  316. }
  317. if (lastComment != globalVariable.comment_)
  318. ofs << " // " << globalVariable.comment_ << "\n";
  319. ofs << " " << globalVariable.registration_ << "\n";
  320. isFirst = false;
  321. lastComment = globalVariable.comment_;
  322. }
  323. if (!openedDefine.empty())
  324. ofs << "#endif\n";
  325. ofs <<
  326. "}\n"
  327. "\n"
  328. "}\n";
  329. }
  330. // ============================================================================
  331. vector<ProcessedClass> classes_;
  332. // Write result to ...
  333. static void SaveClasses(const string& outputBasePath)
  334. {
  335. sort(classes_.begin(), classes_.end());
  336. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedClasses.cpp");
  337. ofs <<
  338. "// DO NOT EDIT. This file is generated\n"
  339. "\n"
  340. "// We need register all types before registration of any functions because functions can use any types\n"
  341. "\n"
  342. "#include \"../Precompiled.h\"\n"
  343. "#include \"../AngelScript/APITemplates.h\"\n"
  344. "\n"
  345. "#include \"../AngelScript/GeneratedIncludes.h\"\n"
  346. "\n"
  347. "namespace Urho3D\n"
  348. "{\n"
  349. "\n";
  350. ofs <<
  351. "void ASRegisterGeneratedClasses(asIScriptEngine* engine)\n"
  352. "{\n";
  353. string openedDefine;
  354. bool isFirst = true;
  355. for (const ProcessedClass& processedClass : classes_)
  356. {
  357. if (processedClass.insideDefine_ != openedDefine && !openedDefine.empty())
  358. {
  359. ofs << "#endif\n";
  360. openedDefine.clear();
  361. }
  362. if (!isFirst)
  363. ofs << "\n";
  364. if (processedClass.insideDefine_ != openedDefine && !processedClass.insideDefine_.empty())
  365. {
  366. ofs << "#ifdef " << processedClass.insideDefine_ << "\n";
  367. openedDefine = processedClass.insideDefine_;
  368. }
  369. ofs
  370. << " // " << processedClass.comment_ << "\n"
  371. << " " << processedClass.objectTypeRegistration_ << "\n";
  372. isFirst = false;
  373. }
  374. if (!openedDefine.empty())
  375. ofs << "#endif\n";
  376. ofs <<
  377. "}\n"
  378. "\n"
  379. "}\n";
  380. }
  381. // ============================================================================
  382. // List of all required header files
  383. static vector<string> headers_;
  384. // Discarded header files for statistic
  385. static vector<string> ignoredHeaders_;
  386. // Add header to lists if not added yet
  387. void AddHeader(const string& headerFile)
  388. {
  389. if (IsIgnoredHeader(headerFile))
  390. {
  391. if (!CONTAINS(ignoredHeaders_, headerFile))
  392. ignoredHeaders_.push_back(headerFile);
  393. }
  394. else
  395. {
  396. if (!CONTAINS(headers_, headerFile))
  397. headers_.push_back(headerFile);
  398. }
  399. }
  400. // Write result to GeneratedIncludes.h
  401. static void SaveIncludes(const string& outputBasePath)
  402. {
  403. sort(headers_.begin(), headers_.end());
  404. sort(ignoredHeaders_.begin(), ignoredHeaders_.end());
  405. ofstream ofs(outputBasePath + "/Source/Urho3D/AngelScript/GeneratedIncludes.h");
  406. ofs <<
  407. "// DO NOT EDIT. This file is generated\n"
  408. "\n"
  409. "#pragma once\n"
  410. "\n";
  411. string openedDefine;
  412. bool isFirst = true;
  413. for (const string& header : headers_)
  414. {
  415. string insideDefine = InsideDefine(header);
  416. if (insideDefine != openedDefine)
  417. {
  418. if (!openedDefine.empty())
  419. {
  420. ofs << "#endif\n";
  421. openedDefine.clear();
  422. }
  423. if (!isFirst) // First include can be guarded. Avoid print \n before it
  424. ofs << "\n";
  425. if (!insideDefine.empty())
  426. {
  427. ofs << "#ifdef " << insideDefine << "\n";
  428. openedDefine = insideDefine;
  429. }
  430. }
  431. ofs << "#include \"" << header << "\"\n";
  432. isFirst = false;
  433. }
  434. if (!openedDefine.empty())
  435. {
  436. ofs << "#endif\n";
  437. openedDefine.clear();
  438. }
  439. if (headers_.size() > 0)
  440. ofs << "\n";
  441. if (ignoredHeaders_.size() > 0)
  442. ofs << "// Ignored headers\n\n";
  443. isFirst = true;
  444. for (const string& header : ignoredHeaders_)
  445. {
  446. string insideDefine = InsideDefine(header);
  447. if (insideDefine != openedDefine)
  448. {
  449. if (!openedDefine.empty())
  450. {
  451. ofs << "//#endif\n";
  452. openedDefine.clear();
  453. }
  454. if (!isFirst) // First include can be guarded. Avoid print \n before it
  455. ofs << "\n";
  456. if (!insideDefine.empty())
  457. {
  458. ofs << "//#ifdef " << insideDefine << "\n";
  459. openedDefine = insideDefine;
  460. }
  461. }
  462. ofs << "//#include \"" << header << "\"\n";
  463. isFirst = false;
  464. }
  465. if (!openedDefine.empty())
  466. ofs << "//#endif\n";
  467. }
  468. }
  469. void SaveResult(const string& outputBasePath)
  470. {
  471. Result::SaveEnums(outputBasePath);
  472. Result::SaveGlobalFunctions(outputBasePath);
  473. Result::SaveGlobalVariables(outputBasePath);
  474. Result::SaveClasses(outputBasePath);
  475. Result::SaveIncludes(outputBasePath);
  476. }
  477. }