blob: 345eacb673e1d52eee4d36f063f40a250314557d [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- 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
Mike Stump1eb44332009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlssone1b29ef2008-08-22 16:00:37 +000015
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson283a0622009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000020#include "clang/AST/RecordLayout.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000021#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson6815e942009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000026using namespace clang;
27using namespace CodeGen;
28
Mike Stump1eb44332009-09-09 15:08:12 +000029void
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000030CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor,
31 llvm::Constant *DeclPtr) {
Anders Carlsson6815e942009-09-27 18:58:34 +000032 const llvm::Type *Int8PtrTy =
33 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
Mike Stump1eb44332009-09-09 15:08:12 +000034
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000035 std::vector<const llvm::Type *> Params;
36 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000038 // Get the destructor function type
Mike Stump1eb44332009-09-09 15:08:12 +000039 const llvm::Type *DtorFnTy =
Owen Anderson0032b272009-08-13 21:57:51 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000041 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump1eb44332009-09-09 15:08:12 +000042
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000043 Params.clear();
44 Params.push_back(DtorFnTy);
45 Params.push_back(Int8PtrTy);
46 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000048 // Get the __cxa_atexit function type
49 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Mike Stump1eb44332009-09-09 15:08:12 +000050 const llvm::FunctionType *AtExitFnTy =
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000051 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000053 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
54 "__cxa_atexit");
Mike Stump1eb44332009-09-09 15:08:12 +000055
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000056 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
57 "__dso_handle");
Mike Stump1eb44332009-09-09 15:08:12 +000058
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000059 llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000061 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
62 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
63 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
64 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
65}
66
Mike Stump1eb44332009-09-09 15:08:12 +000067void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000068 llvm::Constant *DeclPtr) {
69 assert(D.hasGlobalStorage() &&
70 "VarDecl must have global storage!");
Mike Stump1eb44332009-09-09 15:08:12 +000071
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000072 const Expr *Init = D.getInit();
73 QualType T = D.getType();
Mike Stump1eb44332009-09-09 15:08:12 +000074
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000075 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000076 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000077 } else if (!hasAggregateLLVMType(T)) {
78 llvm::Value *V = EmitScalarExpr(Init);
79 EmitStoreOfScalar(V, DeclPtr, T.isVolatileQualified(), T);
80 } else if (T->isAnyComplexType()) {
81 EmitComplexExprIntoAddr(Init, DeclPtr, T.isVolatileQualified());
82 } else {
83 EmitAggExpr(Init, DeclPtr, T.isVolatileQualified());
Mike Stump1eb44332009-09-09 15:08:12 +000084
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000085 if (const RecordType *RT = T->getAs<RecordType>()) {
86 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
87 if (!RD->hasTrivialDestructor())
88 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr);
89 }
90 }
91}
92
Anders Carlsson89ed31d2009-08-08 23:24:23 +000093void
94CodeGenModule::EmitCXXGlobalInitFunc() {
95 if (CXXGlobalInits.empty())
96 return;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Owen Anderson0032b272009-08-13 21:57:51 +000098 const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Anders Carlsson89ed31d2009-08-08 23:24:23 +000099 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000101 // Create our global initialization function.
102 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000103 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000104 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
105 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000107 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000108 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000109 CXXGlobalInits.size());
110 AddGlobalCtor(Fn);
111}
112
113void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
114 const VarDecl **Decls,
115 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000116 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000117 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000119 for (unsigned i = 0; i != NumDecls; ++i) {
120 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000122 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
123 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
124 }
125 FinishFunction();
126}
127
Mike Stump1eb44332009-09-09 15:08:12 +0000128void
129CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000130 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000131 // FIXME: This should use __cxa_guard_{acquire,release}?
132
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000133 assert(!getContext().getLangOptions().ThreadsafeStatics &&
134 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000135
Anders Carlsson283a0622009-04-13 18:03:33 +0000136 llvm::SmallString<256> GuardVName;
137 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000138 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000140 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000141 llvm::GlobalValue *GuardV =
Owen Anderson0032b272009-08-13 21:57:51 +0000142 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000143 GV->getLinkage(),
Owen Anderson0032b272009-08-13 21:57:51 +0000144 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000145 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000147 // Load the first byte of the guard variable.
Owen Anderson0032b272009-08-13 21:57:51 +0000148 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000149 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000150 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000152 // Compare it against 0.
Owen Anderson0032b272009-08-13 21:57:51 +0000153 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000154 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbar55e87422008-11-11 02:29:29 +0000156 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000157 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000158
159 // If the guard variable is 0, jump to the initializer code.
160 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000162 EmitBlock(InitBlock);
163
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000164 EmitCXXGlobalVarDeclInit(D, GV);
165
Owen Anderson0032b272009-08-13 21:57:51 +0000166 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000167 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000169 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170}
171
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000172RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
173 llvm::Value *Callee,
174 llvm::Value *This,
175 CallExpr::const_arg_iterator ArgBeg,
176 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000177 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000178 "Trying to emit a member call expr on a static method!");
179
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000180 // A call to a trivial destructor requires no code generation.
181 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
182 if (Destructor->isTrivial())
183 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
John McCall183700f2009-09-21 23:43:11 +0000185 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000187 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000189 // Push the this ptr.
190 Args.push_back(std::make_pair(RValue::get(This),
191 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000193 // And the rest of the call args
194 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
John McCall183700f2009-09-21 23:43:11 +0000196 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000197 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
198 Callee, Args, MD);
199}
200
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000201/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
202/// expr can be devirtualized.
203static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
204 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
205 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
206 // This is a record decl. We know the type and can devirtualize it.
207 return VD->getType()->isRecordType();
208 }
Anders Carlsson76366482009-10-12 19:45:47 +0000209
210 return false;
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000211 }
212
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000213 // We can always devirtualize calls on temporary object expressions.
Anders Carlsson76366482009-10-12 19:45:47 +0000214 if (isa<CXXTemporaryObjectExpr>(Base))
215 return true;
216
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000217 // And calls on bound temporaries.
218 if (isa<CXXBindTemporaryExpr>(Base))
219 return true;
220
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000221 // Check if this is a call expr that returns a record type.
222 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
223 return CE->getCallReturnType()->isRecordType();
224
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000225 // We can't devirtualize the call.
226 return false;
227}
228
Anders Carlsson774e7c62009-04-03 22:50:24 +0000229RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000230 if (isa<BinaryOperator>(CE->getCallee()))
231 return EmitCXXMemberPointerCallExpr(CE);
232
Anders Carlsson774e7c62009-04-03 22:50:24 +0000233 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
234 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000235
Anders Carlsson2472bf02009-09-29 03:54:11 +0000236 if (MD->isStatic()) {
237 // The method is static, emit it as we would a regular call.
238 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
239 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
240 CE->arg_begin(), CE->arg_end(), 0);
241
242 }
243
John McCall183700f2009-09-21 23:43:11 +0000244 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000245
Mike Stump1eb44332009-09-09 15:08:12 +0000246 const llvm::Type *Ty =
247 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000248 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000249 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Anders Carlsson774e7c62009-04-03 22:50:24 +0000251 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000252 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000253 else {
254 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000255 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000256 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000257
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000258 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000259 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000260 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000261 //
262 // We also don't emit a virtual call if the base expression has a record type
263 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000264 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000265 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000266 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000267 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000268 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000269 = dyn_cast<CXXDestructorDecl>(MD))
270 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000271 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000272 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000273
274 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000275 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000276}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000277
Mike Stump1eb44332009-09-09 15:08:12 +0000278RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000279CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
280 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson3eea6352009-10-13 17:41:28 +0000281 const Expr *BaseExpr = BO->getLHS();
282 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000283
Anders Carlsson3eea6352009-10-13 17:41:28 +0000284 const MemberPointerType *MPT =
285 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000286 const FunctionProtoType *FPT =
287 MPT->getPointeeType()->getAs<FunctionProtoType>();
288 const CXXRecordDecl *RD =
289 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl());
290
291 const llvm::FunctionType *FTy =
292 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
293 FPT->isVariadic());
294
295 const llvm::Type *Int8PtrTy =
296 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
297
298 // Get the member function pointer.
299 llvm::Value *MemFnPtr =
Anders Carlsson3eea6352009-10-13 17:41:28 +0000300 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
301 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000302
303 // Emit the 'this' pointer.
304 llvm::Value *This;
305
306 if (BO->getOpcode() == BinaryOperator::PtrMemI)
307 This = EmitScalarExpr(BaseExpr);
308 else
309 This = EmitLValue(BaseExpr).getAddress();
310
311 // Adjust it.
312 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
313 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
314
315 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
316 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
317
318 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
319
320 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
321
322 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
323
324 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
325
326 // If the LSB in the function pointer is 1, the function pointer points to
327 // a virtual function.
328 llvm::Value *IsVirtual
329 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
330 "and");
331
332 IsVirtual = Builder.CreateTrunc(IsVirtual,
333 llvm::Type::getInt1Ty(VMContext));
334
335 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
336 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
337 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
338
339 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
340 EmitBlock(FnVirtual);
341
342 const llvm::Type *VTableTy =
343 FTy->getPointerTo()->getPointerTo()->getPointerTo();
344
345 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
346 VTable = Builder.CreateLoad(VTable);
347
348 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
349
350 // Since the function pointer is 1 plus the virtual table offset, we
351 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000352 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000353
354 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
355
356 EmitBranch(FnEnd);
357 EmitBlock(FnNonVirtual);
358
359 // If the function is not virtual, just load the pointer.
360 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
361 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
362
363 EmitBlock(FnEnd);
364
365 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
366 Callee->reserveOperandSpace(2);
367 Callee->addIncoming(VirtualFn, FnVirtual);
368 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
369
370 CallArgList Args;
371
372 QualType ThisType =
373 getContext().getPointerType(getContext().getTagDeclType(RD));
374
375 // Push the this ptr.
376 Args.push_back(std::make_pair(RValue::get(This), ThisType));
377
378 // And the rest of the call args
379 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
380 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
381 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
382 Callee, Args, 0);
383}
384
385RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000386CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
387 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000388 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000389 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Fariborz Jahanianad258832009-08-13 21:09:41 +0000391 if (MD->isCopyAssignment()) {
392 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
393 if (ClassDecl->hasTrivialCopyAssignment()) {
394 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
395 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
396 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
397 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
398 QualType Ty = E->getType();
399 EmitAggregateCopy(This, Src, Ty);
400 return RValue::get(This);
401 }
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
John McCall183700f2009-09-21 23:43:11 +0000404 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000405 const llvm::Type *Ty =
406 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000407 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000408 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Anders Carlsson0f294632009-05-27 04:18:27 +0000410 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlsson0f294632009-05-27 04:18:27 +0000412 return EmitCXXMemberCall(MD, Callee, This,
413 E->arg_begin() + 1, E->arg_end());
414}
415
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000416llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000417 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000418 "Must be in a C++ member function decl to load 'this'");
419 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
420 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000422 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000423 // ans: See how CodeGenFunction::LoadObjCSelf() uses
424 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000425 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
426}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000427
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000428/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
429/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000430/// array.
431/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
432/// array type and 'ArrayPtr' points to the beginning fo the array.
433/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000434void
435CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000436 const ConstantArrayType *ArrayTy,
437 llvm::Value *ArrayPtr) {
438 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
439 llvm::Value * NumElements =
440 llvm::ConstantInt::get(SizeTy,
441 getContext().getConstantArrayElementCount(ArrayTy));
442
443 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
444}
445
446void
447CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
448 llvm::Value *NumElements,
449 llvm::Value *ArrayPtr) {
450 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000452 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000453 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
454 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
455 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000457 // Start the loop with a block that tests the condition.
458 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
459 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000461 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000463 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000465 // Generate: if (loop-index < number-of-elements fall to the loop body,
466 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000467 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000468 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000469 // If the condition is true, execute the body.
470 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000472 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000474 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000475 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000476 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000477 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
478 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000479 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000481 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000483 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000484 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000485 Counter = Builder.CreateLoad(IndexPtr);
486 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
487 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000489 // Finally, branch back up to the condition for the next iteration.
490 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000492 // Emit the fall-through block.
493 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000494}
495
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000496/// EmitCXXAggrDestructorCall - calls the default destructor on array
497/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000498void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000499CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
500 const ArrayType *Array,
501 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000502 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
503 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000504 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000505 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000506 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000507 // Create a temporary for the loop index and initialize it with count of
508 // array elements.
509 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
510 "loop.index");
511 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000512 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000513 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
514 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000516 // Start the loop with a block that tests the condition.
517 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
518 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000520 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000522 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000524 // Generate: if (loop-index != 0 fall to the loop body,
525 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000526 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000527 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
528 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
529 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
530 "isne");
531 // If the condition is true, execute the body.
532 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000534 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000536 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
537 // Inside the loop body, emit the constructor call on the array element.
538 Counter = Builder.CreateLoad(IndexPtr);
539 Counter = Builder.CreateSub(Counter, One);
540 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
541 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000543 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000545 // Emit the decrement of the loop counter.
546 Counter = Builder.CreateLoad(IndexPtr);
547 Counter = Builder.CreateSub(Counter, One, "dec");
548 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000550 // Finally, branch back up to the condition for the next iteration.
551 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000553 // Emit the fall-through block.
554 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000555}
556
557void
Mike Stump1eb44332009-09-09 15:08:12 +0000558CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
559 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000560 llvm::Value *This,
561 CallExpr::const_arg_iterator ArgBeg,
562 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000563 if (D->isCopyConstructor(getContext())) {
564 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
565 if (ClassDecl->hasTrivialCopyConstructor()) {
566 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
567 "EmitCXXConstructorCall - user declared copy constructor");
568 const Expr *E = (*ArgBeg);
569 QualType Ty = E->getType();
570 llvm::Value *Src = EmitLValue(E).getAddress();
571 EmitAggregateCopy(This, Src, Ty);
572 return;
573 }
574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000576 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
577
578 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000579}
580
Mike Stump1eb44332009-09-09 15:08:12 +0000581void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000582 CXXDtorType Type,
583 llvm::Value *This) {
584 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Anders Carlsson7267c162009-05-29 21:03:38 +0000586 EmitCXXMemberCall(D, Callee, This, 0, 0);
587}
588
Mike Stump1eb44332009-09-09 15:08:12 +0000589void
590CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000591 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000592 assert(Dest && "Must have a destination!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000593 const CXXConstructorDecl *CD = E->getConstructor();
594 // For a copy constructor, even if it is trivial, must fall thru so
595 // its argument is code-gen'ed.
596 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000597 QualType InitType = E->getType();
598 if (const ArrayType *Array = getContext().getAsArrayType(InitType))
599 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000600 const CXXRecordDecl *RD =
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000601 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000602 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000603 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000606 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000607 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000608 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000609 EmitAggExpr((*i), Dest, false);
610 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000611 }
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000612 if (const ConstantArrayType *Array =
613 getContext().getAsConstantArrayType(E->getType())) {
614 QualType BaseElementTy = getContext().getBaseElementType(Array);
615 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
616 BasePtr = llvm::PointerType::getUnqual(BasePtr);
617 llvm::Value *BaseAddrPtr =
618 Builder.CreateBitCast(Dest, BasePtr);
619 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
620 }
621 else
622 // Call the constructor.
623 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
624 E->arg_begin(), E->arg_end());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000625}
626
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000627void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000628 EmitGlobal(GlobalDecl(D, Ctor_Complete));
629 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000630}
Anders Carlsson363c1842009-04-16 23:57:24 +0000631
Mike Stump1eb44332009-09-09 15:08:12 +0000632void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000633 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Anders Carlsson27ae5362009-04-17 01:58:57 +0000635 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000637 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Anders Carlsson27ae5362009-04-17 01:58:57 +0000639 SetFunctionDefinitionAttributes(D, Fn);
640 SetLLVMFunctionAttributesForDefinition(D, Fn);
641}
642
Anders Carlsson363c1842009-04-16 23:57:24 +0000643llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000644CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000645 CXXCtorType Type) {
646 const llvm::FunctionType *FTy =
647 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Anders Carlsson363c1842009-04-16 23:57:24 +0000649 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000650 return cast<llvm::Function>(
651 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000652}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000653
Mike Stump1eb44332009-09-09 15:08:12 +0000654const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000655 CXXCtorType Type) {
656 llvm::SmallString<256> Name;
657 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000658 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Anders Carlsson27ae5362009-04-17 01:58:57 +0000660 Name += '\0';
661 return UniqueMangledName(Name.begin(), Name.end());
662}
663
664void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000665 EmitCXXDestructor(D, Dtor_Complete);
666 EmitCXXDestructor(D, Dtor_Base);
667}
668
Mike Stump1eb44332009-09-09 15:08:12 +0000669void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000670 CXXDtorType Type) {
671 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000673 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Anders Carlsson27ae5362009-04-17 01:58:57 +0000675 SetFunctionDefinitionAttributes(D, Fn);
676 SetLLVMFunctionAttributesForDefinition(D, Fn);
677}
678
679llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000680CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000681 CXXDtorType Type) {
682 const llvm::FunctionType *FTy =
683 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Anders Carlsson27ae5362009-04-17 01:58:57 +0000685 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000686 return cast<llvm::Function>(
687 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000688}
689
Mike Stump1eb44332009-09-09 15:08:12 +0000690const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000691 CXXDtorType Type) {
692 llvm::SmallString<256> Name;
693 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000694 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Anders Carlsson27ae5362009-04-17 01:58:57 +0000696 Name += '\0';
697 return UniqueMangledName(Name.begin(), Name.end());
698}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000699
Mike Stumped032eb2009-09-04 18:27:16 +0000700llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
701 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000702 bool Extern, int64_t nv,
703 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +0000704 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +0000705
706 FunctionArgList Args;
707 ImplicitParamDecl *ThisDecl =
708 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
709 MD->getThisType(getContext()));
710 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
711 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
712 e = MD->param_end();
713 i != e; ++i) {
714 ParmVarDecl *D = *i;
715 Args.push_back(std::make_pair(D, D->getType()));
716 }
717 IdentifierInfo *II
718 = &CGM.getContext().Idents.get("__thunk_named_foo_");
719 FunctionDecl *FD = FunctionDecl::Create(getContext(),
720 getContext().getTranslationUnitDecl(),
721 SourceLocation(), II, R, 0,
722 Extern
723 ? FunctionDecl::Extern
724 : FunctionDecl::Static,
725 false, true);
726 StartFunction(FD, R, Fn, Args, SourceLocation());
727 // FIXME: generate body
728 FinishFunction();
729 return Fn;
730}
731
Mike Stump6e319f62009-09-11 23:25:56 +0000732llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
733 const CXXMethodDecl *MD,
734 bool Extern,
735 int64_t nv_t,
736 int64_t v_t,
737 int64_t nv_r,
738 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +0000739 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000740
741 FunctionArgList Args;
742 ImplicitParamDecl *ThisDecl =
743 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
744 MD->getThisType(getContext()));
745 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
746 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
747 e = MD->param_end();
748 i != e; ++i) {
749 ParmVarDecl *D = *i;
750 Args.push_back(std::make_pair(D, D->getType()));
751 }
752 IdentifierInfo *II
753 = &CGM.getContext().Idents.get("__thunk_named_foo_");
754 FunctionDecl *FD = FunctionDecl::Create(getContext(),
755 getContext().getTranslationUnitDecl(),
756 SourceLocation(), II, R, 0,
757 Extern
758 ? FunctionDecl::Extern
759 : FunctionDecl::Static,
760 false, true);
761 StartFunction(FD, R, Fn, Args, SourceLocation());
762 // FIXME: generate body
763 FinishFunction();
764 return Fn;
765}
766
Mike Stump77ca8f62009-09-05 07:20:32 +0000767llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
768 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000769 llvm::SmallString<256> OutName;
770 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000771 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000772 llvm::GlobalVariable::LinkageTypes linktype;
773 linktype = llvm::GlobalValue::WeakAnyLinkage;
774 if (!Extern)
775 linktype = llvm::GlobalValue::InternalLinkage;
776 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000777 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000778 const llvm::FunctionType *FTy =
779 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
780 FPT->isVariadic());
781
782 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
783 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000784 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000785 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
786 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
787 return m;
788}
789
Mike Stump6e319f62009-09-11 23:25:56 +0000790llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
791 bool Extern, int64_t nv_t,
792 int64_t v_t, int64_t nv_r,
793 int64_t v_r) {
794 llvm::SmallString<256> OutName;
795 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000796 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000797 llvm::GlobalVariable::LinkageTypes linktype;
798 linktype = llvm::GlobalValue::WeakAnyLinkage;
799 if (!Extern)
800 linktype = llvm::GlobalValue::InternalLinkage;
801 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000802 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000803 const llvm::FunctionType *FTy =
804 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
805 FPT->isVariadic());
806
807 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
808 &getModule());
809 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
810 v_r);
811 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
812 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
813 return m;
814}
815
Mike Stumpf0070db2009-08-26 20:46:33 +0000816llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000817CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
818 const CXXRecordDecl *ClassDecl,
819 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000820 const llvm::Type *Int8PtrTy =
821 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
822
823 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
824 Int8PtrTy->getPointerTo());
825 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
826
Anders Carlssondbd920c2009-10-11 22:13:54 +0000827 int64_t VBaseOffsetIndex =
828 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
829
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000830 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000831 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000832 const llvm::Type *PtrDiffTy =
833 ConvertType(getContext().getPointerDiffType());
834
835 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
836 PtrDiffTy->getPointerTo());
837
838 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
839
840 return VBaseOffset;
841}
842
843llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000844CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
845 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000846 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000847
Mike Stumpf0070db2009-08-26 20:46:33 +0000848 Ty = llvm::PointerType::get(Ty, 0);
849 Ty = llvm::PointerType::get(Ty, 0);
850 Ty = llvm::PointerType::get(Ty, 0);
851 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
852 vtbl = Builder.CreateLoad(vtbl);
853 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000854 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000855 vfn = Builder.CreateLoad(vfn);
856 return vfn;
857}
858
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000859/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
860/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
861/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000862// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000863void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000864 llvm::Value *Src,
865 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000866 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000867 QualType Ty) {
868 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
869 assert(CA && "VLA cannot be copied over");
870 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000872 // Create a temporary for the loop index and initialize it with 0.
873 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
874 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000875 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000876 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000877 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000878 // Start the loop with a block that tests the condition.
879 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
880 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000882 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000884 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
885 // Generate: if (loop-index < number-of-elements fall to the loop body,
886 // otherwise, go to the block after the for-loop.
887 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000888 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000889 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
890 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000891 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000892 "isless");
893 // If the condition is true, execute the body.
894 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000896 EmitBlock(ForBody);
897 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
898 // Inside the loop body, emit the constructor call on the array element.
899 Counter = Builder.CreateLoad(IndexPtr);
900 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
901 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
902 if (BitwiseCopy)
903 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000904 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000905 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000906 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000907 Ctor_Complete);
908 CallArgList CallArgs;
909 // Push the this (Dest) ptr.
910 CallArgs.push_back(std::make_pair(RValue::get(Dest),
911 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000913 // Push the Src ptr.
914 CallArgs.push_back(std::make_pair(RValue::get(Src),
915 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000916 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000917 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000918 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
919 Callee, CallArgs, BaseCopyCtor);
920 }
921 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000923 // Emit the increment of the loop counter.
924 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
925 Counter = Builder.CreateLoad(IndexPtr);
926 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
927 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000929 // Finally, branch back up to the condition for the next iteration.
930 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000932 // Emit the fall-through block.
933 EmitBlock(AfterFor, true);
934}
935
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000936/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000937/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000938/// bitwise assignment or via a copy assignment operator function call.
939/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000940void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000941 llvm::Value *Src,
942 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000943 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000944 QualType Ty) {
945 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
946 assert(CA && "VLA cannot be asssigned");
947 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000949 // Create a temporary for the loop index and initialize it with 0.
950 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
951 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000952 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000953 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
954 Builder.CreateStore(zeroConstant, IndexPtr, false);
955 // Start the loop with a block that tests the condition.
956 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
957 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000959 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000961 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
962 // Generate: if (loop-index < number-of-elements fall to the loop body,
963 // otherwise, go to the block after the for-loop.
964 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000965 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000966 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
967 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000968 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000969 "isless");
970 // If the condition is true, execute the body.
971 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000973 EmitBlock(ForBody);
974 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
975 // Inside the loop body, emit the assignment operator call on array element.
976 Counter = Builder.CreateLoad(IndexPtr);
977 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
978 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
979 const CXXMethodDecl *MD = 0;
980 if (BitwiseAssign)
981 EmitAggregateCopy(Dest, Src, Ty);
982 else {
983 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
984 MD);
985 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
986 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +0000987 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000988 const llvm::Type *LTy =
989 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
990 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000991 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000993 CallArgList CallArgs;
994 // Push the this (Dest) ptr.
995 CallArgs.push_back(std::make_pair(RValue::get(Dest),
996 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000998 // Push the Src ptr.
999 CallArgs.push_back(std::make_pair(RValue::get(Src),
1000 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001001 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001002 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1003 Callee, CallArgs, MD);
1004 }
1005 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001007 // Emit the increment of the loop counter.
1008 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1009 Counter = Builder.CreateLoad(IndexPtr);
1010 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1011 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001013 // Finally, branch back up to the condition for the next iteration.
1014 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001016 // Emit the fall-through block.
1017 EmitBlock(AfterFor, true);
1018}
1019
Fariborz Jahanianca283612009-08-07 23:51:33 +00001020/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1021/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001022/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001023void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001024 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001025 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001026 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1027 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001028 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1029 /*NullCheckValue=*/false);
1030 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1031 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001032 }
1033 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1034 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001035 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
1038 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001039 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001040 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001041 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001042 CallArgList CallArgs;
1043 // Push the this (Dest) ptr.
1044 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1045 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Fariborz Jahanianca283612009-08-07 23:51:33 +00001047 // Push the Src ptr.
1048 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001049 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001050 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001051 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001052 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1053 Callee, CallArgs, BaseCopyCtor);
1054 }
1055}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001056
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001057/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001058/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001059/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001060// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001061void CodeGenFunction::EmitClassCopyAssignment(
1062 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001063 const CXXRecordDecl *ClassDecl,
1064 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001065 QualType Ty) {
1066 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001067 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1068 /*NullCheckValue=*/false);
1069 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1070 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001071 }
1072 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1073 EmitAggregateCopy(Dest, Src, Ty);
1074 return;
1075 }
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001077 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001078 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001079 MD);
1080 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1081 (void)ConstCopyAssignOp;
1082
John McCall183700f2009-09-21 23:43:11 +00001083 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001084 const llvm::Type *LTy =
1085 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001086 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001087 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001089 CallArgList CallArgs;
1090 // Push the this (Dest) ptr.
1091 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1092 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001094 // Push the Src ptr.
1095 CallArgs.push_back(std::make_pair(RValue::get(Src),
1096 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001097 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001098 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001099 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1100 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001101}
1102
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001103/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001104void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001105CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1106 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001107 llvm::Function *Fn,
1108 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001109 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1110 SourceLocation());
1111 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001112 FinishFunction();
1113}
1114
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001115/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001116/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001117/// The implicitly-defined copy constructor for class X performs a memberwise
1118/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001119/// of initialization of bases and members in a user-defined constructor
1120/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001121/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001122/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001123/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001124/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001125/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001126/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001127/// Virtual base class subobjects shall be copied only once by the
1128/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001129
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001130void
1131CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1132 CXXCtorType Type,
1133 llvm::Function *Fn,
1134 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001135 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001136 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001137 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001138 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1139 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001141 FunctionArgList::const_iterator i = Args.begin();
1142 const VarDecl *ThisArg = i->first;
1143 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1144 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1145 const VarDecl *SrcArg = (i+1)->first;
1146 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1147 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001149 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1150 Base != ClassDecl->bases_end(); ++Base) {
1151 // FIXME. copy constrution of virtual base NYI
1152 if (Base->isVirtual())
1153 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001155 CXXRecordDecl *BaseClassDecl
1156 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001157 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1158 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001161 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1162 FieldEnd = ClassDecl->field_end();
1163 Field != FieldEnd; ++Field) {
1164 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001165 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001166 getContext().getAsConstantArrayType(FieldType);
1167 if (Array)
1168 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001170 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1171 CXXRecordDecl *FieldClassDecl
1172 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1173 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1174 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001175 if (Array) {
1176 const llvm::Type *BasePtr = ConvertType(FieldType);
1177 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001178 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001179 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001180 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001181 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1182 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1183 FieldClassDecl, FieldType);
1184 }
Mike Stump1eb44332009-09-09 15:08:12 +00001185 else
1186 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001187 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001188 continue;
1189 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001190 // Do a built-in assignment of scalar data members.
1191 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1192 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1193 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1194 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001195 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001196 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001197}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001198
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001199/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001200/// Before the implicitly-declared copy assignment operator for a class is
1201/// implicitly defined, all implicitly- declared copy assignment operators for
1202/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001203/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001204/// The implicitly-defined copy assignment operator for class X performs
1205/// memberwise assignment of its subob- jects. The direct base classes of X are
1206/// assigned first, in the order of their declaration in
1207/// the base-specifier-list, and then the immediate nonstatic data members of X
1208/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001209/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001210/// if the subobject is of class type, the copy assignment operator for the
1211/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001212/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001213///
Mike Stump1eb44332009-09-09 15:08:12 +00001214/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001215/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001216///
Mike Stump1eb44332009-09-09 15:08:12 +00001217/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001218/// used.
1219void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001220 llvm::Function *Fn,
1221 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001222
1223 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1224 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1225 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001226 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001228 FunctionArgList::const_iterator i = Args.begin();
1229 const VarDecl *ThisArg = i->first;
1230 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1231 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1232 const VarDecl *SrcArg = (i+1)->first;
1233 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1234 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001236 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1237 Base != ClassDecl->bases_end(); ++Base) {
1238 // FIXME. copy assignment of virtual base NYI
1239 if (Base->isVirtual())
1240 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001242 CXXRecordDecl *BaseClassDecl
1243 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1244 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1245 Base->getType());
1246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001248 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1249 FieldEnd = ClassDecl->field_end();
1250 Field != FieldEnd; ++Field) {
1251 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001252 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001253 getContext().getAsConstantArrayType(FieldType);
1254 if (Array)
1255 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001257 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1258 CXXRecordDecl *FieldClassDecl
1259 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1260 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1261 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001262 if (Array) {
1263 const llvm::Type *BasePtr = ConvertType(FieldType);
1264 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1265 llvm::Value *DestBaseAddrPtr =
1266 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1267 llvm::Value *SrcBaseAddrPtr =
1268 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1269 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1270 FieldClassDecl, FieldType);
1271 }
1272 else
Mike Stump1eb44332009-09-09 15:08:12 +00001273 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001274 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001275 continue;
1276 }
1277 // Do a built-in assignment of scalar data members.
1278 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1279 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1280 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1281 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001282 }
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001284 // return *this;
1285 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001287 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001288}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001289
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001290/// EmitCtorPrologue - This routine generates necessary code to initialize
1291/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001292/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001293void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1294 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001295 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001296 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001297 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001299 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001300 E = CD->init_end();
1301 B != E; ++B) {
1302 CXXBaseOrMemberInitializer *Member = (*B);
1303 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001304 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001305 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001306 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001307 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001308 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1309 BaseClassDecl,
1310 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001311 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001312 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001313 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001314 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001315 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001316 // non-static data member initilaizers.
1317 FieldDecl *Field = Member->getMember();
1318 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001319 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001320 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001321 if (Array)
1322 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Mike Stumpf1216772009-07-31 18:25:34 +00001324 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001325 LValue LHS;
1326 if (FieldType->isReferenceType()) {
1327 // FIXME: This is really ugly; should be refactored somehow
1328 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1329 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001330 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1331 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001332 } else {
1333 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1334 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001335 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001336 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001337 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001338 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001339 if (Array) {
1340 const llvm::Type *BasePtr = ConvertType(FieldType);
1341 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001342 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001343 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001344 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001345 Array, BaseAddrPtr);
1346 }
1347 else
1348 EmitCXXConstructorCall(Member->getConstructor(),
1349 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001350 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001351 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001352 continue;
1353 }
1354 else {
1355 // Initializing an anonymous union data member.
1356 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001357 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001358 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001359 FieldType = anonMember->getType();
1360 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001363 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001364 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001365 RValue RHS;
1366 if (FieldType->isReferenceType())
1367 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1368 /*IsInitializer=*/true);
1369 else
1370 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1371 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001372 }
1373 }
Mike Stumpf1216772009-07-31 18:25:34 +00001374
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001375 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001376 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001377 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001378 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001379 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001380 ClassDecl->bases_begin();
1381 Base != ClassDecl->bases_end(); ++Base) {
1382 // FIXME. copy assignment of virtual base NYI
1383 if (Base->isVirtual())
1384 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001386 CXXRecordDecl *BaseClassDecl
1387 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1388 if (BaseClassDecl->hasTrivialConstructor())
1389 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001390 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001391 BaseClassDecl->getDefaultConstructor(getContext())) {
1392 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001393 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1394 BaseClassDecl,
1395 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001396 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1397 }
1398 }
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001400 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1401 FieldEnd = ClassDecl->field_end();
1402 Field != FieldEnd; ++Field) {
1403 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001404 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001405 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001406 if (Array)
1407 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001408 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1409 continue;
1410 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001411 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001412 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1413 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1414 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001415 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001416 MemberClassDecl->getDefaultConstructor(getContext())) {
1417 LoadOfThis = LoadCXXThis();
1418 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001419 if (Array) {
1420 const llvm::Type *BasePtr = ConvertType(FieldType);
1421 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001422 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001423 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1424 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1425 }
1426 else
Mike Stump1eb44332009-09-09 15:08:12 +00001427 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001428 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001429 }
1430 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001431 }
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Mike Stumpf1216772009-07-31 18:25:34 +00001433 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001434 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001435 if (!LoadOfThis)
1436 LoadOfThis = LoadCXXThis();
1437 llvm::Value *VtableField;
1438 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001439 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001440 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1441 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1442 llvm::Value *vtable = GenerateVtable(ClassDecl);
1443 Builder.CreateStore(vtable, VtableField);
1444 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001445}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001446
1447/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001448/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001449/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001450/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001451void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1452 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001453 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001454 assert(!ClassDecl->getNumVBases() &&
1455 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001456 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001458 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1459 *E = DD->destr_end(); B != E; ++B) {
1460 uintptr_t BaseOrMember = (*B);
1461 if (DD->isMemberToDestroy(BaseOrMember)) {
1462 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1463 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001464 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001465 getContext().getAsConstantArrayType(FieldType);
1466 if (Array)
1467 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001468 const RecordType *RT = FieldType->getAs<RecordType>();
1469 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1470 if (FieldClassDecl->hasTrivialDestructor())
1471 continue;
1472 llvm::Value *LoadOfThis = LoadCXXThis();
1473 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001474 if (Array) {
1475 const llvm::Type *BasePtr = ConvertType(FieldType);
1476 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001477 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001478 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001479 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001480 Array, BaseAddrPtr);
1481 }
1482 else
1483 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1484 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001485 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001486 const RecordType *RT =
1487 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1488 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1489 if (BaseClassDecl->hasTrivialDestructor())
1490 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001491 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1492 ClassDecl, BaseClassDecl,
1493 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001494 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001495 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001496 }
1497 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001498 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1499 return;
1500 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001501 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001502 // reverse order of their construction.
1503 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001505 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1506 FieldEnd = ClassDecl->field_end();
1507 Field != FieldEnd; ++Field) {
1508 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001509 if (getContext().getAsConstantArrayType(FieldType))
1510 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001511 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1512 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1513 if (FieldClassDecl->hasTrivialDestructor())
1514 continue;
1515 DestructedFields.push_back(*Field);
1516 }
1517 }
1518 if (!DestructedFields.empty())
1519 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1520 FieldDecl *Field = DestructedFields[i];
1521 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001522 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001523 getContext().getAsConstantArrayType(FieldType);
1524 if (Array)
1525 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001526 const RecordType *RT = FieldType->getAs<RecordType>();
1527 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1528 llvm::Value *LoadOfThis = LoadCXXThis();
1529 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001530 if (Array) {
1531 const llvm::Type *BasePtr = ConvertType(FieldType);
1532 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001533 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001534 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001535 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001536 Array, BaseAddrPtr);
1537 }
1538 else
1539 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1540 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001541 }
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001543 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1544 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1545 Base != ClassDecl->bases_end(); ++Base) {
1546 // FIXME. copy assignment of virtual base NYI
1547 if (Base->isVirtual())
1548 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001550 CXXRecordDecl *BaseClassDecl
1551 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1552 if (BaseClassDecl->hasTrivialDestructor())
1553 continue;
1554 DestructedBases.push_back(BaseClassDecl);
1555 }
1556 if (DestructedBases.empty())
1557 return;
1558 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1559 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001560 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1561 ClassDecl,BaseClassDecl,
1562 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001563 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1564 Dtor_Complete, V);
1565 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001566}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001567
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001568void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1569 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001570 llvm::Function *Fn,
1571 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001573 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001574 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1575 "SynthesizeDefaultDestructor - destructor has user declaration");
1576 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001578 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1579 SourceLocation());
1580 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001581 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001582}
Anders Carlsson6815e942009-09-27 18:58:34 +00001583
1584// FIXME: Move this to CGCXXStmt.cpp
1585void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1586 // FIXME: We need to do more here.
1587 EmitStmt(S.getTryBlock());
1588}