Add begin() and end() for TypeManager.
diff --git a/source/opt/type_manager.h b/source/opt/type_manager.h
index fa04d80..3fe6799 100644
--- a/source/opt/type_manager.h
+++ b/source/opt/type_manager.h
@@ -32,8 +32,6 @@
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|.
@@ -55,6 +53,9 @@
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;
@@ -62,6 +63,9 @@
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);
diff --git a/test/opt/test_type_manager.cpp b/test/opt/test_type_manager.cpp
index 180342a..17eb2a4 100644
--- a/test/opt/test_type_manager.cpp
+++ b/test/opt/test_type_manager.cpp
@@ -103,7 +103,7 @@
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
@@ -145,7 +145,7 @@
}
}
-TEST(Struct, DecorationOnMember) {
+TEST(TypeManager, DecorationOnMember) {
const std::string text = R"(
OpMemberDecorate %struct1 0 Offset 0
OpMemberDecorate %struct2 0 Offset 0
@@ -195,7 +195,7 @@
}
}
-TEST(Types, DecorationEmpty) {
+TEST(TypeManager, DecorationEmpty) {
const std::string text = R"(
OpDecorate %struct1 Block
OpMemberDecorate %struct2 0 Offset 0
@@ -226,4 +226,52 @@
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