diff --git a/src/clone_context.cc b/src/clone_context.cc index b0ea74efbd..16fc90bff0 100644 --- a/src/clone_context.cc +++ b/src/clone_context.cc @@ -20,7 +20,6 @@ #include "src/utils/get_or_create.h" TINT_INSTANTIATE_TYPEINFO(tint::Cloneable); -TINT_INSTANTIATE_TYPEINFO(tint::ShareableCloneable); namespace tint { diff --git a/src/clone_context.h b/src/clone_context.h index f547e1782e..c032877c3b 100644 --- a/src/clone_context.h +++ b/src/clone_context.h @@ -56,12 +56,7 @@ inline ProgramID ProgramIDOf(const Cloneable*) { return ProgramID(); } -/// ShareableCloneable is the base class for Cloneable objects which will only -/// be cloned once when CloneContext::Clone() is called with the same object -/// pointer. -class ShareableCloneable : public Castable {}; - -/// CloneContext holds the state used while cloning AST nodes and types. +/// CloneContext holds the state used while cloning AST nodes. class CloneContext { /// ParamTypeIsPtrOf::value is true iff the first parameter of /// F is a pointer of (or derives from) type T. @@ -114,10 +109,9 @@ class CloneContext { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(src, a); } - // Have we cloned this object already, or was Replace() called for this - // object? - auto it = cloned_.find(a); - if (it != cloned_.end()) { + // Was Replace() called for this object? + auto it = replacements_.find(a); + if (it != replacements_.end()) { return CheckedCast(it->second); } @@ -139,13 +133,6 @@ class CloneContext { cloned = a->Clone(this); } - // Does the type derive from ShareableCloneable? - if (Is(a)) { - // Yes. Record this clone mapping so that future calls to Clone() - // return the same cloned object. - cloned_.emplace(a, cloned); - } - auto* out = CheckedCast(cloned); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(dst, out); @@ -179,15 +166,15 @@ class CloneContext { // Have we seen this object before? If so, return the previously cloned // version instead of making yet another copy. - auto it = cloned_.find(a); - if (it != cloned_.end()) { + auto it = replacements_.find(a); + if (it != replacements_.end()) { return CheckedCast(it->second); } // First time clone and no replacer transforms matched. // Clone with T::Clone(). auto* c = a->Clone(this); - cloned_.emplace(a, c); + replacements_.emplace(a, c); return CheckedCast(c); } @@ -374,7 +361,7 @@ class CloneContext { CloneContext& Replace(WHAT* what, WITH* with) { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(src, what); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(dst, with); - cloned_[what] = with; + replacements_[what] = with; return *this; } @@ -514,8 +501,8 @@ class CloneContext { std::unordered_map insert_after_; }; - /// A map of object in #src to their cloned equivalent in #dst - std::unordered_map cloned_; + /// A map of object in #src to their replacement in #dst + std::unordered_map replacements_; /// A map of symbol in #src to their cloned equivalent in #dst std::unordered_map cloned_symbols_; diff --git a/src/clone_context_test.cc b/src/clone_context_test.cc index 9a4a9f7e91..cd53cb76b2 100644 --- a/src/clone_context_test.cc +++ b/src/clone_context_test.cc @@ -30,17 +30,17 @@ struct Allocator { BlockAllocator alloc; }; -struct UniqueNode : public Castable { - UniqueNode(Allocator* alloc, Symbol n) : allocator(alloc), name(n) {} +struct Node : public Castable { + Node(Allocator* alloc, Symbol n) : allocator(alloc), name(n) {} Allocator* const allocator; Symbol name; - UniqueNode* a = nullptr; - UniqueNode* b = nullptr; - UniqueNode* c = nullptr; - std::vector vec; + Node* a = nullptr; + Node* b = nullptr; + Node* c = nullptr; + std::vector vec; - UniqueNode* Clone(CloneContext* ctx) const override { - auto* out = allocator->Create(ctx->Clone(name)); + Node* Clone(CloneContext* ctx) const override { + auto* out = allocator->Create(ctx->Clone(name)); out->a = ctx->Clone(a); out->b = ctx->Clone(b); out->c = ctx->Clone(c); @@ -49,43 +49,12 @@ struct UniqueNode : public Castable { } }; -struct UniqueReplaceable : public Castable { - UniqueReplaceable(Allocator* alloc, Symbol n) : Base(alloc, n) {} +struct Replaceable : public Castable { + Replaceable(Allocator* alloc, Symbol n) : Base(alloc, n) {} }; -struct UniqueReplacement - : public Castable { - UniqueReplacement(Allocator* alloc, Symbol n) : Base(alloc, n) {} -}; - -struct ShareableNode : public Castable { - ShareableNode(Allocator* alloc, Symbol n) : allocator(alloc), name(n) {} - - Allocator* const allocator; - Symbol name; - ShareableNode* a = nullptr; - ShareableNode* b = nullptr; - ShareableNode* c = nullptr; - std::vector vec; - - ShareableNode* Clone(CloneContext* ctx) const override { - auto* out = allocator->Create(ctx->Clone(name)); - out->a = ctx->Clone(a); - out->b = ctx->Clone(b); - out->c = ctx->Clone(c); - out->vec = ctx->Clone(vec); - return out; - } -}; - -struct ShareableReplaceable - : public Castable { - ShareableReplaceable(Allocator* alloc, Symbol n) : Base(alloc, n) {} -}; - -struct ShareableReplacement - : public Castable { - ShareableReplacement(Allocator* alloc, Symbol n) : Base(alloc, n) {} +struct Replacement : public Castable { + Replacement(Allocator* alloc, Symbol n) : Base(alloc, n) {} }; struct NotANode : public Castable { @@ -114,32 +83,9 @@ ProgramID ProgramIDOf(const ProgramNode* node) { return node->program_id; } -struct UniqueTypes { - using Node = UniqueNode; - using Replaceable = UniqueReplaceable; - using Replacement = UniqueReplacement; -}; -struct ShareableTypes { - using Node = ShareableNode; - using Replaceable = ShareableReplaceable; - using Replacement = ShareableReplacement; -}; - -template -struct CloneContextNodeTest : public ::testing::Test { - using Node = typename T::Node; - using Replaceable = typename T::Replaceable; - using Replacement = typename T::Replacement; - static constexpr bool is_unique = std::is_same::value; -}; - -using CloneContextTestNodeTypes = ::testing::Types; -TYPED_TEST_SUITE(CloneContextNodeTest, CloneContextTestNodeTypes, /**/); - -TYPED_TEST(CloneContextNodeTest, Clone) { - using Node = typename TestFixture::Node; - constexpr bool is_unique = TestFixture::is_unique; +using CloneContextNodeTest = ::testing::Test; +TEST_F(CloneContextNodeTest, Clone) { Allocator a; ProgramBuilder builder; @@ -188,23 +134,14 @@ TYPED_TEST(CloneContextNodeTest, Clone) { EXPECT_EQ(cloned_root->b->name, cloned.Symbols().Get("b")); EXPECT_EQ(cloned_root->b->b->name, cloned.Symbols().Get("b->b")); - if (is_unique) { - EXPECT_NE(cloned_root->b->a, cloned_root->a); // De-aliased - EXPECT_NE(cloned_root->c, cloned_root->b); // De-aliased - } else { - EXPECT_EQ(cloned_root->b->a, cloned_root->a); // Aliased - EXPECT_EQ(cloned_root->c, cloned_root->b); // Aliased - } + EXPECT_NE(cloned_root->b->a, cloned_root->a); // De-aliased + EXPECT_NE(cloned_root->c, cloned_root->b); // De-aliased + EXPECT_EQ(cloned_root->b->a->name, cloned_root->a->name); EXPECT_EQ(cloned_root->c->name, cloned_root->b->name); } -TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_Cloneable) { - using Node = typename TestFixture::Node; - using Replaceable = typename TestFixture::Replaceable; - using Replacement = typename TestFixture::Replacement; - constexpr bool is_unique = TestFixture::is_unique; - +TEST_F(CloneContextNodeTest, CloneWithReplaceAll_Cloneable) { Allocator a; ProgramBuilder builder; @@ -284,13 +221,9 @@ TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_Cloneable) { EXPECT_EQ(cloned_root->b->b->name, cloned.Symbols().Get("replacement-child:b")); - if (is_unique) { - EXPECT_NE(cloned_root->b->c, cloned_root->a); // De-aliased - EXPECT_NE(cloned_root->c, cloned_root->b); // De-aliased - } else { - EXPECT_EQ(cloned_root->b->c, cloned_root->a); // Aliased - EXPECT_EQ(cloned_root->c, cloned_root->b); // Aliased - } + EXPECT_NE(cloned_root->b->c, cloned_root->a); // De-aliased + EXPECT_NE(cloned_root->c, cloned_root->b); // De-aliased + EXPECT_EQ(cloned_root->b->c->name, cloned_root->a->name); EXPECT_EQ(cloned_root->c->name, cloned_root->b->name); @@ -301,9 +234,7 @@ TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_Cloneable) { EXPECT_FALSE(Is(cloned_root->b->b)); } -TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_Symbols) { - using Node = typename TestFixture::Node; - +TEST_F(CloneContextNodeTest, CloneWithReplaceAll_Symbols) { Allocator a; ProgramBuilder builder; @@ -342,10 +273,7 @@ TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_Symbols) { EXPECT_EQ(cloned_root->b->b->name, cloned.Symbols().Get("transformedb>")); } -TYPED_TEST(CloneContextNodeTest, CloneWithoutTransform) { - using Node = typename TestFixture::Node; - using Replacement = typename TestFixture::Replacement; - +TEST_F(CloneContextNodeTest, CloneWithoutTransform) { Allocator a; ProgramBuilder builder; @@ -363,9 +291,7 @@ TYPED_TEST(CloneContextNodeTest, CloneWithoutTransform) { EXPECT_EQ(cloned_node->name, cloned.Symbols().Get("root")); } -TYPED_TEST(CloneContextNodeTest, CloneWithReplace) { - using Node = typename TestFixture::Node; - +TEST_F(CloneContextNodeTest, CloneWithReplace) { Allocator a; ProgramBuilder builder; @@ -397,10 +323,7 @@ TYPED_TEST(CloneContextNodeTest, CloneWithReplace) { EXPECT_EQ(cloned_root->c->name, cloned.Symbols().Get("c")); } -TYPED_TEST(CloneContextNodeTest, CloneWithRemove) { - using Node = typename TestFixture::Node; - constexpr bool is_unique = TestFixture::is_unique; - +TEST_F(CloneContextNodeTest, CloneWithRemove) { Allocator a; ProgramBuilder builder; @@ -417,23 +340,16 @@ TYPED_TEST(CloneContextNodeTest, CloneWithRemove) { .Clone(original_root); EXPECT_EQ(cloned_root->vec.size(), 2u); - if (is_unique) { - EXPECT_NE(cloned_root->vec[0], cloned_root->a); - EXPECT_NE(cloned_root->vec[1], cloned_root->c); - } else { - EXPECT_EQ(cloned_root->vec[0], cloned_root->a); - EXPECT_EQ(cloned_root->vec[1], cloned_root->c); - } + + EXPECT_NE(cloned_root->vec[0], cloned_root->a); + EXPECT_NE(cloned_root->vec[1], cloned_root->c); EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root")); EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("a")); EXPECT_EQ(cloned_root->vec[1]->name, cloned.Symbols().Get("c")); } -TYPED_TEST(CloneContextNodeTest, CloneWithInsertBefore) { - using Node = typename TestFixture::Node; - constexpr bool is_unique = TestFixture::is_unique; - +TEST_F(CloneContextNodeTest, CloneWithInsertBefore) { Allocator a; ProgramBuilder builder; @@ -453,15 +369,10 @@ TYPED_TEST(CloneContextNodeTest, CloneWithInsertBefore) { .Clone(original_root); EXPECT_EQ(cloned_root->vec.size(), 4u); - if (is_unique) { - EXPECT_NE(cloned_root->vec[0], cloned_root->a); - EXPECT_NE(cloned_root->vec[2], cloned_root->b); - EXPECT_NE(cloned_root->vec[3], cloned_root->c); - } else { - EXPECT_EQ(cloned_root->vec[0], cloned_root->a); - EXPECT_EQ(cloned_root->vec[2], cloned_root->b); - EXPECT_EQ(cloned_root->vec[3], cloned_root->c); - } + + EXPECT_NE(cloned_root->vec[0], cloned_root->a); + EXPECT_NE(cloned_root->vec[2], cloned_root->b); + EXPECT_NE(cloned_root->vec[3], cloned_root->c); EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root")); EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("a")); @@ -470,10 +381,7 @@ TYPED_TEST(CloneContextNodeTest, CloneWithInsertBefore) { EXPECT_EQ(cloned_root->vec[3]->name, cloned.Symbols().Get("c")); } -TYPED_TEST(CloneContextNodeTest, CloneWithInsertAfter) { - using Node = typename TestFixture::Node; - constexpr bool is_unique = TestFixture::is_unique; - +TEST_F(CloneContextNodeTest, CloneWithInsertAfter) { Allocator a; ProgramBuilder builder; @@ -493,15 +401,10 @@ TYPED_TEST(CloneContextNodeTest, CloneWithInsertAfter) { .Clone(original_root); EXPECT_EQ(cloned_root->vec.size(), 4u); - if (is_unique) { - EXPECT_NE(cloned_root->vec[0], cloned_root->a); - EXPECT_NE(cloned_root->vec[1], cloned_root->b); - EXPECT_NE(cloned_root->vec[3], cloned_root->c); - } else { - EXPECT_EQ(cloned_root->vec[0], cloned_root->a); - EXPECT_EQ(cloned_root->vec[1], cloned_root->b); - EXPECT_EQ(cloned_root->vec[3], cloned_root->c); - } + + EXPECT_NE(cloned_root->vec[0], cloned_root->a); + EXPECT_NE(cloned_root->vec[1], cloned_root->b); + EXPECT_NE(cloned_root->vec[3], cloned_root->c); EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root")); EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("a")); @@ -510,10 +413,7 @@ TYPED_TEST(CloneContextNodeTest, CloneWithInsertAfter) { EXPECT_EQ(cloned_root->vec[3]->name, cloned.Symbols().Get("c")); } -TYPED_TEST(CloneContextNodeTest, CloneWithInsertBeforeAndAfterRemoved) { - using Node = typename TestFixture::Node; - constexpr bool is_unique = TestFixture::is_unique; - +TEST_F(CloneContextNodeTest, CloneWithInsertBeforeAndAfterRemoved) { Allocator a; ProgramBuilder builder; @@ -538,13 +438,9 @@ TYPED_TEST(CloneContextNodeTest, CloneWithInsertBeforeAndAfterRemoved) { .Clone(original_root); EXPECT_EQ(cloned_root->vec.size(), 4u); - if (is_unique) { - EXPECT_NE(cloned_root->vec[0], cloned_root->a); - EXPECT_NE(cloned_root->vec[3], cloned_root->c); - } else { - EXPECT_EQ(cloned_root->vec[0], cloned_root->a); - EXPECT_EQ(cloned_root->vec[3], cloned_root->c); - } + + EXPECT_NE(cloned_root->vec[0], cloned_root->a); + EXPECT_NE(cloned_root->vec[3], cloned_root->c); EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root")); EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("a")); @@ -554,10 +450,7 @@ TYPED_TEST(CloneContextNodeTest, CloneWithInsertBeforeAndAfterRemoved) { EXPECT_EQ(cloned_root->vec[3]->name, cloned.Symbols().Get("c")); } -TYPED_TEST(CloneContextNodeTest, CloneIntoSameBuilder) { - using Node = typename TestFixture::Node; - constexpr bool is_unique = TestFixture::is_unique; - +TEST_F(CloneContextNodeTest, CloneIntoSameBuilder) { ProgramBuilder builder; CloneContext ctx(&builder); Allocator allocator; @@ -566,19 +459,15 @@ TYPED_TEST(CloneContextNodeTest, CloneIntoSameBuilder) { auto* cloned_b = ctx.Clone(original); EXPECT_NE(original, cloned_a); EXPECT_NE(original, cloned_b); - if (is_unique) { - EXPECT_NE(cloned_a, cloned_b); - } else { - EXPECT_EQ(cloned_a, cloned_b); - } + + EXPECT_NE(cloned_a, cloned_b); } -TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_SameTypeTwice) { - std::string node_name = TypeInfo::Of().name; +TEST_F(CloneContextNodeTest, CloneWithReplaceAll_SameTypeTwice) { + std::string node_name = TypeInfo::Of().name; EXPECT_FATAL_FAILURE( { - using Node = typename TestFixture::Node; ProgramBuilder cloned; Program original; CloneContext ctx(&cloned, &original); @@ -590,16 +479,12 @@ TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_SameTypeTwice) { node_name); } -TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_BaseThenDerived) { - std::string node_name = TypeInfo::Of().name; - std::string replaceable_name = - TypeInfo::Of().name; +TEST_F(CloneContextNodeTest, CloneWithReplaceAll_BaseThenDerived) { + std::string node_name = TypeInfo::Of().name; + std::string replaceable_name = TypeInfo::Of().name; EXPECT_FATAL_FAILURE( { - using Node = typename TestFixture::Node; - using Replaceable = typename TestFixture::Replaceable; - ProgramBuilder cloned; Program original; CloneContext ctx(&cloned, &original); @@ -611,15 +496,12 @@ TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_BaseThenDerived) { node_name); } -TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_DerivedThenBase) { - std::string node_name = TypeInfo::Of().name; - std::string replaceable_name = - TypeInfo::Of().name; +TEST_F(CloneContextNodeTest, CloneWithReplaceAll_DerivedThenBase) { + std::string node_name = TypeInfo::Of().name; + std::string replaceable_name = TypeInfo::Of().name; EXPECT_FATAL_FAILURE( { - using Node = typename TestFixture::Node; - using Replaceable = typename TestFixture::Replaceable; ProgramBuilder cloned; Program original; CloneContext ctx(&cloned, &original); @@ -631,10 +513,9 @@ TYPED_TEST(CloneContextNodeTest, CloneWithReplaceAll_DerivedThenBase) { replaceable_name); } -TYPED_TEST(CloneContextNodeTest, CloneWithReplace_WithNotANode) { +TEST_F(CloneContextNodeTest, CloneWithReplace_WithNotANode) { EXPECT_FATAL_FAILURE( { - using Node = typename TestFixture::Node; Allocator allocator; ProgramBuilder builder; auto* original_root = @@ -794,12 +675,9 @@ TEST_F(CloneContextTest, ProgramIDs_Clone_ObjectNotOwnedByDst) { } // namespace -TINT_INSTANTIATE_TYPEINFO(UniqueNode); -TINT_INSTANTIATE_TYPEINFO(UniqueReplaceable); -TINT_INSTANTIATE_TYPEINFO(UniqueReplacement); -TINT_INSTANTIATE_TYPEINFO(ShareableNode); -TINT_INSTANTIATE_TYPEINFO(ShareableReplaceable); -TINT_INSTANTIATE_TYPEINFO(ShareableReplacement); +TINT_INSTANTIATE_TYPEINFO(Node); +TINT_INSTANTIATE_TYPEINFO(Replaceable); +TINT_INSTANTIATE_TYPEINFO(Replacement); TINT_INSTANTIATE_TYPEINFO(NotANode); TINT_INSTANTIATE_TYPEINFO(ProgramNode);