]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/validate.h
d38849012e26fd4569919eaff31588fb144c3559
[libs/gl.git] / source / glsl / validate.h
1 #ifndef MSP_GL_SL_VALIDATE_H_
2 #define MSP_GL_SL_VALIDATE_H_
3
4 #include <string>
5 #include <vector>
6 #include "glsl_error.h"
7 #include "visitor.h"
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 /** Base class for validators.  Contains some utilities for adding diagnostic
14 messages. */
15 class Validator: protected TraversingVisitor
16 {
17 protected:
18         Stage *stage;
19         Node *last_provoker;
20
21         Validator();
22
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 &);
27 };
28
29 /** Verifies that declarations are valid in isolation. */
30 class DeclarationValidator: private Validator
31 {
32 private:
33         enum ScopeType
34         {
35                 GLOBAL,
36                 STRUCT,
37                 INTERFACE_BLOCK,
38                 FUNCTION_PARAM,
39                 FUNCTION
40         };
41
42         ScopeType scope;
43         InterfaceLayout *iface_layout;
44         InterfaceBlock *iface_block;
45         VariableDeclaration *variable;
46
47 public:
48         DeclarationValidator();
49
50         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
51
52 private:
53         static const char *describe_variable(ScopeType);
54
55         virtual void visit(Layout &);
56         virtual void visit(InterfaceLayout &);
57         virtual void visit(BasicTypeDeclaration &);
58         virtual void visit(ImageTypeDeclaration &);
59         virtual void visit(StructDeclaration &);
60         virtual void visit(VariableDeclaration &);
61         virtual void visit(InterfaceBlock &);
62         virtual void visit(FunctionDeclaration &);
63 };
64
65 /** Verifies that identifiers are unique or, in the case of functions, are
66 overloaded only in valid ways. */
67 class IdentifierValidator: private Validator
68 {
69 private:
70         typedef std::map<std::string, Statement *> BlockDeclarationMap;
71
72         std::map<Block *, BlockDeclarationMap> declarations;
73         std::map<std::string, InterfaceBlock *> interface_blocks;
74         std::map<std::string, FunctionDeclaration *> overloaded_functions;
75         bool anonymous_block;
76
77 public:
78         IdentifierValidator();
79
80         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
81
82 private:
83         void multiple_definition(const std::string &, Statement &, Statement &);
84         Statement *find_definition(const std::string &);
85         void check_definition(const std::string &, Statement &);
86         void record_definition(const std::string &, Statement &);
87
88         virtual void visit(TypeDeclaration &);
89         virtual void visit(BasicTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
90         virtual void visit(ImageTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
91         virtual void visit(StructDeclaration &);
92         virtual void visit(VariableDeclaration &);
93         virtual void visit(InterfaceBlock &);
94         virtual void visit(FunctionDeclaration &);
95 };
96
97 /** Verifies that there are no unresolved references. */
98 class ReferenceValidator: private Validator
99 {
100 public:
101         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
102
103 private:
104         virtual void visit(BasicTypeDeclaration &);
105         virtual void visit(ImageTypeDeclaration &);
106         virtual void visit(VariableReference &);
107         virtual void visit(MemberAccess &);
108         virtual void visit(InterfaceBlockReference &);
109         virtual void visit(FunctionCall &);
110         virtual void visit(VariableDeclaration &);
111         virtual void visit(InterfaceBlock &);
112         virtual void visit(FunctionDeclaration &);
113 };
114
115 /** Verifies that expressions are valid.  In most cases an invalid expression
116 is indicated by a null result type. */
117 class ExpressionValidator: private Validator
118 {
119 private:
120         FunctionDeclaration *current_function;
121         bool constant_expression;
122
123 public:
124         ExpressionValidator();
125
126         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
127
128 private:
129         virtual void visit(VariableReference &);
130         virtual void visit(InterfaceBlockReference &);
131         virtual void visit(Swizzle &);
132         virtual void visit(UnaryExpression &);
133         virtual void visit(BinaryExpression &);
134         virtual void visit(Assignment &);
135         virtual void visit(TernaryExpression &);
136         virtual void visit(VariableDeclaration &);
137         virtual void visit(FunctionDeclaration &);
138         virtual void visit(Conditional &);
139         virtual void visit(Iteration &);
140         virtual void visit(Return &);
141 };
142
143 /** Verifies flow control constructs.  Functions returning non-void must have
144 return statements.  Warnings are given about dead code. */
145 class FlowControlValidator: private Validator
146 {
147 private:
148         bool reachable;
149
150 public:
151         FlowControlValidator();
152
153         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
154
155 private:
156         virtual void visit(Block &);
157         virtual void visit(FunctionDeclaration &);
158         virtual void visit(Conditional &);
159         virtual void visit(Iteration &);
160         virtual void visit(Return &) { reachable = false; }
161         virtual void visit(Jump &) { reachable = false; }
162 };
163
164 /** Verifies that stage input and output interfaces are valid.  Linked
165 variables must have matching types and locations and there must not be any
166 overlap in locations. */
167 class StageInterfaceValidator: private Validator
168 {
169 private:
170         std::map<std::string, std::map<unsigned, VariableDeclaration *> > used_locations;
171
172 public:
173         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
174
175 private:
176         int get_location(const Layout &);
177
178         virtual void visit(VariableDeclaration &);
179         virtual void visit(FunctionDeclaration &) { }
180 };
181
182 /** Verifies that uniform interfaces are valid across the entire module.
183 Variables declared with the same binding must have the same name and type. */
184 class GlobalInterfaceValidator: private Validator
185 {
186 private:
187         struct Uniform
188         {
189                 Node *node;
190                 TypeDeclaration *type;
191                 std::string name;
192                 int location;
193                 unsigned loc_count;
194                 int desc_set;
195                 int bind_point;
196
197                 Uniform(): node(0), type(0), location(-1), loc_count(1), desc_set(0), bind_point(-1) { }
198         };
199
200         std::list<Uniform> uniforms;
201         std::map<std::string, const Uniform *> used_names;
202         std::map<unsigned, const Uniform *> used_locations;
203         std::map<unsigned, std::map<unsigned, const Uniform *> > used_bindings;
204
205 public:
206         void apply(Module &);
207
208 private:
209         void check_uniform(const Uniform &);
210
211         virtual void visit(VariableDeclaration &);
212         virtual void visit(InterfaceBlock &);
213         virtual void visit(FunctionDeclaration &) { }
214 };
215
216 } // namespace SL
217 } // namespace GL
218 } // namespace Msp
219
220 #endif