[ir] Track the index of a function parameter
This is useful when inspecting arguments at callsites of a function.
Change-Id: I26eb7ad19de4de2d79e8a126458d4d4bb25f6f24
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/188840
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/lang/core/ir/function.cc b/src/tint/lang/core/ir/function.cc
index f8a3e53..2cce808 100644
--- a/src/tint/lang/core/ir/function.cc
+++ b/src/tint/lang/core/ir/function.cc
@@ -71,8 +71,10 @@
}
params_ = std::move(params);
TINT_ASSERT(!params_.Any(IsNull));
+ uint32_t index = 0;
for (auto* param : params_) {
param->SetFunction(this);
+ param->SetIndex(index++);
}
}
@@ -82,14 +84,17 @@
}
params_ = params;
TINT_ASSERT(!params_.Any(IsNull));
+ uint32_t index = 0;
for (auto* param : params_) {
param->SetFunction(this);
+ param->SetIndex(index++);
}
}
void Function::AppendParam(FunctionParam* param) {
params_.Push(param);
param->SetFunction(this);
+ param->SetIndex(static_cast<uint32_t>(params_.Length() - 1u));
}
void Function::Destroy() {
diff --git a/src/tint/lang/core/ir/function_param.h b/src/tint/lang/core/ir/function_param.h
index 14d8f92..c9a0cea 100644
--- a/src/tint/lang/core/ir/function_param.h
+++ b/src/tint/lang/core/ir/function_param.h
@@ -63,6 +63,13 @@
/// @returns the function that this parameter belongs to, or nullptr
const ir::Function* Function() const { return func_; }
+ /// Sets the index of the parameter in the function's parameter list
+ /// @param index the index
+ void SetIndex(uint32_t index) { index_ = index; }
+
+ /// @returns the index of the parameter in the function's parameter list
+ uint32_t Index() const { return index_; }
+
/// @returns the type of the var
const core::type::Type* Type() const override { return type_; }
@@ -121,6 +128,7 @@
private:
ir::Function* func_ = nullptr;
+ uint32_t index_ = 0xffffffff;
const core::type::Type* type_ = nullptr;
std::optional<core::BuiltinValue> builtin_;
std::optional<struct Location> location_;
diff --git a/src/tint/lang/core/ir/function_test.cc b/src/tint/lang/core/ir/function_test.cc
index 9f0161a..eda7fdd 100644
--- a/src/tint/lang/core/ir/function_test.cc
+++ b/src/tint/lang/core/ir/function_test.cc
@@ -154,16 +154,23 @@
EXPECT_EQ(param1->Function(), f);
EXPECT_EQ(param2->Function(), f);
EXPECT_EQ(param3->Function(), nullptr);
+ EXPECT_EQ(param1->Index(), 0u);
+ EXPECT_EQ(param2->Index(), 1u);
f->SetParams({param1, param3});
EXPECT_EQ(param1->Function(), f);
EXPECT_EQ(param2->Function(), nullptr);
EXPECT_EQ(param3->Function(), f);
+ EXPECT_EQ(param1->Index(), 0u);
+ EXPECT_EQ(param3->Index(), 1u);
f->AppendParam(param2);
EXPECT_EQ(param1->Function(), f);
EXPECT_EQ(param2->Function(), f);
EXPECT_EQ(param3->Function(), f);
+ EXPECT_EQ(param1->Index(), 0u);
+ EXPECT_EQ(param3->Index(), 1u);
+ EXPECT_EQ(param2->Index(), 2u);
}
} // namespace