Context.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "Context.h"
  2. #include "Semantic.h"
  3. #include "Scope.h"
  4. #include "Builtin.h"
  5. using qlow::sem::Context;
  6. using namespace qlow;
  7. size_t std::hash<std::reference_wrapper<qlow::sem::Type>>::operator() (const std::reference_wrapper<qlow::sem::Type>& t) const
  8. {
  9. return t.get().hash();
  10. }
  11. Context::Context(void)
  12. {
  13. nativeScope = std::make_unique<NativeScope>(sem::generateNativeScope(*this));
  14. sem::fillNativeScope(*nativeScope);
  15. }
  16. qlow::sem::TypeId Context::addType(Type&& type) {
  17. if (typesMap.find(type) != typesMap.end()) {
  18. return typesMap[type];
  19. }
  20. else {
  21. Type gogo = std::move(type);
  22. types.push_back({ std::move(gogo), nullptr });
  23. return types.size() - 1;
  24. }
  25. }
  26. qlow::sem::Type& Context::getType(TypeId tid)
  27. {
  28. if (tid <= types.size())
  29. return types[tid].first;
  30. else
  31. throw InternalError(InternalError::INVALID_TYPE);
  32. }
  33. std::optional<std::reference_wrapper<sem::Type>> Context::getMaybeType(TypeId tid)
  34. {
  35. if (tid <= types.size())
  36. return std::make_optional(std::reference_wrapper<Type>(types[tid].first));
  37. else
  38. return std::nullopt;
  39. }
  40. std::string Context::getTypeString(TypeId tid)
  41. {
  42. if (auto type = getMaybeType(tid))
  43. return type.value().get().asString();
  44. else
  45. return "";
  46. }
  47. qlow::sem::TypeId Context::getVoidTypeId(void)
  48. {
  49. return getNativeTypeId(Type::Native::VOID);
  50. }
  51. qlow::sem::TypeId Context::getNativeTypeId(Type::Native n)
  52. {
  53. return nativeScope->getType(n);
  54. }
  55. qlow::sem::NativeScope& Context::getNativeScope(void)
  56. {
  57. return *nativeScope;
  58. }
  59. // TODO rewrite, so on creating already existant type, there should be no need to create type at all
  60. sem::TypeId Context::createNativeType(std::string name, Type::Native type)
  61. {
  62. TypeId reserved = types.size();
  63. Type t = { *this, Type::Union{ Type::NativeType{ type } }, std::move(name), reserved };
  64. return addType(std::move(t));
  65. }
  66. sem::TypeId Context::createClassType(Class* classType)
  67. {
  68. TypeId reserved = types.size();
  69. Type t = Type{ *this, Type::Union{ Type::ClassType{ classType }}, reserved };
  70. return addType(std::move(t));
  71. }
  72. sem::TypeId Context::createPointerType(TypeId pointsTo)
  73. {
  74. TypeId reserved = types.size();
  75. Type t = Type{ *this, Type::Union{ Type::PointerType{ pointsTo }}, reserved };
  76. return addType(std::move(t));
  77. }
  78. sem::TypeId Context::createArrayType(TypeId pointsTo)
  79. {
  80. TypeId reserved = types.size();
  81. Type t = Type{ *this, Type::Union{ Type::ArrayType{ pointsTo }}, reserved };
  82. return addType(std::move(t));
  83. }
  84. llvm::Type* Context::getLlvmType(TypeId id, llvm::LLVMContext& llvmCtxt)
  85. {
  86. // TODO at the moment, all types are integers --> fix that
  87. if (id < types.size()) {
  88. auto& llt = types[id].second;
  89. return llt;
  90. }
  91. else {
  92. return nullptr;
  93. }
  94. }
  95. void Context::createLlvmTypes(llvm::LLVMContext& llvmCtxt)
  96. {
  97. for (auto& [type, llvmType] : types) {
  98. if (type.getKind() != Type::Kind::NATIVE) {
  99. llvmType = llvm::StructType::create(llvmCtxt);
  100. }
  101. }
  102. for (auto& [type, llvmType] : types) {
  103. if (type.getKind() == Type::Kind::NATIVE) {
  104. switch (type.getNativeKind()) {
  105. case Type::Native::BOOLEAN:
  106. llvmType = llvm::Type::getInt1Ty(llvmCtxt);
  107. break;
  108. case Type::Native::INTEGER:
  109. llvmType = llvm::Type::getInt64Ty(llvmCtxt);
  110. break;
  111. case Type::Native::VOID:
  112. llvmType = llvm::Type::getVoidTy(llvmCtxt);
  113. break;
  114. }
  115. }
  116. }
  117. for (auto& [type, llvmType] : types) {
  118. if (type.getKind() == Type::Kind::CLASS) {
  119. std::vector<llvm::Type*> structTypes;
  120. for (auto& [name, field] : type.getClass()->fields) {
  121. structTypes.push_back(types[field->type].second);
  122. field->llvmStructIndex = structTypes.size() - 1;
  123. }
  124. llvm::dyn_cast<llvm::StructType>(llvmType)->setBody(llvm::ArrayRef(structTypes));
  125. }
  126. }
  127. }