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.interface.empty() && var.source!=BUILTIN_SOURCE)
258 error(var, "Type 'bool' not allowed on interface variable");
260 if(var.init_expression)
262 if(scope==GLOBAL && !var.constant)
263 error(var, format("Initializer not allowed on non-constant %s", descr));
264 else if(scope!=GLOBAL && scope!=FUNCTION)
265 error(var, format("Initializer not allowed on %s", descr));
267 var.init_expression->visit(*this);
271 void DeclarationValidator::visit(InterfaceBlock &iface)
273 SetForScope<ScopeType> set_scope(scope, INTERFACE_BLOCK);
274 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
276 if(stage->type==Stage::VERTEX && iface.interface=="in")
277 error(iface, "Interface block not allowed on vertex shader input");
278 else if(stage->type==Stage::FRAGMENT && iface.interface=="out")
279 error(iface, "Interface block not allowed on fragment shader output");
281 TraversingVisitor::visit(iface);
282 if(iface.struct_declaration)
283 iface.struct_declaration->visit(*this);
286 void DeclarationValidator::visit(FunctionDeclaration &func)
288 SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
289 for(const RefPtr<VariableDeclaration> &p: func.parameters)
292 func.body.visit(*this);
296 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
298 error(statement, format("Multiple definition of %s", name));
299 add_info(previous, "Previous definition is here");
302 Statement *IdentifierValidator::find_definition(const string &name)
304 BlockDeclarationMap *decls = &declarations[current_block];
305 auto i = decls->find(name);
306 if(i==decls->end() && anonymous_block)
308 decls = &declarations[current_block->parent];
309 i = decls->find(name);
311 return (i!=decls->end() ? i->second : 0);
314 void IdentifierValidator::check_definition(const string &name, Statement &statement)
316 if(Statement *previous = find_definition(name))
317 multiple_definition(format("'%s'", name), statement, *previous);
319 record_definition(name, statement);
322 void IdentifierValidator::record_definition(const string &name, Statement &statement)
324 declarations[current_block].insert(make_pair(name, &statement));
326 declarations[current_block->parent].insert(make_pair(name, &statement));
329 void IdentifierValidator::visit(TypeDeclaration &type)
331 check_definition(type.name, type);
334 void IdentifierValidator::visit(StructDeclaration &strct)
336 check_definition(strct.name, strct);
337 TraversingVisitor::visit(strct);
340 void IdentifierValidator::visit(VariableDeclaration &var)
342 check_definition(var.name, var);
343 TraversingVisitor::visit(var);
346 void IdentifierValidator::visit(InterfaceBlock &iface)
348 string key = format("%s %s", iface.interface, iface.block_name);
349 auto i = interface_blocks.find(key);
350 if(i!=interface_blocks.end())
351 multiple_definition(format("interface block '%s %s'", iface.interface, iface.block_name), iface, *i->second);
353 interface_blocks.insert(make_pair(key, &iface));
355 if(Statement *previous = find_definition(iface.block_name))
357 if(!dynamic_cast<InterfaceBlock *>(previous))
358 multiple_definition(format("'%s'", iface.block_name), iface, *previous);
361 record_definition(iface.block_name, iface);
363 if(!iface.instance_name.empty())
364 check_definition(iface.instance_name, iface);
366 if(iface.instance_name.empty() && iface.struct_declaration)
368 // Inject anonymous interface block members into the global scope
369 for(const auto &kvp: iface.struct_declaration->members.variables)
370 check_definition(kvp.first, *kvp.second);
374 void IdentifierValidator::visit(FunctionDeclaration &func)
376 string key = func.name+func.signature;
377 auto i = overloaded_functions.find(key);
378 if(i==overloaded_functions.end())
379 overloaded_functions.insert(make_pair(key, &func));
380 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
382 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
383 if(i->second->return_type_declaration)
384 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
387 if(Statement *previous = find_definition(func.name))
389 if(!dynamic_cast<FunctionDeclaration *>(previous))
390 multiple_definition(format("'%s'", func.name), func, *previous);
393 record_definition(func.name, func);
395 if(func.definition==&func)
396 check_definition(func.name+func.signature, func);
398 TraversingVisitor::visit(func);
402 void ReferenceValidator::visit(BasicTypeDeclaration &type)
404 if(!type.base.empty() && !type.base_type)
405 error(type, format("Use of undeclared type '%s'", type.base));
408 void ReferenceValidator::visit(ImageTypeDeclaration &type)
410 if(!type.base.empty() && !type.base_type)
411 error(type, format("Use of undeclared type '%s'", type.base));
414 void ReferenceValidator::visit(VariableReference &var)
417 error(var, format("Use of undeclared variable '%s'", var.name));
418 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
419 error(var, format("Use of unlinked input variable '%s'", var.name));
422 void ReferenceValidator::visit(MemberAccess &memacc)
424 if(memacc.left->type && !memacc.declaration)
425 error(memacc, format("Use of undeclared member '%s'", memacc.member));
426 TraversingVisitor::visit(memacc);
429 void ReferenceValidator::visit(InterfaceBlockReference &iface)
431 /* An interface block reference without a declaration should be impossible
432 since references are generated based on existing declarations. */
433 if(!iface.declaration)
434 error(iface, format("Use of undeclared interface block '%s'", iface.name));
435 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
436 error(iface, format("Use of unlinked input block '%s'", iface.name));
439 void ReferenceValidator::visit(FunctionCall &call)
441 if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
443 bool have_declaration = call.constructor;
444 if(!call.constructor)
446 auto i = stage->functions.lower_bound(call.name);
447 have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
452 bool valid_types = true;
454 for(auto j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
457 append(signature, ", ", (*j)->type->name);
463 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
466 error(call, format("Call to undeclared function '%s'", call.name));
468 TraversingVisitor::visit(call);
471 void ReferenceValidator::visit(VariableDeclaration &var)
473 if(!var.type_declaration)
474 error(var, format("Use of undeclared type '%s'", var.type));
475 TraversingVisitor::visit(var);
478 void ReferenceValidator::visit(InterfaceBlock &iface)
480 if(!iface.struct_declaration)
481 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
482 TraversingVisitor::visit(iface);
485 void ReferenceValidator::visit(FunctionDeclaration &func)
487 if(!func.return_type_declaration)
488 error(func, format("Use of undeclared type '%s'", func.return_type));
489 TraversingVisitor::visit(func);
493 void ExpressionValidator::visit(VariableReference &var)
495 if(var.declaration && constant_expression && !var.declaration->constant)
496 error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
499 void ExpressionValidator::visit(InterfaceBlockReference &iface)
501 if(constant_expression)
502 error(iface, format("Reference to interface block '%s' in a constant expression", iface.name));
505 void ExpressionValidator::visit(Swizzle &swizzle)
508 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
510 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
512 else if(basic->kind==BasicTypeDeclaration::VECTOR)
518 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
520 for(unsigned i=0; i<swizzle.count; ++i)
522 unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
524 flavour = component_flavour;
525 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
527 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
528 swizzle.component_group[i], swizzle.component_group[0]));
532 if(swizzle.components[i]>=size)
533 error(swizzle, format("Access to component '%c' which is not present in '%s'",
534 swizzle.component_group[i], swizzle.left->type->name));
537 else if(swizzle.left->type)
538 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
540 TraversingVisitor::visit(swizzle);
543 void ExpressionValidator::visit(UnaryExpression &unary)
545 if(unary.expression->type)
548 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
549 else if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
551 if(constant_expression)
552 error(unary, format("Use of '%s' in a constant expression", unary.oper->token));
553 else if(!unary.expression->lvalue)
554 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
557 TraversingVisitor::visit(unary);
560 void ExpressionValidator::visit(BinaryExpression &binary)
562 if(!binary.type && binary.left->type && binary.right->type)
564 if(binary.oper->token[0]=='[')
565 error(binary, format("Can't index element of '%s' with '%s'",
566 binary.left->type->name, binary.right->type->name));
568 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
569 binary.oper->token, binary.left->type->name, binary.right->type->name));
571 TraversingVisitor::visit(binary);
574 void ExpressionValidator::visit(Assignment &assign)
576 if(assign.left->type)
578 if(constant_expression)
579 error(assign, "Assignment in constant expression");
580 else if(!assign.left->lvalue)
581 error(assign, "Target of assignment is not an lvalue");
582 if(assign.right->type)
584 if(assign.oper->token[0]!='=')
587 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
588 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
590 else if(assign.left->type!=assign.right->type)
591 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
592 assign.left->type->name, assign.right->type->name));
595 TraversingVisitor::visit(assign);
598 void ExpressionValidator::visit(TernaryExpression &ternary)
600 if(ternary.condition->type)
602 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
603 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
604 error(ternary, "Ternary operator condition is not a boolean");
605 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
606 error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
607 ternary.true_expr->type->name, ternary.false_expr->type->name));
609 TraversingVisitor::visit(ternary);
612 void ExpressionValidator::visit(VariableDeclaration &var)
614 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
615 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
616 var.type_declaration->name, var.init_expression->type->name));
619 var.layout->visit(*this);
620 if(var.init_expression)
622 SetFlag set_const(constant_expression, var.constant);
623 TraversingVisitor::visit(var.init_expression);
627 SetFlag set_const(constant_expression);
628 TraversingVisitor::visit(var.array_size);
632 void ExpressionValidator::visit(FunctionDeclaration &func)
634 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
635 TraversingVisitor::visit(func);
638 void ExpressionValidator::visit(Conditional &cond)
640 if(cond.condition->type)
642 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
643 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
644 error(cond, "Condition is not a boolean");
646 TraversingVisitor::visit(cond);
649 void ExpressionValidator::visit(Iteration &iter)
651 if(iter.condition->type)
653 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
654 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
655 error(iter, "Loop condition is not a boolean");
657 TraversingVisitor::visit(iter);
660 void ExpressionValidator::visit(Return &ret)
662 if(current_function && current_function->return_type_declaration)
664 TypeDeclaration *return_type = current_function->return_type_declaration;
665 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
668 if(ret.expression->type && ret.expression->type!=return_type)
669 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
670 ret.expression->type->name, return_type->name));
672 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
673 error(ret, "Return statement without an expression in a function not returning 'void'");
676 TraversingVisitor::visit(ret);
680 void FlowControlValidator::visit(Block &block)
682 for(const RefPtr<Statement> &s: block.body)
686 diagnose(*s, Diagnostic::WARN, "Unreachable code detected");
693 void FlowControlValidator::visit(FunctionDeclaration &func)
695 func.body.visit(*this);
697 if(func.definition==&func && func.return_type_declaration)
699 const BasicTypeDeclaration *basic_ret = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
700 if(reachable && (!basic_ret || basic_ret->kind!=BasicTypeDeclaration::VOID))
701 error(func, "Missing return statement at the end of a function not returning 'void'");
706 void FlowControlValidator::visit(Conditional &cond)
708 cond.body.visit(*this);
709 bool reachable_if_true = reachable;
711 cond.else_body.visit(*this);
712 reachable |= reachable_if_true;
715 void FlowControlValidator::visit(Iteration &iter)
717 iter.body.visit(*this);
722 int StageInterfaceValidator::get_location(const Layout &layout)
724 return get_layout_value(layout, "location", -1);
727 void StageInterfaceValidator::visit(VariableDeclaration &var)
729 int location = (var.layout ? get_location(*var.layout) : -1);
730 if(var.interface=="in" && var.linked_declaration)
732 const Layout *linked_layout = var.linked_declaration->layout.get();
733 int linked_location = (linked_layout ? get_location(*linked_layout) : -1);
734 if(linked_location!=location)
736 error(var, format("Mismatched location %d for 'in %s'", location, var.name));
737 add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
738 var.linked_declaration->name, linked_location));
740 if(var.type_declaration && var.linked_declaration->type_declaration)
742 TypeDeclaration *type = var.type_declaration;
743 if(stage->type==Stage::GEOMETRY)
745 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
746 if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
747 type = basic->base_type;
749 if(!TypeComparer().apply(*type, *var.linked_declaration->type_declaration))
751 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
752 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
753 var.linked_declaration->name, var.linked_declaration->type_declaration->name));
758 if(location>=0 && !var.interface.empty())
760 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
762 unsigned loc_count = LocationCounter().apply(var);
763 for(unsigned i=0; i<loc_count; ++i)
765 auto j = used.find(location+i);
768 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
769 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
772 used[location+i] = &var;
778 void GlobalInterfaceValidator::apply(Module &module)
780 for(Stage &s: module.stages)
783 s.content.visit(*this);
787 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
789 auto i = used_names.find(uni.name);
790 if(i!=used_names.end())
792 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
794 error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
795 add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
797 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
799 error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
800 add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
802 if(uni.type && i->second->type && !TypeComparer().apply(*uni.type, *i->second->type))
804 string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
805 "structure" : format("type '%s'", uni.type->name));
806 error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
808 string message = "Previously declared here";
809 if(!dynamic_cast<const StructDeclaration *>(i->second->type))
810 message += format(" with type '%s'", i->second->type->name);
811 add_info(*i->second->node, message);
815 used_names.insert(make_pair(uni.name, &uni));
819 auto j = used_locations.find(uni.location);
820 if(j!=used_locations.end())
822 if(j->second->name!=uni.name)
824 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
825 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
830 for(unsigned k=0; k<uni.loc_count; ++k)
831 used_locations.insert(make_pair(uni.location+k, &uni));
835 if(uni.bind_point>=0)
837 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
838 auto j = used.find(uni.bind_point);
841 if(j->second->name!=uni.name)
843 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
844 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
848 used.insert(make_pair(uni.bind_point, &uni));
852 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
854 if(var.interface=="uniform")
858 uni.type = var.type_declaration;
862 uni.location = get_layout_value(*var.layout, "location");
863 uni.loc_count = LocationCounter().apply(var);
864 uni.desc_set = get_layout_value(*var.layout, "set", 0);
865 uni.bind_point = get_layout_value(*var.layout, "binding");
868 uniforms.push_back(uni);
869 check_uniform(uniforms.back());
873 void GlobalInterfaceValidator::visit(InterfaceBlock &iface)
875 if(iface.interface=="uniform")
879 uni.type = iface.struct_declaration;
880 uni.name = iface.block_name;
883 uni.desc_set = get_layout_value(*iface.layout, "set", 0);
884 uni.bind_point = get_layout_value(*iface.layout, "binding");
887 uniforms.push_back(uni);
888 check_uniform(uniforms.back());