CodeGenTypes.cpp revision 47bfcca2d6972d98a1b25239cd1aa658b60680e2
197f06937449c593a248dbbb1365e6ae408fb9decChris Lattner//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
297f06937449c593a248dbbb1365e6ae408fb9decChris Lattner//
37bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola//                     The LLVM Compiler Infrastructure
47bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
67bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola// License. See LICENSE.TXT for details.
77bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola//
87bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola//===----------------------------------------------------------------------===//
97bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola//
107bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola// This is the code that handles AST -> LLVM type lowering.
117bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola//
127bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola//===----------------------------------------------------------------------===//
137bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola
147bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola#include "CodeGenTypes.h"
1595b2c7da5e83670881270c1cd231a240be0556d9Chris Lattner#include "CGCall.h"
16b72d2a92b75daa9cbac7338aff0cd8ae04c2b4bdEvan Cheng#include "CGCXXABI.h"
17c1f6f42049696e7357fb4837e1b25dabbaed3fe6Craig Topper#include "CGRecordLayout.h"
18b72d2a92b75daa9cbac7338aff0cd8ae04c2b4bdEvan Cheng#include "TargetInfo.h"
19a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#include "clang/AST/ASTContext.h"
2097f06937449c593a248dbbb1365e6ae408fb9decChris Lattner#include "clang/AST/DeclObjC.h"
2197f06937449c593a248dbbb1365e6ae408fb9decChris Lattner#include "clang/AST/DeclCXX.h"
2217b443df4368acfad853d09858c033c45c468d5cJason W Kim#include "clang/AST/Expr.h"
23b72d2a92b75daa9cbac7338aff0cd8ae04c2b4bdEvan Cheng#include "clang/AST/RecordLayout.h"
24ee04a6d3a40c3017124e3fd89a0db473a2824498Evan Cheng#include "llvm/DerivedTypes.h"
25ee04a6d3a40c3017124e3fd89a0db473a2824498Evan Cheng#include "llvm/Module.h"
263f282aa94b80f4a93ff3cbc37cf3cd4a851c8432Dale Johannesen#include "llvm/Target/TargetData.h"
277bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindolausing namespace clang;
287bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindolausing namespace CodeGen;
29e55b15fa4753ef08cbfa2127d2d220b77aa07d87Benjamin Kramer
30cf20ac4fd12ea3510a8f32a24fff69eebe7b6f4aDan GohmanCodeGenTypes::CodeGenTypes(CodeGenModule &CGM)
31b0f294c14b4e7098e5170ecfd528bcc9682ce0c7Chris Lattner  : Context(CGM.getContext()), Target(Context.getTargetInfo()),
327bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola    TheModule(CGM.getModule()), TheTargetData(CGM.getTargetData()),
33a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    TheABIInfo(CGM.getTargetCodeGenInfo().getABIInfo()),
34b0f294c14b4e7098e5170ecfd528bcc9682ce0c7Chris Lattner    TheCXXABI(CGM.getCXXABI()),
35cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    CodeGenOpts(CGM.getCodeGenOpts()), CGM(CGM) {
36b0f294c14b4e7098e5170ecfd528bcc9682ce0c7Chris Lattner  SkippedLayout = false;
3797f06937449c593a248dbbb1365e6ae408fb9decChris Lattner}
38f9bdeddb96043559c61f176f8077e3b91a0c544fChris Lattner
39cecbc3d28277ff4916326311cbf87335ed05d106Rafael EspindolaCodeGenTypes::~CodeGenTypes() {
406c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
41325d3dcfe4d5efc91db0f59b20a72a11dea024edChris Lattner         I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
42d62f1b4168d4327c119642d28c26c836ae6717abChris Lattner      I != E; ++I)
43b01c4bbb4573e0007444e218b683840e4519e0c8Rafael Espindola    delete I->second;
447bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola
45c40d9f9bae70c83947bf8fa5f9ee97adbf1bb0c0Jim Grosbach  for (llvm::FoldingSet<CGFunctionInfo>::iterator
4697f06937449c593a248dbbb1365e6ae408fb9decChris Lattner       I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
4759135f49e1699daec9a43fc2d15715d55b910f54Devang Patel    delete &*I++;
483046470919e648ff7c011bda9c094163062c83dcTorok Edwin}
493e74d6fdd248e20a280f1dff3da9a6c689c2c4c3Evan Cheng
50b23569aff0a6d2b231cb93cc4acd0ac060ba560fChris Lattnervoid CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
517bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola                                     llvm::StructType *Ty,
527bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola                                     StringRef suffix) {
537bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola  SmallString<256> TypeName;
5495b2c7da5e83670881270c1cd231a240be0556d9Chris Lattner  llvm::raw_svector_ostream OS(TypeName);
55cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  OS << RD->getKindName() << '.';
56cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola
57cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // Name the codegen type after the typedef name
58cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // if there is no tag type name available
59cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  if (RD->getIdentifier()) {
60cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    // FIXME: We should not have to check for a null decl context here.
61cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    // Right now we do it because the implicit Obj-C decls don't have one.
62cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    if (RD->getDeclContext())
63cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola      OS << RD->getQualifiedNameAsString();
64f009a961caa75465999ef3bc764984d97a7da331Jason W Kim    else
65cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola      RD->printName(OS);
664921e2356ef8f3b3f9ebd0c154b091c3d5dd2ce4Rafael Espindola  } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
67cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    // FIXME: We should not have to check for a null decl context here.
68cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    // Right now we do it because the implicit Obj-C decls don't have one.
69cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    if (TDD->getDeclContext())
70cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola      OS << TDD->getQualifiedNameAsString();
71cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    else
72cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola      TDD->printName(OS);
73cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  } else
74cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    OS << "anon";
75cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola
76cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  if (!suffix.empty())
77cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    OS << suffix;
78cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola
79cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  Ty->setName(OS.str());
80cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola}
81f009a961caa75465999ef3bc764984d97a7da331Jason W Kim
82f009a961caa75465999ef3bc764984d97a7da331Jason W Kim/// ConvertTypeForMem - Convert type T into a llvm::Type.  This differs from
83bc2198133a1836598b54b943420748e75d5dea94Craig Topper/// ConvertType in that it is used to convert to the memory representation for
84f009a961caa75465999ef3bc764984d97a7da331Jason W Kim/// a type.  For example, the scalar representation for _Bool is i1, but the
85590853667345d6fb191764b9d0bd2ff13589e3a3Benjamin Kramer/// memory representation is usually i8 or i32, depending on the target.
86f009a961caa75465999ef3bc764984d97a7da331Jason W Kimllvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){
87728ff0db783152ed4f21f7746bd7874b49708172Renato Golin  llvm::Type *R = ConvertType(T);
88728ff0db783152ed4f21f7746bd7874b49708172Renato Golin
89728ff0db783152ed4f21f7746bd7874b49708172Renato Golin  // If this is a non-bool type, don't map it.
90590853667345d6fb191764b9d0bd2ff13589e3a3Benjamin Kramer  if (!R->isIntegerTy(1))
918e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbach    return R;
92f009a961caa75465999ef3bc764984d97a7da331Jason W Kim
93f009a961caa75465999ef3bc764984d97a7da331Jason W Kim  // Otherwise, return an integer of the target-specified size.
94cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  return llvm::IntegerType::get(getLLVMContext(),
95cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola                                (unsigned)Context.getTypeSize(T));
96cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola}
97cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola
98719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
99719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// isRecordLayoutComplete - Return true if the specified type is already
100719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// completely laid out.
101719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golinbool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
102719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
103719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  RecordDeclTypes.find(Ty);
104719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  return I != RecordDeclTypes.end() && !I->second->isOpaque();
105719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin}
106719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
107719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golinstatic bool
108719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato GolinisSafeToConvert(QualType T, CodeGenTypes &CGT,
109719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
110719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
111719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
112cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola/// isSafeToConvert - Return true if it is safe to convert the specified record
113cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola/// decl to IR and lay it out, false if doing so would cause us to get into a
114719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// recursive compilation mess.
115719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golinstatic bool
116719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato GolinisSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
117719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
118719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // If we have already checked this type (maybe the same type is used by-value
119719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // multiple times in multiple structure fields, don't check again.
120719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  if (!AlreadyChecked.insert(RD)) return true;
121719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
122719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
123719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
124719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // If this type is already laid out, converting it is a noop.
125719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  if (CGT.isRecordLayoutComplete(Key)) return true;
126719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
127719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // If this type is currently being laid out, we can't recursively compile it.
128719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  if (CGT.isRecordBeingLaidOut(Key))
129cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    return false;
130cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola
131cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // If this type would require laying out bases that are currently being laid
132719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // out, don't do it.  This includes virtual base classes which get laid out
133cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // when a class is translated, even though they aren't embedded by-value into
134cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // the class.
135cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
136cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    for (CXXRecordDecl::base_class_const_iterator I = CRD->bases_begin(),
137cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola         E = CRD->bases_end(); I != E; ++I)
138cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola      if (!isSafeToConvert(I->getType()->getAs<RecordType>()->getDecl(),
139cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola                           CGT, AlreadyChecked))
140cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola        return false;
141cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  }
142cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola
143cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // If this type would require laying out members that are currently being laid
144cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // out, don't do it.
145cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  for (RecordDecl::field_iterator I = RD->field_begin(),
1463336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola       E = RD->field_end(); I != E; ++I)
147cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola    if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
148cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola      return false;
149cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola
150719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // If there are no problems, lets do it.
151719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  return true;
152719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin}
153719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
154719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// isSafeToConvert - Return true if it is safe to convert this field type,
155719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// which requires the structure elements contained by-value to all be
156719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// recursively safe to convert.
157719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golinstatic bool
158719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato GolinisSafeToConvert(QualType T, CodeGenTypes &CGT,
159cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola                llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
160cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  T = T.getCanonicalType();
161f009a961caa75465999ef3bc764984d97a7da331Jason W Kim
162719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // If this is a record, check it.
163719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  if (const RecordType *RT = dyn_cast<RecordType>(T))
164719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin    return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
165719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
166719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // If this is an array, check the elements, which are embedded inline.
167719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  if (const ArrayType *AT = dyn_cast<ArrayType>(T))
168719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin    return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
169719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
170719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // Otherwise, there is no concern about transforming this.  We only care about
171719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // things that are contained by-value in a structure that can have another
172719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // structure as a member.
173f009a961caa75465999ef3bc764984d97a7da331Jason W Kim  return true;
174f009a961caa75465999ef3bc764984d97a7da331Jason W Kim}
175cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola
1763336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola
1773336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola/// isSafeToConvert - Return true if it is safe to convert the specified record
178cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola/// decl to IR and lay it out, false if doing so would cause us to get into a
1793336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola/// recursive compilation mess.
1803336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindolastatic bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
181cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // If no structs are being laid out, we can certainly do this one.
1823336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola  if (CGT.noRecordsBeingLaidOut()) return true;
1833336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola
1843336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola  llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
1853336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola  return isSafeToConvert(RD, CGT, AlreadyChecked);
1863336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola}
1873336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola
188cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola
189719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// isFuncTypeArgumentConvertible - Return true if the specified type in a
190719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// function argument or result position can be converted to an IR type at this
191719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// point.  This boils down to being whether it is complete, as well as whether
192719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// we've temporarily deferred expanding the type because we're in a recursive
193719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin/// context.
194719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golinbool CodeGenTypes::isFuncTypeArgumentConvertible(QualType Ty) {
195bc2198133a1836598b54b943420748e75d5dea94Craig Topper  // If this isn't a tagged type, we can convert it!
196719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  const TagType *TT = Ty->getAs<TagType>();
197719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  if (TT == 0) return true;
198719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
199719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // Incomplete types cannot be converted.
200590853667345d6fb191764b9d0bd2ff13589e3a3Benjamin Kramer  if (TT->isIncompleteType())
201719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin    return false;
202719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin
203719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  // If this is an enum, then it is always safe to convert.
204719927a68f5b8ca34bacbeb7c970f281e27cbf63Renato Golin  const RecordType *RT = dyn_cast<RecordType>(TT);
2053336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola  if (RT == 0) return true;
2063336384239e563bdc5f3dbb8affec6c1e9ffbc47Rafael Espindola
207cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // Otherwise, we have to be careful.  If it is a struct that we're in the
208cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // process of expanding, then we can't convert the function type.  That's ok
209cecbc3d28277ff4916326311cbf87335ed05d106Rafael Espindola  // though because we must be in a pointer context under the struct, so we can
2107bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola  // just convert it to a dummy type.
2117bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola  //
212baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach  // We decide this by checking whether ConvertRecordDeclType returns us an
213baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach  // opaque type for a struct that we know is defined.
214baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach  return isSafeToConvert(RT->getDecl(), *this);
215baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach}
216baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach
217baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach
218baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach/// Code to verify a given function type is complete, i.e. the return type
219baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach/// and all of the argument types are complete.  Also check to see if we are in
220baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach/// a RS_StructPointer context, and if so whether any struct types have been
221baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach/// pended.  If so, we don't want to ask the ABI lowering code to handle a type
222baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach/// that cannot be converted to an IR type.
223baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbachbool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
224baf120fbe8056ef68fc91b16465590fdf2311c27Jim Grosbach  if (!isFuncTypeArgumentConvertible(FT->getResultType()))
22527f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel    return false;
2260be77dff1147488814b8eea6ec8619f56e3d9f5eDevang Patel
22727f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
22827f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel    for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
2290be77dff1147488814b8eea6ec8619f56e3d9f5eDevang Patel      if (!isFuncTypeArgumentConvertible(FPT->getArgType(i)))
23027f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel        return false;
23127f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel
23227f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  return true;
2330a6ea83f393d06fb424c470777a1c3e8a8c50ab1Devang Patel}
23427f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel
23527f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel/// UpdateCompletedType - When we find the full definition for a TagDecl,
23627f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel/// replace the 'opaque' type we previously made for it if applicable.
2378e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbachvoid CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
23827f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  // If this is an enum being completed, then we flush all non-struct types from
23927f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  // the cache.  This allows function types and other things that may be derived
24027f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  // from the enum to be recomputed.
24127f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
24227f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel    // Only flush the cache if we've actually already converted this type.
24327f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel    if (TypeCache.count(ED->getTypeForDecl())) {
24427f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel      // Okay, we formed some types based on this.  We speculated that the enum
24527f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel      // would be lowered to i32, so we only need to flush the cache if this
24627f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel      // didn't happen.
24727f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel      if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
24827f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel        TypeCache.clear();
24927f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel    }
25027f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel    return;
25127f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  }
25227f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel
25327f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  // If we completed a RecordDecl that we previously used and converted to an
25427f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  // anonymous type, then go ahead and complete it now.
25527f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  const RecordDecl *RD = cast<RecordDecl>(TD);
25627f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  if (RD->isDependentType()) return;
25727f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel
25827f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  // Only complete it if we converted it already.  If we haven't converted it
25971f3f1146f2ba2773f0467767b67c12258960f34Devang Patel  // yet, we'll just do it lazily.
2600a6ea83f393d06fb424c470777a1c3e8a8c50ab1Devang Patel  if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
26171f3f1146f2ba2773f0467767b67c12258960f34Devang Patel    ConvertRecordDeclType(RD);
2628e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbach}
2638e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbach
26471f3f1146f2ba2773f0467767b67c12258960f34Devang Patelstatic llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
26571f3f1146f2ba2773f0467767b67c12258960f34Devang Patel                                    const llvm::fltSemantics &format) {
26671f3f1146f2ba2773f0467767b67c12258960f34Devang Patel  if (&format == &llvm::APFloat::IEEEhalf)
26771f3f1146f2ba2773f0467767b67c12258960f34Devang Patel    return llvm::Type::getInt16Ty(VMContext);
2688e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbach  if (&format == &llvm::APFloat::IEEEsingle)
26971f3f1146f2ba2773f0467767b67c12258960f34Devang Patel    return llvm::Type::getFloatTy(VMContext);
27071f3f1146f2ba2773f0467767b67c12258960f34Devang Patel  if (&format == &llvm::APFloat::IEEEdouble)
27171f3f1146f2ba2773f0467767b67c12258960f34Devang Patel    return llvm::Type::getDoubleTy(VMContext);
27271f3f1146f2ba2773f0467767b67c12258960f34Devang Patel  if (&format == &llvm::APFloat::IEEEquad)
27371f3f1146f2ba2773f0467767b67c12258960f34Devang Patel    return llvm::Type::getFP128Ty(VMContext);
27471f3f1146f2ba2773f0467767b67c12258960f34Devang Patel  if (&format == &llvm::APFloat::PPCDoubleDouble)
27571f3f1146f2ba2773f0467767b67c12258960f34Devang Patel    return llvm::Type::getPPC_FP128Ty(VMContext);
27671f3f1146f2ba2773f0467767b67c12258960f34Devang Patel  if (&format == &llvm::APFloat::x87DoubleExtended)
27771f3f1146f2ba2773f0467767b67c12258960f34Devang Patel    return llvm::Type::getX86_FP80Ty(VMContext);
27871f3f1146f2ba2773f0467767b67c12258960f34Devang Patel  llvm_unreachable("Unknown float format!");
27971f3f1146f2ba2773f0467767b67c12258960f34Devang Patel}
28071f3f1146f2ba2773f0467767b67c12258960f34Devang Patel
28171f3f1146f2ba2773f0467767b67c12258960f34Devang Patel/// ConvertType - Convert the specified type to its LLVM form.
28227f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patelllvm::Type *CodeGenTypes::ConvertType(QualType T) {
28327f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  T = Context.getCanonicalType(T);
28427f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel
28527f5acb7d49c3ca2e3f7fe13d97cc19a78b15e1aDevang Patel  const Type *Ty = T.getTypePtr();
286953ebb769ada06d22f8ae4963651530b9cb84830Chris Lattner
2872fec6c5ff153786744ba7d0d302b73179731c5e9Owen Anderson  // RecordTypes are cached and processed specially.
2882fec6c5ff153786744ba7d0d302b73179731c5e9Owen Anderson  if (const RecordType *RT = dyn_cast<RecordType>(Ty))
289953ebb769ada06d22f8ae4963651530b9cb84830Chris Lattner    return ConvertRecordDeclType(RT->getDecl());
290ce79299f78bb04e76e1860ab119b85d69f3a19c7Jim Grosbach
2916469540adf63d94a876c2b623cb4ca70479647f7Rafael Espindola  // See if type is already cached.
292953ebb769ada06d22f8ae4963651530b9cb84830Chris Lattner  llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
293b0739b78332275906cd5ace2ae0d65a29135667bJim Grosbach  // If type is found in map then use it. Otherwise, convert type T.
294953ebb769ada06d22f8ae4963651530b9cb84830Chris Lattner  if (TCI != TypeCache.end())
295953ebb769ada06d22f8ae4963651530b9cb84830Chris Lattner    return TCI->second;
296953ebb769ada06d22f8ae4963651530b9cb84830Chris Lattner
29734982576a43887e7f062ed0a3571af2cbab003f3James Molloy  // If we don't have it in the cache, convert it now.
29834982576a43887e7f062ed0a3571af2cbab003f3James Molloy  llvm::Type *ResultType = 0;
29934982576a43887e7f062ed0a3571af2cbab003f3James Molloy  switch (Ty->getTypeClass()) {
30034982576a43887e7f062ed0a3571af2cbab003f3James Molloy  case Type::Record: // Handled above.
3014a1ff2fb3ecaa2576ecf9ed84816b174c235ac1dBill Wendling#define TYPE(Class, Base)
30234982576a43887e7f062ed0a3571af2cbab003f3James Molloy#define ABSTRACT_TYPE(Class, Base)
30334982576a43887e7f062ed0a3571af2cbab003f3James Molloy#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
30434982576a43887e7f062ed0a3571af2cbab003f3James Molloy#define DEPENDENT_TYPE(Class, Base) case Type::Class:
30534982576a43887e7f062ed0a3571af2cbab003f3James Molloy#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
30634982576a43887e7f062ed0a3571af2cbab003f3James Molloy#include "clang/AST/TypeNodes.def"
30734982576a43887e7f062ed0a3571af2cbab003f3James Molloy    llvm_unreachable("Non-canonical or dependent types aren't possible.");
30834982576a43887e7f062ed0a3571af2cbab003f3James Molloy
30934982576a43887e7f062ed0a3571af2cbab003f3James Molloy  case Type::Builtin: {
31034982576a43887e7f062ed0a3571af2cbab003f3James Molloy    switch (cast<BuiltinType>(Ty)->getKind()) {
31134982576a43887e7f062ed0a3571af2cbab003f3James Molloy    case BuiltinType::Void:
31234982576a43887e7f062ed0a3571af2cbab003f3James Molloy    case BuiltinType::ObjCId:
3132317e40539aac11da00bd587b5f0def04d989769Jim Grosbach    case BuiltinType::ObjCClass:
3147bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola    case BuiltinType::ObjCSel:
3157bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola      // LLVM void type can only be used as the result of a function call.  Just
3167bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola      // map to the same as char.
317a8e2989ece6dc46df59b0768184028257f913843Evan Cheng      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
3186d63a728586d56eb3e881905beb9db27f520f5d3Evan Cheng      break;
319a8e2989ece6dc46df59b0768184028257f913843Evan Cheng
320d49fe1b6bc4615684c2ec71140a21e9c4cd69ce3Chris Lattner    case BuiltinType::Bool:
32132bd5f4f6a374f9ab0fcbd2cf6a8561019a6fd56Rafael Espindola      // Note that we always return bool as i1 for use as a scalar type.
32232bd5f4f6a374f9ab0fcbd2cf6a8561019a6fd56Rafael Espindola      ResultType = llvm::Type::getInt1Ty(getLLVMContext());
323055b0310f862b91f33699037ce67d3ab8137c20cEvan Cheng      break;
32435c33bd772b3cfb34fdc6b5c9171f955454d0043Chris Lattner
325055b0310f862b91f33699037ce67d3ab8137c20cEvan Cheng    case BuiltinType::Char_S:
3265cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov    case BuiltinType::Char_U:
3275cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov    case BuiltinType::SChar:
3282f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola    case BuiltinType::UChar:
329bc2198133a1836598b54b943420748e75d5dea94Craig Topper    case BuiltinType::Short:
3305bafff36c798608a189c517d37527e4a38863071Bob Wilson    case BuiltinType::UShort:
3315bafff36c798608a189c517d37527e4a38863071Bob Wilson    case BuiltinType::Int:
3328bc86cba60fbb35fbfb52cc32b9e451e6b903a27Chris Lattner    case BuiltinType::UInt:
33335636281c7ab6eb128b4ced6bf7ae0b6b8458dd2Jim Grosbach    case BuiltinType::Long:
33435636281c7ab6eb128b4ced6bf7ae0b6b8458dd2Jim Grosbach    case BuiltinType::ULong:
3352f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola    case BuiltinType::LongLong:
3365bafff36c798608a189c517d37527e4a38863071Bob Wilson    case BuiltinType::ULongLong:
337a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    case BuiltinType::WChar_S:
3385adb66a646e2ec32265263739f5b01c3f50c176aEvan Cheng    case BuiltinType::WChar_U:
339632606c724ebcfa6a9da71c443151e7a65829c99Anton Korobeynikov    case BuiltinType::Char16:
3405cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov    case BuiltinType::Char32:
341650b7d76afbc7db2dd1a4590149d50a162bb25d8Jason W Kim      ResultType = llvm::IntegerType::get(getLLVMContext(),
3425cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov                                 static_cast<unsigned>(Context.getTypeSize(T)));
3435cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov      break;
344650b7d76afbc7db2dd1a4590149d50a162bb25d8Jason W Kim
3455cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov    case BuiltinType::Half:
346632606c724ebcfa6a9da71c443151e7a65829c99Anton Korobeynikov      // Half is special: it might be lowered to i16 (and will be storage-only
3472f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola      // type),. or can be represented as a set of native operations.
348a8e2989ece6dc46df59b0768184028257f913843Evan Cheng
3492f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola      // FIXME: Ask target which kind of half FP it prefers (storage only vs
3501b2eb0e8a6aaf034675b17be6d853cb1c666200fChris Lattner      // native).
3512f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola      ResultType = llvm::Type::getInt16Ty(getLLVMContext());
35284b19be6ab9544f72eafb11048a1121f5ea77c95Rafael Espindola      break;
35346510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    case BuiltinType::Float:
3545cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov    case BuiltinType::Double:
3555cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov    case BuiltinType::LongDouble:
3565cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov      ResultType = getTypeForFormat(getLLVMContext(),
3575cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov                                    Context.getFloatTypeSemantics(T));
3585cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov      break;
3595cdc3a949af0cef7f2163f8a7acbf3049c226321Anton Korobeynikov
360d62f1b4168d4327c119642d28c26c836ae6717abChris Lattner    case BuiltinType::NullPtr:
3617751ad92daeea5a3502fbc266ae814baec5c03e6Anton Korobeynikov      // Model std::nullptr_t as i8*
3620c08d092049c025c9ccf7143e39f39dc4e30d6b4Chris Lattner      ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
3631d6111c5ac97c321782637b2cd72e2c3e4d3d694Jim Grosbach      break;
3640ae4a3357a556261f25b1584a2d9914637c69e65Lauro Ramos Venancio
3652f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola    case BuiltinType::UInt128:
366a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    case BuiltinType::Int128:
367a8e2989ece6dc46df59b0768184028257f913843Evan Cheng      ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
36810b318bcb39218d2ed525e4862c854bc8d1baf63Chris Lattner      break;
3691d6111c5ac97c321782637b2cd72e2c3e4d3d694Jim Grosbach
3700ae4a3357a556261f25b1584a2d9914637c69e65Lauro Ramos Venancio    case BuiltinType::Dependent:
3712f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola#define BUILTIN_TYPE(Id, SingletonId)
372a8e2989ece6dc46df59b0768184028257f913843Evan Cheng#define PLACEHOLDER_TYPE(Id, SingletonId) \
3732f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola    case BuiltinType::Id:
3741b46f433e02155daba8ed3b1269c86ce63c9713bChris Lattner#include "clang/AST/BuiltinTypes.def"
3752f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola      llvm_unreachable("Unexpected placeholder builtin type!");
376a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    }
3771b46f433e02155daba8ed3b1269c86ce63c9713bChris Lattner    break;
378a8e2989ece6dc46df59b0768184028257f913843Evan Cheng  }
3792f99b6bd9601ae8d4fd248f9bb701283795c38a8Rafael Espindola  case Type::Complex: {
3807bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola    llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
3817bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola    ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
382055b0310f862b91f33699037ce67d3ab8137c20cEvan Cheng    break;
383055b0310f862b91f33699037ce67d3ab8137c20cEvan Cheng  }
3840890cf124f00da3dc943c1882f4221955e0281edChris Lattner  case Type::LValueReference:
3850890cf124f00da3dc943c1882f4221955e0281edChris Lattner  case Type::RValueReference: {
3860890cf124f00da3dc943c1882f4221955e0281edChris Lattner    const ReferenceType *RTy = cast<ReferenceType>(Ty);
3870890cf124f00da3dc943c1882f4221955e0281edChris Lattner    QualType ETy = RTy->getPointeeType();
3880890cf124f00da3dc943c1882f4221955e0281edChris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
389bfcb09688c7db15a9f9415d717a5a31c499a2208Chris Lattner    unsigned AS = Context.getTargetAddressSpace(ETy);
3900890cf124f00da3dc943c1882f4221955e0281edChris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
3919b97a73dedf736e14b04a3d1a153f10d25b2507bChris Lattner    break;
3920890cf124f00da3dc943c1882f4221955e0281edChris Lattner  }
3930890cf124f00da3dc943c1882f4221955e0281edChris Lattner  case Type::Pointer: {
3940890cf124f00da3dc943c1882f4221955e0281edChris Lattner    const PointerType *PTy = cast<PointerType>(Ty);
3950890cf124f00da3dc943c1882f4221955e0281edChris Lattner    QualType ETy = PTy->getPointeeType();
3960890cf124f00da3dc943c1882f4221955e0281edChris Lattner    llvm::Type *PointeeType = ConvertTypeForMem(ETy);
3970890cf124f00da3dc943c1882f4221955e0281edChris Lattner    if (PointeeType->isVoidTy())
398281e7767df71b3f727ade80a16ff0c4fe5a49dd9Chris Lattner      PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
3999b97a73dedf736e14b04a3d1a153f10d25b2507bChris Lattner    unsigned AS = Context.getTargetAddressSpace(ETy);
400bfcb09688c7db15a9f9415d717a5a31c499a2208Chris Lattner    ResultType = llvm::PointerType::get(PointeeType, AS);
401bfcb09688c7db15a9f9415d717a5a31c499a2208Chris Lattner    break;
402433a5785cc8201a8a384f0a5648d3dbac87f9fbcJim Grosbach  }
403433a5785cc8201a8a384f0a5648d3dbac87f9fbcJim Grosbach
404433a5785cc8201a8a384f0a5648d3dbac87f9fbcJim Grosbach  case Type::VariableArray: {
405433a5785cc8201a8a384f0a5648d3dbac87f9fbcJim Grosbach    const VariableArrayType *A = cast<VariableArrayType>(Ty);
406433a5785cc8201a8a384f0a5648d3dbac87f9fbcJim Grosbach    assert(A->getIndexTypeCVRQualifiers() == 0 &&
4079b97a73dedf736e14b04a3d1a153f10d25b2507bChris Lattner           "FIXME: We only handle trivial array types so far!");
4080890cf124f00da3dc943c1882f4221955e0281edChris Lattner    // VLAs resolve to the innermost element type; this matches
4090890cf124f00da3dc943c1882f4221955e0281edChris Lattner    // the return of alloca, and there isn't any obviously better choice.
410055b0310f862b91f33699037ce67d3ab8137c20cEvan Cheng    ResultType = ConvertTypeForMem(A->getElementType());
411c75c028a15a13786eee585aa634b4faf694dd00aChris Lattner    break;
412c75c028a15a13786eee585aa634b4faf694dd00aChris Lattner  }
413a8e2989ece6dc46df59b0768184028257f913843Evan Cheng  case Type::IncompleteArray: {
414a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
415a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    assert(A->getIndexTypeCVRQualifiers() == 0 &&
4168e9ece75db5045ec057efbbdba6550fa0d85e695Anton Korobeynikov           "FIXME: We only handle trivial array types so far!");
417a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    // int X[] -> [0 x int], unless the element type is not sized.  If it is
418a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    // unsized (e.g. an incomplete struct) just use [0 x i8].
4199b4b00ad436daeac6f97f77e9b5a3cc67ada150cBob Wilson    ResultType = ConvertTypeForMem(A->getElementType());
4209b4b00ad436daeac6f97f77e9b5a3cc67ada150cBob Wilson    if (!ResultType->isSized()) {
4212f24c4ece09f1157c5cb29357d91d2a0d77eb57cJim Grosbach      SkippedLayout = true;
4222f24c4ece09f1157c5cb29357d91d2a0d77eb57cJim Grosbach      ResultType = llvm::Type::getInt8Ty(getLLVMContext());
4232f24c4ece09f1157c5cb29357d91d2a0d77eb57cJim Grosbach    }
4249b4b00ad436daeac6f97f77e9b5a3cc67ada150cBob Wilson    ResultType = llvm::ArrayType::get(ResultType, 0);
4259b4b00ad436daeac6f97f77e9b5a3cc67ada150cBob Wilson    break;
4269b4b00ad436daeac6f97f77e9b5a3cc67ada150cBob Wilson  }
4279b4b00ad436daeac6f97f77e9b5a3cc67ada150cBob Wilson  case Type::ConstantArray: {
4284f38b383d5089c49489a9a56d8efd0eb76048b3fBob Wilson    const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
4294f38b383d5089c49489a9a56d8efd0eb76048b3fBob Wilson    llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
4302317e40539aac11da00bd587b5f0def04d989769Jim Grosbach
4318f3434647d3d39b49475239e3be1b8afb06415cfBob Wilson    // Lower arrays of undefined struct type to arrays of i8 just to have a
432e21e39666e8a41ffd4971d8bb023b70b59297267Evan Cheng    // concrete type.
433d831cda3e74235704f163d5a18352584d537517aEvan Cheng    if (!EltTy->isSized()) {
43435c33bd772b3cfb34fdc6b5c9171f955454d0043Chris Lattner      SkippedLayout = true;
43523a95704949b99ca07afe45c6946d0fa26baf9f3Evan Cheng      EltTy = llvm::Type::getInt8Ty(getLLVMContext());
4360628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher    }
4370628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher
4380628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher    ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
4390628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher    break;
4400628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher  }
4410628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher  case Type::ExtVector:
4420628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher  case Type::Vector: {
4430628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher    const VectorType *VT = cast<VectorType>(Ty);
4440628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher    ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
4450628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher                                       VT->getNumElements());
4460628d38085b28a59a4b13d7e35760cce54f0af7aEric Christopher    break;
4474db7dec70b06a1d50a265c3666e126065e09f396Eric Christopher  }
448fef50062eae28fc6d893cd3ef528f8ca85cd50b0Eric Christopher  case Type::FunctionNoProto:
449e1739d598d2c980822cc42bbf9821b91ebbc829fEric Christopher  case Type::FunctionProto: {
450e1739d598d2c980822cc42bbf9821b91ebbc829fEric Christopher    const FunctionType *FT = cast<FunctionType>(Ty);
451e1739d598d2c980822cc42bbf9821b91ebbc829fEric Christopher    // First, check whether we can build the full function type.  If the
452e1739d598d2c980822cc42bbf9821b91ebbc829fEric Christopher    // function type depends on an incomplete type (e.g. a struct or enum), we
453fef50062eae28fc6d893cd3ef528f8ca85cd50b0Eric Christopher    // cannot lower the function type.
4544db7dec70b06a1d50a265c3666e126065e09f396Eric Christopher    if (!isFuncTypeConvertible(FT)) {
4554db7dec70b06a1d50a265c3666e126065e09f396Eric Christopher      // This function's type depends on an incomplete tag type.
4564db7dec70b06a1d50a265c3666e126065e09f396Eric Christopher      // Return a placeholder type.
4574db7dec70b06a1d50a265c3666e126065e09f396Eric Christopher      ResultType = llvm::StructType::get(getLLVMContext());
4583c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher
4593c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher      SkippedLayout = true;
4603c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher      break;
4613c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    }
4623c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher
4633c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    // While we're converting the argument types for a function, we don't want
4643c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    // to recursively convert any pointed-to structs.  Converting directly-used
4653c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    // structs is ok though.
4668e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbach    if (!RecordsBeingLaidOut.insert(Ty)) {
4673c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher      ResultType = llvm::StructType::get(getLLVMContext());
4688e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbach
4693c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher      SkippedLayout = true;
4703c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher      break;
4713c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    }
4723c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher
4733c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    // The function type can be built; call the appropriate routines to
4743c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    // build it.
4758e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbach    const CGFunctionInfo *FI;
4763c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
4773c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher      FI = &arrangeFunctionType(
4783c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher                   CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
4793c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    } else {
4803c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher      const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
4813c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher      FI = &arrangeFunctionType(
4823c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher                CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
4833c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    }
484f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola
485f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    // If there is something higher level prodding our CGFunctionInfo, then
486f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    // don't recurse into it again.
487f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    if (FunctionsBeingProcessed.count(FI)) {
488f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola
489f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola      ResultType = llvm::StructType::get(getLLVMContext());
490f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola      SkippedLayout = true;
491f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    } else {
492f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola
493f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola      // Otherwise, we're good to go, go ahead and convert it.
494f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola      ResultType = GetFunctionType(*FI);
495f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    }
496f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola
497f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    RecordsBeingLaidOut.erase(Ty);
498f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola
499f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    if (SkippedLayout)
500f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola      TypeCache.clear();
501f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola
502f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    if (RecordsBeingLaidOut.empty())
503f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola      while (!DeferredRecords.empty())
504f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola        ConvertRecordDeclType(DeferredRecords.pop_back_val());
505f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    break;
506fef50062eae28fc6d893cd3ef528f8ca85cd50b0Eric Christopher  }
5079cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson
5089cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson  case Type::ObjCObject:
5099cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson    ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
5109cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson    break;
5119cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson
5129cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson  case Type::ObjCInterface: {
5139cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson    // Objective-C interfaces are always opaque (outside of the
5149cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson    // runtime, which can do whatever it likes); we never refine
5159cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson    // these.
5169cd2b9562d23909937ab2e4bb45c2a1ed4c86816Bob Wilson    llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
517f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    if (!T)
518f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola      T = llvm::StructType::create(getLLVMContext());
519f5ade5d39abe5cb12a8202c604321d5992e4a168Rafael Espindola    ResultType = T;
5203c14f24c9da3f811d3530e984e692acf1a471b91Eric Christopher    break;
521fef50062eae28fc6d893cd3ef528f8ca85cd50b0Eric Christopher  }
522fef50062eae28fc6d893cd3ef528f8ca85cd50b0Eric Christopher
523d984eb6073d5445f08fb0cea67a668b1b5e888e0Bob Wilson  case Type::ObjCObjectPointer: {
52484f60b7359e1aa90794bb19de2bbf4d25dc2f01dEvan Cheng    // Protocol qualifications do not influence the LLVM type, we just return a
525a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    // pointer to the underlying interface type. We don't need to worry about
526e9952213086c865eb678bd3f4c9c7d849f0249d2Jim Grosbach    // recursive conversion.
52735c33bd772b3cfb34fdc6b5c9171f955454d0043Chris Lattner    llvm::Type *T =
528a8e2989ece6dc46df59b0768184028257f913843Evan Cheng      ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
529a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    ResultType = T->getPointerTo();
530a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    break;
531224c244f56025c10e70e4204daceadfb3cdd2c06Bob Wilson  }
532055b0310f862b91f33699037ce67d3ab8137c20cEvan Cheng
533c75c028a15a13786eee585aa634b4faf694dd00aChris Lattner  case Type::Enum: {
534c75c028a15a13786eee585aa634b4faf694dd00aChris Lattner    const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
535a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    if (ED->isCompleteDefinition() || ED->isFixed())
536a8e2989ece6dc46df59b0768184028257f913843Evan Cheng      return ConvertType(ED->getIntegerType());
537a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    // Return a placeholder 'i32' type.  This can be changed later when the
5388e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbach    // type is defined (see UpdateCompletedType), but is likely to be the
5398f8946389418ce3cdbf69bbf34443efe0c874b40Eric Christopher    // "right" answer.
54032bfb2c513c4efdc1db9967ddfecce8c922dda4fEric Christopher    ResultType = llvm::Type::getInt32Ty(getLLVMContext());
5418f8946389418ce3cdbf69bbf34443efe0c874b40Eric Christopher    break;
5428f8946389418ce3cdbf69bbf34443efe0c874b40Eric Christopher  }
5438f8946389418ce3cdbf69bbf34443efe0c874b40Eric Christopher
5448f8946389418ce3cdbf69bbf34443efe0c874b40Eric Christopher  case Type::BlockPointer: {
5458f8946389418ce3cdbf69bbf34443efe0c874b40Eric Christopher    const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
5468f8946389418ce3cdbf69bbf34443efe0c874b40Eric Christopher    llvm::Type *PointeeType = ConvertTypeForMem(FTy);
5478f8946389418ce3cdbf69bbf34443efe0c874b40Eric Christopher    unsigned AS = Context.getTargetAddressSpace(FTy);
5488f8946389418ce3cdbf69bbf34443efe0c874b40Eric Christopher    ResultType = llvm::PointerType::get(PointeeType, AS);
5498e0c7697fd9b9354856074efc06eea9f6d80015cJim Grosbach    break;
550765cc0b9d59bf63dfcb02e3d126ea1c63e16f86fBob Wilson  }
551765cc0b9d59bf63dfcb02e3d126ea1c63e16f86fBob Wilson
5522317e40539aac11da00bd587b5f0def04d989769Jim Grosbach  case Type::MemberPointer: {
553224c244f56025c10e70e4204daceadfb3cdd2c06Bob Wilson    ResultType =
554224c244f56025c10e70e4204daceadfb3cdd2c06Bob Wilson      getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
555224c244f56025c10e70e4204daceadfb3cdd2c06Bob Wilson    break;
556812209a58c5520c604bc9279aa069e5ae066e860Bob Wilson  }
5570fb34683b9e33238288d2af1e090582464df8387Bob Wilson
5580fb34683b9e33238288d2af1e090582464df8387Bob Wilson  case Type::Atomic: {
5590fb34683b9e33238288d2af1e090582464df8387Bob Wilson    ResultType = ConvertType(cast<AtomicType>(Ty)->getValueType());
5600fb34683b9e33238288d2af1e090582464df8387Bob Wilson    break;
5610fb34683b9e33238288d2af1e090582464df8387Bob Wilson  }
5620fb34683b9e33238288d2af1e090582464df8387Bob Wilson  }
5630fb34683b9e33238288d2af1e090582464df8387Bob Wilson
5640fb34683b9e33238288d2af1e090582464df8387Bob Wilson  assert(ResultType && "Didn't convert a type?");
5650fb34683b9e33238288d2af1e090582464df8387Bob Wilson
566b0739b78332275906cd5ace2ae0d65a29135667bJim Grosbach  TypeCache[Ty] = ResultType;
5670d805c33d134d88169e3dc4a3272cff9a5713ce7Dan Gohman  return ResultType;
5680d805c33d134d88169e3dc4a3272cff9a5713ce7Dan Gohman}
56929e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilson
57029e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilson/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
57129e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilsonllvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
57229e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilson  // TagDecl's are not necessarily unique, instead use the (clang)
57329e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilson  // type connected to the decl.
57422772214de79aa1c5ca38c4fb1da137d8fb30a05Chris Lattner  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
57522772214de79aa1c5ca38c4fb1da137d8fb30a05Chris Lattner
57622772214de79aa1c5ca38c4fb1da137d8fb30a05Chris Lattner  llvm::StructType *&Entry = RecordDeclTypes[Key];
57729e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilson
57829e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilson  // If we don't have a StructType at all yet, create the forward declaration.
57929e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilson  if (Entry == 0) {
58022772214de79aa1c5ca38c4fb1da137d8fb30a05Chris Lattner    Entry = llvm::StructType::create(getLLVMContext());
58122772214de79aa1c5ca38c4fb1da137d8fb30a05Chris Lattner    addRecordTypeName(RD, Entry, "");
58222772214de79aa1c5ca38c4fb1da137d8fb30a05Chris Lattner  }
58329e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilson  llvm::StructType *Ty = Entry;
58429e066965fb84b3aad2840815c6d0602dafb0b17Bob Wilson
58563db594559dc8eac666204c7907bae664f5234daBob Wilson  // If this is still a forward declaration, or the LLVM type is already
58663db594559dc8eac666204c7907bae664f5234daBob Wilson  // complete, there's nothing more to do.
58763db594559dc8eac666204c7907bae664f5234daBob Wilson  RD = RD->getDefinition();
58863db594559dc8eac666204c7907bae664f5234daBob Wilson  if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque())
58963db594559dc8eac666204c7907bae664f5234daBob Wilson    return Ty;
59063db594559dc8eac666204c7907bae664f5234daBob Wilson
5910fb34683b9e33238288d2af1e090582464df8387Bob Wilson  // If converting this type would cause us to infinitely loop, don't do it!
5920fb34683b9e33238288d2af1e090582464df8387Bob Wilson  if (!isSafeToConvert(RD, *this)) {
5930fb34683b9e33238288d2af1e090582464df8387Bob Wilson    DeferredRecords.push_back(RD);
594e5165490b7ba24bb2f3043399e0d60e7f3bcf8a5Jim Grosbach    return Ty;
595afd1cc25786f68ca56a63d29ea2bd297990e9f81Jason W Kim  }
596d61eca533081580d56fabee38f86507d8019ca75Anton Korobeynikov
59788ce667003a33e008d9ecc6811584681787e8150Anton Korobeynikov  // Okay, this is a definition of a type.  Compile the implementation now.
59807043279f60622243d16d8a3f60805960482083cEvan Cheng  bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
599def9ac48b779a4cb0b1d1486286cda157a2fe86eJason W Kim  assert(InsertResult && "Recursively compiling a struct?");
6007bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola
6017bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola  // Force conversion of non-virtual base classes recursively.
6020f3cc657387d44cd7c56e4ddea896a50ab9106b8Anton Korobeynikov  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
6034a071d667d995b00e7853243ff9c7c1269324478Chris Lattner    for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
6045be54b00bdbe1abd02dde46ca2c4b0e5aaf7b537Evan Cheng         e = CRD->bases_end(); i != e; ++i) {
605f61159b574155b056dbd5d6d44f47f753d424056Chris Lattner      if (i->isVirtual()) continue;
6060d805c33d134d88169e3dc4a3272cff9a5713ce7Dan Gohman
6070d805c33d134d88169e3dc4a3272cff9a5713ce7Dan Gohman      ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
608b0f294c14b4e7098e5170ecfd528bcc9682ce0c7Chris Lattner    }
609b0f294c14b4e7098e5170ecfd528bcc9682ce0c7Chris Lattner  }
610e9952213086c865eb678bd3f4c9c7d849f0249d2Jim Grosbach
611a8e2989ece6dc46df59b0768184028257f913843Evan Cheng  // Layout fields.
612b0f294c14b4e7098e5170ecfd528bcc9682ce0c7Chris Lattner  CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
613cebae36f57456fe6b0e13726acd1e0250654f02dBill Wendling  CGRecordLayouts[Key] = Layout;
614b0f294c14b4e7098e5170ecfd528bcc9682ce0c7Chris Lattner
615ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  // We're done laying out this struct.
6166c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
617c076a9793936b140364671a5e39ee53bd266c6c3Chris Lattner  assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
618b0f294c14b4e7098e5170ecfd528bcc9682ce0c7Chris Lattner
619beb42691b1b58664e99cdc61cb5f4900298b432aChris Lattner  // If this struct blocked a FunctionType conversion, then recompute whatever
620beb42691b1b58664e99cdc61cb5f4900298b432aChris Lattner  // was derived from that.
621beb42691b1b58664e99cdc61cb5f4900298b432aChris Lattner  // FIXME: This is hugely overconservative.
62252a50e5d0e6ac08d86706dbdd8f4a5dbb44da4cbBill Wendling  if (SkippedLayout)
62352a50e5d0e6ac08d86706dbdd8f4a5dbb44da4cbBill Wendling    TypeCache.clear();
624cf6f28d84af09b38e96307007cd93760a7ca42d7Bill Wendling
62552a50e5d0e6ac08d86706dbdd8f4a5dbb44da4cbBill Wendling  // If we're done converting the outer-most record, then convert any deferred
626cf6f28d84af09b38e96307007cd93760a7ca42d7Bill Wendling  // structs as well.
627cf6f28d84af09b38e96307007cd93760a7ca42d7Bill Wendling  if (RecordsBeingLaidOut.empty())
628cf6f28d84af09b38e96307007cd93760a7ca42d7Bill Wendling    while (!DeferredRecords.empty())
629cf6f28d84af09b38e96307007cd93760a7ca42d7Bill Wendling      ConvertRecordDeclType(DeferredRecords.pop_back_val());
6305e1b55d67288874f8669621b9176814ce449f8f5Bill Wendling
6311b935a3d2e2619c7de2488163fc1501285b53fa3Jim Grosbach  return Ty;
6321b935a3d2e2619c7de2488163fc1501285b53fa3Jim Grosbach}
6331b935a3d2e2619c7de2488163fc1501285b53fa3Jim Grosbach
6341b935a3d2e2619c7de2488163fc1501285b53fa3Jim Grosbach/// getCGRecordLayout - Return record layout info for the given record decl.
63552a50e5d0e6ac08d86706dbdd8f4a5dbb44da4cbBill Wendlingconst CGRecordLayout &
63652a50e5d0e6ac08d86706dbdd8f4a5dbb44da4cbBill WendlingCodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
637cf6f28d84af09b38e96307007cd93760a7ca42d7Bill Wendling  const Type *Key = Context.getTagDeclType(RD).getTypePtr();
638ae94e594164b193236002516970aeec4c4574768Evan Cheng
639becd83e3f4eb996f8e43189ce482267b3b8351a8Bill Wendling  const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
640becd83e3f4eb996f8e43189ce482267b3b8351a8Bill Wendling  if (!Layout) {
641becd83e3f4eb996f8e43189ce482267b3b8351a8Bill Wendling    // Compute the type information.
642a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    ConvertRecordDeclType(RD);
643a8e2989ece6dc46df59b0768184028257f913843Evan Cheng
644e4d9ea83c0d4bcc535bd978e1afa599eb3ebb893Chris Lattner    // Now try again.
645e4d9ea83c0d4bcc535bd978e1afa599eb3ebb893Chris Lattner    Layout = CGRecordLayouts.lookup(Key);
6466c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  }
647f3231de60bb64c3f6fc6770b3e6174f4f839a4f3Chris Lattner
6480dc32ea0ef14c7e7f88edb12763a7828052a7fbdChris Lattner  assert(Layout && "Unable to find record layout information for type");
649beb42691b1b58664e99cdc61cb5f4900298b432aChris Lattner  return *Layout;
650beb42691b1b58664e99cdc61cb5f4900298b432aChris Lattner}
651becd83e3f4eb996f8e43189ce482267b3b8351a8Bill Wendling
652cebae36f57456fe6b0e13726acd1e0250654f02dBill Wendlingbool CodeGenTypes::isZeroInitializable(QualType T) {
653cebae36f57456fe6b0e13726acd1e0250654f02dBill Wendling  // No need to check for member pointers when not compiling C++.
654cebae36f57456fe6b0e13726acd1e0250654f02dBill Wendling  if (!Context.getLangOpts().CPlusPlus)
655becd83e3f4eb996f8e43189ce482267b3b8351a8Bill Wendling    return true;
656becd83e3f4eb996f8e43189ce482267b3b8351a8Bill Wendling
657becd83e3f4eb996f8e43189ce482267b3b8351a8Bill Wendling  T = Context.getBaseElementType(T);
658becd83e3f4eb996f8e43189ce482267b3b8351a8Bill Wendling
659becd83e3f4eb996f8e43189ce482267b3b8351a8Bill Wendling  // Records are non-zero-initializable if they contain any
660ae94e594164b193236002516970aeec4c4574768Evan Cheng  // non-zero-initializable subobjects.
661ae94e594164b193236002516970aeec4c4574768Evan Cheng  if (const RecordType *RT = T->getAs<RecordType>()) {
662a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
663a8e2989ece6dc46df59b0768184028257f913843Evan Cheng    return isZeroInitializable(RD);
664a8e2989ece6dc46df59b0768184028257f913843Evan Cheng  }
665a8e2989ece6dc46df59b0768184028257f913843Evan Cheng
666a8e2989ece6dc46df59b0768184028257f913843Evan Cheng  // We have to ask the ABI about member pointers.
667a5ad93a10a5435f21090b09edb6b3a7e44967648Chris Lattner  if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
668b01c4bbb4573e0007444e218b683840e4519e0c8Rafael Espindola    return getCXXABI().isZeroInitializable(MPT);
6697bc59bc3952ad7842b1e079753deb32217a768a3Rafael Espindola
6700bd89712c03c59ea43ce37763685e7f7c0bdd977Anton Korobeynikov  // Everything else is okay.
67197f06937449c593a248dbbb1365e6ae408fb9decChris Lattner  return true;
672def9ac48b779a4cb0b1d1486286cda157a2fe86eJason W Kim}
673def9ac48b779a4cb0b1d1486286cda157a2fe86eJason W Kim
674def9ac48b779a4cb0b1d1486286cda157a2fe86eJason W Kimbool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
675fa7fb64fad0e46e7329e4ba84a1edec5e979c31aJim Grosbach  return getCGRecordLayout(RD).isZeroInitializable();
676def9ac48b779a4cb0b1d1486286cda157a2fe86eJason W Kim}
677def9ac48b779a4cb0b1d1486286cda157a2fe86eJason W Kim