Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Type.cpp - Implement the Type class ----------------------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the Type class for the VMCore library. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "llvm/DerivedTypes.h" |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 8 | #include "llvm/SymbolTable.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 9 | #include "llvm/Constants.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 10 | #include "Support/StringExtras.h" |
| 11 | #include "Support/STLExtras.h" |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 12 | #include <algorithm> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 13 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 14 | // DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are |
| 15 | // created and later destroyed, all in an effort to make sure that there is only |
| 16 | // a single cannonical version of a type. |
| 17 | // |
| 18 | //#define DEBUG_MERGE_TYPES 1 |
| 19 | |
| 20 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | // Type Class Implementation |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | static unsigned CurUID = 0; |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 26 | static std::vector<const Type *> UIDMappings; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | |
Chris Lattner | aa06d2c | 2002-04-04 19:26:02 +0000 | [diff] [blame] | 28 | void PATypeHolder::dump() const { |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 29 | std::cerr << "PATypeHolder(" << (void*)this << ")\n"; |
Chris Lattner | aa06d2c | 2002-04-04 19:26:02 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Chris Lattner | 3855f2f | 2002-04-05 22:25:26 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 33 | Type::Type(const std::string &name, PrimitiveID id) |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 34 | : Value(Type::TypeTy, Value::TypeVal) { |
| 35 | setDescription(name); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | ID = id; |
Chris Lattner | 55fd998 | 2001-10-30 16:39:16 +0000 | [diff] [blame] | 37 | Abstract = Recursive = false; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | UID = CurUID++; // Assign types UID's as they are created |
| 39 | UIDMappings.push_back(this); |
| 40 | } |
| 41 | |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 42 | void Type::setName(const std::string &Name, SymbolTable *ST) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 43 | assert(ST && "Type::setName - Must provide symbol table argument!"); |
| 44 | |
| 45 | if (Name.size()) ST->insert(Name, this); |
| 46 | } |
| 47 | |
| 48 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 49 | const Type *Type::getUniqueIDType(unsigned UID) { |
| 50 | assert(UID < UIDMappings.size() && |
| 51 | "Type::getPrimitiveType: UID out of range!"); |
| 52 | return UIDMappings[UID]; |
| 53 | } |
| 54 | |
| 55 | const Type *Type::getPrimitiveType(PrimitiveID IDNumber) { |
| 56 | switch (IDNumber) { |
| 57 | case VoidTyID : return VoidTy; |
| 58 | case BoolTyID : return BoolTy; |
| 59 | case UByteTyID : return UByteTy; |
| 60 | case SByteTyID : return SByteTy; |
| 61 | case UShortTyID: return UShortTy; |
| 62 | case ShortTyID : return ShortTy; |
| 63 | case UIntTyID : return UIntTy; |
| 64 | case IntTyID : return IntTy; |
| 65 | case ULongTyID : return ULongTy; |
| 66 | case LongTyID : return LongTy; |
| 67 | case FloatTyID : return FloatTy; |
| 68 | case DoubleTyID: return DoubleTy; |
| 69 | case TypeTyID : return TypeTy; |
| 70 | case LabelTyID : return LabelTy; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 71 | default: |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 | |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 76 | // isLosslesslyConvertibleTo - Return true if this type can be converted to |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 77 | // 'Ty' without any reinterpretation of bits. For example, uint to int. |
| 78 | // |
Misha Brukman | f117cc9 | 2003-05-20 18:45:36 +0000 | [diff] [blame] | 79 | bool Type::isLosslesslyConvertibleTo(const Type *Ty) const { |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 80 | if (this == Ty) return true; |
Chris Lattner | d44023e | 2002-05-06 16:14:39 +0000 | [diff] [blame] | 81 | if ((!isPrimitiveType() && !isa<PointerType>(this)) || |
| 82 | (!isa<PointerType>(Ty) && !Ty->isPrimitiveType())) return false; |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 83 | |
| 84 | if (getPrimitiveID() == Ty->getPrimitiveID()) |
| 85 | return true; // Handles identity cast, and cast of differing pointer types |
| 86 | |
| 87 | // Now we know that they are two differing primitive or pointer types |
| 88 | switch (getPrimitiveID()) { |
| 89 | case Type::UByteTyID: return Ty == Type::SByteTy; |
| 90 | case Type::SByteTyID: return Ty == Type::UByteTy; |
| 91 | case Type::UShortTyID: return Ty == Type::ShortTy; |
| 92 | case Type::ShortTyID: return Ty == Type::UShortTy; |
| 93 | case Type::UIntTyID: return Ty == Type::IntTy; |
| 94 | case Type::IntTyID: return Ty == Type::UIntTy; |
| 95 | case Type::ULongTyID: |
| 96 | case Type::LongTyID: |
| 97 | case Type::PointerTyID: |
Chris Lattner | c72114c | 2002-04-27 02:26:03 +0000 | [diff] [blame] | 98 | return Ty == Type::ULongTy || Ty == Type::LongTy || isa<PointerType>(Ty); |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 99 | default: |
| 100 | return false; // Other types have no identity values |
| 101 | } |
| 102 | } |
| 103 | |
Chris Lattner | d44023e | 2002-05-06 16:14:39 +0000 | [diff] [blame] | 104 | // getPrimitiveSize - Return the basic size of this type if it is a primative |
| 105 | // type. These are fixed by LLVM and are not target dependant. This will |
| 106 | // return zero if the type does not have a size or is not a primitive type. |
| 107 | // |
| 108 | unsigned Type::getPrimitiveSize() const { |
| 109 | switch (getPrimitiveID()) { |
| 110 | #define HANDLE_PRIM_TYPE(TY,SIZE) case TY##TyID: return SIZE; |
| 111 | #include "llvm/Type.def" |
| 112 | default: return 0; |
| 113 | } |
| 114 | } |
| 115 | |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 116 | |
| 117 | bool StructType::indexValid(const Value *V) const { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 118 | if (!isa<Constant>(V)) return false; |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 119 | if (V->getType() != Type::UByteTy) return false; |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 120 | unsigned Idx = cast<ConstantUInt>(V)->getValue(); |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 121 | return Idx < ETypes.size(); |
| 122 | } |
| 123 | |
| 124 | // getTypeAtIndex - Given an index value into the type, return the type of the |
| 125 | // element. For a structure type, this must be a constant value... |
| 126 | // |
| 127 | const Type *StructType::getTypeAtIndex(const Value *V) const { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 128 | assert(isa<Constant>(V) && "Structure index must be a constant!!"); |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 129 | assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!"); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 130 | unsigned Idx = cast<ConstantUInt>(V)->getValue(); |
Chris Lattner | 58716b9 | 2001-11-26 17:01:47 +0000 | [diff] [blame] | 131 | assert(Idx < ETypes.size() && "Structure index out of range!"); |
| 132 | assert(indexValid(V) && "Invalid structure index!"); // Duplicate check |
| 133 | |
| 134 | return ETypes[Idx]; |
| 135 | } |
| 136 | |
| 137 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 138 | //===----------------------------------------------------------------------===// |
| 139 | // Auxilliary classes |
| 140 | //===----------------------------------------------------------------------===// |
| 141 | // |
| 142 | // These classes are used to implement specialized behavior for each different |
| 143 | // type. |
| 144 | // |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 145 | struct SignedIntType : public Type { |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 146 | SignedIntType(const std::string &Name, PrimitiveID id) : Type(Name, id) {} |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 147 | |
| 148 | // isSigned - Return whether a numeric type is signed. |
| 149 | virtual bool isSigned() const { return 1; } |
Chris Lattner | 1a2cefc | 2001-07-21 19:15:26 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 151 | // isInteger - Equivalent to isSigned() || isUnsigned, but with only a single |
Chris Lattner | 1a2cefc | 2001-07-21 19:15:26 +0000 | [diff] [blame] | 152 | // virtual function invocation. |
| 153 | // |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 154 | virtual bool isInteger() const { return 1; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 157 | struct UnsignedIntType : public Type { |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 158 | UnsignedIntType(const std::string &N, PrimitiveID id) : Type(N, id) {} |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 159 | |
| 160 | // isUnsigned - Return whether a numeric type is signed. |
| 161 | virtual bool isUnsigned() const { return 1; } |
Chris Lattner | 1a2cefc | 2001-07-21 19:15:26 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 163 | // isInteger - Equivalent to isSigned() || isUnsigned, but with only a single |
Chris Lattner | 1a2cefc | 2001-07-21 19:15:26 +0000 | [diff] [blame] | 164 | // virtual function invocation. |
| 165 | // |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 166 | virtual bool isInteger() const { return 1; } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
Chris Lattner | 950273b | 2003-05-22 21:31:52 +0000 | [diff] [blame] | 169 | struct OtherType : public Type { |
| 170 | OtherType(const std::string &N, PrimitiveID id) : Type(N, id) {} |
| 171 | }; |
| 172 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 173 | static struct TypeType : public Type { |
| 174 | TypeType() : Type("type", TypeTyID) {} |
Chris Lattner | 950273b | 2003-05-22 21:31:52 +0000 | [diff] [blame] | 175 | } TheTypeTy; // Implement the type that is global. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 176 | |
| 177 | |
| 178 | //===----------------------------------------------------------------------===// |
| 179 | // Static 'Type' data |
| 180 | //===----------------------------------------------------------------------===// |
| 181 | |
Chris Lattner | 950273b | 2003-05-22 21:31:52 +0000 | [diff] [blame] | 182 | static OtherType TheVoidTy ("void" , Type::VoidTyID); |
| 183 | static OtherType TheBoolTy ("bool" , Type::BoolTyID); |
| 184 | static SignedIntType TheSByteTy ("sbyte" , Type::SByteTyID); |
| 185 | static UnsignedIntType TheUByteTy ("ubyte" , Type::UByteTyID); |
| 186 | static SignedIntType TheShortTy ("short" , Type::ShortTyID); |
| 187 | static UnsignedIntType TheUShortTy("ushort", Type::UShortTyID); |
| 188 | static SignedIntType TheIntTy ("int" , Type::IntTyID); |
| 189 | static UnsignedIntType TheUIntTy ("uint" , Type::UIntTyID); |
| 190 | static SignedIntType TheLongTy ("long" , Type::LongTyID); |
| 191 | static UnsignedIntType TheULongTy ("ulong" , Type::ULongTyID); |
| 192 | static OtherType TheFloatTy ("float" , Type::FloatTyID); |
| 193 | static OtherType TheDoubleTy("double", Type::DoubleTyID); |
| 194 | static OtherType TheLabelTy ("label" , Type::LabelTyID); |
| 195 | |
| 196 | Type *Type::VoidTy = &TheVoidTy; |
| 197 | Type *Type::BoolTy = &TheBoolTy; |
| 198 | Type *Type::SByteTy = &TheSByteTy; |
| 199 | Type *Type::UByteTy = &TheUByteTy; |
| 200 | Type *Type::ShortTy = &TheShortTy; |
| 201 | Type *Type::UShortTy = &TheUShortTy; |
| 202 | Type *Type::IntTy = &TheIntTy; |
| 203 | Type *Type::UIntTy = &TheUIntTy; |
| 204 | Type *Type::LongTy = &TheLongTy; |
| 205 | Type *Type::ULongTy = &TheULongTy; |
| 206 | Type *Type::FloatTy = &TheFloatTy; |
| 207 | Type *Type::DoubleTy = &TheDoubleTy; |
| 208 | Type *Type::TypeTy = &TheTypeTy; |
| 209 | Type *Type::LabelTy = &TheLabelTy; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 210 | |
| 211 | |
| 212 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 213 | // Derived Type Constructors |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 216 | FunctionType::FunctionType(const Type *Result, |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 217 | const std::vector<const Type*> &Params, |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 218 | bool IsVarArgs) : DerivedType(FunctionTyID), |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 219 | ResultType(PATypeHandle(Result, this)), |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 220 | isVarArgs(IsVarArgs) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 221 | ParamTys.reserve(Params.size()); |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 222 | for (unsigned i = 0; i < Params.size(); ++i) |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 223 | ParamTys.push_back(PATypeHandle(Params[i], this)); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 224 | |
| 225 | setDerivedTypeProperties(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 228 | StructType::StructType(const std::vector<const Type*> &Types) |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame] | 229 | : CompositeType(StructTyID) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 230 | ETypes.reserve(Types.size()); |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 231 | for (unsigned i = 0; i < Types.size(); ++i) { |
| 232 | assert(Types[i] != Type::VoidTy && "Void type in method prototype!!"); |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 233 | ETypes.push_back(PATypeHandle(Types[i], this)); |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 234 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 235 | setDerivedTypeProperties(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame] | 238 | ArrayType::ArrayType(const Type *ElType, unsigned NumEl) |
| 239 | : SequentialType(ArrayTyID, ElType) { |
| 240 | NumElements = NumEl; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 241 | setDerivedTypeProperties(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame] | 244 | PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) { |
| 245 | setDerivedTypeProperties(); |
| 246 | } |
| 247 | |
| 248 | OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 249 | setAbstract(true); |
| 250 | setDescription("opaque"+utostr(getUniqueID())); |
| 251 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 252 | std::cerr << "Derived new type: " << getDescription() << "\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 253 | #endif |
| 254 | } |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 259 | //===----------------------------------------------------------------------===// |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 260 | // Derived Type setDerivedTypeProperties Function |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 261 | //===----------------------------------------------------------------------===// |
| 262 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 263 | // getTypeProps - This is a recursive function that walks a type hierarchy |
| 264 | // calculating the description for a type and whether or not it is abstract or |
| 265 | // recursive. Worst case it will have to do a lot of traversing if you have |
| 266 | // some whacko opaque types, but in most cases, it will do some simple stuff |
| 267 | // when it hits non-abstract types that aren't recursive. |
| 268 | // |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 269 | static std::string getTypeProps(const Type *Ty, |
| 270 | std::vector<const Type *> &TypeStack, |
| 271 | bool &isAbstract, bool &isRecursive) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 272 | if (!Ty->isAbstract() && !Ty->isRecursive() && // Base case for the recursion |
| 273 | Ty->getDescription().size()) { |
Chris Lattner | 6a490ce | 2003-01-14 19:42:39 +0000 | [diff] [blame] | 274 | return Ty->getDescription(); // Primitive = leaf type |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 275 | } else if (isa<OpaqueType>(Ty)) { // Base case for the recursion |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 276 | isAbstract = true; // This whole type is abstract! |
Chris Lattner | 6a490ce | 2003-01-14 19:42:39 +0000 | [diff] [blame] | 277 | return Ty->getDescription(); // Opaque = leaf type |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 278 | } else { |
| 279 | // Check to see if the Type is already on the stack... |
| 280 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 281 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 282 | |
| 283 | // This is another base case for the recursion. In this case, we know |
| 284 | // that we have looped back to a type that we have previously visited. |
| 285 | // Generate the appropriate upreference to handle this. |
| 286 | // |
| 287 | if (Slot < CurSize) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 288 | isRecursive = true; // We know we are recursive |
Chris Lattner | 6a490ce | 2003-01-14 19:42:39 +0000 | [diff] [blame] | 289 | return "\\" + utostr(CurSize-Slot); // Here's the upreference |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 290 | } else { // Recursive case: abstract derived type... |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 291 | std::string Result; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 292 | TypeStack.push_back(Ty); // Add us to the stack.. |
| 293 | |
| 294 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 295 | case Type::FunctionTyID: { |
| 296 | const FunctionType *MTy = cast<const FunctionType>(Ty); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 297 | Result = getTypeProps(MTy->getReturnType(), TypeStack, |
| 298 | isAbstract, isRecursive)+" ("; |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 299 | for (FunctionType::ParamTypes::const_iterator |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 300 | I = MTy->getParamTypes().begin(), |
| 301 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 302 | if (I != MTy->getParamTypes().begin()) |
| 303 | Result += ", "; |
| 304 | Result += getTypeProps(*I, TypeStack, isAbstract, isRecursive); |
| 305 | } |
| 306 | if (MTy->isVarArg()) { |
| 307 | if (!MTy->getParamTypes().empty()) Result += ", "; |
| 308 | Result += "..."; |
| 309 | } |
| 310 | Result += ")"; |
| 311 | break; |
| 312 | } |
| 313 | case Type::StructTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 314 | const StructType *STy = cast<const StructType>(Ty); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 315 | Result = "{ "; |
| 316 | for (StructType::ElementTypes::const_iterator |
| 317 | I = STy->getElementTypes().begin(), |
| 318 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 319 | if (I != STy->getElementTypes().begin()) |
| 320 | Result += ", "; |
| 321 | Result += getTypeProps(*I, TypeStack, isAbstract, isRecursive); |
| 322 | } |
| 323 | Result += " }"; |
| 324 | break; |
| 325 | } |
| 326 | case Type::PointerTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 327 | const PointerType *PTy = cast<const PointerType>(Ty); |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 328 | Result = getTypeProps(PTy->getElementType(), TypeStack, |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 329 | isAbstract, isRecursive) + " *"; |
| 330 | break; |
| 331 | } |
| 332 | case Type::ArrayTyID: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 333 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame] | 334 | unsigned NumElements = ATy->getNumElements(); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 335 | Result = "["; |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame] | 336 | Result += utostr(NumElements) + " x "; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 337 | Result += getTypeProps(ATy->getElementType(), TypeStack, |
| 338 | isAbstract, isRecursive) + "]"; |
| 339 | break; |
| 340 | } |
| 341 | default: |
| 342 | assert(0 && "Unhandled case in getTypeProps!"); |
| 343 | Result = "<error>"; |
| 344 | } |
| 345 | |
| 346 | TypeStack.pop_back(); // Remove self from stack... |
Chris Lattner | 6a490ce | 2003-01-14 19:42:39 +0000 | [diff] [blame] | 347 | return Result; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
| 353 | // setDerivedTypeProperties - This function is used to calculate the |
| 354 | // isAbstract, isRecursive, and the Description settings for a type. The |
| 355 | // getTypeProps function does all the dirty work. |
| 356 | // |
| 357 | void DerivedType::setDerivedTypeProperties() { |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 358 | std::vector<const Type *> TypeStack; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 359 | bool isAbstract = false, isRecursive = false; |
| 360 | |
| 361 | setDescription(getTypeProps(this, TypeStack, isAbstract, isRecursive)); |
| 362 | setAbstract(isAbstract); |
| 363 | setRecursive(isRecursive); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | //===----------------------------------------------------------------------===// |
| 368 | // Type Structural Equality Testing |
| 369 | //===----------------------------------------------------------------------===// |
| 370 | |
| 371 | // TypesEqual - Two types are considered structurally equal if they have the |
| 372 | // same "shape": Every level and element of the types have identical primitive |
| 373 | // ID's, and the graphs have the same edges/nodes in them. Nodes do not have to |
| 374 | // be pointer equals to be equivalent though. This uses an optimistic algorithm |
| 375 | // that assumes that two graphs are the same until proven otherwise. |
| 376 | // |
| 377 | static bool TypesEqual(const Type *Ty, const Type *Ty2, |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 378 | std::map<const Type *, const Type *> &EqTypes) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 379 | if (Ty == Ty2) return true; |
| 380 | if (Ty->getPrimitiveID() != Ty2->getPrimitiveID()) return false; |
| 381 | if (Ty->isPrimitiveType()) return true; |
Chris Lattner | 008f906 | 2001-10-24 05:12:04 +0000 | [diff] [blame] | 382 | if (isa<OpaqueType>(Ty)) |
| 383 | return false; // Two nonequal opaque types are never equal |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 385 | std::map<const Type*, const Type*>::iterator It = EqTypes.find(Ty); |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 386 | if (It != EqTypes.end()) |
| 387 | return It->second == Ty2; // Looping back on a type, check for equality |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 389 | // Otherwise, add the mapping to the table to make sure we don't get |
| 390 | // recursion on the types... |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 391 | EqTypes.insert(std::make_pair(Ty, Ty2)); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 392 | |
| 393 | // Iterate over the types and make sure the the contents are equivalent... |
Chris Lattner | 74c2b76 | 2001-09-09 22:26:58 +0000 | [diff] [blame] | 394 | Type::subtype_iterator I = Ty ->subtype_begin(), IE = Ty ->subtype_end(); |
| 395 | Type::subtype_iterator I2 = Ty2->subtype_begin(), IE2 = Ty2->subtype_end(); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 396 | for (; I != IE && I2 != IE2; ++I, ++I2) |
| 397 | if (!TypesEqual(*I, *I2, EqTypes)) return false; |
| 398 | |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 399 | // Two really annoying special cases that breaks an otherwise nice simple |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 400 | // algorithm is the fact that arraytypes have sizes that differentiates types, |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 401 | // and that method types can be varargs or not. Consider this now. |
| 402 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 403 | if (ATy->getNumElements() != cast<const ArrayType>(Ty2)->getNumElements()) |
| 404 | return false; |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 405 | } else if (const FunctionType *MTy = dyn_cast<FunctionType>(Ty)) { |
| 406 | if (MTy->isVarArg() != cast<const FunctionType>(Ty2)->isVarArg()) |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 410 | return I == IE && I2 == IE2; // Types equal if both iterators are done |
| 411 | } |
| 412 | |
| 413 | static bool TypesEqual(const Type *Ty, const Type *Ty2) { |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 414 | std::map<const Type *, const Type *> EqTypes; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 415 | return TypesEqual(Ty, Ty2, EqTypes); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | |
| 420 | //===----------------------------------------------------------------------===// |
| 421 | // Derived Type Factory Functions |
| 422 | //===----------------------------------------------------------------------===// |
| 423 | |
| 424 | // TypeMap - Make sure that only one instance of a particular type may be |
| 425 | // created on any given run of the compiler... note that this involves updating |
| 426 | // our map if an abstract type gets refined somehow... |
| 427 | // |
| 428 | template<class ValType, class TypeClass> |
| 429 | class TypeMap : public AbstractTypeUser { |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 430 | typedef std::map<ValType, PATypeHandle> MapTy; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 431 | MapTy Map; |
| 432 | public: |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 433 | ~TypeMap() { print("ON EXIT"); } |
| 434 | |
| 435 | inline TypeClass *get(const ValType &V) { |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 436 | typename std::map<ValType, PATypeHandle>::iterator I |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 437 | = Map.find(V); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 438 | // TODO: FIXME: When Types are not CONST. |
| 439 | return (I != Map.end()) ? (TypeClass*)I->second.get() : 0; |
| 440 | } |
| 441 | |
| 442 | inline void add(const ValType &V, TypeClass *T) { |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 443 | Map.insert(std::make_pair(V, PATypeHandle(T, this))); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 444 | print("add"); |
| 445 | } |
| 446 | |
| 447 | // containsEquivalent - Return true if the typemap contains a type that is |
| 448 | // structurally equivalent to the specified type. |
| 449 | // |
| 450 | inline const TypeClass *containsEquivalent(const TypeClass *Ty) { |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 451 | for (typename MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I) |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 452 | if (I->second.get() != Ty && TypesEqual(Ty, I->second.get())) |
| 453 | return (TypeClass*)I->second.get(); // FIXME TODO when types not const |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | // refineAbstractType - This is called when one of the contained abstract |
| 458 | // types gets refined... this simply removes the abstract type from our table. |
| 459 | // We expect that whoever refined the type will add it back to the table, |
| 460 | // corrected. |
| 461 | // |
| 462 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 463 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 464 | std::cerr << "Removing Old type from Tab: " << (void*)OldTy << ", " |
| 465 | << OldTy->getDescription() << " replacement == " << (void*)NewTy |
| 466 | << ", " << NewTy->getDescription() << "\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 467 | #endif |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 468 | for (typename MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I) |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 469 | if (I->second == OldTy) { |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 470 | // Check to see if the type just became concrete. If so, remove self |
| 471 | // from user list. |
| 472 | I->second.removeUserFromConcrete(); |
| 473 | I->second = cast<TypeClass>(NewTy); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 474 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void remove(const ValType &OldVal) { |
Chris Lattner | fe8041a | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 478 | typename MapTy::iterator I = Map.find(OldVal); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 479 | assert(I != Map.end() && "TypeMap::remove, element not found!"); |
| 480 | Map.erase(I); |
| 481 | } |
| 482 | |
Chris Lattner | aa06d2c | 2002-04-04 19:26:02 +0000 | [diff] [blame] | 483 | void print(const char *Arg) const { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 484 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 485 | std::cerr << "TypeMap<>::" << Arg << " table contents:\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 486 | unsigned i = 0; |
Chris Lattner | aa06d2c | 2002-04-04 19:26:02 +0000 | [diff] [blame] | 487 | for (MapTy::const_iterator I = Map.begin(), E = Map.end(); I != E; ++I) |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 488 | std::cerr << " " << (++i) << ". " << I->second << " " |
| 489 | << I->second->getDescription() << "\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 490 | #endif |
| 491 | } |
Chris Lattner | aa06d2c | 2002-04-04 19:26:02 +0000 | [diff] [blame] | 492 | |
| 493 | void dump() const { print("dump output"); } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | |
| 497 | // ValTypeBase - This is the base class that is used by the various |
| 498 | // instantiations of TypeMap. This class is an AbstractType user that notifies |
| 499 | // the underlying TypeMap when it gets modified. |
| 500 | // |
| 501 | template<class ValType, class TypeClass> |
| 502 | class ValTypeBase : public AbstractTypeUser { |
| 503 | TypeMap<ValType, TypeClass> &MyTable; |
| 504 | protected: |
| 505 | inline ValTypeBase(TypeMap<ValType, TypeClass> &tab) : MyTable(tab) {} |
| 506 | |
| 507 | // Subclass should override this... to update self as usual |
| 508 | virtual void doRefinement(const DerivedType *OldTy, const Type *NewTy) = 0; |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 509 | |
| 510 | // typeBecameConcrete - This callback occurs when a contained type refines |
| 511 | // to itself, but becomes concrete in the process. Our subclass should remove |
| 512 | // itself from the ATU list of the specified type. |
| 513 | // |
| 514 | virtual void typeBecameConcrete(const DerivedType *Ty) = 0; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 515 | |
| 516 | virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) { |
Chris Lattner | 3855f2f | 2002-04-05 22:25:26 +0000 | [diff] [blame] | 517 | assert(OldTy == NewTy || OldTy->isAbstract()); |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 518 | |
| 519 | if (!OldTy->isAbstract()) |
| 520 | typeBecameConcrete(OldTy); |
| 521 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 522 | TypeMap<ValType, TypeClass> &Table = MyTable; // Copy MyTable reference |
| 523 | ValType Tmp(*(ValType*)this); // Copy this. |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 524 | PATypeHandle OldType(Table.get(*(ValType*)this), this); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 525 | Table.remove(*(ValType*)this); // Destroy's this! |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 527 | // Refine temporary to new state... |
| 528 | if (OldTy != NewTy) |
| 529 | Tmp.doRefinement(OldTy, NewTy); |
| 530 | |
| 531 | // FIXME: when types are not const! |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 532 | Table.add((ValType&)Tmp, (TypeClass*)OldType.get()); |
| 533 | } |
Chris Lattner | aa06d2c | 2002-04-04 19:26:02 +0000 | [diff] [blame] | 534 | |
| 535 | void dump() const { |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 536 | std::cerr << "ValTypeBase instance!\n"; |
Chris Lattner | aa06d2c | 2002-04-04 19:26:02 +0000 | [diff] [blame] | 537 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | |
| 541 | |
| 542 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 543 | // Function Type Factory and Value Class... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 544 | // |
| 545 | |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 546 | // FunctionValType - Define a class to hold the key that goes into the TypeMap |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 547 | // |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 548 | class FunctionValType : public ValTypeBase<FunctionValType, FunctionType> { |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 549 | PATypeHandle RetTy; |
| 550 | std::vector<PATypeHandle> ArgTypes; |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 551 | bool isVarArg; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 552 | public: |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 553 | FunctionValType(const Type *ret, const std::vector<const Type*> &args, |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 554 | bool IVA, TypeMap<FunctionValType, FunctionType> &Tab) |
| 555 | : ValTypeBase<FunctionValType, FunctionType>(Tab), RetTy(ret, this), |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 556 | isVarArg(IVA) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 557 | for (unsigned i = 0; i < args.size(); ++i) |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 558 | ArgTypes.push_back(PATypeHandle(args[i], this)); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // We *MUST* have an explicit copy ctor so that the TypeHandles think that |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 562 | // this FunctionValType owns them, not the old one! |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 563 | // |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 564 | FunctionValType(const FunctionValType &MVT) |
| 565 | : ValTypeBase<FunctionValType, FunctionType>(MVT), RetTy(MVT.RetTy, this), |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 566 | isVarArg(MVT.isVarArg) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 567 | ArgTypes.reserve(MVT.ArgTypes.size()); |
| 568 | for (unsigned i = 0; i < MVT.ArgTypes.size(); ++i) |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 569 | ArgTypes.push_back(PATypeHandle(MVT.ArgTypes[i], this)); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // Subclass should override this... to update self as usual |
| 573 | virtual void doRefinement(const DerivedType *OldType, const Type *NewType) { |
| 574 | if (RetTy == OldType) RetTy = NewType; |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 575 | for (unsigned i = 0, e = ArgTypes.size(); i != e; ++i) |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 576 | if (ArgTypes[i] == OldType) ArgTypes[i] = NewType; |
| 577 | } |
| 578 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 579 | virtual void typeBecameConcrete(const DerivedType *Ty) { |
| 580 | if (RetTy == Ty) RetTy.removeUserFromConcrete(); |
| 581 | |
| 582 | for (unsigned i = 0; i < ArgTypes.size(); ++i) |
| 583 | if (ArgTypes[i] == Ty) ArgTypes[i].removeUserFromConcrete(); |
| 584 | } |
| 585 | |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 586 | inline bool operator<(const FunctionValType &MTV) const { |
Chris Lattner | 56c5acb | 2001-10-13 07:01:33 +0000 | [diff] [blame] | 587 | if (RetTy.get() < MTV.RetTy.get()) return true; |
| 588 | if (RetTy.get() > MTV.RetTy.get()) return false; |
| 589 | |
| 590 | if (ArgTypes < MTV.ArgTypes) return true; |
| 591 | return (ArgTypes == MTV.ArgTypes) && isVarArg < MTV.isVarArg; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 592 | } |
| 593 | }; |
| 594 | |
| 595 | // Define the actual map itself now... |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 596 | static TypeMap<FunctionValType, FunctionType> FunctionTypes; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 598 | // FunctionType::get - The factory function for the FunctionType class... |
| 599 | FunctionType *FunctionType::get(const Type *ReturnType, |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 600 | const std::vector<const Type*> &Params, |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 601 | bool isVarArg) { |
| 602 | FunctionValType VT(ReturnType, Params, isVarArg, FunctionTypes); |
| 603 | FunctionType *MT = FunctionTypes.get(VT); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 604 | if (MT) return MT; |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 606 | FunctionTypes.add(VT, MT = new FunctionType(ReturnType, Params, isVarArg)); |
Chris Lattner | e5a57ee | 2001-07-25 22:47:55 +0000 | [diff] [blame] | 607 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 608 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 609 | std::cerr << "Derived new type: " << MT << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 610 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 611 | return MT; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 614 | //===----------------------------------------------------------------------===// |
| 615 | // Array Type Factory... |
| 616 | // |
| 617 | class ArrayValType : public ValTypeBase<ArrayValType, ArrayType> { |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 618 | PATypeHandle ValTy; |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame] | 619 | unsigned Size; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 620 | public: |
| 621 | ArrayValType(const Type *val, int sz, TypeMap<ArrayValType, ArrayType> &Tab) |
| 622 | : ValTypeBase<ArrayValType, ArrayType>(Tab), ValTy(val, this), Size(sz) {} |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 623 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 624 | // We *MUST* have an explicit copy ctor so that the ValTy thinks that this |
| 625 | // ArrayValType owns it, not the old one! |
| 626 | // |
| 627 | ArrayValType(const ArrayValType &AVT) |
| 628 | : ValTypeBase<ArrayValType, ArrayType>(AVT), ValTy(AVT.ValTy, this), |
| 629 | Size(AVT.Size) {} |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 630 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 631 | // Subclass should override this... to update self as usual |
| 632 | virtual void doRefinement(const DerivedType *OldType, const Type *NewType) { |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 633 | assert(ValTy == OldType); |
| 634 | ValTy = NewType; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 637 | virtual void typeBecameConcrete(const DerivedType *Ty) { |
| 638 | assert(ValTy == Ty && |
| 639 | "Contained type became concrete but we're not using it!"); |
| 640 | ValTy.removeUserFromConcrete(); |
| 641 | } |
| 642 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 643 | inline bool operator<(const ArrayValType &MTV) const { |
| 644 | if (Size < MTV.Size) return true; |
| 645 | return Size == MTV.Size && ValTy.get() < MTV.ValTy.get(); |
| 646 | } |
| 647 | }; |
| 648 | |
| 649 | static TypeMap<ArrayValType, ArrayType> ArrayTypes; |
| 650 | |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame] | 651 | ArrayType *ArrayType::get(const Type *ElementType, unsigned NumElements) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 652 | assert(ElementType && "Can't get array of null types!"); |
| 653 | |
| 654 | ArrayValType AVT(ElementType, NumElements, ArrayTypes); |
| 655 | ArrayType *AT = ArrayTypes.get(AVT); |
| 656 | if (AT) return AT; // Found a match, return it! |
| 657 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 658 | // Value not found. Derive a new type! |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 659 | ArrayTypes.add(AVT, AT = new ArrayType(ElementType, NumElements)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 660 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 661 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 662 | std::cerr << "Derived new type: " << AT->getDescription() << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 663 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 664 | return AT; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 667 | //===----------------------------------------------------------------------===// |
| 668 | // Struct Type Factory... |
| 669 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 670 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 671 | // StructValType - Define a class to hold the key that goes into the TypeMap |
| 672 | // |
| 673 | class StructValType : public ValTypeBase<StructValType, StructType> { |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 674 | std::vector<PATypeHandle> ElTypes; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 675 | public: |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 676 | StructValType(const std::vector<const Type*> &args, |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 677 | TypeMap<StructValType, StructType> &Tab) |
| 678 | : ValTypeBase<StructValType, StructType>(Tab) { |
Chris Lattner | 3855f2f | 2002-04-05 22:25:26 +0000 | [diff] [blame] | 679 | ElTypes.reserve(args.size()); |
| 680 | for (unsigned i = 0, e = args.size(); i != e; ++i) |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 681 | ElTypes.push_back(PATypeHandle(args[i], this)); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 682 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 683 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 684 | // We *MUST* have an explicit copy ctor so that the TypeHandles think that |
| 685 | // this StructValType owns them, not the old one! |
| 686 | // |
| 687 | StructValType(const StructValType &SVT) |
| 688 | : ValTypeBase<StructValType, StructType>(SVT){ |
| 689 | ElTypes.reserve(SVT.ElTypes.size()); |
Chris Lattner | 3855f2f | 2002-04-05 22:25:26 +0000 | [diff] [blame] | 690 | for (unsigned i = 0, e = SVT.ElTypes.size(); i != e; ++i) |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 691 | ElTypes.push_back(PATypeHandle(SVT.ElTypes[i], this)); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // Subclass should override this... to update self as usual |
| 695 | virtual void doRefinement(const DerivedType *OldType, const Type *NewType) { |
| 696 | for (unsigned i = 0; i < ElTypes.size(); ++i) |
| 697 | if (ElTypes[i] == OldType) ElTypes[i] = NewType; |
| 698 | } |
| 699 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 700 | virtual void typeBecameConcrete(const DerivedType *Ty) { |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 701 | for (unsigned i = 0, e = ElTypes.size(); i != e; ++i) |
| 702 | if (ElTypes[i] == Ty) |
| 703 | ElTypes[i].removeUserFromConcrete(); |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 706 | inline bool operator<(const StructValType &STV) const { |
| 707 | return ElTypes < STV.ElTypes; |
| 708 | } |
| 709 | }; |
| 710 | |
| 711 | static TypeMap<StructValType, StructType> StructTypes; |
| 712 | |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 713 | StructType *StructType::get(const std::vector<const Type*> &ETypes) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 714 | StructValType STV(ETypes, StructTypes); |
| 715 | StructType *ST = StructTypes.get(STV); |
| 716 | if (ST) return ST; |
| 717 | |
| 718 | // Value not found. Derive a new type! |
| 719 | StructTypes.add(STV, ST = new StructType(ETypes)); |
| 720 | |
| 721 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 722 | std::cerr << "Derived new type: " << ST->getDescription() << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 723 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 724 | return ST; |
| 725 | } |
| 726 | |
| 727 | //===----------------------------------------------------------------------===// |
| 728 | // Pointer Type Factory... |
| 729 | // |
| 730 | |
| 731 | // PointerValType - Define a class to hold the key that goes into the TypeMap |
| 732 | // |
| 733 | class PointerValType : public ValTypeBase<PointerValType, PointerType> { |
Chris Lattner | 893f025 | 2003-06-18 19:22:36 +0000 | [diff] [blame^] | 734 | PATypeHandle ValTy; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 735 | public: |
| 736 | PointerValType(const Type *val, TypeMap<PointerValType, PointerType> &Tab) |
| 737 | : ValTypeBase<PointerValType, PointerType>(Tab), ValTy(val, this) {} |
| 738 | |
| 739 | // We *MUST* have an explicit copy ctor so that the ValTy thinks that this |
| 740 | // PointerValType owns it, not the old one! |
| 741 | // |
| 742 | PointerValType(const PointerValType &PVT) |
| 743 | : ValTypeBase<PointerValType, PointerType>(PVT), ValTy(PVT.ValTy, this) {} |
| 744 | |
| 745 | // Subclass should override this... to update self as usual |
| 746 | virtual void doRefinement(const DerivedType *OldType, const Type *NewType) { |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 747 | assert(ValTy == OldType); |
| 748 | ValTy = NewType; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 751 | virtual void typeBecameConcrete(const DerivedType *Ty) { |
| 752 | assert(ValTy == Ty && |
| 753 | "Contained type became concrete but we're not using it!"); |
| 754 | ValTy.removeUserFromConcrete(); |
| 755 | } |
| 756 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 757 | inline bool operator<(const PointerValType &MTV) const { |
| 758 | return ValTy.get() < MTV.ValTy.get(); |
| 759 | } |
| 760 | }; |
| 761 | |
| 762 | static TypeMap<PointerValType, PointerType> PointerTypes; |
| 763 | |
| 764 | PointerType *PointerType::get(const Type *ValueType) { |
| 765 | assert(ValueType && "Can't get a pointer to <null> type!"); |
| 766 | PointerValType PVT(ValueType, PointerTypes); |
| 767 | |
| 768 | PointerType *PT = PointerTypes.get(PVT); |
| 769 | if (PT) return PT; |
| 770 | |
| 771 | // Value not found. Derive a new type! |
| 772 | PointerTypes.add(PVT, PT = new PointerType(ValueType)); |
| 773 | |
| 774 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 775 | std::cerr << "Derived new type: " << PT->getDescription() << "\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 776 | #endif |
| 777 | return PT; |
| 778 | } |
| 779 | |
Chris Lattner | 339ba45 | 2002-04-06 00:21:11 +0000 | [diff] [blame] | 780 | void debug_type_tables() { |
| 781 | FunctionTypes.dump(); |
| 782 | ArrayTypes.dump(); |
| 783 | StructTypes.dump(); |
| 784 | PointerTypes.dump(); |
| 785 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 786 | |
| 787 | |
| 788 | //===----------------------------------------------------------------------===// |
| 789 | // Derived Type Refinement Functions |
| 790 | //===----------------------------------------------------------------------===// |
| 791 | |
Chris Lattner | 3855f2f | 2002-04-05 22:25:26 +0000 | [diff] [blame] | 792 | // addAbstractTypeUser - Notify an abstract type that there is a new user of |
| 793 | // it. This function is called primarily by the PATypeHandle class. |
| 794 | // |
| 795 | void DerivedType::addAbstractTypeUser(AbstractTypeUser *U) const { |
| 796 | assert(isAbstract() && "addAbstractTypeUser: Current type not abstract!"); |
Chris Lattner | 3855f2f | 2002-04-05 22:25:26 +0000 | [diff] [blame] | 797 | |
| 798 | #if DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 799 | std::cerr << " addAbstractTypeUser[" << (void*)this << ", " |
| 800 | << getDescription() << "][" << AbstractTypeUsers.size() |
| 801 | << "] User = " << U << "\n"; |
Chris Lattner | 3855f2f | 2002-04-05 22:25:26 +0000 | [diff] [blame] | 802 | #endif |
| 803 | AbstractTypeUsers.push_back(U); |
| 804 | } |
| 805 | |
| 806 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 807 | // removeAbstractTypeUser - Notify an abstract type that a user of the class |
| 808 | // no longer has a handle to the type. This function is called primarily by |
| 809 | // the PATypeHandle class. When there are no users of the abstract type, it |
| 810 | // is anihilated, because there is no way to get a reference to it ever again. |
| 811 | // |
| 812 | void DerivedType::removeAbstractTypeUser(AbstractTypeUser *U) const { |
| 813 | // Search from back to front because we will notify users from back to |
| 814 | // front. Also, it is likely that there will be a stack like behavior to |
| 815 | // users that register and unregister users. |
| 816 | // |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 817 | unsigned i; |
| 818 | for (i = AbstractTypeUsers.size(); AbstractTypeUsers[i-1] != U; --i) |
| 819 | assert(i != 0 && "AbstractTypeUser not in user list!"); |
| 820 | |
| 821 | --i; // Convert to be in range 0 <= i < size() |
| 822 | assert(i < AbstractTypeUsers.size() && "Index out of range!"); // Wraparound? |
| 823 | |
| 824 | AbstractTypeUsers.erase(AbstractTypeUsers.begin()+i); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 825 | |
| 826 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 827 | std::cerr << " remAbstractTypeUser[" << (void*)this << ", " |
| 828 | << getDescription() << "][" << i << "] User = " << U << "\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 829 | #endif |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 830 | |
| 831 | if (AbstractTypeUsers.empty() && isAbstract()) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 832 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 833 | std::cerr << "DELETEing unused abstract type: <" << getDescription() |
| 834 | << ">[" << (void*)this << "]" << "\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 835 | #endif |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 836 | delete this; // No users of this abstract type! |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 837 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | |
| 841 | // refineAbstractTypeTo - This function is used to when it is discovered that |
| 842 | // the 'this' abstract type is actually equivalent to the NewType specified. |
| 843 | // This causes all users of 'this' to switch to reference the more concrete |
| 844 | // type NewType and for 'this' to be deleted. |
| 845 | // |
| 846 | void DerivedType::refineAbstractTypeTo(const Type *NewType) { |
| 847 | assert(isAbstract() && "refineAbstractTypeTo: Current type is not abstract!"); |
| 848 | assert(this != NewType && "Can't refine to myself!"); |
| 849 | |
| 850 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 851 | std::cerr << "REFINING abstract type [" << (void*)this << " " |
| 852 | << getDescription() << "] to [" << (void*)NewType << " " |
| 853 | << NewType->getDescription() << "]!\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 854 | #endif |
| 855 | |
| 856 | |
| 857 | // Make sure to put the type to be refined to into a holder so that if IT gets |
| 858 | // refined, that we will not continue using a dead reference... |
| 859 | // |
Chris Lattner | aa06d2c | 2002-04-04 19:26:02 +0000 | [diff] [blame] | 860 | PATypeHolder NewTy(NewType); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 861 | |
| 862 | // Add a self use of the current type so that we don't delete ourself until |
| 863 | // after this while loop. We are careful to never invoke refine on ourself, |
| 864 | // so this extra reference shouldn't be a problem. Note that we must only |
| 865 | // remove a single reference at the end, but we must tolerate multiple self |
| 866 | // references because we could be refineAbstractTypeTo'ing recursively on the |
| 867 | // same type. |
| 868 | // |
| 869 | addAbstractTypeUser(this); |
| 870 | |
| 871 | // Count the number of self uses. Stop looping when sizeof(list) == NSU. |
| 872 | unsigned NumSelfUses = 0; |
| 873 | |
| 874 | // Iterate over all of the uses of this type, invoking callback. Each user |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 875 | // should remove itself from our use list automatically. We have to check to |
| 876 | // make sure that NewTy doesn't _become_ 'this'. If it does, resolving types |
| 877 | // will not cause users to drop off of the use list. If we resolve to ourself |
| 878 | // we succeed! |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 879 | // |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 880 | while (AbstractTypeUsers.size() > NumSelfUses && NewTy != this) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 881 | AbstractTypeUser *User = AbstractTypeUsers.back(); |
| 882 | |
| 883 | if (User == this) { |
| 884 | // Move self use to the start of the list. Increment NSU. |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 885 | std::swap(AbstractTypeUsers.back(), AbstractTypeUsers[NumSelfUses++]); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 886 | } else { |
| 887 | unsigned OldSize = AbstractTypeUsers.size(); |
| 888 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 889 | std::cerr << " REFINING user " << OldSize-1 << "[" << (void*)User |
| 890 | << "] of abstract type [" << (void*)this << " " |
| 891 | << getDescription() << "] to [" << (void*)NewTy.get() << " " |
| 892 | << NewTy->getDescription() << "]!\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 893 | #endif |
Chris Lattner | 008f906 | 2001-10-24 05:12:04 +0000 | [diff] [blame] | 894 | User->refineAbstractType(this, NewTy); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 895 | |
Chris Lattner | cecb520 | 2002-04-05 19:53:06 +0000 | [diff] [blame] | 896 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 897 | if (AbstractTypeUsers.size() == OldSize) { |
Chris Lattner | cecb520 | 2002-04-05 19:53:06 +0000 | [diff] [blame] | 898 | User->refineAbstractType(this, NewTy); |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 899 | if (AbstractTypeUsers.back() != User) |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 900 | std::cerr << "User changed!\n"; |
| 901 | std::cerr << "Top of user list is:\n"; |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 902 | AbstractTypeUsers.back()->dump(); |
| 903 | |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 904 | std::cerr <<"\nOld User=\n"; |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 905 | User->dump(); |
| 906 | } |
Chris Lattner | cecb520 | 2002-04-05 19:53:06 +0000 | [diff] [blame] | 907 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 908 | assert(AbstractTypeUsers.size() != OldSize && |
| 909 | "AbsTyUser did not remove self from user list!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 913 | // Remove a single self use, even though there may be several here. This will |
| 914 | // probably 'delete this', so no instance variables may be used after this |
| 915 | // occurs... |
Chris Lattner | 3f59b7e | 2002-04-05 19:44:07 +0000 | [diff] [blame] | 916 | // |
| 917 | assert((NewTy == this || AbstractTypeUsers.back() == this) && |
| 918 | "Only self uses should be left!"); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 919 | removeAbstractTypeUser(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 922 | // typeIsRefined - Notify AbstractTypeUsers of this type that the current type |
| 923 | // has been refined a bit. The pointer is still valid and still should be |
| 924 | // used, but the subtypes have changed. |
| 925 | // |
| 926 | void DerivedType::typeIsRefined() { |
| 927 | assert(isRefining >= 0 && isRefining <= 2 && "isRefining out of bounds!"); |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 928 | if (isRefining == 1) return; // Kill recursion here... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 929 | ++isRefining; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 930 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 931 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 932 | std::cerr << "typeIsREFINED type: " << (void*)this <<" "<<getDescription() |
| 933 | << "\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 934 | #endif |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 935 | |
| 936 | // In this loop we have to be very careful not to get into infinite loops and |
| 937 | // other problem cases. Specifically, we loop through all of the abstract |
| 938 | // type users in the user list, notifying them that the type has been refined. |
| 939 | // At their choice, they may or may not choose to remove themselves from the |
| 940 | // list of users. Regardless of whether they do or not, we have to be sure |
| 941 | // that we only notify each user exactly once. Because the refineAbstractType |
| 942 | // method can cause an arbitrary permutation to the user list, we cannot loop |
| 943 | // through it in any particular order and be guaranteed that we will be |
| 944 | // successful at this aim. Because of this, we keep track of all the users we |
| 945 | // have visited and only visit users we have not seen. Because this user list |
| 946 | // should be small, we use a vector instead of a full featured set to keep |
| 947 | // track of what users we have notified so far. |
| 948 | // |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 949 | std::vector<AbstractTypeUser*> Refined; |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 950 | while (1) { |
| 951 | unsigned i; |
| 952 | for (i = AbstractTypeUsers.size(); i != 0; --i) |
| 953 | if (find(Refined.begin(), Refined.end(), AbstractTypeUsers[i-1]) == |
| 954 | Refined.end()) |
| 955 | break; // Found an unrefined user? |
| 956 | |
| 957 | if (i == 0) break; // Noone to refine left, break out of here! |
| 958 | |
| 959 | AbstractTypeUser *ATU = AbstractTypeUsers[--i]; |
| 960 | Refined.push_back(ATU); // Keep track of which users we have refined! |
| 961 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 962 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 963 | std::cerr << " typeIsREFINED user " << i << "[" << ATU |
| 964 | << "] of abstract type [" << (void*)this << " " |
| 965 | << getDescription() << "]\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 966 | #endif |
| 967 | ATU->refineAbstractType(this, this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 970 | --isRefining; |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 971 | |
| 972 | #ifndef _NDEBUG |
| 973 | if (!(isAbstract() || AbstractTypeUsers.empty())) |
| 974 | for (unsigned i = 0; i < AbstractTypeUsers.size(); ++i) { |
| 975 | if (AbstractTypeUsers[i] != this) { |
| 976 | // Debugging hook |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 977 | std::cerr << "FOUND FAILURE\nUser: "; |
Chris Lattner | cecb520 | 2002-04-05 19:53:06 +0000 | [diff] [blame] | 978 | AbstractTypeUsers[i]->dump(); |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 979 | std::cerr << "\nCatch:\n"; |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 980 | AbstractTypeUsers[i]->refineAbstractType(this, this); |
| 981 | assert(0 && "Type became concrete," |
| 982 | " but it still has abstract type users hanging around!"); |
| 983 | } |
| 984 | } |
| 985 | #endif |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 988 | |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 989 | |
| 990 | |
| 991 | // refineAbstractType - Called when a contained type is found to be more |
| 992 | // concrete - this could potentially change us from an abstract type to a |
| 993 | // concrete type. |
| 994 | // |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 995 | void FunctionType::refineAbstractType(const DerivedType *OldType, |
| 996 | const Type *NewType) { |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 997 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 998 | std::cerr << "FunctionTy::refineAbstractTy(" << (void*)OldType << "[" |
| 999 | << OldType->getDescription() << "], " << (void*)NewType << " [" |
| 1000 | << NewType->getDescription() << "])\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1001 | #endif |
Chris Lattner | 3855f2f | 2002-04-05 22:25:26 +0000 | [diff] [blame] | 1002 | // Find the type element we are refining... |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 1003 | if (ResultType == OldType) { |
| 1004 | ResultType.removeUserFromConcrete(); |
| 1005 | ResultType = NewType; |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 1006 | } |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 1007 | for (unsigned i = 0, e = ParamTys.size(); i != e; ++i) |
| 1008 | if (ParamTys[i] == OldType) { |
| 1009 | ParamTys[i].removeUserFromConcrete(); |
| 1010 | ParamTys[i] = NewType; |
| 1011 | } |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | 6bfd6a5 | 2002-03-29 03:44:36 +0000 | [diff] [blame] | 1013 | const FunctionType *MT = FunctionTypes.containsEquivalent(this); |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1014 | if (MT && MT != this) { |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 1015 | refineAbstractTypeTo(MT); // Different type altogether... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1016 | } else { |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1017 | setDerivedTypeProperties(); // Update the name and isAbstract |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 1018 | typeIsRefined(); // Same type, different contents... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1019 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | // refineAbstractType - Called when a contained type is found to be more |
| 1024 | // concrete - this could potentially change us from an abstract type to a |
| 1025 | // concrete type. |
| 1026 | // |
| 1027 | void ArrayType::refineAbstractType(const DerivedType *OldType, |
| 1028 | const Type *NewType) { |
| 1029 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 1030 | std::cerr << "ArrayTy::refineAbstractTy(" << (void*)OldType << "[" |
| 1031 | << OldType->getDescription() << "], " << (void*)NewType << " [" |
| 1032 | << NewType->getDescription() << "])\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1033 | #endif |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 1035 | assert(getElementType() == OldType); |
| 1036 | ElementType.removeUserFromConcrete(); |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1037 | ElementType = NewType; |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 1038 | |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1039 | const ArrayType *AT = ArrayTypes.containsEquivalent(this); |
| 1040 | if (AT && AT != this) { |
| 1041 | refineAbstractTypeTo(AT); // Different type altogether... |
| 1042 | } else { |
| 1043 | setDerivedTypeProperties(); // Update the name and isAbstract |
| 1044 | typeIsRefined(); // Same type, different contents... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1045 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | |
| 1049 | // refineAbstractType - Called when a contained type is found to be more |
| 1050 | // concrete - this could potentially change us from an abstract type to a |
| 1051 | // concrete type. |
| 1052 | // |
| 1053 | void StructType::refineAbstractType(const DerivedType *OldType, |
| 1054 | const Type *NewType) { |
| 1055 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 1056 | std::cerr << "StructTy::refineAbstractTy(" << (void*)OldType << "[" |
| 1057 | << OldType->getDescription() << "], " << (void*)NewType << " [" |
| 1058 | << NewType->getDescription() << "])\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1059 | #endif |
Chris Lattner | 6a490ce | 2003-01-14 19:42:39 +0000 | [diff] [blame] | 1060 | for (int i = ETypes.size()-1; i >= 0; --i) |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 1061 | if (ETypes[i] == OldType) { |
| 1062 | ETypes[i].removeUserFromConcrete(); |
Chris Lattner | e244a25 | 2001-11-03 03:27:53 +0000 | [diff] [blame] | 1063 | |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 1064 | // Update old type to new type in the array... |
| 1065 | ETypes[i] = NewType; |
| 1066 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1068 | const StructType *ST = StructTypes.containsEquivalent(this); |
| 1069 | if (ST && ST != this) { |
| 1070 | refineAbstractTypeTo(ST); // Different type altogether... |
| 1071 | } else { |
| 1072 | setDerivedTypeProperties(); // Update the name and isAbstract |
| 1073 | typeIsRefined(); // Same type, different contents... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1074 | } |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | // refineAbstractType - Called when a contained type is found to be more |
| 1078 | // concrete - this could potentially change us from an abstract type to a |
| 1079 | // concrete type. |
| 1080 | // |
| 1081 | void PointerType::refineAbstractType(const DerivedType *OldType, |
| 1082 | const Type *NewType) { |
| 1083 | #ifdef DEBUG_MERGE_TYPES |
Chris Lattner | 47697a1 | 2003-05-22 21:21:43 +0000 | [diff] [blame] | 1084 | std::cerr << "PointerTy::refineAbstractTy(" << (void*)OldType << "[" |
| 1085 | << OldType->getDescription() << "], " << (void*)NewType << " [" |
| 1086 | << NewType->getDescription() << "])\n"; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1087 | #endif |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1088 | |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 1089 | assert(ElementType == OldType); |
| 1090 | ElementType.removeUserFromConcrete(); |
Chris Lattner | 6c42c31 | 2001-12-14 16:41:56 +0000 | [diff] [blame] | 1091 | ElementType = NewType; |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | 417081c | 2002-04-07 06:14:56 +0000 | [diff] [blame] | 1093 | const PointerType *PT = PointerTypes.containsEquivalent(this); |
Chris Lattner | 3b1e3f4 | 2001-11-02 07:51:31 +0000 | [diff] [blame] | 1094 | if (PT && PT != this) { |
| 1095 | refineAbstractTypeTo(PT); // Different type altogether... |
| 1096 | } else { |
| 1097 | setDerivedTypeProperties(); // Update the name and isAbstract |
| 1098 | typeIsRefined(); // Same type, different contents... |
Chris Lattner | c038a2f | 2001-09-07 16:56:42 +0000 | [diff] [blame] | 1099 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |