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 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(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
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 BlockDeclarationMap::const_iterator 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 map<string, InterfaceBlock *>::const_iterator 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 const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
386 for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
387 check_definition(j->first, *j->second);
391 void IdentifierValidator::visit(FunctionDeclaration &func)
393 string key = func.name+func.signature;
394 map<string, FunctionDeclaration *>::const_iterator i = overloaded_functions.find(key);
395 if(i==overloaded_functions.end())
396 overloaded_functions.insert(make_pair(key, &func));
397 else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
399 error(func, format("Conflicting return type '%s' for function '%s'", func.return_type_declaration->name, func.name));
400 if(i->second->return_type_declaration)
401 add_info(*i->second, format("Previously declared as returning '%s'", i->second->return_type_declaration->name));
404 if(Statement *previous = find_definition(func.name))
406 if(!dynamic_cast<FunctionDeclaration *>(previous))
407 multiple_definition(format("'%s'", func.name), func, *previous);
410 record_definition(func.name, func);
412 if(func.definition==&func)
413 check_definition(func.name+func.signature, func);
415 TraversingVisitor::visit(func);
419 void ReferenceValidator::visit(BasicTypeDeclaration &type)
421 if(!type.base.empty() && !type.base_type)
422 error(type, format("Use of undeclared type '%s'", type.base));
425 void ReferenceValidator::visit(ImageTypeDeclaration &type)
427 if(!type.base.empty() && !type.base_type)
428 error(type, format("Use of undeclared type '%s'", type.base));
431 void ReferenceValidator::visit(VariableReference &var)
434 error(var, format("Use of undeclared variable '%s'", var.name));
435 else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
436 error(var, format("Use of unlinked input variable '%s'", var.name));
439 void ReferenceValidator::visit(MemberAccess &memacc)
441 if(memacc.left->type && !memacc.declaration)
442 error(memacc, format("Use of undeclared member '%s'", memacc.member));
443 TraversingVisitor::visit(memacc);
446 void ReferenceValidator::visit(InterfaceBlockReference &iface)
448 /* An interface block reference without a declaration should be impossible
449 since references are generated based on existing declarations. */
450 if(!iface.declaration)
451 error(iface, format("Use of undeclared interface block '%s'", iface.name));
452 else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
453 error(iface, format("Use of unlinked input block '%s'", iface.name));
456 void ReferenceValidator::visit(FunctionCall &call)
458 if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
460 bool have_declaration = call.constructor;
461 if(!call.constructor)
463 map<string, FunctionDeclaration *>::iterator i = stage->functions.lower_bound(call.name);
464 have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
469 bool valid_types = true;
471 for(NodeArray<Expression>::const_iterator j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
474 append(signature, ", ", (*j)->type->name);
480 error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
483 error(call, format("Call to undeclared function '%s'", call.name));
485 TraversingVisitor::visit(call);
488 void ReferenceValidator::visit(VariableDeclaration &var)
490 if(!var.type_declaration)
491 error(var, format("Use of undeclared type '%s'", var.type));
492 TraversingVisitor::visit(var);
495 void ReferenceValidator::visit(InterfaceBlock &iface)
497 if(!iface.struct_declaration)
498 error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
499 TraversingVisitor::visit(iface);
502 void ReferenceValidator::visit(FunctionDeclaration &func)
504 if(!func.return_type_declaration)
505 error(func, format("Use of undeclared type '%s'", func.return_type));
506 TraversingVisitor::visit(func);
510 ExpressionValidator::ExpressionValidator():
512 constant_expression(false)
515 void ExpressionValidator::visit(VariableReference &var)
517 if(var.declaration && constant_expression && !var.declaration->constant)
518 error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
521 void ExpressionValidator::visit(InterfaceBlockReference &iface)
523 if(constant_expression)
524 error(iface, format("Reference to interface block '%s' in a constant expression", iface.name));
527 void ExpressionValidator::visit(Swizzle &swizzle)
530 if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
532 if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
534 else if(basic->kind==BasicTypeDeclaration::VECTOR)
540 static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
542 for(unsigned i=0; i<swizzle.count; ++i)
544 unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
546 flavour = component_flavour;
547 else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
549 error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
550 swizzle.component_group[i], swizzle.component_group[0]));
554 if(swizzle.components[i]>=size)
555 error(swizzle, format("Access to component '%c' which is not present in '%s'",
556 swizzle.component_group[i], swizzle.left->type->name));
559 else if(swizzle.left->type)
560 error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
562 TraversingVisitor::visit(swizzle);
565 void ExpressionValidator::visit(UnaryExpression &unary)
567 if(unary.expression->type)
570 error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
571 else if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
573 if(constant_expression)
574 error(unary, format("Use of '%s' in a constant expression", unary.oper->token));
575 else if(!unary.expression->lvalue)
576 error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
579 TraversingVisitor::visit(unary);
582 void ExpressionValidator::visit(BinaryExpression &binary)
584 if(!binary.type && binary.left->type && binary.right->type)
586 if(binary.oper->token[0]=='[')
587 error(binary, format("Can't index element of '%s' with '%s'",
588 binary.left->type->name, binary.right->type->name));
590 error(binary, format("No matching operator '%s' found for '%s' and '%s'",
591 binary.oper->token, binary.left->type->name, binary.right->type->name));
593 TraversingVisitor::visit(binary);
596 void ExpressionValidator::visit(Assignment &assign)
598 if(assign.left->type)
600 if(constant_expression)
601 error(assign, "Assignment in constant expression");
602 else if(!assign.left->lvalue)
603 error(assign, "Target of assignment is not an lvalue");
604 if(assign.right->type)
606 if(assign.oper->token[0]!='=')
609 error(assign, format("No matching operator '%s' found for '%s' and '%s'",
610 string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
612 else if(assign.left->type!=assign.right->type)
613 error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
614 assign.left->type->name, assign.right->type->name));
617 TraversingVisitor::visit(assign);
620 void ExpressionValidator::visit(TernaryExpression &ternary)
622 if(ternary.condition->type)
624 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
625 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
626 error(ternary, "Ternary operator condition is not a boolean");
627 else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
628 error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
629 ternary.true_expr->type->name, ternary.false_expr->type->name));
631 TraversingVisitor::visit(ternary);
634 void ExpressionValidator::visit(VariableDeclaration &var)
636 if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
637 error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
638 var.type_declaration->name, var.init_expression->type->name));
641 var.layout->visit(*this);
642 if(var.init_expression)
644 SetFlag set_const(constant_expression, var.constant);
645 TraversingVisitor::visit(var.init_expression);
649 SetFlag set_const(constant_expression);
650 TraversingVisitor::visit(var.array_size);
654 void ExpressionValidator::visit(FunctionDeclaration &func)
656 SetForScope<FunctionDeclaration *> set_func(current_function, &func);
657 TraversingVisitor::visit(func);
660 void ExpressionValidator::visit(Conditional &cond)
662 if(cond.condition->type)
664 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
665 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
666 error(cond, "Condition is not a boolean");
668 TraversingVisitor::visit(cond);
671 void ExpressionValidator::visit(Iteration &iter)
673 if(iter.condition->type)
675 BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
676 if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
677 error(iter, "Loop condition is not a boolean");
679 TraversingVisitor::visit(iter);
682 void ExpressionValidator::visit(Return &ret)
684 if(current_function && current_function->return_type_declaration)
686 TypeDeclaration *return_type = current_function->return_type_declaration;
687 BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
690 if(ret.expression->type && ret.expression->type!=return_type)
691 error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
692 ret.expression->type->name, return_type->name));
694 else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
695 error(ret, "Return statement without an expression in a function not returning 'void'");
698 TraversingVisitor::visit(ret);
702 FlowControlValidator::FlowControlValidator():
706 void FlowControlValidator::visit(Block &block)
708 for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); ++i)
712 diagnose(**i, Diagnostic::WARN, "Unreachable code detected");
719 void FlowControlValidator::visit(FunctionDeclaration &func)
721 func.body.visit(*this);
723 if(func.definition==&func && func.return_type_declaration)
725 const BasicTypeDeclaration *basic_ret = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
726 if(reachable && (!basic_ret || basic_ret->kind!=BasicTypeDeclaration::VOID))
727 error(func, "Missing return statement at the end of a function not returning 'void'");
732 void FlowControlValidator::visit(Conditional &cond)
734 cond.body.visit(*this);
735 bool reachable_if_true = reachable;
737 cond.else_body.visit(*this);
738 reachable |= reachable_if_true;
741 void FlowControlValidator::visit(Iteration &iter)
743 iter.body.visit(*this);
748 int StageInterfaceValidator::get_location(const Layout &layout)
750 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
751 if(i->name=="location")
756 void StageInterfaceValidator::visit(VariableDeclaration &var)
758 int location = (var.layout ? get_location(*var.layout) : -1);
759 if(var.interface=="in" && var.linked_declaration)
761 const Layout *linked_layout = var.linked_declaration->layout.get();
762 int linked_location = (linked_layout ? get_location(*linked_layout) : -1);
763 if(linked_location!=location)
765 error(var, format("Mismatched location %d for 'in %s'", location, var.name));
766 add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
767 var.linked_declaration->name, linked_location));
769 if(var.type_declaration && var.linked_declaration->type_declaration)
771 TypeDeclaration *type = var.type_declaration;
772 if(stage->type==Stage::GEOMETRY)
774 if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
775 if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
776 type = basic->base_type;
778 if(!TypeComparer().apply(*type, *var.linked_declaration->type_declaration))
780 error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
781 add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
782 var.linked_declaration->name, var.linked_declaration->type_declaration->name));
787 if(location>=0 && !var.interface.empty())
789 map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
791 unsigned loc_count = LocationCounter().apply(var);
792 for(unsigned i=0; i<loc_count; ++i)
794 map<unsigned, VariableDeclaration *>::const_iterator j = used.find(location+i);
797 error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
798 add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
801 used[location+i] = &var;
807 void GlobalInterfaceValidator::apply(Module &module)
809 for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
812 i->content.visit(*this);
816 void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
818 map<string, const Uniform *>::const_iterator i = used_names.find(uni.name);
819 if(i!=used_names.end())
821 if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
823 error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
824 add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
826 if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
828 error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
829 add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
831 if(uni.type && i->second->type && !TypeComparer().apply(*uni.type, *i->second->type))
833 string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
834 "structure" : format("type '%s'", uni.type->name));
835 error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
837 string message = "Previously declared here";
838 if(!dynamic_cast<const StructDeclaration *>(i->second->type))
839 message += format(" with type '%s'", i->second->type->name);
840 add_info(*i->second->node, message);
844 used_names.insert(make_pair(uni.name, &uni));
848 map<unsigned, const Uniform *>::const_iterator j = used_locations.find(uni.location);
849 if(j!=used_locations.end())
851 if(j->second->name!=uni.name)
853 error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
854 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
859 for(unsigned k=0; k<uni.loc_count; ++k)
860 used_locations.insert(make_pair(uni.location+k, &uni));
864 if(uni.bind_point>=0)
866 map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
867 map<unsigned, const Uniform *>::const_iterator j = used.find(uni.bind_point);
870 if(j->second->name!=uni.name)
872 error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
873 add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
877 used.insert(make_pair(uni.bind_point, &uni));
881 void GlobalInterfaceValidator::visit(VariableDeclaration &var)
883 if(var.interface=="uniform")
887 uni.type = var.type_declaration;
891 uni.location = get_layout_value(*var.layout, "location");
892 uni.loc_count = LocationCounter().apply(var);
893 uni.desc_set = get_layout_value(*var.layout, "set", 0);
894 uni.bind_point = get_layout_value(*var.layout, "binding");
897 uniforms.push_back(uni);
898 check_uniform(uniforms.back());
902 void GlobalInterfaceValidator::visit(InterfaceBlock &iface)
904 if(iface.interface=="uniform")
908 uni.type = iface.struct_declaration;
909 uni.name = iface.block_name;
912 uni.desc_set = get_layout_value(*iface.layout, "set", 0);
913 uni.bind_point = get_layout_value(*iface.layout, "binding");
916 uniforms.push_back(uni);
917 check_uniform(uniforms.back());