]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/validate.h
Process loop initialization outside the body in UnusedVariableRemover
[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         ScopeType scope = GLOBAL;
43         InterfaceLayout *iface_layout = 0;
44         InterfaceBlock *iface_block = 0;
45         VariableDeclaration *variable = 0;
46
47 public:
48         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
49
50 private:
51         static const char *describe_variable(ScopeType);
52
53         virtual void visit(Layout &);
54         virtual void visit(InterfaceLayout &);
55         virtual void visit(BasicTypeDeclaration &);
56         virtual void visit(ImageTypeDeclaration &);
57         virtual void visit(StructDeclaration &);
58         virtual void visit(VariableDeclaration &);
59         virtual void visit(InterfaceBlock &);
60         virtual void visit(FunctionDeclaration &);
61 };
62
63 /** Verifies that identifiers are unique or, in the case of functions, are
64 overloaded only in valid ways. */
65 class IdentifierValidator: private Validator
66 {
67 private:
68         typedef std::map<std::string, Statement *> BlockDeclarationMap;
69
70         std::map<Block *, BlockDeclarationMap> declarations;
71         std::map<std::string, InterfaceBlock *> interface_blocks;
72         std::map<std::string, FunctionDeclaration *> overloaded_functions;
73         bool anonymous_block = false;
74
75 public:
76         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
77
78 private:
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 &);
83
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(InterfaceBlock &);
90         virtual void visit(FunctionDeclaration &);
91 };
92
93 /** Verifies that there are no unresolved references. */
94 class ReferenceValidator: private Validator
95 {
96 public:
97         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
98
99 private:
100         virtual void visit(BasicTypeDeclaration &);
101         virtual void visit(ImageTypeDeclaration &);
102         virtual void visit(VariableReference &);
103         virtual void visit(MemberAccess &);
104         virtual void visit(InterfaceBlockReference &);
105         virtual void visit(FunctionCall &);
106         virtual void visit(VariableDeclaration &);
107         virtual void visit(InterfaceBlock &);
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(InterfaceBlockReference &);
133         virtual void visit(Swizzle &);
134         virtual void visit(UnaryExpression &);
135         virtual void visit(BinaryExpression &);
136         virtual void visit(Assignment &);
137         virtual void visit(TernaryExpression &);
138         virtual void visit(StructDeclaration &);
139         virtual void visit(VariableDeclaration &);
140         virtual void visit(FunctionDeclaration &);
141         virtual void visit(Conditional &);
142         virtual void visit(Iteration &);
143         virtual void visit(Return &);
144 };
145
146 /** Verifies flow control constructs.  Functions returning non-void must have
147 return statements.  Warnings are given about dead code. */
148 class FlowControlValidator: private Validator
149 {
150 private:
151         bool reachable = true;
152
153 public:
154         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
155
156 private:
157         virtual void visit(Block &);
158         virtual void visit(FunctionDeclaration &);
159         virtual void visit(Conditional &);
160         virtual void visit(Iteration &);
161         virtual void visit(Return &) { reachable = false; }
162         virtual void visit(Jump &) { reachable = false; }
163 };
164
165 /** Verifies that stage input and output interfaces are valid.  Linked
166 variables must have matching types and locations and there must not be any
167 overlap in locations. */
168 class StageInterfaceValidator: private Validator
169 {
170 private:
171         std::map<std::string, std::map<unsigned, VariableDeclaration *> > used_locations;
172
173 public:
174         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
175
176 private:
177         int get_location(const Layout &);
178
179         virtual void visit(VariableDeclaration &);
180         virtual void visit(FunctionDeclaration &) { }
181 };
182
183 /** Verifies that uniform interfaces are valid across the entire module.
184 Variables declared with the same binding must have the same name and type. */
185 class GlobalInterfaceValidator: private Validator
186 {
187 private:
188         struct Uniform
189         {
190                 Node *node = 0;
191                 TypeDeclaration *type = 0;
192                 std::string name;
193                 int location = -1;
194                 unsigned loc_count = 1;
195                 int desc_set = 0;
196                 int bind_point = -1;
197         };
198
199         std::list<Uniform> uniforms;
200         std::map<std::string, const Uniform *> used_names;
201         std::map<unsigned, const Uniform *> used_locations;
202         std::map<unsigned, std::map<unsigned, const Uniform *> > used_bindings;
203
204 public:
205         void apply(Module &);
206
207 private:
208         void check_uniform(const Uniform &);
209
210         virtual void visit(VariableDeclaration &);
211         virtual void visit(InterfaceBlock &);
212         virtual void visit(FunctionDeclaration &) { }
213 };
214
215 } // namespace SL
216 } // namespace GL
217 } // namespace Msp
218
219 #endif