CodeGenTypes.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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.Target), 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. llvm::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::IEEEsingle)
  224. return llvm::Type::getFloatTy(VMContext);
  225. if (&format == &llvm::APFloat::IEEEdouble)
  226. return llvm::Type::getDoubleTy(VMContext);
  227. if (&format == &llvm::APFloat::IEEEquad)
  228. return llvm::Type::getFP128Ty(VMContext);
  229. if (&format == &llvm::APFloat::PPCDoubleDouble)
  230. return llvm::Type::getPPC_FP128Ty(VMContext);
  231. if (&format == &llvm::APFloat::x87DoubleExtended)
  232. return llvm::Type::getX86_FP80Ty(VMContext);
  233. assert(0 && "Unknown float format!");
  234. return 0;
  235. }
  236. /// ConvertType - Convert the specified type to its LLVM form.
  237. llvm::Type *CodeGenTypes::ConvertType(QualType T) {
  238. T = Context.getCanonicalType(T);
  239. const Type *Ty = T.getTypePtr();
  240. // RecordTypes are cached and processed specially.
  241. if (const RecordType *RT = dyn_cast<RecordType>(Ty))
  242. return ConvertRecordDeclType(RT->getDecl());
  243. // See if type is already cached.
  244. llvm::DenseMap<const Type *, llvm::Type *>::iterator TCI = TypeCache.find(Ty);
  245. // If type is found in map then use it. Otherwise, convert type T.
  246. if (TCI != TypeCache.end())
  247. return TCI->second;
  248. // If we don't have it in the cache, convert it now.
  249. llvm::Type *ResultType = 0;
  250. switch (Ty->getTypeClass()) {
  251. case Type::Record: // Handled above.
  252. #define TYPE(Class, Base)
  253. #define ABSTRACT_TYPE(Class, Base)
  254. #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
  255. #define DEPENDENT_TYPE(Class, Base) case Type::Class:
  256. #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
  257. #include "clang/AST/TypeNodes.def"
  258. llvm_unreachable("Non-canonical or dependent types aren't possible.");
  259. break;
  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::Float:
  294. case BuiltinType::Double:
  295. case BuiltinType::LongDouble:
  296. ResultType = getTypeForFormat(getLLVMContext(),
  297. Context.getFloatTypeSemantics(T));
  298. break;
  299. case BuiltinType::NullPtr:
  300. // Model std::nullptr_t as i8*
  301. ResultType = llvm::Type::getInt8PtrTy(getLLVMContext());
  302. break;
  303. case BuiltinType::UInt128:
  304. case BuiltinType::Int128:
  305. ResultType = llvm::IntegerType::get(getLLVMContext(), 128);
  306. break;
  307. case BuiltinType::Overload:
  308. case BuiltinType::Dependent:
  309. case BuiltinType::BoundMember:
  310. case BuiltinType::UnknownAny:
  311. llvm_unreachable("Unexpected placeholder builtin type!");
  312. break;
  313. }
  314. break;
  315. }
  316. case Type::Complex: {
  317. llvm::Type *EltTy = ConvertType(cast<ComplexType>(Ty)->getElementType());
  318. ResultType = llvm::StructType::get(EltTy, EltTy, NULL);
  319. break;
  320. }
  321. case Type::LValueReference:
  322. case Type::RValueReference: {
  323. const ReferenceType *RTy = cast<ReferenceType>(Ty);
  324. QualType ETy = RTy->getPointeeType();
  325. llvm::Type *PointeeType = ConvertTypeForMem(ETy);
  326. unsigned AS = Context.getTargetAddressSpace(ETy);
  327. ResultType = llvm::PointerType::get(PointeeType, AS);
  328. break;
  329. }
  330. case Type::Pointer: {
  331. const PointerType *PTy = cast<PointerType>(Ty);
  332. QualType ETy = PTy->getPointeeType();
  333. llvm::Type *PointeeType = ConvertTypeForMem(ETy);
  334. if (PointeeType->isVoidTy())
  335. PointeeType = llvm::Type::getInt8Ty(getLLVMContext());
  336. unsigned AS = Context.getTargetAddressSpace(ETy);
  337. ResultType = llvm::PointerType::get(PointeeType, AS);
  338. break;
  339. }
  340. case Type::VariableArray: {
  341. const VariableArrayType *A = cast<VariableArrayType>(Ty);
  342. assert(A->getIndexTypeCVRQualifiers() == 0 &&
  343. "FIXME: We only handle trivial array types so far!");
  344. // VLAs resolve to the innermost element type; this matches
  345. // the return of alloca, and there isn't any obviously better choice.
  346. ResultType = ConvertTypeForMem(A->getElementType());
  347. break;
  348. }
  349. case Type::IncompleteArray: {
  350. const IncompleteArrayType *A = cast<IncompleteArrayType>(Ty);
  351. assert(A->getIndexTypeCVRQualifiers() == 0 &&
  352. "FIXME: We only handle trivial array types so far!");
  353. // int X[] -> [0 x int], unless the element type is not sized. If it is
  354. // unsized (e.g. an incomplete struct) just use [0 x i8].
  355. ResultType = ConvertTypeForMem(A->getElementType());
  356. if (!ResultType->isSized()) {
  357. SkippedLayout = true;
  358. ResultType = llvm::Type::getInt8Ty(getLLVMContext());
  359. }
  360. ResultType = llvm::ArrayType::get(ResultType, 0);
  361. break;
  362. }
  363. case Type::ConstantArray: {
  364. const ConstantArrayType *A = cast<ConstantArrayType>(Ty);
  365. llvm::Type *EltTy = ConvertTypeForMem(A->getElementType());
  366. // Lower arrays of undefined struct type to arrays of i8 just to have a
  367. // concrete type.
  368. if (!EltTy->isSized()) {
  369. SkippedLayout = true;
  370. EltTy = llvm::Type::getInt8Ty(getLLVMContext());
  371. }
  372. ResultType = llvm::ArrayType::get(EltTy, A->getSize().getZExtValue());
  373. break;
  374. }
  375. case Type::ExtVector:
  376. case Type::Vector: {
  377. const VectorType *VT = cast<VectorType>(Ty);
  378. ResultType = llvm::VectorType::get(ConvertType(VT->getElementType()),
  379. VT->getNumElements());
  380. break;
  381. }
  382. case Type::FunctionNoProto:
  383. case Type::FunctionProto: {
  384. const FunctionType *FT = cast<FunctionType>(Ty);
  385. // First, check whether we can build the full function type. If the
  386. // function type depends on an incomplete type (e.g. a struct or enum), we
  387. // cannot lower the function type.
  388. if (!isFuncTypeConvertible(FT)) {
  389. // This function's type depends on an incomplete tag type.
  390. // Return a placeholder type.
  391. ResultType = llvm::StructType::get(getLLVMContext());
  392. SkippedLayout = true;
  393. break;
  394. }
  395. // While we're converting the argument types for a function, we don't want
  396. // to recursively convert any pointed-to structs. Converting directly-used
  397. // structs is ok though.
  398. if (!RecordsBeingLaidOut.insert(Ty)) {
  399. ResultType = llvm::StructType::get(getLLVMContext());
  400. SkippedLayout = true;
  401. break;
  402. }
  403. // The function type can be built; call the appropriate routines to
  404. // build it.
  405. const CGFunctionInfo *FI;
  406. bool isVariadic;
  407. if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
  408. FI = &getFunctionInfo(
  409. CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)));
  410. isVariadic = FPT->isVariadic();
  411. } else {
  412. const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(FT);
  413. FI = &getFunctionInfo(
  414. CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)));
  415. isVariadic = true;
  416. }
  417. // If there is something higher level prodding our CGFunctionInfo, then
  418. // don't recurse into it again.
  419. if (FunctionsBeingProcessed.count(FI)) {
  420. ResultType = llvm::StructType::get(getLLVMContext());
  421. SkippedLayout = true;
  422. } else {
  423. // Otherwise, we're good to go, go ahead and convert it.
  424. ResultType = GetFunctionType(*FI, isVariadic);
  425. }
  426. RecordsBeingLaidOut.erase(Ty);
  427. if (SkippedLayout)
  428. TypeCache.clear();
  429. if (RecordsBeingLaidOut.empty())
  430. while (!DeferredRecords.empty())
  431. ConvertRecordDeclType(DeferredRecords.pop_back_val());
  432. break;
  433. }
  434. case Type::ObjCObject:
  435. ResultType = ConvertType(cast<ObjCObjectType>(Ty)->getBaseType());
  436. break;
  437. case Type::ObjCInterface: {
  438. // Objective-C interfaces are always opaque (outside of the
  439. // runtime, which can do whatever it likes); we never refine
  440. // these.
  441. llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(Ty)];
  442. if (!T)
  443. T = llvm::StructType::create(getLLVMContext());
  444. ResultType = T;
  445. break;
  446. }
  447. case Type::ObjCObjectPointer: {
  448. // Protocol qualifications do not influence the LLVM type, we just return a
  449. // pointer to the underlying interface type. We don't need to worry about
  450. // recursive conversion.
  451. llvm::Type *T =
  452. ConvertTypeForMem(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
  453. ResultType = T->getPointerTo();
  454. break;
  455. }
  456. case Type::Enum: {
  457. const EnumDecl *ED = cast<EnumType>(Ty)->getDecl();
  458. if (ED->isDefinition() || ED->isFixed())
  459. return ConvertType(ED->getIntegerType());
  460. // Return a placeholder 'i32' type. This can be changed later when the
  461. // type is defined (see UpdateCompletedType), but is likely to be the
  462. // "right" answer.
  463. ResultType = llvm::Type::getInt32Ty(getLLVMContext());
  464. break;
  465. }
  466. case Type::BlockPointer: {
  467. const QualType FTy = cast<BlockPointerType>(Ty)->getPointeeType();
  468. llvm::Type *PointeeType = ConvertTypeForMem(FTy);
  469. unsigned AS = Context.getTargetAddressSpace(FTy);
  470. ResultType = llvm::PointerType::get(PointeeType, AS);
  471. break;
  472. }
  473. case Type::MemberPointer: {
  474. ResultType =
  475. getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(Ty));
  476. break;
  477. }
  478. }
  479. assert(ResultType && "Didn't convert a type?");
  480. TypeCache[Ty] = ResultType;
  481. return ResultType;
  482. }
  483. /// ConvertRecordDeclType - Lay out a tagged decl type like struct or union.
  484. llvm::StructType *CodeGenTypes::ConvertRecordDeclType(const RecordDecl *RD) {
  485. // TagDecl's are not necessarily unique, instead use the (clang)
  486. // type connected to the decl.
  487. const Type *Key = Context.getTagDeclType(RD).getTypePtr();
  488. llvm::StructType *&Entry = RecordDeclTypes[Key];
  489. // If we don't have a StructType at all yet, create the forward declaration.
  490. if (Entry == 0) {
  491. Entry = llvm::StructType::create(getLLVMContext());
  492. addRecordTypeName(RD, Entry, "");
  493. }
  494. llvm::StructType *Ty = Entry;
  495. // If this is still a forward declaration, or the LLVM type is already
  496. // complete, there's nothing more to do.
  497. RD = RD->getDefinition();
  498. if (RD == 0 || !Ty->isOpaque())
  499. return Ty;
  500. // If converting this type would cause us to infinitely loop, don't do it!
  501. if (!isSafeToConvert(RD, *this)) {
  502. DeferredRecords.push_back(RD);
  503. return Ty;
  504. }
  505. // Okay, this is a definition of a type. Compile the implementation now.
  506. bool InsertResult = RecordsBeingLaidOut.insert(Key); (void)InsertResult;
  507. assert(InsertResult && "Recursively compiling a struct?");
  508. // Force conversion of non-virtual base classes recursively.
  509. if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
  510. for (CXXRecordDecl::base_class_const_iterator i = CRD->bases_begin(),
  511. e = CRD->bases_end(); i != e; ++i) {
  512. if (i->isVirtual()) continue;
  513. ConvertRecordDeclType(i->getType()->getAs<RecordType>()->getDecl());
  514. }
  515. }
  516. // Layout fields.
  517. CGRecordLayout *Layout = ComputeRecordLayout(RD, Ty);
  518. CGRecordLayouts[Key] = Layout;
  519. // We're done laying out this struct.
  520. bool EraseResult = RecordsBeingLaidOut.erase(Key); (void)EraseResult;
  521. assert(EraseResult && "struct not in RecordsBeingLaidOut set?");
  522. // If this struct blocked a FunctionType conversion, then recompute whatever
  523. // was derived from that.
  524. // FIXME: This is hugely overconservative.
  525. if (SkippedLayout)
  526. TypeCache.clear();
  527. // If we're done converting the outer-most record, then convert any deferred
  528. // structs as well.
  529. if (RecordsBeingLaidOut.empty())
  530. while (!DeferredRecords.empty())
  531. ConvertRecordDeclType(DeferredRecords.pop_back_val());
  532. return Ty;
  533. }
  534. /// getCGRecordLayout - Return record layout info for the given record decl.
  535. const CGRecordLayout &
  536. CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
  537. const Type *Key = Context.getTagDeclType(RD).getTypePtr();
  538. const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
  539. if (!Layout) {
  540. // Compute the type information.
  541. ConvertRecordDeclType(RD);
  542. // Now try again.
  543. Layout = CGRecordLayouts.lookup(Key);
  544. }
  545. assert(Layout && "Unable to find record layout information for type");
  546. return *Layout;
  547. }
  548. bool CodeGenTypes::isZeroInitializable(QualType T) {
  549. // No need to check for member pointers when not compiling C++.
  550. if (!Context.getLangOptions().CPlusPlus)
  551. return true;
  552. T = Context.getBaseElementType(T);
  553. // Records are non-zero-initializable if they contain any
  554. // non-zero-initializable subobjects.
  555. if (const RecordType *RT = T->getAs<RecordType>()) {
  556. const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
  557. return isZeroInitializable(RD);
  558. }
  559. // We have to ask the ABI about member pointers.
  560. if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
  561. return getCXXABI().isZeroInitializable(MPT);
  562. // Everything else is okay.
  563. return true;
  564. }
  565. bool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
  566. return getCGRecordLayout(RD).isZeroInitializable();
  567. }