blob: ff56c8c85a2780eb8b4a9037d769d1658e8e1e3f [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());
281 const DeclRefExpr *BaseExpr = cast<DeclRefExpr>(BO->getLHS());
282 const DeclRefExpr *MemFn = cast<DeclRefExpr>(BO->getRHS());
283
284 const MemberPointerType *MPT = MemFn->getType()->getAs<MemberPointerType>();
285 const FunctionProtoType *FPT =
286 MPT->getPointeeType()->getAs<FunctionProtoType>();
287 const CXXRecordDecl *RD =
288 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl());
289
290 const llvm::FunctionType *FTy =
291 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
292 FPT->isVariadic());
293
294 const llvm::Type *Int8PtrTy =
295 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
296
297 // Get the member function pointer.
298 llvm::Value *MemFnPtr =
299 CreateTempAlloca(ConvertType(MemFn->getType()), "mem.fn");
300 EmitAggExpr(MemFn, MemFnPtr, /*VolatileDest=*/false);
301
302 // Emit the 'this' pointer.
303 llvm::Value *This;
304
305 if (BO->getOpcode() == BinaryOperator::PtrMemI)
306 This = EmitScalarExpr(BaseExpr);
307 else
308 This = EmitLValue(BaseExpr).getAddress();
309
310 // Adjust it.
311 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
312 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
313
314 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
315 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
316
317 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
318
319 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
320
321 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
322
323 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
324
325 // If the LSB in the function pointer is 1, the function pointer points to
326 // a virtual function.
327 llvm::Value *IsVirtual
328 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
329 "and");
330
331 IsVirtual = Builder.CreateTrunc(IsVirtual,
332 llvm::Type::getInt1Ty(VMContext));
333
334 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
335 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
336 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
337
338 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
339 EmitBlock(FnVirtual);
340
341 const llvm::Type *VTableTy =
342 FTy->getPointerTo()->getPointerTo()->getPointerTo();
343
344 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
345 VTable = Builder.CreateLoad(VTable);
346
347 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
348
349 // Since the function pointer is 1 plus the virtual table offset, we
350 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000351 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000352
353 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
354
355 EmitBranch(FnEnd);
356 EmitBlock(FnNonVirtual);
357
358 // If the function is not virtual, just load the pointer.
359 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
360 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
361
362 EmitBlock(FnEnd);
363
364 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
365 Callee->reserveOperandSpace(2);
366 Callee->addIncoming(VirtualFn, FnVirtual);
367 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
368
369 CallArgList Args;
370
371 QualType ThisType =
372 getContext().getPointerType(getContext().getTagDeclType(RD));
373
374 // Push the this ptr.
375 Args.push_back(std::make_pair(RValue::get(This), ThisType));
376
377 // And the rest of the call args
378 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
379 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
380 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
381 Callee, Args, 0);
382}
383
384RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000385CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
386 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000387 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000388 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Fariborz Jahanianad258832009-08-13 21:09:41 +0000390 if (MD->isCopyAssignment()) {
391 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
392 if (ClassDecl->hasTrivialCopyAssignment()) {
393 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
394 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
395 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
396 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
397 QualType Ty = E->getType();
398 EmitAggregateCopy(This, Src, Ty);
399 return RValue::get(This);
400 }
401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
John McCall183700f2009-09-21 23:43:11 +0000403 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000404 const llvm::Type *Ty =
405 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000406 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000407 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Anders Carlsson0f294632009-05-27 04:18:27 +0000409 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Anders Carlsson0f294632009-05-27 04:18:27 +0000411 return EmitCXXMemberCall(MD, Callee, This,
412 E->arg_begin() + 1, E->arg_end());
413}
414
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000415llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000416 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000417 "Must be in a C++ member function decl to load 'this'");
418 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
419 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000421 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000422 // ans: See how CodeGenFunction::LoadObjCSelf() uses
423 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000424 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
425}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000426
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000427/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
428/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000429/// array.
430/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
431/// array type and 'ArrayPtr' points to the beginning fo the array.
432/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000433void
434CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000435 const ConstantArrayType *ArrayTy,
436 llvm::Value *ArrayPtr) {
437 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
438 llvm::Value * NumElements =
439 llvm::ConstantInt::get(SizeTy,
440 getContext().getConstantArrayElementCount(ArrayTy));
441
442 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
443}
444
445void
446CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
447 llvm::Value *NumElements,
448 llvm::Value *ArrayPtr) {
449 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000451 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000452 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
453 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
454 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000456 // Start the loop with a block that tests the condition.
457 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
458 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000460 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000462 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000464 // Generate: if (loop-index < number-of-elements fall to the loop body,
465 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000466 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000467 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000468 // If the condition is true, execute the body.
469 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000471 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000473 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000474 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000475 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000476 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
477 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000478 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000480 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000482 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000483 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000484 Counter = Builder.CreateLoad(IndexPtr);
485 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
486 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000488 // Finally, branch back up to the condition for the next iteration.
489 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000491 // Emit the fall-through block.
492 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000493}
494
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000495/// EmitCXXAggrDestructorCall - calls the default destructor on array
496/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000497void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000498CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
499 const ArrayType *Array,
500 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000501 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
502 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000503 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000504 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000505 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000506 // Create a temporary for the loop index and initialize it with count of
507 // array elements.
508 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
509 "loop.index");
510 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000511 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000512 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
513 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000515 // Start the loop with a block that tests the condition.
516 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
517 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000519 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000521 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000523 // Generate: if (loop-index != 0 fall to the loop body,
524 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000525 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000526 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
527 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
528 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
529 "isne");
530 // If the condition is true, execute the body.
531 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000533 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000535 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
536 // Inside the loop body, emit the constructor call on the array element.
537 Counter = Builder.CreateLoad(IndexPtr);
538 Counter = Builder.CreateSub(Counter, One);
539 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
540 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000542 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000544 // Emit the decrement of the loop counter.
545 Counter = Builder.CreateLoad(IndexPtr);
546 Counter = Builder.CreateSub(Counter, One, "dec");
547 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000549 // Finally, branch back up to the condition for the next iteration.
550 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000552 // Emit the fall-through block.
553 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000554}
555
556void
Mike Stump1eb44332009-09-09 15:08:12 +0000557CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
558 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000559 llvm::Value *This,
560 CallExpr::const_arg_iterator ArgBeg,
561 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000562 if (D->isCopyConstructor(getContext())) {
563 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
564 if (ClassDecl->hasTrivialCopyConstructor()) {
565 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
566 "EmitCXXConstructorCall - user declared copy constructor");
567 const Expr *E = (*ArgBeg);
568 QualType Ty = E->getType();
569 llvm::Value *Src = EmitLValue(E).getAddress();
570 EmitAggregateCopy(This, Src, Ty);
571 return;
572 }
573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000575 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
576
577 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000578}
579
Mike Stump1eb44332009-09-09 15:08:12 +0000580void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000581 CXXDtorType Type,
582 llvm::Value *This) {
583 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlsson7267c162009-05-29 21:03:38 +0000585 EmitCXXMemberCall(D, Callee, This, 0, 0);
586}
587
Mike Stump1eb44332009-09-09 15:08:12 +0000588void
589CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000590 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000591 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000592
593 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000594 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000595 if (RD->hasTrivialConstructor())
596 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000597
Mike Stump1eb44332009-09-09 15:08:12 +0000598 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000599 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000600 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000601 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000602 EmitAggExpr((*i), Dest, false);
603 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000604 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000605 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000606 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000607 E->arg_begin(), E->arg_end());
608}
609
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000610void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000611 EmitGlobal(GlobalDecl(D, Ctor_Complete));
612 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000613}
Anders Carlsson363c1842009-04-16 23:57:24 +0000614
Mike Stump1eb44332009-09-09 15:08:12 +0000615void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000616 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Anders Carlsson27ae5362009-04-17 01:58:57 +0000618 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000620 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Anders Carlsson27ae5362009-04-17 01:58:57 +0000622 SetFunctionDefinitionAttributes(D, Fn);
623 SetLLVMFunctionAttributesForDefinition(D, Fn);
624}
625
Anders Carlsson363c1842009-04-16 23:57:24 +0000626llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000627CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000628 CXXCtorType Type) {
629 const llvm::FunctionType *FTy =
630 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Anders Carlsson363c1842009-04-16 23:57:24 +0000632 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000633 return cast<llvm::Function>(
634 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000635}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000636
Mike Stump1eb44332009-09-09 15:08:12 +0000637const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000638 CXXCtorType Type) {
639 llvm::SmallString<256> Name;
640 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000641 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Anders Carlsson27ae5362009-04-17 01:58:57 +0000643 Name += '\0';
644 return UniqueMangledName(Name.begin(), Name.end());
645}
646
647void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000648 EmitCXXDestructor(D, Dtor_Complete);
649 EmitCXXDestructor(D, Dtor_Base);
650}
651
Mike Stump1eb44332009-09-09 15:08:12 +0000652void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000653 CXXDtorType Type) {
654 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000656 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Anders Carlsson27ae5362009-04-17 01:58:57 +0000658 SetFunctionDefinitionAttributes(D, Fn);
659 SetLLVMFunctionAttributesForDefinition(D, Fn);
660}
661
662llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000663CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000664 CXXDtorType Type) {
665 const llvm::FunctionType *FTy =
666 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Anders Carlsson27ae5362009-04-17 01:58:57 +0000668 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000669 return cast<llvm::Function>(
670 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000671}
672
Mike Stump1eb44332009-09-09 15:08:12 +0000673const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000674 CXXDtorType Type) {
675 llvm::SmallString<256> Name;
676 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000677 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Anders Carlsson27ae5362009-04-17 01:58:57 +0000679 Name += '\0';
680 return UniqueMangledName(Name.begin(), Name.end());
681}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000682
Mike Stumped032eb2009-09-04 18:27:16 +0000683llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
684 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000685 bool Extern, int64_t nv,
686 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +0000687 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +0000688
689 FunctionArgList Args;
690 ImplicitParamDecl *ThisDecl =
691 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
692 MD->getThisType(getContext()));
693 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
694 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
695 e = MD->param_end();
696 i != e; ++i) {
697 ParmVarDecl *D = *i;
698 Args.push_back(std::make_pair(D, D->getType()));
699 }
700 IdentifierInfo *II
701 = &CGM.getContext().Idents.get("__thunk_named_foo_");
702 FunctionDecl *FD = FunctionDecl::Create(getContext(),
703 getContext().getTranslationUnitDecl(),
704 SourceLocation(), II, R, 0,
705 Extern
706 ? FunctionDecl::Extern
707 : FunctionDecl::Static,
708 false, true);
709 StartFunction(FD, R, Fn, Args, SourceLocation());
710 // FIXME: generate body
711 FinishFunction();
712 return Fn;
713}
714
Mike Stump6e319f62009-09-11 23:25:56 +0000715llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
716 const CXXMethodDecl *MD,
717 bool Extern,
718 int64_t nv_t,
719 int64_t v_t,
720 int64_t nv_r,
721 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +0000722 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000723
724 FunctionArgList Args;
725 ImplicitParamDecl *ThisDecl =
726 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
727 MD->getThisType(getContext()));
728 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
729 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
730 e = MD->param_end();
731 i != e; ++i) {
732 ParmVarDecl *D = *i;
733 Args.push_back(std::make_pair(D, D->getType()));
734 }
735 IdentifierInfo *II
736 = &CGM.getContext().Idents.get("__thunk_named_foo_");
737 FunctionDecl *FD = FunctionDecl::Create(getContext(),
738 getContext().getTranslationUnitDecl(),
739 SourceLocation(), II, R, 0,
740 Extern
741 ? FunctionDecl::Extern
742 : FunctionDecl::Static,
743 false, true);
744 StartFunction(FD, R, Fn, Args, SourceLocation());
745 // FIXME: generate body
746 FinishFunction();
747 return Fn;
748}
749
Mike Stump77ca8f62009-09-05 07:20:32 +0000750llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
751 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000752 llvm::SmallString<256> OutName;
753 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000754 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000755 llvm::GlobalVariable::LinkageTypes linktype;
756 linktype = llvm::GlobalValue::WeakAnyLinkage;
757 if (!Extern)
758 linktype = llvm::GlobalValue::InternalLinkage;
759 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000760 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000761 const llvm::FunctionType *FTy =
762 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
763 FPT->isVariadic());
764
765 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
766 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000767 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000768 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
769 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
770 return m;
771}
772
Mike Stump6e319f62009-09-11 23:25:56 +0000773llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
774 bool Extern, int64_t nv_t,
775 int64_t v_t, int64_t nv_r,
776 int64_t v_r) {
777 llvm::SmallString<256> OutName;
778 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000779 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000780 llvm::GlobalVariable::LinkageTypes linktype;
781 linktype = llvm::GlobalValue::WeakAnyLinkage;
782 if (!Extern)
783 linktype = llvm::GlobalValue::InternalLinkage;
784 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000785 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000786 const llvm::FunctionType *FTy =
787 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
788 FPT->isVariadic());
789
790 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
791 &getModule());
792 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
793 v_r);
794 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
795 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
796 return m;
797}
798
Mike Stumpf0070db2009-08-26 20:46:33 +0000799llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000800CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
801 const CXXRecordDecl *ClassDecl,
802 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000803 const llvm::Type *Int8PtrTy =
804 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
805
806 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
807 Int8PtrTy->getPointerTo());
808 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
809
Anders Carlssondbd920c2009-10-11 22:13:54 +0000810 int64_t VBaseOffsetIndex =
811 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
812
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000813 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000814 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000815 const llvm::Type *PtrDiffTy =
816 ConvertType(getContext().getPointerDiffType());
817
818 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
819 PtrDiffTy->getPointerTo());
820
821 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
822
823 return VBaseOffset;
824}
825
826llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000827CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
828 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000829 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000830
Mike Stumpf0070db2009-08-26 20:46:33 +0000831 Ty = llvm::PointerType::get(Ty, 0);
832 Ty = llvm::PointerType::get(Ty, 0);
833 Ty = llvm::PointerType::get(Ty, 0);
834 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
835 vtbl = Builder.CreateLoad(vtbl);
836 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000837 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000838 vfn = Builder.CreateLoad(vfn);
839 return vfn;
840}
841
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000842/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
843/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
844/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000845// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000846void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000847 llvm::Value *Src,
848 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000849 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000850 QualType Ty) {
851 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
852 assert(CA && "VLA cannot be copied over");
853 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000855 // Create a temporary for the loop index and initialize it with 0.
856 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
857 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000858 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000859 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000860 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000861 // Start the loop with a block that tests the condition.
862 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
863 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000865 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000867 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
868 // Generate: if (loop-index < number-of-elements fall to the loop body,
869 // otherwise, go to the block after the for-loop.
870 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000871 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000872 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
873 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000874 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000875 "isless");
876 // If the condition is true, execute the body.
877 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000879 EmitBlock(ForBody);
880 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
881 // Inside the loop body, emit the constructor call on the array element.
882 Counter = Builder.CreateLoad(IndexPtr);
883 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
884 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
885 if (BitwiseCopy)
886 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000887 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000888 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000889 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000890 Ctor_Complete);
891 CallArgList CallArgs;
892 // Push the this (Dest) ptr.
893 CallArgs.push_back(std::make_pair(RValue::get(Dest),
894 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000896 // Push the Src ptr.
897 CallArgs.push_back(std::make_pair(RValue::get(Src),
898 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000899 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000900 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000901 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
902 Callee, CallArgs, BaseCopyCtor);
903 }
904 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000906 // Emit the increment of the loop counter.
907 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
908 Counter = Builder.CreateLoad(IndexPtr);
909 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
910 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000912 // Finally, branch back up to the condition for the next iteration.
913 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000915 // Emit the fall-through block.
916 EmitBlock(AfterFor, true);
917}
918
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000919/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000920/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000921/// bitwise assignment or via a copy assignment operator function call.
922/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000923void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000924 llvm::Value *Src,
925 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000926 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000927 QualType Ty) {
928 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
929 assert(CA && "VLA cannot be asssigned");
930 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000932 // Create a temporary for the loop index and initialize it with 0.
933 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
934 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000935 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000936 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
937 Builder.CreateStore(zeroConstant, IndexPtr, false);
938 // Start the loop with a block that tests the condition.
939 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
940 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000942 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000944 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
945 // Generate: if (loop-index < number-of-elements fall to the loop body,
946 // otherwise, go to the block after the for-loop.
947 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000948 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000949 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
950 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000951 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000952 "isless");
953 // If the condition is true, execute the body.
954 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000956 EmitBlock(ForBody);
957 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
958 // Inside the loop body, emit the assignment operator call on array element.
959 Counter = Builder.CreateLoad(IndexPtr);
960 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
961 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
962 const CXXMethodDecl *MD = 0;
963 if (BitwiseAssign)
964 EmitAggregateCopy(Dest, Src, Ty);
965 else {
966 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
967 MD);
968 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
969 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +0000970 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000971 const llvm::Type *LTy =
972 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
973 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000974 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000976 CallArgList CallArgs;
977 // Push the this (Dest) ptr.
978 CallArgs.push_back(std::make_pair(RValue::get(Dest),
979 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000981 // Push the Src ptr.
982 CallArgs.push_back(std::make_pair(RValue::get(Src),
983 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +0000984 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000985 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
986 Callee, CallArgs, MD);
987 }
988 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000990 // Emit the increment of the loop counter.
991 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
992 Counter = Builder.CreateLoad(IndexPtr);
993 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
994 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000996 // Finally, branch back up to the condition for the next iteration.
997 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000999 // Emit the fall-through block.
1000 EmitBlock(AfterFor, true);
1001}
1002
Fariborz Jahanianca283612009-08-07 23:51:33 +00001003/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1004/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001005/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001006void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001007 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001008 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001009 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1010 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001011 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1012 /*NullCheckValue=*/false);
1013 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1014 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001015 }
1016 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1017 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001018 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001019 }
Mike Stump1eb44332009-09-09 15:08:12 +00001020
1021 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001022 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001023 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001024 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001025 CallArgList CallArgs;
1026 // Push the this (Dest) ptr.
1027 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1028 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Fariborz Jahanianca283612009-08-07 23:51:33 +00001030 // Push the Src ptr.
1031 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001032 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001033 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001034 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001035 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1036 Callee, CallArgs, BaseCopyCtor);
1037 }
1038}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001039
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001040/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001041/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001042/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001043// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001044void CodeGenFunction::EmitClassCopyAssignment(
1045 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001046 const CXXRecordDecl *ClassDecl,
1047 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001048 QualType Ty) {
1049 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001050 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1051 /*NullCheckValue=*/false);
1052 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1053 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001054 }
1055 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1056 EmitAggregateCopy(Dest, Src, Ty);
1057 return;
1058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001060 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001061 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001062 MD);
1063 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1064 (void)ConstCopyAssignOp;
1065
John McCall183700f2009-09-21 23:43:11 +00001066 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001067 const llvm::Type *LTy =
1068 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001069 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001070 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001072 CallArgList CallArgs;
1073 // Push the this (Dest) ptr.
1074 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1075 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001077 // Push the Src ptr.
1078 CallArgs.push_back(std::make_pair(RValue::get(Src),
1079 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001080 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001081 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001082 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1083 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001084}
1085
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001086/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001087void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001088CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1089 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001090 llvm::Function *Fn,
1091 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001092 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1093 SourceLocation());
1094 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001095 FinishFunction();
1096}
1097
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001098/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001099/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001100/// The implicitly-defined copy constructor for class X performs a memberwise
1101/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001102/// of initialization of bases and members in a user-defined constructor
1103/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001104/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001105/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001106/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001107/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001108/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001109/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001110/// Virtual base class subobjects shall be copied only once by the
1111/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001112
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001113void
1114CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1115 CXXCtorType Type,
1116 llvm::Function *Fn,
1117 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001118 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001119 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001120 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001121 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1122 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001124 FunctionArgList::const_iterator i = Args.begin();
1125 const VarDecl *ThisArg = i->first;
1126 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1127 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1128 const VarDecl *SrcArg = (i+1)->first;
1129 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1130 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001132 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1133 Base != ClassDecl->bases_end(); ++Base) {
1134 // FIXME. copy constrution of virtual base NYI
1135 if (Base->isVirtual())
1136 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001138 CXXRecordDecl *BaseClassDecl
1139 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001140 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1141 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001144 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1145 FieldEnd = ClassDecl->field_end();
1146 Field != FieldEnd; ++Field) {
1147 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001148 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001149 getContext().getAsConstantArrayType(FieldType);
1150 if (Array)
1151 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001153 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1154 CXXRecordDecl *FieldClassDecl
1155 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1156 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1157 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001158 if (Array) {
1159 const llvm::Type *BasePtr = ConvertType(FieldType);
1160 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001161 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001162 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001163 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001164 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1165 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1166 FieldClassDecl, FieldType);
1167 }
Mike Stump1eb44332009-09-09 15:08:12 +00001168 else
1169 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001170 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001171 continue;
1172 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001173 // Do a built-in assignment of scalar data members.
1174 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1175 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1176 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1177 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001178 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001179 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001180}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001181
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001182/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001183/// Before the implicitly-declared copy assignment operator for a class is
1184/// implicitly defined, all implicitly- declared copy assignment operators for
1185/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001186/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001187/// The implicitly-defined copy assignment operator for class X performs
1188/// memberwise assignment of its subob- jects. The direct base classes of X are
1189/// assigned first, in the order of their declaration in
1190/// the base-specifier-list, and then the immediate nonstatic data members of X
1191/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001192/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001193/// if the subobject is of class type, the copy assignment operator for the
1194/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001195/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001196///
Mike Stump1eb44332009-09-09 15:08:12 +00001197/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001198/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001199///
Mike Stump1eb44332009-09-09 15:08:12 +00001200/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001201/// used.
1202void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001203 llvm::Function *Fn,
1204 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001205
1206 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1207 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1208 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001209 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001211 FunctionArgList::const_iterator i = Args.begin();
1212 const VarDecl *ThisArg = i->first;
1213 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1214 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1215 const VarDecl *SrcArg = (i+1)->first;
1216 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1217 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001219 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1220 Base != ClassDecl->bases_end(); ++Base) {
1221 // FIXME. copy assignment of virtual base NYI
1222 if (Base->isVirtual())
1223 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001225 CXXRecordDecl *BaseClassDecl
1226 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1227 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1228 Base->getType());
1229 }
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001231 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1232 FieldEnd = ClassDecl->field_end();
1233 Field != FieldEnd; ++Field) {
1234 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001235 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001236 getContext().getAsConstantArrayType(FieldType);
1237 if (Array)
1238 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001240 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1241 CXXRecordDecl *FieldClassDecl
1242 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1243 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1244 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001245 if (Array) {
1246 const llvm::Type *BasePtr = ConvertType(FieldType);
1247 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1248 llvm::Value *DestBaseAddrPtr =
1249 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1250 llvm::Value *SrcBaseAddrPtr =
1251 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1252 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1253 FieldClassDecl, FieldType);
1254 }
1255 else
Mike Stump1eb44332009-09-09 15:08:12 +00001256 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001257 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001258 continue;
1259 }
1260 // Do a built-in assignment of scalar data members.
1261 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1262 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1263 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1264 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001265 }
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001267 // return *this;
1268 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001270 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001271}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001272
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001273/// EmitCtorPrologue - This routine generates necessary code to initialize
1274/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001275/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001276void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1277 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001278 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001279 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001280 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001282 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001283 E = CD->init_end();
1284 B != E; ++B) {
1285 CXXBaseOrMemberInitializer *Member = (*B);
1286 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001287 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001288 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001289 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001290 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001291 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1292 BaseClassDecl,
1293 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001294 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001295 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001296 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001297 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001298 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001299 // non-static data member initilaizers.
1300 FieldDecl *Field = Member->getMember();
1301 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001302 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001303 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001304 if (Array)
1305 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Mike Stumpf1216772009-07-31 18:25:34 +00001307 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001308 LValue LHS;
1309 if (FieldType->isReferenceType()) {
1310 // FIXME: This is really ugly; should be refactored somehow
1311 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1312 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001313 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1314 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001315 } else {
1316 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1317 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001318 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001319 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001320 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001321 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001322 if (Array) {
1323 const llvm::Type *BasePtr = ConvertType(FieldType);
1324 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001325 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001326 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001327 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001328 Array, BaseAddrPtr);
1329 }
1330 else
1331 EmitCXXConstructorCall(Member->getConstructor(),
1332 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001333 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001334 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001335 continue;
1336 }
1337 else {
1338 // Initializing an anonymous union data member.
1339 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001340 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001341 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001342 FieldType = anonMember->getType();
1343 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001344 }
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001346 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001347 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001348 RValue RHS;
1349 if (FieldType->isReferenceType())
1350 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1351 /*IsInitializer=*/true);
1352 else
1353 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1354 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001355 }
1356 }
Mike Stumpf1216772009-07-31 18:25:34 +00001357
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001358 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001359 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001360 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001361 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001362 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001363 ClassDecl->bases_begin();
1364 Base != ClassDecl->bases_end(); ++Base) {
1365 // FIXME. copy assignment of virtual base NYI
1366 if (Base->isVirtual())
1367 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001369 CXXRecordDecl *BaseClassDecl
1370 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1371 if (BaseClassDecl->hasTrivialConstructor())
1372 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001373 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001374 BaseClassDecl->getDefaultConstructor(getContext())) {
1375 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001376 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1377 BaseClassDecl,
1378 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001379 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1380 }
1381 }
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001383 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1384 FieldEnd = ClassDecl->field_end();
1385 Field != FieldEnd; ++Field) {
1386 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001387 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001388 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001389 if (Array)
1390 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001391 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1392 continue;
1393 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001394 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001395 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1396 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1397 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001398 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001399 MemberClassDecl->getDefaultConstructor(getContext())) {
1400 LoadOfThis = LoadCXXThis();
1401 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001402 if (Array) {
1403 const llvm::Type *BasePtr = ConvertType(FieldType);
1404 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001405 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001406 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1407 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1408 }
1409 else
Mike Stump1eb44332009-09-09 15:08:12 +00001410 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001411 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001412 }
1413 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001414 }
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Mike Stumpf1216772009-07-31 18:25:34 +00001416 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001417 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001418 if (!LoadOfThis)
1419 LoadOfThis = LoadCXXThis();
1420 llvm::Value *VtableField;
1421 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001422 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001423 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1424 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1425 llvm::Value *vtable = GenerateVtable(ClassDecl);
1426 Builder.CreateStore(vtable, VtableField);
1427 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001428}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001429
1430/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001431/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001432/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001433/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001434void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1435 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001436 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001437 assert(!ClassDecl->getNumVBases() &&
1438 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001439 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001441 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1442 *E = DD->destr_end(); B != E; ++B) {
1443 uintptr_t BaseOrMember = (*B);
1444 if (DD->isMemberToDestroy(BaseOrMember)) {
1445 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1446 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001447 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001448 getContext().getAsConstantArrayType(FieldType);
1449 if (Array)
1450 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001451 const RecordType *RT = FieldType->getAs<RecordType>();
1452 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1453 if (FieldClassDecl->hasTrivialDestructor())
1454 continue;
1455 llvm::Value *LoadOfThis = LoadCXXThis();
1456 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001457 if (Array) {
1458 const llvm::Type *BasePtr = ConvertType(FieldType);
1459 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001460 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001461 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001462 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001463 Array, BaseAddrPtr);
1464 }
1465 else
1466 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1467 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001468 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001469 const RecordType *RT =
1470 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1471 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1472 if (BaseClassDecl->hasTrivialDestructor())
1473 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001474 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1475 ClassDecl, BaseClassDecl,
1476 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001477 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001478 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001479 }
1480 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001481 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1482 return;
1483 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001484 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001485 // reverse order of their construction.
1486 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001488 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1489 FieldEnd = ClassDecl->field_end();
1490 Field != FieldEnd; ++Field) {
1491 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001492 if (getContext().getAsConstantArrayType(FieldType))
1493 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001494 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1495 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1496 if (FieldClassDecl->hasTrivialDestructor())
1497 continue;
1498 DestructedFields.push_back(*Field);
1499 }
1500 }
1501 if (!DestructedFields.empty())
1502 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1503 FieldDecl *Field = DestructedFields[i];
1504 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001505 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001506 getContext().getAsConstantArrayType(FieldType);
1507 if (Array)
1508 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001509 const RecordType *RT = FieldType->getAs<RecordType>();
1510 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1511 llvm::Value *LoadOfThis = LoadCXXThis();
1512 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001513 if (Array) {
1514 const llvm::Type *BasePtr = ConvertType(FieldType);
1515 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001516 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001517 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001518 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001519 Array, BaseAddrPtr);
1520 }
1521 else
1522 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1523 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001524 }
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001526 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1527 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1528 Base != ClassDecl->bases_end(); ++Base) {
1529 // FIXME. copy assignment of virtual base NYI
1530 if (Base->isVirtual())
1531 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001533 CXXRecordDecl *BaseClassDecl
1534 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1535 if (BaseClassDecl->hasTrivialDestructor())
1536 continue;
1537 DestructedBases.push_back(BaseClassDecl);
1538 }
1539 if (DestructedBases.empty())
1540 return;
1541 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1542 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001543 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1544 ClassDecl,BaseClassDecl,
1545 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001546 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1547 Dtor_Complete, V);
1548 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001549}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001550
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001551void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1552 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001553 llvm::Function *Fn,
1554 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001556 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001557 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1558 "SynthesizeDefaultDestructor - destructor has user declaration");
1559 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001561 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1562 SourceLocation());
1563 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001564 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001565}
Anders Carlsson6815e942009-09-27 18:58:34 +00001566
1567// FIXME: Move this to CGCXXStmt.cpp
1568void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1569 // FIXME: We need to do more here.
1570 EmitStmt(S.getTryBlock());
1571}