]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/validate.cpp
f43217ce6314c24a00d1e5885c6f405e129fba38
[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, (strct.block_name.empty() ? STRUCT : INTERFACE_BLOCK));
216         SetForScope<VariableDeclaration *> set_iface(iface_block, strct.block_declaration);
217         TraversingVisitor::visit(strct);
218 }
219
220 void DeclarationValidator::visit(VariableDeclaration &var)
221 {
222         SetForScope<VariableDeclaration *> set_var((var.block_declaration ? iface_block : variable), &var);
223
224         const char *descr = describe_variable(scope);
225
226         if(var.layout)
227         {
228                 if(scope!=GLOBAL && scope!=INTERFACE_BLOCK)
229                         error(var, format("Layout qualifier not allowed on %s", descr));
230                 else
231                         var.layout->visit(*this);
232         }
233
234         if(var.constant)
235         {
236                 if(scope==STRUCT || scope==INTERFACE_BLOCK)
237                         error(var, format("Constant qualifier not allowed on %s", descr));
238                 if(!var.init_expression)
239                         error(var, "Constant variable must have an initializer");
240         }
241
242         if(!var.interpolation.empty() || !var.sampling.empty())
243         {
244                 if(var.interface!="in" && stage->type==Stage::VERTEX)
245                         error(var, "Interpolation qualifier not allowed on vertex input");
246                 else if(var.interface!="out" && stage->type==Stage::FRAGMENT)
247                         error(var, "Interpolation qualifier not allowed on fragment output");
248                 else if((var.interface!="in" && var.interface!="out") || (scope==FUNCTION_PARAM || scope==FUNCTION))
249                         error(var, "Interpolation qualifier not allowed on non-interpolated variable");
250         }
251
252         if(!var.interface.empty())
253         {
254                 if(iface_block && var.interface!=iface_block->interface)
255                         error(var, format("Mismatched interface qualifier '%s' inside '%s' block", var.interface, iface_block->interface));
256                 else if(scope==STRUCT || scope==FUNCTION)
257                         error(var, format("Interface qualifier not allowed on %s", descr));
258                 else if(scope==GLOBAL && var.interface=="uniform" && !var.block_declaration && features.target_api==VULKAN)
259                 {
260                         if(!dynamic_cast<const ImageTypeDeclaration *>(get_ultimate_base_type(var.type_declaration)))
261                                 error(var, "Interface qualifier 'uniform' not allowed on non-opaque variable in global scope");
262                 }
263         }
264
265         TypeDeclaration *type = var.type_declaration;
266         BasicTypeDeclaration::Kind kind = BasicTypeDeclaration::ALIAS;
267         while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
268         {
269                 kind = basic->kind;
270                 type = basic->base_type;
271         }
272         if(dynamic_cast<ImageTypeDeclaration *>(type))
273         {
274                 if(scope!=GLOBAL && scope!=FUNCTION_PARAM)
275                         error(var, format("Type '%s' not allowed on %s", type->name, descr));
276                 else if(scope==GLOBAL && var.interface!="uniform")
277                         error(var, format("Type '%s' only allowed with uniform interface", type->name));
278         }
279         else if(var.block_declaration)
280         {
281                 if(stage->type==Stage::VERTEX && var.interface=="in")
282                         error(var, "Interface block not allowed on vertex shader input");
283                 else if(stage->type==Stage::FRAGMENT && var.interface=="out")
284                         error(var, "Interface block not allowed on fragment shader output");
285         }
286         else if(kind==BasicTypeDeclaration::VOID)
287                 error(var, "Type 'void' not allowed on variable");
288         else if(kind==BasicTypeDeclaration::BOOL && var.source!=BUILTIN_SOURCE)
289         {
290                 if(scope==INTERFACE_BLOCK)
291                         error(var, "Type 'bool' not allowed in an interface block");
292                 else if(!var.interface.empty())
293                         error(var, "Type 'bool' not allowed on interface variable");
294         }
295
296         if(var.init_expression)
297         {
298                 if(scope==GLOBAL && !var.constant)
299                         error(var, format("Initializer not allowed on non-constant %s", descr));
300                 else if(scope!=GLOBAL && scope!=FUNCTION)
301                         error(var, format("Initializer not allowed on %s", descr));
302                 else
303                         var.init_expression->visit(*this);
304         }
305 }
306
307 void DeclarationValidator::visit(FunctionDeclaration &func)
308 {
309         SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
310         for(const RefPtr<VariableDeclaration> &p: func.parameters)
311                 p->visit(*this);
312         scope = FUNCTION;
313         func.body.visit(*this);
314 }
315
316
317 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
318 {
319         error(statement, format("Multiple definition of %s", name));
320         add_info(previous, "Previous definition is here");
321 }
322
323 Statement *IdentifierValidator::find_definition(const string &name)
324 {
325         BlockDeclarationMap *decls = &declarations[current_block];
326         auto i = decls->find(name);
327         if(i==decls->end() && anonymous_block)
328         {
329                 decls = &declarations[current_block->parent];
330                 i = decls->find(name);
331         }
332         return (i!=decls->end() ? i->second : 0);
333 }
334
335 void IdentifierValidator::check_definition(const string &name, Statement &statement)
336 {
337         if(Statement *previous = find_definition(name))
338                 multiple_definition(format("'%s'", name), statement, *previous);
339         else
340                 record_definition(name, statement);
341 }
342
343 void IdentifierValidator::record_definition(const string &name, Statement &statement)
344 {
345         declarations[current_block].insert(make_pair(name, &statement));
346         if(anonymous_block)
347                 declarations[current_block->parent].insert(make_pair(name, &statement));
348 }
349
350 void IdentifierValidator::visit(TypeDeclaration &type)
351 {
352         if(type.source!=INTERNAL_SOURCE)
353                 check_definition(type.name, type);
354 }
355
356 void IdentifierValidator::visit(StructDeclaration &strct)
357 {
358         if(strct.block_name.empty())
359                 check_definition(strct.name, strct);
360         TraversingVisitor::visit(strct);
361 }
362
363 void IdentifierValidator::visit(VariableDeclaration &var)
364 {
365         if(var.block_declaration)
366         {
367                 string key = format("%s %s", var.interface, var.block_declaration->block_name);
368                 auto i = interface_blocks.find(key);
369                 if(i!=interface_blocks.end())
370                         multiple_definition(format("interface block '%s %s'", var.interface, var.block_declaration->block_name), var, *i->second);
371                 else
372                         interface_blocks.insert(make_pair(key, &var));
373
374                 if(Statement *previous = find_definition(var.block_declaration->block_name))
375                 {
376                         const VariableDeclaration *prev_var = dynamic_cast<const VariableDeclaration *>(previous);
377                         if(!prev_var || !prev_var->block_declaration)
378                                 multiple_definition(format("'%s'", var.block_declaration->block_name), var, *previous);
379                 }
380                 else
381                         record_definition(var.block_declaration->block_name, var);
382
383                 if(var.name.find(' ')!=string::npos)
384                 {
385                         // Inject anonymous interface block members into the global scope
386                         for(const auto &kvp: var.block_declaration->members.variables)
387                                 check_definition(kvp.first, *kvp.second);
388                 }
389         }
390
391         if(var.name.find(' ')==string::npos)
392                 check_definition(var.name, var);
393
394         TraversingVisitor::visit(var);
395 }
396
397 void IdentifierValidator::visit(FunctionDeclaration &func)
398 {
399         string key = func.name+func.signature;
400         auto i = overloaded_functions.find(key);
401         if(i==overloaded_functions.end())
402                 overloaded_functions.insert(make_pair(key, &func));
403         else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
404         {
405                 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
406                 if(i->second->return_type_declaration)
407                         add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
408         }
409
410         if(Statement *previous = find_definition(func.name))
411         {
412                 if(!dynamic_cast<FunctionDeclaration *>(previous))
413                         multiple_definition(format("'%s'", func.name), func, *previous);
414         }
415         else
416                 record_definition(func.name, func);
417
418         if(func.definition==&func)
419                 check_definition(func.name+func.signature, func);
420
421         TraversingVisitor::visit(func);
422 }
423
424
425 void ReferenceValidator::visit(BasicTypeDeclaration &type)
426 {
427         if(!type.base.empty() && !type.base_type)
428                 error(type, format("Use of undeclared type '%s'", type.base));
429 }
430
431 void ReferenceValidator::visit(ImageTypeDeclaration &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(VariableReference &var)
438 {
439         if(!var.declaration)
440                 error(var, format("Use of undeclared variable '%s'", var.name));
441         else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
442                 error(var, format("Use of unlinked input %s '%s'", (var.declaration->block_declaration ? "block" : "variable"), var.name));
443 }
444
445 void ReferenceValidator::visit(MemberAccess &memacc)
446 {
447         if(memacc.left->type && !memacc.declaration)
448                 error(memacc, format("Use of undeclared member '%s'", memacc.member));
449         TraversingVisitor::visit(memacc);
450 }
451
452 void ReferenceValidator::visit(FunctionCall &call)
453 {
454         if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
455         {
456                 bool have_declaration = call.constructor;
457                 if(!call.constructor)
458                 {
459                         auto i = stage->functions.lower_bound(call.name);
460                         have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
461                 }
462
463                 if(have_declaration)
464                 {
465                         bool valid_types = true;
466                         string signature;
467                         for(auto j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
468                         {
469                                 if((*j)->type)
470                                         append(signature, ", ", (*j)->type->name);
471                                 else
472                                         valid_types = false;
473                         }
474
475                         if(valid_types)
476                                 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
477                 }
478                 else
479                         error(call, format("Call to undeclared function '%s'", call.name));
480         }
481         TraversingVisitor::visit(call);
482 }
483
484 void ReferenceValidator::visit(VariableDeclaration &var)
485 {
486         if(!var.type_declaration)
487                 error(var, format("Use of undeclared type '%s'", var.type));
488         TraversingVisitor::visit(var);
489 }
490
491 void ReferenceValidator::visit(FunctionDeclaration &func)
492 {
493         if(!func.return_type_declaration)
494                 error(func, format("Use of undeclared type '%s'", func.return_type));
495         TraversingVisitor::visit(func);
496 }
497
498
499 void ExpressionValidator::visit(VariableReference &var)
500 {
501         if(var.declaration && constant_expression)
502         {
503                 if(!var.declaration->constant)
504                 {
505                         const char *kind = (var.declaration->block_declaration ? "interface block" : "non-constant variable");
506                         error(var, format("Reference to %s '%s' in a constant expression", kind, var.name));
507                 }
508                 else if(var.declaration->layout && constant_expression==FIXED_CONSTANT)
509                 {
510                         if(has_layout_qualifier(var.declaration->layout.get(), "constant_id"))
511                                 error(var, format("Reference to specialization constant '%s' in a fixed constant expression", var.name));
512                 }
513         }
514 }
515
516 void ExpressionValidator::visit(Swizzle &swizzle)
517 {
518         unsigned size = 0;
519         if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
520         {
521                 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
522                         size = 1;
523                 else if(basic->kind==BasicTypeDeclaration::VECTOR)
524                         size = basic->size;
525         }
526
527         if(size)
528         {
529                 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
530                 int flavour = -1;
531                 for(unsigned i=0; i<swizzle.count; ++i)
532                 {
533                         unsigned component_flavour = (std::find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
534                         if(flavour==-1)
535                                 flavour = component_flavour;
536                         else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
537                         {
538                                 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
539                                         swizzle.component_group[i], swizzle.component_group[0]));
540                                 flavour = -2;
541                         }
542
543                         if(swizzle.components[i]>=size)
544                                 error(swizzle, format("Access to component '%c' which is not present in '%s'",
545                                         swizzle.component_group[i], swizzle.left->type->name));
546                 }
547         }
548         else if(swizzle.left->type)
549                 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
550
551         TraversingVisitor::visit(swizzle);
552 }
553
554 void ExpressionValidator::visit(UnaryExpression &unary)
555 {
556         if(unary.expression->type)
557         {
558                 if(!unary.type)
559                         error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
560                 else if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
561                 {
562                         if(constant_expression)
563                                 error(unary, format("Use of '%s' in a constant expression", unary.oper->token));
564                         else if(!unary.expression->lvalue)
565                                 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
566                 }
567         }
568         TraversingVisitor::visit(unary);
569 }
570
571 void ExpressionValidator::visit(BinaryExpression &binary)
572 {
573         if(!binary.type && binary.left->type && binary.right->type)
574         {
575                 if(binary.oper->token[0]=='[')
576                         error(binary, format("Can't index element of '%s' with '%s'",
577                                 binary.left->type->name, binary.right->type->name));
578                 else
579                         error(binary, format("No matching operator '%s' found for '%s' and '%s'",
580                                 binary.oper->token, binary.left->type->name, binary.right->type->name));
581         }
582         TraversingVisitor::visit(binary);
583 }
584
585 void ExpressionValidator::visit(Assignment &assign)
586 {
587         if(assign.left->type)
588         {
589                 if(constant_expression)
590                         error(assign, "Assignment in constant expression");
591                 else if(!assign.left->lvalue)
592                         error(assign, "Target of assignment is not an lvalue");
593                 if(assign.right->type)
594                 {
595                         if(assign.oper->token[0]!='=')
596                         {
597                                 if(!assign.type)
598                                         error(assign, format("No matching operator '%s' found for '%s' and '%s'",
599                                                 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
600                         }
601                         else if(assign.left->type!=assign.right->type)
602                                 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
603                                         assign.left->type->name, assign.right->type->name));
604                 }
605         }
606         TraversingVisitor::visit(assign);
607 }
608
609 void ExpressionValidator::visit(TernaryExpression &ternary)
610 {
611         if(ternary.condition->type)
612         {
613                 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
614                 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
615                         error(ternary, "Ternary operator condition is not a boolean");
616                 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
617                         error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
618                                 ternary.true_expr->type->name, ternary.false_expr->type->name));
619         }
620         TraversingVisitor::visit(ternary);
621 }
622
623 void ExpressionValidator::visit(StructDeclaration &strct)
624 {
625         SetFlag set_struct(in_struct);
626         TraversingVisitor::visit(strct);
627 }
628
629 void ExpressionValidator::visit(VariableDeclaration &var)
630 {
631         if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
632                 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
633                         var.type_declaration->name, var.init_expression->type->name));
634
635         if(var.layout)
636                 var.layout->visit(*this);
637         if(var.init_expression)
638         {
639                 ConstantKind const_kind = (!var.constant ? NOT_CONSTANT :
640                         has_layout_qualifier(var.layout.get(), "constant_id") ? FIXED_CONSTANT : SPEC_CONSTANT);
641
642                 SetForScope<ConstantKind> set_const(constant_expression, const_kind);
643                 TraversingVisitor::visit(var.init_expression);
644         }
645         if(var.array_size)
646         {
647                 SetForScope<ConstantKind> set_const(constant_expression, (in_struct || !var.interface.empty() ? FIXED_CONSTANT : SPEC_CONSTANT));
648                 TraversingVisitor::visit(var.array_size);
649         }
650 }
651
652 void ExpressionValidator::visit(FunctionDeclaration &func)
653 {
654         SetForScope<FunctionDeclaration *> set_func(current_function, &func);
655         TraversingVisitor::visit(func);
656 }
657
658 void ExpressionValidator::visit(Conditional &cond)
659 {
660         if(cond.condition->type)
661         {
662                 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
663                 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
664                         error(cond, "Condition is not a boolean");
665         }
666         TraversingVisitor::visit(cond);
667 }
668
669 void ExpressionValidator::visit(Iteration &iter)
670 {
671         if(iter.condition->type)
672         {
673                 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
674                 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
675                         error(iter, "Loop condition is not a boolean");
676         }
677         TraversingVisitor::visit(iter);
678 }
679
680 void ExpressionValidator::visit(Return &ret)
681 {
682         if(current_function && current_function->return_type_declaration)
683         {
684                 TypeDeclaration *return_type = current_function->return_type_declaration;
685                 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
686                 if(ret.expression)
687                 {
688                         if(ret.expression->type && ret.expression->type!=return_type)
689                                 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
690                                         ret.expression->type->name, return_type->name));
691                 }
692                 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
693                         error(ret, "Return statement without an expression in a function not returning 'void'");
694         }
695
696         TraversingVisitor::visit(ret);
697 }
698
699
700 void FlowControlValidator::visit(Block &block)
701 {
702         for(const RefPtr<Statement> &s: block.body)
703         {
704                 if(!reachable)
705                 {
706                         diagnose(*s, Diagnostic::WARN, "Unreachable code detected");
707                         break;
708                 }
709                 s->visit(*this);
710         }
711 }
712
713 void FlowControlValidator::visit(FunctionDeclaration &func)
714 {
715         func.body.visit(*this);
716
717         if(func.definition==&func && func.return_type_declaration)
718         {
719                 const BasicTypeDeclaration *basic_ret = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
720                 if(reachable && (!basic_ret || basic_ret->kind!=BasicTypeDeclaration::VOID))
721                         error(func, "Missing return statement at the end of a function not returning 'void'");
722         }
723         reachable = true;
724 }
725
726 void FlowControlValidator::visit(Conditional &cond)
727 {
728         cond.body.visit(*this);
729         bool reachable_if_true = reachable;
730         reachable = true;
731         cond.else_body.visit(*this);
732         reachable |= reachable_if_true;
733 }
734
735 void FlowControlValidator::visit(Iteration &iter)
736 {
737         iter.body.visit(*this);
738         reachable = true;
739 }
740
741
742 void StageInterfaceValidator::visit(VariableDeclaration &var)
743 {
744         int location = get_layout_value(var.layout.get(), "location");
745         if(var.interface=="in" && var.linked_declaration)
746         {
747                 const Layout *linked_layout = var.linked_declaration->layout.get();
748                 int linked_location = get_layout_value(linked_layout, "location");
749                 if(linked_location!=location)
750                 {
751                         error(var, format("Mismatched location %d for 'in %s'", location, var.name));
752                         add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
753                                 var.linked_declaration->name, linked_location));
754                 }
755                 if(var.type_declaration && var.linked_declaration->type_declaration)
756                 {
757                         TypeDeclaration *type = var.type_declaration;
758                         if(stage->type==Stage::GEOMETRY)
759                         {
760                                 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
761                                         if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
762                                                 type = basic->base_type;
763                         }
764                         if(!TypeComparer().apply(*type, *var.linked_declaration->type_declaration))
765                         {
766                                 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
767                                 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
768                                         var.linked_declaration->name, var.linked_declaration->type_declaration->name));
769                         }
770                 }
771         }
772
773         if(location>=0 && !var.interface.empty())
774         {
775                 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
776
777                 unsigned loc_count = LocationCounter().apply(var);
778                 for(unsigned i=0; i<loc_count; ++i)
779                 {
780                         auto j = used.find(location+i);
781                         if(j!=used.end())
782                         {
783                                 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
784                                 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
785                         }
786                         else
787                                 used[location+i] = &var;
788                 }
789         }
790 }
791
792
793 void GlobalInterfaceValidator::apply(Module &module)
794 {
795         for(Stage &s: module.stages)
796         {
797                 stage = &s;
798                 s.content.visit(*this);
799         }
800 }
801
802 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
803 {
804         auto i = used_names.find(uni.name);
805         if(i!=used_names.end())
806         {
807                 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
808                 {
809                         error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
810                         add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
811                 }
812                 if(i->second->desc_set!=uni.desc_set)
813                 {
814                         error(*uni.node, format("Mismatched descriptor set %d for uniform '%s'", uni.desc_set, uni.name));
815                         add_info(*i->second->node, format("Previously declared here with descriptor set %d", i->second->desc_set));
816                 }
817                 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
818                 {
819                         error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
820                         add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
821                 }
822                 if(uni.type && i->second->type && !TypeComparer().apply(*uni.type, *i->second->type))
823                 {
824                         string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
825                                 "structure" : format("type '%s'", uni.type->name));
826                         error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
827
828                         string message = "Previously declared here";
829                         if(!dynamic_cast<const StructDeclaration *>(i->second->type))
830                                 message += format(" with type '%s'", i->second->type->name);
831                         add_info(*i->second->node, message);
832                 }
833         }
834         else
835                 used_names.insert(make_pair(uni.name, &uni));
836
837         if(uni.location>=0)
838         {
839                 auto j = used_locations.find(uni.location);
840                 if(j!=used_locations.end())
841                 {
842                         if(j->second->name!=uni.name)
843                         {
844                                 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
845                                 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
846                         }
847                 }
848                 else
849                 {
850                         for(unsigned k=0; k<uni.loc_count; ++k)
851                                 used_locations.insert(make_pair(uni.location+k, &uni));
852                 }
853         }
854
855         if(uni.bind_point>=0)
856         {
857                 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
858                 auto j = used.find(uni.bind_point);
859                 if(j!=used.end())
860                 {
861                         if(j->second->name!=uni.name)
862                         {
863                                 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
864                                 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
865                         }
866                 }
867                 else
868                         used.insert(make_pair(uni.bind_point, &uni));
869         }
870 }
871
872 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
873 {
874         if(var.interface=="uniform")
875         {
876                 Uniform uni;
877                 uni.node = &var;
878                 uni.type = var.type_declaration;
879                 uni.name = (var.block_declaration ? var.block_declaration->block_name : var.name);
880                 if(var.layout)
881                 {
882                         if(!var.block_declaration)
883                         {
884                                 uni.location = get_layout_value(var.layout.get(), "location");
885                                 uni.loc_count = LocationCounter().apply(var);
886                         }
887                         uni.desc_set = get_layout_value(var.layout.get(), "set", 0);
888                         uni.bind_point = get_layout_value(var.layout.get(), "binding");
889                 }
890
891                 uniforms.push_back(uni);
892                 check_uniform(uniforms.back());
893         }
894 }
895
896 } // namespace SL
897 } // namespace GL
898 } // namespace Msp