Type.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "Type.h"
  2. #include "Semantic.h"
  3. #include "Builtin.h"
  4. #include <llvm/IR/DerivedTypes.h>
  5. #include <llvm/IR/Type.h>
  6. using namespace qlow;
  7. sem::Type::Type(sem::Class* classType) :
  8. kind{ Kind::CLASS }
  9. {
  10. data.classType = classType;
  11. }
  12. sem::Type::Type(Kind kind, Class* classType) :
  13. kind{ kind }
  14. {
  15. data.classType = classType;
  16. }
  17. bool sem::Type::isClassType(void) const
  18. {
  19. return kind == Kind::CLASS;
  20. }
  21. bool sem::Type::isNative(void) const
  22. {
  23. return kind != Kind::CLASS;
  24. }
  25. sem::Class* sem::Type::getClassType(void)
  26. {
  27. if (kind != Kind::CLASS)
  28. throw "internal error";
  29. return data.classType;
  30. }
  31. llvm::Type* sem::Type::getLlvmType(llvm::LLVMContext& context) const
  32. {
  33. switch (kind) {
  34. case Kind::NULL_TYPE:
  35. return llvm::Type::getVoidTy(context);
  36. case Kind::INTEGER:
  37. return llvm::Type::getInt32Ty(context);
  38. case Kind::CLASS:
  39. return data.classType->llvmType;
  40. }
  41. }
  42. bool sem::Type::operator == (const Type& other) const
  43. {
  44. if (other.kind != this->kind)
  45. return false;
  46. switch (kind) {
  47. case Kind::CLASS:
  48. return data.classType == other.data.classType;
  49. default:
  50. return true;
  51. }
  52. }
  53. bool sem::Type::operator != (const Type& other) const
  54. {
  55. return !(*this == other);
  56. }
  57. const sem::Type sem::Type::NULL_TYPE = sem::Type{ sem::Type::Kind::NULL_TYPE };
  58. const sem::Type sem::Type::INTEGER = sem::Type{ sem::Type::Kind::INTEGER, &sem::int32 };