Type.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef QLOW_SEM_TYPE_H
  2. #define QLOW_SEM_TYPE_H
  3. #include <variant>
  4. #include <memory>
  5. #include <string>
  6. #include <limits>
  7. namespace llvm {
  8. class Value;
  9. class Type;
  10. class LLVMContext;
  11. }
  12. namespace qlow::sem
  13. {
  14. struct SemanticObject;
  15. class Type;
  16. class NativeType;
  17. class ClassType;
  18. class ArrayType;
  19. // forward declarations to other files
  20. struct Class;
  21. class TypeScope;
  22. class Context;
  23. }
  24. struct qlow::sem::SemanticObject
  25. {
  26. Context& context;
  27. inline SemanticObject(Context& context) :
  28. context{ context } {}
  29. virtual ~SemanticObject(void);
  30. /**
  31. * @brief converts the object to a readable string for debugging purposes.
  32. */
  33. virtual std::string toString(void) const;
  34. };
  35. class qlow::sem::Type
  36. {
  37. friend class Context;
  38. protected:
  39. std::unique_ptr<TypeScope> typeScope;
  40. llvm::Type* llvmType;
  41. Type(void);
  42. public:
  43. virtual ~Type(void);
  44. Type(const Type& other) = delete;
  45. Type(Type&& other) = delete;
  46. void operator = (const Type& other) = delete;
  47. Type& operator = (Type&& other) = delete;
  48. virtual std::string asString(void) const = 0;
  49. virtual std::string asIdentifier(void) const = 0;
  50. virtual size_t hash(void) const = 0;
  51. bool operator == (const Type& other) const;
  52. inline bool operator != (const Type& other) const { return !this->operator==(other); }
  53. virtual bool equals(const Type& other) const = 0;
  54. /**
  55. * @brief return the class of this type if it is a class type,
  56. * <code>nullptr</code> otherwise.
  57. * @post ensures that if <code>this->getKind() == Kind::CLASS</code>,
  58. * it will not return a <code>nullptr</code>
  59. */
  60. virtual Class* getClass(void) const;
  61. /**
  62. * @brief returns the type scope of this type
  63. */
  64. inline TypeScope& getTypeScope(void) const { return *typeScope; }
  65. /**
  66. * @brief sets the type scope of this type
  67. */
  68. void setTypeScope(std::unique_ptr<TypeScope> scope);
  69. //virtual void setLlvmType(llvm::Type* type);
  70. virtual llvm::Type* getLlvmType(llvm::LLVMContext&) const;
  71. virtual void createLlvmTypeDecl(llvm::LLVMContext&) = 0;
  72. virtual bool isClassType(void) const;
  73. virtual bool isNativeType(void) const;
  74. virtual bool isArrayType(void) const;
  75. virtual bool isVoid(void) const;
  76. };
  77. class qlow::sem::NativeType : public Type
  78. {
  79. friend class Context;
  80. public:
  81. enum class NType
  82. {
  83. VOID,
  84. INTEGER,
  85. BOOLEAN,
  86. };
  87. protected:
  88. NType type;
  89. inline NativeType(NType type) :
  90. type{ type }
  91. {
  92. }
  93. public:
  94. virtual bool equals(const Type& other) const override;
  95. virtual bool isNativeType(void) const override;
  96. virtual bool isVoid(void) const override;
  97. virtual std::string asString(void) const override;
  98. virtual std::string asIdentifier(void) const override;
  99. virtual size_t hash(void) const override;
  100. virtual void createLlvmTypeDecl(llvm::LLVMContext&) override;
  101. };
  102. class qlow::sem::ClassType : public Type
  103. {
  104. friend class Context;
  105. protected:
  106. Class* type;
  107. inline ClassType(Class* type) :
  108. type{ type }
  109. {
  110. }
  111. public:
  112. virtual bool equals(const Type& other) const override;
  113. virtual bool isClassType(void) const override;
  114. virtual Class* getClass(void) const override;
  115. virtual std::string asString(void) const override;
  116. virtual std::string asIdentifier(void) const override;
  117. virtual size_t hash(void) const override;
  118. virtual void createLlvmTypeDecl(llvm::LLVMContext&) override;
  119. };
  120. class qlow::sem::ArrayType : public Type
  121. {
  122. friend class Context;
  123. protected:
  124. Type* elementType;
  125. inline ArrayType(Type* elementType) :
  126. elementType{ elementType }
  127. {
  128. }
  129. public:
  130. virtual bool equals(const Type& other) const override;
  131. virtual bool isArrayType(void) const override;
  132. virtual std::string asString(void) const override;
  133. virtual std::string asIdentifier(void) const override;
  134. virtual size_t hash(void) const override;
  135. virtual void createLlvmTypeDecl(llvm::LLVMContext&) override;
  136. };
  137. #endif // QLOW_SEM_TYPE_H