+ check_definition(func.name+func.signature, func);
+
+ TraversingVisitor::visit(func);
+}
+
+
+void ReferenceValidator::visit(BasicTypeDeclaration &type)
+{
+ if(!type.base.empty() && !type.base_type)
+ error(type, format("Use of undeclared type '%s'", type.base));
+}
+
+void ReferenceValidator::visit(ImageTypeDeclaration &type)
+{
+ if(!type.base.empty() && !type.base_type)
+ error(type, format("Use of undeclared type '%s'", type.base));
+}
+
+void ReferenceValidator::visit(VariableReference &var)
+{
+ if(!var.declaration)
+ error(var, format("Use of undeclared variable '%s'", var.name));
+ else if(stage->type!=Stage::VERTEX && var.declaration->interface=="in" && var.name.compare(0, 3, "gl_") && !var.declaration->linked_declaration)
+ error(var, format("Use of unlinked input variable '%s'", var.name));
+}
+
+void ReferenceValidator::visit(MemberAccess &memacc)
+{
+ if(memacc.left->type && !memacc.declaration)
+ error(memacc, format("Use of undeclared member '%s'", memacc.member));
+ TraversingVisitor::visit(memacc);
+}
+
+void ReferenceValidator::visit(InterfaceBlockReference &iface)
+{
+ /* An interface block reference without a declaration should be impossible
+ since references are generated based on existing declarations. */
+ if(!iface.declaration)
+ error(iface, format("Use of undeclared interface block '%s'", iface.name));
+ else if(stage->type!=Stage::VERTEX && iface.declaration->interface=="in" && !iface.declaration->linked_block)
+ error(iface, format("Use of unlinked input block '%s'", iface.name));
+}
+
+void ReferenceValidator::visit(FunctionCall &call)
+{
+ if((!call.constructor && !call.declaration) || (call.constructor && !call.type))
+ {
+ bool have_declaration = call.constructor;
+ if(!call.constructor)
+ {
+ auto i = stage->functions.lower_bound(call.name);
+ have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
+ }
+
+ if(have_declaration)
+ {
+ bool valid_types = true;
+ string signature;
+ for(auto j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
+ {
+ if((*j)->type)
+ append(signature, ", ", (*j)->type->name);
+ else
+ valid_types = false;
+ }
+
+ if(valid_types)
+ error(call, format("No matching %s found for '%s(%s)'", (call.constructor ? "constructor" : "overload"), call.name, signature));
+ }
+ else
+ error(call, format("Call to undeclared function '%s'", call.name));
+ }
+ TraversingVisitor::visit(call);
+}
+
+void ReferenceValidator::visit(VariableDeclaration &var)
+{
+ if(!var.type_declaration)
+ error(var, format("Use of undeclared type '%s'", var.type));
+ TraversingVisitor::visit(var);
+}
+
+void ReferenceValidator::visit(InterfaceBlock &iface)
+{
+ if(!iface.struct_declaration)
+ error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.block_name));
+ TraversingVisitor::visit(iface);
+}
+
+void ReferenceValidator::visit(FunctionDeclaration &func)
+{
+ if(!func.return_type_declaration)
+ error(func, format("Use of undeclared type '%s'", func.return_type));
+ TraversingVisitor::visit(func);
+}
+
+
+void ExpressionValidator::visit(VariableReference &var)
+{
+ if(var.declaration && constant_expression)
+ {
+ if(!var.declaration->constant)
+ error(var, format("Reference to non-constant variable '%s' in a constant expression", var.name));
+ else if(var.declaration->layout && constant_expression==FIXED_CONSTANT)
+ {
+ auto i = find_member(var.declaration->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+ if(i!=var.declaration->layout->qualifiers.end())
+ error(var, format("Reference to specialization constant '%s' in a fixed 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 = (std::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(StructDeclaration &strct)
+{
+ SetFlag set_struct(in_struct);
+ TraversingVisitor::visit(strct);
+}
+
+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)
+ {
+ 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);