class TypeManager {
public:
using IdToTypeMap = std::unordered_map<uint32_t, std::unique_ptr<Type>>;
- using TypeToIdMap = std::unordered_map<const Type*, uint32_t>;
- using ForwardPointerVector = std::vector<std::unique_ptr<ForwardPointer>>;
// Constructs a type manager from the given |module|. All internal messages
// will be communicated to the outside via the given message |consumer|.
uint32_t GetId(const Type* type) const;
// Returns the number of types hold in this manager.
size_t NumTypes() const { return id_to_type_.size(); }
+ // Iterators for all types contained in this manager.
+ IdToTypeMap::const_iterator begin() const { return id_to_type_.cbegin(); }
+ IdToTypeMap::const_iterator end() const { return id_to_type_.cend(); }
// Returns the forward pointer type at the given |index|.
ForwardPointer* GetForwardPointer(uint32_t index) const;
size_t NumForwardPointers() const { return forward_pointers_.size(); }
private:
+ using TypeToIdMap = std::unordered_map<const Type*, uint32_t>;
+ using ForwardPointerVector = std::vector<std::unique_ptr<ForwardPointer>>;
+
// Analyzes the types and decorations on types in the given |module|.
void AnalyzeTypes(const spvtools::ir::Module& module);
EXPECT_EQ("forward_pointer(10000)", manager.GetForwardPointer(1)->str());
}
-TEST(Struct, DecorationOnStruct) {
+TEST(TypeManager, DecorationOnStruct) {
const std::string text = R"(
OpDecorate %struct1 Block
OpDecorate %struct2 Block
}
}
-TEST(Struct, DecorationOnMember) {
+TEST(TypeManager, DecorationOnMember) {
const std::string text = R"(
OpMemberDecorate %struct1 0 Offset 0
OpMemberDecorate %struct2 0 Offset 0
}
}
-TEST(Types, DecorationEmpty) {
+TEST(TypeManager, DecorationEmpty) {
const std::string text = R"(
OpDecorate %struct1 Block
OpMemberDecorate %struct2 0 Offset 0
EXPECT_TRUE(manager.GetType(5)->decoration_empty());
}
+TEST(TypeManager, BeginEndForEmptyModule) {
+ const std::string text = "";
+ std::unique_ptr<ir::Module> module =
+ BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
+ opt::analysis::TypeManager manager(nullptr, *module);
+ ASSERT_EQ(0u, manager.NumTypes());
+ ASSERT_EQ(0u, manager.NumForwardPointers());
+
+ EXPECT_EQ(manager.begin(), manager.end());
+}
+
+TEST(TypeManager, BeginEnd) {
+ const std::string text = R"(
+ %void1 = OpTypeVoid
+ %void2 = OpTypeVoid
+ %bool = OpTypeBool
+ %u32 = OpTypeInt 32 0
+ %f64 = OpTypeFloat 64
+ )";
+ std::unique_ptr<ir::Module> module =
+ BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text);
+ opt::analysis::TypeManager manager(nullptr, *module);
+ ASSERT_EQ(5u, manager.NumTypes());
+ ASSERT_EQ(0u, manager.NumForwardPointers());
+
+ EXPECT_NE(manager.begin(), manager.end());
+ for (const auto& t : manager) {
+ switch (t.first) {
+ case 1:
+ case 2:
+ EXPECT_EQ("void", t.second->str());
+ break;
+ case 3:
+ EXPECT_EQ("bool", t.second->str());
+ break;
+ case 4:
+ EXPECT_EQ("uint32", t.second->str());
+ break;
+ case 5:
+ EXPECT_EQ("float64", t.second->str());
+ break;
+ default:
+ EXPECT_TRUE(false && "unreachable");
+ break;
+ }
+ }
+}
+
} // anonymous namespace