2 #include <msp/core/algorithm.h>
3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/utils.h>
15 void Validator::diagnose(Node &node, Node &provoking_node, Diagnostic::Severity severity, const string &message)
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);
26 last_provoker = &provoking_node;
29 void Validator::add_info(Node &node, const string &message)
32 throw logic_error("Tried to add info without a previous provoker");
33 diagnose(node, *last_provoker, Diagnostic::INFO, message);
37 const char *DeclarationValidator::describe_variable(ScopeType scope)
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";
50 void DeclarationValidator::visit(Layout &layout)
52 bool push_constant = false;
54 for(const Layout::Qualifier &q: layout.qualifiers)
59 if(q.name=="location")
60 allowed = (variable && scope==GLOBAL);
61 else if(q.name=="binding" || q.name=="set")
67 error(layout, "Layout qualifier 'set' not allowed when targeting OpenGL");
73 TypeDeclaration *type = variable->type_declaration;
74 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
75 type = basic->base_type;
76 bool uniform = (variable->interface=="uniform");
77 allowed = (scope==GLOBAL && uniform && dynamic_cast<ImageTypeDeclaration *>(type));
78 err_descr = (uniform ? "variable of non-opaque type" : "non-uniform variable");
82 allowed = (iface_block->interface=="uniform");
83 err_descr = "non-uniform interface block";
86 else if(q.name=="constant_id")
88 allowed = (variable && scope==GLOBAL);
91 if(!variable->constant)
94 err_descr = "non-constant variable";
98 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
99 if(!basic || basic->kind<BasicTypeDeclaration::BOOL || basic->kind>BasicTypeDeclaration::INT)
102 err_descr = format("variable of type '%s'",
103 (variable->type_declaration ? variable->type_declaration->name : variable->type));
108 else if(q.name=="offset")
109 allowed = (variable && scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
110 else if(q.name=="align")
111 allowed = (scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
112 else if(q.name=="points")
114 allowed = (stage->type==Stage::GEOMETRY && iface_layout && (iface_layout->interface=="in" || iface_layout->interface=="out"));
117 else if(q.name=="lines" || q.name=="lines_adjacency" || q.name=="triangles" || q.name=="triangles_adjacency")
119 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
122 else if(q.name=="line_strip" || q.name=="triangle_strip")
124 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
127 else if(q.name=="invocations")
128 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
129 else if(q.name=="max_vertices")
130 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
131 else if(q.name=="std140" || q.name=="std430")
133 allowed = (iface_block && !variable && iface_block->interface=="uniform");
136 else if(q.name=="column_major" || q.name=="row_major")
138 allowed = (variable && scope==INTERFACE_BLOCK);
141 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
142 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
143 basic = dynamic_cast<BasicTypeDeclaration *>(basic->base_type);
144 allowed = (basic && basic->kind==BasicTypeDeclaration::MATRIX);
145 err_descr = "non-matrix variable";
148 else if(q.name=="push_constant")
150 push_constant = true;
151 allowed = (iface_block && !variable && iface_block->interface=="uniform");
157 if(err_descr.empty())
160 err_descr = describe_variable(scope);
162 err_descr = "interface block";
163 else if(iface_layout)
164 err_descr = format("interface '%s'", iface_layout->interface);
166 err_descr = "unknown declaration";
168 error(layout, format("Layout qualifier '%s' not allowed on %s", q.name, err_descr));
170 else if(value && !q.has_value)
171 error(layout, format("Layout qualifier '%s' requires a value", q.name));
172 else if(!value && q.has_value)
173 error(layout, format("Layout qualifier '%s' does not allow a value", q.name));
176 if(push_constant && binding)
177 error(layout, "Layout qualifier 'push_constant' not allowed together with 'binding' or 'set'");
180 void DeclarationValidator::visit(InterfaceLayout &layout)
182 SetForScope<InterfaceLayout *> set_layout(iface_layout, &layout);
183 TraversingVisitor::visit(layout);
186 void DeclarationValidator::visit(BasicTypeDeclaration &type)
188 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
189 BasicTypeDeclaration::Kind base_kind = (basic_base ? basic_base->kind : BasicTypeDeclaration::VOID);
191 if(type.kind==BasicTypeDeclaration::VECTOR && (base_kind<BasicTypeDeclaration::BOOL || base_kind>BasicTypeDeclaration::FLOAT))
192 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
193 else if(type.kind==BasicTypeDeclaration::MATRIX && base_kind!=BasicTypeDeclaration::VECTOR)
194 error(type, format("Invalid base type '%s' for matrix type '%s'", type.base, type.name));
195 else if(type.kind==BasicTypeDeclaration::ARRAY && basic_base && base_kind==BasicTypeDeclaration::VOID)
196 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
199 void DeclarationValidator::visit(ImageTypeDeclaration &type)
201 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
202 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
203 base_kind = basic_base->kind;
204 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
205 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
208 void DeclarationValidator::visit(StructDeclaration &strct)
210 SetForScope<ScopeType> set_scope(scope, (scope!=INTERFACE_BLOCK ? STRUCT : scope));
211 TraversingVisitor::visit(strct);
214 void DeclarationValidator::visit(VariableDeclaration &var)
216 SetForScope<VariableDeclaration *> set_var(variable, &var);
218 const char *descr = describe_variable(scope);
222 if(scope!=GLOBAL && scope!=INTERFACE_BLOCK)
223 error(var, format("Layout qualifier not allowed on %s", descr));
225 var.layout->visit(*this);
230 if(scope==STRUCT || scope==INTERFACE_BLOCK)
231 error(var, format("Constant qualifier not allowed on %s", descr));
232 if(!var.init_expression)
233 error(var, "Constant variable must have an initializer");
236 if(!var.interpolation.empty() || !var.sampling.empty())
238 if(var.interface!="in" && stage->type==Stage::VERTEX)
239 error(var, "Interpolation qualifier not allowed on vertex input");
240 else if(var.interface!="out" && stage->type==Stage::FRAGMENT)
241 error(var, "Interpolation qualifier not allowed on fragment output");
242 else if((var.interface!="in" && var.interface!="out") || (scope==FUNCTION_PARAM || scope==FUNCTION))
243 error(var, "Interpolation qualifier not allowed on non-interpolated variable");
246 if(!var.interface.empty())
248 if(iface_block && var.interface!=iface_block->interface)
249 error(var, format("Mismatched interface qualifier '%s' inside '%s' block", var.interface, iface_block->interface));
250 else if(scope==STRUCT || scope==FUNCTION)
251 error(var, format("Interface qualifier not allowed on %s", descr));
254 TypeDeclaration *type = var.type_declaration;
255 BasicTypeDeclaration::Kind kind = BasicTypeDeclaration::ALIAS;
256 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
259 type = basic->base_type;
261 if(dynamic_cast<ImageTypeDeclaration *>(type))
263 if(scope!=GLOBAL && scope!=FUNCTION_PARAM)
264 error(var, format("Type '%s' not allowed on %s", type->name, descr));
265 else if(scope==GLOBAL && var.interface!="uniform")
266 error(var, format("Type '%s' only allowed with uniform interface", type->name));
268 else if(kind==BasicTypeDeclaration::VOID)
269 error(var, "Type 'void' not allowed on variable");
270 else if(kind==BasicTypeDeclaration::BOOL && var.source!=BUILTIN_SOURCE)
272 if(scope==INTERFACE_BLOCK)
273 error(var, "Type 'bool' not allowed in an interface block");
274 else if(!var.interface.empty())
275 error(var, "Type 'bool' not allowed on interface variable");
278 if(var.init_expression)
280 if(scope==GLOBAL && !var.constant)
281 error(var, format("Initializer not allowed on non-constant %s", descr));
282 else if(scope!=GLOBAL && scope!=FUNCTION)
283 error(var, format("Initializer not allowed on %s", descr));
285 var.init_expression->visit(*this);
289 void DeclarationValidator::visit(InterfaceBlock &iface)
291 SetForScope<ScopeType> set_scope(scope, INTERFACE_BLOCK);
292 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
294 if(stage->type==Stage::VERTEX && iface.interface=="in")
295 error(iface, "Interface block not allowed on vertex shader input");
296 else if(stage->type==Stage::FRAGMENT && iface.interface=="out")
297 error(iface, "Interface block not allowed on fragment shader output");
299 TraversingVisitor::visit(iface);
300 if(iface.struct_declaration)
301 iface.struct_declaration->visit(*this);
304 void DeclarationValidator::visit(FunctionDeclaration &func)
306 SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
307 for(const RefPtr<VariableDeclaration> &p: func.parameters)
310 func.body.visit(*this);
314 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
316 error(statement, format("Multiple definition of %s", name));
317 add_info(previous, "Previous definition is here");
320 Statement *IdentifierValidator::find_definition(const string &name)
322 BlockDeclarationMap *decls = &declarations[current_block];
323 auto i = decls->find(name);
324 if(i==decls->end() && anonymous_block)
326 decls = &declarations[current_block->parent];
327 i = decls->find(name);
329 return (i!=decls->end() ? i->second : 0);
332 void IdentifierValidator::check_definition(const string &name, Statement &statement)
334 if(Statement *previous = find_definition(name))
335 multiple_definition(format("'%s'", name), statement, *previous);
337 record_definition(name, statement);
340 void IdentifierValidator::record_definition(const string &name, Statement &statement)
342 declarations[current_block].insert(make_pair(name, &statement));
344 declarations[current_block->parent].insert(make_pair(name, &statement));
347 void IdentifierValidator::visit(TypeDeclaration &type)
349 check_definition(type.name, type);
352 void IdentifierValidator::visit(StructDeclaration &strct)
354 check_definition(strct.name, strct);
355 TraversingVisitor::visit(strct);
358 void IdentifierValidator::visit(VariableDeclaration &var)
360 check_definition(var.name, var);
361 TraversingVisitor::visit(var);
364 void IdentifierValidator::visit(InterfaceBlock &iface)
366 string key = format("%s %s", iface.interface, iface.block_name);
367 auto i = interface_blocks.find(key);
368 if(i!=interface_blocks.end())
369 multiple_definition(format("interface block '%s %s'", iface.interface, iface.block_name), iface, *i->second);
371 interface_blocks.insert(make_pair(key, &iface));
373 if(Statement *previous = find_definition(iface.block_name))
375 if(!dynamic_cast<InterfaceBlock *>(previous))
376 multiple_definition(format("'%s'", iface.block_name), iface, *previous);
379 record_definition(iface.block_name, iface);
381 if(!iface.instance_name.empty())
382 check_definition(iface.instance_name, iface);
384 if(iface.instance_name.empty() && iface.struct_declaration)
386 // Inject anonymous interface block members into the global scope
387 for(const auto &kvp: iface.struct_declaration->members.variables)
388 check_definition(kvp.first, *kvp.second);
392 void IdentifierValidator::visit(FunctionDeclaration &func)
394 string key = func.name+func.signature;
395 auto i = overloaded_functions.find(key);
396 if(i==overloaded_functions.end())
397 overloaded_functions.insert(make_pair(key, &func));
398 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
400 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
401 if(i->second->return_type_declaration)
402 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
405 if(Statement *previous = find_definition(func.name))
407 if(!dynamic_cast<FunctionDeclaration *>(previous))
408 multiple_definition(format("'%s'", func.name), func, *previous);
411 record_definition(func.name, func);
413 if(func.definition==&func)
414 check_definition(func.name+func.signature, func);
416 TraversingVisitor::visit(func);
420 void ReferenceValidator::visit(BasicTypeDeclaration &type)
422 if(!type.base.empty() && !type.base_type)
423 error(type, format("Use of undeclared type '%s'", type.base));
426 void ReferenceValidator::visit(ImageTypeDeclaration &type)
428 if(!type.base.empty() && !type.base_type)
429 error(type, format("Use of undeclared type '%s'", type.base));
432 void ReferenceValidator::visit(VariableReference &var)
435 error(var, format("Use of undeclared variable '%s'", var.name));
436 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
437 error(var, format("Use of unlinked input variable '%s'", var.name));
440 void ReferenceValidator::visit(MemberAccess &memacc)
442 if(memacc.left->type && !memacc.declaration)
443 error(memacc, format("Use of undeclared member '%s'", memacc.member));
444 TraversingVisitor::visit(memacc);
447 void ReferenceValidator::visit(InterfaceBlockReference &iface)
449 /* An interface block reference without a declaration should be impossible
450 since references are generated based on existing declarations. */
451 if(!iface.declaration)
452 error(iface, format("Use of undeclared interface block '%s'", iface.name));
453 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
454 error(iface, format("Use of unlinked input block '%s'", iface.name));
457 void ReferenceValidator::visit(FunctionCall &call)
459 if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
461 bool have_declaration = call.constructor;
462 if(!call.constructor)
464 auto i = stage->functions.lower_bound(call.name);
465 have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
470 bool valid_types = true;
472 for(auto j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
475 append(signature, ", ", (*j)->type->name);
481 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
484 error(call, format("Call to undeclared function '%s'", call.name));
486 TraversingVisitor::visit(call);
489 void ReferenceValidator::visit(VariableDeclaration &var)
491 if(!var.type_declaration)
492 error(var, format("Use of undeclared type '%s'", var.type));
493 TraversingVisitor::visit(var);
496 void ReferenceValidator::visit(InterfaceBlock &iface)
498 if(!iface.struct_declaration)
499 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
500 TraversingVisitor::visit(iface);
503 void ReferenceValidator::visit(FunctionDeclaration &func)
505 if(!func.return_type_declaration)
506 error(func, format("Use of undeclared type '%s'", func.return_type));
507 TraversingVisitor::visit(func);
511 void ExpressionValidator::visit(VariableReference &var)
513 if(var.declaration && constant_expression)
515 if(!var.declaration->constant)
516 error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
517 else if(var.declaration->layout && constant_expression==FIXED_CONSTANT)
519 auto i = find_member(var.declaration->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
520 if(i!=var.declaration->layout->qualifiers.end())
521 error(var, format("Reference to specialization constant '%s' in a fixed constant expression", var.name));
526 void ExpressionValidator::visit(InterfaceBlockReference &iface)
528 if(constant_expression)
529 error(iface, format("Reference to interface block '%s' in a constant expression", iface.name));
532 void ExpressionValidator::visit(Swizzle &swizzle)
535 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
537 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
539 else if(basic->kind==BasicTypeDeclaration::VECTOR)
545 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
547 for(unsigned i=0; i<swizzle.count; ++i)
549 unsigned component_flavour = (std::find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
551 flavour = component_flavour;
552 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
554 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
555 swizzle.component_group[i], swizzle.component_group[0]));
559 if(swizzle.components[i]>=size)
560 error(swizzle, format("Access to component '%c' which is not present in '%s'",
561 swizzle.component_group[i], swizzle.left->type->name));
564 else if(swizzle.left->type)
565 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
567 TraversingVisitor::visit(swizzle);
570 void ExpressionValidator::visit(UnaryExpression &unary)
572 if(unary.expression->type)
575 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
576 else if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
578 if(constant_expression)
579 error(unary, format("Use of '%s' in a constant expression", unary.oper->token));
580 else if(!unary.expression->lvalue)
581 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
584 TraversingVisitor::visit(unary);
587 void ExpressionValidator::visit(BinaryExpression &binary)
589 if(!binary.type && binary.left->type && binary.right->type)
591 if(binary.oper->token[0]=='[')
592 error(binary, format("Can't index element of '%s' with '%s'",
593 binary.left->type->name, binary.right->type->name));
595 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
596 binary.oper->token, binary.left->type->name, binary.right->type->name));
598 TraversingVisitor::visit(binary);
601 void ExpressionValidator::visit(Assignment &assign)
603 if(assign.left->type)
605 if(constant_expression)
606 error(assign, "Assignment in constant expression");
607 else if(!assign.left->lvalue)
608 error(assign, "Target of assignment is not an lvalue");
609 if(assign.right->type)
611 if(assign.oper->token[0]!='=')
614 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
615 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
617 else if(assign.left->type!=assign.right->type)
618 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
619 assign.left->type->name, assign.right->type->name));
622 TraversingVisitor::visit(assign);
625 void ExpressionValidator::visit(TernaryExpression &ternary)
627 if(ternary.condition->type)
629 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
630 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
631 error(ternary, "Ternary operator condition is not a boolean");
632 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
633 error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
634 ternary.true_expr->type->name, ternary.false_expr->type->name));
636 TraversingVisitor::visit(ternary);
639 void ExpressionValidator::visit(StructDeclaration &strct)
641 SetFlag set_struct(in_struct);
642 TraversingVisitor::visit(strct);
645 void ExpressionValidator::visit(VariableDeclaration &var)
647 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
648 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
649 var.type_declaration->name, var.init_expression->type->name));
652 var.layout->visit(*this);
653 if(var.init_expression)
655 ConstantKind const_kind = (var.constant ? SPEC_CONSTANT : NOT_CONSTANT);
658 auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
659 if(i!=var.layout->qualifiers.end())
660 const_kind = FIXED_CONSTANT;
663 SetForScope<ConstantKind> set_const(constant_expression, const_kind);
664 TraversingVisitor::visit(var.init_expression);
668 SetForScope<ConstantKind> set_const(constant_expression, (in_struct || !var.interface.empty() ? FIXED_CONSTANT : SPEC_CONSTANT));
669 TraversingVisitor::visit(var.array_size);
673 void ExpressionValidator::visit(FunctionDeclaration &func)
675 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
676 TraversingVisitor::visit(func);
679 void ExpressionValidator::visit(Conditional &cond)
681 if(cond.condition->type)
683 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
684 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
685 error(cond, "Condition is not a boolean");
687 TraversingVisitor::visit(cond);
690 void ExpressionValidator::visit(Iteration &iter)
692 if(iter.condition->type)
694 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
695 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
696 error(iter, "Loop condition is not a boolean");
698 TraversingVisitor::visit(iter);
701 void ExpressionValidator::visit(Return &ret)
703 if(current_function && current_function->return_type_declaration)
705 TypeDeclaration *return_type = current_function->return_type_declaration;
706 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
709 if(ret.expression->type && ret.expression->type!=return_type)
710 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
711 ret.expression->type->name, return_type->name));
713 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
714 error(ret, "Return statement without an expression in a function not returning 'void'");
717 TraversingVisitor::visit(ret);
721 void FlowControlValidator::visit(Block &block)
723 for(const RefPtr<Statement> &s: block.body)
727 diagnose(*s, Diagnostic::WARN, "Unreachable code detected");
734 void FlowControlValidator::visit(FunctionDeclaration &func)
736 func.body.visit(*this);
738 if(func.definition==&func && func.return_type_declaration)
740 const BasicTypeDeclaration *basic_ret = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
741 if(reachable && (!basic_ret || basic_ret->kind!=BasicTypeDeclaration::VOID))
742 error(func, "Missing return statement at the end of a function not returning 'void'");
747 void FlowControlValidator::visit(Conditional &cond)
749 cond.body.visit(*this);
750 bool reachable_if_true = reachable;
752 cond.else_body.visit(*this);
753 reachable |= reachable_if_true;
756 void FlowControlValidator::visit(Iteration &iter)
758 iter.body.visit(*this);
763 int StageInterfaceValidator::get_location(const Layout &layout)
765 return get_layout_value(layout, "location", -1);
768 void StageInterfaceValidator::visit(VariableDeclaration &var)
770 int location = (var.layout ? get_location(*var.layout) : -1);
771 if(var.interface=="in" && var.linked_declaration)
773 const Layout *linked_layout = var.linked_declaration->layout.get();
774 int linked_location = (linked_layout ? get_location(*linked_layout) : -1);
775 if(linked_location!=location)
777 error(var, format("Mismatched location %d for 'in %s'", location, var.name));
778 add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
779 var.linked_declaration->name, linked_location));
781 if(var.type_declaration && var.linked_declaration->type_declaration)
783 TypeDeclaration *type = var.type_declaration;
784 if(stage->type==Stage::GEOMETRY)
786 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
787 if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
788 type = basic->base_type;
790 if(!TypeComparer().apply(*type, *var.linked_declaration->type_declaration))
792 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
793 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
794 var.linked_declaration->name, var.linked_declaration->type_declaration->name));
799 if(location>=0 && !var.interface.empty())
801 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
803 unsigned loc_count = LocationCounter().apply(var);
804 for(unsigned i=0; i<loc_count; ++i)
806 auto j = used.find(location+i);
809 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
810 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
813 used[location+i] = &var;
819 void GlobalInterfaceValidator::apply(Module &module)
821 for(Stage &s: module.stages)
824 s.content.visit(*this);
828 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
830 auto i = used_names.find(uni.name);
831 if(i!=used_names.end())
833 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
835 error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
836 add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
838 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
840 error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
841 add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
843 if(uni.type && i->second->type && !TypeComparer().apply(*uni.type, *i->second->type))
845 string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
846 "structure" : format("type '%s'", uni.type->name));
847 error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
849 string message = "Previously declared here";
850 if(!dynamic_cast<const StructDeclaration *>(i->second->type))
851 message += format(" with type '%s'", i->second->type->name);
852 add_info(*i->second->node, message);
856 used_names.insert(make_pair(uni.name, &uni));
860 auto j = used_locations.find(uni.location);
861 if(j!=used_locations.end())
863 if(j->second->name!=uni.name)
865 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
866 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
871 for(unsigned k=0; k<uni.loc_count; ++k)
872 used_locations.insert(make_pair(uni.location+k, &uni));
876 if(uni.bind_point>=0)
878 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
879 auto j = used.find(uni.bind_point);
882 if(j->second->name!=uni.name)
884 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
885 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
889 used.insert(make_pair(uni.bind_point, &uni));
893 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
895 if(var.interface=="uniform")
899 uni.type = var.type_declaration;
903 uni.location = get_layout_value(*var.layout, "location");
904 uni.loc_count = LocationCounter().apply(var);
905 uni.desc_set = get_layout_value(*var.layout, "set", 0);
906 uni.bind_point = get_layout_value(*var.layout, "binding");
909 uniforms.push_back(uni);
910 check_uniform(uniforms.back());
914 void GlobalInterfaceValidator::visit(InterfaceBlock &iface)
916 if(iface.interface=="uniform")
920 uni.type = iface.struct_declaration;
921 uni.name = iface.block_name;
924 uni.desc_set = get_layout_value(*iface.layout, "set", 0);
925 uni.bind_point = get_layout_value(*iface.layout, "binding");
928 uniforms.push_back(uni);
929 check_uniform(uniforms.back());