Type.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Type(void);
  43. public:
  44. virtual ~Type(void);
  45. Type(const Type& other) = delete;
  46. Type(Type&& other) = delete;
  47. void operator = (const Type& other) = delete;
  48. Type& operator = (Type&& other) = delete;
  49. virtual std::string asString(void) const = 0;
  50. virtual std::string asIdentifier(void) const = 0;
  51. virtual size_t hash(void) const = 0;
  52. bool operator == (const Type& other) const;
  53. inline bool operator != (const Type& other) const { return !this->operator==(other); }
  54. virtual bool equals(const Type& other) const = 0;
  55. /**
  56. * @brief return the class of this type if it is a class type,
  57. * <code>nullptr</code> otherwise.
  58. * @post ensures that if <code>this->getKind() == Kind::CLASS</code>,
  59. * it will not return a <code>nullptr</code>
  60. */
  61. virtual Class* getClass(void) const;
  62. /**
  63. * @brief returns the type scope of this type
  64. */
  65. inline TypeScope& getTypeScope(void) const { return *typeScope; }
  66. /**
  67. * @brief sets the type scope of this type
  68. */
  69. void setTypeScope(std::unique_ptr<TypeScope> scope);
  70. //virtual void setLlvmType(llvm::Type* type);
  71. virtual llvm::Type* getLlvmType(llvm::LLVMContext&) const;
  72. virtual void createLlvmTypeDecl(llvm::LLVMContext&) = 0;
  73. virtual bool isClassType(void) const;
  74. virtual bool isNativeType(void) const;
  75. virtual bool isArrayType(void) const;
  76. virtual bool isVoid(void) const;
  77. };
  78. class qlow::sem::NativeType : public Type
  79. {
  80. friend class Context;
  81. public:
  82. enum class NType
  83. {
  84. VOID,
  85. INTEGER,
  86. BOOLEAN,
  87. C_CHAR,
  88. C_SHORT,
  89. C_INT,
  90. C_LONG,
  91. };
  92. static const std::vector<NType> nativeTypes;
  93. protected:
  94. NType type;
  95. inline NativeType(NType type) :
  96. type{ type }
  97. {
  98. }
  99. public:
  100. virtual bool equals(const Type& other) const override;
  101. virtual bool isNativeType(void) const override;
  102. virtual bool isVoid(void) const override;
  103. virtual std::string asString(void) const override;
  104. virtual std::string asIdentifier(void) const override;
  105. virtual size_t hash(void) const override;
  106. virtual void createLlvmTypeDecl(llvm::LLVMContext&) override;
  107. };
  108. class qlow::sem::ClassType : public Type
  109. {
  110. friend class Context;
  111. protected:
  112. Class* type;
  113. inline ClassType(Class* type) :
  114. type{ type }
  115. {
  116. }
  117. public:
  118. virtual bool equals(const Type& other) const override;
  119. virtual bool isClassType(void) const override;
  120. virtual Class* getClass(void) const override;
  121. virtual std::string asString(void) const override;
  122. virtual std::string asIdentifier(void) const override;
  123. virtual size_t hash(void) const override;
  124. virtual void createLlvmTypeDecl(llvm::LLVMContext&) override;
  125. };
  126. class qlow::sem::ArrayType : public Type
  127. {
  128. friend class Context;
  129. protected:
  130. Type* elementType;
  131. inline ArrayType(Type* elementType) :
  132. elementType{ elementType }
  133. {
  134. }
  135. public:
  136. virtual bool equals(const Type& other) const override;
  137. virtual bool isArrayType(void) const override;
  138. virtual std::string asString(void) const override;
  139. virtual std::string asIdentifier(void) const override;
  140. virtual size_t hash(void) const override;
  141. virtual void createLlvmTypeDecl(llvm::LLVMContext&) override;
  142. };
  143. #endif // QLOW_SEM_TYPE_H