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/Support/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; }
74 // isIntegral - Equivalent to isSigned() || isUnsigned, but with only a single
75 // virtual function invocation.
77 virtual bool isIntegral() const { return 1; }
80 class UnsignedIntType : public Type {
83 UnsignedIntType(const string &N, PrimitiveID id, int size) : Type(N, id) {
87 // isUnsigned - Return whether a numeric type is signed.
88 virtual bool isUnsigned() const { return 1; }
90 // isIntegral - Equivalent to isSigned() || isUnsigned, but with only a single
91 // virtual function invocation.
93 virtual bool isIntegral() const { return 1; }
96 static struct TypeType : public Type {
97 TypeType() : Type("type", TypeTyID) {}
98 } TheTypeType; // Implement the type that is global.
101 //===----------------------------------------------------------------------===//
102 // Static 'Type' data
103 //===----------------------------------------------------------------------===//
105 const Type *Type::VoidTy = new Type("void" , VoidTyID),
106 *Type::BoolTy = new Type("bool" , BoolTyID),
107 *Type::SByteTy = new SignedIntType("sbyte" , SByteTyID, 1),
108 *Type::UByteTy = new UnsignedIntType("ubyte" , UByteTyID, 1),
109 *Type::ShortTy = new SignedIntType("short" , ShortTyID, 2),
110 *Type::UShortTy = new UnsignedIntType("ushort", UShortTyID, 2),
111 *Type::IntTy = new SignedIntType("int" , IntTyID, 4),
112 *Type::UIntTy = new UnsignedIntType("uint" , UIntTyID, 4),
113 *Type::LongTy = new SignedIntType("long" , LongTyID, 8),
114 *Type::ULongTy = new UnsignedIntType("ulong" , ULongTyID, 8),
115 *Type::FloatTy = new Type("float" , FloatTyID),
116 *Type::DoubleTy = new Type("double", DoubleTyID),
117 *Type::TypeTy = &TheTypeType,
118 *Type::LabelTy = new Type("label" , LabelTyID),
119 *Type::LockTy = new Type("lock" , LockTyID);
122 //===----------------------------------------------------------------------===//
123 // Derived Type Implementations
124 //===----------------------------------------------------------------------===//
126 // Make sure that only one instance of a particular type may be created on any
127 // given run of the compiler...
129 // TODO: This list should be kept in sorted order so that we can do a binary
130 // TODO: search instead of linear search!
132 // TODO: This should be templatized so that every derived type can use the same
135 #define TEST_MERGE_TYPES 0
138 #include "llvm/Assembly/Writer.h"
141 //===----------------------------------------------------------------------===//
142 // Derived Type Constructors
143 //===----------------------------------------------------------------------===//
145 MethodType::MethodType(const Type *Result, const vector<const Type*> &Params,
146 bool IsVarArgs, const string &Name)
147 : Type(Name, MethodTyID), ResultType(Result),
148 ParamTys(Params.begin(), Params.end()-IsVarArgs),
149 isVarArgs(IsVarArgs) {
152 ArrayType::ArrayType(const Type *ElType, int NumEl, const string &Name)
153 : Type(Name, ArrayTyID), ElementType(ElType) {
157 StructType::StructType(const vector<const Type*> &Types, const string &Name)
158 : Type(Name, StructTyID), ETypes(Types) {
161 PointerType::PointerType(const Type *E)
162 : Type(E->getName() + " *", PointerTyID), ValueType(E) {
165 //===----------------------------------------------------------------------===//
166 // Derived Type Creator Functions
167 //===----------------------------------------------------------------------===//
169 const MethodType *MethodType::getMethodType(const Type *ReturnType,
170 const vector<const Type*> &Params) {
171 static vector<const MethodType*> ExistingMethodTypesCache;
173 bool IsVarArg = Params.size() && (Params[Params.size()-1] == Type::VoidTy);
175 for (unsigned i = 0; i < ExistingMethodTypesCache.size(); ++i) {
176 const MethodType *T = ExistingMethodTypesCache[i];
177 if (T->getReturnType() == ReturnType && T->isVarArg() == IsVarArg) {
178 const ParamTypes &EParams = T->getParamTypes();
179 ParamTypes::const_iterator I = Params.begin(), EI = Params.end()-IsVarArg;
180 ParamTypes::const_iterator J = EParams.begin();
181 for (; I != EI && J != EParams.end(); ++I, ++J)
182 if (*I != *J) break; // These types aren't equal!
184 if (I == EI && J == EParams.end()) {
185 #if TEST_MERGE_TYPES == 2
186 ostream_iterator<const Type*> out(cerr, ", ");
188 copy(Params.begin(), EI, out);
189 cerr << "\"\nEquals: \"";
190 copy(EParams.begin(), EParams.end(), out);
191 cerr << "\"" << endl;
197 #if TEST_MERGE_TYPES == 2
198 ostream_iterator<const Type*> out(cerr, ", ");
199 cerr << "Input Types: ";
200 copy(Params.begin(), Params.end()-IsVarArg, out);
204 // Calculate the string name for the new type...
205 string Name = ReturnType->getName() + " (";
206 for (ParamTypes::const_iterator I = Params.begin();
207 I != (Params.end()-IsVarArg); ++I) {
208 if (I != Params.begin())
210 Name += (*I)->getName();
213 if (Params.size() > 1) Name += ", ";
219 cerr << "Derived new type: " << Name << endl;
222 MethodType *Result = new MethodType(ReturnType, Params, IsVarArg, Name);
223 ExistingMethodTypesCache.push_back(Result);
228 const ArrayType *ArrayType::getArrayType(const Type *ElementType,
229 int NumElements = -1) {
230 assert(ElementType && "Can't get array of null types!");
231 static vector<const ArrayType*> ExistingTypesCache;
233 // Search cache for value...
234 for (unsigned i = 0; i < ExistingTypesCache.size(); ++i) {
235 const ArrayType *T = ExistingTypesCache[i];
237 if (T->getElementType() == ElementType &&
238 T->getNumElements() == NumElements)
242 // Value not found. Derive a new type!
244 if (NumElements != -1) Name += itostr(NumElements) + " x ";
246 Name += ElementType->getName();
248 ArrayType *Result = new ArrayType(ElementType, NumElements, Name + "]");
249 ExistingTypesCache.push_back(Result);
252 cerr << "Derived new type: " << Result->getName() << endl;
257 const StructType *StructType::getStructType(const ElementTypes &ETypes) {
258 static vector<const StructType*> ExistingStructTypesCache;
260 for (unsigned i = 0; i < ExistingStructTypesCache.size(); ++i) {
261 const StructType *T = ExistingStructTypesCache[i];
263 const ElementTypes &Elements = T->getElementTypes();
264 ElementTypes::const_iterator I = ETypes.begin();
265 ElementTypes::const_iterator J = Elements.begin();
266 for (; I != ETypes.end() && J != Elements.end(); ++I, ++J)
267 if (*I != *J) break; // These types aren't equal!
269 if (I == ETypes.end() && J == Elements.end()) {
270 #if TEST_MERGE_TYPES == 2
271 ostream_iterator<const Type*> out(cerr, ", ");
273 copy(ETypes.begin(), ETypes.end(), out);
274 cerr << "\"\nEquals: \"";
275 copy(Elements.begin(), Elements.end(), out);
276 cerr << "\"" << endl;
282 #if TEST_MERGE_TYPES == 2
283 ostream_iterator<const Type*> out(cerr, ", ");
284 cerr << "Input Types: ";
285 copy(ETypes.begin(), ETypes.end(), out);
289 // Calculate the string name for the new type...
291 for (ElementTypes::const_iterator I = ETypes.begin();
292 I != ETypes.end(); ++I) {
293 if (I != ETypes.begin())
295 Name += (*I)->getName();
300 cerr << "Derived new type: " << Name << endl;
303 StructType *Result = new StructType(ETypes, Name);
304 ExistingStructTypesCache.push_back(Result);
309 const PointerType *PointerType::getPointerType(const Type *ValueType) {
310 assert(ValueType && "Can't get a pointer to <null> type!");
311 static vector<const PointerType*> ExistingTypesCache;
313 // Search cache for value...
314 for (unsigned i = 0; i < ExistingTypesCache.size(); ++i) {
315 const PointerType *T = ExistingTypesCache[i];
317 if (T->getValueType() == ValueType)
321 PointerType *Result = new PointerType(ValueType);
322 ExistingTypesCache.push_back(Result);
325 cerr << "Derived new type: " << Result->getName() << endl;