3 #include <msp/core/raii.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/utils.h>
15 Validator::Validator():
20 void Validator::diagnose(Node &node, Node &provoking_node, Diagnostic::Severity severity, const string &message)
23 diag.severity = severity;
24 diag.source = node.source;
25 diag.line = node.line;
26 diag.provoking_source = provoking_node.source;
27 diag.provoking_line = provoking_node.line;
28 diag.message = message;
29 stage->diagnostics.push_back(diag);
31 last_provoker = &provoking_node;
34 void Validator::add_info(Node &node, const string &message)
37 throw logic_error("Tried to add info without a previous provoker");
38 diagnose(node, *last_provoker, Diagnostic::INFO, message);
42 DeclarationValidator::DeclarationValidator():
49 const char *DeclarationValidator::describe_variable(ScopeType scope)
53 case GLOBAL: return "global variable";
54 case STRUCT: return "struct member";
55 case INTERFACE_BLOCK: return "interface block member";
56 case FUNCTION_PARAM: return "function parameter";
57 case FUNCTION: return "local variable";
58 default: return "variable";
62 void DeclarationValidator::visit(Layout &layout)
64 for(const Layout::Qualifier &q: layout.qualifiers)
69 if(q.name=="location")
70 allowed = (variable && scope==GLOBAL);
71 else if(q.name=="binding" || q.name=="set")
75 error(layout, "Layout qualifier 'set' not allowed when targeting OpenGL");
81 TypeDeclaration *type = variable->type_declaration;
82 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
83 type = basic->base_type;
84 bool uniform = (variable->interface=="uniform");
85 allowed = (scope==GLOBAL && uniform && dynamic_cast<ImageTypeDeclaration *>(type));
86 err_descr = (uniform ? "variable of non-opaque type" : "non-uniform variable");
90 allowed = (iface_block->interface=="uniform");
91 err_descr = "non-uniform interface block";
94 else if(q.name=="constant_id")
96 allowed = (variable && scope==GLOBAL);
99 if(!variable->constant)
102 err_descr = "non-constant variable";
106 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
107 if(!basic || basic->kind<BasicTypeDeclaration::BOOL || basic->kind>BasicTypeDeclaration::INT)
110 err_descr = format("variable of type '%s'",
111 (variable->type_declaration ? variable->type_declaration->name : variable->type));
116 else if(q.name=="offset")
117 allowed = (variable && scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
118 else if(q.name=="align")
119 allowed = (scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
120 else if(q.name=="points")
122 allowed = (stage->type==Stage::GEOMETRY && iface_layout && (iface_layout->interface=="in" || iface_layout->interface=="out"));
125 else if(q.name=="lines" || q.name=="lines_adjacency" || q.name=="triangles" || q.name=="triangles_adjacency")
127 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
130 else if(q.name=="line_strip" || q.name=="triangle_strip")
132 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
135 else if(q.name=="invocations")
136 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
137 else if(q.name=="max_vertices")
138 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
139 else if(q.name=="std140" || q.name=="std430")
141 allowed = (iface_block && !variable && iface_block->interface=="uniform");
144 else if(q.name=="column_major" || q.name=="row_major")
146 allowed = (variable && scope==INTERFACE_BLOCK);
149 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(variable->type_declaration);
150 while(basic && basic->kind==BasicTypeDeclaration::ARRAY)
151 basic = dynamic_cast<BasicTypeDeclaration *>(basic->base_type);
152 allowed = (basic && basic->kind==BasicTypeDeclaration::MATRIX);
153 err_descr = "non-matrix variable";
159 if(err_descr.empty())
162 err_descr = describe_variable(scope);
164 err_descr = "interface block";
165 else if(iface_layout)
166 err_descr = format("interface '%s'", iface_layout->interface);
168 err_descr = "unknown declaration";
170 error(layout, format("Layout qualifier '%s' not allowed on %s", q.name, err_descr));
172 else if(value && !q.has_value)
173 error(layout, format("Layout qualifier '%s' requires a value", q.name));
174 else if(!value && q.has_value)
175 error(layout, format("Layout qualifier '%s' does not allow a value", q.name));
179 void DeclarationValidator::visit(InterfaceLayout &layout)
181 SetForScope<InterfaceLayout *> set_layout(iface_layout, &layout);
182 TraversingVisitor::visit(layout);
185 void DeclarationValidator::visit(BasicTypeDeclaration &type)
187 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
188 BasicTypeDeclaration::Kind base_kind = (basic_base ? basic_base->kind : BasicTypeDeclaration::VOID);
190 if(type.kind==BasicTypeDeclaration::VECTOR && (base_kind<BasicTypeDeclaration::BOOL || base_kind>BasicTypeDeclaration::FLOAT))
191 error(type, format("Invalid base type '%s' for vector type '%s'", type.base, type.name));
192 else if(type.kind==BasicTypeDeclaration::MATRIX && base_kind!=BasicTypeDeclaration::VECTOR)
193 error(type, format("Invalid base type '%s' for matrix type '%s'", type.base, type.name));
194 else if(type.kind==BasicTypeDeclaration::ARRAY && basic_base && base_kind==BasicTypeDeclaration::VOID)
195 error(type, format("Invalid base type '%s' for array type '%s'", type.base, type.name));
198 void DeclarationValidator::visit(ImageTypeDeclaration &type)
200 BasicTypeDeclaration::Kind base_kind = BasicTypeDeclaration::VOID;
201 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
202 base_kind = basic_base->kind;
203 if(base_kind!=BasicTypeDeclaration::INT && base_kind!=BasicTypeDeclaration::FLOAT)
204 error(type, format("Invalid base type '%s' for image type '%s'", type.base, type.name));
207 void DeclarationValidator::visit(StructDeclaration &strct)
209 SetForScope<ScopeType> set_scope(scope, (scope!=INTERFACE_BLOCK ? STRUCT : scope));
210 TraversingVisitor::visit(strct);
213 void DeclarationValidator::visit(VariableDeclaration &var)
215 SetForScope<VariableDeclaration *> set_var(variable, &var);
217 const char *descr = describe_variable(scope);
221 if(scope!=GLOBAL && scope!=INTERFACE_BLOCK)
222 error(var, format("Layout qualifier not allowed on %s", descr));
224 var.layout->visit(*this);
229 if(scope==STRUCT || scope==INTERFACE_BLOCK)
230 error(var, format("Constant qualifier not allowed on %s", descr));
231 if(!var.init_expression)
232 error(var, "Constant variable must have an initializer");
235 if(!var.interpolation.empty() || !var.sampling.empty())
237 if(var.interface!="in" && stage->type==Stage::VERTEX)
238 error(var, "Interpolation qualifier not allowed on vertex input");
239 else if(var.interface!="out" && stage->type==Stage::FRAGMENT)
240 error(var, "Interpolation qualifier not allowed on fragment output");
241 else if((var.interface!="in" && var.interface!="out") || (scope==FUNCTION_PARAM || scope==FUNCTION))
242 error(var, "Interpolation qualifier not allowed on non-interpolated variable");
245 if(!var.interface.empty())
247 if(iface_block && var.interface!=iface_block->interface)
248 error(var, format("Mismatched interface qualifier '%s' inside '%s' block", var.interface, iface_block->interface));
249 else if(scope==STRUCT || scope==FUNCTION)
250 error(var, format("Interface qualifier not allowed on %s", descr));
253 TypeDeclaration *type = var.type_declaration;
254 BasicTypeDeclaration::Kind kind = BasicTypeDeclaration::ALIAS;
255 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
258 type = basic->base_type;
260 if(dynamic_cast<ImageTypeDeclaration *>(type))
262 if(scope!=GLOBAL && scope!=FUNCTION_PARAM)
263 error(var, format("Type '%s' not allowed on %s", type->name, descr));
264 else if(scope==GLOBAL && var.interface!="uniform")
265 error(var, format("Type '%s' only allowed with uniform interface", type->name));
267 else if(kind==BasicTypeDeclaration::VOID)
268 error(var, "Type 'void' not allowed on variable");
269 else if(kind==BasicTypeDeclaration::BOOL && !var.interface.empty() && var.source!=BUILTIN_SOURCE)
270 error(var, "Type 'bool' not allowed on interface variable");
272 if(var.init_expression)
274 if(scope==GLOBAL && !var.constant)
275 error(var, format("Initializer not allowed on non-constant %s", descr));
276 else if(scope!=GLOBAL && scope!=FUNCTION)
277 error(var, format("Initializer not allowed on %s", descr));
279 var.init_expression->visit(*this);
283 void DeclarationValidator::visit(InterfaceBlock &iface)
285 SetForScope<ScopeType> set_scope(scope, INTERFACE_BLOCK);
286 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
288 if(stage->type==Stage::VERTEX && iface.interface=="in")
289 error(iface, "Interface block not allowed on vertex shader input");
290 else if(stage->type==Stage::FRAGMENT && iface.interface=="out")
291 error(iface, "Interface block not allowed on fragment shader output");
293 TraversingVisitor::visit(iface);
294 if(iface.struct_declaration)
295 iface.struct_declaration->visit(*this);
298 void DeclarationValidator::visit(FunctionDeclaration &func)
300 SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
301 for(const RefPtr<VariableDeclaration> &p: func.parameters)
304 func.body.visit(*this);
308 IdentifierValidator::IdentifierValidator():
309 anonymous_block(false)
312 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
314 error(statement, format("Multiple definition of %s", name));
315 add_info(previous, "Previous definition is here");
318 Statement *IdentifierValidator::find_definition(const string &name)
320 BlockDeclarationMap *decls = &declarations[current_block];
321 auto i = decls->find(name);
322 if(i==decls->end() && anonymous_block)
324 decls = &declarations[current_block->parent];
325 i = decls->find(name);
327 return (i!=decls->end() ? i->second : 0);
330 void IdentifierValidator::check_definition(const string &name, Statement &statement)
332 if(Statement *previous = find_definition(name))
333 multiple_definition(format("'%s'", name), statement, *previous);
335 record_definition(name, statement);
338 void IdentifierValidator::record_definition(const string &name, Statement &statement)
340 declarations[current_block].insert(make_pair(name, &statement));
342 declarations[current_block->parent].insert(make_pair(name, &statement));
345 void IdentifierValidator::visit(TypeDeclaration &type)
347 check_definition(type.name, type);
350 void IdentifierValidator::visit(StructDeclaration &strct)
352 check_definition(strct.name, strct);
353 TraversingVisitor::visit(strct);
356 void IdentifierValidator::visit(VariableDeclaration &var)
358 check_definition(var.name, var);
359 TraversingVisitor::visit(var);
362 void IdentifierValidator::visit(InterfaceBlock &iface)
364 string key = format("%s %s", iface.interface, iface.block_name);
365 auto i = interface_blocks.find(key);
366 if(i!=interface_blocks.end())
367 multiple_definition(format("interface block '%s %s'", iface.interface, iface.block_name), iface, *i->second);
369 interface_blocks.insert(make_pair(key, &iface));
371 if(Statement *previous = find_definition(iface.block_name))
373 if(!dynamic_cast<InterfaceBlock *>(previous))
374 multiple_definition(format("'%s'", iface.block_name), iface, *previous);
377 record_definition(iface.block_name, iface);
379 if(!iface.instance_name.empty())
380 check_definition(iface.instance_name, iface);
382 if(iface.instance_name.empty() && iface.struct_declaration)
384 // Inject anonymous interface block members into the global scope
385 for(const auto &kvp: iface.struct_declaration->members.variables)
386 check_definition(kvp.first, *kvp.second);
390 void IdentifierValidator::visit(FunctionDeclaration &func)
392 string key = func.name+func.signature;
393 auto i = overloaded_functions.find(key);
394 if(i==overloaded_functions.end())
395 overloaded_functions.insert(make_pair(key, &func));
396 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
398 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
399 if(i->second->return_type_declaration)
400 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
403 if(Statement *previous = find_definition(func.name))
405 if(!dynamic_cast<FunctionDeclaration *>(previous))
406 multiple_definition(format("'%s'", func.name), func, *previous);
409 record_definition(func.name, func);
411 if(func.definition==&func)
412 check_definition(func.name+func.signature, func);
414 TraversingVisitor::visit(func);
418 void ReferenceValidator::visit(BasicTypeDeclaration &type)
420 if(!type.base.empty() && !type.base_type)
421 error(type, format("Use of undeclared type '%s'", type.base));
424 void ReferenceValidator::visit(ImageTypeDeclaration &type)
426 if(!type.base.empty() && !type.base_type)
427 error(type, format("Use of undeclared type '%s'", type.base));
430 void ReferenceValidator::visit(VariableReference &var)
433 error(var, format("Use of undeclared variable '%s'", var.name));
434 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
435 error(var, format("Use of unlinked input variable '%s'", var.name));
438 void ReferenceValidator::visit(MemberAccess &memacc)
440 if(memacc.left->type && !memacc.declaration)
441 error(memacc, format("Use of undeclared member '%s'", memacc.member));
442 TraversingVisitor::visit(memacc);
445 void ReferenceValidator::visit(InterfaceBlockReference &iface)
447 /* An interface block reference without a declaration should be impossible
448 since references are generated based on existing declarations. */
449 if(!iface.declaration)
450 error(iface, format("Use of undeclared interface block '%s'", iface.name));
451 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
452 error(iface, format("Use of unlinked input block '%s'", iface.name));
455 void ReferenceValidator::visit(FunctionCall &call)
457 if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
459 bool have_declaration = call.constructor;
460 if(!call.constructor)
462 auto i = stage->functions.lower_bound(call.name);
463 have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
468 bool valid_types = true;
470 for(auto j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
473 append(signature, ", ", (*j)->type->name);
479 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
482 error(call, format("Call to undeclared function '%s'", call.name));
484 TraversingVisitor::visit(call);
487 void ReferenceValidator::visit(VariableDeclaration &var)
489 if(!var.type_declaration)
490 error(var, format("Use of undeclared type '%s'", var.type));
491 TraversingVisitor::visit(var);
494 void ReferenceValidator::visit(InterfaceBlock &iface)
496 if(!iface.struct_declaration)
497 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
498 TraversingVisitor::visit(iface);
501 void ReferenceValidator::visit(FunctionDeclaration &func)
503 if(!func.return_type_declaration)
504 error(func, format("Use of undeclared type '%s'", func.return_type));
505 TraversingVisitor::visit(func);
509 ExpressionValidator::ExpressionValidator():
511 constant_expression(false)
514 void ExpressionValidator::visit(VariableReference &var)
516 if(var.declaration && constant_expression && !var.declaration->constant)
517 error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
520 void ExpressionValidator::visit(InterfaceBlockReference &iface)
522 if(constant_expression)
523 error(iface, format("Reference to interface block '%s' in a constant expression", iface.name));
526 void ExpressionValidator::visit(Swizzle &swizzle)
529 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
531 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
533 else if(basic->kind==BasicTypeDeclaration::VECTOR)
539 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
541 for(unsigned i=0; i<swizzle.count; ++i)
543 unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
545 flavour = component_flavour;
546 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
548 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
549 swizzle.component_group[i], swizzle.component_group[0]));
553 if(swizzle.components[i]>=size)
554 error(swizzle, format("Access to component '%c' which is not present in '%s'",
555 swizzle.component_group[i], swizzle.left->type->name));
558 else if(swizzle.left->type)
559 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
561 TraversingVisitor::visit(swizzle);
564 void ExpressionValidator::visit(UnaryExpression &unary)
566 if(unary.expression->type)
569 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
570 else if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
572 if(constant_expression)
573 error(unary, format("Use of '%s' in a constant expression", unary.oper->token));
574 else if(!unary.expression->lvalue)
575 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
578 TraversingVisitor::visit(unary);
581 void ExpressionValidator::visit(BinaryExpression &binary)
583 if(!binary.type && binary.left->type && binary.right->type)
585 if(binary.oper->token[0]=='[')
586 error(binary, format("Can't index element of '%s' with '%s'",
587 binary.left->type->name, binary.right->type->name));
589 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
590 binary.oper->token, binary.left->type->name, binary.right->type->name));
592 TraversingVisitor::visit(binary);
595 void ExpressionValidator::visit(Assignment &assign)
597 if(assign.left->type)
599 if(constant_expression)
600 error(assign, "Assignment in constant expression");
601 else if(!assign.left->lvalue)
602 error(assign, "Target of assignment is not an lvalue");
603 if(assign.right->type)
605 if(assign.oper->token[0]!='=')
608 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
609 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
611 else if(assign.left->type!=assign.right->type)
612 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
613 assign.left->type->name, assign.right->type->name));
616 TraversingVisitor::visit(assign);
619 void ExpressionValidator::visit(TernaryExpression &ternary)
621 if(ternary.condition->type)
623 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
624 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
625 error(ternary, "Ternary operator condition is not a boolean");
626 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
627 error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
628 ternary.true_expr->type->name, ternary.false_expr->type->name));
630 TraversingVisitor::visit(ternary);
633 void ExpressionValidator::visit(VariableDeclaration &var)
635 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
636 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
637 var.type_declaration->name, var.init_expression->type->name));
640 var.layout->visit(*this);
641 if(var.init_expression)
643 SetFlag set_const(constant_expression, var.constant);
644 TraversingVisitor::visit(var.init_expression);
648 SetFlag set_const(constant_expression);
649 TraversingVisitor::visit(var.array_size);
653 void ExpressionValidator::visit(FunctionDeclaration &func)
655 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
656 TraversingVisitor::visit(func);
659 void ExpressionValidator::visit(Conditional &cond)
661 if(cond.condition->type)
663 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
664 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
665 error(cond, "Condition is not a boolean");
667 TraversingVisitor::visit(cond);
670 void ExpressionValidator::visit(Iteration &iter)
672 if(iter.condition->type)
674 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
675 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
676 error(iter, "Loop condition is not a boolean");
678 TraversingVisitor::visit(iter);
681 void ExpressionValidator::visit(Return &ret)
683 if(current_function && current_function->return_type_declaration)
685 TypeDeclaration *return_type = current_function->return_type_declaration;
686 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
689 if(ret.expression->type && ret.expression->type!=return_type)
690 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
691 ret.expression->type->name, return_type->name));
693 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
694 error(ret, "Return statement without an expression in a function not returning 'void'");
697 TraversingVisitor::visit(ret);
701 FlowControlValidator::FlowControlValidator():
705 void FlowControlValidator::visit(Block &block)
707 for(const RefPtr<Statement> &s: block.body)
711 diagnose(*s, Diagnostic::WARN, "Unreachable code detected");
718 void FlowControlValidator::visit(FunctionDeclaration &func)
720 func.body.visit(*this);
722 if(func.definition==&func && func.return_type_declaration)
724 const BasicTypeDeclaration *basic_ret = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
725 if(reachable && (!basic_ret || basic_ret->kind!=BasicTypeDeclaration::VOID))
726 error(func, "Missing return statement at the end of a function not returning 'void'");
731 void FlowControlValidator::visit(Conditional &cond)
733 cond.body.visit(*this);
734 bool reachable_if_true = reachable;
736 cond.else_body.visit(*this);
737 reachable |= reachable_if_true;
740 void FlowControlValidator::visit(Iteration &iter)
742 iter.body.visit(*this);
747 int StageInterfaceValidator::get_location(const Layout &layout)
749 return get_layout_value(layout, "location", -1);
752 void StageInterfaceValidator::visit(VariableDeclaration &var)
754 int location = (var.layout ? get_location(*var.layout) : -1);
755 if(var.interface=="in" && var.linked_declaration)
757 const Layout *linked_layout = var.linked_declaration->layout.get();
758 int linked_location = (linked_layout ? get_location(*linked_layout) : -1);
759 if(linked_location!=location)
761 error(var, format("Mismatched location %d for 'in %s'", location, var.name));
762 add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
763 var.linked_declaration->name, linked_location));
765 if(var.type_declaration && var.linked_declaration->type_declaration)
767 TypeDeclaration *type = var.type_declaration;
768 if(stage->type==Stage::GEOMETRY)
770 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
771 if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
772 type = basic->base_type;
774 if(!TypeComparer().apply(*type, *var.linked_declaration->type_declaration))
776 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
777 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
778 var.linked_declaration->name, var.linked_declaration->type_declaration->name));
783 if(location>=0 && !var.interface.empty())
785 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
787 unsigned loc_count = LocationCounter().apply(var);
788 for(unsigned i=0; i<loc_count; ++i)
790 auto j = used.find(location+i);
793 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
794 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
797 used[location+i] = &var;
803 void GlobalInterfaceValidator::apply(Module &module)
805 for(Stage &s: module.stages)
808 s.content.visit(*this);
812 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
814 auto i = used_names.find(uni.name);
815 if(i!=used_names.end())
817 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
819 error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
820 add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
822 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
824 error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
825 add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
827 if(uni.type && i->second->type && !TypeComparer().apply(*uni.type, *i->second->type))
829 string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
830 "structure" : format("type '%s'", uni.type->name));
831 error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
833 string message = "Previously declared here";
834 if(!dynamic_cast<const StructDeclaration *>(i->second->type))
835 message += format(" with type '%s'", i->second->type->name);
836 add_info(*i->second->node, message);
840 used_names.insert(make_pair(uni.name, &uni));
844 auto j = used_locations.find(uni.location);
845 if(j!=used_locations.end())
847 if(j->second->name!=uni.name)
849 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
850 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
855 for(unsigned k=0; k<uni.loc_count; ++k)
856 used_locations.insert(make_pair(uni.location+k, &uni));
860 if(uni.bind_point>=0)
862 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
863 auto j = used.find(uni.bind_point);
866 if(j->second->name!=uni.name)
868 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
869 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
873 used.insert(make_pair(uni.bind_point, &uni));
877 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
879 if(var.interface=="uniform")
883 uni.type = var.type_declaration;
887 uni.location = get_layout_value(*var.layout, "location");
888 uni.loc_count = LocationCounter().apply(var);
889 uni.desc_set = get_layout_value(*var.layout, "set", 0);
890 uni.bind_point = get_layout_value(*var.layout, "binding");
893 uniforms.push_back(uni);
894 check_uniform(uniforms.back());
898 void GlobalInterfaceValidator::visit(InterfaceBlock &iface)
900 if(iface.interface=="uniform")
904 uni.type = iface.struct_declaration;
905 uni.name = iface.block_name;
908 uni.desc_set = get_layout_value(*iface.layout, "set", 0);
909 uni.bind_point = get_layout_value(*iface.layout, "binding");
912 uniforms.push_back(uni);
913 check_uniform(uniforms.back());