Type.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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(void)
  8. {
  9. }
  10. bool sem::Type::equals(const Type* other) const
  11. {
  12. return this == other;
  13. }
  14. sem::Type* sem::Type::VOID = new sem::NativeType(sem::NativeType::Type::VOID);
  15. sem::Type* sem::Type::INTEGER = new sem::NativeType(sem::NativeType::Type::INTEGER);
  16. sem::Type* sem::Type::BOOLEAN = new sem::NativeType(sem::NativeType::Type::BOOLEAN);
  17. llvm::Type* sem::ClassType::getLlvmType (llvm::LLVMContext& context) const
  18. {
  19. return classType->llvmType;
  20. }
  21. bool sem::ClassType::equals(const Type* other) const
  22. {
  23. if (auto* oct = dynamic_cast<const ClassType*>(other); oct) {
  24. return this->classType == oct->classType;
  25. }
  26. else {
  27. return false;
  28. }
  29. }
  30. llvm::Type* sem::ArrayType::getLlvmType (llvm::LLVMContext& context) const
  31. {
  32. // TODO implement
  33. return nullptr;
  34. }
  35. bool sem::ArrayType::equals(const Type* other) const
  36. {
  37. if (auto* oct = dynamic_cast<const ArrayType*>(other); oct) {
  38. return this->arrayType->equals(oct->arrayType);
  39. }
  40. else {
  41. return false;
  42. }
  43. }
  44. bool sem::NativeType::isIntegerType(void) const
  45. {
  46. switch(type) {
  47. case INTEGER:
  48. case INT8:
  49. case INT16:
  50. case INT32:
  51. case INT64:
  52. case INT128:
  53. case UINT8:
  54. case UINT16:
  55. case UINT32:
  56. case UINT64:
  57. case UINT128:
  58. return true;
  59. default:
  60. return false;
  61. }
  62. }
  63. llvm::Type* sem::NativeType::getLlvmType (llvm::LLVMContext& context) const
  64. {
  65. switch (type) {
  66. case VOID:
  67. return llvm::Type::getVoidTy(context);
  68. case INTEGER:
  69. return llvm::Type::getInt32Ty(context);
  70. case BOOLEAN:
  71. return llvm::Type::getInt1Ty(context);
  72. case CHAR:
  73. return llvm::Type::getInt32Ty(context);
  74. case INT8:
  75. return llvm::Type::getInt8Ty(context);
  76. case INT16:
  77. return llvm::Type::getInt16Ty(context);
  78. case INT32:
  79. return llvm::Type::getInt32Ty(context);
  80. case INT64:
  81. return llvm::Type::getInt64Ty(context);
  82. case INT128:
  83. return llvm::Type::getInt128Ty(context);
  84. case UINT8:
  85. return llvm::Type::getInt8Ty(context);
  86. case UINT16:
  87. return llvm::Type::getInt16Ty(context);
  88. case UINT32:
  89. return llvm::Type::getInt32Ty(context);
  90. case UINT64:
  91. return llvm::Type::getInt64Ty(context);
  92. case UINT128:
  93. return llvm::Type::getInt128Ty(context);
  94. case FLOAT32:
  95. return llvm::Type::getFloatTy(context);
  96. case FLOAT64:
  97. return llvm::Type::getDoubleTy(context);
  98. case FLOAT128:
  99. return llvm::Type::getFP128Ty(context);
  100. }
  101. }
  102. bool sem::NativeType::equals(const sem::Type* other) const
  103. {
  104. if (auto* oct = dynamic_cast<const NativeType*>(other); oct) {
  105. return this->type == oct->type;
  106. }
  107. else {
  108. return false;
  109. }
  110. }
  111. llvm::Value* sem::NativeType::generateImplicitCast(llvm::Value* value)
  112. {
  113. // TODO implement
  114. }