+
+ if(var.layout)
+ var.layout->visit(*this);
+ if(var.init_expression)
+ {
+ ConstantKind const_kind = (var.constant ? SPEC_CONSTANT : NOT_CONSTANT);
+ if(var.layout)
+ {
+ auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+ if(i!=var.layout->qualifiers.end())
+ const_kind = FIXED_CONSTANT;
+ }
+
+ SetForScope<ConstantKind> set_const(constant_expression, const_kind);
+ TraversingVisitor::visit(var.init_expression);
+ }
+ if(var.array_size)
+ {
+ SetForScope<ConstantKind> set_const(constant_expression, (in_struct || !var.interface.empty() ? FIXED_CONSTANT : SPEC_CONSTANT));
+ TraversingVisitor::visit(var.array_size);
+ }
+}
+
+void ExpressionValidator::visit(FunctionDeclaration &func)
+{
+ SetForScope<FunctionDeclaration *> set_func(current_function, &func);
+ TraversingVisitor::visit(func);
+}
+
+void ExpressionValidator::visit(Conditional &cond)
+{
+ if(cond.condition->type)
+ {
+ BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(cond.condition->type);
+ if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
+ error(cond, "Condition is not a boolean");
+ }
+ TraversingVisitor::visit(cond);
+}
+
+void ExpressionValidator::visit(Iteration &iter)
+{
+ if(iter.condition->type)
+ {
+ BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(iter.condition->type);
+ if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
+ error(iter, "Loop condition is not a boolean");
+ }
+ TraversingVisitor::visit(iter);
+}
+
+void ExpressionValidator::visit(Return &ret)
+{
+ if(current_function && current_function->return_type_declaration)
+ {
+ TypeDeclaration *return_type = current_function->return_type_declaration;
+ BasicTypeDeclaration *basic_return = dynamic_cast<BasicTypeDeclaration *>(return_type);
+ if(ret.expression)
+ {
+ if(ret.expression->type && ret.expression->type!=return_type)
+ error(ret, format("Return expression type '%s' is incompatible with declared return type '%s'",
+ ret.expression->type->name, return_type->name));
+ }
+ else if(!basic_return || basic_return->kind!=BasicTypeDeclaration::VOID)
+ error(ret, "Return statement without an expression in a function not returning 'void'");
+ }
+
+ TraversingVisitor::visit(ret);
+}
+
+
+void FlowControlValidator::visit(Block &block)
+{
+ for(const RefPtr<Statement> &s: block.body)
+ {
+ if(!reachable)
+ {
+ diagnose(*s, Diagnostic::WARN, "Unreachable code detected");
+ break;
+ }
+ s->visit(*this);
+ }
+}
+
+void FlowControlValidator::visit(FunctionDeclaration &func)
+{
+ func.body.visit(*this);
+
+ if(func.definition==&func && func.return_type_declaration)
+ {
+ const BasicTypeDeclaration *basic_ret = dynamic_cast<const BasicTypeDeclaration *>(func.return_type_declaration);
+ if(reachable && (!basic_ret || basic_ret->kind!=BasicTypeDeclaration::VOID))
+ error(func, "Missing return statement at the end of a function not returning 'void'");
+ }
+ reachable = true;
+}
+
+void FlowControlValidator::visit(Conditional &cond)
+{
+ cond.body.visit(*this);
+ bool reachable_if_true = reachable;
+ reachable = true;
+ cond.else_body.visit(*this);
+ reachable |= reachable_if_true;
+}
+
+void FlowControlValidator::visit(Iteration &iter)
+{
+ iter.body.visit(*this);
+ reachable = true;
+}
+
+
+int StageInterfaceValidator::get_location(const Layout &layout)
+{
+ return get_layout_value(layout, "location", -1);
+}
+
+void StageInterfaceValidator::visit(VariableDeclaration &var)
+{
+ int location = (var.layout ? get_location(*var.layout) : -1);
+ if(var.interface=="in" && var.linked_declaration)
+ {
+ const Layout *linked_layout = var.linked_declaration->layout.get();
+ int linked_location = (linked_layout ? get_location(*linked_layout) : -1);
+ if(linked_location!=location)
+ {
+ error(var, format("Mismatched location %d for 'in %s'", location, var.name));
+ add_info(*var.linked_declaration, format("Linked to 'out %s' with location %d",
+ var.linked_declaration->name, linked_location));
+ }
+ if(var.type_declaration && var.linked_declaration->type_declaration)
+ {
+ TypeDeclaration *type = var.type_declaration;
+ if(stage->type==Stage::GEOMETRY)
+ {
+ if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
+ if(basic->kind==BasicTypeDeclaration::ARRAY && basic->base_type)
+ type = basic->base_type;
+ }
+ if(!TypeComparer().apply(*type, *var.linked_declaration->type_declaration))
+ {
+ error(var, format("Mismatched type '%s' for 'in %s'", type->name, var.name));
+ add_info(*var.linked_declaration, format("Linked to 'out %s' with type '%s'",
+ var.linked_declaration->name, var.linked_declaration->type_declaration->name));
+ }
+ }
+ }
+
+ if(location>=0 && !var.interface.empty())
+ {
+ map<unsigned, VariableDeclaration *> &used = used_locations[var.interface];
+
+ unsigned loc_count = LocationCounter().apply(var);
+ for(unsigned i=0; i<loc_count; ++i)
+ {
+ auto j = used.find(location+i);
+ if(j!=used.end())
+ {
+ error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
+ add_info(*j->second, format("Previously used here for '%s %s'", j->second->interface, j->second->name));
+ }
+ else
+ used[location+i] = &var;
+ }
+ }
+}
+
+
+void GlobalInterfaceValidator::apply(Module &module)
+{
+ for(Stage &s: module.stages)
+ {
+ stage = &s;
+ s.content.visit(*this);
+ }
+}
+
+void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
+{
+ auto i = used_names.find(uni.name);
+ if(i!=used_names.end())
+ {
+ if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
+ {
+ error(*uni.node, format("Mismatched location %d for uniform '%s'", uni.location, uni.name));
+ add_info(*i->second->node, format("Previously declared here with location %d", i->second->location));
+ }
+ if(uni.bind_point>=0 && i->second->bind_point>=0 && i->second->bind_point!=uni.bind_point)
+ {
+ error(*uni.node, format("Mismatched binding %d for uniform '%s'", uni.bind_point, uni.name));
+ add_info(*i->second->node, format("Previously declared here with binding %d", i->second->bind_point));
+ }
+ if(uni.type && i->second->type && !TypeComparer().apply(*uni.type, *i->second->type))
+ {
+ string type_name = (dynamic_cast<const StructDeclaration *>(uni.type) ?
+ "structure" : format("type '%s'", uni.type->name));
+ error(*uni.node, format("Mismatched %s for uniform '%s'", type_name, uni.name));
+
+ string message = "Previously declared here";
+ if(!dynamic_cast<const StructDeclaration *>(i->second->type))
+ message += format(" with type '%s'", i->second->type->name);
+ add_info(*i->second->node, message);
+ }
+ }
+ else
+ used_names.insert(make_pair(uni.name, &uni));
+
+ if(uni.location>=0)
+ {
+ auto j = used_locations.find(uni.location);
+ if(j!=used_locations.end())
+ {
+ if(j->second->name!=uni.name)
+ {
+ error(*uni.node, format("Overlapping location %d for '%s'", uni.location, uni.name));
+ add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
+ }
+ }
+ else
+ {
+ for(unsigned k=0; k<uni.loc_count; ++k)
+ used_locations.insert(make_pair(uni.location+k, &uni));
+ }
+ }
+
+ if(uni.bind_point>=0)
+ {
+ map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
+ auto j = used.find(uni.bind_point);
+ if(j!=used.end())
+ {
+ if(j->second->name!=uni.name)
+ {
+ error(*uni.node, format("Overlapping binding %d for '%s'", uni.bind_point, uni.name));
+ add_info(*j->second->node, format("Previously used here for '%s'", j->second->name));
+ }
+ }
+ else
+ used.insert(make_pair(uni.bind_point, &uni));
+ }
+}
+
+void GlobalInterfaceValidator::visit(VariableDeclaration &var)
+{
+ if(var.interface=="uniform")
+ {
+ Uniform uni;
+ uni.node = &var;
+ uni.type = var.type_declaration;
+ uni.name = var.name;
+ if(var.layout)
+ {
+ uni.location = get_layout_value(*var.layout, "location");
+ uni.loc_count = LocationCounter().apply(var);
+ uni.desc_set = get_layout_value(*var.layout, "set", 0);
+ uni.bind_point = get_layout_value(*var.layout, "binding");
+ }
+
+ uniforms.push_back(uni);
+ check_uniform(uniforms.back());
+ }
+}
+
+void GlobalInterfaceValidator::visit(InterfaceBlock &iface)
+{
+ if(iface.interface=="uniform")
+ {
+ Uniform uni;
+ uni.node = &iface;
+ uni.type = iface.struct_declaration;
+ uni.name = iface.block_name;
+ if(iface.layout)
+ {
+ uni.desc_set = get_layout_value(*iface.layout, "set", 0);
+ uni.bind_point = get_layout_value(*iface.layout, "binding");
+ }
+
+ uniforms.push_back(uni);
+ check_uniform(uniforms.back());
+ }