CodeGenTypes.cpp 24 KB

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