|
| 1 | +#ifndef LLVM_ABI_TYPES_H |
| 2 | +#define LLVM_ABI_TYPES_H |
| 3 | + |
| 4 | +#include <cstdint> |
| 5 | +#include <memory> |
| 6 | +#include <string> |
| 7 | + |
| 8 | +namespace llvm { |
| 9 | +namespace abi { |
| 10 | + |
| 11 | +enum class TypeKind { |
| 12 | + Void, |
| 13 | + Integer, |
| 14 | + Float, |
| 15 | + Pointer, |
| 16 | + Array, |
| 17 | + Vector, |
| 18 | + Struct, |
| 19 | + Union, |
| 20 | + Function |
| 21 | +}; |
| 22 | +class Type { |
| 23 | +protected: |
| 24 | + TypeKind Kind; |
| 25 | + uint64_t SizeInBits; |
| 26 | + uint64_t AlignInBits; |
| 27 | + bool IsExplicitlyAligned; |
| 28 | + |
| 29 | + Type(TypeKind K, uint64_t Size, uint64_t Align, bool ExplicitAlign = false) |
| 30 | + : Kind(K), SizeInBits(Size), AlignInBits(Align), |
| 31 | + IsExplicitlyAligned(ExplicitAlign) {} |
| 32 | + |
| 33 | +public: |
| 34 | + virtual ~Type() = default; |
| 35 | + |
| 36 | + TypeKind getKind() const { return Kind; } |
| 37 | + uint64_t getSizeInBits() const { return SizeInBits; } |
| 38 | + uint64_t getAlignInBits() const { return AlignInBits; } |
| 39 | + bool hasExplicitAlignment() const { return IsExplicitlyAligned; } |
| 40 | + |
| 41 | + void setExplicitAlignment(uint64_t Align) { |
| 42 | + AlignInBits = Align; |
| 43 | + IsExplicitlyAligned = true; |
| 44 | + } |
| 45 | + |
| 46 | + bool isVoid() const { return Kind == TypeKind::Void; } |
| 47 | + bool isInteger() const { return Kind == TypeKind::Integer; } |
| 48 | + bool isFloat() const { return Kind == TypeKind::Float; } |
| 49 | + bool isPointer() const { return Kind == TypeKind::Pointer; } |
| 50 | + bool isArray() const { return Kind == TypeKind::Array; } |
| 51 | + bool isVector() const { return Kind == TypeKind::Vector; } |
| 52 | + bool isStruct() const { return Kind == TypeKind::Struct; } |
| 53 | + bool isUnion() const { return Kind == TypeKind::Union; } |
| 54 | + bool isFunction() const { return Kind == TypeKind::Function; } |
| 55 | + |
| 56 | + static bool classof(const Type *) { return true; } |
| 57 | +}; |
| 58 | +class VoidType : public Type { |
| 59 | +public: |
| 60 | + VoidType() : Type(TypeKind::Void, 0, 0) {} |
| 61 | + |
| 62 | + static bool classof(const Type *T) { return T->getKind() == TypeKind::Void; } |
| 63 | +}; |
| 64 | + |
| 65 | +class IntegerType : public Type { |
| 66 | +private: |
| 67 | + bool IsSigned; |
| 68 | + bool IsAltRepresentation; |
| 69 | + std::string TypeName; |
| 70 | + |
| 71 | +public: |
| 72 | + IntegerType(uint64_t BitWidth, uint64_t Align, bool Signed, |
| 73 | + bool AltRep = false, const std::string &Name = "") |
| 74 | + : Type(TypeKind::Integer, BitWidth, Align), IsSigned(Signed), |
| 75 | + IsAltRepresentation(AltRep), TypeName(Name) {} |
| 76 | + |
| 77 | + bool isSigned() const { return IsSigned; } |
| 78 | + bool isAltRepresentation() const { return IsAltRepresentation; } |
| 79 | + const std::string &getTypeName() const { return TypeName; } |
| 80 | + |
| 81 | + static bool classof(const Type *T) { |
| 82 | + return T->getKind() == TypeKind::Integer; |
| 83 | + } |
| 84 | +}; |
| 85 | +class FloatType : public Type { |
| 86 | +private: |
| 87 | + std::string TypeName; |
| 88 | + |
| 89 | +public: |
| 90 | + FloatType(uint64_t BitWidth, uint64_t Align, const std::string &Name) |
| 91 | + : Type(TypeKind::Float, BitWidth, Align), TypeName(Name) {} |
| 92 | + |
| 93 | + const std::string &getTypeName() const { return TypeName; } |
| 94 | + |
| 95 | + static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; } |
| 96 | +}; |
| 97 | +class PointerType : public Type { |
| 98 | +private: |
| 99 | + std::unique_ptr<Type> PointeeType; |
| 100 | + bool IsConst; |
| 101 | + bool IsVolatile; |
| 102 | + |
| 103 | +public: |
| 104 | + PointerType(std::unique_ptr<Type> Pointee, uint64_t Size, uint64_t Align, |
| 105 | + bool Const = false, bool Volatile = false) |
| 106 | + : Type(TypeKind::Pointer, Size, Align), PointeeType(std::move(Pointee)), |
| 107 | + IsConst(Const), IsVolatile(Volatile) {} |
| 108 | + |
| 109 | + const Type *getPointeeType() const { return PointeeType.get(); } |
| 110 | + bool isConst() const { return IsConst; } |
| 111 | + bool isVolatile() const { return IsVolatile; } |
| 112 | + |
| 113 | + static bool classof(const Type *T) { |
| 114 | + return T->getKind() == TypeKind::Pointer; |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +} // namespace abi |
| 119 | +} // namespace llvm |
| 120 | + |
| 121 | +#endif |
0 commit comments