Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "src/tint/clone_context.h" |
| 16 | |
| 17 | #include <string> |
| 18 | |
| 19 | #include "src/tint/program_builder.h" |
| 20 | #include "src/tint/utils/map.h" |
| 21 | |
| 22 | TINT_INSTANTIATE_TYPEINFO(tint::Cloneable); |
| 23 | |
| 24 | namespace tint { |
| 25 | |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 26 | Cloneable::Cloneable() = default; |
| 27 | Cloneable::Cloneable(Cloneable&&) = default; |
| 28 | Cloneable::~Cloneable() = default; |
| 29 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 30 | CloneContext::CloneContext(ProgramBuilder* to, Program const* from, bool auto_clone_symbols) |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | : dst(to), src(from) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 32 | if (auto_clone_symbols) { |
| 33 | // Almost all transforms will want to clone all symbols before doing any |
| 34 | // work, to avoid any newly created symbols clashing with existing symbols |
| 35 | // in the source program and causing them to be renamed. |
| 36 | from->Symbols().Foreach([&](Symbol s, const std::string&) { Clone(s); }); |
| 37 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 38 | } |
| 39 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | CloneContext::CloneContext(ProgramBuilder* builder) : CloneContext(builder, nullptr, false) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 41 | |
| 42 | CloneContext::~CloneContext() = default; |
| 43 | |
| 44 | Symbol CloneContext::Clone(Symbol s) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 45 | if (!src) { |
| 46 | return s; // In-place clone |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 47 | } |
Ben Clayton | 18dc315 | 2022-08-23 18:38:35 +0000 | [diff] [blame] | 48 | return cloned_symbols_.GetOrCreate(s, [&]() -> Symbol { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 49 | if (symbol_transform_) { |
| 50 | return symbol_transform_(s); |
| 51 | } |
| 52 | return dst->Symbols().New(src->Symbols().NameFor(s)); |
| 53 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void CloneContext::Clone() { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 57 | dst->AST().Copy(this, &src->AST()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | ast::FunctionList CloneContext::Clone(const ast::FunctionList& v) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 61 | ast::FunctionList out; |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 62 | out.Reserve(v.Length()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 63 | for (const ast::Function* el : v) { |
| 64 | out.Add(Clone(el)); |
| 65 | } |
| 66 | return out; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | const tint::Cloneable* CloneContext::CloneCloneable(const Cloneable* object) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 70 | // If the input is nullptr, there's nothing to clone - just return nullptr. |
| 71 | if (object == nullptr) { |
| 72 | return nullptr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 73 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 74 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 75 | // Was Replace() called for this object? |
Ben Clayton | 18dc315 | 2022-08-23 18:38:35 +0000 | [diff] [blame] | 76 | if (auto* fn = replacements_.Find(object)) { |
| 77 | return (*fn)(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Attempt to clone using the registered replacer functions. |
| 81 | auto& typeinfo = object->TypeInfo(); |
| 82 | for (auto& transform : transforms_) { |
| 83 | if (typeinfo.Is(transform.typeinfo)) { |
| 84 | if (auto* transformed = transform.function(object)) { |
| 85 | return transformed; |
| 86 | } |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // No transform for this type, or the transform returned nullptr. |
| 92 | // Clone with T::Clone(). |
| 93 | return object->Clone(this); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 94 | } |
| 95 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | void CloneContext::CheckedCastFailure(const Cloneable* got, const TypeInfo& expected) { |
| 97 | TINT_ICE(Clone, Diagnostics()) << "Cloned object was not of the expected type\n" |
| 98 | << "got: " << got->TypeInfo().name << "\n" |
| 99 | << "expected: " << expected.name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | diag::List& CloneContext::Diagnostics() const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 103 | return dst->Diagnostics(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | CloneContext::CloneableTransform::CloneableTransform() = default; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 107 | CloneContext::CloneableTransform::CloneableTransform(const CloneableTransform&) = default; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 108 | CloneContext::CloneableTransform::~CloneableTransform() = default; |
| 109 | |
| 110 | } // namespace tint |