CodeGenTypes.cpp revision 21f18c4fda167dc5f72feddbd6a7ac1b63200a0d
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// This is the code that handles AST -> LLVM type lowering.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenTypes.h"
15f16aa103d3afd42fbca2ab346f191bf745cec092John McCall#include "CGCXXABI.h"
1655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CGCall.h"
17b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei#include "CGOpenCLRuntime.h"
182924ade97ee4228fcf3518d89cd4bd1653236b48Daniel Dunbar#include "CGRecordLayout.h"
19de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall#include "TargetInfo.h"
20de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/ASTContext.h"
21742cd1b7bb86b52b23b335d47abbd842dac0e1bfFariborz Jahanian#include "clang/AST/DeclCXX.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/DeclObjC.h"
23de7fb8413b13651fd85b7125d08b3c9ac2816d9dDaniel Dunbar#include "clang/AST/Expr.h"
2419cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
253b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h"
263b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DerivedTypes.h"
273b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Module.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen;
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
31de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::CodeGenTypes(CodeGenModule &CGM)
32de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  : Context(CGM.getContext()), Target(Context.getTargetInfo()),
3325a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow    TheModule(CGM.getModule()), TheDataLayout(CGM.getDataLayout()),
34de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    TheABIInfo(CGM.getTargetCodeGenInfo().getABIInfo()),
35de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    TheCXXABI(CGM.getCXXABI()),
36de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    CodeGenOpts(CGM.getCodeGenOpts()), CGM(CGM) {
3757eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner  SkippedLayout = false;
38d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner}
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
40b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang PatelCodeGenTypes::~CodeGenTypes() {
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
43b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel      I != E; ++I)
44b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel    delete I->second;
456f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner
466f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner  for (llvm::FoldingSet<CGFunctionInfo>::iterator
476f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner       I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
486f41c17ae854c8a26126e5ea1c3cd53bd746b7baChris Lattner    delete &*I++;
49b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
50b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel
519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnervoid CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     llvm::StructType *Ty,
535f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                     StringRef suffix) {
54f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  SmallString<256> TypeName;
55e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  llvm::raw_svector_ostream OS(TypeName);
56e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson  OS << RD->getKindName() << '.';
57e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson
58e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  // Name the codegen type after the typedef name
59e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  // if there is no tag type name available
60e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson  if (RD->getIdentifier()) {
61e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // FIXME: We should not have to check for a null decl context here.
62e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // Right now we do it because the implicit Obj-C decls don't have one.
63e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson    if (RD->getDeclContext())
64e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson      OS << RD->getQualifiedNameAsString();
65e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    else
66e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson      RD->printName(OS);
67e0047b13f0025fa1588f1b776592ed3458b226d6Anders Carlsson  } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
68e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // FIXME: We should not have to check for a null decl context here.
69e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    // Right now we do it because the implicit Obj-C decls don't have one.
70e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    if (TDD->getDeclContext())
71e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson      OS << TDD->getQualifiedNameAsString();
72e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    else
73e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson      TDD->printName(OS);
74e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  } else
75e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    OS << "anon";
76e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson
77e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson  if (!suffix.empty())
78e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson    OS << suffix;
79e9742b0f7281d0a21b79e661ec8879e01c4a02e4Anders Carlsson
809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  Ty->setName(OS.str());
8130ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel}
8230ec9972be5a5af1f7a2277360dfa3aa1540b4faDevang Patel
834581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
844581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// ConvertType in that it is used to convert to the memory representation for
854581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// a type.  For example, the scalar representation for _Bool is i1, but the
864581fff8af09a156a9dcc4de62587385c5da9574Chris Lattner/// memory representation is usually i8 or i32, depending on the target.
879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){
889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *R = ConvertType(T);
891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9019009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner  // If this is a non-bool type, don't map it.
91f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands  if (!R->isIntegerTy(1))
9219009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner    return R;
931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9419009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner  // Otherwise, return an integer of the target-specified size.
950032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  return llvm::IntegerType::get(getLLVMContext(),
960032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson                                (unsigned)Context.getTypeSize(T));
9719009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner}
9819009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner
9971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
10071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isRecordLayoutComplete - Return true if the specified type is already
10171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// completely laid out.
10271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerbool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
10371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
10471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  RecordDeclTypes.find(Ty);
10571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return I != RecordDeclTypes.end() && !I->second->isOpaque();
10671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
10771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
10871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool
10971305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(QualType T, CodeGenTypes &CGT,
11071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
11171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
11271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
11371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert the specified record
11471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// decl to IR and lay it out, false if doing so would cause us to get into a
11571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursive compilation mess.
11671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool
11771305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
11871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
11971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If we have already checked this type (maybe the same type is used by-value
12071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // multiple times in multiple structure fields, don't check again.
12171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (!AlreadyChecked.insert(RD)) return true;
12271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
12371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
12471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
12571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this type is already laid out, converting it is a noop.
12671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (CGT.isRecordLayoutComplete(Key)) return true;
12771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
12871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this type is currently being laid out, we can't recursively compile it.
12971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (CGT.isRecordBeingLaidOut(Key))
13071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    return false;
13171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
13271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this type would require laying out bases that are currently being laid
13371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // out, don't do it.  This includes virtual base classes which get laid out
13471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // when a class is translated, even though they aren't embedded by-value into
13571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // the class.
13671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
13771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    for (CXXRecordDecl::base_class_const_iterator I = CRD->bases_begin(),
13871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner         E = CRD->bases_end(); I != E; ++I)
13971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      if (!isSafeToConvert(I->getType()->getAs<RecordType>()->getDecl(),
14071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                           CGT, AlreadyChecked))
14171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner        return false;
14271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  }
14371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
14471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this type would require laying out members that are currently being laid
14571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // out, don't do it.
14671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  for (RecordDecl::field_iterator I = RD->field_begin(),
14771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner       E = RD->field_end(); I != E; ++I)
14871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
14971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      return false;
15071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
15171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If there are no problems, lets do it.
15271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return true;
15371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
15471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
15571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert this field type,
15671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// which requires the structure elements contained by-value to all be
15771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursively safe to convert.
15871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool
15971305cc81bd379ddb8aa0d49e268267383202ca9Chris LattnerisSafeToConvert(QualType T, CodeGenTypes &CGT,
16071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
16171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  T = T.getCanonicalType();
16271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
16371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this is a record, check it.
16471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(T))
16571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
16671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
16771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this is an array, check the elements, which are embedded inline.
16871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (const ArrayType *AT = dyn_cast<ArrayType>(T))
16971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
17071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
17171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // Otherwise, there is no concern about transforming this.  We only care about
17271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // things that are contained by-value in a structure that can have another
17371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // structure as a member.
17471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return true;
17571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
17671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
17771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
17871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// isSafeToConvert - Return true if it is safe to convert the specified record
17971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// decl to IR and lay it out, false if doing so would cause us to get into a
18071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner/// recursive compilation mess.
18171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerstatic bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
18271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If no structs are being laid out, we can certainly do this one.
18371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (CGT.noRecordsBeingLaidOut()) return true;
18471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
18571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
18671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return isSafeToConvert(RD, CGT, AlreadyChecked);
18771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner}
18871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
18971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
190f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// isFuncTypeArgumentConvertible - Return true if the specified type in a
191f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// function argument or result position can be converted to an IR type at this
192f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// point.  This boils down to being whether it is complete, as well as whether
193f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// we've temporarily deferred expanding the type because we're in a recursive
194f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// context.
19571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattnerbool CodeGenTypes::isFuncTypeArgumentConvertible(QualType Ty) {
196f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // If this isn't a tagged type, we can convert it!
197f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  const TagType *TT = Ty->getAs<TagType>();
198f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (TT == 0) return true;
199bcf38f2782d9cada139e22aa1ab8df10c9b047e5Douglas Gregor
200bcf38f2782d9cada139e22aa1ab8df10c9b047e5Douglas Gregor  // Incomplete types cannot be converted.
2014a59bc26b3f2d00055e24332c3164c894fafac27Douglas Gregor  if (TT->isIncompleteType())
202f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    return false;
203f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
20471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If this is an enum, then it is always safe to convert.
205f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  const RecordType *RT = dyn_cast<RecordType>(TT);
206f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (RT == 0) return true;
207f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
208f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // Otherwise, we have to be careful.  If it is a struct that we're in the
209f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // process of expanding, then we can't convert the function type.  That's ok
210f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // though because we must be in a pointer context under the struct, so we can
211f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // just convert it to a dummy type.
212f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  //
213f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // We decide this by checking whether ConvertRecordDeclType returns us an
214f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  // opaque type for a struct that we know is defined.
21571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  return isSafeToConvert(RT->getDecl(), *this);
216f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner}
217f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
218f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
219f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// Code to verify a given function type is complete, i.e. the return type
220f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// and all of the argument types are complete.  Also check to see if we are in
221f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// a RS_StructPointer context, and if so whether any struct types have been
222f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// pended.  If so, we don't want to ask the ABI lowering code to handle a type
223f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner/// that cannot be converted to an IR type.
224f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattnerbool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
225f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (!isFuncTypeArgumentConvertible(FT->getResultType()))
226f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    return false;
227f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
228f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
2299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
230f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner      if (!isFuncTypeArgumentConvertible(FPT->getArgType(i)))
231f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner        return false;
232f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
233f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  return true;
234b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman}
235b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman
236c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// UpdateCompletedType - When we find the full definition for a TagDecl,
237c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner/// replace the 'opaque' type we previously made for it if applicable.
238c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
2399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If this is an enum being completed, then we flush all non-struct types from
2409cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // the cache.  This allows function types and other things that may be derived
2419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // from the enum to be recomputed.
2428dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner  if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
2438dd5cdfc462026648b480adaf774e24bc620f7c3Chris Lattner    // Only flush the cache if we've actually already converted this type.
2442045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    if (TypeCache.count(ED->getTypeForDecl())) {
2452045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      // Okay, we formed some types based on this.  We speculated that the enum
2462045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      // would be lowered to i32, so we only need to flush the cache if this
2472045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      // didn't happen.
2482045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner      if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
2492045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner        TypeCache.clear();
2502045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    }
2519cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return;
252b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman  }
2539cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
254f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // If we completed a RecordDecl that we previously used and converted to an
255f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // anonymous type, then go ahead and complete it now.
2569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const RecordDecl *RD = cast<RecordDecl>(TD);
257f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  if (RD->isDependentType()) return;
258f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
259f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // Only complete it if we converted it already.  If we haven't converted it
260f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // yet, we'll just do it lazily.
2613ade97504790bbc5a148baa5b3f7e577aed9c1c6Chris Lattner  if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
2629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ConvertRecordDeclType(RD);
263d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner}
264d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner
2659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
26619dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                                    const llvm::fltSemantics &format,
26719dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                                    bool UseNativeHalf = false) {
26819dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly  if (&format == &llvm::APFloat::IEEEhalf) {
26919dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly    if (UseNativeHalf)
27019dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly      return llvm::Type::getHalfTy(VMContext);
27119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly    else
27219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly      return llvm::Type::getInt16Ty(VMContext);
27319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly  }
274b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEsingle)
2750032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFloatTy(VMContext);
276b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEdouble)
2770032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getDoubleTy(VMContext);
278b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::IEEEquad)
2790032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getFP128Ty(VMContext);
280b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::PPCDoubleDouble)
2810032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getPPC_FP128Ty(VMContext);
282b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  if (&format == &llvm::APFloat::x87DoubleExtended)
2830032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    return llvm::Type::getX86_FP80Ty(VMContext);
284b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unknown float format!");
285f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman}
286f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman
2879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertType - Convert the specified type to its LLVM form.
2889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *CodeGenTypes::ConvertType(QualType T) {
2899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  T = Context.getCanonicalType(T);
2909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Ty = T.getTypePtr();
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // RecordTypes are cached and processed specially.
2949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const RecordType *RT = dyn_cast<RecordType>(Ty))
2959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return ConvertRecordDeclType(RT->getDecl());
2969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
2979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // See if type is already cached.
2989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
2999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If type is found in map then use it. Otherwise, convert type T.
3009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (TCI != TypeCache.end())
3019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return TCI->second;
3029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
3039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have it in the cache, convert it now.
3049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResultType = 0;
3059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  switch (Ty->getTypeClass()) {
3069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  case Type::Record: // Handled above.
30772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
30872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
30972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
31072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
311ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
31272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
313864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall    llvm_unreachable("Non-canonical or dependent types aren't possible.");
31472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
3155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Builtin: {
3169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    switch (cast<BuiltinType>(Ty)->getKind()) {
3175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Void:
318de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCId:
319de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    case BuiltinType::ObjCClass:
32013dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian    case BuiltinType::ObjCSel:
3215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // LLVM void type can only be used as the result of a function call.  Just
3225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // map to the same as char.
3239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
3249cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Bool:
32719009e6fe7e0f51d2e49f4c94928a048c11c5281Chris Lattner      // Note that we always return bool as i1 for use as a scalar type.
3289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt1Ty(getLLVMContext());
3299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
331d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_S:
332d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Char_U:
333d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::SChar:
334d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::UChar:
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Short:
3365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UShort:
3375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Int:
3385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::UInt:
3395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::Long:
3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULong:
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongLong:
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::ULongLong:
3433f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
3443f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
345f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
346f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
3479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(),
3489cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 static_cast<unsigned>(Context.getTypeSize(T)));
3499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
351aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    case BuiltinType::Half:
35219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly      // Half FP can either be storage-only (lowered to i16) or native.
35319dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly      ResultType = getTypeForFormat(getLLVMContext(),
35419dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          Context.getFloatTypeSemantics(T),
35519dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly          Context.getLangOpts().NativeHalfType);
356aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      break;
357f6a943e047f541619a2202f9e43b20b3d7c0a96dEli Friedman    case BuiltinType::Float:
358c8b1227fa8c17d9881815e40c04e19334be536f8Nate Begeman    case BuiltinType::Double:
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    case BuiltinType::LongDouble:
3609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = getTypeForFormat(getLLVMContext(),
36119dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                                    Context.getFloatTypeSemantics(T),
36219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly                                    /* UseNativeHalf = */ false);
3639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    case BuiltinType::NullPtr:
366c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson      // Model std::nullptr_t as i8*
3679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
3689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
369c1eb14a66fdd955aff3f957a5843295f27952bddAnders Carlsson
3702df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::UInt128:
3712df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    case BuiltinType::Int128:
3729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
3739cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
374b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
375b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage1d:
376b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage1dArray:
377b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage1dBuffer:
378b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage2d:
379b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage2dArray:
380b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage3d:
38121f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    case BuiltinType::OCLSampler:
382e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    case BuiltinType::OCLEvent:
383b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
384b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      break;
3858c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman
3868c69235d21dfb067efb530a8cf1f35d633394270Eli Friedman    case BuiltinType::Dependent:
3872dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define BUILTIN_TYPE(Id, SingletonId)
3882dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#define PLACEHOLDER_TYPE(Id, SingletonId) \
3892dde35bc626153492f5f58202506c88a27fbff5bJohn McCall    case BuiltinType::Id:
3902dde35bc626153492f5f58202506c88a27fbff5bJohn McCall#include "clang/AST/BuiltinTypes.def"
391864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall      llvm_unreachable("Unexpected placeholder builtin type!");
3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    break;
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Complex: {
396ef6de3da8572607f786303c07150daa6e140ab19Jay Foad    llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
3979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
3989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4007c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
4017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference: {
4029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ReferenceType *RTy = cast<ReferenceType>(Ty);
4039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = RTy->getPointeeType();
4049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
405207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
4069cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
4079cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4086aeae7fa9cfaacba3a4077d62c01c2531d88a63eDaniel Dunbar  }
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Pointer: {
4109cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const PointerType *PTy = cast<PointerType>(Ty);
4119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    QualType ETy = PTy->getPointeeType();
4129cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
4139cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    if (PointeeType->isVoidTy())
4149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
415207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(ETy);
4169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
4179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
420fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray: {
4219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VariableArrayType *A = cast<VariableArrayType>(Ty);
4229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer           "FIXME: We only handle trivial array types so far!");
424c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // VLAs resolve to the innermost element type; this matches
425c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // the return of alloca, and there isn't any obviously better choice.
4269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertTypeForMem(A->getElementType());
4279cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
428c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  }
429c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  case Type::IncompleteArray: {
4309cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
4319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    assert(A->getIndexTypeCVRQualifiers() == 0 &&
432c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman           "FIXME: We only handle trivial array types so far!");
4333a2b657088de9413714a51bff153a59565adb3efChris Lattner    // int X[] -> [0 x int], unless the element type is not sized.  If it is
4343a2b657088de9413714a51bff153a59565adb3efChris Lattner    // unsized (e.g. an incomplete struct) just use [0 x i8].
4353a2b657088de9413714a51bff153a59565adb3efChris Lattner    ResultType = ConvertTypeForMem(A->getElementType());
4363a2b657088de9413714a51bff153a59565adb3efChris Lattner    if (!ResultType->isSized()) {
4373a2b657088de9413714a51bff153a59565adb3efChris Lattner      SkippedLayout = true;
4383a2b657088de9413714a51bff153a59565adb3efChris Lattner      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
4393a2b657088de9413714a51bff153a59565adb3efChris Lattner    }
4403a2b657088de9413714a51bff153a59565adb3efChris Lattner    ResultType = llvm::ArrayType::get(ResultType, 0);
4419cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
443fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
4449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
4452acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
44601c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner
44701c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    // Lower arrays of undefined struct type to arrays of i8 just to have a
44801c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    // concrete type.
44901c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    if (!EltTy->isSized()) {
45001c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner      SkippedLayout = true;
45101c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner      EltTy = llvm::Type::getInt8Ty(getLLVMContext());
45201c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner    }
45301c5d1ddc8bd589b629b8c0185c279d090bba115Chris Lattner
4549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
4559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
456fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  }
457213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::Vector: {
4599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const VectorType *VT = cast<VectorType>(Ty);
4609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
4619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                       VT->getNumElements());
4629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
4635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case Type::FunctionNoProto:
465bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  case Type::FunctionProto: {
46671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    const FunctionType *FT = cast<FunctionType>(Ty);
467bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // First, check whether we can build the full function type.  If the
468bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    // function type depends on an incomplete type (e.g. a struct or enum), we
4699cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    // cannot lower the function type.
47071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!isFuncTypeConvertible(FT)) {
4719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // This function's type depends on an incomplete tag type.
47284fd6df966208b380cba072d2f2883962b9d270aEli Friedman
47384fd6df966208b380cba072d2f2883962b9d270aEli Friedman      // Force conversion of all the relevant record types, to make sure
47484fd6df966208b380cba072d2f2883962b9d270aEli Friedman      // we re-convert the FunctionType when appropriate.
47584fd6df966208b380cba072d2f2883962b9d270aEli Friedman      if (const RecordType *RT = FT->getResultType()->getAs<RecordType>())
47684fd6df966208b380cba072d2f2883962b9d270aEli Friedman        ConvertRecordDeclType(RT->getDecl());
47784fd6df966208b380cba072d2f2883962b9d270aEli Friedman      if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
47884fd6df966208b380cba072d2f2883962b9d270aEli Friedman        for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
47984fd6df966208b380cba072d2f2883962b9d270aEli Friedman          if (const RecordType *RT = FPT->getArgType(i)->getAs<RecordType>())
48084fd6df966208b380cba072d2f2883962b9d270aEli Friedman            ConvertRecordDeclType(RT->getDecl());
48184fd6df966208b380cba072d2f2883962b9d270aEli Friedman
4829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      // Return a placeholder type.
4839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
48484fd6df966208b380cba072d2f2883962b9d270aEli Friedman
48571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
4869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      break;
487b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    }
4889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
489f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // While we're converting the argument types for a function, we don't want
490f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // to recursively convert any pointed-to structs.  Converting directly-used
491f0a8679b6e6635117533b89894646f1450cea25bChris Lattner    // structs is ok though.
49271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (!RecordsBeingLaidOut.insert(Ty)) {
49371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
49471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
49571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
49671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      break;
49771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    }
498f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
499b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // The function type can be built; call the appropriate routines to
500b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377Eli Friedman    // build it.
501bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    const CGFunctionInfo *FI;
50271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
5030f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall      FI = &arrangeFreeFunctionType(
5049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                   CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
505bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    } else {
50671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
5070f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall      FI = &arrangeFreeFunctionType(
5089cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
509bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    }
510f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
51171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    // If there is something higher level prodding our CGFunctionInfo, then
51271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    // don't recurse into it again.
51371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (FunctionsBeingProcessed.count(FI)) {
51471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
51571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ResultType = llvm::StructType::get(getLLVMContext());
51671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      SkippedLayout = true;
51771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    } else {
51871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
51971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      // Otherwise, we're good to go, go ahead and convert it.
520de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      ResultType = GetFunctionType(*FI);
52171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    }
52271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
52371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    RecordsBeingLaidOut.erase(Ty);
52457eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner
52557eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner    if (SkippedLayout)
52657eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner      TypeCache.clear();
527f0a8679b6e6635117533b89894646f1450cea25bChris Lattner
52871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner    if (RecordsBeingLaidOut.empty())
529f0a8679b6e6635117533b89894646f1450cea25bChris Lattner      while (!DeferredRecords.empty())
530f0a8679b6e6635117533b89894646f1450cea25bChris Lattner        ConvertRecordDeclType(DeferredRecords.pop_back_val());
5319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
532bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  }
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
534c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
5359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
5369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
537c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
538391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  case Type::ObjCInterface: {
539412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // Objective-C interfaces are always opaque (outside of the
540412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // runtime, which can do whatever it likes); we never refine
541412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    // these.
5429cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
543412f59b23fc502b199b9ca96c72ef5d5ad21d62bDaniel Dunbar    if (!T)
544c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner      T = llvm::StructType::create(getLLVMContext());
5459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T;
5469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
547391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  }
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
55028e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // Protocol qualifications do not influence the LLVM type, we just return a
55128e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // pointer to the underlying interface type. We don't need to worry about
55228e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar    // recursive conversion.
5532acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *T =
554181eeee36b057a1a66227123fad0978d1d0f034aChris Lattner      ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
5559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = T->getPointerTo();
5569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
55714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  }
55828e478010eb4d789da85c6378dbfa9d66b95830bDaniel Dunbar
5592045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner  case Type::Enum: {
5609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
5615e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall    if (ED->isCompleteDefinition() || ED->isFixed())
5629cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      return ConvertType(ED->getIntegerType());
5632045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // Return a placeholder 'i32' type.  This can be changed later when the
5642045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // type is defined (see UpdateCompletedType), but is likely to be the
5652045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    // "right" answer.
5662045b2dd6a779e9b35ac42a60cf30a6b07cb4bb6Chris Lattner    ResultType = llvm::Type::getInt32Ty(getLLVMContext());
5679cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
568de0efb3b6eac36bdeae0e60f753a974cc4118a31Chris Lattner  }
5699048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar
5709048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  case Type::BlockPointer: {
5719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
5729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(FTy);
573207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(FTy);
5749cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
5759cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
5769048891ff983d0681c116c6e8f1073aa31bdd6e8Daniel Dunbar  }
577424c51d3d4bea87291919b75e73ca59386702ad5Sebastian Redl
5780e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  case Type::MemberPointer: {
5799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResultType =
5809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
5819cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    break;
5820e650017acdbbeb0c590e77bbea88c200ea1caefAnders Carlsson  }
583b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
584b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
58547bfcca2d6972d98a1b25239cd1aa658b60680e2Douglas Gregor    ResultType = ConvertType(cast<AtomicType>(Ty)->getValueType());
586b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
587b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
5909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  assert(ResultType && "Didn't convert a type?");
5919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
5929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  TypeCache[Ty] = ResultType;
5939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return ResultType;
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
5979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
598efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // TagDecl's are not necessarily unique, instead use the (clang)
599efb6d0dc3eafbcf4f8cd053138bd1abed1dda8d4Daniel Dunbar  // type connected to the decl.
6009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
6011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *&Entry = RecordDeclTypes[Key];
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If we don't have a StructType at all yet, create the forward declaration.
605cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner  if (Entry == 0) {
606c1c20114a401e503c07d68c47e0728bb063f35c8Chris Lattner    Entry = llvm::StructType::create(getLLVMContext());
607cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner    addRecordTypeName(RD, Entry, "");
608cd87d1e4d1b0097877b0f9c2065900717d2aacbaChris Lattner  }
6099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *Ty = Entry;
6105de00fcf7c923a14bb79bdbaabb2faeb5633d85aChris Lattner
6119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // If this is still a forward declaration, or the LLVM type is already
6129cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // complete, there's nothing more to do.
61371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  RD = RD->getDefinition();
6145e1cdac63c3d9c9b32fa41fa0b2d242a58a20d49John McCall  if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque())
6159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
6169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
61771305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If converting this type would cause us to infinitely loop, don't do it!
61871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (!isSafeToConvert(RD, *this)) {
6199cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    DeferredRecords.push_back(RD);
6209cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    return Ty;
6219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  }
6221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  // Okay, this is a definition of a type.  Compile the implementation now.
62471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
62571305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(InsertResult && "Recursively compiling a struct?");
62671305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
62786ff308724171494395a840fd2efbe25e62f352eJohn McCall  // Force conversion of non-virtual base classes recursively.
6289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
6299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
6309cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner         e = CRD->bases_end(); i != e; ++i) {
63171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      if (i->isVirtual()) continue;
63271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
63371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner      ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
63486ff308724171494395a840fd2efbe25e62f352eJohn McCall    }
63586ff308724171494395a840fd2efbe25e62f352eJohn McCall  }
63686ff308724171494395a840fd2efbe25e62f352eJohn McCall
637696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  // Layout fields.
6389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
639696798febaf1f69020cdf7474b91e71736c5aa69Anders Carlsson  CGRecordLayouts[Key] = Layout;
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // We're done laying out this struct.
64271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
64371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
64471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
645f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // If this struct blocked a FunctionType conversion, then recompute whatever
646f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // was derived from that.
647f0a8679b6e6635117533b89894646f1450cea25bChris Lattner  // FIXME: This is hugely overconservative.
64857eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner  if (SkippedLayout)
64957eb23f34ed2586069273c66f1a9994fe2b42967Chris Lattner    TypeCache.clear();
65071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
65171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // If we're done converting the outer-most record, then convert any deferred
65271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  // structs as well.
65371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  if (RecordsBeingLaidOut.empty())
6549cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    while (!DeferredRecords.empty())
6559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      ConvertRecordDeclType(DeferredRecords.pop_back_val());
6569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner
6579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  return Ty;
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump}
659fc3b8e9c1381d5e6ec361591d649c56a870ff971Chris Lattner
6602d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson/// getCGRecordLayout - Return record layout info for the given record decl.
661ad3e7118c40711faf5f51f08d599dbd525d3408aAnders Carlssonconst CGRecordLayout &
6622d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders CarlssonCodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
6632d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
66482926963cccbc54bc707d5dcf79389166046ef08Anders Carlsson
665270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
666c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  if (!Layout) {
6672d987772c2ba3bff93b9fbe7b73bf06eed2b1607Anders Carlsson    // Compute the type information.
6689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ConvertRecordDeclType(RD);
669c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
670c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    // Now try again.
671c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson    Layout = CGRecordLayouts.lookup(Key);
672c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson  }
673c8f01ebbce3c874b43ee78535f7d179517f5f436Anders Carlsson
674270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  assert(Layout && "Unable to find record layout information for type");
675270e203b50ed8791e61afd357596bcf050cf2bfdDaniel Dunbar  return *Layout;
676b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel}
6773e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
678f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(QualType T) {
6793e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  // No need to check for member pointers when not compiling C++.
6804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!Context.getLangOpts().CPlusPlus)
681f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return true;
6823e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
6833e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  T = Context.getBaseElementType(T);
6843e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
685f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Records are non-zero-initializable if they contain any
686f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // non-zero-initializable subobjects.
6873e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const RecordType *RT = T->getAs<RecordType>()) {
6883e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
689f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return isZeroInitializable(RD);
6903e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  }
691f16aa103d3afd42fbca2ab346f191bf745cec092John McCall
692f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // We have to ask the ABI about member pointers.
6933e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson  if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
694f16aa103d3afd42fbca2ab346f191bf745cec092John McCall    return getCXXABI().isZeroInitializable(MPT);
6953e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson
696f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  // Everything else is okay.
697f16aa103d3afd42fbca2ab346f191bf745cec092John McCall  return true;
6983e5af908ebf4c56b657c488bb2ca22f418e0868bAnders Carlsson}
699c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson
700f16aa103d3afd42fbca2ab346f191bf745cec092John McCallbool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
7013379e9bd71c0e0051bd97e90d4f2ec964078091dAnders Carlsson  return getCGRecordLayout(RD).isZeroInitializable();
702c39211d2aef5b8ca2bc69c9fb81ca3dfb3711eb0Anders Carlsson}
703