Type.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }
  18. namespace sem
  19. {
  20. struct SemanticObject;
  21. class Type;
  22. class ClassType;
  23. class ArrayType;
  24. class NativeType;
  25. }
  26. }
  27. struct qlow::sem::SemanticObject
  28. {
  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 : public SemanticObject,
  36. protected std::enable_shared_from_this<Type>
  37. {
  38. public:
  39. virtual ~Type(void);
  40. virtual bool isClassType(void) const = 0;
  41. virtual bool isNativeType(void) const = 0;
  42. virtual bool isArrayType(void) const = 0;
  43. virtual Scope& getScope(void) = 0;
  44. virtual llvm::Type* getLlvmType(llvm::LLVMContext& context) const = 0;
  45. virtual bool equals(const Type& other) const;
  46. static Type* VOID;
  47. static Type* INTEGER;
  48. static Type* BOOLEAN;
  49. };
  50. class qlow::sem::ClassType : public Type
  51. {
  52. sem::Class* classType;
  53. sem::TypeScope scope;
  54. public:
  55. inline ClassType(sem::Class* classType) :
  56. classType{ classType },
  57. scope{ shared_from_this() }
  58. {
  59. }
  60. inline bool isClassType(void) const override { return true; }
  61. inline bool isNativeType(void) const override { return false; }
  62. inline bool isArrayType(void) const override { return false; }
  63. Scope& getScope(void);
  64. virtual llvm::Type* getLlvmType(llvm::LLVMContext& context) const override;
  65. inline sem::Class* getClassType(void) { return classType; }
  66. virtual bool equals(const Type& other) const;
  67. };
  68. class qlow::sem::ArrayType : public Type
  69. {
  70. std::shared_ptr<sem::Type> arrayType;
  71. TypeScope scope;
  72. public:
  73. inline ArrayType(std::shared_ptr<sem::Type> arrayType) :
  74. arrayType{ std::move(arrayType) },
  75. scope{ shared_from_this() }
  76. {
  77. }
  78. inline bool isClassType(void) const override { return false; }
  79. inline bool isNativeType(void) const override { return false; }
  80. inline bool isArrayType(void) const override { return true; }
  81. Scope& getScope(void);
  82. virtual llvm::Type* getLlvmType(llvm::LLVMContext& context) const override;
  83. inline std::shared_ptr<sem::Type> getArrayType(void) { return arrayType; }
  84. virtual bool equals(const Type& other) const;
  85. };
  86. class qlow::sem::NativeType : public Type
  87. {
  88. NativeTypeScope scope;
  89. public:
  90. enum Type {
  91. VOID,
  92. INTEGER,
  93. BOOLEAN,
  94. CHAR,
  95. STRING,
  96. INT8, INT16, INT32, INT64, INT128,
  97. UINT8, UINT16, UINT32, UINT64, UINT128,
  98. FLOAT32, FLOAT64, FLOAT128,
  99. };
  100. Type type;
  101. inline NativeType(Type type) :
  102. type{ type },
  103. scope{ shared_from_this() }
  104. {
  105. }
  106. inline bool isClassType(void) const override { return false; }
  107. inline bool isNativeType(void) const override { return true; }
  108. inline bool isArrayType(void) const override { return false; }
  109. Scope& getScope(void);
  110. bool isIntegerType(void) const;
  111. llvm::Type* getLlvmType(llvm::LLVMContext& context) const override;
  112. virtual bool equals(const sem::Type& other) const;
  113. /// cast an llvm::Value from another native type to this one
  114. llvm::Value* generateImplicitCast(llvm::Value* value);
  115. };
  116. #endif // QLOW_SEM_TYPE_H