Type.h 4.9 KB

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