diff --git a/src/IR.cpp b/src/IR.cpp index 2a64b5e..bee67fc 100644 --- a/src/IR.cpp +++ b/src/IR.cpp @@ -4,8 +4,8 @@ #include #include #include -#include #include +#include #include namespace sysy { @@ -46,9 +46,11 @@ Type *Type::getFunctionType(Type *returnType, } PointerType *PointerType::get(Type *baseType) { - static std::map pointerTypes; - auto result = pointerTypes.try_emplace(baseType, baseType); - return &(result.first->second); + static std::map> pointerTypes; + auto type = new PointerType(baseType); + assert(type); + auto result = pointerTypes.try_emplace(baseType, type); + return result.first->second.get(); } bool operator<(const FunctionType &lhs, const FunctionType &rhs) { @@ -62,12 +64,11 @@ bool operator<(const FunctionType &lhs, const FunctionType &rhs) { FunctionType *FunctionType::get(Type *returnType, const std::vector ¶mTypes) { - static std::set functionTypes; - auto result = functionTypes.emplace(returnType, paramTypes); - // STL set objects are immutable, sothe emplace method returns const_iterator. - // We are sure FunctionType pointers cannot be used to mutate the underlying - // FunctionType object because all FunctionType methods are const methods. - return const_cast(&(*result.first)); + static std::set> functionTypes; + auto type = new FunctionType(returnType, paramTypes); + assert(type); + auto result = functionTypes.emplace(type); + return result.first->get(); } void Value::replaceAllUsesWith(Value *value) { diff --git a/src/IR.h b/src/IR.h index beb3114..29abe4a 100644 --- a/src/IR.h +++ b/src/IR.h @@ -75,10 +75,10 @@ public: }; // class Type class PointerType : public Type { -private: +protected: Type *baseType; -public: +protected: PointerType(Type *baseType) : Type(kPointer), baseType(baseType) {} public: @@ -93,8 +93,7 @@ private: Type *returnType; std::vector paramTypes; -public: - FunctionType(Type *returnType) : Type(kFunction), returnType(returnType) {} +protected: FunctionType(Type *returnType, const std::vector ¶mTypes = {}) : Type(kFunction), returnType(returnType), paramTypes(paramTypes) {}