Move type_manager to the ast dir/namespace

First step to moving this to the `ast::Module`.

Also remove a bunch of redundant includes to `type_manager.h` as this is already included in `context.h`

Bug: tint:307
Bug: tint:337
Change-Id: Ic4baffa7b76ddefa29f56f758c25b1003ef40888
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33665
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton
2020-11-23 19:30:55 +00:00
committed by Commit Bot service account
parent 4c32dd9735
commit 3e67c5dba6
22 changed files with 25 additions and 36 deletions

40
src/ast/type_manager.cc Normal file
View File

@@ -0,0 +1,40 @@
// 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.
#include "src/ast/type_manager.h"
#include <utility>
namespace tint {
namespace ast {
TypeManager::TypeManager() = default;
TypeManager::~TypeManager() = default;
void TypeManager::Reset() {
types_.clear();
}
ast::type::Type* TypeManager::Get(std::unique_ptr<ast::type::Type> type) {
auto name = type->type_name();
if (types_.find(name) == types_.end()) {
types_[name] = std::move(type);
}
return types_.find(name)->second.get();
}
} // namespace ast
} // namespace tint

65
src/ast/type_manager.h Normal file
View File

@@ -0,0 +1,65 @@
// 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_AST_TYPE_MANAGER_H_
#define SRC_AST_TYPE_MANAGER_H_
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include "src/ast/type/type.h"
namespace tint {
namespace ast {
/// The type manager holds all the pointers to the known types.
class TypeManager {
public:
TypeManager();
~TypeManager();
/// Clears all registered types.
void Reset();
/// Get the given type from the type manager
/// @param type The type to register
/// @return the pointer to the registered type
ast::type::Type* Get(std::unique_ptr<ast::type::Type> type);
/// Get the given type `T` from the type manager
/// @param args the arguments to pass to the type constructor
/// @return the pointer to the registered type
template <typename T, typename... ARGS>
T* Get(ARGS&&... args) {
auto ty = Get(std::make_unique<T>(std::forward<ARGS>(args)...));
return static_cast<T*>(ty);
}
/// Returns the type map
/// @returns the mapping from name string to type.
const std::unordered_map<std::string, std::unique_ptr<ast::type::Type>>&
types() {
return types_;
}
private:
std::unordered_map<std::string, std::unique_ptr<ast::type::Type>> types_;
};
} // namespace ast
} // namespace tint
#endif // SRC_AST_TYPE_MANAGER_H_

View File

@@ -0,0 +1,70 @@
// 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.
#include "src/ast/type_manager.h"
#include "gtest/gtest.h"
#include "src/ast/test_helper.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/u32_type.h"
namespace tint {
namespace {
using TypeManagerTest = testing::Test;
TEST_F(TypeManagerTest, GetUnregistered) {
ast::TypeManager tm;
auto* t = tm.Get(std::make_unique<ast::type::I32Type>());
ASSERT_NE(t, nullptr);
EXPECT_TRUE(t->IsI32());
}
TEST_F(TypeManagerTest, GetSameTypeReturnsSamePtr) {
ast::TypeManager tm;
auto* t = tm.Get(std::make_unique<ast::type::I32Type>());
ASSERT_NE(t, nullptr);
EXPECT_TRUE(t->IsI32());
auto* t2 = tm.Get(std::make_unique<ast::type::I32Type>());
EXPECT_EQ(t, t2);
}
TEST_F(TypeManagerTest, GetDifferentTypeReturnsDifferentPtr) {
ast::TypeManager tm;
auto* t = tm.Get(std::make_unique<ast::type::I32Type>());
ASSERT_NE(t, nullptr);
EXPECT_TRUE(t->IsI32());
auto* t2 = tm.Get(std::make_unique<ast::type::U32Type>());
ASSERT_NE(t2, nullptr);
EXPECT_NE(t, t2);
EXPECT_TRUE(t2->IsU32());
}
TEST_F(TypeManagerTest, ResetClearsPreviousData) {
ast::TypeManager tm;
auto* t = tm.Get(std::make_unique<ast::type::I32Type>());
ASSERT_NE(t, nullptr);
EXPECT_FALSE(tm.types().empty());
tm.Reset();
EXPECT_TRUE(tm.types().empty());
auto* t2 = tm.Get(std::make_unique<ast::type::I32Type>());
ASSERT_NE(t2, nullptr);
}
} // namespace
} // namespace tint