aboutsummaryrefslogtreecommitdiffstats
path: root/lib/VMCore
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2001-11-26 17:01:47 +0000
committerChris Lattner <sabre@nondot.org>2001-11-26 17:01:47 +0000
commit58716b9791972eff6c6e2be629df1effe624ff79 (patch)
tree427fb49a4e370ec501d34c3cea01d2e587b541f7 /lib/VMCore
parentc267f7bcc0e123cb98cc2fa3d9149bc1cbbef370 (diff)
downloadexternal_llvm-58716b9791972eff6c6e2be629df1effe624ff79.zip
external_llvm-58716b9791972eff6c6e2be629df1effe624ff79.tar.gz
external_llvm-58716b9791972eff6c6e2be629df1effe624ff79.tar.bz2
Implement: isLosslesslyConvertableTo and new CompositeType base class
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1347 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r--lib/VMCore/Type.cpp55
1 files changed, 53 insertions, 2 deletions
diff --git a/lib/VMCore/Type.cpp b/lib/VMCore/Type.cpp
index ad50ce0..53844d6 100644
--- a/lib/VMCore/Type.cpp
+++ b/lib/VMCore/Type.cpp
@@ -67,6 +67,57 @@ const Type *Type::getPrimitiveType(PrimitiveID IDNumber) {
}
}
+// isLosslesslyConvertableTo - Return true if this type can be converted to
+// 'Ty' without any reinterpretation of bits. For example, uint to int.
+//
+bool Type::isLosslesslyConvertableTo(const Type *Ty) const {
+ if (this == Ty) return true;
+ if ((!isPrimitiveType() && !Ty->isPointerType()) ||
+ (!isPointerType() && !Ty->isPrimitiveType())) return false;
+
+ if (getPrimitiveID() == Ty->getPrimitiveID())
+ return true; // Handles identity cast, and cast of differing pointer types
+
+ // Now we know that they are two differing primitive or pointer types
+ switch (getPrimitiveID()) {
+ case Type::UByteTyID: return Ty == Type::SByteTy;
+ case Type::SByteTyID: return Ty == Type::UByteTy;
+ case Type::UShortTyID: return Ty == Type::ShortTy;
+ case Type::ShortTyID: return Ty == Type::UShortTy;
+ case Type::UIntTyID: return Ty == Type::IntTy;
+ case Type::IntTyID: return Ty == Type::UIntTy;
+ case Type::ULongTyID:
+ case Type::LongTyID:
+ case Type::PointerTyID:
+ return Ty == Type::ULongTy || Ty == Type::LongTy ||
+ Ty->getPrimitiveID() == Type::PointerTyID;
+ default:
+ return false; // Other types have no identity values
+ }
+}
+
+
+bool StructType::indexValid(const Value *V) const {
+ if (!isa<ConstPoolVal>(V)) return false;
+ if (V->getType() != Type::UByteTy) return false;
+ unsigned Idx = cast<ConstPoolUInt>(V)->getValue();
+ return Idx < 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...
+//
+const Type *StructType::getTypeAtIndex(const Value *V) const {
+ assert(isa<ConstPoolVal>(V) && "Structure index must be a constant!!");
+ assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!");
+ unsigned Idx = cast<ConstPoolUInt>(V)->getValue();
+ assert(Idx < ETypes.size() && "Structure index out of range!");
+ assert(indexValid(V) && "Invalid structure index!"); // Duplicate check
+
+ return ETypes[Idx];
+}
+
+
//===----------------------------------------------------------------------===//
// Auxilliary classes
//===----------------------------------------------------------------------===//
@@ -147,13 +198,13 @@ MethodType::MethodType(const Type *Result, const vector<const Type*> &Params,
}
ArrayType::ArrayType(const Type *ElType, int NumEl)
- : DerivedType("", ArrayTyID), ElementType(PATypeHandle<Type>(ElType, this)) {
+ : CompositeType("", ArrayTyID), ElementType(PATypeHandle<Type>(ElType, this)){
NumElements = NumEl;
setDerivedTypeProperties();
}
StructType::StructType(const vector<const Type*> &Types)
- : DerivedType("", StructTyID) {
+ : CompositeType("", StructTyID) {
ETypes.reserve(Types.size());
for (unsigned i = 0; i < Types.size(); ++i) {
assert(Types[i] != Type::VoidTy && "Void type in method prototype!!");