[ir] Add FunctionParam::SetType()
Remove the assertion that the type must not be nullptr on construction
and check that in the validator instead. This allows transforms to
create parameters with nullptr types and then set the type later.
Change-Id: I0dedaa702ef52370ab20d3f98f33384b9ab1b077
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/189800
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/lang/core/ir/disassembly.cc b/src/tint/lang/core/ir/disassembly.cc
index 1a8a9a6..6909d45 100644
--- a/src/tint/lang/core/ir/disassembly.cc
+++ b/src/tint/lang/core/ir/disassembly.cc
@@ -320,7 +320,7 @@
out_ << ", ";
}
SourceMarker sm(this);
- out_ << NameOf(p) << ":" << StyleType(p->Type()->FriendlyName());
+ out_ << NameOf(p) << ":" << StyleType(p->Type() ? p->Type()->FriendlyName() : "undef");
sm.Store(p);
EmitParamAttributes(p);
diff --git a/src/tint/lang/core/ir/function_param.cc b/src/tint/lang/core/ir/function_param.cc
index 69b1dc0..00d72e5 100644
--- a/src/tint/lang/core/ir/function_param.cc
+++ b/src/tint/lang/core/ir/function_param.cc
@@ -35,9 +35,7 @@
namespace tint::core::ir {
-FunctionParam::FunctionParam(const core::type::Type* ty) : type_(ty) {
- TINT_ASSERT(ty != nullptr);
-}
+FunctionParam::FunctionParam(const core::type::Type* ty) : type_(ty) {}
FunctionParam::~FunctionParam() = default;
diff --git a/src/tint/lang/core/ir/function_param.h b/src/tint/lang/core/ir/function_param.h
index c9a0cea..6ff2235 100644
--- a/src/tint/lang/core/ir/function_param.h
+++ b/src/tint/lang/core/ir/function_param.h
@@ -73,6 +73,10 @@
/// @returns the type of the var
const core::type::Type* Type() const override { return type_; }
+ /// Sets the type of the parameter to @p type
+ /// @param type the new type of the parameter
+ void SetType(const core::type::Type* type) { type_ = type; }
+
/// @copydoc Value::Clone()
FunctionParam* Clone(CloneContext& ctx) override;
diff --git a/src/tint/lang/core/ir/function_param_test.cc b/src/tint/lang/core/ir/function_param_test.cc
index 80b150f..30db305 100644
--- a/src/tint/lang/core/ir/function_param_test.cc
+++ b/src/tint/lang/core/ir/function_param_test.cc
@@ -36,16 +36,6 @@
using namespace tint::core::number_suffixes; // NOLINT
using IR_FunctionParamTest = IRTestHelper;
-TEST_F(IR_FunctionParamTest, Fail_NullType) {
- EXPECT_DEATH_IF_SUPPORTED(
- {
- Module mod;
- Builder b{mod};
- b.FunctionParam(nullptr);
- },
- "");
-}
-
TEST_F(IR_FunctionParamTest, Fail_SetDuplicateBuiltin) {
EXPECT_DEATH_IF_SUPPORTED(
{
diff --git a/src/tint/lang/core/ir/validator.cc b/src/tint/lang/core/ir/validator.cc
index 4a9a866..2912315 100644
--- a/src/tint/lang/core/ir/validator.cc
+++ b/src/tint/lang/core/ir/validator.cc
@@ -769,6 +769,11 @@
return;
}
+ if (!param->Type()) {
+ AddError(param) << "function parameter has nullptr type";
+ return;
+ }
+
// References not allowed on function signatures even with Capability::kAllowRefTypes
if (HoldsType<type::Reference>(param->Type())) {
AddError(param) << "references are not permitted as parameter types";
diff --git a/src/tint/lang/core/ir/validator_test.cc b/src/tint/lang/core/ir/validator_test.cc
index 820182a..a73a3c5 100644
--- a/src/tint/lang/core/ir/validator_test.cc
+++ b/src/tint/lang/core/ir/validator_test.cc
@@ -239,6 +239,28 @@
)");
}
+TEST_F(IR_ValidatorTest, Function_ParameterWithNullType) {
+ auto* f = b.Function("my_func", ty.void_());
+ auto* p = b.FunctionParam("my_param", nullptr);
+ f->SetParams({p});
+ f->Block()->Append(b.Return(f));
+
+ auto res = ir::Validate(mod);
+ ASSERT_NE(res, Success);
+ EXPECT_EQ(res.Failure().reason.Str(),
+ R"(:1:17 error: function parameter has nullptr type
+%my_func = func(%my_param:undef):void {
+ ^^^^^^^^^^^^^^^^^
+
+note: # Disassembly
+%my_func = func(%my_param:undef):void {
+ $B1: {
+ ret
+ }
+}
+)");
+}
+
TEST_F(IR_ValidatorTest, CallToFunctionOutsideModule) {
auto* f = b.Function("f", ty.void_());
auto* g = b.Function("g", ty.void_());