CodeGenTypes.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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)
  30. : Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD),
  31. TheABIInfo(Info), TheCXXABI(CXXABI) {
  32. }
  33. CodeGenTypes::~CodeGenTypes() {
  34. for (llvm::DenseMap<const Type *, CGRecordLayout *>::iterator
  35. I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
  36. I != E; ++I)
  37. delete I->second;
  38. for (llvm::FoldingSet<CGFunctionInfo>::iterator
  39. I = FunctionInfos.begin(), E = FunctionInfos.end(); I != E; )
  40. delete &*I++;
  41. }
  42. /// HandleLateResolvedPointers - For top-level ConvertType calls, this handles
  43. /// pointers that are referenced but have not been converted yet. This is used
  44. /// to handle cyclic structures properly.
  45. void CodeGenTypes::HandleLateResolvedPointers() {
  46. assert(!PointersToResolve.empty() && "No pointers to resolve!");
  47. // Any pointers that were converted deferred evaluation of their pointee type,
  48. // creating an opaque type instead. This is in order to avoid problems with
  49. // circular types. Loop through all these defered pointees, if any, and
  50. // resolve them now.
  51. while (!PointersToResolve.empty()) {
  52. std::pair<QualType, llvm::OpaqueType*> P = PointersToResolve.pop_back_val();
  53. // We can handle bare pointers here because we know that the only pointers
  54. // to the Opaque type are P.second and from other types. Refining the
  55. // opqaue type away will invalidate P.second, but we don't mind :).
  56. const llvm::Type *NT = ConvertTypeForMemRecursive(P.first);
  57. P.second->refineAbstractTypeTo(NT);
  58. }
  59. }
  60. void CodeGenTypes::addTagTypeName(const TagDecl *TD, const llvm::Type *Ty,
  61. llvm::StringRef suffix) {
  62. llvm::SmallString<256> TypeName;
  63. llvm::raw_svector_ostream OS(TypeName);
  64. OS << TD->getKindName() << '.';
  65. // Name the codegen type after the typedef name
  66. // if there is no tag type name available
  67. if (TD->getIdentifier()) {
  68. // FIXME: We should not have to check for a null decl context here.
  69. // Right now we do it because the implicit Obj-C decls don't have one.
  70. if (TD->getDeclContext())
  71. OS << TD->getQualifiedNameAsString();
  72. else
  73. TD->printName(OS);
  74. } else if (const TypedefNameDecl *TDD = TD->getTypedefNameForAnonDecl()) {
  75. // FIXME: We should not have to check for a null decl context here.
  76. // Right now we do it because the implicit Obj-C decls don't have one.
  77. if (TDD->getDeclContext())
  78. OS << TDD->getQualifiedNameAsString();
  79. else
  80. TDD->printName(OS);
  81. } else
  82. OS << "anon";
  83. if (!suffix.empty())
  84. OS << suffix;
  85. TheModule.addTypeName(OS.str(), Ty);
  86. }
  87. /// ConvertType - Convert the specified type to its LLVM form.
  88. const llvm::Type *CodeGenTypes::ConvertType(QualType T, bool IsRecursive) {
  89. const llvm::Type *Result = ConvertTypeRecursive(T);
  90. // If this is a top-level call to ConvertType and sub-conversions caused
  91. // pointers to get lazily built as opaque types, resolve the pointers, which
  92. // might cause Result to be merged away.
  93. if (!IsRecursive && !PointersToResolve.empty()) {
  94. llvm::PATypeHolder ResultHandle = Result;
  95. HandleLateResolvedPointers();
  96. Result = ResultHandle;
  97. }
  98. return Result;
  99. }
  100. const llvm::Type *CodeGenTypes::ConvertTypeRecursive(QualType T) {
  101. T = Context.getCanonicalType(T);
  102. // See if type is already cached.
  103. llvm::DenseMap<const Type *, llvm::PATypeHolder>::iterator
  104. I = TypeCache.find(T.getTypePtr());
  105. // If type is found in map and this is not a definition for a opaque
  106. // place holder type then use it. Otherwise, convert type T.
  107. if (I != TypeCache.end())
  108. return I->second.get();
  109. const llvm::Type *ResultType = ConvertNewType(T);
  110. TypeCache.insert(std::make_pair(T.getTypePtr(),
  111. llvm::PATypeHolder(ResultType)));
  112. return ResultType;
  113. }
  114. /// ConvertTypeForMem - Convert type T into a llvm::Type. This differs from
  115. /// ConvertType in that it is used to convert to the memory representation for
  116. /// a type. For example, the scalar representation for _Bool is i1, but the
  117. /// memory representation is usually i8 or i32, depending on the target.
  118. const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T, bool IsRecursive){
  119. const llvm::Type *R = ConvertType(T, IsRecursive);
  120. // If this is a non-bool type, don't map it.
  121. if (!R->isIntegerTy(1))
  122. return R;
  123. // Otherwise, return an integer of the target-specified size.
  124. return llvm::IntegerType::get(getLLVMContext(),
  125. (unsigned)Context.getTypeSize(T));
  126. }
  127. // Code to verify a given function type is complete, i.e. the return type
  128. // and all of the argument types are complete.
  129. const TagType *CodeGenTypes::VerifyFuncTypeComplete(const Type* T) {
  130. const FunctionType *FT = cast<FunctionType>(T);
  131. if (const TagType* TT = FT->getResultType()->getAs<TagType>())
  132. if (!TT->getDecl()->isDefinition())
  133. return TT;
  134. if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T))
  135. for (unsigned i = 0; i < FPT->getNumArgs(); i++)
  136. if (const TagType* TT = FPT->getArgType(i)->getAs<TagType>())
  137. if (!TT->getDecl()->isDefinition())
  138. return TT;
  139. return 0;
  140. }
  141. /// UpdateCompletedType - When we find the full definition for a TagDecl,
  142. /// replace the 'opaque' type we previously made for it if applicable.
  143. void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
  144. const Type *Key = Context.getTagDeclType(TD).getTypePtr();
  145. llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
  146. TagDeclTypes.find(Key);
  147. if (TDTI == TagDeclTypes.end()) return;
  148. // Remember the opaque LLVM type for this tagdecl.
  149. llvm::PATypeHolder OpaqueHolder = TDTI->second;
  150. assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) &&
  151. "Updating compilation of an already non-opaque type?");
  152. // Remove it from TagDeclTypes so that it will be regenerated.
  153. TagDeclTypes.erase(TDTI);
  154. // Generate the new type.
  155. const llvm::Type *NT = ConvertTagDeclType(TD);
  156. // Refine the old opaque type to its new definition.
  157. cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT);
  158. // Since we just completed a tag type, check to see if any function types
  159. // were completed along with the tag type.
  160. // FIXME: This is very inefficient; if we track which function types depend
  161. // on which tag types, though, it should be reasonably efficient.
  162. llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator i;
  163. for (i = FunctionTypes.begin(); i != FunctionTypes.end(); ++i) {
  164. if (const TagType* TT = VerifyFuncTypeComplete(i->first)) {
  165. // This function type still depends on an incomplete tag type; make sure
  166. // that tag type has an associated opaque type.
  167. ConvertTagDeclType(TT->getDecl());
  168. } else {
  169. // This function no longer depends on an incomplete tag type; create the
  170. // function type, and refine the opaque type to the new function type.
  171. llvm::PATypeHolder OpaqueHolder = i->second;
  172. const llvm::Type *NFT = ConvertNewType(QualType(i->first, 0));
  173. cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NFT);
  174. FunctionTypes.erase(i);
  175. }
  176. }
  177. }
  178. static const llvm::Type* getTypeForFormat(llvm::LLVMContext &VMContext,
  179. const llvm::fltSemantics &format) {
  180. if (&format == &llvm::APFloat::IEEEsingle)
  181. return llvm::Type::getFloatTy(VMContext);
  182. if (&format == &llvm::APFloat::IEEEdouble)
  183. return llvm::Type::getDoubleTy(VMContext);
  184. if (&format == &llvm::APFloat::IEEEquad)
  185. return llvm::Type::getFP128Ty(VMContext);
  186. if (&format == &llvm::APFloat::PPCDoubleDouble)
  187. return llvm::Type::getPPC_FP128Ty(VMContext);
  188. if (&format == &llvm::APFloat::x87DoubleExtended)
  189. return llvm::Type::getX86_FP80Ty(VMContext);
  190. assert(0 && "Unknown float format!");
  191. return 0;
  192. }
  193. const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
  194. const clang::Type &Ty = *Context.getCanonicalType(T).getTypePtr();
  195. switch (Ty.getTypeClass()) {
  196. #define TYPE(Class, Base)
  197. #define ABSTRACT_TYPE(Class, Base)
  198. #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
  199. #define DEPENDENT_TYPE(Class, Base) case Type::Class:
  200. #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
  201. #include "clang/AST/TypeNodes.def"
  202. assert(false && "Non-canonical or dependent types aren't possible.");
  203. break;
  204. case Type::Builtin: {
  205. switch (cast<BuiltinType>(Ty).getKind()) {
  206. case BuiltinType::Void:
  207. case BuiltinType::ObjCId:
  208. case BuiltinType::ObjCClass:
  209. case BuiltinType::ObjCSel:
  210. // LLVM void type can only be used as the result of a function call. Just
  211. // map to the same as char.
  212. return llvm::Type::getInt8Ty(getLLVMContext());
  213. case BuiltinType::Bool:
  214. // Note that we always return bool as i1 for use as a scalar type.
  215. return llvm::Type::getInt1Ty(getLLVMContext());
  216. case BuiltinType::Char_S:
  217. case BuiltinType::Char_U:
  218. case BuiltinType::SChar:
  219. case BuiltinType::UChar:
  220. case BuiltinType::Short:
  221. case BuiltinType::UShort:
  222. case BuiltinType::Int:
  223. case BuiltinType::UInt:
  224. case BuiltinType::Long:
  225. case BuiltinType::ULong:
  226. case BuiltinType::LongLong:
  227. case BuiltinType::ULongLong:
  228. case BuiltinType::WChar_S:
  229. case BuiltinType::WChar_U:
  230. case BuiltinType::Char16:
  231. case BuiltinType::Char32:
  232. return llvm::IntegerType::get(getLLVMContext(),
  233. static_cast<unsigned>(Context.getTypeSize(T)));
  234. case BuiltinType::Float:
  235. case BuiltinType::Double:
  236. case BuiltinType::LongDouble:
  237. return getTypeForFormat(getLLVMContext(),
  238. Context.getFloatTypeSemantics(T));
  239. case BuiltinType::NullPtr: {
  240. // Model std::nullptr_t as i8*
  241. const llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext());
  242. return llvm::PointerType::getUnqual(Ty);
  243. }
  244. case BuiltinType::UInt128:
  245. case BuiltinType::Int128:
  246. return llvm::IntegerType::get(getLLVMContext(), 128);
  247. case BuiltinType::Overload:
  248. case BuiltinType::Dependent:
  249. case BuiltinType::UnknownAny:
  250. llvm_unreachable("Unexpected builtin type!");
  251. break;
  252. }
  253. llvm_unreachable("Unknown builtin type!");
  254. break;
  255. }
  256. case Type::Complex: {
  257. const llvm::Type *EltTy =
  258. ConvertTypeRecursive(cast<ComplexType>(Ty).getElementType());
  259. return llvm::StructType::get(TheModule.getContext(), EltTy, EltTy, NULL);
  260. }
  261. case Type::LValueReference:
  262. case Type::RValueReference: {
  263. const ReferenceType &RTy = cast<ReferenceType>(Ty);
  264. QualType ETy = RTy.getPointeeType();
  265. llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
  266. PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
  267. unsigned AS = Context.getTargetAddressSpace(ETy);
  268. return llvm::PointerType::get(PointeeType, AS);
  269. }
  270. case Type::Pointer: {
  271. const PointerType &PTy = cast<PointerType>(Ty);
  272. QualType ETy = PTy.getPointeeType();
  273. llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
  274. PointersToResolve.push_back(std::make_pair(ETy, PointeeType));
  275. unsigned AS = Context.getTargetAddressSpace(ETy);
  276. return llvm::PointerType::get(PointeeType, AS);
  277. }
  278. case Type::VariableArray: {
  279. const VariableArrayType &A = cast<VariableArrayType>(Ty);
  280. assert(A.getIndexTypeCVRQualifiers() == 0 &&
  281. "FIXME: We only handle trivial array types so far!");
  282. // VLAs resolve to the innermost element type; this matches
  283. // the return of alloca, and there isn't any obviously better choice.
  284. return ConvertTypeForMemRecursive(A.getElementType());
  285. }
  286. case Type::IncompleteArray: {
  287. const IncompleteArrayType &A = cast<IncompleteArrayType>(Ty);
  288. assert(A.getIndexTypeCVRQualifiers() == 0 &&
  289. "FIXME: We only handle trivial array types so far!");
  290. // int X[] -> [0 x int]
  291. return llvm::ArrayType::get(ConvertTypeForMemRecursive(A.getElementType()),
  292. 0);
  293. }
  294. case Type::ConstantArray: {
  295. const ConstantArrayType &A = cast<ConstantArrayType>(Ty);
  296. const llvm::Type *EltTy = ConvertTypeForMemRecursive(A.getElementType());
  297. return llvm::ArrayType::get(EltTy, A.getSize().getZExtValue());
  298. }
  299. case Type::ExtVector:
  300. case Type::Vector: {
  301. const VectorType &VT = cast<VectorType>(Ty);
  302. return llvm::VectorType::get(ConvertTypeRecursive(VT.getElementType()),
  303. VT.getNumElements());
  304. }
  305. case Type::FunctionNoProto:
  306. case Type::FunctionProto: {
  307. // First, check whether we can build the full function type. If the
  308. // function type depends on an incomplete type (e.g. a struct or enum), we
  309. // cannot lower the function type. Instead, turn it into an Opaque pointer
  310. // and have UpdateCompletedType revisit the function type when/if the opaque
  311. // argument type is defined.
  312. if (const TagType *TT = VerifyFuncTypeComplete(&Ty)) {
  313. // This function's type depends on an incomplete tag type; make sure
  314. // we have an opaque type corresponding to the tag type.
  315. ConvertTagDeclType(TT->getDecl());
  316. // Create an opaque type for this function type, save it, and return it.
  317. llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
  318. FunctionTypes.insert(std::make_pair(&Ty, ResultType));
  319. return ResultType;
  320. }
  321. // The function type can be built; call the appropriate routines to
  322. // build it.
  323. const CGFunctionInfo *FI;
  324. bool isVariadic;
  325. if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(&Ty)) {
  326. FI = &getFunctionInfo(
  327. CanQual<FunctionProtoType>::CreateUnsafe(QualType(FPT, 0)),
  328. true /*Recursive*/);
  329. isVariadic = FPT->isVariadic();
  330. } else {
  331. const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(&Ty);
  332. FI = &getFunctionInfo(
  333. CanQual<FunctionNoProtoType>::CreateUnsafe(QualType(FNPT, 0)),
  334. true /*Recursive*/);
  335. isVariadic = true;
  336. }
  337. return GetFunctionType(*FI, isVariadic, true);
  338. }
  339. case Type::ObjCObject:
  340. return ConvertTypeRecursive(cast<ObjCObjectType>(Ty).getBaseType());
  341. case Type::ObjCInterface: {
  342. // Objective-C interfaces are always opaque (outside of the
  343. // runtime, which can do whatever it likes); we never refine
  344. // these.
  345. const llvm::Type *&T = InterfaceTypes[cast<ObjCInterfaceType>(&Ty)];
  346. if (!T)
  347. T = llvm::OpaqueType::get(getLLVMContext());
  348. return T;
  349. }
  350. case Type::ObjCObjectPointer: {
  351. // Protocol qualifications do not influence the LLVM type, we just return a
  352. // pointer to the underlying interface type. We don't need to worry about
  353. // recursive conversion.
  354. const llvm::Type *T =
  355. ConvertTypeRecursive(cast<ObjCObjectPointerType>(Ty).getPointeeType());
  356. return llvm::PointerType::getUnqual(T);
  357. }
  358. case Type::Record:
  359. case Type::Enum: {
  360. const TagDecl *TD = cast<TagType>(Ty).getDecl();
  361. const llvm::Type *Res = ConvertTagDeclType(TD);
  362. addTagTypeName(TD, Res, llvm::StringRef());
  363. return Res;
  364. }
  365. case Type::BlockPointer: {
  366. const QualType FTy = cast<BlockPointerType>(Ty).getPointeeType();
  367. llvm::OpaqueType *PointeeType = llvm::OpaqueType::get(getLLVMContext());
  368. PointersToResolve.push_back(std::make_pair(FTy, PointeeType));
  369. unsigned AS = Context.getTargetAddressSpace(FTy);
  370. return llvm::PointerType::get(PointeeType, AS);
  371. }
  372. case Type::MemberPointer: {
  373. return getCXXABI().ConvertMemberPointerType(cast<MemberPointerType>(&Ty));
  374. }
  375. }
  376. // FIXME: implement.
  377. return llvm::OpaqueType::get(getLLVMContext());
  378. }
  379. /// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
  380. /// enum.
  381. const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) {
  382. // TagDecl's are not necessarily unique, instead use the (clang)
  383. // type connected to the decl.
  384. const Type *Key =
  385. Context.getTagDeclType(TD).getTypePtr();
  386. llvm::DenseMap<const Type*, llvm::PATypeHolder>::iterator TDTI =
  387. TagDeclTypes.find(Key);
  388. // If we've already compiled this tag type, use the previous definition.
  389. if (TDTI != TagDeclTypes.end())
  390. return TDTI->second;
  391. const EnumDecl *ED = dyn_cast<EnumDecl>(TD);
  392. // If this is still a forward declaration, just define an opaque
  393. // type to use for this tagged decl.
  394. // C++0x: If this is a enumeration type with fixed underlying type,
  395. // consider it complete.
  396. if (!TD->isDefinition() && !(ED && ED->isFixed())) {
  397. llvm::Type *ResultType = llvm::OpaqueType::get(getLLVMContext());
  398. TagDeclTypes.insert(std::make_pair(Key, ResultType));
  399. return ResultType;
  400. }
  401. // Okay, this is a definition of a type. Compile the implementation now.
  402. if (ED) // Don't bother storing enums in TagDeclTypes.
  403. return ConvertTypeRecursive(ED->getIntegerType());
  404. // This decl could well be recursive. In this case, insert an opaque
  405. // definition of this type, which the recursive uses will get. We will then
  406. // refine this opaque version later.
  407. // Create new OpaqueType now for later use in case this is a recursive
  408. // type. This will later be refined to the actual type.
  409. llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get(getLLVMContext());
  410. TagDeclTypes.insert(std::make_pair(Key, ResultHolder));
  411. const RecordDecl *RD = cast<const RecordDecl>(TD);
  412. // Force conversion of non-virtual base classes recursively.
  413. if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TD)) {
  414. for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
  415. e = RD->bases_end(); i != e; ++i) {
  416. if (!i->isVirtual()) {
  417. const CXXRecordDecl *Base =
  418. cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
  419. ConvertTagDeclType(Base);
  420. }
  421. }
  422. }
  423. // Layout fields.
  424. CGRecordLayout *Layout = ComputeRecordLayout(RD);
  425. CGRecordLayouts[Key] = Layout;
  426. const llvm::Type *ResultType = Layout->getLLVMType();
  427. // Refine our Opaque type to ResultType. This can invalidate ResultType, so
  428. // make sure to read the result out of the holder.
  429. cast<llvm::OpaqueType>(ResultHolder.get())
  430. ->refineAbstractTypeTo(ResultType);
  431. return ResultHolder.get();
  432. }
  433. /// getCGRecordLayout - Return record layout info for the given record decl.
  434. const CGRecordLayout &
  435. CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
  436. const Type *Key = Context.getTagDeclType(RD).getTypePtr();
  437. const CGRecordLayout *Layout = CGRecordLayouts.lookup(Key);
  438. if (!Layout) {
  439. // Compute the type information.
  440. ConvertTagDeclType(RD);
  441. // Now try again.
  442. Layout = CGRecordLayouts.lookup(Key);
  443. }
  444. assert(Layout && "Unable to find record layout information for type");
  445. return *Layout;
  446. }
  447. void CodeGenTypes::addBaseSubobjectTypeName(const CXXRecordDecl *RD,
  448. const CGRecordLayout &layout) {
  449. llvm::StringRef suffix;
  450. if (layout.getBaseSubobjectLLVMType() != layout.getLLVMType())
  451. suffix = ".base";
  452. addTagTypeName(RD, layout.getBaseSubobjectLLVMType(), suffix);
  453. }
  454. bool CodeGenTypes::isZeroInitializable(QualType T) {
  455. // No need to check for member pointers when not compiling C++.
  456. if (!Context.getLangOptions().CPlusPlus)
  457. return true;
  458. T = Context.getBaseElementType(T);
  459. // Records are non-zero-initializable if they contain any
  460. // non-zero-initializable subobjects.
  461. if (const RecordType *RT = T->getAs<RecordType>()) {
  462. const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
  463. return isZeroInitializable(RD);
  464. }
  465. // We have to ask the ABI about member pointers.
  466. if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
  467. return getCXXABI().isZeroInitializable(MPT);
  468. // Everything else is okay.
  469. return true;
  470. }
  471. bool CodeGenTypes::isZeroInitializable(const CXXRecordDecl *RD) {
  472. return getCGRecordLayout(RD).isZeroInitializable();
  473. }