summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/llvm/DerivedTypes.h33
-rw-r--r--lib/VMCore/Type.cpp58
2 files changed, 37 insertions, 54 deletions
diff --git a/include/llvm/DerivedTypes.h b/include/llvm/DerivedTypes.h
index 9e79339f4a..417d0418c5 100644
--- a/include/llvm/DerivedTypes.h
+++ b/include/llvm/DerivedTypes.h
@@ -205,10 +205,10 @@ 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 const Type *getTypeAtIndex(unsigned Idx) const = 0;
- virtual bool indexValid(const Value *V) const = 0;
- virtual bool indexValid(unsigned Idx) const = 0;
+ const Type *getTypeAtIndex(const Value *V) const;
+ const Type *getTypeAtIndex(unsigned Idx) const;
+ bool indexValid(const Value *V) const;
+ bool indexValid(unsigned Idx) const;
// Methods for support type inquiry through isa, cast, and dyn_cast.
static inline bool classof(const CompositeType *) { return true; }
@@ -264,14 +264,6 @@ public:
return ContainedTys[N];
}
- /// 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 const Type *getTypeAtIndex(unsigned Idx) const;
- virtual bool indexValid(const Value *V) const;
- virtual bool indexValid(unsigned Idx) const;
-
// Implement the AbstractTypeUser interface.
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
virtual void typeBecameConcrete(const DerivedType *AbsTy);
@@ -306,22 +298,7 @@ protected:
}
public:
- inline const Type *getElementType() const { return ContainedTys[0]; }
-
- virtual bool indexValid(const Value *V) const;
- virtual bool indexValid(unsigned) const {
- return true;
- }
-
- /// getTypeAtIndex - Given an index value into the type, return the type of
- /// the element. For sequential types, there is only one subtype...
- ///
- virtual const Type *getTypeAtIndex(const Value *) const {
- return ContainedTys[0];
- }
- virtual const Type *getTypeAtIndex(unsigned) const {
- return ContainedTys[0];
- }
+ const Type *getElementType() const { return ContainedTys[0]; }
// Methods for support type inquiry through isa, cast, and dyn_cast.
static inline bool classof(const SequentialType *) { return true; }
diff --git a/lib/VMCore/Type.cpp b/lib/VMCore/Type.cpp
index 6c348be18f..ebe431bdc2 100644
--- a/lib/VMCore/Type.cpp
+++ b/lib/VMCore/Type.cpp
@@ -286,29 +286,41 @@ void Type::typeBecameConcrete(const DerivedType *AbsTy) {
llvm_unreachable("DerivedType is already a concrete type!");
}
-bool StructType::indexValid(const Value *V) const {
- // Structure indexes require 32-bit integer constants.
- if (V->getType()->isIntegerTy(32))
- if (const ConstantInt *CU = dyn_cast<ConstantInt>(V))
- return indexValid(CU->getZExtValue());
- return false;
-}
-
-bool StructType::indexValid(unsigned V) const {
- return V < NumContainedTys;
+const Type *CompositeType::getTypeAtIndex(const Value *V) const {
+ if (const StructType *STy = dyn_cast<StructType>(this)) {
+ unsigned Idx = (unsigned)cast<ConstantInt>(V)->getZExtValue();
+ assert(indexValid(Idx) && "Invalid structure index!");
+ return STy->getElementType(Idx);
+ }
+
+ return cast<SequentialType>(this)->getElementType();
}
-
-// getTypeAtIndex - Given an index value into the type, return the type of the
-// element. For a structure type, this must be a constant value...
-//
-const Type *StructType::getTypeAtIndex(const Value *V) const {
- unsigned Idx = (unsigned)cast<ConstantInt>(V)->getZExtValue();
- return getTypeAtIndex(Idx);
+const Type *CompositeType::getTypeAtIndex(unsigned Idx) const {
+ if (const StructType *STy = dyn_cast<StructType>(this)) {
+ assert(indexValid(Idx) && "Invalid structure index!");
+ return STy->getElementType(Idx);
+ }
+
+ return cast<SequentialType>(this)->getElementType();
+}
+bool CompositeType::indexValid(const Value *V) const {
+ if (const StructType *STy = dyn_cast<StructType>(this)) {
+ // Structure indexes require 32-bit integer constants.
+ if (V->getType()->isIntegerTy(32))
+ if (const ConstantInt *CU = dyn_cast<ConstantInt>(V))
+ return CU->getZExtValue() < STy->getNumElements();
+ return false;
+ }
+
+ // Sequential types can be indexed by any integer.
+ return V->getType()->isIntegerTy();
}
-const Type *StructType::getTypeAtIndex(unsigned Idx) const {
- assert(indexValid(Idx) && "Invalid structure index!");
- return ContainedTys[Idx];
+bool CompositeType::indexValid(unsigned Idx) const {
+ if (const StructType *STy = dyn_cast<StructType>(this))
+ return Idx < STy->getNumElements();
+ // Sequential types can be indexed by any integer.
+ return true;
}
@@ -1201,12 +1213,6 @@ void PointerType::typeBecameConcrete(const DerivedType *AbsTy) {
pImpl->PointerTypes.TypeBecameConcrete(this, AbsTy);
}
-bool SequentialType::indexValid(const Value *V) const {
- if (V->getType()->isIntegerTy())
- return true;
- return false;
-}
-
namespace llvm {
raw_ostream &operator<<(raw_ostream &OS, const Type &T) {
T.print(OS);