Add new CompositeType shared baseclass of ArrayType and StructType
authorChris Lattner <sabre@nondot.org>
Mon, 26 Nov 2001 16:46:45 +0000 (16:46 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 26 Nov 2001 16:46:45 +0000 (16:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1329 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/DerivedTypes.h

index 8c6b4a533b7146ffd76f65cc8c2cc063c7631c59..d98d33363254c454b6251d89f00972827f7629a9 100644 (file)
@@ -145,7 +145,40 @@ public:
 };
 
 
-class ArrayType : public DerivedType {
+// CompositeType - Common super class of ArrayType and StructType...
+//
+class CompositeType : public DerivedType {
+protected:
+  inline CompositeType(const string &Name, PrimitiveID id)
+    : DerivedType(Name, id) { }
+
+public:
+
+  // getTypeAtIndex - Given an index value into the type, return the type of the
+  // element.
+  //
+  virtual const Type *getTypeAtIndex(const Value *V) const = 0;
+  virtual bool indexValid(const Value *V) const = 0;
+
+  // 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 = 0;
+
+
+  // Methods for support type inquiry through isa, cast, and dyn_cast:
+  static inline bool classof(const CompositeType *T) { return true; }
+  static inline bool classof(const Type *T) {
+    return T->getPrimitiveID() == ArrayTyID || 
+           T->getPrimitiveID() == StructTyID;
+  }
+  static inline bool classof(const Value *V) {
+    return isa<Type>(V) && classof(cast<const Type>(V));
+  }
+};
+
+
+class ArrayType : public CompositeType {
 private:
   PATypeHandle<Type> ElementType;
   int NumElements;       // >= 0 for sized array, -1 for unbounded/unknown array
@@ -173,6 +206,21 @@ public:
   }
   virtual unsigned getNumContainedTypes() const { return 1; }
 
+  // getTypeAtIndex - Given an index value into the type, return the type of the
+  // element.  For an arraytype, there is only one subtype...
+  //
+  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.
+  // For structures, this is ubyte, for arrays, this is uint
+  //
+  virtual const Type *getIndexType() const { return Type::UIntTy; }
+
   // refineAbstractType - Called when a contained type is found to be more
   // concrete - this could potentially change us from an abstract type to a
   // concrete type.
@@ -192,7 +240,7 @@ public:
 };
 
 
-class StructType : public DerivedType {
+class StructType : public CompositeType {
 public:
   typedef vector<PATypeHandle<Type> > ElementTypes;
 
@@ -218,6 +266,17 @@ public:
   }
   virtual unsigned getNumContainedTypes() const { return ETypes.size(); }
 
+  // getTypeAtIndex - Given an index value into the type, return the type of the
+  // element.  For a structure type, this must be a constant value...
+  //
+  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::UByteTy; }
+
   // refineAbstractType - Called when a contained type is found to be more
   // concrete - this could potentially change us from an abstract type to a
   // concrete type.