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" 258b54999a831bb195c08541ca995ef0505c96193fMark Lacey#include "clang/CodeGen/CGFunctionInfo.h" 263b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h" 273b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DerivedTypes.h" 283b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Module.h" 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3264aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCallCodeGenTypes::CodeGenTypes(CodeGenModule &cgm) 3364aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()), 3464aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall TheDataLayout(cgm.getDataLayout()), 3564aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()), 3664aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) { 3757eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner SkippedLayout = false; 38d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner} 395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 40b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang PatelCodeGenTypes::~CodeGenTypes() { 41651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines llvm::DeleteContainerSeconds(CGRecordLayouts); 426f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner 436f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner for (llvm::FoldingSet<CGFunctionInfo>::iterator 446f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; ) 456f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner delete &*I++; 46b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel} 47b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel 489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnervoid CodeGenTypes::addRecordTypeName(const RecordDecl *RD, 499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::StructType *Ty, 505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef suffix) { 51f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith SmallString<256> TypeName; 52e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson llvm::raw_svector_ostream OS(TypeName); 53e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson OS << RD->getKindName() << '.'; 54e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson 55e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // Name the codegen type after the typedef name 56e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // if there is no tag type name available 57e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson if (RD->getIdentifier()) { 58e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // FIXME: We should not have to check for a null decl context here. 59e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // Right now we do it because the implicit Obj-C decls don't have one. 60e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson if (RD->getDeclContext()) 61b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer RD->printQualifiedName(OS); 62e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson else 63e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson RD->printName(OS); 64e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) { 65e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // FIXME: We should not have to check for a null decl context here. 66e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson // Right now we do it because the implicit Obj-C decls don't have one. 67e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson if (TDD->getDeclContext()) 68b063ef0222a99ee168631afa7b5a882d494b8fdeBenjamin Kramer TDD->printQualifiedName(OS); 69e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson else 70e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson TDD->printName(OS); 71e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson } else 72e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson OS << "anon"; 73e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson 74e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson if (!suffix.empty()) 75e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson OS << suffix; 76e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson 779cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner Ty->setName(OS.str()); 7830ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel} 7930ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel 804581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from 814581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertType in that it is used to convert to the memory representation for 824581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// a type. For example, the scalar representation for _Bool is i1, but the 834581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// memory representation is usually i8 or i32, depending on the target. 849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){ 859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *R = ConvertType(T); 861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8719009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner // If this is a non-bool type, don't map it. 88f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands if (!R->isIntegerTy(1)) 8919009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner return R; 901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9119009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner // Otherwise, return an integer of the target-specified size. 920032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::IntegerType::get(getLLVMContext(), 930032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson (unsigned)Context.getTypeSize(T)); 9419009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner} 9519009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner 9671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 9771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isRecordLayoutComplete - Return true if the specified type is already 9871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// completely laid out. 9971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerbool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const { 10071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I = 10171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner RecordDeclTypes.find(Ty); 10271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return I != RecordDeclTypes.end() && !I->second->isOpaque(); 10371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner} 10471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 10571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool 10671305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(QualType T, CodeGenTypes &CGT, 10771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked); 10871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 10971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 11071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert the specified record 11171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// decl to IR and lay it out, false if doing so would cause us to get into a 11271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursive compilation mess. 11371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool 11471305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT, 11571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { 11671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If we have already checked this type (maybe the same type is used by-value 11771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // multiple times in multiple structure fields, don't check again. 11871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!AlreadyChecked.insert(RD)) return true; 11971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 12071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr(); 12171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 12271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this type is already laid out, converting it is a noop. 12371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (CGT.isRecordLayoutComplete(Key)) return true; 12471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 12571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this type is currently being laid out, we can't recursively compile it. 12671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (CGT.isRecordBeingLaidOut(Key)) 12771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return false; 12871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 12971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this type would require laying out bases that are currently being laid 13071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // out, don't do it. This includes virtual base classes which get laid out 13171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // when a class is translated, even though they aren't embedded by-value into 13271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // the class. 13371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 134651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto &I : CRD->bases()) 135651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!isSafeToConvert(I.getType()->getAs<RecordType>()->getDecl(), 13671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner CGT, AlreadyChecked)) 13771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return false; 13871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner } 13971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 14071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this type would require laying out members that are currently being laid 14171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // out, don't do it. 142651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto *I : RD->fields()) 14371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked)) 14471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return false; 14571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 14671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If there are no problems, lets do it. 14771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return true; 14871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner} 14971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 15071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert this field type, 15171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// which requires the structure elements contained by-value to all be 15271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursively safe to convert. 15371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool 15471305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(QualType T, CodeGenTypes &CGT, 15571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) { 15671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner T = T.getCanonicalType(); 15771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 15871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this is a record, check it. 15971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (const RecordType *RT = dyn_cast<RecordType>(T)) 16071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked); 16171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 16271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this is an array, check the elements, which are embedded inline. 16371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (const ArrayType *AT = dyn_cast<ArrayType>(T)) 16471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked); 16571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 16671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // Otherwise, there is no concern about transforming this. We only care about 16771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // things that are contained by-value in a structure that can have another 16871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // structure as a member. 16971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return true; 17071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner} 17171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 17271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 17371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert the specified record 17471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// decl to IR and lay it out, false if doing so would cause us to get into a 17571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursive compilation mess. 17671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) { 17771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If no structs are being laid out, we can certainly do this one. 17871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (CGT.noRecordsBeingLaidOut()) return true; 17971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 18071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked; 18171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return isSafeToConvert(RD, CGT, AlreadyChecked); 18271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner} 18371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 184651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// isFuncParamTypeConvertible - Return true if the specified type in a 185651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// function parameter or result position can be converted to an IR type at this 186f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// point. This boils down to being whether it is complete, as well as whether 187f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// we've temporarily deferred expanding the type because we're in a recursive 188f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// context. 189651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesbool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) { 190f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // If this isn't a tagged type, we can convert it! 191f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner const TagType *TT = Ty->getAs<TagType>(); 1926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!TT) return true; 1936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 194bcf38f2782d9cada139e22aa1ab8df10c9b047e5Douglas Gregor // Incomplete types cannot be converted. 1954a59bc26b3f2d00055e24332c3164c894fafac27Douglas Gregor if (TT->isIncompleteType()) 196f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner return false; 197f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 19871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If this is an enum, then it is always safe to convert. 199f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner const RecordType *RT = dyn_cast<RecordType>(TT); 2006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!RT) return true; 201f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 202f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // Otherwise, we have to be careful. If it is a struct that we're in the 203f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // process of expanding, then we can't convert the function type. That's ok 204f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // though because we must be in a pointer context under the struct, so we can 205f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // just convert it to a dummy type. 206f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // 207f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // We decide this by checking whether ConvertRecordDeclType returns us an 208f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner // opaque type for a struct that we know is defined. 20971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner return isSafeToConvert(RT->getDecl(), *this); 210f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner} 211f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 212f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 213f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// Code to verify a given function type is complete, i.e. the return type 214651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// and all of the parameter types are complete. Also check to see if we are in 215f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// a RS_StructPointer context, and if so whether any struct types have been 216f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// pended. If so, we don't want to ask the ABI lowering code to handle a type 217f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// that cannot be converted to an IR type. 218f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattnerbool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) { 219651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!isFuncParamTypeConvertible(FT->getReturnType())) 220f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner return false; 221f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 222f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) 223651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) 224651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (!isFuncParamTypeConvertible(FPT->getParamType(i))) 225f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner return false; 226f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 227f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner return true; 228b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman} 229b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman 230c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// UpdateCompletedType - When we find the full definition for a TagDecl, 231c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// replace the 'opaque' type we previously made for it if applicable. 232c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { 2339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If this is an enum being completed, then we flush all non-struct types from 2349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // the cache. This allows function types and other things that may be derived 2359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // from the enum to be recomputed. 2368dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) { 2378dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner // Only flush the cache if we've actually already converted this type. 2382045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner if (TypeCache.count(ED->getTypeForDecl())) { 2392045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // Okay, we formed some types based on this. We speculated that the enum 2402045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // would be lowered to i32, so we only need to flush the cache if this 2412045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // didn't happen. 2422045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner if (!ConvertType(ED->getIntegerType())->isIntegerTy(32)) 2432045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner TypeCache.clear(); 2442045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner } 2456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // If necessary, provide the full definition of a type only used with a 2466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines // declaration so far. 2476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) 2486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines DI->completeType(ED); 2499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return; 250b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman } 2519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 252f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // If we completed a RecordDecl that we previously used and converted to an 253f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // anonymous type, then go ahead and complete it now. 2549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const RecordDecl *RD = cast<RecordDecl>(TD); 255f0a8679b6e6635117533b89894646f1450cea25bChris Lattner if (RD->isDependentType()) return; 256f0a8679b6e6635117533b89894646f1450cea25bChris Lattner 257f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // Only complete it if we converted it already. If we haven't converted it 258f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // yet, we'll just do it lazily. 2593ade97504790bbc5a148baa5b3f7e577aed9c1c6Chris Lattner if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr())) 2609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ConvertRecordDeclType(RD); 261eab6a36cf1eed9a7fec0babd2206048e28dedd02David Blaikie 262eab6a36cf1eed9a7fec0babd2206048e28dedd02David Blaikie // If necessary, provide the full definition of a type only used with a 263eab6a36cf1eed9a7fec0babd2206048e28dedd02David Blaikie // declaration so far. 264eab6a36cf1eed9a7fec0babd2206048e28dedd02David Blaikie if (CGDebugInfo *DI = CGM.getModuleDebugInfo()) 265278048971c8b2e153d305ee8917618c9bf38b1c3David Blaikie DI->completeType(RD); 266d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner} 267d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 2689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext, 26919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly const llvm::fltSemantics &format, 27019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly bool UseNativeHalf = false) { 27119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly if (&format == &llvm::APFloat::IEEEhalf) { 27219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly if (UseNativeHalf) 27319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly return llvm::Type::getHalfTy(VMContext); 27419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly else 27519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly return llvm::Type::getInt16Ty(VMContext); 27619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly } 277b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::IEEEsingle) 2780032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getFloatTy(VMContext); 279b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::IEEEdouble) 2800032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getDoubleTy(VMContext); 281b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::IEEEquad) 2820032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getFP128Ty(VMContext); 283b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::PPCDoubleDouble) 2840032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getPPC_FP128Ty(VMContext); 285b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner if (&format == &llvm::APFloat::x87DoubleExtended) 2860032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson return llvm::Type::getX86_FP80Ty(VMContext); 287b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Unknown float format!"); 288f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman} 289f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman 2909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertType - Convert the specified type to its LLVM form. 2919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertType(QualType T) { 2929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner T = Context.getCanonicalType(T); 2939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 2949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const Type *Ty = T.getTypePtr(); 2951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // RecordTypes are cached and processed specially. 2979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 2989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return ConvertRecordDeclType(RT->getDecl()); 2999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 3009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // See if type is already cached. 3019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty); 3029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If type is found in map then use it. Otherwise, convert type T. 3039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner if (TCI != TypeCache.end()) 3049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return TCI->second; 3059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 3069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If we don't have it in the cache, convert it now. 3076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines llvm::Type *ResultType = nullptr; 3089cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner switch (Ty->getTypeClass()) { 3099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner case Type::Record: // Handled above. 31072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base) 31172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 31272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 31372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class: 314ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: 31572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def" 316864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall llvm_unreachable("Non-canonical or dependent types aren't possible."); 31772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 3185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::Builtin: { 3199cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner switch (cast<BuiltinType>(Ty)->getKind()) { 3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Void: 321de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff case BuiltinType::ObjCId: 322de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff case BuiltinType::ObjCClass: 32313dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian case BuiltinType::ObjCSel: 3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // LLVM void type can only be used as the result of a function call. Just 3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // map to the same as char. 3269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::Type::getInt8Ty(getLLVMContext()); 3279cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Bool: 33019009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner // Note that we always return bool as i1 for use as a scalar type. 3319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::Type::getInt1Ty(getLLVMContext()); 3329cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 3331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 334d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::Char_S: 335d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::Char_U: 336d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::SChar: 337d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case BuiltinType::UChar: 3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Short: 3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::UShort: 3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Int: 3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::UInt: 3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::Long: 3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::ULong: 3445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::LongLong: 3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::ULongLong: 3463f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner case BuiltinType::WChar_S: 3473f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner case BuiltinType::WChar_U: 348f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case BuiltinType::Char16: 349f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith case BuiltinType::Char32: 3509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::IntegerType::get(getLLVMContext(), 3519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner static_cast<unsigned>(Context.getTypeSize(T))); 3529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 3531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 354aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov case BuiltinType::Half: 35519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly // Half FP can either be storage-only (lowered to i16) or native. 35619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly ResultType = getTypeForFormat(getLLVMContext(), 35719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly Context.getFloatTypeSemantics(T), 35819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly Context.getLangOpts().NativeHalfType); 359aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov break; 360f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman case BuiltinType::Float: 361c8b1227fa8c17d9881815e40c04e19334be536f8Nate Begeman case BuiltinType::Double: 3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case BuiltinType::LongDouble: 3639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = getTypeForFormat(getLLVMContext(), 36419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly Context.getFloatTypeSemantics(T), 36519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly /* UseNativeHalf = */ false); 3669cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 3671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner case BuiltinType::NullPtr: 369c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson // Model std::nullptr_t as i8* 3709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::Type::getInt8PtrTy(getLLVMContext()); 3719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 372c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson 3732df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::UInt128: 3742df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner case BuiltinType::Int128: 3759cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::IntegerType::get(getLLVMContext(), 128); 3769cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 377b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei 378b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage1d: 379b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage1dArray: 380b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage1dBuffer: 381b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage2d: 382b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage2dArray: 383b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei case BuiltinType::OCLImage3d: 38421f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei case BuiltinType::OCLSampler: 385e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei case BuiltinType::OCLEvent: 386b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty); 387b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei break; 3888c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman 3898c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman case BuiltinType::Dependent: 3902dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define BUILTIN_TYPE(Id, SingletonId) 3912dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define PLACEHOLDER_TYPE(Id, SingletonId) \ 3922dde35bc626153492f5f58202506c88a27fbff5bJohn McCall case BuiltinType::Id: 3932dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#include "clang/AST/BuiltinTypes.def" 394864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall llvm_unreachable("Unexpected placeholder builtin type!"); 3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 3965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 398dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith case Type::Auto: 399dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith llvm_unreachable("Unexpected undeduced auto type!"); 4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::Complex: { 401ef6de3da8572607f786303c07150daa6e140ab19Jay Foad llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType()); 4029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::StructType::get(EltTy, EltTy, NULL); 4039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4057c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::LValueReference: 4067c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl case Type::RValueReference: { 4079cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const ReferenceType *RTy = cast<ReferenceType>(Ty); 4089cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner QualType ETy = RTy->getPointeeType(); 4099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *PointeeType = ConvertTypeForMem(ETy); 410207f4d8543529221932af82836016a2ef066c917Peter Collingbourne unsigned AS = Context.getTargetAddressSpace(ETy); 4119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::PointerType::get(PointeeType, AS); 4129cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4136aeae7fa9cfaacba3a4077d62c01c2531d88a63eDaniel Dunbar } 4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::Pointer: { 4159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const PointerType *PTy = cast<PointerType>(Ty); 4169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner QualType ETy = PTy->getPointeeType(); 4179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *PointeeType = ConvertTypeForMem(ETy); 4189cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner if (PointeeType->isVoidTy()) 4199cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner PointeeType = llvm::Type::getInt8Ty(getLLVMContext()); 420207f4d8543529221932af82836016a2ef066c917Peter Collingbourne unsigned AS = Context.getTargetAddressSpace(ETy); 4219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::PointerType::get(PointeeType, AS); 4229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 425fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff case Type::VariableArray: { 4269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const VariableArrayType *A = cast<VariableArrayType>(Ty); 4279cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner assert(A->getIndexTypeCVRQualifiers() == 0 && 4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer "FIXME: We only handle trivial array types so far!"); 429c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // VLAs resolve to the innermost element type; this matches 430c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman // the return of alloca, and there isn't any obviously better choice. 4319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = ConvertTypeForMem(A->getElementType()); 4329cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 433c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman } 434c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman case Type::IncompleteArray: { 4359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty); 4369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner assert(A->getIndexTypeCVRQualifiers() == 0 && 437c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman "FIXME: We only handle trivial array types so far!"); 4383a2b657088de9413714a51bff153a59565adb3efChris Lattner // int X[] -> [0 x int], unless the element type is not sized. If it is 4393a2b657088de9413714a51bff153a59565adb3efChris Lattner // unsized (e.g. an incomplete struct) just use [0 x i8]. 4403a2b657088de9413714a51bff153a59565adb3efChris Lattner ResultType = ConvertTypeForMem(A->getElementType()); 4413a2b657088de9413714a51bff153a59565adb3efChris Lattner if (!ResultType->isSized()) { 4423a2b657088de9413714a51bff153a59565adb3efChris Lattner SkippedLayout = true; 4433a2b657088de9413714a51bff153a59565adb3efChris Lattner ResultType = llvm::Type::getInt8Ty(getLLVMContext()); 4443a2b657088de9413714a51bff153a59565adb3efChris Lattner } 4453a2b657088de9413714a51bff153a59565adb3efChris Lattner ResultType = llvm::ArrayType::get(ResultType, 0); 4469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 448fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff case Type::ConstantArray: { 4499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const ConstantArrayType *A = cast<ConstantArrayType>(Ty); 4502acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *EltTy = ConvertTypeForMem(A->getElementType()); 45101c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner 45201c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner // Lower arrays of undefined struct type to arrays of i8 just to have a 45301c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner // concrete type. 45401c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner if (!EltTy->isSized()) { 45501c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner SkippedLayout = true; 45601c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner EltTy = llvm::Type::getInt8Ty(getLLVMContext()); 45701c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner } 45801c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner 4599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue()); 4609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 461fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff } 462213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman case Type::ExtVector: 4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::Vector: { 4649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const VectorType *VT = cast<VectorType>(Ty); 4659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()), 4669cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner VT->getNumElements()); 4679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 4685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case Type::FunctionNoProto: 470bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar case Type::FunctionProto: { 47171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner const FunctionType *FT = cast<FunctionType>(Ty); 472bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner // First, check whether we can build the full function type. If the 473bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner // function type depends on an incomplete type (e.g. a struct or enum), we 4749cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // cannot lower the function type. 47571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!isFuncTypeConvertible(FT)) { 4769cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // This function's type depends on an incomplete tag type. 47784fd6df966208b380cba072d2f2883962b9d270aEli Friedman 47884fd6df966208b380cba072d2f2883962b9d270aEli Friedman // Force conversion of all the relevant record types, to make sure 47984fd6df966208b380cba072d2f2883962b9d270aEli Friedman // we re-convert the FunctionType when appropriate. 480651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>()) 48184fd6df966208b380cba072d2f2883962b9d270aEli Friedman ConvertRecordDeclType(RT->getDecl()); 48284fd6df966208b380cba072d2f2883962b9d270aEli Friedman if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) 483651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++) 484651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>()) 48584fd6df966208b380cba072d2f2883962b9d270aEli Friedman ConvertRecordDeclType(RT->getDecl()); 48684fd6df966208b380cba072d2f2883962b9d270aEli Friedman 4879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // Return a placeholder type. 4889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::StructType::get(getLLVMContext()); 48984fd6df966208b380cba072d2f2883962b9d270aEli Friedman 49071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner SkippedLayout = true; 4919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 492b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman } 4939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 494651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // While we're converting the parameter types for a function, we don't want 495f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // to recursively convert any pointed-to structs. Converting directly-used 496f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // structs is ok though. 49771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!RecordsBeingLaidOut.insert(Ty)) { 49871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner ResultType = llvm::StructType::get(getLLVMContext()); 49971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 50071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner SkippedLayout = true; 50171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner break; 50271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner } 503f0a8679b6e6635117533b89894646f1450cea25bChris Lattner 504b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman // The function type can be built; call the appropriate routines to 505b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman // build it. 506bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner const CGFunctionInfo *FI; 50771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { 5080f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FI = &arrangeFreeFunctionType( 5099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0))); 510bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner } else { 51171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT); 5120f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FI = &arrangeFreeFunctionType( 5139cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0))); 514bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner } 515f0a8679b6e6635117533b89894646f1450cea25bChris Lattner 51671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If there is something higher level prodding our CGFunctionInfo, then 51771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // don't recurse into it again. 51871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (FunctionsBeingProcessed.count(FI)) { 51971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 52071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner ResultType = llvm::StructType::get(getLLVMContext()); 52171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner SkippedLayout = true; 52271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner } else { 52371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 52471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // Otherwise, we're good to go, go ahead and convert it. 525de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall ResultType = GetFunctionType(*FI); 52671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner } 52771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 52871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner RecordsBeingLaidOut.erase(Ty); 52957eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner 53057eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner if (SkippedLayout) 53157eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner TypeCache.clear(); 532f0a8679b6e6635117533b89894646f1450cea25bChris Lattner 53371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (RecordsBeingLaidOut.empty()) 534f0a8679b6e6635117533b89894646f1450cea25bChris Lattner while (!DeferredRecords.empty()) 535f0a8679b6e6635117533b89894646f1450cea25bChris Lattner ConvertRecordDeclType(DeferredRecords.pop_back_val()); 5369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 537bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar } 5381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 539c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall case Type::ObjCObject: 5409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType()); 5419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 542c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall 543391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner case Type::ObjCInterface: { 544412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar // Objective-C interfaces are always opaque (outside of the 545412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar // runtime, which can do whatever it likes); we never refine 546412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar // these. 5479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)]; 548412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar if (!T) 549c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner T = llvm::StructType::create(getLLVMContext()); 5509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = T; 5519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 552391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner } 5531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 55414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff case Type::ObjCObjectPointer: { 55528e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar // Protocol qualifications do not influence the LLVM type, we just return a 55628e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar // pointer to the underlying interface type. We don't need to worry about 55728e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar // recursive conversion. 5582acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *T = 559181eeee36b057a1a66227123fad0978d1d0f034aChris Lattner ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); 5609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = T->getPointerTo(); 5619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 56214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff } 56328e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar 5642045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner case Type::Enum: { 5659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const EnumDecl *ED = cast<EnumType>(Ty)->getDecl(); 5665e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall if (ED->isCompleteDefinition() || ED->isFixed()) 5679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return ConvertType(ED->getIntegerType()); 5682045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // Return a placeholder 'i32' type. This can be changed later when the 5692045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // type is defined (see UpdateCompletedType), but is likely to be the 5702045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner // "right" answer. 5712045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner ResultType = llvm::Type::getInt32Ty(getLLVMContext()); 5729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 573de0efb3b6eac36bdeae0e60f753a974cc4118a31Chris Lattner } 5749048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar 5759048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar case Type::BlockPointer: { 5769cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType(); 5779cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *PointeeType = ConvertTypeForMem(FTy); 578207f4d8543529221932af82836016a2ef066c917Peter Collingbourne unsigned AS = Context.getTargetAddressSpace(FTy); 5799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = llvm::PointerType::get(PointeeType, AS); 5809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 5819048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar } 582424c51d3d4bea87291919b75e73ca59386702ad5Sebastian Redl 5830e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson case Type::MemberPointer: { 5849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ResultType = 5859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty)); 5869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner break; 5870e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson } 588b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman 589b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman case Type::Atomic: { 5909eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall QualType valueType = cast<AtomicType>(Ty)->getValueType(); 5919eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall ResultType = ConvertTypeForMem(valueType); 5929eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 5939eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall // Pad out to the inflated size if necessary. 5949eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall uint64_t valueSize = Context.getTypeSize(valueType); 5959eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall uint64_t atomicSize = Context.getTypeSize(Ty); 5969eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall if (valueSize != atomicSize) { 5979eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall assert(valueSize < atomicSize); 5989eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall llvm::Type *elts[] = { 5999eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall ResultType, 6009eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8) 6019eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall }; 6029eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall ResultType = llvm::StructType::get(getLLVMContext(), 6039eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall llvm::makeArrayRef(elts)); 6049eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall } 605b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman break; 606b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman } 6075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 6089cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 6099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner assert(ResultType && "Didn't convert a type?"); 6109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 6119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner TypeCache[Ty] = ResultType; 6129cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return ResultType; 6135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 6145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 6159eda3abe7e183b05834947391c0cdc291f4ee0d8John McCallbool CodeGenModule::isPaddedAtomicType(QualType type) { 6169eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return isPaddedAtomicType(type->castAs<AtomicType>()); 6179eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall} 6189eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6199eda3abe7e183b05834947391c0cdc291f4ee0d8John McCallbool CodeGenModule::isPaddedAtomicType(const AtomicType *type) { 6209eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType()); 6219eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall} 6229eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall 6239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union. 6249cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) { 625efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar // TagDecl's are not necessarily unique, instead use the (clang) 626efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar // type connected to the decl. 6279cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const Type *Key = Context.getTagDeclType(RD).getTypePtr(); 6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::StructType *&Entry = RecordDeclTypes[Key]; 6301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If we don't have a StructType at all yet, create the forward declaration. 6326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!Entry) { 633c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner Entry = llvm::StructType::create(getLLVMContext()); 634cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner addRecordTypeName(RD, Entry, ""); 635cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner } 6369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::StructType *Ty = Entry; 6375de00fcf7c923a14bb79bdbaabb2faeb5633d85aChris Lattner 6389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // If this is still a forward declaration, or the LLVM type is already 6399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // complete, there's nothing more to do. 64071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner RD = RD->getDefinition(); 6416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque()) 6429cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return Ty; 6439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 64471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If converting this type would cause us to infinitely loop, don't do it! 64571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (!isSafeToConvert(RD, *this)) { 6469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner DeferredRecords.push_back(RD); 6479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return Ty; 6489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner } 6491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner // Okay, this is a definition of a type. Compile the implementation now. 65171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult; 65271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner assert(InsertResult && "Recursively compiling a struct?"); 65371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 65486ff308724171494395a840fd2efbe25e62f352eJohn McCall // Force conversion of non-virtual base classes recursively. 6559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { 656651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto &I : CRD->bases()) { 657651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (I.isVirtual()) continue; 65871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 659651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ConvertRecordDeclType(I.getType()->getAs<RecordType>()->getDecl()); 66086ff308724171494395a840fd2efbe25e62f352eJohn McCall } 66186ff308724171494395a840fd2efbe25e62f352eJohn McCall } 66286ff308724171494395a840fd2efbe25e62f352eJohn McCall 663696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson // Layout fields. 6649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty); 665696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson CGRecordLayouts[Key] = Layout; 6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 66771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // We're done laying out this struct. 66871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult; 66971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner assert(EraseResult && "struct not in RecordsBeingLaidOut set?"); 67071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 671f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // If this struct blocked a FunctionType conversion, then recompute whatever 672f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // was derived from that. 673f0a8679b6e6635117533b89894646f1450cea25bChris Lattner // FIXME: This is hugely overconservative. 67457eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner if (SkippedLayout) 67557eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner TypeCache.clear(); 67671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 67771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // If we're done converting the outer-most record, then convert any deferred 67871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner // structs as well. 67971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner if (RecordsBeingLaidOut.empty()) 6809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner while (!DeferredRecords.empty()) 6819cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ConvertRecordDeclType(DeferredRecords.pop_back_val()); 6829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner 6839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner return Ty; 6841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump} 685fc3b8e9c1381d5e6ec361591d649c56a870ff971Chris Lattner 6862d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson/// getCGRecordLayout - Return record layout info for the given record decl. 687ad3e7118c40711faf5f51f08d599dbd525d3408aAnders Carlssonconst CGRecordLayout & 6882d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders CarlssonCodeGenTypes::getCGRecordLayout(const RecordDecl *RD) { 6892d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson const Type *Key = Context.getTagDeclType(RD).getTypePtr(); 69082926963cccbc54bc707d5dcf79389166046ef08Anders Carlsson 691270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key); 692c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson if (!Layout) { 6932d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson // Compute the type information. 6949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner ConvertRecordDeclType(RD); 695c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson 696c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson // Now try again. 697c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson Layout = CGRecordLayouts.lookup(Key); 698c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson } 699c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson 700270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar assert(Layout && "Unable to find record layout information for type"); 701270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar return *Layout; 702b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel} 7033e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson 704f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(QualType T) { 7053e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson // No need to check for member pointers when not compiling C++. 7064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (!Context.getLangOpts().CPlusPlus) 707f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return true; 7083e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson 7093e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson T = Context.getBaseElementType(T); 7103e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson 711f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // Records are non-zero-initializable if they contain any 712f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // non-zero-initializable subobjects. 7133e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson if (const RecordType *RT = T->getAs<RecordType>()) { 7143e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 715f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return isZeroInitializable(RD); 7163e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson } 717f16aa103d3afd42fbca2ab346f191bf745cec092John McCall 718f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // We have to ask the ABI about member pointers. 7193e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) 720f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return getCXXABI().isZeroInitializable(MPT); 7213e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson 722f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // Everything else is okay. 723f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return true; 7243e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson} 725c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson 726f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) { 7273379e9bd71c0e0051bd97e90d4f2ec964078091dAnders Carlsson return getCGRecordLayout(RD).isZeroInitializable(); 728c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson} 729