2020-12-03 18:10:39 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-01-21 16:20:40 +00:00
|
|
|
#include "src/clone_context.h"
|
2020-12-03 18:10:39 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program_builder.h"
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace {
|
|
|
|
|
2021-01-21 16:20:40 +00:00
|
|
|
struct Cloneable : public Castable<Cloneable, ast::Node> {
|
2021-01-26 16:57:10 +00:00
|
|
|
explicit Cloneable(const Source& source) : Base(source) {}
|
2020-12-12 13:00:34 +00:00
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
Cloneable* a = nullptr;
|
|
|
|
Cloneable* b = nullptr;
|
|
|
|
Cloneable* c = nullptr;
|
|
|
|
|
|
|
|
Cloneable* Clone(CloneContext* ctx) const override {
|
2021-01-22 13:41:06 +00:00
|
|
|
auto* out = ctx->dst->create<Cloneable>();
|
2020-12-03 18:10:39 +00:00
|
|
|
out->a = ctx->Clone(a);
|
|
|
|
out->b = ctx->Clone(b);
|
|
|
|
out->c = ctx->Clone(c);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid() const override { return true; }
|
|
|
|
void to_str(std::ostream&, size_t) const override {}
|
|
|
|
};
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
struct Replaceable : public Castable<Replaceable, Cloneable> {
|
|
|
|
explicit Replaceable(const Source& source) : Base(source) {}
|
|
|
|
};
|
|
|
|
struct Replacement : public Castable<Replacement, Replaceable> {
|
|
|
|
explicit Replacement(const Source& source) : Base(source) {}
|
|
|
|
};
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
TEST(CloneContext, Clone) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = builder.create<Cloneable>();
|
|
|
|
original_root->a = builder.create<Cloneable>();
|
|
|
|
original_root->a->b = builder.create<Cloneable>();
|
|
|
|
original_root->b = builder.create<Cloneable>();
|
2020-12-03 18:10:39 +00:00
|
|
|
original_root->b->a = original_root->a; // Aliased
|
2021-01-26 16:57:10 +00:00
|
|
|
original_root->b->b = builder.create<Cloneable>();
|
2020-12-03 18:10:39 +00:00
|
|
|
original_root->c = original_root->b; // Aliased
|
2021-01-26 16:57:10 +00:00
|
|
|
Program original(std::move(builder));
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭──────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
|
|
|
// C <──────┐ C <───────────────┘
|
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
|
|
|
// C └───┘ C
|
|
|
|
//
|
|
|
|
// C: Clonable
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder cloned;
|
2020-12-15 12:32:18 +00:00
|
|
|
auto* cloned_root = CloneContext(&cloned, &original).Clone(original_root);
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->a->a, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->a->b, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->a->c, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->b, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->b->a, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->b->b, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->b->c, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->c, nullptr);
|
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, original_root->a);
|
|
|
|
EXPECT_NE(cloned_root->a->b, original_root->a->b);
|
|
|
|
EXPECT_NE(cloned_root->b, original_root->b);
|
|
|
|
EXPECT_NE(cloned_root->b->a, original_root->b->a);
|
|
|
|
EXPECT_NE(cloned_root->b->b, original_root->b->b);
|
|
|
|
EXPECT_NE(cloned_root->c, original_root->c);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->b->a, cloned_root->a); // Aliased
|
|
|
|
EXPECT_EQ(cloned_root->c, cloned_root->b); // Aliased
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST(CloneContext, CloneWithReplacements) {
|
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = builder.create<Cloneable>();
|
|
|
|
original_root->a = builder.create<Cloneable>();
|
|
|
|
original_root->a->b = builder.create<Replaceable>();
|
|
|
|
original_root->b = builder.create<Replaceable>();
|
2020-12-03 18:10:39 +00:00
|
|
|
original_root->b->a = original_root->a; // Aliased
|
|
|
|
original_root->c = original_root->b; // Aliased
|
2021-01-26 16:57:10 +00:00
|
|
|
Program original(std::move(builder));
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭──────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
|
|
|
// C <──────┐ R <───────────────┘
|
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
|
|
|
// R └───┘
|
|
|
|
//
|
|
|
|
// C: Clonable
|
|
|
|
// R: Replaceable
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder cloned;
|
2020-12-15 12:32:18 +00:00
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.ReplaceAll([&](CloneContext* ctx, Replaceable* in) {
|
|
|
|
auto* out = cloned.create<Replacement>();
|
|
|
|
out->b = cloned.create<Cloneable>();
|
|
|
|
out->c = ctx->Clone(in->a);
|
|
|
|
return out;
|
|
|
|
})
|
|
|
|
.Clone(original_root);
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭─────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
|
|
|
// C <──────┐ R <───────────────┘
|
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
|
|
|
// R │ C |
|
|
|
|
// ╭────┼────╮ └────────────┘
|
|
|
|
// (a) (b) (c)
|
|
|
|
// C
|
|
|
|
//
|
|
|
|
// C: Clonable
|
|
|
|
// R: Replacement
|
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->a->a, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->a->b, nullptr); // Replaced
|
|
|
|
EXPECT_EQ(cloned_root->a->b->a, nullptr); // From replacement
|
|
|
|
EXPECT_NE(cloned_root->a->b->b, nullptr); // From replacement
|
|
|
|
EXPECT_EQ(cloned_root->a->b->c, nullptr); // From replacement
|
|
|
|
EXPECT_EQ(cloned_root->a->c, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->b, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->b->a, nullptr); // From replacement
|
|
|
|
EXPECT_NE(cloned_root->b->b, nullptr); // From replacement
|
|
|
|
EXPECT_NE(cloned_root->b->c, nullptr); // From replacement
|
|
|
|
EXPECT_NE(cloned_root->c, nullptr);
|
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, original_root->a);
|
|
|
|
EXPECT_NE(cloned_root->a->b, original_root->a->b);
|
|
|
|
EXPECT_NE(cloned_root->b, original_root->b);
|
|
|
|
EXPECT_NE(cloned_root->b->a, original_root->b->a);
|
|
|
|
EXPECT_NE(cloned_root->c, original_root->c);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->b->c, cloned_root->a); // Aliased
|
|
|
|
EXPECT_EQ(cloned_root->c, cloned_root->b); // Aliased
|
|
|
|
|
|
|
|
EXPECT_FALSE(cloned_root->a->Is<Replacement>());
|
|
|
|
EXPECT_TRUE(cloned_root->a->b->Is<Replacement>());
|
|
|
|
EXPECT_FALSE(cloned_root->a->b->b->Is<Replacement>());
|
|
|
|
EXPECT_TRUE(cloned_root->b->Is<Replacement>());
|
|
|
|
EXPECT_FALSE(cloned_root->b->b->Is<Replacement>());
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
TEST(CloneContext, CloneWithReplace) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = builder.create<Cloneable>();
|
|
|
|
original_root->a = builder.create<Cloneable>();
|
|
|
|
original_root->b = builder.create<Cloneable>();
|
|
|
|
original_root->c = builder.create<Cloneable>();
|
|
|
|
Program original(std::move(builder));
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭──────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
|
|
|
// Replaced
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder cloned;
|
2021-01-26 16:57:10 +00:00
|
|
|
auto* replacement = cloned.create<Cloneable>();
|
|
|
|
|
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.Replace(original_root->b, replacement)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, replacement);
|
|
|
|
EXPECT_EQ(cloned_root->b, replacement);
|
|
|
|
EXPECT_NE(cloned_root->c, replacement);
|
|
|
|
}
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-01-21 16:20:40 +00:00
|
|
|
TINT_INSTANTIATE_CLASS_ID(Cloneable);
|
|
|
|
TINT_INSTANTIATE_CLASS_ID(Replaceable);
|
|
|
|
TINT_INSTANTIATE_CLASS_ID(Replacement);
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
} // namespace tint
|