CGCXX.cpp revision 607d037c3f4376cbc8979d0eb9cd2f49a58ea033
1//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
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.
11//
12//===----------------------------------------------------------------------===//
13
14// We might split this into multiple files if it gets too unwieldy
15
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
18#include "Mangle.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/RecordLayout.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/StmtCXX.h"
25#include "llvm/ADT/StringExtras.h"
26using namespace clang;
27using namespace CodeGen;
28
29RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
30                                          llvm::Value *Callee,
31                                          ReturnValueSlot ReturnValue,
32                                          llvm::Value *This,
33                                          CallExpr::const_arg_iterator ArgBeg,
34                                          CallExpr::const_arg_iterator ArgEnd) {
35  assert(MD->isInstance() &&
36         "Trying to emit a member call expr on a static method!");
37
38  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
39
40  CallArgList Args;
41
42  // Push the this ptr.
43  Args.push_back(std::make_pair(RValue::get(This),
44                                MD->getThisType(getContext())));
45
46  // And the rest of the call args
47  EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
48
49  QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
50  return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), Callee,
51                  ReturnValue, Args, MD);
52}
53
54/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
55/// expr can be devirtualized.
56static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
57  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
58    if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
59      // This is a record decl. We know the type and can devirtualize it.
60      return VD->getType()->isRecordType();
61    }
62
63    return false;
64  }
65
66  // We can always devirtualize calls on temporary object expressions.
67  if (isa<CXXTemporaryObjectExpr>(Base))
68    return true;
69
70  // And calls on bound temporaries.
71  if (isa<CXXBindTemporaryExpr>(Base))
72    return true;
73
74  // Check if this is a call expr that returns a record type.
75  if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
76    return CE->getCallReturnType()->isRecordType();
77
78  // We can't devirtualize the call.
79  return false;
80}
81
82RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
83                                              ReturnValueSlot ReturnValue) {
84  if (isa<BinaryOperator>(CE->getCallee()->IgnoreParens()))
85    return EmitCXXMemberPointerCallExpr(CE, ReturnValue);
86
87  const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()->IgnoreParens());
88  const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
89
90  if (MD->isStatic()) {
91    // The method is static, emit it as we would a regular call.
92    llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
93    return EmitCall(getContext().getPointerType(MD->getType()), Callee,
94                    ReturnValue, CE->arg_begin(), CE->arg_end());
95  }
96
97  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
98
99  const llvm::Type *Ty =
100    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
101                                   FPT->isVariadic());
102  llvm::Value *This;
103
104  if (ME->isArrow())
105    This = EmitScalarExpr(ME->getBase());
106  else {
107    LValue BaseLV = EmitLValue(ME->getBase());
108    This = BaseLV.getAddress();
109  }
110
111  if (MD->isCopyAssignment() && MD->isTrivial()) {
112    // We don't like to generate the trivial copy assignment operator when
113    // it isn't necessary; just produce the proper effect here.
114    llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
115    EmitAggregateCopy(This, RHS, CE->getType());
116    return RValue::get(This);
117  }
118
119  // C++ [class.virtual]p12:
120  //   Explicit qualification with the scope operator (5.1) suppresses the
121  //   virtual call mechanism.
122  //
123  // We also don't emit a virtual call if the base expression has a record type
124  // because then we know what the type is.
125  llvm::Value *Callee;
126  if (const CXXDestructorDecl *Destructor
127             = dyn_cast<CXXDestructorDecl>(MD)) {
128    if (Destructor->isTrivial())
129      return RValue::get(0);
130    if (MD->isVirtual() && !ME->hasQualifier() &&
131        !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
132      Callee = BuildVirtualCall(Destructor, Dtor_Complete, This, Ty);
133    } else {
134      Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
135    }
136  } else if (MD->isVirtual() && !ME->hasQualifier() &&
137             !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
138    Callee = BuildVirtualCall(MD, This, Ty);
139  } else {
140    Callee = CGM.GetAddrOfFunction(MD, Ty);
141  }
142
143  return EmitCXXMemberCall(MD, Callee, ReturnValue, This,
144                           CE->arg_begin(), CE->arg_end());
145}
146
147RValue
148CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
149                                              ReturnValueSlot ReturnValue) {
150  const BinaryOperator *BO =
151      cast<BinaryOperator>(E->getCallee()->IgnoreParens());
152  const Expr *BaseExpr = BO->getLHS();
153  const Expr *MemFnExpr = BO->getRHS();
154
155  const MemberPointerType *MPT =
156    MemFnExpr->getType()->getAs<MemberPointerType>();
157  const FunctionProtoType *FPT =
158    MPT->getPointeeType()->getAs<FunctionProtoType>();
159  const CXXRecordDecl *RD =
160    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
161
162  const llvm::FunctionType *FTy =
163    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
164                                   FPT->isVariadic());
165
166  const llvm::Type *Int8PtrTy =
167    llvm::Type::getInt8Ty(VMContext)->getPointerTo();
168
169  // Get the member function pointer.
170  llvm::Value *MemFnPtr =
171    CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
172  EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
173
174  // Emit the 'this' pointer.
175  llvm::Value *This;
176
177  if (BO->getOpcode() == BinaryOperator::PtrMemI)
178    This = EmitScalarExpr(BaseExpr);
179  else
180    This = EmitLValue(BaseExpr).getAddress();
181
182  // Adjust it.
183  llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
184  Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
185
186  llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
187  Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
188
189  This = Builder.CreateBitCast(Ptr, This->getType(), "this");
190
191  llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
192
193  const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
194
195  llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
196
197  // If the LSB in the function pointer is 1, the function pointer points to
198  // a virtual function.
199  llvm::Value *IsVirtual
200    = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
201                        "and");
202
203  IsVirtual = Builder.CreateTrunc(IsVirtual,
204                                  llvm::Type::getInt1Ty(VMContext));
205
206  llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
207  llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
208  llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
209
210  Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
211  EmitBlock(FnVirtual);
212
213  const llvm::Type *VTableTy =
214    FTy->getPointerTo()->getPointerTo()->getPointerTo();
215
216  llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
217  VTable = Builder.CreateLoad(VTable);
218
219  VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
220
221  // Since the function pointer is 1 plus the virtual table offset, we
222  // subtract 1 by using a GEP.
223  VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
224
225  llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
226
227  EmitBranch(FnEnd);
228  EmitBlock(FnNonVirtual);
229
230  // If the function is not virtual, just load the pointer.
231  llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
232  NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
233
234  EmitBlock(FnEnd);
235
236  llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
237  Callee->reserveOperandSpace(2);
238  Callee->addIncoming(VirtualFn, FnVirtual);
239  Callee->addIncoming(NonVirtualFn, FnNonVirtual);
240
241  CallArgList Args;
242
243  QualType ThisType =
244    getContext().getPointerType(getContext().getTagDeclType(RD));
245
246  // Push the this ptr.
247  Args.push_back(std::make_pair(RValue::get(This), ThisType));
248
249  // And the rest of the call args
250  EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
251  QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
252  return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), Callee,
253                  ReturnValue, Args);
254}
255
256RValue
257CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
258                                               const CXXMethodDecl *MD,
259                                               ReturnValueSlot ReturnValue) {
260  assert(MD->isInstance() &&
261         "Trying to emit a member call expr on a static method!");
262
263  if (MD->isCopyAssignment()) {
264    const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
265    if (ClassDecl->hasTrivialCopyAssignment()) {
266      assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
267             "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
268      llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
269      llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
270      QualType Ty = E->getType();
271      EmitAggregateCopy(This, Src, Ty);
272      return RValue::get(This);
273    }
274  }
275
276  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
277  const llvm::Type *Ty =
278    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
279                                   FPT->isVariadic());
280
281  llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
282
283  llvm::Value *Callee;
284  if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
285    Callee = BuildVirtualCall(MD, This, Ty);
286  else
287    Callee = CGM.GetAddrOfFunction(MD, Ty);
288
289  return EmitCXXMemberCall(MD, Callee, ReturnValue, This,
290                           E->arg_begin() + 1, E->arg_end());
291}
292
293llvm::Value *CodeGenFunction::LoadCXXThis() {
294  assert(isa<CXXMethodDecl>(CurFuncDecl) &&
295         "Must be in a C++ member function decl to load 'this'");
296  assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
297         "Must be in a C++ member function decl to load 'this'");
298
299  // FIXME: What if we're inside a block?
300  // ans: See how CodeGenFunction::LoadObjCSelf() uses
301  // CodeGenFunction::BlockForwardSelf() for how to do this.
302  return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
303}
304
305/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
306/// for-loop to call the default constructor on individual members of the
307/// array.
308/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
309/// array type and 'ArrayPtr' points to the beginning fo the array.
310/// It is assumed that all relevant checks have been made by the caller.
311void
312CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
313                                          const ConstantArrayType *ArrayTy,
314                                          llvm::Value *ArrayPtr,
315                                          CallExpr::const_arg_iterator ArgBeg,
316                                          CallExpr::const_arg_iterator ArgEnd) {
317
318  const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
319  llvm::Value * NumElements =
320    llvm::ConstantInt::get(SizeTy,
321                           getContext().getConstantArrayElementCount(ArrayTy));
322
323  EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd);
324}
325
326void
327CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
328                                          llvm::Value *NumElements,
329                                          llvm::Value *ArrayPtr,
330                                          CallExpr::const_arg_iterator ArgBeg,
331                                          CallExpr::const_arg_iterator ArgEnd) {
332  const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
333
334  // Create a temporary for the loop index and initialize it with 0.
335  llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
336  llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
337  Builder.CreateStore(Zero, IndexPtr);
338
339  // Start the loop with a block that tests the condition.
340  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
341  llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
342
343  EmitBlock(CondBlock);
344
345  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
346
347  // Generate: if (loop-index < number-of-elements fall to the loop body,
348  // otherwise, go to the block after the for-loop.
349  llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
350  llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
351  // If the condition is true, execute the body.
352  Builder.CreateCondBr(IsLess, ForBody, AfterFor);
353
354  EmitBlock(ForBody);
355
356  llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
357  // Inside the loop body, emit the constructor call on the array element.
358  Counter = Builder.CreateLoad(IndexPtr);
359  llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
360                                                   "arrayidx");
361
362  // C++ [class.temporary]p4:
363  // There are two contexts in which temporaries are destroyed at a different
364  // point than the end of the full-expression. The first context is when a
365  // default constructor is called to initialize an element of an array.
366  // If the constructor has one or more default arguments, the destruction of
367  // every temporary created in a default argument expression is sequenced
368  // before the construction of the next array element, if any.
369
370  // Keep track of the current number of live temporaries.
371  unsigned OldNumLiveTemporaries = LiveTemporaries.size();
372
373  EmitCXXConstructorCall(D, Ctor_Complete, Address, ArgBeg, ArgEnd);
374
375  // Pop temporaries.
376  while (LiveTemporaries.size() > OldNumLiveTemporaries)
377    PopCXXTemporary();
378
379  EmitBlock(ContinueBlock);
380
381  // Emit the increment of the loop counter.
382  llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
383  Counter = Builder.CreateLoad(IndexPtr);
384  NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
385  Builder.CreateStore(NextVal, IndexPtr);
386
387  // Finally, branch back up to the condition for the next iteration.
388  EmitBranch(CondBlock);
389
390  // Emit the fall-through block.
391  EmitBlock(AfterFor, true);
392}
393
394/// EmitCXXAggrDestructorCall - calls the default destructor on array
395/// elements in reverse order of construction.
396void
397CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
398                                           const ArrayType *Array,
399                                           llvm::Value *This) {
400  const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
401  assert(CA && "Do we support VLA for destruction ?");
402  uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
403
404  const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType());
405  llvm::Value* ElementCountPtr = llvm::ConstantInt::get(SizeLTy, ElementCount);
406  EmitCXXAggrDestructorCall(D, ElementCountPtr, This);
407}
408
409/// EmitCXXAggrDestructorCall - calls the default destructor on array
410/// elements in reverse order of construction.
411void
412CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
413                                           llvm::Value *UpperCount,
414                                           llvm::Value *This) {
415  const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType());
416  llvm::Value *One = llvm::ConstantInt::get(SizeLTy, 1);
417
418  // Create a temporary for the loop index and initialize it with count of
419  // array elements.
420  llvm::Value *IndexPtr = CreateTempAlloca(SizeLTy, "loop.index");
421
422  // Store the number of elements in the index pointer.
423  Builder.CreateStore(UpperCount, IndexPtr);
424
425  // Start the loop with a block that tests the condition.
426  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
427  llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
428
429  EmitBlock(CondBlock);
430
431  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
432
433  // Generate: if (loop-index != 0 fall to the loop body,
434  // otherwise, go to the block after the for-loop.
435  llvm::Value* zeroConstant =
436    llvm::Constant::getNullValue(SizeLTy);
437  llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
438  llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
439                                            "isne");
440  // If the condition is true, execute the body.
441  Builder.CreateCondBr(IsNE, ForBody, AfterFor);
442
443  EmitBlock(ForBody);
444
445  llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
446  // Inside the loop body, emit the constructor call on the array element.
447  Counter = Builder.CreateLoad(IndexPtr);
448  Counter = Builder.CreateSub(Counter, One);
449  llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
450  EmitCXXDestructorCall(D, Dtor_Complete, Address);
451
452  EmitBlock(ContinueBlock);
453
454  // Emit the decrement of the loop counter.
455  Counter = Builder.CreateLoad(IndexPtr);
456  Counter = Builder.CreateSub(Counter, One, "dec");
457  Builder.CreateStore(Counter, IndexPtr);
458
459  // Finally, branch back up to the condition for the next iteration.
460  EmitBranch(CondBlock);
461
462  // Emit the fall-through block.
463  EmitBlock(AfterFor, true);
464}
465
466/// GenerateCXXAggrDestructorHelper - Generates a helper function which when
467/// invoked, calls the default destructor on array elements in reverse order of
468/// construction.
469llvm::Constant *
470CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
471                                                 const ArrayType *Array,
472                                                 llvm::Value *This) {
473  FunctionArgList Args;
474  ImplicitParamDecl *Dst =
475    ImplicitParamDecl::Create(getContext(), 0,
476                              SourceLocation(), 0,
477                              getContext().getPointerType(getContext().VoidTy));
478  Args.push_back(std::make_pair(Dst, Dst->getType()));
479
480  llvm::SmallString<16> Name;
481  llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueAggrDestructorCount);
482  QualType R = getContext().VoidTy;
483  const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args);
484  const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
485  llvm::Function *Fn =
486    llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
487                           Name.str(),
488                           &CGM.getModule());
489  IdentifierInfo *II = &CGM.getContext().Idents.get(Name.str());
490  FunctionDecl *FD = FunctionDecl::Create(getContext(),
491                                          getContext().getTranslationUnitDecl(),
492                                          SourceLocation(), II, R, 0,
493                                          FunctionDecl::Static,
494                                          false, true);
495  StartFunction(FD, R, Fn, Args, SourceLocation());
496  QualType BaseElementTy = getContext().getBaseElementType(Array);
497  const llvm::Type *BasePtr = ConvertType(BaseElementTy);
498  BasePtr = llvm::PointerType::getUnqual(BasePtr);
499  llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
500  EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
501  FinishFunction();
502  llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
503                                              0);
504  llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
505  return m;
506}
507
508void
509CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
510                                        CXXCtorType Type,
511                                        llvm::Value *This,
512                                        CallExpr::const_arg_iterator ArgBeg,
513                                        CallExpr::const_arg_iterator ArgEnd) {
514  if (D->isCopyConstructor()) {
515    const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
516    if (ClassDecl->hasTrivialCopyConstructor()) {
517      assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
518             "EmitCXXConstructorCall - user declared copy constructor");
519      const Expr *E = (*ArgBeg);
520      QualType Ty = E->getType();
521      llvm::Value *Src = EmitLValue(E).getAddress();
522      EmitAggregateCopy(This, Src, Ty);
523      return;
524    }
525  } else if (D->isTrivial()) {
526    // FIXME: Track down why we're trying to generate calls to the trivial
527    // default constructor!
528    return;
529  }
530
531  llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
532
533  EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, ArgBeg, ArgEnd);
534}
535
536void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD,
537                                            CXXDtorType Type,
538                                            llvm::Value *This) {
539  llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(DD, Type);
540
541  CallArgList Args;
542
543  // Push the this ptr.
544  Args.push_back(std::make_pair(RValue::get(This),
545                                DD->getThisType(getContext())));
546
547  // Add a VTT parameter if necessary.
548  // FIXME: This should not be a dummy null parameter!
549  if (Type == Dtor_Base && DD->getParent()->getNumVBases() != 0) {
550    QualType T = getContext().getPointerType(getContext().VoidPtrTy);
551
552    Args.push_back(std::make_pair(RValue::get(CGM.EmitNullConstant(T)), T));
553  }
554
555  // FIXME: We should try to share this code with EmitCXXMemberCall.
556
557  QualType ResultType = DD->getType()->getAs<FunctionType>()->getResultType();
558  EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), Callee,
559           ReturnValueSlot(), Args, DD);
560}
561
562void
563CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
564                                      const CXXConstructExpr *E) {
565  assert(Dest && "Must have a destination!");
566  const CXXConstructorDecl *CD = E->getConstructor();
567  const ConstantArrayType *Array =
568    getContext().getAsConstantArrayType(E->getType());
569  // For a copy constructor, even if it is trivial, must fall thru so
570  // its argument is code-gen'ed.
571  if (!CD->isCopyConstructor()) {
572    QualType InitType = E->getType();
573    if (Array)
574      InitType = getContext().getBaseElementType(Array);
575    const CXXRecordDecl *RD =
576      cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
577    if (RD->hasTrivialConstructor())
578      return;
579  }
580  // Code gen optimization to eliminate copy constructor and return
581  // its first argument instead.
582  if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
583    const Expr *Arg = E->getArg(0);
584
585    if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
586      assert((ICE->getCastKind() == CastExpr::CK_NoOp ||
587              ICE->getCastKind() == CastExpr::CK_ConstructorConversion ||
588              ICE->getCastKind() == CastExpr::CK_UserDefinedConversion) &&
589             "Unknown implicit cast kind in constructor elision");
590      Arg = ICE->getSubExpr();
591    }
592
593    if (const CXXBindTemporaryExpr *BindExpr =
594           dyn_cast<CXXBindTemporaryExpr>(Arg))
595      Arg = BindExpr->getSubExpr();
596
597    EmitAggExpr(Arg, Dest, false);
598    return;
599  }
600  if (Array) {
601    QualType BaseElementTy = getContext().getBaseElementType(Array);
602    const llvm::Type *BasePtr = ConvertType(BaseElementTy);
603    BasePtr = llvm::PointerType::getUnqual(BasePtr);
604    llvm::Value *BaseAddrPtr =
605      Builder.CreateBitCast(Dest, BasePtr);
606
607    EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
608                               E->arg_begin(), E->arg_end());
609  }
610  else
611    // Call the constructor.
612    EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
613                           E->arg_begin(), E->arg_end());
614}
615
616void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
617  EmitGlobal(GlobalDecl(D, Ctor_Complete));
618  EmitGlobal(GlobalDecl(D, Ctor_Base));
619}
620
621void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
622                                       CXXCtorType Type) {
623
624  llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
625
626  CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
627
628  SetFunctionDefinitionAttributes(D, Fn);
629  SetLLVMFunctionAttributesForDefinition(D, Fn);
630}
631
632llvm::Function *
633CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
634                                       CXXCtorType Type) {
635  const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
636  const llvm::FunctionType *FTy =
637    getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
638                               FPT->isVariadic());
639
640  const char *Name = getMangledCXXCtorName(D, Type);
641  return cast<llvm::Function>(
642                      GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
643}
644
645const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
646                                                 CXXCtorType Type) {
647  llvm::SmallString<256> Name;
648  getMangleContext().mangleCXXCtor(D, Type, Name);
649
650  Name += '\0';
651  return UniqueMangledName(Name.begin(), Name.end());
652}
653
654void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
655  if (D->isVirtual())
656    EmitGlobal(GlobalDecl(D, Dtor_Deleting));
657  EmitGlobal(GlobalDecl(D, Dtor_Complete));
658  EmitGlobal(GlobalDecl(D, Dtor_Base));
659}
660
661void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
662                                      CXXDtorType Type) {
663  llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
664
665  CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
666
667  SetFunctionDefinitionAttributes(D, Fn);
668  SetLLVMFunctionAttributesForDefinition(D, Fn);
669}
670
671llvm::Function *
672CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
673                                      CXXDtorType Type) {
674  const llvm::FunctionType *FTy =
675    getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
676
677  const char *Name = getMangledCXXDtorName(D, Type);
678  return cast<llvm::Function>(
679                      GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
680}
681
682const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
683                                                 CXXDtorType Type) {
684  llvm::SmallString<256> Name;
685  getMangleContext().mangleCXXDtor(D, Type, Name);
686
687  Name += '\0';
688  return UniqueMangledName(Name.begin(), Name.end());
689}
690
691llvm::Constant *
692CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
693                               bool Extern,
694                               const ThunkAdjustment &ThisAdjustment) {
695  return GenerateCovariantThunk(Fn, GD, Extern,
696                                CovariantThunkAdjustment(ThisAdjustment,
697                                                         ThunkAdjustment()));
698}
699
700llvm::Value *
701CodeGenFunction::DynamicTypeAdjust(llvm::Value *V,
702                                   const ThunkAdjustment &Adjustment) {
703  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
704
705  const llvm::Type *OrigTy = V->getType();
706  if (Adjustment.NonVirtual) {
707    // Do the non-virtual adjustment
708    V = Builder.CreateBitCast(V, Int8PtrTy);
709    V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual);
710    V = Builder.CreateBitCast(V, OrigTy);
711  }
712
713  if (!Adjustment.Virtual)
714    return V;
715
716  assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 &&
717         "vtable entry unaligned");
718
719  // Do the virtual this adjustment
720  const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
721  const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo();
722
723  llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy);
724  V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo());
725  V = Builder.CreateLoad(V, "vtable");
726
727  llvm::Value *VTablePtr = V;
728  uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8);
729  V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
730  V = Builder.CreateLoad(V);
731  V = Builder.CreateGEP(ThisVal, V);
732
733  return Builder.CreateBitCast(V, OrigTy);
734}
735
736llvm::Constant *
737CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
738                                   GlobalDecl GD, bool Extern,
739                                   const CovariantThunkAdjustment &Adjustment) {
740  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
741  QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
742
743  FunctionArgList Args;
744  ImplicitParamDecl *ThisDecl =
745    ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
746                              MD->getThisType(getContext()));
747  Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
748  for (FunctionDecl::param_const_iterator i = MD->param_begin(),
749         e = MD->param_end();
750       i != e; ++i) {
751    ParmVarDecl *D = *i;
752    Args.push_back(std::make_pair(D, D->getType()));
753  }
754  IdentifierInfo *II
755    = &CGM.getContext().Idents.get("__thunk_named_foo_");
756  FunctionDecl *FD = FunctionDecl::Create(getContext(),
757                                          getContext().getTranslationUnitDecl(),
758                                          SourceLocation(), II, ResultType, 0,
759                                          Extern
760                                            ? FunctionDecl::Extern
761                                            : FunctionDecl::Static,
762                                          false, true);
763  StartFunction(FD, ResultType, Fn, Args, SourceLocation());
764
765  // generate body
766  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
767  const llvm::Type *Ty =
768    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
769                                   FPT->isVariadic());
770  llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
771
772  CallArgList CallArgs;
773
774  bool ShouldAdjustReturnPointer = true;
775  QualType ArgType = MD->getThisType(getContext());
776  llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
777  if (!Adjustment.ThisAdjustment.isEmpty()) {
778    // Do the this adjustment.
779    const llvm::Type *OrigTy = Callee->getType();
780    Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment);
781
782    if (!Adjustment.ReturnAdjustment.isEmpty()) {
783      const CovariantThunkAdjustment &ReturnAdjustment =
784        CovariantThunkAdjustment(ThunkAdjustment(),
785                                 Adjustment.ReturnAdjustment);
786
787      Callee = CGM.BuildCovariantThunk(GD, Extern, ReturnAdjustment);
788
789      Callee = Builder.CreateBitCast(Callee, OrigTy);
790      ShouldAdjustReturnPointer = false;
791    }
792  }
793
794  CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
795
796  for (FunctionDecl::param_const_iterator i = MD->param_begin(),
797         e = MD->param_end();
798       i != e; ++i) {
799    ParmVarDecl *D = *i;
800    QualType ArgType = D->getType();
801
802    // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
803    Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType.getNonReferenceType(),
804                                               SourceLocation());
805    CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
806  }
807
808  RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
809                       Callee, ReturnValueSlot(), CallArgs, MD);
810  if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) {
811    bool CanBeZero = !(ResultType->isReferenceType()
812    // FIXME: attr nonnull can't be zero either
813                       /* || ResultType->hasAttr<NonNullAttr>() */ );
814    // Do the return result adjustment.
815    if (CanBeZero) {
816      llvm::BasicBlock *NonZeroBlock = createBasicBlock();
817      llvm::BasicBlock *ZeroBlock = createBasicBlock();
818      llvm::BasicBlock *ContBlock = createBasicBlock();
819
820      const llvm::Type *Ty = RV.getScalarVal()->getType();
821      llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
822      Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
823                           NonZeroBlock, ZeroBlock);
824      EmitBlock(NonZeroBlock);
825      llvm::Value *NZ =
826        DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment);
827      EmitBranch(ContBlock);
828      EmitBlock(ZeroBlock);
829      llvm::Value *Z = RV.getScalarVal();
830      EmitBlock(ContBlock);
831      llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
832      RVOrZero->reserveOperandSpace(2);
833      RVOrZero->addIncoming(NZ, NonZeroBlock);
834      RVOrZero->addIncoming(Z, ZeroBlock);
835      RV = RValue::get(RVOrZero);
836    } else
837      RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(),
838                                         Adjustment.ReturnAdjustment));
839  }
840
841  if (!ResultType->isVoidType())
842    EmitReturnOfRValue(RV, ResultType);
843
844  FinishFunction();
845  return Fn;
846}
847
848llvm::Constant *
849CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
850                              const ThunkAdjustment &ThisAdjustment) {
851  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
852
853  // Compute mangled name
854  llvm::SmallString<256> OutName;
855  if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
856    getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), ThisAdjustment,
857                                          OutName);
858  else
859    getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
860  OutName += '\0';
861  const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
862
863  // Get function for mangled name
864  const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
865  return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
866}
867
868llvm::Constant *
869CodeGenModule::GetAddrOfCovariantThunk(GlobalDecl GD,
870                                   const CovariantThunkAdjustment &Adjustment) {
871  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
872
873  // Compute mangled name
874  llvm::SmallString<256> OutName;
875  getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
876  OutName += '\0';
877  const char* Name = UniqueMangledName(OutName.begin(), OutName.end());
878
879  // Get function for mangled name
880  const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
881  return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
882}
883
884void CodeGenModule::BuildThunksForVirtual(GlobalDecl GD) {
885  CGVtableInfo::AdjustmentVectorTy *AdjPtr = getVtableInfo().getAdjustments(GD);
886  if (!AdjPtr)
887    return;
888  CGVtableInfo::AdjustmentVectorTy &Adj = *AdjPtr;
889  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
890  for (unsigned i = 0; i < Adj.size(); i++) {
891    GlobalDecl OGD = Adj[i].first;
892    const CXXMethodDecl *OMD = cast<CXXMethodDecl>(OGD.getDecl());
893    QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
894    CanQualType oret = getContext().getCanonicalType(nc_oret);
895    QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
896    CanQualType ret = getContext().getCanonicalType(nc_ret);
897    ThunkAdjustment ReturnAdjustment;
898    if (oret != ret) {
899      QualType qD = nc_ret->getPointeeType();
900      QualType qB = nc_oret->getPointeeType();
901      CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
902      CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
903      ReturnAdjustment = ComputeThunkAdjustment(D, B);
904    }
905    ThunkAdjustment ThisAdjustment = Adj[i].second;
906    bool Extern = !cast<CXXRecordDecl>(OMD->getDeclContext())->isInAnonymousNamespace();
907    if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) {
908      CovariantThunkAdjustment CoAdj(ThisAdjustment, ReturnAdjustment);
909      llvm::Constant *FnConst;
910      if (!ReturnAdjustment.isEmpty())
911        FnConst = GetAddrOfCovariantThunk(GD, CoAdj);
912      else
913        FnConst = GetAddrOfThunk(GD, ThisAdjustment);
914      if (!isa<llvm::Function>(FnConst)) {
915        llvm::Constant *SubExpr =
916            cast<llvm::ConstantExpr>(FnConst)->getOperand(0);
917        llvm::Function *OldFn = cast<llvm::Function>(SubExpr);
918        std::string Name = OldFn->getNameStr();
919        GlobalDeclMap.erase(UniqueMangledName(Name.data(),
920                                              Name.data() + Name.size() + 1));
921        llvm::Constant *NewFnConst;
922        if (!ReturnAdjustment.isEmpty())
923          NewFnConst = GetAddrOfCovariantThunk(GD, CoAdj);
924        else
925          NewFnConst = GetAddrOfThunk(GD, ThisAdjustment);
926        llvm::Function *NewFn = cast<llvm::Function>(NewFnConst);
927        NewFn->takeName(OldFn);
928        llvm::Constant *NewPtrForOldDecl =
929            llvm::ConstantExpr::getBitCast(NewFn, OldFn->getType());
930        OldFn->replaceAllUsesWith(NewPtrForOldDecl);
931        OldFn->eraseFromParent();
932        FnConst = NewFn;
933      }
934      llvm::Function *Fn = cast<llvm::Function>(FnConst);
935      if (Fn->isDeclaration()) {
936        llvm::GlobalVariable::LinkageTypes linktype;
937        linktype = llvm::GlobalValue::WeakAnyLinkage;
938        if (!Extern)
939          linktype = llvm::GlobalValue::InternalLinkage;
940        Fn->setLinkage(linktype);
941        if (!Features.Exceptions && !Features.ObjCNonFragileABI)
942          Fn->addFnAttr(llvm::Attribute::NoUnwind);
943        Fn->setAlignment(2);
944        CodeGenFunction(*this).GenerateCovariantThunk(Fn, GD, Extern, CoAdj);
945      }
946    }
947  }
948}
949
950llvm::Constant *
951CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern,
952                          const ThunkAdjustment &ThisAdjustment) {
953  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
954  llvm::SmallString<256> OutName;
955  if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(MD)) {
956    getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment,
957                                          OutName);
958  } else
959    getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
960
961  llvm::GlobalVariable::LinkageTypes linktype;
962  linktype = llvm::GlobalValue::WeakAnyLinkage;
963  if (!Extern)
964    linktype = llvm::GlobalValue::InternalLinkage;
965  llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
966  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
967  const llvm::FunctionType *FTy =
968    getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
969                               FPT->isVariadic());
970
971  llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
972                                              &getModule());
973  CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment);
974  llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
975  return m;
976}
977
978llvm::Constant *
979CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern,
980                                   const CovariantThunkAdjustment &Adjustment) {
981  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
982  llvm::SmallString<256> OutName;
983  getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
984  llvm::GlobalVariable::LinkageTypes linktype;
985  linktype = llvm::GlobalValue::WeakAnyLinkage;
986  if (!Extern)
987    linktype = llvm::GlobalValue::InternalLinkage;
988  llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
989  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
990  const llvm::FunctionType *FTy =
991    getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
992                               FPT->isVariadic());
993
994  llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
995                                              &getModule());
996  CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment);
997  llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
998  return m;
999}
1000
1001llvm::Value *
1002CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
1003                                              const CXXRecordDecl *ClassDecl,
1004                                           const CXXRecordDecl *BaseClassDecl) {
1005  const llvm::Type *Int8PtrTy =
1006    llvm::Type::getInt8Ty(VMContext)->getPointerTo();
1007
1008  llvm::Value *VTablePtr = Builder.CreateBitCast(This,
1009                                                 Int8PtrTy->getPointerTo());
1010  VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
1011
1012  int64_t VBaseOffsetIndex =
1013    CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
1014
1015  llvm::Value *VBaseOffsetPtr =
1016    Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
1017  const llvm::Type *PtrDiffTy =
1018    ConvertType(getContext().getPointerDiffType());
1019
1020  VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
1021                                         PtrDiffTy->getPointerTo());
1022
1023  llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1024
1025  return VBaseOffset;
1026}
1027
1028static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex,
1029                                     llvm::Value *This, const llvm::Type *Ty) {
1030  Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
1031
1032  llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
1033  Vtable = CGF.Builder.CreateLoad(Vtable);
1034
1035  llvm::Value *VFuncPtr =
1036    CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
1037  return CGF.Builder.CreateLoad(VFuncPtr);
1038}
1039
1040llvm::Value *
1041CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
1042                                  const llvm::Type *Ty) {
1043  MD = MD->getCanonicalDecl();
1044  uint64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
1045
1046  return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
1047}
1048
1049llvm::Value *
1050CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
1051                                  llvm::Value *&This, const llvm::Type *Ty) {
1052  DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
1053  uint64_t VtableIndex =
1054    CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
1055
1056  return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
1057}
1058
1059void CodeGenFunction::InitializeVtablePtrs(const CXXRecordDecl *ClassDecl) {
1060  if (!ClassDecl->isDynamicClass())
1061    return;
1062
1063  llvm::Constant *Vtable = CGM.getVtableInfo().getVtable(ClassDecl);
1064  CodeGenModule::AddrSubMap_t& AddressPoints =
1065      *(*CGM.AddressPoints[ClassDecl])[ClassDecl];
1066  llvm::Value *ThisPtr = LoadCXXThis();
1067  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(ClassDecl);
1068
1069  // Store address points for virtual bases
1070  for (CXXRecordDecl::base_class_const_iterator I =
1071       ClassDecl->vbases_begin(), E = ClassDecl->vbases_end(); I != E; ++I) {
1072    const CXXBaseSpecifier &Base = *I;
1073    CXXRecordDecl *BaseClassDecl
1074      = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1075    uint64_t Offset = Layout.getVBaseClassOffset(BaseClassDecl);
1076    InitializeVtablePtrsRecursive(BaseClassDecl, Vtable, AddressPoints,
1077                                  ThisPtr, Offset);
1078  }
1079
1080  // Store address points for non-virtual bases and current class
1081  InitializeVtablePtrsRecursive(ClassDecl, Vtable, AddressPoints, ThisPtr, 0);
1082}
1083
1084void CodeGenFunction::InitializeVtablePtrsRecursive(
1085        const CXXRecordDecl *ClassDecl,
1086        llvm::Constant *Vtable,
1087        CodeGenModule::AddrSubMap_t& AddressPoints,
1088        llvm::Value *ThisPtr,
1089        uint64_t Offset) {
1090  if (!ClassDecl->isDynamicClass())
1091    return;
1092
1093  // Store address points for non-virtual bases
1094  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(ClassDecl);
1095  for (CXXRecordDecl::base_class_const_iterator I =
1096       ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) {
1097    const CXXBaseSpecifier &Base = *I;
1098    if (Base.isVirtual())
1099      continue;
1100    CXXRecordDecl *BaseClassDecl
1101      = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1102    uint64_t NewOffset = Offset + Layout.getBaseClassOffset(BaseClassDecl);
1103    InitializeVtablePtrsRecursive(BaseClassDecl, Vtable, AddressPoints,
1104                                  ThisPtr, NewOffset);
1105  }
1106
1107  // Compute the address point
1108  uint64_t AddressPoint = AddressPoints[std::make_pair(ClassDecl, Offset)];
1109  llvm::Value *VtableAddressPoint =
1110      Builder.CreateConstInBoundsGEP2_64(Vtable, 0, AddressPoint);
1111
1112  // Compute the address to store the address point
1113  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
1114  llvm::Value *VtableField = Builder.CreateBitCast(ThisPtr, Int8PtrTy);
1115  VtableField = Builder.CreateConstInBoundsGEP1_64(VtableField, Offset/8);
1116  const llvm::Type *AddressPointPtrTy =
1117      VtableAddressPoint->getType()->getPointerTo();
1118  VtableField = Builder.CreateBitCast(ThisPtr, AddressPointPtrTy);
1119
1120  // Store address point
1121  Builder.CreateStore(VtableAddressPoint, VtableField);
1122}
1123
1124