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