2
0

ASClassBinderNew.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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()\", asFUNCTION(" + wrapperName + "), asCALL_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->glue_ =
  115. "static void " + wrapperName + "(" + className + "* ptr)\n"
  116. "{\n"
  117. " ptr->~" + className + "();\n"
  118. "}\n";
  119. result->registration_ = "engine->RegisterObjectBehaviour(\"" + className + "\", asBEHAVE_DESTRUCT, \"void f()\", asFUNCTION(" + wrapperName + "), asCALL_CDECL_OBJFIRST);";
  120. shared_ptr<ClassFunctionAnalyzer> thisDestructor = classAnalyzer.GetDefinedThisDestructor();
  121. if (thisDestructor)
  122. {
  123. result->comment_ = thisDestructor->GetDeclaration();
  124. processedClass.destructor_ = result;
  125. }
  126. else if (!classAnalyzer.HasThisDestructor() && IsDestructorRequired(classAnalyzer))
  127. {
  128. result->comment_ = className + "::~" + className + "() | Implicitly-declared";
  129. processedClass.destructor_ = result;
  130. }
  131. }
  132. static void RegisterNonDefaultConstructor(const ClassFunctionAnalyzer& classFunctionAnalyzer, ProcessedClass& processedClass)
  133. {
  134. processedClass.nonDefaultConstructors_.push_back(classFunctionAnalyzer.GetLocation());
  135. }
  136. static void ProcessClass(const ClassAnalyzer& classAnalyzer)
  137. {
  138. if (classAnalyzer.IsInternal())
  139. return;
  140. // TODO: Remove
  141. if (classAnalyzer.IsTemplate())
  142. return;
  143. // TODO: Remove
  144. if (classAnalyzer.IsAbstract() && !(classAnalyzer.IsRefCounted() || Contains(classAnalyzer.GetComment(), "FAKE_REF")))
  145. return;
  146. string header = classAnalyzer.GetHeaderFile();
  147. Result::AddHeader(header);
  148. if (IsIgnoredHeader(header))
  149. return;
  150. ProcessedClass processedClass;
  151. processedClass.name_ = classAnalyzer.GetClassName();
  152. processedClass.comment_ = classAnalyzer.GetLocation();
  153. processedClass.insideDefine_ = InsideDefine(header);
  154. string classComment = classAnalyzer.GetComment();
  155. if (Contains(classComment, "NO_BIND"))
  156. {
  157. processedClass.objectTypeRegistration_ = "// Not registered because have @nobind mark";
  158. Result::classes_.push_back(processedClass);
  159. return;
  160. }
  161. if (Contains(classComment, "MANUAL_BIND"))
  162. {
  163. processedClass.objectTypeRegistration_ = "// Not registered because have @manualbind mark";
  164. Result::classes_.push_back(processedClass);
  165. return;
  166. }
  167. RegisterObjectType(classAnalyzer, processedClass);
  168. RegisterDefaultConstructor(classAnalyzer, processedClass);
  169. vector<ClassFunctionAnalyzer> nonDefaultConstructors = classAnalyzer.GetThisNonDefaultConstructors();
  170. for (const ClassFunctionAnalyzer& classFunctionAnalyzer : nonDefaultConstructors)
  171. RegisterNonDefaultConstructor(classFunctionAnalyzer, processedClass);
  172. RegisterDestructor(classAnalyzer, processedClass);
  173. Result::classes_.push_back(processedClass);
  174. }
  175. void ProcessAllClassesNew()
  176. {
  177. #if 0
  178. // Old order of classes for tests
  179. vector<string> classIDs;
  180. classIDs.reserve(SourceData::classesByID_.size());
  181. for (pair<const string, xml_node>& it : SourceData::classesByID_)
  182. classIDs.push_back(it.first);
  183. sort(classIDs.begin(), classIDs.end());
  184. for (string classID : classIDs)
  185. {
  186. xml_node compounddef = SourceData::classesByID_[classID];
  187. ClassAnalyzer analyzer(compounddef);
  188. ProcessClass(analyzer);
  189. }
  190. #else
  191. for (auto element : SourceData::classesByID_)
  192. {
  193. xml_node compounddef = element.second;
  194. ClassAnalyzer analyzer(compounddef);
  195. ProcessClass(analyzer);
  196. }
  197. #endif
  198. }
  199. } // namespace ASBindingGenerator