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
|
|
|
#ifndef SRC_CLONE_CONTEXT_H_
|
|
|
|
#define SRC_CLONE_CONTEXT_H_
|
2020-12-01 18:04:17 +00:00
|
|
|
|
2021-02-16 22:27:11 +00:00
|
|
|
#include <cassert>
|
2020-12-03 18:10:39 +00:00
|
|
|
#include <functional>
|
2020-12-01 18:04:17 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
#include "src/castable.h"
|
2020-12-01 18:04:17 +00:00
|
|
|
#include "src/source.h"
|
2020-12-15 12:32:18 +00:00
|
|
|
#include "src/symbol.h"
|
2021-01-21 16:20:40 +00:00
|
|
|
#include "src/traits.h"
|
2020-12-01 18:04:17 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
|
2021-01-21 16:20:40 +00:00
|
|
|
// Forward declarations
|
2021-01-26 16:57:10 +00:00
|
|
|
class Program;
|
2021-01-26 16:57:10 +00:00
|
|
|
class ProgramBuilder;
|
|
|
|
|
|
|
|
namespace ast {
|
|
|
|
|
|
|
|
class FunctionList;
|
|
|
|
|
|
|
|
} // namespace ast
|
2020-12-01 18:04:17 +00:00
|
|
|
|
|
|
|
/// CloneContext holds the state used while cloning AST nodes and types.
|
|
|
|
class CloneContext {
|
|
|
|
public:
|
|
|
|
/// Constructor
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @param to the target ProgramBuilder to clone into
|
|
|
|
/// @param from the source Program to clone from
|
|
|
|
CloneContext(ProgramBuilder* to, Program const* from);
|
2020-12-03 18:10:39 +00:00
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
/// Destructor
|
|
|
|
~CloneContext();
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Clones the Node or type::Type `a` into the ProgramBuilder #dst if `a` is
|
|
|
|
/// not null. If `a` is null, then Clone() returns null. If `a` has been
|
|
|
|
/// cloned already by this CloneContext then the same cloned pointer is
|
|
|
|
/// returned.
|
2020-12-03 18:10:39 +00:00
|
|
|
///
|
|
|
|
/// Clone() may use a function registered with ReplaceAll() to create a
|
|
|
|
/// transformed version of the object. See ReplaceAll() for more information.
|
|
|
|
///
|
2021-01-26 16:57:10 +00:00
|
|
|
/// The Node or type::Type `a` must be owned by the Program #src.
|
2020-12-15 12:32:18 +00:00
|
|
|
///
|
2020-12-01 18:04:17 +00:00
|
|
|
/// @note Semantic information such as resolved expression type and intrinsic
|
|
|
|
/// information is not cloned.
|
|
|
|
/// @param a the `Node` or `type::Type` to clone
|
|
|
|
/// @return the cloned node
|
|
|
|
template <typename T>
|
|
|
|
T* Clone(T* a) {
|
2020-12-03 18:10:39 +00:00
|
|
|
// If the input is nullptr, there's nothing to clone - just return nullptr.
|
2020-12-01 18:04:17 +00:00
|
|
|
if (a == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
// See if we've already cloned this object - if we have return the
|
|
|
|
// previously cloned pointer.
|
|
|
|
// If we haven't cloned this before, try cloning using a replacer transform.
|
|
|
|
if (auto* c = LookupOrTransform(a)) {
|
2021-02-16 22:27:11 +00:00
|
|
|
return CheckedCast<T>(c);
|
2020-12-01 18:04:17 +00:00
|
|
|
}
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
// First time clone and no replacer transforms matched.
|
|
|
|
// Clone with T::Clone().
|
2020-12-01 18:04:17 +00:00
|
|
|
auto* c = a->Clone(this);
|
|
|
|
cloned_.emplace(a, c);
|
2021-02-16 22:27:11 +00:00
|
|
|
return CheckedCast<T>(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clones the Node or type::Type `a` into the ProgramBuilder #dst if `a` is
|
|
|
|
/// not null. If `a` is null, then Clone() returns null. If `a` has been
|
|
|
|
/// cloned already by this CloneContext then the same cloned pointer is
|
|
|
|
/// returned.
|
|
|
|
///
|
|
|
|
/// Unlike Clone(), this method does not invoke or use any transformations
|
|
|
|
/// registered by ReplaceAll().
|
|
|
|
///
|
|
|
|
/// The Node or type::Type `a` must be owned by the Program #src.
|
|
|
|
///
|
|
|
|
/// @note Semantic information such as resolved expression type and intrinsic
|
|
|
|
/// information is not cloned.
|
|
|
|
/// @param a the `Node` or `type::Type` to clone
|
|
|
|
/// @return the cloned node
|
|
|
|
template <typename T>
|
|
|
|
T* CloneWithoutTransform(T* a) {
|
|
|
|
// If the input is nullptr, there's nothing to clone - just return nullptr.
|
|
|
|
if (a == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()) {
|
|
|
|
return CheckedCast<T>(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// First time clone and no replacer transforms matched.
|
|
|
|
// Clone with T::Clone().
|
|
|
|
auto* c = a->Clone(this);
|
|
|
|
cloned_.emplace(a, c);
|
|
|
|
return CheckedCast<T>(c);
|
2020-12-01 18:04:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 13:41:06 +00:00
|
|
|
/// Clones the Source `s` into `dst`
|
2020-12-01 18:04:17 +00:00
|
|
|
/// TODO(bclayton) - Currently this 'clone' is a shallow copy. If/when
|
2021-01-26 16:57:10 +00:00
|
|
|
/// `Source.File`s are owned by the Program this should make a copy of the
|
2020-12-01 18:04:17 +00:00
|
|
|
/// file.
|
|
|
|
/// @param s the `Source` to clone
|
|
|
|
/// @return the cloned source
|
2020-12-15 12:32:18 +00:00
|
|
|
Source Clone(const Source& s) const { return s; }
|
|
|
|
|
2021-01-22 13:41:06 +00:00
|
|
|
/// Clones the Symbol `s` into `dst`
|
2020-12-15 12:32:18 +00:00
|
|
|
///
|
2021-01-26 16:57:10 +00:00
|
|
|
/// The Symbol `s` must be owned by the Program #src.
|
2020-12-15 12:32:18 +00:00
|
|
|
///
|
|
|
|
/// @param s the Symbol to clone
|
|
|
|
/// @return the cloned source
|
|
|
|
Symbol Clone(const Symbol& s) const;
|
2020-12-01 18:04:17 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Clones each of the elements of the vector `v` into the ProgramBuilder
|
|
|
|
/// #dst.
|
2020-12-15 12:32:18 +00:00
|
|
|
///
|
2021-01-26 16:57:10 +00:00
|
|
|
/// All the elements of the vector `v` must be owned by the Program #src.
|
2020-12-15 12:32:18 +00:00
|
|
|
///
|
2020-12-01 18:04:17 +00:00
|
|
|
/// @param v the vector to clone
|
|
|
|
/// @return the cloned vector
|
|
|
|
template <typename T>
|
|
|
|
std::vector<T> Clone(const std::vector<T>& v) {
|
|
|
|
std::vector<T> out;
|
|
|
|
out.reserve(v.size());
|
|
|
|
for (auto& el : v) {
|
|
|
|
out.emplace_back(Clone(el));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-02-16 23:09:31 +00:00
|
|
|
/// Clones each of the elements of the vector `v` into the ProgramBuilder
|
|
|
|
/// #dst, inserting any additional elements into the list that were registered
|
|
|
|
/// with calls to InsertBefore().
|
|
|
|
///
|
|
|
|
/// All the elements of the vector `v` must be owned by the Program #src.
|
|
|
|
///
|
|
|
|
/// @param v the vector to clone
|
|
|
|
/// @return the cloned vector
|
|
|
|
template <typename T>
|
|
|
|
std::vector<T*> Clone(const std::vector<T*>& v) {
|
|
|
|
std::vector<T*> out;
|
|
|
|
out.reserve(v.size());
|
|
|
|
for (auto& el : v) {
|
|
|
|
auto it = insert_before_.find(el);
|
|
|
|
if (it != insert_before_.end()) {
|
|
|
|
for (auto insert : it->second) {
|
|
|
|
out.emplace_back(CheckedCast<T>(insert));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.emplace_back(Clone(el));
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Clones each of the elements of the vector `v` into the ProgramBuilder
|
|
|
|
/// #dst.
|
2021-01-26 16:57:10 +00:00
|
|
|
///
|
|
|
|
/// All the elements of the vector `v` must be owned by the Program #src.
|
|
|
|
///
|
|
|
|
/// @param v the vector to clone
|
|
|
|
/// @return the cloned vector
|
|
|
|
ast::FunctionList Clone(const ast::FunctionList& v);
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
/// ReplaceAll() registers `replacer` to be called whenever the Clone() method
|
2020-12-15 12:32:18 +00:00
|
|
|
/// is called with a type that matches (or derives from) the type of the
|
|
|
|
/// second parameter of `replacer`.
|
2020-12-03 18:10:39 +00:00
|
|
|
///
|
2020-12-15 12:32:18 +00:00
|
|
|
/// `replacer` must be function-like with the signature:
|
|
|
|
/// `T* (CloneContext*, T*)`
|
|
|
|
/// where `T` is a type deriving from CastableBase.
|
2020-12-03 18:10:39 +00:00
|
|
|
///
|
|
|
|
/// If `replacer` returns a nullptr then Clone() will attempt the next
|
|
|
|
/// registered replacer function that matches the object type. If no replacers
|
|
|
|
/// match the object type, or all returned nullptr then Clone() will call
|
|
|
|
/// `T::Clone()` to clone the object.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // Replace all ast::UintLiterals with the number 42
|
2020-12-15 12:32:18 +00:00
|
|
|
/// CloneCtx ctx(&out, in)
|
|
|
|
/// .ReplaceAll([&] (CloneContext* ctx, ast::UintLiteral* l) {
|
2021-01-26 16:57:10 +00:00
|
|
|
/// return ctx->dst->create<ast::UintLiteral>(
|
|
|
|
/// ctx->Clone(l->source()),
|
|
|
|
/// ctx->Clone(l->type()),
|
|
|
|
/// 42);
|
2020-12-15 12:32:18 +00:00
|
|
|
/// }).Clone();
|
2020-12-03 18:10:39 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2021-02-16 22:27:11 +00:00
|
|
|
/// @warning The replacement object must be of the correct type for all
|
|
|
|
/// references of the original object. A type mismatch will result in an
|
|
|
|
/// assertion in debug builds, and undefined behavior in release builds.
|
2020-12-03 18:10:39 +00:00
|
|
|
/// @param replacer a function or function-like object with the signature
|
2020-12-15 12:32:18 +00:00
|
|
|
/// `T* (CloneContext*, T*)`, where `T` derives from CastableBase
|
|
|
|
/// @returns this CloneContext so calls can be chained
|
2020-12-03 18:10:39 +00:00
|
|
|
template <typename F>
|
2020-12-15 12:32:18 +00:00
|
|
|
CloneContext& ReplaceAll(F replacer) {
|
|
|
|
using TPtr = traits::ParamTypeT<F, 1>;
|
2020-12-03 18:10:39 +00:00
|
|
|
using T = typename std::remove_pointer<TPtr>::type;
|
|
|
|
transforms_.emplace_back([=](CastableBase* in) {
|
|
|
|
auto* in_as_t = in->As<T>();
|
2020-12-15 12:32:18 +00:00
|
|
|
return in_as_t != nullptr ? replacer(this, in_as_t) : nullptr;
|
2020-12-03 18:10:39 +00:00
|
|
|
});
|
2020-12-15 12:32:18 +00:00
|
|
|
return *this;
|
2020-12-03 18:10:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Replace replaces all occurrences of `what` in #src with `with` in #dst
|
|
|
|
/// when calling Clone().
|
|
|
|
/// @param what a pointer to the object in #src that will be replaced with
|
|
|
|
/// `with`
|
2021-02-16 22:27:11 +00:00
|
|
|
/// @param with a pointer to the replacement object owned by #dst that will be
|
|
|
|
/// used as a replacement for `what`
|
|
|
|
/// @warning The replacement object must be of the correct type for all
|
|
|
|
/// references of the original object. A type mismatch will result in an
|
|
|
|
/// assertion in debug builds, and undefined behavior in release builds.
|
2021-01-26 16:57:10 +00:00
|
|
|
/// @returns this CloneContext so calls can be chained
|
2021-02-16 22:27:11 +00:00
|
|
|
template <typename WHAT, typename WITH>
|
|
|
|
CloneContext& Replace(WHAT* what, WITH* with) {
|
|
|
|
cloned_[what] = with;
|
2021-01-26 16:57:10 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-02-16 23:09:31 +00:00
|
|
|
/// Inserts `object` before `before` whenever a vector containing `object` is
|
|
|
|
/// cloned.
|
|
|
|
/// @param before a pointer to the object in #src
|
|
|
|
/// @param object a pointer to the object in #dst that will be inserted before
|
|
|
|
/// any occurrence of the clone of `before`
|
|
|
|
/// @returns this CloneContext so calls can be chained
|
|
|
|
template <typename BEFORE, typename OBJECT>
|
|
|
|
CloneContext& InsertBefore(BEFORE* before, OBJECT* object) {
|
|
|
|
auto& list = insert_before_[before];
|
|
|
|
list.emplace_back(object);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Clone performs the clone of the entire Program #src to #dst.
|
2020-12-15 12:32:18 +00:00
|
|
|
void Clone();
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// The target ProgramBuilder to clone into.
|
|
|
|
ProgramBuilder* const dst;
|
2020-12-01 18:04:17 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// The source Program to clone from.
|
2021-01-26 16:57:10 +00:00
|
|
|
Program const* const src;
|
2020-12-15 12:32:18 +00:00
|
|
|
|
2020-12-01 18:04:17 +00:00
|
|
|
private:
|
2020-12-03 18:10:39 +00:00
|
|
|
using Transform = std::function<CastableBase*(CastableBase*)>;
|
|
|
|
|
2020-12-15 12:32:18 +00:00
|
|
|
CloneContext(const CloneContext&) = delete;
|
|
|
|
CloneContext& operator=(const CloneContext&) = delete;
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
/// LookupOrTransform is the template-independent logic of Clone().
|
|
|
|
/// This is outside of Clone() to reduce the amount of template-instantiated
|
|
|
|
/// code.
|
|
|
|
CastableBase* LookupOrTransform(CastableBase* a) {
|
|
|
|
// 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()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to clone using the registered replacer functions.
|
|
|
|
for (auto& f : transforms_) {
|
|
|
|
if (CastableBase* c = f(a)) {
|
|
|
|
cloned_.emplace(a, c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No luck, Clone() will have to call T::Clone().
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:27:11 +00:00
|
|
|
/// Cast `obj` from type `FROM` to type `TO`, returning the cast object.
|
|
|
|
/// Asserts if the cast failed.
|
|
|
|
template <typename TO, typename FROM>
|
|
|
|
TO* CheckedCast(FROM* obj) {
|
|
|
|
TO* cast = obj->template As<TO>();
|
|
|
|
assert(cast /* cloned object was not of the expected type */);
|
|
|
|
return cast;
|
|
|
|
}
|
|
|
|
|
2021-02-16 23:09:31 +00:00
|
|
|
/// A vector of CastableBase*
|
|
|
|
using CastableList = std::vector<CastableBase*>;
|
|
|
|
|
|
|
|
/// A map of object in #src to their cloned equivalent in #dst
|
2020-12-03 18:10:39 +00:00
|
|
|
std::unordered_map<CastableBase*, CastableBase*> cloned_;
|
2021-02-16 23:09:31 +00:00
|
|
|
|
|
|
|
/// A map of object in #src to the list of cloned objects in #dst.
|
|
|
|
/// Clone(const std::vector<T*>& v) will use this to insert the map-value list
|
|
|
|
/// into the target vector/ before cloning and inserting the map-key.
|
|
|
|
std::unordered_map<CastableBase*, CastableList> insert_before_;
|
|
|
|
|
|
|
|
/// Transform functions registered with ReplaceAll()
|
2020-12-03 18:10:39 +00:00
|
|
|
std::vector<Transform> transforms_;
|
2020-12-01 18:04:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace tint
|
|
|
|
|
2021-01-21 16:20:40 +00:00
|
|
|
#endif // SRC_CLONE_CONTEXT_H_
|