]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/validate.h
Fix a name conflict in certain inlining scenarios
[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         InterfaceBlock *iface_block = 0;
46         VariableDeclaration *variable = 0;
47
48 public:
49         void apply(Stage &, const Features &);
50
51 private:
52         static const char *describe_variable(ScopeType);
53
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(InterfaceBlock &);
61         virtual void visit(FunctionDeclaration &);
62 };
63
64 /** Verifies that identifiers are unique or, in the case of functions, are
65 overloaded only in valid ways. */
66 class IdentifierValidator: private Validator
67 {
68 private:
69         typedef std::map<std::string, Statement *> BlockDeclarationMap;
70
71         std::map<Block *, BlockDeclarationMap> declarations;
72         std::map<std::string, InterfaceBlock *> interface_blocks;
73         std::map<std::string, FunctionDeclaration *> overloaded_functions;
74         bool anonymous_block = false;
75
76 public:
77         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
78
79 private:
80         void multiple_definition(const std::string &, Statement &, Statement &);
81         Statement *find_definition(const std::string &);
82         void check_definition(const std::string &, Statement &);
83         void record_definition(const std::string &, Statement &);
84
85         virtual void visit(TypeDeclaration &);
86         virtual void visit(BasicTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
87         virtual void visit(ImageTypeDeclaration &t) { visit(static_cast<TypeDeclaration &>(t)); }
88         virtual void visit(StructDeclaration &);
89         virtual void visit(VariableDeclaration &);
90         virtual void visit(InterfaceBlock &);
91         virtual void visit(FunctionDeclaration &);
92 };
93
94 /** Verifies that there are no unresolved references. */
95 class ReferenceValidator: private Validator
96 {
97 public:
98         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
99
100 private:
101         virtual void visit(BasicTypeDeclaration &);
102         virtual void visit(ImageTypeDeclaration &);
103         virtual void visit(VariableReference &);
104         virtual void visit(MemberAccess &);
105         virtual void visit(InterfaceBlockReference &);
106         virtual void visit(FunctionCall &);
107         virtual void visit(VariableDeclaration &);
108         virtual void visit(InterfaceBlock &);
109         virtual void visit(FunctionDeclaration &);
110 };
111
112 /** Verifies that expressions are valid.  In most cases an invalid expression
113 is indicated by a null result type. */
114 class ExpressionValidator: private Validator
115 {
116 private:
117         enum ConstantKind
118         {
119                 NOT_CONSTANT,
120                 FIXED_CONSTANT,
121                 SPEC_CONSTANT
122         };
123
124         FunctionDeclaration *current_function = 0;
125         bool in_struct = false;
126         ConstantKind constant_expression = NOT_CONSTANT;
127
128 public:
129         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
130
131 private:
132         virtual void visit(VariableReference &);
133         virtual void visit(InterfaceBlockReference &);
134         virtual void visit(Swizzle &);
135         virtual void visit(UnaryExpression &);
136         virtual void visit(BinaryExpression &);
137         virtual void visit(Assignment &);
138         virtual void visit(TernaryExpression &);
139         virtual void visit(StructDeclaration &);
140         virtual void visit(VariableDeclaration &);
141         virtual void visit(FunctionDeclaration &);
142         virtual void visit(Conditional &);
143         virtual void visit(Iteration &);
144         virtual void visit(Return &);
145 };
146
147 /** Verifies flow control constructs.  Functions returning non-void must have
148 return statements.  Warnings are given about dead code. */
149 class FlowControlValidator: private Validator
150 {
151 private:
152         bool reachable = true;
153
154 public:
155         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
156
157 private:
158         virtual void visit(Block &);
159         virtual void visit(FunctionDeclaration &);
160         virtual void visit(Conditional &);
161         virtual void visit(Iteration &);
162         virtual void visit(Return &) { reachable = false; }
163         virtual void visit(Jump &) { reachable = false; }
164 };
165
166 /** Verifies that stage input and output interfaces are valid.  Linked
167 variables must have matching types and locations and there must not be any
168 overlap in locations. */
169 class StageInterfaceValidator: private Validator
170 {
171 private:
172         std::map<std::string, std::map<unsigned, VariableDeclaration *> > used_locations;
173
174 public:
175         void apply(Stage &s) { stage = &s; s.content.visit(*this); }
176
177 private:
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 = 0;
190                 TypeDeclaration *type = 0;
191                 std::string name;
192                 int location = -1;
193                 unsigned loc_count = 1;
194                 int desc_set = 0;
195                 int bind_point = -1;
196         };
197
198         std::list<Uniform> uniforms;
199         std::map<std::string, const Uniform *> used_names;
200         std::map<unsigned, const Uniform *> used_locations;
201         std::map<unsigned, std::map<unsigned, const Uniform *> > used_bindings;
202
203 public:
204         void apply(Module &);
205
206 private:
207         void check_uniform(const Uniform &);
208
209         virtual void visit(VariableDeclaration &);
210         virtual void visit(InterfaceBlock &);
211         virtual void visit(FunctionDeclaration &) { }
212 };
213
214 } // namespace SL
215 } // namespace GL
216 } // namespace Msp
217
218 #endif