CodeGenTypes.cpp revision 8dd5cdfc462026648b480adaf774e24bc620f7c3
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)
326b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar  : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD),
3308d4792bb5d9e0e7f122c6273636807029c06aaaDaniel Dunbar    TheABIInfo(Info), TheCXXABI(CXXABI), CodeGenOpts(CGO) {
34d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner}
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
36b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang PatelCodeGenTypes::~CodeGenTypes() {
371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
39b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel      I != E; ++I)
40b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel    delete I->second;
416f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner
426f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner  for (llvm::FoldingSet<CGFunctionInfo>::iterator
436f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner       I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
446f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner    delete &*I++;
45b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
46b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel
479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnervoid CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     llvm::StructType *Ty,
49e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson                                     llvm::StringRef suffix) {
50e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  llvm::SmallString<256> TypeName;
51e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  llvm::raw_svector_ostream OS(TypeName);
52e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson  OS << RD->getKindName() << '.';
53e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson
54e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  // Name the codegen type after the typedef name
55e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  // if there is no tag type name available
56e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson  if (RD->getIdentifier()) {
57e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // FIXME: We should not have to check for a null decl context here.
58e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // Right now we do it because the implicit Obj-C decls don't have one.
59e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson    if (RD->getDeclContext())
60e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson      OS << RD->getQualifiedNameAsString();
61e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    else
62e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson      RD->printName(OS);
63e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson  } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
64e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // FIXME: We should not have to check for a null decl context here.
65e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // Right now we do it because the implicit Obj-C decls don't have one.
66e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    if (TDD->getDeclContext())
67e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson      OS << TDD->getQualifiedNameAsString();
68e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    else
69e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson      TDD->printName(OS);
70e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  } else
71e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    OS << "anon";
72e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson
73e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  if (!suffix.empty())
74e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    OS << suffix;
75e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson
769cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  Ty->setName(OS.str());
7730ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel}
7830ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel
794581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
804581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertType in that it is used to convert to the memory representation for
814581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// a type.  For example, the scalar representation for _Bool is i1, but the
824581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// memory representation is usually i8 or i32, depending on the target.
839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){
849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *R = ConvertType(T);
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8619009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner  // If this is a non-bool type, don't map it.
87f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands  if (!R->isIntegerTy(1))
8819009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner    return R;
891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9019009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner  // Otherwise, return an integer of the target-specified size.
910032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  return llvm::IntegerType::get(getLLVMContext(),
920032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                (unsigned)Context.getTypeSize(T));
931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9419009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner}
9519009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner
96b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman// Code to verify a given function type is complete, i.e. the return type
97b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman// and all of the argument types are complete.
98c00129a08c829bb34f22dc13727043c994c85644Eli Friedmanconst TagType *CodeGenTypes::VerifyFuncTypeComplete(const Type* T) {
99b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman  const FunctionType *FT = cast<FunctionType>(T);
1006217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const TagType* TT = FT->getResultType()->getAs<TagType>())
101b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    if (!TT->getDecl()->isDefinition())
102b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman      return TT;
103b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T))
1049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
1059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      if (const TagType *TT = FPT->getArgType(i)->getAs<TagType>())
106b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman        if (!TT->getDecl()->isDefinition())
107b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman          return TT;
108b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman  return 0;
109b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman}
110b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman
111c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// UpdateCompletedType - When we find the full definition for a TagDecl,
112c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// replace the 'opaque' type we previously made for it if applicable.
113c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
1149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If this is an enum being completed, then we flush all non-struct types from
1159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // the cache.  This allows function types and other things that may be derived
1169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // from the enum to be recomputed.
1178dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner  if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
1188dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner    // Only flush the cache if we've actually already converted this type.
1198dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner    if (TypeCache.count(ED->getTypeForDecl()))
1208dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner      TypeCache.clear();
1219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return;
122b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman  }
1239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
1249cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const RecordDecl *RD = cast<RecordDecl>(TD);
1259cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (!RD->isDependentType())
1269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ConvertRecordDeclType(RD);
127d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner}
128d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
1299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
1309cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                    const llvm::fltSemantics &format) {
131b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEsingle)
1320032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFloatTy(VMContext);
133b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEdouble)
1340032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getDoubleTy(VMContext);
135b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEquad)
1360032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFP128Ty(VMContext);
137b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::PPCDoubleDouble)
1380032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getPPC_FP128Ty(VMContext);
139b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::x87DoubleExtended)
1400032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getX86_FP80Ty(VMContext);
141b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(0 && "Unknown float format!");
142f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman  return 0;
143f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman}
144f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman
1459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertType - Convert the specified type to its LLVM form.
1469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertType(QualType T) {
1479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  T = Context.getCanonicalType(T);
1489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
1499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Ty = T.getTypePtr();
1501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // RecordTypes are cached and processed specially.
1529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return ConvertRecordDeclType(RT->getDecl());
1549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
1559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // See if type is already cached.
1569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
1579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If type is found in map then use it. Otherwise, convert type T.
1589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (TCI != TypeCache.end())
1599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return TCI->second;
1609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
1619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have it in the cache, convert it now.
1629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResultType = 0;
1639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  switch (Ty->getTypeClass()) {
1649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  case Type::Record: // Handled above.
16572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
16672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
16772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
16872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
169ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
17072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
171864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    llvm_unreachable("Non-canonical or dependent types aren't possible.");
17272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
17372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Builtin: {
1759cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    switch (cast<BuiltinType>(Ty)->getKind()) {
1765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Void:
177de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCId:
178de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCClass:
17913dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    case BuiltinType::ObjCSel:
1805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LLVM void type can only be used as the result of a function call.  Just
1815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // map to the same as char.
1829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
1839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
1845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Bool:
18619009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner      // Note that we always return bool as i1 for use as a scalar type.
1879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt1Ty(getLLVMContext());
1889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
1891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
190d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_S:
191d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_U:
192d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::SChar:
193d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::UChar:
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Short:
1955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UShort:
1965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Int:
1975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UInt:
1985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Long:
1995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULong:
2005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongLong:
2015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULongLong:
2023f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
2033f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
204f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
205f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
2069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(),
2079cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 static_cast<unsigned>(Context.getTypeSize(T)));
2089cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
210f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman    case BuiltinType::Float:
211c8b1227fa8c17d9881815e40c04e19334be536f8Nate Begeman    case BuiltinType::Double:
2125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongDouble:
2139cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = getTypeForFormat(getLLVMContext(),
2149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                    Context.getFloatTypeSemantics(T));
2159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
2161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    case BuiltinType::NullPtr:
218c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson      // Model std::nullptr_t as i8*
2199cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
2209cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
221c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson
2222df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::UInt128:
2232df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::Int128:
2249cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
2259cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
2268c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman
2278c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman    case BuiltinType::Overload:
2288c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman    case BuiltinType::Dependent:
229864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    case BuiltinType::BoundMember:
2301de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall    case BuiltinType::UnknownAny:
231864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall      llvm_unreachable("Unexpected placeholder builtin type!");
2328c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman      break;
2335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
2355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Complex: {
2371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const llvm::Type *EltTy =
2389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ConvertType(cast<ComplexType>(Ty)->getElementType());
2399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
2409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
2437c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference: {
2449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    RecursionStatePointerRAII X(RecursionState);
2459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ReferenceType *RTy = cast<ReferenceType>(Ty);
2469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = RTy->getPointeeType();
2479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
248207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
2499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
2509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
2516aeae7fa9cfaacba3a4077d62c01c2531d88a63eDaniel Dunbar  }
2525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Pointer: {
2539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    RecursionStatePointerRAII X(RecursionState);
2549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const PointerType *PTy = cast<PointerType>(Ty);
2559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = PTy->getPointeeType();
2569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
2579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    if (PointeeType->isVoidTy())
2589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
259207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
2609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
2619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
2625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
264fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray: {
2659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VariableArrayType *A = cast<VariableArrayType>(Ty);
2669cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
2675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "FIXME: We only handle trivial array types so far!");
268c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // VLAs resolve to the innermost element type; this matches
269c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // the return of alloca, and there isn't any obviously better choice.
2709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertTypeForMem(A->getElementType());
2719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
272c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  }
273c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  case Type::IncompleteArray: {
2749cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
2759cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
276c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman           "FIXME: We only handle trivial array types so far!");
277c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // int X[] -> [0 x int]
2789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::ArrayType::get(ConvertTypeForMem(A->getElementType()),0);
2799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
2805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
281fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
2829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
2839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
2849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
2859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
286fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  }
287213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Vector: {
2899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VectorType *VT = cast<VectorType>(Ty);
2909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
2919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                       VT->getNumElements());
2929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
2935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::FunctionNoProto:
295bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  case Type::FunctionProto: {
296bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // First, check whether we can build the full function type.  If the
297bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // function type depends on an incomplete type (e.g. a struct or enum), we
2989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    // cannot lower the function type.
2999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    if (VerifyFuncTypeComplete(Ty)) {
3009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // This function's type depends on an incomplete tag type.
3019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // Return a placeholder type.
3029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
3039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
304b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    }
3059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
306b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // The function type can be built; call the appropriate routines to
307b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // build it.
308bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    const CGFunctionInfo *FI;
309bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    bool isVariadic;
3109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
311bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      FI = &getFunctionInfo(
3129cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                   CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
313bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      isVariadic = FPT->isVariadic();
314bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    } else {
3159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(Ty);
316bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      FI = &getFunctionInfo(
3179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
318bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      isVariadic = true;
319bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    }
320bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner
3219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = GetFunctionType(*FI, isVariadic);
3229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
323bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  }
3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
325c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
3269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
3279cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
328c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
329391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  case Type::ObjCInterface: {
330412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // Objective-C interfaces are always opaque (outside of the
331412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // runtime, which can do whatever it likes); we never refine
332412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // these.
3339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
334412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    if (!T)
3359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      T = llvm::StructType::createNamed(getLLVMContext(), "");
3369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T;
3379cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
338391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  }
3391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
3419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    RecursionStatePointerRAII X(RecursionState);
34228e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // Protocol qualifications do not influence the LLVM type, we just return a
34328e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // pointer to the underlying interface type. We don't need to worry about
34428e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // recursive conversion.
3451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const llvm::Type *T =
3469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ConvertType(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
3479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T->getPointerTo();
3489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
34914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
35028e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar
3519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner   case Type::Enum: {
3529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
3539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    if (ED->isDefinition() || ED->isFixed())
3549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      return ConvertType(ED->getIntegerType());
3559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    // Return a placeholder '{}' type.
3569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::StructType::get(getLLVMContext());
3579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
358de0efb3b6eac36bdeae0e60f753a974cc4118a31Chris Lattner  }
3599048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar
3609048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  case Type::BlockPointer: {
3619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    RecursionStatePointerRAII X(RecursionState);
3629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
3639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(FTy);
364207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(FTy);
3659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
3669cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3679048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  }
368424c51d3d4bea87291919b75e73ca59386702ad5Sebastian Redl
3690e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  case Type::MemberPointer: {
3709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType =
3719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
3729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3730e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  }
3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3759cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
3769cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  assert(ResultType && "Didn't convert a type?");
3779cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
3789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  TypeCache[Ty] = ResultType;
3799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return ResultType;
3805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
3839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
384efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // TagDecl's are not necessarily unique, instead use the (clang)
385efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // type connected to the decl.
3869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
3871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *&Entry = RecordDeclTypes[Key];
3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have a StructType at all yet, create the forward declaration.
3919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (Entry == 0)
3929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    Entry = llvm::StructType::createNamed(getLLVMContext(),
3939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                          std::string(RD->getKindName()) + "." +
3949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                          RD->getQualifiedNameAsString());
3959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *Ty = Entry;
3965de00fcf7c923a14bb79bdbaabb2faeb5633d85aChris Lattner
3979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If this is still a forward declaration, or the LLVM type is already
3989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // complete, there's nothing more to do.
3999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (!RD->isDefinition() || !Ty->isOpaque())
4009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
4019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
4029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we're recursively nested inside the conversion of a pointer inside the
4039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // struct, defer conversion.
4049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (RecursionState == RS_StructPointer) {
4059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    DeferredRecords.push_back(RD);
4069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
4079cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  }
4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // Okay, this is a definition of a type.  Compile the implementation now.
4109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  RecursionStateTy SavedRecursionState = RecursionState;
4119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  RecursionState = RS_Struct;
412ae287239879386b9abf6da50a67bbb7fe81897f1Daniel Dunbar
41386ff308724171494395a840fd2efbe25e62f352eJohn McCall  // Force conversion of non-virtual base classes recursively.
4149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
4159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
4169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner         e = CRD->bases_end(); i != e; ++i) {
41786ff308724171494395a840fd2efbe25e62f352eJohn McCall      if (!i->isVirtual()) {
41886ff308724171494395a840fd2efbe25e62f352eJohn McCall        const CXXRecordDecl *Base =
41986ff308724171494395a840fd2efbe25e62f352eJohn McCall          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
4209cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner        ConvertRecordDeclType(Base);
42186ff308724171494395a840fd2efbe25e62f352eJohn McCall      }
42286ff308724171494395a840fd2efbe25e62f352eJohn McCall    }
42386ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
42486ff308724171494395a840fd2efbe25e62f352eJohn McCall
425696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  // Layout fields.
4269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
427696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  CGRecordLayouts[Key] = Layout;
4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
4309cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // Restore our recursion state.  If we're done converting the outer-most
4319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // record, then convert any deferred structs as well.
4329cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  RecursionState = SavedRecursionState;
4339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (RecursionState == RS_Normal)
4349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    while (!DeferredRecords.empty())
4359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ConvertRecordDeclType(DeferredRecords.pop_back_val());
4369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
4379cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return Ty;
4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
439fc3b8e9c1381d5e6ec361591d649c56a870ff971Chris Lattner
4402d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson/// getCGRecordLayout - Return record layout info for the given record decl.
441ad3e7118c40711faf5f51f08d599dbd525d3408aAnders Carlssonconst CGRecordLayout &
4422d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders CarlssonCodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
4432d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
44482926963cccbc54bc707d5dcf79389166046ef08Anders Carlsson
445270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
446c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  if (!Layout) {
4472d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson    // Compute the type information.
4489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ConvertRecordDeclType(RD);
449c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
450c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    // Now try again.
451c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    Layout = CGRecordLayouts.lookup(Key);
452c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  }
453c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
454270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  assert(Layout && "Unable to find record layout information for type");
455270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  return *Layout;
456b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
4573e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
458f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(QualType T) {
4593e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  // No need to check for member pointers when not compiling C++.
4603e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (!Context.getLangOptions().CPlusPlus)
461f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return true;
4623e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
4633e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  T = Context.getBaseElementType(T);
4643e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
465f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Records are non-zero-initializable if they contain any
466f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // non-zero-initializable subobjects.
4673e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const RecordType *RT = T->getAs<RecordType>()) {
4683e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
469f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return isZeroInitializable(RD);
4703e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  }
471f16aa103d3afd42fbca2ab346f191bf745cec092John McCall
472f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // We have to ask the ABI about member pointers.
4733e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
474f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return getCXXABI().isZeroInitializable(MPT);
4753e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
476f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Everything else is okay.
477f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return true;
4783e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson}
479c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson
480f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
4813379e9bd71c0e0051bd97e90d4f2ec964078091dAnders Carlsson  return getCGRecordLayout(RD).isZeroInitializable();
482c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson}
483