2020-12-01 18:04:17 +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-01 18:04:17 +00:00
|
|
|
|
2021-04-13 20:07:57 +00:00
|
|
|
#include <string>
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program_builder.h"
|
2021-04-07 11:16:01 +00:00
|
|
|
#include "src/utils/get_or_create.h"
|
2020-12-15 12:32:18 +00:00
|
|
|
|
2021-03-02 20:51:18 +00:00
|
|
|
TINT_INSTANTIATE_TYPEINFO(tint::Cloneable);
|
2021-02-17 13:17:39 +00:00
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
namespace tint {
|
|
|
|
|
2021-03-31 21:00:26 +00:00
|
|
|
CloneContext::ListTransforms::ListTransforms() = default;
|
|
|
|
CloneContext::ListTransforms::~ListTransforms() = default;
|
|
|
|
|
2021-04-16 10:29:54 +00:00
|
|
|
CloneContext::CloneContext(ProgramBuilder* to,
|
|
|
|
Program const* from,
|
|
|
|
bool auto_clone_symbols)
|
|
|
|
: dst(to), src(from) {
|
|
|
|
if (auto_clone_symbols) {
|
|
|
|
// Almost all transforms will want to clone all symbols before doing any
|
|
|
|
// work, to avoid any newly created symbols clashing with existing symbols
|
|
|
|
// in the source program and causing them to be renamed.
|
|
|
|
from->Symbols().Foreach([&](Symbol s, const std::string&) { Clone(s); });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:06:31 +00:00
|
|
|
CloneContext::CloneContext(ProgramBuilder* builder)
|
|
|
|
: CloneContext(builder, nullptr, false) {}
|
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
CloneContext::~CloneContext() = default;
|
|
|
|
|
2021-04-07 11:16:01 +00:00
|
|
|
Symbol CloneContext::Clone(Symbol s) {
|
2021-05-04 18:06:31 +00:00
|
|
|
if (!src) {
|
|
|
|
return s; // In-place clone
|
|
|
|
}
|
2021-04-07 11:16:01 +00:00
|
|
|
return utils::GetOrCreate(cloned_symbols_, s, [&]() -> Symbol {
|
|
|
|
if (symbol_transform_) {
|
|
|
|
return symbol_transform_(s);
|
|
|
|
}
|
2021-04-13 20:07:57 +00:00
|
|
|
return dst->Symbols().New(src->Symbols().NameFor(s));
|
2021-04-07 11:16:01 +00:00
|
|
|
});
|
2020-12-15 12:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloneContext::Clone() {
|
2021-02-11 20:23:03 +00:00
|
|
|
dst->AST().Copy(this, &src->AST());
|
2021-06-25 10:26:26 +00:00
|
|
|
dst->SetTransformApplied(src->TransformsApplied());
|
2020-12-15 12:32:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ast::FunctionList CloneContext::Clone(const ast::FunctionList& v) {
|
|
|
|
ast::FunctionList out;
|
|
|
|
out.reserve(v.size());
|
|
|
|
for (ast::Function* el : v) {
|
|
|
|
out.Add(Clone(el));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
diag::List& CloneContext::Diagnostics() const {
|
|
|
|
return dst->Diagnostics();
|
|
|
|
}
|
|
|
|
|
2021-03-04 10:42:55 +00:00
|
|
|
CloneContext::CloneableTransform::CloneableTransform() = default;
|
|
|
|
CloneContext::CloneableTransform::CloneableTransform(
|
|
|
|
const CloneableTransform&) = default;
|
|
|
|
CloneContext::CloneableTransform::~CloneableTransform() = default;
|
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
} // namespace tint
|