Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1 | //===--- CGVtable.cpp - Emit LLVM Code for C++ vtables --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ code generation of virtual tables. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenModule.h" |
| 15 | #include "CodeGenFunction.h" |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseSet.h" |
Zhongxing Xu | 1721ef7 | 2009-11-13 05:46:16 +0000 | [diff] [blame] | 19 | #include <cstdio> |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Anders Carlsson | d5988502 | 2009-11-27 22:21:51 +0000 | [diff] [blame] | 24 | namespace { |
Benjamin Kramer | 337e3a5 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 25 | class VtableBuilder { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 26 | public: |
| 27 | /// Index_t - Vtable index type. |
| 28 | typedef uint64_t Index_t; |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 29 | typedef std::vector<std::pair<GlobalDecl, |
| 30 | std::pair<GlobalDecl, ThunkAdjustment> > > |
| 31 | SavedAdjustmentsVectorTy; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 32 | private: |
Anders Carlsson | 472404f | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 33 | |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 34 | // VtableComponents - The components of the vtable being built. |
| 35 | typedef llvm::SmallVector<llvm::Constant *, 64> VtableComponentsVectorTy; |
| 36 | VtableComponentsVectorTy VtableComponents; |
| 37 | |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 38 | const bool BuildVtable; |
| 39 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 40 | llvm::Type *Ptr8Ty; |
Anders Carlsson | bad80eb | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 41 | |
| 42 | /// MostDerivedClass - The most derived class that this vtable is being |
| 43 | /// built for. |
| 44 | const CXXRecordDecl *MostDerivedClass; |
| 45 | |
Mike Stump | 83066c8 | 2009-11-13 01:54:23 +0000 | [diff] [blame] | 46 | /// LayoutClass - The most derived class used for virtual base layout |
| 47 | /// information. |
| 48 | const CXXRecordDecl *LayoutClass; |
Mike Stump | 653d0b9 | 2009-11-13 02:13:54 +0000 | [diff] [blame] | 49 | /// LayoutOffset - The offset for Class in LayoutClass. |
| 50 | uint64_t LayoutOffset; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 51 | /// BLayout - Layout for the most derived class that this vtable is being |
| 52 | /// built for. |
| 53 | const ASTRecordLayout &BLayout; |
| 54 | llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary; |
| 55 | llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase; |
| 56 | llvm::Constant *rtti; |
| 57 | llvm::LLVMContext &VMContext; |
| 58 | CodeGenModule &CGM; // Per-module state. |
Anders Carlsson | f2f31f4 | 2009-12-04 03:46:21 +0000 | [diff] [blame] | 59 | |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 60 | llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall; |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 61 | llvm::DenseMap<GlobalDecl, Index_t> VCallOffset; |
Mike Stump | 77537b1 | 2010-01-26 03:42:22 +0000 | [diff] [blame] | 62 | llvm::DenseMap<GlobalDecl, Index_t> VCallOffsetForVCall; |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 63 | // This is the offset to the nearest virtual base |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 64 | llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 65 | llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex; |
Mike Stump | bb9ff05 | 2009-10-27 23:46:47 +0000 | [diff] [blame] | 66 | |
Anders Carlsson | 323bb04 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 67 | /// PureVirtualFunction - Points to __cxa_pure_virtual. |
| 68 | llvm::Constant *PureVirtualFn; |
| 69 | |
Anders Carlsson | a84b6e8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 70 | /// VtableMethods - A data structure for keeping track of methods in a vtable. |
| 71 | /// Can add methods, override methods and iterate in vtable order. |
| 72 | class VtableMethods { |
| 73 | // MethodToIndexMap - Maps from a global decl to the index it has in the |
| 74 | // Methods vector. |
| 75 | llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap; |
| 76 | |
| 77 | /// Methods - The methods, in vtable order. |
| 78 | typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy; |
| 79 | MethodsVectorTy Methods; |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 80 | MethodsVectorTy OrigMethods; |
Anders Carlsson | a84b6e8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 81 | |
| 82 | public: |
| 83 | /// AddMethod - Add a method to the vtable methods. |
| 84 | void AddMethod(GlobalDecl GD) { |
| 85 | assert(!MethodToIndexMap.count(GD) && |
| 86 | "Method has already been added!"); |
| 87 | |
| 88 | MethodToIndexMap[GD] = Methods.size(); |
| 89 | Methods.push_back(GD); |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 90 | OrigMethods.push_back(GD); |
Anders Carlsson | a84b6e8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /// OverrideMethod - Replace a method with another. |
| 94 | void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) { |
| 95 | llvm::DenseMap<GlobalDecl, uint64_t>::iterator i |
| 96 | = MethodToIndexMap.find(OverriddenGD); |
| 97 | assert(i != MethodToIndexMap.end() && "Did not find entry!"); |
| 98 | |
| 99 | // Get the index of the old decl. |
| 100 | uint64_t Index = i->second; |
| 101 | |
| 102 | // Replace the old decl with the new decl. |
| 103 | Methods[Index] = GD; |
| 104 | |
Anders Carlsson | a84b6e8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 105 | // And add the new. |
| 106 | MethodToIndexMap[GD] = Index; |
| 107 | } |
| 108 | |
Anders Carlsson | 7bb7076 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 109 | /// getIndex - Gives the index of a passed in GlobalDecl. Returns false if |
| 110 | /// the index couldn't be found. |
Benjamin Kramer | 62ab616 | 2009-12-04 22:45:27 +0000 | [diff] [blame] | 111 | bool getIndex(GlobalDecl GD, uint64_t &Index) const { |
Anders Carlsson | 7bb7076 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 112 | llvm::DenseMap<GlobalDecl, uint64_t>::const_iterator i |
| 113 | = MethodToIndexMap.find(GD); |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 114 | |
Anders Carlsson | 7bb7076 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 115 | if (i == MethodToIndexMap.end()) |
| 116 | return false; |
Anders Carlsson | e609636 | 2009-12-04 03:41:37 +0000 | [diff] [blame] | 117 | |
Anders Carlsson | 7bb7076 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 118 | Index = i->second; |
| 119 | return true; |
Anders Carlsson | e609636 | 2009-12-04 03:41:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 122 | GlobalDecl getOrigMethod(uint64_t Index) const { |
| 123 | return OrigMethods[Index]; |
| 124 | } |
| 125 | |
Anders Carlsson | a84b6e8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 126 | MethodsVectorTy::size_type size() const { |
| 127 | return Methods.size(); |
| 128 | } |
| 129 | |
| 130 | void clear() { |
| 131 | MethodToIndexMap.clear(); |
| 132 | Methods.clear(); |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 133 | OrigMethods.clear(); |
Anders Carlsson | a84b6e8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Anders Carlsson | e609636 | 2009-12-04 03:41:37 +0000 | [diff] [blame] | 136 | GlobalDecl operator[](uint64_t Index) const { |
Anders Carlsson | a84b6e8 | 2009-12-04 02:01:07 +0000 | [diff] [blame] | 137 | return Methods[Index]; |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | /// Methods - The vtable methods we're currently building. |
| 142 | VtableMethods Methods; |
| 143 | |
Anders Carlsson | 4c837d2 | 2009-12-04 02:26:15 +0000 | [diff] [blame] | 144 | /// ThisAdjustments - For a given index in the vtable, contains the 'this' |
| 145 | /// pointer adjustment needed for a method. |
| 146 | typedef llvm::DenseMap<uint64_t, ThunkAdjustment> ThisAdjustmentsMapTy; |
| 147 | ThisAdjustmentsMapTy ThisAdjustments; |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 148 | |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 149 | SavedAdjustmentsVectorTy SavedAdjustments; |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 150 | |
Anders Carlsson | c521f95 | 2009-12-04 02:22:02 +0000 | [diff] [blame] | 151 | /// BaseReturnTypes - Contains the base return types of methods who have been |
| 152 | /// overridden with methods whose return types require adjustment. Used for |
| 153 | /// generating covariant thunk information. |
| 154 | typedef llvm::DenseMap<uint64_t, CanQualType> BaseReturnTypesMapTy; |
| 155 | BaseReturnTypesMapTy BaseReturnTypes; |
Anders Carlsson | d420a31 | 2009-11-26 19:32:45 +0000 | [diff] [blame] | 156 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 157 | std::vector<Index_t> VCalls; |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 158 | |
| 159 | typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t; |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 160 | // subAddressPoints - Used to hold the AddressPoints (offsets) into the built |
| 161 | // vtable for use in computing the initializers for the VTT. |
| 162 | llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints; |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 163 | |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 164 | /// AddressPoints - Address points for this vtable. |
| 165 | CGVtableInfo::AddressPointsMapTy& AddressPoints; |
| 166 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 167 | typedef CXXRecordDecl::method_iterator method_iter; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 168 | const uint32_t LLVMPointerWidth; |
| 169 | Index_t extra; |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 170 | typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t; |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 171 | static llvm::DenseMap<CtorVtable_t, int64_t>& |
| 172 | AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l, |
| 173 | const CXXRecordDecl *c) { |
Anders Carlsson | 93a1884 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 174 | CGVtableInfo::AddrMap_t *&oref = cgm.getVtableInfo().AddressPoints[l]; |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 175 | if (oref == 0) |
Anders Carlsson | 93a1884 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 176 | oref = new CGVtableInfo::AddrMap_t; |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 177 | |
| 178 | llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c]; |
| 179 | if (ref == 0) |
| 180 | ref = new llvm::DenseMap<CtorVtable_t, int64_t>; |
| 181 | return *ref; |
| 182 | } |
Anders Carlsson | 323bb04 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 183 | |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 184 | bool DclIsSame(const FunctionDecl *New, const FunctionDecl *Old) { |
| 185 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
| 186 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 187 | |
| 188 | // C++ [temp.fct]p2: |
| 189 | // A function template can be overloaded with other function templates |
| 190 | // and with normal (non-template) functions. |
| 191 | if ((OldTemplate == 0) != (NewTemplate == 0)) |
| 192 | return false; |
| 193 | |
| 194 | // Is the function New an overload of the function Old? |
| 195 | QualType OldQType = CGM.getContext().getCanonicalType(Old->getType()); |
| 196 | QualType NewQType = CGM.getContext().getCanonicalType(New->getType()); |
| 197 | |
| 198 | // Compare the signatures (C++ 1.3.10) of the two functions to |
| 199 | // determine whether they are overloads. If we find any mismatch |
| 200 | // in the signature, they are overloads. |
| 201 | |
| 202 | // If either of these functions is a K&R-style function (no |
| 203 | // prototype), then we consider them to have matching signatures. |
| 204 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 205 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
| 206 | return true; |
| 207 | |
| 208 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 209 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
| 210 | |
| 211 | // The signature of a function includes the types of its |
| 212 | // parameters (C++ 1.3.10), which includes the presence or absence |
| 213 | // of the ellipsis; see C++ DR 357). |
| 214 | if (OldQType != NewQType && |
| 215 | (OldType->getNumArgs() != NewType->getNumArgs() || |
| 216 | OldType->isVariadic() != NewType->isVariadic() || |
| 217 | !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(), |
| 218 | NewType->arg_type_begin()))) |
| 219 | return false; |
| 220 | |
| 221 | #if 0 |
| 222 | // C++ [temp.over.link]p4: |
| 223 | // The signature of a function template consists of its function |
| 224 | // signature, its return type and its template parameter list. The names |
| 225 | // of the template parameters are significant only for establishing the |
| 226 | // relationship between the template parameters and the rest of the |
| 227 | // signature. |
| 228 | // |
| 229 | // We check the return type and template parameter lists for function |
| 230 | // templates first; the remaining checks follow. |
| 231 | if (NewTemplate && |
| 232 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 233 | OldTemplate->getTemplateParameters(), |
| 234 | TPL_TemplateMatch) || |
| 235 | OldType->getResultType() != NewType->getResultType())) |
| 236 | return false; |
| 237 | #endif |
| 238 | |
| 239 | // If the function is a class member, its signature includes the |
| 240 | // cv-qualifiers (if any) on the function itself. |
| 241 | // |
| 242 | // As part of this, also check whether one of the member functions |
| 243 | // is static, in which case they are not overloads (C++ |
| 244 | // 13.1p2). While not part of the definition of the signature, |
| 245 | // this check is important to determine whether these functions |
| 246 | // can be overloaded. |
| 247 | const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old); |
| 248 | const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New); |
| 249 | if (OldMethod && NewMethod && |
| 250 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
| 251 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
| 252 | return false; |
| 253 | |
| 254 | // The signatures match; this is not an overload. |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | typedef llvm::DenseMap<const CXXMethodDecl *, const CXXMethodDecl*> |
| 259 | ForwardUnique_t; |
| 260 | ForwardUnique_t ForwardUnique; |
| 261 | llvm::DenseMap<const CXXMethodDecl*, const CXXMethodDecl*> UniqueOverrider; |
| 262 | |
| 263 | void BuildUniqueOverrider(const CXXMethodDecl *U, const CXXMethodDecl *MD) { |
| 264 | const CXXMethodDecl *PrevU = UniqueOverrider[MD]; |
| 265 | assert(U && "no unique overrider"); |
| 266 | if (PrevU == U) |
| 267 | return; |
| 268 | if (PrevU != U && PrevU != 0) { |
| 269 | // If already set, note the two sets as the same |
| 270 | if (0) |
| 271 | printf("%s::%s same as %s::%s\n", |
| 272 | PrevU->getParent()->getNameAsCString(), |
| 273 | PrevU->getNameAsCString(), |
| 274 | U->getParent()->getNameAsCString(), |
| 275 | U->getNameAsCString()); |
| 276 | ForwardUnique[PrevU] = U; |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | // Not set, set it now |
| 281 | if (0) |
| 282 | printf("marking %s::%s %p override as %s::%s\n", |
| 283 | MD->getParent()->getNameAsCString(), |
| 284 | MD->getNameAsCString(), |
| 285 | (void*)MD, |
| 286 | U->getParent()->getNameAsCString(), |
| 287 | U->getNameAsCString()); |
| 288 | UniqueOverrider[MD] = U; |
| 289 | |
| 290 | for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(), |
| 291 | me = MD->end_overridden_methods(); mi != me; ++mi) { |
| 292 | BuildUniqueOverrider(U, *mi); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void BuildUniqueOverriders(const CXXRecordDecl *RD) { |
| 297 | if (0) printf("walking %s\n", RD->getNameAsCString()); |
| 298 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 299 | e = RD->method_end(); i != e; ++i) { |
| 300 | const CXXMethodDecl *MD = *i; |
| 301 | if (!MD->isVirtual()) |
| 302 | continue; |
| 303 | |
| 304 | if (UniqueOverrider[MD] == 0) { |
| 305 | // Only set this, if it hasn't been set yet. |
| 306 | BuildUniqueOverrider(MD, MD); |
| 307 | if (0) |
| 308 | printf("top set is %s::%s %p\n", |
| 309 | MD->getParent()->getNameAsCString(), |
| 310 | MD->getNameAsCString(), |
| 311 | (void*)MD); |
| 312 | ForwardUnique[MD] = MD; |
| 313 | } |
| 314 | } |
| 315 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 316 | e = RD->bases_end(); i != e; ++i) { |
| 317 | const CXXRecordDecl *Base = |
| 318 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 319 | BuildUniqueOverriders(Base); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static int DclCmp(const void *p1, const void *p2) { |
| 324 | const CXXMethodDecl *MD1 = (const CXXMethodDecl *)p1; |
| 325 | const CXXMethodDecl *MD2 = (const CXXMethodDecl *)p2; |
| 326 | return (MD1->getIdentifier() - MD2->getIdentifier()); |
| 327 | } |
| 328 | |
| 329 | void MergeForwarding() { |
| 330 | typedef llvm::SmallVector<const CXXMethodDecl *, 100> A_t; |
| 331 | A_t A; |
| 332 | for (ForwardUnique_t::iterator I = ForwardUnique.begin(), |
| 333 | E = ForwardUnique.end(); I != E; ++I) { |
| 334 | if (I->first == I->second) |
| 335 | // Only add the roots of all trees |
| 336 | A.push_back(I->first); |
| 337 | } |
| 338 | llvm::array_pod_sort(A.begin(), A.end(), DclCmp); |
| 339 | for (A_t::iterator I = A.begin(), |
| 340 | E = A.end(); I != E; ++I) { |
| 341 | A_t::iterator J = I; |
| 342 | while (++J != E && DclCmp(*I, *J) == 0) |
| 343 | if (DclIsSame(*I, *J)) { |
| 344 | printf("connecting %s\n", (*I)->getNameAsCString()); |
| 345 | ForwardUnique[*J] = *I; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | const CXXMethodDecl *getUnique(const CXXMethodDecl *MD) { |
| 351 | const CXXMethodDecl *U = UniqueOverrider[MD]; |
| 352 | assert(U && "unique overrider not found"); |
| 353 | while (ForwardUnique.count(U)) { |
| 354 | const CXXMethodDecl *NU = ForwardUnique[U]; |
| 355 | if (NU == U) break; |
| 356 | U = NU; |
| 357 | } |
| 358 | return U; |
| 359 | } |
Anders Carlsson | 7228117 | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 360 | |
| 361 | GlobalDecl getUnique(GlobalDecl GD) { |
| 362 | const CXXMethodDecl *Unique = getUnique(cast<CXXMethodDecl>(GD.getDecl())); |
| 363 | |
| 364 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Unique)) |
| 365 | return GlobalDecl(CD, GD.getCtorType()); |
| 366 | |
| 367 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Unique)) |
| 368 | return GlobalDecl(DD, GD.getDtorType()); |
| 369 | |
| 370 | return Unique; |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Anders Carlsson | 323bb04 | 2009-11-26 19:54:33 +0000 | [diff] [blame] | 373 | /// getPureVirtualFn - Return the __cxa_pure_virtual function. |
| 374 | llvm::Constant* getPureVirtualFn() { |
| 375 | if (!PureVirtualFn) { |
| 376 | const llvm::FunctionType *Ty = |
| 377 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
| 378 | /*isVarArg=*/false); |
| 379 | PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual")); |
| 380 | } |
| 381 | |
| 382 | return PureVirtualFn; |
| 383 | } |
| 384 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 385 | public: |
Anders Carlsson | bad80eb | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 386 | VtableBuilder(const CXXRecordDecl *MostDerivedClass, |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 387 | const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm, |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 388 | bool build, CGVtableInfo::AddressPointsMapTy& AddressPoints) |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 389 | : BuildVtable(build), MostDerivedClass(MostDerivedClass), LayoutClass(l), |
| 390 | LayoutOffset(lo), BLayout(cgm.getContext().getASTRecordLayout(l)), |
| 391 | rtti(0), VMContext(cgm.getModule().getContext()),CGM(cgm), |
| 392 | PureVirtualFn(0), |
Anders Carlsson | bad80eb | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 393 | subAddressPoints(AllocAddressPoint(cgm, l, MostDerivedClass)), |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 394 | AddressPoints(AddressPoints), |
| 395 | LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) |
| 396 | { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 397 | Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); |
Anders Carlsson | 3f4336c | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 398 | if (BuildVtable) { |
| 399 | QualType ClassType = CGM.getContext().getTagDeclType(MostDerivedClass); |
| 400 | rtti = CGM.GetAddrOfRTTIDescriptor(ClassType); |
| 401 | } |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 402 | BuildUniqueOverriders(MostDerivedClass); |
| 403 | MergeForwarding(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 406 | // getVtableComponents - Returns a reference to the vtable components. |
| 407 | const VtableComponentsVectorTy &getVtableComponents() const { |
| 408 | return VtableComponents; |
Anders Carlsson | 472404f | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 411 | llvm::DenseMap<const CXXRecordDecl *, uint64_t> &getVBIndex() |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 412 | { return VBIndex; } |
| 413 | |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 414 | SavedAdjustmentsVectorTy &getSavedAdjustments() |
| 415 | { return SavedAdjustments; } |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 416 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 417 | llvm::Constant *wrap(Index_t i) { |
| 418 | llvm::Constant *m; |
| 419 | m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i); |
| 420 | return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); |
| 421 | } |
| 422 | |
| 423 | llvm::Constant *wrap(llvm::Constant *m) { |
| 424 | return llvm::ConstantExpr::getBitCast(m, Ptr8Ty); |
| 425 | } |
| 426 | |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 427 | //#define D1(x) |
| 428 | #define D1(X) do { if (getenv("DEBUG")) { X; } } while (0) |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 429 | |
| 430 | void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset, |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 431 | bool updateVBIndex, Index_t current_vbindex) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 432 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 433 | e = RD->bases_end(); i != e; ++i) { |
| 434 | const CXXRecordDecl *Base = |
| 435 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 436 | Index_t next_vbindex = current_vbindex; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 437 | if (i->isVirtual() && !SeenVBase.count(Base)) { |
| 438 | SeenVBase.insert(Base); |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 439 | if (updateVBIndex) { |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 440 | next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8) |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 441 | - 3*LLVMPointerWidth/8); |
| 442 | VBIndex[Base] = next_vbindex; |
| 443 | } |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 444 | int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8; |
| 445 | VCalls.push_back((0?700:0) + BaseOffset); |
| 446 | D1(printf(" vbase for %s at %d delta %d most derived %s\n", |
| 447 | Base->getNameAsCString(), |
| 448 | (int)-VCalls.size()-3, (int)BaseOffset, |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 449 | MostDerivedClass->getNameAsCString())); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 450 | } |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 451 | // We also record offsets for non-virtual bases to closest enclosing |
| 452 | // virtual base. We do this so that we don't have to search |
| 453 | // for the nearst virtual base class when generating thunks. |
| 454 | if (updateVBIndex && VBIndex.count(Base) == 0) |
| 455 | VBIndex[Base] = next_vbindex; |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 456 | GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | void StartNewTable() { |
| 461 | SeenVBase.clear(); |
| 462 | } |
| 463 | |
Mike Stump | 8bccbfd | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 464 | Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B, |
| 465 | Index_t Offset = 0) { |
| 466 | |
| 467 | if (B == D) |
| 468 | return Offset; |
| 469 | |
| 470 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D); |
| 471 | for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(), |
| 472 | e = D->bases_end(); i != e; ++i) { |
| 473 | const CXXRecordDecl *Base = |
| 474 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 475 | int64_t BaseOffset = 0; |
| 476 | if (!i->isVirtual()) |
| 477 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 478 | int64_t o = getNVOffset_1(Base, B, BaseOffset); |
| 479 | if (o >= 0) |
| 480 | return o; |
| 481 | } |
| 482 | |
| 483 | return -1; |
| 484 | } |
| 485 | |
| 486 | /// getNVOffset - Returns the non-virtual offset for the given (B) base of the |
| 487 | /// derived class D. |
| 488 | Index_t getNVOffset(QualType qB, QualType qD) { |
Mike Stump | 4627132 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 489 | qD = qD->getPointeeType(); |
| 490 | qB = qB->getPointeeType(); |
Mike Stump | 8bccbfd | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 491 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 492 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
| 493 | int64_t o = getNVOffset_1(D, B); |
| 494 | if (o >= 0) |
| 495 | return o; |
| 496 | |
| 497 | assert(false && "FIXME: non-virtual base not found"); |
| 498 | return 0; |
| 499 | } |
| 500 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 501 | /// getVbaseOffset - Returns the index into the vtable for the virtual base |
| 502 | /// offset for the given (B) virtual base of the derived class D. |
| 503 | Index_t getVbaseOffset(QualType qB, QualType qD) { |
Mike Stump | 4627132 | 2009-11-03 19:03:17 +0000 | [diff] [blame] | 504 | qD = qD->getPointeeType(); |
| 505 | qB = qB->getPointeeType(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 506 | CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); |
| 507 | CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); |
Anders Carlsson | bad80eb | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 508 | if (D != MostDerivedClass) |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 509 | return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 510 | llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i; |
| 511 | i = VBIndex.find(B); |
| 512 | if (i != VBIndex.end()) |
| 513 | return i->second; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 514 | |
Mike Stump | 2843121 | 2009-10-13 22:54:56 +0000 | [diff] [blame] | 515 | assert(false && "FIXME: Base not found"); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 516 | return 0; |
| 517 | } |
| 518 | |
Eli Friedman | 81fb0d2 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 519 | bool OverrideMethod(GlobalDecl GD, bool MorallyVirtual, |
| 520 | Index_t OverrideOffset, Index_t Offset, |
| 521 | int64_t CurrentVBaseOffset); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 522 | |
Anders Carlsson | 495634e | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 523 | /// AppendMethods - Append the current methods to the vtable. |
Anders Carlsson | ddf42c8 | 2009-12-04 02:56:03 +0000 | [diff] [blame] | 524 | void AppendMethodsToVtable(); |
Anders Carlsson | 495634e | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 525 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 526 | llvm::Constant *WrapAddrOf(GlobalDecl GD) { |
| 527 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 528 | |
Anders Carlsson | 6445773 | 2009-11-24 05:08:52 +0000 | [diff] [blame] | 529 | const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD); |
Mike Stump | 18e8b47 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 530 | |
Mike Stump | cdeb800 | 2009-12-03 16:55:20 +0000 | [diff] [blame] | 531 | return wrap(CGM.GetAddrOfFunction(GD, Ty)); |
Mike Stump | 18e8b47 | 2009-10-27 23:36:26 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 534 | void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset, |
| 535 | int64_t CurrentVBaseOffset) { |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 536 | for (Path_t::reverse_iterator i = Path->rbegin(), |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 537 | e = Path->rend(); i != e; ++i) { |
| 538 | const CXXRecordDecl *RD = i->first; |
Mike Stump | 8bccbfd | 2009-10-15 09:30:16 +0000 | [diff] [blame] | 539 | int64_t OverrideOffset = i->second; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 540 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
| 541 | ++mi) { |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 542 | const CXXMethodDecl *MD = *mi; |
| 543 | |
| 544 | if (!MD->isVirtual()) |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 545 | continue; |
| 546 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 547 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 548 | // Override both the complete and the deleting destructor. |
| 549 | GlobalDecl CompDtor(DD, Dtor_Complete); |
Eli Friedman | 81fb0d2 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 550 | OverrideMethod(CompDtor, MorallyVirtual, OverrideOffset, Offset, |
| 551 | CurrentVBaseOffset); |
| 552 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 553 | GlobalDecl DeletingDtor(DD, Dtor_Deleting); |
Eli Friedman | 81fb0d2 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 554 | OverrideMethod(DeletingDtor, MorallyVirtual, OverrideOffset, Offset, |
| 555 | CurrentVBaseOffset); |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 556 | } else { |
Eli Friedman | 81fb0d2 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 557 | OverrideMethod(MD, MorallyVirtual, OverrideOffset, Offset, |
| 558 | CurrentVBaseOffset); |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 559 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 564 | void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 565 | int64_t CurrentVBaseOffset) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 566 | // If we can find a previously allocated slot for this, reuse it. |
Eli Friedman | 81fb0d2 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 567 | if (OverrideMethod(GD, MorallyVirtual, Offset, Offset, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 568 | CurrentVBaseOffset)) |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 569 | return; |
| 570 | |
Mike Stump | 1f49d65 | 2010-01-22 18:48:47 +0000 | [diff] [blame] | 571 | D1(printf(" vfn for %s at %d\n", |
| 572 | dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(), |
| 573 | (int)Methods.size())); |
| 574 | |
Anders Carlsson | cdf1898 | 2009-12-04 02:08:24 +0000 | [diff] [blame] | 575 | // We didn't find an entry in the vtable that we could use, add a new |
| 576 | // entry. |
| 577 | Methods.AddMethod(GD); |
| 578 | |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 579 | VCallOffset[GD] = Offset/8 - CurrentVBaseOffset/8; |
| 580 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 581 | if (MorallyVirtual) { |
Anders Carlsson | 7228117 | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 582 | GlobalDecl UGD = getUnique(GD); |
| 583 | const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl()); |
| 584 | |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 585 | assert(UMD && "final overrider not found"); |
| 586 | |
| 587 | Index_t &idx = VCall[UMD]; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 588 | // Allocate the first one, after that, we reuse the previous one. |
| 589 | if (idx == 0) { |
Anders Carlsson | 7228117 | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 590 | VCallOffsetForVCall[UGD] = Offset/8; |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 591 | NonVirtualOffset[UMD] = Offset/8 - CurrentVBaseOffset/8; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 592 | idx = VCalls.size()+1; |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 593 | VCalls.push_back(Offset/8 - CurrentVBaseOffset/8); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 594 | D1(printf(" vcall for %s at %d with delta %d\n", |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 595 | dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(), |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 596 | (int)-VCalls.size()-3, (int)VCalls[idx-1])); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 602 | Index_t Offset, int64_t CurrentVBaseOffset) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 603 | for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 604 | ++mi) { |
| 605 | const CXXMethodDecl *MD = *mi; |
| 606 | if (!MD->isVirtual()) |
| 607 | continue; |
| 608 | |
| 609 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 610 | // For destructors, add both the complete and the deleting destructor |
| 611 | // to the vtable. |
| 612 | AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 613 | CurrentVBaseOffset); |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 614 | AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset, |
| 615 | CurrentVBaseOffset); |
| 616 | } else |
| 617 | AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 618 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, |
| 622 | const CXXRecordDecl *PrimaryBase, |
| 623 | bool PrimaryBaseWasVirtual, bool MorallyVirtual, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 624 | int64_t Offset, int64_t CurrentVBaseOffset, |
| 625 | Path_t *Path) { |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 626 | Path->push_back(std::make_pair(RD, Offset)); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 627 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 628 | e = RD->bases_end(); i != e; ++i) { |
| 629 | if (i->isVirtual()) |
| 630 | continue; |
| 631 | const CXXRecordDecl *Base = |
| 632 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 633 | uint64_t o = Offset + Layout.getBaseClassOffset(Base); |
| 634 | StartNewTable(); |
| 635 | GenerateVtableForBase(Base, o, MorallyVirtual, false, |
| 636 | true, Base == PrimaryBase && !PrimaryBaseWasVirtual, |
| 637 | CurrentVBaseOffset, Path); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 638 | } |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 639 | Path->pop_back(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 642 | // #define D(X) do { X; } while (0) |
| 643 | #define D(X) |
| 644 | |
| 645 | void insertVCalls(int InsertionPoint) { |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 646 | D1(printf("============= combining vbase/vcall\n")); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 647 | D(VCalls.insert(VCalls.begin(), 673)); |
| 648 | D(VCalls.push_back(672)); |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 649 | |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 650 | VtableComponents.insert(VtableComponents.begin() + InsertionPoint, |
| 651 | VCalls.size(), 0); |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 652 | if (BuildVtable) { |
| 653 | // The vcalls come first... |
| 654 | for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(), |
| 655 | e = VCalls.rend(); |
| 656 | i != e; ++i) |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 657 | VtableComponents[InsertionPoint++] = wrap((0?600:0) + *i); |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 658 | } |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 659 | VCalls.clear(); |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 660 | VCall.clear(); |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 661 | VCallOffsetForVCall.clear(); |
| 662 | VCallOffset.clear(); |
| 663 | NonVirtualOffset.clear(); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Mike Stump | 559387f | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 666 | void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset, |
| 667 | Index_t AddressPoint) { |
| 668 | D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n", |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 669 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString(), |
Mike Stump | 559387f | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 670 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 671 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 672 | AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint; |
Mike Stump | 559387f | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 673 | |
| 674 | // Now also add the address point for all our primary bases. |
| 675 | while (1) { |
| 676 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 677 | RD = Layout.getPrimaryBase(); |
| 678 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 679 | // FIXME: Double check this. |
| 680 | if (RD == 0) |
| 681 | break; |
| 682 | if (PrimaryBaseWasVirtual && |
| 683 | BLayout.getVBaseClassOffset(RD) != Offset) |
| 684 | break; |
| 685 | D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n", |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 686 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString(), |
Mike Stump | 559387f | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 687 | LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint)); |
Mike Stump | cd2b821 | 2009-11-19 20:52:19 +0000 | [diff] [blame] | 688 | subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint; |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 689 | AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint; |
Mike Stump | 559387f | 2009-11-13 23:13:20 +0000 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
| 693 | |
Mike Stump | d538a6d | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 694 | void FinishGenerateVtable(const CXXRecordDecl *RD, |
| 695 | const ASTRecordLayout &Layout, |
| 696 | const CXXRecordDecl *PrimaryBase, |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 697 | bool ForNPNVBases, bool WasPrimaryBase, |
Mike Stump | d538a6d | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 698 | bool PrimaryBaseWasVirtual, |
| 699 | bool MorallyVirtual, int64_t Offset, |
| 700 | bool ForVirtualBase, int64_t CurrentVBaseOffset, |
| 701 | Path_t *Path) { |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 702 | bool alloc = false; |
| 703 | if (Path == 0) { |
| 704 | alloc = true; |
| 705 | Path = new Path_t; |
| 706 | } |
| 707 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 708 | StartNewTable(); |
| 709 | extra = 0; |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 710 | Index_t AddressPoint = 0; |
| 711 | int VCallInsertionPoint = 0; |
| 712 | if (!ForNPNVBases || !WasPrimaryBase) { |
| 713 | bool DeferVCalls = MorallyVirtual || ForVirtualBase; |
| 714 | VCallInsertionPoint = VtableComponents.size(); |
| 715 | if (!DeferVCalls) { |
| 716 | insertVCalls(VCallInsertionPoint); |
| 717 | } else |
| 718 | // FIXME: just for extra, or for all uses of VCalls.size post this? |
| 719 | extra = -VCalls.size(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 720 | |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 721 | // Add the offset to top. |
| 722 | VtableComponents.push_back(BuildVtable ? wrap(-((Offset-LayoutOffset)/8)) : 0); |
Anders Carlsson | 472404f | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 723 | |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 724 | // Add the RTTI information. |
| 725 | VtableComponents.push_back(rtti); |
Anders Carlsson | 472404f | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 726 | |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 727 | AddressPoint = VtableComponents.size(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 728 | |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 729 | AppendMethodsToVtable(); |
| 730 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 731 | |
| 732 | // and then the non-virtual bases. |
| 733 | NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 734 | MorallyVirtual, Offset, CurrentVBaseOffset, Path); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 735 | |
| 736 | if (ForVirtualBase) { |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 737 | // FIXME: We're adding to VCalls in callers, we need to do the overrides |
| 738 | // in the inner part, so that we know the complete set of vcalls during |
| 739 | // the build and don't have to insert into methods. Saving out the |
| 740 | // AddressPoint here, would need to be fixed, if we didn't do that. Also |
| 741 | // retroactively adding vcalls for overrides later wind up in the wrong |
| 742 | // place, the vcall slot has to be alloted during the walk of the base |
| 743 | // when the function is first introduces. |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 744 | AddressPoint += VCalls.size(); |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 745 | insertVCalls(VCallInsertionPoint); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 748 | if (!ForNPNVBases || !WasPrimaryBase) |
| 749 | AddAddressPoints(RD, Offset, AddressPoint); |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 750 | |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 751 | if (alloc) { |
| 752 | delete Path; |
| 753 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 756 | void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 757 | bool updateVBIndex, Index_t current_vbindex, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 758 | int64_t CurrentVBaseOffset) { |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 759 | if (!RD->isDynamicClass()) |
| 760 | return; |
| 761 | |
| 762 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 763 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 764 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 765 | |
| 766 | // vtables are composed from the chain of primaries. |
Eli Friedman | 65d8722 | 2009-12-04 08:52:11 +0000 | [diff] [blame] | 767 | if (PrimaryBase && !PrimaryBaseWasVirtual) { |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 768 | D1(printf(" doing primaries for %s most derived %s\n", |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 769 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Eli Friedman | 65d8722 | 2009-12-04 08:52:11 +0000 | [diff] [blame] | 770 | Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
| 771 | updateVBIndex, current_vbindex, CurrentVBaseOffset); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | D1(printf(" doing vcall entries for %s most derived %s\n", |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 775 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 776 | |
| 777 | // And add the virtuals for the class to the primary vtable. |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 778 | AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset, |
| 782 | bool updateVBIndex, Index_t current_vbindex, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 783 | bool RDisVirtualBase, int64_t CurrentVBaseOffset, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 784 | bool bottom) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 785 | if (!RD->isDynamicClass()) |
| 786 | return; |
| 787 | |
| 788 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 789 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 790 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 791 | |
| 792 | // vtables are composed from the chain of primaries. |
| 793 | if (PrimaryBase) { |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 794 | int BaseCurrentVBaseOffset = CurrentVBaseOffset; |
| 795 | if (PrimaryBaseWasVirtual) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 796 | IndirectPrimary.insert(PrimaryBase); |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 797 | BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase); |
| 798 | } |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 799 | |
| 800 | D1(printf(" doing primaries for %s most derived %s\n", |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 801 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 802 | |
| 803 | VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset, |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 804 | updateVBIndex, current_vbindex, PrimaryBaseWasVirtual, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 805 | BaseCurrentVBaseOffset, false); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 808 | D1(printf(" doing vbase entries for %s most derived %s\n", |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 809 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 810 | GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex); |
| 811 | |
| 812 | if (RDisVirtualBase || bottom) { |
| 813 | Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex, |
Eli Friedman | 03aa2f1 | 2009-11-30 01:19:33 +0000 | [diff] [blame] | 814 | CurrentVBaseOffset); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 815 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Mike Stump | d538a6d | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 818 | void GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0, |
| 819 | bool MorallyVirtual = false, |
| 820 | bool ForVirtualBase = false, |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 821 | bool ForNPNVBases = false, |
| 822 | bool WasPrimaryBase = true, |
Mike Stump | d538a6d | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 823 | int CurrentVBaseOffset = 0, |
| 824 | Path_t *Path = 0) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 825 | if (!RD->isDynamicClass()) |
Mike Stump | d538a6d | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 826 | return; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 827 | |
Mike Stump | fa81808 | 2009-11-13 02:35:38 +0000 | [diff] [blame] | 828 | // Construction vtable don't need parts that have no virtual bases and |
| 829 | // aren't morally virtual. |
Anders Carlsson | bad80eb | 2009-12-04 18:36:22 +0000 | [diff] [blame] | 830 | if ((LayoutClass != MostDerivedClass) && |
| 831 | RD->getNumVBases() == 0 && !MorallyVirtual) |
Mike Stump | d538a6d | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 832 | return; |
Mike Stump | fa81808 | 2009-11-13 02:35:38 +0000 | [diff] [blame] | 833 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 834 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 835 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 836 | const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); |
| 837 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 838 | extra = 0; |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 839 | D1(printf("building entries for base %s most derived %s\n", |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 840 | RD->getNameAsCString(), MostDerivedClass->getNameAsCString())); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 841 | |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 842 | if (ForVirtualBase) |
| 843 | extra = VCalls.size(); |
| 844 | |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 845 | if (!ForNPNVBases || !WasPrimaryBase) { |
| 846 | VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, |
| 847 | ForVirtualBase, CurrentVBaseOffset, true); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 848 | |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 849 | if (Path) |
| 850 | OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset); |
| 851 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 852 | |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 853 | FinishGenerateVtable(RD, Layout, PrimaryBase, ForNPNVBases, WasPrimaryBase, |
| 854 | PrimaryBaseWasVirtual, MorallyVirtual, Offset, |
| 855 | ForVirtualBase, CurrentVBaseOffset, Path); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | void GenerateVtableForVBases(const CXXRecordDecl *RD, |
| 859 | int64_t Offset = 0, |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 860 | Path_t *Path = 0) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 861 | bool alloc = false; |
| 862 | if (Path == 0) { |
| 863 | alloc = true; |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 864 | Path = new Path_t; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 865 | } |
| 866 | // FIXME: We also need to override using all paths to a virtual base, |
| 867 | // right now, we just process the first path |
| 868 | Path->push_back(std::make_pair(RD, Offset)); |
| 869 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 870 | e = RD->bases_end(); i != e; ++i) { |
| 871 | const CXXRecordDecl *Base = |
| 872 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 873 | if (i->isVirtual() && !IndirectPrimary.count(Base)) { |
| 874 | // Mark it so we don't output it twice. |
| 875 | IndirectPrimary.insert(Base); |
| 876 | StartNewTable(); |
Mike Stump | b21c4ee | 2009-10-14 18:14:51 +0000 | [diff] [blame] | 877 | VCall.clear(); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 878 | int64_t BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | cd6f9ed | 2009-11-06 23:27:42 +0000 | [diff] [blame] | 879 | int64_t CurrentVBaseOffset = BaseOffset; |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 880 | D1(printf("vtable %s virtual base %s\n", |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 881 | MostDerivedClass->getNameAsCString(), Base->getNameAsCString())); |
Mike Stump | 9eb76d4 | 2010-01-22 06:45:05 +0000 | [diff] [blame] | 882 | GenerateVtableForBase(Base, BaseOffset, true, true, false, |
| 883 | true, CurrentVBaseOffset, Path); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 884 | } |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 885 | int64_t BaseOffset; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 886 | if (i->isVirtual()) |
| 887 | BaseOffset = BLayout.getVBaseClassOffset(Base); |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 888 | else { |
| 889 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 890 | BaseOffset = Offset + Layout.getBaseClassOffset(Base); |
| 891 | } |
| 892 | |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 893 | if (Base->getNumVBases()) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 894 | GenerateVtableForVBases(Base, BaseOffset, Path); |
Mike Stump | 37dbe96 | 2009-10-15 02:04:03 +0000 | [diff] [blame] | 895 | } |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 896 | } |
| 897 | Path->pop_back(); |
| 898 | if (alloc) |
| 899 | delete Path; |
| 900 | } |
| 901 | }; |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 902 | } // end anonymous namespace |
| 903 | |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 904 | /// TypeConversionRequiresAdjustment - Returns whether conversion from a |
| 905 | /// derived type to a base type requires adjustment. |
| 906 | static bool |
| 907 | TypeConversionRequiresAdjustment(ASTContext &Ctx, |
| 908 | const CXXRecordDecl *DerivedDecl, |
| 909 | const CXXRecordDecl *BaseDecl) { |
| 910 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 911 | /*RecordPaths=*/true, /*DetectVirtual=*/true); |
| 912 | if (!const_cast<CXXRecordDecl *>(DerivedDecl)-> |
| 913 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) { |
| 914 | assert(false && "Class must be derived from the passed in base class!"); |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | // If we found a virtual base we always want to require adjustment. |
| 919 | if (Paths.getDetectedVirtual()) |
| 920 | return true; |
| 921 | |
| 922 | const CXXBasePath &Path = Paths.front(); |
| 923 | |
| 924 | for (size_t Start = 0, End = Path.size(); Start != End; ++Start) { |
| 925 | const CXXBasePathElement &Element = Path[Start]; |
| 926 | |
| 927 | // Check the base class offset. |
| 928 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class); |
| 929 | |
| 930 | const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>(); |
| 931 | const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 932 | |
| 933 | if (Layout.getBaseClassOffset(Base) != 0) { |
| 934 | // This requires an adjustment. |
| 935 | return true; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return false; |
| 940 | } |
| 941 | |
| 942 | static bool |
| 943 | TypeConversionRequiresAdjustment(ASTContext &Ctx, |
| 944 | QualType DerivedType, QualType BaseType) { |
| 945 | // Canonicalize the types. |
| 946 | QualType CanDerivedType = Ctx.getCanonicalType(DerivedType); |
| 947 | QualType CanBaseType = Ctx.getCanonicalType(BaseType); |
| 948 | |
| 949 | assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() && |
| 950 | "Types must have same type class!"); |
| 951 | |
| 952 | if (CanDerivedType == CanBaseType) { |
| 953 | // No adjustment needed. |
| 954 | return false; |
| 955 | } |
| 956 | |
| 957 | if (const ReferenceType *RT = dyn_cast<ReferenceType>(CanDerivedType)) { |
| 958 | CanDerivedType = RT->getPointeeType(); |
| 959 | CanBaseType = cast<ReferenceType>(CanBaseType)->getPointeeType(); |
| 960 | } else if (const PointerType *PT = dyn_cast<PointerType>(CanDerivedType)) { |
| 961 | CanDerivedType = PT->getPointeeType(); |
| 962 | CanBaseType = cast<PointerType>(CanBaseType)->getPointeeType(); |
| 963 | } else { |
| 964 | assert(false && "Unexpected return type!"); |
| 965 | } |
| 966 | |
| 967 | if (CanDerivedType == CanBaseType) { |
| 968 | // No adjustment needed. |
| 969 | return false; |
| 970 | } |
| 971 | |
| 972 | const CXXRecordDecl *DerivedDecl = |
Anders Carlsson | a442499 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 973 | cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl()); |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 974 | |
| 975 | const CXXRecordDecl *BaseDecl = |
Anders Carlsson | a442499 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 976 | cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl()); |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 977 | |
| 978 | return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl); |
| 979 | } |
| 980 | |
Eli Friedman | 81fb0d2 | 2009-12-04 08:40:51 +0000 | [diff] [blame] | 981 | bool VtableBuilder::OverrideMethod(GlobalDecl GD, bool MorallyVirtual, |
| 982 | Index_t OverrideOffset, Index_t Offset, |
| 983 | int64_t CurrentVBaseOffset) { |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 984 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 985 | |
| 986 | const bool isPure = MD->isPure(); |
Anders Carlsson | 21bbc1e | 2009-12-05 17:04:47 +0000 | [diff] [blame] | 987 | |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 988 | // FIXME: Should OverrideOffset's be Offset? |
| 989 | |
Anders Carlsson | 21bbc1e | 2009-12-05 17:04:47 +0000 | [diff] [blame] | 990 | for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(), |
| 991 | e = MD->end_overridden_methods(); mi != e; ++mi) { |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 992 | GlobalDecl OGD; |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 993 | GlobalDecl OGD2; |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 994 | |
Anders Carlsson | f3935b4 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 995 | const CXXMethodDecl *OMD = *mi; |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 996 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD)) |
| 997 | OGD = GlobalDecl(DD, GD.getDtorType()); |
| 998 | else |
| 999 | OGD = OMD; |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1000 | |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1001 | // Check whether this is the method being overridden in this section of |
| 1002 | // the vtable. |
Anders Carlsson | 7bb7076 | 2009-12-04 15:49:02 +0000 | [diff] [blame] | 1003 | uint64_t Index; |
| 1004 | if (!Methods.getIndex(OGD, Index)) |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1005 | continue; |
| 1006 | |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1007 | OGD2 = OGD; |
| 1008 | |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1009 | // Get the original method, which we should be computing thunks, etc, |
| 1010 | // against. |
| 1011 | OGD = Methods.getOrigMethod(Index); |
| 1012 | OMD = cast<CXXMethodDecl>(OGD.getDecl()); |
| 1013 | |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1014 | QualType ReturnType = |
| 1015 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 1016 | QualType OverriddenReturnType = |
| 1017 | OMD->getType()->getAs<FunctionType>()->getResultType(); |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1018 | |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1019 | // Check if we need a return type adjustment. |
| 1020 | if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType, |
| 1021 | OverriddenReturnType)) { |
| 1022 | CanQualType &BaseReturnType = BaseReturnTypes[Index]; |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1023 | |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1024 | // Insert the base return type. |
| 1025 | if (BaseReturnType.isNull()) |
| 1026 | BaseReturnType = |
| 1027 | CGM.getContext().getCanonicalType(OverriddenReturnType); |
| 1028 | } |
Anders Carlsson | a93e980 | 2009-12-04 03:52:52 +0000 | [diff] [blame] | 1029 | |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1030 | Methods.OverrideMethod(OGD, GD); |
| 1031 | |
Anders Carlsson | 7228117 | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 1032 | GlobalDecl UGD = getUnique(GD); |
| 1033 | const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl()); |
| 1034 | assert(UGD.getDecl() && "unique overrider not found"); |
| 1035 | assert(UGD == getUnique(OGD) && "unique overrider not unique"); |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1036 | |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1037 | ThisAdjustments.erase(Index); |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1038 | if (MorallyVirtual || VCall.count(UMD)) { |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1039 | |
Mike Stump | 90181eb | 2010-01-26 00:05:04 +0000 | [diff] [blame] | 1040 | Index_t &idx = VCall[UMD]; |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1041 | if (idx == 0) { |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1042 | VCallOffset[GD] = VCallOffset[OGD]; |
| 1043 | // NonVirtualOffset[UMD] = CurrentVBaseOffset/8 - OverrideOffset/8; |
| 1044 | NonVirtualOffset[UMD] = VCallOffset[OGD]; |
Mike Stump | 77537b1 | 2010-01-26 03:42:22 +0000 | [diff] [blame] | 1045 | VCallOffsetForVCall[UMD] = OverrideOffset/8; |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1046 | idx = VCalls.size()+1; |
Mike Stump | 77537b1 | 2010-01-26 03:42:22 +0000 | [diff] [blame] | 1047 | VCalls.push_back(OverrideOffset/8 - CurrentVBaseOffset/8); |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1048 | D1(printf(" vcall for %s at %d with delta %d most derived %s\n", |
| 1049 | MD->getNameAsString().c_str(), (int)-idx-3, |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1050 | (int)VCalls[idx-1], MostDerivedClass->getNameAsCString())); |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1051 | } else { |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1052 | VCallOffset[GD] = NonVirtualOffset[UMD]; |
Anders Carlsson | 7228117 | 2010-01-26 17:36:47 +0000 | [diff] [blame] | 1053 | VCalls[idx-1] = -VCallOffsetForVCall[UGD] + OverrideOffset/8; |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1054 | D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n", |
| 1055 | MD->getNameAsString().c_str(), (int)-idx-3, |
Mike Stump | d280844 | 2010-01-22 02:51:26 +0000 | [diff] [blame] | 1056 | (int)VCalls[idx-1], MostDerivedClass->getNameAsCString())); |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1057 | } |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1058 | int64_t NonVirtualAdjustment = -VCallOffset[OGD]; |
Mike Stump | ded0a40 | 2010-01-26 22:44:01 +0000 | [diff] [blame] | 1059 | QualType DerivedType = MD->getThisType(CGM.getContext()); |
| 1060 | QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext()); |
| 1061 | int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8); |
| 1062 | if (NonVirtualAdjustment2 != NonVirtualAdjustment) { |
| 1063 | NonVirtualAdjustment = NonVirtualAdjustment2; |
| 1064 | } |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1065 | int64_t VirtualAdjustment = |
| 1066 | -((idx + extra + 2) * LLVMPointerWidth / 8); |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 1067 | |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1068 | // Optimize out virtual adjustments of 0. |
| 1069 | if (VCalls[idx-1] == 0) |
| 1070 | VirtualAdjustment = 0; |
Anders Carlsson | 657f139 | 2009-12-03 02:32:59 +0000 | [diff] [blame] | 1071 | |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1072 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, |
| 1073 | VirtualAdjustment); |
Anders Carlsson | 2ca285f | 2009-12-03 02:22:59 +0000 | [diff] [blame] | 1074 | |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1075 | if (!isPure && !ThisAdjustment.isEmpty()) { |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1076 | ThisAdjustments[Index] = ThisAdjustment; |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1077 | SavedAdjustments.push_back( |
| 1078 | std::make_pair(GD, std::make_pair(OGD, ThisAdjustment))); |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1079 | } |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1080 | return true; |
| 1081 | } |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1082 | |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1083 | VCallOffset[GD] = VCallOffset[OGD2] - OverrideOffset/8; |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1084 | |
Mike Stump | a04ecfb | 2010-01-26 21:35:27 +0000 | [diff] [blame] | 1085 | int64_t NonVirtualAdjustment = -VCallOffset[GD]; |
| 1086 | QualType DerivedType = MD->getThisType(CGM.getContext()); |
| 1087 | QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext()); |
| 1088 | int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8); |
| 1089 | if (NonVirtualAdjustment2 != NonVirtualAdjustment) { |
| 1090 | NonVirtualAdjustment = NonVirtualAdjustment2; |
| 1091 | } |
| 1092 | |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1093 | if (NonVirtualAdjustment) { |
| 1094 | ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0); |
| 1095 | |
Eli Friedman | f2eda5e | 2009-12-06 22:33:51 +0000 | [diff] [blame] | 1096 | if (!isPure) { |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1097 | ThisAdjustments[Index] = ThisAdjustment; |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1098 | SavedAdjustments.push_back( |
| 1099 | std::make_pair(GD, std::make_pair(OGD, ThisAdjustment))); |
Eli Friedman | f2eda5e | 2009-12-06 22:33:51 +0000 | [diff] [blame] | 1100 | } |
Eli Friedman | 3d2e9de | 2009-12-04 08:34:14 +0000 | [diff] [blame] | 1101 | } |
| 1102 | return true; |
Anders Carlsson | ca1bf68 | 2009-12-03 01:54:02 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | return false; |
Anders Carlsson | d5988502 | 2009-11-27 22:21:51 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Anders Carlsson | ddf42c8 | 2009-12-04 02:56:03 +0000 | [diff] [blame] | 1108 | void VtableBuilder::AppendMethodsToVtable() { |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1109 | if (!BuildVtable) { |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1110 | VtableComponents.insert(VtableComponents.end(), Methods.size(), |
| 1111 | (llvm::Constant *)0); |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1112 | ThisAdjustments.clear(); |
| 1113 | BaseReturnTypes.clear(); |
| 1114 | Methods.clear(); |
| 1115 | return; |
| 1116 | } |
| 1117 | |
Anders Carlsson | 472404f | 2009-12-04 16:19:30 +0000 | [diff] [blame] | 1118 | // Reserve room in the vtable for our new methods. |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1119 | VtableComponents.reserve(VtableComponents.size() + Methods.size()); |
Anders Carlsson | b07567c | 2009-12-04 03:07:26 +0000 | [diff] [blame] | 1120 | |
Anders Carlsson | 86809cd | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 1121 | for (unsigned i = 0, e = Methods.size(); i != e; ++i) { |
| 1122 | GlobalDecl GD = Methods[i]; |
Anders Carlsson | 5b3ea9b | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 1123 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 1124 | |
| 1125 | // Get the 'this' pointer adjustment. |
Anders Carlsson | 86809cd | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 1126 | ThunkAdjustment ThisAdjustment = ThisAdjustments.lookup(i); |
Anders Carlsson | 5b3ea9b | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 1127 | |
| 1128 | // Construct the return type adjustment. |
| 1129 | ThunkAdjustment ReturnAdjustment; |
| 1130 | |
| 1131 | QualType BaseReturnType = BaseReturnTypes.lookup(i); |
| 1132 | if (!BaseReturnType.isNull() && !MD->isPure()) { |
| 1133 | QualType DerivedType = |
| 1134 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 1135 | |
| 1136 | int64_t NonVirtualAdjustment = |
| 1137 | getNVOffset(BaseReturnType, DerivedType) / 8; |
| 1138 | |
| 1139 | int64_t VirtualAdjustment = |
| 1140 | getVbaseOffset(BaseReturnType, DerivedType); |
| 1141 | |
| 1142 | ReturnAdjustment = ThunkAdjustment(NonVirtualAdjustment, |
| 1143 | VirtualAdjustment); |
| 1144 | } |
| 1145 | |
Anders Carlsson | 50f1474 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 1146 | llvm::Constant *Method = 0; |
Anders Carlsson | 5b3ea9b | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 1147 | if (!ReturnAdjustment.isEmpty()) { |
| 1148 | // Build a covariant thunk. |
| 1149 | CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment); |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1150 | Method = wrap(CGM.GetAddrOfCovariantThunk(GD, Adjustment)); |
Anders Carlsson | 5b3ea9b | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 1151 | } else if (!ThisAdjustment.isEmpty()) { |
| 1152 | // Build a "regular" thunk. |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1153 | Method = wrap(CGM.GetAddrOfThunk(GD, ThisAdjustment)); |
Anders Carlsson | ddf42c8 | 2009-12-04 02:56:03 +0000 | [diff] [blame] | 1154 | } else if (MD->isPure()) { |
| 1155 | // We have a pure virtual method. |
Anders Carlsson | 50f1474 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 1156 | Method = getPureVirtualFn(); |
| 1157 | } else { |
| 1158 | // We have a good old regular method. |
| 1159 | Method = WrapAddrOf(GD); |
Anders Carlsson | 5b3ea9b | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 1160 | } |
Anders Carlsson | 50f1474 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Add the method to the vtable. |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1163 | VtableComponents.push_back(Method); |
Anders Carlsson | 86809cd | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Anders Carlsson | 50f1474 | 2009-12-04 03:06:03 +0000 | [diff] [blame] | 1166 | |
Anders Carlsson | 86809cd | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 1167 | ThisAdjustments.clear(); |
Anders Carlsson | 5b3ea9b | 2009-12-04 02:52:22 +0000 | [diff] [blame] | 1168 | BaseReturnTypes.clear(); |
Anders Carlsson | 86809cd | 2009-12-04 02:43:50 +0000 | [diff] [blame] | 1169 | |
Anders Carlsson | 495634e | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 1170 | Methods.clear(); |
Anders Carlsson | 495634e | 2009-12-04 02:39:04 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1173 | void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) { |
| 1174 | |
| 1175 | // Itanium C++ ABI 2.5.2: |
Anders Carlsson | 259688c | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 1176 | // The order of the virtual function pointers in a virtual table is the |
| 1177 | // order of declaration of the corresponding member functions in the class. |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1178 | // |
Anders Carlsson | 259688c | 2010-02-02 03:37:46 +0000 | [diff] [blame] | 1179 | // There is an entry for any virtual function declared in a class, |
| 1180 | // whether it is a new function or overrides a base class function, |
| 1181 | // unless it overrides a function from the primary base, and conversion |
| 1182 | // between their return types does not require an adjustment. |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1183 | |
| 1184 | int64_t CurrentIndex = 0; |
| 1185 | |
| 1186 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 1187 | const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); |
| 1188 | |
| 1189 | if (PrimaryBase) { |
Anders Carlsson | c920fa2 | 2009-11-30 19:43:26 +0000 | [diff] [blame] | 1190 | assert(PrimaryBase->isDefinition() && |
| 1191 | "Should have the definition decl of the primary base!"); |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1192 | |
| 1193 | // Since the record decl shares its vtable pointer with the primary base |
| 1194 | // we need to start counting at the end of the primary base's vtable. |
| 1195 | CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase); |
| 1196 | } |
Eli Friedman | 2151725 | 2009-12-15 03:31:17 +0000 | [diff] [blame] | 1197 | |
| 1198 | // Collect all the primary bases, so we can check whether methods override |
| 1199 | // a method from the base. |
| 1200 | llvm::SmallPtrSet<const CXXRecordDecl *, 5> PrimaryBases; |
| 1201 | for (ASTRecordLayout::primary_base_info_iterator |
| 1202 | I = Layout.primary_base_begin(), E = Layout.primary_base_end(); |
| 1203 | I != E; ++I) |
| 1204 | PrimaryBases.insert((*I).getBase()); |
| 1205 | |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1206 | const CXXDestructorDecl *ImplicitVirtualDtor = 0; |
| 1207 | |
| 1208 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 1209 | e = RD->method_end(); i != e; ++i) { |
| 1210 | const CXXMethodDecl *MD = *i; |
| 1211 | |
| 1212 | // We only want virtual methods. |
| 1213 | if (!MD->isVirtual()) |
| 1214 | continue; |
| 1215 | |
| 1216 | bool ShouldAddEntryForMethod = true; |
| 1217 | |
| 1218 | // Check if this method overrides a method in the primary base. |
| 1219 | for (CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(), |
| 1220 | e = MD->end_overridden_methods(); i != e; ++i) { |
Anders Carlsson | f3935b4 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 1221 | const CXXMethodDecl *OverriddenMD = *i; |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1222 | const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent(); |
| 1223 | assert(OverriddenMD->isCanonicalDecl() && |
| 1224 | "Should have the canonical decl of the overridden RD!"); |
| 1225 | |
Eli Friedman | 2151725 | 2009-12-15 03:31:17 +0000 | [diff] [blame] | 1226 | if (PrimaryBases.count(OverriddenRD)) { |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1227 | // Check if converting from the return type of the method to the |
| 1228 | // return type of the overridden method requires conversion. |
| 1229 | QualType ReturnType = |
| 1230 | MD->getType()->getAs<FunctionType>()->getResultType(); |
| 1231 | QualType OverriddenReturnType = |
| 1232 | OverriddenMD->getType()->getAs<FunctionType>()->getResultType(); |
| 1233 | |
| 1234 | if (!TypeConversionRequiresAdjustment(CGM.getContext(), |
| 1235 | ReturnType, OverriddenReturnType)) { |
| 1236 | // This index is shared between the index in the vtable of the primary |
| 1237 | // base class. |
| 1238 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1239 | const CXXDestructorDecl *OverriddenDD = |
| 1240 | cast<CXXDestructorDecl>(OverriddenMD); |
| 1241 | |
| 1242 | // Add both the complete and deleting entries. |
| 1243 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = |
| 1244 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete)); |
| 1245 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = |
| 1246 | getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting)); |
| 1247 | } else { |
| 1248 | MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD); |
| 1249 | } |
| 1250 | |
| 1251 | // We don't need to add an entry for this method. |
| 1252 | ShouldAddEntryForMethod = false; |
| 1253 | break; |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | if (!ShouldAddEntryForMethod) |
| 1259 | continue; |
| 1260 | |
| 1261 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { |
| 1262 | if (MD->isImplicit()) { |
| 1263 | assert(!ImplicitVirtualDtor && |
| 1264 | "Did already see an implicit virtual dtor!"); |
| 1265 | ImplicitVirtualDtor = DD; |
| 1266 | continue; |
| 1267 | } |
| 1268 | |
| 1269 | // Add the complete dtor. |
| 1270 | MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++; |
| 1271 | |
| 1272 | // Add the deleting dtor. |
| 1273 | MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++; |
| 1274 | } else { |
| 1275 | // Add the entry. |
| 1276 | MethodVtableIndices[MD] = CurrentIndex++; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | if (ImplicitVirtualDtor) { |
| 1281 | // Itanium C++ ABI 2.5.2: |
| 1282 | // If a class has an implicitly-defined virtual destructor, |
| 1283 | // its entries come after the declared virtual function pointers. |
| 1284 | |
| 1285 | // Add the complete dtor. |
| 1286 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] = |
| 1287 | CurrentIndex++; |
| 1288 | |
| 1289 | // Add the deleting dtor. |
| 1290 | MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] = |
| 1291 | CurrentIndex++; |
| 1292 | } |
| 1293 | |
| 1294 | NumVirtualFunctionPointers[RD] = CurrentIndex; |
| 1295 | } |
| 1296 | |
| 1297 | uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) { |
| 1298 | llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 1299 | NumVirtualFunctionPointers.find(RD); |
| 1300 | if (I != NumVirtualFunctionPointers.end()) |
| 1301 | return I->second; |
| 1302 | |
| 1303 | ComputeMethodVtableIndices(RD); |
| 1304 | |
| 1305 | I = NumVirtualFunctionPointers.find(RD); |
| 1306 | assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!"); |
| 1307 | return I->second; |
| 1308 | } |
| 1309 | |
| 1310 | uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) { |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1311 | MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1312 | if (I != MethodVtableIndices.end()) |
| 1313 | return I->second; |
| 1314 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1315 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
Anders Carlsson | f942ee0 | 2009-11-27 20:47:55 +0000 | [diff] [blame] | 1316 | |
| 1317 | ComputeMethodVtableIndices(RD); |
| 1318 | |
Anders Carlsson | fb4dda4 | 2009-11-13 17:08:56 +0000 | [diff] [blame] | 1319 | I = MethodVtableIndices.find(GD); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1320 | assert(I != MethodVtableIndices.end() && "Did not find index!"); |
| 1321 | return I->second; |
| 1322 | } |
| 1323 | |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1324 | CGVtableInfo::AdjustmentVectorTy* |
| 1325 | CGVtableInfo::getAdjustments(GlobalDecl GD) { |
| 1326 | SavedAdjustmentsTy::iterator I = SavedAdjustments.find(GD); |
| 1327 | if (I != SavedAdjustments.end()) |
| 1328 | return &I->second; |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1329 | |
| 1330 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(GD.getDecl()->getDeclContext()); |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1331 | if (!SavedAdjustmentRecords.insert(RD).second) |
| 1332 | return 0; |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1333 | |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1334 | AddressPointsMapTy AddressPoints; |
| 1335 | VtableBuilder b(RD, RD, 0, CGM, false, AddressPoints); |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1336 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
| 1337 | b.GenerateVtableForBase(RD); |
| 1338 | b.GenerateVtableForVBases(RD); |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1339 | |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1340 | for (VtableBuilder::SavedAdjustmentsVectorTy::iterator |
| 1341 | i = b.getSavedAdjustments().begin(), |
| 1342 | e = b.getSavedAdjustments().end(); i != e; i++) |
| 1343 | SavedAdjustments[i->first].push_back(i->second); |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1344 | |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1345 | I = SavedAdjustments.find(GD); |
| 1346 | if (I != SavedAdjustments.end()) |
| 1347 | return &I->second; |
| 1348 | |
| 1349 | return 0; |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1352 | int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD, |
| 1353 | const CXXRecordDecl *VBase) { |
| 1354 | ClassPairTy ClassPair(RD, VBase); |
| 1355 | |
| 1356 | VirtualBaseClassIndiciesTy::iterator I = |
| 1357 | VirtualBaseClassIndicies.find(ClassPair); |
| 1358 | if (I != VirtualBaseClassIndicies.end()) |
| 1359 | return I->second; |
| 1360 | |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1361 | // FIXME: This seems expensive. Can we do a partial job to get |
| 1362 | // just this data. |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1363 | AddressPointsMapTy AddressPoints; |
| 1364 | VtableBuilder b(RD, RD, 0, CGM, false, AddressPoints); |
Mike Stump | 75ce573 | 2009-10-31 20:06:59 +0000 | [diff] [blame] | 1365 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1366 | b.GenerateVtableForBase(RD); |
| 1367 | b.GenerateVtableForVBases(RD); |
| 1368 | |
| 1369 | for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I = |
| 1370 | b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) { |
| 1371 | // Insert all types. |
| 1372 | ClassPairTy ClassPair(RD, I->first); |
| 1373 | |
| 1374 | VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second)); |
| 1375 | } |
| 1376 | |
| 1377 | I = VirtualBaseClassIndicies.find(ClassPair); |
| 1378 | assert(I != VirtualBaseClassIndicies.end() && "Did not find index!"); |
| 1379 | |
| 1380 | return I->second; |
| 1381 | } |
| 1382 | |
Anders Carlsson | c8e39ec | 2009-12-05 21:03:56 +0000 | [diff] [blame] | 1383 | uint64_t CGVtableInfo::getVtableAddressPoint(const CXXRecordDecl *RD) { |
| 1384 | uint64_t AddressPoint = |
Anders Carlsson | 93a1884 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 1385 | (*(*(CGM.getVtableInfo().AddressPoints[RD]))[RD])[std::make_pair(RD, 0)]; |
Anders Carlsson | c8e39ec | 2009-12-05 21:03:56 +0000 | [diff] [blame] | 1386 | |
| 1387 | return AddressPoint; |
| 1388 | } |
| 1389 | |
Anders Carlsson | 7e28c5f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 1390 | llvm::GlobalVariable * |
Anders Carlsson | 0911ae8 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 1391 | CGVtableInfo::GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage, |
Anders Carlsson | 232324c | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 1392 | bool GenerateDefinition, |
Anders Carlsson | 0911ae8 | 2009-12-06 00:23:49 +0000 | [diff] [blame] | 1393 | const CXXRecordDecl *LayoutClass, |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1394 | const CXXRecordDecl *RD, uint64_t Offset, |
| 1395 | AddressPointsMapTy& AddressPoints) { |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1396 | llvm::SmallString<256> OutName; |
Mike Stump | 2cefe38 | 2009-11-12 20:47:57 +0000 | [diff] [blame] | 1397 | if (LayoutClass != RD) |
Anders Carlsson | 7e28c5f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 1398 | CGM.getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset / 8, |
| 1399 | RD, OutName); |
Mike Stump | eac4559 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1400 | else |
Anders Carlsson | 7e28c5f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 1401 | CGM.getMangleContext().mangleCXXVtable(RD, OutName); |
Daniel Dunbar | e128dd1 | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 1402 | llvm::StringRef Name = OutName.str(); |
Benjamin Kramer | bb0a07b | 2009-10-11 22:57:54 +0000 | [diff] [blame] | 1403 | |
Anders Carlsson | 7e28c5f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 1404 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
Anders Carlsson | 93a1884 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 1405 | if (GV == 0 || CGM.getVtableInfo().AddressPoints[LayoutClass] == 0 || |
| 1406 | GV->isDeclaration()) { |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1407 | VtableBuilder b(RD, LayoutClass, Offset, CGM, GenerateDefinition, |
| 1408 | AddressPoints); |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1409 | |
| 1410 | D1(printf("vtable %s\n", RD->getNameAsCString())); |
| 1411 | // First comes the vtables for all the non-virtual bases... |
Mike Stump | d538a6d | 2009-12-24 07:29:41 +0000 | [diff] [blame] | 1412 | b.GenerateVtableForBase(RD, Offset); |
Eli Friedman | 6c08ce7 | 2009-12-05 01:05:03 +0000 | [diff] [blame] | 1413 | |
| 1414 | // then the vtables for all the virtual bases. |
| 1415 | b.GenerateVtableForVBases(RD, Offset); |
| 1416 | |
Anders Carlsson | 58b271d | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 1417 | llvm::Constant *Init = 0; |
Anders Carlsson | 7e28c5f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 1418 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
Anders Carlsson | 58b271d | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 1419 | llvm::ArrayType *ArrayType = |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1420 | llvm::ArrayType::get(Int8PtrTy, b.getVtableComponents().size()); |
Anders Carlsson | a95d4c5 | 2009-12-05 21:09:05 +0000 | [diff] [blame] | 1421 | |
Anders Carlsson | 232324c | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 1422 | if (GenerateDefinition) |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1423 | Init = llvm::ConstantArray::get(ArrayType, &b.getVtableComponents()[0], |
| 1424 | b.getVtableComponents().size()); |
Anders Carlsson | 232324c | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 1425 | |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1426 | llvm::GlobalVariable *OGV = GV; |
Anders Carlsson | 232324c | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 1427 | |
| 1428 | GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType, |
| 1429 | /*isConstant=*/true, Linkage, Init, Name); |
Anders Carlsson | fe5f7d9 | 2009-12-06 01:09:21 +0000 | [diff] [blame] | 1430 | CGM.setGlobalVisibility(GV, RD); |
| 1431 | |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1432 | if (OGV) { |
| 1433 | GV->takeName(OGV); |
Anders Carlsson | 58b271d | 2009-12-05 22:19:10 +0000 | [diff] [blame] | 1434 | llvm::Constant *NewPtr = |
| 1435 | llvm::ConstantExpr::getBitCast(GV, OGV->getType()); |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1436 | OGV->replaceAllUsesWith(NewPtr); |
| 1437 | OGV->eraseFromParent(); |
| 1438 | } |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1439 | } |
Anders Carlsson | b3f54b7 | 2009-12-05 21:28:12 +0000 | [diff] [blame] | 1440 | |
| 1441 | return GV; |
Anders Carlsson | 2bb27f5 | 2009-10-11 22:13:54 +0000 | [diff] [blame] | 1442 | } |
Mike Stump | 9f23a14 | 2009-11-10 02:30:51 +0000 | [diff] [blame] | 1443 | |
Anders Carlsson | 232324c | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 1444 | void CGVtableInfo::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage, |
| 1445 | const CXXRecordDecl *RD) { |
Anders Carlsson | e1b3e62 | 2009-12-07 07:59:52 +0000 | [diff] [blame] | 1446 | llvm::GlobalVariable *&Vtable = Vtables[RD]; |
| 1447 | if (Vtable) { |
| 1448 | assert(Vtable->getInitializer() && "Vtable doesn't have a definition!"); |
| 1449 | return; |
| 1450 | } |
| 1451 | |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1452 | AddressPointsMapTy AddressPoints; |
| 1453 | Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0, |
| 1454 | AddressPoints); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1455 | GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD); |
Mike Stump | 1a139f8 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Anders Carlsson | 7e28c5f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 1458 | llvm::GlobalVariable *CGVtableInfo::getVtable(const CXXRecordDecl *RD) { |
Anders Carlsson | 232324c | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 1459 | llvm::GlobalVariable *Vtable = Vtables.lookup(RD); |
Anders Carlsson | 7e28c5f | 2009-12-06 00:01:05 +0000 | [diff] [blame] | 1460 | |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1461 | if (!Vtable) { |
| 1462 | AddressPointsMapTy AddressPoints; |
Anders Carlsson | 232324c | 2009-12-06 00:53:22 +0000 | [diff] [blame] | 1463 | Vtable = GenerateVtable(llvm::GlobalValue::ExternalLinkage, |
Anders Carlsson | 5f9a881 | 2010-01-14 02:29:07 +0000 | [diff] [blame] | 1464 | /*GenerateDefinition=*/false, RD, RD, 0, |
| 1465 | AddressPoints); |
| 1466 | } |
Mike Stump | aa51ad6 | 2009-11-19 04:04:36 +0000 | [diff] [blame] | 1467 | |
Anders Carlsson | 4ed44eb | 2009-12-05 22:42:54 +0000 | [diff] [blame] | 1468 | return Vtable; |
Mike Stump | d846d08 | 2009-11-10 07:44:33 +0000 | [diff] [blame] | 1469 | } |
Mike Stump | eac4559 | 2009-11-11 20:26:26 +0000 | [diff] [blame] | 1470 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1471 | void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) { |
| 1472 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); |
| 1473 | const CXXRecordDecl *RD = MD->getParent(); |
| 1474 | |
Anders Carlsson | 4ed44eb | 2009-12-05 22:42:54 +0000 | [diff] [blame] | 1475 | // If the class doesn't have a vtable we don't need to emit one. |
| 1476 | if (!RD->isDynamicClass()) |
| 1477 | return; |
| 1478 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1479 | // Get the key function. |
Anders Carlsson | 5ebf8b4 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1480 | const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD); |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1481 | |
Anders Carlsson | 4ed44eb | 2009-12-05 22:42:54 +0000 | [diff] [blame] | 1482 | if (KeyFunction) { |
| 1483 | // We don't have the right key function. |
| 1484 | if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl()) |
| 1485 | return; |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1488 | // Emit the data. |
Douglas Gregor | ccecc1b | 2010-01-06 20:27:16 +0000 | [diff] [blame] | 1489 | GenerateClassData(CGM.getVtableLinkage(RD), RD); |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1490 | |
| 1491 | for (CXXRecordDecl::method_iterator i = RD->method_begin(), |
| 1492 | e = RD->method_end(); i != e; ++i) { |
Eli Friedman | 31bc3ad | 2009-12-07 23:56:34 +0000 | [diff] [blame] | 1493 | if ((*i)->isVirtual() && ((*i)->hasInlineBody() || (*i)->isImplicit())) { |
Eli Friedman | 8174f2c | 2009-12-06 22:01:30 +0000 | [diff] [blame] | 1494 | if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(*i)) { |
| 1495 | CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Complete)); |
| 1496 | CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Deleting)); |
| 1497 | } else { |
| 1498 | CGM.BuildThunksForVirtual(GlobalDecl(*i)); |
| 1499 | } |
| 1500 | } |
| 1501 | } |
Anders Carlsson | b1d3f7c | 2009-11-30 23:41:22 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |