main
Su Xing 3 years ago
parent edff5c2b63
commit 2ad17c9ad4

@ -4,8 +4,8 @@
#include <cstddef>
#include <iterator>
#include <map>
#include <memory>
#include <set>
#include <memory>
#include <vector>
namespace sysy {
@ -46,9 +46,11 @@ Type *Type::getFunctionType(Type *returnType,
}
PointerType *PointerType::get(Type *baseType) {
static std::map<Type *, PointerType> pointerTypes;
auto result = pointerTypes.try_emplace(baseType, baseType);
return &(result.first->second);
static std::map<Type *, std::unique_ptr<PointerType>> 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<Type *> &paramTypes) {
static std::set<FunctionType> 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<FunctionType *>(&(*result.first));
static std::set<std::unique_ptr<FunctionType>> functionTypes;
auto type = new FunctionType(returnType, paramTypes);
assert(type);
auto result = functionTypes.emplace(type);
return result.first->get();
}
void Value::replaceAllUsesWith(Value *value) {

@ -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<Type *> paramTypes;
public:
FunctionType(Type *returnType) : Type(kFunction), returnType(returnType) {}
protected:
FunctionType(Type *returnType, const std::vector<Type *> &paramTypes = {})
: Type(kFunction), returnType(returnType), paramTypes(paramTypes) {}

Loading…
Cancel
Save