return nullptr;
}
+uint32_t TypeManager::GetId(Type* type) const {
+ if (type_to_id_.count(type) != 0) return type_to_id_.at(type);
+ return 0;
+}
+
ForwardPointer* TypeManager::GetForwardPointer(uint32_t index) const {
if (index >= forward_pointers_.size()) return nullptr;
return forward_pointers_.at(index).get();
} else {
assert(type != nullptr && "type should not be nullptr at this point");
id_to_type_[id].reset(type);
+ type_to_id_[type] = id;
}
return type;
}
class TypeManager {
public:
using IdToTypeMap = std::unordered_map<uint32_t, std::unique_ptr<Type>>;
+ using TypeToIdMap = std::unordered_map<Type*, uint32_t>;
using ForwardPointerVector = std::vector<std::unique_ptr<ForwardPointer>>;
TypeManager(const spvtools::ir::Module& module) { AnalyzeTypes(module); }
// Returns the type for the given type |id|. Returns nullptr if the given |id|
// does not define a type.
Type* GetType(uint32_t id) const;
+ // Returns the id for the given |type|. Returns 0 if can not find the given
+ // |type|.
+ uint32_t GetId(Type* type) const;
// Returns the number of types hold in this manager.
size_t NumTypes() const { return id_to_type_.size(); }
void AttachIfTypeDecoration(const spvtools::ir::Instruction& inst);
IdToTypeMap id_to_type_; // Mapping from ids to their type representations.
+ TypeToIdMap type_to_id_; // Mapping from types to their defining ids.
ForwardPointerVector forward_pointers_; // All forward pointer declarations.
// All unresolved forward pointer declarations.
// Refers the contents in the above vector.
for (const auto& p : type_id_strs) {
EXPECT_EQ(p.second, manager.GetType(p.first)->str());
+ EXPECT_EQ(p.first, manager.GetId(manager.GetType(p.first)));
}
EXPECT_EQ("forward_pointer({uint32}*)", manager.GetForwardPointer(0)->str());
EXPECT_EQ("forward_pointer(10000)", manager.GetForwardPointer(1)->str());