+
+void ExpressionValidator::visit(VariableReference &var)
+{
+ if(var.declaration && constant_expression && !var.declaration->constant)
+ error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
+}
+
+void ExpressionValidator::visit(InterfaceBlockReference &iface)
+{
+ if(constant_expression)
+ error(iface, format("Reference to interface block '%s' in a constant expression", iface.name));
+}
+
+void ExpressionValidator::visit(Swizzle &swizzle)
+{
+ unsigned size = 0;
+ if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(swizzle.left->type))
+ {
+ if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
+ size = 1;
+ else if(basic->kind==BasicTypeDeclaration::VECTOR)
+ size = basic->size;
+ }
+
+ if(size)
+ {
+ static const char component_names[] = { 'x', 'y', 'z', 'w', 'r', 'g', 'b', 'a', 's', 't', 'p', 'q' };
+ int flavour = -1;
+ for(unsigned i=0; i<swizzle.count; ++i)
+ {
+ unsigned component_flavour = (find(component_names, component_names+12, swizzle.component_group[i])-component_names)/4;
+ if(flavour==-1)
+ flavour = component_flavour;
+ else if(flavour>=0 && component_flavour!=static_cast<unsigned>(flavour))
+ {
+ error(swizzle, format("Flavour of swizzle component '%c' is inconsistent with '%c'",
+ swizzle.component_group[i], swizzle.component_group[0]));
+ flavour = -2;
+ }
+
+ if(swizzle.components[i]>=size)
+ error(swizzle, format("Access to component '%c' which is not present in '%s'",
+ swizzle.component_group[i], swizzle.left->type->name));
+ }
+ }
+ else if(swizzle.left->type)
+ error(swizzle, format("Swizzle applied to '%s' which is neither a scalar nor a vector", swizzle.left->type->name));
+
+ TraversingVisitor::visit(swizzle);
+}
+
+void ExpressionValidator::visit(UnaryExpression &unary)
+{
+ if(unary.expression->type)
+ {
+ if(!unary.type)
+ error(unary, format("No matching operator '%s' found for '%s'", unary.oper->token, unary.expression->type->name));
+ else if(unary.oper->token[1]=='+' || unary.oper->token[1]=='-')
+ {
+ if(constant_expression)
+ error(unary, format("Use of '%s' in a constant expression", unary.oper->token));
+ else if(!unary.expression->lvalue)
+ error(unary, format("Operand of '%s' is not an lvalue", unary.oper->token));
+ }
+ }
+ TraversingVisitor::visit(unary);
+}
+
+void ExpressionValidator::visit(BinaryExpression &binary)
+{
+ if(!binary.type && binary.left->type && binary.right->type)
+ {
+ if(binary.oper->token[0]=='[')
+ error(binary, format("Can't index element of '%s' with '%s'",
+ binary.left->type->name, binary.right->type->name));
+ else
+ error(binary, format("No matching operator '%s' found for '%s' and '%s'",
+ binary.oper->token, binary.left->type->name, binary.right->type->name));
+ }
+ TraversingVisitor::visit(binary);
+}
+
+void ExpressionValidator::visit(Assignment &assign)
+{
+ if(assign.left->type)
+ {
+ if(constant_expression)
+ error(assign, "Assignment in constant expression");
+ else if(!assign.left->lvalue)
+ error(assign, "Target of assignment is not an lvalue");
+ if(assign.right->type)
+ {
+ if(assign.oper->token[0]!='=')
+ {
+ if(!assign.type)
+ error(assign, format("No matching operator '%s' found for '%s' and '%s'",
+ string(assign.oper->token, strlen(assign.oper->token)-1), assign.left->type->name, assign.right->type->name));
+ }
+ else if(assign.left->type!=assign.right->type)
+ error(assign, format("Assignment to variable of type '%s' from expression of incompatible type '%s'",
+ assign.left->type->name, assign.right->type->name));
+ }
+ }
+ TraversingVisitor::visit(assign);
+}
+
+void ExpressionValidator::visit(TernaryExpression &ternary)
+{
+ if(ternary.condition->type)
+ {
+ BasicTypeDeclaration *basic_cond = dynamic_cast<BasicTypeDeclaration *>(ternary.condition->type);
+ if(!basic_cond || basic_cond->kind!=BasicTypeDeclaration::BOOL)
+ error(ternary, "Ternary operator condition is not a boolean");
+ else if(!ternary.type && ternary.true_expr->type && ternary.false_expr->type)
+ error(ternary, format("Ternary operator has incompatible types '%s' and '%s'",
+ ternary.true_expr->type->name, ternary.false_expr->type->name));
+ }
+ TraversingVisitor::visit(ternary);
+}
+
+void ExpressionValidator::visit(VariableDeclaration &var)
+{
+ if(var.init_expression && var.init_expression->type && var.type_declaration && var.init_expression->type!=var.type_declaration)
+ error(var, format("Initializing a variable of type '%s' with an expression of incompatible type '%s'",
+ var.type_declaration->name, var.init_expression->type->name));
+
+ if(var.layout)
+ var.layout->visit(*this);
+ if(var.init_expression)
+ {
+ SetFlag set_const(constant_expression, var.constant);
+ TraversingVisitor::visit(var.init_expression);
+ }
+ if(var.array_size)
+ {
+ SetFlag set_const(constant_expression);
+ 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());
+ }
+}
+