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