+// 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() && !isa<PointerType>(this)) ||
+ (!isa<PointerType>(Ty) && !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 || isa<PointerType>(Ty);
+ default:
+ return false; // Other types have no identity values
+ }
+}
+
+// getPrimitiveSize - Return the basic size of this type if it is a primative
+// type. These are fixed by LLVM and are not target dependant. This will
+// return zero if the type does not have a size or is not a primitive type.
+//
+unsigned Type::getPrimitiveSize() const {
+ switch (getPrimitiveID()) {
+#define HANDLE_PRIM_TYPE(TY,SIZE) case TY##TyID: return SIZE;
+#include "llvm/Type.def"
+ default: return 0;
+ }
+}
+
+
+bool StructType::indexValid(const Value *V) const {
+ if (!isa<Constant>(V)) return false;
+ if (V->getType() != Type::UByteTy) return false;
+ unsigned Idx = cast<ConstantUInt>(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<Constant>(V) && "Structure index must be a constant!!");
+ assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!");
+ unsigned Idx = cast<ConstantUInt>(V)->getValue();
+ assert(Idx < ETypes.size() && "Structure index out of range!");
+ assert(indexValid(V) && "Invalid structure index!"); // Duplicate check
+
+ return ETypes[Idx];
+}
+
+