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(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
69 if(i->name=="location")
70 allowed = (variable && scope==GLOBAL);
71 else if(i->name=="binding" || i->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(i->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(i->name=="offset")
117 allowed = (variable && scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
118 else if(i->name=="align")
119 allowed = (scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
120 else if(i->name=="points")
122 allowed = (stage->type==Stage::GEOMETRY && iface_layout && (iface_layout->interface=="in" || iface_layout->interface=="out"));
125 else if(i->name=="lines" || i->name=="lines_adjacency" || i->name=="triangles" || i->name=="triangles_adjacency")
127 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
130 else if(i->name=="line_strip" || i->name=="triangle_strip")
132 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
135 else if(i->name=="invocations")
136 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
137 else if(i->name=="max_vertices")
138 allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
139 else if(i->name=="std140" || i->name=="std430")
141 allowed = (iface_block && !variable && iface_block->interface=="uniform");
144 else if(i->name=="column_major" || i->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", i->name, err_descr));
172 else if(value && !i->has_value)
173 error(layout, format("Layout qualifier '%s' requires a value", i->name));
174 else if(!value && i->has_value)
175 error(layout, format("Layout qualifier '%s' does not allow a value", i->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 while(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(type))
255 type = basic->base_type;
256 if(dynamic_cast<ImageTypeDeclaration *>(type))
258 if(scope!=GLOBAL && scope!=FUNCTION_PARAM)
259 error(var, format("Type '%s' not allowed on %s", type->name, descr));
260 else if(scope==GLOBAL && var.interface!="uniform")
261 error(var, format("Type '%s' only allowed with uniform interface", type->name));
264 if(var.init_expression)
266 if(scope==GLOBAL && !var.constant)
267 error(var, format("Initializer not allowed on non-constant %s", descr));
268 else if(scope!=GLOBAL && scope!=FUNCTION)
269 error(var, format("Initializer not allowed on %s", descr));
271 var.init_expression->visit(*this);
275 void DeclarationValidator::visit(InterfaceBlock &iface)
277 SetForScope<ScopeType> set_scope(scope, INTERFACE_BLOCK);
278 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
280 if(stage->type==Stage::VERTEX && iface.interface=="in")
281 error(iface, "Interface block not allowed on vertex shader input");
282 else if(stage->type==Stage::FRAGMENT && iface.interface=="out")
283 error(iface, "Interface block not allowed on fragment shader output");
285 TraversingVisitor::visit(iface);
286 if(iface.struct_declaration)
287 iface.struct_declaration->visit(*this);
290 void DeclarationValidator::visit(FunctionDeclaration &func)
292 SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
293 for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
296 func.body.visit(*this);
300 IdentifierValidator::IdentifierValidator():
301 anonymous_block(false)
304 void IdentifierValidator::multiple_definition(const string &name, Statement &statement, Statement &previous)
306 error(statement, format("Multiple definition of %s", name));
307 add_info(previous, "Previous definition is here");
310 Statement *IdentifierValidator::find_definition(const string &name)
312 BlockDeclarationMap *decls = &declarations[current_block];
313 BlockDeclarationMap::const_iterator i = decls->find(name);
314 if(i==decls->end() && anonymous_block)
316 decls = &declarations[current_block->parent];
317 i = decls->find(name);
319 return (i!=decls->end() ? i->second : 0);
322 void IdentifierValidator::check_definition(const string &name, Statement &statement)
324 if(Statement *previous = find_definition(name))
325 multiple_definition(format("'%s'", name), statement, *previous);
327 record_definition(name, statement);
330 void IdentifierValidator::record_definition(const string &name, Statement &statement)
332 declarations[current_block].insert(make_pair(name, &statement));
334 declarations[current_block->parent].insert(make_pair(name, &statement));
337 void IdentifierValidator::visit(TypeDeclaration &type)
339 check_definition(type.name, type);
342 void IdentifierValidator::visit(StructDeclaration &strct)
344 check_definition(strct.name, strct);
345 TraversingVisitor::visit(strct);
348 void IdentifierValidator::visit(VariableDeclaration &var)
350 check_definition(var.name, var);
351 TraversingVisitor::visit(var);
354 void IdentifierValidator::visit(InterfaceBlock &iface)
356 string key = format("%s %s", iface.interface, iface.block_name);
357 map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
358 if(i!=interface_blocks.end())
359 multiple_definition(format("interface block '%s %s'", iface.interface, iface.block_name), iface, *i->second);
361 interface_blocks.insert(make_pair(key, &iface));
363 if(Statement *previous = find_definition(iface.block_name))
365 if(!dynamic_cast<InterfaceBlock *>(previous))
366 multiple_definition(format("'%s'", iface.block_name), iface, *previous);
369 record_definition(iface.block_name, iface);
371 if(!iface.instance_name.empty())
372 check_definition(iface.instance_name, iface);
374 if(iface.instance_name.empty() && iface.struct_declaration)
376 // Inject anonymous interface block members into the global scope
377 const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
378 for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
379 check_definition(j->first, *j->second);
383 void IdentifierValidator::visit(FunctionDeclaration &func)
385 string key = func.name+func.signature;
386 map<string, FunctionDeclaration *>::const_iterator i = overloaded_functions.find(key);
387 if(i==overloaded_functions.end())
388 overloaded_functions.insert(make_pair(key, &func));
389 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
391 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
392 if(i->second->return_type_declaration)
393 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
396 if(Statement *previous = find_definition(func.name))
398 if(!dynamic_cast<FunctionDeclaration *>(previous))
399 multiple_definition(format("'%s'", func.name), func, *previous);
402 record_definition(func.name, func);
404 if(func.definition==&func)
405 check_definition(func.name+func.signature, func);
407 TraversingVisitor::visit(func);
411 void ReferenceValidator::visit(BasicTypeDeclaration &type)
413 if(!type.base.empty() && !type.base_type)
414 error(type, format("Use of undeclared type '%s'", type.base));
417 void ReferenceValidator::visit(ImageTypeDeclaration &type)
419 if(!type.base.empty() && !type.base_type)
420 error(type, format("Use of undeclared type '%s'", type.base));
423 void ReferenceValidator::visit(VariableReference &var)
426 error(var, format("Use of undeclared variable '%s'", var.name));
427 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
428 error(var, format("Use of unlinked input variable '%s'", var.name));
431 void ReferenceValidator::visit(MemberAccess &memacc)
433 if(memacc.left->type && !memacc.declaration)
434 error(memacc, format("Use of undeclared member '%s'", memacc.member));
435 TraversingVisitor::visit(memacc);
438 void ReferenceValidator::visit(InterfaceBlockReference &iface)
440 /* An interface block reference without a declaration should be impossible
441 since references are generated based on existing declarations. */
442 if(!iface.declaration)
443 error(iface, format("Use of undeclared interface block '%s'", iface.name));
444 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
445 error(iface, format("Use of unlinked input block '%s'", iface.name));
448 void ReferenceValidator::visit(FunctionCall &call)
450 if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
452 bool have_declaration = call.constructor;
453 if(!call.constructor)
455 map<string, FunctionDeclaration *>::iterator i = stage->functions.lower_bound(call.name);
456 have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
461 bool valid_types = true;
463 for(NodeArray<Expression>::const_iterator j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
466 append(signature, ", ", (*j)->type->name);
472 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
475 error(call, format("Call to undeclared function '%s'", call.name));
477 TraversingVisitor::visit(call);
480 void ReferenceValidator::visit(VariableDeclaration &var)
482 if(!var.type_declaration)
483 error(var, format("Use of undeclared type '%s'", var.type));
484 TraversingVisitor::visit(var);
487 void ReferenceValidator::visit(InterfaceBlock &iface)
489 if(!iface.struct_declaration)
490 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
491 TraversingVisitor::visit(iface);
494 void ReferenceValidator::visit(FunctionDeclaration &func)
496 if(!func.return_type_declaration)
497 error(func, format("Use of undeclared type '%s'", func.return_type));
498 TraversingVisitor::visit(func);
502 ExpressionValidator::ExpressionValidator():
506 void ExpressionValidator::visit(Swizzle &swizzle)
509 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
511 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
513 else if(basic->kind==BasicTypeDeclaration::VECTOR)
519 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
521 for(unsigned i=0; i<swizzle.count; ++i)
523 unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
525 flavour = component_flavour;
526 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
528 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
529 swizzle.component_group[i], swizzle.component_group[0]));
533 if(swizzle.components[i]>=size)
534 error(swizzle, format("Access to component '%c' which is not present in '%s'",
535 swizzle.component_group[i], swizzle.left->type->name));
538 else if(swizzle.left->type)
539 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
541 TraversingVisitor::visit(swizzle);
544 void ExpressionValidator::visit(UnaryExpression &unary)
546 if(unary.expression->type)
549 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
550 else if((unary.oper->token[1]=='+' || unary.oper->token[1]=='-') && !unary.expression->lvalue)
551 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
553 TraversingVisitor::visit(unary);
556 void ExpressionValidator::visit(BinaryExpression &binary)
558 if(!binary.type && binary.left->type && binary.right->type)
560 if(binary.oper->token[0]=='[')
561 error(binary, format("Can't index element of '%s' with '%s'",
562 binary.left->type->name, binary.right->type->name));
564 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
565 binary.oper->token, binary.left->type->name, binary.right->type->name));
567 TraversingVisitor::visit(binary);
570 void ExpressionValidator::visit(Assignment &assign)
572 if(assign.left->type)
574 if(!assign.left->lvalue)
575 error(assign, "Target of assignment is not an lvalue");
576 if(assign.right->type)
578 if(assign.oper->token[0]!='=')
581 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
582 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
584 else if(assign.left->type!=assign.right->type)
585 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
586 assign.left->type->name, assign.right->type->name));
589 TraversingVisitor::visit(assign);
592 void ExpressionValidator::visit(TernaryExpression &ternary)
594 if(ternary.condition->type)
596 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
597 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
598 error(ternary, "Ternary operator condition is not a boolean");
599 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
600 error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
601 ternary.true_expr->type->name, ternary.false_expr->type->name));
603 TraversingVisitor::visit(ternary);
606 void ExpressionValidator::visit(VariableDeclaration &var)
608 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
609 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
610 var.type_declaration->name, var.init_expression->type->name));
611 TraversingVisitor::visit(var);
614 void ExpressionValidator::visit(FunctionDeclaration &func)
616 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
617 TraversingVisitor::visit(func);
620 void ExpressionValidator::visit(Return &ret)
622 if(current_function && current_function->return_type_declaration)
624 TypeDeclaration *return_type = current_function->return_type_declaration;
625 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
628 if(ret.expression->type && ret.expression->type!=return_type)
629 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
630 ret.expression->type->name, return_type->name));
632 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
633 error(ret, "Return statement without an expression in a function not returning 'void'");
636 TraversingVisitor::visit(ret);
640 int StageInterfaceValidator::get_location(const Layout &layout)
642 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
643 if(i->name=="location")
648 void StageInterfaceValidator::visit(VariableDeclaration &var)
650 int location = (var.layout ? get_location(*var.layout) : -1);
651 if(var.interface=="in" && var.linked_declaration)
653 const Layout *linked_layout = var.linked_declaration->layout.get();
654 int linked_location = (linked_layout ? get_location(*linked_layout) : -1);
655 if(linked_location!=location)
657 error(var, format("Mismatched location %d for 'in %s'", location, var.name));
658 add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
659 var.linked_declaration->name, linked_location));
661 if(var.type_declaration && var.linked_declaration->type_declaration)
663 const TypeDeclaration *type = var.type_declaration;
664 if(stage->type==Stage::GEOMETRY)
666 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
667 if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
668 type = basic->base_type;
670 if(!is_same_type(*type, *var.linked_declaration->type_declaration))
672 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
673 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
674 var.linked_declaration->name, var.linked_declaration->type_declaration->name));
679 if(location>=0 && !var.interface.empty())
681 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
683 unsigned loc_count = LocationCounter().apply(var);
684 for(unsigned i=0; i<loc_count; ++i)
686 map<unsigned, VariableDeclaration *>::const_iterator j = used.find(location+i);
689 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
690 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
693 used[location+i] = &var;
699 void GlobalInterfaceValidator::apply(Module &module)
701 for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
704 i->content.visit(*this);
708 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
710 map<std::string, const Uniform *>::const_iterator i = used_names.find(uni.name);
711 if(i!=used_names.end())
713 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
715 error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
716 add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
718 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
720 error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
721 add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
723 if(uni.type && i->second->type && !is_same_type(*uni.type, *i->second->type))
725 string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
726 "structure" : format("type '%s'", uni.type->name));
727 error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
729 string message = "Previously declared here";
730 if(!dynamic_cast<const StructDeclaration *>(i->second->type))
731 message += format(" with type '%s'", i->second->type->name);
732 add_info(*i->second->node, message);
736 used_names.insert(make_pair(uni.name, &uni));
740 map<unsigned, const Uniform *>::const_iterator j = used_locations.find(uni.location);
741 if(j!=used_locations.end())
743 if(j->second->name!=uni.name)
745 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
746 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
751 for(unsigned k=0; k<uni.loc_count; ++k)
752 used_locations.insert(make_pair(uni.location+k, &uni));
756 if(uni.bind_point>=0)
758 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
759 map<unsigned, const Uniform *>::const_iterator j = used.find(uni.bind_point);
762 if(j->second->name!=uni.name)
764 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
765 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
769 used.insert(make_pair(uni.bind_point, &uni));
773 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
775 if(var.interface=="uniform")
779 uni.type = var.type_declaration;
783 uni.location = get_layout_value(*var.layout, "location");
784 uni.loc_count = LocationCounter().apply(var);
785 uni.desc_set = get_layout_value(*var.layout, "set", 0);
786 uni.bind_point = get_layout_value(*var.layout, "binding");
789 uniforms.push_back(uni);
790 check_uniform(uniforms.back());
794 void GlobalInterfaceValidator::visit(InterfaceBlock &iface)
796 if(iface.interface=="uniform")
800 uni.type = iface.struct_declaration;
801 uni.name = iface.block_name;
804 uni.desc_set = get_layout_value(*iface.layout, "set", 0);
805 uni.bind_point = get_layout_value(*iface.layout, "binding");
808 uniforms.push_back(uni);
809 check_uniform(uniforms.back());