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