// 当前仅支持 void、i32 和 i32*。
#include "ir/IR.h"

namespace ir {

Type::Type(Kind k) : kind_(k) {}
Type::Type(Kind k, std::shared_ptr<Type> elem_ty, int num_elems)
    : kind_(k), elem_ty_(std::move(elem_ty)), num_elems_(num_elems) {}

const std::shared_ptr<Type>& Type::GetVoidType() {
  static const std::shared_ptr<Type> type = std::make_shared<Type>(Kind::Void);
  return type;
}

const std::shared_ptr<Type>& Type::GetInt1Type() {
  static const std::shared_ptr<Type> type = std::make_shared<Type>(Kind::Int1);
  return type;
}

const std::shared_ptr<Type>& Type::GetInt32Type() {
  static const std::shared_ptr<Type> type = std::make_shared<Type>(Kind::Int32);
  return type;
}

const std::shared_ptr<Type>& Type::GetPtrInt32Type() {
  static const std::shared_ptr<Type> type = std::make_shared<Type>(Kind::PtrInt32, GetInt32Type(), 0);
  return type;
}

const std::shared_ptr<Type>& Type::GetFloatType() {
  static std::shared_ptr<Type> ty = std::make_shared<Type>(Kind::Float);
  return ty;
}

const std::shared_ptr<Type>& Type::GetPtrFloatType() {
  static std::shared_ptr<Type> ty = std::make_shared<Type>(Kind::PtrFloat, GetFloatType(), 0);
  return ty;
}

std::shared_ptr<Type> Type::GetArrayType(std::shared_ptr<Type> elem_ty, int num_elems) {
  return std::make_shared<Type>(Kind::Array, std::move(elem_ty), num_elems);
}

std::shared_ptr<Type> Type::GetPointerType(std::shared_ptr<Type> pointed_ty) {
  return std::make_shared<Type>(Kind::Pointer, std::move(pointed_ty), 0);
}

Type::Kind Type::GetKind() const { return kind_; }

bool Type::IsVoid() const { return kind_ == Kind::Void; }

bool Type::IsInt1() const { return kind_ == Kind::Int1; }

bool Type::IsInt32() const { return kind_ == Kind::Int32; }

bool Type::IsPtrInt32() const {
  return kind_ == Kind::PtrInt32 || (kind_ == Kind::Pointer && GetPointedType() && GetPointedType()->IsInt32());
}

bool Type::IsFloat() const { return kind_ == Kind::Float; }

bool Type::IsPtrFloat() const {
  return kind_ == Kind::PtrFloat || (kind_ == Kind::Pointer && GetPointedType() && GetPointedType()->IsFloat());
}

bool Type::IsArray() const { return kind_ == Kind::Array; }

bool Type::IsPointer() const { return kind_ == Kind::Pointer || kind_ == Kind::PtrInt32 || kind_ == Kind::PtrFloat; }

}  // namespace ir
