CodeGenTypes.cpp 26 KB

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