blob: 44494ae9897da54926c06e8ae7594f36335a54f0 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This is the code that handles AST -> LLVM type lowering.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenTypes.h"
John McCallf16aa102010-08-22 21:01:12 +000015#include "CGCXXABI.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "CGCall.h"
Guy Benyeib13621d2012-12-18 14:38:23 +000017#include "CGOpenCLRuntime.h"
Daniel Dunbar2924ade2010-03-30 22:26:10 +000018#include "CGRecordLayout.h"
John McCallde5d3c72012-02-17 03:33:10 +000019#include "TargetInfo.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000020#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000021#include "clang/AST/DeclCXX.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/AST/DeclObjC.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000023#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Mark Lacey8b549992013-10-30 21:53:58 +000025#include "clang/CodeGen/CGFunctionInfo.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Module.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30using namespace CodeGen;
31
John McCall64aa4b32013-04-16 22:48:15 +000032CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
33 : CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
34 TheDataLayout(cgm.getDataLayout()),
35 Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
John McCall64aa4b32013-04-16 22:48:15 +000036 TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
Chris Lattner57eb23f2011-07-10 05:39:13 +000037 SkippedLayout = false;
Chris Lattnerd2d2a112007-07-14 01:29:45 +000038}
Reid Spencer5f016e22007-07-11 17:01:13 +000039
Devang Patelb84a06e2007-10-23 02:10:49 +000040CodeGenTypes::~CodeGenTypes() {
Stephen Hines651f13c2014-04-23 16:59:28 -070041 llvm::DeleteContainerSeconds(CGRecordLayouts);
Chris Lattner6f41c172010-01-11 19:58:10 +000042
43 for (llvm::FoldingSet<CGFunctionInfo>::iterator
44 I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
45 delete &*I++;
Devang Patelb84a06e2007-10-23 02:10:49 +000046}
47
Chris Lattner9cbe4f02011-07-09 17:41:47 +000048void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
49 llvm::StructType *Ty,
Chris Lattner5f9e2722011-07-23 10:55:15 +000050 StringRef suffix) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000051 SmallString<256> TypeName;
Anders Carlssone9742b02011-04-17 21:36:59 +000052 llvm::raw_svector_ostream OS(TypeName);
Anders Carlssone0047b12011-04-20 23:51:43 +000053 OS << RD->getKindName() << '.';
Anders Carlssone9742b02011-04-17 21:36:59 +000054
55 // Name the codegen type after the typedef name
56 // if there is no tag type name available
Anders Carlssone0047b12011-04-20 23:51:43 +000057 if (RD->getIdentifier()) {
Anders Carlssone9742b02011-04-17 21:36:59 +000058 // FIXME: We should not have to check for a null decl context here.
59 // Right now we do it because the implicit Obj-C decls don't have one.
Anders Carlssone0047b12011-04-20 23:51:43 +000060 if (RD->getDeclContext())
Benjamin Kramerb063ef02013-02-23 13:53:57 +000061 RD->printQualifiedName(OS);
Anders Carlssone9742b02011-04-17 21:36:59 +000062 else
Anders Carlssone0047b12011-04-20 23:51:43 +000063 RD->printName(OS);
64 } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
Anders Carlssone9742b02011-04-17 21:36:59 +000065 // FIXME: We should not have to check for a null decl context here.
66 // Right now we do it because the implicit Obj-C decls don't have one.
67 if (TDD->getDeclContext())
Benjamin Kramerb063ef02013-02-23 13:53:57 +000068 TDD->printQualifiedName(OS);
Anders Carlssone9742b02011-04-17 21:36:59 +000069 else
70 TDD->printName(OS);
71 } else
72 OS << "anon";
73
74 if (!suffix.empty())
75 OS << suffix;
76
Chris Lattner9cbe4f02011-07-09 17:41:47 +000077 Ty->setName(OS.str());
Devang Patel30ec9972007-10-25 18:32:36 +000078}
79
Chris Lattner4581fff2008-02-06 05:21:55 +000080/// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
81/// ConvertType in that it is used to convert to the memory representation for
82/// a type. For example, the scalar representation for _Bool is i1, but the
83/// memory representation is usually i8 or i32, depending on the target.
Stephen Hines176edba2014-12-01 14:53:08 -080084llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000085 llvm::Type *R = ConvertType(T);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Chris Lattner19009e62008-01-09 18:47:25 +000087 // If this is a non-bool type, don't map it.
Duncan Sandsf177d9d2010-02-15 16:14:01 +000088 if (!R->isIntegerTy(1))
Chris Lattner19009e62008-01-09 18:47:25 +000089 return R;
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattner19009e62008-01-09 18:47:25 +000091 // Otherwise, return an integer of the target-specified size.
Owen Anderson0032b272009-08-13 21:57:51 +000092 return llvm::IntegerType::get(getLLVMContext(),
93 (unsigned)Context.getTypeSize(T));
Chris Lattner19009e62008-01-09 18:47:25 +000094}
95
Chris Lattner71305cc2011-07-15 05:16:14 +000096
97/// isRecordLayoutComplete - Return true if the specified type is already
98/// completely laid out.
99bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
100 llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
101 RecordDeclTypes.find(Ty);
102 return I != RecordDeclTypes.end() && !I->second->isOpaque();
103}
104
105static bool
106isSafeToConvert(QualType T, CodeGenTypes &CGT,
107 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
108
109
110/// isSafeToConvert - Return true if it is safe to convert the specified record
111/// decl to IR and lay it out, false if doing so would cause us to get into a
112/// recursive compilation mess.
113static bool
114isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
115 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
116 // If we have already checked this type (maybe the same type is used by-value
117 // multiple times in multiple structure fields, don't check again.
Stephen Hines176edba2014-12-01 14:53:08 -0800118 if (!AlreadyChecked.insert(RD).second)
119 return true;
120
Chris Lattner71305cc2011-07-15 05:16:14 +0000121 const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
122
123 // If this type is already laid out, converting it is a noop.
124 if (CGT.isRecordLayoutComplete(Key)) return true;
125
126 // If this type is currently being laid out, we can't recursively compile it.
127 if (CGT.isRecordBeingLaidOut(Key))
128 return false;
129
130 // If this type would require laying out bases that are currently being laid
131 // out, don't do it. This includes virtual base classes which get laid out
132 // when a class is translated, even though they aren't embedded by-value into
133 // the class.
134 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700135 for (const auto &I : CRD->bases())
136 if (!isSafeToConvert(I.getType()->getAs<RecordType>()->getDecl(),
Chris Lattner71305cc2011-07-15 05:16:14 +0000137 CGT, AlreadyChecked))
138 return false;
139 }
140
141 // If this type would require laying out members that are currently being laid
142 // out, don't do it.
Stephen Hines651f13c2014-04-23 16:59:28 -0700143 for (const auto *I : RD->fields())
Chris Lattner71305cc2011-07-15 05:16:14 +0000144 if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
145 return false;
146
147 // If there are no problems, lets do it.
148 return true;
149}
150
151/// isSafeToConvert - Return true if it is safe to convert this field type,
152/// which requires the structure elements contained by-value to all be
153/// recursively safe to convert.
154static bool
155isSafeToConvert(QualType T, CodeGenTypes &CGT,
156 llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
157 T = T.getCanonicalType();
158
159 // If this is a record, check it.
160 if (const RecordType *RT = dyn_cast<RecordType>(T))
161 return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
162
163 // If this is an array, check the elements, which are embedded inline.
164 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
165 return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
166
167 // Otherwise, there is no concern about transforming this. We only care about
168 // things that are contained by-value in a structure that can have another
169 // structure as a member.
170 return true;
171}
172
173
174/// isSafeToConvert - Return true if it is safe to convert the specified record
175/// decl to IR and lay it out, false if doing so would cause us to get into a
176/// recursive compilation mess.
177static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
178 // If no structs are being laid out, we can certainly do this one.
179 if (CGT.noRecordsBeingLaidOut()) return true;
180
181 llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
182 return isSafeToConvert(RD, CGT, AlreadyChecked);
183}
184
Stephen Hines651f13c2014-04-23 16:59:28 -0700185/// isFuncParamTypeConvertible - Return true if the specified type in a
186/// function parameter or result position can be converted to an IR type at this
Chris Lattnerf742eb02011-07-10 00:18:59 +0000187/// point. This boils down to being whether it is complete, as well as whether
188/// we've temporarily deferred expanding the type because we're in a recursive
189/// context.
Stephen Hines651f13c2014-04-23 16:59:28 -0700190bool CodeGenTypes::isFuncParamTypeConvertible(QualType Ty) {
Stephen Hines176edba2014-12-01 14:53:08 -0800191 // Some ABIs cannot have their member pointers represented in IR unless
192 // certain circumstances have been reached.
193 if (const auto *MPT = Ty->getAs<MemberPointerType>())
194 return getCXXABI().isMemberPointerConvertible(MPT);
195
Chris Lattnerf742eb02011-07-10 00:18:59 +0000196 // If this isn't a tagged type, we can convert it!
197 const TagType *TT = Ty->getAs<TagType>();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700198 if (!TT) return true;
199
Douglas Gregorbcf38f22012-02-25 00:16:17 +0000200 // Incomplete types cannot be converted.
Douglas Gregor4a59bc22012-02-25 00:06:47 +0000201 if (TT->isIncompleteType())
Chris Lattnerf742eb02011-07-10 00:18:59 +0000202 return false;
Stephen Hines176edba2014-12-01 14:53:08 -0800203
Chris Lattner71305cc2011-07-15 05:16:14 +0000204 // If this is an enum, then it is always safe to convert.
Chris Lattnerf742eb02011-07-10 00:18:59 +0000205 const RecordType *RT = dyn_cast<RecordType>(TT);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700206 if (!RT) return true;
Chris Lattnerf742eb02011-07-10 00:18:59 +0000207
208 // Otherwise, we have to be careful. If it is a struct that we're in the
209 // process of expanding, then we can't convert the function type. That's ok
210 // though because we must be in a pointer context under the struct, so we can
211 // just convert it to a dummy type.
212 //
213 // We decide this by checking whether ConvertRecordDeclType returns us an
214 // opaque type for a struct that we know is defined.
Chris Lattner71305cc2011-07-15 05:16:14 +0000215 return isSafeToConvert(RT->getDecl(), *this);
Chris Lattnerf742eb02011-07-10 00:18:59 +0000216}
217
218
219/// Code to verify a given function type is complete, i.e. the return type
Stephen Hines651f13c2014-04-23 16:59:28 -0700220/// and all of the parameter types are complete. Also check to see if we are in
Chris Lattnerf742eb02011-07-10 00:18:59 +0000221/// a RS_StructPointer context, and if so whether any struct types have been
222/// pended. If so, we don't want to ask the ABI lowering code to handle a type
223/// that cannot be converted to an IR type.
224bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700225 if (!isFuncParamTypeConvertible(FT->getReturnType()))
Chris Lattnerf742eb02011-07-10 00:18:59 +0000226 return false;
227
228 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
Stephen Hines651f13c2014-04-23 16:59:28 -0700229 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
230 if (!isFuncParamTypeConvertible(FPT->getParamType(i)))
Chris Lattnerf742eb02011-07-10 00:18:59 +0000231 return false;
232
233 return true;
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000234}
235
Chris Lattnerc5b88062008-02-06 05:08:19 +0000236/// UpdateCompletedType - When we find the full definition for a TagDecl,
237/// replace the 'opaque' type we previously made for it if applicable.
238void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000239 // If this is an enum being completed, then we flush all non-struct types from
240 // the cache. This allows function types and other things that may be derived
241 // from the enum to be recomputed.
Chris Lattner8dd5cdf2011-07-09 18:53:56 +0000242 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
243 // Only flush the cache if we've actually already converted this type.
Chris Lattner2045b2d2011-07-13 05:31:19 +0000244 if (TypeCache.count(ED->getTypeForDecl())) {
245 // Okay, we formed some types based on this. We speculated that the enum
246 // would be lowered to i32, so we only need to flush the cache if this
247 // didn't happen.
248 if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
249 TypeCache.clear();
250 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700251 // If necessary, provide the full definition of a type only used with a
252 // declaration so far.
253 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
254 DI->completeType(ED);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000255 return;
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000256 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000257
Chris Lattnerf0a86792011-07-10 03:47:27 +0000258 // If we completed a RecordDecl that we previously used and converted to an
259 // anonymous type, then go ahead and complete it now.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000260 const RecordDecl *RD = cast<RecordDecl>(TD);
Chris Lattnerf0a86792011-07-10 03:47:27 +0000261 if (RD->isDependentType()) return;
262
263 // Only complete it if we converted it already. If we haven't converted it
264 // yet, we'll just do it lazily.
Chris Lattner3ade9752011-07-10 06:03:22 +0000265 if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000266 ConvertRecordDeclType(RD);
David Blaikieeab6a362013-06-21 00:40:50 +0000267
268 // If necessary, provide the full definition of a type only used with a
269 // declaration so far.
270 if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
David Blaikie27804892013-08-15 20:49:17 +0000271 DI->completeType(RD);
Chris Lattnerd86e6bc2008-02-05 08:06:13 +0000272}
273
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000274static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
Joey Gouly19dbb202013-01-23 11:56:20 +0000275 const llvm::fltSemantics &format,
276 bool UseNativeHalf = false) {
277 if (&format == &llvm::APFloat::IEEEhalf) {
278 if (UseNativeHalf)
279 return llvm::Type::getHalfTy(VMContext);
280 else
281 return llvm::Type::getInt16Ty(VMContext);
282 }
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000283 if (&format == &llvm::APFloat::IEEEsingle)
Owen Anderson0032b272009-08-13 21:57:51 +0000284 return llvm::Type::getFloatTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000285 if (&format == &llvm::APFloat::IEEEdouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000286 return llvm::Type::getDoubleTy(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000287 if (&format == &llvm::APFloat::IEEEquad)
Owen Anderson0032b272009-08-13 21:57:51 +0000288 return llvm::Type::getFP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000289 if (&format == &llvm::APFloat::PPCDoubleDouble)
Owen Anderson0032b272009-08-13 21:57:51 +0000290 return llvm::Type::getPPC_FP128Ty(VMContext);
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000291 if (&format == &llvm::APFloat::x87DoubleExtended)
Owen Anderson0032b272009-08-13 21:57:51 +0000292 return llvm::Type::getX86_FP80Ty(VMContext);
David Blaikieb219cfc2011-09-23 05:06:16 +0000293 llvm_unreachable("Unknown float format!");
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000294}
295
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000296/// ConvertType - Convert the specified type to its LLVM form.
297llvm::Type *CodeGenTypes::ConvertType(QualType T) {
298 T = Context.getCanonicalType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000300 const Type *Ty = T.getTypePtr();
301
302 // RecordTypes are cached and processed specially.
303 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
304 return ConvertRecordDeclType(RT->getDecl());
305
306 // See if type is already cached.
307 llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
308 // If type is found in map then use it. Otherwise, convert type T.
309 if (TCI != TypeCache.end())
310 return TCI->second;
311
312 // If we don't have it in the cache, convert it now.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700313 llvm::Type *ResultType = nullptr;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000314 switch (Ty->getTypeClass()) {
315 case Type::Record: // Handled above.
Douglas Gregor72564e72009-02-26 23:50:07 +0000316#define TYPE(Class, Base)
317#define ABSTRACT_TYPE(Class, Base)
318#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
319#define DEPENDENT_TYPE(Class, Base) case Type::Class:
John McCallad5e7382010-03-01 23:49:17 +0000320#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +0000321#include "clang/AST/TypeNodes.def"
John McCall864c0412011-04-26 20:42:42 +0000322 llvm_unreachable("Non-canonical or dependent types aren't possible.");
Douglas Gregor72564e72009-02-26 23:50:07 +0000323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 case Type::Builtin: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000325 switch (cast<BuiltinType>(Ty)->getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 case BuiltinType::Void:
Steve Naroffde2e22d2009-07-15 18:40:39 +0000327 case BuiltinType::ObjCId:
328 case BuiltinType::ObjCClass:
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000329 case BuiltinType::ObjCSel:
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 // LLVM void type can only be used as the result of a function call. Just
331 // map to the same as char.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000332 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
333 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000334
335 case BuiltinType::Bool:
Chris Lattner19009e62008-01-09 18:47:25 +0000336 // Note that we always return bool as i1 for use as a scalar type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000337 ResultType = llvm::Type::getInt1Ty(getLLVMContext());
338 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000340 case BuiltinType::Char_S:
341 case BuiltinType::Char_U:
342 case BuiltinType::SChar:
343 case BuiltinType::UChar:
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 case BuiltinType::Short:
345 case BuiltinType::UShort:
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 case BuiltinType::Int:
347 case BuiltinType::UInt:
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 case BuiltinType::Long:
349 case BuiltinType::ULong:
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 case BuiltinType::LongLong:
351 case BuiltinType::ULongLong:
Chris Lattner3f59c972010-12-25 23:25:43 +0000352 case BuiltinType::WChar_S:
353 case BuiltinType::WChar_U:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000354 case BuiltinType::Char16:
355 case BuiltinType::Char32:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000356 ResultType = llvm::IntegerType::get(getLLVMContext(),
357 static_cast<unsigned>(Context.getTypeSize(T)));
358 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000360 case BuiltinType::Half:
Joey Gouly19dbb202013-01-23 11:56:20 +0000361 // Half FP can either be storage-only (lowered to i16) or native.
Stephen Hines176edba2014-12-01 14:53:08 -0800362 ResultType =
363 getTypeForFormat(getLLVMContext(), Context.getFloatTypeSemantics(T),
364 Context.getLangOpts().NativeHalfType ||
365 Context.getLangOpts().HalfArgsAndReturns);
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000366 break;
Eli Friedmanf6a943e2008-05-27 04:20:05 +0000367 case BuiltinType::Float:
Nate Begemanc8b12272008-04-18 05:41:31 +0000368 case BuiltinType::Double:
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 case BuiltinType::LongDouble:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000370 ResultType = getTypeForFormat(getLLVMContext(),
Joey Gouly19dbb202013-01-23 11:56:20 +0000371 Context.getFloatTypeSemantics(T),
372 /* UseNativeHalf = */ false);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000373 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000375 case BuiltinType::NullPtr:
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000376 // Model std::nullptr_t as i8*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000377 ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
378 break;
Anders Carlssonc1eb14a2009-09-15 04:39:46 +0000379
Chris Lattner2df9ced2009-04-30 02:43:43 +0000380 case BuiltinType::UInt128:
381 case BuiltinType::Int128:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000382 ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
383 break;
Guy Benyeib13621d2012-12-18 14:38:23 +0000384
385 case BuiltinType::OCLImage1d:
386 case BuiltinType::OCLImage1dArray:
387 case BuiltinType::OCLImage1dBuffer:
388 case BuiltinType::OCLImage2d:
389 case BuiltinType::OCLImage2dArray:
390 case BuiltinType::OCLImage3d:
Guy Benyei21f18c42013-02-07 10:55:47 +0000391 case BuiltinType::OCLSampler:
Guy Benyeie6b9d802013-01-20 12:31:11 +0000392 case BuiltinType::OCLEvent:
Guy Benyeib13621d2012-12-18 14:38:23 +0000393 ResultType = CGM.getOpenCLRuntime().convertOpenCLSpecificType(Ty);
394 break;
Eli Friedman8c692352009-12-18 23:28:34 +0000395
Eli Friedman8c692352009-12-18 23:28:34 +0000396 case BuiltinType::Dependent:
John McCall2dde35b2011-10-18 22:28:37 +0000397#define BUILTIN_TYPE(Id, SingletonId)
398#define PLACEHOLDER_TYPE(Id, SingletonId) \
399 case BuiltinType::Id:
400#include "clang/AST/BuiltinTypes.def"
John McCall864c0412011-04-26 20:42:42 +0000401 llvm_unreachable("Unexpected placeholder builtin type!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 }
403 break;
404 }
Richard Smithdc7a4f52013-04-30 13:56:41 +0000405 case Type::Auto:
406 llvm_unreachable("Unexpected undeduced auto type!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 case Type::Complex: {
Jay Foadef6de3d2011-07-11 09:56:20 +0000408 llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000409 ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
410 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000412 case Type::LValueReference:
413 case Type::RValueReference: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000414 const ReferenceType *RTy = cast<ReferenceType>(Ty);
415 QualType ETy = RTy->getPointeeType();
416 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000417 unsigned AS = Context.getTargetAddressSpace(ETy);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000418 ResultType = llvm::PointerType::get(PointeeType, AS);
419 break;
Daniel Dunbar6aeae7f2009-02-26 19:48:14 +0000420 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 case Type::Pointer: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000422 const PointerType *PTy = cast<PointerType>(Ty);
423 QualType ETy = PTy->getPointeeType();
424 llvm::Type *PointeeType = ConvertTypeForMem(ETy);
425 if (PointeeType->isVoidTy())
426 PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000427 unsigned AS = Context.getTargetAddressSpace(ETy);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000428 ResultType = llvm::PointerType::get(PointeeType, AS);
429 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 }
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Steve Narofffb22d962007-08-30 01:06:46 +0000432 case Type::VariableArray: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000433 const VariableArrayType *A = cast<VariableArrayType>(Ty);
434 assert(A->getIndexTypeCVRQualifiers() == 0 &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 "FIXME: We only handle trivial array types so far!");
Eli Friedmanc5773c42008-02-15 18:16:39 +0000436 // VLAs resolve to the innermost element type; this matches
437 // the return of alloca, and there isn't any obviously better choice.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000438 ResultType = ConvertTypeForMem(A->getElementType());
439 break;
Eli Friedmanc5773c42008-02-15 18:16:39 +0000440 }
441 case Type::IncompleteArray: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000442 const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
443 assert(A->getIndexTypeCVRQualifiers() == 0 &&
Eli Friedmanc5773c42008-02-15 18:16:39 +0000444 "FIXME: We only handle trivial array types so far!");
Chris Lattner3a2b6572011-07-12 06:52:18 +0000445 // int X[] -> [0 x int], unless the element type is not sized. If it is
446 // unsized (e.g. an incomplete struct) just use [0 x i8].
447 ResultType = ConvertTypeForMem(A->getElementType());
448 if (!ResultType->isSized()) {
449 SkippedLayout = true;
450 ResultType = llvm::Type::getInt8Ty(getLLVMContext());
451 }
452 ResultType = llvm::ArrayType::get(ResultType, 0);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000453 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 }
Steve Narofffb22d962007-08-30 01:06:46 +0000455 case Type::ConstantArray: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000456 const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000457 llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
Chris Lattner01c5d1d2011-07-22 06:27:26 +0000458
459 // Lower arrays of undefined struct type to arrays of i8 just to have a
460 // concrete type.
461 if (!EltTy->isSized()) {
462 SkippedLayout = true;
463 EltTy = llvm::Type::getInt8Ty(getLLVMContext());
464 }
465
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000466 ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
467 break;
Steve Narofffb22d962007-08-30 01:06:46 +0000468 }
Nate Begeman213541a2008-04-18 23:10:10 +0000469 case Type::ExtVector:
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 case Type::Vector: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000471 const VectorType *VT = cast<VectorType>(Ty);
472 ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
473 VT->getNumElements());
474 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 }
476 case Type::FunctionNoProto:
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000477 case Type::FunctionProto: {
Chris Lattner71305cc2011-07-15 05:16:14 +0000478 const FunctionType *FT = cast<FunctionType>(Ty);
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000479 // First, check whether we can build the full function type. If the
480 // function type depends on an incomplete type (e.g. a struct or enum), we
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000481 // cannot lower the function type.
Chris Lattner71305cc2011-07-15 05:16:14 +0000482 if (!isFuncTypeConvertible(FT)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000483 // This function's type depends on an incomplete tag type.
Eli Friedman84fd6df2012-11-15 23:40:48 +0000484
485 // Force conversion of all the relevant record types, to make sure
486 // we re-convert the FunctionType when appropriate.
Stephen Hines651f13c2014-04-23 16:59:28 -0700487 if (const RecordType *RT = FT->getReturnType()->getAs<RecordType>())
Eli Friedman84fd6df2012-11-15 23:40:48 +0000488 ConvertRecordDeclType(RT->getDecl());
489 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
Stephen Hines651f13c2014-04-23 16:59:28 -0700490 for (unsigned i = 0, e = FPT->getNumParams(); i != e; i++)
491 if (const RecordType *RT = FPT->getParamType(i)->getAs<RecordType>())
Eli Friedman84fd6df2012-11-15 23:40:48 +0000492 ConvertRecordDeclType(RT->getDecl());
493
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 // Return a placeholder type.
495 ResultType = llvm::StructType::get(getLLVMContext());
Eli Friedman84fd6df2012-11-15 23:40:48 +0000496
Chris Lattner71305cc2011-07-15 05:16:14 +0000497 SkippedLayout = true;
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 break;
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000499 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000500
Stephen Hines651f13c2014-04-23 16:59:28 -0700501 // While we're converting the parameter types for a function, we don't want
Chris Lattnerf0a86792011-07-10 03:47:27 +0000502 // to recursively convert any pointed-to structs. Converting directly-used
503 // structs is ok though.
Stephen Hines176edba2014-12-01 14:53:08 -0800504 if (!RecordsBeingLaidOut.insert(Ty).second) {
Chris Lattner71305cc2011-07-15 05:16:14 +0000505 ResultType = llvm::StructType::get(getLLVMContext());
506
507 SkippedLayout = true;
508 break;
509 }
Chris Lattnerf0a86792011-07-10 03:47:27 +0000510
Eli Friedmanb3b6b9b2009-03-05 03:16:41 +0000511 // The function type can be built; call the appropriate routines to
512 // build it.
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000513 const CGFunctionInfo *FI;
Chris Lattner71305cc2011-07-15 05:16:14 +0000514 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
John McCall0f3d0972012-07-07 06:41:13 +0000515 FI = &arrangeFreeFunctionType(
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000516 CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000517 } else {
Chris Lattner71305cc2011-07-15 05:16:14 +0000518 const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
John McCall0f3d0972012-07-07 06:41:13 +0000519 FI = &arrangeFreeFunctionType(
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000520 CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
Chris Lattnerbcaedae2010-06-30 19:14:05 +0000521 }
Chris Lattnerf0a86792011-07-10 03:47:27 +0000522
Chris Lattner71305cc2011-07-15 05:16:14 +0000523 // If there is something higher level prodding our CGFunctionInfo, then
524 // don't recurse into it again.
525 if (FunctionsBeingProcessed.count(FI)) {
526
527 ResultType = llvm::StructType::get(getLLVMContext());
528 SkippedLayout = true;
529 } else {
530
531 // Otherwise, we're good to go, go ahead and convert it.
John McCallde5d3c72012-02-17 03:33:10 +0000532 ResultType = GetFunctionType(*FI);
Chris Lattner71305cc2011-07-15 05:16:14 +0000533 }
534
535 RecordsBeingLaidOut.erase(Ty);
Chris Lattner57eb23f2011-07-10 05:39:13 +0000536
537 if (SkippedLayout)
538 TypeCache.clear();
Chris Lattnerf0a86792011-07-10 03:47:27 +0000539
Chris Lattner71305cc2011-07-15 05:16:14 +0000540 if (RecordsBeingLaidOut.empty())
Chris Lattnerf0a86792011-07-10 03:47:27 +0000541 while (!DeferredRecords.empty())
542 ConvertRecordDeclType(DeferredRecords.pop_back_val());
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000543 break;
Daniel Dunbarbb36d332009-02-02 21:43:58 +0000544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
John McCallc12c5bb2010-05-15 11:32:37 +0000546 case Type::ObjCObject:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000547 ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
548 break;
John McCallc12c5bb2010-05-15 11:32:37 +0000549
Chris Lattner391d77a2008-03-30 23:03:07 +0000550 case Type::ObjCInterface: {
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000551 // Objective-C interfaces are always opaque (outside of the
552 // runtime, which can do whatever it likes); we never refine
553 // these.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000554 llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
Daniel Dunbar412f59b2009-04-22 10:28:39 +0000555 if (!T)
Chris Lattnerc1c20112011-08-12 17:43:31 +0000556 T = llvm::StructType::create(getLLVMContext());
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000557 ResultType = T;
558 break;
Chris Lattner391d77a2008-03-30 23:03:07 +0000559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Steve Naroff14108da2009-07-10 23:34:53 +0000561 case Type::ObjCObjectPointer: {
Daniel Dunbar28e47802009-07-11 21:12:14 +0000562 // Protocol qualifications do not influence the LLVM type, we just return a
563 // pointer to the underlying interface type. We don't need to worry about
564 // recursive conversion.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000565 llvm::Type *T =
Chris Lattner181eeee2011-07-20 06:23:59 +0000566 ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000567 ResultType = T->getPointerTo();
568 break;
Steve Naroff14108da2009-07-10 23:34:53 +0000569 }
Daniel Dunbar28e47802009-07-11 21:12:14 +0000570
Chris Lattner2045b2d2011-07-13 05:31:19 +0000571 case Type::Enum: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000572 const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
John McCall5e1cdac2011-10-07 06:10:15 +0000573 if (ED->isCompleteDefinition() || ED->isFixed())
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000574 return ConvertType(ED->getIntegerType());
Chris Lattner2045b2d2011-07-13 05:31:19 +0000575 // Return a placeholder 'i32' type. This can be changed later when the
576 // type is defined (see UpdateCompletedType), but is likely to be the
577 // "right" answer.
578 ResultType = llvm::Type::getInt32Ty(getLLVMContext());
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000579 break;
Chris Lattnerde0efb32008-02-06 05:48:29 +0000580 }
Daniel Dunbar90488912008-08-28 18:02:04 +0000581
582 case Type::BlockPointer: {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000583 const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
584 llvm::Type *PointeeType = ConvertTypeForMem(FTy);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000585 unsigned AS = Context.getTargetAddressSpace(FTy);
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000586 ResultType = llvm::PointerType::get(PointeeType, AS);
587 break;
Daniel Dunbar90488912008-08-28 18:02:04 +0000588 }
Sebastian Redl424c51d2009-01-25 13:35:30 +0000589
Anders Carlsson0e650012009-05-17 17:41:20 +0000590 case Type::MemberPointer: {
Stephen Hines176edba2014-12-01 14:53:08 -0800591 if (!getCXXABI().isMemberPointerConvertible(cast<MemberPointerType>(Ty)))
592 return llvm::StructType::create(getLLVMContext());
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000593 ResultType =
594 getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
595 break;
Anders Carlsson0e650012009-05-17 17:41:20 +0000596 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000597
598 case Type::Atomic: {
John McCall9eda3ab2013-03-07 21:37:17 +0000599 QualType valueType = cast<AtomicType>(Ty)->getValueType();
600 ResultType = ConvertTypeForMem(valueType);
601
602 // Pad out to the inflated size if necessary.
603 uint64_t valueSize = Context.getTypeSize(valueType);
604 uint64_t atomicSize = Context.getTypeSize(Ty);
605 if (valueSize != atomicSize) {
606 assert(valueSize < atomicSize);
607 llvm::Type *elts[] = {
608 ResultType,
609 llvm::ArrayType::get(CGM.Int8Ty, (atomicSize - valueSize) / 8)
610 };
611 ResultType = llvm::StructType::get(getLLVMContext(),
612 llvm::makeArrayRef(elts));
613 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000614 break;
615 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000617
618 assert(ResultType && "Didn't convert a type?");
619
620 TypeCache[Ty] = ResultType;
621 return ResultType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000622}
623
John McCall9eda3ab2013-03-07 21:37:17 +0000624bool CodeGenModule::isPaddedAtomicType(QualType type) {
625 return isPaddedAtomicType(type->castAs<AtomicType>());
626}
627
628bool CodeGenModule::isPaddedAtomicType(const AtomicType *type) {
629 return Context.getTypeSize(type) != Context.getTypeSize(type->getValueType());
630}
631
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000632/// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
633llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
Daniel Dunbarefb6d0d2008-09-06 02:26:43 +0000634 // TagDecl's are not necessarily unique, instead use the (clang)
635 // type connected to the decl.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000636 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000638 llvm::StructType *&Entry = RecordDeclTypes[Key];
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000640 // If we don't have a StructType at all yet, create the forward declaration.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700641 if (!Entry) {
Chris Lattnerc1c20112011-08-12 17:43:31 +0000642 Entry = llvm::StructType::create(getLLVMContext());
Chris Lattnercd87d1e2011-07-12 05:53:08 +0000643 addRecordTypeName(RD, Entry, "");
644 }
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000645 llvm::StructType *Ty = Entry;
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000646
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000647 // If this is still a forward declaration, or the LLVM type is already
648 // complete, there's nothing more to do.
Chris Lattner71305cc2011-07-15 05:16:14 +0000649 RD = RD->getDefinition();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700650 if (!RD || !RD->isCompleteDefinition() || !Ty->isOpaque())
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000651 return Ty;
652
Chris Lattner71305cc2011-07-15 05:16:14 +0000653 // If converting this type would cause us to infinitely loop, don't do it!
654 if (!isSafeToConvert(RD, *this)) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000655 DeferredRecords.push_back(RD);
656 return Ty;
Chris Lattner5de00fc2008-02-06 06:03:51 +0000657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Chris Lattner5de00fc2008-02-06 06:03:51 +0000659 // Okay, this is a definition of a type. Compile the implementation now.
Stephen Hines176edba2014-12-01 14:53:08 -0800660 bool InsertResult = RecordsBeingLaidOut.insert(Key).second;
661 (void)InsertResult;
Chris Lattner71305cc2011-07-15 05:16:14 +0000662 assert(InsertResult && "Recursively compiling a struct?");
663
John McCall86ff3082010-02-04 22:26:26 +0000664 // Force conversion of non-virtual base classes recursively.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000665 if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700666 for (const auto &I : CRD->bases()) {
667 if (I.isVirtual()) continue;
Chris Lattner71305cc2011-07-15 05:16:14 +0000668
Stephen Hines651f13c2014-04-23 16:59:28 -0700669 ConvertRecordDeclType(I.getType()->getAs<RecordType>()->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000670 }
671 }
672
Anders Carlsson696798f2009-07-27 17:10:54 +0000673 // Layout fields.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000674 CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
Anders Carlsson696798f2009-07-27 17:10:54 +0000675 CGRecordLayouts[Key] = Layout;
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Chris Lattner71305cc2011-07-15 05:16:14 +0000677 // We're done laying out this struct.
678 bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
679 assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
680
Chris Lattnerf0a86792011-07-10 03:47:27 +0000681 // If this struct blocked a FunctionType conversion, then recompute whatever
682 // was derived from that.
683 // FIXME: This is hugely overconservative.
Chris Lattner57eb23f2011-07-10 05:39:13 +0000684 if (SkippedLayout)
685 TypeCache.clear();
Chris Lattner71305cc2011-07-15 05:16:14 +0000686
687 // If we're done converting the outer-most record, then convert any deferred
688 // structs as well.
689 if (RecordsBeingLaidOut.empty())
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000690 while (!DeferredRecords.empty())
691 ConvertRecordDeclType(DeferredRecords.pop_back_val());
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000693 return Ty;
Mike Stump1eb44332009-09-09 15:08:12 +0000694}
Chris Lattnerfc3b8e92008-02-06 05:18:32 +0000695
Anders Carlsson2d987772010-11-24 20:22:04 +0000696/// getCGRecordLayout - Return record layout info for the given record decl.
Anders Carlssonad3e7112009-08-24 17:16:23 +0000697const CGRecordLayout &
Anders Carlsson2d987772010-11-24 20:22:04 +0000698CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
699 const Type *Key = Context.getTagDeclType(RD).getTypePtr();
Anders Carlsson82926962010-11-24 19:52:29 +0000700
Daniel Dunbar270e2032010-03-31 00:11:27 +0000701 const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000702 if (!Layout) {
Anders Carlsson2d987772010-11-24 20:22:04 +0000703 // Compute the type information.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000704 ConvertRecordDeclType(RD);
Anders Carlssonc8f01eb2010-11-24 19:51:04 +0000705
706 // Now try again.
707 Layout = CGRecordLayouts.lookup(Key);
708 }
709
Daniel Dunbar270e2032010-03-31 00:11:27 +0000710 assert(Layout && "Unable to find record layout information for type");
711 return *Layout;
Devang Patelb84a06e2007-10-23 02:10:49 +0000712}
Anders Carlsson3e5af902010-05-14 19:41:56 +0000713
John McCallf16aa102010-08-22 21:01:12 +0000714bool CodeGenTypes::isZeroInitializable(QualType T) {
Anders Carlsson3e5af902010-05-14 19:41:56 +0000715 // No need to check for member pointers when not compiling C++.
David Blaikie4e4d0842012-03-11 07:00:24 +0000716 if (!Context.getLangOpts().CPlusPlus)
John McCallf16aa102010-08-22 21:01:12 +0000717 return true;
Anders Carlsson3e5af902010-05-14 19:41:56 +0000718
719 T = Context.getBaseElementType(T);
720
John McCallf16aa102010-08-22 21:01:12 +0000721 // Records are non-zero-initializable if they contain any
722 // non-zero-initializable subobjects.
Anders Carlsson3e5af902010-05-14 19:41:56 +0000723 if (const RecordType *RT = T->getAs<RecordType>()) {
724 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
John McCallf16aa102010-08-22 21:01:12 +0000725 return isZeroInitializable(RD);
Anders Carlsson3e5af902010-05-14 19:41:56 +0000726 }
John McCallf16aa102010-08-22 21:01:12 +0000727
728 // We have to ask the ABI about member pointers.
Anders Carlsson3e5af902010-05-14 19:41:56 +0000729 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
John McCallf16aa102010-08-22 21:01:12 +0000730 return getCXXABI().isZeroInitializable(MPT);
Anders Carlsson3e5af902010-05-14 19:41:56 +0000731
John McCallf16aa102010-08-22 21:01:12 +0000732 // Everything else is okay.
733 return true;
Anders Carlsson3e5af902010-05-14 19:41:56 +0000734}
Anders Carlssonc39211d2010-05-18 03:47:15 +0000735
John McCallf16aa102010-08-22 21:01:12 +0000736bool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
Anders Carlsson3379e9b2010-11-24 19:57:04 +0000737 return getCGRecordLayout(RD).isZeroInitializable();
Anders Carlssonc39211d2010-05-18 03:47:15 +0000738}