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