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