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-02-16 23:09:31 +00:00
|
|
|
#include <string>
|
2021-01-26 16:57:10 +00:00
|
|
|
#include <utility>
|
2021-02-16 23:09:31 +00:00
|
|
|
#include <vector>
|
2021-01-26 16:57:10 +00:00
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
#include "gtest/gtest-spi.h"
|
2020-12-03 18:10:39 +00:00
|
|
|
|
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-02-17 13:17:39 +00:00
|
|
|
struct Node : public Castable<Node, ast::Node> {
|
|
|
|
explicit Node(const Source& source, std::string n) : Base(source), name(n) {}
|
2020-12-12 13:00:34 +00:00
|
|
|
|
2021-02-16 23:09:31 +00:00
|
|
|
std::string name;
|
2021-02-17 13:17:39 +00:00
|
|
|
Node* a = nullptr;
|
|
|
|
Node* b = nullptr;
|
|
|
|
Node* c = nullptr;
|
|
|
|
std::vector<Node*> vec;
|
2020-12-03 18:10:39 +00:00
|
|
|
|
2021-02-17 13:17:39 +00:00
|
|
|
Node* Clone(CloneContext* ctx) const override {
|
|
|
|
auto* out = ctx->dst->create<Node>(name);
|
2020-12-03 18:10:39 +00:00
|
|
|
out->a = ctx->Clone(a);
|
|
|
|
out->b = ctx->Clone(b);
|
|
|
|
out->c = ctx->Clone(c);
|
2021-02-16 23:09:31 +00:00
|
|
|
out->vec = ctx->Clone(vec);
|
2020-12-03 18:10:39 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid() const override { return true; }
|
2021-01-29 10:55:40 +00:00
|
|
|
void to_str(const semantic::Info&, std::ostream&, size_t) const override {}
|
2020-12-03 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
2021-02-17 13:17:39 +00:00
|
|
|
struct Replaceable : public Castable<Replaceable, Node> {
|
2021-02-16 23:09:31 +00:00
|
|
|
explicit Replaceable(const Source& source, std::string n) : Base(source, n) {}
|
2021-01-26 16:57:10 +00:00
|
|
|
};
|
|
|
|
struct Replacement : public Castable<Replacement, Replaceable> {
|
2021-02-16 23:09:31 +00:00
|
|
|
explicit Replacement(const Source& source, std::string n) : Base(source, n) {}
|
2021-01-26 16:57:10 +00:00
|
|
|
};
|
2020-12-03 18:10:39 +00:00
|
|
|
|
2021-02-17 13:17:39 +00:00
|
|
|
struct NotANode : public Castable<NotANode, ast::Node> {
|
|
|
|
explicit NotANode(const Source& source) : Base(source) {}
|
2021-02-16 22:27:11 +00:00
|
|
|
|
2021-02-17 13:17:39 +00:00
|
|
|
NotANode* Clone(CloneContext* ctx) const override {
|
|
|
|
return ctx->dst->create<NotANode>();
|
2021-02-16 22:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid() const override { return true; }
|
|
|
|
void to_str(const semantic::Info&, std::ostream&, size_t) const override {}
|
|
|
|
};
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
TEST(CloneContext, Clone) {
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder builder;
|
2021-02-17 13:17:39 +00:00
|
|
|
auto* original_root = builder.create<Node>("root");
|
|
|
|
original_root->a = builder.create<Node>("a");
|
|
|
|
original_root->a->b = builder.create<Node>("a->b");
|
|
|
|
original_root->b = builder.create<Node>("b");
|
2020-12-03 18:10:39 +00:00
|
|
|
original_root->b->a = original_root->a; // Aliased
|
2021-02-17 13:17:39 +00:00
|
|
|
original_root->b->b = builder.create<Node>("b->b");
|
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)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N <──────┐ N <───────────────┘
|
2020-12-03 18:10:39 +00:00
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N └───┘ N
|
2020-12-03 18:10:39 +00:00
|
|
|
//
|
2021-02-17 13:17:39 +00:00
|
|
|
// N: Node
|
2020-12-03 18:10:39 +00:00
|
|
|
|
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);
|
|
|
|
|
2021-02-16 23:09:31 +00:00
|
|
|
EXPECT_EQ(cloned_root->name, "root");
|
|
|
|
EXPECT_EQ(cloned_root->a->name, "a");
|
|
|
|
EXPECT_EQ(cloned_root->a->b->name, "a->b");
|
|
|
|
EXPECT_EQ(cloned_root->b->name, "b");
|
|
|
|
EXPECT_EQ(cloned_root->b->b->name, "b->b");
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
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;
|
2021-02-17 13:17:39 +00:00
|
|
|
auto* original_root = builder.create<Node>("root");
|
|
|
|
original_root->a = builder.create<Node>("a");
|
2021-02-16 23:09:31 +00:00
|
|
|
original_root->a->b = builder.create<Replaceable>("a->b");
|
|
|
|
original_root->b = builder.create<Replaceable>("b");
|
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)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N <──────┐ R <───────────────┘
|
2020-12-03 18:10:39 +00:00
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
|
|
|
// R └───┘
|
|
|
|
//
|
2021-02-17 13:17:39 +00:00
|
|
|
// N: Node
|
2020-12-03 18:10:39 +00:00
|
|
|
// R: Replaceable
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder cloned;
|
2021-02-26 19:33:56 +00:00
|
|
|
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
ctx.ReplaceAll([&](Replaceable* in) {
|
|
|
|
auto* out = cloned.create<Replacement>("replacement:" + in->name);
|
|
|
|
out->b = cloned.create<Node>("replacement-child:" + in->name);
|
|
|
|
out->c = ctx.Clone(in->a);
|
|
|
|
return out;
|
|
|
|
});
|
|
|
|
auto* cloned_root = ctx.Clone(original_root);
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭─────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N <──────┐ R <───────────────┘
|
2020-12-03 18:10:39 +00:00
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// R │ N |
|
2020-12-03 18:10:39 +00:00
|
|
|
// ╭────┼────╮ └────────────┘
|
|
|
|
// (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N
|
2020-12-03 18:10:39 +00:00
|
|
|
//
|
2021-02-17 13:17:39 +00:00
|
|
|
// N: Node
|
2020-12-03 18:10:39 +00:00
|
|
|
// 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);
|
|
|
|
|
2021-02-16 23:09:31 +00:00
|
|
|
EXPECT_EQ(cloned_root->name, "root");
|
|
|
|
EXPECT_EQ(cloned_root->a->name, "a");
|
|
|
|
EXPECT_EQ(cloned_root->a->b->name, "replacement:a->b");
|
|
|
|
EXPECT_EQ(cloned_root->a->b->b->name, "replacement-child:a->b");
|
|
|
|
EXPECT_EQ(cloned_root->b->name, "replacement:b");
|
|
|
|
EXPECT_EQ(cloned_root->b->b->name, "replacement-child:b");
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
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;
|
2021-02-17 13:17:39 +00:00
|
|
|
auto* original_root = builder.create<Node>("root");
|
|
|
|
original_root->a = builder.create<Node>("a");
|
|
|
|
original_root->b = builder.create<Node>("b");
|
|
|
|
original_root->c = builder.create<Node>("c");
|
2021-01-26 16:57:10 +00:00
|
|
|
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-02-17 13:17:39 +00:00
|
|
|
auto* replacement = cloned.create<Node>("replacement");
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
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);
|
2021-02-16 23:09:31 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, "root");
|
|
|
|
EXPECT_EQ(cloned_root->a->name, "a");
|
|
|
|
EXPECT_EQ(cloned_root->b->name, "replacement");
|
|
|
|
EXPECT_EQ(cloned_root->c->name, "c");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CloneContext, CloneWithInsertBefore) {
|
|
|
|
ProgramBuilder builder;
|
2021-02-17 13:17:39 +00:00
|
|
|
auto* original_root = builder.create<Node>("root");
|
|
|
|
original_root->a = builder.create<Node>("a");
|
|
|
|
original_root->b = builder.create<Node>("b");
|
|
|
|
original_root->c = builder.create<Node>("c");
|
2021-02-16 23:09:31 +00:00
|
|
|
original_root->vec = {original_root->a, original_root->b, original_root->c};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
2021-02-17 13:17:39 +00:00
|
|
|
auto* insertion = cloned.create<Node>("insertion");
|
2021-02-16 23:09:31 +00:00
|
|
|
|
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.InsertBefore(original_root->b, insertion)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 4u);
|
|
|
|
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_EQ(cloned_root->name, "root");
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, "a");
|
|
|
|
EXPECT_EQ(cloned_root->vec[1]->name, "insertion");
|
|
|
|
EXPECT_EQ(cloned_root->vec[2]->name, "b");
|
|
|
|
EXPECT_EQ(cloned_root->vec[3]->name, "c");
|
2021-01-26 16:57:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 13:17:39 +00:00
|
|
|
TEST(CloneContext, CloneWithReplace_WithNotANode) {
|
2021-02-17 20:13:34 +00:00
|
|
|
EXPECT_FATAL_FAILURE(
|
|
|
|
{
|
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = builder.create<Node>("root");
|
|
|
|
original_root->a = builder.create<Node>("a");
|
|
|
|
original_root->b = builder.create<Node>("b");
|
|
|
|
original_root->c = builder.create<Node>("c");
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭──────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
|
|
|
// Replaced
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
auto* replacement = cloned.create<NotANode>();
|
|
|
|
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
ctx.Replace(original_root->b, replacement);
|
|
|
|
|
|
|
|
ctx.Clone(original_root);
|
|
|
|
},
|
|
|
|
"internal compiler error");
|
2021-02-16 22:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-03-02 20:51:18 +00:00
|
|
|
TINT_INSTANTIATE_TYPEINFO(Node);
|
|
|
|
TINT_INSTANTIATE_TYPEINFO(Replaceable);
|
|
|
|
TINT_INSTANTIATE_TYPEINFO(Replacement);
|
|
|
|
TINT_INSTANTIATE_TYPEINFO(NotANode);
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
} // namespace tint
|