blob: b91cc8cf41ba2b0f6c92a07d85dcb706f8a77201 [file] [log] [blame]
Mike Stumpc01c2b82009-12-02 18:57:08 +00001//===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===//
Anders Carlsson6ce51fd2009-10-10 20:49:04 +00002//
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 contains code dealing with C++ code generation of RTTI descriptors.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump103a0842009-11-17 23:45:57 +000014#include "clang/AST/Type.h"
Mike Stumpf5b28692009-11-14 23:32:21 +000015#include "clang/AST/RecordLayout.h"
Mike Stumpdb72c892009-11-17 21:44:24 +000016#include "CodeGenModule.h"
Anders Carlsson6ce51fd2009-10-10 20:49:04 +000017using namespace clang;
18using namespace CodeGen;
19
Mike Stumpae1b85d2009-12-02 19:07:44 +000020namespace {
Mike Stumpc01c2b82009-12-02 18:57:08 +000021class RTTIBuilder {
Mike Stump14718422009-11-14 14:25:18 +000022 CodeGenModule &CGM; // Per-module state.
23 llvm::LLVMContext &VMContext;
24 const llvm::Type *Int8PtrTy;
Mike Stump4c808df2009-11-15 03:28:10 +000025 llvm::SmallSet<const CXXRecordDecl *, 16> SeenVBase;
26 llvm::SmallSet<const CXXRecordDecl *, 32> SeenBase;
Anders Carlssond0081292009-12-20 22:30:54 +000027
Anders Carlssone5a94102009-12-17 05:06:03 +000028 std::vector<llvm::Constant *> Info;
Anders Carlssonc9882012009-12-11 01:27:37 +000029
Anders Carlsson3f4336c2009-12-17 07:09:17 +000030 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
31 /// descriptor of the given type.
32 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
33
Anders Carlssond0081292009-12-20 22:30:54 +000034 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
35 llvm::Constant *BuildTypeInfo(QualType Ty);
36
37 /// BuildVtablePointer - Build the vtable pointer for the given type.
38 void BuildVtablePointer(const Type *Ty);
39
Anders Carlssonac2f6812009-12-30 01:00:12 +000040 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
41 /// inheritance, according to the Itanium C++ ABI, 2.95p6b.
42 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
43
44 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
45 /// for pointer types.
Anders Carlssond0081292009-12-20 22:30:54 +000046 void BuildPointerTypeInfo(const PointerType *Ty);
47
48 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
49 /// struct, used for member pointer types.
50 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
51
Mike Stump14718422009-11-14 14:25:18 +000052public:
Mike Stumpc01c2b82009-12-02 18:57:08 +000053 RTTIBuilder(CodeGenModule &cgm)
Mike Stump14718422009-11-14 14:25:18 +000054 : CGM(cgm), VMContext(cgm.getModule().getContext()),
55 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
56
Mike Stumpf5b28692009-11-14 23:32:21 +000057 /// BuildVtableRef - Build a reference to a vtable.
58 llvm::Constant *BuildVtableRef(const char *Name) {
59 // Build a descriptor for Name
Anders Carlssond0081292009-12-20 22:30:54 +000060 llvm::Constant *GV = CGM.getModule().getNamedGlobal(Name);
Mike Stump1acec6a2009-11-14 15:55:18 +000061 if (GV)
62 GV = llvm::ConstantExpr::getBitCast(GV,
63 llvm::PointerType::get(Int8PtrTy, 0));
64 else {
65 llvm::GlobalVariable::LinkageTypes linktype;
66 linktype = llvm::GlobalValue::ExternalLinkage;
67 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy,
68 true, linktype, 0, Name);
Mike Stumpf5b28692009-11-14 23:32:21 +000069 }
Mike Stump1acec6a2009-11-14 15:55:18 +000070 llvm::Constant *C;
71 C = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 2);
72 C = llvm::ConstantExpr::getInBoundsGetElementPtr(GV, &C, 1);
73 return llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
74 }
Mike Stumpf5b28692009-11-14 23:32:21 +000075
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +000076 // FIXME: This should be removed, and clients should pass in the linkage
77 // directly instead.
78 static inline llvm::GlobalVariable::LinkageTypes
79 GetLinkageFromExternFlag(bool Extern) {
80 if (Extern)
81 return llvm::GlobalValue::WeakODRLinkage;
82
83 return llvm::GlobalValue::InternalLinkage;
84 }
85
86 // FIXME: This should be removed, and clients should pass in the linkage
87 // directly instead.
Mike Stump1a139f82009-11-19 01:08:19 +000088 llvm::Constant *BuildName(QualType Ty, bool Hidden, bool Extern) {
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +000089 return BuildName(Ty, Hidden, GetLinkageFromExternFlag(Extern));
90 }
91
92 llvm::Constant *BuildName(QualType Ty, bool Hidden,
93 llvm::GlobalVariable::LinkageTypes Linkage) {
Mike Stump14718422009-11-14 14:25:18 +000094 llvm::SmallString<256> OutName;
Mike Stumpc01c2b82009-12-02 18:57:08 +000095 CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbare128dd12009-11-21 09:06:22 +000096 llvm::StringRef Name = OutName.str();
Mike Stumpf5b28692009-11-14 23:32:21 +000097
Anders Carlssond0081292009-12-20 22:30:54 +000098 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +000099 if (OGV && !OGV->isDeclaration())
100 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump1a139f82009-11-19 01:08:19 +0000101
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000102 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump14718422009-11-14 14:25:18 +0000103
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000104 llvm::GlobalVariable *GV =
105 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
106 C, Name);
Mike Stump1a139f82009-11-19 01:08:19 +0000107 if (OGV) {
108 GV->takeName(OGV);
109 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
110 OGV->getType());
111 OGV->replaceAllUsesWith(NewPtr);
112 OGV->eraseFromParent();
113 }
Mike Stump83d5e002009-11-18 03:46:51 +0000114 if (Hidden)
115 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
116 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbare017ecc2009-12-19 17:50:07 +0000117 }
Mike Stump1acec6a2009-11-14 15:55:18 +0000118
Mike Stumpf5b28692009-11-14 23:32:21 +0000119 /// - BuildFlags - Build a psABI __flags value for __vmi_class_type_info.
120 llvm::Constant *BuildFlags(int f) {
121 return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), f);
122 }
123
124 /// BuildBaseCount - Build a psABI __base_count value for
125 /// __vmi_class_type_info.
126 llvm::Constant *BuildBaseCount(unsigned c) {
127 return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), c);
128 }
129
Mike Stump4c808df2009-11-15 03:28:10 +0000130 /// CalculateFlags - Calculate the flags for the __vmi_class_type_info
131 /// datastructure. 1 for non-diamond repeated inheritance, 2 for a dimond
132 /// shaped class.
Anders Carlssond0081292009-12-20 22:30:54 +0000133 int CalculateFlags(const CXXRecordDecl *RD) {
Mike Stump4c808df2009-11-15 03:28:10 +0000134 int flags = 0;
135 if (SeenBase.count(RD))
136 flags |= 1;
137 else
138 SeenBase.insert(RD);
139 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
140 e = RD->bases_end(); i != e; ++i) {
141 const CXXRecordDecl *Base =
142 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
143 if (i->isVirtual()) {
144 if (SeenVBase.count(Base))
145 flags |= 2;
146 else
147 SeenVBase.insert(Base);
148 }
149 flags |= CalculateFlags(Base);
150 }
151 return flags;
152 }
153
154 bool SimpleInheritance(const CXXRecordDecl *RD) {
155 if (RD->getNumBases() != 1)
156 return false;
157 CXXRecordDecl::base_class_const_iterator i = RD->bases_begin();
158 if (i->isVirtual())
159 return false;
160 if (i->getAccessSpecifier() != AS_public)
161 return false;
162
163 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
164 const CXXRecordDecl *Base =
165 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
166 if (Layout.getBaseClassOffset(Base) != 0)
167 return false;
168 return true;
169 }
170
Anders Carlsson1fd73422009-12-17 05:10:59 +0000171 llvm::Constant *finish(llvm::GlobalVariable *GV,
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000172 llvm::StringRef Name, bool Hidden,
173 llvm::GlobalVariable::LinkageTypes Linkage) {
174 llvm::Constant *C =
Anders Carlsson1fd73422009-12-17 05:10:59 +0000175 llvm::ConstantStruct::get(VMContext, &Info[0], Info.size(),
176 /*Packed=*/false);
Mike Stump96b96d52009-11-17 23:11:22 +0000177
Mike Stump1a139f82009-11-19 01:08:19 +0000178 llvm::GlobalVariable *OGV = GV;
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000179 GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
Mike Stump1a139f82009-11-19 01:08:19 +0000180 C, Name);
181 if (OGV) {
Mike Stump96b96d52009-11-17 23:11:22 +0000182 GV->takeName(OGV);
183 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
184 OGV->getType());
185 OGV->replaceAllUsesWith(NewPtr);
186 OGV->eraseFromParent();
187 }
Mike Stump83d5e002009-11-18 03:46:51 +0000188 if (Hidden)
Mike Stumpc5d2ed72009-11-18 03:21:29 +0000189 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
Mike Stump96b96d52009-11-17 23:11:22 +0000190 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
191 }
192
193
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000194 llvm::Constant *
195 Buildclass_type_info(const CXXRecordDecl *RD,
196 llvm::GlobalVariable::LinkageTypes Linkage) {
Anders Carlssone5a94102009-12-17 05:06:03 +0000197 assert(Info.empty() && "Info vector must be empty!");
Anders Carlsson1d6ad502009-12-11 16:41:51 +0000198
Mike Stump96b96d52009-11-17 23:11:22 +0000199 llvm::Constant *C;
200
Mike Stumpf5b28692009-11-14 23:32:21 +0000201 llvm::SmallString<256> OutName;
Mike Stumpc01c2b82009-12-02 18:57:08 +0000202 CGM.getMangleContext().mangleCXXRTTI(CGM.getContext().getTagDeclType(RD),
Daniel Dunbare128dd12009-11-21 09:06:22 +0000203 OutName);
204 llvm::StringRef Name = OutName.str();
Mike Stumpf5b28692009-11-14 23:32:21 +0000205
206 llvm::GlobalVariable *GV;
Anders Carlssond0081292009-12-20 22:30:54 +0000207 GV = CGM.getModule().getNamedGlobal(Name);
Mike Stumpf5b28692009-11-14 23:32:21 +0000208 if (GV && !GV->isDeclaration())
209 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
210
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000211 // If we're in an anonymous namespace, then we always want internal linkage.
Eli Friedman3ace52b2009-12-11 20:48:18 +0000212 if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000213 Linkage = llvm::GlobalVariable::InternalLinkage;
214
Mike Stump83d5e002009-11-18 03:46:51 +0000215 bool Hidden = CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
216
Mike Stump4c808df2009-11-15 03:28:10 +0000217 bool simple = false;
Mike Stumpf5b28692009-11-14 23:32:21 +0000218 if (RD->getNumBases() == 0)
219 C = BuildVtableRef("_ZTVN10__cxxabiv117__class_type_infoE");
Mike Stump4c808df2009-11-15 03:28:10 +0000220 else if (SimpleInheritance(RD)) {
221 simple = true;
222 C = BuildVtableRef("_ZTVN10__cxxabiv120__si_class_type_infoE");
223 } else
Mike Stumpf5b28692009-11-14 23:32:21 +0000224 C = BuildVtableRef("_ZTVN10__cxxabiv121__vmi_class_type_infoE");
Anders Carlssone5a94102009-12-17 05:06:03 +0000225 Info.push_back(C);
226 Info.push_back(BuildName(CGM.getContext().getTagDeclType(RD), Hidden,
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000227 Linkage));
Mike Stump1acec6a2009-11-14 15:55:18 +0000228
Mike Stumpf5b28692009-11-14 23:32:21 +0000229 // If we have no bases, there are no more fields.
230 if (RD->getNumBases()) {
Mike Stump4c808df2009-11-15 03:28:10 +0000231 if (!simple) {
Anders Carlssone5a94102009-12-17 05:06:03 +0000232 Info.push_back(BuildFlags(CalculateFlags(RD)));
233 Info.push_back(BuildBaseCount(RD->getNumBases()));
Mike Stump4c808df2009-11-15 03:28:10 +0000234 }
Mike Stumpf5b28692009-11-14 23:32:21 +0000235
236 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
237 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
238 e = RD->bases_end(); i != e; ++i) {
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000239 QualType BaseType = i->getType();
Mike Stumpf5b28692009-11-14 23:32:21 +0000240 const CXXRecordDecl *Base =
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000241 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
242 Info.push_back(CGM.GetAddrOfRTTIDescriptor(BaseType));
Mike Stump4c808df2009-11-15 03:28:10 +0000243 if (simple)
244 break;
Mike Stumpf5b28692009-11-14 23:32:21 +0000245 int64_t offset;
246 if (!i->isVirtual())
Mike Stump4c808df2009-11-15 03:28:10 +0000247 offset = Layout.getBaseClassOffset(Base)/8;
Mike Stumpf5b28692009-11-14 23:32:21 +0000248 else
249 offset = CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base);
250 offset <<= 8;
251 // Now set the flags.
252 offset += i->isVirtual() ? 1 : 0;;
253 offset += i->getAccessSpecifier() == AS_public ? 2 : 0;
254 const llvm::Type *LongTy =
255 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
256 C = llvm::ConstantInt::get(LongTy, offset);
Anders Carlssone5a94102009-12-17 05:06:03 +0000257 Info.push_back(C);
Mike Stumpf5b28692009-11-14 23:32:21 +0000258 }
259 }
260
Anders Carlsson1fd73422009-12-17 05:10:59 +0000261 return finish(GV, Name, Hidden, Linkage);
Mike Stump1acec6a2009-11-14 15:55:18 +0000262 }
Mike Stump3f75d552009-11-17 02:16:21 +0000263
Mike Stumpdb72c892009-11-17 21:44:24 +0000264 /// - BuildFlags - Build a __flags value for __pbase_type_info.
Anders Carlssonc9882012009-12-11 01:27:37 +0000265 llvm::Constant *BuildInt(unsigned n) {
266 return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), n);
Mike Stumpdb72c892009-11-17 21:44:24 +0000267 }
268
Mike Stumpbb2f57d2009-12-24 02:33:48 +0000269 // FIXME: unify with DecideExtern
Mike Stump1a139f82009-11-19 01:08:19 +0000270 bool DecideHidden(QualType Ty) {
271 // For this type, see if all components are never hidden.
272 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
273 return (DecideHidden(MPT->getPointeeType())
274 && DecideHidden(QualType(MPT->getClass(), 0)));
275 if (const PointerType *PT = Ty->getAs<PointerType>())
276 return DecideHidden(PT->getPointeeType());
Mike Stumpbb2f57d2009-12-24 02:33:48 +0000277 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
278 if (DecideHidden(FT->getResultType()) == false)
279 return false;
280 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
281 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
282 if (DecideHidden(FPT->getArgType(i)) == false)
283 return false;
284 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
285 if (DecideHidden(FPT->getExceptionType(i)) == false)
286 return false;
287 return true;
288 }
289 }
Mike Stump1a139f82009-11-19 01:08:19 +0000290 if (const RecordType *RT = Ty->getAs<RecordType>())
291 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
292 return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
293 return false;
294 }
295
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000296 /// BuildType - Builds the type info for the given type.
Mike Stump3f75d552009-11-17 02:16:21 +0000297 llvm::Constant *BuildType(QualType Ty) {
298 const clang::Type &Type
299 = *CGM.getContext().getCanonicalType(Ty).getTypePtr();
Mike Stump101f0522009-11-20 00:31:50 +0000300
Mike Stump3f75d552009-11-17 02:16:21 +0000301 switch (Type.getTypeClass()) {
302 default: {
Mike Stump3f75d552009-11-17 02:16:21 +0000303 assert(0 && "typeid expression");
Mike Stump3f75d552009-11-17 02:16:21 +0000304 return llvm::Constant::getNullValue(Int8PtrTy);
305 }
306
307 case Type::Builtin: {
308 // We expect all type_info objects for builtin types to be in the library.
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000309 return GetAddrOfExternalRTTIDescriptor(Ty);
Mike Stump3f75d552009-11-17 02:16:21 +0000310 }
Mike Stump8f5e6772009-11-17 02:57:13 +0000311
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000312 case Type::Record: {
313 const RecordType *RT = cast<RecordType>(&Type);
314
315 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Anders Carlssonac2f6812009-12-30 01:00:12 +0000316 if (RD->getNumBases() != 0 && !SimpleInheritance(RD))
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000317 return BuildClassTypeInfo(RD);
318
319 // Fall through.
320 }
321
Anders Carlssond0081292009-12-20 22:30:54 +0000322 case Type::Pointer:
Mike Stump6fdfea62009-11-17 22:33:00 +0000323 case Type::MemberPointer:
Mike Stump96b96d52009-11-17 23:11:22 +0000324 case Type::FunctionProto:
Mike Stump103a0842009-11-17 23:45:57 +0000325 case Type::FunctionNoProto:
Mike Stump103a0842009-11-17 23:45:57 +0000326 case Type::ConstantArray:
327 case Type::IncompleteArray:
328 case Type::VariableArray:
Anders Carlssonef886952009-12-29 22:13:01 +0000329 case Type::Enum:
Mike Stump103a0842009-11-17 23:45:57 +0000330 case Type::Vector:
331 case Type::ExtVector:
Anders Carlsson79c184e2009-12-29 22:30:11 +0000332 return BuildTypeInfo(Ty);
Mike Stump3f75d552009-11-17 02:16:21 +0000333 }
334 }
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000335
336 /// BuildClassTypeInfo - Builds the class type info (or a reference to it)
337 /// for the given record decl.
338 llvm::Constant *BuildClassTypeInfo(const CXXRecordDecl *RD) {
339 const CXXMethodDecl *KeyFunction = 0;
340
341 if (RD->isDynamicClass())
342 KeyFunction = CGM.getContext().getKeyFunction(RD);
343
344 if (KeyFunction) {
345 // If the key function is defined in this translation unit, then the RTTI
346 // related constants should also be emitted here, with external linkage.
347 if (KeyFunction->getBody())
348 return Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage);
349
350 // Otherwise, we just want a reference to the type info.
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000351 QualType Ty = CGM.getContext().getTagDeclType(RD);
352 return GetAddrOfExternalRTTIDescriptor(Ty);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000353 }
354
355 // If there is no key function (or if the record doesn't have any virtual
356 // member functions or virtual bases), emit the type info with weak_odr
357 // linkage.
358 return Buildclass_type_info(RD, llvm::GlobalValue::WeakODRLinkage);
359 }
Anders Carlssond0081292009-12-20 22:30:54 +0000360
361 // Pointer type info flags.
362 enum {
363 /// PTI_Const - Type has const qualifier.
364 PTI_Const = 0x1,
365
366 /// PTI_Volatile - Type has volatile qualifier.
367 PTI_Volatile = 0x2,
368
369 /// PTI_Restrict - Type has restrict qualifier.
370 PTI_Restrict = 0x4,
371
372 /// PTI_Incomplete - Type is incomplete.
373 PTI_Incomplete = 0x8,
374
375 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
376 /// (in pointer to member).
377 PTI_ContainingClassIncomplete = 0x10
378 };
Mike Stump14718422009-11-14 14:25:18 +0000379};
Mike Stumpae1b85d2009-12-02 19:07:44 +0000380}
Mike Stump14718422009-11-14 14:25:18 +0000381
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000382llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
383 // Mangle the RTTI name.
384 llvm::SmallString<256> OutName;
385 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
386 llvm::StringRef Name = OutName.str();
387
Anders Carlssond0081292009-12-20 22:30:54 +0000388 // Look for an existing global.
389 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000390
391 if (!GV) {
392 // Create a new global variable.
393 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
394 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000395 }
396
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000397 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000398}
399
Anders Carlssond0081292009-12-20 22:30:54 +0000400/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
401/// info for that type is defined in the standard library.
402static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
403 // Itanium C++ ABI 2.9.2:
404 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
405 // the run-time support library. Specifically, the run-time support
406 // library should contain type_info objects for the types X, X* and
407 // X const*, for every X in: void, bool, wchar_t, char, unsigned char,
408 // signed char, short, unsigned short, int, unsigned int, long,
409 // unsigned long, long long, unsigned long long, float, double, long double,
410 // char16_t, char32_t, and the IEEE 754r decimal and half-precision
411 // floating point types.
412 switch (Ty->getKind()) {
413 case BuiltinType::Void:
414 case BuiltinType::Bool:
415 case BuiltinType::WChar:
416 case BuiltinType::Char_U:
417 case BuiltinType::Char_S:
418 case BuiltinType::UChar:
419 case BuiltinType::SChar:
420 case BuiltinType::Short:
421 case BuiltinType::UShort:
422 case BuiltinType::Int:
423 case BuiltinType::UInt:
424 case BuiltinType::Long:
425 case BuiltinType::ULong:
426 case BuiltinType::LongLong:
427 case BuiltinType::ULongLong:
428 case BuiltinType::Float:
429 case BuiltinType::Double:
430 case BuiltinType::LongDouble:
431 case BuiltinType::Char16:
432 case BuiltinType::Char32:
433 case BuiltinType::Int128:
434 case BuiltinType::UInt128:
435 return true;
436
437 case BuiltinType::Overload:
438 case BuiltinType::Dependent:
439 case BuiltinType::UndeducedAuto:
440 assert(false && "Should not see this type here!");
441
442 case BuiltinType::NullPtr:
443 assert(false && "FIXME: nullptr_t is not handled!");
444
445 case BuiltinType::ObjCId:
446 case BuiltinType::ObjCClass:
447 case BuiltinType::ObjCSel:
448 assert(false && "FIXME: Objective-C types are unsupported!");
449 }
450
451 // Silent gcc.
452 return false;
453}
454
455static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
456 QualType PointeeTy = PointerTy->getPointeeType();
457 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
458 if (!BuiltinTy)
459 return false;
460
461 // Check the qualifiers.
462 Qualifiers Quals = PointeeTy.getQualifiers();
463 Quals.removeConst();
464
465 if (!Quals.empty())
466 return false;
467
468 return TypeInfoIsInStandardLibrary(BuiltinTy);
469}
470
471/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
472/// the given type exists somewhere else, and that we should not emit the typ
473/// information in this translation unit.
474bool ShouldUseExternalRTTIDescriptor(QualType Ty) {
475 // Type info for builtin types is defined in the standard library.
476 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
477 return TypeInfoIsInStandardLibrary(BuiltinTy);
478
479 // Type info for some pointer types to builtin types is defined in the
480 // standard library.
481 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
482 return TypeInfoIsInStandardLibrary(PointerTy);
483
484 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000485 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
486 if (!RD->isDynamicClass())
487 return false;
488
489 // Get the key function.
490 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
491 if (KeyFunction && !KeyFunction->getBody()) {
492 // The class has a key function, but it is not defined in this translation
493 // unit, so we should use the external descriptor for it.
494 return true;
495 }
Anders Carlssond0081292009-12-20 22:30:54 +0000496 }
497
498 return false;
499}
500
501/// IsIncompleteClassType - Returns whether the given record type is incomplete.
502static bool IsIncompleteClassType(const RecordType *RecordTy) {
503 return !RecordTy->getDecl()->isDefinition();
504}
505
Anders Carlsson18e6ee12009-12-20 23:37:55 +0000506/// ContainsIncompleteClassType - Returns whether the given type contains an
507/// incomplete class type. This is true if
508///
509/// * The given type is an incomplete class type.
510/// * The given type is a pointer type whose pointee type contains an
511/// incomplete class type.
512/// * The given type is a member pointer type whose class is an incomplete
513/// class type.
514/// * The given type is a member pointer type whoise pointee type contains an
515/// incomplete class type.
Anders Carlssond0081292009-12-20 22:30:54 +0000516/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson18e6ee12009-12-20 23:37:55 +0000517static bool ContainsIncompleteClassType(QualType Ty) {
518 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
519 if (IsIncompleteClassType(RecordTy))
520 return true;
521 }
522
523 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
524 return ContainsIncompleteClassType(PointerTy->getPointeeType());
525
526 if (const MemberPointerType *MemberPointerTy =
527 dyn_cast<MemberPointerType>(Ty)) {
528 // Check if the class type is incomplete.
529 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
530 if (IsIncompleteClassType(ClassType))
531 return true;
532
533 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlssond0081292009-12-20 22:30:54 +0000534 }
535
536 return false;
537}
538
539/// getTypeInfoLinkage - Return the linkage that the type info and type info
540/// name constants should have for the given type.
541static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson18e6ee12009-12-20 23:37:55 +0000542 // Itanium C++ ABI 2.9.5p7:
543 // In addition, it and all of the intermediate abi::__pointer_type_info
544 // structs in the chain down to the abi::__class_type_info for the
545 // incomplete class type must be prevented from resolving to the
546 // corresponding type_info structs for the complete class type, possibly
547 // by making them local static objects. Finally, a dummy class RTTI is
548 // generated for the incomplete type that will not resolve to the final
549 // complete class RTTI (because the latter need not exist), possibly by
550 // making it a local static object.
551 if (ContainsIncompleteClassType(Ty))
552 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000553
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000554 switch (Ty->getTypeClass()) {
555 default:
556 // FIXME: We need to add code to handle all types.
557 assert(false && "Unhandled type!");
558 break;
559
560 case Type::Pointer: {
561 const PointerType *PointerTy = cast<PointerType>(Ty);
562
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000563 // If the pointee type has internal linkage, then the pointer type needs to
564 // have it as well.
565 if (getTypeInfoLinkage(PointerTy->getPointeeType()) ==
566 llvm::GlobalVariable::InternalLinkage)
567 return llvm::GlobalVariable::InternalLinkage;
568
569 return llvm::GlobalVariable::WeakODRLinkage;
Anders Carlssonef886952009-12-29 22:13:01 +0000570 }
571
572 case Type::Enum: {
573 const EnumType *EnumTy = cast<EnumType>(Ty);
574 const EnumDecl *ED = EnumTy->getDecl();
575
576 // If we're in an anonymous namespace, then we always want internal linkage.
577 if (ED->isInAnonymousNamespace() || !ED->hasLinkage())
578 return llvm::GlobalVariable::InternalLinkage;
579
580 return llvm::GlobalValue::WeakODRLinkage;
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000581 }
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000582
583 case Type::Record: {
584 const RecordType *RecordTy = cast<RecordType>(Ty);
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000585 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
586
587 // If we're in an anonymous namespace, then we always want internal linkage.
588 if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
589 return llvm::GlobalVariable::InternalLinkage;
590
591 if (!RD->isDynamicClass())
592 return llvm::GlobalValue::WeakODRLinkage;
593
594 // Get the key function.
595 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
596 if (!KeyFunction) {
597 // There is no key function, the RTTI descriptor is emitted with weak_odr
598 // linkage.
599 return llvm::GlobalValue::WeakODRLinkage;
600 }
601
602 // Otherwise, the RTTI descriptor is emitted with external linkage.
603 return llvm::GlobalValue::ExternalLinkage;
604 }
605
Anders Carlsson79c184e2009-12-29 22:30:11 +0000606 case Type::Vector:
607 case Type::ExtVector:
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000608 case Type::Builtin:
Mike Stumpf3c23c02009-12-23 22:48:20 +0000609 return llvm::GlobalValue::WeakODRLinkage;
Mike Stumpf3c23c02009-12-23 22:48:20 +0000610
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000611 case Type::FunctionProto: {
612 const FunctionProtoType *FPT = cast<FunctionProtoType>(Ty);
613
614 // Check the return type.
615 if (getTypeInfoLinkage(FPT->getResultType()) ==
616 llvm::GlobalValue::InternalLinkage)
Mike Stumpa8a2a332009-12-24 01:10:27 +0000617 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000618
619 // Check the parameter types.
620 for (unsigned i = 0; i != FPT->getNumArgs(); ++i) {
621 if (getTypeInfoLinkage(FPT->getArgType(i)) ==
622 llvm::GlobalValue::InternalLinkage)
623 return llvm::GlobalValue::InternalLinkage;
Mike Stumpa8a2a332009-12-24 01:10:27 +0000624 }
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000625
Mike Stumpa8a2a332009-12-24 01:10:27 +0000626 return llvm::GlobalValue::WeakODRLinkage;
627 }
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000628
629 case Type::ConstantArray:
630 case Type::IncompleteArray: {
631 const ArrayType *AT = cast<ArrayType>(Ty);
Mike Stumpa8a2a332009-12-24 01:10:27 +0000632
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000633 // Check the element type.
634 if (getTypeInfoLinkage(AT->getElementType()) ==
635 llvm::GlobalValue::InternalLinkage)
636 return llvm::GlobalValue::InternalLinkage;
637 }
638
639 }
640
Anders Carlssond0081292009-12-20 22:30:54 +0000641 return llvm::GlobalValue::WeakODRLinkage;
642}
643
Anders Carlssonac2f6812009-12-30 01:00:12 +0000644// CanUseSingleInheritance - Return whether the given record decl has a "single,
645// public, non-virtual base at offset zero (i.e. the derived class is dynamic
646// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
647static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
648 // Check the number of bases.
649 if (RD->getNumBases() != 1)
650 return false;
651
652 // Get the base.
653 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
654
655 // Check that the base is not virtual.
656 if (Base->isVirtual())
657 return false;
658
659 // Check that the base is public.
660 if (Base->getAccessSpecifier() != AS_public)
661 return false;
662
663 // Check that the class is dynamic iff the base is.
664 const CXXRecordDecl *BaseDecl =
665 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
666 if (!BaseDecl->isEmpty() &&
667 BaseDecl->isDynamicClass() != RD->isDynamicClass())
668 return false;
669
670 return true;
671}
672
Anders Carlssond0081292009-12-20 22:30:54 +0000673void RTTIBuilder::BuildVtablePointer(const Type *Ty) {
674 const char *VtableName;
675
676 switch (Ty->getTypeClass()) {
677 default: assert(0 && "Unhandled type!");
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000678
Anders Carlsson79c184e2009-12-29 22:30:11 +0000679 // GCC treats vector types as fundamental types.
680 case Type::Vector:
681 case Type::ExtVector:
682 // abi::__fundamental_type_info
683 VtableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
684 break;
685
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000686 case Type::ConstantArray:
687 case Type::IncompleteArray:
688 // abi::__array_type_info
689 VtableName = "_ZTVN10__cxxabiv117__array_type_infoE";
690 break;
691
692 case Type::FunctionNoProto:
693 case Type::FunctionProto:
694 // abi::__function_type_info
695 VtableName = "_ZTVN10__cxxabiv120__function_type_infoE";
696 break;
697
Anders Carlssonef886952009-12-29 22:13:01 +0000698 case Type::Enum:
699 // abi::__enum_type_info
700 VtableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
701 break;
702
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000703 case Type::Record: {
704 const CXXRecordDecl *RD =
705 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
706 if (!RD->getNumBases()) {
707 // abi::__class_type_info
708 VtableName = "_ZTVN10__cxxabiv117__class_type_infoE";
Anders Carlssonac2f6812009-12-30 01:00:12 +0000709 } else if (CanUseSingleInheritance(RD)) {
710 // abi::__si_class_type_info;
711 VtableName = "_ZTVN10__cxxabiv120__si_class_type_infoE";
712 } else {
713 assert(false && "Should not get here!");
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000714 }
Anders Carlssonac2f6812009-12-30 01:00:12 +0000715
716 break;
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000717 }
718
Anders Carlssond0081292009-12-20 22:30:54 +0000719 case Type::Pointer:
720 // abi::__pointer_type_info
721 VtableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
722 break;
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000723
Anders Carlssond0081292009-12-20 22:30:54 +0000724 case Type::MemberPointer:
725 // abi::__pointer_to_member_type_info
Anders Carlsson26cf4ab2009-12-29 20:20:19 +0000726 VtableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlssond0081292009-12-20 22:30:54 +0000727 break;
728 }
729
730 llvm::Constant *Vtable =
731 CGM.getModule().getOrInsertGlobal(VtableName, Int8PtrTy);
732
733 const llvm::Type *PtrDiffTy =
734 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
735
736 // The vtable address point is 2.
737 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
738 Vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, &Two, 1);
739 Vtable = llvm::ConstantExpr::getBitCast(Vtable, Int8PtrTy);
740
741 Info.push_back(Vtable);
742}
743
744llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty) {
745 // We want to operate on the canonical type.
746 Ty = CGM.getContext().getCanonicalType(Ty);
747
748 // Check if we've already emitted an RTTI descriptor for this type.
749 llvm::SmallString<256> OutName;
750 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
751 llvm::StringRef Name = OutName.str();
752
753 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
754 if (OldGV && !OldGV->isDeclaration())
755 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
756
757 // Check if there is already an external RTTI descriptor for this type.
758 if (ShouldUseExternalRTTIDescriptor(Ty))
759 return GetAddrOfExternalRTTIDescriptor(Ty);
760
761 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty);
762
763 // Add the vtable pointer.
764 BuildVtablePointer(cast<Type>(Ty));
765
766 // And the name.
767 Info.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
768
769 switch (Ty->getTypeClass()) {
770 default: assert(false && "Unhandled type class!");
771 case Type::Builtin:
772 assert(false && "Builtin type info must be in the standard library!");
773 break;
774
Anders Carlsson79c184e2009-12-29 22:30:11 +0000775 // GCC treats vector types as fundamental types.
776 case Type::Vector:
777 case Type::ExtVector:
778 // Itanium C++ ABI 2.9.5p4:
779 // abi::__fundamental_type_info adds no data members to std::type_info.
780 break;
781
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000782 case Type::ConstantArray:
783 case Type::IncompleteArray:
Anders Carlsson79c184e2009-12-29 22:30:11 +0000784 // Itanium C++ ABI 2.9.5p5:
785 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson0e4151c2009-12-29 21:58:32 +0000786 break;
787
Anders Carlsson26cf4ab2009-12-29 20:20:19 +0000788 case Type::FunctionNoProto:
789 case Type::FunctionProto:
Anders Carlsson79c184e2009-12-29 22:30:11 +0000790 // Itanium C++ ABI 2.9.5p5:
791 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson26cf4ab2009-12-29 20:20:19 +0000792 break;
793
Anders Carlssonef886952009-12-29 22:13:01 +0000794 case Type::Enum:
Anders Carlsson79c184e2009-12-29 22:30:11 +0000795 // Itanium C++ ABI 2.9.5p5:
796 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlssonef886952009-12-29 22:13:01 +0000797 break;
798
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000799 case Type::Record: {
800 const CXXRecordDecl *RD =
801 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
802 if (!RD->getNumBases()) {
803 // We don't need to emit any fields.
804 break;
805 }
Anders Carlssonac2f6812009-12-30 01:00:12 +0000806
807 if (CanUseSingleInheritance(RD)) {
808 BuildSIClassTypeInfo(RD);
809 break;
810 }
Anders Carlsson2b7f44432009-12-21 00:41:42 +0000811 }
812
Anders Carlssond0081292009-12-20 22:30:54 +0000813 case Type::Pointer:
814 BuildPointerTypeInfo(cast<PointerType>(Ty));
815 break;
816
817 case Type::MemberPointer:
818 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
819 break;
820 }
821
822 llvm::Constant *Init =
823 llvm::ConstantStruct::get(VMContext, &Info[0], Info.size(),
824 /*Packed=*/false);
825
826 llvm::GlobalVariable *GV =
827 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
828 /*Constant=*/true, Linkage, Init, Name);
829
830 // If there's already an old global variable, replace it with the new one.
831 if (OldGV) {
832 GV->takeName(OldGV);
833 llvm::Constant *NewPtr =
834 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
835 OldGV->replaceAllUsesWith(NewPtr);
836 OldGV->eraseFromParent();
837 }
838
839 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
840}
841
842/// DetermineQualifierFlags - Deterine the pointer type info flags from the
843/// given qualifier.
844static unsigned DetermineQualifierFlags(Qualifiers Quals) {
845 unsigned Flags = 0;
846
847 if (Quals.hasConst())
848 Flags |= RTTIBuilder::PTI_Const;
849 if (Quals.hasVolatile())
850 Flags |= RTTIBuilder::PTI_Volatile;
851 if (Quals.hasRestrict())
852 Flags |= RTTIBuilder::PTI_Restrict;
853
854 return Flags;
855}
856
Anders Carlssonac2f6812009-12-30 01:00:12 +0000857/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
858/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
859void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
860 // Itanium C++ ABI 2.9.5p6b:
861 // It adds to abi::__class_type_info a single member pointing to the
862 // type_info structure for the base type,
863 Info.push_back(RTTIBuilder(CGM).BuildType(RD->bases_begin()->getType()));
864}
865
Anders Carlssond0081292009-12-20 22:30:54 +0000866/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
867/// used for pointer types.
868void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
Anders Carlsson18e6ee12009-12-20 23:37:55 +0000869 QualType PointeeTy = Ty->getPointeeType();
Anders Carlssond0081292009-12-20 22:30:54 +0000870
871 // Itanium C++ ABI 2.9.5p7:
872 // __flags is a flag word describing the cv-qualification and other
873 // attributes of the type pointed to
874 unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers());
875
876 // Itanium C++ ABI 2.9.5p7:
877 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
878 // incomplete class type, the incomplete target type flag is set.
Anders Carlsson18e6ee12009-12-20 23:37:55 +0000879 if (ContainsIncompleteClassType(PointeeTy))
Anders Carlssond0081292009-12-20 22:30:54 +0000880 Flags |= PTI_Incomplete;
881
882 const llvm::Type *UnsignedIntLTy =
883 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
884 Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
885
886 // Itanium C++ ABI 2.9.5p7:
887 // __pointee is a pointer to the std::type_info derivation for the
888 // unqualified type being pointed to.
889 Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType()));
890}
891
892/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
893/// struct, used for member pointer types.
894void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
895 QualType PointeeTy = Ty->getPointeeType();
896
897 // Itanium C++ ABI 2.9.5p7:
898 // __flags is a flag word describing the cv-qualification and other
899 // attributes of the type pointed to.
900 unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers());
901
902 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson18e6ee12009-12-20 23:37:55 +0000903
904 // Itanium C++ ABI 2.9.5p7:
905 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
906 // incomplete class type, the incomplete target type flag is set.
907 if (ContainsIncompleteClassType(PointeeTy))
908 Flags |= PTI_Incomplete;
909
Anders Carlssond0081292009-12-20 22:30:54 +0000910 if (IsIncompleteClassType(ClassType))
911 Flags |= PTI_ContainingClassIncomplete;
912
Anders Carlssond0081292009-12-20 22:30:54 +0000913 const llvm::Type *UnsignedIntLTy =
914 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
915 Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
916
917 // Itanium C++ ABI 2.9.5p7:
918 // __pointee is a pointer to the std::type_info derivation for the
919 // unqualified type being pointed to.
920 Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType()));
921
922 // Itanium C++ ABI 2.9.5p9:
923 // __context is a pointer to an abi::__class_type_info corresponding to the
924 // class type containing the member pointed to
925 // (e.g., the "A" in "int A::*").
926 Info.push_back(RTTIBuilder(CGM).BuildType(QualType(ClassType, 0)));
927}
928
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000929llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty) {
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000930 if (!getContext().getLangOptions().RTTI) {
931 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
932 return llvm::Constant::getNullValue(Int8PtrTy);
933 }
934
935 return RTTIBuilder(*this).BuildType(Ty);
936}