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