1 //===-- Type.cpp - Implement the Type class ----------------------*- C++ -*--=//
3 // This file implements the Type class for the VMCore library.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/DerivedTypes.h"
8 #include "llvm/Tools/StringExtras.h"
10 //===----------------------------------------------------------------------===//
11 // Type Class Implementation
12 //===----------------------------------------------------------------------===//
14 static unsigned CurUID = 0;
15 static vector<const Type *> UIDMappings;
17 Type::Type(const string &name, PrimitiveID id)
18 : Value(Type::TypeTy, Value::TypeVal, name) {
22 UID = CurUID++; // Assign types UID's as they are created
23 UIDMappings.push_back(this);
26 const Type *Type::getUniqueIDType(unsigned UID) {
27 assert(UID < UIDMappings.size() &&
28 "Type::getPrimitiveType: UID out of range!");
29 return UIDMappings[UID];
32 const Type *Type::getPrimitiveType(PrimitiveID IDNumber) {
34 case VoidTyID : return VoidTy;
35 case BoolTyID : return BoolTy;
36 case UByteTyID : return UByteTy;
37 case SByteTyID : return SByteTy;
38 case UShortTyID: return UShortTy;
39 case ShortTyID : return ShortTy;
40 case UIntTyID : return UIntTy;
41 case IntTyID : return IntTy;
42 case ULongTyID : return ULongTy;
43 case LongTyID : return LongTy;
44 case FloatTyID : return FloatTy;
45 case DoubleTyID: return DoubleTy;
46 case TypeTyID : return TypeTy;
47 case LabelTyID : return LabelTy;
48 case LockTyID : return LockTy;
49 case FillerTyID: return new Type("XXX FILLER XXX", FillerTyID); // TODO:KILLME
57 //===----------------------------------------------------------------------===//
59 //===----------------------------------------------------------------------===//
61 // These classes are used to implement specialized behavior for each different
64 class SignedIntType : public Type {
67 SignedIntType(const string &Name, PrimitiveID id, int size) : Type(Name, id) {
71 // isSigned - Return whether a numeric type is signed.
72 virtual bool isSigned() const { return 1; }
75 class UnsignedIntType : public Type {
78 UnsignedIntType(const string &N, PrimitiveID id, int size) : Type(N, id) {
82 // isUnsigned - Return whether a numeric type is signed.
83 virtual bool isUnsigned() const { return 1; }
86 static struct TypeType : public Type {
87 TypeType() : Type("type", TypeTyID) {}
88 } TheTypeType; // Implement the type that is global.
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
95 const Type *Type::VoidTy = new Type("void" , VoidTyID),
96 *Type::BoolTy = new Type("bool" , BoolTyID),
97 *Type::SByteTy = new SignedIntType("sbyte" , SByteTyID, 1),
98 *Type::UByteTy = new UnsignedIntType("ubyte" , UByteTyID, 1),
99 *Type::ShortTy = new SignedIntType("short" , ShortTyID, 2),
100 *Type::UShortTy = new UnsignedIntType("ushort", UShortTyID, 2),
101 *Type::IntTy = new SignedIntType("int" , IntTyID, 4),
102 *Type::UIntTy = new UnsignedIntType("uint" , UIntTyID, 4),
103 *Type::LongTy = new SignedIntType("long" , LongTyID, 8),
104 *Type::ULongTy = new UnsignedIntType("ulong" , ULongTyID, 8),
105 *Type::FloatTy = new Type("float" , FloatTyID),
106 *Type::DoubleTy = new Type("double", DoubleTyID),
107 *Type::TypeTy = &TheTypeType,
108 *Type::LabelTy = new Type("label" , LabelTyID),
109 *Type::LockTy = new Type("lock" , LockTyID);
112 //===----------------------------------------------------------------------===//
113 // Derived Type Implementations
114 //===----------------------------------------------------------------------===//
116 // Make sure that only one instance of a particular type may be created on any
117 // given run of the compiler...
119 // TODO: This list should be kept in sorted order so that we can do a binary
120 // TODO: search instead of linear search!
122 // TODO: This should be templatized so that every derived type can use the same
125 #define TEST_MERGE_TYPES 0
128 #include "llvm/Assembly/Writer.h"
131 //===----------------------------------------------------------------------===//
132 // Derived Type Constructors
133 //===----------------------------------------------------------------------===//
135 MethodType::MethodType(const Type *Result, const vector<const Type*> &Params,
137 : Type(Name, MethodTyID), ResultType(Result), ParamTys(Params) {
140 ArrayType::ArrayType(const Type *ElType, int NumEl, const string &Name)
141 : Type(Name, ArrayTyID), ElementType(ElType) {
145 StructType::StructType(const vector<const Type*> &Types, const string &Name)
146 : Type(Name, StructTyID), ETypes(Types) {
149 PointerType::PointerType(const Type *E)
150 : Type(E->getName() + " *", PointerTyID), ValueType(E) {
153 //===----------------------------------------------------------------------===//
154 // Derived Type Creator Functions
155 //===----------------------------------------------------------------------===//
157 const MethodType *MethodType::getMethodType(const Type *ReturnType,
158 const vector<const Type*> &Params) {
159 static vector<const MethodType*> ExistingMethodTypesCache;
160 for (unsigned i = 0; i < ExistingMethodTypesCache.size(); ++i) {
161 const MethodType *T = ExistingMethodTypesCache[i];
162 if (T->getReturnType() == ReturnType) {
163 const ParamTypes &EParams = T->getParamTypes();
164 ParamTypes::const_iterator I = Params.begin();
165 ParamTypes::const_iterator J = EParams.begin();
166 for (; I != Params.end() && J != EParams.end(); ++I, ++J)
167 if (*I != *J) break; // These types aren't equal!
169 if (I == Params.end() && J == EParams.end()) {
170 #if TEST_MERGE_TYPES == 2
171 ostream_iterator<const Type*> out(cerr, ", ");
173 copy(Params.begin(), Params.end(), out);
174 cerr << "\"\nEquals: \"";
175 copy(EParams.begin(), EParams.end(), out);
176 cerr << "\"" << endl;
182 #if TEST_MERGE_TYPES == 2
183 ostream_iterator<const Type*> out(cerr, ", ");
184 cerr << "Input Types: ";
185 copy(Params.begin(), Params.end(), out);
189 // Calculate the string name for the new type...
190 string Name = ReturnType->getName() + " (";
191 for (ParamTypes::const_iterator I = Params.begin();
192 I != Params.end(); ++I) {
193 if (I != Params.begin())
195 Name += (*I)->getName();
200 cerr << "Derived new type: " << Name << endl;
203 MethodType *Result = new MethodType(ReturnType, Params, Name);
204 ExistingMethodTypesCache.push_back(Result);
209 const ArrayType *ArrayType::getArrayType(const Type *ElementType,
210 int NumElements = -1) {
211 static vector<const ArrayType*> ExistingTypesCache;
213 // Search cache for value...
214 for (unsigned i = 0; i < ExistingTypesCache.size(); ++i) {
215 const ArrayType *T = ExistingTypesCache[i];
217 if (T->getElementType() == ElementType &&
218 T->getNumElements() == NumElements)
222 // Value not found. Derive a new type!
224 if (NumElements != -1) Name += itostr(NumElements) + " x ";
226 Name += ElementType->getName();
228 ArrayType *Result = new ArrayType(ElementType, NumElements, Name + "]");
229 ExistingTypesCache.push_back(Result);
232 cerr << "Derived new type: " << Result->getName() << endl;
237 const StructType *StructType::getStructType(const ElementTypes &ETypes) {
238 static vector<const StructType*> ExistingStructTypesCache;
240 for (unsigned i = 0; i < ExistingStructTypesCache.size(); ++i) {
241 const StructType *T = ExistingStructTypesCache[i];
243 const ElementTypes &Elements = T->getElementTypes();
244 ElementTypes::const_iterator I = ETypes.begin();
245 ElementTypes::const_iterator J = Elements.begin();
246 for (; I != ETypes.end() && J != Elements.end(); ++I, ++J)
247 if (*I != *J) break; // These types aren't equal!
249 if (I == ETypes.end() && J == Elements.end()) {
250 #if TEST_MERGE_TYPES == 2
251 ostream_iterator<const Type*> out(cerr, ", ");
253 copy(ETypes.begin(), ETypes.end(), out);
254 cerr << "\"\nEquals: \"";
255 copy(Elements.begin(), Elements.end(), out);
256 cerr << "\"" << endl;
262 #if TEST_MERGE_TYPES == 2
263 ostream_iterator<const Type*> out(cerr, ", ");
264 cerr << "Input Types: ";
265 copy(ETypes.begin(), ETypes.end(), out);
269 // Calculate the string name for the new type...
271 for (ElementTypes::const_iterator I = ETypes.begin();
272 I != ETypes.end(); ++I) {
273 if (I != ETypes.begin())
275 Name += (*I)->getName();
280 cerr << "Derived new type: " << Name << endl;
283 StructType *Result = new StructType(ETypes, Name);
284 ExistingStructTypesCache.push_back(Result);
289 const PointerType *PointerType::getPointerType(const Type *ValueType) {
290 static vector<const PointerType*> ExistingTypesCache;
292 // Search cache for value...
293 for (unsigned i = 0; i < ExistingTypesCache.size(); ++i) {
294 const PointerType *T = ExistingTypesCache[i];
296 if (T->getValueType() == ValueType)
300 PointerType *Result = new PointerType(ValueType);
301 ExistingTypesCache.push_back(Result);
304 cerr << "Derived new type: " << Result->getName() << endl;