]> git.tdb.fi Git - libs/gl.git/commitdiff
Validate return types of overloaded functions
authorMikko Rasa <tdb@tdb.fi>
Tue, 9 Mar 2021 14:00:19 +0000 (16:00 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 9 Mar 2021 20:26:19 +0000 (22:26 +0200)
source/glsl/validate.cpp
source/glsl/validate.h
tests/glsl/return_type_conflict.glsl [new file with mode: 0644]

index f8ecc5eeefd34aaad243accf7034d9cee8513ca0..91d1de991515dc74dbb359d0385a0429ef855803 100644 (file)
@@ -173,6 +173,17 @@ void DeclarationValidator::visit(InterfaceBlock &iface)
 
 void DeclarationValidator::visit(FunctionDeclaration &func)
 {
+       string key = func.name+func.signature;
+       map<string, FunctionDeclaration *>::const_iterator i = overloaded_functions.find(key);
+       if(i==overloaded_functions.end())
+               overloaded_functions.insert(make_pair(key, &func));
+       else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
+       {
+               error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
+               if(i->second->return_type_declaration)
+                       add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
+       }
+
        if(Statement *previous = find_definition(func.name))
        {
                if(!dynamic_cast<FunctionDeclaration *>(previous))
index 9477b67d70cb7a7ecc9acc38ad81e76d81af1c97..a8c6f11c6a8e12602431abb6fd22a93b280e55fd 100644 (file)
@@ -48,6 +48,7 @@ private:
 
        std::map<Block *, BlockDeclarationMap> declarations;
        std::map<std::string, InterfaceBlock *> interface_blocks;
+       std::map<std::string, FunctionDeclaration *> overloaded_functions;
        bool anonymous_block;
 
 public:
diff --git a/tests/glsl/return_type_conflict.glsl b/tests/glsl/return_type_conflict.glsl
new file mode 100644 (file)
index 0000000..2de20c3
--- /dev/null
@@ -0,0 +1,17 @@
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+void func();
+vec4 func();
+float func();
+float func(float v);
+void main()
+{
+       gl_Position = position;
+}
+
+/* Expected error:
+<test>:4: Conflicting return type 'vec4' for function 'func'
+<test>:3: Previously declared as returning 'void'
+<test>:5: Conflicting return type 'float' for function 'func'
+<test>:3: Previously declared as returning 'void'
+*/