CodeGenTypes.cpp 25 KB

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