]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/validate.h
Check the flat qualifier from the correct member
[libs/gl.git] / source / glsl / validate.h
index 01f2ba4f82c75d6ce76cba2c77eef6eb43d7f707..3166c39bf9bd304c15067c77984e089fd417b22a 100644 (file)
@@ -10,29 +10,73 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
+/** Base class for validators.  Contains some utilities for adding diagnostic
+messages. */
 class Validator: protected TraversingVisitor
 {
 protected:
-       Stage *stage;
+       Stage *stage = 0;
+       Node *last_provoker = 0;
 
-       Validator();
+       Validator() = default;
 
-       void diagnose(Node &, Diagnostic::Severity, const std::string &);
+       void diagnose(Node &, Node &, Diagnostic::Severity, const std::string &);
+       void diagnose(Node &n, Diagnostic::Severity s, const std::string &m) { diagnose(n, n, s, m); }
        void error(Node &n, const std::string &m) { diagnose(n, Diagnostic::ERR, m); }
+       void add_info(Node &, const std::string &);
 };
 
+/** Verifies that declarations are valid in isolation. */
 class DeclarationValidator: private Validator
 {
+private:
+       enum ScopeType
+       {
+               GLOBAL,
+               STRUCT,
+               INTERFACE_BLOCK,
+               FUNCTION_PARAM,
+               FUNCTION
+       };
+
+       Features features;
+       ScopeType scope = GLOBAL;
+       InterfaceLayout *iface_layout = 0;
+       VariableDeclaration *iface_block = 0;
+       VariableDeclaration *variable = 0;
+       bool have_input_primitive = false;
+       bool have_output_primitive = false;
+       bool have_output_vertex_count = false;
+       bool have_workgroup_size = false;
+
+public:
+       void apply(Stage &, const Features &);
+
+private:
+       static const char *describe_variable(ScopeType);
+
+       virtual void visit(Layout &);
+       virtual void visit(InterfaceLayout &);
+       virtual void visit(BasicTypeDeclaration &);
+       virtual void visit(ImageTypeDeclaration &);
+       virtual void visit(StructDeclaration &);
+       virtual void visit(VariableDeclaration &);
+       virtual void visit(FunctionDeclaration &);
+};
+
+/** Verifies that identifiers are unique or, in the case of functions, are
+overloaded only in valid ways. */
+class IdentifierValidator: private Validator
+{
 private:
        typedef std::map<std::string, Statement *> BlockDeclarationMap;
 
        std::map<Block *, BlockDeclarationMap> declarations;
-       std::map<std::string, InterfaceBlock *> interface_blocks;
-       bool anonymous_block;
+       std::map<std::string, VariableDeclaration *> interface_blocks;
+       std::map<std::string, FunctionDeclaration *> overloaded_functions;
+       bool anonymous_block = false;
 
 public:
-       DeclarationValidator();
-
        void apply(Stage &s) { stage = &s; s.content.visit(*this); }
 
 private:
@@ -41,12 +85,130 @@ private:
        void check_definition(const std::string &, Statement &);
        void record_definition(const std::string &, Statement &);
 
+       virtual void visit(TypeDeclaration &);
+       virtual void visit(BasicTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
+       virtual void visit(ImageTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
        virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);
-       virtual void visit(InterfaceBlock &);
        virtual void visit(FunctionDeclaration &);
 };
 
+/** Verifies that there are no unresolved references. */
+class ReferenceValidator: private Validator
+{
+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(MemberAccess &);
+       virtual void visit(FunctionCall &);
+       virtual void visit(VariableDeclaration &);
+       virtual void visit(FunctionDeclaration &);
+};
+
+/** Verifies that expressions are valid.  In most cases an invalid expression
+is indicated by a null result type. */
+class ExpressionValidator: private Validator
+{
+private:
+       enum ConstantKind
+       {
+               NOT_CONSTANT,
+               FIXED_CONSTANT,
+               SPEC_CONSTANT
+       };
+
+       FunctionDeclaration *current_function = 0;
+       bool in_struct = false;
+       ConstantKind constant_expression = NOT_CONSTANT;
+
+public:
+       void apply(Stage &s) { stage = &s; s.content.visit(*this); }
+
+private:
+       virtual void visit(VariableReference &);
+       virtual void visit(Swizzle &);
+       virtual void visit(UnaryExpression &);
+       virtual void visit(BinaryExpression &);
+       virtual void visit(Assignment &);
+       virtual void visit(TernaryExpression &);
+       virtual void visit(StructDeclaration &);
+       virtual void visit(VariableDeclaration &);
+       virtual void visit(FunctionDeclaration &);
+       virtual void visit(Conditional &);
+       virtual void visit(Iteration &);
+       virtual void visit(Return &);
+};
+
+/** Verifies flow control constructs.  Functions returning non-void must have
+return statements.  Warnings are given about dead code. */
+class FlowControlValidator: private Validator
+{
+private:
+       bool reachable = true;
+
+public:
+       void apply(Stage &s) { stage = &s; s.content.visit(*this); }
+
+private:
+       virtual void visit(Block &);
+       virtual void visit(FunctionDeclaration &);
+       virtual void visit(Conditional &);
+       virtual void visit(Iteration &);
+       virtual void visit(Return &) { reachable = false; }
+       virtual void visit(Jump &) { reachable = false; }
+};
+
+/** Verifies that stage input and output interfaces are valid.  Linked
+variables must have matching types and locations and there must not be any
+overlap in locations. */
+class StageInterfaceValidator: private Validator
+{
+private:
+       std::map<std::string, std::map<unsigned, VariableDeclaration *> > used_locations;
+
+public:
+       void apply(Stage &s) { stage = &s; s.content.visit(*this); }
+
+private:
+       virtual void visit(VariableDeclaration &);
+       virtual void visit(FunctionDeclaration &) { }
+};
+
+/** Verifies that uniform interfaces are valid across the entire module.
+Variables declared with the same binding must have the same name and type. */
+class GlobalInterfaceValidator: private Validator
+{
+private:
+       struct Uniform
+       {
+               Node *node = 0;
+               TypeDeclaration *type = 0;
+               std::string name;
+               int location = -1;
+               unsigned loc_count = 1;
+               int desc_set = 0;
+               int bind_point = -1;
+       };
+
+       std::list<Uniform> uniforms;
+       std::map<std::string, const Uniform *> used_names;
+       std::map<unsigned, const Uniform *> used_locations;
+       std::map<unsigned, std::map<unsigned, const Uniform *> > used_bindings;
+
+public:
+       void apply(Module &);
+
+private:
+       void check_uniform(const Uniform &);
+
+       virtual void visit(VariableDeclaration &);
+       virtual void visit(FunctionDeclaration &) { }
+};
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp