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