ASClassBinderNew.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "ASUtils.h"
  24. #include "Tuning.h"
  25. #include "Utils.h"
  26. #include "XmlAnalyzer.h"
  27. #include "XmlSourceData.h"
  28. #include <cassert>
  29. #include <fstream>
  30. #include <iostream>
  31. #include <regex>
  32. #include <vector>
  33. namespace ASBindingGenerator
  34. {
  35. static void RegisterObjectType(const ClassAnalyzer& classAnalyzer, ProcessedClass& inoutProcessedClass)
  36. {
  37. string className = classAnalyzer.GetClassName();
  38. if (classAnalyzer.IsRefCounted() || Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  39. {
  40. inoutProcessedClass.objectTypeRegistration_ = "engine->RegisterObjectType(\"" + className + "\", 0, asOBJ_REF);";
  41. }
  42. else // Value type
  43. {
  44. string flags = "asOBJ_VALUE | asGetTypeTraits<" + className + ">()";
  45. if (classAnalyzer.IsPod())
  46. {
  47. flags += " | asOBJ_POD";
  48. if (classAnalyzer.AllFloats())
  49. flags += " | asOBJ_APP_CLASS_ALLFLOATS";
  50. else if (classAnalyzer.AllInts())
  51. flags += " | asOBJ_APP_CLASS_ALLINTS";
  52. }
  53. inoutProcessedClass.objectTypeRegistration_ = "engine->RegisterObjectType(\"" + className + "\", sizeof(" + className + "), " + flags + ");";
  54. }
  55. }
  56. static bool IsConstructorRequired(const ClassAnalyzer& classAnalyzer)
  57. {
  58. if (classAnalyzer.IsRefCounted())
  59. return false;
  60. if (Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  61. return false;
  62. if (classAnalyzer.IsPod())
  63. return false;
  64. return true;
  65. }
  66. static bool IsDestructorRequired(const ClassAnalyzer& classAnalyzer)
  67. {
  68. if (classAnalyzer.IsRefCounted())
  69. return false;
  70. if (Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  71. return false;
  72. if (classAnalyzer.IsPod())
  73. return false;
  74. return true;
  75. }
  76. // Iterate over overrided funcions
  77. static bool HaveMark(const MethodAnalyzer& methodAnalyzer, const string& mark)
  78. {
  79. if (Contains(methodAnalyzer.GetComment(), mark))
  80. return true;
  81. shared_ptr<MethodAnalyzer> reimplements = methodAnalyzer.Reimplements();
  82. if (!reimplements)
  83. return false;
  84. return HaveMark(*reimplements, mark);
  85. }
  86. static void RegisterConstructor(const MethodAnalyzer& methodAnalyzer, ProcessedClass& processedClass)
  87. {
  88. ClassAnalyzer classAnalyzer = methodAnalyzer.GetClass();
  89. if (classAnalyzer.IsAbstract())
  90. return;
  91. if (HaveMark(methodAnalyzer, "NO_BIND"))
  92. {
  93. RegistrationError regError;
  94. regError.comment_ = methodAnalyzer.GetDeclaration();
  95. regError.message_ = "Not registered because have @nobind mark";
  96. processedClass.unregisteredSpecialMethods_.push_back(regError);
  97. return;
  98. }
  99. if (HaveMark(methodAnalyzer, "MANUAL_BIND"))
  100. {
  101. RegistrationError regError;
  102. regError.comment_ = methodAnalyzer.GetDeclaration();
  103. regError.message_ = "Not registered because have @manualbind mark";
  104. processedClass.unregisteredSpecialMethods_.push_back(regError);
  105. return;
  106. }
  107. MemberRegistration result;
  108. result.name_ = methodAnalyzer.GetName();
  109. result.comment_ = methodAnalyzer.GetDeclaration();
  110. string asClassName = classAnalyzer.GetClassName();
  111. string cppClassName = classAnalyzer.GetClassName();
  112. vector<ParamAnalyzer> params = methodAnalyzer.GetParams();
  113. if (params.empty()) // Default constructor
  114. {
  115. if (classAnalyzer.IsRefCounted() || Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  116. {
  117. }
  118. else
  119. {
  120. result.comment_ = methodAnalyzer.GetLocation(); // Rewrite comment
  121. result.registration_ = "engine->RegisterObjectBehaviour(\"" + asClassName + "\", asBEHAVE_CONSTRUCT, \"void f()\", asFUNCTION(ASCompatibleConstructor<" + cppClassName + ">), AS_CALL_CDECL_OBJFIRST);";
  122. processedClass.defaultConstructor_ = make_shared<MemberRegistration>(result);
  123. return;
  124. }
  125. }
  126. }
  127. static void RegisterDestructor(const ClassAnalyzer& classAnalyzer, ProcessedClass& processedClass)
  128. {
  129. if (classAnalyzer.IsRefCounted())
  130. return;
  131. if (Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  132. return;
  133. string className = classAnalyzer.GetClassName();
  134. string wrapperName = className + "_Destructor";
  135. shared_ptr<MemberRegistration> result = make_shared<MemberRegistration>();
  136. result->name_ = "~" + className;
  137. result->registration_ = "engine->RegisterObjectBehaviour(\"" + className + "\", asBEHAVE_DESTRUCT, \"void f()\", AS_DESTRUCTOR(" + className + "), AS_CALL_CDECL_OBJFIRST);";
  138. shared_ptr<MethodAnalyzer> thisDestructor = classAnalyzer.GetDefinedThisDestructor();
  139. if (thisDestructor)
  140. {
  141. result->comment_ = thisDestructor->GetDeclaration();
  142. processedClass.destructor_ = result;
  143. }
  144. else if (!classAnalyzer.HasThisDestructor() && IsDestructorRequired(classAnalyzer))
  145. {
  146. result->comment_ = className + "::~" + className + "() | Implicitly-declared";
  147. processedClass.destructor_ = result;
  148. }
  149. }
  150. static void ProcessClass(const ClassAnalyzer& classAnalyzer)
  151. {
  152. if (classAnalyzer.IsInternal())
  153. return;
  154. // TODO: Remove
  155. if (classAnalyzer.IsTemplate())
  156. return;
  157. string header = classAnalyzer.GetHeaderFile();
  158. Result::AddHeader(header);
  159. if (IsIgnoredHeader(header))
  160. return;
  161. ProcessedClass processedClass;
  162. processedClass.name_ = classAnalyzer.GetClassName();
  163. processedClass.comment_ = classAnalyzer.GetLocation();
  164. processedClass.insideDefine_ = InsideDefine(header);
  165. vector<MethodAnalyzer> methods = classAnalyzer.GetThisPublicMethods();
  166. for (const MethodAnalyzer& method : methods)
  167. {
  168. if (method.IsThisConstructor())
  169. RegisterConstructor(method, processedClass);
  170. }
  171. // CollectMembers()
  172. if (classAnalyzer.IsAbstract() && !(classAnalyzer.IsRefCounted() || Contains(classAnalyzer.GetComment(), "FAKE_REF")))
  173. {
  174. processedClass.objectTypeRegistration_ = "// Not registered because value types can not be abstract";
  175. processedClass.noBind_ = true;
  176. Result::classes_.push_back(processedClass);
  177. return;
  178. }
  179. string classComment = classAnalyzer.GetComment();
  180. if (Contains(classComment, "NO_BIND"))
  181. {
  182. processedClass.objectTypeRegistration_ = "// Not registered because have @nobind mark";
  183. processedClass.noBind_ = true;
  184. Result::classes_.push_back(processedClass);
  185. return;
  186. }
  187. if (Contains(classComment, "MANUAL_BIND"))
  188. {
  189. processedClass.objectTypeRegistration_ = "// Not registered because have @manualbind mark";
  190. processedClass.noBind_ = true;
  191. Result::classes_.push_back(processedClass);
  192. return;
  193. }
  194. RegisterObjectType(classAnalyzer, processedClass);
  195. vector<ClassAnalyzer> baseClasses = classAnalyzer.GetBaseClasses();
  196. for (const ClassAnalyzer& baseClass : baseClasses)
  197. processedClass.baseClassNames_.push_back(baseClass.GetClassName());
  198. if (classAnalyzer.IsAbstract()) // Abstract refcounted type
  199. {
  200. Result::classes_.push_back(processedClass);
  201. return;
  202. }
  203. if (!classAnalyzer.HasThisConstructor() && IsConstructorRequired(classAnalyzer))
  204. {
  205. if (classAnalyzer.IsRefCounted() || Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  206. {
  207. }
  208. else
  209. {
  210. shared_ptr<MemberRegistration> result = make_shared<MemberRegistration>();
  211. string cppClassName = classAnalyzer.GetClassName();
  212. string asClassName = classAnalyzer.GetClassName();
  213. result->comment_ = cppClassName + "::" + cppClassName + "() | Implicitly-declared";
  214. result->registration_ = "engine->RegisterObjectBehaviour(\"" + asClassName + "\", asBEHAVE_CONSTRUCT, \"void f()\", asFUNCTION(ASCompatibleConstructor<" + cppClassName + ">), AS_CALL_CDECL_OBJFIRST);";
  215. processedClass.defaultConstructor_ = result;
  216. }
  217. }
  218. RegisterDestructor(classAnalyzer, processedClass);
  219. Result::classes_.push_back(processedClass);
  220. }
  221. void ProcessAllClassesNew()
  222. {
  223. #if 0
  224. // Old order of classes for tests
  225. vector<string> classIDs;
  226. classIDs.reserve(SourceData::classesByID_.size());
  227. for (pair<const string, xml_node>& it : SourceData::classesByID_)
  228. classIDs.push_back(it.first);
  229. sort(classIDs.begin(), classIDs.end());
  230. for (string classID : classIDs)
  231. {
  232. xml_node compounddef = SourceData::classesByID_[classID];
  233. ClassAnalyzer analyzer(compounddef);
  234. ProcessClass(analyzer);
  235. }
  236. #else
  237. for (auto element : SourceData::classesByID_)
  238. {
  239. xml_node compounddef = element.second;
  240. ClassAnalyzer analyzer(compounddef);
  241. ProcessClass(analyzer);
  242. }
  243. #endif
  244. }
  245. } // namespace ASBindingGenerator