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