]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/validate.cpp
f8ecc5eeefd34aaad243accf7034d9cee8513ca0
[libs/gl.git] / source / glsl / validate.cpp
1 #include <algorithm>
2 #include <cstring>
3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
5 #include "validate.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 Validator::Validator():
14         stage(0),
15         last_provoker(0)
16 { }
17
18 void Validator::diagnose(Node &node, Node &provoking_node, Diagnostic::Severity severity, const string &message)
19 {
20         Diagnostic diag;
21         diag.severity = severity;
22         diag.source = node.source;
23         diag.line = node.line;
24         diag.provoking_source = provoking_node.source;
25         diag.provoking_line = provoking_node.line;
26         diag.message = message;
27         stage->diagnostics.push_back(diag);
28
29         last_provoker = &provoking_node;
30 }
31
32 void Validator::add_info(Node &node, const string &message)
33 {
34         if(!last_provoker)
35                 throw logic_error("Tried to add info without a previous provoker");
36         diagnose(node, *last_provoker, Diagnostic::INFO, message);
37 }
38
39
40 TypeValidator::TypeValidator():
41         in_struct(false)
42 { }
43
44 void TypeValidator::visit(BasicTypeDeclaration &type)
45 {
46         if(type.kind==BasicTypeDeclaration::VECTOR)
47         {
48                 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
49                 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
50                         base_kind = basic_base->kind;
51                 if(base_kind!=BasicTypeDeclaration::BOOL && base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
52                         error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
53         }
54         else if(type.kind==BasicTypeDeclaration::ARRAY)
55         {
56                 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
57                         if(basic_base->kind==BasicTypeDeclaration::VOID)
58                                 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
59         }
60 }
61
62 void TypeValidator::visit(ImageTypeDeclaration &type)
63 {
64         BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
65         if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
66                 base_kind = basic_base->kind;
67         if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
68                 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
69 }
70
71 void TypeValidator::visit(StructDeclaration &strct)
72 {
73         SetFlag set_struct(in_struct);
74         TraversingVisitor::visit(strct);
75 }
76
77 void TypeValidator::visit(VariableDeclaration &var)
78 {
79         if(in_struct)
80         {
81                 if(var.layout)
82                         error(var, "Struct members can't have layouts");
83                 if(var.init_expression)
84                         error(var, "Struct members can't have initializers");
85         }
86
87         TraversingVisitor::visit(var);
88 }
89
90
91 DeclarationValidator::DeclarationValidator():
92         anonymous_block(false)
93 { }
94
95 void DeclarationValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
96 {
97         error(statement, format("Multiple definition of %s", name));
98         add_info(previous, "Previous definition is here");
99 }
100
101 Statement *DeclarationValidator::find_definition(const string &name)
102 {
103         BlockDeclarationMap *decls = &declarations[current_block];
104         BlockDeclarationMap::const_iterator i = decls->find(name);
105         if(i==decls->end() && anonymous_block)
106         {
107                 decls = &declarations[current_block->parent];
108                 i = decls->find(name);
109         }
110         return (i!=decls->end() ? i->second : 0);
111 }
112
113 void DeclarationValidator::check_definition(const string &name, Statement &statement)
114 {
115         if(Statement *previous = find_definition(name))
116                 multiple_definition(format("'%s'", name), statement, *previous);
117         else
118                 record_definition(name, statement);
119 }
120
121 void DeclarationValidator::record_definition(const string &name, Statement &statement)
122 {
123         declarations[current_block].insert(make_pair(name, &statement));
124         if(anonymous_block)
125                 declarations[current_block->parent].insert(make_pair(name, &statement));
126 }
127
128 void DeclarationValidator::visit(TypeDeclaration &type)
129 {
130         check_definition(type.name, type);
131 }
132
133 void DeclarationValidator::visit(StructDeclaration &strct)
134 {
135         check_definition(strct.name, strct);
136         TraversingVisitor::visit(strct);
137 }
138
139 void DeclarationValidator::visit(VariableDeclaration &var)
140 {
141         check_definition(var.name, var);
142         TraversingVisitor::visit(var);
143 }
144
145 void DeclarationValidator::visit(InterfaceBlock &iface)
146 {
147         string key = iface.interface+iface.name;
148         map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
149         if(i!=interface_blocks.end())
150                 multiple_definition(format("interface block '%s %s'", iface.interface, iface.name), iface, *i->second);
151         else
152                 interface_blocks.insert(make_pair(key, &iface));
153
154         if(Statement *previous = find_definition(iface.name))
155         {
156                 if(!dynamic_cast<InterfaceBlock *>(previous))
157                         multiple_definition(format("'%s'", iface.name), iface, *previous);
158         }
159         else
160                 record_definition(iface.name, iface);
161
162         if(!iface.instance_name.empty())
163                 check_definition(iface.instance_name, iface);
164
165         if(iface.instance_name.empty() && iface.struct_declaration)
166         {
167                 // Inject anonymous interface block members into the global scope
168                 const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
169                 for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
170                         check_definition(j->first, *j->second);
171         }
172 }
173
174 void DeclarationValidator::visit(FunctionDeclaration &func)
175 {
176         if(Statement *previous = find_definition(func.name))
177         {
178                 if(!dynamic_cast<FunctionDeclaration *>(previous))
179                         multiple_definition(format("'%s'", func.name), func, *previous);
180         }
181         else
182                 record_definition(func.name, func);
183
184         if(func.definition==&func)
185                 check_definition(func.name+func.signature, func);
186
187         TraversingVisitor::visit(func);
188 }
189
190
191 void ReferenceValidator::visit(BasicTypeDeclaration &type)
192 {
193         if(!type.base.empty() && !type.base_type)
194                 error(type, format("Use of undeclared type '%s'", type.base));
195 }
196
197 void ReferenceValidator::visit(ImageTypeDeclaration &type)
198 {
199         if(!type.base.empty() && !type.base_type)
200                 error(type, format("Use of undeclared type '%s'", type.base));
201 }
202
203 void ReferenceValidator::visit(VariableReference &var)
204 {
205         if(!var.declaration)
206                 error(var, format("Use of undeclared variable '%s'", var.name));
207         else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && !var.declaration->linked_declaration)
208                 error(var, format("Use of unlinked input variable '%s'", var.name));
209 }
210
211 void ReferenceValidator::visit(MemberAccess &memacc)
212 {
213         if(memacc.left->type && !memacc.declaration)
214                 error(memacc, format("Use of undeclared member '%s'", memacc.member));
215         TraversingVisitor::visit(memacc);
216 }
217
218 void ReferenceValidator::visit(InterfaceBlockReference &iface)
219 {
220         /* An interface block reference without a declaration should be impossible
221         since references are generated based on existing declarations. */
222         if(!iface.declaration)
223                 error(iface, format("Use of undeclared interface block '%s'", iface.name));
224         else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
225                 error(iface, format("Use of unlinked input block '%s'", iface.name));
226 }
227
228 void ReferenceValidator::visit(VariableDeclaration &var)
229 {
230         if(!var.type_declaration)
231                 error(var, format("Use of undeclared type '%s'", var.type));
232         TraversingVisitor::visit(var);
233 }
234
235 void ReferenceValidator::visit(InterfaceBlock &iface)
236 {
237         if(!iface.struct_declaration)
238                 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.name));
239         TraversingVisitor::visit(iface);
240 }
241
242 void ReferenceValidator::visit(FunctionDeclaration &func)
243 {
244         if(!func.return_type_declaration)
245                 error(func, format("Use of undeclared type '%s'", func.return_type));
246         TraversingVisitor::visit(func);
247 }
248
249
250 void ExpressionValidator::visit(Swizzle &swizzle)
251 {
252         unsigned size = 0;
253         if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
254         {
255                 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
256                         size = 1;
257                 else if(basic->kind==BasicTypeDeclaration::VECTOR)
258                         size = basic->size;
259         }
260
261         if(size)
262         {
263                 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
264                 int flavour = -1;
265                 for(unsigned i=0; i<swizzle.count; ++i)
266                 {
267                         unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
268                         if(flavour==-1)
269                                 flavour = component_flavour;
270                         else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
271                         {
272                                 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
273                                         swizzle.component_group[i], swizzle.component_group[0]));
274                                 flavour = -2;
275                         }
276
277                         if(swizzle.components[i]>=size)
278                                 error(swizzle, format("Access to component '%c' which is not present in '%s'",
279                                         swizzle.component_group[i], swizzle.left->type->name));
280                 }
281         }
282         else if(swizzle.left->type)
283                 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
284
285         TraversingVisitor::visit(swizzle);
286 }
287
288 void ExpressionValidator::visit(UnaryExpression &unary)
289 {
290         if(unary.expression->type)
291         {
292                 if(!unary.type)
293                         error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
294                 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
295                         error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
296         }
297         TraversingVisitor::visit(unary);
298 }
299
300 void ExpressionValidator::visit(BinaryExpression &binary)
301 {
302         if(!binary.type && binary.left->type && binary.right->type)
303                 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
304                         binary.oper->token, binary.left->type->name, binary.right->type->name));
305         TraversingVisitor::visit(binary);
306 }
307
308 void ExpressionValidator::visit(Assignment &assign)
309 {
310         if(assign.left->type)
311         {
312                 if(!assign.left->lvalue)
313                         error(assign, "Target of assignment is not an lvalue");
314                 if(assign.right->type)
315                 {
316                         if(assign.oper->token[0]!='=')
317                         {
318                                 if(!assign.type)
319                                         error(assign, format("No matching operator '%s' found for '%s' and '%s'",
320                                                 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
321                         }
322                         else if(assign.left->type!=assign.right->type)
323                                 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
324                                         assign.left->type->name, assign.right->type->name));
325                 }
326         }
327         TraversingVisitor::visit(assign);
328 }
329
330 void ExpressionValidator::visit(VariableDeclaration &var)
331 {
332         if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
333                 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
334                         var.type_declaration->name, var.init_expression->type->name));
335         TraversingVisitor::visit(var);
336 }
337
338 } // namespace SL
339 } // namespace GL
340 } // namespace Msp