Type.h 3.2 KB

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