LLVM API Documentation
00001 //===-- Type.cpp - Implement the Type class -------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Type class for the IR library. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/IR/Type.h" 00015 #include "LLVMContextImpl.h" 00016 #include "llvm/ADT/SmallString.h" 00017 #include "llvm/IR/Module.h" 00018 #include <algorithm> 00019 #include <cstdarg> 00020 using namespace llvm; 00021 00022 //===----------------------------------------------------------------------===// 00023 // Type Class Implementation 00024 //===----------------------------------------------------------------------===// 00025 00026 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) { 00027 switch (IDNumber) { 00028 case VoidTyID : return getVoidTy(C); 00029 case HalfTyID : return getHalfTy(C); 00030 case FloatTyID : return getFloatTy(C); 00031 case DoubleTyID : return getDoubleTy(C); 00032 case X86_FP80TyID : return getX86_FP80Ty(C); 00033 case FP128TyID : return getFP128Ty(C); 00034 case PPC_FP128TyID : return getPPC_FP128Ty(C); 00035 case LabelTyID : return getLabelTy(C); 00036 case MetadataTyID : return getMetadataTy(C); 00037 case X86_MMXTyID : return getX86_MMXTy(C); 00038 default: 00039 return 0; 00040 } 00041 } 00042 00043 /// getScalarType - If this is a vector type, return the element type, 00044 /// otherwise return this. 00045 Type *Type::getScalarType() { 00046 if (VectorType *VTy = dyn_cast<VectorType>(this)) 00047 return VTy->getElementType(); 00048 return this; 00049 } 00050 00051 const Type *Type::getScalarType() const { 00052 if (const VectorType *VTy = dyn_cast<VectorType>(this)) 00053 return VTy->getElementType(); 00054 return this; 00055 } 00056 00057 /// isIntegerTy - Return true if this is an IntegerType of the specified width. 00058 bool Type::isIntegerTy(unsigned Bitwidth) const { 00059 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth; 00060 } 00061 00062 // canLosslesslyBitCastTo - Return true if this type can be converted to 00063 // 'Ty' without any reinterpretation of bits. For example, i8* to i32*. 00064 // 00065 bool Type::canLosslesslyBitCastTo(Type *Ty) const { 00066 // Identity cast means no change so return true 00067 if (this == Ty) 00068 return true; 00069 00070 // They are not convertible unless they are at least first class types 00071 if (!this->isFirstClassType() || !Ty->isFirstClassType()) 00072 return false; 00073 00074 // Vector -> Vector conversions are always lossless if the two vector types 00075 // have the same size, otherwise not. Also, 64-bit vector types can be 00076 // converted to x86mmx. 00077 if (const VectorType *thisPTy = dyn_cast<VectorType>(this)) { 00078 if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty)) 00079 return thisPTy->getBitWidth() == thatPTy->getBitWidth(); 00080 if (Ty->getTypeID() == Type::X86_MMXTyID && 00081 thisPTy->getBitWidth() == 64) 00082 return true; 00083 } 00084 00085 if (this->getTypeID() == Type::X86_MMXTyID) 00086 if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty)) 00087 if (thatPTy->getBitWidth() == 64) 00088 return true; 00089 00090 // At this point we have only various mismatches of the first class types 00091 // remaining and ptr->ptr. Just select the lossless conversions. Everything 00092 // else is not lossless. 00093 if (this->isPointerTy()) 00094 return Ty->isPointerTy(); 00095 return false; // Other types have no identity values 00096 } 00097 00098 bool Type::isEmptyTy() const { 00099 const ArrayType *ATy = dyn_cast<ArrayType>(this); 00100 if (ATy) { 00101 unsigned NumElements = ATy->getNumElements(); 00102 return NumElements == 0 || ATy->getElementType()->isEmptyTy(); 00103 } 00104 00105 const StructType *STy = dyn_cast<StructType>(this); 00106 if (STy) { 00107 unsigned NumElements = STy->getNumElements(); 00108 for (unsigned i = 0; i < NumElements; ++i) 00109 if (!STy->getElementType(i)->isEmptyTy()) 00110 return false; 00111 return true; 00112 } 00113 00114 return false; 00115 } 00116 00117 unsigned Type::getPrimitiveSizeInBits() const { 00118 switch (getTypeID()) { 00119 case Type::HalfTyID: return 16; 00120 case Type::FloatTyID: return 32; 00121 case Type::DoubleTyID: return 64; 00122 case Type::X86_FP80TyID: return 80; 00123 case Type::FP128TyID: return 128; 00124 case Type::PPC_FP128TyID: return 128; 00125 case Type::X86_MMXTyID: return 64; 00126 case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth(); 00127 case Type::VectorTyID: return cast<VectorType>(this)->getBitWidth(); 00128 default: return 0; 00129 } 00130 } 00131 00132 /// getScalarSizeInBits - If this is a vector type, return the 00133 /// getPrimitiveSizeInBits value for the element type. Otherwise return the 00134 /// getPrimitiveSizeInBits value for this type. 00135 unsigned Type::getScalarSizeInBits() { 00136 return getScalarType()->getPrimitiveSizeInBits(); 00137 } 00138 00139 /// getFPMantissaWidth - Return the width of the mantissa of this type. This 00140 /// is only valid on floating point types. If the FP type does not 00141 /// have a stable mantissa (e.g. ppc long double), this method returns -1. 00142 int Type::getFPMantissaWidth() const { 00143 if (const VectorType *VTy = dyn_cast<VectorType>(this)) 00144 return VTy->getElementType()->getFPMantissaWidth(); 00145 assert(isFloatingPointTy() && "Not a floating point type!"); 00146 if (getTypeID() == HalfTyID) return 11; 00147 if (getTypeID() == FloatTyID) return 24; 00148 if (getTypeID() == DoubleTyID) return 53; 00149 if (getTypeID() == X86_FP80TyID) return 64; 00150 if (getTypeID() == FP128TyID) return 113; 00151 assert(getTypeID() == PPC_FP128TyID && "unknown fp type"); 00152 return -1; 00153 } 00154 00155 /// isSizedDerivedType - Derived types like structures and arrays are sized 00156 /// iff all of the members of the type are sized as well. Since asking for 00157 /// their size is relatively uncommon, move this operation out of line. 00158 bool Type::isSizedDerivedType() const { 00159 if (this->isIntegerTy()) 00160 return true; 00161 00162 if (const ArrayType *ATy = dyn_cast<ArrayType>(this)) 00163 return ATy->getElementType()->isSized(); 00164 00165 if (const VectorType *VTy = dyn_cast<VectorType>(this)) 00166 return VTy->getElementType()->isSized(); 00167 00168 if (!this->isStructTy()) 00169 return false; 00170 00171 return cast<StructType>(this)->isSized(); 00172 } 00173 00174 //===----------------------------------------------------------------------===// 00175 // Subclass Helper Methods 00176 //===----------------------------------------------------------------------===// 00177 00178 unsigned Type::getIntegerBitWidth() const { 00179 return cast<IntegerType>(this)->getBitWidth(); 00180 } 00181 00182 bool Type::isFunctionVarArg() const { 00183 return cast<FunctionType>(this)->isVarArg(); 00184 } 00185 00186 Type *Type::getFunctionParamType(unsigned i) const { 00187 return cast<FunctionType>(this)->getParamType(i); 00188 } 00189 00190 unsigned Type::getFunctionNumParams() const { 00191 return cast<FunctionType>(this)->getNumParams(); 00192 } 00193 00194 StringRef Type::getStructName() const { 00195 return cast<StructType>(this)->getName(); 00196 } 00197 00198 unsigned Type::getStructNumElements() const { 00199 return cast<StructType>(this)->getNumElements(); 00200 } 00201 00202 Type *Type::getStructElementType(unsigned N) const { 00203 return cast<StructType>(this)->getElementType(N); 00204 } 00205 00206 Type *Type::getSequentialElementType() const { 00207 return cast<SequentialType>(this)->getElementType(); 00208 } 00209 00210 uint64_t Type::getArrayNumElements() const { 00211 return cast<ArrayType>(this)->getNumElements(); 00212 } 00213 00214 unsigned Type::getVectorNumElements() const { 00215 return cast<VectorType>(this)->getNumElements(); 00216 } 00217 00218 unsigned Type::getPointerAddressSpace() const { 00219 return cast<PointerType>(getScalarType())->getAddressSpace(); 00220 } 00221 00222 00223 //===----------------------------------------------------------------------===// 00224 // Primitive 'Type' data 00225 //===----------------------------------------------------------------------===// 00226 00227 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; } 00228 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; } 00229 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; } 00230 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; } 00231 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; } 00232 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; } 00233 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; } 00234 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; } 00235 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; } 00236 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; } 00237 00238 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; } 00239 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; } 00240 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; } 00241 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; } 00242 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; } 00243 00244 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) { 00245 return IntegerType::get(C, N); 00246 } 00247 00248 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) { 00249 return getHalfTy(C)->getPointerTo(AS); 00250 } 00251 00252 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) { 00253 return getFloatTy(C)->getPointerTo(AS); 00254 } 00255 00256 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) { 00257 return getDoubleTy(C)->getPointerTo(AS); 00258 } 00259 00260 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) { 00261 return getX86_FP80Ty(C)->getPointerTo(AS); 00262 } 00263 00264 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) { 00265 return getFP128Ty(C)->getPointerTo(AS); 00266 } 00267 00268 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) { 00269 return getPPC_FP128Ty(C)->getPointerTo(AS); 00270 } 00271 00272 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) { 00273 return getX86_MMXTy(C)->getPointerTo(AS); 00274 } 00275 00276 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) { 00277 return getIntNTy(C, N)->getPointerTo(AS); 00278 } 00279 00280 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) { 00281 return getInt1Ty(C)->getPointerTo(AS); 00282 } 00283 00284 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) { 00285 return getInt8Ty(C)->getPointerTo(AS); 00286 } 00287 00288 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) { 00289 return getInt16Ty(C)->getPointerTo(AS); 00290 } 00291 00292 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) { 00293 return getInt32Ty(C)->getPointerTo(AS); 00294 } 00295 00296 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) { 00297 return getInt64Ty(C)->getPointerTo(AS); 00298 } 00299 00300 00301 //===----------------------------------------------------------------------===// 00302 // IntegerType Implementation 00303 //===----------------------------------------------------------------------===// 00304 00305 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) { 00306 assert(NumBits >= MIN_INT_BITS && "bitwidth too small"); 00307 assert(NumBits <= MAX_INT_BITS && "bitwidth too large"); 00308 00309 // Check for the built-in integer types 00310 switch (NumBits) { 00311 case 1: return cast<IntegerType>(Type::getInt1Ty(C)); 00312 case 8: return cast<IntegerType>(Type::getInt8Ty(C)); 00313 case 16: return cast<IntegerType>(Type::getInt16Ty(C)); 00314 case 32: return cast<IntegerType>(Type::getInt32Ty(C)); 00315 case 64: return cast<IntegerType>(Type::getInt64Ty(C)); 00316 default: 00317 break; 00318 } 00319 00320 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits]; 00321 00322 if (Entry == 0) 00323 Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits); 00324 00325 return Entry; 00326 } 00327 00328 bool IntegerType::isPowerOf2ByteWidth() const { 00329 unsigned BitWidth = getBitWidth(); 00330 return (BitWidth > 7) && isPowerOf2_32(BitWidth); 00331 } 00332 00333 APInt IntegerType::getMask() const { 00334 return APInt::getAllOnesValue(getBitWidth()); 00335 } 00336 00337 //===----------------------------------------------------------------------===// 00338 // FunctionType Implementation 00339 //===----------------------------------------------------------------------===// 00340 00341 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params, 00342 bool IsVarArgs) 00343 : Type(Result->getContext(), FunctionTyID) { 00344 Type **SubTys = reinterpret_cast<Type**>(this+1); 00345 assert(isValidReturnType(Result) && "invalid return type for function"); 00346 setSubclassData(IsVarArgs); 00347 00348 SubTys[0] = const_cast<Type*>(Result); 00349 00350 for (unsigned i = 0, e = Params.size(); i != e; ++i) { 00351 assert(isValidArgumentType(Params[i]) && 00352 "Not a valid type for function argument!"); 00353 SubTys[i+1] = Params[i]; 00354 } 00355 00356 ContainedTys = SubTys; 00357 NumContainedTys = Params.size() + 1; // + 1 for result type 00358 } 00359 00360 // FunctionType::get - The factory function for the FunctionType class. 00361 FunctionType *FunctionType::get(Type *ReturnType, 00362 ArrayRef<Type*> Params, bool isVarArg) { 00363 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl; 00364 FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg); 00365 LLVMContextImpl::FunctionTypeMap::iterator I = 00366 pImpl->FunctionTypes.find_as(Key); 00367 FunctionType *FT; 00368 00369 if (I == pImpl->FunctionTypes.end()) { 00370 FT = (FunctionType*) pImpl->TypeAllocator. 00371 Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1), 00372 AlignOf<FunctionType>::Alignment); 00373 new (FT) FunctionType(ReturnType, Params, isVarArg); 00374 pImpl->FunctionTypes[FT] = true; 00375 } else { 00376 FT = I->first; 00377 } 00378 00379 return FT; 00380 } 00381 00382 FunctionType *FunctionType::get(Type *Result, bool isVarArg) { 00383 return get(Result, None, isVarArg); 00384 } 00385 00386 /// isValidReturnType - Return true if the specified type is valid as a return 00387 /// type. 00388 bool FunctionType::isValidReturnType(Type *RetTy) { 00389 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() && 00390 !RetTy->isMetadataTy(); 00391 } 00392 00393 /// isValidArgumentType - Return true if the specified type is valid as an 00394 /// argument type. 00395 bool FunctionType::isValidArgumentType(Type *ArgTy) { 00396 return ArgTy->isFirstClassType(); 00397 } 00398 00399 //===----------------------------------------------------------------------===// 00400 // StructType Implementation 00401 //===----------------------------------------------------------------------===// 00402 00403 // Primitive Constructors. 00404 00405 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes, 00406 bool isPacked) { 00407 LLVMContextImpl *pImpl = Context.pImpl; 00408 AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked); 00409 LLVMContextImpl::StructTypeMap::iterator I = 00410 pImpl->AnonStructTypes.find_as(Key); 00411 StructType *ST; 00412 00413 if (I == pImpl->AnonStructTypes.end()) { 00414 // Value not found. Create a new type! 00415 ST = new (Context.pImpl->TypeAllocator) StructType(Context); 00416 ST->setSubclassData(SCDB_IsLiteral); // Literal struct. 00417 ST->setBody(ETypes, isPacked); 00418 Context.pImpl->AnonStructTypes[ST] = true; 00419 } else { 00420 ST = I->first; 00421 } 00422 00423 return ST; 00424 } 00425 00426 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) { 00427 assert(isOpaque() && "Struct body already set!"); 00428 00429 setSubclassData(getSubclassData() | SCDB_HasBody); 00430 if (isPacked) 00431 setSubclassData(getSubclassData() | SCDB_Packed); 00432 00433 unsigned NumElements = Elements.size(); 00434 Type **Elts = getContext().pImpl->TypeAllocator.Allocate<Type*>(NumElements); 00435 memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements); 00436 00437 ContainedTys = Elts; 00438 NumContainedTys = NumElements; 00439 } 00440 00441 void StructType::setName(StringRef Name) { 00442 if (Name == getName()) return; 00443 00444 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes; 00445 typedef StringMap<StructType *>::MapEntryTy EntryTy; 00446 00447 // If this struct already had a name, remove its symbol table entry. Don't 00448 // delete the data yet because it may be part of the new name. 00449 if (SymbolTableEntry) 00450 SymbolTable.remove((EntryTy *)SymbolTableEntry); 00451 00452 // If this is just removing the name, we're done. 00453 if (Name.empty()) { 00454 if (SymbolTableEntry) { 00455 // Delete the old string data. 00456 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator()); 00457 SymbolTableEntry = 0; 00458 } 00459 return; 00460 } 00461 00462 // Look up the entry for the name. 00463 EntryTy *Entry = &getContext().pImpl->NamedStructTypes.GetOrCreateValue(Name); 00464 00465 // While we have a name collision, try a random rename. 00466 if (Entry->getValue()) { 00467 SmallString<64> TempStr(Name); 00468 TempStr.push_back('.'); 00469 raw_svector_ostream TmpStream(TempStr); 00470 unsigned NameSize = Name.size(); 00471 00472 do { 00473 TempStr.resize(NameSize + 1); 00474 TmpStream.resync(); 00475 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++; 00476 00477 Entry = &getContext().pImpl-> 00478 NamedStructTypes.GetOrCreateValue(TmpStream.str()); 00479 } while (Entry->getValue()); 00480 } 00481 00482 // Okay, we found an entry that isn't used. It's us! 00483 Entry->setValue(this); 00484 00485 // Delete the old string data. 00486 if (SymbolTableEntry) 00487 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator()); 00488 SymbolTableEntry = Entry; 00489 } 00490 00491 //===----------------------------------------------------------------------===// 00492 // StructType Helper functions. 00493 00494 StructType *StructType::create(LLVMContext &Context, StringRef Name) { 00495 StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context); 00496 if (!Name.empty()) 00497 ST->setName(Name); 00498 return ST; 00499 } 00500 00501 StructType *StructType::get(LLVMContext &Context, bool isPacked) { 00502 return get(Context, None, isPacked); 00503 } 00504 00505 StructType *StructType::get(Type *type, ...) { 00506 assert(type != 0 && "Cannot create a struct type with no elements with this"); 00507 LLVMContext &Ctx = type->getContext(); 00508 va_list ap; 00509 SmallVector<llvm::Type*, 8> StructFields; 00510 va_start(ap, type); 00511 while (type) { 00512 StructFields.push_back(type); 00513 type = va_arg(ap, llvm::Type*); 00514 } 00515 return llvm::StructType::get(Ctx, StructFields); 00516 } 00517 00518 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements, 00519 StringRef Name, bool isPacked) { 00520 StructType *ST = create(Context, Name); 00521 ST->setBody(Elements, isPacked); 00522 return ST; 00523 } 00524 00525 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) { 00526 return create(Context, Elements, StringRef()); 00527 } 00528 00529 StructType *StructType::create(LLVMContext &Context) { 00530 return create(Context, StringRef()); 00531 } 00532 00533 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name, 00534 bool isPacked) { 00535 assert(!Elements.empty() && 00536 "This method may not be invoked with an empty list"); 00537 return create(Elements[0]->getContext(), Elements, Name, isPacked); 00538 } 00539 00540 StructType *StructType::create(ArrayRef<Type*> Elements) { 00541 assert(!Elements.empty() && 00542 "This method may not be invoked with an empty list"); 00543 return create(Elements[0]->getContext(), Elements, StringRef()); 00544 } 00545 00546 StructType *StructType::create(StringRef Name, Type *type, ...) { 00547 assert(type != 0 && "Cannot create a struct type with no elements with this"); 00548 LLVMContext &Ctx = type->getContext(); 00549 va_list ap; 00550 SmallVector<llvm::Type*, 8> StructFields; 00551 va_start(ap, type); 00552 while (type) { 00553 StructFields.push_back(type); 00554 type = va_arg(ap, llvm::Type*); 00555 } 00556 return llvm::StructType::create(Ctx, StructFields, Name); 00557 } 00558 00559 bool StructType::isSized() const { 00560 if ((getSubclassData() & SCDB_IsSized) != 0) 00561 return true; 00562 if (isOpaque()) 00563 return false; 00564 00565 // Okay, our struct is sized if all of the elements are, but if one of the 00566 // elements is opaque, the struct isn't sized *yet*, but may become sized in 00567 // the future, so just bail out without caching. 00568 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I) 00569 if (!(*I)->isSized()) 00570 return false; 00571 00572 // Here we cheat a bit and cast away const-ness. The goal is to memoize when 00573 // we find a sized type, as types can only move from opaque to sized, not the 00574 // other way. 00575 const_cast<StructType*>(this)->setSubclassData( 00576 getSubclassData() | SCDB_IsSized); 00577 return true; 00578 } 00579 00580 StringRef StructType::getName() const { 00581 assert(!isLiteral() && "Literal structs never have names"); 00582 if (SymbolTableEntry == 0) return StringRef(); 00583 00584 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey(); 00585 } 00586 00587 void StructType::setBody(Type *type, ...) { 00588 assert(type != 0 && "Cannot create a struct type with no elements with this"); 00589 va_list ap; 00590 SmallVector<llvm::Type*, 8> StructFields; 00591 va_start(ap, type); 00592 while (type) { 00593 StructFields.push_back(type); 00594 type = va_arg(ap, llvm::Type*); 00595 } 00596 setBody(StructFields); 00597 } 00598 00599 bool StructType::isValidElementType(Type *ElemTy) { 00600 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 00601 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy(); 00602 } 00603 00604 /// isLayoutIdentical - Return true if this is layout identical to the 00605 /// specified struct. 00606 bool StructType::isLayoutIdentical(StructType *Other) const { 00607 if (this == Other) return true; 00608 00609 if (isPacked() != Other->isPacked() || 00610 getNumElements() != Other->getNumElements()) 00611 return false; 00612 00613 return std::equal(element_begin(), element_end(), Other->element_begin()); 00614 } 00615 00616 /// getTypeByName - Return the type with the specified name, or null if there 00617 /// is none by that name. 00618 StructType *Module::getTypeByName(StringRef Name) const { 00619 StringMap<StructType*>::iterator I = 00620 getContext().pImpl->NamedStructTypes.find(Name); 00621 if (I != getContext().pImpl->NamedStructTypes.end()) 00622 return I->second; 00623 return 0; 00624 } 00625 00626 00627 //===----------------------------------------------------------------------===// 00628 // CompositeType Implementation 00629 //===----------------------------------------------------------------------===// 00630 00631 Type *CompositeType::getTypeAtIndex(const Value *V) { 00632 if (StructType *STy = dyn_cast<StructType>(this)) { 00633 unsigned Idx = 00634 (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue(); 00635 assert(indexValid(Idx) && "Invalid structure index!"); 00636 return STy->getElementType(Idx); 00637 } 00638 00639 return cast<SequentialType>(this)->getElementType(); 00640 } 00641 Type *CompositeType::getTypeAtIndex(unsigned Idx) { 00642 if (StructType *STy = dyn_cast<StructType>(this)) { 00643 assert(indexValid(Idx) && "Invalid structure index!"); 00644 return STy->getElementType(Idx); 00645 } 00646 00647 return cast<SequentialType>(this)->getElementType(); 00648 } 00649 bool CompositeType::indexValid(const Value *V) const { 00650 if (const StructType *STy = dyn_cast<StructType>(this)) { 00651 // Structure indexes require (vectors of) 32-bit integer constants. In the 00652 // vector case all of the indices must be equal. 00653 if (!V->getType()->getScalarType()->isIntegerTy(32)) 00654 return false; 00655 const Constant *C = dyn_cast<Constant>(V); 00656 if (C && V->getType()->isVectorTy()) 00657 C = C->getSplatValue(); 00658 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C); 00659 return CU && CU->getZExtValue() < STy->getNumElements(); 00660 } 00661 00662 // Sequential types can be indexed by any integer. 00663 return V->getType()->isIntOrIntVectorTy(); 00664 } 00665 00666 bool CompositeType::indexValid(unsigned Idx) const { 00667 if (const StructType *STy = dyn_cast<StructType>(this)) 00668 return Idx < STy->getNumElements(); 00669 // Sequential types can be indexed by any integer. 00670 return true; 00671 } 00672 00673 00674 //===----------------------------------------------------------------------===// 00675 // ArrayType Implementation 00676 //===----------------------------------------------------------------------===// 00677 00678 ArrayType::ArrayType(Type *ElType, uint64_t NumEl) 00679 : SequentialType(ArrayTyID, ElType) { 00680 NumElements = NumEl; 00681 } 00682 00683 ArrayType *ArrayType::get(Type *elementType, uint64_t NumElements) { 00684 Type *ElementType = const_cast<Type*>(elementType); 00685 assert(isValidElementType(ElementType) && "Invalid type for array element!"); 00686 00687 LLVMContextImpl *pImpl = ElementType->getContext().pImpl; 00688 ArrayType *&Entry = 00689 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)]; 00690 00691 if (Entry == 0) 00692 Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements); 00693 return Entry; 00694 } 00695 00696 bool ArrayType::isValidElementType(Type *ElemTy) { 00697 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 00698 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy(); 00699 } 00700 00701 //===----------------------------------------------------------------------===// 00702 // VectorType Implementation 00703 //===----------------------------------------------------------------------===// 00704 00705 VectorType::VectorType(Type *ElType, unsigned NumEl) 00706 : SequentialType(VectorTyID, ElType) { 00707 NumElements = NumEl; 00708 } 00709 00710 VectorType *VectorType::get(Type *elementType, unsigned NumElements) { 00711 Type *ElementType = const_cast<Type*>(elementType); 00712 assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0"); 00713 assert(isValidElementType(ElementType) && 00714 "Elements of a VectorType must be a primitive type"); 00715 00716 LLVMContextImpl *pImpl = ElementType->getContext().pImpl; 00717 VectorType *&Entry = ElementType->getContext().pImpl 00718 ->VectorTypes[std::make_pair(ElementType, NumElements)]; 00719 00720 if (Entry == 0) 00721 Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements); 00722 return Entry; 00723 } 00724 00725 bool VectorType::isValidElementType(Type *ElemTy) { 00726 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() || 00727 ElemTy->isPointerTy(); 00728 } 00729 00730 //===----------------------------------------------------------------------===// 00731 // PointerType Implementation 00732 //===----------------------------------------------------------------------===// 00733 00734 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) { 00735 assert(EltTy && "Can't get a pointer to <null> type!"); 00736 assert(isValidElementType(EltTy) && "Invalid type for pointer element!"); 00737 00738 LLVMContextImpl *CImpl = EltTy->getContext().pImpl; 00739 00740 // Since AddressSpace #0 is the common case, we special case it. 00741 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy] 00742 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)]; 00743 00744 if (Entry == 0) 00745 Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace); 00746 return Entry; 00747 } 00748 00749 00750 PointerType::PointerType(Type *E, unsigned AddrSpace) 00751 : SequentialType(PointerTyID, E) { 00752 #ifndef NDEBUG 00753 const unsigned oldNCT = NumContainedTys; 00754 #endif 00755 setSubclassData(AddrSpace); 00756 // Check for miscompile. PR11652. 00757 assert(oldNCT == NumContainedTys && "bitfield written out of bounds?"); 00758 } 00759 00760 PointerType *Type::getPointerTo(unsigned addrs) { 00761 return PointerType::get(this, addrs); 00762 } 00763 00764 bool PointerType::isValidElementType(Type *ElemTy) { 00765 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 00766 !ElemTy->isMetadataTy(); 00767 }