-Type::Type(const char *Name, TypeID id)
- : ID(id), Abstract(false), SubclassData(0), RefCount(0), ForwardType(0) {
- assert(Name && Name[0] && "Should use other ctor if no name!");
- (*ConcreteTypeDescriptions)[this] = Name;
-}
+/// Because of the way Type subclasses are allocated, this function is necessary
+/// to use the correct kind of "delete" operator to deallocate the Type object.
+/// Some type objects (FunctionTy, StructTy) allocate additional space after
+/// the space for their derived type to hold the contained types array of
+/// PATypeHandles. Using this allocation scheme means all the PATypeHandles are
+/// allocated with the type object, decreasing allocations and eliminating the
+/// need for a std::vector to be used in the Type class itself.
+/// @brief Type destruction function
+void Type::destroy() const {
+
+ // Structures and Functions allocate their contained types past the end of
+ // the type object itself. These need to be destroyed differently than the
+ // other types.
+ if (isa<FunctionType>(this) || isa<StructType>(this)) {
+ // First, make sure we destruct any PATypeHandles allocated by these
+ // subclasses. They must be manually destructed.
+ for (unsigned i = 0; i < NumContainedTys; ++i)
+ ContainedTys[i].PATypeHandle::~PATypeHandle();
+
+ // Now call the destructor for the subclass directly because we're going
+ // to delete this as an array of char.
+ if (isa<FunctionType>(this))
+ ((FunctionType*)this)->FunctionType::~FunctionType();
+ else
+ ((StructType*)this)->StructType::~StructType();
+
+ // Finally, remove the memory as an array deallocation of the chars it was
+ // constructed from.
+ delete [] reinterpret_cast<const char*>(this);
+
+ return;
+ }