CodeGenTypes.cpp revision 4a59bc26b3f2d00055e24332c3164c894fafac27
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"
15270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar#include "CGCall.h"
16f16aa103d3afd42fbca2ab346f191bf745cec092John McCall#include "CGCXXABI.h"
172924ade97ee4228fcf3518d89cd4bd1653236b48Daniel Dunbar#include "CGRecordLayout.h"
18de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall#include "TargetInfo.h"
19de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
21742cd1b7bb86b52b23b335d47abbd842dac0e1bfFariborz Jahanian#include "clang/AST/DeclCXX.h"
22de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/Expr.h"
2319cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/DerivedTypes.h"
254e533287e6a9adac78c9ac370612581aad9b8c5eAnders Carlsson#include "llvm/Module.h"
26d9e9ede6b1703849da739629904dad197306e527Devang Patel#include "llvm/Target/TargetData.h"
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
30de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::CodeGenTypes(CodeGenModule &CGM)
31de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  : Context(CGM.getContext()), Target(Context.getTargetInfo()),
32de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    TheModule(CGM.getModule()), TheTargetData(CGM.getTargetData()),
33de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    TheABIInfo(CGM.getTargetCodeGenInfo().getABIInfo()),
34de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    TheCXXABI(CGM.getCXXABI()),
35de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    CodeGenOpts(CGM.getCodeGenOpts()), CGM(CGM) {
3657eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner  SkippedLayout = false;
37d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner}
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
39b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang PatelCodeGenTypes::~CodeGenTypes() {
401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
42b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel      I != E; ++I)
43b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel    delete I->second;
446f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner
456f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner  for (llvm::FoldingSet<CGFunctionInfo>::iterator
466f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner       I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
476f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner    delete &*I++;
48b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
49b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel
509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnervoid CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     llvm::StructType *Ty,
525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                     StringRef suffix) {
53f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  SmallString<256> TypeName;
54e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  llvm::raw_svector_ostream OS(TypeName);
55e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson  OS << RD->getKindName() << '.';
56e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson
57e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  // Name the codegen type after the typedef name
58e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  // if there is no tag type name available
59e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson  if (RD->getIdentifier()) {
60e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // FIXME: We should not have to check for a null decl context here.
61e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // Right now we do it because the implicit Obj-C decls don't have one.
62e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson    if (RD->getDeclContext())
63e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson      OS << RD->getQualifiedNameAsString();
64e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    else
65e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson      RD->printName(OS);
66e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson  } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
67e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // FIXME: We should not have to check for a null decl context here.
68e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // Right now we do it because the implicit Obj-C decls don't have one.
69e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    if (TDD->getDeclContext())
70e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson      OS << TDD->getQualifiedNameAsString();
71e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    else
72e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson      TDD->printName(OS);
73e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  } else
74e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    OS << "anon";
75e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson
76e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  if (!suffix.empty())
77e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    OS << suffix;
78e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson
799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  Ty->setName(OS.str());
8030ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel}
8130ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel
824581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
834581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertType in that it is used to convert to the memory representation for
844581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// a type.  For example, the scalar representation for _Bool is i1, but the
854581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// memory representation is usually i8 or i32, depending on the target.
869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){
879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *R = ConvertType(T);
881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8919009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner  // If this is a non-bool type, don't map it.
90f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands  if (!R->isIntegerTy(1))
9119009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner    return R;
921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9319009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner  // Otherwise, return an integer of the target-specified size.
940032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  return llvm::IntegerType::get(getLLVMContext(),
950032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                (unsigned)Context.getTypeSize(T));
9619009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner}
9719009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner
9871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
9971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isRecordLayoutComplete - Return true if the specified type is already
10071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// completely laid out.
10171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerbool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
10271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
10371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  RecordDeclTypes.find(Ty);
10471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return I != RecordDeclTypes.end() && !I->second->isOpaque();
10571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
10671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
10771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool
10871305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(QualType T, CodeGenTypes &CGT,
10971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
11071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
11171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
11271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert the specified record
11371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// decl to IR and lay it out, false if doing so would cause us to get into a
11471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursive compilation mess.
11571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool
11671305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
11771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
11871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If we have already checked this type (maybe the same type is used by-value
11971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // multiple times in multiple structure fields, don't check again.
12071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (!AlreadyChecked.insert(RD)) return true;
12171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
12271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
12371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
12471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this type is already laid out, converting it is a noop.
12571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (CGT.isRecordLayoutComplete(Key)) return true;
12671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
12771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this type is currently being laid out, we can't recursively compile it.
12871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (CGT.isRecordBeingLaidOut(Key))
12971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    return false;
13071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
13171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this type would require laying out bases that are currently being laid
13271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // out, don't do it.  This includes virtual base classes which get laid out
13371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // when a class is translated, even though they aren't embedded by-value into
13471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // the class.
13571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
13671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    for (CXXRecordDecl::base_class_const_iterator I = CRD->bases_begin(),
13771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner         E = CRD->bases_end(); I != E; ++I)
13871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      if (!isSafeToConvert(I->getType()->getAs<RecordType>()->getDecl(),
13971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                           CGT, AlreadyChecked))
14071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner        return false;
14171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  }
14271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
14371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this type would require laying out members that are currently being laid
14471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // out, don't do it.
14571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  for (RecordDecl::field_iterator I = RD->field_begin(),
14671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner       E = RD->field_end(); I != E; ++I)
14771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
14871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      return false;
14971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
15071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If there are no problems, lets do it.
15171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return true;
15271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
15371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
15471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert this field type,
15571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// which requires the structure elements contained by-value to all be
15671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursively safe to convert.
15771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool
15871305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(QualType T, CodeGenTypes &CGT,
15971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
16071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  T = T.getCanonicalType();
16171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
16271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this is a record, check it.
16371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(T))
16471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
16571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
16671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this is an array, check the elements, which are embedded inline.
16771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (const ArrayType *AT = dyn_cast<ArrayType>(T))
16871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
16971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
17071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // Otherwise, there is no concern about transforming this.  We only care about
17171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // things that are contained by-value in a structure that can have another
17271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // structure as a member.
17371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return true;
17471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
17571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
17671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
17771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert the specified record
17871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// decl to IR and lay it out, false if doing so would cause us to get into a
17971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursive compilation mess.
18071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
18171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If no structs are being laid out, we can certainly do this one.
18271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (CGT.noRecordsBeingLaidOut()) return true;
18371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
18471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
18571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return isSafeToConvert(RD, CGT, AlreadyChecked);
18671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
18771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
18871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
189f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// isFuncTypeArgumentConvertible - Return true if the specified type in a
190f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// function argument or result position can be converted to an IR type at this
191f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// point.  This boils down to being whether it is complete, as well as whether
192f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// we've temporarily deferred expanding the type because we're in a recursive
193f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// context.
19471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerbool CodeGenTypes::isFuncTypeArgumentConvertible(QualType Ty) {
195f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // If this isn't a tagged type, we can convert it!
196f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  const TagType *TT = Ty->getAs<TagType>();
197f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (TT == 0) return true;
198f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
199f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
20071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If it's a tagged type used by-value, but is just a forward decl, we can't
20171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // convert it.  Note that getDefinition()==0 is not the same as !isDefinition.
202a98a28534e48e59d5f7d69adae121589e298dd43Douglas Gregor  // The exception is an enumeration type with a fixed underlying type; these
203a98a28534e48e59d5f7d69adae121589e298dd43Douglas Gregor  // can be converted even if they are forward declarations.
2044a59bc26b3f2d00055e24332c3164c894fafac27Douglas Gregor  if (TT->isIncompleteType())
205f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    return false;
206f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
20771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this is an enum, then it is always safe to convert.
208f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  const RecordType *RT = dyn_cast<RecordType>(TT);
209f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (RT == 0) return true;
210f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
211f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // Otherwise, we have to be careful.  If it is a struct that we're in the
212f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // process of expanding, then we can't convert the function type.  That's ok
213f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // though because we must be in a pointer context under the struct, so we can
214f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // just convert it to a dummy type.
215f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  //
216f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // We decide this by checking whether ConvertRecordDeclType returns us an
217f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // opaque type for a struct that we know is defined.
21871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return isSafeToConvert(RT->getDecl(), *this);
219f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner}
220f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
221f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
222f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// Code to verify a given function type is complete, i.e. the return type
223f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// and all of the argument types are complete.  Also check to see if we are in
224f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// a RS_StructPointer context, and if so whether any struct types have been
225f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// pended.  If so, we don't want to ask the ABI lowering code to handle a type
226f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// that cannot be converted to an IR type.
227f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattnerbool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
228f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (!isFuncTypeArgumentConvertible(FT->getResultType()))
229f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    return false;
230f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
231f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
2329cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
233f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner      if (!isFuncTypeArgumentConvertible(FPT->getArgType(i)))
234f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner        return false;
235f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
236f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  return true;
237b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman}
238b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman
239c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// UpdateCompletedType - When we find the full definition for a TagDecl,
240c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// replace the 'opaque' type we previously made for it if applicable.
241c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
2429cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If this is an enum being completed, then we flush all non-struct types from
2439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // the cache.  This allows function types and other things that may be derived
2449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // from the enum to be recomputed.
2458dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner  if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
2468dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner    // Only flush the cache if we've actually already converted this type.
2472045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    if (TypeCache.count(ED->getTypeForDecl())) {
2482045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      // Okay, we formed some types based on this.  We speculated that the enum
2492045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      // would be lowered to i32, so we only need to flush the cache if this
2502045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      // didn't happen.
2512045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
2522045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner        TypeCache.clear();
2532045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    }
2549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return;
255b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman  }
2569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
257f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // If we completed a RecordDecl that we previously used and converted to an
258f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // anonymous type, then go ahead and complete it now.
2599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const RecordDecl *RD = cast<RecordDecl>(TD);
260f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  if (RD->isDependentType()) return;
261f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
262f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // Only complete it if we converted it already.  If we haven't converted it
263f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // yet, we'll just do it lazily.
2643ade97504790bbc5a148baa5b3f7e577aed9c1c6Chris Lattner  if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
2659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ConvertRecordDeclType(RD);
266d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner}
267d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
2689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
2699cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                    const llvm::fltSemantics &format) {
270aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (&format == &llvm::APFloat::IEEEhalf)
271aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return llvm::Type::getInt16Ty(VMContext);
272b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEsingle)
2730032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFloatTy(VMContext);
274b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEdouble)
2750032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getDoubleTy(VMContext);
276b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEquad)
2770032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFP128Ty(VMContext);
278b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::PPCDoubleDouble)
2790032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getPPC_FP128Ty(VMContext);
280b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::x87DoubleExtended)
2810032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getX86_FP80Ty(VMContext);
282b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unknown float format!");
283f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman}
284f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman
2859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertType - Convert the specified type to its LLVM form.
2869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertType(QualType T) {
2879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  T = Context.getCanonicalType(T);
2889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Ty = T.getTypePtr();
2901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // RecordTypes are cached and processed specially.
2929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(Ty))
2939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return ConvertRecordDeclType(RT->getDecl());
2949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // See if type is already cached.
2969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
2979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If type is found in map then use it. Otherwise, convert type T.
2989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (TCI != TypeCache.end())
2999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return TCI->second;
3009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
3019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have it in the cache, convert it now.
3029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResultType = 0;
3039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  switch (Ty->getTypeClass()) {
3049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  case Type::Record: // Handled above.
30572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
30672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
30772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
30872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
309ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
31072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
311864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    llvm_unreachable("Non-canonical or dependent types aren't possible.");
31272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Builtin: {
3149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    switch (cast<BuiltinType>(Ty)->getKind()) {
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Void:
316de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCId:
317de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCClass:
31813dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    case BuiltinType::ObjCSel:
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LLVM void type can only be used as the result of a function call.  Just
3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // map to the same as char.
3219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
3229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Bool:
32519009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner      // Note that we always return bool as i1 for use as a scalar type.
3269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt1Ty(getLLVMContext());
3279cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
329d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_S:
330d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_U:
331d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::SChar:
332d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::UChar:
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Short:
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UShort:
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Int:
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UInt:
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Long:
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULong:
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongLong:
3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULongLong:
3413f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
3423f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
343f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
344f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
3459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(),
3469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 static_cast<unsigned>(Context.getTypeSize(T)));
3479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
349aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    case BuiltinType::Half:
350aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Half is special: it might be lowered to i16 (and will be storage-only
351aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // type),. or can be represented as a set of native operations.
352aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
353aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: Ask target which kind of half FP it prefers (storage only vs
354aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // native).
355aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      ResultType = llvm::Type::getInt16Ty(getLLVMContext());
356aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      break;
357f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman    case BuiltinType::Float:
358c8b1227fa8c17d9881815e40c04e19334be536f8Nate Begeman    case BuiltinType::Double:
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongDouble:
3609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = getTypeForFormat(getLLVMContext(),
3619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                    Context.getFloatTypeSemantics(T));
3629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    case BuiltinType::NullPtr:
365c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson      // Model std::nullptr_t as i8*
3669cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
3679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
368c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson
3692df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::UInt128:
3702df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::Int128:
3719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
3729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3738c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman
3748c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman    case BuiltinType::Dependent:
3752dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define BUILTIN_TYPE(Id, SingletonId)
3762dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define PLACEHOLDER_TYPE(Id, SingletonId) \
3772dde35bc626153492f5f58202506c88a27fbff5bJohn McCall    case BuiltinType::Id:
3782dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#include "clang/AST/BuiltinTypes.def"
379864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall      llvm_unreachable("Unexpected placeholder builtin type!");
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
3825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Complex: {
384ef6de3da8572607f786303c07150daa6e140ab19Jay Foad    llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
3859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
3869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3887c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
3897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference: {
3909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ReferenceType *RTy = cast<ReferenceType>(Ty);
3919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = RTy->getPointeeType();
3929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
393207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
3949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
3959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3966aeae7fa9cfaacba3a4077d62c01c2531d88a63eDaniel Dunbar  }
3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Pointer: {
3989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const PointerType *PTy = cast<PointerType>(Ty);
3999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = PTy->getPointeeType();
4009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
4019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    if (PointeeType->isVoidTy())
4029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
403207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
4049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
4059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
408fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray: {
4099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VariableArrayType *A = cast<VariableArrayType>(Ty);
4109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "FIXME: We only handle trivial array types so far!");
412c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // VLAs resolve to the innermost element type; this matches
413c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // the return of alloca, and there isn't any obviously better choice.
4149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertTypeForMem(A->getElementType());
4159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
416c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  }
417c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  case Type::IncompleteArray: {
4189cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
4199cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
420c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman           "FIXME: We only handle trivial array types so far!");
4213a2b657088de9413714a51bff153a59565adb3efChris Lattner    // int X[] -> [0 x int], unless the element type is not sized.  If it is
4223a2b657088de9413714a51bff153a59565adb3efChris Lattner    // unsized (e.g. an incomplete struct) just use [0 x i8].
4233a2b657088de9413714a51bff153a59565adb3efChris Lattner    ResultType = ConvertTypeForMem(A->getElementType());
4243a2b657088de9413714a51bff153a59565adb3efChris Lattner    if (!ResultType->isSized()) {
4253a2b657088de9413714a51bff153a59565adb3efChris Lattner      SkippedLayout = true;
4263a2b657088de9413714a51bff153a59565adb3efChris Lattner      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
4273a2b657088de9413714a51bff153a59565adb3efChris Lattner    }
4283a2b657088de9413714a51bff153a59565adb3efChris Lattner    ResultType = llvm::ArrayType::get(ResultType, 0);
4299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
431fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
4329cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
4332acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
43401c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner
43501c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    // Lower arrays of undefined struct type to arrays of i8 just to have a
43601c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    // concrete type.
43701c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    if (!EltTy->isSized()) {
43801c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner      SkippedLayout = true;
43901c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner      EltTy = llvm::Type::getInt8Ty(getLLVMContext());
44001c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    }
44101c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner
4429cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
4439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
444fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  }
445213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
4465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Vector: {
4479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VectorType *VT = cast<VectorType>(Ty);
4489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
4499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                       VT->getNumElements());
4509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::FunctionNoProto:
453bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  case Type::FunctionProto: {
45471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    const FunctionType *FT = cast<FunctionType>(Ty);
455bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // First, check whether we can build the full function type.  If the
456bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // function type depends on an incomplete type (e.g. a struct or enum), we
4579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    // cannot lower the function type.
45871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!isFuncTypeConvertible(FT)) {
4599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // This function's type depends on an incomplete tag type.
4609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // Return a placeholder type.
4619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
46257eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner
46371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
4649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
465b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    }
4669cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
467f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // While we're converting the argument types for a function, we don't want
468f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // to recursively convert any pointed-to structs.  Converting directly-used
469f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // structs is ok though.
47071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!RecordsBeingLaidOut.insert(Ty)) {
47171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
47271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
47371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
47471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      break;
47571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    }
476f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
477b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // The function type can be built; call the appropriate routines to
478b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // build it.
479bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    const CGFunctionInfo *FI;
48071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
481de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      FI = &arrangeFunctionType(
4829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                   CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
483bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    } else {
48471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
485de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      FI = &arrangeFunctionType(
4869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
487bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    }
488f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
48971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    // If there is something higher level prodding our CGFunctionInfo, then
49071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    // don't recurse into it again.
49171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (FunctionsBeingProcessed.count(FI)) {
49271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
49371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
49471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
49571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    } else {
49671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
49771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      // Otherwise, we're good to go, go ahead and convert it.
498de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      ResultType = GetFunctionType(*FI);
49971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    }
50071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
50171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    RecordsBeingLaidOut.erase(Ty);
50257eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner
50357eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner    if (SkippedLayout)
50457eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner      TypeCache.clear();
505f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
50671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (RecordsBeingLaidOut.empty())
507f0a8679b6e6635117533b89894646f1450cea25bChris Lattner      while (!DeferredRecords.empty())
508f0a8679b6e6635117533b89894646f1450cea25bChris Lattner        ConvertRecordDeclType(DeferredRecords.pop_back_val());
5099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
510bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  }
5111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
512c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
5139cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
5149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
515c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
516391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  case Type::ObjCInterface: {
517412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // Objective-C interfaces are always opaque (outside of the
518412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // runtime, which can do whatever it likes); we never refine
519412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // these.
5209cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
521412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    if (!T)
522c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner      T = llvm::StructType::create(getLLVMContext());
5239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T;
5249cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
525391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  }
5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
52828e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // Protocol qualifications do not influence the LLVM type, we just return a
52928e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // pointer to the underlying interface type. We don't need to worry about
53028e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // recursive conversion.
5312acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *T =
532181eeee36b057a1a66227123fad0978d1d0f034aChris Lattner      ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
5339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T->getPointerTo();
5349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
53514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
53628e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar
5372045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner  case Type::Enum: {
5389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
5395e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall    if (ED->isCompleteDefinition() || ED->isFixed())
5409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      return ConvertType(ED->getIntegerType());
5412045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // Return a placeholder 'i32' type.  This can be changed later when the
5422045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // type is defined (see UpdateCompletedType), but is likely to be the
5432045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // "right" answer.
5442045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    ResultType = llvm::Type::getInt32Ty(getLLVMContext());
5459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
546de0efb3b6eac36bdeae0e60f753a974cc4118a31Chris Lattner  }
5479048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar
5489048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  case Type::BlockPointer: {
5499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
5509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(FTy);
551207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(FTy);
5529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
5539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
5549048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  }
555424c51d3d4bea87291919b75e73ca59386702ad5Sebastian Redl
5560e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  case Type::MemberPointer: {
5579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType =
5589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
5599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
5600e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  }
561b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
562b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
563b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    ResultType = ConvertTypeForMem(cast<AtomicType>(Ty)->getValueType());
564b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
565b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
5689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  assert(ResultType && "Didn't convert a type?");
5699cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
5709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  TypeCache[Ty] = ResultType;
5719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return ResultType;
5725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5749cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
5759cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
576efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // TagDecl's are not necessarily unique, instead use the (clang)
577efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // type connected to the decl.
5789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *&Entry = RecordDeclTypes[Key];
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have a StructType at all yet, create the forward declaration.
583cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner  if (Entry == 0) {
584c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner    Entry = llvm::StructType::create(getLLVMContext());
585cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner    addRecordTypeName(RD, Entry, "");
586cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner  }
5879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *Ty = Entry;
5885de00fcf7c923a14bb79bdbaabb2faeb5633d85aChris Lattner
5899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If this is still a forward declaration, or the LLVM type is already
5909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // complete, there's nothing more to do.
59171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  RD = RD->getDefinition();
5925e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque())
5939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
5949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
59571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If converting this type would cause us to infinitely loop, don't do it!
59671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (!isSafeToConvert(RD, *this)) {
5979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    DeferredRecords.push_back(RD);
5989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
5999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  }
6001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // Okay, this is a definition of a type.  Compile the implementation now.
60271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
60371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(InsertResult && "Recursively compiling a struct?");
60471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
60586ff308724171494395a840fd2efbe25e62f352eJohn McCall  // Force conversion of non-virtual base classes recursively.
6069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
6079cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
6089cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner         e = CRD->bases_end(); i != e; ++i) {
60971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      if (i->isVirtual()) continue;
61071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
61171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
61286ff308724171494395a840fd2efbe25e62f352eJohn McCall    }
61386ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
61486ff308724171494395a840fd2efbe25e62f352eJohn McCall
615696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  // Layout fields.
6169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
617696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  CGRecordLayouts[Key] = Layout;
6181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // We're done laying out this struct.
62071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
62171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
62271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
623f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // If this struct blocked a FunctionType conversion, then recompute whatever
624f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // was derived from that.
625f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // FIXME: This is hugely overconservative.
62657eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner  if (SkippedLayout)
62757eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner    TypeCache.clear();
62871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
62971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If we're done converting the outer-most record, then convert any deferred
63071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // structs as well.
63171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (RecordsBeingLaidOut.empty())
6329cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    while (!DeferredRecords.empty())
6339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ConvertRecordDeclType(DeferredRecords.pop_back_val());
6349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
6359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return Ty;
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
637fc3b8e9c1381d5e6ec361591d649c56a870ff971Chris Lattner
6382d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson/// getCGRecordLayout - Return record layout info for the given record decl.
639ad3e7118c40711faf5f51f08d599dbd525d3408aAnders Carlssonconst CGRecordLayout &
6402d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders CarlssonCodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
6412d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
64282926963cccbc54bc707d5dcf79389166046ef08Anders Carlsson
643270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
644c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  if (!Layout) {
6452d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson    // Compute the type information.
6469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ConvertRecordDeclType(RD);
647c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
648c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    // Now try again.
649c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    Layout = CGRecordLayouts.lookup(Key);
650c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  }
651c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
652270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  assert(Layout && "Unable to find record layout information for type");
653270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  return *Layout;
654b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
6553e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
656f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(QualType T) {
6573e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  // No need to check for member pointers when not compiling C++.
6583e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (!Context.getLangOptions().CPlusPlus)
659f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return true;
6603e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
6613e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  T = Context.getBaseElementType(T);
6623e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
663f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Records are non-zero-initializable if they contain any
664f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // non-zero-initializable subobjects.
6653e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const RecordType *RT = T->getAs<RecordType>()) {
6663e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
667f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return isZeroInitializable(RD);
6683e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  }
669f16aa103d3afd42fbca2ab346f191bf745cec092John McCall
670f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // We have to ask the ABI about member pointers.
6713e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
672f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return getCXXABI().isZeroInitializable(MPT);
6733e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
674f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Everything else is okay.
675f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return true;
6763e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson}
677c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson
678f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
6793379e9bd71c0e0051bd97e90d4f2ec964078091dAnders Carlsson  return getCGRecordLayout(RD).isZeroInitializable();
680c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson}
681