2020-12-04 09:06:09 +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.
|
|
|
|
|
|
|
|
#ifndef SRC_TRANSFORM_TRANSFORM_H_
|
|
|
|
#define SRC_TRANSFORM_TRANSFORM_H_
|
|
|
|
|
|
|
|
#include <memory>
|
2021-02-24 15:55:24 +00:00
|
|
|
#include <unordered_map>
|
2020-12-04 09:06:09 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2021-06-25 10:26:26 +00:00
|
|
|
#include "src/castable.h"
|
2021-01-25 18:14:08 +00:00
|
|
|
#include "src/program.h"
|
2020-12-04 09:06:09 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace transform {
|
|
|
|
|
2021-03-29 21:03:59 +00:00
|
|
|
/// Data is the base class for transforms that accept extra input or emit extra
|
|
|
|
/// output information along with a Program.
|
2021-02-24 15:55:24 +00:00
|
|
|
class Data : public Castable<Data> {
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
Data();
|
|
|
|
|
|
|
|
/// Copy constructor
|
|
|
|
Data(const Data&);
|
|
|
|
|
|
|
|
/// Destructor
|
|
|
|
~Data() override;
|
2021-04-13 18:46:47 +00:00
|
|
|
|
|
|
|
/// Assignment operator
|
|
|
|
/// @returns this Data
|
|
|
|
Data& operator=(const Data&);
|
2021-02-24 15:55:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// DataMap is a map of Data unique pointers keyed by the Data's ClassID.
|
|
|
|
class DataMap {
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
DataMap();
|
|
|
|
|
|
|
|
/// Move constructor
|
|
|
|
DataMap(DataMap&&);
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
/// @param data_unique_ptrs a variadic list of additional data unique_ptrs
|
|
|
|
/// produced by the transform
|
|
|
|
template <typename... DATA>
|
|
|
|
explicit DataMap(DATA... data_unique_ptrs) {
|
|
|
|
PutAll(std::forward<DATA>(data_unique_ptrs)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destructor
|
|
|
|
~DataMap();
|
|
|
|
|
2021-04-16 08:35:24 +00:00
|
|
|
/// Move assignment operator
|
|
|
|
/// @param rhs the DataMap to move into this DataMap
|
|
|
|
/// @return this DataMap
|
|
|
|
DataMap& operator=(DataMap&& rhs);
|
|
|
|
|
2021-02-24 15:55:24 +00:00
|
|
|
/// Adds the data into DataMap keyed by the ClassID of type T.
|
|
|
|
/// @param data the data to add to the DataMap
|
|
|
|
template <typename T>
|
|
|
|
void Put(std::unique_ptr<T>&& data) {
|
|
|
|
static_assert(std::is_base_of<Data, T>::value,
|
|
|
|
"T does not derive from Data");
|
2021-03-02 20:30:08 +00:00
|
|
|
map_[&TypeInfo::Of<T>()] = std::move(data);
|
2021-02-24 15:55:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:03:59 +00:00
|
|
|
/// Creates the data of type `T` with the provided arguments and adds it into
|
|
|
|
/// DataMap keyed by the ClassID of type T.
|
|
|
|
/// @param args the arguments forwarded to the constructor for type T
|
|
|
|
template <typename T, typename... ARGS>
|
|
|
|
void Add(ARGS&&... args) {
|
|
|
|
Put(std::make_unique<T>(std::forward<ARGS>(args)...));
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:55:24 +00:00
|
|
|
/// @returns a pointer to the Data placed into the DataMap with a call to
|
|
|
|
/// Put()
|
|
|
|
template <typename T>
|
|
|
|
T const* Get() const {
|
2021-03-02 20:30:08 +00:00
|
|
|
auto it = map_.find(&TypeInfo::Of<T>());
|
2021-02-24 15:55:24 +00:00
|
|
|
if (it == map_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return static_cast<T*>(it->second.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add moves all the data from other into this DataMap
|
|
|
|
/// @param other the DataMap to move into this DataMap
|
|
|
|
void Add(DataMap&& other) {
|
|
|
|
for (auto& it : other.map_) {
|
|
|
|
map_.emplace(it.first, std::move(it.second));
|
|
|
|
}
|
|
|
|
other.map_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T0>
|
|
|
|
void PutAll(T0&& first) {
|
|
|
|
Put(std::forward<T0>(first));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T0, typename... Tn>
|
|
|
|
void PutAll(T0&& first, Tn&&... remainder) {
|
|
|
|
Put(std::forward<T0>(first));
|
|
|
|
PutAll(std::forward<Tn>(remainder)...);
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:30:08 +00:00
|
|
|
std::unordered_map<const TypeInfo*, std::unique_ptr<Data>> map_;
|
2021-02-24 15:55:24 +00:00
|
|
|
};
|
|
|
|
|
2021-04-16 08:35:24 +00:00
|
|
|
/// The return type of Run()
|
|
|
|
class Output {
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
Output();
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
/// @param program the program to move into this Output
|
|
|
|
explicit Output(Program&& program);
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
/// @param program_ the program to move into this Output
|
|
|
|
/// @param data_ a variadic list of additional data unique_ptrs produced by
|
|
|
|
/// the transform
|
|
|
|
template <typename... DATA>
|
|
|
|
Output(Program&& program_, DATA... data_)
|
|
|
|
: program(std::move(program_)), data(std::forward<DATA>(data_)...) {}
|
|
|
|
|
|
|
|
/// The transformed program. May be empty on error.
|
|
|
|
Program program;
|
|
|
|
|
|
|
|
/// Extra output generated by the transforms.
|
|
|
|
DataMap data;
|
|
|
|
};
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Interface for Program transforms
|
2021-06-25 10:26:26 +00:00
|
|
|
class Transform : public Castable<Transform> {
|
2020-12-04 09:06:09 +00:00
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
Transform();
|
|
|
|
/// Destructor
|
2021-06-25 10:26:26 +00:00
|
|
|
~Transform() override;
|
2020-12-04 09:06:09 +00:00
|
|
|
|
2021-01-25 18:14:08 +00:00
|
|
|
/// Runs the transform on `program`, returning the transformation result.
|
|
|
|
/// @param program the source program to transform
|
2021-03-29 21:03:59 +00:00
|
|
|
/// @param data optional extra transform-specific input data
|
2021-01-25 18:14:08 +00:00
|
|
|
/// @returns the transformation result
|
2021-06-25 10:26:26 +00:00
|
|
|
virtual Output Run(const Program* program, const DataMap& data = {});
|
2021-01-25 18:14:08 +00:00
|
|
|
|
2020-12-14 20:31:17 +00:00
|
|
|
protected:
|
2021-06-25 10:26:26 +00:00
|
|
|
/// Runs the transform using the CloneContext built for transforming a
|
|
|
|
/// program. Run() is responsible for calling Clone() on the CloneContext.
|
|
|
|
/// @param ctx the CloneContext primed with the input program and
|
|
|
|
/// ProgramBuilder
|
|
|
|
/// @param inputs optional extra transform-specific input data
|
|
|
|
/// @param outputs optional extra transform-specific output data
|
|
|
|
virtual void Run(CloneContext& ctx, const DataMap& inputs, DataMap& outputs);
|
|
|
|
|
|
|
|
/// Requires appends an error diagnostic to `ctx.dst` if the template type
|
|
|
|
/// transforms were not already run on `ctx.src`.
|
|
|
|
/// @param ctx the CloneContext
|
|
|
|
/// @returns true if all dependency transforms have been run
|
|
|
|
template <typename... TRANSFORMS>
|
|
|
|
bool Requires(CloneContext& ctx) {
|
|
|
|
return Requires(ctx, {&::tint::TypeInfo::Of<TRANSFORMS>()...});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Requires appends an error diagnostic to `ctx.dst` if the list of
|
|
|
|
/// Transforms were not already run on `ctx.src`.
|
|
|
|
/// @param ctx the CloneContext
|
|
|
|
/// @param deps the list of Transform TypeInfos
|
|
|
|
/// @returns true if all dependency transforms have been run
|
|
|
|
bool Requires(CloneContext& ctx,
|
|
|
|
std::initializer_list<const ::tint::TypeInfo*> deps);
|
|
|
|
|
2020-12-14 20:31:17 +00:00
|
|
|
/// Clones the function `in` adding `statements` to the beginning of the
|
|
|
|
/// cloned function body.
|
|
|
|
/// @param ctx the clone context
|
|
|
|
/// @param in the function to clone
|
|
|
|
/// @param statements the statements to prepend to `in`'s body
|
|
|
|
/// @return the cloned function
|
|
|
|
static ast::Function* CloneWithStatementsAtStart(
|
2021-01-21 16:20:40 +00:00
|
|
|
CloneContext* ctx,
|
2020-12-14 20:31:17 +00:00
|
|
|
ast::Function* in,
|
|
|
|
ast::StatementList statements);
|
2021-03-04 23:03:25 +00:00
|
|
|
|
2021-03-24 17:08:06 +00:00
|
|
|
/// Clones the decoration list `in`, removing decorations based on a filter.
|
|
|
|
/// @param ctx the clone context
|
|
|
|
/// @param in the decorations to clone
|
|
|
|
/// @param should_remove the function to select which decorations to remove
|
|
|
|
/// @return the cloned decorations
|
|
|
|
static ast::DecorationList RemoveDecorations(
|
|
|
|
CloneContext* ctx,
|
|
|
|
const ast::DecorationList& in,
|
|
|
|
std::function<bool(const ast::Decoration*)> should_remove);
|
2021-05-04 18:13:21 +00:00
|
|
|
|
|
|
|
/// CreateASTTypeFor constructs new ast::Type nodes that reconstructs the
|
|
|
|
/// semantic type `ty`.
|
|
|
|
/// @param ctx the clone context
|
|
|
|
/// @param ty the semantic type to reconstruct
|
|
|
|
/// @returns a ast::Type that when resolved, will produce the semantic type
|
|
|
|
/// `ty`.
|
|
|
|
static ast::Type* CreateASTTypeFor(CloneContext* ctx, const sem::Type* ty);
|
2020-12-04 09:06:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace transform
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_TRANSFORM_TRANSFORM_H_
|