ASEnumBinder.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. namespace ASBindingGenerator
  30. {
  31. static shared_ptr<ASGeneratedFile_Enums> _result;
  32. static void ProcessEnum(EnumAnalyzer analyzer)
  33. {
  34. if (analyzer.IsInternal())
  35. return;
  36. string header = analyzer.GetHeaderFile();
  37. if (IsIgnoredHeader(header))
  38. {
  39. _result->AddIgnoredHeader(header);
  40. return;
  41. }
  42. string insideDefine = InsideDefine(header);
  43. string comment = analyzer.GetComment();
  44. string location = analyzer.GetLocation();
  45. if (Contains(comment, "NO_BIND"))
  46. {
  47. if (!insideDefine.empty())
  48. _result->reg_ << "#ifdef " << insideDefine << "\n";
  49. _result->reg_ <<
  50. " // " << location << "\n"
  51. " // Not registered because have @nobind mark\n";
  52. if (!insideDefine.empty())
  53. _result->reg_ << "#endif\n";
  54. _result->reg_ << "\n";
  55. return;
  56. }
  57. if (Contains(comment, "MANUAL_BIND"))
  58. {
  59. if (!insideDefine.empty())
  60. _result->reg_ << "#ifdef " << insideDefine << "\n";
  61. _result->reg_ <<
  62. " // " << location << "\n"
  63. " // Not registered because have @manualbind mark\n";
  64. if (!insideDefine.empty())
  65. _result->reg_ << "#endif\n";
  66. _result->reg_ << "\n";
  67. return;
  68. }
  69. _result->AddHeader(header);
  70. string enumTypeName = analyzer.GetTypeName();
  71. string cppEnumBaseType = analyzer.GetBaseType();
  72. if (cppEnumBaseType == "int") // Enums in AngelScript can be only int
  73. {
  74. if (!insideDefine.empty())
  75. _result->reg_ << "#ifdef " << insideDefine << "\n";
  76. _result->reg_ <<
  77. " // " << location << "\n"
  78. " engine->RegisterEnum(\"" << enumTypeName << "\");\n";
  79. for (string value : analyzer.GetEnumerators())
  80. _result->reg_ << " engine->RegisterEnumValue(\"" << enumTypeName << "\", \"" << value << "\", " << value << ");\n";
  81. if (!insideDefine.empty())
  82. _result->reg_ << "#endif\n";
  83. _result->reg_ << "\n";
  84. return;
  85. }
  86. // If enum is not int then register as typedef. But this type can not be used in switch
  87. if (!insideDefine.empty())
  88. _result->reg_ << "#ifdef " << insideDefine << "\n";
  89. string asEnumBaseType = CppFundamentalTypeToAS(cppEnumBaseType);
  90. _result->reg_ <<
  91. " // " << location << "\n"
  92. " engine->RegisterTypedef(\"" << enumTypeName << "\", \"" << asEnumBaseType << "\");\n";
  93. _result->glue_ << "// " << location << "\n";
  94. vector<string> enumerators = analyzer.GetEnumerators();
  95. for (string enumerator : enumerators)
  96. {
  97. string constName = enumTypeName + "_" + enumerator;
  98. _result->glue_ << "static const " << cppEnumBaseType << " " << constName << " = " << enumerator << ";\n";
  99. _result->reg_ << " engine->RegisterGlobalProperty(\"const " << asEnumBaseType << " " << enumerator << "\", (void*)&" << constName << ");\n";
  100. }
  101. if (!insideDefine.empty())
  102. _result->glue_ << "#endif\n";
  103. _result->glue_ << "\n";
  104. if (!insideDefine.empty())
  105. _result->reg_ << "#endif\n";
  106. _result->reg_ << "\n";
  107. }
  108. static void ProcessFlagset(GlobalFunctionAnalyzer analyzer)
  109. {
  110. string header = analyzer.GetHeaderFile();
  111. if (IsIgnoredHeader(header))
  112. {
  113. _result->AddIgnoredHeader(header);
  114. return;
  115. }
  116. _result->AddHeader(header);
  117. vector<ParamAnalyzer> params = analyzer.GetParams();
  118. assert(params.size() == 2);
  119. string enumTypeName = params[0].GetType().GetName();
  120. string flagsetName = params[1].GetType().GetName();
  121. shared_ptr<EnumAnalyzer> enumAnalyzer = FindEnum(enumTypeName);
  122. assert(enumAnalyzer);
  123. string cppEnumBaseType = enumAnalyzer->GetBaseType();
  124. string asEnumBaseType = CppFundamentalTypeToAS(cppEnumBaseType);
  125. string insideDefine = InsideDefine(header);
  126. string location = analyzer.GetLocation();
  127. if (!insideDefine.empty())
  128. _result->reg_ << "#ifdef " << insideDefine << "\n";
  129. _result->reg_ <<
  130. " // " << location << "\n"
  131. " engine->RegisterTypedef(\"" << flagsetName << "\", \"" << asEnumBaseType << "\");\n";
  132. if (!insideDefine.empty())
  133. _result->reg_ << "#endif\n";
  134. }
  135. void ProcessAllEnums(const string& outputBasePath)
  136. {
  137. string outputPath = outputBasePath + "/Source/Urho3D/AngelScript/Generated_Enums.cpp";
  138. _result = make_shared<ASGeneratedFile_Enums>(outputPath, "ASRegisterGenerated_Enums");
  139. NamespaceAnalyzer namespaceAnalyzer(SourceData::namespaceUrho3D_);
  140. vector<EnumAnalyzer> enumAnalyzers = namespaceAnalyzer.GetEnums();
  141. for (EnumAnalyzer enumAnalyzer : enumAnalyzers)
  142. ProcessEnum(enumAnalyzer);
  143. vector<GlobalFunctionAnalyzer> globalFunctionAnalyzers = namespaceAnalyzer.GetFunctions();
  144. for (GlobalFunctionAnalyzer globalFunctionAnalyzer : globalFunctionAnalyzers)
  145. {
  146. string functionName = globalFunctionAnalyzer.GetName();
  147. if (functionName == "URHO3D_FLAGSET")
  148. ProcessFlagset(globalFunctionAnalyzer);
  149. }
  150. _result->Save();
  151. }
  152. } // namespace ASBindingGenerator