Add begin() and end() for TypeManager.

This commit is contained in:
Lei Zhang 2016-09-29 12:00:35 -04:00
parent f0a96c91f8
commit c1d5e660ee
2 changed files with 57 additions and 5 deletions

View File

@ -32,8 +32,6 @@ namespace analysis {
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 @@ class TypeManager {
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 @@ class TypeManager {
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);

View File

@ -103,7 +103,7 @@ TEST(TypeManager, TypeStrings) {
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, DecorationOnStruct) {
}
}
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(Struct, DecorationOnMember) {
}
}
TEST(Types, DecorationEmpty) {
TEST(TypeManager, DecorationEmpty) {
const std::string text = R"(
OpDecorate %struct1 Block
OpMemberDecorate %struct2 0 Offset 0
@ -226,4 +226,52 @@ TEST(Types, DecorationEmpty) {
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