CGCXX.cpp revision ae32e2459edad5cd13384c70a726d7ecb78c9b5c
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
29void
30CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
31                                               llvm::Constant *DeclPtr) {
32  const llvm::Type *Int8PtrTy =
33    llvm::Type::getInt8Ty(VMContext)->getPointerTo();
34
35  std::vector<const llvm::Type *> Params;
36  Params.push_back(Int8PtrTy);
37
38  // Get the destructor function type
39  const llvm::Type *DtorFnTy =
40    llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
41  DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
42
43  Params.clear();
44  Params.push_back(DtorFnTy);
45  Params.push_back(Int8PtrTy);
46  Params.push_back(Int8PtrTy);
47
48  // Get the __cxa_atexit function type
49  // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
50  const llvm::FunctionType *AtExitFnTy =
51    llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
52
53  llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
54                                                       "__cxa_atexit");
55
56  llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
57                                                     "__dso_handle");
58  llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
59                           llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
60                           llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
61  Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
62}
63
64void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
65                                               llvm::Constant *DeclPtr) {
66  assert(D.hasGlobalStorage() &&
67         "VarDecl must have global storage!");
68
69  const Expr *Init = D.getInit();
70  QualType T = D.getType();
71  bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified();
72
73  if (T->isReferenceType()) {
74    ErrorUnsupported(Init, "global variable that binds to a reference");
75  } else if (!hasAggregateLLVMType(T)) {
76    llvm::Value *V = EmitScalarExpr(Init);
77    EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
78  } else if (T->isAnyComplexType()) {
79    EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
80  } else {
81    EmitAggExpr(Init, DeclPtr, isVolatile);
82    // Avoid generating destructor(s) for initialized objects.
83    if (!isa<CXXConstructExpr>(Init))
84      return;
85    const ConstantArrayType *Array = getContext().getAsConstantArrayType(T);
86    if (Array)
87      T = getContext().getBaseElementType(Array);
88
89    if (const RecordType *RT = T->getAs<RecordType>()) {
90      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
91      if (!RD->hasTrivialDestructor()) {
92        llvm::Constant *DtorFn;
93        if (Array) {
94          DtorFn = CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(
95                                                RD->getDestructor(getContext()),
96                                                Array, DeclPtr);
97          DeclPtr =
98            llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
99        }
100        else
101          DtorFn = CGM.GetAddrOfCXXDestructor(RD->getDestructor(getContext()),
102                                              Dtor_Complete);
103        EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
104      }
105    }
106  }
107}
108
109void
110CodeGenModule::EmitCXXGlobalInitFunc() {
111  if (CXXGlobalInits.empty())
112    return;
113
114  const llvm::FunctionType *FTy
115    = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
116                              false);
117
118  // Create our global initialization function.
119  // FIXME: Should this be tweakable by targets?
120  llvm::Function *Fn =
121    llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
122                           "__cxx_global_initialization", &TheModule);
123
124  CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
125                                                   &CXXGlobalInits[0],
126                                                   CXXGlobalInits.size());
127  AddGlobalCtor(Fn);
128}
129
130void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
131                                                const VarDecl **Decls,
132                                                unsigned NumDecls) {
133  StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
134                SourceLocation());
135
136  for (unsigned i = 0; i != NumDecls; ++i) {
137    const VarDecl *D = Decls[i];
138
139    llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
140    EmitCXXGlobalVarDeclInit(*D, DeclPtr);
141  }
142  FinishFunction();
143}
144
145void
146CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
147                                               llvm::GlobalVariable *GV) {
148  // FIXME: This should use __cxa_guard_{acquire,release}?
149
150  assert(!getContext().getLangOptions().ThreadsafeStatics &&
151         "thread safe statics are currently not supported!");
152
153  llvm::SmallString<256> GuardVName;
154  CGM.getMangleContext().mangleGuardVariable(&D, GuardVName);
155
156  // Create the guard variable.
157  llvm::GlobalValue *GuardV =
158    new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext),
159                             false, GV->getLinkage(),
160                llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
161                             GuardVName.str());
162
163  // Load the first byte of the guard variable.
164  const llvm::Type *PtrTy
165    = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
166  llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
167                                      "tmp");
168
169  // Compare it against 0.
170  llvm::Value *nullValue
171    = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
172  llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
173
174  llvm::BasicBlock *InitBlock = createBasicBlock("init");
175  llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
176
177  // If the guard variable is 0, jump to the initializer code.
178  Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
179
180  EmitBlock(InitBlock);
181
182  EmitCXXGlobalVarDeclInit(D, GV);
183
184  Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
185                                             1),
186                      Builder.CreateBitCast(GuardV, PtrTy));
187
188  EmitBlock(EndBlock);
189}
190
191RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
192                                          llvm::Value *Callee,
193                                          llvm::Value *This,
194                                          CallExpr::const_arg_iterator ArgBeg,
195                                          CallExpr::const_arg_iterator ArgEnd) {
196  assert(MD->isInstance() &&
197         "Trying to emit a member call expr on a static method!");
198
199  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
200
201  CallArgList Args;
202
203  // Push the this ptr.
204  Args.push_back(std::make_pair(RValue::get(This),
205                                MD->getThisType(getContext())));
206
207  // And the rest of the call args
208  EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
209
210  QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
211  return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
212                  Callee, Args, MD);
213}
214
215/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
216/// expr can be devirtualized.
217static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
218  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
219    if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
220      // This is a record decl. We know the type and can devirtualize it.
221      return VD->getType()->isRecordType();
222    }
223
224    return false;
225  }
226
227  // We can always devirtualize calls on temporary object expressions.
228  if (isa<CXXTemporaryObjectExpr>(Base))
229    return true;
230
231  // And calls on bound temporaries.
232  if (isa<CXXBindTemporaryExpr>(Base))
233    return true;
234
235  // Check if this is a call expr that returns a record type.
236  if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
237    return CE->getCallReturnType()->isRecordType();
238
239  // We can't devirtualize the call.
240  return false;
241}
242
243RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
244  if (isa<BinaryOperator>(CE->getCallee()))
245    return EmitCXXMemberPointerCallExpr(CE);
246
247  const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
248  const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
249  const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
250
251  if (MD->isStatic()) {
252    // The method is static, emit it as we would a regular call.
253    llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
254    return EmitCall(Callee, getContext().getPointerType(MD->getType()),
255                    CE->arg_begin(), CE->arg_end(), 0);
256
257  }
258
259  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
260
261  const llvm::Type *Ty =
262    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
263                                   FPT->isVariadic());
264  llvm::Value *This;
265
266  if (ME->isArrow())
267    This = EmitScalarExpr(ME->getBase());
268  else {
269    LValue BaseLV = EmitLValue(ME->getBase());
270    This = BaseLV.getAddress();
271  }
272
273  if (MD->isCopyAssignment() && MD->isTrivial()) {
274    // We don't like to generate the trivial copy assignment operator when
275    // it isn't necessary; just produce the proper effect here.
276    llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress();
277    EmitAggregateCopy(This, RHS, CE->getType());
278    return RValue::get(This);
279  }
280
281  // C++ [class.virtual]p12:
282  //   Explicit qualification with the scope operator (5.1) suppresses the
283  //   virtual call mechanism.
284  //
285  // We also don't emit a virtual call if the base expression has a record type
286  // because then we know what the type is.
287  llvm::Value *Callee;
288  if (const CXXDestructorDecl *Destructor
289             = dyn_cast<CXXDestructorDecl>(MD)) {
290    if (Destructor->isTrivial())
291      return RValue::get(0);
292    if (MD->isVirtual() && !ME->hasQualifier() &&
293        !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
294      Callee = BuildVirtualCall(Destructor, Dtor_Complete, This, Ty);
295    } else {
296      Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
297    }
298  } else if (MD->isVirtual() && !ME->hasQualifier() &&
299             !canDevirtualizeMemberFunctionCalls(ME->getBase())) {
300    Callee = BuildVirtualCall(MD, This, Ty);
301  } else {
302    Callee = CGM.GetAddrOfFunction(MD, Ty);
303  }
304
305  return EmitCXXMemberCall(MD, Callee, This,
306                           CE->arg_begin(), CE->arg_end());
307}
308
309RValue
310CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
311  const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
312  const Expr *BaseExpr = BO->getLHS();
313  const Expr *MemFnExpr = BO->getRHS();
314
315  const MemberPointerType *MPT =
316    MemFnExpr->getType()->getAs<MemberPointerType>();
317  const FunctionProtoType *FPT =
318    MPT->getPointeeType()->getAs<FunctionProtoType>();
319  const CXXRecordDecl *RD =
320    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
321
322  const llvm::FunctionType *FTy =
323    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
324                                   FPT->isVariadic());
325
326  const llvm::Type *Int8PtrTy =
327    llvm::Type::getInt8Ty(VMContext)->getPointerTo();
328
329  // Get the member function pointer.
330  llvm::Value *MemFnPtr =
331    CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
332  EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
333
334  // Emit the 'this' pointer.
335  llvm::Value *This;
336
337  if (BO->getOpcode() == BinaryOperator::PtrMemI)
338    This = EmitScalarExpr(BaseExpr);
339  else
340    This = EmitLValue(BaseExpr).getAddress();
341
342  // Adjust it.
343  llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
344  Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
345
346  llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
347  Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
348
349  This = Builder.CreateBitCast(Ptr, This->getType(), "this");
350
351  llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
352
353  const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
354
355  llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
356
357  // If the LSB in the function pointer is 1, the function pointer points to
358  // a virtual function.
359  llvm::Value *IsVirtual
360    = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
361                        "and");
362
363  IsVirtual = Builder.CreateTrunc(IsVirtual,
364                                  llvm::Type::getInt1Ty(VMContext));
365
366  llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
367  llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
368  llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
369
370  Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
371  EmitBlock(FnVirtual);
372
373  const llvm::Type *VTableTy =
374    FTy->getPointerTo()->getPointerTo()->getPointerTo();
375
376  llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
377  VTable = Builder.CreateLoad(VTable);
378
379  VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
380
381  // Since the function pointer is 1 plus the virtual table offset, we
382  // subtract 1 by using a GEP.
383  VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
384
385  llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
386
387  EmitBranch(FnEnd);
388  EmitBlock(FnNonVirtual);
389
390  // If the function is not virtual, just load the pointer.
391  llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
392  NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
393
394  EmitBlock(FnEnd);
395
396  llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
397  Callee->reserveOperandSpace(2);
398  Callee->addIncoming(VirtualFn, FnVirtual);
399  Callee->addIncoming(NonVirtualFn, FnNonVirtual);
400
401  CallArgList Args;
402
403  QualType ThisType =
404    getContext().getPointerType(getContext().getTagDeclType(RD));
405
406  // Push the this ptr.
407  Args.push_back(std::make_pair(RValue::get(This), ThisType));
408
409  // And the rest of the call args
410  EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
411  QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
412  return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
413                  Callee, Args, 0);
414}
415
416RValue
417CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
418                                               const CXXMethodDecl *MD) {
419  assert(MD->isInstance() &&
420         "Trying to emit a member call expr on a static method!");
421
422  if (MD->isCopyAssignment()) {
423    const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
424    if (ClassDecl->hasTrivialCopyAssignment()) {
425      assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
426             "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
427      llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
428      llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
429      QualType Ty = E->getType();
430      EmitAggregateCopy(This, Src, Ty);
431      return RValue::get(This);
432    }
433  }
434
435  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
436  const llvm::Type *Ty =
437    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
438                                   FPT->isVariadic());
439
440  llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
441
442  llvm::Value *Callee;
443  if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0)))
444    Callee = BuildVirtualCall(MD, This, Ty);
445  else
446    Callee = CGM.GetAddrOfFunction(MD, Ty);
447
448  return EmitCXXMemberCall(MD, Callee, This,
449                           E->arg_begin() + 1, E->arg_end());
450}
451
452llvm::Value *CodeGenFunction::LoadCXXThis() {
453  assert(isa<CXXMethodDecl>(CurFuncDecl) &&
454         "Must be in a C++ member function decl to load 'this'");
455  assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
456         "Must be in a C++ member function decl to load 'this'");
457
458  // FIXME: What if we're inside a block?
459  // ans: See how CodeGenFunction::LoadObjCSelf() uses
460  // CodeGenFunction::BlockForwardSelf() for how to do this.
461  return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
462}
463
464/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
465/// for-loop to call the default constructor on individual members of the
466/// array.
467/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
468/// array type and 'ArrayPtr' points to the beginning fo the array.
469/// It is assumed that all relevant checks have been made by the caller.
470void
471CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
472                                          const ConstantArrayType *ArrayTy,
473                                          llvm::Value *ArrayPtr,
474                                          CallExpr::const_arg_iterator ArgBeg,
475                                          CallExpr::const_arg_iterator ArgEnd) {
476
477  const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
478  llvm::Value * NumElements =
479    llvm::ConstantInt::get(SizeTy,
480                           getContext().getConstantArrayElementCount(ArrayTy));
481
482  EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd);
483}
484
485void
486CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
487                                          llvm::Value *NumElements,
488                                          llvm::Value *ArrayPtr,
489                                          CallExpr::const_arg_iterator ArgBeg,
490                                          CallExpr::const_arg_iterator ArgEnd) {
491  const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
492
493  // Create a temporary for the loop index and initialize it with 0.
494  llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
495  llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
496  Builder.CreateStore(Zero, IndexPtr, false);
497
498  // Start the loop with a block that tests the condition.
499  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
500  llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
501
502  EmitBlock(CondBlock);
503
504  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
505
506  // Generate: if (loop-index < number-of-elements fall to the loop body,
507  // otherwise, go to the block after the for-loop.
508  llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
509  llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
510  // If the condition is true, execute the body.
511  Builder.CreateCondBr(IsLess, ForBody, AfterFor);
512
513  EmitBlock(ForBody);
514
515  llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
516  // Inside the loop body, emit the constructor call on the array element.
517  Counter = Builder.CreateLoad(IndexPtr);
518  llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
519                                                   "arrayidx");
520
521  // C++ [class.temporary]p4:
522  // There are two contexts in which temporaries are destroyed at a different
523  // point than the end of the full- expression. The first context is when a
524  // default constructor is called to initialize an element of an array.
525  // If the constructor has one or more default arguments, the destruction of
526  // every temporary created in a default argument expression is sequenced
527  // before the construction of the next array element, if any.
528
529  // Keep track of the current number of live temporaries.
530  unsigned OldNumLiveTemporaries = LiveTemporaries.size();
531
532  EmitCXXConstructorCall(D, Ctor_Complete, Address, ArgBeg, ArgEnd);
533
534  // Pop temporaries.
535  while (LiveTemporaries.size() > OldNumLiveTemporaries)
536    PopCXXTemporary();
537
538  EmitBlock(ContinueBlock);
539
540  // Emit the increment of the loop counter.
541  llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
542  Counter = Builder.CreateLoad(IndexPtr);
543  NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
544  Builder.CreateStore(NextVal, IndexPtr, false);
545
546  // Finally, branch back up to the condition for the next iteration.
547  EmitBranch(CondBlock);
548
549  // Emit the fall-through block.
550  EmitBlock(AfterFor, true);
551}
552
553/// EmitCXXAggrDestructorCall - calls the default destructor on array
554/// elements in reverse order of construction.
555void
556CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
557                                           const ArrayType *Array,
558                                           llvm::Value *This) {
559  const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
560  assert(CA && "Do we support VLA for destruction ?");
561  uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
562  llvm::Value* ElementCountPtr =
563    llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
564  EmitCXXAggrDestructorCall(D, ElementCountPtr, This);
565}
566
567/// EmitCXXAggrDestructorCall - calls the default destructor on array
568/// elements in reverse order of construction.
569void
570CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
571                                           llvm::Value *UpperCount,
572                                           llvm::Value *This) {
573  llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
574                                            1);
575  // Create a temporary for the loop index and initialize it with count of
576  // array elements.
577  llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
578                                           "loop.index");
579  // Index = ElementCount;
580  Builder.CreateStore(UpperCount, IndexPtr, false);
581
582  // Start the loop with a block that tests the condition.
583  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
584  llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
585
586  EmitBlock(CondBlock);
587
588  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
589
590  // Generate: if (loop-index != 0 fall to the loop body,
591  // otherwise, go to the block after the for-loop.
592  llvm::Value* zeroConstant =
593    llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
594  llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
595  llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
596                                            "isne");
597  // If the condition is true, execute the body.
598  Builder.CreateCondBr(IsNE, ForBody, AfterFor);
599
600  EmitBlock(ForBody);
601
602  llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
603  // Inside the loop body, emit the constructor call on the array element.
604  Counter = Builder.CreateLoad(IndexPtr);
605  Counter = Builder.CreateSub(Counter, One);
606  llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
607  if (D->isVirtual()) {
608    const llvm::Type *Ty =
609      CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(D),
610                                     /*isVariadic=*/false);
611
612    llvm::Value *Callee = BuildVirtualCall(D, Dtor_Deleting, Address, Ty);
613    EmitCXXMemberCall(D, Callee, Address, 0, 0);
614  }
615  else
616    EmitCXXDestructorCall(D, Dtor_Complete, Address);
617
618  EmitBlock(ContinueBlock);
619
620  // Emit the decrement of the loop counter.
621  Counter = Builder.CreateLoad(IndexPtr);
622  Counter = Builder.CreateSub(Counter, One, "dec");
623  Builder.CreateStore(Counter, IndexPtr, false);
624
625  // Finally, branch back up to the condition for the next iteration.
626  EmitBranch(CondBlock);
627
628  // Emit the fall-through block.
629  EmitBlock(AfterFor, true);
630}
631
632/// GenerateCXXAggrDestructorHelper - Generates a helper function which when
633/// invoked, calls the default destructor on array elements in reverse order of
634/// construction.
635llvm::Constant *
636CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
637                                                 const ArrayType *Array,
638                                                 llvm::Value *This) {
639  static int UniqueCount;
640  FunctionArgList Args;
641  ImplicitParamDecl *Dst =
642    ImplicitParamDecl::Create(getContext(), 0,
643                              SourceLocation(), 0,
644                              getContext().getPointerType(getContext().VoidTy));
645  Args.push_back(std::make_pair(Dst, Dst->getType()));
646
647  llvm::SmallString<16> Name;
648  llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueCount);
649  QualType R = getContext().VoidTy;
650  const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args);
651  const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
652  llvm::Function *Fn =
653    llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
654                           Name.c_str(),
655                           &CGM.getModule());
656  IdentifierInfo *II
657    = &CGM.getContext().Idents.get(Name.c_str());
658  FunctionDecl *FD = FunctionDecl::Create(getContext(),
659                                          getContext().getTranslationUnitDecl(),
660                                          SourceLocation(), II, R, 0,
661                                          FunctionDecl::Static,
662                                          false, true);
663  StartFunction(FD, R, Fn, Args, SourceLocation());
664  QualType BaseElementTy = getContext().getBaseElementType(Array);
665  const llvm::Type *BasePtr = ConvertType(BaseElementTy);
666  BasePtr = llvm::PointerType::getUnqual(BasePtr);
667  llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
668  EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
669  FinishFunction();
670  llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
671                                              0);
672  llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
673  return m;
674}
675
676void
677CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
678                                        CXXCtorType Type,
679                                        llvm::Value *This,
680                                        CallExpr::const_arg_iterator ArgBeg,
681                                        CallExpr::const_arg_iterator ArgEnd) {
682  if (D->isCopyConstructor(getContext())) {
683    const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
684    if (ClassDecl->hasTrivialCopyConstructor()) {
685      assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
686             "EmitCXXConstructorCall - user declared copy constructor");
687      const Expr *E = (*ArgBeg);
688      QualType Ty = E->getType();
689      llvm::Value *Src = EmitLValue(E).getAddress();
690      EmitAggregateCopy(This, Src, Ty);
691      return;
692    }
693  } else if (D->isTrivial()) {
694    // FIXME: Track down why we're trying to generate calls to the trivial
695    // default constructor!
696    return;
697  }
698
699  llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
700
701  EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
702}
703
704void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
705                                            CXXDtorType Type,
706                                            llvm::Value *This) {
707  llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
708
709  EmitCXXMemberCall(D, Callee, This, 0, 0);
710}
711
712void
713CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
714                                      const CXXConstructExpr *E) {
715  assert(Dest && "Must have a destination!");
716  const CXXConstructorDecl *CD = E->getConstructor();
717  const ConstantArrayType *Array =
718    getContext().getAsConstantArrayType(E->getType());
719  // For a copy constructor, even if it is trivial, must fall thru so
720  // its argument is code-gen'ed.
721  if (!CD->isCopyConstructor(getContext())) {
722    QualType InitType = E->getType();
723    if (Array)
724      InitType = getContext().getBaseElementType(Array);
725    const CXXRecordDecl *RD =
726      cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
727    if (RD->hasTrivialConstructor())
728    return;
729  }
730  // Code gen optimization to eliminate copy constructor and return
731  // its first argument instead.
732  if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
733    const Expr *Arg = E->getArg(0);
734
735    if (const CXXBindTemporaryExpr *BindExpr =
736          dyn_cast<CXXBindTemporaryExpr>(Arg))
737      Arg = BindExpr->getSubExpr();
738
739    EmitAggExpr(Arg, Dest, false);
740    return;
741  }
742  if (Array) {
743    QualType BaseElementTy = getContext().getBaseElementType(Array);
744    const llvm::Type *BasePtr = ConvertType(BaseElementTy);
745    BasePtr = llvm::PointerType::getUnqual(BasePtr);
746    llvm::Value *BaseAddrPtr =
747      Builder.CreateBitCast(Dest, BasePtr);
748    EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr,
749                               E->arg_begin(), E->arg_end());
750  }
751  else
752    // Call the constructor.
753    EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
754                           E->arg_begin(), E->arg_end());
755}
756
757void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
758  EmitGlobal(GlobalDecl(D, Ctor_Complete));
759  EmitGlobal(GlobalDecl(D, Ctor_Base));
760}
761
762void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
763                                       CXXCtorType Type) {
764
765  llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
766
767  CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
768
769  SetFunctionDefinitionAttributes(D, Fn);
770  SetLLVMFunctionAttributesForDefinition(D, Fn);
771}
772
773llvm::Function *
774CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
775                                       CXXCtorType Type) {
776  const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
777  const llvm::FunctionType *FTy =
778    getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type),
779                               FPT->isVariadic());
780
781  const char *Name = getMangledCXXCtorName(D, Type);
782  return cast<llvm::Function>(
783                      GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
784}
785
786const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
787                                                 CXXCtorType Type) {
788  llvm::SmallString<256> Name;
789  getMangleContext().mangleCXXCtor(D, Type, Name);
790
791  Name += '\0';
792  return UniqueMangledName(Name.begin(), Name.end());
793}
794
795void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
796  if (D->isVirtual())
797    EmitCXXDestructor(D, Dtor_Deleting);
798  EmitCXXDestructor(D, Dtor_Complete);
799  EmitCXXDestructor(D, Dtor_Base);
800}
801
802void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
803                                      CXXDtorType Type) {
804  llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
805
806  CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
807
808  SetFunctionDefinitionAttributes(D, Fn);
809  SetLLVMFunctionAttributesForDefinition(D, Fn);
810}
811
812llvm::Function *
813CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
814                                      CXXDtorType Type) {
815  const llvm::FunctionType *FTy =
816    getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false);
817
818  const char *Name = getMangledCXXDtorName(D, Type);
819  return cast<llvm::Function>(
820                      GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
821}
822
823const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
824                                                 CXXDtorType Type) {
825  llvm::SmallString<256> Name;
826  getMangleContext().mangleCXXDtor(D, Type, Name);
827
828  Name += '\0';
829  return UniqueMangledName(Name.begin(), Name.end());
830}
831
832llvm::Constant *
833CodeGenFunction::GenerateThunk(llvm::Function *Fn, const CXXMethodDecl *MD,
834                               bool Extern,
835                               const ThunkAdjustment &ThisAdjustment) {
836  return GenerateCovariantThunk(Fn, MD, Extern,
837                                CovariantThunkAdjustment(ThisAdjustment,
838                                                         ThunkAdjustment()));
839}
840
841llvm::Value *
842CodeGenFunction::DynamicTypeAdjust(llvm::Value *V,
843                                   const ThunkAdjustment &Adjustment) {
844  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
845
846  const llvm::Type *OrigTy = V->getType();
847  if (Adjustment.NonVirtual) {
848    // Do the non-virtual adjustment
849    V = Builder.CreateBitCast(V, Int8PtrTy);
850    V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual);
851    V = Builder.CreateBitCast(V, OrigTy);
852  }
853
854  if (!Adjustment.Virtual)
855    return V;
856
857  assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 &&
858         "vtable entry unaligned");
859
860  // Do the virtual this adjustment
861  const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
862  const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo();
863
864  llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy);
865  V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo());
866  V = Builder.CreateLoad(V, "vtable");
867
868  llvm::Value *VTablePtr = V;
869  uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8);
870  V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
871  V = Builder.CreateLoad(V);
872  V = Builder.CreateGEP(ThisVal, V);
873
874  return Builder.CreateBitCast(V, OrigTy);
875}
876
877llvm::Constant *
878CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
879                                   const CXXMethodDecl *MD, bool Extern,
880                                   const CovariantThunkAdjustment &Adjustment) {
881  QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
882
883  FunctionArgList Args;
884  ImplicitParamDecl *ThisDecl =
885    ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
886                              MD->getThisType(getContext()));
887  Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
888  for (FunctionDecl::param_const_iterator i = MD->param_begin(),
889         e = MD->param_end();
890       i != e; ++i) {
891    ParmVarDecl *D = *i;
892    Args.push_back(std::make_pair(D, D->getType()));
893  }
894  IdentifierInfo *II
895    = &CGM.getContext().Idents.get("__thunk_named_foo_");
896  FunctionDecl *FD = FunctionDecl::Create(getContext(),
897                                          getContext().getTranslationUnitDecl(),
898                                          SourceLocation(), II, ResultType, 0,
899                                          Extern
900                                            ? FunctionDecl::Extern
901                                            : FunctionDecl::Static,
902                                          false, true);
903  StartFunction(FD, ResultType, Fn, Args, SourceLocation());
904
905  // generate body
906  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
907  const llvm::Type *Ty =
908    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
909                                   FPT->isVariadic());
910  llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
911  CallArgList CallArgs;
912
913  bool ShouldAdjustReturnPointer = true;
914  QualType ArgType = MD->getThisType(getContext());
915  llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
916  if (!Adjustment.ThisAdjustment.isEmpty()) {
917    // Do the this adjustment.
918    const llvm::Type *OrigTy = Callee->getType();
919    Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment);
920
921    if (!Adjustment.ReturnAdjustment.isEmpty()) {
922      const CovariantThunkAdjustment &ReturnAdjustment =
923        CovariantThunkAdjustment(ThunkAdjustment(),
924                                 Adjustment.ReturnAdjustment);
925
926      Callee = CGM.BuildCovariantThunk(MD, Extern, ReturnAdjustment);
927
928      Callee = Builder.CreateBitCast(Callee, OrigTy);
929      ShouldAdjustReturnPointer = false;
930    }
931  }
932
933  CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
934
935  for (FunctionDecl::param_const_iterator i = MD->param_begin(),
936         e = MD->param_end();
937       i != e; ++i) {
938    ParmVarDecl *D = *i;
939    QualType ArgType = D->getType();
940
941    // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
942    Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
943    CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
944  }
945
946  RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
947                       Callee, CallArgs, MD);
948  if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) {
949    bool CanBeZero = !(ResultType->isReferenceType()
950    // FIXME: attr nonnull can't be zero either
951                       /* || ResultType->hasAttr<NonNullAttr>() */ );
952    // Do the return result adjustment.
953    if (CanBeZero) {
954      llvm::BasicBlock *NonZeroBlock = createBasicBlock();
955      llvm::BasicBlock *ZeroBlock = createBasicBlock();
956      llvm::BasicBlock *ContBlock = createBasicBlock();
957
958      const llvm::Type *Ty = RV.getScalarVal()->getType();
959      llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
960      Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
961                           NonZeroBlock, ZeroBlock);
962      EmitBlock(NonZeroBlock);
963      llvm::Value *NZ =
964        DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment);
965      EmitBranch(ContBlock);
966      EmitBlock(ZeroBlock);
967      llvm::Value *Z = RV.getScalarVal();
968      EmitBlock(ContBlock);
969      llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
970      RVOrZero->reserveOperandSpace(2);
971      RVOrZero->addIncoming(NZ, NonZeroBlock);
972      RVOrZero->addIncoming(Z, ZeroBlock);
973      RV = RValue::get(RVOrZero);
974    } else
975      RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(),
976                                         Adjustment.ReturnAdjustment));
977  }
978
979  if (!ResultType->isVoidType())
980    EmitReturnOfRValue(RV, ResultType);
981
982  FinishFunction();
983  return Fn;
984}
985
986llvm::Constant *
987CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
988                          const ThunkAdjustment &ThisAdjustment) {
989
990  llvm::SmallString<256> OutName;
991  getMangleContext().mangleThunk(MD, ThisAdjustment, OutName);
992
993  llvm::GlobalVariable::LinkageTypes linktype;
994  linktype = llvm::GlobalValue::WeakAnyLinkage;
995  if (!Extern)
996    linktype = llvm::GlobalValue::InternalLinkage;
997  llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
998  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
999  const llvm::FunctionType *FTy =
1000    getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1001                               FPT->isVariadic());
1002
1003  llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
1004                                              &getModule());
1005  CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, ThisAdjustment);
1006  llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1007  return m;
1008}
1009
1010llvm::Constant *
1011CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD, bool Extern,
1012                                   const CovariantThunkAdjustment &Adjustment) {
1013  llvm::SmallString<256> OutName;
1014  getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName);
1015  llvm::GlobalVariable::LinkageTypes linktype;
1016  linktype = llvm::GlobalValue::WeakAnyLinkage;
1017  if (!Extern)
1018    linktype = llvm::GlobalValue::InternalLinkage;
1019  llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1020  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
1021  const llvm::FunctionType *FTy =
1022    getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1023                               FPT->isVariadic());
1024
1025  llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(),
1026                                              &getModule());
1027  CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment);
1028  llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1029  return m;
1030}
1031
1032llvm::Value *
1033CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
1034                                              const CXXRecordDecl *ClassDecl,
1035                                           const CXXRecordDecl *BaseClassDecl) {
1036  const llvm::Type *Int8PtrTy =
1037    llvm::Type::getInt8Ty(VMContext)->getPointerTo();
1038
1039  llvm::Value *VTablePtr = Builder.CreateBitCast(This,
1040                                                 Int8PtrTy->getPointerTo());
1041  VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
1042
1043  int64_t VBaseOffsetIndex =
1044    CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
1045
1046  llvm::Value *VBaseOffsetPtr =
1047    Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
1048  const llvm::Type *PtrDiffTy =
1049    ConvertType(getContext().getPointerDiffType());
1050
1051  VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
1052                                         PtrDiffTy->getPointerTo());
1053
1054  llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
1055
1056  return VBaseOffset;
1057}
1058
1059static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, int64_t VtableIndex,
1060                                     llvm::Value *This, const llvm::Type *Ty) {
1061  Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
1062
1063  llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
1064  Vtable = CGF.Builder.CreateLoad(Vtable);
1065
1066  llvm::Value *VFuncPtr =
1067    CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
1068  return CGF.Builder.CreateLoad(VFuncPtr);
1069}
1070
1071llvm::Value *
1072CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
1073                                  const llvm::Type *Ty) {
1074  MD = MD->getCanonicalDecl();
1075  int64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
1076
1077  return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
1078}
1079
1080llvm::Value *
1081CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
1082                                  llvm::Value *&This, const llvm::Type *Ty) {
1083  DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
1084  int64_t VtableIndex =
1085    CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
1086
1087  return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
1088}
1089
1090/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1091/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1092/// copy or via a copy constructor call.
1093//  FIXME. Consolidate this with EmitCXXAggrConstructorCall.
1094void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
1095                                            llvm::Value *Src,
1096                                            const ArrayType *Array,
1097                                            const CXXRecordDecl *BaseClassDecl,
1098                                            QualType Ty) {
1099  const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1100  assert(CA && "VLA cannot be copied over");
1101  bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
1102
1103  // Create a temporary for the loop index and initialize it with 0.
1104  llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1105                                           "loop.index");
1106  llvm::Value* zeroConstant =
1107    llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1108  Builder.CreateStore(zeroConstant, IndexPtr, false);
1109  // Start the loop with a block that tests the condition.
1110  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1111  llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
1112
1113  EmitBlock(CondBlock);
1114
1115  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1116  // Generate: if (loop-index < number-of-elements fall to the loop body,
1117  // otherwise, go to the block after the for-loop.
1118  uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
1119  llvm::Value * NumElementsPtr =
1120    llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1121  llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
1122  llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
1123                                              "isless");
1124  // If the condition is true, execute the body.
1125  Builder.CreateCondBr(IsLess, ForBody, AfterFor);
1126
1127  EmitBlock(ForBody);
1128  llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1129  // Inside the loop body, emit the constructor call on the array element.
1130  Counter = Builder.CreateLoad(IndexPtr);
1131  Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1132  Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1133  if (BitwiseCopy)
1134    EmitAggregateCopy(Dest, Src, Ty);
1135  else if (CXXConstructorDecl *BaseCopyCtor =
1136           BaseClassDecl->getCopyConstructor(getContext(), 0)) {
1137    llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
1138                                                      Ctor_Complete);
1139    CallArgList CallArgs;
1140    // Push the this (Dest) ptr.
1141    CallArgs.push_back(std::make_pair(RValue::get(Dest),
1142                                      BaseCopyCtor->getThisType(getContext())));
1143
1144    // Push the Src ptr.
1145    CallArgs.push_back(std::make_pair(RValue::get(Src),
1146                                     BaseCopyCtor->getParamDecl(0)->getType()));
1147    QualType ResultType =
1148      BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
1149    EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1150             Callee, CallArgs, BaseCopyCtor);
1151  }
1152  EmitBlock(ContinueBlock);
1153
1154  // Emit the increment of the loop counter.
1155  llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1156  Counter = Builder.CreateLoad(IndexPtr);
1157  NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1158  Builder.CreateStore(NextVal, IndexPtr, false);
1159
1160  // Finally, branch back up to the condition for the next iteration.
1161  EmitBranch(CondBlock);
1162
1163  // Emit the fall-through block.
1164  EmitBlock(AfterFor, true);
1165}
1166
1167/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
1168/// array of objects from SrcValue to DestValue. Assignment can be either a
1169/// bitwise assignment or via a copy assignment operator function call.
1170/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
1171void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
1172                                            llvm::Value *Src,
1173                                            const ArrayType *Array,
1174                                            const CXXRecordDecl *BaseClassDecl,
1175                                            QualType Ty) {
1176  const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1177  assert(CA && "VLA cannot be asssigned");
1178  bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
1179
1180  // Create a temporary for the loop index and initialize it with 0.
1181  llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1182                                           "loop.index");
1183  llvm::Value* zeroConstant =
1184  llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1185  Builder.CreateStore(zeroConstant, IndexPtr, false);
1186  // Start the loop with a block that tests the condition.
1187  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1188  llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
1189
1190  EmitBlock(CondBlock);
1191
1192  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1193  // Generate: if (loop-index < number-of-elements fall to the loop body,
1194  // otherwise, go to the block after the for-loop.
1195  uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
1196  llvm::Value * NumElementsPtr =
1197  llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1198  llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
1199  llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
1200                                              "isless");
1201  // If the condition is true, execute the body.
1202  Builder.CreateCondBr(IsLess, ForBody, AfterFor);
1203
1204  EmitBlock(ForBody);
1205  llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1206  // Inside the loop body, emit the assignment operator call on array element.
1207  Counter = Builder.CreateLoad(IndexPtr);
1208  Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1209  Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1210  const CXXMethodDecl *MD = 0;
1211  if (BitwiseAssign)
1212    EmitAggregateCopy(Dest, Src, Ty);
1213  else {
1214    bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1215                                                               MD);
1216    assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1217    (void)hasCopyAssign;
1218    const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
1219    const llvm::Type *LTy =
1220    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1221                                   FPT->isVariadic());
1222    llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
1223
1224    CallArgList CallArgs;
1225    // Push the this (Dest) ptr.
1226    CallArgs.push_back(std::make_pair(RValue::get(Dest),
1227                                      MD->getThisType(getContext())));
1228
1229    // Push the Src ptr.
1230    CallArgs.push_back(std::make_pair(RValue::get(Src),
1231                                      MD->getParamDecl(0)->getType()));
1232    QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
1233    EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1234             Callee, CallArgs, MD);
1235  }
1236  EmitBlock(ContinueBlock);
1237
1238  // Emit the increment of the loop counter.
1239  llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1240  Counter = Builder.CreateLoad(IndexPtr);
1241  NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1242  Builder.CreateStore(NextVal, IndexPtr, false);
1243
1244  // Finally, branch back up to the condition for the next iteration.
1245  EmitBranch(CondBlock);
1246
1247  // Emit the fall-through block.
1248  EmitBlock(AfterFor, true);
1249}
1250
1251/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1252/// object from SrcValue to DestValue. Copying can be either a bitwise copy
1253/// or via a copy constructor call.
1254void CodeGenFunction::EmitClassMemberwiseCopy(
1255                        llvm::Value *Dest, llvm::Value *Src,
1256                        const CXXRecordDecl *ClassDecl,
1257                        const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1258  if (ClassDecl) {
1259    Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1260                                 /*NullCheckValue=*/false);
1261    Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl,
1262                                /*NullCheckValue=*/false);
1263  }
1264  if (BaseClassDecl->hasTrivialCopyConstructor()) {
1265    EmitAggregateCopy(Dest, Src, Ty);
1266    return;
1267  }
1268
1269  if (CXXConstructorDecl *BaseCopyCtor =
1270      BaseClassDecl->getCopyConstructor(getContext(), 0)) {
1271    llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
1272                                                      Ctor_Complete);
1273    CallArgList CallArgs;
1274    // Push the this (Dest) ptr.
1275    CallArgs.push_back(std::make_pair(RValue::get(Dest),
1276                                      BaseCopyCtor->getThisType(getContext())));
1277
1278    // Push the Src ptr.
1279    CallArgs.push_back(std::make_pair(RValue::get(Src),
1280                       BaseCopyCtor->getParamDecl(0)->getType()));
1281    QualType ResultType =
1282    BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
1283    EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1284             Callee, CallArgs, BaseCopyCtor);
1285  }
1286}
1287
1288/// EmitClassCopyAssignment - This routine generates code to copy assign a class
1289/// object from SrcValue to DestValue. Assignment can be either a bitwise
1290/// assignment of via an assignment operator call.
1291// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
1292void CodeGenFunction::EmitClassCopyAssignment(
1293                                        llvm::Value *Dest, llvm::Value *Src,
1294                                        const CXXRecordDecl *ClassDecl,
1295                                        const CXXRecordDecl *BaseClassDecl,
1296                                        QualType Ty) {
1297  if (ClassDecl) {
1298    Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1299                                 /*NullCheckValue=*/false);
1300    Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl,
1301                                /*NullCheckValue=*/false);
1302  }
1303  if (BaseClassDecl->hasTrivialCopyAssignment()) {
1304    EmitAggregateCopy(Dest, Src, Ty);
1305    return;
1306  }
1307
1308  const CXXMethodDecl *MD = 0;
1309  bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
1310                                                                 MD);
1311  assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1312  (void)ConstCopyAssignOp;
1313
1314  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
1315  const llvm::Type *LTy =
1316    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1317                                   FPT->isVariadic());
1318  llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
1319
1320  CallArgList CallArgs;
1321  // Push the this (Dest) ptr.
1322  CallArgs.push_back(std::make_pair(RValue::get(Dest),
1323                                    MD->getThisType(getContext())));
1324
1325  // Push the Src ptr.
1326  CallArgs.push_back(std::make_pair(RValue::get(Src),
1327                                    MD->getParamDecl(0)->getType()));
1328  QualType ResultType =
1329    MD->getType()->getAs<FunctionType>()->getResultType();
1330  EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1331           Callee, CallArgs, MD);
1332}
1333
1334/// SynthesizeDefaultConstructor - synthesize a default constructor
1335void
1336CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1337                                              CXXCtorType Type,
1338                                              llvm::Function *Fn,
1339                                              const FunctionArgList &Args) {
1340  assert(!Ctor->isTrivial() && "shouldn't need to generate trivial ctor");
1341  StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1342                SourceLocation());
1343  EmitCtorPrologue(Ctor, Type);
1344  FinishFunction();
1345}
1346
1347/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1348/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
1349/// The implicitly-defined copy constructor for class X performs a memberwise
1350/// copy of its subobjects. The order of copying is the same as the order of
1351/// initialization of bases and members in a user-defined constructor
1352/// Each subobject is copied in the manner appropriate to its type:
1353///  if the subobject is of class type, the copy constructor for the class is
1354///  used;
1355///  if the subobject is an array, each element is copied, in the manner
1356///  appropriate to the element type;
1357///  if the subobject is of scalar type, the built-in assignment operator is
1358///  used.
1359/// Virtual base class subobjects shall be copied only once by the
1360/// implicitly-defined copy constructor
1361
1362void
1363CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1364                                              CXXCtorType Type,
1365                                              llvm::Function *Fn,
1366                                              const FunctionArgList &Args) {
1367  const CXXRecordDecl *ClassDecl = Ctor->getParent();
1368  assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
1369      "SynthesizeCXXCopyConstructor - copy constructor has definition already");
1370  assert(!Ctor->isTrivial() && "shouldn't need to generate trivial ctor");
1371  StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1372                SourceLocation());
1373
1374  FunctionArgList::const_iterator i = Args.begin();
1375  const VarDecl *ThisArg = i->first;
1376  llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1377  llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1378  const VarDecl *SrcArg = (i+1)->first;
1379  llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1380  llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
1381
1382  for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1383       Base != ClassDecl->bases_end(); ++Base) {
1384    // FIXME. copy constrution of virtual base NYI
1385    if (Base->isVirtual())
1386      continue;
1387
1388    CXXRecordDecl *BaseClassDecl
1389      = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1390    EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1391                            Base->getType());
1392  }
1393
1394  for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1395       E = ClassDecl->field_end(); I != E; ++I) {
1396    const FieldDecl *Field = *I;
1397
1398    QualType FieldType = getContext().getCanonicalType(Field->getType());
1399    const ConstantArrayType *Array =
1400      getContext().getAsConstantArrayType(FieldType);
1401    if (Array)
1402      FieldType = getContext().getBaseElementType(FieldType);
1403
1404    if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1405      CXXRecordDecl *FieldClassDecl
1406        = cast<CXXRecordDecl>(FieldClassType->getDecl());
1407      LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1408      LValue RHS = EmitLValueForField(LoadOfSrc, Field, false, 0);
1409      if (Array) {
1410        const llvm::Type *BasePtr = ConvertType(FieldType);
1411        BasePtr = llvm::PointerType::getUnqual(BasePtr);
1412        llvm::Value *DestBaseAddrPtr =
1413          Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1414        llvm::Value *SrcBaseAddrPtr =
1415          Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1416        EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1417                                    FieldClassDecl, FieldType);
1418      }
1419      else
1420        EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
1421                                0 /*ClassDecl*/, FieldClassDecl, FieldType);
1422      continue;
1423    }
1424
1425    if (Field->getType()->isReferenceType()) {
1426      unsigned FieldIndex = CGM.getTypes().getLLVMFieldNo(Field);
1427
1428      llvm::Value *LHS = Builder.CreateStructGEP(LoadOfThis, FieldIndex,
1429                                                 "lhs.ref");
1430
1431      llvm::Value *RHS = Builder.CreateStructGEP(LoadOfThis, FieldIndex,
1432                                                 "rhs.ref");
1433
1434      // Load the value in RHS.
1435      RHS = Builder.CreateLoad(RHS);
1436
1437      // And store it in the LHS
1438      Builder.CreateStore(RHS, LHS);
1439
1440      continue;
1441    }
1442    // Do a built-in assignment of scalar data members.
1443    LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1444    LValue RHS = EmitLValueForField(LoadOfSrc, Field, false, 0);
1445
1446    if (!hasAggregateLLVMType(Field->getType())) {
1447      RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType());
1448      EmitStoreThroughLValue(RVRHS, LHS, Field->getType());
1449    } else if (Field->getType()->isAnyComplexType()) {
1450      ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(),
1451                                               RHS.isVolatileQualified());
1452      StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified());
1453    } else {
1454      EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType());
1455    }
1456  }
1457  FinishFunction();
1458}
1459
1460/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
1461/// Before the implicitly-declared copy assignment operator for a class is
1462/// implicitly defined, all implicitly- declared copy assignment operators for
1463/// its direct base classes and its nonstatic data members shall have been
1464/// implicitly defined. [12.8-p12]
1465/// The implicitly-defined copy assignment operator for class X performs
1466/// memberwise assignment of its subob- jects. The direct base classes of X are
1467/// assigned first, in the order of their declaration in
1468/// the base-specifier-list, and then the immediate nonstatic data members of X
1469/// are assigned, in the order in which they were declared in the class
1470/// definition.Each subobject is assigned in the manner appropriate to its type:
1471///   if the subobject is of class type, the copy assignment operator for the
1472///   class is used (as if by explicit qualification; that is, ignoring any
1473///   possible virtual overriding functions in more derived classes);
1474///
1475///   if the subobject is an array, each element is assigned, in the manner
1476///   appropriate to the element type;
1477///
1478///   if the subobject is of scalar type, the built-in assignment operator is
1479///   used.
1480void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
1481                                                  llvm::Function *Fn,
1482                                                  const FunctionArgList &Args) {
1483
1484  const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1485  assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1486         "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
1487  StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
1488
1489  FunctionArgList::const_iterator i = Args.begin();
1490  const VarDecl *ThisArg = i->first;
1491  llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1492  llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1493  const VarDecl *SrcArg = (i+1)->first;
1494  llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1495  llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
1496
1497  for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1498       Base != ClassDecl->bases_end(); ++Base) {
1499    // FIXME. copy assignment of virtual base NYI
1500    if (Base->isVirtual())
1501      continue;
1502
1503    CXXRecordDecl *BaseClassDecl
1504      = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1505    EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1506                            Base->getType());
1507  }
1508
1509  for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1510       FieldEnd = ClassDecl->field_end();
1511       Field != FieldEnd; ++Field) {
1512    QualType FieldType = getContext().getCanonicalType((*Field)->getType());
1513    const ConstantArrayType *Array =
1514      getContext().getAsConstantArrayType(FieldType);
1515    if (Array)
1516      FieldType = getContext().getBaseElementType(FieldType);
1517
1518    if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1519      CXXRecordDecl *FieldClassDecl
1520      = cast<CXXRecordDecl>(FieldClassType->getDecl());
1521      LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1522      LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1523      if (Array) {
1524        const llvm::Type *BasePtr = ConvertType(FieldType);
1525        BasePtr = llvm::PointerType::getUnqual(BasePtr);
1526        llvm::Value *DestBaseAddrPtr =
1527          Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1528        llvm::Value *SrcBaseAddrPtr =
1529          Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1530        EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1531                                    FieldClassDecl, FieldType);
1532      }
1533      else
1534        EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
1535                               0 /*ClassDecl*/, FieldClassDecl, FieldType);
1536      continue;
1537    }
1538    // Do a built-in assignment of scalar data members.
1539    LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1540    LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1541    RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1542    EmitStoreThroughLValue(RVRHS, LHS, FieldType);
1543  }
1544
1545  // return *this;
1546  Builder.CreateStore(LoadOfThis, ReturnValue);
1547
1548  FinishFunction();
1549}
1550
1551static void EmitBaseInitializer(CodeGenFunction &CGF,
1552                                const CXXRecordDecl *ClassDecl,
1553                                CXXBaseOrMemberInitializer *BaseInit,
1554                                CXXCtorType CtorType) {
1555  assert(BaseInit->isBaseInitializer() &&
1556         "Must have base initializer!");
1557
1558  llvm::Value *ThisPtr = CGF.LoadCXXThis();
1559
1560  const Type *BaseType = BaseInit->getBaseClass();
1561  CXXRecordDecl *BaseClassDecl =
1562    cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
1563  llvm::Value *V = CGF.GetAddressOfBaseClass(ThisPtr, ClassDecl,
1564                                             BaseClassDecl,
1565                                             /*NullCheckValue=*/false);
1566  CGF.EmitCXXConstructorCall(BaseInit->getConstructor(),
1567                             CtorType, V,
1568                             BaseInit->const_arg_begin(),
1569                             BaseInit->const_arg_end());
1570}
1571
1572static void EmitMemberInitializer(CodeGenFunction &CGF,
1573                                  const CXXRecordDecl *ClassDecl,
1574                                  CXXBaseOrMemberInitializer *MemberInit) {
1575  assert(MemberInit->isMemberInitializer() &&
1576         "Must have member initializer!");
1577
1578  // non-static data member initializers.
1579  FieldDecl *Field = MemberInit->getMember();
1580  QualType FieldType = CGF.getContext().getCanonicalType(Field->getType());
1581
1582  llvm::Value *ThisPtr = CGF.LoadCXXThis();
1583  LValue LHS;
1584  if (FieldType->isReferenceType()) {
1585    // FIXME: This is really ugly; should be refactored somehow
1586    unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field);
1587    llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp");
1588    assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1589    LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType));
1590  } else {
1591    LHS = CGF.EmitLValueForField(ThisPtr, Field, ClassDecl->isUnion(), 0);
1592  }
1593
1594  // If we are initializing an anonymous union field, drill down to the field.
1595  if (MemberInit->getAnonUnionMember()) {
1596    Field = MemberInit->getAnonUnionMember();
1597    LHS = CGF.EmitLValueForField(LHS.getAddress(), Field,
1598                                 /*IsUnion=*/true, 0);
1599    FieldType = Field->getType();
1600  }
1601
1602  // If the field is an array, branch based on the element type.
1603  const ConstantArrayType *Array =
1604    CGF.getContext().getAsConstantArrayType(FieldType);
1605  if (Array)
1606    FieldType = CGF.getContext().getBaseElementType(FieldType);
1607
1608  // We lose the constructor for anonymous union members, so handle them
1609  // explicitly.
1610  // FIXME: This is somwhat ugly.
1611  if (MemberInit->getAnonUnionMember() && FieldType->getAs<RecordType>()) {
1612    if (MemberInit->getNumArgs())
1613      CGF.EmitAggExpr(*MemberInit->arg_begin(), LHS.getAddress(),
1614                      LHS.isVolatileQualified());
1615    else
1616      CGF.EmitAggregateClear(LHS.getAddress(), Field->getType());
1617    return;
1618  }
1619
1620  if (FieldType->getAs<RecordType>()) {
1621    assert(MemberInit->getConstructor() &&
1622           "EmitCtorPrologue - no constructor to initialize member");
1623    if (Array) {
1624      const llvm::Type *BasePtr = CGF.ConvertType(FieldType);
1625      BasePtr = llvm::PointerType::getUnqual(BasePtr);
1626      llvm::Value *BaseAddrPtr =
1627        CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1628      CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(),
1629                                     Array, BaseAddrPtr,
1630                                     MemberInit->const_arg_begin(),
1631                                     MemberInit->const_arg_end());
1632    }
1633    else
1634      CGF.EmitCXXConstructorCall(MemberInit->getConstructor(),
1635                                 Ctor_Complete, LHS.getAddress(),
1636                                 MemberInit->const_arg_begin(),
1637                                 MemberInit->const_arg_end());
1638    return;
1639  }
1640
1641  assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only");
1642  Expr *RhsExpr = *MemberInit->arg_begin();
1643  RValue RHS;
1644  if (FieldType->isReferenceType()) {
1645    RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType,
1646                                    /*IsInitializer=*/true);
1647    CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
1648  } else if (Array) {
1649    CGF.EmitMemSetToZero(LHS.getAddress(), Field->getType());
1650  } else if (!CGF.hasAggregateLLVMType(RhsExpr->getType())) {
1651    RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true));
1652    CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
1653  } else if (RhsExpr->getType()->isAnyComplexType()) {
1654    CGF.EmitComplexExprIntoAddr(RhsExpr, LHS.getAddress(),
1655                                LHS.isVolatileQualified());
1656  } else {
1657    // Handle member function pointers; other aggregates shouldn't get this far.
1658    CGF.EmitAggExpr(RhsExpr, LHS.getAddress(), LHS.isVolatileQualified());
1659  }
1660}
1661
1662/// EmitCtorPrologue - This routine generates necessary code to initialize
1663/// base classes and non-static data members belonging to this constructor.
1664/// FIXME: This needs to take a CXXCtorType.
1665void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1666                                       CXXCtorType CtorType) {
1667  const CXXRecordDecl *ClassDecl = CD->getParent();
1668
1669  // FIXME: Add vbase initialization
1670  llvm::Value *LoadOfThis = 0;
1671
1672  for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
1673       E = CD->init_end();
1674       B != E; ++B) {
1675    CXXBaseOrMemberInitializer *Member = (*B);
1676
1677    assert(LiveTemporaries.empty() &&
1678           "Should not have any live temporaries at initializer start!");
1679
1680    if (Member->isBaseInitializer())
1681      EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
1682    else
1683      EmitMemberInitializer(*this, ClassDecl, Member);
1684
1685    // Pop any live temporaries that the initializers might have pushed.
1686    while (!LiveTemporaries.empty())
1687      PopCXXTemporary();
1688  }
1689
1690  // Initialize the vtable pointer
1691  if (ClassDecl->isDynamicClass()) {
1692    if (!LoadOfThis)
1693      LoadOfThis = LoadCXXThis();
1694    llvm::Value *VtableField;
1695    llvm::Type *Ptr8Ty, *PtrPtr8Ty;
1696    Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
1697    PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1698    VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1699    llvm::Value *vtable = CGM.getVtableInfo().getVtable(ClassDecl);
1700    Builder.CreateStore(vtable, VtableField);
1701  }
1702}
1703
1704/// EmitDtorEpilogue - Emit all code that comes at the end of class's
1705/// destructor. This is to call destructors on members and base classes
1706/// in reverse order of their construction.
1707/// FIXME: This needs to take a CXXDtorType.
1708void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1709                                       CXXDtorType DtorType) {
1710  assert(!DD->isTrivial() &&
1711         "Should not emit dtor epilogue for trivial dtor!");
1712
1713  const CXXRecordDecl *ClassDecl = DD->getParent();
1714
1715  // Collect the fields.
1716  llvm::SmallVector<const FieldDecl *, 16> FieldDecls;
1717  for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(),
1718       E = ClassDecl->field_end(); I != E; ++I) {
1719    const FieldDecl *Field = *I;
1720
1721    QualType FieldType = getContext().getCanonicalType(Field->getType());
1722    FieldType = getContext().getBaseElementType(FieldType);
1723
1724    const RecordType *RT = FieldType->getAs<RecordType>();
1725    if (!RT)
1726      continue;
1727
1728    CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1729    if (FieldClassDecl->hasTrivialDestructor())
1730        continue;
1731
1732    FieldDecls.push_back(Field);
1733  }
1734
1735  // Now destroy the fields.
1736  for (size_t i = FieldDecls.size(); i > 0; --i) {
1737    const FieldDecl *Field = FieldDecls[i - 1];
1738
1739    QualType FieldType = Field->getType();
1740    const ConstantArrayType *Array =
1741      getContext().getAsConstantArrayType(FieldType);
1742    if (Array)
1743      FieldType = getContext().getBaseElementType(FieldType);
1744
1745    const RecordType *RT = FieldType->getAs<RecordType>();
1746    CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1747
1748    llvm::Value *ThisPtr = LoadCXXThis();
1749
1750    LValue LHS = EmitLValueForField(ThisPtr, Field,
1751                                    /*isUnion=*/false,
1752                                    // FIXME: Qualifiers?
1753                                    /*CVRQualifiers=*/0);
1754    if (Array) {
1755      const llvm::Type *BasePtr = ConvertType(FieldType);
1756      BasePtr = llvm::PointerType::getUnqual(BasePtr);
1757      llvm::Value *BaseAddrPtr =
1758        Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1759      EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
1760                                Array, BaseAddrPtr);
1761    } else
1762      EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1763                            Dtor_Complete, LHS.getAddress());
1764  }
1765
1766  // Destroy non-virtual bases.
1767  for (CXXRecordDecl::reverse_base_class_const_iterator I =
1768        ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) {
1769    const CXXBaseSpecifier &Base = *I;
1770
1771    // Ignore virtual bases.
1772    if (Base.isVirtual())
1773      continue;
1774
1775    CXXRecordDecl *BaseClassDecl
1776      = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1777
1778    // Ignore trivial destructors.
1779    if (BaseClassDecl->hasTrivialDestructor())
1780      continue;
1781
1782    llvm::Value *V = GetAddressOfBaseClass(LoadCXXThis(),
1783                                           ClassDecl, BaseClassDecl,
1784                                           /*NullCheckValue=*/false);
1785    EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1786                          Dtor_Base, V);
1787  }
1788
1789  // If we're emitting a base destructor, we don't want to emit calls to the
1790  // virtual bases.
1791  if (DtorType == Dtor_Base)
1792    return;
1793
1794  // FIXME: Handle virtual bases.
1795  for (CXXRecordDecl::reverse_base_class_const_iterator I =
1796       ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); I != E; ++I) {
1797    assert(false && "FIXME: Handle virtual bases.");
1798  }
1799
1800  // If we have a deleting destructor, emit a call to the delete operator.
1801  if (DtorType == Dtor_Deleting)
1802    EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(),
1803                   getContext().getTagDeclType(ClassDecl));
1804}
1805
1806void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1807                                                  CXXDtorType DtorType,
1808                                                  llvm::Function *Fn,
1809                                                  const FunctionArgList &Args) {
1810  assert(!Dtor->getParent()->hasUserDeclaredDestructor() &&
1811         "SynthesizeDefaultDestructor - destructor has user declaration");
1812
1813  StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1814                SourceLocation());
1815
1816  EmitDtorEpilogue(Dtor, DtorType);
1817  FinishFunction();
1818}
1819