1 //===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===//
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 classes
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CGDebugInfo.h"
15 #include "CodeGenFunction.h"
16 #include "clang/AST/CXXInheritance.h"
17 #include "clang/AST/EvaluatedExprVisitor.h"
18 #include "clang/AST/RecordLayout.h"
19 #include "clang/AST/StmtCXX.h"
20 #include "clang/Frontend/CodeGenOptions.h"
21 
22 using namespace clang;
23 using namespace CodeGen;
24 
25 static CharUnits
26 ComputeNonVirtualBaseClassOffset(ASTContext &Context,
27                                  const CXXRecordDecl *DerivedClass,
28                                  CastExpr::path_const_iterator Start,
29                                  CastExpr::path_const_iterator End) {
30   CharUnits Offset = CharUnits::Zero();
31 
32   const CXXRecordDecl *RD = DerivedClass;
33 
34   for (CastExpr::path_const_iterator I = Start; I != End; ++I) {
35     const CXXBaseSpecifier *Base = *I;
36     assert(!Base->isVirtual() && "Should not see virtual bases here!");
37 
38     // Get the layout.
39     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
40 
41     const CXXRecordDecl *BaseDecl =
42       cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
43 
44     // Add the offset.
45     Offset += Layout.getBaseClassOffset(BaseDecl);
46 
47     RD = BaseDecl;
48   }
49 
50   return Offset;
51 }
52 
53 llvm::Constant *
54 CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
55                                    CastExpr::path_const_iterator PathBegin,
56                                    CastExpr::path_const_iterator PathEnd) {
57   assert(PathBegin != PathEnd && "Base path should not be empty!");
58 
59   CharUnits Offset =
60     ComputeNonVirtualBaseClassOffset(getContext(), ClassDecl,
61                                      PathBegin, PathEnd);
62   if (Offset.isZero())
63     return 0;
64 
65   llvm::Type *PtrDiffTy =
66   Types.ConvertType(getContext().getPointerDiffType());
67 
68   return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
69 }
70 
71 /// Gets the address of a direct base class within a complete object.
72 /// This should only be used for (1) non-virtual bases or (2) virtual bases
73 /// when the type is known to be complete (e.g. in complete destructors).
74 ///
75 /// The object pointed to by 'This' is assumed to be non-null.
76 llvm::Value *
77 CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(llvm::Value *This,
78                                                    const CXXRecordDecl *Derived,
79                                                    const CXXRecordDecl *Base,
80                                                    bool BaseIsVirtual) {
81   // 'this' must be a pointer (in some address space) to Derived.
82   assert(This->getType()->isPointerTy() &&
83          cast<llvm::PointerType>(This->getType())->getElementType()
84            == ConvertType(Derived));
85 
86   // Compute the offset of the virtual base.
87   CharUnits Offset;
88   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
89   if (BaseIsVirtual)
90     Offset = Layout.getVBaseClassOffset(Base);
91   else
92     Offset = Layout.getBaseClassOffset(Base);
93 
94   // Shift and cast down to the base type.
95   // TODO: for complete types, this should be possible with a GEP.
96   llvm::Value *V = This;
97   if (Offset.isPositive()) {
98     llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
99     V = Builder.CreateBitCast(V, Int8PtrTy);
100     V = Builder.CreateConstInBoundsGEP1_64(V, Offset.getQuantity());
101   }
102   V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo());
103 
104   return V;
105 }
106 
107 static llvm::Value *
108 ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ThisPtr,
109                                 CharUnits NonVirtual, llvm::Value *Virtual) {
110   llvm::Type *PtrDiffTy =
111     CGF.ConvertType(CGF.getContext().getPointerDiffType());
112 
113   llvm::Value *NonVirtualOffset = 0;
114   if (!NonVirtual.isZero())
115     NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy,
116                                               NonVirtual.getQuantity());
117 
118   llvm::Value *BaseOffset;
119   if (Virtual) {
120     if (NonVirtualOffset)
121       BaseOffset = CGF.Builder.CreateAdd(Virtual, NonVirtualOffset);
122     else
123       BaseOffset = Virtual;
124   } else
125     BaseOffset = NonVirtualOffset;
126 
127   // Apply the base offset.
128   llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
129   ThisPtr = CGF.Builder.CreateBitCast(ThisPtr, Int8PtrTy);
130   ThisPtr = CGF.Builder.CreateGEP(ThisPtr, BaseOffset, "add.ptr");
131 
132   return ThisPtr;
133 }
134 
135 llvm::Value *
136 CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value,
137                                        const CXXRecordDecl *Derived,
138                                        CastExpr::path_const_iterator PathBegin,
139                                        CastExpr::path_const_iterator PathEnd,
140                                        bool NullCheckValue) {
141   assert(PathBegin != PathEnd && "Base path should not be empty!");
142 
143   CastExpr::path_const_iterator Start = PathBegin;
144   const CXXRecordDecl *VBase = 0;
145 
146   // Get the virtual base.
147   if ((*Start)->isVirtual()) {
148     VBase =
149       cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl());
150     ++Start;
151   }
152 
153   CharUnits NonVirtualOffset =
154     ComputeNonVirtualBaseClassOffset(getContext(), VBase ? VBase : Derived,
155                                      Start, PathEnd);
156 
157   // Get the base pointer type.
158   llvm::Type *BasePtrTy =
159     ConvertType((PathEnd[-1])->getType())->getPointerTo();
160 
161   if (NonVirtualOffset.isZero() && !VBase) {
162     // Just cast back.
163     return Builder.CreateBitCast(Value, BasePtrTy);
164   }
165 
166   llvm::BasicBlock *CastNull = 0;
167   llvm::BasicBlock *CastNotNull = 0;
168   llvm::BasicBlock *CastEnd = 0;
169 
170   if (NullCheckValue) {
171     CastNull = createBasicBlock("cast.null");
172     CastNotNull = createBasicBlock("cast.notnull");
173     CastEnd = createBasicBlock("cast.end");
174 
175     llvm::Value *IsNull = Builder.CreateIsNull(Value);
176     Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
177     EmitBlock(CastNotNull);
178   }
179 
180   llvm::Value *VirtualOffset = 0;
181 
182   if (VBase) {
183     if (Derived->hasAttr<FinalAttr>()) {
184       VirtualOffset = 0;
185 
186       const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
187 
188       CharUnits VBaseOffset = Layout.getVBaseClassOffset(VBase);
189       NonVirtualOffset += VBaseOffset;
190     } else
191       VirtualOffset = GetVirtualBaseClassOffset(Value, Derived, VBase);
192   }
193 
194   // Apply the offsets.
195   Value = ApplyNonVirtualAndVirtualOffset(*this, Value,
196                                           NonVirtualOffset,
197                                           VirtualOffset);
198 
199   // Cast back.
200   Value = Builder.CreateBitCast(Value, BasePtrTy);
201 
202   if (NullCheckValue) {
203     Builder.CreateBr(CastEnd);
204     EmitBlock(CastNull);
205     Builder.CreateBr(CastEnd);
206     EmitBlock(CastEnd);
207 
208     llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
209     PHI->addIncoming(Value, CastNotNull);
210     PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()),
211                      CastNull);
212     Value = PHI;
213   }
214 
215   return Value;
216 }
217 
218 llvm::Value *
219 CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value,
220                                           const CXXRecordDecl *Derived,
221                                         CastExpr::path_const_iterator PathBegin,
222                                           CastExpr::path_const_iterator PathEnd,
223                                           bool NullCheckValue) {
224   assert(PathBegin != PathEnd && "Base path should not be empty!");
225 
226   QualType DerivedTy =
227     getContext().getCanonicalType(getContext().getTagDeclType(Derived));
228   llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo();
229 
230   llvm::Value *NonVirtualOffset =
231     CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd);
232 
233   if (!NonVirtualOffset) {
234     // No offset, we can just cast back.
235     return Builder.CreateBitCast(Value, DerivedPtrTy);
236   }
237 
238   llvm::BasicBlock *CastNull = 0;
239   llvm::BasicBlock *CastNotNull = 0;
240   llvm::BasicBlock *CastEnd = 0;
241 
242   if (NullCheckValue) {
243     CastNull = createBasicBlock("cast.null");
244     CastNotNull = createBasicBlock("cast.notnull");
245     CastEnd = createBasicBlock("cast.end");
246 
247     llvm::Value *IsNull = Builder.CreateIsNull(Value);
248     Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
249     EmitBlock(CastNotNull);
250   }
251 
252   // Apply the offset.
253   Value = Builder.CreatePtrToInt(Value, NonVirtualOffset->getType());
254   Value = Builder.CreateSub(Value, NonVirtualOffset);
255   Value = Builder.CreateIntToPtr(Value, DerivedPtrTy);
256 
257   // Just cast.
258   Value = Builder.CreateBitCast(Value, DerivedPtrTy);
259 
260   if (NullCheckValue) {
261     Builder.CreateBr(CastEnd);
262     EmitBlock(CastNull);
263     Builder.CreateBr(CastEnd);
264     EmitBlock(CastEnd);
265 
266     llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
267     PHI->addIncoming(Value, CastNotNull);
268     PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()),
269                      CastNull);
270     Value = PHI;
271   }
272 
273   return Value;
274 }
275 
276 /// GetVTTParameter - Return the VTT parameter that should be passed to a
277 /// base constructor/destructor with virtual bases.
278 static llvm::Value *GetVTTParameter(CodeGenFunction &CGF, GlobalDecl GD,
279                                     bool ForVirtualBase) {
280   if (!CodeGenVTables::needsVTTParameter(GD)) {
281     // This constructor/destructor does not need a VTT parameter.
282     return 0;
283   }
284 
285   const CXXRecordDecl *RD = cast<CXXMethodDecl>(CGF.CurFuncDecl)->getParent();
286   const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent();
287 
288   llvm::Value *VTT;
289 
290   uint64_t SubVTTIndex;
291 
292   // If the record matches the base, this is the complete ctor/dtor
293   // variant calling the base variant in a class with virtual bases.
294   if (RD == Base) {
295     assert(!CodeGenVTables::needsVTTParameter(CGF.CurGD) &&
296            "doing no-op VTT offset in base dtor/ctor?");
297     assert(!ForVirtualBase && "Can't have same class as virtual base!");
298     SubVTTIndex = 0;
299   } else {
300     const ASTRecordLayout &Layout =
301       CGF.getContext().getASTRecordLayout(RD);
302     CharUnits BaseOffset = ForVirtualBase ?
303       Layout.getVBaseClassOffset(Base) :
304       Layout.getBaseClassOffset(Base);
305 
306     SubVTTIndex =
307       CGF.CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset));
308     assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
309   }
310 
311   if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) {
312     // A VTT parameter was passed to the constructor, use it.
313     VTT = CGF.LoadCXXVTT();
314     VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex);
315   } else {
316     // We're the complete constructor, so get the VTT by name.
317     VTT = CGF.CGM.getVTables().GetAddrOfVTT(RD);
318     VTT = CGF.Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex);
319   }
320 
321   return VTT;
322 }
323 
324 namespace {
325   /// Call the destructor for a direct base class.
326   struct CallBaseDtor : EHScopeStack::Cleanup {
327     const CXXRecordDecl *BaseClass;
328     bool BaseIsVirtual;
329     CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual)
330       : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
331 
332     void Emit(CodeGenFunction &CGF, Flags flags) {
333       const CXXRecordDecl *DerivedClass =
334         cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
335 
336       const CXXDestructorDecl *D = BaseClass->getDestructor();
337       llvm::Value *Addr =
338         CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThis(),
339                                                   DerivedClass, BaseClass,
340                                                   BaseIsVirtual);
341       CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual, Addr);
342     }
343   };
344 
345   /// A visitor which checks whether an initializer uses 'this' in a
346   /// way which requires the vtable to be properly set.
347   struct DynamicThisUseChecker : EvaluatedExprVisitor<DynamicThisUseChecker> {
348     typedef EvaluatedExprVisitor<DynamicThisUseChecker> super;
349 
350     bool UsesThis;
351 
352     DynamicThisUseChecker(ASTContext &C) : super(C), UsesThis(false) {}
353 
354     // Black-list all explicit and implicit references to 'this'.
355     //
356     // Do we need to worry about external references to 'this' derived
357     // from arbitrary code?  If so, then anything which runs arbitrary
358     // external code might potentially access the vtable.
359     void VisitCXXThisExpr(CXXThisExpr *E) { UsesThis = true; }
360   };
361 }
362 
363 static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) {
364   DynamicThisUseChecker Checker(C);
365   Checker.Visit(const_cast<Expr*>(Init));
366   return Checker.UsesThis;
367 }
368 
369 static void EmitBaseInitializer(CodeGenFunction &CGF,
370                                 const CXXRecordDecl *ClassDecl,
371                                 CXXCtorInitializer *BaseInit,
372                                 CXXCtorType CtorType) {
373   assert(BaseInit->isBaseInitializer() &&
374          "Must have base initializer!");
375 
376   llvm::Value *ThisPtr = CGF.LoadCXXThis();
377 
378   const Type *BaseType = BaseInit->getBaseClass();
379   CXXRecordDecl *BaseClassDecl =
380     cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
381 
382   bool isBaseVirtual = BaseInit->isBaseVirtual();
383 
384   // The base constructor doesn't construct virtual bases.
385   if (CtorType == Ctor_Base && isBaseVirtual)
386     return;
387 
388   // If the initializer for the base (other than the constructor
389   // itself) accesses 'this' in any way, we need to initialize the
390   // vtables.
391   if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit()))
392     CGF.InitializeVTablePointers(ClassDecl);
393 
394   // We can pretend to be a complete class because it only matters for
395   // virtual bases, and we only do virtual bases for complete ctors.
396   llvm::Value *V =
397     CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl,
398                                               BaseClassDecl,
399                                               isBaseVirtual);
400 
401   AggValueSlot AggSlot =
402     AggValueSlot::forAddr(V, Qualifiers(),
403                           AggValueSlot::IsDestructed,
404                           AggValueSlot::DoesNotNeedGCBarriers,
405                           AggValueSlot::IsNotAliased);
406 
407   CGF.EmitAggExpr(BaseInit->getInit(), AggSlot);
408 
409   if (CGF.CGM.getLangOptions().Exceptions &&
410       !BaseClassDecl->hasTrivialDestructor())
411     CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl,
412                                           isBaseVirtual);
413 }
414 
415 static void EmitAggMemberInitializer(CodeGenFunction &CGF,
416                                      LValue LHS,
417                                      llvm::Value *ArrayIndexVar,
418                                      CXXCtorInitializer *MemberInit,
419                                      QualType T,
420                                      unsigned Index) {
421   if (Index == MemberInit->getNumArrayIndices()) {
422     CodeGenFunction::RunCleanupsScope Cleanups(CGF);
423 
424     llvm::Value *Dest = LHS.getAddress();
425     if (ArrayIndexVar) {
426       // If we have an array index variable, load it and use it as an offset.
427       // Then, increment the value.
428       llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar);
429       Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress");
430       llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1);
431       Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc");
432       CGF.Builder.CreateStore(Next, ArrayIndexVar);
433     }
434 
435     if (!CGF.hasAggregateLLVMType(T)) {
436       LValue lvalue = CGF.MakeAddrLValue(Dest, T);
437       CGF.EmitScalarInit(MemberInit->getInit(), /*decl*/ 0, lvalue, false);
438     } else if (T->isAnyComplexType()) {
439       CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), Dest,
440                                   LHS.isVolatileQualified());
441     } else {
442       AggValueSlot Slot =
443         AggValueSlot::forAddr(Dest, LHS.getQuals(),
444                               AggValueSlot::IsDestructed,
445                               AggValueSlot::DoesNotNeedGCBarriers,
446                               AggValueSlot::IsNotAliased);
447 
448       CGF.EmitAggExpr(MemberInit->getInit(), Slot);
449     }
450 
451     return;
452   }
453 
454   const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T);
455   assert(Array && "Array initialization without the array type?");
456   llvm::Value *IndexVar
457     = CGF.GetAddrOfLocalVar(MemberInit->getArrayIndex(Index));
458   assert(IndexVar && "Array index variable not loaded");
459 
460   // Initialize this index variable to zero.
461   llvm::Value* Zero
462     = llvm::Constant::getNullValue(
463                               CGF.ConvertType(CGF.getContext().getSizeType()));
464   CGF.Builder.CreateStore(Zero, IndexVar);
465 
466   // Start the loop with a block that tests the condition.
467   llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond");
468   llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end");
469 
470   CGF.EmitBlock(CondBlock);
471 
472   llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body");
473   // Generate: if (loop-index < number-of-elements) fall to the loop body,
474   // otherwise, go to the block after the for-loop.
475   uint64_t NumElements = Array->getSize().getZExtValue();
476   llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar);
477   llvm::Value *NumElementsPtr =
478     llvm::ConstantInt::get(Counter->getType(), NumElements);
479   llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr,
480                                                   "isless");
481 
482   // If the condition is true, execute the body.
483   CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor);
484 
485   CGF.EmitBlock(ForBody);
486   llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc");
487 
488   {
489     CodeGenFunction::RunCleanupsScope Cleanups(CGF);
490 
491     // Inside the loop body recurse to emit the inner loop or, eventually, the
492     // constructor call.
493     EmitAggMemberInitializer(CGF, LHS, ArrayIndexVar, MemberInit,
494                              Array->getElementType(), Index + 1);
495   }
496 
497   CGF.EmitBlock(ContinueBlock);
498 
499   // Emit the increment of the loop counter.
500   llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
501   Counter = CGF.Builder.CreateLoad(IndexVar);
502   NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc");
503   CGF.Builder.CreateStore(NextVal, IndexVar);
504 
505   // Finally, branch back up to the condition for the next iteration.
506   CGF.EmitBranch(CondBlock);
507 
508   // Emit the fall-through block.
509   CGF.EmitBlock(AfterFor, true);
510 }
511 
512 namespace {
513   struct CallMemberDtor : EHScopeStack::Cleanup {
514     FieldDecl *Field;
515     CXXDestructorDecl *Dtor;
516 
517     CallMemberDtor(FieldDecl *Field, CXXDestructorDecl *Dtor)
518       : Field(Field), Dtor(Dtor) {}
519 
520     void Emit(CodeGenFunction &CGF, Flags flags) {
521       // FIXME: Is this OK for C++0x delegating constructors?
522       llvm::Value *ThisPtr = CGF.LoadCXXThis();
523       LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, 0);
524 
525       CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
526                                 LHS.getAddress());
527     }
528   };
529 }
530 
531 static bool hasTrivialCopyOrMoveConstructor(const CXXRecordDecl *Record,
532                                             bool Moving) {
533   return Moving ? Record->hasTrivialMoveConstructor() :
534                   Record->hasTrivialCopyConstructor();
535 }
536 
537 static void EmitMemberInitializer(CodeGenFunction &CGF,
538                                   const CXXRecordDecl *ClassDecl,
539                                   CXXCtorInitializer *MemberInit,
540                                   const CXXConstructorDecl *Constructor,
541                                   FunctionArgList &Args) {
542   assert(MemberInit->isAnyMemberInitializer() &&
543          "Must have member initializer!");
544   assert(MemberInit->getInit() && "Must have initializer!");
545 
546   // non-static data member initializers.
547   FieldDecl *Field = MemberInit->getAnyMember();
548   QualType FieldType = CGF.getContext().getCanonicalType(Field->getType());
549 
550   llvm::Value *ThisPtr = CGF.LoadCXXThis();
551   LValue LHS;
552 
553   // If we are initializing an anonymous union field, drill down to the field.
554   if (MemberInit->isIndirectMemberInitializer()) {
555     LHS = CGF.EmitLValueForAnonRecordField(ThisPtr,
556                                            MemberInit->getIndirectMember(), 0);
557     FieldType = MemberInit->getIndirectMember()->getAnonField()->getType();
558   } else {
559     LHS = CGF.EmitLValueForFieldInitialization(ThisPtr, Field, 0);
560   }
561 
562   if (!CGF.hasAggregateLLVMType(Field->getType())) {
563     if (LHS.isSimple()) {
564       CGF.EmitExprAsInit(MemberInit->getInit(), Field, LHS, false);
565     } else {
566       RValue RHS = RValue::get(CGF.EmitScalarExpr(MemberInit->getInit()));
567       CGF.EmitStoreThroughLValue(RHS, LHS);
568     }
569   } else if (MemberInit->getInit()->getType()->isAnyComplexType()) {
570     CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), LHS.getAddress(),
571                                 LHS.isVolatileQualified());
572   } else {
573     llvm::Value *ArrayIndexVar = 0;
574     const ConstantArrayType *Array
575       = CGF.getContext().getAsConstantArrayType(FieldType);
576     if (Array && Constructor->isImplicit() &&
577         Constructor->isCopyOrMoveConstructor()) {
578       llvm::Type *SizeTy
579         = CGF.ConvertType(CGF.getContext().getSizeType());
580 
581       // The LHS is a pointer to the first object we'll be constructing, as
582       // a flat array.
583       QualType BaseElementTy = CGF.getContext().getBaseElementType(Array);
584       llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy);
585       BasePtr = llvm::PointerType::getUnqual(BasePtr);
586       llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(LHS.getAddress(),
587                                                            BasePtr);
588       LHS = CGF.MakeAddrLValue(BaseAddrPtr, BaseElementTy);
589 
590       // Create an array index that will be used to walk over all of the
591       // objects we're constructing.
592       ArrayIndexVar = CGF.CreateTempAlloca(SizeTy, "object.index");
593       llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
594       CGF.Builder.CreateStore(Zero, ArrayIndexVar);
595 
596       // If we are copying an array of PODs or classes with trivial copy
597       // constructors, perform a single aggregate copy.
598       const CXXRecordDecl *Record = BaseElementTy->getAsCXXRecordDecl();
599       if (BaseElementTy.isPODType(CGF.getContext()) ||
600           (Record && hasTrivialCopyOrMoveConstructor(Record,
601                          Constructor->isMoveConstructor()))) {
602         // Find the source pointer. We knows it's the last argument because
603         // we know we're in a copy constructor.
604         unsigned SrcArgIndex = Args.size() - 1;
605         llvm::Value *SrcPtr
606           = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex]));
607         LValue Src = CGF.EmitLValueForFieldInitialization(SrcPtr, Field, 0);
608 
609         // Copy the aggregate.
610         CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType,
611                               LHS.isVolatileQualified());
612         return;
613       }
614 
615       // Emit the block variables for the array indices, if any.
616       for (unsigned I = 0, N = MemberInit->getNumArrayIndices(); I != N; ++I)
617         CGF.EmitAutoVarDecl(*MemberInit->getArrayIndex(I));
618     }
619 
620     EmitAggMemberInitializer(CGF, LHS, ArrayIndexVar, MemberInit, FieldType, 0);
621 
622     if (!CGF.CGM.getLangOptions().Exceptions)
623       return;
624 
625     // FIXME: If we have an array of classes w/ non-trivial destructors,
626     // we need to destroy in reverse order of construction along the exception
627     // path.
628     const RecordType *RT = FieldType->getAs<RecordType>();
629     if (!RT)
630       return;
631 
632     CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
633     if (!RD->hasTrivialDestructor())
634       CGF.EHStack.pushCleanup<CallMemberDtor>(EHCleanup, Field,
635                                               RD->getDestructor());
636   }
637 }
638 
639 /// Checks whether the given constructor is a valid subject for the
640 /// complete-to-base constructor delegation optimization, i.e.
641 /// emitting the complete constructor as a simple call to the base
642 /// constructor.
643 static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) {
644 
645   // Currently we disable the optimization for classes with virtual
646   // bases because (1) the addresses of parameter variables need to be
647   // consistent across all initializers but (2) the delegate function
648   // call necessarily creates a second copy of the parameter variable.
649   //
650   // The limiting example (purely theoretical AFAIK):
651   //   struct A { A(int &c) { c++; } };
652   //   struct B : virtual A {
653   //     B(int count) : A(count) { printf("%d\n", count); }
654   //   };
655   // ...although even this example could in principle be emitted as a
656   // delegation since the address of the parameter doesn't escape.
657   if (Ctor->getParent()->getNumVBases()) {
658     // TODO: white-list trivial vbase initializers.  This case wouldn't
659     // be subject to the restrictions below.
660 
661     // TODO: white-list cases where:
662     //  - there are no non-reference parameters to the constructor
663     //  - the initializers don't access any non-reference parameters
664     //  - the initializers don't take the address of non-reference
665     //    parameters
666     //  - etc.
667     // If we ever add any of the above cases, remember that:
668     //  - function-try-blocks will always blacklist this optimization
669     //  - we need to perform the constructor prologue and cleanup in
670     //    EmitConstructorBody.
671 
672     return false;
673   }
674 
675   // We also disable the optimization for variadic functions because
676   // it's impossible to "re-pass" varargs.
677   if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic())
678     return false;
679 
680   // FIXME: Decide if we can do a delegation of a delegating constructor.
681   if (Ctor->isDelegatingConstructor())
682     return false;
683 
684   return true;
685 }
686 
687 /// EmitConstructorBody - Emits the body of the current constructor.
688 void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) {
689   const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl());
690   CXXCtorType CtorType = CurGD.getCtorType();
691 
692   // Before we go any further, try the complete->base constructor
693   // delegation optimization.
694   if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor)) {
695     if (CGDebugInfo *DI = getDebugInfo())
696       DI->EmitStopPoint(Builder);
697     EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args);
698     return;
699   }
700 
701   Stmt *Body = Ctor->getBody();
702 
703   // Enter the function-try-block before the constructor prologue if
704   // applicable.
705   bool IsTryBody = (Body && isa<CXXTryStmt>(Body));
706   if (IsTryBody)
707     EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
708 
709   EHScopeStack::stable_iterator CleanupDepth = EHStack.stable_begin();
710 
711   // Emit the constructor prologue, i.e. the base and member
712   // initializers.
713   EmitCtorPrologue(Ctor, CtorType, Args);
714 
715   // Emit the body of the statement.
716   if (IsTryBody)
717     EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
718   else if (Body)
719     EmitStmt(Body);
720 
721   // Emit any cleanup blocks associated with the member or base
722   // initializers, which includes (along the exceptional path) the
723   // destructors for those members and bases that were fully
724   // constructed.
725   PopCleanupBlocks(CleanupDepth);
726 
727   if (IsTryBody)
728     ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
729 }
730 
731 /// EmitCtorPrologue - This routine generates necessary code to initialize
732 /// base classes and non-static data members belonging to this constructor.
733 void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
734                                        CXXCtorType CtorType,
735                                        FunctionArgList &Args) {
736   if (CD->isDelegatingConstructor())
737     return EmitDelegatingCXXConstructorCall(CD, Args);
738 
739   const CXXRecordDecl *ClassDecl = CD->getParent();
740 
741   SmallVector<CXXCtorInitializer *, 8> MemberInitializers;
742 
743   for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
744        E = CD->init_end();
745        B != E; ++B) {
746     CXXCtorInitializer *Member = (*B);
747 
748     if (Member->isBaseInitializer()) {
749       EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
750     } else {
751       assert(Member->isAnyMemberInitializer() &&
752             "Delegating initializer on non-delegating constructor");
753       MemberInitializers.push_back(Member);
754     }
755   }
756 
757   InitializeVTablePointers(ClassDecl);
758 
759   for (unsigned I = 0, E = MemberInitializers.size(); I != E; ++I)
760     EmitMemberInitializer(*this, ClassDecl, MemberInitializers[I], CD, Args);
761 }
762 
763 static bool
764 FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field);
765 
766 static bool
767 HasTrivialDestructorBody(ASTContext &Context,
768                          const CXXRecordDecl *BaseClassDecl,
769                          const CXXRecordDecl *MostDerivedClassDecl)
770 {
771   // If the destructor is trivial we don't have to check anything else.
772   if (BaseClassDecl->hasTrivialDestructor())
773     return true;
774 
775   if (!BaseClassDecl->getDestructor()->hasTrivialBody())
776     return false;
777 
778   // Check fields.
779   for (CXXRecordDecl::field_iterator I = BaseClassDecl->field_begin(),
780        E = BaseClassDecl->field_end(); I != E; ++I) {
781     const FieldDecl *Field = *I;
782 
783     if (!FieldHasTrivialDestructorBody(Context, Field))
784       return false;
785   }
786 
787   // Check non-virtual bases.
788   for (CXXRecordDecl::base_class_const_iterator I =
789        BaseClassDecl->bases_begin(), E = BaseClassDecl->bases_end();
790        I != E; ++I) {
791     if (I->isVirtual())
792       continue;
793 
794     const CXXRecordDecl *NonVirtualBase =
795       cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
796     if (!HasTrivialDestructorBody(Context, NonVirtualBase,
797                                   MostDerivedClassDecl))
798       return false;
799   }
800 
801   if (BaseClassDecl == MostDerivedClassDecl) {
802     // Check virtual bases.
803     for (CXXRecordDecl::base_class_const_iterator I =
804          BaseClassDecl->vbases_begin(), E = BaseClassDecl->vbases_end();
805          I != E; ++I) {
806       const CXXRecordDecl *VirtualBase =
807         cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
808       if (!HasTrivialDestructorBody(Context, VirtualBase,
809                                     MostDerivedClassDecl))
810         return false;
811     }
812   }
813 
814   return true;
815 }
816 
817 static bool
818 FieldHasTrivialDestructorBody(ASTContext &Context,
819                               const FieldDecl *Field)
820 {
821   QualType FieldBaseElementType = Context.getBaseElementType(Field->getType());
822 
823   const RecordType *RT = FieldBaseElementType->getAs<RecordType>();
824   if (!RT)
825     return true;
826 
827   CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
828   return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
829 }
830 
831 /// CanSkipVTablePointerInitialization - Check whether we need to initialize
832 /// any vtable pointers before calling this destructor.
833 static bool CanSkipVTablePointerInitialization(ASTContext &Context,
834                                                const CXXDestructorDecl *Dtor) {
835   if (!Dtor->hasTrivialBody())
836     return false;
837 
838   // Check the fields.
839   const CXXRecordDecl *ClassDecl = Dtor->getParent();
840   for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
841        E = ClassDecl->field_end(); I != E; ++I) {
842     const FieldDecl *Field = *I;
843 
844     if (!FieldHasTrivialDestructorBody(Context, Field))
845       return false;
846   }
847 
848   return true;
849 }
850 
851 /// EmitDestructorBody - Emits the body of the current destructor.
852 void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) {
853   const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl());
854   CXXDtorType DtorType = CurGD.getDtorType();
855 
856   // The call to operator delete in a deleting destructor happens
857   // outside of the function-try-block, which means it's always
858   // possible to delegate the destructor body to the complete
859   // destructor.  Do so.
860   if (DtorType == Dtor_Deleting) {
861     EnterDtorCleanups(Dtor, Dtor_Deleting);
862     EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
863                           LoadCXXThis());
864     PopCleanupBlock();
865     return;
866   }
867 
868   Stmt *Body = Dtor->getBody();
869 
870   // If the body is a function-try-block, enter the try before
871   // anything else.
872   bool isTryBody = (Body && isa<CXXTryStmt>(Body));
873   if (isTryBody)
874     EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
875 
876   // Enter the epilogue cleanups.
877   RunCleanupsScope DtorEpilogue(*this);
878 
879   // If this is the complete variant, just invoke the base variant;
880   // the epilogue will destruct the virtual bases.  But we can't do
881   // this optimization if the body is a function-try-block, because
882   // we'd introduce *two* handler blocks.
883   switch (DtorType) {
884   case Dtor_Deleting: llvm_unreachable("already handled deleting case");
885 
886   case Dtor_Complete:
887     // Enter the cleanup scopes for virtual bases.
888     EnterDtorCleanups(Dtor, Dtor_Complete);
889 
890     if (!isTryBody) {
891       EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false,
892                             LoadCXXThis());
893       break;
894     }
895     // Fallthrough: act like we're in the base variant.
896 
897   case Dtor_Base:
898     // Enter the cleanup scopes for fields and non-virtual bases.
899     EnterDtorCleanups(Dtor, Dtor_Base);
900 
901     // Initialize the vtable pointers before entering the body.
902     if (!CanSkipVTablePointerInitialization(getContext(), Dtor))
903         InitializeVTablePointers(Dtor->getParent());
904 
905     if (isTryBody)
906       EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
907     else if (Body)
908       EmitStmt(Body);
909     else {
910       assert(Dtor->isImplicit() && "bodyless dtor not implicit");
911       // nothing to do besides what's in the epilogue
912     }
913     // -fapple-kext must inline any call to this dtor into
914     // the caller's body.
915     if (getContext().getLangOptions().AppleKext)
916       CurFn->addFnAttr(llvm::Attribute::AlwaysInline);
917     break;
918   }
919 
920   // Jump out through the epilogue cleanups.
921   DtorEpilogue.ForceCleanup();
922 
923   // Exit the try if applicable.
924   if (isTryBody)
925     ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
926 }
927 
928 namespace {
929   /// Call the operator delete associated with the current destructor.
930   struct CallDtorDelete : EHScopeStack::Cleanup {
931     CallDtorDelete() {}
932 
933     void Emit(CodeGenFunction &CGF, Flags flags) {
934       const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
935       const CXXRecordDecl *ClassDecl = Dtor->getParent();
936       CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
937                          CGF.getContext().getTagDeclType(ClassDecl));
938     }
939   };
940 
941   class DestroyField  : public EHScopeStack::Cleanup {
942     const FieldDecl *field;
943     CodeGenFunction::Destroyer &destroyer;
944     bool useEHCleanupForArray;
945 
946   public:
947     DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer,
948                  bool useEHCleanupForArray)
949       : field(field), destroyer(*destroyer),
950         useEHCleanupForArray(useEHCleanupForArray) {}
951 
952     void Emit(CodeGenFunction &CGF, Flags flags) {
953       // Find the address of the field.
954       llvm::Value *thisValue = CGF.LoadCXXThis();
955       LValue LV = CGF.EmitLValueForField(thisValue, field, /*CVRQualifiers=*/0);
956       assert(LV.isSimple());
957 
958       CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer,
959                       flags.isForNormalCleanup() && useEHCleanupForArray);
960     }
961   };
962 }
963 
964 /// EmitDtorEpilogue - Emit all code that comes at the end of class's
965 /// destructor. This is to call destructors on members and base classes
966 /// in reverse order of their construction.
967 void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD,
968                                         CXXDtorType DtorType) {
969   assert(!DD->isTrivial() &&
970          "Should not emit dtor epilogue for trivial dtor!");
971 
972   // The deleting-destructor phase just needs to call the appropriate
973   // operator delete that Sema picked up.
974   if (DtorType == Dtor_Deleting) {
975     assert(DD->getOperatorDelete() &&
976            "operator delete missing - EmitDtorEpilogue");
977     EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup);
978     return;
979   }
980 
981   const CXXRecordDecl *ClassDecl = DD->getParent();
982 
983   // The complete-destructor phase just destructs all the virtual bases.
984   if (DtorType == Dtor_Complete) {
985 
986     // We push them in the forward order so that they'll be popped in
987     // the reverse order.
988     for (CXXRecordDecl::base_class_const_iterator I =
989            ClassDecl->vbases_begin(), E = ClassDecl->vbases_end();
990               I != E; ++I) {
991       const CXXBaseSpecifier &Base = *I;
992       CXXRecordDecl *BaseClassDecl
993         = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
994 
995       // Ignore trivial destructors.
996       if (BaseClassDecl->hasTrivialDestructor())
997         continue;
998 
999       EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1000                                         BaseClassDecl,
1001                                         /*BaseIsVirtual*/ true);
1002     }
1003 
1004     return;
1005   }
1006 
1007   assert(DtorType == Dtor_Base);
1008 
1009   // Destroy non-virtual bases.
1010   for (CXXRecordDecl::base_class_const_iterator I =
1011         ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) {
1012     const CXXBaseSpecifier &Base = *I;
1013 
1014     // Ignore virtual bases.
1015     if (Base.isVirtual())
1016       continue;
1017 
1018     CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl();
1019 
1020     // Ignore trivial destructors.
1021     if (BaseClassDecl->hasTrivialDestructor())
1022       continue;
1023 
1024     EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1025                                       BaseClassDecl,
1026                                       /*BaseIsVirtual*/ false);
1027   }
1028 
1029   // Destroy direct fields.
1030   SmallVector<const FieldDecl *, 16> FieldDecls;
1031   for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1032        E = ClassDecl->field_end(); I != E; ++I) {
1033     const FieldDecl *field = *I;
1034     QualType type = field->getType();
1035     QualType::DestructionKind dtorKind = type.isDestructedType();
1036     if (!dtorKind) continue;
1037 
1038     CleanupKind cleanupKind = getCleanupKind(dtorKind);
1039     EHStack.pushCleanup<DestroyField>(cleanupKind, field,
1040                                       getDestroyer(dtorKind),
1041                                       cleanupKind & EHCleanup);
1042   }
1043 }
1044 
1045 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1046 /// constructor for each of several members of an array.
1047 ///
1048 /// \param ctor the constructor to call for each element
1049 /// \param argBegin,argEnd the arguments to evaluate and pass to the
1050 ///   constructor
1051 /// \param arrayType the type of the array to initialize
1052 /// \param arrayBegin an arrayType*
1053 /// \param zeroInitialize true if each element should be
1054 ///   zero-initialized before it is constructed
1055 void
1056 CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
1057                                             const ConstantArrayType *arrayType,
1058                                             llvm::Value *arrayBegin,
1059                                           CallExpr::const_arg_iterator argBegin,
1060                                             CallExpr::const_arg_iterator argEnd,
1061                                             bool zeroInitialize) {
1062   QualType elementType;
1063   llvm::Value *numElements =
1064     emitArrayLength(arrayType, elementType, arrayBegin);
1065 
1066   EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin,
1067                              argBegin, argEnd, zeroInitialize);
1068 }
1069 
1070 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1071 /// constructor for each of several members of an array.
1072 ///
1073 /// \param ctor the constructor to call for each element
1074 /// \param numElements the number of elements in the array;
1075 ///   may be zero
1076 /// \param argBegin,argEnd the arguments to evaluate and pass to the
1077 ///   constructor
1078 /// \param arrayBegin a T*, where T is the type constructed by ctor
1079 /// \param zeroInitialize true if each element should be
1080 ///   zero-initialized before it is constructed
1081 void
1082 CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
1083                                             llvm::Value *numElements,
1084                                             llvm::Value *arrayBegin,
1085                                          CallExpr::const_arg_iterator argBegin,
1086                                            CallExpr::const_arg_iterator argEnd,
1087                                             bool zeroInitialize) {
1088 
1089   // It's legal for numElements to be zero.  This can happen both
1090   // dynamically, because x can be zero in 'new A[x]', and statically,
1091   // because of GCC extensions that permit zero-length arrays.  There
1092   // are probably legitimate places where we could assume that this
1093   // doesn't happen, but it's not clear that it's worth it.
1094   llvm::BranchInst *zeroCheckBranch = 0;
1095 
1096   // Optimize for a constant count.
1097   llvm::ConstantInt *constantCount
1098     = dyn_cast<llvm::ConstantInt>(numElements);
1099   if (constantCount) {
1100     // Just skip out if the constant count is zero.
1101     if (constantCount->isZero()) return;
1102 
1103   // Otherwise, emit the check.
1104   } else {
1105     llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop");
1106     llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty");
1107     zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB);
1108     EmitBlock(loopBB);
1109   }
1110 
1111   // Find the end of the array.
1112   llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements,
1113                                                     "arrayctor.end");
1114 
1115   // Enter the loop, setting up a phi for the current location to initialize.
1116   llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1117   llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop");
1118   EmitBlock(loopBB);
1119   llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2,
1120                                          "arrayctor.cur");
1121   cur->addIncoming(arrayBegin, entryBB);
1122 
1123   // Inside the loop body, emit the constructor call on the array element.
1124 
1125   QualType type = getContext().getTypeDeclType(ctor->getParent());
1126 
1127   // Zero initialize the storage, if requested.
1128   if (zeroInitialize)
1129     EmitNullInitialization(cur, type);
1130 
1131   // C++ [class.temporary]p4:
1132   // There are two contexts in which temporaries are destroyed at a different
1133   // point than the end of the full-expression. The first context is when a
1134   // default constructor is called to initialize an element of an array.
1135   // If the constructor has one or more default arguments, the destruction of
1136   // every temporary created in a default argument expression is sequenced
1137   // before the construction of the next array element, if any.
1138 
1139   {
1140     RunCleanupsScope Scope(*this);
1141 
1142     // Evaluate the constructor and its arguments in a regular
1143     // partial-destroy cleanup.
1144     if (getLangOptions().Exceptions &&
1145         !ctor->getParent()->hasTrivialDestructor()) {
1146       Destroyer *destroyer = destroyCXXObject;
1147       pushRegularPartialArrayCleanup(arrayBegin, cur, type, *destroyer);
1148     }
1149 
1150     EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/ false,
1151                            cur, argBegin, argEnd);
1152   }
1153 
1154   // Go to the next element.
1155   llvm::Value *next =
1156     Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1),
1157                               "arrayctor.next");
1158   cur->addIncoming(next, Builder.GetInsertBlock());
1159 
1160   // Check whether that's the end of the loop.
1161   llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done");
1162   llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont");
1163   Builder.CreateCondBr(done, contBB, loopBB);
1164 
1165   // Patch the earlier check to skip over the loop.
1166   if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
1167 
1168   EmitBlock(contBB);
1169 }
1170 
1171 void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF,
1172                                        llvm::Value *addr,
1173                                        QualType type) {
1174   const RecordType *rtype = type->castAs<RecordType>();
1175   const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl());
1176   const CXXDestructorDecl *dtor = record->getDestructor();
1177   assert(!dtor->isTrivial());
1178   CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false,
1179                             addr);
1180 }
1181 
1182 void
1183 CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
1184                                         CXXCtorType Type, bool ForVirtualBase,
1185                                         llvm::Value *This,
1186                                         CallExpr::const_arg_iterator ArgBeg,
1187                                         CallExpr::const_arg_iterator ArgEnd) {
1188 
1189   CGDebugInfo *DI = getDebugInfo();
1190   if (DI && CGM.getCodeGenOpts().LimitDebugInfo) {
1191     // If debug info for this class has been emitted then this is the right time
1192     // to do so.
1193     const CXXRecordDecl *Parent = D->getParent();
1194     DI->getOrCreateRecordType(CGM.getContext().getTypeDeclType(Parent),
1195                               Parent->getLocation());
1196   }
1197 
1198   if (D->isTrivial()) {
1199     if (ArgBeg == ArgEnd) {
1200       // Trivial default constructor, no codegen required.
1201       assert(D->isDefaultConstructor() &&
1202              "trivial 0-arg ctor not a default ctor");
1203       return;
1204     }
1205 
1206     assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor");
1207     assert(D->isCopyOrMoveConstructor() &&
1208            "trivial 1-arg ctor not a copy/move ctor");
1209 
1210     const Expr *E = (*ArgBeg);
1211     QualType Ty = E->getType();
1212     llvm::Value *Src = EmitLValue(E).getAddress();
1213     EmitAggregateCopy(This, Src, Ty);
1214     return;
1215   }
1216 
1217   llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(D, Type), ForVirtualBase);
1218   llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
1219 
1220   EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, VTT, ArgBeg, ArgEnd);
1221 }
1222 
1223 void
1224 CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1225                                         llvm::Value *This, llvm::Value *Src,
1226                                         CallExpr::const_arg_iterator ArgBeg,
1227                                         CallExpr::const_arg_iterator ArgEnd) {
1228   if (D->isTrivial()) {
1229     assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor");
1230     assert(D->isCopyOrMoveConstructor() &&
1231            "trivial 1-arg ctor not a copy/move ctor");
1232     EmitAggregateCopy(This, Src, (*ArgBeg)->getType());
1233     return;
1234   }
1235   llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D,
1236                                                     clang::Ctor_Complete);
1237   assert(D->isInstance() &&
1238          "Trying to emit a member call expr on a static method!");
1239 
1240   const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
1241 
1242   CallArgList Args;
1243 
1244   // Push the this ptr.
1245   Args.add(RValue::get(This), D->getThisType(getContext()));
1246 
1247 
1248   // Push the src ptr.
1249   QualType QT = *(FPT->arg_type_begin());
1250   llvm::Type *t = CGM.getTypes().ConvertType(QT);
1251   Src = Builder.CreateBitCast(Src, t);
1252   Args.add(RValue::get(Src), QT);
1253 
1254   // Skip over first argument (Src).
1255   ++ArgBeg;
1256   CallExpr::const_arg_iterator Arg = ArgBeg;
1257   for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin()+1,
1258        E = FPT->arg_type_end(); I != E; ++I, ++Arg) {
1259     assert(Arg != ArgEnd && "Running over edge of argument list!");
1260     EmitCallArg(Args, *Arg, *I);
1261   }
1262   // Either we've emitted all the call args, or we have a call to a
1263   // variadic function.
1264   assert((Arg == ArgEnd || FPT->isVariadic()) &&
1265          "Extra arguments in non-variadic function!");
1266   // If we still have any arguments, emit them using the type of the argument.
1267   for (; Arg != ArgEnd; ++Arg) {
1268     QualType ArgType = Arg->getType();
1269     EmitCallArg(Args, *Arg, ArgType);
1270   }
1271 
1272   EmitCall(CGM.getTypes().getFunctionInfo(Args, FPT), Callee,
1273            ReturnValueSlot(), Args, D);
1274 }
1275 
1276 void
1277 CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1278                                                 CXXCtorType CtorType,
1279                                                 const FunctionArgList &Args) {
1280   CallArgList DelegateArgs;
1281 
1282   FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
1283   assert(I != E && "no parameters to constructor");
1284 
1285   // this
1286   DelegateArgs.add(RValue::get(LoadCXXThis()), (*I)->getType());
1287   ++I;
1288 
1289   // vtt
1290   if (llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(Ctor, CtorType),
1291                                          /*ForVirtualBase=*/false)) {
1292     QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy);
1293     DelegateArgs.add(RValue::get(VTT), VoidPP);
1294 
1295     if (CodeGenVTables::needsVTTParameter(CurGD)) {
1296       assert(I != E && "cannot skip vtt parameter, already done with args");
1297       assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type");
1298       ++I;
1299     }
1300   }
1301 
1302   // Explicit arguments.
1303   for (; I != E; ++I) {
1304     const VarDecl *param = *I;
1305     EmitDelegateCallArg(DelegateArgs, param);
1306   }
1307 
1308   EmitCall(CGM.getTypes().getFunctionInfo(Ctor, CtorType),
1309            CGM.GetAddrOfCXXConstructor(Ctor, CtorType),
1310            ReturnValueSlot(), DelegateArgs, Ctor);
1311 }
1312 
1313 namespace {
1314   struct CallDelegatingCtorDtor : EHScopeStack::Cleanup {
1315     const CXXDestructorDecl *Dtor;
1316     llvm::Value *Addr;
1317     CXXDtorType Type;
1318 
1319     CallDelegatingCtorDtor(const CXXDestructorDecl *D, llvm::Value *Addr,
1320                            CXXDtorType Type)
1321       : Dtor(D), Addr(Addr), Type(Type) {}
1322 
1323     void Emit(CodeGenFunction &CGF, Flags flags) {
1324       CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false,
1325                                 Addr);
1326     }
1327   };
1328 }
1329 
1330 void
1331 CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1332                                                   const FunctionArgList &Args) {
1333   assert(Ctor->isDelegatingConstructor());
1334 
1335   llvm::Value *ThisPtr = LoadCXXThis();
1336 
1337   AggValueSlot AggSlot =
1338     AggValueSlot::forAddr(ThisPtr, Qualifiers(),
1339                           AggValueSlot::IsDestructed,
1340                           AggValueSlot::DoesNotNeedGCBarriers,
1341                           AggValueSlot::IsNotAliased);
1342 
1343   EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot);
1344 
1345   const CXXRecordDecl *ClassDecl = Ctor->getParent();
1346   if (CGM.getLangOptions().Exceptions && !ClassDecl->hasTrivialDestructor()) {
1347     CXXDtorType Type =
1348       CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base;
1349 
1350     EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup,
1351                                                 ClassDecl->getDestructor(),
1352                                                 ThisPtr, Type);
1353   }
1354 }
1355 
1356 void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD,
1357                                             CXXDtorType Type,
1358                                             bool ForVirtualBase,
1359                                             llvm::Value *This) {
1360   llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(DD, Type),
1361                                      ForVirtualBase);
1362   llvm::Value *Callee = 0;
1363   if (getContext().getLangOptions().AppleKext)
1364     Callee = BuildAppleKextVirtualDestructorCall(DD, Type,
1365                                                  DD->getParent());
1366 
1367   if (!Callee)
1368     Callee = CGM.GetAddrOfCXXDestructor(DD, Type);
1369 
1370   EmitCXXMemberCall(DD, Callee, ReturnValueSlot(), This, VTT, 0, 0);
1371 }
1372 
1373 namespace {
1374   struct CallLocalDtor : EHScopeStack::Cleanup {
1375     const CXXDestructorDecl *Dtor;
1376     llvm::Value *Addr;
1377 
1378     CallLocalDtor(const CXXDestructorDecl *D, llvm::Value *Addr)
1379       : Dtor(D), Addr(Addr) {}
1380 
1381     void Emit(CodeGenFunction &CGF, Flags flags) {
1382       CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
1383                                 /*ForVirtualBase=*/false, Addr);
1384     }
1385   };
1386 }
1387 
1388 void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D,
1389                                             llvm::Value *Addr) {
1390   EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr);
1391 }
1392 
1393 void CodeGenFunction::PushDestructorCleanup(QualType T, llvm::Value *Addr) {
1394   CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl();
1395   if (!ClassDecl) return;
1396   if (ClassDecl->hasTrivialDestructor()) return;
1397 
1398   const CXXDestructorDecl *D = ClassDecl->getDestructor();
1399   assert(D && D->isUsed() && "destructor not marked as used!");
1400   PushDestructorCleanup(D, Addr);
1401 }
1402 
1403 llvm::Value *
1404 CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This,
1405                                            const CXXRecordDecl *ClassDecl,
1406                                            const CXXRecordDecl *BaseClassDecl) {
1407   llvm::Value *VTablePtr = GetVTablePtr(This, Int8PtrTy);
1408   CharUnits VBaseOffsetOffset =
1409     CGM.getVTables().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl);
1410 
1411   llvm::Value *VBaseOffsetPtr =
1412     Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(),
1413                                "vbase.offset.ptr");
1414   llvm::Type *PtrDiffTy =
1415     ConvertType(getContext().getPointerDiffType());
1416 
1417   VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
1418                                          PtrDiffTy->getPointerTo());
1419 
1420   llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1421 
1422   return VBaseOffset;
1423 }
1424 
1425 void
1426 CodeGenFunction::InitializeVTablePointer(BaseSubobject Base,
1427                                          const CXXRecordDecl *NearestVBase,
1428                                          CharUnits OffsetFromNearestVBase,
1429                                          llvm::Constant *VTable,
1430                                          const CXXRecordDecl *VTableClass) {
1431   const CXXRecordDecl *RD = Base.getBase();
1432 
1433   // Compute the address point.
1434   llvm::Value *VTableAddressPoint;
1435 
1436   // Check if we need to use a vtable from the VTT.
1437   if (CodeGenVTables::needsVTTParameter(CurGD) &&
1438       (RD->getNumVBases() || NearestVBase)) {
1439     // Get the secondary vpointer index.
1440     uint64_t VirtualPointerIndex =
1441      CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base);
1442 
1443     /// Load the VTT.
1444     llvm::Value *VTT = LoadCXXVTT();
1445     if (VirtualPointerIndex)
1446       VTT = Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex);
1447 
1448     // And load the address point from the VTT.
1449     VTableAddressPoint = Builder.CreateLoad(VTT);
1450   } else {
1451     uint64_t AddressPoint = CGM.getVTables().getAddressPoint(Base, VTableClass);
1452     VTableAddressPoint =
1453       Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint);
1454   }
1455 
1456   // Compute where to store the address point.
1457   llvm::Value *VirtualOffset = 0;
1458   CharUnits NonVirtualOffset = CharUnits::Zero();
1459 
1460   if (CodeGenVTables::needsVTTParameter(CurGD) && NearestVBase) {
1461     // We need to use the virtual base offset offset because the virtual base
1462     // might have a different offset in the most derived class.
1463     VirtualOffset = GetVirtualBaseClassOffset(LoadCXXThis(), VTableClass,
1464                                               NearestVBase);
1465     NonVirtualOffset = OffsetFromNearestVBase;
1466   } else {
1467     // We can just use the base offset in the complete class.
1468     NonVirtualOffset = Base.getBaseOffset();
1469   }
1470 
1471   // Apply the offsets.
1472   llvm::Value *VTableField = LoadCXXThis();
1473 
1474   if (!NonVirtualOffset.isZero() || VirtualOffset)
1475     VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField,
1476                                                   NonVirtualOffset,
1477                                                   VirtualOffset);
1478 
1479   // Finally, store the address point.
1480   llvm::Type *AddressPointPtrTy =
1481     VTableAddressPoint->getType()->getPointerTo();
1482   VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy);
1483   Builder.CreateStore(VTableAddressPoint, VTableField);
1484 }
1485 
1486 void
1487 CodeGenFunction::InitializeVTablePointers(BaseSubobject Base,
1488                                           const CXXRecordDecl *NearestVBase,
1489                                           CharUnits OffsetFromNearestVBase,
1490                                           bool BaseIsNonVirtualPrimaryBase,
1491                                           llvm::Constant *VTable,
1492                                           const CXXRecordDecl *VTableClass,
1493                                           VisitedVirtualBasesSetTy& VBases) {
1494   // If this base is a non-virtual primary base the address point has already
1495   // been set.
1496   if (!BaseIsNonVirtualPrimaryBase) {
1497     // Initialize the vtable pointer for this base.
1498     InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase,
1499                             VTable, VTableClass);
1500   }
1501 
1502   const CXXRecordDecl *RD = Base.getBase();
1503 
1504   // Traverse bases.
1505   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1506        E = RD->bases_end(); I != E; ++I) {
1507     CXXRecordDecl *BaseDecl
1508       = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1509 
1510     // Ignore classes without a vtable.
1511     if (!BaseDecl->isDynamicClass())
1512       continue;
1513 
1514     CharUnits BaseOffset;
1515     CharUnits BaseOffsetFromNearestVBase;
1516     bool BaseDeclIsNonVirtualPrimaryBase;
1517 
1518     if (I->isVirtual()) {
1519       // Check if we've visited this virtual base before.
1520       if (!VBases.insert(BaseDecl))
1521         continue;
1522 
1523       const ASTRecordLayout &Layout =
1524         getContext().getASTRecordLayout(VTableClass);
1525 
1526       BaseOffset = Layout.getVBaseClassOffset(BaseDecl);
1527       BaseOffsetFromNearestVBase = CharUnits::Zero();
1528       BaseDeclIsNonVirtualPrimaryBase = false;
1529     } else {
1530       const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1531 
1532       BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl);
1533       BaseOffsetFromNearestVBase =
1534         OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl);
1535       BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl;
1536     }
1537 
1538     InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset),
1539                              I->isVirtual() ? BaseDecl : NearestVBase,
1540                              BaseOffsetFromNearestVBase,
1541                              BaseDeclIsNonVirtualPrimaryBase,
1542                              VTable, VTableClass, VBases);
1543   }
1544 }
1545 
1546 void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) {
1547   // Ignore classes without a vtable.
1548   if (!RD->isDynamicClass())
1549     return;
1550 
1551   // Get the VTable.
1552   llvm::Constant *VTable = CGM.getVTables().GetAddrOfVTable(RD);
1553 
1554   // Initialize the vtable pointers for this class and all of its bases.
1555   VisitedVirtualBasesSetTy VBases;
1556   InitializeVTablePointers(BaseSubobject(RD, CharUnits::Zero()),
1557                            /*NearestVBase=*/0,
1558                            /*OffsetFromNearestVBase=*/CharUnits::Zero(),
1559                            /*BaseIsNonVirtualPrimaryBase=*/false,
1560                            VTable, RD, VBases);
1561 }
1562 
1563 llvm::Value *CodeGenFunction::GetVTablePtr(llvm::Value *This,
1564                                            llvm::Type *Ty) {
1565   llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo());
1566   return Builder.CreateLoad(VTablePtrSrc, "vtable");
1567 }
1568 
1569 static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) {
1570   const Expr *E = Base;
1571 
1572   while (true) {
1573     E = E->IgnoreParens();
1574     if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
1575       if (CE->getCastKind() == CK_DerivedToBase ||
1576           CE->getCastKind() == CK_UncheckedDerivedToBase ||
1577           CE->getCastKind() == CK_NoOp) {
1578         E = CE->getSubExpr();
1579         continue;
1580       }
1581     }
1582 
1583     break;
1584   }
1585 
1586   QualType DerivedType = E->getType();
1587   if (const PointerType *PTy = DerivedType->getAs<PointerType>())
1588     DerivedType = PTy->getPointeeType();
1589 
1590   return cast<CXXRecordDecl>(DerivedType->castAs<RecordType>()->getDecl());
1591 }
1592 
1593 // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
1594 // quite what we want.
1595 static const Expr *skipNoOpCastsAndParens(const Expr *E) {
1596   while (true) {
1597     if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
1598       E = PE->getSubExpr();
1599       continue;
1600     }
1601 
1602     if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
1603       if (CE->getCastKind() == CK_NoOp) {
1604         E = CE->getSubExpr();
1605         continue;
1606       }
1607     }
1608     if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
1609       if (UO->getOpcode() == UO_Extension) {
1610         E = UO->getSubExpr();
1611         continue;
1612       }
1613     }
1614     return E;
1615   }
1616 }
1617 
1618 /// canDevirtualizeMemberFunctionCall - Checks whether the given virtual member
1619 /// function call on the given expr can be devirtualized.
1620 /// expr can be devirtualized.
1621 static bool canDevirtualizeMemberFunctionCall(const Expr *Base,
1622                                               const CXXMethodDecl *MD) {
1623   // If the most derived class is marked final, we know that no subclass can
1624   // override this member function and so we can devirtualize it. For example:
1625   //
1626   // struct A { virtual void f(); }
1627   // struct B final : A { };
1628   //
1629   // void f(B *b) {
1630   //   b->f();
1631   // }
1632   //
1633   const CXXRecordDecl *MostDerivedClassDecl = getMostDerivedClassDecl(Base);
1634   if (MostDerivedClassDecl->hasAttr<FinalAttr>())
1635     return true;
1636 
1637   // If the member function is marked 'final', we know that it can't be
1638   // overridden and can therefore devirtualize it.
1639   if (MD->hasAttr<FinalAttr>())
1640     return true;
1641 
1642   // Similarly, if the class itself is marked 'final' it can't be overridden
1643   // and we can therefore devirtualize the member function call.
1644   if (MD->getParent()->hasAttr<FinalAttr>())
1645     return true;
1646 
1647   Base = skipNoOpCastsAndParens(Base);
1648   if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
1649     if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
1650       // This is a record decl. We know the type and can devirtualize it.
1651       return VD->getType()->isRecordType();
1652     }
1653 
1654     return false;
1655   }
1656 
1657   // We can always devirtualize calls on temporary object expressions.
1658   if (isa<CXXConstructExpr>(Base))
1659     return true;
1660 
1661   // And calls on bound temporaries.
1662   if (isa<CXXBindTemporaryExpr>(Base))
1663     return true;
1664 
1665   // Check if this is a call expr that returns a record type.
1666   if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
1667     return CE->getCallReturnType()->isRecordType();
1668 
1669   // We can't devirtualize the call.
1670   return false;
1671 }
1672 
1673 static bool UseVirtualCall(ASTContext &Context,
1674                            const CXXOperatorCallExpr *CE,
1675                            const CXXMethodDecl *MD) {
1676   if (!MD->isVirtual())
1677     return false;
1678 
1679   // When building with -fapple-kext, all calls must go through the vtable since
1680   // the kernel linker can do runtime patching of vtables.
1681   if (Context.getLangOptions().AppleKext)
1682     return true;
1683 
1684   return !canDevirtualizeMemberFunctionCall(CE->getArg(0), MD);
1685 }
1686 
1687 llvm::Value *
1688 CodeGenFunction::EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E,
1689                                              const CXXMethodDecl *MD,
1690                                              llvm::Value *This) {
1691   const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>();
1692   llvm::Type *Ty =
1693     CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1694                                    FPT->isVariadic());
1695 
1696   if (UseVirtualCall(getContext(), E, MD))
1697     return BuildVirtualCall(MD, This, Ty);
1698 
1699   return CGM.GetAddrOfFunction(MD, Ty);
1700 }
1701