]> git.tdb.fi Git - libs/gl.git/commitdiff
Resolve the return types of functions
authorMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 11:22:47 +0000 (13:22 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 23:00:31 +0000 (01:00 +0200)
source/glsl/generate.cpp
source/glsl/generate.h
source/glsl/syntax.cpp
source/glsl/syntax.h
source/glsl/validate.cpp
source/glsl/validate.h

index 8626cac6761c6f42f896b76ef47d53111df5c460..433d506e1a8ab762f50a0e930e314bf387c58be0 100644 (file)
@@ -162,6 +162,13 @@ void TypeResolver::visit(VariableDeclaration &var)
                var.type_declaration = i->second;
 }
 
+void TypeResolver::visit(FunctionDeclaration &func)
+{
+       map<string, TypeDeclaration *>::iterator i = stage->types.find(func.return_type);
+       func.return_type_declaration = (i!=stage->types.end() ? i->second : 0);
+       TraversingVisitor::visit(func);
+}
+
 
 VariableResolver::VariableResolver():
        stage(0),
index d303af3664b3294c74209b6dce738b999c8213ff..d4e570ab9908b239ceb291c4eafff8765afd39da 100644 (file)
@@ -71,6 +71,7 @@ private:
        virtual void visit(ImageTypeDeclaration &);
        virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);
+       virtual void visit(FunctionDeclaration &);
 };
 
 /** Resolves variable references.  Variable references which match the name
index e20d6c262eae2525a0a979922c800e9956f39f18..c8058c4fe9a228b102cbe4792daa83dc3f921f70 100644 (file)
@@ -347,7 +347,8 @@ void InterfaceBlock::visit(NodeVisitor &visitor)
 
 
 FunctionDeclaration::FunctionDeclaration():
-       definition(0)
+       definition(0),
+       return_type_declaration(0)
 { }
 
 FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
@@ -356,7 +357,8 @@ FunctionDeclaration::FunctionDeclaration(const FunctionDeclaration &other):
        name(other.name),
        parameters(other.parameters),
        body(other.body),
-       definition(other.definition==&other ? this : 0)
+       definition(other.definition==&other ? this : 0),
+       return_type_declaration(0)
 { }
 
 void FunctionDeclaration::visit(NodeVisitor &visitor)
index 62c289d9fbb6ec48f61be268b7eec67072718e98..0379a91146c17c3cbb02680717bea4036bcfb83b 100644 (file)
@@ -404,6 +404,7 @@ struct FunctionDeclaration: Statement
        Block body;
 
        FunctionDeclaration *definition;
+       TypeDeclaration *return_type_declaration;
 
        FunctionDeclaration();
        FunctionDeclaration(const FunctionDeclaration &);
index 8e5e996cb1f430ce1fe7c4103807c31a96f60a33..e6f0c653e45f4ad9e62c108084867dc4374c11c9 100644 (file)
@@ -201,6 +201,13 @@ void ReferenceValidator::visit(VariableDeclaration &var)
        TraversingVisitor::visit(var);
 }
 
+void ReferenceValidator::visit(FunctionDeclaration &func)
+{
+       if(!func.return_type_declaration)
+               error(func, format("Use of undeclared type '%s'", func.return_type));
+       TraversingVisitor::visit(func);
+}
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp
index e4b9edae932cb7dc1e2382a7a9f089da99c8e846..3efa51419d6e6c765f93839c3ed7b76218e0bfe0 100644 (file)
@@ -78,6 +78,7 @@ private:
        virtual void visit(VariableReference &);
        virtual void visit(InterfaceBlockReference &);
        virtual void visit(VariableDeclaration &);
+       virtual void visit(FunctionDeclaration &);
 };
 
 } // namespace SL