}
+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)
{ }
}
+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)
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:
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 &);
--- /dev/null
+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'
+*/