CodeGenTypes.cpp revision eab6a36cf1eed9a7fec0babd2206048e28dedd02
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// This is the code that handles AST -> LLVM type lowering. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenTypes.h" 15f16aa103d3afd42fbca2ab346f191bf745cec092John McCall#include "CGCXXABI.h" 1655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CGCall.h" 17b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei#include "CGOpenCLRuntime.h" 182924ade97ee4228fcf3518d89cd4bd1653236b48Daniel Dunbar#include "CGRecordLayout.h" 19de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall#include "TargetInfo.h" 20de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h" 21742cd1b7bb86b52b23b335d47abbd842dac0e1bfFariborz Jahanian#include "clang/AST/DeclCXX.h" 2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/DeclObjC.h" 23de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/Expr.h" 2419cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h" 253b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h" 263b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DerivedTypes.h" 273b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Module.h" 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3164aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCallCodeGenTypes::CodeGenTypes(CodeGenModule &cgm) 3264aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()), 3364aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall TheDataLayout(cgm.getDataLayout()), 3464aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()), 3564aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall CodeGenOpts(cgm.getCodeGenOpts()), 3664aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) { 3757eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner SkippedLayout = false; 38d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner} 395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 40b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang PatelCodeGenTypes::~CodeGenTypes() { 411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator 421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump I = CGRecordLayouts.begin(), E = CGRecordLayouts.end(); 43b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel I != E; ++I) 44b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel delete I->second; 456f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner 466f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner for (llvm::FoldingSet<CGFunctionInfo>::iterator 476f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; ) 486f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner delete &*I++; 49b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel} 50b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel 519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnervoid CodeGenTypes::addRecordTypeName(const RecordDecl *RD, 529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::StructType *Ty, 535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef suffix) { 54f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith SmallString<256> TypeName; 55e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson llvm::raw_svector_ostream OS(TypeName); 56e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson OS << RD->getKindName() << '.'; 57e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson 58e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // Name the codegen type after the typedef name 59e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // if there is no tag type name available 60e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson if (RD->getIdentifier()) { 61e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // FIXME: We should not have to check for a null decl context here. 62e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // Right now we do it because the implicit Obj-C decls don't have one. 63e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson if (RD->getDeclContext()) 64b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer RD->printQualifiedName(OS); 65e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson else 66e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson RD->printName(OS); 67e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) { 68e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // FIXME: We should not have to check for a null decl context here. 69e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // Right now we do it because the implicit Obj-C decls don't have one. 70e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson if (TDD->getDeclContext()) 71b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer TDD->printQualifiedName(OS); 72e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson else 73e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson TDD->printName(OS); 74e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson } else 75e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson OS << "anon"; 76e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson 77e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson if (!suffix.empty()) 78e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson OS << suffix; 79e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson 809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner Ty->setName(OS.str()); 8130ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel} 8230ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel 834581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 844581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertType in that it is used to convert to the memory representation for 854581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// a type. For example, the scalar representation for _Bool is i1, but the 864581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// memory representation is usually i8 or i32, depending on the target. 879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){ 889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *R = ConvertType(T); 891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9019009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner // If this is a non-bool type, don't map it. 91f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands if (!R->isIntegerTy(1)) 9219009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner return R; 931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9419009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner // Otherwise, return an integer of the target-specified size. 950032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::IntegerType::get(getLLVMContext(), 960032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson (unsigned)Context.getTypeSize(T)); 9719009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner} 9819009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner 9971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 10071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isRecordLayoutComplete - Return true if the specified type is already 10171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// completely laid out. 10271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerbool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const { 10371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I = 10471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner RecordDeclTypes.find(Ty); 10571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return I != RecordDeclTypes.end() && !I->second->isOpaque(); 10671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner} 10771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 10871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool 10971305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(QualType T, CodeGenTypes &CGT, 11071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked); 11171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 11271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 11371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert the specified record 11471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// decl to IR and lay it out, false if doing so would cause us to get into a 11571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursive compilation mess. 11671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool 11771305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT, 11871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { 11971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If we have already checked this type (maybe the same type is used by-value 12071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // multiple times in multiple structure fields, don't check again. 12171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!AlreadyChecked.insert(RD)) return true; 12271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 12371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr(); 12471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 12571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this type is already laid out, converting it is a noop. 12671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (CGT.isRecordLayoutComplete(Key)) return true; 12771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 12871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this type is currently being laid out, we can't recursively compile it. 12971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (CGT.isRecordBeingLaidOut(Key)) 13071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return false; 13171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 13271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this type would require laying out bases that are currently being laid 13371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // out, don't do it. This includes virtual base classes which get laid out 13471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // when a class is translated, even though they aren't embedded by-value into 13571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // the class. 13671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 13771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner for (CXXRecordDecl::base_class_const_iterator I = CRD->bases_begin(), 13871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner E = CRD->bases_end(); I != E; ++I) 13971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!isSafeToConvert(I->getType()->getAs<RecordType>()->getDecl(), 14071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner CGT, AlreadyChecked)) 14171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return false; 14271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner } 14371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 14471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this type would require laying out members that are currently being laid 14571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // out, don't do it. 14671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner for (RecordDecl::field_iterator I = RD->field_begin(), 14771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner E = RD->field_end(); I != E; ++I) 14871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked)) 14971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return false; 15071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 15171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If there are no problems, lets do it. 15271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return true; 15371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner} 15471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 15571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert this field type, 15671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// which requires the structure elements contained by-value to all be 15771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursively safe to convert. 15871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool 15971305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(QualType T, CodeGenTypes &CGT, 16071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { 16171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner T = T.getCanonicalType(); 16271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 16371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this is a record, check it. 16471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (const RecordType *RT = dyn_cast<RecordType>(T)) 16571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked); 16671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 16771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this is an array, check the elements, which are embedded inline. 16871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (const ArrayType *AT = dyn_cast<ArrayType>(T)) 16971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked); 17071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 17171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // Otherwise, there is no concern about transforming this. We only care about 17271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // things that are contained by-value in a structure that can have another 17371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // structure as a member. 17471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return true; 17571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner} 17671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 17771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 17871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert the specified record 17971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// decl to IR and lay it out, false if doing so would cause us to get into a 18071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursive compilation mess. 18171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) { 18271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If no structs are being laid out, we can certainly do this one. 18371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (CGT.noRecordsBeingLaidOut()) return true; 18471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 18571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked; 18671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return isSafeToConvert(RD, CGT, AlreadyChecked); 18771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner} 18871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 18971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 190f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// isFuncTypeArgumentConvertible - Return true if the specified type in a 191f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// function argument or result position can be converted to an IR type at this 192f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// point. This boils down to being whether it is complete, as well as whether 193f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// we've temporarily deferred expanding the type because we're in a recursive 194f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// context. 19571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerbool CodeGenTypes::isFuncTypeArgumentConvertible(QualType Ty) { 196f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // If this isn't a tagged type, we can convert it! 197f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner const TagType *TT = Ty->getAs<TagType>(); 198f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (TT == 0) return true; 199bcf38f2782d9cada139e22aa1ab8df10c9b047e5Douglas Gregor 200bcf38f2782d9cada139e22aa1ab8df10c9b047e5Douglas Gregor // Incomplete types cannot be converted. 2014a59bc26b3f2d00055e24332c3164c894fafac27Douglas Gregor if (TT->isIncompleteType()) 202f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner return false; 203f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 20471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this is an enum, then it is always safe to convert. 205f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner const RecordType *RT = dyn_cast<RecordType>(TT); 206f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (RT == 0) return true; 207f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 208f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // Otherwise, we have to be careful. If it is a struct that we're in the 209f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // process of expanding, then we can't convert the function type. That's ok 210f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // though because we must be in a pointer context under the struct, so we can 211f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // just convert it to a dummy type. 212f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // 213f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // We decide this by checking whether ConvertRecordDeclType returns us an 214f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // opaque type for a struct that we know is defined. 21571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return isSafeToConvert(RT->getDecl(), *this); 216f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner} 217f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 218f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 219f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// Code to verify a given function type is complete, i.e. the return type 220f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// and all of the argument types are complete. Also check to see if we are in 221f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// a RS_StructPointer context, and if so whether any struct types have been 222f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// pended. If so, we don't want to ask the ABI lowering code to handle a type 223f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// that cannot be converted to an IR type. 224f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattnerbool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) { 225f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (!isFuncTypeArgumentConvertible(FT->getResultType())) 226f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner return false; 227f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 228f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) 2299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++) 230f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (!isFuncTypeArgumentConvertible(FPT->getArgType(i))) 231f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner return false; 232f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 233f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner return true; 234b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman} 235b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman 236c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// UpdateCompletedType - When we find the full definition for a TagDecl, 237c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// replace the 'opaque' type we previously made for it if applicable. 238c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { 2399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If this is an enum being completed, then we flush all non-struct types from 2409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // the cache. This allows function types and other things that may be derived 2419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // from the enum to be recomputed. 2428dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) { 2438dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner // Only flush the cache if we've actually already converted this type. 2442045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner if (TypeCache.count(ED->getTypeForDecl())) { 2452045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // Okay, we formed some types based on this. We speculated that the enum 2462045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // would be lowered to i32, so we only need to flush the cache if this 2472045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // didn't happen. 2482045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner if (!ConvertType(ED->getIntegerType())->isIntegerTy(32)) 2492045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner TypeCache.clear(); 2502045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner } 2519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return; 252b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman } 2539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 254f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // If we completed a RecordDecl that we previously used and converted to an 255f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // anonymous type, then go ahead and complete it now. 2569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const RecordDecl *RD = cast<RecordDecl>(TD); 257f0a8679b6e6635117533b89894646f1450cea25bChris Lattner if (RD->isDependentType()) return; 258f0a8679b6e6635117533b89894646f1450cea25bChris Lattner 259f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // Only complete it if we converted it already. If we haven't converted it 260f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // yet, we'll just do it lazily. 2613ade97504790bbc5a148baa5b3f7e577aed9c1c6Chris Lattner if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr())) 2629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ConvertRecordDeclType(RD); 263eab6a36cf1eed9a7fec0babd2206048e28dedd02David Blaikie 264eab6a36cf1eed9a7fec0babd2206048e28dedd02David Blaikie // If necessary, provide the full definition of a type only used with a 265eab6a36cf1eed9a7fec0babd2206048e28dedd02David Blaikie // declaration so far. 266eab6a36cf1eed9a7fec0babd2206048e28dedd02David Blaikie if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) 267eab6a36cf1eed9a7fec0babd2206048e28dedd02David Blaikie DI->completeFwdDecls(RD); 268d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner} 269d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 2709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext, 27119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly const llvm::fltSemantics &format, 27219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly bool UseNativeHalf = false) { 27319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly if (&format == &llvm::APFloat::IEEEhalf) { 27419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly if (UseNativeHalf) 27519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly return llvm::Type::getHalfTy(VMContext); 27619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly else 27719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly return llvm::Type::getInt16Ty(VMContext); 27819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly } 279b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::IEEEsingle) 2800032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getFloatTy(VMContext); 281b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::IEEEdouble) 2820032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getDoubleTy(VMContext); 283b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::IEEEquad) 2840032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getFP128Ty(VMContext); 285b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::PPCDoubleDouble) 2860032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getPPC_FP128Ty(VMContext); 287b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::x87DoubleExtended) 2880032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getX86_FP80Ty(VMContext); 289b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Unknown float format!"); 290f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman} 291f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman 2929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertType - Convert the specified type to its LLVM form. 2939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertType(QualType T) { 2949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner T = Context.getCanonicalType(T); 2959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 2969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const Type *Ty = T.getTypePtr(); 2971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // RecordTypes are cached and processed specially. 2999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 3009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return ConvertRecordDeclType(RT->getDecl()); 3019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 3029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // See if type is already cached. 3039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty); 3049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If type is found in map then use it. Otherwise, convert type T. 3059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner if (TCI != TypeCache.end()) 3069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return TCI->second; 3079cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 3089cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If we don't have it in the cache, convert it now. 3099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *ResultType = 0; 3109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner switch (Ty->getTypeClass()) { 3119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner case Type::Record: // Handled above. 31272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base) 31372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 31472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 31572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class: 316ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 31772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def" 318864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall llvm_unreachable("Non-canonical or dependent types aren't possible."); 31972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::Builtin: { 3219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner switch (cast<BuiltinType>(Ty)->getKind()) { 3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Void: 323de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff case BuiltinType::ObjCId: 324de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff case BuiltinType::ObjCClass: 32513dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian case BuiltinType::ObjCSel: 3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // LLVM void type can only be used as the result of a function call. Just 3275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // map to the same as char. 3289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::Type::getInt8Ty(getLLVMContext()); 3299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Bool: 33219009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner // Note that we always return bool as i1 for use as a scalar type. 3339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::Type::getInt1Ty(getLLVMContext()); 3349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 3351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 336d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::Char_S: 337d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::Char_U: 338d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::SChar: 339d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::UChar: 3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Short: 3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::UShort: 3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Int: 3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::UInt: 3445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Long: 3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::ULong: 3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::LongLong: 3475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::ULongLong: 3483f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner case BuiltinType::WChar_S: 3493f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner case BuiltinType::WChar_U: 350f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case BuiltinType::Char16: 351f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case BuiltinType::Char32: 3529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::IntegerType::get(getLLVMContext(), 3539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner static_cast<unsigned>(Context.getTypeSize(T))); 3549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 3551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 356aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov case BuiltinType::Half: 35719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly // Half FP can either be storage-only (lowered to i16) or native. 35819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly ResultType = getTypeForFormat(getLLVMContext(), 35919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly Context.getFloatTypeSemantics(T), 36019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly Context.getLangOpts().NativeHalfType); 361aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov break; 362f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman case BuiltinType::Float: 363c8b1227fa8c17d9881815e40c04e19334be536f8Nate Begeman case BuiltinType::Double: 3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::LongDouble: 3659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = getTypeForFormat(getLLVMContext(), 36619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly Context.getFloatTypeSemantics(T), 36719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly /* UseNativeHalf = */ false); 3689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner case BuiltinType::NullPtr: 371c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson // Model std::nullptr_t as i8* 3729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::Type::getInt8PtrTy(getLLVMContext()); 3739cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 374c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson 3752df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::UInt128: 3762df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::Int128: 3779cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::IntegerType::get(getLLVMContext(), 128); 3789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 379b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei 380b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage1d: 381b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage1dArray: 382b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage1dBuffer: 383b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage2d: 384b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage2dArray: 385b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage3d: 38621f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei case BuiltinType::OCLSampler: 387e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei case BuiltinType::OCLEvent: 388b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty); 389b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei break; 3908c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman 3918c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman case BuiltinType::Dependent: 3922dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define BUILTIN_TYPE(Id, SingletonId) 3932dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define PLACEHOLDER_TYPE(Id, SingletonId) \ 3942dde35bc626153492f5f58202506c88a27fbff5bJohn McCall case BuiltinType::Id: 3952dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#include "clang/AST/BuiltinTypes.def" 396864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall llvm_unreachable("Unexpected placeholder builtin type!"); 3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 3985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 400dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith case Type::Auto: 401dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith llvm_unreachable("Unexpected undeduced auto type!"); 4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::Complex: { 403ef6de3da8572607f786303c07150daa6e140ab19Jay Foad llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType()); 4049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::StructType::get(EltTy, EltTy, NULL); 4059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::LValueReference: 4087c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::RValueReference: { 4099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const ReferenceType *RTy = cast<ReferenceType>(Ty); 4109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner QualType ETy = RTy->getPointeeType(); 4119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *PointeeType = ConvertTypeForMem(ETy); 412207f4d8543529221932af82836016a2ef066c917Peter Collingbourne unsigned AS = Context.getTargetAddressSpace(ETy); 4139cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::PointerType::get(PointeeType, AS); 4149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4156aeae7fa9cfaacba3a4077d62c01c2531d88a63eDaniel Dunbar } 4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::Pointer: { 4179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const PointerType *PTy = cast<PointerType>(Ty); 4189cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner QualType ETy = PTy->getPointeeType(); 4199cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *PointeeType = ConvertTypeForMem(ETy); 4209cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner if (PointeeType->isVoidTy()) 4219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner PointeeType = llvm::Type::getInt8Ty(getLLVMContext()); 422207f4d8543529221932af82836016a2ef066c917Peter Collingbourne unsigned AS = Context.getTargetAddressSpace(ETy); 4239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::PointerType::get(PointeeType, AS); 4249cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 427fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff case Type::VariableArray: { 4289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const VariableArrayType *A = cast<VariableArrayType>(Ty); 4299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner assert(A->getIndexTypeCVRQualifiers() == 0 && 4305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "FIXME: We only handle trivial array types so far!"); 431c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // VLAs resolve to the innermost element type; this matches 432c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // the return of alloca, and there isn't any obviously better choice. 4339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = ConvertTypeForMem(A->getElementType()); 4349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 435c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman } 436c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman case Type::IncompleteArray: { 4379cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty); 4389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner assert(A->getIndexTypeCVRQualifiers() == 0 && 439c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman "FIXME: We only handle trivial array types so far!"); 4403a2b657088de9413714a51bff153a59565adb3efChris Lattner // int X[] -> [0 x int], unless the element type is not sized. If it is 4413a2b657088de9413714a51bff153a59565adb3efChris Lattner // unsized (e.g. an incomplete struct) just use [0 x i8]. 4423a2b657088de9413714a51bff153a59565adb3efChris Lattner ResultType = ConvertTypeForMem(A->getElementType()); 4433a2b657088de9413714a51bff153a59565adb3efChris Lattner if (!ResultType->isSized()) { 4443a2b657088de9413714a51bff153a59565adb3efChris Lattner SkippedLayout = true; 4453a2b657088de9413714a51bff153a59565adb3efChris Lattner ResultType = llvm::Type::getInt8Ty(getLLVMContext()); 4463a2b657088de9413714a51bff153a59565adb3efChris Lattner } 4473a2b657088de9413714a51bff153a59565adb3efChris Lattner ResultType = llvm::ArrayType::get(ResultType, 0); 4489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 450fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff case Type::ConstantArray: { 4519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const ConstantArrayType *A = cast<ConstantArrayType>(Ty); 4522acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *EltTy = ConvertTypeForMem(A->getElementType()); 45301c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner 45401c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner // Lower arrays of undefined struct type to arrays of i8 just to have a 45501c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner // concrete type. 45601c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner if (!EltTy->isSized()) { 45701c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner SkippedLayout = true; 45801c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner EltTy = llvm::Type::getInt8Ty(getLLVMContext()); 45901c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner } 46001c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner 4619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue()); 4629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 463fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff } 464213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman case Type::ExtVector: 4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::Vector: { 4669cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const VectorType *VT = cast<VectorType>(Ty); 4679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()), 4689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner VT->getNumElements()); 4699cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::FunctionNoProto: 472bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar case Type::FunctionProto: { 47371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner const FunctionType *FT = cast<FunctionType>(Ty); 474bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner // First, check whether we can build the full function type. If the 475bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner // function type depends on an incomplete type (e.g. a struct or enum), we 4769cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // cannot lower the function type. 47771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!isFuncTypeConvertible(FT)) { 4789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // This function's type depends on an incomplete tag type. 47984fd6df966208b380cba072d2f2883962b9d270aEli Friedman 48084fd6df966208b380cba072d2f2883962b9d270aEli Friedman // Force conversion of all the relevant record types, to make sure 48184fd6df966208b380cba072d2f2883962b9d270aEli Friedman // we re-convert the FunctionType when appropriate. 48284fd6df966208b380cba072d2f2883962b9d270aEli Friedman if (const RecordType *RT = FT->getResultType()->getAs<RecordType>()) 48384fd6df966208b380cba072d2f2883962b9d270aEli Friedman ConvertRecordDeclType(RT->getDecl()); 48484fd6df966208b380cba072d2f2883962b9d270aEli Friedman if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) 48584fd6df966208b380cba072d2f2883962b9d270aEli Friedman for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++) 48684fd6df966208b380cba072d2f2883962b9d270aEli Friedman if (const RecordType *RT = FPT->getArgType(i)->getAs<RecordType>()) 48784fd6df966208b380cba072d2f2883962b9d270aEli Friedman ConvertRecordDeclType(RT->getDecl()); 48884fd6df966208b380cba072d2f2883962b9d270aEli Friedman 4899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // Return a placeholder type. 4909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::StructType::get(getLLVMContext()); 49184fd6df966208b380cba072d2f2883962b9d270aEli Friedman 49271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner SkippedLayout = true; 4939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 494b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman } 4959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 496f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // While we're converting the argument types for a function, we don't want 497f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // to recursively convert any pointed-to structs. Converting directly-used 498f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // structs is ok though. 49971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!RecordsBeingLaidOut.insert(Ty)) { 50071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner ResultType = llvm::StructType::get(getLLVMContext()); 50171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 50271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner SkippedLayout = true; 50371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner break; 50471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner } 505f0a8679b6e6635117533b89894646f1450cea25bChris Lattner 506b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman // The function type can be built; call the appropriate routines to 507b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman // build it. 508bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner const CGFunctionInfo *FI; 50971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { 5100f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FI = &arrangeFreeFunctionType( 5119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0))); 512bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner } else { 51371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT); 5140f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FI = &arrangeFreeFunctionType( 5159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0))); 516bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner } 517f0a8679b6e6635117533b89894646f1450cea25bChris Lattner 51871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If there is something higher level prodding our CGFunctionInfo, then 51971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // don't recurse into it again. 52071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (FunctionsBeingProcessed.count(FI)) { 52171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 52271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner ResultType = llvm::StructType::get(getLLVMContext()); 52371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner SkippedLayout = true; 52471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner } else { 52571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 52671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // Otherwise, we're good to go, go ahead and convert it. 527de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall ResultType = GetFunctionType(*FI); 52871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner } 52971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 53071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner RecordsBeingLaidOut.erase(Ty); 53157eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner 53257eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner if (SkippedLayout) 53357eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner TypeCache.clear(); 534f0a8679b6e6635117533b89894646f1450cea25bChris Lattner 53571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (RecordsBeingLaidOut.empty()) 536f0a8679b6e6635117533b89894646f1450cea25bChris Lattner while (!DeferredRecords.empty()) 537f0a8679b6e6635117533b89894646f1450cea25bChris Lattner ConvertRecordDeclType(DeferredRecords.pop_back_val()); 5389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 539bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar } 5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 541c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall case Type::ObjCObject: 5429cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType()); 5439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 544c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall 545391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner case Type::ObjCInterface: { 546412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar // Objective-C interfaces are always opaque (outside of the 547412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar // runtime, which can do whatever it likes); we never refine 548412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar // these. 5499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)]; 550412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar if (!T) 551c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner T = llvm::StructType::create(getLLVMContext()); 5529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = T; 5539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 554391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner } 5551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 55614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff case Type::ObjCObjectPointer: { 55728e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar // Protocol qualifications do not influence the LLVM type, we just return a 55828e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar // pointer to the underlying interface type. We don't need to worry about 55928e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar // recursive conversion. 5602acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *T = 561181eeee36b057a1a66227123fad0978d1d0f034aChris Lattner ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); 5629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = T->getPointerTo(); 5639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 56414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 56528e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar 5662045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner case Type::Enum: { 5679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const EnumDecl *ED = cast<EnumType>(Ty)->getDecl(); 5685e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall if (ED->isCompleteDefinition() || ED->isFixed()) 5699cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return ConvertType(ED->getIntegerType()); 5702045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // Return a placeholder 'i32' type. This can be changed later when the 5712045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // type is defined (see UpdateCompletedType), but is likely to be the 5722045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // "right" answer. 5732045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner ResultType = llvm::Type::getInt32Ty(getLLVMContext()); 5749cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 575de0efb3b6eac36bdeae0e60f753a974cc4118a31Chris Lattner } 5769048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar 5779048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar case Type::BlockPointer: { 5789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType(); 5799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *PointeeType = ConvertTypeForMem(FTy); 580207f4d8543529221932af82836016a2ef066c917Peter Collingbourne unsigned AS = Context.getTargetAddressSpace(FTy); 5819cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::PointerType::get(PointeeType, AS); 5829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 5839048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar } 584424c51d3d4bea87291919b75e73ca59386702ad5Sebastian Redl 5850e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson case Type::MemberPointer: { 5869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = 5879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty)); 5889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 5890e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson } 590b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman 591b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman case Type::Atomic: { 5929eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall QualType valueType = cast<AtomicType>(Ty)->getValueType(); 5939eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall ResultType = ConvertTypeForMem(valueType); 5949eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 5959eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // Pad out to the inflated size if necessary. 5969eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall uint64_t valueSize = Context.getTypeSize(valueType); 5979eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall uint64_t atomicSize = Context.getTypeSize(Ty); 5989eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (valueSize != atomicSize) { 5999eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall assert(valueSize < atomicSize); 6009eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall llvm::Type *elts[] = { 6019eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall ResultType, 6029eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8) 6039eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall }; 6049eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall ResultType = llvm::StructType::get(getLLVMContext(), 6059eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall llvm::makeArrayRef(elts)); 6069eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 607b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman break; 608b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman } 6095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 6119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner assert(ResultType && "Didn't convert a type?"); 6129cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 6139cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner TypeCache[Ty] = ResultType; 6149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return ResultType; 6155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6179eda3abe7e183b05834947391c0cdc291f4ee0d8John McCallbool CodeGenModule::isPaddedAtomicType(QualType type) { 6189eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return isPaddedAtomicType(type->castAs<AtomicType>()); 6199eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall} 6209eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6219eda3abe7e183b05834947391c0cdc291f4ee0d8John McCallbool CodeGenModule::isPaddedAtomicType(const AtomicType *type) { 6229eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType()); 6239eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall} 6249eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6259cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. 6269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) { 627efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar // TagDecl's are not necessarily unique, instead use the (clang) 628efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar // type connected to the decl. 6299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const Type *Key = Context.getTagDeclType(RD).getTypePtr(); 6301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::StructType *&Entry = RecordDeclTypes[Key]; 6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If we don't have a StructType at all yet, create the forward declaration. 634cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner if (Entry == 0) { 635c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner Entry = llvm::StructType::create(getLLVMContext()); 636cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner addRecordTypeName(RD, Entry, ""); 637cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner } 6389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::StructType *Ty = Entry; 6395de00fcf7c923a14bb79bdbaabb2faeb5633d85aChris Lattner 6409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If this is still a forward declaration, or the LLVM type is already 6419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // complete, there's nothing more to do. 64271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner RD = RD->getDefinition(); 6435e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque()) 6449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return Ty; 6459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 64671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If converting this type would cause us to infinitely loop, don't do it! 64771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!isSafeToConvert(RD, *this)) { 6489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner DeferredRecords.push_back(RD); 6499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return Ty; 6509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner } 6511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // Okay, this is a definition of a type. Compile the implementation now. 65371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult; 65471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner assert(InsertResult && "Recursively compiling a struct?"); 65571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 65686ff308724171494395a840fd2efbe25e62f352eJohn McCall // Force conversion of non-virtual base classes recursively. 6579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 6589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(), 6599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner e = CRD->bases_end(); i != e; ++i) { 66071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (i->isVirtual()) continue; 66171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 66271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl()); 66386ff308724171494395a840fd2efbe25e62f352eJohn McCall } 66486ff308724171494395a840fd2efbe25e62f352eJohn McCall } 66586ff308724171494395a840fd2efbe25e62f352eJohn McCall 666696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson // Layout fields. 6679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty); 668696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson CGRecordLayouts[Key] = Layout; 6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 67071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // We're done laying out this struct. 67171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult; 67271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner assert(EraseResult && "struct not in RecordsBeingLaidOut set?"); 67371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 674f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // If this struct blocked a FunctionType conversion, then recompute whatever 675f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // was derived from that. 676f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // FIXME: This is hugely overconservative. 67757eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner if (SkippedLayout) 67857eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner TypeCache.clear(); 67971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 68071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If we're done converting the outer-most record, then convert any deferred 68171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // structs as well. 68271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (RecordsBeingLaidOut.empty()) 6839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner while (!DeferredRecords.empty()) 6849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ConvertRecordDeclType(DeferredRecords.pop_back_val()); 6859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 6869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return Ty; 6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump} 688fc3b8e9c1381d5e6ec361591d649c56a870ff971Chris Lattner 6892d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson/// getCGRecordLayout - Return record layout info for the given record decl. 690ad3e7118c40711faf5f51f08d599dbd525d3408aAnders Carlssonconst CGRecordLayout & 6912d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders CarlssonCodeGenTypes::getCGRecordLayout(const RecordDecl *RD) { 6922d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson const Type *Key = Context.getTagDeclType(RD).getTypePtr(); 69382926963cccbc54bc707d5dcf79389166046ef08Anders Carlsson 694270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key); 695c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson if (!Layout) { 6962d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson // Compute the type information. 6979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ConvertRecordDeclType(RD); 698c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson 699c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson // Now try again. 700c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson Layout = CGRecordLayouts.lookup(Key); 701c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson } 702c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson 703270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar assert(Layout && "Unable to find record layout information for type"); 704270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar return *Layout; 705b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel} 7063e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson 707f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(QualType T) { 7083e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson // No need to check for member pointers when not compiling C++. 7094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Context.getLangOpts().CPlusPlus) 710f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return true; 7113e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson 7123e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson T = Context.getBaseElementType(T); 7133e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson 714f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // Records are non-zero-initializable if they contain any 715f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // non-zero-initializable subobjects. 7163e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson if (const RecordType *RT = T->getAs<RecordType>()) { 7173e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 718f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return isZeroInitializable(RD); 7193e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson } 720f16aa103d3afd42fbca2ab346f191bf745cec092John McCall 721f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // We have to ask the ABI about member pointers. 7223e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) 723f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return getCXXABI().isZeroInitializable(MPT); 7243e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson 725f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // Everything else is okay. 726f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return true; 7273e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson} 728c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson 729f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) { 7303379e9bd71c0e0051bd97e90d4f2ec964078091dAnders Carlsson return getCGRecordLayout(RD).isZeroInitializable(); 731c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson} 732