]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/validate.cpp
Better checking of where the binding qualifier is allowed
[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 "reflect.h"
7 #include "validate.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13 namespace SL {
14
15 Validator::Validator():
16         stage(0),
17         last_provoker(0)
18 { }
19
20 void Validator::diagnose(Node &node, Node &provoking_node, Diagnostic::Severity severity, const string &message)
21 {
22         Diagnostic diag;
23         diag.severity = severity;
24         diag.source = node.source;
25         diag.line = node.line;
26         diag.provoking_source = provoking_node.source;
27         diag.provoking_line = provoking_node.line;
28         diag.message = message;
29         stage->diagnostics.push_back(diag);
30
31         last_provoker = &provoking_node;
32 }
33
34 void Validator::add_info(Node &node, const string &message)
35 {
36         if(!last_provoker)
37                 throw logic_error("Tried to add info without a previous provoker");
38         diagnose(node, *last_provoker, Diagnostic::INFO, message);
39 }
40
41
42 DeclarationValidator::DeclarationValidator():
43         scope(GLOBAL),
44         iface_layout(0),
45         iface_block(0),
46         variable(0)
47 { }
48
49 const char *DeclarationValidator::describe_variable(ScopeType scope)
50 {
51         switch(scope)
52         {
53         case GLOBAL: return "global variable";
54         case STRUCT: return "struct member";
55         case INTERFACE_BLOCK: return "interface block member";
56         case FUNCTION_PARAM: return "function parameter";
57         case FUNCTION: return "local variable";
58         default: return "variable";
59         }
60 }
61
62 void DeclarationValidator::visit(Layout &layout)
63 {
64         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
65         {
66                 bool allowed = false;
67                 string err_descr;
68                 bool value = true;
69                 if(i->name=="location")
70                         allowed = (variable && scope==GLOBAL);
71                 else if(i->name=="binding" || i->name=="set")
72                 {
73                         if(i->name=="set")
74                         {
75                                 error(layout, "Layout qualifier 'set' not allowed when targeting OpenGL");
76                                 continue;
77                         }
78
79                         if(variable)
80                         {
81                                 TypeDeclaration *type = variable->type_declaration;
82                                 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
83                                         type = basic->base_type;
84                                 bool uniform = (variable->interface=="uniform");
85                                 allowed = (scope==GLOBAL && uniform && dynamic_cast<ImageTypeDeclaration *>(type));
86                                 err_descr = (uniform ? "variable of non-opaque type" : "non-uniform variable");
87                         }
88                         else if(iface_block)
89                         {
90                                 allowed = (iface_block->interface=="uniform");
91                                 err_descr = "non-uniform interface block";
92                         }
93                 }
94                 else if(i->name=="constant_id")
95                 {
96                         allowed = (variable && scope==GLOBAL);
97                         if(allowed)
98                         {
99                                 if(!variable->constant)
100                                 {
101                                         allowed = false;
102                                         err_descr = "non-constant variable";
103                                 }
104                                 else
105                                 {
106                                         BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
107                                         if(!basic || basic->kind<BasicTypeDeclaration::BOOL || basic->kind>BasicTypeDeclaration::INT)
108                                         {
109                                                 allowed = false;
110                                                 err_descr = format("variable of type '%s'",
111                                                         (variable->type_declaration ? variable->type_declaration->name : variable->type));
112                                         }
113                                 }
114                         }
115                 }
116                 else if(i->name=="offset")
117                         allowed = (variable && scope==INTERFACE_BLOCK);
118                 else if(i->name=="points")
119                 {
120                         allowed = (stage->type==Stage::GEOMETRY && iface_layout && (iface_layout->interface=="in" || iface_layout->interface=="out"));
121                         value = false;
122                 }
123                 else if(i->name=="lines" || i->name=="lines_adjacency" || i->name=="triangles" || i->name=="triangles_adjacency")
124                 {
125                         allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
126                         value = false;
127                 }
128                 else if(i->name=="line_strip" || i->name=="triangle_strip")
129                 {
130                         allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
131                         value = false;
132                 }
133                 else if(i->name=="invocations")
134                         allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
135                 else if(i->name=="max_vertices")
136                         allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
137                 else if(i->name=="std140" || i->name=="std430")
138                 {
139                         allowed = (iface_block && !variable && iface_block->interface=="uniform");
140                         value = false;
141                 }
142
143                 if(!allowed)
144                 {
145                         if(err_descr.empty())
146                         {
147                                 if(variable)
148                                         err_descr = describe_variable(scope);
149                                 else if(iface_block)
150                                         err_descr = "interface block";
151                                 else if(iface_layout)
152                                         err_descr = format("interface '%s'", iface_layout->interface);
153                                 else
154                                         err_descr = "unknown declaration";
155                         }
156                         error(layout, format("Layout qualifier '%s' not allowed on %s", i->name, err_descr));
157                 }
158                 else if(value && !i->has_value)
159                         error(layout, format("Layout qualifier '%s' requires a value", i->name));
160                 else if(!value && i->has_value)
161                         error(layout, format("Layout qualifier '%s' does not allow a value", i->name));
162         }
163 }
164
165 void DeclarationValidator::visit(InterfaceLayout &layout)
166 {
167         SetForScope<InterfaceLayout *> set_layout(iface_layout, &layout);
168         TraversingVisitor::visit(layout);
169 }
170
171 void DeclarationValidator::visit(BasicTypeDeclaration &type)
172 {
173         BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
174         BasicTypeDeclaration::Kind base_kind = (basic_base ? basic_base->kind : BasicTypeDeclaration::VOID);
175
176         if(type.kind==BasicTypeDeclaration::VECTOR && (base_kind<BasicTypeDeclaration::BOOL || base_kind>BasicTypeDeclaration::FLOAT))
177                 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
178         else if(type.kind==BasicTypeDeclaration::MATRIX && base_kind!=BasicTypeDeclaration::VECTOR)
179                 error(type, format("Invalid base type '%s' for matrix type '%s'", type.base, type.name));
180         else if(type.kind==BasicTypeDeclaration::ARRAY && basic_base && base_kind==BasicTypeDeclaration::VOID)
181                 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
182 }
183
184 void DeclarationValidator::visit(ImageTypeDeclaration &type)
185 {
186         BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
187         if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
188                 base_kind = basic_base->kind;
189         if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
190                 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
191 }
192
193 void DeclarationValidator::visit(StructDeclaration &strct)
194 {
195         SetForScope<ScopeType> set_scope(scope, (scope!=INTERFACE_BLOCK ? STRUCT : scope));
196         TraversingVisitor::visit(strct);
197 }
198
199 void DeclarationValidator::visit(VariableDeclaration &var)
200 {
201         SetForScope<VariableDeclaration *> set_var(variable, &var);
202
203         const char *descr = describe_variable(scope);
204
205         if(var.layout)
206         {
207                 if(scope!=GLOBAL && scope!=INTERFACE_BLOCK)
208                         error(var, format("Layout qualifier not allowed on %s", descr));
209                 else
210                         var.layout->visit(*this);
211         }
212
213         if(var.constant)
214         {
215                 if(scope==STRUCT || scope==INTERFACE_BLOCK)
216                         error(var, format("Constant qualifier not allowed on %s", descr));
217         }
218
219         if(!var.interpolation.empty() || !var.sampling.empty())
220         {
221                 if(var.interface!="in" && stage->type==Stage::VERTEX)
222                         error(var, "Interpolation qualifier not allowed on vertex input");
223                 else if(var.interface!="out" && stage->type==Stage::FRAGMENT)
224                         error(var, "Interpolation qualifier not allowed on fragment output");
225                 else if((var.interface!="in" && var.interface!="out") || (scope==FUNCTION_PARAM || scope==FUNCTION))
226                         error(var, "Interpolation qualifier not allowed on non-interpolated variable");
227         }
228
229         if(!var.interface.empty())
230         {
231                 if(iface_block && var.interface!=iface_block->interface)
232                         error(var, format("Mismatched interface qualifier '%s' inside '%s' block", var.interface, iface_block->interface));
233                 else if(scope==STRUCT || scope==FUNCTION)
234                         error(var, format("Interface qualifier not allowed on %s", descr));
235         }
236
237         TypeDeclaration *type = var.type_declaration;
238         while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
239                 type = basic->base_type;
240         if(dynamic_cast<ImageTypeDeclaration *>(type))
241         {
242                 if(scope!=GLOBAL && scope!=FUNCTION_PARAM)
243                         error(var, format("Type '%s' not allowed on %s", type->name, descr));
244                 else if(scope==GLOBAL && var.interface!="uniform")
245                         error(var, format("Type '%s' only allowed with uniform interface", type->name));
246         }
247
248         if(var.init_expression)
249         {
250                 if(scope==GLOBAL && !var.constant)
251                         error(var, format("Initializer not allowed on non-constant %s", descr));
252                 else if(scope!=GLOBAL && scope!=FUNCTION)
253                         error(var, format("Initializer not allowed on %s", descr));
254                 else
255                         var.init_expression->visit(*this);
256         }
257 }
258
259 void DeclarationValidator::visit(InterfaceBlock &iface)
260 {
261         SetForScope<ScopeType> set_scope(scope, INTERFACE_BLOCK);
262         SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
263
264         if(stage->type==Stage::VERTEX && iface.interface=="in")
265                 error(iface, "Interface block not allowed on vertex shader input");
266         else if(stage->type==Stage::FRAGMENT && iface.interface=="out")
267                 error(iface, "Interface block not allowed on fragment shader output");
268
269         TraversingVisitor::visit(iface);
270         if(iface.struct_declaration)
271                 iface.struct_declaration->visit(*this);
272 }
273
274 void DeclarationValidator::visit(FunctionDeclaration &func)
275 {
276         SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
277         for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
278                 (*i)->visit(*this);
279         scope = FUNCTION;
280         func.body.visit(*this);
281 }
282
283
284 IdentifierValidator::IdentifierValidator():
285         anonymous_block(false)
286 { }
287
288 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
289 {
290         error(statement, format("Multiple definition of %s", name));
291         add_info(previous, "Previous definition is here");
292 }
293
294 Statement *IdentifierValidator::find_definition(const string &name)
295 {
296         BlockDeclarationMap *decls = &declarations[current_block];
297         BlockDeclarationMap::const_iterator i = decls->find(name);
298         if(i==decls->end() && anonymous_block)
299         {
300                 decls = &declarations[current_block->parent];
301                 i = decls->find(name);
302         }
303         return (i!=decls->end() ? i->second : 0);
304 }
305
306 void IdentifierValidator::check_definition(const string &name, Statement &statement)
307 {
308         if(Statement *previous = find_definition(name))
309                 multiple_definition(format("'%s'", name), statement, *previous);
310         else
311                 record_definition(name, statement);
312 }
313
314 void IdentifierValidator::record_definition(const string &name, Statement &statement)
315 {
316         declarations[current_block].insert(make_pair(name, &statement));
317         if(anonymous_block)
318                 declarations[current_block->parent].insert(make_pair(name, &statement));
319 }
320
321 void IdentifierValidator::visit(TypeDeclaration &type)
322 {
323         check_definition(type.name, type);
324 }
325
326 void IdentifierValidator::visit(StructDeclaration &strct)
327 {
328         check_definition(strct.name, strct);
329         TraversingVisitor::visit(strct);
330 }
331
332 void IdentifierValidator::visit(VariableDeclaration &var)
333 {
334         check_definition(var.name, var);
335         TraversingVisitor::visit(var);
336 }
337
338 void IdentifierValidator::visit(InterfaceBlock &iface)
339 {
340         string key = iface.interface+iface.block_name;
341         map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
342         if(i!=interface_blocks.end())
343                 multiple_definition(format("interface block '%s %s'", iface.interface, iface.block_name), iface, *i->second);
344         else
345                 interface_blocks.insert(make_pair(key, &iface));
346
347         if(Statement *previous = find_definition(iface.block_name))
348         {
349                 if(!dynamic_cast<InterfaceBlock *>(previous))
350                         multiple_definition(format("'%s'", iface.block_name), iface, *previous);
351         }
352         else
353                 record_definition(iface.block_name, iface);
354
355         if(!iface.instance_name.empty())
356                 check_definition(iface.instance_name, iface);
357
358         if(iface.instance_name.empty() && iface.struct_declaration)
359         {
360                 // Inject anonymous interface block members into the global scope
361                 const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
362                 for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
363                         check_definition(j->first, *j->second);
364         }
365 }
366
367 void IdentifierValidator::visit(FunctionDeclaration &func)
368 {
369         string key = func.name+func.signature;
370         map<string, FunctionDeclaration *>::const_iterator i = overloaded_functions.find(key);
371         if(i==overloaded_functions.end())
372                 overloaded_functions.insert(make_pair(key, &func));
373         else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
374         {
375                 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
376                 if(i->second->return_type_declaration)
377                         add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
378         }
379
380         if(Statement *previous = find_definition(func.name))
381         {
382                 if(!dynamic_cast<FunctionDeclaration *>(previous))
383                         multiple_definition(format("'%s'", func.name), func, *previous);
384         }
385         else
386                 record_definition(func.name, func);
387
388         if(func.definition==&func)
389                 check_definition(func.name+func.signature, func);
390
391         TraversingVisitor::visit(func);
392 }
393
394
395 void ReferenceValidator::visit(BasicTypeDeclaration &type)
396 {
397         if(!type.base.empty() && !type.base_type)
398                 error(type, format("Use of undeclared type '%s'", type.base));
399 }
400
401 void ReferenceValidator::visit(ImageTypeDeclaration &type)
402 {
403         if(!type.base.empty() && !type.base_type)
404                 error(type, format("Use of undeclared type '%s'", type.base));
405 }
406
407 void ReferenceValidator::visit(VariableReference &var)
408 {
409         if(!var.declaration)
410                 error(var, format("Use of undeclared variable '%s'", var.name));
411         else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
412                 error(var, format("Use of unlinked input variable '%s'", var.name));
413 }
414
415 void ReferenceValidator::visit(MemberAccess &memacc)
416 {
417         if(memacc.left->type && !memacc.declaration)
418                 error(memacc, format("Use of undeclared member '%s'", memacc.member));
419         TraversingVisitor::visit(memacc);
420 }
421
422 void ReferenceValidator::visit(InterfaceBlockReference &iface)
423 {
424         /* An interface block reference without a declaration should be impossible
425         since references are generated based on existing declarations. */
426         if(!iface.declaration)
427                 error(iface, format("Use of undeclared interface block '%s'", iface.name));
428         else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
429                 error(iface, format("Use of unlinked input block '%s'", iface.name));
430 }
431
432 void ReferenceValidator::visit(FunctionCall &call)
433 {
434         if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
435         {
436                 bool have_declaration = call.constructor;
437                 if(!call.constructor)
438                 {
439                         map<string, FunctionDeclaration *>::iterator i = stage->functions.lower_bound(call.name);
440                         have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
441                 }
442
443                 if(have_declaration)
444                 {
445                         bool valid_types = true;
446                         string signature;
447                         for(NodeArray<Expression>::const_iterator j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
448                         {
449                                 if((*j)->type)
450                                         append(signature, ", ", (*j)->type->name);
451                                 else
452                                         valid_types = false;
453                         }
454
455                         if(valid_types)
456                                 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
457                 }
458                 else
459                         error(call, format("Call to undeclared function '%s'", call.name));
460         }
461         TraversingVisitor::visit(call);
462 }
463
464 void ReferenceValidator::visit(VariableDeclaration &var)
465 {
466         if(!var.type_declaration)
467                 error(var, format("Use of undeclared type '%s'", var.type));
468         TraversingVisitor::visit(var);
469 }
470
471 void ReferenceValidator::visit(InterfaceBlock &iface)
472 {
473         if(!iface.struct_declaration)
474                 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
475         TraversingVisitor::visit(iface);
476 }
477
478 void ReferenceValidator::visit(FunctionDeclaration &func)
479 {
480         if(!func.return_type_declaration)
481                 error(func, format("Use of undeclared type '%s'", func.return_type));
482         TraversingVisitor::visit(func);
483 }
484
485
486 ExpressionValidator::ExpressionValidator():
487         current_function(0)
488 { }
489
490 void ExpressionValidator::visit(Swizzle &swizzle)
491 {
492         unsigned size = 0;
493         if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
494         {
495                 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
496                         size = 1;
497                 else if(basic->kind==BasicTypeDeclaration::VECTOR)
498                         size = basic->size;
499         }
500
501         if(size)
502         {
503                 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
504                 int flavour = -1;
505                 for(unsigned i=0; i<swizzle.count; ++i)
506                 {
507                         unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
508                         if(flavour==-1)
509                                 flavour = component_flavour;
510                         else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
511                         {
512                                 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
513                                         swizzle.component_group[i], swizzle.component_group[0]));
514                                 flavour = -2;
515                         }
516
517                         if(swizzle.components[i]>=size)
518                                 error(swizzle, format("Access to component '%c' which is not present in '%s'",
519                                         swizzle.component_group[i], swizzle.left->type->name));
520                 }
521         }
522         else if(swizzle.left->type)
523                 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
524
525         TraversingVisitor::visit(swizzle);
526 }
527
528 void ExpressionValidator::visit(UnaryExpression &unary)
529 {
530         if(unary.expression->type)
531         {
532                 if(!unary.type)
533                         error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
534                 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
535                         error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
536         }
537         TraversingVisitor::visit(unary);
538 }
539
540 void ExpressionValidator::visit(BinaryExpression &binary)
541 {
542         if(!binary.type && binary.left->type && binary.right->type)
543         {
544                 if(binary.oper->token[0]=='[')
545                         error(binary, format("Can't index element of '%s' with '%s'",
546                                 binary.left->type->name, binary.right->type->name));
547                 else
548                         error(binary, format("No matching operator '%s' found for '%s' and '%s'",
549                                 binary.oper->token, binary.left->type->name, binary.right->type->name));
550         }
551         TraversingVisitor::visit(binary);
552 }
553
554 void ExpressionValidator::visit(Assignment &assign)
555 {
556         if(assign.left->type)
557         {
558                 if(!assign.left->lvalue)
559                         error(assign, "Target of assignment is not an lvalue");
560                 if(assign.right->type)
561                 {
562                         if(assign.oper->token[0]!='=')
563                         {
564                                 if(!assign.type)
565                                         error(assign, format("No matching operator '%s' found for '%s' and '%s'",
566                                                 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
567                         }
568                         else if(assign.left->type!=assign.right->type)
569                                 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
570                                         assign.left->type->name, assign.right->type->name));
571                 }
572         }
573         TraversingVisitor::visit(assign);
574 }
575
576 void ExpressionValidator::visit(TernaryExpression &ternary)
577 {
578         if(ternary.condition->type)
579         {
580                 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
581                 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
582                         error(ternary, "Ternary operator condition is not a boolean");
583                 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
584                         error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
585                                 ternary.true_expr->type->name, ternary.false_expr->type->name));
586         }
587         TraversingVisitor::visit(ternary);
588 }
589
590 void ExpressionValidator::visit(VariableDeclaration &var)
591 {
592         if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
593                 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
594                         var.type_declaration->name, var.init_expression->type->name));
595         TraversingVisitor::visit(var);
596 }
597
598 void ExpressionValidator::visit(FunctionDeclaration &func)
599 {
600         SetForScope<FunctionDeclaration *> set_func(current_function, &func);
601         TraversingVisitor::visit(func);
602 }
603
604 void ExpressionValidator::visit(Return &ret)
605 {
606         if(current_function && current_function->return_type_declaration)
607         {
608                 TypeDeclaration *return_type = current_function->return_type_declaration;
609                 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
610                 if(ret.expression)
611                 {
612                         if(ret.expression->type && ret.expression->type!=return_type)
613                                 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
614                                         ret.expression->type->name, return_type->name));
615                 }
616                 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
617                         error(ret, "Return statement without an expression in a function not returning 'void'");
618         }
619
620         TraversingVisitor::visit(ret);
621 }
622
623
624 int StageInterfaceValidator::get_location(const Layout &layout)
625 {
626         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
627                 if(i->name=="location")
628                         return i->value;
629         return -1;
630 }
631
632 void StageInterfaceValidator::visit(VariableDeclaration &var)
633 {
634         int location = (var.layout ? get_location(*var.layout) : -1);
635         if(var.interface=="in" && var.linked_declaration)
636         {
637                 const Layout *linked_layout = var.linked_declaration->layout.get();
638                 int linked_location = (linked_layout ? get_location(*linked_layout) : -1);
639                 if(linked_location!=location)
640                 {
641                         error(var, format("Mismatched location %d for 'in %s'", location, var.name));
642                         add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
643                                 var.linked_declaration->name, linked_location));
644                 }
645                 if(var.type_declaration && var.linked_declaration->type_declaration)
646                 {
647                         const TypeDeclaration *type = var.type_declaration;
648                         if(stage->type==Stage::GEOMETRY)
649                         {
650                                 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
651                                         if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
652                                                 type = basic->base_type;
653                         }
654                         if(!is_same_type(*type, *var.linked_declaration->type_declaration))
655                         {
656                                 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
657                                 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
658                                         var.linked_declaration->name, var.linked_declaration->type_declaration->name));
659                         }
660                 }
661         }
662
663         if(location>=0 && !var.interface.empty())
664         {
665                 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
666
667                 unsigned loc_count = LocationCounter().apply(var);
668                 for(unsigned i=0; i<loc_count; ++i)
669                 {
670                         map<unsigned, VariableDeclaration *>::const_iterator j = used.find(location+i);
671                         if(j!=used.end())
672                         {
673                                 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
674                                 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
675                         }
676                         else
677                                 used[location+i] = &var;
678                 }
679         }
680 }
681
682
683 void GlobalInterfaceValidator::apply(Module &module)
684 {
685         for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
686         {
687                 stage = &*i;
688                 i->content.visit(*this);
689         }
690 }
691
692 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
693 {
694         map<std::string, const Uniform *>::const_iterator i = used_names.find(uni.name);
695         if(i!=used_names.end())
696         {
697                 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
698                 {
699                         error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
700                         add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
701                 }
702                 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
703                 {
704                         error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
705                         add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
706                 }
707                 if(uni.type && i->second->type && !is_same_type(*uni.type, *i->second->type))
708                 {
709                         string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
710                                 "structure" : format("type '%s'", uni.type->name));
711                         error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
712
713                         string message = "Previously declared here";
714                         if(!dynamic_cast<const StructDeclaration *>(i->second->type))
715                                 message += format(" with type '%s'", i->second->type->name);
716                         add_info(*i->second->node, message);
717                 }
718         }
719         else
720                 used_names.insert(make_pair(uni.name, &uni));
721
722         if(uni.location>=0)
723         {
724                 map<unsigned, const Uniform *>::const_iterator j = used_locations.find(uni.location);
725                 if(j!=used_locations.end())
726                 {
727                         if(j->second->name!=uni.name)
728                         {
729                                 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
730                                 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
731                         }
732                 }
733                 else
734                 {
735                         for(unsigned k=0; k<uni.loc_count; ++k)
736                                 used_locations.insert(make_pair(uni.location+k, &uni));
737                 }
738         }
739
740         if(uni.bind_point>=0)
741         {
742                 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
743                 map<unsigned, const Uniform *>::const_iterator j = used.find(uni.bind_point);
744                 if(j!=used.end())
745                 {
746                         if(j->second->name!=uni.name)
747                         {
748                                 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
749                                 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
750                         }
751                 }
752                 else
753                         used.insert(make_pair(uni.bind_point, &uni));
754         }
755 }
756
757 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
758 {
759         if(var.interface=="uniform")
760         {
761                 Uniform uni;
762                 uni.node = &var;
763                 uni.type = var.type_declaration;
764                 uni.name = var.name;
765                 if(var.layout)
766                 {
767                         uni.location = get_layout_value(*var.layout, "location");
768                         uni.loc_count = LocationCounter().apply(var);
769                         uni.desc_set = get_layout_value(*var.layout, "set", 0);
770                         uni.bind_point = get_layout_value(*var.layout, "binding");
771                 }
772
773                 uniforms.push_back(uni);
774                 check_uniform(uniforms.back());
775         }
776 }
777
778 void GlobalInterfaceValidator::visit(InterfaceBlock &iface)
779 {
780         if(iface.interface=="uniform")
781         {
782                 Uniform uni;
783                 uni.node = &iface;
784                 uni.type = iface.struct_declaration;
785                 uni.name = iface.block_name;
786                 if(iface.layout)
787                 {
788                         uni.desc_set = get_layout_value(*iface.layout, "set", 0);
789                         uni.bind_point = get_layout_value(*iface.layout, "binding");
790                 }
791
792                 uniforms.push_back(uni);
793                 check_uniform(uniforms.back());
794         }
795 }
796
797 } // namespace SL
798 } // namespace GL
799 } // namespace Msp