X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fvalidate.h;h=fe52ff5aae1878aa0006c2dce8b4c60ca6b4b7af;hb=ea489c40680f9e8e90eeb163d17c8dddd53981d4;hp=a8c6f11c6a8e12602431abb6fd22a93b280e55fd;hpb=7d66c70e15b84cbaf6b1973db07629f5bd3e5cdf;p=libs%2Fgl.git diff --git a/source/glsl/validate.h b/source/glsl/validate.h index a8c6f11c..fe52ff5a 100644 --- a/source/glsl/validate.h +++ b/source/glsl/validate.h @@ -10,6 +10,8 @@ namespace Msp { namespace GL { namespace SL { +/** Base class for validators. Contains some utilities for adding diagnostic +messages. */ class Validator: protected TraversingVisitor { protected: @@ -24,24 +26,45 @@ protected: void add_info(Node &, const std::string &); }; -class TypeValidator: private Validator +/** Verifies that declarations are valid in isolation. */ +class DeclarationValidator: private Validator { private: - bool in_struct; + enum ScopeType + { + GLOBAL, + STRUCT, + INTERFACE_BLOCK, + FUNCTION_PARAM, + FUNCTION + }; + + ScopeType scope; + InterfaceLayout *iface_layout; + InterfaceBlock *iface_block; + VariableDeclaration *variable; public: - TypeValidator(); + DeclarationValidator(); void apply(Stage &s) { stage = &s; s.content.visit(*this); } 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(InterfaceBlock &); + virtual void visit(FunctionDeclaration &); }; -class DeclarationValidator: private Validator +/** 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 BlockDeclarationMap; @@ -52,7 +75,7 @@ private: bool anonymous_block; public: - DeclarationValidator(); + IdentifierValidator(); void apply(Stage &s) { stage = &s; s.content.visit(*this); } @@ -71,6 +94,7 @@ private: virtual void visit(FunctionDeclaration &); }; +/** Verifies that there are no unresolved references. */ class ReferenceValidator: private Validator { public: @@ -82,22 +106,90 @@ private: virtual void visit(VariableReference &); virtual void visit(MemberAccess &); virtual void visit(InterfaceBlockReference &); + virtual void visit(FunctionCall &); virtual void visit(VariableDeclaration &); virtual void visit(InterfaceBlock &); 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: + FunctionDeclaration *current_function; + bool constant_expression; + public: + ExpressionValidator(); + void apply(Stage &s) { stage = &s; s.content.visit(*this); } private: + virtual void visit(VariableReference &); + virtual void visit(InterfaceBlockReference &); virtual void visit(Swizzle &); virtual void visit(UnaryExpression &); virtual void visit(BinaryExpression &); virtual void visit(Assignment &); + virtual void visit(TernaryExpression &); virtual void visit(VariableDeclaration &); + virtual void visit(FunctionDeclaration &); + virtual void visit(Conditional &); + virtual void visit(Iteration &); + virtual void visit(Return &); +}; + +/** 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 > used_locations; + +public: + void apply(Stage &s) { stage = &s; s.content.visit(*this); } + +private: + int get_location(const Layout &); + + 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; + TypeDeclaration *type; + std::string name; + int location; + unsigned loc_count; + int desc_set; + int bind_point; + + Uniform(): node(0), type(0), location(-1), loc_count(1), desc_set(0), bind_point(-1) { } + }; + + std::list uniforms; + std::map used_names; + std::map used_locations; + std::map > used_bindings; + +public: + void apply(Module &); + +private: + void check_uniform(const Uniform &); + + virtual void visit(VariableDeclaration &); + virtual void visit(InterfaceBlock &); + virtual void visit(FunctionDeclaration &) { } }; } // namespace SL