Reduce the exposure of Triple::OSType in the ELF object writer. This will
[oota-llvm.git] / lib / VMCore / Type.cpp
1 //===-- Type.cpp - Implement the Type class -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Type class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "LLVMContextImpl.h"
15 #include "llvm/Module.h"
16 #include <algorithm>
17 #include <cstdarg>
18 #include "llvm/ADT/SmallString.h"
19 using namespace llvm;
20
21 //===----------------------------------------------------------------------===//
22 //                         Type Class Implementation
23 //===----------------------------------------------------------------------===//
24
25 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
26   switch (IDNumber) {
27   case VoidTyID      : return getVoidTy(C);
28   case HalfTyID      : return getHalfTy(C);
29   case FloatTyID     : return getFloatTy(C);
30   case DoubleTyID    : return getDoubleTy(C);
31   case X86_FP80TyID  : return getX86_FP80Ty(C);
32   case FP128TyID     : return getFP128Ty(C);
33   case PPC_FP128TyID : return getPPC_FP128Ty(C);
34   case LabelTyID     : return getLabelTy(C);
35   case MetadataTyID  : return getMetadataTy(C);
36   case X86_MMXTyID   : return getX86_MMXTy(C);
37   default:
38     return 0;
39   }
40 }
41
42 /// getScalarType - If this is a vector type, return the element type,
43 /// otherwise return this.
44 Type *Type::getScalarType() {
45   if (VectorType *VTy = dyn_cast<VectorType>(this))
46     return VTy->getElementType();
47   return this;
48 }
49
50 /// getNumElements - If this is a vector type, return the number of elements,
51 /// otherwise return zero.
52 unsigned Type::getNumElements() {
53   if (VectorType *VTy = dyn_cast<VectorType>(this))
54     return VTy->getNumElements();
55   return 0;
56 }
57
58 /// isIntegerTy - Return true if this is an IntegerType of the specified width.
59 bool Type::isIntegerTy(unsigned Bitwidth) const {
60   return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
61 }
62
63 /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
64 /// integer types.
65 ///
66 bool Type::isIntOrIntVectorTy() const {
67   if (isIntegerTy())
68     return true;
69   if (ID != Type::VectorTyID) return false;
70   
71   return cast<VectorType>(this)->getElementType()->isIntegerTy();
72 }
73
74 /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP types.
75 ///
76 bool Type::isFPOrFPVectorTy() const {
77   if (ID == Type::HalfTyID || ID == Type::FloatTyID || ID == Type::DoubleTyID ||
78       ID == Type::FP128TyID || ID == Type::X86_FP80TyID || 
79       ID == Type::PPC_FP128TyID)
80     return true;
81   if (ID != Type::VectorTyID) return false;
82   
83   return cast<VectorType>(this)->getElementType()->isFloatingPointTy();
84 }
85
86 // canLosslesslyBitCastTo - Return true if this type can be converted to
87 // 'Ty' without any reinterpretation of bits.  For example, i8* to i32*.
88 //
89 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
90   // Identity cast means no change so return true
91   if (this == Ty) 
92     return true;
93   
94   // They are not convertible unless they are at least first class types
95   if (!this->isFirstClassType() || !Ty->isFirstClassType())
96     return false;
97
98   // Vector -> Vector conversions are always lossless if the two vector types
99   // have the same size, otherwise not.  Also, 64-bit vector types can be
100   // converted to x86mmx.
101   if (const VectorType *thisPTy = dyn_cast<VectorType>(this)) {
102     if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
103       return thisPTy->getBitWidth() == thatPTy->getBitWidth();
104     if (Ty->getTypeID() == Type::X86_MMXTyID &&
105         thisPTy->getBitWidth() == 64)
106       return true;
107   }
108
109   if (this->getTypeID() == Type::X86_MMXTyID)
110     if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
111       if (thatPTy->getBitWidth() == 64)
112         return true;
113
114   // At this point we have only various mismatches of the first class types
115   // remaining and ptr->ptr. Just select the lossless conversions. Everything
116   // else is not lossless.
117   if (this->isPointerTy())
118     return Ty->isPointerTy();
119   return false;  // Other types have no identity values
120 }
121
122 bool Type::isEmptyTy() const {
123   const ArrayType *ATy = dyn_cast<ArrayType>(this);
124   if (ATy) {
125     unsigned NumElements = ATy->getNumElements();
126     return NumElements == 0 || ATy->getElementType()->isEmptyTy();
127   }
128
129   const StructType *STy = dyn_cast<StructType>(this);
130   if (STy) {
131     unsigned NumElements = STy->getNumElements();
132     for (unsigned i = 0; i < NumElements; ++i)
133       if (!STy->getElementType(i)->isEmptyTy())
134         return false;
135     return true;
136   }
137
138   return false;
139 }
140
141 unsigned Type::getPrimitiveSizeInBits() const {
142   switch (getTypeID()) {
143   case Type::HalfTyID: return 16;
144   case Type::FloatTyID: return 32;
145   case Type::DoubleTyID: return 64;
146   case Type::X86_FP80TyID: return 80;
147   case Type::FP128TyID: return 128;
148   case Type::PPC_FP128TyID: return 128;
149   case Type::X86_MMXTyID: return 64;
150   case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
151   case Type::VectorTyID:  return cast<VectorType>(this)->getBitWidth();
152   default: return 0;
153   }
154 }
155
156 /// getScalarSizeInBits - If this is a vector type, return the
157 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
158 /// getPrimitiveSizeInBits value for this type.
159 unsigned Type::getScalarSizeInBits() {
160   return getScalarType()->getPrimitiveSizeInBits();
161 }
162
163 /// getFPMantissaWidth - Return the width of the mantissa of this type.  This
164 /// is only valid on floating point types.  If the FP type does not
165 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
166 int Type::getFPMantissaWidth() const {
167   if (const VectorType *VTy = dyn_cast<VectorType>(this))
168     return VTy->getElementType()->getFPMantissaWidth();
169   assert(isFloatingPointTy() && "Not a floating point type!");
170   if (ID == HalfTyID) return 11;
171   if (ID == FloatTyID) return 24;
172   if (ID == DoubleTyID) return 53;
173   if (ID == X86_FP80TyID) return 64;
174   if (ID == FP128TyID) return 113;
175   assert(ID == PPC_FP128TyID && "unknown fp type");
176   return -1;
177 }
178
179 /// isSizedDerivedType - Derived types like structures and arrays are sized
180 /// iff all of the members of the type are sized as well.  Since asking for
181 /// their size is relatively uncommon, move this operation out of line.
182 bool Type::isSizedDerivedType() const {
183   if (this->isIntegerTy())
184     return true;
185
186   if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
187     return ATy->getElementType()->isSized();
188
189   if (const VectorType *VTy = dyn_cast<VectorType>(this))
190     return VTy->getElementType()->isSized();
191
192   if (!this->isStructTy()) 
193     return false;
194
195   // Opaque structs have no size.
196   if (cast<StructType>(this)->isOpaque())
197     return false;
198   
199   // Okay, our struct is sized if all of the elements are.
200   for (subtype_iterator I = subtype_begin(), E = subtype_end(); I != E; ++I)
201     if (!(*I)->isSized()) 
202       return false;
203
204   return true;
205 }
206
207 //===----------------------------------------------------------------------===//
208 //                          Primitive 'Type' data
209 //===----------------------------------------------------------------------===//
210
211 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
212 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
213 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
214 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
215 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
216 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
217 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
218 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
219 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
220 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
221
222 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
223 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
224 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
225 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
226 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
227
228 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
229   return IntegerType::get(C, N);
230 }
231
232 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
233   return getHalfTy(C)->getPointerTo(AS);
234 }
235
236 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
237   return getFloatTy(C)->getPointerTo(AS);
238 }
239
240 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
241   return getDoubleTy(C)->getPointerTo(AS);
242 }
243
244 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
245   return getX86_FP80Ty(C)->getPointerTo(AS);
246 }
247
248 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
249   return getFP128Ty(C)->getPointerTo(AS);
250 }
251
252 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
253   return getPPC_FP128Ty(C)->getPointerTo(AS);
254 }
255
256 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
257   return getX86_MMXTy(C)->getPointerTo(AS);
258 }
259
260 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
261   return getIntNTy(C, N)->getPointerTo(AS);
262 }
263
264 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
265   return getInt1Ty(C)->getPointerTo(AS);
266 }
267
268 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
269   return getInt8Ty(C)->getPointerTo(AS);
270 }
271
272 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
273   return getInt16Ty(C)->getPointerTo(AS);
274 }
275
276 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
277   return getInt32Ty(C)->getPointerTo(AS);
278 }
279
280 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
281   return getInt64Ty(C)->getPointerTo(AS);
282 }
283
284
285 //===----------------------------------------------------------------------===//
286 //                       IntegerType Implementation
287 //===----------------------------------------------------------------------===//
288
289 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
290   assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
291   assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
292   
293   // Check for the built-in integer types
294   switch (NumBits) {
295   case  1: return cast<IntegerType>(Type::getInt1Ty(C));
296   case  8: return cast<IntegerType>(Type::getInt8Ty(C));
297   case 16: return cast<IntegerType>(Type::getInt16Ty(C));
298   case 32: return cast<IntegerType>(Type::getInt32Ty(C));
299   case 64: return cast<IntegerType>(Type::getInt64Ty(C));
300   default: 
301     break;
302   }
303   
304   IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
305   
306   if (Entry == 0)
307     Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
308   
309   return Entry;
310 }
311
312 bool IntegerType::isPowerOf2ByteWidth() const {
313   unsigned BitWidth = getBitWidth();
314   return (BitWidth > 7) && isPowerOf2_32(BitWidth);
315 }
316
317 APInt IntegerType::getMask() const {
318   return APInt::getAllOnesValue(getBitWidth());
319 }
320
321 //===----------------------------------------------------------------------===//
322 //                       FunctionType Implementation
323 //===----------------------------------------------------------------------===//
324
325 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
326                            bool IsVarArgs)
327   : Type(Result->getContext(), FunctionTyID) {
328   Type **SubTys = reinterpret_cast<Type**>(this+1);
329   assert(isValidReturnType(Result) && "invalid return type for function");
330   setSubclassData(IsVarArgs);
331
332   SubTys[0] = const_cast<Type*>(Result);
333
334   for (unsigned i = 0, e = Params.size(); i != e; ++i) {
335     assert(isValidArgumentType(Params[i]) &&
336            "Not a valid type for function argument!");
337     SubTys[i+1] = Params[i];
338   }
339
340   ContainedTys = SubTys;
341   NumContainedTys = Params.size() + 1; // + 1 for result type
342 }
343
344 // FunctionType::get - The factory function for the FunctionType class.
345 FunctionType *FunctionType::get(Type *ReturnType,
346                                 ArrayRef<Type*> Params, bool isVarArg) {
347   // TODO: This is brutally slow.
348   std::vector<Type*> Key;
349   Key.reserve(Params.size()+2);
350   Key.push_back(const_cast<Type*>(ReturnType));
351   for (unsigned i = 0, e = Params.size(); i != e; ++i)
352     Key.push_back(const_cast<Type*>(Params[i]));
353   if (isVarArg)
354     Key.push_back(0);
355   
356   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
357   FunctionType *&FT = pImpl->FunctionTypes[Key];
358   
359   if (FT == 0) {
360     FT = (FunctionType*) pImpl->TypeAllocator.
361       Allocate(sizeof(FunctionType) + sizeof(Type*)*(Params.size()+1),
362                AlignOf<FunctionType>::Alignment);
363     new (FT) FunctionType(ReturnType, Params, isVarArg);
364   }
365
366   return FT;
367 }
368
369
370 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
371   return get(Result, ArrayRef<Type *>(), isVarArg);
372 }
373
374
375 /// isValidReturnType - Return true if the specified type is valid as a return
376 /// type.
377 bool FunctionType::isValidReturnType(Type *RetTy) {
378   return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
379   !RetTy->isMetadataTy();
380 }
381
382 /// isValidArgumentType - Return true if the specified type is valid as an
383 /// argument type.
384 bool FunctionType::isValidArgumentType(Type *ArgTy) {
385   return ArgTy->isFirstClassType();
386 }
387
388 //===----------------------------------------------------------------------===//
389 //                       StructType Implementation
390 //===----------------------------------------------------------------------===//
391
392 // Primitive Constructors.
393
394 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes, 
395                             bool isPacked) {
396   // FIXME: std::vector is horribly inefficient for this probe.
397   std::vector<Type*> Key;
398   for (unsigned i = 0, e = ETypes.size(); i != e; ++i) {
399     assert(isValidElementType(ETypes[i]) &&
400            "Invalid type for structure element!");
401     Key.push_back(ETypes[i]);
402   }
403   if (isPacked)
404     Key.push_back(0);
405   
406   StructType *&ST = Context.pImpl->AnonStructTypes[Key];
407   if (ST) return ST;
408   
409   // Value not found.  Create a new type!
410   ST = new (Context.pImpl->TypeAllocator) StructType(Context);
411   ST->setSubclassData(SCDB_IsLiteral);  // Literal struct.
412   ST->setBody(ETypes, isPacked);
413   return ST;
414 }
415
416 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
417   assert(isOpaque() && "Struct body already set!");
418   
419   setSubclassData(getSubclassData() | SCDB_HasBody);
420   if (isPacked)
421     setSubclassData(getSubclassData() | SCDB_Packed);
422   
423   Type **Elts = getContext().pImpl->
424     TypeAllocator.Allocate<Type*>(Elements.size());
425   memcpy(Elts, Elements.data(), sizeof(Elements[0])*Elements.size());
426   
427   ContainedTys = Elts;
428   NumContainedTys = Elements.size();
429 }
430
431 void StructType::setName(StringRef Name) {
432   if (Name == getName()) return;
433
434   // If this struct already had a name, remove its symbol table entry.
435   if (SymbolTableEntry) {
436     getContext().pImpl->NamedStructTypes.erase(getName());
437     SymbolTableEntry = 0;
438   }
439   
440   // If this is just removing the name, we're done.
441   if (Name.empty())
442     return;
443   
444   // Look up the entry for the name.
445   StringMapEntry<StructType*> *Entry =
446     &getContext().pImpl->NamedStructTypes.GetOrCreateValue(Name);
447   
448   // While we have a name collision, try a random rename.
449   if (Entry->getValue()) {
450     SmallString<64> TempStr(Name);
451     TempStr.push_back('.');
452     raw_svector_ostream TmpStream(TempStr);
453    
454     do {
455       TempStr.resize(Name.size()+1);
456       TmpStream.resync();
457       TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
458       
459       Entry = &getContext().pImpl->
460                  NamedStructTypes.GetOrCreateValue(TmpStream.str());
461     } while (Entry->getValue());
462   }
463
464   // Okay, we found an entry that isn't used.  It's us!
465   Entry->setValue(this);
466     
467   SymbolTableEntry = Entry;
468 }
469
470 //===----------------------------------------------------------------------===//
471 // StructType Helper functions.
472
473 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
474   StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
475   if (!Name.empty())
476     ST->setName(Name);
477   return ST;
478 }
479
480 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
481   return get(Context, llvm::ArrayRef<Type*>(), isPacked);
482 }
483
484 StructType *StructType::get(Type *type, ...) {
485   assert(type != 0 && "Cannot create a struct type with no elements with this");
486   LLVMContext &Ctx = type->getContext();
487   va_list ap;
488   SmallVector<llvm::Type*, 8> StructFields;
489   va_start(ap, type);
490   while (type) {
491     StructFields.push_back(type);
492     type = va_arg(ap, llvm::Type*);
493   }
494   return llvm::StructType::get(Ctx, StructFields);
495 }
496
497 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
498                                StringRef Name, bool isPacked) {
499   StructType *ST = create(Context, Name);
500   ST->setBody(Elements, isPacked);
501   return ST;
502 }
503
504 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
505   return create(Context, Elements, StringRef());
506 }
507
508 StructType *StructType::create(LLVMContext &Context) {
509   return create(Context, StringRef());
510 }
511
512
513 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
514                                bool isPacked) {
515   assert(!Elements.empty() &&
516          "This method may not be invoked with an empty list");
517   return create(Elements[0]->getContext(), Elements, Name, isPacked);
518 }
519
520 StructType *StructType::create(ArrayRef<Type*> Elements) {
521   assert(!Elements.empty() &&
522          "This method may not be invoked with an empty list");
523   return create(Elements[0]->getContext(), Elements, StringRef());
524 }
525
526 StructType *StructType::create(StringRef Name, Type *type, ...) {
527   assert(type != 0 && "Cannot create a struct type with no elements with this");
528   LLVMContext &Ctx = type->getContext();
529   va_list ap;
530   SmallVector<llvm::Type*, 8> StructFields;
531   va_start(ap, type);
532   while (type) {
533     StructFields.push_back(type);
534     type = va_arg(ap, llvm::Type*);
535   }
536   return llvm::StructType::create(Ctx, StructFields, Name);
537 }
538
539
540 StringRef StructType::getName() const {
541   assert(!isLiteral() && "Literal structs never have names");
542   if (SymbolTableEntry == 0) return StringRef();
543   
544   return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
545 }
546
547 void StructType::setBody(Type *type, ...) {
548   assert(type != 0 && "Cannot create a struct type with no elements with this");
549   va_list ap;
550   SmallVector<llvm::Type*, 8> StructFields;
551   va_start(ap, type);
552   while (type) {
553     StructFields.push_back(type);
554     type = va_arg(ap, llvm::Type*);
555   }
556   setBody(StructFields);
557 }
558
559 bool StructType::isValidElementType(Type *ElemTy) {
560   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
561          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
562 }
563
564 /// isLayoutIdentical - Return true if this is layout identical to the
565 /// specified struct.
566 bool StructType::isLayoutIdentical(StructType *Other) const {
567   if (this == Other) return true;
568   
569   if (isPacked() != Other->isPacked() ||
570       getNumElements() != Other->getNumElements())
571     return false;
572   
573   return std::equal(element_begin(), element_end(), Other->element_begin());
574 }
575
576
577 /// getTypeByName - Return the type with the specified name, or null if there
578 /// is none by that name.
579 StructType *Module::getTypeByName(StringRef Name) const {
580   StringMap<StructType*>::iterator I =
581     getContext().pImpl->NamedStructTypes.find(Name);
582   if (I != getContext().pImpl->NamedStructTypes.end())
583     return I->second;
584   return 0;
585 }
586
587
588 //===----------------------------------------------------------------------===//
589 //                       CompositeType Implementation
590 //===----------------------------------------------------------------------===//
591
592 Type *CompositeType::getTypeAtIndex(const Value *V) {
593   if (StructType *STy = dyn_cast<StructType>(this)) {
594     unsigned Idx = (unsigned)cast<ConstantInt>(V)->getZExtValue();
595     assert(indexValid(Idx) && "Invalid structure index!");
596     return STy->getElementType(Idx);
597   }
598   
599   return cast<SequentialType>(this)->getElementType();
600 }
601 Type *CompositeType::getTypeAtIndex(unsigned Idx) {
602   if (StructType *STy = dyn_cast<StructType>(this)) {
603     assert(indexValid(Idx) && "Invalid structure index!");
604     return STy->getElementType(Idx);
605   }
606   
607   return cast<SequentialType>(this)->getElementType();
608 }
609 bool CompositeType::indexValid(const Value *V) const {
610   if (const StructType *STy = dyn_cast<StructType>(this)) {
611     // Structure indexes require 32-bit integer constants.
612     if (V->getType()->isIntegerTy(32))
613       if (const ConstantInt *CU = dyn_cast<ConstantInt>(V))
614         return CU->getZExtValue() < STy->getNumElements();
615     return false;
616   }
617   
618   // Sequential types can be indexed by any integer.
619   return V->getType()->isIntegerTy();
620 }
621
622 bool CompositeType::indexValid(unsigned Idx) const {
623   if (const StructType *STy = dyn_cast<StructType>(this))
624     return Idx < STy->getNumElements();
625   // Sequential types can be indexed by any integer.
626   return true;
627 }
628
629
630 //===----------------------------------------------------------------------===//
631 //                           ArrayType Implementation
632 //===----------------------------------------------------------------------===//
633
634 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
635   : SequentialType(ArrayTyID, ElType) {
636   NumElements = NumEl;
637 }
638
639
640 ArrayType *ArrayType::get(Type *elementType, uint64_t NumElements) {
641   Type *ElementType = const_cast<Type*>(elementType);
642   assert(isValidElementType(ElementType) && "Invalid type for array element!");
643     
644   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
645   ArrayType *&Entry = 
646     pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
647   
648   if (Entry == 0)
649     Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
650   return Entry;
651 }
652
653 bool ArrayType::isValidElementType(Type *ElemTy) {
654   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
655          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
656 }
657
658 //===----------------------------------------------------------------------===//
659 //                          VectorType Implementation
660 //===----------------------------------------------------------------------===//
661
662 VectorType::VectorType(Type *ElType, unsigned NumEl)
663   : SequentialType(VectorTyID, ElType) {
664   NumElements = NumEl;
665 }
666
667 VectorType *VectorType::get(Type *elementType, unsigned NumElements) {
668   Type *ElementType = const_cast<Type*>(elementType);
669   assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
670   assert(isValidElementType(ElementType) &&
671          "Elements of a VectorType must be a primitive type");
672   
673   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
674   VectorType *&Entry = ElementType->getContext().pImpl
675     ->VectorTypes[std::make_pair(ElementType, NumElements)];
676   
677   if (Entry == 0)
678     Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
679   return Entry;
680 }
681
682 bool VectorType::isValidElementType(Type *ElemTy) {
683   if (PointerType *PTy = dyn_cast<PointerType>(ElemTy))
684     ElemTy = PTy->getElementType();
685   return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy();
686 }
687
688 //===----------------------------------------------------------------------===//
689 //                         PointerType Implementation
690 //===----------------------------------------------------------------------===//
691
692 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
693   assert(EltTy && "Can't get a pointer to <null> type!");
694   assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
695   
696   LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
697   
698   // Since AddressSpace #0 is the common case, we special case it.
699   PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
700      : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
701
702   if (Entry == 0)
703     Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
704   return Entry;
705 }
706
707
708 PointerType::PointerType(Type *E, unsigned AddrSpace)
709   : SequentialType(PointerTyID, E) {
710   setSubclassData(AddrSpace);
711 }
712
713 PointerType *Type::getPointerTo(unsigned addrs) {
714   return PointerType::get(this, addrs);
715 }
716
717 bool PointerType::isValidElementType(Type *ElemTy) {
718   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
719          !ElemTy->isMetadataTy();
720 }