CodeGenTypes.cpp revision 5e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49
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"
18de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
19c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h"
20742cd1b7bb86b52b23b335d47abbd842dac0e1bfFariborz Jahanian#include "clang/AST/DeclCXX.h"
21de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/Expr.h"
2219cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/DerivedTypes.h"
244e533287e6a9adac78c9ac370612581aad9b8c5eAnders Carlsson#include "llvm/Module.h"
25d9e9ede6b1703849da739629904dad197306e527Devang Patel#include "llvm/Target/TargetData.h"
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
297a4718e813e5e99d478567a482217c7eef8572c5Devang PatelCodeGenTypes::CodeGenTypes(ASTContext &Ctx, llvm::Module& M,
30f16aa103d3afd42fbca2ab346f191bf745cec092John McCall                           const llvm::TargetData &TD, const ABIInfo &Info,
3108d4792bb5d9e0e7f122c6273636807029c06aaaDaniel Dunbar                           CGCXXABI &CXXABI, const CodeGenOptions &CGO)
32bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  : Context(Ctx), Target(Ctx.getTargetInfo()), TheModule(M), TheTargetData(TD),
3308d4792bb5d9e0e7f122c6273636807029c06aaaDaniel Dunbar    TheABIInfo(Info), TheCXXABI(CXXABI), CodeGenOpts(CGO) {
3457eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner  SkippedLayout = false;
35d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner}
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
37b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang PatelCodeGenTypes::~CodeGenTypes() {
381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
40b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel      I != E; ++I)
41b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel    delete I->second;
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) {
51e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  llvm::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())
61e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson      OS << RD->getQualifiedNameAsString();
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())
68e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson      OS << TDD->getQualifiedNameAsString();
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)) {
13471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    for (CXXRecordDecl::base_class_const_iterator I = CRD->bases_begin(),
13571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner         E = CRD->bases_end(); I != E; ++I)
13671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      if (!isSafeToConvert(I->getType()->getAs<RecordType>()->getDecl(),
13771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                           CGT, AlreadyChecked))
13871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner        return false;
13971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  }
14071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
14171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this type would require laying out members that are currently being laid
14271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // out, don't do it.
14371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  for (RecordDecl::field_iterator I = RD->field_begin(),
14471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner       E = RD->field_end(); I != E; ++I)
14571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
14671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      return false;
14771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
14871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If there are no problems, lets do it.
14971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return true;
15071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
15171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
15271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert this field type,
15371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// which requires the structure elements contained by-value to all be
15471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursively safe to convert.
15571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool
15671305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(QualType T, CodeGenTypes &CGT,
15771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
15871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  T = T.getCanonicalType();
15971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
16071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this is a record, check it.
16171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(T))
16271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
16371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
16471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this is an array, check the elements, which are embedded inline.
16571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (const ArrayType *AT = dyn_cast<ArrayType>(T))
16671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
16771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
16871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // Otherwise, there is no concern about transforming this.  We only care about
16971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // things that are contained by-value in a structure that can have another
17071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // structure as a member.
17171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return true;
17271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
17371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
17471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
17571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert the specified record
17671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// decl to IR and lay it out, false if doing so would cause us to get into a
17771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursive compilation mess.
17871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
17971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If no structs are being laid out, we can certainly do this one.
18071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (CGT.noRecordsBeingLaidOut()) return true;
18171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
18271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
18371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return isSafeToConvert(RD, CGT, AlreadyChecked);
18471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
18571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
18671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
187f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// isFuncTypeArgumentConvertible - Return true if the specified type in a
188f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// function argument or result position can be converted to an IR type at this
189f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// point.  This boils down to being whether it is complete, as well as whether
190f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// we've temporarily deferred expanding the type because we're in a recursive
191f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// context.
19271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerbool CodeGenTypes::isFuncTypeArgumentConvertible(QualType Ty) {
193f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // If this isn't a tagged type, we can convert it!
194f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  const TagType *TT = Ty->getAs<TagType>();
195f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (TT == 0) return true;
196f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
197f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
19871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If it's a tagged type used by-value, but is just a forward decl, we can't
19971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // convert it.  Note that getDefinition()==0 is not the same as !isDefinition.
20071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (TT->getDecl()->getDefinition() == 0)
201f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    return false;
202f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
20371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this is an enum, then it is always safe to convert.
204f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  const RecordType *RT = dyn_cast<RecordType>(TT);
205f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (RT == 0) return true;
206f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
207f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // Otherwise, we have to be careful.  If it is a struct that we're in the
208f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // process of expanding, then we can't convert the function type.  That's ok
209f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // though because we must be in a pointer context under the struct, so we can
210f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // just convert it to a dummy type.
211f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  //
212f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // We decide this by checking whether ConvertRecordDeclType returns us an
213f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // opaque type for a struct that we know is defined.
21471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return isSafeToConvert(RT->getDecl(), *this);
215f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner}
216f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
217f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
218f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// Code to verify a given function type is complete, i.e. the return type
219f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// and all of the argument types are complete.  Also check to see if we are in
220f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// a RS_StructPointer context, and if so whether any struct types have been
221f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// pended.  If so, we don't want to ask the ABI lowering code to handle a type
222f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// that cannot be converted to an IR type.
223f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattnerbool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
224f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (!isFuncTypeArgumentConvertible(FT->getResultType()))
225f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    return false;
226f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
227f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
2289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
229f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner      if (!isFuncTypeArgumentConvertible(FPT->getArgType(i)))
230f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner        return false;
231f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
232f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  return true;
233b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman}
234b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman
235c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// UpdateCompletedType - When we find the full definition for a TagDecl,
236c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// replace the 'opaque' type we previously made for it if applicable.
237c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
2389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If this is an enum being completed, then we flush all non-struct types from
2399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // the cache.  This allows function types and other things that may be derived
2409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // from the enum to be recomputed.
2418dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner  if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
2428dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner    // Only flush the cache if we've actually already converted this type.
2432045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    if (TypeCache.count(ED->getTypeForDecl())) {
2442045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      // Okay, we formed some types based on this.  We speculated that the enum
2452045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      // would be lowered to i32, so we only need to flush the cache if this
2462045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      // didn't happen.
2472045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
2482045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner        TypeCache.clear();
2492045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    }
2509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return;
251b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman  }
2529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
253f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // If we completed a RecordDecl that we previously used and converted to an
254f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // anonymous type, then go ahead and complete it now.
2559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const RecordDecl *RD = cast<RecordDecl>(TD);
256f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  if (RD->isDependentType()) return;
257f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
258f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // Only complete it if we converted it already.  If we haven't converted it
259f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // yet, we'll just do it lazily.
2603ade97504790bbc5a148baa5b3f7e577aed9c1c6Chris Lattner  if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
2619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ConvertRecordDeclType(RD);
262d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner}
263d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
2649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
2659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                    const llvm::fltSemantics &format) {
266b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEsingle)
2670032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFloatTy(VMContext);
268b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEdouble)
2690032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getDoubleTy(VMContext);
270b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEquad)
2710032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFP128Ty(VMContext);
272b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::PPCDoubleDouble)
2730032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getPPC_FP128Ty(VMContext);
274b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::x87DoubleExtended)
2750032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getX86_FP80Ty(VMContext);
276b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unknown float format!");
277f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman}
278f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman
2799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertType - Convert the specified type to its LLVM form.
2809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertType(QualType T) {
2819cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  T = Context.getCanonicalType(T);
2829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Ty = T.getTypePtr();
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // RecordTypes are cached and processed specially.
2869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(Ty))
2879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return ConvertRecordDeclType(RT->getDecl());
2889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // See if type is already cached.
2909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
2919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If type is found in map then use it. Otherwise, convert type T.
2929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (TCI != TypeCache.end())
2939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return TCI->second;
2949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have it in the cache, convert it now.
2969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResultType = 0;
2979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  switch (Ty->getTypeClass()) {
2989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  case Type::Record: // Handled above.
29972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
30072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
30172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
30272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
303ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
30472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
305864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    llvm_unreachable("Non-canonical or dependent types aren't possible.");
30672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
30772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
3085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Builtin: {
3099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    switch (cast<BuiltinType>(Ty)->getKind()) {
3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Void:
311de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCId:
312de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCClass:
31313dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    case BuiltinType::ObjCSel:
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LLVM void type can only be used as the result of a function call.  Just
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // map to the same as char.
3169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
3179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Bool:
32019009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner      // Note that we always return bool as i1 for use as a scalar type.
3219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt1Ty(getLLVMContext());
3229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
324d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_S:
325d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_U:
326d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::SChar:
327d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::UChar:
3285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Short:
3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UShort:
3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Int:
3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UInt:
3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Long:
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULong:
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongLong:
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULongLong:
3363f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
3373f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
338f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
339f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
3409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(),
3419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 static_cast<unsigned>(Context.getTypeSize(T)));
3429cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
344f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman    case BuiltinType::Float:
345c8b1227fa8c17d9881815e40c04e19334be536f8Nate Begeman    case BuiltinType::Double:
3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongDouble:
3479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = getTypeForFormat(getLLVMContext(),
3489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                    Context.getFloatTypeSemantics(T));
3499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    case BuiltinType::NullPtr:
352c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson      // Model std::nullptr_t as i8*
3539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
3549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
355c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson
3562df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::UInt128:
3572df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::Int128:
3589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
3599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3608c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman
3618c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman    case BuiltinType::Overload:
3628c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman    case BuiltinType::Dependent:
363864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    case BuiltinType::BoundMember:
3641de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall    case BuiltinType::UnknownAny:
365864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall      llvm_unreachable("Unexpected placeholder builtin type!");
3668c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman      break;
3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Complex: {
371ef6de3da8572607f786303c07150daa6e140ab19Jay Foad    llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
3729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
3739cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
3767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference: {
3779cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ReferenceType *RTy = cast<ReferenceType>(Ty);
3789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = RTy->getPointeeType();
3799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
380207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
3819cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
3829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3836aeae7fa9cfaacba3a4077d62c01c2531d88a63eDaniel Dunbar  }
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Pointer: {
3859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const PointerType *PTy = cast<PointerType>(Ty);
3869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = PTy->getPointeeType();
3879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
3889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    if (PointeeType->isVoidTy())
3899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
390207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
3919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
3929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
395fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray: {
3969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VariableArrayType *A = cast<VariableArrayType>(Ty);
3979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
3985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "FIXME: We only handle trivial array types so far!");
399c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // VLAs resolve to the innermost element type; this matches
400c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // the return of alloca, and there isn't any obviously better choice.
4019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertTypeForMem(A->getElementType());
4029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
403c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  }
404c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  case Type::IncompleteArray: {
4059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
4069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
407c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman           "FIXME: We only handle trivial array types so far!");
4083a2b657088de9413714a51bff153a59565adb3efChris Lattner    // int X[] -> [0 x int], unless the element type is not sized.  If it is
4093a2b657088de9413714a51bff153a59565adb3efChris Lattner    // unsized (e.g. an incomplete struct) just use [0 x i8].
4103a2b657088de9413714a51bff153a59565adb3efChris Lattner    ResultType = ConvertTypeForMem(A->getElementType());
4113a2b657088de9413714a51bff153a59565adb3efChris Lattner    if (!ResultType->isSized()) {
4123a2b657088de9413714a51bff153a59565adb3efChris Lattner      SkippedLayout = true;
4133a2b657088de9413714a51bff153a59565adb3efChris Lattner      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
4143a2b657088de9413714a51bff153a59565adb3efChris Lattner    }
4153a2b657088de9413714a51bff153a59565adb3efChris Lattner    ResultType = llvm::ArrayType::get(ResultType, 0);
4169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
418fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
4199cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
4202acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
42101c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner
42201c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    // Lower arrays of undefined struct type to arrays of i8 just to have a
42301c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    // concrete type.
42401c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    if (!EltTy->isSized()) {
42501c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner      SkippedLayout = true;
42601c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner      EltTy = llvm::Type::getInt8Ty(getLLVMContext());
42701c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    }
42801c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner
4299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
4309cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
431fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  }
432213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
4335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Vector: {
4349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VectorType *VT = cast<VectorType>(Ty);
4359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
4369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                       VT->getNumElements());
4379cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::FunctionNoProto:
440bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  case Type::FunctionProto: {
44171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    const FunctionType *FT = cast<FunctionType>(Ty);
442bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // First, check whether we can build the full function type.  If the
443bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // function type depends on an incomplete type (e.g. a struct or enum), we
4449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    // cannot lower the function type.
44571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!isFuncTypeConvertible(FT)) {
4469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // This function's type depends on an incomplete tag type.
4479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // Return a placeholder type.
4489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
44957eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner
45071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
4519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
452b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    }
4539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
454f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // While we're converting the argument types for a function, we don't want
455f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // to recursively convert any pointed-to structs.  Converting directly-used
456f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // structs is ok though.
45771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!RecordsBeingLaidOut.insert(Ty)) {
45871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
45971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
46071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
46171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      break;
46271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    }
463f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
464b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // The function type can be built; call the appropriate routines to
465b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // build it.
466bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    const CGFunctionInfo *FI;
467bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    bool isVariadic;
46871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
469bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      FI = &getFunctionInfo(
4709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                   CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
471bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      isVariadic = FPT->isVariadic();
472bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    } else {
47371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
474bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      FI = &getFunctionInfo(
4759cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
476bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      isVariadic = true;
477bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    }
478f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
47971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    // If there is something higher level prodding our CGFunctionInfo, then
48071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    // don't recurse into it again.
48171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (FunctionsBeingProcessed.count(FI)) {
48271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
48371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
48471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
48571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    } else {
48671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
48771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      // Otherwise, we're good to go, go ahead and convert it.
48871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ResultType = GetFunctionType(*FI, isVariadic);
48971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    }
49071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
49171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    RecordsBeingLaidOut.erase(Ty);
49257eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner
49357eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner    if (SkippedLayout)
49457eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner      TypeCache.clear();
495f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
49671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (RecordsBeingLaidOut.empty())
497f0a8679b6e6635117533b89894646f1450cea25bChris Lattner      while (!DeferredRecords.empty())
498f0a8679b6e6635117533b89894646f1450cea25bChris Lattner        ConvertRecordDeclType(DeferredRecords.pop_back_val());
4999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
500bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  }
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
502c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
5039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
5049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
505c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
506391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  case Type::ObjCInterface: {
507412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // Objective-C interfaces are always opaque (outside of the
508412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // runtime, which can do whatever it likes); we never refine
509412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // these.
5109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
511412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    if (!T)
512c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner      T = llvm::StructType::create(getLLVMContext());
5139cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T;
5149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
515391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  }
5161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
51828e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // Protocol qualifications do not influence the LLVM type, we just return a
51928e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // pointer to the underlying interface type. We don't need to worry about
52028e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // recursive conversion.
5212acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *T =
522181eeee36b057a1a66227123fad0978d1d0f034aChris Lattner      ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
5239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T->getPointerTo();
5249cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
52514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
52628e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar
5272045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner  case Type::Enum: {
5289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
5295e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall    if (ED->isCompleteDefinition() || ED->isFixed())
5309cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      return ConvertType(ED->getIntegerType());
5312045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // Return a placeholder 'i32' type.  This can be changed later when the
5322045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // type is defined (see UpdateCompletedType), but is likely to be the
5332045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // "right" answer.
5342045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    ResultType = llvm::Type::getInt32Ty(getLLVMContext());
5359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
536de0efb3b6eac36bdeae0e60f753a974cc4118a31Chris Lattner  }
5379048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar
5389048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  case Type::BlockPointer: {
5399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
5409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(FTy);
541207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(FTy);
5429cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
5439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
5449048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  }
545424c51d3d4bea87291919b75e73ca59386702ad5Sebastian Redl
5460e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  case Type::MemberPointer: {
5479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType =
5489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
5499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
5500e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  }
551b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
552b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
553b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    ResultType = ConvertTypeForMem(cast<AtomicType>(Ty)->getValueType());
554b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
555b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
5589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  assert(ResultType && "Didn't convert a type?");
5599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
5609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  TypeCache[Ty] = ResultType;
5619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return ResultType;
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
5659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
566efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // TagDecl's are not necessarily unique, instead use the (clang)
567efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // type connected to the decl.
5689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *&Entry = RecordDeclTypes[Key];
5711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have a StructType at all yet, create the forward declaration.
573cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner  if (Entry == 0) {
574c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner    Entry = llvm::StructType::create(getLLVMContext());
575cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner    addRecordTypeName(RD, Entry, "");
576cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner  }
5779cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *Ty = Entry;
5785de00fcf7c923a14bb79bdbaabb2faeb5633d85aChris Lattner
5799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If this is still a forward declaration, or the LLVM type is already
5809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // complete, there's nothing more to do.
58171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  RD = RD->getDefinition();
5825e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque())
5839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
5849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
58571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If converting this type would cause us to infinitely loop, don't do it!
58671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (!isSafeToConvert(RD, *this)) {
5879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    DeferredRecords.push_back(RD);
5889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
5899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  }
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // Okay, this is a definition of a type.  Compile the implementation now.
59271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
59371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(InsertResult && "Recursively compiling a struct?");
59471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
59586ff308724171494395a840fd2efbe25e62f352eJohn McCall  // Force conversion of non-virtual base classes recursively.
5969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
5979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
5989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner         e = CRD->bases_end(); i != e; ++i) {
59971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      if (i->isVirtual()) continue;
60071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
60171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
60286ff308724171494395a840fd2efbe25e62f352eJohn McCall    }
60386ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
60486ff308724171494395a840fd2efbe25e62f352eJohn McCall
605696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  // Layout fields.
6069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
607696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  CGRecordLayouts[Key] = Layout;
6081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
60971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // We're done laying out this struct.
61071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
61171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
61271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
613f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // If this struct blocked a FunctionType conversion, then recompute whatever
614f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // was derived from that.
615f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // FIXME: This is hugely overconservative.
61657eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner  if (SkippedLayout)
61757eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner    TypeCache.clear();
61871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
61971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If we're done converting the outer-most record, then convert any deferred
62071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // structs as well.
62171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (RecordsBeingLaidOut.empty())
6229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    while (!DeferredRecords.empty())
6239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ConvertRecordDeclType(DeferredRecords.pop_back_val());
6249cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
6259cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return Ty;
6261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
627fc3b8e9c1381d5e6ec361591d649c56a870ff971Chris Lattner
6282d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson/// getCGRecordLayout - Return record layout info for the given record decl.
629ad3e7118c40711faf5f51f08d599dbd525d3408aAnders Carlssonconst CGRecordLayout &
6302d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders CarlssonCodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
6312d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
63282926963cccbc54bc707d5dcf79389166046ef08Anders Carlsson
633270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
634c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  if (!Layout) {
6352d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson    // Compute the type information.
6369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ConvertRecordDeclType(RD);
637c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
638c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    // Now try again.
639c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    Layout = CGRecordLayouts.lookup(Key);
640c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  }
641c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
642270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  assert(Layout && "Unable to find record layout information for type");
643270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  return *Layout;
644b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
6453e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
646f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(QualType T) {
6473e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  // No need to check for member pointers when not compiling C++.
6483e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (!Context.getLangOptions().CPlusPlus)
649f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return true;
6503e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
6513e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  T = Context.getBaseElementType(T);
6523e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
653f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Records are non-zero-initializable if they contain any
654f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // non-zero-initializable subobjects.
6553e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const RecordType *RT = T->getAs<RecordType>()) {
6563e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
657f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return isZeroInitializable(RD);
6583e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  }
659f16aa103d3afd42fbca2ab346f191bf745cec092John McCall
660f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // We have to ask the ABI about member pointers.
6613e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
662f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return getCXXABI().isZeroInitializable(MPT);
6633e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
664f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Everything else is okay.
665f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return true;
6663e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson}
667c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson
668f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
6693379e9bd71c0e0051bd97e90d4f2ec964078091dAnders Carlsson  return getCGRecordLayout(RD).isZeroInitializable();
670c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson}
671