blob: f19067941e71d738784dc2fe6a9ca11a0c6d39be [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 Carlsson76366482009-10-12 19:45:47 +0000213 // We can always devirtualize calls on temporaries.
214 if (isa<CXXTemporaryObjectExpr>(Base))
215 return true;
216
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000217 // Check if this is a call expr that returns a record type.
218 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
219 return CE->getCallReturnType()->isRecordType();
220
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000221 // We can't devirtualize the call.
222 return false;
223}
224
Anders Carlsson774e7c62009-04-03 22:50:24 +0000225RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000226 if (isa<BinaryOperator>(CE->getCallee()))
227 return EmitCXXMemberPointerCallExpr(CE);
228
Anders Carlsson774e7c62009-04-03 22:50:24 +0000229 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
230 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000231
Anders Carlsson2472bf02009-09-29 03:54:11 +0000232 if (MD->isStatic()) {
233 // The method is static, emit it as we would a regular call.
234 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
235 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
236 CE->arg_begin(), CE->arg_end(), 0);
237
238 }
239
John McCall183700f2009-09-21 23:43:11 +0000240 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000241
Mike Stump1eb44332009-09-09 15:08:12 +0000242 const llvm::Type *Ty =
243 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000244 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000245 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Anders Carlsson774e7c62009-04-03 22:50:24 +0000247 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000248 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000249 else {
250 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000251 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000252 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000253
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000254 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000255 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000256 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000257 //
258 // We also don't emit a virtual call if the base expression has a record type
259 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000260 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000261 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000262 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000263 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000264 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000265 = dyn_cast<CXXDestructorDecl>(MD))
266 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000267 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000268 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000269
270 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000271 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000272}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000273
Mike Stump1eb44332009-09-09 15:08:12 +0000274RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000275CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
276 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
277 const DeclRefExpr *BaseExpr = cast<DeclRefExpr>(BO->getLHS());
278 const DeclRefExpr *MemFn = cast<DeclRefExpr>(BO->getRHS());
279
280 const MemberPointerType *MPT = MemFn->getType()->getAs<MemberPointerType>();
281 const FunctionProtoType *FPT =
282 MPT->getPointeeType()->getAs<FunctionProtoType>();
283 const CXXRecordDecl *RD =
284 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl());
285
286 const llvm::FunctionType *FTy =
287 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
288 FPT->isVariadic());
289
290 const llvm::Type *Int8PtrTy =
291 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
292
293 // Get the member function pointer.
294 llvm::Value *MemFnPtr =
295 CreateTempAlloca(ConvertType(MemFn->getType()), "mem.fn");
296 EmitAggExpr(MemFn, MemFnPtr, /*VolatileDest=*/false);
297
298 // Emit the 'this' pointer.
299 llvm::Value *This;
300
301 if (BO->getOpcode() == BinaryOperator::PtrMemI)
302 This = EmitScalarExpr(BaseExpr);
303 else
304 This = EmitLValue(BaseExpr).getAddress();
305
306 // Adjust it.
307 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
308 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
309
310 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
311 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
312
313 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
314
315 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
316
317 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
318
319 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
320
321 // If the LSB in the function pointer is 1, the function pointer points to
322 // a virtual function.
323 llvm::Value *IsVirtual
324 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
325 "and");
326
327 IsVirtual = Builder.CreateTrunc(IsVirtual,
328 llvm::Type::getInt1Ty(VMContext));
329
330 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
331 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
332 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
333
334 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
335 EmitBlock(FnVirtual);
336
337 const llvm::Type *VTableTy =
338 FTy->getPointerTo()->getPointerTo()->getPointerTo();
339
340 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
341 VTable = Builder.CreateLoad(VTable);
342
343 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
344
345 // Since the function pointer is 1 plus the virtual table offset, we
346 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000347 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000348
349 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
350
351 EmitBranch(FnEnd);
352 EmitBlock(FnNonVirtual);
353
354 // If the function is not virtual, just load the pointer.
355 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
356 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
357
358 EmitBlock(FnEnd);
359
360 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
361 Callee->reserveOperandSpace(2);
362 Callee->addIncoming(VirtualFn, FnVirtual);
363 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
364
365 CallArgList Args;
366
367 QualType ThisType =
368 getContext().getPointerType(getContext().getTagDeclType(RD));
369
370 // Push the this ptr.
371 Args.push_back(std::make_pair(RValue::get(This), ThisType));
372
373 // And the rest of the call args
374 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
375 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
376 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
377 Callee, Args, 0);
378}
379
380RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000381CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
382 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000383 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000384 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Fariborz Jahanianad258832009-08-13 21:09:41 +0000386 if (MD->isCopyAssignment()) {
387 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
388 if (ClassDecl->hasTrivialCopyAssignment()) {
389 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
390 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
391 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
392 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
393 QualType Ty = E->getType();
394 EmitAggregateCopy(This, Src, Ty);
395 return RValue::get(This);
396 }
397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
John McCall183700f2009-09-21 23:43:11 +0000399 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000400 const llvm::Type *Ty =
401 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000402 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000403 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Anders Carlsson0f294632009-05-27 04:18:27 +0000405 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlsson0f294632009-05-27 04:18:27 +0000407 return EmitCXXMemberCall(MD, Callee, This,
408 E->arg_begin() + 1, E->arg_end());
409}
410
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000411llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000412 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000413 "Must be in a C++ member function decl to load 'this'");
414 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
415 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000417 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000418 // ans: See how CodeGenFunction::LoadObjCSelf() uses
419 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000420 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
421}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000422
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000423/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
424/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000425/// array.
426/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
427/// array type and 'ArrayPtr' points to the beginning fo the array.
428/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000429void
430CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000431 const ConstantArrayType *ArrayTy,
432 llvm::Value *ArrayPtr) {
433 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
434 llvm::Value * NumElements =
435 llvm::ConstantInt::get(SizeTy,
436 getContext().getConstantArrayElementCount(ArrayTy));
437
438 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
439}
440
441void
442CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
443 llvm::Value *NumElements,
444 llvm::Value *ArrayPtr) {
445 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000447 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000448 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
449 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
450 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000452 // Start the loop with a block that tests the condition.
453 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
454 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000456 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000458 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000460 // Generate: if (loop-index < number-of-elements fall to the loop body,
461 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000462 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000463 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000464 // If the condition is true, execute the body.
465 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000467 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000469 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000470 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000471 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000472 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
473 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000474 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000476 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000478 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000479 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000480 Counter = Builder.CreateLoad(IndexPtr);
481 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
482 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000484 // Finally, branch back up to the condition for the next iteration.
485 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000487 // Emit the fall-through block.
488 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000489}
490
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000491/// EmitCXXAggrDestructorCall - calls the default destructor on array
492/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000493void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000494CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
495 const ArrayType *Array,
496 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000497 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
498 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000499 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000500 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000501 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000502 // Create a temporary for the loop index and initialize it with count of
503 // array elements.
504 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
505 "loop.index");
506 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000507 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000508 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
509 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000511 // Start the loop with a block that tests the condition.
512 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
513 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000515 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000517 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000519 // Generate: if (loop-index != 0 fall to the loop body,
520 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000521 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000522 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
523 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
524 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
525 "isne");
526 // If the condition is true, execute the body.
527 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000529 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000531 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
532 // Inside the loop body, emit the constructor call on the array element.
533 Counter = Builder.CreateLoad(IndexPtr);
534 Counter = Builder.CreateSub(Counter, One);
535 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
536 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000538 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000540 // Emit the decrement of the loop counter.
541 Counter = Builder.CreateLoad(IndexPtr);
542 Counter = Builder.CreateSub(Counter, One, "dec");
543 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000545 // Finally, branch back up to the condition for the next iteration.
546 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000548 // Emit the fall-through block.
549 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000550}
551
552void
Mike Stump1eb44332009-09-09 15:08:12 +0000553CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
554 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000555 llvm::Value *This,
556 CallExpr::const_arg_iterator ArgBeg,
557 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000558 if (D->isCopyConstructor(getContext())) {
559 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
560 if (ClassDecl->hasTrivialCopyConstructor()) {
561 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
562 "EmitCXXConstructorCall - user declared copy constructor");
563 const Expr *E = (*ArgBeg);
564 QualType Ty = E->getType();
565 llvm::Value *Src = EmitLValue(E).getAddress();
566 EmitAggregateCopy(This, Src, Ty);
567 return;
568 }
569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000571 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
572
573 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000574}
575
Mike Stump1eb44332009-09-09 15:08:12 +0000576void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000577 CXXDtorType Type,
578 llvm::Value *This) {
579 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Anders Carlsson7267c162009-05-29 21:03:38 +0000581 EmitCXXMemberCall(D, Callee, This, 0, 0);
582}
583
Mike Stump1eb44332009-09-09 15:08:12 +0000584void
585CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000586 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000587 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000588
589 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000590 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000591 if (RD->hasTrivialConstructor())
592 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000593
Mike Stump1eb44332009-09-09 15:08:12 +0000594 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000595 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000596 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000597 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000598 EmitAggExpr((*i), Dest, false);
599 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000600 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000601 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000602 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000603 E->arg_begin(), E->arg_end());
604}
605
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000606void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000607 EmitGlobal(GlobalDecl(D, Ctor_Complete));
608 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000609}
Anders Carlsson363c1842009-04-16 23:57:24 +0000610
Mike Stump1eb44332009-09-09 15:08:12 +0000611void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000612 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Anders Carlsson27ae5362009-04-17 01:58:57 +0000614 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000616 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Anders Carlsson27ae5362009-04-17 01:58:57 +0000618 SetFunctionDefinitionAttributes(D, Fn);
619 SetLLVMFunctionAttributesForDefinition(D, Fn);
620}
621
Anders Carlsson363c1842009-04-16 23:57:24 +0000622llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000623CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000624 CXXCtorType Type) {
625 const llvm::FunctionType *FTy =
626 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Anders Carlsson363c1842009-04-16 23:57:24 +0000628 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000629 return cast<llvm::Function>(
630 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000631}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000632
Mike Stump1eb44332009-09-09 15:08:12 +0000633const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000634 CXXCtorType Type) {
635 llvm::SmallString<256> Name;
636 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000637 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Anders Carlsson27ae5362009-04-17 01:58:57 +0000639 Name += '\0';
640 return UniqueMangledName(Name.begin(), Name.end());
641}
642
643void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000644 EmitCXXDestructor(D, Dtor_Complete);
645 EmitCXXDestructor(D, Dtor_Base);
646}
647
Mike Stump1eb44332009-09-09 15:08:12 +0000648void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000649 CXXDtorType Type) {
650 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000652 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Anders Carlsson27ae5362009-04-17 01:58:57 +0000654 SetFunctionDefinitionAttributes(D, Fn);
655 SetLLVMFunctionAttributesForDefinition(D, Fn);
656}
657
658llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000659CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000660 CXXDtorType Type) {
661 const llvm::FunctionType *FTy =
662 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Anders Carlsson27ae5362009-04-17 01:58:57 +0000664 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000665 return cast<llvm::Function>(
666 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000667}
668
Mike Stump1eb44332009-09-09 15:08:12 +0000669const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000670 CXXDtorType Type) {
671 llvm::SmallString<256> Name;
672 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000673 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Anders Carlsson27ae5362009-04-17 01:58:57 +0000675 Name += '\0';
676 return UniqueMangledName(Name.begin(), Name.end());
677}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000678
Mike Stumped032eb2009-09-04 18:27:16 +0000679llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
680 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000681 bool Extern, int64_t nv,
682 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +0000683 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +0000684
685 FunctionArgList Args;
686 ImplicitParamDecl *ThisDecl =
687 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
688 MD->getThisType(getContext()));
689 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
690 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
691 e = MD->param_end();
692 i != e; ++i) {
693 ParmVarDecl *D = *i;
694 Args.push_back(std::make_pair(D, D->getType()));
695 }
696 IdentifierInfo *II
697 = &CGM.getContext().Idents.get("__thunk_named_foo_");
698 FunctionDecl *FD = FunctionDecl::Create(getContext(),
699 getContext().getTranslationUnitDecl(),
700 SourceLocation(), II, R, 0,
701 Extern
702 ? FunctionDecl::Extern
703 : FunctionDecl::Static,
704 false, true);
705 StartFunction(FD, R, Fn, Args, SourceLocation());
706 // FIXME: generate body
707 FinishFunction();
708 return Fn;
709}
710
Mike Stump6e319f62009-09-11 23:25:56 +0000711llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
712 const CXXMethodDecl *MD,
713 bool Extern,
714 int64_t nv_t,
715 int64_t v_t,
716 int64_t nv_r,
717 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +0000718 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000719
720 FunctionArgList Args;
721 ImplicitParamDecl *ThisDecl =
722 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
723 MD->getThisType(getContext()));
724 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
725 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
726 e = MD->param_end();
727 i != e; ++i) {
728 ParmVarDecl *D = *i;
729 Args.push_back(std::make_pair(D, D->getType()));
730 }
731 IdentifierInfo *II
732 = &CGM.getContext().Idents.get("__thunk_named_foo_");
733 FunctionDecl *FD = FunctionDecl::Create(getContext(),
734 getContext().getTranslationUnitDecl(),
735 SourceLocation(), II, R, 0,
736 Extern
737 ? FunctionDecl::Extern
738 : FunctionDecl::Static,
739 false, true);
740 StartFunction(FD, R, Fn, Args, SourceLocation());
741 // FIXME: generate body
742 FinishFunction();
743 return Fn;
744}
745
Mike Stump77ca8f62009-09-05 07:20:32 +0000746llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
747 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000748 llvm::SmallString<256> OutName;
749 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000750 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000751 llvm::GlobalVariable::LinkageTypes linktype;
752 linktype = llvm::GlobalValue::WeakAnyLinkage;
753 if (!Extern)
754 linktype = llvm::GlobalValue::InternalLinkage;
755 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000756 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000757 const llvm::FunctionType *FTy =
758 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
759 FPT->isVariadic());
760
761 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
762 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000763 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000764 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
765 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
766 return m;
767}
768
Mike Stump6e319f62009-09-11 23:25:56 +0000769llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
770 bool Extern, int64_t nv_t,
771 int64_t v_t, int64_t nv_r,
772 int64_t v_r) {
773 llvm::SmallString<256> OutName;
774 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000775 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000776 llvm::GlobalVariable::LinkageTypes linktype;
777 linktype = llvm::GlobalValue::WeakAnyLinkage;
778 if (!Extern)
779 linktype = llvm::GlobalValue::InternalLinkage;
780 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000781 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000782 const llvm::FunctionType *FTy =
783 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
784 FPT->isVariadic());
785
786 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
787 &getModule());
788 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
789 v_r);
790 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
791 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
792 return m;
793}
794
Mike Stumpf0070db2009-08-26 20:46:33 +0000795llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000796CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
797 const CXXRecordDecl *ClassDecl,
798 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000799 const llvm::Type *Int8PtrTy =
800 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
801
802 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
803 Int8PtrTy->getPointerTo());
804 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
805
Anders Carlssondbd920c2009-10-11 22:13:54 +0000806 int64_t VBaseOffsetIndex =
807 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
808
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000809 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000810 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000811 const llvm::Type *PtrDiffTy =
812 ConvertType(getContext().getPointerDiffType());
813
814 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
815 PtrDiffTy->getPointerTo());
816
817 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
818
819 return VBaseOffset;
820}
821
822llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000823CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
824 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000825 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000826
Mike Stumpf0070db2009-08-26 20:46:33 +0000827 Ty = llvm::PointerType::get(Ty, 0);
828 Ty = llvm::PointerType::get(Ty, 0);
829 Ty = llvm::PointerType::get(Ty, 0);
830 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
831 vtbl = Builder.CreateLoad(vtbl);
832 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000833 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000834 vfn = Builder.CreateLoad(vfn);
835 return vfn;
836}
837
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000838/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
839/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
840/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000841// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000842void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000843 llvm::Value *Src,
844 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000845 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000846 QualType Ty) {
847 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
848 assert(CA && "VLA cannot be copied over");
849 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000851 // Create a temporary for the loop index and initialize it with 0.
852 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
853 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000854 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000855 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000856 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000857 // Start the loop with a block that tests the condition.
858 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
859 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000861 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000863 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
864 // Generate: if (loop-index < number-of-elements fall to the loop body,
865 // otherwise, go to the block after the for-loop.
866 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000867 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000868 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
869 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000870 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000871 "isless");
872 // If the condition is true, execute the body.
873 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000875 EmitBlock(ForBody);
876 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
877 // Inside the loop body, emit the constructor call on the array element.
878 Counter = Builder.CreateLoad(IndexPtr);
879 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
880 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
881 if (BitwiseCopy)
882 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000883 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000884 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000885 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000886 Ctor_Complete);
887 CallArgList CallArgs;
888 // Push the this (Dest) ptr.
889 CallArgs.push_back(std::make_pair(RValue::get(Dest),
890 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000892 // Push the Src ptr.
893 CallArgs.push_back(std::make_pair(RValue::get(Src),
894 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000895 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000896 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000897 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
898 Callee, CallArgs, BaseCopyCtor);
899 }
900 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000902 // Emit the increment of the loop counter.
903 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
904 Counter = Builder.CreateLoad(IndexPtr);
905 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
906 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000908 // Finally, branch back up to the condition for the next iteration.
909 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000911 // Emit the fall-through block.
912 EmitBlock(AfterFor, true);
913}
914
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000915/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000916/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000917/// bitwise assignment or via a copy assignment operator function call.
918/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000919void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000920 llvm::Value *Src,
921 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000922 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000923 QualType Ty) {
924 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
925 assert(CA && "VLA cannot be asssigned");
926 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000928 // Create a temporary for the loop index and initialize it with 0.
929 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
930 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000931 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000932 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
933 Builder.CreateStore(zeroConstant, IndexPtr, false);
934 // Start the loop with a block that tests the condition.
935 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
936 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000938 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000940 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
941 // Generate: if (loop-index < number-of-elements fall to the loop body,
942 // otherwise, go to the block after the for-loop.
943 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000944 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000945 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
946 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000947 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000948 "isless");
949 // If the condition is true, execute the body.
950 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000952 EmitBlock(ForBody);
953 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
954 // Inside the loop body, emit the assignment operator call on array element.
955 Counter = Builder.CreateLoad(IndexPtr);
956 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
957 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
958 const CXXMethodDecl *MD = 0;
959 if (BitwiseAssign)
960 EmitAggregateCopy(Dest, Src, Ty);
961 else {
962 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
963 MD);
964 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
965 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +0000966 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000967 const llvm::Type *LTy =
968 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
969 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000970 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000972 CallArgList CallArgs;
973 // Push the this (Dest) ptr.
974 CallArgs.push_back(std::make_pair(RValue::get(Dest),
975 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000977 // Push the Src ptr.
978 CallArgs.push_back(std::make_pair(RValue::get(Src),
979 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +0000980 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000981 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
982 Callee, CallArgs, MD);
983 }
984 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000986 // Emit the increment of the loop counter.
987 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
988 Counter = Builder.CreateLoad(IndexPtr);
989 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
990 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000992 // Finally, branch back up to the condition for the next iteration.
993 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000995 // Emit the fall-through block.
996 EmitBlock(AfterFor, true);
997}
998
Fariborz Jahanianca283612009-08-07 23:51:33 +0000999/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1000/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001001/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001002void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001003 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001004 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001005 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1006 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001007 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1008 /*NullCheckValue=*/false);
1009 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1010 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001011 }
1012 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1013 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001014 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001015 }
Mike Stump1eb44332009-09-09 15:08:12 +00001016
1017 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001018 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001019 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001020 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001021 CallArgList CallArgs;
1022 // Push the this (Dest) ptr.
1023 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1024 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Fariborz Jahanianca283612009-08-07 23:51:33 +00001026 // Push the Src ptr.
1027 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001028 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001029 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001030 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001031 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1032 Callee, CallArgs, BaseCopyCtor);
1033 }
1034}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001035
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001036/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001037/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001038/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001039// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001040void CodeGenFunction::EmitClassCopyAssignment(
1041 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001042 const CXXRecordDecl *ClassDecl,
1043 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001044 QualType Ty) {
1045 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001046 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1047 /*NullCheckValue=*/false);
1048 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1049 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001050 }
1051 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1052 EmitAggregateCopy(Dest, Src, Ty);
1053 return;
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001056 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001057 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001058 MD);
1059 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1060 (void)ConstCopyAssignOp;
1061
John McCall183700f2009-09-21 23:43:11 +00001062 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001063 const llvm::Type *LTy =
1064 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001065 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001066 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001068 CallArgList CallArgs;
1069 // Push the this (Dest) ptr.
1070 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1071 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001073 // Push the Src ptr.
1074 CallArgs.push_back(std::make_pair(RValue::get(Src),
1075 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001076 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001077 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001078 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1079 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001080}
1081
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001082/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001083void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001084CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1085 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001086 llvm::Function *Fn,
1087 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001088 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1089 SourceLocation());
1090 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001091 FinishFunction();
1092}
1093
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001094/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001095/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001096/// The implicitly-defined copy constructor for class X performs a memberwise
1097/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001098/// of initialization of bases and members in a user-defined constructor
1099/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001100/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001101/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001102/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001103/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001104/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001105/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001106/// Virtual base class subobjects shall be copied only once by the
1107/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001108
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001109void
1110CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1111 CXXCtorType Type,
1112 llvm::Function *Fn,
1113 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001114 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001115 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001116 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001117 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1118 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001120 FunctionArgList::const_iterator i = Args.begin();
1121 const VarDecl *ThisArg = i->first;
1122 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1123 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1124 const VarDecl *SrcArg = (i+1)->first;
1125 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1126 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001128 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1129 Base != ClassDecl->bases_end(); ++Base) {
1130 // FIXME. copy constrution of virtual base NYI
1131 if (Base->isVirtual())
1132 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001134 CXXRecordDecl *BaseClassDecl
1135 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001136 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1137 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001140 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1141 FieldEnd = ClassDecl->field_end();
1142 Field != FieldEnd; ++Field) {
1143 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001144 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001145 getContext().getAsConstantArrayType(FieldType);
1146 if (Array)
1147 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001149 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1150 CXXRecordDecl *FieldClassDecl
1151 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1152 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1153 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001154 if (Array) {
1155 const llvm::Type *BasePtr = ConvertType(FieldType);
1156 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001157 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001158 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001159 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001160 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1161 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1162 FieldClassDecl, FieldType);
1163 }
Mike Stump1eb44332009-09-09 15:08:12 +00001164 else
1165 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001166 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001167 continue;
1168 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001169 // Do a built-in assignment of scalar data members.
1170 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1171 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1172 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1173 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001174 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001175 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001176}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001177
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001178/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001179/// Before the implicitly-declared copy assignment operator for a class is
1180/// implicitly defined, all implicitly- declared copy assignment operators for
1181/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001182/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001183/// The implicitly-defined copy assignment operator for class X performs
1184/// memberwise assignment of its subob- jects. The direct base classes of X are
1185/// assigned first, in the order of their declaration in
1186/// the base-specifier-list, and then the immediate nonstatic data members of X
1187/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001188/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001189/// if the subobject is of class type, the copy assignment operator for the
1190/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001191/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001192///
Mike Stump1eb44332009-09-09 15:08:12 +00001193/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001194/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001195///
Mike Stump1eb44332009-09-09 15:08:12 +00001196/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001197/// used.
1198void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001199 llvm::Function *Fn,
1200 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001201
1202 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1203 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1204 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001205 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001207 FunctionArgList::const_iterator i = Args.begin();
1208 const VarDecl *ThisArg = i->first;
1209 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1210 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1211 const VarDecl *SrcArg = (i+1)->first;
1212 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1213 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001215 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1216 Base != ClassDecl->bases_end(); ++Base) {
1217 // FIXME. copy assignment of virtual base NYI
1218 if (Base->isVirtual())
1219 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001221 CXXRecordDecl *BaseClassDecl
1222 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1223 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1224 Base->getType());
1225 }
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001227 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1228 FieldEnd = ClassDecl->field_end();
1229 Field != FieldEnd; ++Field) {
1230 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001231 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001232 getContext().getAsConstantArrayType(FieldType);
1233 if (Array)
1234 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001236 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1237 CXXRecordDecl *FieldClassDecl
1238 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1239 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1240 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001241 if (Array) {
1242 const llvm::Type *BasePtr = ConvertType(FieldType);
1243 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1244 llvm::Value *DestBaseAddrPtr =
1245 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1246 llvm::Value *SrcBaseAddrPtr =
1247 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1248 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1249 FieldClassDecl, FieldType);
1250 }
1251 else
Mike Stump1eb44332009-09-09 15:08:12 +00001252 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001253 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001254 continue;
1255 }
1256 // Do a built-in assignment of scalar data members.
1257 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1258 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1259 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1260 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001261 }
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001263 // return *this;
1264 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001266 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001267}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001268
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001269/// EmitCtorPrologue - This routine generates necessary code to initialize
1270/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001271/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001272void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1273 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001274 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001275 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001276 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001278 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001279 E = CD->init_end();
1280 B != E; ++B) {
1281 CXXBaseOrMemberInitializer *Member = (*B);
1282 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001283 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001284 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001285 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001286 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001287 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1288 BaseClassDecl,
1289 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001290 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001291 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001292 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001293 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001294 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001295 // non-static data member initilaizers.
1296 FieldDecl *Field = Member->getMember();
1297 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001298 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001299 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001300 if (Array)
1301 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Mike Stumpf1216772009-07-31 18:25:34 +00001303 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001304 LValue LHS;
1305 if (FieldType->isReferenceType()) {
1306 // FIXME: This is really ugly; should be refactored somehow
1307 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1308 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001309 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1310 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001311 } else {
1312 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1313 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001314 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001315 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001316 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001317 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001318 if (Array) {
1319 const llvm::Type *BasePtr = ConvertType(FieldType);
1320 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001321 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001322 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001323 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001324 Array, BaseAddrPtr);
1325 }
1326 else
1327 EmitCXXConstructorCall(Member->getConstructor(),
1328 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001329 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001330 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001331 continue;
1332 }
1333 else {
1334 // Initializing an anonymous union data member.
1335 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001336 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001337 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001338 FieldType = anonMember->getType();
1339 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001340 }
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001342 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001343 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001344 RValue RHS;
1345 if (FieldType->isReferenceType())
1346 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1347 /*IsInitializer=*/true);
1348 else
1349 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1350 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001351 }
1352 }
Mike Stumpf1216772009-07-31 18:25:34 +00001353
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001354 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001355 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001356 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001357 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001358 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001359 ClassDecl->bases_begin();
1360 Base != ClassDecl->bases_end(); ++Base) {
1361 // FIXME. copy assignment of virtual base NYI
1362 if (Base->isVirtual())
1363 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001365 CXXRecordDecl *BaseClassDecl
1366 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1367 if (BaseClassDecl->hasTrivialConstructor())
1368 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001369 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001370 BaseClassDecl->getDefaultConstructor(getContext())) {
1371 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001372 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1373 BaseClassDecl,
1374 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001375 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1376 }
1377 }
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001379 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1380 FieldEnd = ClassDecl->field_end();
1381 Field != FieldEnd; ++Field) {
1382 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001383 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001384 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001385 if (Array)
1386 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001387 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1388 continue;
1389 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001390 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001391 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1392 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1393 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001394 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001395 MemberClassDecl->getDefaultConstructor(getContext())) {
1396 LoadOfThis = LoadCXXThis();
1397 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001398 if (Array) {
1399 const llvm::Type *BasePtr = ConvertType(FieldType);
1400 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001401 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001402 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1403 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1404 }
1405 else
Mike Stump1eb44332009-09-09 15:08:12 +00001406 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001407 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001408 }
1409 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001410 }
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Mike Stumpf1216772009-07-31 18:25:34 +00001412 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001413 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001414 if (!LoadOfThis)
1415 LoadOfThis = LoadCXXThis();
1416 llvm::Value *VtableField;
1417 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001418 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001419 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1420 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1421 llvm::Value *vtable = GenerateVtable(ClassDecl);
1422 Builder.CreateStore(vtable, VtableField);
1423 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001424}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001425
1426/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001427/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001428/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001429/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001430void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1431 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001432 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001433 assert(!ClassDecl->getNumVBases() &&
1434 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001435 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001437 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1438 *E = DD->destr_end(); B != E; ++B) {
1439 uintptr_t BaseOrMember = (*B);
1440 if (DD->isMemberToDestroy(BaseOrMember)) {
1441 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1442 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001443 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001444 getContext().getAsConstantArrayType(FieldType);
1445 if (Array)
1446 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001447 const RecordType *RT = FieldType->getAs<RecordType>();
1448 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1449 if (FieldClassDecl->hasTrivialDestructor())
1450 continue;
1451 llvm::Value *LoadOfThis = LoadCXXThis();
1452 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001453 if (Array) {
1454 const llvm::Type *BasePtr = ConvertType(FieldType);
1455 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001456 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001457 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001458 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001459 Array, BaseAddrPtr);
1460 }
1461 else
1462 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1463 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001464 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001465 const RecordType *RT =
1466 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1467 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1468 if (BaseClassDecl->hasTrivialDestructor())
1469 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001470 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1471 ClassDecl, BaseClassDecl,
1472 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001473 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001474 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001475 }
1476 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001477 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1478 return;
1479 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001480 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001481 // reverse order of their construction.
1482 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001484 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1485 FieldEnd = ClassDecl->field_end();
1486 Field != FieldEnd; ++Field) {
1487 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001488 if (getContext().getAsConstantArrayType(FieldType))
1489 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001490 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1491 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1492 if (FieldClassDecl->hasTrivialDestructor())
1493 continue;
1494 DestructedFields.push_back(*Field);
1495 }
1496 }
1497 if (!DestructedFields.empty())
1498 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1499 FieldDecl *Field = DestructedFields[i];
1500 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001501 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001502 getContext().getAsConstantArrayType(FieldType);
1503 if (Array)
1504 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001505 const RecordType *RT = FieldType->getAs<RecordType>();
1506 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1507 llvm::Value *LoadOfThis = LoadCXXThis();
1508 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001509 if (Array) {
1510 const llvm::Type *BasePtr = ConvertType(FieldType);
1511 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001512 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001513 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001514 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001515 Array, BaseAddrPtr);
1516 }
1517 else
1518 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1519 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001520 }
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001522 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1523 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1524 Base != ClassDecl->bases_end(); ++Base) {
1525 // FIXME. copy assignment of virtual base NYI
1526 if (Base->isVirtual())
1527 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001529 CXXRecordDecl *BaseClassDecl
1530 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1531 if (BaseClassDecl->hasTrivialDestructor())
1532 continue;
1533 DestructedBases.push_back(BaseClassDecl);
1534 }
1535 if (DestructedBases.empty())
1536 return;
1537 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1538 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001539 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1540 ClassDecl,BaseClassDecl,
1541 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001542 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1543 Dtor_Complete, V);
1544 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001545}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001546
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001547void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1548 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001549 llvm::Function *Fn,
1550 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001552 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001553 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1554 "SynthesizeDefaultDestructor - destructor has user declaration");
1555 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001557 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1558 SourceLocation());
1559 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001560 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001561}
Anders Carlsson6815e942009-09-27 18:58:34 +00001562
1563// FIXME: Move this to CGCXXStmt.cpp
1564void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1565 // FIXME: We need to do more here.
1566 EmitStmt(S.getTryBlock());
1567}