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