Type.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef QLOW_SEM_TYPE_H
  2. #define QLOW_SEM_TYPE_H
  3. #include <memory>
  4. #include "Scope.h"
  5. namespace llvm {
  6. class Value;
  7. class Type;
  8. class LLVMContext;
  9. }
  10. namespace qlow
  11. {
  12. namespace sem
  13. {
  14. // forward declarations
  15. struct Class;
  16. class Scope;
  17. struct NativeMethod;
  18. }
  19. namespace sem
  20. {
  21. struct SemanticObject;
  22. class Type;
  23. class PointerType;
  24. class ClassType;
  25. class ArrayType;
  26. class NativeType;
  27. }
  28. }
  29. struct qlow::sem::SemanticObject
  30. {
  31. virtual ~SemanticObject(void);
  32. /**
  33. * \brief converts the object to a readable string for debugging purposes.
  34. */
  35. virtual std::string toString(void) const;
  36. };
  37. class qlow::sem::Type : public SemanticObject
  38. {
  39. public:
  40. virtual ~Type(void);
  41. /// \returns false by default
  42. virtual inline bool isPointerType(void) const { return false; }
  43. /// \returns false by default
  44. virtual inline bool isClassType(void) const { return false; }
  45. /// \returns false by default
  46. virtual inline bool isNativeType(void) const { return false; }
  47. /// \returns false by default
  48. virtual inline bool isArrayType(void) const { return false; }
  49. virtual std::string asString(void) const = 0;
  50. virtual Scope& getScope(void) = 0;
  51. virtual llvm::Type* getLlvmType(llvm::LLVMContext& context) const = 0;
  52. virtual bool equals(const Type& other) const;
  53. // static std::shared_ptr<Type> VOID;
  54. // static std::shared_ptr<Type> INTEGER;
  55. // static std::shared_ptr<Type> BOOLEAN;
  56. };
  57. class qlow::sem::PointerType : public Type
  58. {
  59. std::shared_ptr<Type> derefType;
  60. sem::TypeScope scope;
  61. public:
  62. inline PointerType(std::shared_ptr<Type> derefType) :
  63. derefType{ derefType },
  64. scope{ *this }
  65. {
  66. }
  67. const std::shared_ptr<Type>& getDerefType(void) const { return derefType; }
  68. inline bool isPointerType(void) const override { return true; }
  69. virtual std::string asString(void) const override;
  70. virtual Scope& getScope(void) override;
  71. virtual llvm::Type* getLlvmType(llvm::LLVMContext& context) const override;
  72. virtual bool equals(const Type& other) const override;
  73. };
  74. class qlow::sem::ClassType : public Type
  75. {
  76. sem::Class* classType;
  77. sem::TypeScope scope;
  78. public:
  79. inline ClassType(sem::Class* classType) :
  80. classType{ classType },
  81. scope{ *this }
  82. {
  83. }
  84. inline bool isClassType(void) const override { return true; }
  85. std::string asString(void) const;
  86. Scope& getScope(void);
  87. virtual llvm::Type* getLlvmType(llvm::LLVMContext& context) const override;
  88. inline sem::Class* getClassType(void) { return classType; }
  89. virtual bool equals(const Type& other) const;
  90. };
  91. class qlow::sem::ArrayType : public Type
  92. {
  93. std::shared_ptr<sem::Type> arrayType;
  94. TypeScope scope;
  95. public:
  96. inline ArrayType(std::shared_ptr<sem::Type> arrayType) :
  97. arrayType{ std::move(arrayType) },
  98. scope{ *this }
  99. {
  100. }
  101. inline bool isArrayType(void) const override { return true; }
  102. std::string asString(void) const;
  103. Scope& getScope(void);
  104. virtual llvm::Type* getLlvmType(llvm::LLVMContext& context) const override;
  105. inline std::shared_ptr<sem::Type> getArrayType(void) { return arrayType; }
  106. virtual bool equals(const Type& other) const;
  107. };
  108. class qlow::sem::NativeType : public Type
  109. {
  110. NativeTypeScope scope;
  111. public:
  112. enum Type {
  113. VOID,
  114. INTEGER,
  115. BOOLEAN,
  116. CHAR,
  117. STRING,
  118. INT8, INT16, INT32, INT64, INT128,
  119. UINT8, UINT16, UINT32, UINT64, UINT128,
  120. FLOAT32, FLOAT64, FLOAT128,
  121. };
  122. Type type;
  123. SymbolTable<NativeMethod> nativeMethods;
  124. inline NativeType(Type type) :
  125. type{ type },
  126. scope{ *this }
  127. {
  128. }
  129. inline bool isNativeType(void) const override { return true; }
  130. std::string asString(void) const;
  131. Scope& getScope(void);
  132. bool isIntegerType(void) const;
  133. llvm::Type* getLlvmType(llvm::LLVMContext& context) const override;
  134. virtual bool equals(const sem::Type& other) const;
  135. /// cast an llvm::Value from another native type to this one
  136. llvm::Value* generateImplicitCast(llvm::Value* value);
  137. };
  138. #endif // QLOW_SEM_TYPE_H