ASClassBinderNew.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. static void RegisterDefaultConstructor(const ClassAnalyzer& classAnalyzer, ProcessedClass& processedClass)
  77. {
  78. if (classAnalyzer.IsRefCounted())
  79. return;
  80. if (Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  81. return;
  82. string className = classAnalyzer.GetClassName();
  83. string wrapperName = className + "_Constructor";
  84. shared_ptr<ClassMemberRegistration> result = make_shared<ClassMemberRegistration>();
  85. result->name_ = className;
  86. result->glue_ =
  87. "static void " + wrapperName + "(" + className + "* ptr)\n"
  88. "{\n"
  89. " new(ptr) " + className + "();\n"
  90. "}\n";
  91. result->registration_ = "engine->RegisterObjectBehaviour(\"" + className + "\", asBEHAVE_CONSTRUCT, \"void f()\", AS_FUNCTION_OBJFIRST(" + wrapperName + "), AS_CALL_CDECL_OBJFIRST);";
  92. shared_ptr<ClassFunctionAnalyzer> defaultConstructor = classAnalyzer.GetDefinedThisDefaultConstructor();
  93. if (defaultConstructor)
  94. {
  95. result->comment_ = defaultConstructor->GetLocation();
  96. processedClass.defaultConstructor_ = result;
  97. }
  98. else if (!classAnalyzer.HasThisConstructor() && IsConstructorRequired(classAnalyzer))
  99. {
  100. result->comment_ = className + "::" + className + "() | Implicitly-declared";
  101. processedClass.defaultConstructor_ = result;
  102. }
  103. }
  104. static void RegisterDestructor(const ClassAnalyzer& classAnalyzer, ProcessedClass& processedClass)
  105. {
  106. if (classAnalyzer.IsRefCounted())
  107. return;
  108. if (Contains(classAnalyzer.GetComment(), "FAKE_REF"))
  109. return;
  110. string className = classAnalyzer.GetClassName();
  111. string wrapperName = className + "_Destructor";
  112. shared_ptr<ClassMemberRegistration> result = make_shared<ClassMemberRegistration>();
  113. result->name_ = "~" + className;
  114. result->registration_ = "engine->RegisterObjectBehaviour(\"" + className + "\", asBEHAVE_DESTRUCT, \"void f()\", AS_DESTRUCTOR(" + className + "), AS_CALL_CDECL_OBJFIRST);";
  115. shared_ptr<ClassFunctionAnalyzer> thisDestructor = classAnalyzer.GetDefinedThisDestructor();
  116. if (thisDestructor)
  117. {
  118. result->comment_ = thisDestructor->GetDeclaration();
  119. processedClass.destructor_ = result;
  120. }
  121. else if (!classAnalyzer.HasThisDestructor() && IsDestructorRequired(classAnalyzer))
  122. {
  123. result->comment_ = className + "::~" + className + "() | Implicitly-declared";
  124. processedClass.destructor_ = result;
  125. }
  126. }
  127. static void RegisterNonDefaultConstructor(const ClassFunctionAnalyzer& classFunctionAnalyzer, ProcessedClass& processedClass)
  128. {
  129. processedClass.nonDefaultConstructors_.push_back(classFunctionAnalyzer.GetLocation());
  130. }
  131. static void ProcessClass(const ClassAnalyzer& classAnalyzer)
  132. {
  133. if (classAnalyzer.IsInternal())
  134. return;
  135. // TODO: Remove
  136. if (classAnalyzer.IsTemplate())
  137. return;
  138. string header = classAnalyzer.GetHeaderFile();
  139. Result::AddHeader(header);
  140. if (IsIgnoredHeader(header))
  141. return;
  142. ProcessedClass processedClass;
  143. processedClass.name_ = classAnalyzer.GetClassName();
  144. processedClass.comment_ = classAnalyzer.GetLocation();
  145. processedClass.insideDefine_ = InsideDefine(header);
  146. // CollectMembers()
  147. if (classAnalyzer.IsAbstract() && !(classAnalyzer.IsRefCounted() || Contains(classAnalyzer.GetComment(), "FAKE_REF")))
  148. {
  149. processedClass.objectTypeRegistration_ = "// Not registered because value types can not be abstract";
  150. processedClass.noBind_ = true;
  151. Result::classes_.push_back(processedClass);
  152. return;
  153. }
  154. string classComment = classAnalyzer.GetComment();
  155. if (Contains(classComment, "NO_BIND"))
  156. {
  157. processedClass.objectTypeRegistration_ = "// Not registered because have @nobind mark";
  158. processedClass.noBind_ = true;
  159. Result::classes_.push_back(processedClass);
  160. return;
  161. }
  162. if (Contains(classComment, "MANUAL_BIND"))
  163. {
  164. processedClass.objectTypeRegistration_ = "// Not registered because have @manualbind mark";
  165. processedClass.noBind_ = true;
  166. Result::classes_.push_back(processedClass);
  167. return;
  168. }
  169. RegisterObjectType(classAnalyzer, processedClass);
  170. vector<ClassAnalyzer> baseClasses = classAnalyzer.GetBaseClasses();
  171. for (const ClassAnalyzer& baseClass : baseClasses)
  172. processedClass.baseClassNames_.push_back(baseClass.GetClassName());
  173. if (classAnalyzer.IsAbstract()) // Abstract refcounted type
  174. {
  175. Result::classes_.push_back(processedClass);
  176. return;
  177. }
  178. // RegisterSpecialMethods()
  179. RegisterDefaultConstructor(classAnalyzer, processedClass);
  180. vector<ClassFunctionAnalyzer> nonDefaultConstructors = classAnalyzer.GetThisNonDefaultConstructors();
  181. for (const ClassFunctionAnalyzer& classFunctionAnalyzer : nonDefaultConstructors)
  182. RegisterNonDefaultConstructor(classFunctionAnalyzer, processedClass);
  183. RegisterDestructor(classAnalyzer, processedClass);
  184. Result::classes_.push_back(processedClass);
  185. }
  186. void ProcessAllClassesNew()
  187. {
  188. #if 0
  189. // Old order of classes for tests
  190. vector<string> classIDs;
  191. classIDs.reserve(SourceData::classesByID_.size());
  192. for (pair<const string, xml_node>& it : SourceData::classesByID_)
  193. classIDs.push_back(it.first);
  194. sort(classIDs.begin(), classIDs.end());
  195. for (string classID : classIDs)
  196. {
  197. xml_node compounddef = SourceData::classesByID_[classID];
  198. ClassAnalyzer analyzer(compounddef);
  199. ProcessClass(analyzer);
  200. }
  201. #else
  202. for (auto element : SourceData::classesByID_)
  203. {
  204. xml_node compounddef = element.second;
  205. ClassAnalyzer analyzer(compounddef);
  206. ProcessClass(analyzer);
  207. }
  208. #endif
  209. }
  210. } // namespace ASBindingGenerator