//===----------------------------------------------------------------------===//
#include "llvm/DerivedTypes.h"
-#include "llvm/Support/StringExtras.h"
#include "llvm/SymbolTable.h"
-#include "llvm/Support/STLExtras.h"
+#include "Support/StringExtras.h"
+#include "Support/STLExtras.h"
+#include <iostream>
+
+using std::vector;
+using std::string;
+using std::map;
+using std::swap;
+using std::make_pair;
+using std::cerr;
// DEBUG_MERGE_TYPES - Enable this #define to see how and when derived types are
// created and later destroyed, all in an effort to make sure that there is only
static unsigned CurUID = 0;
static vector<const Type *> UIDMappings;
+void PATypeHolder::dump() const {
+ cerr << "PATypeHolder(" << (void*)this << ")\n";
+}
+
Type::Type(const string &name, PrimitiveID id)
: Value(Type::TypeTy, Value::TypeVal) {
setDescription(name);
//
bool Type::isLosslesslyConvertableTo(const Type *Ty) const {
if (this == Ty) return true;
- if ((!isPrimitiveType() && !Ty->isPointerType()) ||
- (!isPointerType() && !Ty->isPrimitiveType())) return false;
+ if ((!isPrimitiveType() && !isPointerType()) ||
+ (!Ty->isPointerType() && !Ty->isPrimitiveType())) return false;
if (getPrimitiveID() == Ty->getPrimitiveID())
return true; // Handles identity cast, and cast of differing pointer types
bool StructType::indexValid(const Value *V) const {
- if (!isa<ConstPoolVal>(V)) return false;
+ if (!isa<Constant>(V)) return false;
if (V->getType() != Type::UByteTy) return false;
- unsigned Idx = cast<ConstPoolUInt>(V)->getValue();
+ unsigned Idx = cast<ConstantUInt>(V)->getValue();
return Idx < ETypes.size();
}
// element. For a structure type, this must be a constant value...
//
const Type *StructType::getTypeAtIndex(const Value *V) const {
- assert(isa<ConstPoolVal>(V) && "Structure index must be a constant!!");
+ assert(isa<Constant>(V) && "Structure index must be a constant!!");
assert(V->getType() == Type::UByteTy && "Structure index must be ubyte!");
- unsigned Idx = cast<ConstPoolUInt>(V)->getValue();
+ unsigned Idx = cast<ConstantUInt>(V)->getValue();
assert(Idx < ETypes.size() && "Structure index out of range!");
assert(indexValid(V) && "Invalid structure index!"); // Duplicate check
// Derived Type Constructors
//===----------------------------------------------------------------------===//
-MethodType::MethodType(const Type *Result, const vector<const Type*> &Params,
- bool IsVarArgs) : DerivedType("", MethodTyID),
+FunctionType::FunctionType(const Type *Result,
+ const vector<const Type*> &Params,
+ bool IsVarArgs) : DerivedType(FunctionTyID),
ResultType(PATypeHandle<Type>(Result, this)),
isVarArgs(IsVarArgs) {
ParamTys.reserve(Params.size());
setDerivedTypeProperties();
}
-ArrayType::ArrayType(const Type *ElType, int NumEl)
- : CompositeType("", ArrayTyID), ElementType(PATypeHandle<Type>(ElType, this)){
- NumElements = NumEl;
- setDerivedTypeProperties();
-}
-
StructType::StructType(const vector<const Type*> &Types)
- : CompositeType("", StructTyID) {
+ : CompositeType(StructTyID) {
ETypes.reserve(Types.size());
for (unsigned i = 0; i < Types.size(); ++i) {
assert(Types[i] != Type::VoidTy && "Void type in method prototype!!");
setDerivedTypeProperties();
}
-PointerType::PointerType(const Type *E) : DerivedType("", PointerTyID),
- ValueType(PATypeHandle<Type>(E, this)) {
+ArrayType::ArrayType(const Type *ElType, unsigned NumEl)
+ : SequentialType(ArrayTyID, ElType) {
+ NumElements = NumEl;
+ setDerivedTypeProperties();
+}
+
+PointerType::PointerType(const Type *E) : SequentialType(PointerTyID, E) {
setDerivedTypeProperties();
}
-OpaqueType::OpaqueType() : DerivedType("", OpaqueTyID) {
+OpaqueType::OpaqueType() : DerivedType(OpaqueTyID) {
setAbstract(true);
setDescription("opaque"+utostr(getUniqueID()));
#ifdef DEBUG_MERGE_TYPES
TypeStack.push_back(Ty); // Add us to the stack..
switch (Ty->getPrimitiveID()) {
- case Type::MethodTyID: {
- const MethodType *MTy = cast<const MethodType>(Ty);
+ case Type::FunctionTyID: {
+ const FunctionType *MTy = cast<const FunctionType>(Ty);
Result = getTypeProps(MTy->getReturnType(), TypeStack,
isAbstract, isRecursive)+" (";
- for (MethodType::ParamTypes::const_iterator
+ for (FunctionType::ParamTypes::const_iterator
I = MTy->getParamTypes().begin(),
E = MTy->getParamTypes().end(); I != E; ++I) {
if (I != MTy->getParamTypes().begin())
}
case Type::PointerTyID: {
const PointerType *PTy = cast<const PointerType>(Ty);
- Result = getTypeProps(PTy->getValueType(), TypeStack,
+ Result = getTypeProps(PTy->getElementType(), TypeStack,
isAbstract, isRecursive) + " *";
break;
}
case Type::ArrayTyID: {
const ArrayType *ATy = cast<const ArrayType>(Ty);
- int NumElements = ATy->getNumElements();
+ unsigned NumElements = ATy->getNumElements();
Result = "[";
- if (NumElements != -1) Result += itostr(NumElements) + " x ";
+ Result += utostr(NumElements) + " x ";
Result += getTypeProps(ATy->getElementType(), TypeStack,
isAbstract, isRecursive) + "]";
break;
if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
if (ATy->getNumElements() != cast<const ArrayType>(Ty2)->getNumElements())
return false;
- } else if (const MethodType *MTy = dyn_cast<MethodType>(Ty)) {
- if (MTy->isVarArg() != cast<const MethodType>(Ty2)->isVarArg())
+ } else if (const FunctionType *MTy = dyn_cast<FunctionType>(Ty)) {
+ if (MTy->isVarArg() != cast<const FunctionType>(Ty2)->isVarArg())
return false;
}
Map.erase(I);
}
- void print(const char *Arg) {
+ void print(const char *Arg) const {
#ifdef DEBUG_MERGE_TYPES
cerr << "TypeMap<>::" << Arg << " table contents:\n";
unsigned i = 0;
- for (MapTy::iterator I = Map.begin(), E = Map.end(); I != E; ++I)
+ for (MapTy::const_iterator I = Map.begin(), E = Map.end(); I != E; ++I)
cerr << " " << (++i) << ". " << I->second << " "
<< I->second->getDescription() << endl;
#endif
}
+
+ void dump() const { print("dump output"); }
};
Table.add((ValType&)Tmp, (TypeClass*)OldType.get());
#endif
}
+
+ void dump() const {
+ cerr << "ValTypeBase instance!\n";
+ }
};
//===----------------------------------------------------------------------===//
-// Method Type Factory and Value Class...
+// Function Type Factory and Value Class...
//
-// MethodValType - Define a class to hold the key that goes into the TypeMap
+// FunctionValType - Define a class to hold the key that goes into the TypeMap
//
-class MethodValType : public ValTypeBase<MethodValType, MethodType> {
+class FunctionValType : public ValTypeBase<FunctionValType, FunctionType> {
PATypeHandle<Type> RetTy;
vector<PATypeHandle<Type> > ArgTypes;
bool isVarArg;
public:
- MethodValType(const Type *ret, const vector<const Type*> &args,
- bool IVA, TypeMap<MethodValType, MethodType> &Tab)
- : ValTypeBase<MethodValType, MethodType>(Tab), RetTy(ret, this),
+ FunctionValType(const Type *ret, const vector<const Type*> &args,
+ bool IVA, TypeMap<FunctionValType, FunctionType> &Tab)
+ : ValTypeBase<FunctionValType, FunctionType>(Tab), RetTy(ret, this),
isVarArg(IVA) {
for (unsigned i = 0; i < args.size(); ++i)
ArgTypes.push_back(PATypeHandle<Type>(args[i], this));
}
// We *MUST* have an explicit copy ctor so that the TypeHandles think that
- // this MethodValType owns them, not the old one!
+ // this FunctionValType owns them, not the old one!
//
- MethodValType(const MethodValType &MVT)
- : ValTypeBase<MethodValType, MethodType>(MVT), RetTy(MVT.RetTy, this),
+ FunctionValType(const FunctionValType &MVT)
+ : ValTypeBase<FunctionValType, FunctionType>(MVT), RetTy(MVT.RetTy, this),
isVarArg(MVT.isVarArg) {
ArgTypes.reserve(MVT.ArgTypes.size());
for (unsigned i = 0; i < MVT.ArgTypes.size(); ++i)
if (ArgTypes[i] == Ty) ArgTypes[i].removeUserFromConcrete();
}
- inline bool operator<(const MethodValType &MTV) const {
+ inline bool operator<(const FunctionValType &MTV) const {
if (RetTy.get() < MTV.RetTy.get()) return true;
if (RetTy.get() > MTV.RetTy.get()) return false;
};
// Define the actual map itself now...
-static TypeMap<MethodValType, MethodType> MethodTypes;
-
-// MethodType::get - The factory function for the MethodType class...
-MethodType *MethodType::get(const Type *ReturnType,
- const vector<const Type*> &Params,
- bool isVarArg) {
- MethodValType VT(ReturnType, Params, isVarArg, MethodTypes);
- MethodType *MT = MethodTypes.get(VT);
+static TypeMap<FunctionValType, FunctionType> FunctionTypes;
+
+// FunctionType::get - The factory function for the FunctionType class...
+FunctionType *FunctionType::get(const Type *ReturnType,
+ const vector<const Type*> &Params,
+ bool isVarArg) {
+ FunctionValType VT(ReturnType, Params, isVarArg, FunctionTypes);
+ FunctionType *MT = FunctionTypes.get(VT);
if (MT) return MT;
- MethodTypes.add(VT, MT = new MethodType(ReturnType, Params, isVarArg));
+ FunctionTypes.add(VT, MT = new FunctionType(ReturnType, Params, isVarArg));
#ifdef DEBUG_MERGE_TYPES
cerr << "Derived new type: " << MT << endl;
//
class ArrayValType : public ValTypeBase<ArrayValType, ArrayType> {
PATypeHandle<Type> ValTy;
- int Size;
+ unsigned Size;
public:
ArrayValType(const Type *val, int sz, TypeMap<ArrayValType, ArrayType> &Tab)
: ValTypeBase<ArrayValType, ArrayType>(Tab), ValTy(val, this), Size(sz) {}
static TypeMap<ArrayValType, ArrayType> ArrayTypes;
-ArrayType *ArrayType::get(const Type *ElementType, int NumElements = -1) {
+ArrayType *ArrayType::get(const Type *ElementType, unsigned NumElements) {
assert(ElementType && "Can't get array of null types!");
ArrayValType AVT(ElementType, NumElements, ArrayTypes);
// Make sure to put the type to be refined to into a holder so that if IT gets
// refined, that we will not continue using a dead reference...
//
- PATypeHolder<Type> NewTy(NewType);
+ PATypeHolder NewTy(NewType);
// Add a self use of the current type so that we don't delete ourself until
// after this while loop. We are careful to never invoke refine on ourself,
#endif
User->refineAbstractType(this, NewTy);
- if (AbstractTypeUsers.size() == OldSize) {
+ if (AbstractTypeUsers.size() == OldSize)
User->refineAbstractType(this, NewTy);
- }
+
assert(AbstractTypeUsers.size() != OldSize &&
"AbsTyUser did not remove self from user list!");
}
removeAbstractTypeUser(this);
}
-
// typeIsRefined - Notify AbstractTypeUsers of this type that the current type
// has been refined a bit. The pointer is still valid and still should be
// used, but the subtypes have changed.
// concrete - this could potentially change us from an abstract type to a
// concrete type.
//
-void MethodType::refineAbstractType(const DerivedType *OldType,
- const Type *NewType) {
+void FunctionType::refineAbstractType(const DerivedType *OldType,
+ const Type *NewType) {
#ifdef DEBUG_MERGE_TYPES
- cerr << "MethodTy::refineAbstractTy(" << (void*)OldType << "["
+ cerr << "FunctionTy::refineAbstractTy(" << (void*)OldType << "["
<< OldType->getDescription() << "], " << (void*)NewType << " ["
<< NewType->getDescription() << "])\n";
#endif
if (ParamTys[i] == OldType) ParamTys[i] = NewType;
}
- const MethodType *MT = MethodTypes.containsEquivalent(this);
+ const FunctionType *MT = FunctionTypes.containsEquivalent(this);
if (MT && MT != this) {
refineAbstractTypeTo(MT); // Different type altogether...
} else {
#endif
if (!OldType->isAbstract()) {
- assert(ElementType == OldType);
+ assert(getElementType() == OldType);
ElementType.removeUserFromConcrete();
}
#endif
if (!OldType->isAbstract()) {
- assert(ValueType == OldType);
- ValueType.removeUserFromConcrete();
+ assert(ElementType == OldType);
+ ElementType.removeUserFromConcrete();
}
- ValueType = NewType;
+ ElementType = NewType;
const PointerType *PT = PointerTypes.containsEquivalent(this);
if (PT && PT != this) {