mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-18 17:35:30 +00:00
Add create<T>() method to Module for types
Migrate all uses to use this and the new `unique_type<T>()` and `types()` methods. Remove the `type_mgr()` accessor. `TypeManager` is now an implementation detail of the module, allowing us to unify the allocation of types and nodes (if we so wish). Fixes: tint:337 Bug: tint:307 Change-Id: I233fa9dc73d60515dd721f02ea7ba089ef7d374f Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33667 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
committed by
Commit Bot service account
parent
0fb5168fc7
commit
7e4ffa0064
@@ -17,16 +17,16 @@
|
||||
namespace tint {
|
||||
namespace ast {
|
||||
|
||||
TypesBuilder::TypesBuilder(TypeManager* tm)
|
||||
: bool_(tm->Get<ast::type::BoolType>()),
|
||||
f32(tm->Get<ast::type::F32Type>()),
|
||||
i32(tm->Get<ast::type::I32Type>()),
|
||||
u32(tm->Get<ast::type::U32Type>()),
|
||||
void_(tm->Get<ast::type::VoidType>()),
|
||||
tm_(tm) {}
|
||||
TypesBuilder::TypesBuilder(Module* mod)
|
||||
: bool_(mod->create<ast::type::BoolType>()),
|
||||
f32(mod->create<ast::type::F32Type>()),
|
||||
i32(mod->create<ast::type::I32Type>()),
|
||||
u32(mod->create<ast::type::U32Type>()),
|
||||
void_(mod->create<ast::type::VoidType>()),
|
||||
mod_(mod) {}
|
||||
|
||||
Builder::Builder(tint::Context* c, tint::ast::Module* m)
|
||||
: ctx(c), mod(m), ty(&m->type_mgr()) {}
|
||||
: ctx(c), mod(m), ty(m) {}
|
||||
Builder::~Builder() = default;
|
||||
|
||||
ast::Variable* Builder::Var(const std::string& name,
|
||||
|
||||
@@ -47,8 +47,8 @@ namespace ast {
|
||||
class TypesBuilder {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param tm the type manager
|
||||
explicit TypesBuilder(TypeManager* tm);
|
||||
/// @param mod the module
|
||||
explicit TypesBuilder(Module* mod);
|
||||
|
||||
/// A boolean type
|
||||
ast::type::BoolType* const bool_;
|
||||
@@ -70,80 +70,80 @@ class TypesBuilder {
|
||||
/// @return the tint AST type for a 2-element vector of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::VectorType* vec2() const {
|
||||
return tm_->Get<ast::type::VectorType>(Of<T>(), 2);
|
||||
return mod_->create<ast::type::VectorType>(Of<T>(), 2);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 3-element vector of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::VectorType* vec3() const {
|
||||
return tm_->Get<ast::type::VectorType>(Of<T>(), 3);
|
||||
return mod_->create<ast::type::VectorType>(Of<T>(), 3);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 4-element vector of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::Type* vec4() const {
|
||||
return tm_->Get<ast::type::VectorType>(Of<T>(), 4);
|
||||
return mod_->create<ast::type::VectorType>(Of<T>(), 4);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 2x3 matrix of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::MatrixType* mat2x2() const {
|
||||
return tm_->Get<ast::type::MatrixType>(Of<T>(), 2, 2);
|
||||
return mod_->create<ast::type::MatrixType>(Of<T>(), 2, 2);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 2x3 matrix of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::MatrixType* mat2x3() const {
|
||||
return tm_->Get<ast::type::MatrixType>(Of<T>(), 3, 2);
|
||||
return mod_->create<ast::type::MatrixType>(Of<T>(), 3, 2);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 2x4 matrix of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::MatrixType* mat2x4() const {
|
||||
return tm_->Get<ast::type::MatrixType>(Of<T>(), 4, 2);
|
||||
return mod_->create<ast::type::MatrixType>(Of<T>(), 4, 2);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 3x2 matrix of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::MatrixType* mat3x2() const {
|
||||
return tm_->Get<ast::type::MatrixType>(Of<T>(), 2, 3);
|
||||
return mod_->create<ast::type::MatrixType>(Of<T>(), 2, 3);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 3x3 matrix of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::MatrixType* mat3x3() const {
|
||||
return tm_->Get<ast::type::MatrixType>(Of<T>(), 3, 3);
|
||||
return mod_->create<ast::type::MatrixType>(Of<T>(), 3, 3);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 3x4 matrix of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::MatrixType* mat3x4() const {
|
||||
return tm_->Get<ast::type::MatrixType>(Of<T>(), 4, 3);
|
||||
return mod_->create<ast::type::MatrixType>(Of<T>(), 4, 3);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 4x2 matrix of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::MatrixType* mat4x2() const {
|
||||
return tm_->Get<ast::type::MatrixType>(Of<T>(), 2, 4);
|
||||
return mod_->create<ast::type::MatrixType>(Of<T>(), 2, 4);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 4x3 matrix of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::MatrixType* mat4x3() const {
|
||||
return tm_->Get<ast::type::MatrixType>(Of<T>(), 3, 4);
|
||||
return mod_->create<ast::type::MatrixType>(Of<T>(), 3, 4);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for a 4x4 matrix of the C type `T`.
|
||||
template <typename T>
|
||||
ast::type::MatrixType* mat4x4() const {
|
||||
return tm_->Get<ast::type::MatrixType>(Of<T>(), 4, 4);
|
||||
return mod_->create<ast::type::MatrixType>(Of<T>(), 4, 4);
|
||||
}
|
||||
|
||||
/// @param subtype the array element type
|
||||
/// @param n the array size. 0 represents unbounded
|
||||
/// @return the tint AST type for a array of size `n` of type `T`
|
||||
ast::type::ArrayType* array(ast::type::Type* subtype, uint32_t n) const {
|
||||
return tm_->Get<ast::type::ArrayType>(subtype, n);
|
||||
return mod_->create<ast::type::ArrayType>(subtype, n);
|
||||
}
|
||||
|
||||
/// @return the tint AST type for an array of size `N` of type `T`
|
||||
@@ -161,7 +161,7 @@ class TypesBuilder {
|
||||
template <typename T>
|
||||
struct CToAST {};
|
||||
|
||||
TypeManager* const tm_;
|
||||
Module* const mod_;
|
||||
};
|
||||
|
||||
/// Helper for building common AST constructs.
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -30,6 +32,10 @@ namespace ast {
|
||||
|
||||
/// Represents all the source in a given program.
|
||||
class Module {
|
||||
template <typename T, typename BASE>
|
||||
using EnableIfIsType =
|
||||
typename std::enable_if<std::is_base_of<BASE, T>::value, T>::type;
|
||||
|
||||
public:
|
||||
Module();
|
||||
/// Move constructor
|
||||
@@ -78,15 +84,12 @@ class Module {
|
||||
/// @returns a string representation of the module
|
||||
std::string to_str() const;
|
||||
|
||||
/// @returns the Type Manager
|
||||
ast::TypeManager& type_mgr() { return type_mgr_; }
|
||||
|
||||
/// Creates a new `ast::Node` owned by the Module. When the Module is
|
||||
/// destructed, the `ast::Node` will also be destructed.
|
||||
/// @param args the arguments to pass to the type constructor
|
||||
/// @returns the node pointer
|
||||
template <typename T, typename... ARGS>
|
||||
T* create(ARGS&&... args) {
|
||||
EnableIfIsType<T, ast::Node>* create(ARGS&&... args) {
|
||||
static_assert(std::is_base_of<ast::Node, T>::value,
|
||||
"T does not derive from ast::Node");
|
||||
auto uptr = std::make_unique<T>(std::forward<ARGS>(args)...);
|
||||
@@ -95,6 +98,38 @@ class Module {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/// Creates a new `ast::Type` owned by the Module.
|
||||
/// When the Module is destructed, owned Module and the returned
|
||||
/// `ast::Type` will also be destructed.
|
||||
/// Types are unique (de-aliased), and so `create()` for the same `T` and
|
||||
/// arguments will return the same pointer.
|
||||
/// @param args the arguments to pass to the type constructor
|
||||
/// @returns the de-aliased type pointer
|
||||
template <typename T, typename... ARGS>
|
||||
EnableIfIsType<T, ast::type::Type>* create(ARGS&&... args) {
|
||||
static_assert(std::is_base_of<ast::type::Type, T>::value,
|
||||
"T does not derive from ast::type::Type");
|
||||
return type_mgr_.Get<T>(std::forward<ARGS>(args)...);
|
||||
}
|
||||
|
||||
/// Moves the type `ty` to the Module, returning a pointer to the unique
|
||||
/// (de-aliased) type.
|
||||
/// When the Module is destructed, the returned `ast::Type` will also be
|
||||
/// destructed.
|
||||
/// @param ty the type to add to the module
|
||||
/// @returns the de-aliased type pointer
|
||||
template <typename T>
|
||||
EnableIfIsType<T, ast::type::Type>* unique_type(std::unique_ptr<T> ty) {
|
||||
return static_cast<T*>(type_mgr_.Get(std::move(ty)));
|
||||
}
|
||||
|
||||
/// Returns all the declared types in the module
|
||||
/// @returns the mapping from name string to type.
|
||||
const std::unordered_map<std::string, std::unique_ptr<ast::type::Type>>&
|
||||
types() {
|
||||
return type_mgr_.types();
|
||||
}
|
||||
|
||||
private:
|
||||
Module(const Module&) = delete;
|
||||
|
||||
|
||||
@@ -80,9 +80,9 @@ TEST_F(StorageTextureTypeTest, TypeName) {
|
||||
TEST_F(StorageTextureTypeTest, F32Type) {
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
ast::type::Type* s = mod.type_mgr().Get(std::make_unique<StorageTextureType>(
|
||||
ast::type::Type* s = mod.create<StorageTextureType>(
|
||||
TextureDimension::k2dArray, AccessControl::kReadOnly,
|
||||
ImageFormat::kRgba32Float));
|
||||
ImageFormat::kRgba32Float);
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
@@ -94,9 +94,9 @@ TEST_F(StorageTextureTypeTest, F32Type) {
|
||||
TEST_F(StorageTextureTypeTest, U32Type) {
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
ast::type::Type* s = mod.type_mgr().Get(std::make_unique<StorageTextureType>(
|
||||
ast::type::Type* s = mod.create<StorageTextureType>(
|
||||
TextureDimension::k2dArray, AccessControl::kReadOnly,
|
||||
ImageFormat::kRgba8Unorm));
|
||||
ImageFormat::kRgba8Unorm);
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
@@ -108,9 +108,9 @@ TEST_F(StorageTextureTypeTest, U32Type) {
|
||||
TEST_F(StorageTextureTypeTest, I32Type) {
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
ast::type::Type* s = mod.type_mgr().Get(std::make_unique<StorageTextureType>(
|
||||
ast::type::Type* s = mod.create<StorageTextureType>(
|
||||
TextureDimension::k2dArray, AccessControl::kReadOnly,
|
||||
ImageFormat::kRgba32Sint));
|
||||
ImageFormat::kRgba32Sint);
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
Reference in New Issue
Block a user