ASEnumBinder.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // Copyright (c) 2008-2022 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. using namespace std;
  30. namespace ASBindingGenerator
  31. {
  32. static void ProcessEnum(const EnumAnalyzer& analyzer)
  33. {
  34. if (analyzer.IsInternal())
  35. return;
  36. string header = analyzer.GetHeaderFile();
  37. Result::AddHeader(header);
  38. if (IsIgnoredHeader(header))
  39. return;
  40. ProcessedEnum processedEnum;
  41. processedEnum.name_ = analyzer.GetTypeName();
  42. processedEnum.insideDefine_ = InsideDefine(header);
  43. processedEnum.comment_ = analyzer.GetLocation();
  44. string comment = analyzer.GetComment();
  45. if (Contains(comment, "NO_BIND"))
  46. {
  47. processedEnum.registration_.push_back("// Not registered because have @nobind mark");
  48. Result::enums_.push_back(processedEnum);
  49. return;
  50. }
  51. if (Contains(comment, "MANUAL_BIND"))
  52. {
  53. processedEnum.registration_.push_back("// Not registered because have @manualbind mark");
  54. Result::enums_.push_back(processedEnum);
  55. return;
  56. }
  57. string enumTypeName = analyzer.GetTypeName();
  58. string cppEnumBaseType = analyzer.GetBaseType();
  59. if (cppEnumBaseType == "int") // Enums in AngelScript can be only int
  60. {
  61. processedEnum.registration_.push_back("engine->RegisterEnum(\"" + enumTypeName + "\");");
  62. for (const string& value : analyzer.GetEnumerators())
  63. processedEnum.registration_.push_back("engine->RegisterEnumValue(\"" + enumTypeName + "\", \"" + value + "\", " + value + ");");
  64. }
  65. else // If enum is not int then register as typedef. But this type can not be used in switch
  66. {
  67. string asEnumBaseType = CppPrimitiveTypeToAS(cppEnumBaseType);
  68. processedEnum.registration_.push_back("engine->RegisterTypedef(\"" + enumTypeName + "\", \"" + asEnumBaseType + "\");");
  69. for (const string& enumerator : analyzer.GetEnumerators())
  70. {
  71. string constName = enumTypeName + "_" + enumerator;
  72. processedEnum.glue_.push_back("static const " + cppEnumBaseType + " " + constName + " = " + enumerator + ";");
  73. processedEnum.registration_.push_back("engine->RegisterGlobalProperty(\"const " + asEnumBaseType + " " + enumerator + "\", (void*)&" + constName + ");");
  74. }
  75. }
  76. Result::enums_.push_back(processedEnum);
  77. }
  78. static void ProcessFlagset(const GlobalFunctionAnalyzer& analyzer)
  79. {
  80. string header = analyzer.GetHeaderFile();
  81. Result::AddHeader(header);
  82. if (IsIgnoredHeader(header))
  83. return;
  84. vector<ParamAnalyzer> params = analyzer.GetParams();
  85. assert(params.size() == 2);
  86. string enumTypeName = params[0].GetType().GetName();
  87. string flagsetName = params[1].GetType().GetName();
  88. shared_ptr<EnumAnalyzer> enumAnalyzer = FindEnum(enumTypeName);
  89. assert(enumAnalyzer);
  90. string cppEnumBaseType = enumAnalyzer->GetBaseType();
  91. string asEnumBaseType = CppPrimitiveTypeToAS(cppEnumBaseType);
  92. ProcessedEnum processedEnum;
  93. processedEnum.name_ = flagsetName;
  94. processedEnum.insideDefine_ = InsideDefine(header);
  95. processedEnum.comment_ = analyzer.GetLocation();
  96. processedEnum.registration_.push_back("engine->RegisterTypedef(\"" + flagsetName + "\", \"" + asEnumBaseType + "\");");
  97. Result::enums_.push_back(processedEnum);
  98. }
  99. void ProcessAllEnums()
  100. {
  101. NamespaceAnalyzer namespaceAnalyzer(SourceData::namespaceUrho3D_);
  102. vector<EnumAnalyzer> enumAnalyzers = namespaceAnalyzer.GetEnums();
  103. for (const EnumAnalyzer& enumAnalyzer : enumAnalyzers)
  104. ProcessEnum(enumAnalyzer);
  105. vector<GlobalFunctionAnalyzer> globalFunctionAnalyzers = namespaceAnalyzer.GetFunctions();
  106. for (const GlobalFunctionAnalyzer& globalFunctionAnalyzer : globalFunctionAnalyzers)
  107. {
  108. string functionName = globalFunctionAnalyzer.GetName();
  109. if (functionName == "URHO3D_FLAGSET")
  110. ProcessFlagset(globalFunctionAnalyzer);
  111. }
  112. }
  113. } // namespace ASBindingGenerator