]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGVTables.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / clang / lib / CodeGen / CGVTables.cpp
1 //===--- CGVTables.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 "CodeGenFunction.h"
15 #include "CGCXXABI.h"
16 #include "CodeGenModule.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/RecordLayout.h"
19 #include "clang/CodeGen/CGFunctionInfo.h"
20 #include "clang/Frontend/CodeGenOptions.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Transforms/Utils/Cloning.h"
26 #include <algorithm>
27 #include <cstdio>
28
29 using namespace clang;
30 using namespace CodeGen;
31
32 CodeGenVTables::CodeGenVTables(CodeGenModule &CGM)
33   : CGM(CGM), ItaniumVTContext(CGM.getContext()) {
34   if (CGM.getTarget().getCXXABI().isMicrosoft()) {
35     // FIXME: Eventually, we should only have one of V*TContexts available.
36     // Today we use both in the Microsoft ABI as MicrosoftVFTableContext
37     // is not completely supported in CodeGen yet.
38     MicrosoftVTContext.reset(new MicrosoftVTableContext(CGM.getContext()));
39   }
40 }
41
42 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD, 
43                                               const ThunkInfo &Thunk) {
44   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
45
46   // Compute the mangled name.
47   SmallString<256> Name;
48   llvm::raw_svector_ostream Out(Name);
49   if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
50     getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
51                                                       Thunk.This, Out);
52   else
53     getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
54   Out.flush();
55
56   llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
57   return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
58 }
59
60 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
61                                const ThunkInfo &Thunk, llvm::Function *Fn) {
62   CGM.setGlobalVisibility(Fn, MD);
63
64   if (!CGM.getCodeGenOpts().HiddenWeakVTables)
65     return;
66
67   // If the thunk has weak/linkonce linkage, but the function must be
68   // emitted in every translation unit that references it, then we can
69   // emit its thunks with hidden visibility, since its thunks must be
70   // emitted when the function is.
71
72   // This follows CodeGenModule::setTypeVisibility; see the comments
73   // there for explanation.
74
75   if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
76        Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
77       Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
78     return;
79
80   if (MD->getExplicitVisibility(ValueDecl::VisibilityForValue))
81     return;
82
83   switch (MD->getTemplateSpecializationKind()) {
84   case TSK_ExplicitInstantiationDefinition:
85   case TSK_ExplicitInstantiationDeclaration:
86     return;
87
88   case TSK_Undeclared:
89     break;
90
91   case TSK_ExplicitSpecialization:
92   case TSK_ImplicitInstantiation:
93     return;
94     break;
95   }
96
97   // If there's an explicit definition, and that definition is
98   // out-of-line, then we can't assume that all users will have a
99   // definition to emit.
100   const FunctionDecl *Def = 0;
101   if (MD->hasBody(Def) && Def->isOutOfLine())
102     return;
103
104   Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
105 }
106
107 #ifndef NDEBUG
108 static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
109                     const ABIArgInfo &infoR, CanQualType typeR) {
110   return (infoL.getKind() == infoR.getKind() &&
111           (typeL == typeR ||
112            (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
113            (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
114 }
115 #endif
116
117 static RValue PerformReturnAdjustment(CodeGenFunction &CGF,
118                                       QualType ResultType, RValue RV,
119                                       const ThunkInfo &Thunk) {
120   // Emit the return adjustment.
121   bool NullCheckValue = !ResultType->isReferenceType();
122   
123   llvm::BasicBlock *AdjustNull = 0;
124   llvm::BasicBlock *AdjustNotNull = 0;
125   llvm::BasicBlock *AdjustEnd = 0;
126   
127   llvm::Value *ReturnValue = RV.getScalarVal();
128
129   if (NullCheckValue) {
130     AdjustNull = CGF.createBasicBlock("adjust.null");
131     AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
132     AdjustEnd = CGF.createBasicBlock("adjust.end");
133   
134     llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
135     CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
136     CGF.EmitBlock(AdjustNotNull);
137   }
138
139   ReturnValue = CGF.CGM.getCXXABI().performReturnAdjustment(CGF, ReturnValue,
140                                                             Thunk.Return);
141
142   if (NullCheckValue) {
143     CGF.Builder.CreateBr(AdjustEnd);
144     CGF.EmitBlock(AdjustNull);
145     CGF.Builder.CreateBr(AdjustEnd);
146     CGF.EmitBlock(AdjustEnd);
147   
148     llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
149     PHI->addIncoming(ReturnValue, AdjustNotNull);
150     PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()), 
151                      AdjustNull);
152     ReturnValue = PHI;
153   }
154   
155   return RValue::get(ReturnValue);
156 }
157
158 // This function does roughly the same thing as GenerateThunk, but in a
159 // very different way, so that va_start and va_end work correctly.
160 // FIXME: This function assumes "this" is the first non-sret LLVM argument of
161 //        a function, and that there is an alloca built in the entry block
162 //        for all accesses to "this".
163 // FIXME: This function assumes there is only one "ret" statement per function.
164 // FIXME: Cloning isn't correct in the presence of indirect goto!
165 // FIXME: This implementation of thunks bloats codesize by duplicating the
166 //        function definition.  There are alternatives:
167 //        1. Add some sort of stub support to LLVM for cases where we can
168 //           do a this adjustment, then a sibcall.
169 //        2. We could transform the definition to take a va_list instead of an
170 //           actual variable argument list, then have the thunks (including a
171 //           no-op thunk for the regular definition) call va_start/va_end.
172 //           There's a bit of per-call overhead for this solution, but it's
173 //           better for codesize if the definition is long.
174 void CodeGenFunction::GenerateVarArgsThunk(
175                                       llvm::Function *Fn,
176                                       const CGFunctionInfo &FnInfo,
177                                       GlobalDecl GD, const ThunkInfo &Thunk) {
178   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
179   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
180   QualType ResultType = FPT->getResultType();
181
182   // Get the original function
183   assert(FnInfo.isVariadic());
184   llvm::Type *Ty = CGM.getTypes().GetFunctionType(FnInfo);
185   llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
186   llvm::Function *BaseFn = cast<llvm::Function>(Callee);
187
188   // Clone to thunk.
189   llvm::ValueToValueMapTy VMap;
190   llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap,
191                                               /*ModuleLevelChanges=*/false);
192   CGM.getModule().getFunctionList().push_back(NewFn);
193   Fn->replaceAllUsesWith(NewFn);
194   NewFn->takeName(Fn);
195   Fn->eraseFromParent();
196   Fn = NewFn;
197
198   // "Initialize" CGF (minimally).
199   CurFn = Fn;
200
201   // Get the "this" value
202   llvm::Function::arg_iterator AI = Fn->arg_begin();
203   if (CGM.ReturnTypeUsesSRet(FnInfo))
204     ++AI;
205
206   // Find the first store of "this", which will be to the alloca associated
207   // with "this".
208   llvm::Value *ThisPtr = &*AI;
209   llvm::BasicBlock *EntryBB = Fn->begin();
210   llvm::Instruction *ThisStore = 0;
211   for (llvm::BasicBlock::iterator I = EntryBB->begin(), E = EntryBB->end();
212        I != E; I++) {
213     if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
214       ThisStore = cast<llvm::StoreInst>(I);
215       break;
216     }
217   }
218   assert(ThisStore && "Store of this should be in entry block?");
219   // Adjust "this", if necessary.
220   Builder.SetInsertPoint(ThisStore);
221   llvm::Value *AdjustedThisPtr =
222       CGM.getCXXABI().performThisAdjustment(*this, ThisPtr, Thunk.This);
223   ThisStore->setOperand(0, AdjustedThisPtr);
224
225   if (!Thunk.Return.isEmpty()) {
226     // Fix up the returned value, if necessary.
227     for (llvm::Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) {
228       llvm::Instruction *T = I->getTerminator();
229       if (isa<llvm::ReturnInst>(T)) {
230         RValue RV = RValue::get(T->getOperand(0));
231         T->eraseFromParent();
232         Builder.SetInsertPoint(&*I);
233         RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
234         Builder.CreateRet(RV.getScalarVal());
235         break;
236       }
237     }
238   }
239 }
240
241 void CodeGenFunction::StartThunk(llvm::Function *Fn, GlobalDecl GD,
242                                  const CGFunctionInfo &FnInfo) {
243   assert(!CurGD.getDecl() && "CurGD was already set!");
244   CurGD = GD;
245
246   // Build FunctionArgs.
247   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
248   QualType ThisType = MD->getThisType(getContext());
249   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
250   QualType ResultType =
251     CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getResultType();
252   FunctionArgList FunctionArgs;
253
254   // Create the implicit 'this' parameter declaration.
255   CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
256
257   // Add the rest of the parameters.
258   for (FunctionDecl::param_const_iterator I = MD->param_begin(),
259                                           E = MD->param_end();
260        I != E; ++I)
261     FunctionArgs.push_back(*I);
262
263   // Start defining the function.
264   StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
265                 SourceLocation());
266
267   // Since we didn't pass a GlobalDecl to StartFunction, do this ourselves.
268   CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
269   CXXThisValue = CXXABIThisValue;
270 }
271
272 void CodeGenFunction::EmitCallAndReturnForThunk(GlobalDecl GD,
273                                                 llvm::Value *Callee,
274                                                 const ThunkInfo *Thunk) {
275   assert(isa<CXXMethodDecl>(CurGD.getDecl()) &&
276          "Please use a new CGF for this thunk");
277   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
278
279   // Adjust the 'this' pointer if necessary
280   llvm::Value *AdjustedThisPtr = Thunk ? CGM.getCXXABI().performThisAdjustment(
281                                              *this, LoadCXXThis(), Thunk->This)
282                                        : LoadCXXThis();
283
284   // Start building CallArgs.
285   CallArgList CallArgs;
286   QualType ThisType = MD->getThisType(getContext());
287   CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
288
289   if (isa<CXXDestructorDecl>(MD))
290     CGM.getCXXABI().adjustCallArgsForDestructorThunk(*this, GD, CallArgs);
291
292   // Add the rest of the arguments.
293   for (FunctionDecl::param_const_iterator I = MD->param_begin(),
294        E = MD->param_end(); I != E; ++I)
295     EmitDelegateCallArg(CallArgs, *I, (*I)->getLocStart());
296
297   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
298
299 #ifndef NDEBUG
300   const CGFunctionInfo &CallFnInfo =
301     CGM.getTypes().arrangeCXXMethodCall(CallArgs, FPT,
302                                        RequiredArgs::forPrototypePlus(FPT, 1));
303   assert(CallFnInfo.getRegParm() == CurFnInfo->getRegParm() &&
304          CallFnInfo.isNoReturn() == CurFnInfo->isNoReturn() &&
305          CallFnInfo.getCallingConvention() == CurFnInfo->getCallingConvention());
306   assert(isa<CXXDestructorDecl>(MD) || // ignore dtor return types
307          similar(CallFnInfo.getReturnInfo(), CallFnInfo.getReturnType(),
308                  CurFnInfo->getReturnInfo(), CurFnInfo->getReturnType()));
309   assert(CallFnInfo.arg_size() == CurFnInfo->arg_size());
310   for (unsigned i = 0, e = CurFnInfo->arg_size(); i != e; ++i)
311     assert(similar(CallFnInfo.arg_begin()[i].info,
312                    CallFnInfo.arg_begin()[i].type,
313                    CurFnInfo->arg_begin()[i].info,
314                    CurFnInfo->arg_begin()[i].type));
315 #endif
316
317   // Determine whether we have a return value slot to use.
318   QualType ResultType =
319     CGM.getCXXABI().HasThisReturn(GD) ? ThisType : FPT->getResultType();
320   ReturnValueSlot Slot;
321   if (!ResultType->isVoidType() &&
322       CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
323       !hasScalarEvaluationKind(CurFnInfo->getReturnType()))
324     Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
325   
326   // Now emit our call.
327   RValue RV = EmitCall(*CurFnInfo, Callee, Slot, CallArgs, MD);
328   
329   // Consider return adjustment if we have ThunkInfo.
330   if (Thunk && !Thunk->Return.isEmpty())
331     RV = PerformReturnAdjustment(*this, ResultType, RV, *Thunk);
332
333   // Emit return.
334   if (!ResultType->isVoidType() && Slot.isNull())
335     CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
336
337   // Disable the final ARC autorelease.
338   AutoreleaseResult = false;
339
340   FinishFunction();
341 }
342
343 void CodeGenFunction::GenerateThunk(llvm::Function *Fn,
344                                     const CGFunctionInfo &FnInfo,
345                                     GlobalDecl GD, const ThunkInfo &Thunk) {
346   StartThunk(Fn, GD, FnInfo);
347
348   // Get our callee.
349   llvm::Type *Ty =
350     CGM.getTypes().GetFunctionType(CGM.getTypes().arrangeGlobalDeclaration(GD));
351   llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
352
353   // Make the call and return the result.
354   EmitCallAndReturnForThunk(GD, Callee, &Thunk);
355
356   // Set the right linkage.
357   CGM.setFunctionLinkage(GD, Fn);
358   
359   // Set the right visibility.
360   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
361   setThunkVisibility(CGM, MD, Thunk, Fn);
362 }
363
364 void CodeGenVTables::emitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
365                                bool ForVTable) {
366   const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeGlobalDeclaration(GD);
367
368   // FIXME: re-use FnInfo in this computation.
369   llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
370   
371   // Strip off a bitcast if we got one back.
372   if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
373     assert(CE->getOpcode() == llvm::Instruction::BitCast);
374     Entry = CE->getOperand(0);
375   }
376   
377   // There's already a declaration with the same name, check if it has the same
378   // type or if we need to replace it.
379   if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != 
380       CGM.getTypes().GetFunctionTypeForVTable(GD)) {
381     llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
382     
383     // If the types mismatch then we have to rewrite the definition.
384     assert(OldThunkFn->isDeclaration() &&
385            "Shouldn't replace non-declaration");
386
387     // Remove the name from the old thunk function and get a new thunk.
388     OldThunkFn->setName(StringRef());
389     Entry = CGM.GetAddrOfThunk(GD, Thunk);
390     
391     // If needed, replace the old thunk with a bitcast.
392     if (!OldThunkFn->use_empty()) {
393       llvm::Constant *NewPtrForOldDecl =
394         llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
395       OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
396     }
397     
398     // Remove the old thunk.
399     OldThunkFn->eraseFromParent();
400   }
401
402   llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
403   bool ABIHasKeyFunctions = CGM.getTarget().getCXXABI().hasKeyFunctions();
404   bool UseAvailableExternallyLinkage = ForVTable && ABIHasKeyFunctions;
405
406   if (!ThunkFn->isDeclaration()) {
407     if (!ABIHasKeyFunctions || UseAvailableExternallyLinkage) {
408       // There is already a thunk emitted for this function, do nothing.
409       return;
410     }
411
412     // Change the linkage.
413     CGM.setFunctionLinkage(GD, ThunkFn);
414     return;
415   }
416
417   CGM.SetLLVMFunctionAttributesForDefinition(GD.getDecl(), ThunkFn);
418
419   if (ThunkFn->isVarArg()) {
420     // Varargs thunks are special; we can't just generate a call because
421     // we can't copy the varargs.  Our implementation is rather
422     // expensive/sucky at the moment, so don't generate the thunk unless
423     // we have to.
424     // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
425     if (!UseAvailableExternallyLinkage) {
426       CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
427       CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable);
428     }
429   } else {
430     // Normal thunk body generation.
431     CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
432     CGM.getCXXABI().setThunkLinkage(ThunkFn, ForVTable);
433   }
434 }
435
436 void CodeGenVTables::maybeEmitThunkForVTable(GlobalDecl GD,
437                                              const ThunkInfo &Thunk) {
438   // If the ABI has key functions, only the TU with the key function should emit
439   // the thunk. However, we can allow inlining of thunks if we emit them with
440   // available_externally linkage together with vtables when optimizations are
441   // enabled.
442   if (CGM.getTarget().getCXXABI().hasKeyFunctions() &&
443       !CGM.getCodeGenOpts().OptimizationLevel)
444     return;
445
446   // We can't emit thunks for member functions with incomplete types.
447   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
448   if (!CGM.getTypes().isFuncTypeConvertible(
449            MD->getType()->castAs<FunctionType>()))
450     return;
451
452   emitThunk(GD, Thunk, /*ForVTable=*/true);
453 }
454
455 void CodeGenVTables::EmitThunks(GlobalDecl GD)
456 {
457   const CXXMethodDecl *MD = 
458     cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
459
460   // We don't need to generate thunks for the base destructor.
461   if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
462     return;
463
464   const VTableContextBase::ThunkInfoVectorTy *ThunkInfoVector;
465   if (MicrosoftVTContext.isValid()) {
466     ThunkInfoVector = MicrosoftVTContext->getThunkInfo(GD);
467   } else {
468     ThunkInfoVector = ItaniumVTContext.getThunkInfo(GD);
469   }
470
471   if (!ThunkInfoVector)
472     return;
473
474   for (unsigned I = 0, E = ThunkInfoVector->size(); I != E; ++I)
475     emitThunk(GD, (*ThunkInfoVector)[I], /*ForVTable=*/false);
476 }
477
478 llvm::Constant *
479 CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
480                                         const VTableComponent *Components, 
481                                         unsigned NumComponents,
482                                 const VTableLayout::VTableThunkTy *VTableThunks,
483                                         unsigned NumVTableThunks) {
484   SmallVector<llvm::Constant *, 64> Inits;
485
486   llvm::Type *Int8PtrTy = CGM.Int8PtrTy;
487   
488   llvm::Type *PtrDiffTy = 
489     CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
490
491   QualType ClassType = CGM.getContext().getTagDeclType(RD);
492   llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
493   
494   unsigned NextVTableThunkIndex = 0;
495   
496   llvm::Constant *PureVirtualFn = 0, *DeletedVirtualFn = 0;
497
498   for (unsigned I = 0; I != NumComponents; ++I) {
499     VTableComponent Component = Components[I];
500
501     llvm::Constant *Init = 0;
502
503     switch (Component.getKind()) {
504     case VTableComponent::CK_VCallOffset:
505       Init = llvm::ConstantInt::get(PtrDiffTy, 
506                                     Component.getVCallOffset().getQuantity());
507       Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
508       break;
509     case VTableComponent::CK_VBaseOffset:
510       Init = llvm::ConstantInt::get(PtrDiffTy, 
511                                     Component.getVBaseOffset().getQuantity());
512       Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
513       break;
514     case VTableComponent::CK_OffsetToTop:
515       Init = llvm::ConstantInt::get(PtrDiffTy, 
516                                     Component.getOffsetToTop().getQuantity());
517       Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
518       break;
519     case VTableComponent::CK_RTTI:
520       Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
521       break;
522     case VTableComponent::CK_FunctionPointer:
523     case VTableComponent::CK_CompleteDtorPointer:
524     case VTableComponent::CK_DeletingDtorPointer: {
525       GlobalDecl GD;
526       
527       // Get the right global decl.
528       switch (Component.getKind()) {
529       default:
530         llvm_unreachable("Unexpected vtable component kind");
531       case VTableComponent::CK_FunctionPointer:
532         GD = Component.getFunctionDecl();
533         break;
534       case VTableComponent::CK_CompleteDtorPointer:
535         GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
536         break;
537       case VTableComponent::CK_DeletingDtorPointer:
538         GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
539         break;
540       }
541
542       if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
543         // We have a pure virtual member function.
544         if (!PureVirtualFn) {
545           llvm::FunctionType *Ty = 
546             llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
547           StringRef PureCallName = CGM.getCXXABI().GetPureVirtualCallName();
548           PureVirtualFn = CGM.CreateRuntimeFunction(Ty, PureCallName);
549           PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
550                                                          CGM.Int8PtrTy);
551         }
552         Init = PureVirtualFn;
553       } else if (cast<CXXMethodDecl>(GD.getDecl())->isDeleted()) {
554         if (!DeletedVirtualFn) {
555           llvm::FunctionType *Ty =
556             llvm::FunctionType::get(CGM.VoidTy, /*isVarArg=*/false);
557           StringRef DeletedCallName =
558             CGM.getCXXABI().GetDeletedVirtualCallName();
559           DeletedVirtualFn = CGM.CreateRuntimeFunction(Ty, DeletedCallName);
560           DeletedVirtualFn = llvm::ConstantExpr::getBitCast(DeletedVirtualFn,
561                                                          CGM.Int8PtrTy);
562         }
563         Init = DeletedVirtualFn;
564       } else {
565         // Check if we should use a thunk.
566         if (NextVTableThunkIndex < NumVTableThunks &&
567             VTableThunks[NextVTableThunkIndex].first == I) {
568           const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
569         
570           maybeEmitThunkForVTable(GD, Thunk);
571           Init = CGM.GetAddrOfThunk(GD, Thunk);
572
573           NextVTableThunkIndex++;
574         } else {
575           llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
576         
577           Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
578         }
579
580         Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
581       }
582       break;
583     }
584
585     case VTableComponent::CK_UnusedFunctionPointer:
586       Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
587       break;
588     };
589     
590     Inits.push_back(Init);
591   }
592   
593   llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
594   return llvm::ConstantArray::get(ArrayType, Inits);
595 }
596
597 llvm::GlobalVariable *
598 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD, 
599                                       const BaseSubobject &Base, 
600                                       bool BaseIsVirtual, 
601                                    llvm::GlobalVariable::LinkageTypes Linkage,
602                                       VTableAddressPointsMapTy& AddressPoints) {
603   if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
604     DI->completeClassData(Base.getBase());
605
606   OwningPtr<VTableLayout> VTLayout(
607       ItaniumVTContext.createConstructionVTableLayout(
608           Base.getBase(), Base.getBaseOffset(), BaseIsVirtual, RD));
609
610   // Add the address points.
611   AddressPoints = VTLayout->getAddressPoints();
612
613   // Get the mangled construction vtable name.
614   SmallString<256> OutName;
615   llvm::raw_svector_ostream Out(OutName);
616   cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
617       .mangleCXXCtorVTable(RD, Base.getBaseOffset().getQuantity(),
618                            Base.getBase(), Out);
619   Out.flush();
620   StringRef Name = OutName.str();
621
622   llvm::ArrayType *ArrayType = 
623     llvm::ArrayType::get(CGM.Int8PtrTy, VTLayout->getNumVTableComponents());
624
625   // Construction vtable symbols are not part of the Itanium ABI, so we cannot
626   // guarantee that they actually will be available externally. Instead, when
627   // emitting an available_externally VTT, we provide references to an internal
628   // linkage construction vtable. The ABI only requires complete-object vtables
629   // to be the same for all instances of a type, not construction vtables.
630   if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
631     Linkage = llvm::GlobalVariable::InternalLinkage;
632
633   // Create the variable that will hold the construction vtable.
634   llvm::GlobalVariable *VTable = 
635     CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType, Linkage);
636   CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForConstructionVTable);
637
638   // V-tables are always unnamed_addr.
639   VTable->setUnnamedAddr(true);
640
641   // Create and set the initializer.
642   llvm::Constant *Init = 
643     CreateVTableInitializer(Base.getBase(), 
644                             VTLayout->vtable_component_begin(), 
645                             VTLayout->getNumVTableComponents(),
646                             VTLayout->vtable_thunk_begin(),
647                             VTLayout->getNumVTableThunks());
648   VTable->setInitializer(Init);
649   
650   return VTable;
651 }
652
653 /// Compute the required linkage of the v-table for the given class.
654 ///
655 /// Note that we only call this at the end of the translation unit.
656 llvm::GlobalVariable::LinkageTypes 
657 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
658   if (!RD->isExternallyVisible())
659     return llvm::GlobalVariable::InternalLinkage;
660
661   // We're at the end of the translation unit, so the current key
662   // function is fully correct.
663   if (const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction(RD)) {
664     // If this class has a key function, use that to determine the
665     // linkage of the vtable.
666     const FunctionDecl *def = 0;
667     if (keyFunction->hasBody(def))
668       keyFunction = cast<CXXMethodDecl>(def);
669     
670     switch (keyFunction->getTemplateSpecializationKind()) {
671       case TSK_Undeclared:
672       case TSK_ExplicitSpecialization:
673         assert(def && "Should not have been asked to emit this");
674         if (keyFunction->isInlined())
675           return !Context.getLangOpts().AppleKext ?
676                    llvm::GlobalVariable::LinkOnceODRLinkage :
677                    llvm::Function::InternalLinkage;
678         
679         return llvm::GlobalVariable::ExternalLinkage;
680         
681       case TSK_ImplicitInstantiation:
682         return !Context.getLangOpts().AppleKext ?
683                  llvm::GlobalVariable::LinkOnceODRLinkage :
684                  llvm::Function::InternalLinkage;
685
686       case TSK_ExplicitInstantiationDefinition:
687         return !Context.getLangOpts().AppleKext ?
688                  llvm::GlobalVariable::WeakODRLinkage :
689                  llvm::Function::InternalLinkage;
690   
691       case TSK_ExplicitInstantiationDeclaration:
692         llvm_unreachable("Should not have been asked to emit this");
693     }
694   }
695
696   // -fapple-kext mode does not support weak linkage, so we must use
697   // internal linkage.
698   if (Context.getLangOpts().AppleKext)
699     return llvm::Function::InternalLinkage;
700   
701   switch (RD->getTemplateSpecializationKind()) {
702   case TSK_Undeclared:
703   case TSK_ExplicitSpecialization:
704   case TSK_ImplicitInstantiation:
705     return llvm::GlobalVariable::LinkOnceODRLinkage;
706
707   case TSK_ExplicitInstantiationDeclaration:
708     llvm_unreachable("Should not have been asked to emit this");
709
710   case TSK_ExplicitInstantiationDefinition:
711       return llvm::GlobalVariable::WeakODRLinkage;
712   }
713
714   llvm_unreachable("Invalid TemplateSpecializationKind!");
715 }
716
717 /// This is a callback from Sema to tell us that it believes that a
718 /// particular v-table is required to be emitted in this translation
719 /// unit.
720 ///
721 /// The reason we don't simply trust this callback is because Sema
722 /// will happily report that something is used even when it's used
723 /// only in code that we don't actually have to emit.
724 ///
725 /// \param isRequired - if true, the v-table is mandatory, e.g.
726 ///   because the translation unit defines the key function
727 void CodeGenModule::EmitVTable(CXXRecordDecl *theClass, bool isRequired) {
728   if (!isRequired) return;
729
730   VTables.GenerateClassData(theClass);
731 }
732
733 void 
734 CodeGenVTables::GenerateClassData(const CXXRecordDecl *RD) {
735   if (CGDebugInfo *DI = CGM.getModuleDebugInfo())
736     DI->completeClassData(RD);
737
738   if (RD->getNumVBases())
739     CGM.getCXXABI().emitVirtualInheritanceTables(RD);
740
741   CGM.getCXXABI().emitVTableDefinitions(*this, RD);
742 }
743
744 /// At this point in the translation unit, does it appear that can we
745 /// rely on the vtable being defined elsewhere in the program?
746 ///
747 /// The response is really only definitive when called at the end of
748 /// the translation unit.
749 ///
750 /// The only semantic restriction here is that the object file should
751 /// not contain a v-table definition when that v-table is defined
752 /// strongly elsewhere.  Otherwise, we'd just like to avoid emitting
753 /// v-tables when unnecessary.
754 bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
755   assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
756
757   // If we have an explicit instantiation declaration (and not a
758   // definition), the v-table is defined elsewhere.
759   TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
760   if (TSK == TSK_ExplicitInstantiationDeclaration)
761     return true;
762
763   // Otherwise, if the class is an instantiated template, the
764   // v-table must be defined here.
765   if (TSK == TSK_ImplicitInstantiation ||
766       TSK == TSK_ExplicitInstantiationDefinition)
767     return false;
768
769   // Otherwise, if the class doesn't have a key function (possibly
770   // anymore), the v-table must be defined here.
771   const CXXMethodDecl *keyFunction = CGM.getContext().getCurrentKeyFunction(RD);
772   if (!keyFunction)
773     return false;
774
775   // Otherwise, if we don't have a definition of the key function, the
776   // v-table must be defined somewhere else.
777   return !keyFunction->hasBody();
778 }
779
780 /// Given that we're currently at the end of the translation unit, and
781 /// we've emitted a reference to the v-table for this class, should
782 /// we define that v-table?
783 static bool shouldEmitVTableAtEndOfTranslationUnit(CodeGenModule &CGM,
784                                                    const CXXRecordDecl *RD) {
785   return !CGM.getVTables().isVTableExternal(RD);
786 }
787
788 /// Given that at some point we emitted a reference to one or more
789 /// v-tables, and that we are now at the end of the translation unit,
790 /// decide whether we should emit them.
791 void CodeGenModule::EmitDeferredVTables() {
792 #ifndef NDEBUG
793   // Remember the size of DeferredVTables, because we're going to assume
794   // that this entire operation doesn't modify it.
795   size_t savedSize = DeferredVTables.size();
796 #endif
797
798   typedef std::vector<const CXXRecordDecl *>::const_iterator const_iterator;
799   for (const_iterator i = DeferredVTables.begin(),
800                       e = DeferredVTables.end(); i != e; ++i) {
801     const CXXRecordDecl *RD = *i;
802     if (shouldEmitVTableAtEndOfTranslationUnit(*this, RD))
803       VTables.GenerateClassData(RD);
804   }
805
806   assert(savedSize == DeferredVTables.size() &&
807          "deferred extra v-tables during v-table emission?");
808   DeferredVTables.clear();
809 }