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