blob: 8b90f2848cab5cd6b0bc2a1d584e74f20a837e8b [file] [log] [blame]
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001//===--- 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 Carlssonf942ee02009-11-27 20:47:55 +000016#include "clang/AST/CXXInheritance.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000017#include "clang/AST/RecordLayout.h"
Anders Carlssond420a312009-11-26 19:32:45 +000018#include "llvm/ADT/DenseSet.h"
Zhongxing Xu1721ef72009-11-13 05:46:16 +000019#include <cstdio>
Anders Carlsson2bb27f52009-10-11 22:13:54 +000020
21using namespace clang;
22using namespace CodeGen;
23
Anders Carlssond59885022009-11-27 22:21:51 +000024namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +000025class VtableBuilder {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000026public:
27 /// Index_t - Vtable index type.
28 typedef uint64_t Index_t;
Eli Friedman31bc3ad2009-12-07 23:56:34 +000029 typedef std::vector<std::pair<GlobalDecl,
30 std::pair<GlobalDecl, ThunkAdjustment> > >
31 SavedAdjustmentsVectorTy;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000032private:
Anders Carlsson472404f2009-12-04 16:19:30 +000033
Anders Carlssonfe5f7d92009-12-06 01:09:21 +000034 // VtableComponents - The components of the vtable being built.
35 typedef llvm::SmallVector<llvm::Constant *, 64> VtableComponentsVectorTy;
36 VtableComponentsVectorTy VtableComponents;
37
Eli Friedman6c08ce72009-12-05 01:05:03 +000038 const bool BuildVtable;
39
Anders Carlsson2bb27f52009-10-11 22:13:54 +000040 llvm::Type *Ptr8Ty;
Anders Carlssonbad80eb2009-12-04 18:36:22 +000041
42 /// MostDerivedClass - The most derived class that this vtable is being
43 /// built for.
44 const CXXRecordDecl *MostDerivedClass;
45
Mike Stump83066c82009-11-13 01:54:23 +000046 /// LayoutClass - The most derived class used for virtual base layout
47 /// information.
48 const CXXRecordDecl *LayoutClass;
Mike Stump653d0b92009-11-13 02:13:54 +000049 /// LayoutOffset - The offset for Class in LayoutClass.
50 uint64_t LayoutOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000051 /// 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 Carlssonf2f31f42009-12-04 03:46:21 +000059
Mike Stump90181eb2010-01-26 00:05:04 +000060 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
Anders Carlssonfb4dda42009-11-13 17:08:56 +000061 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stump77537b12010-01-26 03:42:22 +000062 llvm::DenseMap<GlobalDecl, Index_t> VCallOffsetForVCall;
Mike Stumpcd6f9ed2009-11-06 23:27:42 +000063 // This is the offset to the nearest virtual base
Mike Stump90181eb2010-01-26 00:05:04 +000064 llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000065 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stumpbb9ff052009-10-27 23:46:47 +000066
Anders Carlsson323bb042009-11-26 19:54:33 +000067 /// PureVirtualFunction - Points to __cxa_pure_virtual.
68 llvm::Constant *PureVirtualFn;
69
Anders Carlssona84b6e82009-12-04 02:01:07 +000070 /// 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 Friedman8174f2c2009-12-06 22:01:30 +000080 MethodsVectorTy OrigMethods;
Anders Carlssona84b6e82009-12-04 02:01:07 +000081
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 Friedman8174f2c2009-12-06 22:01:30 +000090 OrigMethods.push_back(GD);
Anders Carlssona84b6e82009-12-04 02:01:07 +000091 }
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 Carlssona84b6e82009-12-04 02:01:07 +0000105 // And add the new.
106 MethodToIndexMap[GD] = Index;
107 }
108
Anders Carlsson7bb70762009-12-04 15:49:02 +0000109 /// getIndex - Gives the index of a passed in GlobalDecl. Returns false if
110 /// the index couldn't be found.
Benjamin Kramer62ab6162009-12-04 22:45:27 +0000111 bool getIndex(GlobalDecl GD, uint64_t &Index) const {
Anders Carlsson7bb70762009-12-04 15:49:02 +0000112 llvm::DenseMap<GlobalDecl, uint64_t>::const_iterator i
113 = MethodToIndexMap.find(GD);
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000114
Anders Carlsson7bb70762009-12-04 15:49:02 +0000115 if (i == MethodToIndexMap.end())
116 return false;
Anders Carlssone6096362009-12-04 03:41:37 +0000117
Anders Carlsson7bb70762009-12-04 15:49:02 +0000118 Index = i->second;
119 return true;
Anders Carlssone6096362009-12-04 03:41:37 +0000120 }
121
Eli Friedman8174f2c2009-12-06 22:01:30 +0000122 GlobalDecl getOrigMethod(uint64_t Index) const {
123 return OrigMethods[Index];
124 }
125
Anders Carlssona84b6e82009-12-04 02:01:07 +0000126 MethodsVectorTy::size_type size() const {
127 return Methods.size();
128 }
129
130 void clear() {
131 MethodToIndexMap.clear();
132 Methods.clear();
Eli Friedman8174f2c2009-12-06 22:01:30 +0000133 OrigMethods.clear();
Anders Carlssona84b6e82009-12-04 02:01:07 +0000134 }
135
Anders Carlssone6096362009-12-04 03:41:37 +0000136 GlobalDecl operator[](uint64_t Index) const {
Anders Carlssona84b6e82009-12-04 02:01:07 +0000137 return Methods[Index];
138 }
139 };
140
141 /// Methods - The vtable methods we're currently building.
142 VtableMethods Methods;
143
Anders Carlsson4c837d22009-12-04 02:26:15 +0000144 /// 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 Carlssond420a312009-11-26 19:32:45 +0000148
Eli Friedman31bc3ad2009-12-07 23:56:34 +0000149 SavedAdjustmentsVectorTy SavedAdjustments;
Eli Friedman8174f2c2009-12-06 22:01:30 +0000150
Anders Carlssonc521f952009-12-04 02:22:02 +0000151 /// 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 Carlssond420a312009-11-26 19:32:45 +0000156
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000157 std::vector<Index_t> VCalls;
Mike Stump2cefe382009-11-12 20:47:57 +0000158
159 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000160 // 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 Stump2cefe382009-11-12 20:47:57 +0000163
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000164 /// AddressPoints - Address points for this vtable.
165 CGVtableInfo::AddressPointsMapTy& AddressPoints;
166
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000167 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000168 const uint32_t LLVMPointerWidth;
169 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +0000170 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000171 static llvm::DenseMap<CtorVtable_t, int64_t>&
172 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
173 const CXXRecordDecl *c) {
Anders Carlsson93a18842010-01-02 18:02:32 +0000174 CGVtableInfo::AddrMap_t *&oref = cgm.getVtableInfo().AddressPoints[l];
Mike Stumpcd2b8212009-11-19 20:52:19 +0000175 if (oref == 0)
Anders Carlsson93a18842010-01-02 18:02:32 +0000176 oref = new CGVtableInfo::AddrMap_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000177
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 Carlsson323bb042009-11-26 19:54:33 +0000183
Mike Stump90181eb2010-01-26 00:05:04 +0000184 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 Carlsson72281172010-01-26 17:36:47 +0000360
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 Stump90181eb2010-01-26 00:05:04 +0000371 }
372
Anders Carlsson323bb042009-11-26 19:54:33 +0000373 /// 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 Carlsson2bb27f52009-10-11 22:13:54 +0000385public:
Anders Carlssonbad80eb2009-12-04 18:36:22 +0000386 VtableBuilder(const CXXRecordDecl *MostDerivedClass,
Eli Friedman6c08ce72009-12-05 01:05:03 +0000387 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm,
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000388 bool build, CGVtableInfo::AddressPointsMapTy& AddressPoints)
Eli Friedman6c08ce72009-12-05 01:05:03 +0000389 : 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 Carlssonbad80eb2009-12-04 18:36:22 +0000393 subAddressPoints(AllocAddressPoint(cgm, l, MostDerivedClass)),
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000394 AddressPoints(AddressPoints),
395 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0))
396 {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000397 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000398 if (BuildVtable) {
399 QualType ClassType = CGM.getContext().getTagDeclType(MostDerivedClass);
400 rtti = CGM.GetAddrOfRTTIDescriptor(ClassType);
401 }
Mike Stump90181eb2010-01-26 00:05:04 +0000402 BuildUniqueOverriders(MostDerivedClass);
403 MergeForwarding();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000404 }
405
Anders Carlssonfe5f7d92009-12-06 01:09:21 +0000406 // getVtableComponents - Returns a reference to the vtable components.
407 const VtableComponentsVectorTy &getVtableComponents() const {
408 return VtableComponents;
Anders Carlsson472404f2009-12-04 16:19:30 +0000409 }
410
Anders Carlssone36a6b32010-01-02 01:01:18 +0000411 llvm::DenseMap<const CXXRecordDecl *, uint64_t> &getVBIndex()
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000412 { return VBIndex; }
413
Eli Friedman31bc3ad2009-12-07 23:56:34 +0000414 SavedAdjustmentsVectorTy &getSavedAdjustments()
415 { return SavedAdjustments; }
Eli Friedman8174f2c2009-12-06 22:01:30 +0000416
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000417 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 Stumpd2808442010-01-22 02:51:26 +0000427//#define D1(x)
428#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump75ce5732009-10-31 20:06:59 +0000429
430 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +0000431 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000432 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 Stump28431212009-10-13 22:54:56 +0000436 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000437 if (i->isVirtual() && !SeenVBase.count(Base)) {
438 SeenVBase.insert(Base);
Mike Stump28431212009-10-13 22:54:56 +0000439 if (updateVBIndex) {
Mike Stump75ce5732009-10-31 20:06:59 +0000440 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stump28431212009-10-13 22:54:56 +0000441 - 3*LLVMPointerWidth/8);
442 VBIndex[Base] = next_vbindex;
443 }
Mike Stump75ce5732009-10-31 20:06:59 +0000444 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 Stumpd2808442010-01-22 02:51:26 +0000449 MostDerivedClass->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000450 }
Mike Stump28431212009-10-13 22:54:56 +0000451 // 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 Stump75ce5732009-10-31 20:06:59 +0000456 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000457 }
458 }
459
460 void StartNewTable() {
461 SeenVBase.clear();
462 }
463
Mike Stump8bccbfd2009-10-15 09:30:16 +0000464 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 Stump46271322009-11-03 19:03:17 +0000489 qD = qD->getPointeeType();
490 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000491 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 Carlsson2bb27f52009-10-11 22:13:54 +0000501 /// 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 Stump46271322009-11-03 19:03:17 +0000504 qD = qD->getPointeeType();
505 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000506 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
507 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
Anders Carlssonbad80eb2009-12-04 18:36:22 +0000508 if (D != MostDerivedClass)
Eli Friedman03aa2f12009-11-30 01:19:33 +0000509 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000510 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
511 i = VBIndex.find(B);
512 if (i != VBIndex.end())
513 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000514
Mike Stump28431212009-10-13 22:54:56 +0000515 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000516 return 0;
517 }
518
Eli Friedman81fb0d22009-12-04 08:40:51 +0000519 bool OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
520 Index_t OverrideOffset, Index_t Offset,
521 int64_t CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000522
Anders Carlsson495634e2009-12-04 02:39:04 +0000523 /// AppendMethods - Append the current methods to the vtable.
Anders Carlssonddf42c82009-12-04 02:56:03 +0000524 void AppendMethodsToVtable();
Anders Carlsson495634e2009-12-04 02:39:04 +0000525
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000526 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
527 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
528
Anders Carlsson64457732009-11-24 05:08:52 +0000529 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump18e8b472009-10-27 23:36:26 +0000530
Mike Stumpcdeb8002009-12-03 16:55:20 +0000531 return wrap(CGM.GetAddrOfFunction(GD, Ty));
Mike Stump18e8b472009-10-27 23:36:26 +0000532 }
533
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000534 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
535 int64_t CurrentVBaseOffset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000536 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000537 e = Path->rend(); i != e; ++i) {
538 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000539 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000540 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
541 ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000542 const CXXMethodDecl *MD = *mi;
543
544 if (!MD->isVirtual())
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000545 continue;
546
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000547 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
548 // Override both the complete and the deleting destructor.
549 GlobalDecl CompDtor(DD, Dtor_Complete);
Eli Friedman81fb0d22009-12-04 08:40:51 +0000550 OverrideMethod(CompDtor, MorallyVirtual, OverrideOffset, Offset,
551 CurrentVBaseOffset);
552
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000553 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
Eli Friedman81fb0d22009-12-04 08:40:51 +0000554 OverrideMethod(DeletingDtor, MorallyVirtual, OverrideOffset, Offset,
555 CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000556 } else {
Eli Friedman81fb0d22009-12-04 08:40:51 +0000557 OverrideMethod(MD, MorallyVirtual, OverrideOffset, Offset,
558 CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000559 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000560 }
561 }
562 }
563
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000564 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000565 int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000566 // If we can find a previously allocated slot for this, reuse it.
Eli Friedman81fb0d22009-12-04 08:40:51 +0000567 if (OverrideMethod(GD, MorallyVirtual, Offset, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000568 CurrentVBaseOffset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000569 return;
570
Mike Stump1f49d652010-01-22 18:48:47 +0000571 D1(printf(" vfn for %s at %d\n",
572 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(),
573 (int)Methods.size()));
574
Anders Carlssoncdf18982009-12-04 02:08:24 +0000575 // We didn't find an entry in the vtable that we could use, add a new
576 // entry.
577 Methods.AddMethod(GD);
578
Mike Stumpa04ecfb2010-01-26 21:35:27 +0000579 VCallOffset[GD] = Offset/8 - CurrentVBaseOffset/8;
580
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000581 if (MorallyVirtual) {
Anders Carlsson72281172010-01-26 17:36:47 +0000582 GlobalDecl UGD = getUnique(GD);
583 const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl());
584
Mike Stump90181eb2010-01-26 00:05:04 +0000585 assert(UMD && "final overrider not found");
586
587 Index_t &idx = VCall[UMD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000588 // Allocate the first one, after that, we reuse the previous one.
589 if (idx == 0) {
Anders Carlsson72281172010-01-26 17:36:47 +0000590 VCallOffsetForVCall[UGD] = Offset/8;
Mike Stumpa04ecfb2010-01-26 21:35:27 +0000591 NonVirtualOffset[UMD] = Offset/8 - CurrentVBaseOffset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000592 idx = VCalls.size()+1;
Mike Stump90181eb2010-01-26 00:05:04 +0000593 VCalls.push_back(Offset/8 - CurrentVBaseOffset/8);
Mike Stump75ce5732009-10-31 20:06:59 +0000594 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000595 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(),
Mike Stump90181eb2010-01-26 00:05:04 +0000596 (int)-VCalls.size()-3, (int)VCalls[idx-1]));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000597 }
598 }
599 }
600
601 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000602 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000603 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000604 ++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 Stumpcd6f9ed2009-11-06 23:27:42 +0000613 CurrentVBaseOffset);
Eli Friedman03aa2f12009-11-30 01:19:33 +0000614 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
615 CurrentVBaseOffset);
616 } else
617 AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000618 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000619 }
620
621 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
622 const CXXRecordDecl *PrimaryBase,
623 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000624 int64_t Offset, int64_t CurrentVBaseOffset,
625 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000626 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000627 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 Stump9eb76d42010-01-22 06:45:05 +0000633 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
634 StartNewTable();
635 GenerateVtableForBase(Base, o, MorallyVirtual, false,
636 true, Base == PrimaryBase && !PrimaryBaseWasVirtual,
637 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000638 }
Mike Stump37dbe962009-10-15 02:04:03 +0000639 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000640 }
641
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000642// #define D(X) do { X; } while (0)
643#define D(X)
644
645 void insertVCalls(int InsertionPoint) {
Mike Stump75ce5732009-10-31 20:06:59 +0000646 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000647 D(VCalls.insert(VCalls.begin(), 673));
648 D(VCalls.push_back(672));
Eli Friedman6c08ce72009-12-05 01:05:03 +0000649
Anders Carlssonfe5f7d92009-12-06 01:09:21 +0000650 VtableComponents.insert(VtableComponents.begin() + InsertionPoint,
651 VCalls.size(), 0);
Eli Friedman6c08ce72009-12-05 01:05:03 +0000652 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 Carlssonfe5f7d92009-12-06 01:09:21 +0000657 VtableComponents[InsertionPoint++] = wrap((0?600:0) + *i);
Eli Friedman6c08ce72009-12-05 01:05:03 +0000658 }
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000659 VCalls.clear();
Mike Stump9f23a142009-11-10 02:30:51 +0000660 VCall.clear();
Mike Stumpa04ecfb2010-01-26 21:35:27 +0000661 VCallOffsetForVCall.clear();
662 VCallOffset.clear();
663 NonVirtualOffset.clear();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000664 }
665
Mike Stump559387f2009-11-13 23:13:20 +0000666 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 Stumpd2808442010-01-22 02:51:26 +0000669 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump559387f2009-11-13 23:13:20 +0000670 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000671 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000672 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000673
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 Stumpd2808442010-01-22 02:51:26 +0000686 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump559387f2009-11-13 23:13:20 +0000687 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000688 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000689 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000690 }
691 }
692
693
Mike Stumpd538a6d2009-12-24 07:29:41 +0000694 void FinishGenerateVtable(const CXXRecordDecl *RD,
695 const ASTRecordLayout &Layout,
696 const CXXRecordDecl *PrimaryBase,
Mike Stump9eb76d42010-01-22 06:45:05 +0000697 bool ForNPNVBases, bool WasPrimaryBase,
Mike Stumpd538a6d2009-12-24 07:29:41 +0000698 bool PrimaryBaseWasVirtual,
699 bool MorallyVirtual, int64_t Offset,
700 bool ForVirtualBase, int64_t CurrentVBaseOffset,
701 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000702 bool alloc = false;
703 if (Path == 0) {
704 alloc = true;
705 Path = new Path_t;
706 }
707
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000708 StartNewTable();
709 extra = 0;
Mike Stump9eb76d42010-01-22 06:45:05 +0000710 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 Carlsson2bb27f52009-10-11 22:13:54 +0000720
Mike Stump9eb76d42010-01-22 06:45:05 +0000721 // Add the offset to top.
722 VtableComponents.push_back(BuildVtable ? wrap(-((Offset-LayoutOffset)/8)) : 0);
Anders Carlsson472404f2009-12-04 16:19:30 +0000723
Mike Stump9eb76d42010-01-22 06:45:05 +0000724 // Add the RTTI information.
725 VtableComponents.push_back(rtti);
Anders Carlsson472404f2009-12-04 16:19:30 +0000726
Mike Stump9eb76d42010-01-22 06:45:05 +0000727 AddressPoint = VtableComponents.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000728
Mike Stump9eb76d42010-01-22 06:45:05 +0000729 AppendMethodsToVtable();
730 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000731
732 // and then the non-virtual bases.
733 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000734 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000735
736 if (ForVirtualBase) {
Mike Stump2cefe382009-11-12 20:47:57 +0000737 // 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 Stumpb21c4ee2009-10-14 18:14:51 +0000744 AddressPoint += VCalls.size();
Mike Stump2cefe382009-11-12 20:47:57 +0000745 insertVCalls(VCallInsertionPoint);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000746 }
747
Mike Stump9eb76d42010-01-22 06:45:05 +0000748 if (!ForNPNVBases || !WasPrimaryBase)
749 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump2cefe382009-11-12 20:47:57 +0000750
Mike Stump37dbe962009-10-15 02:04:03 +0000751 if (alloc) {
752 delete Path;
753 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000754 }
755
Mike Stump75ce5732009-10-31 20:06:59 +0000756 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
757 bool updateVBIndex, Index_t current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000758 int64_t CurrentVBaseOffset) {
Mike Stump75ce5732009-10-31 20:06:59 +0000759 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 Friedman65d87222009-12-04 08:52:11 +0000767 if (PrimaryBase && !PrimaryBaseWasVirtual) {
Mike Stump75ce5732009-10-31 20:06:59 +0000768 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000769 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Eli Friedman65d87222009-12-04 08:52:11 +0000770 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
771 updateVBIndex, current_vbindex, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000772 }
773
774 D1(printf(" doing vcall entries for %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000775 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump75ce5732009-10-31 20:06:59 +0000776
777 // And add the virtuals for the class to the primary vtable.
Eli Friedman03aa2f12009-11-30 01:19:33 +0000778 AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000779 }
780
781 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
782 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000783 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000784 bool bottom) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000785 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 Stumpcd6f9ed2009-11-06 23:27:42 +0000794 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
795 if (PrimaryBaseWasVirtual) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000796 IndirectPrimary.insert(PrimaryBase);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000797 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
798 }
Mike Stump75ce5732009-10-31 20:06:59 +0000799
800 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000801 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump75ce5732009-10-31 20:06:59 +0000802
803 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000804 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000805 BaseCurrentVBaseOffset, false);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000806 }
807
Mike Stump75ce5732009-10-31 20:06:59 +0000808 D1(printf(" doing vbase entries for %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000809 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump75ce5732009-10-31 20:06:59 +0000810 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
811
812 if (RDisVirtualBase || bottom) {
813 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000814 CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000815 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000816 }
817
Mike Stumpd538a6d2009-12-24 07:29:41 +0000818 void GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
819 bool MorallyVirtual = false,
820 bool ForVirtualBase = false,
Mike Stump9eb76d42010-01-22 06:45:05 +0000821 bool ForNPNVBases = false,
822 bool WasPrimaryBase = true,
Mike Stumpd538a6d2009-12-24 07:29:41 +0000823 int CurrentVBaseOffset = 0,
824 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000825 if (!RD->isDynamicClass())
Mike Stumpd538a6d2009-12-24 07:29:41 +0000826 return;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000827
Mike Stumpfa818082009-11-13 02:35:38 +0000828 // Construction vtable don't need parts that have no virtual bases and
829 // aren't morally virtual.
Anders Carlssonbad80eb2009-12-04 18:36:22 +0000830 if ((LayoutClass != MostDerivedClass) &&
831 RD->getNumVBases() == 0 && !MorallyVirtual)
Mike Stumpd538a6d2009-12-24 07:29:41 +0000832 return;
Mike Stumpfa818082009-11-13 02:35:38 +0000833
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000834 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
835 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
836 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
837
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000838 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000839 D1(printf("building entries for base %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000840 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000841
Mike Stump75ce5732009-10-31 20:06:59 +0000842 if (ForVirtualBase)
843 extra = VCalls.size();
844
Mike Stump9eb76d42010-01-22 06:45:05 +0000845 if (!ForNPNVBases || !WasPrimaryBase) {
846 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0,
847 ForVirtualBase, CurrentVBaseOffset, true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000848
Mike Stump9eb76d42010-01-22 06:45:05 +0000849 if (Path)
850 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
851 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000852
Mike Stump9eb76d42010-01-22 06:45:05 +0000853 FinishGenerateVtable(RD, Layout, PrimaryBase, ForNPNVBases, WasPrimaryBase,
854 PrimaryBaseWasVirtual, MorallyVirtual, Offset,
855 ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000856 }
857
858 void GenerateVtableForVBases(const CXXRecordDecl *RD,
859 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000860 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000861 bool alloc = false;
862 if (Path == 0) {
863 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000864 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000865 }
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 Stumpb21c4ee2009-10-14 18:14:51 +0000877 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000878 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000879 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +0000880 D1(printf("vtable %s virtual base %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000881 MostDerivedClass->getNameAsCString(), Base->getNameAsCString()));
Mike Stump9eb76d42010-01-22 06:45:05 +0000882 GenerateVtableForBase(Base, BaseOffset, true, true, false,
883 true, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000884 }
Mike Stump2cefe382009-11-12 20:47:57 +0000885 int64_t BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000886 if (i->isVirtual())
887 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2cefe382009-11-12 20:47:57 +0000888 else {
889 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
890 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
891 }
892
Mike Stump37dbe962009-10-15 02:04:03 +0000893 if (Base->getNumVBases()) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000894 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000895 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000896 }
897 Path->pop_back();
898 if (alloc)
899 delete Path;
900 }
901};
Anders Carlssonca1bf682009-12-03 01:54:02 +0000902} // end anonymous namespace
903
Anders Carlsson657f1392009-12-03 02:32:59 +0000904/// TypeConversionRequiresAdjustment - Returns whether conversion from a
905/// derived type to a base type requires adjustment.
906static bool
907TypeConversionRequiresAdjustment(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
942static bool
943TypeConversionRequiresAdjustment(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 Carlssona4424992009-12-30 23:47:56 +0000973 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl());
Anders Carlsson657f1392009-12-03 02:32:59 +0000974
975 const CXXRecordDecl *BaseDecl =
Anders Carlssona4424992009-12-30 23:47:56 +0000976 cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl());
Anders Carlsson657f1392009-12-03 02:32:59 +0000977
978 return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl);
979}
980
Eli Friedman81fb0d22009-12-04 08:40:51 +0000981bool VtableBuilder::OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
982 Index_t OverrideOffset, Index_t Offset,
983 int64_t CurrentVBaseOffset) {
Anders Carlssonca1bf682009-12-03 01:54:02 +0000984 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
985
986 const bool isPure = MD->isPure();
Anders Carlsson21bbc1e2009-12-05 17:04:47 +0000987
Anders Carlssonca1bf682009-12-03 01:54:02 +0000988 // FIXME: Should OverrideOffset's be Offset?
989
Anders Carlsson21bbc1e2009-12-05 17:04:47 +0000990 for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(),
991 e = MD->end_overridden_methods(); mi != e; ++mi) {
Anders Carlssonca1bf682009-12-03 01:54:02 +0000992 GlobalDecl OGD;
Mike Stumpa04ecfb2010-01-26 21:35:27 +0000993 GlobalDecl OGD2;
Anders Carlssonca1bf682009-12-03 01:54:02 +0000994
Anders Carlssonf3935b42009-12-04 05:51:56 +0000995 const CXXMethodDecl *OMD = *mi;
Anders Carlssonca1bf682009-12-03 01:54:02 +0000996 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
997 OGD = GlobalDecl(DD, GD.getDtorType());
998 else
999 OGD = OMD;
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001000
Eli Friedman8174f2c2009-12-06 22:01:30 +00001001 // Check whether this is the method being overridden in this section of
1002 // the vtable.
Anders Carlsson7bb70762009-12-04 15:49:02 +00001003 uint64_t Index;
1004 if (!Methods.getIndex(OGD, Index))
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001005 continue;
1006
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001007 OGD2 = OGD;
1008
Eli Friedman8174f2c2009-12-06 22:01:30 +00001009 // 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 Friedman3d2e9de2009-12-04 08:34:14 +00001014 QualType ReturnType =
1015 MD->getType()->getAs<FunctionType>()->getResultType();
1016 QualType OverriddenReturnType =
1017 OMD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonca1bf682009-12-03 01:54:02 +00001018
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001019 // Check if we need a return type adjustment.
1020 if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType,
1021 OverriddenReturnType)) {
1022 CanQualType &BaseReturnType = BaseReturnTypes[Index];
Anders Carlssonca1bf682009-12-03 01:54:02 +00001023
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001024 // Insert the base return type.
1025 if (BaseReturnType.isNull())
1026 BaseReturnType =
1027 CGM.getContext().getCanonicalType(OverriddenReturnType);
1028 }
Anders Carlssona93e9802009-12-04 03:52:52 +00001029
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001030 Methods.OverrideMethod(OGD, GD);
1031
Anders Carlsson72281172010-01-26 17:36:47 +00001032 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 Stump90181eb2010-01-26 00:05:04 +00001036
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001037 ThisAdjustments.erase(Index);
Mike Stump90181eb2010-01-26 00:05:04 +00001038 if (MorallyVirtual || VCall.count(UMD)) {
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001039
Mike Stump90181eb2010-01-26 00:05:04 +00001040 Index_t &idx = VCall[UMD];
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001041 if (idx == 0) {
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001042 VCallOffset[GD] = VCallOffset[OGD];
1043 // NonVirtualOffset[UMD] = CurrentVBaseOffset/8 - OverrideOffset/8;
1044 NonVirtualOffset[UMD] = VCallOffset[OGD];
Mike Stump77537b12010-01-26 03:42:22 +00001045 VCallOffsetForVCall[UMD] = OverrideOffset/8;
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001046 idx = VCalls.size()+1;
Mike Stump77537b12010-01-26 03:42:22 +00001047 VCalls.push_back(OverrideOffset/8 - CurrentVBaseOffset/8);
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001048 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
1049 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd2808442010-01-22 02:51:26 +00001050 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001051 } else {
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001052 VCallOffset[GD] = NonVirtualOffset[UMD];
Anders Carlsson72281172010-01-26 17:36:47 +00001053 VCalls[idx-1] = -VCallOffsetForVCall[UGD] + OverrideOffset/8;
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001054 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
1055 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd2808442010-01-22 02:51:26 +00001056 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001057 }
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001058 int64_t NonVirtualAdjustment = -VCallOffset[OGD];
Mike Stumpded0a402010-01-26 22:44:01 +00001059 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 Friedman3d2e9de2009-12-04 08:34:14 +00001065 int64_t VirtualAdjustment =
1066 -((idx + extra + 2) * LLVMPointerWidth / 8);
Anders Carlsson657f1392009-12-03 02:32:59 +00001067
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001068 // Optimize out virtual adjustments of 0.
1069 if (VCalls[idx-1] == 0)
1070 VirtualAdjustment = 0;
Anders Carlsson657f1392009-12-03 02:32:59 +00001071
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001072 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
1073 VirtualAdjustment);
Anders Carlsson2ca285f2009-12-03 02:22:59 +00001074
Eli Friedman8174f2c2009-12-06 22:01:30 +00001075 if (!isPure && !ThisAdjustment.isEmpty()) {
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001076 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001077 SavedAdjustments.push_back(
1078 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedman8174f2c2009-12-06 22:01:30 +00001079 }
Anders Carlssonca1bf682009-12-03 01:54:02 +00001080 return true;
1081 }
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001082
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001083 VCallOffset[GD] = VCallOffset[OGD2] - OverrideOffset/8;
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001084
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001085 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 Friedman3d2e9de2009-12-04 08:34:14 +00001093 if (NonVirtualAdjustment) {
1094 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
1095
Eli Friedmanf2eda5e2009-12-06 22:33:51 +00001096 if (!isPure) {
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001097 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001098 SavedAdjustments.push_back(
1099 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedmanf2eda5e2009-12-06 22:33:51 +00001100 }
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001101 }
1102 return true;
Anders Carlssonca1bf682009-12-03 01:54:02 +00001103 }
1104
1105 return false;
Anders Carlssond59885022009-11-27 22:21:51 +00001106}
1107
Anders Carlssonddf42c82009-12-04 02:56:03 +00001108void VtableBuilder::AppendMethodsToVtable() {
Eli Friedman6c08ce72009-12-05 01:05:03 +00001109 if (!BuildVtable) {
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001110 VtableComponents.insert(VtableComponents.end(), Methods.size(),
1111 (llvm::Constant *)0);
Eli Friedman6c08ce72009-12-05 01:05:03 +00001112 ThisAdjustments.clear();
1113 BaseReturnTypes.clear();
1114 Methods.clear();
1115 return;
1116 }
1117
Anders Carlsson472404f2009-12-04 16:19:30 +00001118 // Reserve room in the vtable for our new methods.
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001119 VtableComponents.reserve(VtableComponents.size() + Methods.size());
Anders Carlssonb07567c2009-12-04 03:07:26 +00001120
Anders Carlsson86809cd2009-12-04 02:43:50 +00001121 for (unsigned i = 0, e = Methods.size(); i != e; ++i) {
1122 GlobalDecl GD = Methods[i];
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001123 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1124
1125 // Get the 'this' pointer adjustment.
Anders Carlsson86809cd2009-12-04 02:43:50 +00001126 ThunkAdjustment ThisAdjustment = ThisAdjustments.lookup(i);
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001127
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 Carlsson50f14742009-12-04 03:06:03 +00001146 llvm::Constant *Method = 0;
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001147 if (!ReturnAdjustment.isEmpty()) {
1148 // Build a covariant thunk.
1149 CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment);
Eli Friedman8174f2c2009-12-06 22:01:30 +00001150 Method = wrap(CGM.GetAddrOfCovariantThunk(GD, Adjustment));
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001151 } else if (!ThisAdjustment.isEmpty()) {
1152 // Build a "regular" thunk.
Eli Friedman8174f2c2009-12-06 22:01:30 +00001153 Method = wrap(CGM.GetAddrOfThunk(GD, ThisAdjustment));
Anders Carlssonddf42c82009-12-04 02:56:03 +00001154 } else if (MD->isPure()) {
1155 // We have a pure virtual method.
Anders Carlsson50f14742009-12-04 03:06:03 +00001156 Method = getPureVirtualFn();
1157 } else {
1158 // We have a good old regular method.
1159 Method = WrapAddrOf(GD);
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001160 }
Anders Carlsson50f14742009-12-04 03:06:03 +00001161
1162 // Add the method to the vtable.
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001163 VtableComponents.push_back(Method);
Anders Carlsson86809cd2009-12-04 02:43:50 +00001164 }
1165
Anders Carlsson50f14742009-12-04 03:06:03 +00001166
Anders Carlsson86809cd2009-12-04 02:43:50 +00001167 ThisAdjustments.clear();
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001168 BaseReturnTypes.clear();
Anders Carlsson86809cd2009-12-04 02:43:50 +00001169
Anders Carlsson495634e2009-12-04 02:39:04 +00001170 Methods.clear();
Anders Carlsson495634e2009-12-04 02:39:04 +00001171}
1172
Anders Carlssonf942ee02009-11-27 20:47:55 +00001173void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
1174
1175 // Itanium C++ ABI 2.5.2:
Anders Carlsson259688c2010-02-02 03:37:46 +00001176 // 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 Carlssonf942ee02009-11-27 20:47:55 +00001178 //
Anders Carlsson259688c2010-02-02 03:37:46 +00001179 // 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 Carlssonf942ee02009-11-27 20:47:55 +00001183
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 Carlssonc920fa22009-11-30 19:43:26 +00001190 assert(PrimaryBase->isDefinition() &&
1191 "Should have the definition decl of the primary base!");
Anders Carlssonf942ee02009-11-27 20:47:55 +00001192
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 Friedman21517252009-12-15 03:31:17 +00001197
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 Carlssonf942ee02009-11-27 20:47:55 +00001206 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 Carlssonf3935b42009-12-04 05:51:56 +00001221 const CXXMethodDecl *OverriddenMD = *i;
Anders Carlssonf942ee02009-11-27 20:47:55 +00001222 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
1223 assert(OverriddenMD->isCanonicalDecl() &&
1224 "Should have the canonical decl of the overridden RD!");
1225
Eli Friedman21517252009-12-15 03:31:17 +00001226 if (PrimaryBases.count(OverriddenRD)) {
Anders Carlssonf942ee02009-11-27 20:47:55 +00001227 // 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
1297uint64_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
1310uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001311 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001312 if (I != MethodVtableIndices.end())
1313 return I->second;
1314
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001315 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssonf942ee02009-11-27 20:47:55 +00001316
1317 ComputeMethodVtableIndices(RD);
1318
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001319 I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001320 assert(I != MethodVtableIndices.end() && "Did not find index!");
1321 return I->second;
1322}
1323
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001324CGVtableInfo::AdjustmentVectorTy*
1325CGVtableInfo::getAdjustments(GlobalDecl GD) {
1326 SavedAdjustmentsTy::iterator I = SavedAdjustments.find(GD);
1327 if (I != SavedAdjustments.end())
1328 return &I->second;
Eli Friedman8174f2c2009-12-06 22:01:30 +00001329
1330 const CXXRecordDecl *RD = cast<CXXRecordDecl>(GD.getDecl()->getDeclContext());
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001331 if (!SavedAdjustmentRecords.insert(RD).second)
1332 return 0;
Eli Friedman8174f2c2009-12-06 22:01:30 +00001333
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001334 AddressPointsMapTy AddressPoints;
1335 VtableBuilder b(RD, RD, 0, CGM, false, AddressPoints);
Eli Friedman8174f2c2009-12-06 22:01:30 +00001336 D1(printf("vtable %s\n", RD->getNameAsCString()));
1337 b.GenerateVtableForBase(RD);
1338 b.GenerateVtableForVBases(RD);
Eli Friedman8174f2c2009-12-06 22:01:30 +00001339
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001340 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 Friedman8174f2c2009-12-06 22:01:30 +00001344
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001345 I = SavedAdjustments.find(GD);
1346 if (I != SavedAdjustments.end())
1347 return &I->second;
1348
1349 return 0;
Eli Friedman8174f2c2009-12-06 22:01:30 +00001350}
1351
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001352int64_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 Carlsson2bb27f52009-10-11 22:13:54 +00001361 // FIXME: This seems expensive. Can we do a partial job to get
1362 // just this data.
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001363 AddressPointsMapTy AddressPoints;
1364 VtableBuilder b(RD, RD, 0, CGM, false, AddressPoints);
Mike Stump75ce5732009-10-31 20:06:59 +00001365 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001366 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 Carlssonc8e39ec2009-12-05 21:03:56 +00001383uint64_t CGVtableInfo::getVtableAddressPoint(const CXXRecordDecl *RD) {
1384 uint64_t AddressPoint =
Anders Carlsson93a18842010-01-02 18:02:32 +00001385 (*(*(CGM.getVtableInfo().AddressPoints[RD]))[RD])[std::make_pair(RD, 0)];
Anders Carlssonc8e39ec2009-12-05 21:03:56 +00001386
1387 return AddressPoint;
1388}
1389
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001390llvm::GlobalVariable *
Anders Carlsson0911ae82009-12-06 00:23:49 +00001391CGVtableInfo::GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson232324c2009-12-06 00:53:22 +00001392 bool GenerateDefinition,
Anders Carlsson0911ae82009-12-06 00:23:49 +00001393 const CXXRecordDecl *LayoutClass,
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001394 const CXXRecordDecl *RD, uint64_t Offset,
1395 AddressPointsMapTy& AddressPoints) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001396 llvm::SmallString<256> OutName;
Mike Stump2cefe382009-11-12 20:47:57 +00001397 if (LayoutClass != RD)
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001398 CGM.getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset / 8,
1399 RD, OutName);
Mike Stumpeac45592009-11-11 20:26:26 +00001400 else
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001401 CGM.getMangleContext().mangleCXXVtable(RD, OutName);
Daniel Dunbare128dd12009-11-21 09:06:22 +00001402 llvm::StringRef Name = OutName.str();
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +00001403
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001404 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
Anders Carlsson93a18842010-01-02 18:02:32 +00001405 if (GV == 0 || CGM.getVtableInfo().AddressPoints[LayoutClass] == 0 ||
1406 GV->isDeclaration()) {
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001407 VtableBuilder b(RD, LayoutClass, Offset, CGM, GenerateDefinition,
1408 AddressPoints);
Eli Friedman6c08ce72009-12-05 01:05:03 +00001409
1410 D1(printf("vtable %s\n", RD->getNameAsCString()));
1411 // First comes the vtables for all the non-virtual bases...
Mike Stumpd538a6d2009-12-24 07:29:41 +00001412 b.GenerateVtableForBase(RD, Offset);
Eli Friedman6c08ce72009-12-05 01:05:03 +00001413
1414 // then the vtables for all the virtual bases.
1415 b.GenerateVtableForVBases(RD, Offset);
1416
Anders Carlsson58b271d2009-12-05 22:19:10 +00001417 llvm::Constant *Init = 0;
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001418 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Anders Carlsson58b271d2009-12-05 22:19:10 +00001419 llvm::ArrayType *ArrayType =
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001420 llvm::ArrayType::get(Int8PtrTy, b.getVtableComponents().size());
Anders Carlssona95d4c52009-12-05 21:09:05 +00001421
Anders Carlsson232324c2009-12-06 00:53:22 +00001422 if (GenerateDefinition)
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001423 Init = llvm::ConstantArray::get(ArrayType, &b.getVtableComponents()[0],
1424 b.getVtableComponents().size());
Anders Carlsson232324c2009-12-06 00:53:22 +00001425
Mike Stumpaa51ad62009-11-19 04:04:36 +00001426 llvm::GlobalVariable *OGV = GV;
Anders Carlsson232324c2009-12-06 00:53:22 +00001427
1428 GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType,
1429 /*isConstant=*/true, Linkage, Init, Name);
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001430 CGM.setGlobalVisibility(GV, RD);
1431
Mike Stumpaa51ad62009-11-19 04:04:36 +00001432 if (OGV) {
1433 GV->takeName(OGV);
Anders Carlsson58b271d2009-12-05 22:19:10 +00001434 llvm::Constant *NewPtr =
1435 llvm::ConstantExpr::getBitCast(GV, OGV->getType());
Mike Stumpaa51ad62009-11-19 04:04:36 +00001436 OGV->replaceAllUsesWith(NewPtr);
1437 OGV->eraseFromParent();
1438 }
Mike Stumpaa51ad62009-11-19 04:04:36 +00001439 }
Anders Carlssonb3f54b72009-12-05 21:28:12 +00001440
1441 return GV;
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001442}
Mike Stump9f23a142009-11-10 02:30:51 +00001443
Anders Carlsson232324c2009-12-06 00:53:22 +00001444void CGVtableInfo::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
1445 const CXXRecordDecl *RD) {
Anders Carlssone1b3e622009-12-07 07:59:52 +00001446 llvm::GlobalVariable *&Vtable = Vtables[RD];
1447 if (Vtable) {
1448 assert(Vtable->getInitializer() && "Vtable doesn't have a definition!");
1449 return;
1450 }
1451
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001452 AddressPointsMapTy AddressPoints;
1453 Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0,
1454 AddressPoints);
Anders Carlssone36a6b32010-01-02 01:01:18 +00001455 GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD);
Mike Stump1a139f82009-11-19 01:08:19 +00001456}
1457
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001458llvm::GlobalVariable *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Anders Carlsson232324c2009-12-06 00:53:22 +00001459 llvm::GlobalVariable *Vtable = Vtables.lookup(RD);
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001460
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001461 if (!Vtable) {
1462 AddressPointsMapTy AddressPoints;
Anders Carlsson232324c2009-12-06 00:53:22 +00001463 Vtable = GenerateVtable(llvm::GlobalValue::ExternalLinkage,
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001464 /*GenerateDefinition=*/false, RD, RD, 0,
1465 AddressPoints);
1466 }
Mike Stumpaa51ad62009-11-19 04:04:36 +00001467
Anders Carlsson4ed44eb2009-12-05 22:42:54 +00001468 return Vtable;
Mike Stumpd846d082009-11-10 07:44:33 +00001469}
Mike Stumpeac45592009-11-11 20:26:26 +00001470
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001471void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) {
1472 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1473 const CXXRecordDecl *RD = MD->getParent();
1474
Anders Carlsson4ed44eb2009-12-05 22:42:54 +00001475 // If the class doesn't have a vtable we don't need to emit one.
1476 if (!RD->isDynamicClass())
1477 return;
1478
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001479 // Get the key function.
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001480 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001481
Anders Carlsson4ed44eb2009-12-05 22:42:54 +00001482 if (KeyFunction) {
1483 // We don't have the right key function.
1484 if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
1485 return;
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001486 }
1487
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001488 // Emit the data.
Douglas Gregorccecc1b2010-01-06 20:27:16 +00001489 GenerateClassData(CGM.getVtableLinkage(RD), RD);
Eli Friedman8174f2c2009-12-06 22:01:30 +00001490
1491 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
1492 e = RD->method_end(); i != e; ++i) {
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001493 if ((*i)->isVirtual() && ((*i)->hasInlineBody() || (*i)->isImplicit())) {
Eli Friedman8174f2c2009-12-06 22:01:30 +00001494 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 Carlssonb1d3f7c2009-11-30 23:41:22 +00001502}
1503