1 #ifndef MSP_GL_SL_VALIDATE_H_
2 #define MSP_GL_SL_VALIDATE_H_
6 #include "glsl_error.h"
13 /** Base class for validators. Contains some utilities for adding diagnostic
15 class Validator: protected TraversingVisitor
19 Node *last_provoker = 0;
21 Validator() = default;
23 void diagnose(Node &, Node &, Diagnostic::Severity, const std::string &);
24 void diagnose(Node &n, Diagnostic::Severity s, const std::string &m) { diagnose(n, n, s, m); }
25 void error(Node &n, const std::string &m) { diagnose(n, Diagnostic::ERR, m); }
26 void add_info(Node &, const std::string &);
29 /** Verifies that declarations are valid in isolation. */
30 class DeclarationValidator: private Validator
43 ScopeType scope = GLOBAL;
44 InterfaceLayout *iface_layout = 0;
45 VariableDeclaration *iface_block = 0;
46 VariableDeclaration *variable = 0;
49 void apply(Stage &, const Features &);
52 static const char *describe_variable(ScopeType);
54 virtual void visit(Layout &);
55 virtual void visit(InterfaceLayout &);
56 virtual void visit(BasicTypeDeclaration &);
57 virtual void visit(ImageTypeDeclaration &);
58 virtual void visit(StructDeclaration &);
59 virtual void visit(VariableDeclaration &);
60 virtual void visit(FunctionDeclaration &);
63 /** Verifies that identifiers are unique or, in the case of functions, are
64 overloaded only in valid ways. */
65 class IdentifierValidator: private Validator
68 typedef std::map<std::string, Statement *> BlockDeclarationMap;
70 std::map<Block *, BlockDeclarationMap> declarations;
71 std::map<std::string, VariableDeclaration *> interface_blocks;
72 std::map<std::string, FunctionDeclaration *> overloaded_functions;
73 bool anonymous_block = false;
76 void apply(Stage &s) { stage = &s; s.content.visit(*this); }
79 void multiple_definition(const std::string &, Statement &, Statement &);
80 Statement *find_definition(const std::string &);
81 void check_definition(const std::string &, Statement &);
82 void record_definition(const std::string &, Statement &);
84 virtual void visit(TypeDeclaration &);
85 virtual void visit(BasicTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
86 virtual void visit(ImageTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
87 virtual void visit(StructDeclaration &);
88 virtual void visit(VariableDeclaration &);
89 virtual void visit(FunctionDeclaration &);
92 /** Verifies that there are no unresolved references. */
93 class ReferenceValidator: private Validator
96 void apply(Stage &s) { stage = &s; s.content.visit(*this); }
99 virtual void visit(BasicTypeDeclaration &);
100 virtual void visit(ImageTypeDeclaration &);
101 virtual void visit(VariableReference &);
102 virtual void visit(MemberAccess &);
103 virtual void visit(FunctionCall &);
104 virtual void visit(VariableDeclaration &);
105 virtual void visit(FunctionDeclaration &);
108 /** Verifies that expressions are valid. In most cases an invalid expression
109 is indicated by a null result type. */
110 class ExpressionValidator: private Validator
120 FunctionDeclaration *current_function = 0;
121 bool in_struct = false;
122 ConstantKind constant_expression = NOT_CONSTANT;
125 void apply(Stage &s) { stage = &s; s.content.visit(*this); }
128 virtual void visit(VariableReference &);
129 virtual void visit(Swizzle &);
130 virtual void visit(UnaryExpression &);
131 virtual void visit(BinaryExpression &);
132 virtual void visit(Assignment &);
133 virtual void visit(TernaryExpression &);
134 virtual void visit(StructDeclaration &);
135 virtual void visit(VariableDeclaration &);
136 virtual void visit(FunctionDeclaration &);
137 virtual void visit(Conditional &);
138 virtual void visit(Iteration &);
139 virtual void visit(Return &);
142 /** Verifies flow control constructs. Functions returning non-void must have
143 return statements. Warnings are given about dead code. */
144 class FlowControlValidator: private Validator
147 bool reachable = true;
150 void apply(Stage &s) { stage = &s; s.content.visit(*this); }
153 virtual void visit(Block &);
154 virtual void visit(FunctionDeclaration &);
155 virtual void visit(Conditional &);
156 virtual void visit(Iteration &);
157 virtual void visit(Return &) { reachable = false; }
158 virtual void visit(Jump &) { reachable = false; }
161 /** Verifies that stage input and output interfaces are valid. Linked
162 variables must have matching types and locations and there must not be any
163 overlap in locations. */
164 class StageInterfaceValidator: private Validator
167 std::map<std::string, std::map<unsigned, VariableDeclaration *> > used_locations;
170 void apply(Stage &s) { stage = &s; s.content.visit(*this); }
173 virtual void visit(VariableDeclaration &);
174 virtual void visit(FunctionDeclaration &) { }
177 /** Verifies that uniform interfaces are valid across the entire module.
178 Variables declared with the same binding must have the same name and type. */
179 class GlobalInterfaceValidator: private Validator
185 TypeDeclaration *type = 0;
188 unsigned loc_count = 1;
193 std::list<Uniform> uniforms;
194 std::map<std::string, const Uniform *> used_names;
195 std::map<unsigned, const Uniform *> used_locations;
196 std::map<unsigned, std::map<unsigned, const Uniform *> > used_bindings;
199 void apply(Module &);
202 void check_uniform(const Uniform &);
204 virtual void visit(VariableDeclaration &);
205 virtual void visit(FunctionDeclaration &) { }