From 001cdc10d0e0b7e006a4dce5363a5c593e35b7ae Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 20 Apr 2021 15:16:30 +0300 Subject: [PATCH] Resolve function calls where argument types need to be converted This is a rather basic implementation and will reject a call if there's multiple candidates, without considering conversion ranks. --- source/glsl/resolve.cpp | 38 +++++++++++++++++++ source/glsl/resolve.h | 2 + .../glsl/inexact_function_argument_type.glsl | 18 +++++++++ 3 files changed, 58 insertions(+) create mode 100644 tests/glsl/inexact_function_argument_type.glsl diff --git a/source/glsl/resolve.cpp b/source/glsl/resolve.cpp index 176b13c5..3f959a7d 100644 --- a/source/glsl/resolve.cpp +++ b/source/glsl/resolve.cpp @@ -1102,6 +1102,29 @@ bool FunctionResolver::apply(Stage &s) return r_any_resolved; } +bool FunctionResolver::can_convert_arguments(const FunctionCall &call, const FunctionDeclaration &decl) +{ + if(decl.parameters.size()!=call.arguments.size()) + return false; + + for(unsigned j=0; jtype; + const TypeDeclaration *param_type = decl.parameters[j]->type_declaration; + if(arg_type==param_type) + continue; + + const BasicTypeDeclaration *arg_basic = dynamic_cast(arg_type); + const BasicTypeDeclaration *param_basic = dynamic_cast(param_type); + if(arg_basic && param_basic && can_convert(*arg_basic, *param_basic)) + continue; + + return false; + } + + return true; +} + void FunctionResolver::visit(FunctionCall &call) { FunctionDeclaration *declaration = 0; @@ -1123,6 +1146,21 @@ void FunctionResolver::visit(FunctionCall &call) { map::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types)); declaration = (i!=stage->functions.end() ? i->second : 0); + + if(!declaration) + { + for(i=stage->functions.lower_bound(call.name+"("); (i!=stage->functions.end() && i->second->name==call.name); ++i) + if(can_convert_arguments(call, *i->second)) + { + if(declaration) + { + declaration = 0; + break; + } + else + declaration = i->second; + } + } } } diff --git a/source/glsl/resolve.h b/source/glsl/resolve.h index 2416502c..69c877fc 100644 --- a/source/glsl/resolve.h +++ b/source/glsl/resolve.h @@ -156,6 +156,8 @@ public: bool apply(Stage &); private: + static bool can_convert_arguments(const FunctionCall &, const FunctionDeclaration &); + virtual void visit(FunctionCall &); virtual void visit(FunctionDeclaration &); }; diff --git a/tests/glsl/inexact_function_argument_type.glsl b/tests/glsl/inexact_function_argument_type.glsl new file mode 100644 index 00000000..2bbece37 --- /dev/null +++ b/tests/glsl/inexact_function_argument_type.glsl @@ -0,0 +1,18 @@ +#pragma MSP stage(vertex) +layout(location=0) in vec4 position; +vec2 scale(vec2 v, float s) +{ + return v*s; +} +void main() +{ + gl_Position = vec4(scale(position.xy, 2), 0.0, 1.0); +} + +/* Expected output: vertex +layout(location=0) in vec4 position; +void main() +{ + gl_Position = vec4(position.xy*float(2), 0.0, 1.0); +} +*/ -- 2.43.0