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