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 for(const Layout::Qualifier &q: layout.qualifiers)
57 if(q.name=="location")
58 allowed = (variable && scope==GLOBAL);
59 else if(q.name=="binding" || q.name=="set")
63 error(layout, "Layout qualifier 'set' not allowed when targeting OpenGL");
69 TypeDeclaration *type = variable->type_declaration;
70 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
71 type = basic->base_type;
72 bool uniform = (variable->interface=="uniform");
73 allowed = (scope==GLOBAL && uniform && dynamic_cast<ImageTypeDeclaration *>(type));
74 err_descr = (uniform ? "variable of non-opaque type" : "non-uniform variable");
78 allowed = (iface_block->interface=="uniform");
79 err_descr = "non-uniform interface block";
82 else if(q.name=="constant_id")
84 allowed = (variable && scope==GLOBAL);
87 if(!variable->constant)
90 err_descr = "non-constant variable";
94 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
95 if(!basic || basic->kind<BasicTypeDeclaration::BOOL || basic->kind>BasicTypeDeclaration::INT)
98 err_descr = format("variable of type '%s'",
99 (variable->type_declaration ? variable->type_declaration->name : variable->type));
104 else if(q.name=="offset")
105 allowed = (variable && scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
106 else if(q.name=="align")
107 allowed = (scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
108 else if(q.name=="points")
110 allowed = (stage->type==Stage::GEOMETRY && iface_layout && (iface_layout->interface=="in" || iface_layout->interface=="out"));
113 else if(q.name=="lines" || q.name=="lines_adjacency" || q.name=="triangles" || q.name=="triangles_adjacency")
115 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
118 else if(q.name=="line_strip" || q.name=="triangle_strip")
120 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
123 else if(q.name=="invocations")
124 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
125 else if(q.name=="max_vertices")
126 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
127 else if(q.name=="std140" || q.name=="std430")
129 allowed = (iface_block && !variable && iface_block->interface=="uniform");
132 else if(q.name=="column_major" || q.name=="row_major")
134 allowed = (variable && scope==INTERFACE_BLOCK);
137 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
138 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
139 basic = dynamic_cast<BasicTypeDeclaration *>(basic->base_type);
140 allowed = (basic && basic->kind==BasicTypeDeclaration::MATRIX);
141 err_descr = "non-matrix variable";
147 if(err_descr.empty())
150 err_descr = describe_variable(scope);
152 err_descr = "interface block";
153 else if(iface_layout)
154 err_descr = format("interface '%s'", iface_layout->interface);
156 err_descr = "unknown declaration";
158 error(layout, format("Layout qualifier '%s' not allowed on %s", q.name, err_descr));
160 else if(value && !q.has_value)
161 error(layout, format("Layout qualifier '%s' requires a value", q.name));
162 else if(!value && q.has_value)
163 error(layout, format("Layout qualifier '%s' does not allow a value", q.name));
167 void DeclarationValidator::visit(InterfaceLayout &layout)
169 SetForScope<InterfaceLayout *> set_layout(iface_layout, &layout);
170 TraversingVisitor::visit(layout);
173 void DeclarationValidator::visit(BasicTypeDeclaration &type)
175 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
176 BasicTypeDeclaration::Kind base_kind = (basic_base ? basic_base->kind : BasicTypeDeclaration::VOID);
178 if(type.kind==BasicTypeDeclaration::VECTOR && (base_kind<BasicTypeDeclaration::BOOL || base_kind>BasicTypeDeclaration::FLOAT))
179 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
180 else if(type.kind==BasicTypeDeclaration::MATRIX && base_kind!=BasicTypeDeclaration::VECTOR)
181 error(type, format("Invalid base type '%s' for matrix type '%s'", type.base, type.name));
182 else if(type.kind==BasicTypeDeclaration::ARRAY && basic_base && base_kind==BasicTypeDeclaration::VOID)
183 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
186 void DeclarationValidator::visit(ImageTypeDeclaration &type)
188 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
189 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
190 base_kind = basic_base->kind;
191 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
192 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
195 void DeclarationValidator::visit(StructDeclaration &strct)
197 SetForScope<ScopeType> set_scope(scope, (scope!=INTERFACE_BLOCK ? STRUCT : scope));
198 TraversingVisitor::visit(strct);
201 void DeclarationValidator::visit(VariableDeclaration &var)
203 SetForScope<VariableDeclaration *> set_var(variable, &var);
205 const char *descr = describe_variable(scope);
209 if(scope!=GLOBAL && scope!=INTERFACE_BLOCK)
210 error(var, format("Layout qualifier not allowed on %s", descr));
212 var.layout->visit(*this);
217 if(scope==STRUCT || scope==INTERFACE_BLOCK)
218 error(var, format("Constant qualifier not allowed on %s", descr));
219 if(!var.init_expression)
220 error(var, "Constant variable must have an initializer");
223 if(!var.interpolation.empty() || !var.sampling.empty())
225 if(var.interface!="in" && stage->type==Stage::VERTEX)
226 error(var, "Interpolation qualifier not allowed on vertex input");
227 else if(var.interface!="out" && stage->type==Stage::FRAGMENT)
228 error(var, "Interpolation qualifier not allowed on fragment output");
229 else if((var.interface!="in" && var.interface!="out") || (scope==FUNCTION_PARAM || scope==FUNCTION))
230 error(var, "Interpolation qualifier not allowed on non-interpolated variable");
233 if(!var.interface.empty())
235 if(iface_block && var.interface!=iface_block->interface)
236 error(var, format("Mismatched interface qualifier '%s' inside '%s' block", var.interface, iface_block->interface));
237 else if(scope==STRUCT || scope==FUNCTION)
238 error(var, format("Interface qualifier not allowed on %s", descr));
241 TypeDeclaration *type = var.type_declaration;
242 BasicTypeDeclaration::Kind kind = BasicTypeDeclaration::ALIAS;
243 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
246 type = basic->base_type;
248 if(dynamic_cast<ImageTypeDeclaration *>(type))
250 if(scope!=GLOBAL && scope!=FUNCTION_PARAM)
251 error(var, format("Type '%s' not allowed on %s", type->name, descr));
252 else if(scope==GLOBAL && var.interface!="uniform")
253 error(var, format("Type '%s' only allowed with uniform interface", type->name));
255 else if(kind==BasicTypeDeclaration::VOID)
256 error(var, "Type 'void' not allowed on variable");
257 else if(kind==BasicTypeDeclaration::BOOL && var.source!=BUILTIN_SOURCE)
259 if(scope==INTERFACE_BLOCK)
260 error(var, "Type 'bool' not allowed in an interface block");
261 else if(!var.interface.empty())
262 error(var, "Type 'bool' not allowed on interface variable");
265 if(var.init_expression)
267 if(scope==GLOBAL && !var.constant)
268 error(var, format("Initializer not allowed on non-constant %s", descr));
269 else if(scope!=GLOBAL && scope!=FUNCTION)
270 error(var, format("Initializer not allowed on %s", descr));
272 var.init_expression->visit(*this);
276 void DeclarationValidator::visit(InterfaceBlock &iface)
278 SetForScope<ScopeType> set_scope(scope, INTERFACE_BLOCK);
279 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
281 if(stage->type==Stage::VERTEX && iface.interface=="in")
282 error(iface, "Interface block not allowed on vertex shader input");
283 else if(stage->type==Stage::FRAGMENT && iface.interface=="out")
284 error(iface, "Interface block not allowed on fragment shader output");
286 TraversingVisitor::visit(iface);
287 if(iface.struct_declaration)
288 iface.struct_declaration->visit(*this);
291 void DeclarationValidator::visit(FunctionDeclaration &func)
293 SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
294 for(const RefPtr<VariableDeclaration> &p: func.parameters)
297 func.body.visit(*this);
301 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
303 error(statement, format("Multiple definition of %s", name));
304 add_info(previous, "Previous definition is here");
307 Statement *IdentifierValidator::find_definition(const string &name)
309 BlockDeclarationMap *decls = &declarations[current_block];
310 auto i = decls->find(name);
311 if(i==decls->end() && anonymous_block)
313 decls = &declarations[current_block->parent];
314 i = decls->find(name);
316 return (i!=decls->end() ? i->second : 0);
319 void IdentifierValidator::check_definition(const string &name, Statement &statement)
321 if(Statement *previous = find_definition(name))
322 multiple_definition(format("'%s'", name), statement, *previous);
324 record_definition(name, statement);
327 void IdentifierValidator::record_definition(const string &name, Statement &statement)
329 declarations[current_block].insert(make_pair(name, &statement));
331 declarations[current_block->parent].insert(make_pair(name, &statement));
334 void IdentifierValidator::visit(TypeDeclaration &type)
336 check_definition(type.name, type);
339 void IdentifierValidator::visit(StructDeclaration &strct)
341 check_definition(strct.name, strct);
342 TraversingVisitor::visit(strct);
345 void IdentifierValidator::visit(VariableDeclaration &var)
347 check_definition(var.name, var);
348 TraversingVisitor::visit(var);
351 void IdentifierValidator::visit(InterfaceBlock &iface)
353 string key = format("%s %s", iface.interface, iface.block_name);
354 auto i = interface_blocks.find(key);
355 if(i!=interface_blocks.end())
356 multiple_definition(format("interface block '%s %s'", iface.interface, iface.block_name), iface, *i->second);
358 interface_blocks.insert(make_pair(key, &iface));
360 if(Statement *previous = find_definition(iface.block_name))
362 if(!dynamic_cast<InterfaceBlock *>(previous))
363 multiple_definition(format("'%s'", iface.block_name), iface, *previous);
366 record_definition(iface.block_name, iface);
368 if(!iface.instance_name.empty())
369 check_definition(iface.instance_name, iface);
371 if(iface.instance_name.empty() && iface.struct_declaration)
373 // Inject anonymous interface block members into the global scope
374 for(const auto &kvp: iface.struct_declaration->members.variables)
375 check_definition(kvp.first, *kvp.second);
379 void IdentifierValidator::visit(FunctionDeclaration &func)
381 string key = func.name+func.signature;
382 auto i = overloaded_functions.find(key);
383 if(i==overloaded_functions.end())
384 overloaded_functions.insert(make_pair(key, &func));
385 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
387 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
388 if(i->second->return_type_declaration)
389 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
392 if(Statement *previous = find_definition(func.name))
394 if(!dynamic_cast<FunctionDeclaration *>(previous))
395 multiple_definition(format("'%s'", func.name), func, *previous);
398 record_definition(func.name, func);
400 if(func.definition==&func)
401 check_definition(func.name+func.signature, func);
403 TraversingVisitor::visit(func);
407 void ReferenceValidator::visit(BasicTypeDeclaration &type)
409 if(!type.base.empty() && !type.base_type)
410 error(type, format("Use of undeclared type '%s'", type.base));
413 void ReferenceValidator::visit(ImageTypeDeclaration &type)
415 if(!type.base.empty() && !type.base_type)
416 error(type, format("Use of undeclared type '%s'", type.base));
419 void ReferenceValidator::visit(VariableReference &var)
422 error(var, format("Use of undeclared variable '%s'", var.name));
423 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
424 error(var, format("Use of unlinked input variable '%s'", var.name));
427 void ReferenceValidator::visit(MemberAccess &memacc)
429 if(memacc.left->type && !memacc.declaration)
430 error(memacc, format("Use of undeclared member '%s'", memacc.member));
431 TraversingVisitor::visit(memacc);
434 void ReferenceValidator::visit(InterfaceBlockReference &iface)
436 /* An interface block reference without a declaration should be impossible
437 since references are generated based on existing declarations. */
438 if(!iface.declaration)
439 error(iface, format("Use of undeclared interface block '%s'", iface.name));
440 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
441 error(iface, format("Use of unlinked input block '%s'", iface.name));
444 void ReferenceValidator::visit(FunctionCall &call)
446 if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
448 bool have_declaration = call.constructor;
449 if(!call.constructor)
451 auto i = stage->functions.lower_bound(call.name);
452 have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
457 bool valid_types = true;
459 for(auto j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
462 append(signature, ", ", (*j)->type->name);
468 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
471 error(call, format("Call to undeclared function '%s'", call.name));
473 TraversingVisitor::visit(call);
476 void ReferenceValidator::visit(VariableDeclaration &var)
478 if(!var.type_declaration)
479 error(var, format("Use of undeclared type '%s'", var.type));
480 TraversingVisitor::visit(var);
483 void ReferenceValidator::visit(InterfaceBlock &iface)
485 if(!iface.struct_declaration)
486 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
487 TraversingVisitor::visit(iface);
490 void ReferenceValidator::visit(FunctionDeclaration &func)
492 if(!func.return_type_declaration)
493 error(func, format("Use of undeclared type '%s'", func.return_type));
494 TraversingVisitor::visit(func);
498 void ExpressionValidator::visit(VariableReference &var)
500 if(var.declaration && constant_expression)
502 if(!var.declaration->constant)
503 error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
504 else if(var.declaration->layout && constant_expression==FIXED_CONSTANT)
506 auto i = find_member(var.declaration->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
507 if(i!=var.declaration->layout->qualifiers.end())
508 error(var, format("Reference to specialization constant '%s' in a fixed constant expression", var.name));
513 void ExpressionValidator::visit(InterfaceBlockReference &iface)
515 if(constant_expression)
516 error(iface, format("Reference to interface block '%s' in a constant expression", iface.name));
519 void ExpressionValidator::visit(Swizzle &swizzle)
522 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
524 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
526 else if(basic->kind==BasicTypeDeclaration::VECTOR)
532 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
534 for(unsigned i=0; i<swizzle.count; ++i)
536 unsigned component_flavour = (std::find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
538 flavour = component_flavour;
539 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
541 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
542 swizzle.component_group[i], swizzle.component_group[0]));
546 if(swizzle.components[i]>=size)
547 error(swizzle, format("Access to component '%c' which is not present in '%s'",
548 swizzle.component_group[i], swizzle.left->type->name));
551 else if(swizzle.left->type)
552 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
554 TraversingVisitor::visit(swizzle);
557 void ExpressionValidator::visit(UnaryExpression &unary)
559 if(unary.expression->type)
562 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
563 else if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
565 if(constant_expression)
566 error(unary, format("Use of '%s' in a constant expression", unary.oper->token));
567 else if(!unary.expression->lvalue)
568 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
571 TraversingVisitor::visit(unary);
574 void ExpressionValidator::visit(BinaryExpression &binary)
576 if(!binary.type && binary.left->type && binary.right->type)
578 if(binary.oper->token[0]=='[')
579 error(binary, format("Can't index element of '%s' with '%s'",
580 binary.left->type->name, binary.right->type->name));
582 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
583 binary.oper->token, binary.left->type->name, binary.right->type->name));
585 TraversingVisitor::visit(binary);
588 void ExpressionValidator::visit(Assignment &assign)
590 if(assign.left->type)
592 if(constant_expression)
593 error(assign, "Assignment in constant expression");
594 else if(!assign.left->lvalue)
595 error(assign, "Target of assignment is not an lvalue");
596 if(assign.right->type)
598 if(assign.oper->token[0]!='=')
601 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
602 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
604 else if(assign.left->type!=assign.right->type)
605 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
606 assign.left->type->name, assign.right->type->name));
609 TraversingVisitor::visit(assign);
612 void ExpressionValidator::visit(TernaryExpression &ternary)
614 if(ternary.condition->type)
616 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
617 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
618 error(ternary, "Ternary operator condition is not a boolean");
619 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
620 error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
621 ternary.true_expr->type->name, ternary.false_expr->type->name));
623 TraversingVisitor::visit(ternary);
626 void ExpressionValidator::visit(StructDeclaration &strct)
628 SetFlag set_struct(in_struct);
629 TraversingVisitor::visit(strct);
632 void ExpressionValidator::visit(VariableDeclaration &var)
634 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
635 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
636 var.type_declaration->name, var.init_expression->type->name));
639 var.layout->visit(*this);
640 if(var.init_expression)
642 ConstantKind const_kind = (var.constant ? SPEC_CONSTANT : NOT_CONSTANT);
645 auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
646 if(i!=var.layout->qualifiers.end())
647 const_kind = FIXED_CONSTANT;
650 SetForScope<ConstantKind> set_const(constant_expression, const_kind);
651 TraversingVisitor::visit(var.init_expression);
655 SetForScope<ConstantKind> set_const(constant_expression, (in_struct || !var.interface.empty() ? FIXED_CONSTANT : SPEC_CONSTANT));
656 TraversingVisitor::visit(var.array_size);
660 void ExpressionValidator::visit(FunctionDeclaration &func)
662 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
663 TraversingVisitor::visit(func);
666 void ExpressionValidator::visit(Conditional &cond)
668 if(cond.condition->type)
670 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
671 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
672 error(cond, "Condition is not a boolean");
674 TraversingVisitor::visit(cond);
677 void ExpressionValidator::visit(Iteration &iter)
679 if(iter.condition->type)
681 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
682 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
683 error(iter, "Loop condition is not a boolean");
685 TraversingVisitor::visit(iter);
688 void ExpressionValidator::visit(Return &ret)
690 if(current_function && current_function->return_type_declaration)
692 TypeDeclaration *return_type = current_function->return_type_declaration;
693 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
696 if(ret.expression->type && ret.expression->type!=return_type)
697 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
698 ret.expression->type->name, return_type->name));
700 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
701 error(ret, "Return statement without an expression in a function not returning 'void'");
704 TraversingVisitor::visit(ret);
708 void FlowControlValidator::visit(Block &block)
710 for(const RefPtr<Statement> &s: block.body)
714 diagnose(*s, Diagnostic::WARN, "Unreachable code detected");
721 void FlowControlValidator::visit(FunctionDeclaration &func)
723 func.body.visit(*this);
725 if(func.definition==&func && func.return_type_declaration)
727 const BasicTypeDeclaration *basic_ret = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
728 if(reachable && (!basic_ret || basic_ret->kind!=BasicTypeDeclaration::VOID))
729 error(func, "Missing return statement at the end of a function not returning 'void'");
734 void FlowControlValidator::visit(Conditional &cond)
736 cond.body.visit(*this);
737 bool reachable_if_true = reachable;
739 cond.else_body.visit(*this);
740 reachable |= reachable_if_true;
743 void FlowControlValidator::visit(Iteration &iter)
745 iter.body.visit(*this);
750 int StageInterfaceValidator::get_location(const Layout &layout)
752 return get_layout_value(layout, "location", -1);
755 void StageInterfaceValidator::visit(VariableDeclaration &var)
757 int location = (var.layout ? get_location(*var.layout) : -1);
758 if(var.interface=="in" && var.linked_declaration)
760 const Layout *linked_layout = var.linked_declaration->layout.get();
761 int linked_location = (linked_layout ? get_location(*linked_layout) : -1);
762 if(linked_location!=location)
764 error(var, format("Mismatched location %d for 'in %s'", location, var.name));
765 add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
766 var.linked_declaration->name, linked_location));
768 if(var.type_declaration && var.linked_declaration->type_declaration)
770 TypeDeclaration *type = var.type_declaration;
771 if(stage->type==Stage::GEOMETRY)
773 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
774 if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
775 type = basic->base_type;
777 if(!TypeComparer().apply(*type, *var.linked_declaration->type_declaration))
779 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
780 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
781 var.linked_declaration->name, var.linked_declaration->type_declaration->name));
786 if(location>=0 && !var.interface.empty())
788 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
790 unsigned loc_count = LocationCounter().apply(var);
791 for(unsigned i=0; i<loc_count; ++i)
793 auto j = used.find(location+i);
796 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
797 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
800 used[location+i] = &var;
806 void GlobalInterfaceValidator::apply(Module &module)
808 for(Stage &s: module.stages)
811 s.content.visit(*this);
815 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
817 auto i = used_names.find(uni.name);
818 if(i!=used_names.end())
820 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
822 error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
823 add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
825 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
827 error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
828 add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
830 if(uni.type && i->second->type && !TypeComparer().apply(*uni.type, *i->second->type))
832 string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
833 "structure" : format("type '%s'", uni.type->name));
834 error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
836 string message = "Previously declared here";
837 if(!dynamic_cast<const StructDeclaration *>(i->second->type))
838 message += format(" with type '%s'", i->second->type->name);
839 add_info(*i->second->node, message);
843 used_names.insert(make_pair(uni.name, &uni));
847 auto j = used_locations.find(uni.location);
848 if(j!=used_locations.end())
850 if(j->second->name!=uni.name)
852 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
853 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
858 for(unsigned k=0; k<uni.loc_count; ++k)
859 used_locations.insert(make_pair(uni.location+k, &uni));
863 if(uni.bind_point>=0)
865 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
866 auto j = used.find(uni.bind_point);
869 if(j->second->name!=uni.name)
871 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
872 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
876 used.insert(make_pair(uni.bind_point, &uni));
880 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
882 if(var.interface=="uniform")
886 uni.type = var.type_declaration;
890 uni.location = get_layout_value(*var.layout, "location");
891 uni.loc_count = LocationCounter().apply(var);
892 uni.desc_set = get_layout_value(*var.layout, "set", 0);
893 uni.bind_point = get_layout_value(*var.layout, "binding");
896 uniforms.push_back(uni);
897 check_uniform(uniforms.back());
901 void GlobalInterfaceValidator::visit(InterfaceBlock &iface)
903 if(iface.interface=="uniform")
907 uni.type = iface.struct_declaration;
908 uni.name = iface.block_name;
911 uni.desc_set = get_layout_value(*iface.layout, "set", 0);
912 uni.bind_point = get_layout_value(*iface.layout, "binding");
915 uniforms.push_back(uni);
916 check_uniform(uniforms.back());