From 58716b9791972eff6c6e2be629df1effe624ff79 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 26 Nov 2001 17:01:47 +0000 Subject: [PATCH] Implement: isLosslesslyConvertableTo and new CompositeType base class git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1347 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Type.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/VMCore/Type.cpp b/lib/VMCore/Type.cpp index ad50ce08839..53844d605d3 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(V)) return false; + if (V->getType() != Type::UByteTy) return false; + unsigned Idx = cast(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(V) && "Structure index must be a constant!!"); + assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!"); + unsigned Idx = cast(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 &Params, } ArrayType::ArrayType(const Type *ElType, int NumEl) - : DerivedType("", ArrayTyID), ElementType(PATypeHandle(ElType, this)) { + : CompositeType("", ArrayTyID), ElementType(PATypeHandle(ElType, this)){ NumElements = NumEl; setDerivedTypeProperties(); } StructType::StructType(const vector &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!!");