Browse Source

AS Autobindings: Join converted varibles by separate functions

1vanK 4 years ago
parent
commit
0e488a902d

+ 4 - 30
Source/Tools/BindingGenerator/ASClassBinder.cpp

@@ -235,26 +235,13 @@ static void RegisterStaticFunction(const ClassStaticFunctionAnalyzer& functionAn
     if (convertedReturn.NeedWrapper())
         needWrapper = true;
 
-    string declParams;
-
-    for (const ConvertedVariable& conv : convertedParams)
-    {
-        if (!conv.asDeclaration_.empty())
-        {
-            if (declParams.length() > 0)
-                declParams += ", ";
-
-            declParams += conv.asDeclaration_;
-        }
-    }
-
     string asFunctionName = functionAnalyzer.GetName();
     string className = functionAnalyzer.GetClassName();
 
     if (needWrapper)
         result->glue_ << GenerateWrapper(functionAnalyzer, convertedParams, convertedReturn);
 
-    string decl = convertedReturn.asDeclaration_ + " " + asFunctionName + "(" + declParams + ")";
+    string decl = convertedReturn.asDeclaration_ + " " + asFunctionName + "(" + JoinASDeclarations(convertedParams) + ")";
 
     result->reg_ << "    engine->SetDefaultNamespace(\"" << className << "\");\n";
 
@@ -286,7 +273,7 @@ static void RegisterRefCountedConstructor(const ClassFunctionAnalyzer& functionA
         args = CutStart(args, "Context *context");
         args = CutStart(args, ", ");
     }
-    else if (args.find("Context") != string::npos)
+    else if (Contains(args, "Context"))
     {
         result->reg_ <<
             "    // " << functionAnalyzer.GetLocation() << "\n"
@@ -821,19 +808,6 @@ static void RegisterMethod(const ClassFunctionAnalyzer& functionAnalyzer, bool t
     if (retConv.NeedWrapper())
         needWrapper = true;
 
-    string declParams = "";
-
-    for (const ConvertedVariable& conv : convertedParams)
-    {
-        if (!conv.asDeclaration_.empty())
-        {
-            if (declParams.length() > 0)
-                declParams += ", ";
-
-            declParams += conv.asDeclaration_; // TODO функцию для джойна сделать внутри конвертора
-        }
-    }
-
     string asReturnType = retConv.asDeclaration_;
 
     string asFunctionName = functionAnalyzer.GetName();
@@ -853,7 +827,7 @@ static void RegisterMethod(const ClassFunctionAnalyzer& functionAnalyzer, bool t
     if (needWrapper)
         result->glue_ << GenerateWrapper(functionAnalyzer, templateVersion, convertedParams, retConv);
 
-    string decl = asReturnType + " " + asFunctionName + "(" + declParams + ")";
+    string decl = asReturnType + " " + asFunctionName + "(" + JoinASDeclarations(convertedParams) + ")";
 
     if (functionAnalyzer.IsConst())
         decl += " const";
@@ -889,7 +863,7 @@ static void RegisterMethod(const ClassFunctionAnalyzer& functionAnalyzer, bool t
             }
         }
         
-        decl = asReturnType + " " + asFunctionName + "(" + declParams + ")";
+        decl = asReturnType + " " + asFunctionName + "(" + JoinASDeclarations(convertedParams) + ")";
 
         if (functionAnalyzer.IsConst())
             decl += " const";

+ 2 - 11
Source/Tools/BindingGenerator/ASGlobalFunctionBinder.cpp

@@ -104,7 +104,6 @@ static vector<map<string, string>> GetSpecializations(const GlobalFunctionAnalyz
 
 static void BindGlobalFunction(const GlobalFunctionAnalyzer& functionAnalyzer)
 {
-    string declParams = "";
     vector<ParamAnalyzer> params = functionAnalyzer.GetParams();
     string outGlue;
 
@@ -132,14 +131,6 @@ static void BindGlobalFunction(const GlobalFunctionAnalyzer& functionAnalyzer)
             return;
         }
 
-        if (!conv.asDeclaration_.empty())
-        {
-            if (declParams.length() > 0)
-                declParams += ", ";
-
-            declParams += conv.asDeclaration_;
-        }
-
         if (conv.NeedWrapper())
             needWrapper = true;
 
@@ -169,7 +160,7 @@ static void BindGlobalFunction(const GlobalFunctionAnalyzer& functionAnalyzer)
 
     string asFunctionName = functionAnalyzer.GetName();
 
-    string decl = asReturnType + " " + asFunctionName + "(" + declParams + ")";
+    string decl = asReturnType + " " + asFunctionName + "(" + JoinASDeclarations(convertedParams) + ")";
 
     processedGlobalFunction.registration_ = "engine->RegisterGlobalFunction(\"" + decl + "\", ";
 
@@ -187,7 +178,7 @@ static void BindGlobalFunction(const GlobalFunctionAnalyzer& functionAnalyzer)
     {
         asFunctionName = CutStart(aliasMark, "BIND_AS_ALIAS_");
 
-        decl = asReturnType + " " + asFunctionName + "(" + declParams + ")";
+        decl = asReturnType + " " + asFunctionName + "(" + JoinASDeclarations(convertedParams) + ")";
 
         processedGlobalFunction.registration_ = "engine->RegisterGlobalFunction(\"" + decl + "\", ";
 

+ 50 - 98
Source/Tools/BindingGenerator/ASUtils.cpp

@@ -86,6 +86,47 @@ string CppPrimitiveTypeToAS(const string& cppType)
     throw Exception(cppType + " not a primitive type");
 }
 
+string JoinASDeclarations(const vector<ConvertedVariable>& vars)
+{
+    string result;
+
+    for (const ConvertedVariable& var : vars)
+    {
+        if (!var.asDeclaration_.empty())
+        {
+            if (!result.empty())
+                result += ", ";
+
+            result += var.asDeclaration_;
+        }
+    }
+
+    return result;
+}
+
+string JoinCppDeclarations(const string& firstCppDeclaration, const vector<ConvertedVariable>& vars)
+{
+    string result = firstCppDeclaration;
+
+    for (const ConvertedVariable& var : vars)
+    {
+        if (!var.cppDeclaration_.empty())
+        {
+            if (!result.empty())
+                result += ", ";
+
+            result += var.cppDeclaration_;
+        }
+    }
+
+    return result;
+}
+
+string JoinCppDeclarations(const vector<ConvertedVariable>& vars)
+{
+    return JoinCppDeclarations("", vars);
+}
+
 shared_ptr<EnumAnalyzer> FindEnum(const string& name)
 {
     NamespaceAnalyzer namespaceAnalyzer(SourceData::namespaceUrho3D_);
@@ -435,7 +476,7 @@ ConvertedVariable CppVariableToAS(const TypeAnalyzer& type, VariableUsage usage,
         asTypeName = cppTypeName;
     }
 
-    if (asTypeName.find('<') != string::npos)
+    if (Contains(asTypeName, '<'))
         throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
 
     if (Contains(type.ToString(), "::"))
@@ -534,7 +575,7 @@ string CppTypeToAS(const TypeAnalyzer& type, TypeUsage typeUsage)
     if (asTypeName == "void" && type.IsPointer())
         throw Exception("Error: type \"void*\" can not automatically bind");
 
-    if (asTypeName.find('<') != string::npos)
+    if (Contains(asTypeName, '<'))
         throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
 
     if (Contains(type.ToString(), "::"))
@@ -637,27 +678,8 @@ string GenerateWrapper(const GlobalFunctionAnalyzer& functionAnalyzer, const vec
 
     string glueReturnType = convertedReturn.cppDeclaration_;
 
-    vector<ParamAnalyzer> params = functionAnalyzer.GetParams();
-    
-    result = "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer) + "(";
-
-    string cppDecl;
-
-    for (size_t i = 0; i < convertedParams.size(); i++)
-    {
-        if (!convertedParams[i].cppDeclaration_.empty())
-        {
-            if (!cppDecl.empty())
-                cppDecl += ", ";
-
-            cppDecl += convertedParams[i].cppDeclaration_;
-        }
-    }
-
-    result += cppDecl;
-
-    result +=
-        ")\n"
+    result =
+        "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer) + "(" + JoinCppDeclarations(convertedParams) + ")\n"
         "{\n";
 
     for (size_t i = 0; i < convertedParams.size(); i++)
@@ -668,17 +690,7 @@ string GenerateWrapper(const GlobalFunctionAnalyzer& functionAnalyzer, const vec
     else
         result += "    ";
 
-    result += functionAnalyzer.GetName() + "(";
-
-    for (size_t i = 0; i < convertedParams.size(); i++)
-    {
-        if (i != 0)
-            result += ", ";
-
-        result += params[i].GetDeclname();
-    }
-
-    result += ");\n";
+    result += functionAnalyzer.GetName() + "(" + functionAnalyzer.JoinParamsNames() + ");\n";
 
     if (!convertedReturn.glue_.empty())
         result += "    " + convertedReturn.glue_;
@@ -703,27 +715,7 @@ string GenerateWrapper(const ClassStaticFunctionAnalyzer& functionAnalyzer, cons
 
     result +=
         "// " + functionAnalyzer.GetLocation() + "\n"
-        "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer) + "(";
-
-    vector<ParamAnalyzer> params = functionAnalyzer.GetParams();
-
-    string cppDecl;
-
-    for (size_t i = 0; i < convertedParams.size(); i++)
-    {
-        if (!convertedParams[i].cppDeclaration_.empty())
-        {
-            if (!cppDecl.empty())
-                cppDecl += ", ";
-
-            cppDecl += convertedParams[i].cppDeclaration_;
-        }
-    }
-
-    result += cppDecl;
-
-    result +=
-        ")\n"
+        "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer) + "(" + JoinCppDeclarations(convertedParams) + ")\n"
         "{\n";
 
     for (size_t i = 0; i < convertedParams.size(); i++)
@@ -734,17 +726,7 @@ string GenerateWrapper(const ClassStaticFunctionAnalyzer& functionAnalyzer, cons
     else
         result += "    ";
 
-    result += functionAnalyzer.GetClassName() + "::" + functionAnalyzer.GetName() + "(";
-
-    for (size_t i = 0; i < convertedParams.size(); i++)
-    {
-        if (i != 0)
-            result += ", ";
-
-        result += params[i].GetDeclname();
-    }
-
-    result += ");\n";
+    result += functionAnalyzer.GetClassName() + "::" + functionAnalyzer.GetName() + "(" + functionAnalyzer.JoinParamsNames() + ");\n";
 
     if (!convertedReturn.glue_.empty())
         result += "    " + convertedReturn.glue_;
@@ -774,27 +756,7 @@ string GenerateWrapper(const ClassFunctionAnalyzer& functionAnalyzer, bool templ
 
     result +=
         "// " + functionAnalyzer.GetLocation() + "\n"
-        "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer, templateVersion) + "(";
-
-    vector<ParamAnalyzer> params = functionAnalyzer.GetParams();
-
-    string cppDecl = functionAnalyzer.GetClassName() + string("* ptr");
-
-    for (size_t i = 0; i < convertedParams.size(); i++)
-    {
-        if (!convertedParams[i].cppDeclaration_.empty())
-        {
-            if (!cppDecl.empty())
-                cppDecl += ", ";
-
-            cppDecl += convertedParams[i].cppDeclaration_;
-        }
-    }
-
-    result += cppDecl;
-
-    result +=
-        ")\n"
+        "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer, templateVersion) + "(" + JoinCppDeclarations(functionAnalyzer.GetClassName() + "* ptr", convertedParams) + ")\n"
         "{\n";
 
     for (size_t i = 0; i < convertedParams.size(); i++)
@@ -805,17 +767,7 @@ string GenerateWrapper(const ClassFunctionAnalyzer& functionAnalyzer, bool templ
     else
         result += "    ";
 
-    result += "ptr->" + functionAnalyzer.GetName() + "(";
-
-    for (size_t i = 0; i < convertedParams.size(); i++)
-    {
-        if (i != 0)
-            result += ", ";
-
-        result += params[i].GetDeclname();
-    }
-
-    result += ");\n";
+    result += "ptr->" + functionAnalyzer.GetName() + "(" + functionAnalyzer.JoinParamsNames() + ");\n";
 
     if (!convertedReturn.glue_.empty())
         result += "    " + convertedReturn.glue_;

+ 3 - 1
Source/Tools/BindingGenerator/ASUtils.h

@@ -50,6 +50,8 @@ struct ConvertedVariable
     bool NeedWrapper() const { return !glue_.empty(); }
 };
 
+string JoinASDeclarations(const vector<ConvertedVariable>& vars);
+
 enum class VariableUsage
 {
     FunctionParameter = 0,
@@ -82,7 +84,7 @@ shared_ptr<ClassAnalyzer> FindClassByID(const string& name);
 
 string GenerateWrapperName(const GlobalFunctionAnalyzer& functionAnalyzer);
 string GenerateWrapperName(const ClassStaticFunctionAnalyzer& functionAnalyzer);
-string GenerateWrapperName(const ClassFunctionAnalyzer& functionAnalyzer, bool templateVersion);
+string GenerateWrapperName(const ClassFunctionAnalyzer& functionAnalyzer, bool templateVersion = false);
 
 string GenerateWrapper(const GlobalFunctionAnalyzer& functionAnalyzer, const vector<ConvertedVariable>& convertedParams, const ConvertedVariable& convertedReturn);
 string GenerateWrapper(const ClassStaticFunctionAnalyzer& functionAnalyzer, const vector<ConvertedVariable>& convertedParams, const ConvertedVariable& convertedReturn);

+ 5 - 0
Source/Tools/BindingGenerator/Utils.cpp

@@ -195,6 +195,11 @@ bool Contains(const string& str, const string& substr)
     return str.find(substr) != string::npos;
 }
 
+bool Contains(const string& str, char c)
+{
+    return str.find(c) != string::npos;
+}
+
 string FirstCharToLower(const string& str)
 {
     if (str.empty())

+ 2 - 0
Source/Tools/BindingGenerator/Utils.h

@@ -44,7 +44,9 @@ string RemoveFirst(const string& src, const string& value);
 vector<string> Split(const string& str, char delim);
 vector<string> Split(const string& str, const string& delim);
 string Join(const vector<string>& values, const string& separator);
+
 bool Contains(const string& str, const string& substr);
+bool Contains(const string& str, char c);
 
 // Return all after last found substring
 string CutToLast(const string& src, const string& value, bool inclusive);

+ 1 - 1
Source/Tools/BindingGenerator/XmlAnalyzer.h

@@ -320,7 +320,7 @@ public:
     vector<string> GetTemplateParams() const { return ExtractTemplateParams(memberdef_); }
     bool IsDefine() const { return CONTAINS(SourceData::defines_, GetName()); }
     bool IsTemplate() const { return ::IsTemplate(memberdef_); }
-    string JoinParamsNames(bool skipContext = false) const { return ::JoinParamsNames(memberdef_, skipContext); }
+    string JoinParamsNames(bool skipContext = false) const { return ::JoinParamsNames(memberdef_, skipContext); } // TODO убрать skipContext, он больше не нужен
     string JoinParamsTypes() const { return ::JoinParamsTypes(memberdef_, specialization_); }
 
     virtual string GetLocation() const { return GetFunctionLocation(memberdef_); }