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