ASClassBinderNew.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. result.registration_ = "engine->RegisterObjectBehaviour(\"" + asClassName + "\", asBEHAVE_FACTORY, \"" + asClassName + "@+ f()\", asFUNCTION(ASCompatibleFactory<" + cppClassName + ">), AS_CALL_CDECL);";
  117. else
  118. result.registration_ = "engine->RegisterObjectBehaviour(\"" + asClassName + "\", asBEHAVE_CONSTRUCT, \"void f()\", asFUNCTION(ASCompatibleConstructor<" + cppClassName + ">), AS_CALL_CDECL_OBJFIRST);";
  119. result.comment_ = methodAnalyzer.GetLocation(); // Rewrite comment
  120. processedClass.defaultConstructor_ = make_shared<MemberRegistration>(result);
  121. return;
  122. }
  123. }
  124. static void RegisterDestructor(const ClassAnalyzer& classAnalyzer, ProcessedClass& processedClass)
  125. {
  126. if (classAnalyzer.IsRefCounted())
  127. return;
  128. if (Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  129. return;
  130. string className = classAnalyzer.GetClassName();
  131. string wrapperName = className + "_Destructor";
  132. shared_ptr<MemberRegistration> result = make_shared<MemberRegistration>();
  133. result->name_ = "~" + className;
  134. result->registration_ = "engine->RegisterObjectBehaviour(\"" + className + "\", asBEHAVE_DESTRUCT, \"void f()\", AS_DESTRUCTOR(" + className + "), AS_CALL_CDECL_OBJFIRST);";
  135. shared_ptr<MethodAnalyzer> thisDestructor = classAnalyzer.GetDefinedThisDestructor();
  136. if (thisDestructor)
  137. {
  138. result->comment_ = thisDestructor->GetDeclaration();
  139. processedClass.destructor_ = result;
  140. }
  141. else if (!classAnalyzer.HasThisDestructor() && IsDestructorRequired(classAnalyzer))
  142. {
  143. result->comment_ = className + "::~" + className + "() | Implicitly-declared";
  144. processedClass.destructor_ = result;
  145. }
  146. }
  147. static void ProcessClass(const ClassAnalyzer& classAnalyzer)
  148. {
  149. if (classAnalyzer.IsInternal())
  150. return;
  151. // TODO: Remove
  152. if (classAnalyzer.IsTemplate())
  153. return;
  154. string header = classAnalyzer.GetHeaderFile();
  155. Result::AddHeader(header);
  156. if (IsIgnoredHeader(header))
  157. return;
  158. ProcessedClass processedClass;
  159. processedClass.name_ = classAnalyzer.GetClassName();
  160. processedClass.comment_ = classAnalyzer.GetLocation();
  161. processedClass.insideDefine_ = InsideDefine(header);
  162. vector<MethodAnalyzer> methods = classAnalyzer.GetThisPublicMethods();
  163. for (const MethodAnalyzer& method : methods)
  164. {
  165. if (method.IsThisConstructor())
  166. RegisterConstructor(method, processedClass);
  167. }
  168. // CollectMembers()
  169. if (classAnalyzer.IsAbstract() && !(classAnalyzer.IsRefCounted() || Contains(classAnalyzer.GetComment(), "FAKE_REF")))
  170. {
  171. processedClass.objectTypeRegistration_ = "// Not registered because value types can not be abstract";
  172. processedClass.noBind_ = true;
  173. Result::classes_.push_back(processedClass);
  174. return;
  175. }
  176. string classComment = classAnalyzer.GetComment();
  177. if (Contains(classComment, "NO_BIND"))
  178. {
  179. processedClass.objectTypeRegistration_ = "// Not registered because have @nobind mark";
  180. processedClass.noBind_ = true;
  181. Result::classes_.push_back(processedClass);
  182. return;
  183. }
  184. if (Contains(classComment, "MANUAL_BIND"))
  185. {
  186. processedClass.objectTypeRegistration_ = "// Not registered because have @manualbind mark";
  187. processedClass.noBind_ = true;
  188. Result::classes_.push_back(processedClass);
  189. return;
  190. }
  191. RegisterObjectType(classAnalyzer, processedClass);
  192. vector<ClassAnalyzer> baseClasses = classAnalyzer.GetBaseClasses();
  193. for (const ClassAnalyzer& baseClass : baseClasses)
  194. processedClass.baseClassNames_.push_back(baseClass.GetClassName());
  195. if (classAnalyzer.IsAbstract()) // Abstract refcounted type
  196. {
  197. Result::classes_.push_back(processedClass);
  198. return;
  199. }
  200. if (!classAnalyzer.HasThisConstructor() && IsConstructorRequired(classAnalyzer))
  201. {
  202. shared_ptr<MemberRegistration> result = make_shared<MemberRegistration>();
  203. string cppClassName = classAnalyzer.GetClassName();
  204. string asClassName = classAnalyzer.GetClassName();
  205. if (classAnalyzer.IsRefCounted() || Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  206. result->registration_ = "engine->RegisterObjectBehaviour(\"" + asClassName + "\", asBEHAVE_FACTORY, \"" + asClassName + "@+ f()\", asFUNCTION(ASCompatibleFactory<" + cppClassName + ">), AS_CALL_CDECL);";
  207. else
  208. result->registration_ = "engine->RegisterObjectBehaviour(\"" + asClassName + "\", asBEHAVE_CONSTRUCT, \"void f()\", asFUNCTION(ASCompatibleConstructor<" + cppClassName + ">), AS_CALL_CDECL_OBJFIRST);";
  209. result->comment_ = cppClassName + "::" + cppClassName + "() | Implicitly-declared";
  210. processedClass.defaultConstructor_ = result;
  211. }
  212. RegisterDestructor(classAnalyzer, processedClass);
  213. Result::classes_.push_back(processedClass);
  214. }
  215. void ProcessAllClassesNew()
  216. {
  217. #if 0
  218. // Old order of classes for tests
  219. vector<string> classIDs;
  220. classIDs.reserve(SourceData::classesByID_.size());
  221. for (pair<const string, xml_node>& it : SourceData::classesByID_)
  222. classIDs.push_back(it.first);
  223. sort(classIDs.begin(), classIDs.end());
  224. for (string classID : classIDs)
  225. {
  226. xml_node compounddef = SourceData::classesByID_[classID];
  227. ClassAnalyzer analyzer(compounddef);
  228. ProcessClass(analyzer);
  229. }
  230. #else
  231. for (auto element : SourceData::classesByID_)
  232. {
  233. xml_node compounddef = element.second;
  234. ClassAnalyzer analyzer(compounddef);
  235. ProcessClass(analyzer);
  236. }
  237. #endif
  238. }
  239. } // namespace ASBindingGenerator