bool Compiler::validate(Stage &stage)
{
DeclarationValidator().apply(stage);
+ ReferenceValidator().apply(stage);
for(vector<Diagnostic>::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i)
if(i->severity==Diagnostic::ERR)
TraversingVisitor::visit(func);
}
+
+void ReferenceValidator::visit(VariableReference &var)
+{
+ if(!var.declaration)
+ error(var, format("Use of undeclared variable '%s'", var.name));
+ else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && !var.declaration->linked_declaration)
+ error(var, format("Use of unlinked input variable '%s'", var.name));
+}
+
+void ReferenceValidator::visit(InterfaceBlockReference &iface)
+{
+ /* An interface block reference without a declaration should be impossible
+ since references are generated based on existing declarations. */
+ if(!iface.declaration)
+ error(iface, format("Use of undeclared interface block '%s'", iface.name));
+ else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
+ error(iface, format("Use of unlinked input block '%s'", iface.name));
+}
+
+void ReferenceValidator::visit(VariableDeclaration &var)
+{
+ if(!var.type_declaration)
+ error(var, format("Use of undeclared type '%s'", var.type));
+ TraversingVisitor::visit(var);
+}
+
} // namespace SL
} // namespace GL
} // namespace Msp
virtual void visit(FunctionDeclaration &);
};
+class ReferenceValidator: private Validator
+{
+public:
+ void apply(Stage &s) { stage = &s; s.content.visit(*this); }
+
+private:
+ virtual void visit(VariableReference &);
+ virtual void visit(InterfaceBlockReference &);
+ virtual void visit(VariableDeclaration &);
+};
+
} // namespace SL
} // namespace GL
} // namespace Msp
--- /dev/null
+uniform Transform
+{
+ mat4 mvp;
+} transform;
+
+#pragma MSP stage(vertex)
+void main()
+{
+ gl_Position = transform.model*position;
+}
+
+/* Expected error:
+<test>:9: Use of undeclared member 'model'
+<test>:9: Use of undeclared variable 'position'
+*/
--- /dev/null
+uniform sampler2D tex;
+
+#pragma MSP stage(vertex)
+layout(location=0) in vec4 position;
+void main()
+{
+ gl_Position = position;
+}
+
+#pragma MSP stage(fragment)
+in vec2 texcoord;
+in VertexOut
+{
+ vec4 color;
+} vs_out;
+out vec4 frag_color;
+void main()
+{
+ frag_color = texture(tex, texcoord)*vs_out.color;
+}
+
+/* Expected error:
+<test>:19: Use of unlinked input variable 'texcoord'
+<test>:19: Use of unlinked input block 'vs_out'
+*/