Make type manager RAII-like w.r.t. the module to be analyzed.

This commit is contained in:
Lei Zhang 2016-08-10 12:37:19 -04:00
parent c562e231e3
commit dda9de965d
3 changed files with 8 additions and 11 deletions

View File

@ -44,7 +44,7 @@ ForwardPointer* TypeManager::GetForwardPointer(uint32_t index) const {
return forward_pointers_.at(index).get();
}
void TypeManager::AnalyzeType(const spvtools::ir::Module& module) {
void TypeManager::AnalyzeTypes(const spvtools::ir::Module& module) {
for (const auto* inst : module.GetTypes()) RecordIfTypeDefinition(*inst);
for (const auto& inst : module.annotations()) AttachIfTypeDecoration(*inst);
}

View File

@ -44,7 +44,7 @@ class TypeManager {
using IdToTypeMap = std::unordered_map<uint32_t, std::unique_ptr<Type>>;
using ForwardPointerVector = std::vector<std::unique_ptr<ForwardPointer>>;
TypeManager() = default;
TypeManager(const spvtools::ir::Module& module) { AnalyzeTypes(module); }
TypeManager(const TypeManager&) = delete;
TypeManager(TypeManager&&) = delete;
TypeManager& operator=(const TypeManager&) = delete;
@ -61,10 +61,10 @@ class TypeManager {
// Returns the number of forward pointer types hold in this manager.
size_t NumForwardPointers() const { return forward_pointers_.size(); }
// Analyzes the types and decorations on types in the given |module|.
void AnalyzeType(const spvtools::ir::Module& module);
private:
// Analyzes the types and decorations on types in the given |module|.
void AnalyzeTypes(const spvtools::ir::Module& module);
// Creates and returns a type from the given SPIR-V |inst|. Returns nullptr if
// the given instruction is not for defining a type.
Type* RecordIfTypeDefinition(const spvtools::ir::Instruction& inst);

View File

@ -100,10 +100,9 @@ TEST(TypeManager, TypeStrings) {
{28, "named_barrier"},
};
opt::analysis::TypeManager manager;
std::unique_ptr<ir::Module> module =
SpvTools(SPV_ENV_UNIVERSAL_1_1).BuildModule(text);
manager.AnalyzeType(*module);
opt::analysis::TypeManager manager(*module);
EXPECT_EQ(type_id_strs.size(), manager.NumTypes());
EXPECT_EQ(2u, manager.NumForwardPointers());
@ -130,10 +129,9 @@ TEST(Struct, DecorationOnStruct) {
%struct4 = OpTypeStruct %u32 %f32 ; the same
%struct7 = OpTypeStruct %f32 ; no decoration
)";
opt::analysis::TypeManager manager;
std::unique_ptr<ir::Module> module =
SpvTools(SPV_ENV_UNIVERSAL_1_1).BuildModule(text);
manager.AnalyzeType(*module);
opt::analysis::TypeManager manager(*module);
ASSERT_EQ(7u, manager.NumTypes());
ASSERT_EQ(0u, manager.NumForwardPointers());
@ -181,10 +179,9 @@ TEST(Struct, DecorationOnMember) {
%struct7 = OpTypeStruct %u32 %f32 ; extra decoration on the struct
%struct10 = OpTypeStruct %u32 %f32 ; no member decoration
)";
opt::analysis::TypeManager manager;
std::unique_ptr<ir::Module> module =
SpvTools(SPV_ENV_UNIVERSAL_1_1).BuildModule(text);
manager.AnalyzeType(*module);
opt::analysis::TypeManager manager(*module);
ASSERT_EQ(10u, manager.NumTypes());
ASSERT_EQ(0u, manager.NumForwardPointers());