Add type::Type::UnwrapAliasIfNeeded()

Unwraps aliased types until reaching a non-alias.

Change-Id: I6546d60b7cbe07d4c8cc5a0b439329af8b468ca9
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41500
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2021-02-10 21:40:15 +00:00 committed by Commit Bot service account
parent 46d78d731b
commit 81da283b2c
4 changed files with 20 additions and 4 deletions

View File

@ -96,10 +96,7 @@ class Matcher {
/// Match may add to, or compare against the open types and numbers in state. /// Match may add to, or compare against the open types and numbers in state.
/// @returns true if the argument type is as expected. /// @returns true if the argument type is as expected.
bool Match(MatchState& state, type::Type* argument_type) const { bool Match(MatchState& state, type::Type* argument_type) const {
auto* unwrapped = argument_type; auto* unwrapped = argument_type->UnwrapAliasIfNeeded();
while (auto* alias = unwrapped->As<type::Alias>()) {
unwrapped = alias->type();
}
return MatchUnwrapped(state, unwrapped); return MatchUnwrapped(state, unwrapped);
} }

View File

@ -225,6 +225,13 @@ TEST_F(AliasTest, BaseAlignmentStruct) {
EXPECT_EQ(4u, alias->BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(4u, alias->BaseAlignment(MemoryLayout::kStorageBuffer));
} }
TEST_F(AliasTest, UnwrapAliasIfNeeded) {
auto* alias1 = ty.alias("alias1", ty.f32());
auto* alias2 = ty.alias("alias2", alias1);
auto* alias3 = ty.alias("alias3", alias2);
EXPECT_EQ(alias3->UnwrapAliasIfNeeded(), ty.f32());
}
} // namespace } // namespace
} // namespace type } // namespace type
} // namespace tint } // namespace tint

View File

@ -49,6 +49,14 @@ Type* Type::UnwrapPtrIfNeeded() {
return this; return this;
} }
Type* Type::UnwrapAliasIfNeeded() {
Type* unwrapped = this;
while (auto* ptr = unwrapped->As<type::Alias>()) {
unwrapped = ptr->type();
}
return unwrapped;
}
Type* Type::UnwrapIfNeeded() { Type* Type::UnwrapIfNeeded() {
auto* where = this; auto* where = this;
while (true) { while (true) {

View File

@ -64,6 +64,10 @@ class Type : public Castable<Type> {
/// @returns the pointee type if this is a pointer, `this` otherwise /// @returns the pointee type if this is a pointer, `this` otherwise
Type* UnwrapPtrIfNeeded(); Type* UnwrapPtrIfNeeded();
/// @returns the most deeply nested aliased type if this is an alias, `this`
/// otherwise
Type* UnwrapAliasIfNeeded();
/// Removes all levels of aliasing and access control. /// Removes all levels of aliasing and access control.
/// This is just enough to assist with WGSL translation /// This is just enough to assist with WGSL translation
/// in that you want see through one level of pointer to get from an /// in that you want see through one level of pointer to get from an