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