CodeGenTypes.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. //===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This is the code that handles AST -> LLVM type lowering.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenTypes.h"
  14. #include "CGCXXABI.h"
  15. #include "CGCall.h"
  16. #include "CGRecordLayout.h"
  17. #include "TargetInfo.h"
  18. #include "clang/AST/ASTContext.h"
  19. #include "clang/AST/DeclCXX.h"
  20. #include "clang/AST/DeclObjC.h"
  21. #include "clang/AST/Expr.h"
  22. #include "clang/AST/RecordLayout.h"
  23. #include "llvm/DataLayout.h"
  24. #include "llvm/DerivedTypes.h"
  25. #include "llvm/Module.h"
  26. using namespace clang;
  27. using namespace CodeGen;
  28. CodeGenTypes::CodeGenTypes(CodeGenModule &CGM)
  29. : Context(CGM.getContext()), Target(Context.getTargetInfo()),
  30. TheModule(CGM.getModule()), TheDataLayout(CGM.getDataLayout()),
  31. TheABIInfo(CGM.getTargetCodeGenInfo().getABIInfo()),
  32. TheCXXABI(CGM.getCXXABI()),
  33. CodeGenOpts(CGM.getCodeGenOpts()), CGM(CGM) {
  34. SkippedLayout = false;
  35. }
  36. CodeGenTypes::~CodeGenTypes() {
  37. for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
  38. I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
  39. I != E; ++I)
  40. delete I->second;
  41. for (llvm::FoldingSet<CGFunctionInfo>::iterator
  42. I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
  43. delete &*I++;
  44. }
  45. void CodeGenTypes::addRecordTypeName(const RecordDecl *RD,
  46. llvm::StructType *Ty,
  47. StringRef suffix) {
  48. SmallString<256> TypeName;
  49. llvm::raw_svector_ostream OS(TypeName);
  50. OS << RD->getKindName() << '.';
  51. // Name the codegen type after the typedef name
  52. // if there is no tag type name available
  53. if (RD->getIdentifier()) {
  54. // FIXME: We should not have to check for a null decl context here.
  55. // Right now we do it because the implicit Obj-C decls don't have one.
  56. if (RD->getDeclContext())
  57. OS << RD->getQualifiedNameAsString();
  58. else
  59. RD->printName(OS);
  60. } else if (const TypedefNameDecl *TDD = RD->getTypedefNameForAnonDecl()) {
  61. // FIXME: We should not have to check for a null decl context here.
  62. // Right now we do it because the implicit Obj-C decls don't have one.
  63. if (TDD->getDeclContext())
  64. OS << TDD->getQualifiedNameAsString();
  65. else
  66. TDD->printName(OS);
  67. } else
  68. OS << "anon";
  69. if (!suffix.empty())
  70. OS << suffix;
  71. Ty->setName(OS.str());
  72. }
  73. /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
  74. /// ConvertType in that it is used to convert to the memory representation for
  75. /// a type. For example, the scalar representation for _Bool is i1, but the
  76. /// memory representation is usually i8 or i32, depending on the target.
  77. llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T){
  78. llvm::Type *R = ConvertType(T);
  79. // If this is a non-bool type, don't map it.
  80. if (!R->isIntegerTy(1))
  81. return R;
  82. // Otherwise, return an integer of the target-specified size.
  83. return llvm::IntegerType::get(getLLVMContext(),
  84. (unsigned)Context.getTypeSize(T));
  85. }
  86. /// isRecordLayoutComplete - Return true if the specified type is already
  87. /// completely laid out.
  88. bool CodeGenTypes::isRecordLayoutComplete(const Type *Ty) const {
  89. llvm::DenseMap<const Type*, llvm::StructType *>::const_iterator I =
  90. RecordDeclTypes.find(Ty);
  91. return I != RecordDeclTypes.end() && !I->second->isOpaque();
  92. }
  93. static bool
  94. isSafeToConvert(QualType T, CodeGenTypes &CGT,
  95. llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked);
  96. /// isSafeToConvert - Return true if it is safe to convert the specified record
  97. /// decl to IR and lay it out, false if doing so would cause us to get into a
  98. /// recursive compilation mess.
  99. static bool
  100. isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT,
  101. llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
  102. // If we have already checked this type (maybe the same type is used by-value
  103. // multiple times in multiple structure fields, don't check again.
  104. if (!AlreadyChecked.insert(RD)) return true;
  105. const Type *Key = CGT.getContext().getTagDeclType(RD).getTypePtr();
  106. // If this type is already laid out, converting it is a noop.
  107. if (CGT.isRecordLayoutComplete(Key)) return true;
  108. // If this type is currently being laid out, we can't recursively compile it.
  109. if (CGT.isRecordBeingLaidOut(Key))
  110. return false;
  111. // If this type would require laying out bases that are currently being laid
  112. // out, don't do it. This includes virtual base classes which get laid out
  113. // when a class is translated, even though they aren't embedded by-value into
  114. // the class.
  115. if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
  116. for (CXXRecordDecl::base_class_const_iterator I = CRD->bases_begin(),
  117. E = CRD->bases_end(); I != E; ++I)
  118. if (!isSafeToConvert(I->getType()->getAs<RecordType>()->getDecl(),
  119. CGT, AlreadyChecked))
  120. return false;
  121. }
  122. // If this type would require laying out members that are currently being laid
  123. // out, don't do it.
  124. for (RecordDecl::field_iterator I = RD->field_begin(),
  125. E = RD->field_end(); I != E; ++I)
  126. if (!isSafeToConvert(I->getType(), CGT, AlreadyChecked))
  127. return false;
  128. // If there are no problems, lets do it.
  129. return true;
  130. }
  131. /// isSafeToConvert - Return true if it is safe to convert this field type,
  132. /// which requires the structure elements contained by-value to all be
  133. /// recursively safe to convert.
  134. static bool
  135. isSafeToConvert(QualType T, CodeGenTypes &CGT,
  136. llvm::SmallPtrSet<const RecordDecl*, 16> &AlreadyChecked) {
  137. T = T.getCanonicalType();
  138. // If this is a record, check it.
  139. if (const RecordType *RT = dyn_cast<RecordType>(T))
  140. return isSafeToConvert(RT->getDecl(), CGT, AlreadyChecked);
  141. // If this is an array, check the elements, which are embedded inline.
  142. if (const ArrayType *AT = dyn_cast<ArrayType>(T))
  143. return isSafeToConvert(AT->getElementType(), CGT, AlreadyChecked);
  144. // Otherwise, there is no concern about transforming this. We only care about
  145. // things that are contained by-value in a structure that can have another
  146. // structure as a member.
  147. return true;
  148. }
  149. /// isSafeToConvert - Return true if it is safe to convert the specified record
  150. /// decl to IR and lay it out, false if doing so would cause us to get into a
  151. /// recursive compilation mess.
  152. static bool isSafeToConvert(const RecordDecl *RD, CodeGenTypes &CGT) {
  153. // If no structs are being laid out, we can certainly do this one.
  154. if (CGT.noRecordsBeingLaidOut()) return true;
  155. llvm::SmallPtrSet<const RecordDecl*, 16> AlreadyChecked;
  156. return isSafeToConvert(RD, CGT, AlreadyChecked);
  157. }
  158. /// isFuncTypeArgumentConvertible - Return true if the specified type in a
  159. /// function argument or result position can be converted to an IR type at this
  160. /// point. This boils down to being whether it is complete, as well as whether
  161. /// we've temporarily deferred expanding the type because we're in a recursive
  162. /// context.
  163. bool CodeGenTypes::isFuncTypeArgumentConvertible(QualType Ty) {
  164. // If this isn't a tagged type, we can convert it!
  165. const TagType *TT = Ty->getAs<TagType>();
  166. if (TT == 0) return true;
  167. // Incomplete types cannot be converted.
  168. if (TT->isIncompleteType())
  169. return false;
  170. // If this is an enum, then it is always safe to convert.
  171. const RecordType *RT = dyn_cast<RecordType>(TT);
  172. if (RT == 0) return true;
  173. // Otherwise, we have to be careful. If it is a struct that we're in the
  174. // process of expanding, then we can't convert the function type. That's ok
  175. // though because we must be in a pointer context under the struct, so we can
  176. // just convert it to a dummy type.
  177. //
  178. // We decide this by checking whether ConvertRecordDeclType returns us an
  179. // opaque type for a struct that we know is defined.
  180. return isSafeToConvert(RT->getDecl(), *this);
  181. }
  182. /// Code to verify a given function type is complete, i.e. the return type
  183. /// and all of the argument types are complete. Also check to see if we are in
  184. /// a RS_StructPointer context, and if so whether any struct types have been
  185. /// pended. If so, we don't want to ask the ABI lowering code to handle a type
  186. /// that cannot be converted to an IR type.
  187. bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
  188. if (!isFuncTypeArgumentConvertible(FT->getResultType()))
  189. return false;
  190. if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
  191. for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
  192. if (!isFuncTypeArgumentConvertible(FPT->getArgType(i)))
  193. return false;
  194. return true;
  195. }
  196. /// UpdateCompletedType - When we find the full definition for a TagDecl,
  197. /// replace the 'opaque' type we previously made for it if applicable.
  198. void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
  199. // If this is an enum being completed, then we flush all non-struct types from
  200. // the cache. This allows function types and other things that may be derived
  201. // from the enum to be recomputed.
  202. if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
  203. // Only flush the cache if we've actually already converted this type.
  204. if (TypeCache.count(ED->getTypeForDecl())) {
  205. // Okay, we formed some types based on this. We speculated that the enum
  206. // would be lowered to i32, so we only need to flush the cache if this
  207. // didn't happen.
  208. if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
  209. TypeCache.clear();
  210. }
  211. return;
  212. }
  213. // If we completed a RecordDecl that we previously used and converted to an
  214. // anonymous type, then go ahead and complete it now.
  215. const RecordDecl *RD = cast<RecordDecl>(TD);
  216. if (RD->isDependentType()) return;
  217. // Only complete it if we converted it already. If we haven't converted it
  218. // yet, we'll just do it lazily.
  219. if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
  220. ConvertRecordDeclType(RD);
  221. }
  222. static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
  223. const llvm::fltSemantics &format) {
  224. if (&format == &llvm::APFloat::IEEEhalf)
  225. return llvm::Type::getInt16Ty(VMContext);
  226. if (&format == &llvm::APFloat::IEEEsingle)
  227. return llvm::Type::getFloatTy(VMContext);
  228. if (&format == &llvm::APFloat::IEEEdouble)
  229. return llvm::Type::getDoubleTy(VMContext);
  230. if (&format == &llvm::APFloat::IEEEquad)
  231. return llvm::Type::getFP128Ty(VMContext);
  232. if (&format == &llvm::APFloat::PPCDoubleDouble)
  233. return llvm::Type::getPPC_FP128Ty(VMContext);
  234. if (&format == &llvm::APFloat::x87DoubleExtended)
  235. return llvm::Type::getX86_FP80Ty(VMContext);
  236. llvm_unreachable("Unknown float format!");
  237. }
  238. /// ConvertType - Convert the specified type to its LLVM form.
  239. llvm::Type *CodeGenTypes::ConvertType(QualType T) {
  240. T = Context.getCanonicalType(T);
  241. const Type *Ty = T.getTypePtr();
  242. // RecordTypes are cached and processed specially.
  243. if (const RecordType *RT = dyn_cast<RecordType>(Ty))
  244. return ConvertRecordDeclType(RT->getDecl());
  245. // See if type is already cached.
  246. llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
  247. // If type is found in map then use it. Otherwise, convert type T.
  248. if (TCI != TypeCache.end())
  249. return TCI->second;
  250. // If we don't have it in the cache, convert it now.
  251. llvm::Type *ResultType = 0;
  252. switch (Ty->getTypeClass()) {
  253. case Type::Record: // Handled above.
  254. #define TYPE(Class, Base)
  255. #define ABSTRACT_TYPE(Class, Base)
  256. #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
  257. #define DEPENDENT_TYPE(Class, Base) case Type::Class:
  258. #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
  259. #include "clang/AST/TypeNodes.def"
  260. llvm_unreachable("Non-canonical or dependent types aren't possible.");
  261. case Type::Builtin: {
  262. switch (cast<BuiltinType>(Ty)->getKind()) {
  263. case BuiltinType::Void:
  264. case BuiltinType::ObjCId:
  265. case BuiltinType::ObjCClass:
  266. case BuiltinType::ObjCSel:
  267. // LLVM void type can only be used as the result of a function call. Just
  268. // map to the same as char.
  269. ResultType = llvm::Type::getInt8Ty(getLLVMContext());
  270. break;
  271. case BuiltinType::Bool:
  272. // Note that we always return bool as i1 for use as a scalar type.
  273. ResultType = llvm::Type::getInt1Ty(getLLVMContext());
  274. break;
  275. case BuiltinType::Char_S:
  276. case BuiltinType::Char_U:
  277. case BuiltinType::SChar:
  278. case BuiltinType::UChar:
  279. case BuiltinType::Short:
  280. case BuiltinType::UShort:
  281. case BuiltinType::Int:
  282. case BuiltinType::UInt:
  283. case BuiltinType::Long:
  284. case BuiltinType::ULong:
  285. case BuiltinType::LongLong:
  286. case BuiltinType::ULongLong:
  287. case BuiltinType::WChar_S:
  288. case BuiltinType::WChar_U:
  289. case BuiltinType::Char16:
  290. case BuiltinType::Char32:
  291. ResultType = llvm::IntegerType::get(getLLVMContext(),
  292. static_cast<unsigned>(Context.getTypeSize(T)));
  293. break;
  294. case BuiltinType::Half:
  295. // Half is special: it might be lowered to i16 (and will be storage-only
  296. // type),. or can be represented as a set of native operations.
  297. // FIXME: Ask target which kind of half FP it prefers (storage only vs
  298. // native).
  299. ResultType = llvm::Type::getInt16Ty(getLLVMContext());
  300. break;
  301. case BuiltinType::Float:
  302. case BuiltinType::Double:
  303. case BuiltinType::LongDouble:
  304. ResultType = getTypeForFormat(getLLVMContext(),
  305. Context.getFloatTypeSemantics(T));
  306. break;
  307. case BuiltinType::NullPtr:
  308. // Model std::nullptr_t as i8*
  309. ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
  310. break;
  311. case BuiltinType::UInt128:
  312. case BuiltinType::Int128:
  313. ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
  314. break;
  315. case BuiltinType::Dependent:
  316. #define BUILTIN_TYPE(Id, SingletonId)
  317. #define PLACEHOLDER_TYPE(Id, SingletonId) \
  318. case BuiltinType::Id:
  319. #include "clang/AST/BuiltinTypes.def"
  320. llvm_unreachable("Unexpected placeholder builtin type!");
  321. }
  322. break;
  323. }
  324. case Type::Complex: {
  325. llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
  326. ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
  327. break;
  328. }
  329. case Type::LValueReference:
  330. case Type::RValueReference: {
  331. const ReferenceType *RTy = cast<ReferenceType>(Ty);
  332. QualType ETy = RTy->getPointeeType();
  333. llvm::Type *PointeeType = ConvertTypeForMem(ETy);
  334. unsigned AS = Context.getTargetAddressSpace(ETy);
  335. ResultType = llvm::PointerType::get(PointeeType, AS);
  336. break;
  337. }
  338. case Type::Pointer: {
  339. const PointerType *PTy = cast<PointerType>(Ty);
  340. QualType ETy = PTy->getPointeeType();
  341. llvm::Type *PointeeType = ConvertTypeForMem(ETy);
  342. if (PointeeType->isVoidTy())
  343. PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
  344. unsigned AS = Context.getTargetAddressSpace(ETy);
  345. ResultType = llvm::PointerType::get(PointeeType, AS);
  346. break;
  347. }
  348. case Type::VariableArray: {
  349. const VariableArrayType *A = cast<VariableArrayType>(Ty);
  350. assert(A->getIndexTypeCVRQualifiers() == 0 &&
  351. "FIXME: We only handle trivial array types so far!");
  352. // VLAs resolve to the innermost element type; this matches
  353. // the return of alloca, and there isn't any obviously better choice.
  354. ResultType = ConvertTypeForMem(A->getElementType());
  355. break;
  356. }
  357. case Type::IncompleteArray: {
  358. const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
  359. assert(A->getIndexTypeCVRQualifiers() == 0 &&
  360. "FIXME: We only handle trivial array types so far!");
  361. // int X[] -> [0 x int], unless the element type is not sized. If it is
  362. // unsized (e.g. an incomplete struct) just use [0 x i8].
  363. ResultType = ConvertTypeForMem(A->getElementType());
  364. if (!ResultType->isSized()) {
  365. SkippedLayout = true;
  366. ResultType = llvm::Type::getInt8Ty(getLLVMContext());
  367. }
  368. ResultType = llvm::ArrayType::get(ResultType, 0);
  369. break;
  370. }
  371. case Type::ConstantArray: {
  372. const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
  373. llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
  374. // Lower arrays of undefined struct type to arrays of i8 just to have a
  375. // concrete type.
  376. if (!EltTy->isSized()) {
  377. SkippedLayout = true;
  378. EltTy = llvm::Type::getInt8Ty(getLLVMContext());
  379. }
  380. ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
  381. break;
  382. }
  383. case Type::ExtVector:
  384. case Type::Vector: {
  385. const VectorType *VT = cast<VectorType>(Ty);
  386. ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
  387. VT->getNumElements());
  388. break;
  389. }
  390. case Type::FunctionNoProto:
  391. case Type::FunctionProto: {
  392. const FunctionType *FT = cast<FunctionType>(Ty);
  393. // First, check whether we can build the full function type. If the
  394. // function type depends on an incomplete type (e.g. a struct or enum), we
  395. // cannot lower the function type.
  396. if (!isFuncTypeConvertible(FT)) {
  397. // This function's type depends on an incomplete tag type.
  398. // Force conversion of all the relevant record types, to make sure
  399. // we re-convert the FunctionType when appropriate.
  400. if (const RecordType *RT = FT->getResultType()->getAs<RecordType>())
  401. ConvertRecordDeclType(RT->getDecl());
  402. if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
  403. for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
  404. if (const RecordType *RT = FPT->getArgType(i)->getAs<RecordType>())
  405. ConvertRecordDeclType(RT->getDecl());
  406. // Return a placeholder type.
  407. ResultType = llvm::StructType::get(getLLVMContext());
  408. SkippedLayout = true;
  409. break;
  410. }
  411. // While we're converting the argument types for a function, we don't want
  412. // to recursively convert any pointed-to structs. Converting directly-used
  413. // structs is ok though.
  414. if (!RecordsBeingLaidOut.insert(Ty)) {
  415. ResultType = llvm::StructType::get(getLLVMContext());
  416. SkippedLayout = true;
  417. break;
  418. }
  419. // The function type can be built; call the appropriate routines to
  420. // build it.
  421. const CGFunctionInfo *FI;
  422. if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
  423. FI = &arrangeFreeFunctionType(
  424. CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
  425. } else {
  426. const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
  427. FI = &arrangeFreeFunctionType(
  428. CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
  429. }
  430. // If there is something higher level prodding our CGFunctionInfo, then
  431. // don't recurse into it again.
  432. if (FunctionsBeingProcessed.count(FI)) {
  433. ResultType = llvm::StructType::get(getLLVMContext());
  434. SkippedLayout = true;
  435. } else {
  436. // Otherwise, we're good to go, go ahead and convert it.
  437. ResultType = GetFunctionType(*FI);
  438. }
  439. RecordsBeingLaidOut.erase(Ty);
  440. if (SkippedLayout)
  441. TypeCache.clear();
  442. if (RecordsBeingLaidOut.empty())
  443. while (!DeferredRecords.empty())
  444. ConvertRecordDeclType(DeferredRecords.pop_back_val());
  445. break;
  446. }
  447. case Type::ObjCObject:
  448. ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
  449. break;
  450. case Type::ObjCInterface: {
  451. // Objective-C interfaces are always opaque (outside of the
  452. // runtime, which can do whatever it likes); we never refine
  453. // these.
  454. llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
  455. if (!T)
  456. T = llvm::StructType::create(getLLVMContext());
  457. ResultType = T;
  458. break;
  459. }
  460. case Type::ObjCObjectPointer: {
  461. // Protocol qualifications do not influence the LLVM type, we just return a
  462. // pointer to the underlying interface type. We don't need to worry about
  463. // recursive conversion.
  464. llvm::Type *T =
  465. ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
  466. ResultType = T->getPointerTo();
  467. break;
  468. }
  469. case Type::Enum: {
  470. const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
  471. if (ED->isCompleteDefinition() || ED->isFixed())
  472. return ConvertType(ED->getIntegerType());
  473. // Return a placeholder 'i32' type. This can be changed later when the
  474. // type is defined (see UpdateCompletedType), but is likely to be the
  475. // "right" answer.
  476. ResultType = llvm::Type::getInt32Ty(getLLVMContext());
  477. break;
  478. }
  479. case Type::BlockPointer: {
  480. const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
  481. llvm::Type *PointeeType = ConvertTypeForMem(FTy);
  482. unsigned AS = Context.getTargetAddressSpace(FTy);
  483. ResultType = llvm::PointerType::get(PointeeType, AS);
  484. break;
  485. }
  486. case Type::MemberPointer: {
  487. ResultType =
  488. getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
  489. break;
  490. }
  491. case Type::Atomic: {
  492. ResultType = ConvertType(cast<AtomicType>(Ty)->getValueType());
  493. break;
  494. }
  495. }
  496. assert(ResultType && "Didn't convert a type?");
  497. TypeCache[Ty] = ResultType;
  498. return ResultType;
  499. }
  500. /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
  501. llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
  502. // TagDecl's are not necessarily unique, instead use the (clang)
  503. // type connected to the decl.
  504. const Type *Key = Context.getTagDeclType(RD).getTypePtr();
  505. llvm::StructType *&Entry = RecordDeclTypes[Key];
  506. // If we don't have a StructType at all yet, create the forward declaration.
  507. if (Entry == 0) {
  508. Entry = llvm::StructType::create(getLLVMContext());
  509. addRecordTypeName(RD, Entry, "");
  510. }
  511. llvm::StructType *Ty = Entry;
  512. // If this is still a forward declaration, or the LLVM type is already
  513. // complete, there's nothing more to do.
  514. RD = RD->getDefinition();
  515. if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque())
  516. return Ty;
  517. // If converting this type would cause us to infinitely loop, don't do it!
  518. if (!isSafeToConvert(RD, *this)) {
  519. DeferredRecords.push_back(RD);
  520. return Ty;
  521. }
  522. // Okay, this is a definition of a type. Compile the implementation now.
  523. bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
  524. assert(InsertResult && "Recursively compiling a struct?");
  525. // Force conversion of non-virtual base classes recursively.
  526. if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
  527. for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
  528. e = CRD->bases_end(); i != e; ++i) {
  529. if (i->isVirtual()) continue;
  530. ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
  531. }
  532. }
  533. // Layout fields.
  534. CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
  535. CGRecordLayouts[Key] = Layout;
  536. // We're done laying out this struct.
  537. bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
  538. assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
  539. // If this struct blocked a FunctionType conversion, then recompute whatever
  540. // was derived from that.
  541. // FIXME: This is hugely overconservative.
  542. if (SkippedLayout)
  543. TypeCache.clear();
  544. // If we're done converting the outer-most record, then convert any deferred
  545. // structs as well.
  546. if (RecordsBeingLaidOut.empty())
  547. while (!DeferredRecords.empty())
  548. ConvertRecordDeclType(DeferredRecords.pop_back_val());
  549. return Ty;
  550. }
  551. /// getCGRecordLayout - Return record layout info for the given record decl.
  552. const CGRecordLayout &
  553. CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
  554. const Type *Key = Context.getTagDeclType(RD).getTypePtr();
  555. const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
  556. if (!Layout) {
  557. // Compute the type information.
  558. ConvertRecordDeclType(RD);
  559. // Now try again.
  560. Layout = CGRecordLayouts.lookup(Key);
  561. }
  562. assert(Layout && "Unable to find record layout information for type");
  563. return *Layout;
  564. }
  565. bool CodeGenTypes::isZeroInitializable(QualType T) {
  566. // No need to check for member pointers when not compiling C++.
  567. if (!Context.getLangOpts().CPlusPlus)
  568. return true;
  569. T = Context.getBaseElementType(T);
  570. // Records are non-zero-initializable if they contain any
  571. // non-zero-initializable subobjects.
  572. if (const RecordType *RT = T->getAs<RecordType>()) {
  573. const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
  574. return isZeroInitializable(RD);
  575. }
  576. // We have to ask the ABI about member pointers.
  577. if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
  578. return getCXXABI().isZeroInitializable(MPT);
  579. // Everything else is okay.
  580. return true;
  581. }
  582. bool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
  583. return getCGRecordLayout(RD).isZeroInitializable();
  584. }