]> git.tdb.fi Git - libs/gl.git/commitdiff
Validate some aspects of type declarations
authorMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 11:09:55 +0000 (13:09 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 23:00:31 +0000 (01:00 +0200)
source/glsl/validate.cpp
source/glsl/validate.h
tests/glsl/bad_types.glsl [new file with mode: 0644]
tests/glsl/struct_member_initializer.glsl [new file with mode: 0644]
tests/glsl/undeclared_types.glsl [new file with mode: 0644]

index 6f0869ea141897813c4e4f3190a81082457ceed1..8e5e996cb1f430ce1fe7c4103807c31a96f60a33 100644 (file)
@@ -23,6 +23,57 @@ void Validator::diagnose(Node &node, Diagnostic::Severity severity, const string
 }
 
 
+TypeValidator::TypeValidator():
+       in_struct(false)
+{ }
+
+void TypeValidator::visit(BasicTypeDeclaration &type)
+{
+       if(type.kind==BasicTypeDeclaration::VECTOR)
+       {
+               BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
+               if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
+                       base_kind = basic_base->kind;
+               if(base_kind!=BasicTypeDeclaration::BOOL && base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
+                       error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
+       }
+       else if(type.kind==BasicTypeDeclaration::ARRAY)
+       {
+               if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
+                       if(basic_base->kind==BasicTypeDeclaration::VOID)
+                               error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
+       }
+}
+
+void TypeValidator::visit(ImageTypeDeclaration &type)
+{
+       BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
+       if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
+               base_kind = basic_base->kind;
+       if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
+               error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
+}
+
+void TypeValidator::visit(StructDeclaration &strct)
+{
+       SetFlag set_struct(in_struct);
+       TraversingVisitor::visit(strct);
+}
+
+void TypeValidator::visit(VariableDeclaration &var)
+{
+       if(in_struct)
+       {
+               if(var.layout)
+                       error(var, "Struct members can't have layouts");
+               if(var.init_expression)
+                       error(var, "Struct members can't have initializers");
+       }
+
+       TraversingVisitor::visit(var);
+}
+
+
 DeclarationValidator::DeclarationValidator():
        anonymous_block(false)
 { }
@@ -113,6 +164,18 @@ void DeclarationValidator::visit(FunctionDeclaration &func)
 }
 
 
+void ReferenceValidator::visit(BasicTypeDeclaration &type)
+{
+       if(!type.base.empty() && !type.base_type)
+               error(type, format("Use of undeclared type '%s'", type.base));
+}
+
+void ReferenceValidator::visit(ImageTypeDeclaration &type)
+{
+       if(!type.base.empty() && !type.base_type)
+               error(type, format("Use of undeclared type '%s'", type.base));
+}
+
 void ReferenceValidator::visit(VariableReference &var)
 {
        if(!var.declaration)
index bf2677e8d1cd993f67a5fa1936c4aeb37c0f32f1..e4b9edae932cb7dc1e2382a7a9f089da99c8e846 100644 (file)
@@ -21,6 +21,23 @@ protected:
        void error(Node &n, const std::string &m) { diagnose(n, Diagnostic::ERR, m); }
 };
 
+class TypeValidator: private Validator
+{
+private:
+       bool in_struct;
+
+public:
+       TypeValidator();
+
+       void apply(Stage &s) { stage = &s; s.content.visit(*this); }
+
+private:
+       virtual void visit(BasicTypeDeclaration &);
+       virtual void visit(ImageTypeDeclaration &);
+       virtual void visit(StructDeclaration &);
+       virtual void visit(VariableDeclaration &);
+};
+
 class DeclarationValidator: private Validator
 {
 private:
@@ -56,6 +73,8 @@ public:
        void apply(Stage &s) { stage = &s; s.content.visit(*this); }
 
 private:
+       virtual void visit(BasicTypeDeclaration &);
+       virtual void visit(ImageTypeDeclaration &);
        virtual void visit(VariableReference &);
        virtual void visit(InterfaceBlockReference &);
        virtual void visit(VariableDeclaration &);
diff --git a/tests/glsl/bad_types.glsl b/tests/glsl/bad_types.glsl
new file mode 100644 (file)
index 0000000..95f47e4
--- /dev/null
@@ -0,0 +1,14 @@
+typedef vector(2) void vvec2;
+typedef vector(2) mat2 hyper;
+typedef vector(2) sampler2D samplervec2;
+typedef image(dimensions=2) vec2 vec2sampler;
+
+#pragma MSP stage(vertex)
+void main() { }
+
+/* Expected error:
+<test>:1: Invalid base type 'void' for vector type 'vvec2'
+<test>:2: Invalid base type 'mat2' for vector type 'hyper'
+<test>:3: Invalid base type 'sampler2D' for vector type 'samplervec2'
+<test>:4: Invalid base type 'vec2' for image type 'vec2sampler'
+*/
diff --git a/tests/glsl/struct_member_initializer.glsl b/tests/glsl/struct_member_initializer.glsl
new file mode 100644 (file)
index 0000000..8be2e09
--- /dev/null
@@ -0,0 +1,12 @@
+struct Material
+{
+       vec4 diffuse;
+       vec4 specular = vec4(0.0);
+};
+
+#pragma MSP stage(vertex)
+void main() { }
+
+/* Expected error:
+<test>:4: Struct members can't have initializers
+*/
diff --git a/tests/glsl/undeclared_types.glsl b/tests/glsl/undeclared_types.glsl
new file mode 100644 (file)
index 0000000..4941ce4
--- /dev/null
@@ -0,0 +1,7 @@
+typedef vector(2) nonexistent nvec2;
+typedef image(dimensions=2) nonexistent nsampler2D;
+
+/* Expected error:
+<test>:1: Parse error at 'nonexistent': expected a type
+<test>:2: Parse error at 'nonexistent': expected a type
+*/