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