blob: 44e5207e53d2b23e532d408b7178c0514b5ff4cb [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);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000569 // FIXME. This may not be correct. But till now, we were skipping
570 // code gen of trivial copy constructors regardless of their arguments.
571 if (isa<CXXZeroInitValueExpr>(E))
572 return;
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000573 QualType Ty = E->getType();
574 llvm::Value *Src = EmitLValue(E).getAddress();
575 EmitAggregateCopy(This, Src, Ty);
576 return;
577 }
578 }
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000580 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
581
582 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000583}
584
Mike Stump1eb44332009-09-09 15:08:12 +0000585void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000586 CXXDtorType Type,
587 llvm::Value *This) {
588 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Anders Carlsson7267c162009-05-29 21:03:38 +0000590 EmitCXXMemberCall(D, Callee, This, 0, 0);
591}
592
Mike Stump1eb44332009-09-09 15:08:12 +0000593void
594CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000595 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000596 assert(Dest && "Must have a destination!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000597 const CXXConstructorDecl *CD = E->getConstructor();
598 // For a copy constructor, even if it is trivial, must fall thru so
599 // its argument is code-gen'ed.
600 if (!CD->isCopyConstructor(getContext())) {
601 const CXXRecordDecl *RD =
602 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
603 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000604 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000607 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000608 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000609 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000610 EmitAggExpr((*i), Dest, false);
611 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000612 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000613 // Call the constructor.
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000614 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000615 E->arg_begin(), E->arg_end());
616}
617
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000618void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000619 EmitGlobal(GlobalDecl(D, Ctor_Complete));
620 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000621}
Anders Carlsson363c1842009-04-16 23:57:24 +0000622
Mike Stump1eb44332009-09-09 15:08:12 +0000623void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000624 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Anders Carlsson27ae5362009-04-17 01:58:57 +0000626 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000628 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Anders Carlsson27ae5362009-04-17 01:58:57 +0000630 SetFunctionDefinitionAttributes(D, Fn);
631 SetLLVMFunctionAttributesForDefinition(D, Fn);
632}
633
Anders Carlsson363c1842009-04-16 23:57:24 +0000634llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000635CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000636 CXXCtorType Type) {
637 const llvm::FunctionType *FTy =
638 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Anders Carlsson363c1842009-04-16 23:57:24 +0000640 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000641 return cast<llvm::Function>(
642 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000643}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000644
Mike Stump1eb44332009-09-09 15:08:12 +0000645const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000646 CXXCtorType Type) {
647 llvm::SmallString<256> Name;
648 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000649 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Anders Carlsson27ae5362009-04-17 01:58:57 +0000651 Name += '\0';
652 return UniqueMangledName(Name.begin(), Name.end());
653}
654
655void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000656 EmitCXXDestructor(D, Dtor_Complete);
657 EmitCXXDestructor(D, Dtor_Base);
658}
659
Mike Stump1eb44332009-09-09 15:08:12 +0000660void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000661 CXXDtorType Type) {
662 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000664 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Anders Carlsson27ae5362009-04-17 01:58:57 +0000666 SetFunctionDefinitionAttributes(D, Fn);
667 SetLLVMFunctionAttributesForDefinition(D, Fn);
668}
669
670llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000671CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000672 CXXDtorType Type) {
673 const llvm::FunctionType *FTy =
674 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Anders Carlsson27ae5362009-04-17 01:58:57 +0000676 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000677 return cast<llvm::Function>(
678 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000679}
680
Mike Stump1eb44332009-09-09 15:08:12 +0000681const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000682 CXXDtorType Type) {
683 llvm::SmallString<256> Name;
684 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000685 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Anders Carlsson27ae5362009-04-17 01:58:57 +0000687 Name += '\0';
688 return UniqueMangledName(Name.begin(), Name.end());
689}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000690
Mike Stumped032eb2009-09-04 18:27:16 +0000691llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
692 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000693 bool Extern, int64_t nv,
694 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +0000695 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +0000696
697 FunctionArgList Args;
698 ImplicitParamDecl *ThisDecl =
699 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
700 MD->getThisType(getContext()));
701 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
702 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
703 e = MD->param_end();
704 i != e; ++i) {
705 ParmVarDecl *D = *i;
706 Args.push_back(std::make_pair(D, D->getType()));
707 }
708 IdentifierInfo *II
709 = &CGM.getContext().Idents.get("__thunk_named_foo_");
710 FunctionDecl *FD = FunctionDecl::Create(getContext(),
711 getContext().getTranslationUnitDecl(),
712 SourceLocation(), II, R, 0,
713 Extern
714 ? FunctionDecl::Extern
715 : FunctionDecl::Static,
716 false, true);
717 StartFunction(FD, R, Fn, Args, SourceLocation());
718 // FIXME: generate body
719 FinishFunction();
720 return Fn;
721}
722
Mike Stump6e319f62009-09-11 23:25:56 +0000723llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
724 const CXXMethodDecl *MD,
725 bool Extern,
726 int64_t nv_t,
727 int64_t v_t,
728 int64_t nv_r,
729 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +0000730 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000731
732 FunctionArgList Args;
733 ImplicitParamDecl *ThisDecl =
734 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
735 MD->getThisType(getContext()));
736 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
737 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
738 e = MD->param_end();
739 i != e; ++i) {
740 ParmVarDecl *D = *i;
741 Args.push_back(std::make_pair(D, D->getType()));
742 }
743 IdentifierInfo *II
744 = &CGM.getContext().Idents.get("__thunk_named_foo_");
745 FunctionDecl *FD = FunctionDecl::Create(getContext(),
746 getContext().getTranslationUnitDecl(),
747 SourceLocation(), II, R, 0,
748 Extern
749 ? FunctionDecl::Extern
750 : FunctionDecl::Static,
751 false, true);
752 StartFunction(FD, R, Fn, Args, SourceLocation());
753 // FIXME: generate body
754 FinishFunction();
755 return Fn;
756}
757
Mike Stump77ca8f62009-09-05 07:20:32 +0000758llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
759 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000760 llvm::SmallString<256> OutName;
761 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000762 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000763 llvm::GlobalVariable::LinkageTypes linktype;
764 linktype = llvm::GlobalValue::WeakAnyLinkage;
765 if (!Extern)
766 linktype = llvm::GlobalValue::InternalLinkage;
767 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000768 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000769 const llvm::FunctionType *FTy =
770 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
771 FPT->isVariadic());
772
773 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
774 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000775 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000776 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
777 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
778 return m;
779}
780
Mike Stump6e319f62009-09-11 23:25:56 +0000781llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
782 bool Extern, int64_t nv_t,
783 int64_t v_t, int64_t nv_r,
784 int64_t v_r) {
785 llvm::SmallString<256> OutName;
786 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000787 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000788 llvm::GlobalVariable::LinkageTypes linktype;
789 linktype = llvm::GlobalValue::WeakAnyLinkage;
790 if (!Extern)
791 linktype = llvm::GlobalValue::InternalLinkage;
792 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000793 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000794 const llvm::FunctionType *FTy =
795 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
796 FPT->isVariadic());
797
798 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
799 &getModule());
800 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
801 v_r);
802 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
803 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
804 return m;
805}
806
Mike Stumpf0070db2009-08-26 20:46:33 +0000807llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000808CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
809 const CXXRecordDecl *ClassDecl,
810 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000811 const llvm::Type *Int8PtrTy =
812 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
813
814 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
815 Int8PtrTy->getPointerTo());
816 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
817
Anders Carlssondbd920c2009-10-11 22:13:54 +0000818 int64_t VBaseOffsetIndex =
819 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
820
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000821 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000822 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000823 const llvm::Type *PtrDiffTy =
824 ConvertType(getContext().getPointerDiffType());
825
826 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
827 PtrDiffTy->getPointerTo());
828
829 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
830
831 return VBaseOffset;
832}
833
834llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000835CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
836 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000837 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000838
Mike Stumpf0070db2009-08-26 20:46:33 +0000839 Ty = llvm::PointerType::get(Ty, 0);
840 Ty = llvm::PointerType::get(Ty, 0);
841 Ty = llvm::PointerType::get(Ty, 0);
842 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
843 vtbl = Builder.CreateLoad(vtbl);
844 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000845 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000846 vfn = Builder.CreateLoad(vfn);
847 return vfn;
848}
849
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000850/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
851/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
852/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000853// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000854void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000855 llvm::Value *Src,
856 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000857 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000858 QualType Ty) {
859 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
860 assert(CA && "VLA cannot be copied over");
861 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000863 // Create a temporary for the loop index and initialize it with 0.
864 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
865 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000866 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000867 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000868 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000869 // Start the loop with a block that tests the condition.
870 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
871 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000873 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000875 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
876 // Generate: if (loop-index < number-of-elements fall to the loop body,
877 // otherwise, go to the block after the for-loop.
878 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000879 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000880 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
881 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000882 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000883 "isless");
884 // If the condition is true, execute the body.
885 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000887 EmitBlock(ForBody);
888 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
889 // Inside the loop body, emit the constructor call on the array element.
890 Counter = Builder.CreateLoad(IndexPtr);
891 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
892 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
893 if (BitwiseCopy)
894 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000895 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000896 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000897 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000898 Ctor_Complete);
899 CallArgList CallArgs;
900 // Push the this (Dest) ptr.
901 CallArgs.push_back(std::make_pair(RValue::get(Dest),
902 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000904 // Push the Src ptr.
905 CallArgs.push_back(std::make_pair(RValue::get(Src),
906 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000907 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000908 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000909 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
910 Callee, CallArgs, BaseCopyCtor);
911 }
912 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000914 // Emit the increment of the loop counter.
915 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
916 Counter = Builder.CreateLoad(IndexPtr);
917 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
918 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000920 // Finally, branch back up to the condition for the next iteration.
921 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000923 // Emit the fall-through block.
924 EmitBlock(AfterFor, true);
925}
926
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000927/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000928/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000929/// bitwise assignment or via a copy assignment operator function call.
930/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000931void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000932 llvm::Value *Src,
933 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000934 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000935 QualType Ty) {
936 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
937 assert(CA && "VLA cannot be asssigned");
938 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000940 // Create a temporary for the loop index and initialize it with 0.
941 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
942 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000943 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000944 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
945 Builder.CreateStore(zeroConstant, IndexPtr, false);
946 // Start the loop with a block that tests the condition.
947 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
948 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000950 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000952 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
953 // Generate: if (loop-index < number-of-elements fall to the loop body,
954 // otherwise, go to the block after the for-loop.
955 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000956 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000957 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
958 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000959 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000960 "isless");
961 // If the condition is true, execute the body.
962 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000964 EmitBlock(ForBody);
965 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
966 // Inside the loop body, emit the assignment operator call on array element.
967 Counter = Builder.CreateLoad(IndexPtr);
968 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
969 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
970 const CXXMethodDecl *MD = 0;
971 if (BitwiseAssign)
972 EmitAggregateCopy(Dest, Src, Ty);
973 else {
974 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
975 MD);
976 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
977 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +0000978 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000979 const llvm::Type *LTy =
980 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
981 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000982 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000984 CallArgList CallArgs;
985 // Push the this (Dest) ptr.
986 CallArgs.push_back(std::make_pair(RValue::get(Dest),
987 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000989 // Push the Src ptr.
990 CallArgs.push_back(std::make_pair(RValue::get(Src),
991 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +0000992 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000993 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
994 Callee, CallArgs, MD);
995 }
996 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000998 // Emit the increment of the loop counter.
999 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1000 Counter = Builder.CreateLoad(IndexPtr);
1001 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1002 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001004 // Finally, branch back up to the condition for the next iteration.
1005 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001007 // Emit the fall-through block.
1008 EmitBlock(AfterFor, true);
1009}
1010
Fariborz Jahanianca283612009-08-07 23:51:33 +00001011/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1012/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001013/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001014void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001015 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001016 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001017 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1018 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001019 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1020 /*NullCheckValue=*/false);
1021 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1022 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001023 }
1024 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1025 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001026 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028
1029 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001030 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001031 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001032 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001033 CallArgList CallArgs;
1034 // Push the this (Dest) ptr.
1035 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1036 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Fariborz Jahanianca283612009-08-07 23:51:33 +00001038 // Push the Src ptr.
1039 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001040 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001041 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001042 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001043 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1044 Callee, CallArgs, BaseCopyCtor);
1045 }
1046}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001047
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001048/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001049/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001050/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001051// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001052void CodeGenFunction::EmitClassCopyAssignment(
1053 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001054 const CXXRecordDecl *ClassDecl,
1055 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001056 QualType Ty) {
1057 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001058 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1059 /*NullCheckValue=*/false);
1060 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1061 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001062 }
1063 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1064 EmitAggregateCopy(Dest, Src, Ty);
1065 return;
1066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001068 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001069 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001070 MD);
1071 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1072 (void)ConstCopyAssignOp;
1073
John McCall183700f2009-09-21 23:43:11 +00001074 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001075 const llvm::Type *LTy =
1076 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001077 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001078 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001080 CallArgList CallArgs;
1081 // Push the this (Dest) ptr.
1082 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1083 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001085 // Push the Src ptr.
1086 CallArgs.push_back(std::make_pair(RValue::get(Src),
1087 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001088 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001089 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001090 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1091 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001092}
1093
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001094/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001095void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001096CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1097 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001098 llvm::Function *Fn,
1099 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001100 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1101 SourceLocation());
1102 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001103 FinishFunction();
1104}
1105
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001106/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001107/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001108/// The implicitly-defined copy constructor for class X performs a memberwise
1109/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001110/// of initialization of bases and members in a user-defined constructor
1111/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001112/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001113/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001114/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001115/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001116/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001117/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001118/// Virtual base class subobjects shall be copied only once by the
1119/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001120
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001121void
1122CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1123 CXXCtorType Type,
1124 llvm::Function *Fn,
1125 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001126 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001127 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001128 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001129 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1130 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001132 FunctionArgList::const_iterator i = Args.begin();
1133 const VarDecl *ThisArg = i->first;
1134 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1135 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1136 const VarDecl *SrcArg = (i+1)->first;
1137 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1138 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001140 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1141 Base != ClassDecl->bases_end(); ++Base) {
1142 // FIXME. copy constrution of virtual base NYI
1143 if (Base->isVirtual())
1144 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001146 CXXRecordDecl *BaseClassDecl
1147 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001148 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1149 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001152 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1153 FieldEnd = ClassDecl->field_end();
1154 Field != FieldEnd; ++Field) {
1155 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001156 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001157 getContext().getAsConstantArrayType(FieldType);
1158 if (Array)
1159 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001161 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1162 CXXRecordDecl *FieldClassDecl
1163 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1164 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1165 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001166 if (Array) {
1167 const llvm::Type *BasePtr = ConvertType(FieldType);
1168 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001169 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001170 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001171 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001172 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1173 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1174 FieldClassDecl, FieldType);
1175 }
Mike Stump1eb44332009-09-09 15:08:12 +00001176 else
1177 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001178 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001179 continue;
1180 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001181 // Do a built-in assignment of scalar data members.
1182 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1183 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1184 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1185 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001186 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001187 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001188}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001189
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001190/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001191/// Before the implicitly-declared copy assignment operator for a class is
1192/// implicitly defined, all implicitly- declared copy assignment operators for
1193/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001194/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001195/// The implicitly-defined copy assignment operator for class X performs
1196/// memberwise assignment of its subob- jects. The direct base classes of X are
1197/// assigned first, in the order of their declaration in
1198/// the base-specifier-list, and then the immediate nonstatic data members of X
1199/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001200/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001201/// if the subobject is of class type, the copy assignment operator for the
1202/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001203/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001204///
Mike Stump1eb44332009-09-09 15:08:12 +00001205/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001206/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001207///
Mike Stump1eb44332009-09-09 15:08:12 +00001208/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001209/// used.
1210void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001211 llvm::Function *Fn,
1212 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001213
1214 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1215 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1216 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001217 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001219 FunctionArgList::const_iterator i = Args.begin();
1220 const VarDecl *ThisArg = i->first;
1221 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1222 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1223 const VarDecl *SrcArg = (i+1)->first;
1224 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1225 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001227 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1228 Base != ClassDecl->bases_end(); ++Base) {
1229 // FIXME. copy assignment of virtual base NYI
1230 if (Base->isVirtual())
1231 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001233 CXXRecordDecl *BaseClassDecl
1234 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1235 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1236 Base->getType());
1237 }
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001239 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1240 FieldEnd = ClassDecl->field_end();
1241 Field != FieldEnd; ++Field) {
1242 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001243 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001244 getContext().getAsConstantArrayType(FieldType);
1245 if (Array)
1246 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001248 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1249 CXXRecordDecl *FieldClassDecl
1250 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1251 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1252 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001253 if (Array) {
1254 const llvm::Type *BasePtr = ConvertType(FieldType);
1255 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1256 llvm::Value *DestBaseAddrPtr =
1257 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1258 llvm::Value *SrcBaseAddrPtr =
1259 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1260 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1261 FieldClassDecl, FieldType);
1262 }
1263 else
Mike Stump1eb44332009-09-09 15:08:12 +00001264 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001265 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001266 continue;
1267 }
1268 // Do a built-in assignment of scalar data members.
1269 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1270 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1271 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1272 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001273 }
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001275 // return *this;
1276 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001278 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001279}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001280
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001281/// EmitCtorPrologue - This routine generates necessary code to initialize
1282/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001283/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001284void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1285 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001286 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001287 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001288 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001290 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001291 E = CD->init_end();
1292 B != E; ++B) {
1293 CXXBaseOrMemberInitializer *Member = (*B);
1294 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001295 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001296 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001297 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001298 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001299 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1300 BaseClassDecl,
1301 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001302 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001303 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001304 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001305 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001306 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001307 // non-static data member initilaizers.
1308 FieldDecl *Field = Member->getMember();
1309 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001310 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001311 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001312 if (Array)
1313 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Mike Stumpf1216772009-07-31 18:25:34 +00001315 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001316 LValue LHS;
1317 if (FieldType->isReferenceType()) {
1318 // FIXME: This is really ugly; should be refactored somehow
1319 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1320 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001321 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1322 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001323 } else {
1324 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1325 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001326 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001327 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001328 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001329 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001330 if (Array) {
1331 const llvm::Type *BasePtr = ConvertType(FieldType);
1332 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001333 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001334 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001335 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001336 Array, BaseAddrPtr);
1337 }
1338 else
1339 EmitCXXConstructorCall(Member->getConstructor(),
1340 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001341 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001342 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001343 continue;
1344 }
1345 else {
1346 // Initializing an anonymous union data member.
1347 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001348 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001349 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001350 FieldType = anonMember->getType();
1351 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001352 }
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001354 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001355 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001356 RValue RHS;
1357 if (FieldType->isReferenceType())
1358 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1359 /*IsInitializer=*/true);
1360 else
1361 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1362 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001363 }
1364 }
Mike Stumpf1216772009-07-31 18:25:34 +00001365
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001366 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001367 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001368 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001369 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001370 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001371 ClassDecl->bases_begin();
1372 Base != ClassDecl->bases_end(); ++Base) {
1373 // FIXME. copy assignment of virtual base NYI
1374 if (Base->isVirtual())
1375 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001377 CXXRecordDecl *BaseClassDecl
1378 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1379 if (BaseClassDecl->hasTrivialConstructor())
1380 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001381 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001382 BaseClassDecl->getDefaultConstructor(getContext())) {
1383 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001384 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1385 BaseClassDecl,
1386 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001387 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1388 }
1389 }
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001391 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1392 FieldEnd = ClassDecl->field_end();
1393 Field != FieldEnd; ++Field) {
1394 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001395 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001396 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001397 if (Array)
1398 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001399 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1400 continue;
1401 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001402 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001403 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1404 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1405 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001406 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001407 MemberClassDecl->getDefaultConstructor(getContext())) {
1408 LoadOfThis = LoadCXXThis();
1409 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001410 if (Array) {
1411 const llvm::Type *BasePtr = ConvertType(FieldType);
1412 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001413 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001414 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1415 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1416 }
1417 else
Mike Stump1eb44332009-09-09 15:08:12 +00001418 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001419 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001420 }
1421 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001422 }
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Mike Stumpf1216772009-07-31 18:25:34 +00001424 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001425 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001426 if (!LoadOfThis)
1427 LoadOfThis = LoadCXXThis();
1428 llvm::Value *VtableField;
1429 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001430 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001431 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1432 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1433 llvm::Value *vtable = GenerateVtable(ClassDecl);
1434 Builder.CreateStore(vtable, VtableField);
1435 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001436}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001437
1438/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001439/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001440/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001441/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001442void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1443 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001444 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001445 assert(!ClassDecl->getNumVBases() &&
1446 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001447 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001449 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1450 *E = DD->destr_end(); B != E; ++B) {
1451 uintptr_t BaseOrMember = (*B);
1452 if (DD->isMemberToDestroy(BaseOrMember)) {
1453 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1454 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001455 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001456 getContext().getAsConstantArrayType(FieldType);
1457 if (Array)
1458 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001459 const RecordType *RT = FieldType->getAs<RecordType>();
1460 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1461 if (FieldClassDecl->hasTrivialDestructor())
1462 continue;
1463 llvm::Value *LoadOfThis = LoadCXXThis();
1464 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001465 if (Array) {
1466 const llvm::Type *BasePtr = ConvertType(FieldType);
1467 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001468 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001469 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001470 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001471 Array, BaseAddrPtr);
1472 }
1473 else
1474 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1475 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001476 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001477 const RecordType *RT =
1478 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1479 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1480 if (BaseClassDecl->hasTrivialDestructor())
1481 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001482 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1483 ClassDecl, BaseClassDecl,
1484 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001485 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001486 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001487 }
1488 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001489 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1490 return;
1491 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001492 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001493 // reverse order of their construction.
1494 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001496 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1497 FieldEnd = ClassDecl->field_end();
1498 Field != FieldEnd; ++Field) {
1499 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001500 if (getContext().getAsConstantArrayType(FieldType))
1501 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001502 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1503 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1504 if (FieldClassDecl->hasTrivialDestructor())
1505 continue;
1506 DestructedFields.push_back(*Field);
1507 }
1508 }
1509 if (!DestructedFields.empty())
1510 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1511 FieldDecl *Field = DestructedFields[i];
1512 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001513 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001514 getContext().getAsConstantArrayType(FieldType);
1515 if (Array)
1516 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001517 const RecordType *RT = FieldType->getAs<RecordType>();
1518 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1519 llvm::Value *LoadOfThis = LoadCXXThis();
1520 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001521 if (Array) {
1522 const llvm::Type *BasePtr = ConvertType(FieldType);
1523 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001524 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001525 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001526 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001527 Array, BaseAddrPtr);
1528 }
1529 else
1530 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1531 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001532 }
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001534 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1535 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1536 Base != ClassDecl->bases_end(); ++Base) {
1537 // FIXME. copy assignment of virtual base NYI
1538 if (Base->isVirtual())
1539 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001541 CXXRecordDecl *BaseClassDecl
1542 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1543 if (BaseClassDecl->hasTrivialDestructor())
1544 continue;
1545 DestructedBases.push_back(BaseClassDecl);
1546 }
1547 if (DestructedBases.empty())
1548 return;
1549 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1550 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001551 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1552 ClassDecl,BaseClassDecl,
1553 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001554 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1555 Dtor_Complete, V);
1556 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001557}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001558
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001559void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1560 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001561 llvm::Function *Fn,
1562 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001564 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001565 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1566 "SynthesizeDefaultDestructor - destructor has user declaration");
1567 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001569 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1570 SourceLocation());
1571 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001572 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001573}
Anders Carlsson6815e942009-09-27 18:58:34 +00001574
1575// FIXME: Move this to CGCXXStmt.cpp
1576void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1577 // FIXME: We need to do more here.
1578 EmitStmt(S.getTryBlock());
1579}