]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/validate.h
Validate the presence of in/out layout qualifiers for geometry shaders
[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 = 0;
19         Node *last_provoker = 0;
20
21         Validator() = default;
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         Features features;
43         ScopeType scope = GLOBAL;
44         InterfaceLayout *iface_layout = 0;
45         VariableDeclaration *iface_block = 0;
46         VariableDeclaration *variable = 0;
47         bool have_input_primitive = false;
48         bool have_output_primitive = false;
49         bool have_output_vertex_count = false;
50
51 public:
52         void apply(Stage &, const Features &);
53
54 private:
55         static const char *describe_variable(ScopeType);
56
57         virtual void visit(Layout &);
58         virtual void visit(InterfaceLayout &);
59         virtual void visit(BasicTypeDeclaration &);
60         virtual void visit(ImageTypeDeclaration &);
61         virtual void visit(StructDeclaration &);
62         virtual void visit(VariableDeclaration &);
63         virtual void visit(FunctionDeclaration &);
64 };
65
66 /** Verifies that identifiers are unique or, in the case of functions, are
67 overloaded only in valid ways. */
68 class IdentifierValidator: private Validator
69 {
70 private:
71         typedef std::map<std::string, Statement *> BlockDeclarationMap;
72
73         std::map<Block *, BlockDeclarationMap> declarations;
74         std::map<std::string, VariableDeclaration *> interface_blocks;
75         std::map<std::string, FunctionDeclaration *> overloaded_functions;
76         bool anonymous_block = false;
77
78 public:
79         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
80
81 private:
82         void multiple_definition(const std::string &, Statement &, Statement &);
83         Statement *find_definition(const std::string &);
84         void check_definition(const std::string &, Statement &);
85         void record_definition(const std::string &, Statement &);
86
87         virtual void visit(TypeDeclaration &);
88         virtual void visit(BasicTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
89         virtual void visit(ImageTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
90         virtual void visit(StructDeclaration &);
91         virtual void visit(VariableDeclaration &);
92         virtual void visit(FunctionDeclaration &);
93 };
94
95 /** Verifies that there are no unresolved references. */
96 class ReferenceValidator: private Validator
97 {
98 public:
99         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
100
101 private:
102         virtual void visit(BasicTypeDeclaration &);
103         virtual void visit(ImageTypeDeclaration &);
104         virtual void visit(VariableReference &);
105         virtual void visit(MemberAccess &);
106         virtual void visit(FunctionCall &);
107         virtual void visit(VariableDeclaration &);
108         virtual void visit(FunctionDeclaration &);
109 };
110
111 /** Verifies that expressions are valid.  In most cases an invalid expression
112 is indicated by a null result type. */
113 class ExpressionValidator: private Validator
114 {
115 private:
116         enum ConstantKind
117         {
118                 NOT_CONSTANT,
119                 FIXED_CONSTANT,
120                 SPEC_CONSTANT
121         };
122
123         FunctionDeclaration *current_function = 0;
124         bool in_struct = false;
125         ConstantKind constant_expression = NOT_CONSTANT;
126
127 public:
128         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
129
130 private:
131         virtual void visit(VariableReference &);
132         virtual void visit(Swizzle &);
133         virtual void visit(UnaryExpression &);
134         virtual void visit(BinaryExpression &);
135         virtual void visit(Assignment &);
136         virtual void visit(TernaryExpression &);
137         virtual void visit(StructDeclaration &);
138         virtual void visit(VariableDeclaration &);
139         virtual void visit(FunctionDeclaration &);
140         virtual void visit(Conditional &);
141         virtual void visit(Iteration &);
142         virtual void visit(Return &);
143 };
144
145 /** Verifies flow control constructs.  Functions returning non-void must have
146 return statements.  Warnings are given about dead code. */
147 class FlowControlValidator: private Validator
148 {
149 private:
150         bool reachable = true;
151
152 public:
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         virtual void visit(VariableDeclaration &);
177         virtual void visit(FunctionDeclaration &) { }
178 };
179
180 /** Verifies that uniform interfaces are valid across the entire module.
181 Variables declared with the same binding must have the same name and type. */
182 class GlobalInterfaceValidator: private Validator
183 {
184 private:
185         struct Uniform
186         {
187                 Node *node = 0;
188                 TypeDeclaration *type = 0;
189                 std::string name;
190                 int location = -1;
191                 unsigned loc_count = 1;
192                 int desc_set = 0;
193                 int bind_point = -1;
194         };
195
196         std::list<Uniform> uniforms;
197         std::map<std::string, const Uniform *> used_names;
198         std::map<unsigned, const Uniform *> used_locations;
199         std::map<unsigned, std::map<unsigned, const Uniform *> > used_bindings;
200
201 public:
202         void apply(Module &);
203
204 private:
205         void check_uniform(const Uniform &);
206
207         virtual void visit(VariableDeclaration &);
208         virtual void visit(FunctionDeclaration &) { }
209 };
210
211 } // namespace SL
212 } // namespace GL
213 } // namespace Msp
214
215 #endif