CodeGenTypes.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 "CGCall.h"
  15. #include "CGCXXABI.h"
  16. #include "CGRecordLayout.h"
  17. #include "clang/AST/ASTContext.h"
  18. #include "clang/AST/DeclObjC.h"
  19. #include "clang/AST/DeclCXX.h"
  20. #include "clang/AST/Expr.h"
  21. #include "clang/AST/RecordLayout.h"
  22. #include "llvm/DerivedTypes.h"
  23. #include "llvm/Module.h"
  24. #include "llvm/Target/TargetData.h"
  25. using namespace clang;
  26. using namespace CodeGen;
  27. CodeGenTypes::CodeGenTypes(ASTContext &Ctx, llvm::Module& M,
  28. const llvm::TargetData &TD, const ABIInfo &Info,
  29. CGCXXABI &CXXABI, const CodeGenOptions &CGO)
  30. : Context(Ctx), Target(Ctx.getTargetInfo()), TheModule(M), TheTargetData(TD),
  31. TheABIInfo(Info), TheCXXABI(CXXABI), CodeGenOpts(CGO) {
  32. SkippedLayout = false;
  33. }
  34. CodeGenTypes::~CodeGenTypes() {
  35. for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
  36. I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
  37. I != E; ++I)
  38. delete I->second;
  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. OS << RD->getQualifiedNameAsString();
  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. OS << TDD->getQualifiedNameAsString();
  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. /// isFuncTypeArgumentConvertible - Return true if the specified type in a
  157. /// function argument 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::isFuncTypeArgumentConvertible(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. // If it's a tagged type used by-value, but is just a forward decl, we can't
  166. // convert it. Note that getDefinition()==0 is not the same as !isDefinition.
  167. if (TT->getDecl()->getDefinition() == 0)
  168. return false;
  169. // If this is an enum, then it is always safe to convert.
  170. const RecordType *RT = dyn_cast<RecordType>(TT);
  171. if (RT == 0) return true;
  172. // Otherwise, we have to be careful. If it is a struct that we're in the
  173. // process of expanding, then we can't convert the function type. That's ok
  174. // though because we must be in a pointer context under the struct, so we can
  175. // just convert it to a dummy type.
  176. //
  177. // We decide this by checking whether ConvertRecordDeclType returns us an
  178. // opaque type for a struct that we know is defined.
  179. return isSafeToConvert(RT->getDecl(), *this);
  180. }
  181. /// Code to verify a given function type is complete, i.e. the return type
  182. /// and all of the argument types are complete. Also check to see if we are in
  183. /// a RS_StructPointer context, and if so whether any struct types have been
  184. /// pended. If so, we don't want to ask the ABI lowering code to handle a type
  185. /// that cannot be converted to an IR type.
  186. bool CodeGenTypes::isFuncTypeConvertible(const FunctionType *FT) {
  187. if (!isFuncTypeArgumentConvertible(FT->getResultType()))
  188. return false;
  189. if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT))
  190. for (unsigned i = 0, e = FPT->getNumArgs(); i != e; i++)
  191. if (!isFuncTypeArgumentConvertible(FPT->getArgType(i)))
  192. return false;
  193. return true;
  194. }
  195. /// UpdateCompletedType - When we find the full definition for a TagDecl,
  196. /// replace the 'opaque' type we previously made for it if applicable.
  197. void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
  198. // If this is an enum being completed, then we flush all non-struct types from
  199. // the cache. This allows function types and other things that may be derived
  200. // from the enum to be recomputed.
  201. if (const EnumDecl *ED = dyn_cast<EnumDecl>(TD)) {
  202. // Only flush the cache if we've actually already converted this type.
  203. if (TypeCache.count(ED->getTypeForDecl())) {
  204. // Okay, we formed some types based on this. We speculated that the enum
  205. // would be lowered to i32, so we only need to flush the cache if this
  206. // didn't happen.
  207. if (!ConvertType(ED->getIntegerType())->isIntegerTy(32))
  208. TypeCache.clear();
  209. }
  210. return;
  211. }
  212. // If we completed a RecordDecl that we previously used and converted to an
  213. // anonymous type, then go ahead and complete it now.
  214. const RecordDecl *RD = cast<RecordDecl>(TD);
  215. if (RD->isDependentType()) return;
  216. // Only complete it if we converted it already. If we haven't converted it
  217. // yet, we'll just do it lazily.
  218. if (RecordDeclTypes.count(Context.getTagDeclType(RD).getTypePtr()))
  219. ConvertRecordDeclType(RD);
  220. }
  221. static llvm::Type *getTypeForFormat(llvm::LLVMContext &VMContext,
  222. const llvm::fltSemantics &format) {
  223. if (&format == &llvm::APFloat::IEEEhalf)
  224. return llvm::Type::getInt16Ty(VMContext);
  225. if (&format == &llvm::APFloat::IEEEsingle)
  226. return llvm::Type::getFloatTy(VMContext);
  227. if (&format == &llvm::APFloat::IEEEdouble)
  228. return llvm::Type::getDoubleTy(VMContext);
  229. if (&format == &llvm::APFloat::IEEEquad)
  230. return llvm::Type::getFP128Ty(VMContext);
  231. if (&format == &llvm::APFloat::PPCDoubleDouble)
  232. return llvm::Type::getPPC_FP128Ty(VMContext);
  233. if (&format == &llvm::APFloat::x87DoubleExtended)
  234. return llvm::Type::getX86_FP80Ty(VMContext);
  235. llvm_unreachable("Unknown float format!");
  236. }
  237. /// ConvertType - Convert the specified type to its LLVM form.
  238. llvm::Type *CodeGenTypes::ConvertType(QualType T) {
  239. T = Context.getCanonicalType(T);
  240. const Type *Ty = T.getTypePtr();
  241. // RecordTypes are cached and processed specially.
  242. if (const RecordType *RT = dyn_cast<RecordType>(Ty))
  243. return ConvertRecordDeclType(RT->getDecl());
  244. // See if type is already cached.
  245. llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
  246. // If type is found in map then use it. Otherwise, convert type T.
  247. if (TCI != TypeCache.end())
  248. return TCI->second;
  249. // If we don't have it in the cache, convert it now.
  250. llvm::Type *ResultType = 0;
  251. switch (Ty->getTypeClass()) {
  252. case Type::Record: // Handled above.
  253. #define TYPE(Class, Base)
  254. #define ABSTRACT_TYPE(Class, Base)
  255. #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
  256. #define DEPENDENT_TYPE(Class, Base) case Type::Class:
  257. #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
  258. #include "clang/AST/TypeNodes.def"
  259. llvm_unreachable("Non-canonical or dependent types aren't possible.");
  260. case Type::Builtin: {
  261. switch (cast<BuiltinType>(Ty)->getKind()) {
  262. case BuiltinType::Void:
  263. case BuiltinType::ObjCId:
  264. case BuiltinType::ObjCClass:
  265. case BuiltinType::ObjCSel:
  266. // LLVM void type can only be used as the result of a function call. Just
  267. // map to the same as char.
  268. ResultType = llvm::Type::getInt8Ty(getLLVMContext());
  269. break;
  270. case BuiltinType::Bool:
  271. // Note that we always return bool as i1 for use as a scalar type.
  272. ResultType = llvm::Type::getInt1Ty(getLLVMContext());
  273. break;
  274. case BuiltinType::Char_S:
  275. case BuiltinType::Char_U:
  276. case BuiltinType::SChar:
  277. case BuiltinType::UChar:
  278. case BuiltinType::Short:
  279. case BuiltinType::UShort:
  280. case BuiltinType::Int:
  281. case BuiltinType::UInt:
  282. case BuiltinType::Long:
  283. case BuiltinType::ULong:
  284. case BuiltinType::LongLong:
  285. case BuiltinType::ULongLong:
  286. case BuiltinType::WChar_S:
  287. case BuiltinType::WChar_U:
  288. case BuiltinType::Char16:
  289. case BuiltinType::Char32:
  290. ResultType = llvm::IntegerType::get(getLLVMContext(),
  291. static_cast<unsigned>(Context.getTypeSize(T)));
  292. break;
  293. case BuiltinType::Half:
  294. // Half is special: it might be lowered to i16 (and will be storage-only
  295. // type),. or can be represented as a set of native operations.
  296. // FIXME: Ask target which kind of half FP it prefers (storage only vs
  297. // native).
  298. ResultType = llvm::Type::getInt16Ty(getLLVMContext());
  299. break;
  300. case BuiltinType::Float:
  301. case BuiltinType::Double:
  302. case BuiltinType::LongDouble:
  303. ResultType = getTypeForFormat(getLLVMContext(),
  304. Context.getFloatTypeSemantics(T));
  305. break;
  306. case BuiltinType::NullPtr:
  307. // Model std::nullptr_t as i8*
  308. ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
  309. break;
  310. case BuiltinType::UInt128:
  311. case BuiltinType::Int128:
  312. ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
  313. break;
  314. case BuiltinType::Dependent:
  315. #define BUILTIN_TYPE(Id, SingletonId)
  316. #define PLACEHOLDER_TYPE(Id, SingletonId) \
  317. case BuiltinType::Id:
  318. #include "clang/AST/BuiltinTypes.def"
  319. llvm_unreachable("Unexpected placeholder builtin type!");
  320. }
  321. break;
  322. }
  323. case Type::Complex: {
  324. llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
  325. ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
  326. break;
  327. }
  328. case Type::LValueReference:
  329. case Type::RValueReference: {
  330. const ReferenceType *RTy = cast<ReferenceType>(Ty);
  331. QualType ETy = RTy->getPointeeType();
  332. llvm::Type *PointeeType = ConvertTypeForMem(ETy);
  333. unsigned AS = Context.getTargetAddressSpace(ETy);
  334. ResultType = llvm::PointerType::get(PointeeType, AS);
  335. break;
  336. }
  337. case Type::Pointer: {
  338. const PointerType *PTy = cast<PointerType>(Ty);
  339. QualType ETy = PTy->getPointeeType();
  340. llvm::Type *PointeeType = ConvertTypeForMem(ETy);
  341. if (PointeeType->isVoidTy())
  342. PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
  343. unsigned AS = Context.getTargetAddressSpace(ETy);
  344. ResultType = llvm::PointerType::get(PointeeType, AS);
  345. break;
  346. }
  347. case Type::VariableArray: {
  348. const VariableArrayType *A = cast<VariableArrayType>(Ty);
  349. assert(A->getIndexTypeCVRQualifiers() == 0 &&
  350. "FIXME: We only handle trivial array types so far!");
  351. // VLAs resolve to the innermost element type; this matches
  352. // the return of alloca, and there isn't any obviously better choice.
  353. ResultType = ConvertTypeForMem(A->getElementType());
  354. break;
  355. }
  356. case Type::IncompleteArray: {
  357. const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
  358. assert(A->getIndexTypeCVRQualifiers() == 0 &&
  359. "FIXME: We only handle trivial array types so far!");
  360. // int X[] -> [0 x int], unless the element type is not sized. If it is
  361. // unsized (e.g. an incomplete struct) just use [0 x i8].
  362. ResultType = ConvertTypeForMem(A->getElementType());
  363. if (!ResultType->isSized()) {
  364. SkippedLayout = true;
  365. ResultType = llvm::Type::getInt8Ty(getLLVMContext());
  366. }
  367. ResultType = llvm::ArrayType::get(ResultType, 0);
  368. break;
  369. }
  370. case Type::ConstantArray: {
  371. const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
  372. llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
  373. // Lower arrays of undefined struct type to arrays of i8 just to have a
  374. // concrete type.
  375. if (!EltTy->isSized()) {
  376. SkippedLayout = true;
  377. EltTy = llvm::Type::getInt8Ty(getLLVMContext());
  378. }
  379. ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
  380. break;
  381. }
  382. case Type::ExtVector:
  383. case Type::Vector: {
  384. const VectorType *VT = cast<VectorType>(Ty);
  385. ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
  386. VT->getNumElements());
  387. break;
  388. }
  389. case Type::FunctionNoProto:
  390. case Type::FunctionProto: {
  391. const FunctionType *FT = cast<FunctionType>(Ty);
  392. // First, check whether we can build the full function type. If the
  393. // function type depends on an incomplete type (e.g. a struct or enum), we
  394. // cannot lower the function type.
  395. if (!isFuncTypeConvertible(FT)) {
  396. // This function's type depends on an incomplete tag type.
  397. // Return a placeholder type.
  398. ResultType = llvm::StructType::get(getLLVMContext());
  399. SkippedLayout = true;
  400. break;
  401. }
  402. // While we're converting the argument types for a function, we don't want
  403. // to recursively convert any pointed-to structs. Converting directly-used
  404. // structs is ok though.
  405. if (!RecordsBeingLaidOut.insert(Ty)) {
  406. ResultType = llvm::StructType::get(getLLVMContext());
  407. SkippedLayout = true;
  408. break;
  409. }
  410. // The function type can be built; call the appropriate routines to
  411. // build it.
  412. const CGFunctionInfo *FI;
  413. bool isVariadic;
  414. if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
  415. FI = &getFunctionInfo(
  416. CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
  417. isVariadic = FPT->isVariadic();
  418. } else {
  419. const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
  420. FI = &getFunctionInfo(
  421. CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
  422. isVariadic = true;
  423. }
  424. // If there is something higher level prodding our CGFunctionInfo, then
  425. // don't recurse into it again.
  426. if (FunctionsBeingProcessed.count(FI)) {
  427. ResultType = llvm::StructType::get(getLLVMContext());
  428. SkippedLayout = true;
  429. } else {
  430. // Otherwise, we're good to go, go ahead and convert it.
  431. ResultType = GetFunctionType(*FI, isVariadic);
  432. }
  433. RecordsBeingLaidOut.erase(Ty);
  434. if (SkippedLayout)
  435. TypeCache.clear();
  436. if (RecordsBeingLaidOut.empty())
  437. while (!DeferredRecords.empty())
  438. ConvertRecordDeclType(DeferredRecords.pop_back_val());
  439. break;
  440. }
  441. case Type::ObjCObject:
  442. ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
  443. break;
  444. case Type::ObjCInterface: {
  445. // Objective-C interfaces are always opaque (outside of the
  446. // runtime, which can do whatever it likes); we never refine
  447. // these.
  448. llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
  449. if (!T)
  450. T = llvm::StructType::create(getLLVMContext());
  451. ResultType = T;
  452. break;
  453. }
  454. case Type::ObjCObjectPointer: {
  455. // Protocol qualifications do not influence the LLVM type, we just return a
  456. // pointer to the underlying interface type. We don't need to worry about
  457. // recursive conversion.
  458. llvm::Type *T =
  459. ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
  460. ResultType = T->getPointerTo();
  461. break;
  462. }
  463. case Type::Enum: {
  464. const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
  465. if (ED->isCompleteDefinition() || ED->isFixed())
  466. return ConvertType(ED->getIntegerType());
  467. // Return a placeholder 'i32' type. This can be changed later when the
  468. // type is defined (see UpdateCompletedType), but is likely to be the
  469. // "right" answer.
  470. ResultType = llvm::Type::getInt32Ty(getLLVMContext());
  471. break;
  472. }
  473. case Type::BlockPointer: {
  474. const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
  475. llvm::Type *PointeeType = ConvertTypeForMem(FTy);
  476. unsigned AS = Context.getTargetAddressSpace(FTy);
  477. ResultType = llvm::PointerType::get(PointeeType, AS);
  478. break;
  479. }
  480. case Type::MemberPointer: {
  481. ResultType =
  482. getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
  483. break;
  484. }
  485. case Type::Atomic: {
  486. ResultType = ConvertTypeForMem(cast<AtomicType>(Ty)->getValueType());
  487. break;
  488. }
  489. }
  490. assert(ResultType && "Didn't convert a type?");
  491. TypeCache[Ty] = ResultType;
  492. return ResultType;
  493. }
  494. /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
  495. llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
  496. // TagDecl's are not necessarily unique, instead use the (clang)
  497. // type connected to the decl.
  498. const Type *Key = Context.getTagDeclType(RD).getTypePtr();
  499. llvm::StructType *&Entry = RecordDeclTypes[Key];
  500. // If we don't have a StructType at all yet, create the forward declaration.
  501. if (Entry == 0) {
  502. Entry = llvm::StructType::create(getLLVMContext());
  503. addRecordTypeName(RD, Entry, "");
  504. }
  505. llvm::StructType *Ty = Entry;
  506. // If this is still a forward declaration, or the LLVM type is already
  507. // complete, there's nothing more to do.
  508. RD = RD->getDefinition();
  509. if (RD == 0 || !RD->isCompleteDefinition() || !Ty->isOpaque())
  510. return Ty;
  511. // If converting this type would cause us to infinitely loop, don't do it!
  512. if (!isSafeToConvert(RD, *this)) {
  513. DeferredRecords.push_back(RD);
  514. return Ty;
  515. }
  516. // Okay, this is a definition of a type. Compile the implementation now.
  517. bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
  518. assert(InsertResult && "Recursively compiling a struct?");
  519. // Force conversion of non-virtual base classes recursively.
  520. if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
  521. for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
  522. e = CRD->bases_end(); i != e; ++i) {
  523. if (i->isVirtual()) continue;
  524. ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
  525. }
  526. }
  527. // Layout fields.
  528. CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
  529. CGRecordLayouts[Key] = Layout;
  530. // We're done laying out this struct.
  531. bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
  532. assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
  533. // If this struct blocked a FunctionType conversion, then recompute whatever
  534. // was derived from that.
  535. // FIXME: This is hugely overconservative.
  536. if (SkippedLayout)
  537. TypeCache.clear();
  538. // If we're done converting the outer-most record, then convert any deferred
  539. // structs as well.
  540. if (RecordsBeingLaidOut.empty())
  541. while (!DeferredRecords.empty())
  542. ConvertRecordDeclType(DeferredRecords.pop_back_val());
  543. return Ty;
  544. }
  545. /// getCGRecordLayout - Return record layout info for the given record decl.
  546. const CGRecordLayout &
  547. CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
  548. const Type *Key = Context.getTagDeclType(RD).getTypePtr();
  549. const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
  550. if (!Layout) {
  551. // Compute the type information.
  552. ConvertRecordDeclType(RD);
  553. // Now try again.
  554. Layout = CGRecordLayouts.lookup(Key);
  555. }
  556. assert(Layout && "Unable to find record layout information for type");
  557. return *Layout;
  558. }
  559. bool CodeGenTypes::isZeroInitializable(QualType T) {
  560. // No need to check for member pointers when not compiling C++.
  561. if (!Context.getLangOptions().CPlusPlus)
  562. return true;
  563. T = Context.getBaseElementType(T);
  564. // Records are non-zero-initializable if they contain any
  565. // non-zero-initializable subobjects.
  566. if (const RecordType *RT = T->getAs<RecordType>()) {
  567. const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
  568. return isZeroInitializable(RD);
  569. }
  570. // We have to ask the ABI about member pointers.
  571. if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
  572. return getCXXABI().isZeroInitializable(MPT);
  573. // Everything else is okay.
  574. return true;
  575. }
  576. bool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
  577. return getCGRecordLayout(RD).isZeroInitializable();
  578. }