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