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 void DeclarationValidator::apply(Stage &s, const Features &f)
41 s.content.visit(*this);
44 const char *DeclarationValidator::describe_variable(ScopeType scope)
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";
57 void DeclarationValidator::visit(Layout &layout)
59 bool push_constant = false;
61 for(const Layout::Qualifier &q: layout.qualifiers)
66 if(q.name=="location")
67 allowed = (variable && scope==GLOBAL);
68 else if(q.name=="binding" || q.name=="set")
72 if(q.name=="set" && features.target_api!=VULKAN)
74 error(layout, "Layout qualifier 'set' not allowed when targeting OpenGL");
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");
87 allowed = (iface_block->interface=="uniform");
88 err_descr = "non-uniform interface block";
91 else if(q.name=="constant_id")
93 allowed = (variable && scope==GLOBAL);
96 if(!variable->constant)
99 err_descr = "non-constant variable";
103 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
104 if(!basic || basic->kind<BasicTypeDeclaration::BOOL || basic->kind>BasicTypeDeclaration::INT)
107 err_descr = format("variable of type '%s'",
108 (variable->type_declaration ? variable->type_declaration->name : variable->type));
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")
119 allowed = (stage->type==Stage::GEOMETRY && iface_layout && (iface_layout->interface=="in" || iface_layout->interface=="out"));
122 else if(q.name=="lines" || q.name=="lines_adjacency" || q.name=="triangles" || q.name=="triangles_adjacency")
124 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
127 else if(q.name=="line_strip" || q.name=="triangle_strip")
129 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
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")
138 allowed = (iface_block && !variable && iface_block->interface=="uniform");
141 else if(q.name=="column_major" || q.name=="row_major")
143 allowed = (variable && scope==INTERFACE_BLOCK);
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";
153 else if(q.name=="push_constant")
155 push_constant = true;
156 allowed = (iface_block && !variable && iface_block->interface=="uniform");
162 if(err_descr.empty())
165 err_descr = describe_variable(scope);
167 err_descr = "interface block";
168 else if(iface_layout)
169 err_descr = format("interface '%s'", iface_layout->interface);
171 err_descr = "unknown declaration";
173 error(layout, format("Layout qualifier '%s' not allowed on %s", q.name, err_descr));
175 else if(value && !q.has_value)
176 error(layout, format("Layout qualifier '%s' requires a value", q.name));
177 else if(!value && q.has_value)
178 error(layout, format("Layout qualifier '%s' does not allow a value", q.name));
181 if(push_constant && binding)
182 error(layout, "Layout qualifier 'push_constant' not allowed together with 'binding' or 'set'");
185 void DeclarationValidator::visit(InterfaceLayout &layout)
187 SetForScope<InterfaceLayout *> set_layout(iface_layout, &layout);
188 TraversingVisitor::visit(layout);
191 void DeclarationValidator::visit(BasicTypeDeclaration &type)
193 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
194 BasicTypeDeclaration::Kind base_kind = (basic_base ? basic_base->kind : BasicTypeDeclaration::VOID);
196 if(type.kind==BasicTypeDeclaration::VECTOR && (base_kind<BasicTypeDeclaration::BOOL || base_kind>BasicTypeDeclaration::FLOAT))
197 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
198 else if(type.kind==BasicTypeDeclaration::MATRIX && base_kind!=BasicTypeDeclaration::VECTOR)
199 error(type, format("Invalid base type '%s' for matrix type '%s'", type.base, type.name));
200 else if(type.kind==BasicTypeDeclaration::ARRAY && basic_base && base_kind==BasicTypeDeclaration::VOID)
201 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
204 void DeclarationValidator::visit(ImageTypeDeclaration &type)
206 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
207 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
208 base_kind = basic_base->kind;
209 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
210 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
213 void DeclarationValidator::visit(StructDeclaration &strct)
215 SetForScope<ScopeType> set_scope(scope, (scope!=INTERFACE_BLOCK ? STRUCT : scope));
216 TraversingVisitor::visit(strct);
219 void DeclarationValidator::visit(VariableDeclaration &var)
221 SetForScope<VariableDeclaration *> set_var(variable, &var);
223 const char *descr = describe_variable(scope);
227 if(scope!=GLOBAL && scope!=INTERFACE_BLOCK)
228 error(var, format("Layout qualifier not allowed on %s", descr));
230 var.layout->visit(*this);
235 if(scope==STRUCT || scope==INTERFACE_BLOCK)
236 error(var, format("Constant qualifier not allowed on %s", descr));
237 if(!var.init_expression)
238 error(var, "Constant variable must have an initializer");
241 if(!var.interpolation.empty() || !var.sampling.empty())
243 if(var.interface!="in" && stage->type==Stage::VERTEX)
244 error(var, "Interpolation qualifier not allowed on vertex input");
245 else if(var.interface!="out" && stage->type==Stage::FRAGMENT)
246 error(var, "Interpolation qualifier not allowed on fragment output");
247 else if((var.interface!="in" && var.interface!="out") || (scope==FUNCTION_PARAM || scope==FUNCTION))
248 error(var, "Interpolation qualifier not allowed on non-interpolated variable");
251 if(!var.interface.empty())
253 if(iface_block && var.interface!=iface_block->interface)
254 error(var, format("Mismatched interface qualifier '%s' inside '%s' block", var.interface, iface_block->interface));
255 else if(scope==STRUCT || scope==FUNCTION)
256 error(var, format("Interface qualifier not allowed on %s", descr));
257 else if(scope==GLOBAL && variable->interface=="uniform" && features.target_api==VULKAN)
259 if(!dynamic_cast<const ImageTypeDeclaration *>(get_ultimate_base_type(variable->type_declaration)))
260 error(var, "Interface qualifier 'uniform' not allowed on non-opaque variable in global scope");
264 TypeDeclaration *type = var.type_declaration;
265 BasicTypeDeclaration::Kind kind = BasicTypeDeclaration::ALIAS;
266 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
269 type = basic->base_type;
271 if(dynamic_cast<ImageTypeDeclaration *>(type))
273 if(scope!=GLOBAL && scope!=FUNCTION_PARAM)
274 error(var, format("Type '%s' not allowed on %s", type->name, descr));
275 else if(scope==GLOBAL && var.interface!="uniform")
276 error(var, format("Type '%s' only allowed with uniform interface", type->name));
278 else if(kind==BasicTypeDeclaration::VOID)
279 error(var, "Type 'void' not allowed on variable");
280 else if(kind==BasicTypeDeclaration::BOOL && var.source!=BUILTIN_SOURCE)
282 if(scope==INTERFACE_BLOCK)
283 error(var, "Type 'bool' not allowed in an interface block");
284 else if(!var.interface.empty())
285 error(var, "Type 'bool' not allowed on interface variable");
288 if(var.init_expression)
290 if(scope==GLOBAL && !var.constant)
291 error(var, format("Initializer not allowed on non-constant %s", descr));
292 else if(scope!=GLOBAL && scope!=FUNCTION)
293 error(var, format("Initializer not allowed on %s", descr));
295 var.init_expression->visit(*this);
299 void DeclarationValidator::visit(InterfaceBlock &iface)
301 SetForScope<ScopeType> set_scope(scope, INTERFACE_BLOCK);
302 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
304 if(stage->type==Stage::VERTEX && iface.interface=="in")
305 error(iface, "Interface block not allowed on vertex shader input");
306 else if(stage->type==Stage::FRAGMENT && iface.interface=="out")
307 error(iface, "Interface block not allowed on fragment shader output");
309 TraversingVisitor::visit(iface);
310 if(iface.struct_declaration)
311 iface.struct_declaration->visit(*this);
314 void DeclarationValidator::visit(FunctionDeclaration &func)
316 SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
317 for(const RefPtr<VariableDeclaration> &p: func.parameters)
320 func.body.visit(*this);
324 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
326 error(statement, format("Multiple definition of %s", name));
327 add_info(previous, "Previous definition is here");
330 Statement *IdentifierValidator::find_definition(const string &name)
332 BlockDeclarationMap *decls = &declarations[current_block];
333 auto i = decls->find(name);
334 if(i==decls->end() && anonymous_block)
336 decls = &declarations[current_block->parent];
337 i = decls->find(name);
339 return (i!=decls->end() ? i->second : 0);
342 void IdentifierValidator::check_definition(const string &name, Statement &statement)
344 if(Statement *previous = find_definition(name))
345 multiple_definition(format("'%s'", name), statement, *previous);
347 record_definition(name, statement);
350 void IdentifierValidator::record_definition(const string &name, Statement &statement)
352 declarations[current_block].insert(make_pair(name, &statement));
354 declarations[current_block->parent].insert(make_pair(name, &statement));
357 void IdentifierValidator::visit(TypeDeclaration &type)
359 check_definition(type.name, type);
362 void IdentifierValidator::visit(StructDeclaration &strct)
364 check_definition(strct.name, strct);
365 TraversingVisitor::visit(strct);
368 void IdentifierValidator::visit(VariableDeclaration &var)
370 check_definition(var.name, var);
371 TraversingVisitor::visit(var);
374 void IdentifierValidator::visit(InterfaceBlock &iface)
376 string key = format("%s %s", iface.interface, iface.block_name);
377 auto i = interface_blocks.find(key);
378 if(i!=interface_blocks.end())
379 multiple_definition(format("interface block '%s %s'", iface.interface, iface.block_name), iface, *i->second);
381 interface_blocks.insert(make_pair(key, &iface));
383 if(Statement *previous = find_definition(iface.block_name))
385 if(!dynamic_cast<InterfaceBlock *>(previous))
386 multiple_definition(format("'%s'", iface.block_name), iface, *previous);
389 record_definition(iface.block_name, iface);
391 if(!iface.instance_name.empty())
392 check_definition(iface.instance_name, iface);
394 if(iface.instance_name.empty() && iface.struct_declaration)
396 // Inject anonymous interface block members into the global scope
397 for(const auto &kvp: iface.struct_declaration->members.variables)
398 check_definition(kvp.first, *kvp.second);
402 void IdentifierValidator::visit(FunctionDeclaration &func)
404 string key = func.name+func.signature;
405 auto i = overloaded_functions.find(key);
406 if(i==overloaded_functions.end())
407 overloaded_functions.insert(make_pair(key, &func));
408 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
410 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
411 if(i->second->return_type_declaration)
412 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
415 if(Statement *previous = find_definition(func.name))
417 if(!dynamic_cast<FunctionDeclaration *>(previous))
418 multiple_definition(format("'%s'", func.name), func, *previous);
421 record_definition(func.name, func);
423 if(func.definition==&func)
424 check_definition(func.name+func.signature, func);
426 TraversingVisitor::visit(func);
430 void ReferenceValidator::visit(BasicTypeDeclaration &type)
432 if(!type.base.empty() && !type.base_type)
433 error(type, format("Use of undeclared type '%s'", type.base));
436 void ReferenceValidator::visit(ImageTypeDeclaration &type)
438 if(!type.base.empty() && !type.base_type)
439 error(type, format("Use of undeclared type '%s'", type.base));
442 void ReferenceValidator::visit(VariableReference &var)
445 error(var, format("Use of undeclared variable '%s'", var.name));
446 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
447 error(var, format("Use of unlinked input variable '%s'", var.name));
450 void ReferenceValidator::visit(MemberAccess &memacc)
452 if(memacc.left->type && !memacc.declaration)
453 error(memacc, format("Use of undeclared member '%s'", memacc.member));
454 TraversingVisitor::visit(memacc);
457 void ReferenceValidator::visit(InterfaceBlockReference &iface)
459 /* An interface block reference without a declaration should be impossible
460 since references are generated based on existing declarations. */
461 if(!iface.declaration)
462 error(iface, format("Use of undeclared interface block '%s'", iface.name));
463 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
464 error(iface, format("Use of unlinked input block '%s'", iface.name));
467 void ReferenceValidator::visit(FunctionCall &call)
469 if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
471 bool have_declaration = call.constructor;
472 if(!call.constructor)
474 auto i = stage->functions.lower_bound(call.name);
475 have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
480 bool valid_types = true;
482 for(auto j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
485 append(signature, ", ", (*j)->type->name);
491 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
494 error(call, format("Call to undeclared function '%s'", call.name));
496 TraversingVisitor::visit(call);
499 void ReferenceValidator::visit(VariableDeclaration &var)
501 if(!var.type_declaration)
502 error(var, format("Use of undeclared type '%s'", var.type));
503 TraversingVisitor::visit(var);
506 void ReferenceValidator::visit(InterfaceBlock &iface)
508 if(!iface.struct_declaration)
509 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
510 TraversingVisitor::visit(iface);
513 void ReferenceValidator::visit(FunctionDeclaration &func)
515 if(!func.return_type_declaration)
516 error(func, format("Use of undeclared type '%s'", func.return_type));
517 TraversingVisitor::visit(func);
521 void ExpressionValidator::visit(VariableReference &var)
523 if(var.declaration && constant_expression)
525 if(!var.declaration->constant)
526 error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
527 else if(var.declaration->layout && constant_expression==FIXED_CONSTANT)
529 auto i = find_member(var.declaration->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
530 if(i!=var.declaration->layout->qualifiers.end())
531 error(var, format("Reference to specialization constant '%s' in a fixed constant expression", var.name));
536 void ExpressionValidator::visit(InterfaceBlockReference &iface)
538 if(constant_expression)
539 error(iface, format("Reference to interface block '%s' in a constant expression", iface.name));
542 void ExpressionValidator::visit(Swizzle &swizzle)
545 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
547 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
549 else if(basic->kind==BasicTypeDeclaration::VECTOR)
555 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
557 for(unsigned i=0; i<swizzle.count; ++i)
559 unsigned component_flavour = (std::find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
561 flavour = component_flavour;
562 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
564 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
565 swizzle.component_group[i], swizzle.component_group[0]));
569 if(swizzle.components[i]>=size)
570 error(swizzle, format("Access to component '%c' which is not present in '%s'",
571 swizzle.component_group[i], swizzle.left->type->name));
574 else if(swizzle.left->type)
575 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
577 TraversingVisitor::visit(swizzle);
580 void ExpressionValidator::visit(UnaryExpression &unary)
582 if(unary.expression->type)
585 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
586 else if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
588 if(constant_expression)
589 error(unary, format("Use of '%s' in a constant expression", unary.oper->token));
590 else if(!unary.expression->lvalue)
591 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
594 TraversingVisitor::visit(unary);
597 void ExpressionValidator::visit(BinaryExpression &binary)
599 if(!binary.type && binary.left->type && binary.right->type)
601 if(binary.oper->token[0]=='[')
602 error(binary, format("Can't index element of '%s' with '%s'",
603 binary.left->type->name, binary.right->type->name));
605 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
606 binary.oper->token, binary.left->type->name, binary.right->type->name));
608 TraversingVisitor::visit(binary);
611 void ExpressionValidator::visit(Assignment &assign)
613 if(assign.left->type)
615 if(constant_expression)
616 error(assign, "Assignment in constant expression");
617 else if(!assign.left->lvalue)
618 error(assign, "Target of assignment is not an lvalue");
619 if(assign.right->type)
621 if(assign.oper->token[0]!='=')
624 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
625 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
627 else if(assign.left->type!=assign.right->type)
628 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
629 assign.left->type->name, assign.right->type->name));
632 TraversingVisitor::visit(assign);
635 void ExpressionValidator::visit(TernaryExpression &ternary)
637 if(ternary.condition->type)
639 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
640 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
641 error(ternary, "Ternary operator condition is not a boolean");
642 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
643 error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
644 ternary.true_expr->type->name, ternary.false_expr->type->name));
646 TraversingVisitor::visit(ternary);
649 void ExpressionValidator::visit(StructDeclaration &strct)
651 SetFlag set_struct(in_struct);
652 TraversingVisitor::visit(strct);
655 void ExpressionValidator::visit(VariableDeclaration &var)
657 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
658 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
659 var.type_declaration->name, var.init_expression->type->name));
662 var.layout->visit(*this);
663 if(var.init_expression)
665 ConstantKind const_kind = (var.constant ? SPEC_CONSTANT : NOT_CONSTANT);
668 auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
669 if(i!=var.layout->qualifiers.end())
670 const_kind = FIXED_CONSTANT;
673 SetForScope<ConstantKind> set_const(constant_expression, const_kind);
674 TraversingVisitor::visit(var.init_expression);
678 SetForScope<ConstantKind> set_const(constant_expression, (in_struct || !var.interface.empty() ? FIXED_CONSTANT : SPEC_CONSTANT));
679 TraversingVisitor::visit(var.array_size);
683 void ExpressionValidator::visit(FunctionDeclaration &func)
685 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
686 TraversingVisitor::visit(func);
689 void ExpressionValidator::visit(Conditional &cond)
691 if(cond.condition->type)
693 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
694 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
695 error(cond, "Condition is not a boolean");
697 TraversingVisitor::visit(cond);
700 void ExpressionValidator::visit(Iteration &iter)
702 if(iter.condition->type)
704 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
705 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
706 error(iter, "Loop condition is not a boolean");
708 TraversingVisitor::visit(iter);
711 void ExpressionValidator::visit(Return &ret)
713 if(current_function && current_function->return_type_declaration)
715 TypeDeclaration *return_type = current_function->return_type_declaration;
716 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
719 if(ret.expression->type && ret.expression->type!=return_type)
720 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
721 ret.expression->type->name, return_type->name));
723 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
724 error(ret, "Return statement without an expression in a function not returning 'void'");
727 TraversingVisitor::visit(ret);
731 void FlowControlValidator::visit(Block &block)
733 for(const RefPtr<Statement> &s: block.body)
737 diagnose(*s, Diagnostic::WARN, "Unreachable code detected");
744 void FlowControlValidator::visit(FunctionDeclaration &func)
746 func.body.visit(*this);
748 if(func.definition==&func && func.return_type_declaration)
750 const BasicTypeDeclaration *basic_ret = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
751 if(reachable && (!basic_ret || basic_ret->kind!=BasicTypeDeclaration::VOID))
752 error(func, "Missing return statement at the end of a function not returning 'void'");
757 void FlowControlValidator::visit(Conditional &cond)
759 cond.body.visit(*this);
760 bool reachable_if_true = reachable;
762 cond.else_body.visit(*this);
763 reachable |= reachable_if_true;
766 void FlowControlValidator::visit(Iteration &iter)
768 iter.body.visit(*this);
773 int StageInterfaceValidator::get_location(const Layout &layout)
775 return get_layout_value(layout, "location", -1);
778 void StageInterfaceValidator::visit(VariableDeclaration &var)
780 int location = (var.layout ? get_location(*var.layout) : -1);
781 if(var.interface=="in" && var.linked_declaration)
783 const Layout *linked_layout = var.linked_declaration->layout.get();
784 int linked_location = (linked_layout ? get_location(*linked_layout) : -1);
785 if(linked_location!=location)
787 error(var, format("Mismatched location %d for 'in %s'", location, var.name));
788 add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
789 var.linked_declaration->name, linked_location));
791 if(var.type_declaration && var.linked_declaration->type_declaration)
793 TypeDeclaration *type = var.type_declaration;
794 if(stage->type==Stage::GEOMETRY)
796 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
797 if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
798 type = basic->base_type;
800 if(!TypeComparer().apply(*type, *var.linked_declaration->type_declaration))
802 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
803 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
804 var.linked_declaration->name, var.linked_declaration->type_declaration->name));
809 if(location>=0 && !var.interface.empty())
811 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
813 unsigned loc_count = LocationCounter().apply(var);
814 for(unsigned i=0; i<loc_count; ++i)
816 auto j = used.find(location+i);
819 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
820 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
823 used[location+i] = &var;
829 void GlobalInterfaceValidator::apply(Module &module)
831 for(Stage &s: module.stages)
834 s.content.visit(*this);
838 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
840 auto i = used_names.find(uni.name);
841 if(i!=used_names.end())
843 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
845 error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
846 add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
848 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
850 error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
851 add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
853 if(uni.type && i->second->type && !TypeComparer().apply(*uni.type, *i->second->type))
855 string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
856 "structure" : format("type '%s'", uni.type->name));
857 error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
859 string message = "Previously declared here";
860 if(!dynamic_cast<const StructDeclaration *>(i->second->type))
861 message += format(" with type '%s'", i->second->type->name);
862 add_info(*i->second->node, message);
866 used_names.insert(make_pair(uni.name, &uni));
870 auto j = used_locations.find(uni.location);
871 if(j!=used_locations.end())
873 if(j->second->name!=uni.name)
875 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
876 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
881 for(unsigned k=0; k<uni.loc_count; ++k)
882 used_locations.insert(make_pair(uni.location+k, &uni));
886 if(uni.bind_point>=0)
888 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
889 auto j = used.find(uni.bind_point);
892 if(j->second->name!=uni.name)
894 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
895 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
899 used.insert(make_pair(uni.bind_point, &uni));
903 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
905 if(var.interface=="uniform")
909 uni.type = var.type_declaration;
913 uni.location = get_layout_value(*var.layout, "location");
914 uni.loc_count = LocationCounter().apply(var);
915 uni.desc_set = get_layout_value(*var.layout, "set", 0);
916 uni.bind_point = get_layout_value(*var.layout, "binding");
919 uniforms.push_back(uni);
920 check_uniform(uniforms.back());
924 void GlobalInterfaceValidator::visit(InterfaceBlock &iface)
926 if(iface.interface=="uniform")
930 uni.type = iface.struct_declaration;
931 uni.name = iface.block_name;
934 uni.desc_set = get_layout_value(*iface.layout, "set", 0);
935 uni.bind_point = get_layout_value(*iface.layout, "binding");
938 uniforms.push_back(uni);
939 check_uniform(uniforms.back());