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