clang API Documentation
00001 //===--- VTableBuilder.cpp - C++ vtable layout builder --------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This contains code dealing with generation of the layout of virtual tables. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/AST/VTableBuilder.h" 00015 #include "clang/AST/CXXInheritance.h" 00016 #include "clang/AST/RecordLayout.h" 00017 #include "clang/Basic/TargetInfo.h" 00018 #include "llvm/Support/Format.h" 00019 #include <algorithm> 00020 #include <cstdio> 00021 00022 using namespace clang; 00023 00024 #define DUMP_OVERRIDERS 0 00025 00026 namespace { 00027 00028 /// BaseOffset - Represents an offset from a derived class to a direct or 00029 /// indirect base class. 00030 struct BaseOffset { 00031 /// DerivedClass - The derived class. 00032 const CXXRecordDecl *DerivedClass; 00033 00034 /// VirtualBase - If the path from the derived class to the base class 00035 /// involves a virtual base class, this holds its declaration. 00036 const CXXRecordDecl *VirtualBase; 00037 00038 /// NonVirtualOffset - The offset from the derived class to the base class. 00039 /// (Or the offset from the virtual base class to the base class, if the 00040 /// path from the derived class to the base class involves a virtual base 00041 /// class. 00042 CharUnits NonVirtualOffset; 00043 00044 BaseOffset() : DerivedClass(0), VirtualBase(0), 00045 NonVirtualOffset(CharUnits::Zero()) { } 00046 BaseOffset(const CXXRecordDecl *DerivedClass, 00047 const CXXRecordDecl *VirtualBase, CharUnits NonVirtualOffset) 00048 : DerivedClass(DerivedClass), VirtualBase(VirtualBase), 00049 NonVirtualOffset(NonVirtualOffset) { } 00050 00051 bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; } 00052 }; 00053 00054 /// FinalOverriders - Contains the final overrider member functions for all 00055 /// member functions in the base subobjects of a class. 00056 class FinalOverriders { 00057 public: 00058 /// OverriderInfo - Information about a final overrider. 00059 struct OverriderInfo { 00060 /// Method - The method decl of the overrider. 00061 const CXXMethodDecl *Method; 00062 00063 /// Offset - the base offset of the overrider in the layout class. 00064 CharUnits Offset; 00065 00066 OverriderInfo() : Method(0), Offset(CharUnits::Zero()) { } 00067 }; 00068 00069 private: 00070 /// MostDerivedClass - The most derived class for which the final overriders 00071 /// are stored. 00072 const CXXRecordDecl *MostDerivedClass; 00073 00074 /// MostDerivedClassOffset - If we're building final overriders for a 00075 /// construction vtable, this holds the offset from the layout class to the 00076 /// most derived class. 00077 const CharUnits MostDerivedClassOffset; 00078 00079 /// LayoutClass - The class we're using for layout information. Will be 00080 /// different than the most derived class if the final overriders are for a 00081 /// construction vtable. 00082 const CXXRecordDecl *LayoutClass; 00083 00084 ASTContext &Context; 00085 00086 /// MostDerivedClassLayout - the AST record layout of the most derived class. 00087 const ASTRecordLayout &MostDerivedClassLayout; 00088 00089 /// MethodBaseOffsetPairTy - Uniquely identifies a member function 00090 /// in a base subobject. 00091 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy; 00092 00093 typedef llvm::DenseMap<MethodBaseOffsetPairTy, 00094 OverriderInfo> OverridersMapTy; 00095 00096 /// OverridersMap - The final overriders for all virtual member functions of 00097 /// all the base subobjects of the most derived class. 00098 OverridersMapTy OverridersMap; 00099 00100 /// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented 00101 /// as a record decl and a subobject number) and its offsets in the most 00102 /// derived class as well as the layout class. 00103 typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>, 00104 CharUnits> SubobjectOffsetMapTy; 00105 00106 typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy; 00107 00108 /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the 00109 /// given base. 00110 void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, 00111 CharUnits OffsetInLayoutClass, 00112 SubobjectOffsetMapTy &SubobjectOffsets, 00113 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, 00114 SubobjectCountMapTy &SubobjectCounts); 00115 00116 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; 00117 00118 /// dump - dump the final overriders for a base subobject, and all its direct 00119 /// and indirect base subobjects. 00120 void dump(raw_ostream &Out, BaseSubobject Base, 00121 VisitedVirtualBasesSetTy& VisitedVirtualBases); 00122 00123 public: 00124 FinalOverriders(const CXXRecordDecl *MostDerivedClass, 00125 CharUnits MostDerivedClassOffset, 00126 const CXXRecordDecl *LayoutClass); 00127 00128 /// getOverrider - Get the final overrider for the given method declaration in 00129 /// the subobject with the given base offset. 00130 OverriderInfo getOverrider(const CXXMethodDecl *MD, 00131 CharUnits BaseOffset) const { 00132 assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) && 00133 "Did not find overrider!"); 00134 00135 return OverridersMap.lookup(std::make_pair(MD, BaseOffset)); 00136 } 00137 00138 /// dump - dump the final overriders. 00139 void dump() { 00140 VisitedVirtualBasesSetTy VisitedVirtualBases; 00141 dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()), 00142 VisitedVirtualBases); 00143 } 00144 00145 }; 00146 00147 #define DUMP_OVERRIDERS 0 00148 00149 FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass, 00150 CharUnits MostDerivedClassOffset, 00151 const CXXRecordDecl *LayoutClass) 00152 : MostDerivedClass(MostDerivedClass), 00153 MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass), 00154 Context(MostDerivedClass->getASTContext()), 00155 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) { 00156 00157 // Compute base offsets. 00158 SubobjectOffsetMapTy SubobjectOffsets; 00159 SubobjectOffsetMapTy SubobjectLayoutClassOffsets; 00160 SubobjectCountMapTy SubobjectCounts; 00161 ComputeBaseOffsets(BaseSubobject(MostDerivedClass, CharUnits::Zero()), 00162 /*IsVirtual=*/false, 00163 MostDerivedClassOffset, 00164 SubobjectOffsets, SubobjectLayoutClassOffsets, 00165 SubobjectCounts); 00166 00167 // Get the the final overriders. 00168 CXXFinalOverriderMap FinalOverriders; 00169 MostDerivedClass->getFinalOverriders(FinalOverriders); 00170 00171 for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(), 00172 E = FinalOverriders.end(); I != E; ++I) { 00173 const CXXMethodDecl *MD = I->first; 00174 const OverridingMethods& Methods = I->second; 00175 00176 for (OverridingMethods::const_iterator I = Methods.begin(), 00177 E = Methods.end(); I != E; ++I) { 00178 unsigned SubobjectNumber = I->first; 00179 assert(SubobjectOffsets.count(std::make_pair(MD->getParent(), 00180 SubobjectNumber)) && 00181 "Did not find subobject offset!"); 00182 00183 CharUnits BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(), 00184 SubobjectNumber)]; 00185 00186 assert(I->second.size() == 1 && "Final overrider is not unique!"); 00187 const UniqueVirtualMethod &Method = I->second.front(); 00188 00189 const CXXRecordDecl *OverriderRD = Method.Method->getParent(); 00190 assert(SubobjectLayoutClassOffsets.count( 00191 std::make_pair(OverriderRD, Method.Subobject)) 00192 && "Did not find subobject offset!"); 00193 CharUnits OverriderOffset = 00194 SubobjectLayoutClassOffsets[std::make_pair(OverriderRD, 00195 Method.Subobject)]; 00196 00197 OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)]; 00198 assert(!Overrider.Method && "Overrider should not exist yet!"); 00199 00200 Overrider.Offset = OverriderOffset; 00201 Overrider.Method = Method.Method; 00202 } 00203 } 00204 00205 #if DUMP_OVERRIDERS 00206 // And dump them (for now). 00207 dump(); 00208 #endif 00209 } 00210 00211 static BaseOffset ComputeBaseOffset(ASTContext &Context, 00212 const CXXRecordDecl *DerivedRD, 00213 const CXXBasePath &Path) { 00214 CharUnits NonVirtualOffset = CharUnits::Zero(); 00215 00216 unsigned NonVirtualStart = 0; 00217 const CXXRecordDecl *VirtualBase = 0; 00218 00219 // First, look for the virtual base class. 00220 for (unsigned I = 0, E = Path.size(); I != E; ++I) { 00221 const CXXBasePathElement &Element = Path[I]; 00222 00223 if (Element.Base->isVirtual()) { 00224 // FIXME: Can we break when we find the first virtual base? 00225 // (If we can't, can't we just iterate over the path in reverse order?) 00226 NonVirtualStart = I + 1; 00227 QualType VBaseType = Element.Base->getType(); 00228 VirtualBase = 00229 cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); 00230 } 00231 } 00232 00233 // Now compute the non-virtual offset. 00234 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) { 00235 const CXXBasePathElement &Element = Path[I]; 00236 00237 // Check the base class offset. 00238 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); 00239 00240 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>(); 00241 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl()); 00242 00243 NonVirtualOffset += Layout.getBaseClassOffset(Base); 00244 } 00245 00246 // FIXME: This should probably use CharUnits or something. Maybe we should 00247 // even change the base offsets in ASTRecordLayout to be specified in 00248 // CharUnits. 00249 return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset); 00250 00251 } 00252 00253 static BaseOffset ComputeBaseOffset(ASTContext &Context, 00254 const CXXRecordDecl *BaseRD, 00255 const CXXRecordDecl *DerivedRD) { 00256 CXXBasePaths Paths(/*FindAmbiguities=*/false, 00257 /*RecordPaths=*/true, /*DetectVirtual=*/false); 00258 00259 if (!const_cast<CXXRecordDecl *>(DerivedRD)-> 00260 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) { 00261 llvm_unreachable("Class must be derived from the passed in base class!"); 00262 } 00263 00264 return ComputeBaseOffset(Context, DerivedRD, Paths.front()); 00265 } 00266 00267 static BaseOffset 00268 ComputeReturnAdjustmentBaseOffset(ASTContext &Context, 00269 const CXXMethodDecl *DerivedMD, 00270 const CXXMethodDecl *BaseMD) { 00271 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>(); 00272 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>(); 00273 00274 // Canonicalize the return types. 00275 CanQualType CanDerivedReturnType = 00276 Context.getCanonicalType(DerivedFT->getResultType()); 00277 CanQualType CanBaseReturnType = 00278 Context.getCanonicalType(BaseFT->getResultType()); 00279 00280 assert(CanDerivedReturnType->getTypeClass() == 00281 CanBaseReturnType->getTypeClass() && 00282 "Types must have same type class!"); 00283 00284 if (CanDerivedReturnType == CanBaseReturnType) { 00285 // No adjustment needed. 00286 return BaseOffset(); 00287 } 00288 00289 if (isa<ReferenceType>(CanDerivedReturnType)) { 00290 CanDerivedReturnType = 00291 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType(); 00292 CanBaseReturnType = 00293 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType(); 00294 } else if (isa<PointerType>(CanDerivedReturnType)) { 00295 CanDerivedReturnType = 00296 CanDerivedReturnType->getAs<PointerType>()->getPointeeType(); 00297 CanBaseReturnType = 00298 CanBaseReturnType->getAs<PointerType>()->getPointeeType(); 00299 } else { 00300 llvm_unreachable("Unexpected return type!"); 00301 } 00302 00303 // We need to compare unqualified types here; consider 00304 // const T *Base::foo(); 00305 // T *Derived::foo(); 00306 if (CanDerivedReturnType.getUnqualifiedType() == 00307 CanBaseReturnType.getUnqualifiedType()) { 00308 // No adjustment needed. 00309 return BaseOffset(); 00310 } 00311 00312 const CXXRecordDecl *DerivedRD = 00313 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl()); 00314 00315 const CXXRecordDecl *BaseRD = 00316 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl()); 00317 00318 return ComputeBaseOffset(Context, BaseRD, DerivedRD); 00319 } 00320 00321 void 00322 FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual, 00323 CharUnits OffsetInLayoutClass, 00324 SubobjectOffsetMapTy &SubobjectOffsets, 00325 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets, 00326 SubobjectCountMapTy &SubobjectCounts) { 00327 const CXXRecordDecl *RD = Base.getBase(); 00328 00329 unsigned SubobjectNumber = 0; 00330 if (!IsVirtual) 00331 SubobjectNumber = ++SubobjectCounts[RD]; 00332 00333 // Set up the subobject to offset mapping. 00334 assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber)) 00335 && "Subobject offset already exists!"); 00336 assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber)) 00337 && "Subobject offset already exists!"); 00338 00339 SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = Base.getBaseOffset(); 00340 SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] = 00341 OffsetInLayoutClass; 00342 00343 // Traverse our bases. 00344 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 00345 E = RD->bases_end(); I != E; ++I) { 00346 const CXXRecordDecl *BaseDecl = 00347 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 00348 00349 CharUnits BaseOffset; 00350 CharUnits BaseOffsetInLayoutClass; 00351 if (I->isVirtual()) { 00352 // Check if we've visited this virtual base before. 00353 if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0))) 00354 continue; 00355 00356 const ASTRecordLayout &LayoutClassLayout = 00357 Context.getASTRecordLayout(LayoutClass); 00358 00359 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); 00360 BaseOffsetInLayoutClass = 00361 LayoutClassLayout.getVBaseClassOffset(BaseDecl); 00362 } else { 00363 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 00364 CharUnits Offset = Layout.getBaseClassOffset(BaseDecl); 00365 00366 BaseOffset = Base.getBaseOffset() + Offset; 00367 BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset; 00368 } 00369 00370 ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset), 00371 I->isVirtual(), BaseOffsetInLayoutClass, 00372 SubobjectOffsets, SubobjectLayoutClassOffsets, 00373 SubobjectCounts); 00374 } 00375 } 00376 00377 void FinalOverriders::dump(raw_ostream &Out, BaseSubobject Base, 00378 VisitedVirtualBasesSetTy &VisitedVirtualBases) { 00379 const CXXRecordDecl *RD = Base.getBase(); 00380 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 00381 00382 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 00383 E = RD->bases_end(); I != E; ++I) { 00384 const CXXRecordDecl *BaseDecl = 00385 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 00386 00387 // Ignore bases that don't have any virtual member functions. 00388 if (!BaseDecl->isPolymorphic()) 00389 continue; 00390 00391 CharUnits BaseOffset; 00392 if (I->isVirtual()) { 00393 if (!VisitedVirtualBases.insert(BaseDecl)) { 00394 // We've visited this base before. 00395 continue; 00396 } 00397 00398 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); 00399 } else { 00400 BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset(); 00401 } 00402 00403 dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases); 00404 } 00405 00406 Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", "; 00407 Out << Base.getBaseOffset().getQuantity() << ")\n"; 00408 00409 // Now dump the overriders for this base subobject. 00410 for (CXXRecordDecl::method_iterator I = RD->method_begin(), 00411 E = RD->method_end(); I != E; ++I) { 00412 const CXXMethodDecl *MD = &*I; 00413 00414 if (!MD->isVirtual()) 00415 continue; 00416 00417 OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset()); 00418 00419 Out << " " << MD->getQualifiedNameAsString() << " - ("; 00420 Out << Overrider.Method->getQualifiedNameAsString(); 00421 Out << ", " << ", " << Overrider.Offset.getQuantity() << ')'; 00422 00423 BaseOffset Offset; 00424 if (!Overrider.Method->isPure()) 00425 Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD); 00426 00427 if (!Offset.isEmpty()) { 00428 Out << " [ret-adj: "; 00429 if (Offset.VirtualBase) 00430 Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, "; 00431 00432 Out << Offset.NonVirtualOffset.getQuantity() << " nv]"; 00433 } 00434 00435 Out << "\n"; 00436 } 00437 } 00438 00439 /// VCallOffsetMap - Keeps track of vcall offsets when building a vtable. 00440 struct VCallOffsetMap { 00441 00442 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy; 00443 00444 /// Offsets - Keeps track of methods and their offsets. 00445 // FIXME: This should be a real map and not a vector. 00446 SmallVector<MethodAndOffsetPairTy, 16> Offsets; 00447 00448 /// MethodsCanShareVCallOffset - Returns whether two virtual member functions 00449 /// can share the same vcall offset. 00450 static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, 00451 const CXXMethodDecl *RHS); 00452 00453 public: 00454 /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the 00455 /// add was successful, or false if there was already a member function with 00456 /// the same signature in the map. 00457 bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset); 00458 00459 /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the 00460 /// vtable address point) for the given virtual member function. 00461 CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD); 00462 00463 // empty - Return whether the offset map is empty or not. 00464 bool empty() const { return Offsets.empty(); } 00465 }; 00466 00467 static bool HasSameVirtualSignature(const CXXMethodDecl *LHS, 00468 const CXXMethodDecl *RHS) { 00469 const FunctionProtoType *LT = 00470 cast<FunctionProtoType>(LHS->getType().getCanonicalType()); 00471 const FunctionProtoType *RT = 00472 cast<FunctionProtoType>(RHS->getType().getCanonicalType()); 00473 00474 // Fast-path matches in the canonical types. 00475 if (LT == RT) return true; 00476 00477 // Force the signatures to match. We can't rely on the overrides 00478 // list here because there isn't necessarily an inheritance 00479 // relationship between the two methods. 00480 if (LT->getTypeQuals() != RT->getTypeQuals() || 00481 LT->getNumArgs() != RT->getNumArgs()) 00482 return false; 00483 for (unsigned I = 0, E = LT->getNumArgs(); I != E; ++I) 00484 if (LT->getArgType(I) != RT->getArgType(I)) 00485 return false; 00486 return true; 00487 } 00488 00489 bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS, 00490 const CXXMethodDecl *RHS) { 00491 assert(LHS->isVirtual() && "LHS must be virtual!"); 00492 assert(RHS->isVirtual() && "LHS must be virtual!"); 00493 00494 // A destructor can share a vcall offset with another destructor. 00495 if (isa<CXXDestructorDecl>(LHS)) 00496 return isa<CXXDestructorDecl>(RHS); 00497 00498 // FIXME: We need to check more things here. 00499 00500 // The methods must have the same name. 00501 DeclarationName LHSName = LHS->getDeclName(); 00502 DeclarationName RHSName = RHS->getDeclName(); 00503 if (LHSName != RHSName) 00504 return false; 00505 00506 // And the same signatures. 00507 return HasSameVirtualSignature(LHS, RHS); 00508 } 00509 00510 bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD, 00511 CharUnits OffsetOffset) { 00512 // Check if we can reuse an offset. 00513 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) { 00514 if (MethodsCanShareVCallOffset(Offsets[I].first, MD)) 00515 return false; 00516 } 00517 00518 // Add the offset. 00519 Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset)); 00520 return true; 00521 } 00522 00523 CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) { 00524 // Look for an offset. 00525 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) { 00526 if (MethodsCanShareVCallOffset(Offsets[I].first, MD)) 00527 return Offsets[I].second; 00528 } 00529 00530 llvm_unreachable("Should always find a vcall offset offset!"); 00531 } 00532 00533 /// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets. 00534 class VCallAndVBaseOffsetBuilder { 00535 public: 00536 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> 00537 VBaseOffsetOffsetsMapTy; 00538 00539 private: 00540 /// MostDerivedClass - The most derived class for which we're building vcall 00541 /// and vbase offsets. 00542 const CXXRecordDecl *MostDerivedClass; 00543 00544 /// LayoutClass - The class we're using for layout information. Will be 00545 /// different than the most derived class if we're building a construction 00546 /// vtable. 00547 const CXXRecordDecl *LayoutClass; 00548 00549 /// Context - The ASTContext which we will use for layout information. 00550 ASTContext &Context; 00551 00552 /// Components - vcall and vbase offset components 00553 typedef SmallVector<VTableComponent, 64> VTableComponentVectorTy; 00554 VTableComponentVectorTy Components; 00555 00556 /// VisitedVirtualBases - Visited virtual bases. 00557 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases; 00558 00559 /// VCallOffsets - Keeps track of vcall offsets. 00560 VCallOffsetMap VCallOffsets; 00561 00562 00563 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets, 00564 /// relative to the address point. 00565 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; 00566 00567 /// FinalOverriders - The final overriders of the most derived class. 00568 /// (Can be null when we're not building a vtable of the most derived class). 00569 const FinalOverriders *Overriders; 00570 00571 /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the 00572 /// given base subobject. 00573 void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual, 00574 CharUnits RealBaseOffset); 00575 00576 /// AddVCallOffsets - Add vcall offsets for the given base subobject. 00577 void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset); 00578 00579 /// AddVBaseOffsets - Add vbase offsets for the given class. 00580 void AddVBaseOffsets(const CXXRecordDecl *Base, 00581 CharUnits OffsetInLayoutClass); 00582 00583 /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in 00584 /// chars, relative to the vtable address point. 00585 CharUnits getCurrentOffsetOffset() const; 00586 00587 public: 00588 VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass, 00589 const CXXRecordDecl *LayoutClass, 00590 const FinalOverriders *Overriders, 00591 BaseSubobject Base, bool BaseIsVirtual, 00592 CharUnits OffsetInLayoutClass) 00593 : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass), 00594 Context(MostDerivedClass->getASTContext()), Overriders(Overriders) { 00595 00596 // Add vcall and vbase offsets. 00597 AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass); 00598 } 00599 00600 /// Methods for iterating over the components. 00601 typedef VTableComponentVectorTy::const_reverse_iterator const_iterator; 00602 const_iterator components_begin() const { return Components.rbegin(); } 00603 const_iterator components_end() const { return Components.rend(); } 00604 00605 const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; } 00606 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { 00607 return VBaseOffsetOffsets; 00608 } 00609 }; 00610 00611 void 00612 VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base, 00613 bool BaseIsVirtual, 00614 CharUnits RealBaseOffset) { 00615 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase()); 00616 00617 // Itanium C++ ABI 2.5.2: 00618 // ..in classes sharing a virtual table with a primary base class, the vcall 00619 // and vbase offsets added by the derived class all come before the vcall 00620 // and vbase offsets required by the base class, so that the latter may be 00621 // laid out as required by the base class without regard to additions from 00622 // the derived class(es). 00623 00624 // (Since we're emitting the vcall and vbase offsets in reverse order, we'll 00625 // emit them for the primary base first). 00626 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { 00627 bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual(); 00628 00629 CharUnits PrimaryBaseOffset; 00630 00631 // Get the base offset of the primary base. 00632 if (PrimaryBaseIsVirtual) { 00633 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 && 00634 "Primary vbase should have a zero offset!"); 00635 00636 const ASTRecordLayout &MostDerivedClassLayout = 00637 Context.getASTRecordLayout(MostDerivedClass); 00638 00639 PrimaryBaseOffset = 00640 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase); 00641 } else { 00642 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 && 00643 "Primary base should have a zero offset!"); 00644 00645 PrimaryBaseOffset = Base.getBaseOffset(); 00646 } 00647 00648 AddVCallAndVBaseOffsets( 00649 BaseSubobject(PrimaryBase,PrimaryBaseOffset), 00650 PrimaryBaseIsVirtual, RealBaseOffset); 00651 } 00652 00653 AddVBaseOffsets(Base.getBase(), RealBaseOffset); 00654 00655 // We only want to add vcall offsets for virtual bases. 00656 if (BaseIsVirtual) 00657 AddVCallOffsets(Base, RealBaseOffset); 00658 } 00659 00660 CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const { 00661 // OffsetIndex is the index of this vcall or vbase offset, relative to the 00662 // vtable address point. (We subtract 3 to account for the information just 00663 // above the address point, the RTTI info, the offset to top, and the 00664 // vcall offset itself). 00665 int64_t OffsetIndex = -(int64_t)(3 + Components.size()); 00666 00667 CharUnits PointerWidth = 00668 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0)); 00669 CharUnits OffsetOffset = PointerWidth * OffsetIndex; 00670 return OffsetOffset; 00671 } 00672 00673 void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base, 00674 CharUnits VBaseOffset) { 00675 const CXXRecordDecl *RD = Base.getBase(); 00676 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 00677 00678 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 00679 00680 // Handle the primary base first. 00681 // We only want to add vcall offsets if the base is non-virtual; a virtual 00682 // primary base will have its vcall and vbase offsets emitted already. 00683 if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) { 00684 // Get the base offset of the primary base. 00685 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 && 00686 "Primary base should have a zero offset!"); 00687 00688 AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()), 00689 VBaseOffset); 00690 } 00691 00692 // Add the vcall offsets. 00693 for (CXXRecordDecl::method_iterator I = RD->method_begin(), 00694 E = RD->method_end(); I != E; ++I) { 00695 const CXXMethodDecl *MD = &*I; 00696 00697 if (!MD->isVirtual()) 00698 continue; 00699 00700 CharUnits OffsetOffset = getCurrentOffsetOffset(); 00701 00702 // Don't add a vcall offset if we already have one for this member function 00703 // signature. 00704 if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset)) 00705 continue; 00706 00707 CharUnits Offset = CharUnits::Zero(); 00708 00709 if (Overriders) { 00710 // Get the final overrider. 00711 FinalOverriders::OverriderInfo Overrider = 00712 Overriders->getOverrider(MD, Base.getBaseOffset()); 00713 00714 /// The vcall offset is the offset from the virtual base to the object 00715 /// where the function was overridden. 00716 Offset = Overrider.Offset - VBaseOffset; 00717 } 00718 00719 Components.push_back( 00720 VTableComponent::MakeVCallOffset(Offset)); 00721 } 00722 00723 // And iterate over all non-virtual bases (ignoring the primary base). 00724 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 00725 E = RD->bases_end(); I != E; ++I) { 00726 00727 if (I->isVirtual()) 00728 continue; 00729 00730 const CXXRecordDecl *BaseDecl = 00731 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 00732 if (BaseDecl == PrimaryBase) 00733 continue; 00734 00735 // Get the base offset of this base. 00736 CharUnits BaseOffset = Base.getBaseOffset() + 00737 Layout.getBaseClassOffset(BaseDecl); 00738 00739 AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset), 00740 VBaseOffset); 00741 } 00742 } 00743 00744 void 00745 VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD, 00746 CharUnits OffsetInLayoutClass) { 00747 const ASTRecordLayout &LayoutClassLayout = 00748 Context.getASTRecordLayout(LayoutClass); 00749 00750 // Add vbase offsets. 00751 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 00752 E = RD->bases_end(); I != E; ++I) { 00753 const CXXRecordDecl *BaseDecl = 00754 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 00755 00756 // Check if this is a virtual base that we haven't visited before. 00757 if (I->isVirtual() && VisitedVirtualBases.insert(BaseDecl)) { 00758 CharUnits Offset = 00759 LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass; 00760 00761 // Add the vbase offset offset. 00762 assert(!VBaseOffsetOffsets.count(BaseDecl) && 00763 "vbase offset offset already exists!"); 00764 00765 CharUnits VBaseOffsetOffset = getCurrentOffsetOffset(); 00766 VBaseOffsetOffsets.insert( 00767 std::make_pair(BaseDecl, VBaseOffsetOffset)); 00768 00769 Components.push_back( 00770 VTableComponent::MakeVBaseOffset(Offset)); 00771 } 00772 00773 // Check the base class looking for more vbase offsets. 00774 AddVBaseOffsets(BaseDecl, OffsetInLayoutClass); 00775 } 00776 } 00777 00778 /// VTableBuilder - Class for building vtable layout information. 00779 class VTableBuilder { 00780 public: 00781 /// PrimaryBasesSetVectorTy - A set vector of direct and indirect 00782 /// primary bases. 00783 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> 00784 PrimaryBasesSetVectorTy; 00785 00786 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> 00787 VBaseOffsetOffsetsMapTy; 00788 00789 typedef llvm::DenseMap<BaseSubobject, uint64_t> 00790 AddressPointsMapTy; 00791 00792 private: 00793 /// VTables - Global vtable information. 00794 VTableContext &VTables; 00795 00796 /// MostDerivedClass - The most derived class for which we're building this 00797 /// vtable. 00798 const CXXRecordDecl *MostDerivedClass; 00799 00800 /// MostDerivedClassOffset - If we're building a construction vtable, this 00801 /// holds the offset from the layout class to the most derived class. 00802 const CharUnits MostDerivedClassOffset; 00803 00804 /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual 00805 /// base. (This only makes sense when building a construction vtable). 00806 bool MostDerivedClassIsVirtual; 00807 00808 /// LayoutClass - The class we're using for layout information. Will be 00809 /// different than the most derived class if we're building a construction 00810 /// vtable. 00811 const CXXRecordDecl *LayoutClass; 00812 00813 /// Context - The ASTContext which we will use for layout information. 00814 ASTContext &Context; 00815 00816 /// FinalOverriders - The final overriders of the most derived class. 00817 const FinalOverriders Overriders; 00818 00819 /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual 00820 /// bases in this vtable. 00821 llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases; 00822 00823 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for 00824 /// the most derived class. 00825 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets; 00826 00827 /// Components - The components of the vtable being built. 00828 SmallVector<VTableComponent, 64> Components; 00829 00830 /// AddressPoints - Address points for the vtable being built. 00831 AddressPointsMapTy AddressPoints; 00832 00833 /// MethodInfo - Contains information about a method in a vtable. 00834 /// (Used for computing 'this' pointer adjustment thunks. 00835 struct MethodInfo { 00836 /// BaseOffset - The base offset of this method. 00837 const CharUnits BaseOffset; 00838 00839 /// BaseOffsetInLayoutClass - The base offset in the layout class of this 00840 /// method. 00841 const CharUnits BaseOffsetInLayoutClass; 00842 00843 /// VTableIndex - The index in the vtable that this method has. 00844 /// (For destructors, this is the index of the complete destructor). 00845 const uint64_t VTableIndex; 00846 00847 MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass, 00848 uint64_t VTableIndex) 00849 : BaseOffset(BaseOffset), 00850 BaseOffsetInLayoutClass(BaseOffsetInLayoutClass), 00851 VTableIndex(VTableIndex) { } 00852 00853 MethodInfo() 00854 : BaseOffset(CharUnits::Zero()), 00855 BaseOffsetInLayoutClass(CharUnits::Zero()), 00856 VTableIndex(0) { } 00857 }; 00858 00859 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy; 00860 00861 /// MethodInfoMap - The information for all methods in the vtable we're 00862 /// currently building. 00863 MethodInfoMapTy MethodInfoMap; 00864 00865 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy; 00866 00867 /// VTableThunks - The thunks by vtable index in the vtable currently being 00868 /// built. 00869 VTableThunksMapTy VTableThunks; 00870 00871 typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; 00872 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; 00873 00874 /// Thunks - A map that contains all the thunks needed for all methods in the 00875 /// most derived class for which the vtable is currently being built. 00876 ThunksMapTy Thunks; 00877 00878 /// AddThunk - Add a thunk for the given method. 00879 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk); 00880 00881 /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the 00882 /// part of the vtable we're currently building. 00883 void ComputeThisAdjustments(); 00884 00885 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy; 00886 00887 /// PrimaryVirtualBases - All known virtual bases who are a primary base of 00888 /// some other base. 00889 VisitedVirtualBasesSetTy PrimaryVirtualBases; 00890 00891 /// ComputeReturnAdjustment - Compute the return adjustment given a return 00892 /// adjustment base offset. 00893 ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset); 00894 00895 /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting 00896 /// the 'this' pointer from the base subobject to the derived subobject. 00897 BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base, 00898 BaseSubobject Derived) const; 00899 00900 /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the 00901 /// given virtual member function, its offset in the layout class and its 00902 /// final overrider. 00903 ThisAdjustment 00904 ComputeThisAdjustment(const CXXMethodDecl *MD, 00905 CharUnits BaseOffsetInLayoutClass, 00906 FinalOverriders::OverriderInfo Overrider); 00907 00908 /// AddMethod - Add a single virtual member function to the vtable 00909 /// components vector. 00910 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment); 00911 00912 /// IsOverriderUsed - Returns whether the overrider will ever be used in this 00913 /// part of the vtable. 00914 /// 00915 /// Itanium C++ ABI 2.5.2: 00916 /// 00917 /// struct A { virtual void f(); }; 00918 /// struct B : virtual public A { int i; }; 00919 /// struct C : virtual public A { int j; }; 00920 /// struct D : public B, public C {}; 00921 /// 00922 /// When B and C are declared, A is a primary base in each case, so although 00923 /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this 00924 /// adjustment is required and no thunk is generated. However, inside D 00925 /// objects, A is no longer a primary base of C, so if we allowed calls to 00926 /// C::f() to use the copy of A's vtable in the C subobject, we would need 00927 /// to adjust this from C* to B::A*, which would require a third-party 00928 /// thunk. Since we require that a call to C::f() first convert to A*, 00929 /// C-in-D's copy of A's vtable is never referenced, so this is not 00930 /// necessary. 00931 bool IsOverriderUsed(const CXXMethodDecl *Overrider, 00932 CharUnits BaseOffsetInLayoutClass, 00933 const CXXRecordDecl *FirstBaseInPrimaryBaseChain, 00934 CharUnits FirstBaseOffsetInLayoutClass) const; 00935 00936 00937 /// AddMethods - Add the methods of this base subobject and all its 00938 /// primary bases to the vtable components vector. 00939 void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass, 00940 const CXXRecordDecl *FirstBaseInPrimaryBaseChain, 00941 CharUnits FirstBaseOffsetInLayoutClass, 00942 PrimaryBasesSetVectorTy &PrimaryBases); 00943 00944 // LayoutVTable - Layout the vtable for the given base class, including its 00945 // secondary vtables and any vtables for virtual bases. 00946 void LayoutVTable(); 00947 00948 /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the 00949 /// given base subobject, as well as all its secondary vtables. 00950 /// 00951 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base 00952 /// or a direct or indirect base of a virtual base. 00953 /// 00954 /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual 00955 /// in the layout class. 00956 void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base, 00957 bool BaseIsMorallyVirtual, 00958 bool BaseIsVirtualInLayoutClass, 00959 CharUnits OffsetInLayoutClass); 00960 00961 /// LayoutSecondaryVTables - Layout the secondary vtables for the given base 00962 /// subobject. 00963 /// 00964 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base 00965 /// or a direct or indirect base of a virtual base. 00966 void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual, 00967 CharUnits OffsetInLayoutClass); 00968 00969 /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this 00970 /// class hierarchy. 00971 void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD, 00972 CharUnits OffsetInLayoutClass, 00973 VisitedVirtualBasesSetTy &VBases); 00974 00975 /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the 00976 /// given base (excluding any primary bases). 00977 void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD, 00978 VisitedVirtualBasesSetTy &VBases); 00979 00980 /// isBuildingConstructionVTable - Return whether this vtable builder is 00981 /// building a construction vtable. 00982 bool isBuildingConstructorVTable() const { 00983 return MostDerivedClass != LayoutClass; 00984 } 00985 00986 public: 00987 VTableBuilder(VTableContext &VTables, const CXXRecordDecl *MostDerivedClass, 00988 CharUnits MostDerivedClassOffset, 00989 bool MostDerivedClassIsVirtual, const 00990 CXXRecordDecl *LayoutClass) 00991 : VTables(VTables), MostDerivedClass(MostDerivedClass), 00992 MostDerivedClassOffset(MostDerivedClassOffset), 00993 MostDerivedClassIsVirtual(MostDerivedClassIsVirtual), 00994 LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()), 00995 Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) { 00996 00997 LayoutVTable(); 00998 00999 if (Context.getLangOpts().DumpVTableLayouts) 01000 dumpLayout(llvm::errs()); 01001 } 01002 01003 uint64_t getNumThunks() const { 01004 return Thunks.size(); 01005 } 01006 01007 ThunksMapTy::const_iterator thunks_begin() const { 01008 return Thunks.begin(); 01009 } 01010 01011 ThunksMapTy::const_iterator thunks_end() const { 01012 return Thunks.end(); 01013 } 01014 01015 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const { 01016 return VBaseOffsetOffsets; 01017 } 01018 01019 const AddressPointsMapTy &getAddressPoints() const { 01020 return AddressPoints; 01021 } 01022 01023 /// getNumVTableComponents - Return the number of components in the vtable 01024 /// currently built. 01025 uint64_t getNumVTableComponents() const { 01026 return Components.size(); 01027 } 01028 01029 const VTableComponent *vtable_component_begin() const { 01030 return Components.begin(); 01031 } 01032 01033 const VTableComponent *vtable_component_end() const { 01034 return Components.end(); 01035 } 01036 01037 AddressPointsMapTy::const_iterator address_points_begin() const { 01038 return AddressPoints.begin(); 01039 } 01040 01041 AddressPointsMapTy::const_iterator address_points_end() const { 01042 return AddressPoints.end(); 01043 } 01044 01045 VTableThunksMapTy::const_iterator vtable_thunks_begin() const { 01046 return VTableThunks.begin(); 01047 } 01048 01049 VTableThunksMapTy::const_iterator vtable_thunks_end() const { 01050 return VTableThunks.end(); 01051 } 01052 01053 /// dumpLayout - Dump the vtable layout. 01054 void dumpLayout(raw_ostream&); 01055 }; 01056 01057 void VTableBuilder::AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) { 01058 assert(!isBuildingConstructorVTable() && 01059 "Can't add thunks for construction vtable"); 01060 01061 SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD]; 01062 01063 // Check if we have this thunk already. 01064 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) != 01065 ThunksVector.end()) 01066 return; 01067 01068 ThunksVector.push_back(Thunk); 01069 } 01070 01071 typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy; 01072 01073 /// ComputeAllOverriddenMethods - Given a method decl, will return a set of all 01074 /// the overridden methods that the function decl overrides. 01075 static void 01076 ComputeAllOverriddenMethods(const CXXMethodDecl *MD, 01077 OverriddenMethodsSetTy& OverriddenMethods) { 01078 assert(MD->isVirtual() && "Method is not virtual!"); 01079 01080 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), 01081 E = MD->end_overridden_methods(); I != E; ++I) { 01082 const CXXMethodDecl *OverriddenMD = *I; 01083 01084 OverriddenMethods.insert(OverriddenMD); 01085 01086 ComputeAllOverriddenMethods(OverriddenMD, OverriddenMethods); 01087 } 01088 } 01089 01090 void VTableBuilder::ComputeThisAdjustments() { 01091 // Now go through the method info map and see if any of the methods need 01092 // 'this' pointer adjustments. 01093 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(), 01094 E = MethodInfoMap.end(); I != E; ++I) { 01095 const CXXMethodDecl *MD = I->first; 01096 const MethodInfo &MethodInfo = I->second; 01097 01098 // Ignore adjustments for unused function pointers. 01099 uint64_t VTableIndex = MethodInfo.VTableIndex; 01100 if (Components[VTableIndex].getKind() == 01101 VTableComponent::CK_UnusedFunctionPointer) 01102 continue; 01103 01104 // Get the final overrider for this method. 01105 FinalOverriders::OverriderInfo Overrider = 01106 Overriders.getOverrider(MD, MethodInfo.BaseOffset); 01107 01108 // Check if we need an adjustment at all. 01109 if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) { 01110 // When a return thunk is needed by a derived class that overrides a 01111 // virtual base, gcc uses a virtual 'this' adjustment as well. 01112 // While the thunk itself might be needed by vtables in subclasses or 01113 // in construction vtables, there doesn't seem to be a reason for using 01114 // the thunk in this vtable. Still, we do so to match gcc. 01115 if (VTableThunks.lookup(VTableIndex).Return.isEmpty()) 01116 continue; 01117 } 01118 01119 ThisAdjustment ThisAdjustment = 01120 ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider); 01121 01122 if (ThisAdjustment.isEmpty()) 01123 continue; 01124 01125 // Add it. 01126 VTableThunks[VTableIndex].This = ThisAdjustment; 01127 01128 if (isa<CXXDestructorDecl>(MD)) { 01129 // Add an adjustment for the deleting destructor as well. 01130 VTableThunks[VTableIndex + 1].This = ThisAdjustment; 01131 } 01132 } 01133 01134 /// Clear the method info map. 01135 MethodInfoMap.clear(); 01136 01137 if (isBuildingConstructorVTable()) { 01138 // We don't need to store thunk information for construction vtables. 01139 return; 01140 } 01141 01142 for (VTableThunksMapTy::const_iterator I = VTableThunks.begin(), 01143 E = VTableThunks.end(); I != E; ++I) { 01144 const VTableComponent &Component = Components[I->first]; 01145 const ThunkInfo &Thunk = I->second; 01146 const CXXMethodDecl *MD; 01147 01148 switch (Component.getKind()) { 01149 default: 01150 llvm_unreachable("Unexpected vtable component kind!"); 01151 case VTableComponent::CK_FunctionPointer: 01152 MD = Component.getFunctionDecl(); 01153 break; 01154 case VTableComponent::CK_CompleteDtorPointer: 01155 MD = Component.getDestructorDecl(); 01156 break; 01157 case VTableComponent::CK_DeletingDtorPointer: 01158 // We've already added the thunk when we saw the complete dtor pointer. 01159 continue; 01160 } 01161 01162 if (MD->getParent() == MostDerivedClass) 01163 AddThunk(MD, Thunk); 01164 } 01165 } 01166 01167 ReturnAdjustment VTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) { 01168 ReturnAdjustment Adjustment; 01169 01170 if (!Offset.isEmpty()) { 01171 if (Offset.VirtualBase) { 01172 // Get the virtual base offset offset. 01173 if (Offset.DerivedClass == MostDerivedClass) { 01174 // We can get the offset offset directly from our map. 01175 Adjustment.VBaseOffsetOffset = 01176 VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity(); 01177 } else { 01178 Adjustment.VBaseOffsetOffset = 01179 VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass, 01180 Offset.VirtualBase).getQuantity(); 01181 } 01182 } 01183 01184 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity(); 01185 } 01186 01187 return Adjustment; 01188 } 01189 01190 BaseOffset 01191 VTableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base, 01192 BaseSubobject Derived) const { 01193 const CXXRecordDecl *BaseRD = Base.getBase(); 01194 const CXXRecordDecl *DerivedRD = Derived.getBase(); 01195 01196 CXXBasePaths Paths(/*FindAmbiguities=*/true, 01197 /*RecordPaths=*/true, /*DetectVirtual=*/true); 01198 01199 if (!const_cast<CXXRecordDecl *>(DerivedRD)-> 01200 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) { 01201 llvm_unreachable("Class must be derived from the passed in base class!"); 01202 } 01203 01204 // We have to go through all the paths, and see which one leads us to the 01205 // right base subobject. 01206 for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end(); 01207 I != E; ++I) { 01208 BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I); 01209 01210 CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset; 01211 01212 if (Offset.VirtualBase) { 01213 // If we have a virtual base class, the non-virtual offset is relative 01214 // to the virtual base class offset. 01215 const ASTRecordLayout &LayoutClassLayout = 01216 Context.getASTRecordLayout(LayoutClass); 01217 01218 /// Get the virtual base offset, relative to the most derived class 01219 /// layout. 01220 OffsetToBaseSubobject += 01221 LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase); 01222 } else { 01223 // Otherwise, the non-virtual offset is relative to the derived class 01224 // offset. 01225 OffsetToBaseSubobject += Derived.getBaseOffset(); 01226 } 01227 01228 // Check if this path gives us the right base subobject. 01229 if (OffsetToBaseSubobject == Base.getBaseOffset()) { 01230 // Since we're going from the base class _to_ the derived class, we'll 01231 // invert the non-virtual offset here. 01232 Offset.NonVirtualOffset = -Offset.NonVirtualOffset; 01233 return Offset; 01234 } 01235 } 01236 01237 return BaseOffset(); 01238 } 01239 01240 ThisAdjustment 01241 VTableBuilder::ComputeThisAdjustment(const CXXMethodDecl *MD, 01242 CharUnits BaseOffsetInLayoutClass, 01243 FinalOverriders::OverriderInfo Overrider) { 01244 // Ignore adjustments for pure virtual member functions. 01245 if (Overrider.Method->isPure()) 01246 return ThisAdjustment(); 01247 01248 BaseSubobject OverriddenBaseSubobject(MD->getParent(), 01249 BaseOffsetInLayoutClass); 01250 01251 BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(), 01252 Overrider.Offset); 01253 01254 // Compute the adjustment offset. 01255 BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject, 01256 OverriderBaseSubobject); 01257 if (Offset.isEmpty()) 01258 return ThisAdjustment(); 01259 01260 ThisAdjustment Adjustment; 01261 01262 if (Offset.VirtualBase) { 01263 // Get the vcall offset map for this virtual base. 01264 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase]; 01265 01266 if (VCallOffsets.empty()) { 01267 // We don't have vcall offsets for this virtual base, go ahead and 01268 // build them. 01269 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass, 01270 /*FinalOverriders=*/0, 01271 BaseSubobject(Offset.VirtualBase, 01272 CharUnits::Zero()), 01273 /*BaseIsVirtual=*/true, 01274 /*OffsetInLayoutClass=*/ 01275 CharUnits::Zero()); 01276 01277 VCallOffsets = Builder.getVCallOffsets(); 01278 } 01279 01280 Adjustment.VCallOffsetOffset = 01281 VCallOffsets.getVCallOffsetOffset(MD).getQuantity(); 01282 } 01283 01284 // Set the non-virtual part of the adjustment. 01285 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity(); 01286 01287 return Adjustment; 01288 } 01289 01290 void 01291 VTableBuilder::AddMethod(const CXXMethodDecl *MD, 01292 ReturnAdjustment ReturnAdjustment) { 01293 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 01294 assert(ReturnAdjustment.isEmpty() && 01295 "Destructor can't have return adjustment!"); 01296 01297 // Add both the complete destructor and the deleting destructor. 01298 Components.push_back(VTableComponent::MakeCompleteDtor(DD)); 01299 Components.push_back(VTableComponent::MakeDeletingDtor(DD)); 01300 } else { 01301 // Add the return adjustment if necessary. 01302 if (!ReturnAdjustment.isEmpty()) 01303 VTableThunks[Components.size()].Return = ReturnAdjustment; 01304 01305 // Add the function. 01306 Components.push_back(VTableComponent::MakeFunction(MD)); 01307 } 01308 } 01309 01310 /// OverridesIndirectMethodInBase - Return whether the given member function 01311 /// overrides any methods in the set of given bases. 01312 /// Unlike OverridesMethodInBase, this checks "overriders of overriders". 01313 /// For example, if we have: 01314 /// 01315 /// struct A { virtual void f(); } 01316 /// struct B : A { virtual void f(); } 01317 /// struct C : B { virtual void f(); } 01318 /// 01319 /// OverridesIndirectMethodInBase will return true if given C::f as the method 01320 /// and { A } as the set of bases. 01321 static bool 01322 OverridesIndirectMethodInBases(const CXXMethodDecl *MD, 01323 VTableBuilder::PrimaryBasesSetVectorTy &Bases) { 01324 if (Bases.count(MD->getParent())) 01325 return true; 01326 01327 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(), 01328 E = MD->end_overridden_methods(); I != E; ++I) { 01329 const CXXMethodDecl *OverriddenMD = *I; 01330 01331 // Check "indirect overriders". 01332 if (OverridesIndirectMethodInBases(OverriddenMD, Bases)) 01333 return true; 01334 } 01335 01336 return false; 01337 } 01338 01339 bool 01340 VTableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider, 01341 CharUnits BaseOffsetInLayoutClass, 01342 const CXXRecordDecl *FirstBaseInPrimaryBaseChain, 01343 CharUnits FirstBaseOffsetInLayoutClass) const { 01344 // If the base and the first base in the primary base chain have the same 01345 // offsets, then this overrider will be used. 01346 if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass) 01347 return true; 01348 01349 // We know now that Base (or a direct or indirect base of it) is a primary 01350 // base in part of the class hierarchy, but not a primary base in the most 01351 // derived class. 01352 01353 // If the overrider is the first base in the primary base chain, we know 01354 // that the overrider will be used. 01355 if (Overrider->getParent() == FirstBaseInPrimaryBaseChain) 01356 return true; 01357 01358 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases; 01359 01360 const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain; 01361 PrimaryBases.insert(RD); 01362 01363 // Now traverse the base chain, starting with the first base, until we find 01364 // the base that is no longer a primary base. 01365 while (true) { 01366 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 01367 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 01368 01369 if (!PrimaryBase) 01370 break; 01371 01372 if (Layout.isPrimaryBaseVirtual()) { 01373 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 && 01374 "Primary base should always be at offset 0!"); 01375 01376 const ASTRecordLayout &LayoutClassLayout = 01377 Context.getASTRecordLayout(LayoutClass); 01378 01379 // Now check if this is the primary base that is not a primary base in the 01380 // most derived class. 01381 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != 01382 FirstBaseOffsetInLayoutClass) { 01383 // We found it, stop walking the chain. 01384 break; 01385 } 01386 } else { 01387 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 && 01388 "Primary base should always be at offset 0!"); 01389 } 01390 01391 if (!PrimaryBases.insert(PrimaryBase)) 01392 llvm_unreachable("Found a duplicate primary base!"); 01393 01394 RD = PrimaryBase; 01395 } 01396 01397 // If the final overrider is an override of one of the primary bases, 01398 // then we know that it will be used. 01399 return OverridesIndirectMethodInBases(Overrider, PrimaryBases); 01400 } 01401 01402 /// FindNearestOverriddenMethod - Given a method, returns the overridden method 01403 /// from the nearest base. Returns null if no method was found. 01404 static const CXXMethodDecl * 01405 FindNearestOverriddenMethod(const CXXMethodDecl *MD, 01406 VTableBuilder::PrimaryBasesSetVectorTy &Bases) { 01407 OverriddenMethodsSetTy OverriddenMethods; 01408 ComputeAllOverriddenMethods(MD, OverriddenMethods); 01409 01410 for (int I = Bases.size(), E = 0; I != E; --I) { 01411 const CXXRecordDecl *PrimaryBase = Bases[I - 1]; 01412 01413 // Now check the overriden methods. 01414 for (OverriddenMethodsSetTy::const_iterator I = OverriddenMethods.begin(), 01415 E = OverriddenMethods.end(); I != E; ++I) { 01416 const CXXMethodDecl *OverriddenMD = *I; 01417 01418 // We found our overridden method. 01419 if (OverriddenMD->getParent() == PrimaryBase) 01420 return OverriddenMD; 01421 } 01422 } 01423 01424 return 0; 01425 } 01426 01427 void 01428 VTableBuilder::AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass, 01429 const CXXRecordDecl *FirstBaseInPrimaryBaseChain, 01430 CharUnits FirstBaseOffsetInLayoutClass, 01431 PrimaryBasesSetVectorTy &PrimaryBases) { 01432 const CXXRecordDecl *RD = Base.getBase(); 01433 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 01434 01435 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { 01436 CharUnits PrimaryBaseOffset; 01437 CharUnits PrimaryBaseOffsetInLayoutClass; 01438 if (Layout.isPrimaryBaseVirtual()) { 01439 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 && 01440 "Primary vbase should have a zero offset!"); 01441 01442 const ASTRecordLayout &MostDerivedClassLayout = 01443 Context.getASTRecordLayout(MostDerivedClass); 01444 01445 PrimaryBaseOffset = 01446 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase); 01447 01448 const ASTRecordLayout &LayoutClassLayout = 01449 Context.getASTRecordLayout(LayoutClass); 01450 01451 PrimaryBaseOffsetInLayoutClass = 01452 LayoutClassLayout.getVBaseClassOffset(PrimaryBase); 01453 } else { 01454 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 && 01455 "Primary base should have a zero offset!"); 01456 01457 PrimaryBaseOffset = Base.getBaseOffset(); 01458 PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass; 01459 } 01460 01461 AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset), 01462 PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain, 01463 FirstBaseOffsetInLayoutClass, PrimaryBases); 01464 01465 if (!PrimaryBases.insert(PrimaryBase)) 01466 llvm_unreachable("Found a duplicate primary base!"); 01467 } 01468 01469 // Now go through all virtual member functions and add them. 01470 for (CXXRecordDecl::method_iterator I = RD->method_begin(), 01471 E = RD->method_end(); I != E; ++I) { 01472 const CXXMethodDecl *MD = &*I; 01473 01474 if (!MD->isVirtual()) 01475 continue; 01476 01477 // Get the final overrider. 01478 FinalOverriders::OverriderInfo Overrider = 01479 Overriders.getOverrider(MD, Base.getBaseOffset()); 01480 01481 // Check if this virtual member function overrides a method in a primary 01482 // base. If this is the case, and the return type doesn't require adjustment 01483 // then we can just use the member function from the primary base. 01484 if (const CXXMethodDecl *OverriddenMD = 01485 FindNearestOverriddenMethod(MD, PrimaryBases)) { 01486 if (ComputeReturnAdjustmentBaseOffset(Context, MD, 01487 OverriddenMD).isEmpty()) { 01488 // Replace the method info of the overridden method with our own 01489 // method. 01490 assert(MethodInfoMap.count(OverriddenMD) && 01491 "Did not find the overridden method!"); 01492 MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD]; 01493 01494 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, 01495 OverriddenMethodInfo.VTableIndex); 01496 01497 assert(!MethodInfoMap.count(MD) && 01498 "Should not have method info for this method yet!"); 01499 01500 MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); 01501 MethodInfoMap.erase(OverriddenMD); 01502 01503 // If the overridden method exists in a virtual base class or a direct 01504 // or indirect base class of a virtual base class, we need to emit a 01505 // thunk if we ever have a class hierarchy where the base class is not 01506 // a primary base in the complete object. 01507 if (!isBuildingConstructorVTable() && OverriddenMD != MD) { 01508 // Compute the this adjustment. 01509 ThisAdjustment ThisAdjustment = 01510 ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass, 01511 Overrider); 01512 01513 if (ThisAdjustment.VCallOffsetOffset && 01514 Overrider.Method->getParent() == MostDerivedClass) { 01515 01516 // There's no return adjustment from OverriddenMD and MD, 01517 // but that doesn't mean there isn't one between MD and 01518 // the final overrider. 01519 BaseOffset ReturnAdjustmentOffset = 01520 ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD); 01521 ReturnAdjustment ReturnAdjustment = 01522 ComputeReturnAdjustment(ReturnAdjustmentOffset); 01523 01524 // This is a virtual thunk for the most derived class, add it. 01525 AddThunk(Overrider.Method, 01526 ThunkInfo(ThisAdjustment, ReturnAdjustment)); 01527 } 01528 } 01529 01530 continue; 01531 } 01532 } 01533 01534 // Insert the method info for this method. 01535 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass, 01536 Components.size()); 01537 01538 assert(!MethodInfoMap.count(MD) && 01539 "Should not have method info for this method yet!"); 01540 MethodInfoMap.insert(std::make_pair(MD, MethodInfo)); 01541 01542 // Check if this overrider is going to be used. 01543 const CXXMethodDecl *OverriderMD = Overrider.Method; 01544 if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass, 01545 FirstBaseInPrimaryBaseChain, 01546 FirstBaseOffsetInLayoutClass)) { 01547 Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD)); 01548 continue; 01549 } 01550 01551 // Check if this overrider needs a return adjustment. 01552 // We don't want to do this for pure virtual member functions. 01553 BaseOffset ReturnAdjustmentOffset; 01554 if (!OverriderMD->isPure()) { 01555 ReturnAdjustmentOffset = 01556 ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD); 01557 } 01558 01559 ReturnAdjustment ReturnAdjustment = 01560 ComputeReturnAdjustment(ReturnAdjustmentOffset); 01561 01562 AddMethod(Overrider.Method, ReturnAdjustment); 01563 } 01564 } 01565 01566 void VTableBuilder::LayoutVTable() { 01567 LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass, 01568 CharUnits::Zero()), 01569 /*BaseIsMorallyVirtual=*/false, 01570 MostDerivedClassIsVirtual, 01571 MostDerivedClassOffset); 01572 01573 VisitedVirtualBasesSetTy VBases; 01574 01575 // Determine the primary virtual bases. 01576 DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset, 01577 VBases); 01578 VBases.clear(); 01579 01580 LayoutVTablesForVirtualBases(MostDerivedClass, VBases); 01581 01582 // -fapple-kext adds an extra entry at end of vtbl. 01583 bool IsAppleKext = Context.getLangOpts().AppleKext; 01584 if (IsAppleKext) 01585 Components.push_back(VTableComponent::MakeVCallOffset(CharUnits::Zero())); 01586 } 01587 01588 void 01589 VTableBuilder::LayoutPrimaryAndSecondaryVTables(BaseSubobject Base, 01590 bool BaseIsMorallyVirtual, 01591 bool BaseIsVirtualInLayoutClass, 01592 CharUnits OffsetInLayoutClass) { 01593 assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!"); 01594 01595 // Add vcall and vbase offsets for this vtable. 01596 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders, 01597 Base, BaseIsVirtualInLayoutClass, 01598 OffsetInLayoutClass); 01599 Components.append(Builder.components_begin(), Builder.components_end()); 01600 01601 // Check if we need to add these vcall offsets. 01602 if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) { 01603 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()]; 01604 01605 if (VCallOffsets.empty()) 01606 VCallOffsets = Builder.getVCallOffsets(); 01607 } 01608 01609 // If we're laying out the most derived class we want to keep track of the 01610 // virtual base class offset offsets. 01611 if (Base.getBase() == MostDerivedClass) 01612 VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets(); 01613 01614 // Add the offset to top. 01615 CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass; 01616 Components.push_back( 01617 VTableComponent::MakeOffsetToTop(OffsetToTop)); 01618 01619 // Next, add the RTTI. 01620 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass)); 01621 01622 uint64_t AddressPoint = Components.size(); 01623 01624 // Now go through all virtual member functions and add them. 01625 PrimaryBasesSetVectorTy PrimaryBases; 01626 AddMethods(Base, OffsetInLayoutClass, 01627 Base.getBase(), OffsetInLayoutClass, 01628 PrimaryBases); 01629 01630 // Compute 'this' pointer adjustments. 01631 ComputeThisAdjustments(); 01632 01633 // Add all address points. 01634 const CXXRecordDecl *RD = Base.getBase(); 01635 while (true) { 01636 AddressPoints.insert(std::make_pair( 01637 BaseSubobject(RD, OffsetInLayoutClass), 01638 AddressPoint)); 01639 01640 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 01641 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 01642 01643 if (!PrimaryBase) 01644 break; 01645 01646 if (Layout.isPrimaryBaseVirtual()) { 01647 // Check if this virtual primary base is a primary base in the layout 01648 // class. If it's not, we don't want to add it. 01649 const ASTRecordLayout &LayoutClassLayout = 01650 Context.getASTRecordLayout(LayoutClass); 01651 01652 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) != 01653 OffsetInLayoutClass) { 01654 // We don't want to add this class (or any of its primary bases). 01655 break; 01656 } 01657 } 01658 01659 RD = PrimaryBase; 01660 } 01661 01662 // Layout secondary vtables. 01663 LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass); 01664 } 01665 01666 void VTableBuilder::LayoutSecondaryVTables(BaseSubobject Base, 01667 bool BaseIsMorallyVirtual, 01668 CharUnits OffsetInLayoutClass) { 01669 // Itanium C++ ABI 2.5.2: 01670 // Following the primary virtual table of a derived class are secondary 01671 // virtual tables for each of its proper base classes, except any primary 01672 // base(s) with which it shares its primary virtual table. 01673 01674 const CXXRecordDecl *RD = Base.getBase(); 01675 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 01676 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 01677 01678 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 01679 E = RD->bases_end(); I != E; ++I) { 01680 // Ignore virtual bases, we'll emit them later. 01681 if (I->isVirtual()) 01682 continue; 01683 01684 const CXXRecordDecl *BaseDecl = 01685 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 01686 01687 // Ignore bases that don't have a vtable. 01688 if (!BaseDecl->isDynamicClass()) 01689 continue; 01690 01691 if (isBuildingConstructorVTable()) { 01692 // Itanium C++ ABI 2.6.4: 01693 // Some of the base class subobjects may not need construction virtual 01694 // tables, which will therefore not be present in the construction 01695 // virtual table group, even though the subobject virtual tables are 01696 // present in the main virtual table group for the complete object. 01697 if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases()) 01698 continue; 01699 } 01700 01701 // Get the base offset of this base. 01702 CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl); 01703 CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset; 01704 01705 CharUnits BaseOffsetInLayoutClass = 01706 OffsetInLayoutClass + RelativeBaseOffset; 01707 01708 // Don't emit a secondary vtable for a primary base. We might however want 01709 // to emit secondary vtables for other bases of this base. 01710 if (BaseDecl == PrimaryBase) { 01711 LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset), 01712 BaseIsMorallyVirtual, BaseOffsetInLayoutClass); 01713 continue; 01714 } 01715 01716 // Layout the primary vtable (and any secondary vtables) for this base. 01717 LayoutPrimaryAndSecondaryVTables( 01718 BaseSubobject(BaseDecl, BaseOffset), 01719 BaseIsMorallyVirtual, 01720 /*BaseIsVirtualInLayoutClass=*/false, 01721 BaseOffsetInLayoutClass); 01722 } 01723 } 01724 01725 void 01726 VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD, 01727 CharUnits OffsetInLayoutClass, 01728 VisitedVirtualBasesSetTy &VBases) { 01729 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 01730 01731 // Check if this base has a primary base. 01732 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) { 01733 01734 // Check if it's virtual. 01735 if (Layout.isPrimaryBaseVirtual()) { 01736 bool IsPrimaryVirtualBase = true; 01737 01738 if (isBuildingConstructorVTable()) { 01739 // Check if the base is actually a primary base in the class we use for 01740 // layout. 01741 const ASTRecordLayout &LayoutClassLayout = 01742 Context.getASTRecordLayout(LayoutClass); 01743 01744 CharUnits PrimaryBaseOffsetInLayoutClass = 01745 LayoutClassLayout.getVBaseClassOffset(PrimaryBase); 01746 01747 // We know that the base is not a primary base in the layout class if 01748 // the base offsets are different. 01749 if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass) 01750 IsPrimaryVirtualBase = false; 01751 } 01752 01753 if (IsPrimaryVirtualBase) 01754 PrimaryVirtualBases.insert(PrimaryBase); 01755 } 01756 } 01757 01758 // Traverse bases, looking for more primary virtual bases. 01759 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 01760 E = RD->bases_end(); I != E; ++I) { 01761 const CXXRecordDecl *BaseDecl = 01762 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 01763 01764 CharUnits BaseOffsetInLayoutClass; 01765 01766 if (I->isVirtual()) { 01767 if (!VBases.insert(BaseDecl)) 01768 continue; 01769 01770 const ASTRecordLayout &LayoutClassLayout = 01771 Context.getASTRecordLayout(LayoutClass); 01772 01773 BaseOffsetInLayoutClass = 01774 LayoutClassLayout.getVBaseClassOffset(BaseDecl); 01775 } else { 01776 BaseOffsetInLayoutClass = 01777 OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl); 01778 } 01779 01780 DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases); 01781 } 01782 } 01783 01784 void 01785 VTableBuilder::LayoutVTablesForVirtualBases(const CXXRecordDecl *RD, 01786 VisitedVirtualBasesSetTy &VBases) { 01787 // Itanium C++ ABI 2.5.2: 01788 // Then come the virtual base virtual tables, also in inheritance graph 01789 // order, and again excluding primary bases (which share virtual tables with 01790 // the classes for which they are primary). 01791 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 01792 E = RD->bases_end(); I != E; ++I) { 01793 const CXXRecordDecl *BaseDecl = 01794 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 01795 01796 // Check if this base needs a vtable. (If it's virtual, not a primary base 01797 // of some other class, and we haven't visited it before). 01798 if (I->isVirtual() && BaseDecl->isDynamicClass() && 01799 !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) { 01800 const ASTRecordLayout &MostDerivedClassLayout = 01801 Context.getASTRecordLayout(MostDerivedClass); 01802 CharUnits BaseOffset = 01803 MostDerivedClassLayout.getVBaseClassOffset(BaseDecl); 01804 01805 const ASTRecordLayout &LayoutClassLayout = 01806 Context.getASTRecordLayout(LayoutClass); 01807 CharUnits BaseOffsetInLayoutClass = 01808 LayoutClassLayout.getVBaseClassOffset(BaseDecl); 01809 01810 LayoutPrimaryAndSecondaryVTables( 01811 BaseSubobject(BaseDecl, BaseOffset), 01812 /*BaseIsMorallyVirtual=*/true, 01813 /*BaseIsVirtualInLayoutClass=*/true, 01814 BaseOffsetInLayoutClass); 01815 } 01816 01817 // We only need to check the base for virtual base vtables if it actually 01818 // has virtual bases. 01819 if (BaseDecl->getNumVBases()) 01820 LayoutVTablesForVirtualBases(BaseDecl, VBases); 01821 } 01822 } 01823 01824 /// dumpLayout - Dump the vtable layout. 01825 void VTableBuilder::dumpLayout(raw_ostream& Out) { 01826 01827 if (isBuildingConstructorVTable()) { 01828 Out << "Construction vtable for ('"; 01829 Out << MostDerivedClass->getQualifiedNameAsString() << "', "; 01830 Out << MostDerivedClassOffset.getQuantity() << ") in '"; 01831 Out << LayoutClass->getQualifiedNameAsString(); 01832 } else { 01833 Out << "Vtable for '"; 01834 Out << MostDerivedClass->getQualifiedNameAsString(); 01835 } 01836 Out << "' (" << Components.size() << " entries).\n"; 01837 01838 // Iterate through the address points and insert them into a new map where 01839 // they are keyed by the index and not the base object. 01840 // Since an address point can be shared by multiple subobjects, we use an 01841 // STL multimap. 01842 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex; 01843 for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(), 01844 E = AddressPoints.end(); I != E; ++I) { 01845 const BaseSubobject& Base = I->first; 01846 uint64_t Index = I->second; 01847 01848 AddressPointsByIndex.insert(std::make_pair(Index, Base)); 01849 } 01850 01851 for (unsigned I = 0, E = Components.size(); I != E; ++I) { 01852 uint64_t Index = I; 01853 01854 Out << llvm::format("%4d | ", I); 01855 01856 const VTableComponent &Component = Components[I]; 01857 01858 // Dump the component. 01859 switch (Component.getKind()) { 01860 01861 case VTableComponent::CK_VCallOffset: 01862 Out << "vcall_offset (" 01863 << Component.getVCallOffset().getQuantity() 01864 << ")"; 01865 break; 01866 01867 case VTableComponent::CK_VBaseOffset: 01868 Out << "vbase_offset (" 01869 << Component.getVBaseOffset().getQuantity() 01870 << ")"; 01871 break; 01872 01873 case VTableComponent::CK_OffsetToTop: 01874 Out << "offset_to_top (" 01875 << Component.getOffsetToTop().getQuantity() 01876 << ")"; 01877 break; 01878 01879 case VTableComponent::CK_RTTI: 01880 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI"; 01881 break; 01882 01883 case VTableComponent::CK_FunctionPointer: { 01884 const CXXMethodDecl *MD = Component.getFunctionDecl(); 01885 01886 std::string Str = 01887 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, 01888 MD); 01889 Out << Str; 01890 if (MD->isPure()) 01891 Out << " [pure]"; 01892 01893 ThunkInfo Thunk = VTableThunks.lookup(I); 01894 if (!Thunk.isEmpty()) { 01895 // If this function pointer has a return adjustment, dump it. 01896 if (!Thunk.Return.isEmpty()) { 01897 Out << "\n [return adjustment: "; 01898 Out << Thunk.Return.NonVirtual << " non-virtual"; 01899 01900 if (Thunk.Return.VBaseOffsetOffset) { 01901 Out << ", " << Thunk.Return.VBaseOffsetOffset; 01902 Out << " vbase offset offset"; 01903 } 01904 01905 Out << ']'; 01906 } 01907 01908 // If this function pointer has a 'this' pointer adjustment, dump it. 01909 if (!Thunk.This.isEmpty()) { 01910 Out << "\n [this adjustment: "; 01911 Out << Thunk.This.NonVirtual << " non-virtual"; 01912 01913 if (Thunk.This.VCallOffsetOffset) { 01914 Out << ", " << Thunk.This.VCallOffsetOffset; 01915 Out << " vcall offset offset"; 01916 } 01917 01918 Out << ']'; 01919 } 01920 } 01921 01922 break; 01923 } 01924 01925 case VTableComponent::CK_CompleteDtorPointer: 01926 case VTableComponent::CK_DeletingDtorPointer: { 01927 bool IsComplete = 01928 Component.getKind() == VTableComponent::CK_CompleteDtorPointer; 01929 01930 const CXXDestructorDecl *DD = Component.getDestructorDecl(); 01931 01932 Out << DD->getQualifiedNameAsString(); 01933 if (IsComplete) 01934 Out << "() [complete]"; 01935 else 01936 Out << "() [deleting]"; 01937 01938 if (DD->isPure()) 01939 Out << " [pure]"; 01940 01941 ThunkInfo Thunk = VTableThunks.lookup(I); 01942 if (!Thunk.isEmpty()) { 01943 // If this destructor has a 'this' pointer adjustment, dump it. 01944 if (!Thunk.This.isEmpty()) { 01945 Out << "\n [this adjustment: "; 01946 Out << Thunk.This.NonVirtual << " non-virtual"; 01947 01948 if (Thunk.This.VCallOffsetOffset) { 01949 Out << ", " << Thunk.This.VCallOffsetOffset; 01950 Out << " vcall offset offset"; 01951 } 01952 01953 Out << ']'; 01954 } 01955 } 01956 01957 break; 01958 } 01959 01960 case VTableComponent::CK_UnusedFunctionPointer: { 01961 const CXXMethodDecl *MD = Component.getUnusedFunctionDecl(); 01962 01963 std::string Str = 01964 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, 01965 MD); 01966 Out << "[unused] " << Str; 01967 if (MD->isPure()) 01968 Out << " [pure]"; 01969 } 01970 01971 } 01972 01973 Out << '\n'; 01974 01975 // Dump the next address point. 01976 uint64_t NextIndex = Index + 1; 01977 if (AddressPointsByIndex.count(NextIndex)) { 01978 if (AddressPointsByIndex.count(NextIndex) == 1) { 01979 const BaseSubobject &Base = 01980 AddressPointsByIndex.find(NextIndex)->second; 01981 01982 Out << " -- (" << Base.getBase()->getQualifiedNameAsString(); 01983 Out << ", " << Base.getBaseOffset().getQuantity(); 01984 Out << ") vtable address --\n"; 01985 } else { 01986 CharUnits BaseOffset = 01987 AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset(); 01988 01989 // We store the class names in a set to get a stable order. 01990 std::set<std::string> ClassNames; 01991 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I = 01992 AddressPointsByIndex.lower_bound(NextIndex), E = 01993 AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) { 01994 assert(I->second.getBaseOffset() == BaseOffset && 01995 "Invalid base offset!"); 01996 const CXXRecordDecl *RD = I->second.getBase(); 01997 ClassNames.insert(RD->getQualifiedNameAsString()); 01998 } 01999 02000 for (std::set<std::string>::const_iterator I = ClassNames.begin(), 02001 E = ClassNames.end(); I != E; ++I) { 02002 Out << " -- (" << *I; 02003 Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n"; 02004 } 02005 } 02006 } 02007 } 02008 02009 Out << '\n'; 02010 02011 if (isBuildingConstructorVTable()) 02012 return; 02013 02014 if (MostDerivedClass->getNumVBases()) { 02015 // We store the virtual base class names and their offsets in a map to get 02016 // a stable order. 02017 02018 std::map<std::string, CharUnits> ClassNamesAndOffsets; 02019 for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(), 02020 E = VBaseOffsetOffsets.end(); I != E; ++I) { 02021 std::string ClassName = I->first->getQualifiedNameAsString(); 02022 CharUnits OffsetOffset = I->second; 02023 ClassNamesAndOffsets.insert( 02024 std::make_pair(ClassName, OffsetOffset)); 02025 } 02026 02027 Out << "Virtual base offset offsets for '"; 02028 Out << MostDerivedClass->getQualifiedNameAsString() << "' ("; 02029 Out << ClassNamesAndOffsets.size(); 02030 Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n"; 02031 02032 for (std::map<std::string, CharUnits>::const_iterator I = 02033 ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end(); 02034 I != E; ++I) 02035 Out << " " << I->first << " | " << I->second.getQuantity() << '\n'; 02036 02037 Out << "\n"; 02038 } 02039 02040 if (!Thunks.empty()) { 02041 // We store the method names in a map to get a stable order. 02042 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls; 02043 02044 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end(); 02045 I != E; ++I) { 02046 const CXXMethodDecl *MD = I->first; 02047 std::string MethodName = 02048 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, 02049 MD); 02050 02051 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD)); 02052 } 02053 02054 for (std::map<std::string, const CXXMethodDecl *>::const_iterator I = 02055 MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end(); 02056 I != E; ++I) { 02057 const std::string &MethodName = I->first; 02058 const CXXMethodDecl *MD = I->second; 02059 02060 ThunkInfoVectorTy ThunksVector = Thunks[MD]; 02061 std::sort(ThunksVector.begin(), ThunksVector.end()); 02062 02063 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size(); 02064 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n"; 02065 02066 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) { 02067 const ThunkInfo &Thunk = ThunksVector[I]; 02068 02069 Out << llvm::format("%4d | ", I); 02070 02071 // If this function pointer has a return pointer adjustment, dump it. 02072 if (!Thunk.Return.isEmpty()) { 02073 Out << "return adjustment: " << Thunk.This.NonVirtual; 02074 Out << " non-virtual"; 02075 if (Thunk.Return.VBaseOffsetOffset) { 02076 Out << ", " << Thunk.Return.VBaseOffsetOffset; 02077 Out << " vbase offset offset"; 02078 } 02079 02080 if (!Thunk.This.isEmpty()) 02081 Out << "\n "; 02082 } 02083 02084 // If this function pointer has a 'this' pointer adjustment, dump it. 02085 if (!Thunk.This.isEmpty()) { 02086 Out << "this adjustment: "; 02087 Out << Thunk.This.NonVirtual << " non-virtual"; 02088 02089 if (Thunk.This.VCallOffsetOffset) { 02090 Out << ", " << Thunk.This.VCallOffsetOffset; 02091 Out << " vcall offset offset"; 02092 } 02093 } 02094 02095 Out << '\n'; 02096 } 02097 02098 Out << '\n'; 02099 } 02100 } 02101 02102 // Compute the vtable indices for all the member functions. 02103 // Store them in a map keyed by the index so we'll get a sorted table. 02104 std::map<uint64_t, std::string> IndicesMap; 02105 02106 for (CXXRecordDecl::method_iterator i = MostDerivedClass->method_begin(), 02107 e = MostDerivedClass->method_end(); i != e; ++i) { 02108 const CXXMethodDecl *MD = &*i; 02109 02110 // We only want virtual member functions. 02111 if (!MD->isVirtual()) 02112 continue; 02113 02114 std::string MethodName = 02115 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual, 02116 MD); 02117 02118 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 02119 IndicesMap[VTables.getMethodVTableIndex(GlobalDecl(DD, Dtor_Complete))] = 02120 MethodName + " [complete]"; 02121 IndicesMap[VTables.getMethodVTableIndex(GlobalDecl(DD, Dtor_Deleting))] = 02122 MethodName + " [deleting]"; 02123 } else { 02124 IndicesMap[VTables.getMethodVTableIndex(MD)] = MethodName; 02125 } 02126 } 02127 02128 // Print the vtable indices for all the member functions. 02129 if (!IndicesMap.empty()) { 02130 Out << "VTable indices for '"; 02131 Out << MostDerivedClass->getQualifiedNameAsString(); 02132 Out << "' (" << IndicesMap.size() << " entries).\n"; 02133 02134 for (std::map<uint64_t, std::string>::const_iterator I = IndicesMap.begin(), 02135 E = IndicesMap.end(); I != E; ++I) { 02136 uint64_t VTableIndex = I->first; 02137 const std::string &MethodName = I->second; 02138 02139 Out << llvm::format(" %4" PRIu64 " | ", VTableIndex) << MethodName 02140 << '\n'; 02141 } 02142 } 02143 02144 Out << '\n'; 02145 } 02146 02147 } 02148 02149 VTableLayout::VTableLayout(uint64_t NumVTableComponents, 02150 const VTableComponent *VTableComponents, 02151 uint64_t NumVTableThunks, 02152 const VTableThunkTy *VTableThunks, 02153 const AddressPointsMapTy &AddressPoints) 02154 : NumVTableComponents(NumVTableComponents), 02155 VTableComponents(new VTableComponent[NumVTableComponents]), 02156 NumVTableThunks(NumVTableThunks), 02157 VTableThunks(new VTableThunkTy[NumVTableThunks]), 02158 AddressPoints(AddressPoints) { 02159 std::copy(VTableComponents, VTableComponents+NumVTableComponents, 02160 this->VTableComponents.get()); 02161 std::copy(VTableThunks, VTableThunks+NumVTableThunks, 02162 this->VTableThunks.get()); 02163 } 02164 02165 VTableLayout::~VTableLayout() { } 02166 02167 VTableContext::~VTableContext() { 02168 llvm::DeleteContainerSeconds(VTableLayouts); 02169 } 02170 02171 static void 02172 CollectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context, 02173 VTableBuilder::PrimaryBasesSetVectorTy &PrimaryBases) { 02174 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 02175 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 02176 02177 if (!PrimaryBase) 02178 return; 02179 02180 CollectPrimaryBases(PrimaryBase, Context, PrimaryBases); 02181 02182 if (!PrimaryBases.insert(PrimaryBase)) 02183 llvm_unreachable("Found a duplicate primary base!"); 02184 } 02185 02186 void VTableContext::ComputeMethodVTableIndices(const CXXRecordDecl *RD) { 02187 02188 // Itanium C++ ABI 2.5.2: 02189 // The order of the virtual function pointers in a virtual table is the 02190 // order of declaration of the corresponding member functions in the class. 02191 // 02192 // There is an entry for any virtual function declared in a class, 02193 // whether it is a new function or overrides a base class function, 02194 // unless it overrides a function from the primary base, and conversion 02195 // between their return types does not require an adjustment. 02196 02197 int64_t CurrentIndex = 0; 02198 02199 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); 02200 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 02201 02202 if (PrimaryBase) { 02203 assert(PrimaryBase->isCompleteDefinition() && 02204 "Should have the definition decl of the primary base!"); 02205 02206 // Since the record decl shares its vtable pointer with the primary base 02207 // we need to start counting at the end of the primary base's vtable. 02208 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase); 02209 } 02210 02211 // Collect all the primary bases, so we can check whether methods override 02212 // a method from the base. 02213 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases; 02214 CollectPrimaryBases(RD, Context, PrimaryBases); 02215 02216 const CXXDestructorDecl *ImplicitVirtualDtor = 0; 02217 02218 for (CXXRecordDecl::method_iterator i = RD->method_begin(), 02219 e = RD->method_end(); i != e; ++i) { 02220 const CXXMethodDecl *MD = &*i; 02221 02222 // We only want virtual methods. 02223 if (!MD->isVirtual()) 02224 continue; 02225 02226 // Check if this method overrides a method in the primary base. 02227 if (const CXXMethodDecl *OverriddenMD = 02228 FindNearestOverriddenMethod(MD, PrimaryBases)) { 02229 // Check if converting from the return type of the method to the 02230 // return type of the overridden method requires conversion. 02231 if (ComputeReturnAdjustmentBaseOffset(Context, MD, 02232 OverriddenMD).isEmpty()) { 02233 // This index is shared between the index in the vtable of the primary 02234 // base class. 02235 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 02236 const CXXDestructorDecl *OverriddenDD = 02237 cast<CXXDestructorDecl>(OverriddenMD); 02238 02239 // Add both the complete and deleting entries. 02240 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = 02241 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Complete)); 02242 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = 02243 getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting)); 02244 } else { 02245 MethodVTableIndices[MD] = getMethodVTableIndex(OverriddenMD); 02246 } 02247 02248 // We don't need to add an entry for this method. 02249 continue; 02250 } 02251 } 02252 02253 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { 02254 if (MD->isImplicit()) { 02255 assert(!ImplicitVirtualDtor && 02256 "Did already see an implicit virtual dtor!"); 02257 ImplicitVirtualDtor = DD; 02258 continue; 02259 } 02260 02261 // Add the complete dtor. 02262 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++; 02263 02264 // Add the deleting dtor. 02265 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++; 02266 } else { 02267 // Add the entry. 02268 MethodVTableIndices[MD] = CurrentIndex++; 02269 } 02270 } 02271 02272 if (ImplicitVirtualDtor) { 02273 // Itanium C++ ABI 2.5.2: 02274 // If a class has an implicitly-defined virtual destructor, 02275 // its entries come after the declared virtual function pointers. 02276 02277 // Add the complete dtor. 02278 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] = 02279 CurrentIndex++; 02280 02281 // Add the deleting dtor. 02282 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] = 02283 CurrentIndex++; 02284 } 02285 02286 NumVirtualFunctionPointers[RD] = CurrentIndex; 02287 } 02288 02289 uint64_t VTableContext::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) { 02290 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = 02291 NumVirtualFunctionPointers.find(RD); 02292 if (I != NumVirtualFunctionPointers.end()) 02293 return I->second; 02294 02295 ComputeMethodVTableIndices(RD); 02296 02297 I = NumVirtualFunctionPointers.find(RD); 02298 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!"); 02299 return I->second; 02300 } 02301 02302 uint64_t VTableContext::getMethodVTableIndex(GlobalDecl GD) { 02303 MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD); 02304 if (I != MethodVTableIndices.end()) 02305 return I->second; 02306 02307 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); 02308 02309 ComputeMethodVTableIndices(RD); 02310 02311 I = MethodVTableIndices.find(GD); 02312 assert(I != MethodVTableIndices.end() && "Did not find index!"); 02313 return I->second; 02314 } 02315 02316 CharUnits 02317 VTableContext::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, 02318 const CXXRecordDecl *VBase) { 02319 ClassPairTy ClassPair(RD, VBase); 02320 02321 VirtualBaseClassOffsetOffsetsMapTy::iterator I = 02322 VirtualBaseClassOffsetOffsets.find(ClassPair); 02323 if (I != VirtualBaseClassOffsetOffsets.end()) 02324 return I->second; 02325 02326 VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0, 02327 BaseSubobject(RD, CharUnits::Zero()), 02328 /*BaseIsVirtual=*/false, 02329 /*OffsetInLayoutClass=*/CharUnits::Zero()); 02330 02331 for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I = 02332 Builder.getVBaseOffsetOffsets().begin(), 02333 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) { 02334 // Insert all types. 02335 ClassPairTy ClassPair(RD, I->first); 02336 02337 VirtualBaseClassOffsetOffsets.insert( 02338 std::make_pair(ClassPair, I->second)); 02339 } 02340 02341 I = VirtualBaseClassOffsetOffsets.find(ClassPair); 02342 assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!"); 02343 02344 return I->second; 02345 } 02346 02347 static VTableLayout *CreateVTableLayout(const VTableBuilder &Builder) { 02348 SmallVector<VTableLayout::VTableThunkTy, 1> 02349 VTableThunks(Builder.vtable_thunks_begin(), Builder.vtable_thunks_end()); 02350 std::sort(VTableThunks.begin(), VTableThunks.end()); 02351 02352 return new VTableLayout(Builder.getNumVTableComponents(), 02353 Builder.vtable_component_begin(), 02354 VTableThunks.size(), 02355 VTableThunks.data(), 02356 Builder.getAddressPoints()); 02357 } 02358 02359 void VTableContext::ComputeVTableRelatedInformation(const CXXRecordDecl *RD) { 02360 const VTableLayout *&Entry = VTableLayouts[RD]; 02361 02362 // Check if we've computed this information before. 02363 if (Entry) 02364 return; 02365 02366 VTableBuilder Builder(*this, RD, CharUnits::Zero(), 02367 /*MostDerivedClassIsVirtual=*/0, RD); 02368 Entry = CreateVTableLayout(Builder); 02369 02370 // Add the known thunks. 02371 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end()); 02372 02373 // If we don't have the vbase information for this class, insert it. 02374 // getVirtualBaseOffsetOffset will compute it separately without computing 02375 // the rest of the vtable related information. 02376 if (!RD->getNumVBases()) 02377 return; 02378 02379 const RecordType *VBaseRT = 02380 RD->vbases_begin()->getType()->getAs<RecordType>(); 02381 const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl()); 02382 02383 if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase))) 02384 return; 02385 02386 for (VTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I = 02387 Builder.getVBaseOffsetOffsets().begin(), 02388 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) { 02389 // Insert all types. 02390 ClassPairTy ClassPair(RD, I->first); 02391 02392 VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second)); 02393 } 02394 } 02395 02396 VTableLayout *VTableContext::createConstructionVTableLayout( 02397 const CXXRecordDecl *MostDerivedClass, 02398 CharUnits MostDerivedClassOffset, 02399 bool MostDerivedClassIsVirtual, 02400 const CXXRecordDecl *LayoutClass) { 02401 VTableBuilder Builder(*this, MostDerivedClass, MostDerivedClassOffset, 02402 MostDerivedClassIsVirtual, LayoutClass); 02403 return CreateVTableLayout(Builder); 02404 }