From 389e1118a7cb7601e4f888ae58fe16fc92fce18a Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 14 Dec 2001 16:20:21 +0000 Subject: [PATCH] PointerType and ArrayType now subclass Sequential type, which contains addressability logic for indexing. Unsized arrays have been removed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1454 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/DerivedTypes.h | 156 ++++++++++++++++++++---------------- 1 file changed, 86 insertions(+), 70 deletions(-) diff --git a/include/llvm/DerivedTypes.h b/include/llvm/DerivedTypes.h index 0f7e3609ba0..4e69a8c0678 100644 --- a/include/llvm/DerivedTypes.h +++ b/include/llvm/DerivedTypes.h @@ -23,7 +23,7 @@ class DerivedType : public Type { char isRefining; // Used for recursive types protected: - inline DerivedType(const string &Name, PrimitiveID id) : Type(Name, id) { + inline DerivedType(PrimitiveID id) : Type("", id) { isRefining = false; } @@ -145,12 +145,11 @@ public: }; -// CompositeType - Common super class of ArrayType and StructType... +// CompositeType - Common super class of ArrayType, StructType, and PointerType // class CompositeType : public DerivedType { protected: - inline CompositeType(const string &Name, PrimitiveID id) - : DerivedType(Name, id) { } + inline CompositeType(PrimitiveID id) : DerivedType(id) { } public: @@ -170,7 +169,8 @@ public: static inline bool classof(const CompositeType *T) { return true; } static inline bool classof(const Type *T) { return T->getPrimitiveID() == ArrayTyID || - T->getPrimitiveID() == StructTyID; + T->getPrimitiveID() == StructTyID || + T->getPrimitiveID() == PointerTyID; } static inline bool classof(const Value *V) { return isa(V) && classof(cast(V)); @@ -178,48 +178,42 @@ public: }; -class ArrayType : public CompositeType { +class StructType : public CompositeType { +public: + typedef vector > ElementTypes; + private: - PATypeHandle ElementType; - int NumElements; // >= 0 for sized array, -1 for unbounded/unknown array + ElementTypes ETypes; // Element types of struct + + StructType(const StructType &); // Do not implement + const StructType &operator=(const StructType &); // Do not implement - ArrayType(const ArrayType &); // Do not implement - const ArrayType &operator=(const ArrayType &); // Do not implement protected: // This should really be private, but it squelches a bogus warning - // from GCC to make them protected: warning: `class ArrayType' only + // from GCC to make them protected: warning: `class StructType' only // defines private constructors and has no friends - // Private ctor - Only can be created by a static member... - ArrayType(const Type *ElType, int NumEl); + StructType(const vector &Types); + public: - - inline const Type *getElementType() const { return ElementType; } - inline int getNumElements() const { return NumElements; } - - inline bool isSized() const { return NumElements >= 0; } - inline bool isUnsized() const { return NumElements == -1; } + inline const ElementTypes &getElementTypes() const { return ETypes; } virtual const Type *getContainedType(unsigned i) const { - return i == 0 ? ElementType.get() : 0; + return i < ETypes.size() ? ETypes[i].get() : 0; } - virtual unsigned getNumContainedTypes() const { return 1; } + virtual unsigned getNumContainedTypes() const { return ETypes.size(); } // getTypeAtIndex - Given an index value into the type, return the type of the - // element. For an arraytype, there is only one subtype... + // element. For a structure type, this must be a constant value... // - virtual const Type *getTypeAtIndex(const Value *V) const { - return ElementType.get(); - } - virtual bool indexValid(const Value *V) const { - return V->getType() == Type::UIntTy; // Must be an unsigned int index - } + virtual const Type *getTypeAtIndex(const Value *V) const ; + virtual bool indexValid(const Value *V) const; // getIndexType - Return the type required of indices for this composite. // For structures, this is ubyte, for arrays, this is uint // - virtual const Type *getIndexType() const { return Type::UIntTy; } + virtual const Type *getIndexType() const { return Type::UByteTy; } // refineAbstractType - Called when a contained type is found to be more // concrete - this could potentially change us from an abstract type to a @@ -227,12 +221,12 @@ public: // virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); - static ArrayType *get(const Type *ElementType, int NumElements = -1); + static StructType *get(const vector &Params); // Methods for support type inquiry through isa, cast, and dyn_cast: - static inline bool classof(const ArrayType *T) { return true; } + static inline bool classof(const StructType *T) { return true; } static inline bool classof(const Type *T) { - return T->getPrimitiveID() == ArrayTyID; + return T->getPrimitiveID() == StructTyID; } static inline bool classof(const Value *V) { return isa(V) && classof(cast(V)); @@ -240,42 +234,72 @@ public: }; -class StructType : public CompositeType { -public: - typedef vector > ElementTypes; - -private: - ElementTypes ETypes; // Element types of struct - - StructType(const StructType &); // Do not implement - const StructType &operator=(const StructType &); // Do not implement - +// SequentialType - This is the superclass of the array and pointer type +// classes. Both of these represent "arrays" in memory. The array type +// represents a specifically sized array, pointer types are unsized/unknown size +// arrays. SequentialType holds the common features of both, which stem from +// the fact that both lay their components out in memory identically. +// +class SequentialType : public CompositeType { + SequentialType(const SequentialType &); // Do not implement! + const SequentialType &operator=(const SequentialType &); // Do not implement! protected: - // This should really be private, but it squelches a bogus warning - // from GCC to make them protected: warning: `class StructType' only - // defines private constructors and has no friends + PATypeHandle ElementType; - // Private ctor - Only can be created by a static member... - StructType(const vector &Types); - + SequentialType(PrimitiveID TID, const Type *ElType) + : CompositeType(TID), ElementType(PATypeHandle(ElType, this)) { + } public: - inline const ElementTypes &getElementTypes() const { return ETypes; } + + inline const Type *getElementType() const { return ElementType; } virtual const Type *getContainedType(unsigned i) const { - return i < ETypes.size() ? ETypes[i].get() : 0; + return i == 0 ? ElementType.get() : 0; } - virtual unsigned getNumContainedTypes() const { return ETypes.size(); } + virtual unsigned getNumContainedTypes() const { return 1; } // getTypeAtIndex - Given an index value into the type, return the type of the - // element. For a structure type, this must be a constant value... + // element. For sequential types, there is only one subtype... // - virtual const Type *getTypeAtIndex(const Value *V) const ; - virtual bool indexValid(const Value *V) const; + virtual const Type *getTypeAtIndex(const Value *V) const { + return ElementType.get(); + } + virtual bool indexValid(const Value *V) const { + return V->getType() == Type::UIntTy; // Must be an unsigned int index + } - // getIndexType - Return the type required of indices for this composite. + // getIndexType() - Return the type required of indices for this composite. // For structures, this is ubyte, for arrays, this is uint // - virtual const Type *getIndexType() const { return Type::UByteTy; } + virtual const Type *getIndexType() const { return Type::UIntTy; } + + // Methods for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const SequentialType *T) { return true; } + static inline bool classof(const Type *T) { + return T->getPrimitiveID() == ArrayTyID || + T->getPrimitiveID() == PointerTyID; + } + static inline bool classof(const Value *V) { + return isa(V) && classof(cast(V)); + } +}; + + +class ArrayType : public SequentialType { + unsigned NumElements; + + ArrayType(const ArrayType &); // Do not implement + const ArrayType &operator=(const ArrayType &); // Do not implement +protected: + // This should really be private, but it squelches a bogus warning + // from GCC to make them protected: warning: `class ArrayType' only + // defines private constructors and has no friends + + + // Private ctor - Only can be created by a static member... + ArrayType(const Type *ElType, unsigned NumEl); +public: + inline unsigned getNumElements() const { return NumElements; } // refineAbstractType - Called when a contained type is found to be more // concrete - this could potentially change us from an abstract type to a @@ -283,12 +307,12 @@ public: // virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); - static StructType *get(const vector &Params); + static ArrayType *get(const Type *ElementType, unsigned NumElements); // Methods for support type inquiry through isa, cast, and dyn_cast: - static inline bool classof(const StructType *T) { return true; } + static inline bool classof(const ArrayType *T) { return true; } static inline bool classof(const Type *T) { - return T->getPrimitiveID() == StructTyID; + return T->getPrimitiveID() == ArrayTyID; } static inline bool classof(const Value *V) { return isa(V) && classof(cast(V)); @@ -296,10 +320,8 @@ public: }; -class PointerType : public DerivedType { -private: - PATypeHandle ValueType; +class PointerType : public SequentialType { PointerType(const PointerType &); // Do not implement const PointerType &operator=(const PointerType &); // Do not implement protected: @@ -311,14 +333,7 @@ protected: // Private ctor - Only can be created by a static member... PointerType(const Type *ElType); public: - - inline const Type *getElementType() const { return ValueType; } - - virtual const Type *getContainedType(unsigned i) const { - return i == 0 ? ValueType.get() : 0; - } - virtual unsigned getNumContainedTypes() const { return 1; } - + // PointerType::get - Named constructor for pointer types... static PointerType *get(const Type *ElementType); // refineAbstractType - Called when a contained type is found to be more @@ -375,6 +390,7 @@ public: // the code. Hence this bit of uglyness. // template void PATypeHandle::addUser() { + assert(Ty && "Type Handle has a null type!"); if (Ty->isAbstract()) cast(Ty)->addAbstractTypeUser(User); } -- 2.34.1