clang API Documentation

CodeGenTypes.cpp
Go to the documentation of this file.
00001 //===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
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 is the code that handles AST -> LLVM type lowering.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "CodeGenTypes.h"
00015 #include "CGCall.h"
00016 #include "CGCXXABI.h"
00017 #include "CGRecordLayout.h"
00018 #include "TargetInfo.h"
00019 #include "clang/AST/ASTContext.h"
00020 #include "clang/AST/DeclObjC.h"
00021 #include "clang/AST/DeclCXX.h"
00022 #include "clang/AST/Expr.h"
00023 #include "clang/AST/RecordLayout.h"
00024 #include "llvm/DerivedTypes.h"
00025 #include "llvm/Module.h"
00026 #include "llvm/Target/TargetData.h"
00027 using namespace clang;
00028 using namespace CodeGen;
00029 
00030 CodeGenTypes::CodeGenTypes(CodeGenModule &CGM)
00031   : Context(CGM.getContext()), Target(Context.getTargetInfo()),
00032     TheModule(CGM.getModule()), TheTargetData(CGM.getTargetData()),
00033     TheABIInfo(CGM.getTargetCodeGenInfo().getABIInfo()),
00034     TheCXXABI(CGM.getCXXABI()),
00035     CodeGenOpts(CGM.getCodeGenOpts()), CGM(CGM) {
00036   SkippedLayout = false;
00037 }
00038 
00039 CodeGenTypes::~CodeGenTypes() {
00040   for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
00041          I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
00042       I != E; ++I)
00043     delete I->second;
00044 
00045   for (llvm::FoldingSet<CGFunctionInfo>::iterator
00046        I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
00047     delete &*I++;
00048 }
00049 
00050 void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
00051                                      llvm::StructType *Ty,
00052                                      StringRef suffix) {
00053   SmallString<256> TypeName;
00054   llvm::raw_svector_ostream OS(TypeName);
00055   OS << RD->getKindName() << '.';
00056   
00057   // Name the codegen type after the typedef name
00058   // if there is no tag type name available
00059   if (RD->getIdentifier()) {
00060     // FIXME: We should not have to check for a null decl context here.
00061     // Right now we do it because the implicit Obj-C decls don't have one.
00062     if (RD->getDeclContext())
00063       OS << RD->getQualifiedNameAsString();
00064     else
00065       RD->printName(OS);
00066   } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
00067     // FIXME: We should not have to check for a null decl context here.
00068     // Right now we do it because the implicit Obj-C decls don't have one.
00069     if (TDD->getDeclContext())
00070       OS << TDD->getQualifiedNameAsString();
00071     else
00072       TDD->printName(OS);
00073   } else
00074     OS << "anon";
00075 
00076   if (!suffix.empty())
00077     OS << suffix;
00078 
00079   Ty->setName(OS.str());
00080 }
00081 
00082 /// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
00083 /// ConvertType in that it is used to convert to the memory representation for
00084 /// a type.  For example, the scalar representation for _Bool is i1, but the
00085 /// memory representation is usually i8 or i32, depending on the target.
00086 llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){
00087   llvm::Type *R = ConvertType(T);
00088 
00089   // If this is a non-bool type, don't map it.
00090   if (!R->isIntegerTy(1))
00091     return R;
00092 
00093   // Otherwise, return an integer of the target-specified size.
00094   return llvm::IntegerType::get(getLLVMContext(),
00095                                 (unsigned)Context.getTypeSize(T));
00096 }
00097 
00098 
00099 /// isRecordLayoutComplete - Return true if the specified type is already
00100 /// completely laid out.
00101 bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
00102   llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I = 
00103   RecordDeclTypes.find(Ty);
00104   return I != RecordDeclTypes.end() && !I->second->isOpaque();
00105 }
00106 
00107 static bool
00108 isSafeToConvert(QualType T, CodeGenTypes &CGT,
00109                 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
00110 
00111 
00112 /// isSafeToConvert - Return true if it is safe to convert the specified record
00113 /// decl to IR and lay it out, false if doing so would cause us to get into a
00114 /// recursive compilation mess.
00115 static bool 
00116 isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
00117                 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
00118   // If we have already checked this type (maybe the same type is used by-value
00119   // multiple times in multiple structure fields, don't check again.
00120   if (!AlreadyChecked.insert(RD)) return true;
00121   
00122   const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
00123   
00124   // If this type is already laid out, converting it is a noop.
00125   if (CGT.isRecordLayoutComplete(Key)) return true;
00126   
00127   // If this type is currently being laid out, we can't recursively compile it.
00128   if (CGT.isRecordBeingLaidOut(Key))
00129     return false;
00130   
00131   // If this type would require laying out bases that are currently being laid
00132   // out, don't do it.  This includes virtual base classes which get laid out
00133   // when a class is translated, even though they aren't embedded by-value into
00134   // the class.
00135   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
00136     for (CXXRecordDecl::base_class_const_iterator I = CRD->bases_begin(),
00137          E = CRD->bases_end(); I != E; ++I)
00138       if (!isSafeToConvert(I->getType()->getAs<RecordType>()->getDecl(),
00139                            CGT, AlreadyChecked))
00140         return false;
00141   }
00142   
00143   // If this type would require laying out members that are currently being laid
00144   // out, don't do it.
00145   for (RecordDecl::field_iterator I = RD->field_begin(),
00146        E = RD->field_end(); I != E; ++I)
00147     if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
00148       return false;
00149   
00150   // If there are no problems, lets do it.
00151   return true;
00152 }
00153 
00154 /// isSafeToConvert - Return true if it is safe to convert this field type,
00155 /// which requires the structure elements contained by-value to all be
00156 /// recursively safe to convert.
00157 static bool
00158 isSafeToConvert(QualType T, CodeGenTypes &CGT,
00159                 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
00160   T = T.getCanonicalType();
00161   
00162   // If this is a record, check it.
00163   if (const RecordType *RT = dyn_cast<RecordType>(T))
00164     return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
00165   
00166   // If this is an array, check the elements, which are embedded inline.
00167   if (const ArrayType *AT = dyn_cast<ArrayType>(T))
00168     return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
00169 
00170   // Otherwise, there is no concern about transforming this.  We only care about
00171   // things that are contained by-value in a structure that can have another 
00172   // structure as a member.
00173   return true;
00174 }
00175 
00176 
00177 /// isSafeToConvert - Return true if it is safe to convert the specified record
00178 /// decl to IR and lay it out, false if doing so would cause us to get into a
00179 /// recursive compilation mess.
00180 static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
00181   // If no structs are being laid out, we can certainly do this one.
00182   if (CGT.noRecordsBeingLaidOut()) return true;
00183   
00184   llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
00185   return isSafeToConvert(RD, CGT, AlreadyChecked);
00186 }
00187 
00188 
00189 /// isFuncTypeArgumentConvertible - Return true if the specified type in a 
00190 /// function argument or result position can be converted to an IR type at this
00191 /// point.  This boils down to being whether it is complete, as well as whether
00192 /// we've temporarily deferred expanding the type because we're in a recursive
00193 /// context.
00194 bool CodeGenTypes::isFuncTypeArgumentConvertible(QualType Ty) {
00195   // If this isn't a tagged type, we can convert it!
00196   const TagType *TT = Ty->getAs<TagType>();
00197   if (TT == 0) return true;
00198     
00199   // Incomplete types cannot be converted.
00200   if (TT->isIncompleteType())
00201     return false;
00202   
00203   // If this is an enum, then it is always safe to convert.
00204   const RecordType *RT = dyn_cast<RecordType>(TT);
00205   if (RT == 0) return true;
00206 
00207   // Otherwise, we have to be careful.  If it is a struct that we're in the
00208   // process of expanding, then we can't convert the function type.  That's ok
00209   // though because we must be in a pointer context under the struct, so we can
00210   // just convert it to a dummy type.
00211   //
00212   // We decide this by checking whether ConvertRecordDeclType returns us an
00213   // opaque type for a struct that we know is defined.
00214   return isSafeToConvert(RT->getDecl(), *this);
00215 }
00216 
00217 
00218 /// Code to verify a given function type is complete, i.e. the return type
00219 /// and all of the argument types are complete.  Also check to see if we are in
00220 /// a RS_StructPointer context, and if so whether any struct types have been
00221 /// pended.  If so, we don't want to ask the ABI lowering code to handle a type
00222 /// that cannot be converted to an IR type.
00223 bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
00224   if (!isFuncTypeArgumentConvertible(FT->getResultType()))
00225     return false;
00226   
00227   if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
00228     for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
00229       if (!isFuncTypeArgumentConvertible(FPT->getArgType(i)))
00230         return false;
00231 
00232   return true;
00233 }
00234 
00235 /// UpdateCompletedType - When we find the full definition for a TagDecl,
00236 /// replace the 'opaque' type we previously made for it if applicable.
00237 void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
00238   // If this is an enum being completed, then we flush all non-struct types from
00239   // the cache.  This allows function types and other things that may be derived
00240   // from the enum to be recomputed.
00241   if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
00242     // Only flush the cache if we've actually already converted this type.
00243     if (TypeCache.count(ED->getTypeForDecl())) {
00244       // Okay, we formed some types based on this.  We speculated that the enum
00245       // would be lowered to i32, so we only need to flush the cache if this
00246       // didn't happen.
00247       if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
00248         TypeCache.clear();
00249     }
00250     return;
00251   }
00252   
00253   // If we completed a RecordDecl that we previously used and converted to an
00254   // anonymous type, then go ahead and complete it now.
00255   const RecordDecl *RD = cast<RecordDecl>(TD);
00256   if (RD->isDependentType()) return;
00257 
00258   // Only complete it if we converted it already.  If we haven't converted it
00259   // yet, we'll just do it lazily.
00260   if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
00261     ConvertRecordDeclType(RD);
00262 }
00263 
00264 static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
00265                                     const llvm::fltSemantics &format) {
00266   if (&format == &llvm::APFloat::IEEEhalf)
00267     return llvm::Type::getInt16Ty(VMContext);
00268   if (&format == &llvm::APFloat::IEEEsingle)
00269     return llvm::Type::getFloatTy(VMContext);
00270   if (&format == &llvm::APFloat::IEEEdouble)
00271     return llvm::Type::getDoubleTy(VMContext);
00272   if (&format == &llvm::APFloat::IEEEquad)
00273     return llvm::Type::getFP128Ty(VMContext);
00274   if (&format == &llvm::APFloat::PPCDoubleDouble)
00275     return llvm::Type::getPPC_FP128Ty(VMContext);
00276   if (&format == &llvm::APFloat::x87DoubleExtended)
00277     return llvm::Type::getX86_FP80Ty(VMContext);
00278   llvm_unreachable("Unknown float format!");
00279 }
00280 
00281 /// ConvertType - Convert the specified type to its LLVM form.
00282 llvm::Type *CodeGenTypes::ConvertType(QualType T) {
00283   T = Context.getCanonicalType(T);
00284 
00285   const Type *Ty = T.getTypePtr();
00286 
00287   // RecordTypes are cached and processed specially.
00288   if (const RecordType *RT = dyn_cast<RecordType>(Ty))
00289     return ConvertRecordDeclType(RT->getDecl());
00290   
00291   // See if type is already cached.
00292   llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
00293   // If type is found in map then use it. Otherwise, convert type T.
00294   if (TCI != TypeCache.end())
00295     return TCI->second;
00296 
00297   // If we don't have it in the cache, convert it now.
00298   llvm::Type *ResultType = 0;
00299   switch (Ty->getTypeClass()) {
00300   case Type::Record: // Handled above.
00301 #define TYPE(Class, Base)
00302 #define ABSTRACT_TYPE(Class, Base)
00303 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
00304 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
00305 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
00306 #include "clang/AST/TypeNodes.def"
00307     llvm_unreachable("Non-canonical or dependent types aren't possible.");
00308 
00309   case Type::Builtin: {
00310     switch (cast<BuiltinType>(Ty)->getKind()) {
00311     case BuiltinType::Void:
00312     case BuiltinType::ObjCId:
00313     case BuiltinType::ObjCClass:
00314     case BuiltinType::ObjCSel:
00315       // LLVM void type can only be used as the result of a function call.  Just
00316       // map to the same as char.
00317       ResultType = llvm::Type::getInt8Ty(getLLVMContext());
00318       break;
00319 
00320     case BuiltinType::Bool:
00321       // Note that we always return bool as i1 for use as a scalar type.
00322       ResultType = llvm::Type::getInt1Ty(getLLVMContext());
00323       break;
00324 
00325     case BuiltinType::Char_S:
00326     case BuiltinType::Char_U:
00327     case BuiltinType::SChar:
00328     case BuiltinType::UChar:
00329     case BuiltinType::Short:
00330     case BuiltinType::UShort:
00331     case BuiltinType::Int:
00332     case BuiltinType::UInt:
00333     case BuiltinType::Long:
00334     case BuiltinType::ULong:
00335     case BuiltinType::LongLong:
00336     case BuiltinType::ULongLong:
00337     case BuiltinType::WChar_S:
00338     case BuiltinType::WChar_U:
00339     case BuiltinType::Char16:
00340     case BuiltinType::Char32:
00341       ResultType = llvm::IntegerType::get(getLLVMContext(),
00342                                  static_cast<unsigned>(Context.getTypeSize(T)));
00343       break;
00344 
00345     case BuiltinType::Half:
00346       // Half is special: it might be lowered to i16 (and will be storage-only
00347       // type),. or can be represented as a set of native operations.
00348 
00349       // FIXME: Ask target which kind of half FP it prefers (storage only vs
00350       // native).
00351       ResultType = llvm::Type::getInt16Ty(getLLVMContext());
00352       break;
00353     case BuiltinType::Float:
00354     case BuiltinType::Double:
00355     case BuiltinType::LongDouble:
00356       ResultType = getTypeForFormat(getLLVMContext(),
00357                                     Context.getFloatTypeSemantics(T));
00358       break;
00359 
00360     case BuiltinType::NullPtr:
00361       // Model std::nullptr_t as i8*
00362       ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
00363       break;
00364         
00365     case BuiltinType::UInt128:
00366     case BuiltinType::Int128:
00367       ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
00368       break;
00369     
00370     case BuiltinType::Dependent:
00371 #define BUILTIN_TYPE(Id, SingletonId)
00372 #define PLACEHOLDER_TYPE(Id, SingletonId) \
00373     case BuiltinType::Id:
00374 #include "clang/AST/BuiltinTypes.def"
00375       llvm_unreachable("Unexpected placeholder builtin type!");
00376     }
00377     break;
00378   }
00379   case Type::Complex: {
00380     llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
00381     ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
00382     break;
00383   }
00384   case Type::LValueReference:
00385   case Type::RValueReference: {
00386     const ReferenceType *RTy = cast<ReferenceType>(Ty);
00387     QualType ETy = RTy->getPointeeType();
00388     llvm::Type *PointeeType = ConvertTypeForMem(ETy);
00389     unsigned AS = Context.getTargetAddressSpace(ETy);
00390     ResultType = llvm::PointerType::get(PointeeType, AS);
00391     break;
00392   }
00393   case Type::Pointer: {
00394     const PointerType *PTy = cast<PointerType>(Ty);
00395     QualType ETy = PTy->getPointeeType();
00396     llvm::Type *PointeeType = ConvertTypeForMem(ETy);
00397     if (PointeeType->isVoidTy())
00398       PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
00399     unsigned AS = Context.getTargetAddressSpace(ETy);
00400     ResultType = llvm::PointerType::get(PointeeType, AS);
00401     break;
00402   }
00403 
00404   case Type::VariableArray: {
00405     const VariableArrayType *A = cast<VariableArrayType>(Ty);
00406     assert(A->getIndexTypeCVRQualifiers() == 0 &&
00407            "FIXME: We only handle trivial array types so far!");
00408     // VLAs resolve to the innermost element type; this matches
00409     // the return of alloca, and there isn't any obviously better choice.
00410     ResultType = ConvertTypeForMem(A->getElementType());
00411     break;
00412   }
00413   case Type::IncompleteArray: {
00414     const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
00415     assert(A->getIndexTypeCVRQualifiers() == 0 &&
00416            "FIXME: We only handle trivial array types so far!");
00417     // int X[] -> [0 x int], unless the element type is not sized.  If it is
00418     // unsized (e.g. an incomplete struct) just use [0 x i8].
00419     ResultType = ConvertTypeForMem(A->getElementType());
00420     if (!ResultType->isSized()) {
00421       SkippedLayout = true;
00422       ResultType = llvm::Type::getInt8Ty(getLLVMContext());
00423     }
00424     ResultType = llvm::ArrayType::get(ResultType, 0);
00425     break;
00426   }
00427   case Type::ConstantArray: {
00428     const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
00429     llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
00430     
00431     // Lower arrays of undefined struct type to arrays of i8 just to have a 
00432     // concrete type.
00433     if (!EltTy->isSized()) {
00434       SkippedLayout = true;
00435       EltTy = llvm::Type::getInt8Ty(getLLVMContext());
00436     }
00437 
00438     ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
00439     break;
00440   }
00441   case Type::ExtVector:
00442   case Type::Vector: {
00443     const VectorType *VT = cast<VectorType>(Ty);
00444     ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
00445                                        VT->getNumElements());
00446     break;
00447   }
00448   case Type::FunctionNoProto:
00449   case Type::FunctionProto: {
00450     const FunctionType *FT = cast<FunctionType>(Ty);
00451     // First, check whether we can build the full function type.  If the
00452     // function type depends on an incomplete type (e.g. a struct or enum), we
00453     // cannot lower the function type.
00454     if (!isFuncTypeConvertible(FT)) {
00455       // This function's type depends on an incomplete tag type.
00456       // Return a placeholder type.
00457       ResultType = llvm::StructType::get(getLLVMContext());
00458       
00459       SkippedLayout = true;
00460       break;
00461     }
00462 
00463     // While we're converting the argument types for a function, we don't want
00464     // to recursively convert any pointed-to structs.  Converting directly-used
00465     // structs is ok though.
00466     if (!RecordsBeingLaidOut.insert(Ty)) {
00467       ResultType = llvm::StructType::get(getLLVMContext());
00468       
00469       SkippedLayout = true;
00470       break;
00471     }
00472     
00473     // The function type can be built; call the appropriate routines to
00474     // build it.
00475     const CGFunctionInfo *FI;
00476     if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
00477       FI = &arrangeFunctionType(
00478                    CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
00479     } else {
00480       const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
00481       FI = &arrangeFunctionType(
00482                 CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
00483     }
00484     
00485     // If there is something higher level prodding our CGFunctionInfo, then
00486     // don't recurse into it again.
00487     if (FunctionsBeingProcessed.count(FI)) {
00488 
00489       ResultType = llvm::StructType::get(getLLVMContext());
00490       SkippedLayout = true;
00491     } else {
00492 
00493       // Otherwise, we're good to go, go ahead and convert it.
00494       ResultType = GetFunctionType(*FI);
00495     }
00496 
00497     RecordsBeingLaidOut.erase(Ty);
00498 
00499     if (SkippedLayout)
00500       TypeCache.clear();
00501     
00502     if (RecordsBeingLaidOut.empty())
00503       while (!DeferredRecords.empty())
00504         ConvertRecordDeclType(DeferredRecords.pop_back_val());
00505     break;
00506   }
00507 
00508   case Type::ObjCObject:
00509     ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
00510     break;
00511 
00512   case Type::ObjCInterface: {
00513     // Objective-C interfaces are always opaque (outside of the
00514     // runtime, which can do whatever it likes); we never refine
00515     // these.
00516     llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
00517     if (!T)
00518       T = llvm::StructType::create(getLLVMContext());
00519     ResultType = T;
00520     break;
00521   }
00522 
00523   case Type::ObjCObjectPointer: {
00524     // Protocol qualifications do not influence the LLVM type, we just return a
00525     // pointer to the underlying interface type. We don't need to worry about
00526     // recursive conversion.
00527     llvm::Type *T =
00528       ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
00529     ResultType = T->getPointerTo();
00530     break;
00531   }
00532 
00533   case Type::Enum: {
00534     const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
00535     if (ED->isCompleteDefinition() || ED->isFixed())
00536       return ConvertType(ED->getIntegerType());
00537     // Return a placeholder 'i32' type.  This can be changed later when the
00538     // type is defined (see UpdateCompletedType), but is likely to be the
00539     // "right" answer.
00540     ResultType = llvm::Type::getInt32Ty(getLLVMContext());
00541     break;
00542   }
00543 
00544   case Type::BlockPointer: {
00545     const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
00546     llvm::Type *PointeeType = ConvertTypeForMem(FTy);
00547     unsigned AS = Context.getTargetAddressSpace(FTy);
00548     ResultType = llvm::PointerType::get(PointeeType, AS);
00549     break;
00550   }
00551 
00552   case Type::MemberPointer: {
00553     ResultType = 
00554       getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
00555     break;
00556   }
00557 
00558   case Type::Atomic: {
00559     ResultType = ConvertType(cast<AtomicType>(Ty)->getValueType());
00560     break;
00561   }
00562   }
00563   
00564   assert(ResultType && "Didn't convert a type?");
00565   
00566   TypeCache[Ty] = ResultType;
00567   return ResultType;
00568 }
00569 
00570 /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
00571 llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
00572   // TagDecl's are not necessarily unique, instead use the (clang)
00573   // type connected to the decl.
00574   const Type *Key = Context.getTagDeclType(RD).getTypePtr();
00575 
00576   llvm::StructType *&Entry = RecordDeclTypes[Key];
00577 
00578   // If we don't have a StructType at all yet, create the forward declaration.
00579   if (Entry == 0) {
00580     Entry = llvm::StructType::create(getLLVMContext());
00581     addRecordTypeName(RD, Entry, "");
00582   }
00583   llvm::StructType *Ty = Entry;
00584 
00585   // If this is still a forward declaration, or the LLVM type is already
00586   // complete, there's nothing more to do.
00587   RD = RD->getDefinition();
00588   if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque())
00589     return Ty;
00590   
00591   // If converting this type would cause us to infinitely loop, don't do it!
00592   if (!isSafeToConvert(RD, *this)) {
00593     DeferredRecords.push_back(RD);
00594     return Ty;
00595   }
00596 
00597   // Okay, this is a definition of a type.  Compile the implementation now.
00598   bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
00599   assert(InsertResult && "Recursively compiling a struct?");
00600   
00601   // Force conversion of non-virtual base classes recursively.
00602   if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
00603     for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
00604          e = CRD->bases_end(); i != e; ++i) {
00605       if (i->isVirtual()) continue;
00606       
00607       ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
00608     }
00609   }
00610 
00611   // Layout fields.
00612   CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
00613   CGRecordLayouts[Key] = Layout;
00614 
00615   // We're done laying out this struct.
00616   bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
00617   assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
00618    
00619   // If this struct blocked a FunctionType conversion, then recompute whatever
00620   // was derived from that.
00621   // FIXME: This is hugely overconservative.
00622   if (SkippedLayout)
00623     TypeCache.clear();
00624     
00625   // If we're done converting the outer-most record, then convert any deferred
00626   // structs as well.
00627   if (RecordsBeingLaidOut.empty())
00628     while (!DeferredRecords.empty())
00629       ConvertRecordDeclType(DeferredRecords.pop_back_val());
00630 
00631   return Ty;
00632 }
00633 
00634 /// getCGRecordLayout - Return record layout info for the given record decl.
00635 const CGRecordLayout &
00636 CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
00637   const Type *Key = Context.getTagDeclType(RD).getTypePtr();
00638 
00639   const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
00640   if (!Layout) {
00641     // Compute the type information.
00642     ConvertRecordDeclType(RD);
00643 
00644     // Now try again.
00645     Layout = CGRecordLayouts.lookup(Key);
00646   }
00647 
00648   assert(Layout && "Unable to find record layout information for type");
00649   return *Layout;
00650 }
00651 
00652 bool CodeGenTypes::isZeroInitializable(QualType T) {
00653   // No need to check for member pointers when not compiling C++.
00654   if (!Context.getLangOpts().CPlusPlus)
00655     return true;
00656   
00657   T = Context.getBaseElementType(T);
00658   
00659   // Records are non-zero-initializable if they contain any
00660   // non-zero-initializable subobjects.
00661   if (const RecordType *RT = T->getAs<RecordType>()) {
00662     const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
00663     return isZeroInitializable(RD);
00664   }
00665 
00666   // We have to ask the ABI about member pointers.
00667   if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
00668     return getCXXABI().isZeroInitializable(MPT);
00669   
00670   // Everything else is okay.
00671   return true;
00672 }
00673 
00674 bool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
00675   return getCGRecordLayout(RD).isZeroInitializable();
00676 }