ASEnumBinder.cpp 5.0 KB

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