CodeGenTypes.cpp revision 0f3d0970dcdf6cf17550b86838dff12813968dbc
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;
198bcf38f2782d9cada139e22aa1ab8df10c9b047e5Douglas Gregor
199bcf38f2782d9cada139e22aa1ab8df10c9b047e5Douglas Gregor  // Incomplete types cannot be converted.
2004a59bc26b3f2d00055e24332c3164c894fafac27Douglas Gregor  if (TT->isIncompleteType())
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) {
266aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  if (&format == &llvm::APFloat::IEEEhalf)
267aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    return llvm::Type::getInt16Ty(VMContext);
268b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEsingle)
2690032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFloatTy(VMContext);
270b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEdouble)
2710032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getDoubleTy(VMContext);
272b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEquad)
2730032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFP128Ty(VMContext);
274b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::PPCDoubleDouble)
2750032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getPPC_FP128Ty(VMContext);
276b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::x87DoubleExtended)
2770032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getX86_FP80Ty(VMContext);
278b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unknown float format!");
279f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman}
280f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman
2819cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertType - Convert the specified type to its LLVM form.
2829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertType(QualType T) {
2839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  T = Context.getCanonicalType(T);
2849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Ty = T.getTypePtr();
2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // RecordTypes are cached and processed specially.
2889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(Ty))
2899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return ConvertRecordDeclType(RT->getDecl());
2909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // See if type is already cached.
2929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
2939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If type is found in map then use it. Otherwise, convert type T.
2949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (TCI != TypeCache.end())
2959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return TCI->second;
2969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have it in the cache, convert it now.
2989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResultType = 0;
2999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  switch (Ty->getTypeClass()) {
3009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  case Type::Record: // Handled above.
30172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
30272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
30372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
30472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
305ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
30672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
307864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    llvm_unreachable("Non-canonical or dependent types aren't possible.");
30872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
3095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Builtin: {
3109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    switch (cast<BuiltinType>(Ty)->getKind()) {
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Void:
312de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCId:
313de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCClass:
31413dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    case BuiltinType::ObjCSel:
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LLVM void type can only be used as the result of a function call.  Just
3165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // map to the same as char.
3179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
3189cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Bool:
32119009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner      // Note that we always return bool as i1 for use as a scalar type.
3229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt1Ty(getLLVMContext());
3239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
325d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_S:
326d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_U:
327d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::SChar:
328d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::UChar:
3295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Short:
3305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UShort:
3315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Int:
3325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UInt:
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Long:
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULong:
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongLong:
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULongLong:
3373f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
3383f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
339f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
340f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
3419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(),
3429cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 static_cast<unsigned>(Context.getTypeSize(T)));
3439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
345aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    case BuiltinType::Half:
346aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // Half is special: it might be lowered to i16 (and will be storage-only
347aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // type),. or can be represented as a set of native operations.
348aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
349aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // FIXME: Ask target which kind of half FP it prefers (storage only vs
350aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      // native).
351aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      ResultType = llvm::Type::getInt16Ty(getLLVMContext());
352aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      break;
353f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman    case BuiltinType::Float:
354c8b1227fa8c17d9881815e40c04e19334be536f8Nate Begeman    case BuiltinType::Double:
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongDouble:
3569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = getTypeForFormat(getLLVMContext(),
3579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                    Context.getFloatTypeSemantics(T));
3589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    case BuiltinType::NullPtr:
361c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson      // Model std::nullptr_t as i8*
3629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
3639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
364c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson
3652df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::UInt128:
3662df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::Int128:
3679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
3689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3698c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman
3708c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman    case BuiltinType::Dependent:
3712dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define BUILTIN_TYPE(Id, SingletonId)
3722dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define PLACEHOLDER_TYPE(Id, SingletonId) \
3732dde35bc626153492f5f58202506c88a27fbff5bJohn McCall    case BuiltinType::Id:
3742dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#include "clang/AST/BuiltinTypes.def"
375864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall      llvm_unreachable("Unexpected placeholder builtin type!");
3765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
3785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Complex: {
380ef6de3da8572607f786303c07150daa6e140ab19Jay Foad    llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
3819cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
3829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3847c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
3857c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference: {
3869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ReferenceType *RTy = cast<ReferenceType>(Ty);
3879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = RTy->getPointeeType();
3889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
389207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
3909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
3919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3926aeae7fa9cfaacba3a4077d62c01c2531d88a63eDaniel Dunbar  }
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Pointer: {
3949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const PointerType *PTy = cast<PointerType>(Ty);
3959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = PTy->getPointeeType();
3969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
3979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    if (PointeeType->isVoidTy())
3989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
399207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
4009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
4019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray: {
4059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VariableArrayType *A = cast<VariableArrayType>(Ty);
4069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
4075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "FIXME: We only handle trivial array types so far!");
408c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // VLAs resolve to the innermost element type; this matches
409c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // the return of alloca, and there isn't any obviously better choice.
4109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertTypeForMem(A->getElementType());
4119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
412c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  }
413c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  case Type::IncompleteArray: {
4149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
4159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
416c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman           "FIXME: We only handle trivial array types so far!");
4173a2b657088de9413714a51bff153a59565adb3efChris Lattner    // int X[] -> [0 x int], unless the element type is not sized.  If it is
4183a2b657088de9413714a51bff153a59565adb3efChris Lattner    // unsized (e.g. an incomplete struct) just use [0 x i8].
4193a2b657088de9413714a51bff153a59565adb3efChris Lattner    ResultType = ConvertTypeForMem(A->getElementType());
4203a2b657088de9413714a51bff153a59565adb3efChris Lattner    if (!ResultType->isSized()) {
4213a2b657088de9413714a51bff153a59565adb3efChris Lattner      SkippedLayout = true;
4223a2b657088de9413714a51bff153a59565adb3efChris Lattner      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
4233a2b657088de9413714a51bff153a59565adb3efChris Lattner    }
4243a2b657088de9413714a51bff153a59565adb3efChris Lattner    ResultType = llvm::ArrayType::get(ResultType, 0);
4259cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
427fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
4289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
4292acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
43001c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner
43101c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    // Lower arrays of undefined struct type to arrays of i8 just to have a
43201c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    // concrete type.
43301c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    if (!EltTy->isSized()) {
43401c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner      SkippedLayout = true;
43501c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner      EltTy = llvm::Type::getInt8Ty(getLLVMContext());
43601c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    }
43701c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner
4389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
4399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
440fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  }
441213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Vector: {
4439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VectorType *VT = cast<VectorType>(Ty);
4449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
4459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                       VT->getNumElements());
4469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::FunctionNoProto:
449bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  case Type::FunctionProto: {
45071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    const FunctionType *FT = cast<FunctionType>(Ty);
451bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // First, check whether we can build the full function type.  If the
452bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // function type depends on an incomplete type (e.g. a struct or enum), we
4539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    // cannot lower the function type.
45471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!isFuncTypeConvertible(FT)) {
4559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // This function's type depends on an incomplete tag type.
4569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // Return a placeholder type.
4579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
45857eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner
45971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
4609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
461b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    }
4629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
463f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // While we're converting the argument types for a function, we don't want
464f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // to recursively convert any pointed-to structs.  Converting directly-used
465f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // structs is ok though.
46671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!RecordsBeingLaidOut.insert(Ty)) {
46771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
46871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
46971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
47071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      break;
47171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    }
472f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
473b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // The function type can be built; call the appropriate routines to
474b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // build it.
475bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    const CGFunctionInfo *FI;
47671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
4770f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall      FI = &arrangeFreeFunctionType(
4789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                   CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
479bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    } else {
48071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
4810f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall      FI = &arrangeFreeFunctionType(
4829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
483bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    }
484f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
48571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    // If there is something higher level prodding our CGFunctionInfo, then
48671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    // don't recurse into it again.
48771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (FunctionsBeingProcessed.count(FI)) {
48871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
48971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
49071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
49171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    } else {
49271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
49371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      // Otherwise, we're good to go, go ahead and convert it.
494de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      ResultType = GetFunctionType(*FI);
49571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    }
49671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
49771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    RecordsBeingLaidOut.erase(Ty);
49857eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner
49957eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner    if (SkippedLayout)
50057eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner      TypeCache.clear();
501f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
50271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (RecordsBeingLaidOut.empty())
503f0a8679b6e6635117533b89894646f1450cea25bChris Lattner      while (!DeferredRecords.empty())
504f0a8679b6e6635117533b89894646f1450cea25bChris Lattner        ConvertRecordDeclType(DeferredRecords.pop_back_val());
5059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
506bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  }
5071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
508c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
5099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
5109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
511c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
512391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  case Type::ObjCInterface: {
513412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // Objective-C interfaces are always opaque (outside of the
514412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // runtime, which can do whatever it likes); we never refine
515412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // these.
5169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
517412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    if (!T)
518c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner      T = llvm::StructType::create(getLLVMContext());
5199cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T;
5209cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
521391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  }
5221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
52428e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // Protocol qualifications do not influence the LLVM type, we just return a
52528e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // pointer to the underlying interface type. We don't need to worry about
52628e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // recursive conversion.
5272acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *T =
528181eeee36b057a1a66227123fad0978d1d0f034aChris Lattner      ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
5299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T->getPointerTo();
5309cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
53114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
53228e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar
5332045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner  case Type::Enum: {
5349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
5355e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall    if (ED->isCompleteDefinition() || ED->isFixed())
5369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      return ConvertType(ED->getIntegerType());
5372045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // Return a placeholder 'i32' type.  This can be changed later when the
5382045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // type is defined (see UpdateCompletedType), but is likely to be the
5392045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // "right" answer.
5402045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    ResultType = llvm::Type::getInt32Ty(getLLVMContext());
5419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
542de0efb3b6eac36bdeae0e60f753a974cc4118a31Chris Lattner  }
5439048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar
5449048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  case Type::BlockPointer: {
5459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
5469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(FTy);
547207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(FTy);
5489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
5499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
5509048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  }
551424c51d3d4bea87291919b75e73ca59386702ad5Sebastian Redl
5520e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  case Type::MemberPointer: {
5539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType =
5549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
5559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
5560e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  }
557b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
558b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
55947bfcca2d6972d98a1b25239cd1aa658b60680e2Douglas Gregor    ResultType = ConvertType(cast<AtomicType>(Ty)->getValueType());
560b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
561b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
5649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  assert(ResultType && "Didn't convert a type?");
5659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
5669cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  TypeCache[Ty] = ResultType;
5679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return ResultType;
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
5719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
572efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // TagDecl's are not necessarily unique, instead use the (clang)
573efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // type connected to the decl.
5749cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5769cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *&Entry = RecordDeclTypes[Key];
5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have a StructType at all yet, create the forward declaration.
579cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner  if (Entry == 0) {
580c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner    Entry = llvm::StructType::create(getLLVMContext());
581cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner    addRecordTypeName(RD, Entry, "");
582cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner  }
5839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *Ty = Entry;
5845de00fcf7c923a14bb79bdbaabb2faeb5633d85aChris Lattner
5859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If this is still a forward declaration, or the LLVM type is already
5869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // complete, there's nothing more to do.
58771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  RD = RD->getDefinition();
5885e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque())
5899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
5909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
59171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If converting this type would cause us to infinitely loop, don't do it!
59271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (!isSafeToConvert(RD, *this)) {
5939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    DeferredRecords.push_back(RD);
5949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
5959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  }
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // Okay, this is a definition of a type.  Compile the implementation now.
59871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
59971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(InsertResult && "Recursively compiling a struct?");
60071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
60186ff308724171494395a840fd2efbe25e62f352eJohn McCall  // Force conversion of non-virtual base classes recursively.
6029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
6039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
6049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner         e = CRD->bases_end(); i != e; ++i) {
60571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      if (i->isVirtual()) continue;
60671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
60771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
60886ff308724171494395a840fd2efbe25e62f352eJohn McCall    }
60986ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
61086ff308724171494395a840fd2efbe25e62f352eJohn McCall
611696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  // Layout fields.
6129cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
613696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  CGRecordLayouts[Key] = Layout;
6141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // We're done laying out this struct.
61671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
61771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
61871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
619f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // If this struct blocked a FunctionType conversion, then recompute whatever
620f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // was derived from that.
621f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // FIXME: This is hugely overconservative.
62257eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner  if (SkippedLayout)
62357eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner    TypeCache.clear();
62471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
62571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If we're done converting the outer-most record, then convert any deferred
62671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // structs as well.
62771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (RecordsBeingLaidOut.empty())
6289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    while (!DeferredRecords.empty())
6299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ConvertRecordDeclType(DeferredRecords.pop_back_val());
6309cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
6319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return Ty;
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
633fc3b8e9c1381d5e6ec361591d649c56a870ff971Chris Lattner
6342d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson/// getCGRecordLayout - Return record layout info for the given record decl.
635ad3e7118c40711faf5f51f08d599dbd525d3408aAnders Carlssonconst CGRecordLayout &
6362d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders CarlssonCodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
6372d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
63882926963cccbc54bc707d5dcf79389166046ef08Anders Carlsson
639270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
640c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  if (!Layout) {
6412d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson    // Compute the type information.
6429cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ConvertRecordDeclType(RD);
643c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
644c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    // Now try again.
645c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    Layout = CGRecordLayouts.lookup(Key);
646c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  }
647c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
648270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  assert(Layout && "Unable to find record layout information for type");
649270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  return *Layout;
650b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
6513e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
652f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(QualType T) {
6533e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  // No need to check for member pointers when not compiling C++.
6544e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!Context.getLangOpts().CPlusPlus)
655f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return true;
6563e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
6573e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  T = Context.getBaseElementType(T);
6583e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
659f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Records are non-zero-initializable if they contain any
660f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // non-zero-initializable subobjects.
6613e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const RecordType *RT = T->getAs<RecordType>()) {
6623e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
663f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return isZeroInitializable(RD);
6643e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  }
665f16aa103d3afd42fbca2ab346f191bf745cec092John McCall
666f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // We have to ask the ABI about member pointers.
6673e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
668f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return getCXXABI().isZeroInitializable(MPT);
6693e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
670f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Everything else is okay.
671f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return true;
6723e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson}
673c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson
674f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
6753379e9bd71c0e0051bd97e90d4f2ec964078091dAnders Carlsson  return getCGRecordLayout(RD).isZeroInitializable();
676c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson}
677