blob: ed7ee4298ccbe8b4941b08e7a09e5cbc5344e1b8 [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 Carlsson8e7670d2009-10-12 19:41:04 +0000217 // We can't devirtualize the call.
218 return false;
219}
220
Anders Carlsson774e7c62009-04-03 22:50:24 +0000221RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000222 if (isa<BinaryOperator>(CE->getCallee()))
223 return EmitCXXMemberPointerCallExpr(CE);
224
Anders Carlsson774e7c62009-04-03 22:50:24 +0000225 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
226 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000227
Anders Carlsson2472bf02009-09-29 03:54:11 +0000228 if (MD->isStatic()) {
229 // The method is static, emit it as we would a regular call.
230 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
231 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
232 CE->arg_begin(), CE->arg_end(), 0);
233
234 }
235
John McCall183700f2009-09-21 23:43:11 +0000236 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000237
Mike Stump1eb44332009-09-09 15:08:12 +0000238 const llvm::Type *Ty =
239 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000240 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000241 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Anders Carlsson774e7c62009-04-03 22:50:24 +0000243 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000244 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000245 else {
246 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000247 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000248 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000249
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000250 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000251 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000252 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000253 //
254 // We also don't emit a virtual call if the base expression has a record type
255 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000256 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000257 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000258 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000259 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000260 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000261 = dyn_cast<CXXDestructorDecl>(MD))
262 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000263 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000264 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000265
266 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000267 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000268}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000269
Mike Stump1eb44332009-09-09 15:08:12 +0000270RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000271CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
272 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
273 const DeclRefExpr *BaseExpr = cast<DeclRefExpr>(BO->getLHS());
274 const DeclRefExpr *MemFn = cast<DeclRefExpr>(BO->getRHS());
275
276 const MemberPointerType *MPT = MemFn->getType()->getAs<MemberPointerType>();
277 const FunctionProtoType *FPT =
278 MPT->getPointeeType()->getAs<FunctionProtoType>();
279 const CXXRecordDecl *RD =
280 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl());
281
282 const llvm::FunctionType *FTy =
283 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
284 FPT->isVariadic());
285
286 const llvm::Type *Int8PtrTy =
287 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
288
289 // Get the member function pointer.
290 llvm::Value *MemFnPtr =
291 CreateTempAlloca(ConvertType(MemFn->getType()), "mem.fn");
292 EmitAggExpr(MemFn, MemFnPtr, /*VolatileDest=*/false);
293
294 // Emit the 'this' pointer.
295 llvm::Value *This;
296
297 if (BO->getOpcode() == BinaryOperator::PtrMemI)
298 This = EmitScalarExpr(BaseExpr);
299 else
300 This = EmitLValue(BaseExpr).getAddress();
301
302 // Adjust it.
303 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
304 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
305
306 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
307 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
308
309 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
310
311 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
312
313 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
314
315 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
316
317 // If the LSB in the function pointer is 1, the function pointer points to
318 // a virtual function.
319 llvm::Value *IsVirtual
320 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
321 "and");
322
323 IsVirtual = Builder.CreateTrunc(IsVirtual,
324 llvm::Type::getInt1Ty(VMContext));
325
326 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
327 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
328 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
329
330 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
331 EmitBlock(FnVirtual);
332
333 const llvm::Type *VTableTy =
334 FTy->getPointerTo()->getPointerTo()->getPointerTo();
335
336 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
337 VTable = Builder.CreateLoad(VTable);
338
339 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
340
341 // Since the function pointer is 1 plus the virtual table offset, we
342 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000343 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000344
345 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
346
347 EmitBranch(FnEnd);
348 EmitBlock(FnNonVirtual);
349
350 // If the function is not virtual, just load the pointer.
351 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
352 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
353
354 EmitBlock(FnEnd);
355
356 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
357 Callee->reserveOperandSpace(2);
358 Callee->addIncoming(VirtualFn, FnVirtual);
359 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
360
361 CallArgList Args;
362
363 QualType ThisType =
364 getContext().getPointerType(getContext().getTagDeclType(RD));
365
366 // Push the this ptr.
367 Args.push_back(std::make_pair(RValue::get(This), ThisType));
368
369 // And the rest of the call args
370 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
371 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
372 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
373 Callee, Args, 0);
374}
375
376RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000377CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
378 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000379 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000380 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Fariborz Jahanianad258832009-08-13 21:09:41 +0000382 if (MD->isCopyAssignment()) {
383 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
384 if (ClassDecl->hasTrivialCopyAssignment()) {
385 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
386 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
387 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
388 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
389 QualType Ty = E->getType();
390 EmitAggregateCopy(This, Src, Ty);
391 return RValue::get(This);
392 }
393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
John McCall183700f2009-09-21 23:43:11 +0000395 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000396 const llvm::Type *Ty =
397 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000398 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000399 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Anders Carlsson0f294632009-05-27 04:18:27 +0000401 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Anders Carlsson0f294632009-05-27 04:18:27 +0000403 return EmitCXXMemberCall(MD, Callee, This,
404 E->arg_begin() + 1, E->arg_end());
405}
406
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000407llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000408 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000409 "Must be in a C++ member function decl to load 'this'");
410 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
411 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000413 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000414 // ans: See how CodeGenFunction::LoadObjCSelf() uses
415 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000416 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
417}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000418
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000419/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
420/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000421/// array.
422/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
423/// array type and 'ArrayPtr' points to the beginning fo the array.
424/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000425void
426CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000427 const ConstantArrayType *ArrayTy,
428 llvm::Value *ArrayPtr) {
429 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
430 llvm::Value * NumElements =
431 llvm::ConstantInt::get(SizeTy,
432 getContext().getConstantArrayElementCount(ArrayTy));
433
434 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
435}
436
437void
438CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
439 llvm::Value *NumElements,
440 llvm::Value *ArrayPtr) {
441 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000443 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000444 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
445 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
446 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000448 // Start the loop with a block that tests the condition.
449 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
450 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000452 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000454 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000456 // Generate: if (loop-index < number-of-elements fall to the loop body,
457 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000458 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000459 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000460 // If the condition is true, execute the body.
461 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000463 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000465 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000466 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000467 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000468 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
469 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000470 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000472 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000474 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000475 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000476 Counter = Builder.CreateLoad(IndexPtr);
477 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
478 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000480 // Finally, branch back up to the condition for the next iteration.
481 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000483 // Emit the fall-through block.
484 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000485}
486
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000487/// EmitCXXAggrDestructorCall - calls the default destructor on array
488/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000489void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000490CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
491 const ArrayType *Array,
492 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000493 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
494 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000495 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000496 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000497 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000498 // Create a temporary for the loop index and initialize it with count of
499 // array elements.
500 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
501 "loop.index");
502 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000503 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000504 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
505 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000507 // Start the loop with a block that tests the condition.
508 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
509 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000511 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000513 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000515 // Generate: if (loop-index != 0 fall to the loop body,
516 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000517 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000518 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
519 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
520 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
521 "isne");
522 // If the condition is true, execute the body.
523 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000525 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000527 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
528 // Inside the loop body, emit the constructor call on the array element.
529 Counter = Builder.CreateLoad(IndexPtr);
530 Counter = Builder.CreateSub(Counter, One);
531 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
532 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000534 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000536 // Emit the decrement of the loop counter.
537 Counter = Builder.CreateLoad(IndexPtr);
538 Counter = Builder.CreateSub(Counter, One, "dec");
539 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000541 // Finally, branch back up to the condition for the next iteration.
542 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000544 // Emit the fall-through block.
545 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000546}
547
548void
Mike Stump1eb44332009-09-09 15:08:12 +0000549CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
550 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000551 llvm::Value *This,
552 CallExpr::const_arg_iterator ArgBeg,
553 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000554 if (D->isCopyConstructor(getContext())) {
555 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
556 if (ClassDecl->hasTrivialCopyConstructor()) {
557 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
558 "EmitCXXConstructorCall - user declared copy constructor");
559 const Expr *E = (*ArgBeg);
560 QualType Ty = E->getType();
561 llvm::Value *Src = EmitLValue(E).getAddress();
562 EmitAggregateCopy(This, Src, Ty);
563 return;
564 }
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000567 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
568
569 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000570}
571
Mike Stump1eb44332009-09-09 15:08:12 +0000572void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000573 CXXDtorType Type,
574 llvm::Value *This) {
575 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Anders Carlsson7267c162009-05-29 21:03:38 +0000577 EmitCXXMemberCall(D, Callee, This, 0, 0);
578}
579
Mike Stump1eb44332009-09-09 15:08:12 +0000580void
581CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000582 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000583 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000584
585 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000586 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000587 if (RD->hasTrivialConstructor())
588 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000589
Mike Stump1eb44332009-09-09 15:08:12 +0000590 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000591 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000592 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000593 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000594 EmitAggExpr((*i), Dest, false);
595 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000596 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000597 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000598 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000599 E->arg_begin(), E->arg_end());
600}
601
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000602void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000603 EmitGlobal(GlobalDecl(D, Ctor_Complete));
604 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000605}
Anders Carlsson363c1842009-04-16 23:57:24 +0000606
Mike Stump1eb44332009-09-09 15:08:12 +0000607void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000608 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Anders Carlsson27ae5362009-04-17 01:58:57 +0000610 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000612 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Anders Carlsson27ae5362009-04-17 01:58:57 +0000614 SetFunctionDefinitionAttributes(D, Fn);
615 SetLLVMFunctionAttributesForDefinition(D, Fn);
616}
617
Anders Carlsson363c1842009-04-16 23:57:24 +0000618llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000619CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000620 CXXCtorType Type) {
621 const llvm::FunctionType *FTy =
622 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Anders Carlsson363c1842009-04-16 23:57:24 +0000624 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000625 return cast<llvm::Function>(
626 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000627}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000628
Mike Stump1eb44332009-09-09 15:08:12 +0000629const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000630 CXXCtorType Type) {
631 llvm::SmallString<256> Name;
632 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000633 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Anders Carlsson27ae5362009-04-17 01:58:57 +0000635 Name += '\0';
636 return UniqueMangledName(Name.begin(), Name.end());
637}
638
639void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000640 EmitCXXDestructor(D, Dtor_Complete);
641 EmitCXXDestructor(D, Dtor_Base);
642}
643
Mike Stump1eb44332009-09-09 15:08:12 +0000644void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000645 CXXDtorType Type) {
646 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000648 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Anders Carlsson27ae5362009-04-17 01:58:57 +0000650 SetFunctionDefinitionAttributes(D, Fn);
651 SetLLVMFunctionAttributesForDefinition(D, Fn);
652}
653
654llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000655CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000656 CXXDtorType Type) {
657 const llvm::FunctionType *FTy =
658 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Anders Carlsson27ae5362009-04-17 01:58:57 +0000660 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000661 return cast<llvm::Function>(
662 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000663}
664
Mike Stump1eb44332009-09-09 15:08:12 +0000665const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000666 CXXDtorType Type) {
667 llvm::SmallString<256> Name;
668 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000669 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Anders Carlsson27ae5362009-04-17 01:58:57 +0000671 Name += '\0';
672 return UniqueMangledName(Name.begin(), Name.end());
673}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000674
Mike Stumped032eb2009-09-04 18:27:16 +0000675llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
676 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000677 bool Extern, int64_t nv,
678 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +0000679 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +0000680
681 FunctionArgList Args;
682 ImplicitParamDecl *ThisDecl =
683 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
684 MD->getThisType(getContext()));
685 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
686 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
687 e = MD->param_end();
688 i != e; ++i) {
689 ParmVarDecl *D = *i;
690 Args.push_back(std::make_pair(D, D->getType()));
691 }
692 IdentifierInfo *II
693 = &CGM.getContext().Idents.get("__thunk_named_foo_");
694 FunctionDecl *FD = FunctionDecl::Create(getContext(),
695 getContext().getTranslationUnitDecl(),
696 SourceLocation(), II, R, 0,
697 Extern
698 ? FunctionDecl::Extern
699 : FunctionDecl::Static,
700 false, true);
701 StartFunction(FD, R, Fn, Args, SourceLocation());
702 // FIXME: generate body
703 FinishFunction();
704 return Fn;
705}
706
Mike Stump6e319f62009-09-11 23:25:56 +0000707llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
708 const CXXMethodDecl *MD,
709 bool Extern,
710 int64_t nv_t,
711 int64_t v_t,
712 int64_t nv_r,
713 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +0000714 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000715
716 FunctionArgList Args;
717 ImplicitParamDecl *ThisDecl =
718 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
719 MD->getThisType(getContext()));
720 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
721 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
722 e = MD->param_end();
723 i != e; ++i) {
724 ParmVarDecl *D = *i;
725 Args.push_back(std::make_pair(D, D->getType()));
726 }
727 IdentifierInfo *II
728 = &CGM.getContext().Idents.get("__thunk_named_foo_");
729 FunctionDecl *FD = FunctionDecl::Create(getContext(),
730 getContext().getTranslationUnitDecl(),
731 SourceLocation(), II, R, 0,
732 Extern
733 ? FunctionDecl::Extern
734 : FunctionDecl::Static,
735 false, true);
736 StartFunction(FD, R, Fn, Args, SourceLocation());
737 // FIXME: generate body
738 FinishFunction();
739 return Fn;
740}
741
Mike Stump77ca8f62009-09-05 07:20:32 +0000742llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
743 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000744 llvm::SmallString<256> OutName;
745 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000746 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000747 llvm::GlobalVariable::LinkageTypes linktype;
748 linktype = llvm::GlobalValue::WeakAnyLinkage;
749 if (!Extern)
750 linktype = llvm::GlobalValue::InternalLinkage;
751 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000752 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000753 const llvm::FunctionType *FTy =
754 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
755 FPT->isVariadic());
756
757 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
758 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000759 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000760 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
761 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
762 return m;
763}
764
Mike Stump6e319f62009-09-11 23:25:56 +0000765llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
766 bool Extern, int64_t nv_t,
767 int64_t v_t, int64_t nv_r,
768 int64_t v_r) {
769 llvm::SmallString<256> OutName;
770 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000771 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000772 llvm::GlobalVariable::LinkageTypes linktype;
773 linktype = llvm::GlobalValue::WeakAnyLinkage;
774 if (!Extern)
775 linktype = llvm::GlobalValue::InternalLinkage;
776 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000777 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000778 const llvm::FunctionType *FTy =
779 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
780 FPT->isVariadic());
781
782 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
783 &getModule());
784 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
785 v_r);
786 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
787 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
788 return m;
789}
790
Mike Stumpf0070db2009-08-26 20:46:33 +0000791llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000792CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
793 const CXXRecordDecl *ClassDecl,
794 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000795 const llvm::Type *Int8PtrTy =
796 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
797
798 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
799 Int8PtrTy->getPointerTo());
800 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
801
Anders Carlssondbd920c2009-10-11 22:13:54 +0000802 int64_t VBaseOffsetIndex =
803 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
804
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000805 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000806 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000807 const llvm::Type *PtrDiffTy =
808 ConvertType(getContext().getPointerDiffType());
809
810 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
811 PtrDiffTy->getPointerTo());
812
813 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
814
815 return VBaseOffset;
816}
817
818llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000819CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
820 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000821 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000822
Mike Stumpf0070db2009-08-26 20:46:33 +0000823 Ty = llvm::PointerType::get(Ty, 0);
824 Ty = llvm::PointerType::get(Ty, 0);
825 Ty = llvm::PointerType::get(Ty, 0);
826 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
827 vtbl = Builder.CreateLoad(vtbl);
828 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000829 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000830 vfn = Builder.CreateLoad(vfn);
831 return vfn;
832}
833
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000834/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
835/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
836/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000837// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000838void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000839 llvm::Value *Src,
840 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000841 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000842 QualType Ty) {
843 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
844 assert(CA && "VLA cannot be copied over");
845 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000847 // Create a temporary for the loop index and initialize it with 0.
848 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
849 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000850 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000851 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000852 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000853 // Start the loop with a block that tests the condition.
854 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
855 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000857 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000859 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
860 // Generate: if (loop-index < number-of-elements fall to the loop body,
861 // otherwise, go to the block after the for-loop.
862 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000863 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000864 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
865 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000866 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000867 "isless");
868 // If the condition is true, execute the body.
869 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000871 EmitBlock(ForBody);
872 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
873 // Inside the loop body, emit the constructor call on the array element.
874 Counter = Builder.CreateLoad(IndexPtr);
875 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
876 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
877 if (BitwiseCopy)
878 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000879 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000880 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000881 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000882 Ctor_Complete);
883 CallArgList CallArgs;
884 // Push the this (Dest) ptr.
885 CallArgs.push_back(std::make_pair(RValue::get(Dest),
886 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000888 // Push the Src ptr.
889 CallArgs.push_back(std::make_pair(RValue::get(Src),
890 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000891 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000892 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000893 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
894 Callee, CallArgs, BaseCopyCtor);
895 }
896 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000898 // Emit the increment of the loop counter.
899 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
900 Counter = Builder.CreateLoad(IndexPtr);
901 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
902 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000904 // Finally, branch back up to the condition for the next iteration.
905 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000907 // Emit the fall-through block.
908 EmitBlock(AfterFor, true);
909}
910
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000911/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000912/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000913/// bitwise assignment or via a copy assignment operator function call.
914/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000915void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000916 llvm::Value *Src,
917 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000918 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000919 QualType Ty) {
920 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
921 assert(CA && "VLA cannot be asssigned");
922 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000924 // Create a temporary for the loop index and initialize it with 0.
925 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
926 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000927 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000928 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
929 Builder.CreateStore(zeroConstant, IndexPtr, false);
930 // Start the loop with a block that tests the condition.
931 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
932 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000934 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000936 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
937 // Generate: if (loop-index < number-of-elements fall to the loop body,
938 // otherwise, go to the block after the for-loop.
939 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000940 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000941 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
942 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000943 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000944 "isless");
945 // If the condition is true, execute the body.
946 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000948 EmitBlock(ForBody);
949 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
950 // Inside the loop body, emit the assignment operator call on array element.
951 Counter = Builder.CreateLoad(IndexPtr);
952 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
953 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
954 const CXXMethodDecl *MD = 0;
955 if (BitwiseAssign)
956 EmitAggregateCopy(Dest, Src, Ty);
957 else {
958 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
959 MD);
960 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
961 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +0000962 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000963 const llvm::Type *LTy =
964 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
965 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000966 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000968 CallArgList CallArgs;
969 // Push the this (Dest) ptr.
970 CallArgs.push_back(std::make_pair(RValue::get(Dest),
971 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000973 // Push the Src ptr.
974 CallArgs.push_back(std::make_pair(RValue::get(Src),
975 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +0000976 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000977 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
978 Callee, CallArgs, MD);
979 }
980 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000982 // Emit the increment of the loop counter.
983 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
984 Counter = Builder.CreateLoad(IndexPtr);
985 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
986 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000988 // Finally, branch back up to the condition for the next iteration.
989 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000991 // Emit the fall-through block.
992 EmitBlock(AfterFor, true);
993}
994
Fariborz Jahanianca283612009-08-07 23:51:33 +0000995/// EmitClassMemberwiseCopy - This routine generates code to copy a class
996/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000997/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +0000998void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +0000999 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001000 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001001 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1002 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001003 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1004 /*NullCheckValue=*/false);
1005 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1006 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001007 }
1008 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1009 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001010 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001011 }
Mike Stump1eb44332009-09-09 15:08:12 +00001012
1013 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001014 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001015 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001016 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001017 CallArgList CallArgs;
1018 // Push the this (Dest) ptr.
1019 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1020 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Fariborz Jahanianca283612009-08-07 23:51:33 +00001022 // Push the Src ptr.
1023 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001024 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001025 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001026 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001027 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1028 Callee, CallArgs, BaseCopyCtor);
1029 }
1030}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001031
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001032/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001033/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001034/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001035// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001036void CodeGenFunction::EmitClassCopyAssignment(
1037 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001038 const CXXRecordDecl *ClassDecl,
1039 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001040 QualType Ty) {
1041 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001042 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1043 /*NullCheckValue=*/false);
1044 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1045 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001046 }
1047 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1048 EmitAggregateCopy(Dest, Src, Ty);
1049 return;
1050 }
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001052 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001053 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001054 MD);
1055 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1056 (void)ConstCopyAssignOp;
1057
John McCall183700f2009-09-21 23:43:11 +00001058 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001059 const llvm::Type *LTy =
1060 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001061 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001062 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001064 CallArgList CallArgs;
1065 // Push the this (Dest) ptr.
1066 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1067 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001069 // Push the Src ptr.
1070 CallArgs.push_back(std::make_pair(RValue::get(Src),
1071 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001072 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001073 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001074 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1075 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001076}
1077
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001078/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001079void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001080CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1081 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001082 llvm::Function *Fn,
1083 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001084 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1085 SourceLocation());
1086 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001087 FinishFunction();
1088}
1089
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001090/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001091/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001092/// The implicitly-defined copy constructor for class X performs a memberwise
1093/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001094/// of initialization of bases and members in a user-defined constructor
1095/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001096/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001097/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001098/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001099/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001100/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001101/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001102/// Virtual base class subobjects shall be copied only once by the
1103/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001104
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001105void
1106CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1107 CXXCtorType Type,
1108 llvm::Function *Fn,
1109 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001110 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001111 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001112 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001113 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1114 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001116 FunctionArgList::const_iterator i = Args.begin();
1117 const VarDecl *ThisArg = i->first;
1118 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1119 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1120 const VarDecl *SrcArg = (i+1)->first;
1121 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1122 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001124 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1125 Base != ClassDecl->bases_end(); ++Base) {
1126 // FIXME. copy constrution of virtual base NYI
1127 if (Base->isVirtual())
1128 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001130 CXXRecordDecl *BaseClassDecl
1131 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001132 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1133 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001136 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1137 FieldEnd = ClassDecl->field_end();
1138 Field != FieldEnd; ++Field) {
1139 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001140 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001141 getContext().getAsConstantArrayType(FieldType);
1142 if (Array)
1143 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001145 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1146 CXXRecordDecl *FieldClassDecl
1147 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1148 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1149 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001150 if (Array) {
1151 const llvm::Type *BasePtr = ConvertType(FieldType);
1152 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001153 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001154 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001155 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001156 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1157 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1158 FieldClassDecl, FieldType);
1159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160 else
1161 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001162 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001163 continue;
1164 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001165 // Do a built-in assignment of scalar data members.
1166 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1167 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1168 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1169 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001170 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001171 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001172}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001173
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001174/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001175/// Before the implicitly-declared copy assignment operator for a class is
1176/// implicitly defined, all implicitly- declared copy assignment operators for
1177/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001178/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001179/// The implicitly-defined copy assignment operator for class X performs
1180/// memberwise assignment of its subob- jects. The direct base classes of X are
1181/// assigned first, in the order of their declaration in
1182/// the base-specifier-list, and then the immediate nonstatic data members of X
1183/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001184/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001185/// if the subobject is of class type, the copy assignment operator for the
1186/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001187/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001188///
Mike Stump1eb44332009-09-09 15:08:12 +00001189/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001190/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001191///
Mike Stump1eb44332009-09-09 15:08:12 +00001192/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001193/// used.
1194void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001195 llvm::Function *Fn,
1196 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001197
1198 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1199 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1200 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001201 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001203 FunctionArgList::const_iterator i = Args.begin();
1204 const VarDecl *ThisArg = i->first;
1205 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1206 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1207 const VarDecl *SrcArg = (i+1)->first;
1208 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1209 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001211 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1212 Base != ClassDecl->bases_end(); ++Base) {
1213 // FIXME. copy assignment of virtual base NYI
1214 if (Base->isVirtual())
1215 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001217 CXXRecordDecl *BaseClassDecl
1218 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1219 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1220 Base->getType());
1221 }
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001223 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1224 FieldEnd = ClassDecl->field_end();
1225 Field != FieldEnd; ++Field) {
1226 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001227 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001228 getContext().getAsConstantArrayType(FieldType);
1229 if (Array)
1230 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001232 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1233 CXXRecordDecl *FieldClassDecl
1234 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1235 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1236 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001237 if (Array) {
1238 const llvm::Type *BasePtr = ConvertType(FieldType);
1239 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1240 llvm::Value *DestBaseAddrPtr =
1241 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1242 llvm::Value *SrcBaseAddrPtr =
1243 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1244 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1245 FieldClassDecl, FieldType);
1246 }
1247 else
Mike Stump1eb44332009-09-09 15:08:12 +00001248 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001249 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001250 continue;
1251 }
1252 // Do a built-in assignment of scalar data members.
1253 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1254 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1255 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1256 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001257 }
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001259 // return *this;
1260 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001262 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001263}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001264
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001265/// EmitCtorPrologue - This routine generates necessary code to initialize
1266/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001267/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001268void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1269 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001270 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001271 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001272 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001274 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001275 E = CD->init_end();
1276 B != E; ++B) {
1277 CXXBaseOrMemberInitializer *Member = (*B);
1278 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001279 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001280 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001281 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001282 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001283 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1284 BaseClassDecl,
1285 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001286 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001287 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001288 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001289 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001290 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001291 // non-static data member initilaizers.
1292 FieldDecl *Field = Member->getMember();
1293 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001294 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001295 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001296 if (Array)
1297 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Mike Stumpf1216772009-07-31 18:25:34 +00001299 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001300 LValue LHS;
1301 if (FieldType->isReferenceType()) {
1302 // FIXME: This is really ugly; should be refactored somehow
1303 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1304 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001305 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1306 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001307 } else {
1308 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1309 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001310 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001311 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001312 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001313 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001314 if (Array) {
1315 const llvm::Type *BasePtr = ConvertType(FieldType);
1316 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001317 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001318 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001319 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001320 Array, BaseAddrPtr);
1321 }
1322 else
1323 EmitCXXConstructorCall(Member->getConstructor(),
1324 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001325 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001326 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001327 continue;
1328 }
1329 else {
1330 // Initializing an anonymous union data member.
1331 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001332 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001333 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001334 FieldType = anonMember->getType();
1335 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001336 }
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001338 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001339 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001340 RValue RHS;
1341 if (FieldType->isReferenceType())
1342 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1343 /*IsInitializer=*/true);
1344 else
1345 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1346 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001347 }
1348 }
Mike Stumpf1216772009-07-31 18:25:34 +00001349
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001350 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001351 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001352 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001353 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001354 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001355 ClassDecl->bases_begin();
1356 Base != ClassDecl->bases_end(); ++Base) {
1357 // FIXME. copy assignment of virtual base NYI
1358 if (Base->isVirtual())
1359 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001361 CXXRecordDecl *BaseClassDecl
1362 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1363 if (BaseClassDecl->hasTrivialConstructor())
1364 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001365 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001366 BaseClassDecl->getDefaultConstructor(getContext())) {
1367 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001368 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1369 BaseClassDecl,
1370 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001371 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1372 }
1373 }
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001375 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1376 FieldEnd = ClassDecl->field_end();
1377 Field != FieldEnd; ++Field) {
1378 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001379 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001380 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001381 if (Array)
1382 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001383 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1384 continue;
1385 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001386 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001387 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1388 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1389 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001390 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001391 MemberClassDecl->getDefaultConstructor(getContext())) {
1392 LoadOfThis = LoadCXXThis();
1393 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001394 if (Array) {
1395 const llvm::Type *BasePtr = ConvertType(FieldType);
1396 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001397 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001398 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1399 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1400 }
1401 else
Mike Stump1eb44332009-09-09 15:08:12 +00001402 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001403 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001404 }
1405 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001406 }
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Mike Stumpf1216772009-07-31 18:25:34 +00001408 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001409 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001410 if (!LoadOfThis)
1411 LoadOfThis = LoadCXXThis();
1412 llvm::Value *VtableField;
1413 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001414 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001415 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1416 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1417 llvm::Value *vtable = GenerateVtable(ClassDecl);
1418 Builder.CreateStore(vtable, VtableField);
1419 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001420}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001421
1422/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001423/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001424/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001425/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001426void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1427 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001428 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001429 assert(!ClassDecl->getNumVBases() &&
1430 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001431 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001433 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1434 *E = DD->destr_end(); B != E; ++B) {
1435 uintptr_t BaseOrMember = (*B);
1436 if (DD->isMemberToDestroy(BaseOrMember)) {
1437 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1438 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001439 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001440 getContext().getAsConstantArrayType(FieldType);
1441 if (Array)
1442 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001443 const RecordType *RT = FieldType->getAs<RecordType>();
1444 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1445 if (FieldClassDecl->hasTrivialDestructor())
1446 continue;
1447 llvm::Value *LoadOfThis = LoadCXXThis();
1448 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001449 if (Array) {
1450 const llvm::Type *BasePtr = ConvertType(FieldType);
1451 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001452 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001453 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001454 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001455 Array, BaseAddrPtr);
1456 }
1457 else
1458 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1459 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001460 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001461 const RecordType *RT =
1462 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1463 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1464 if (BaseClassDecl->hasTrivialDestructor())
1465 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001466 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1467 ClassDecl, BaseClassDecl,
1468 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001469 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001470 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001471 }
1472 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001473 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1474 return;
1475 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001476 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001477 // reverse order of their construction.
1478 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001480 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1481 FieldEnd = ClassDecl->field_end();
1482 Field != FieldEnd; ++Field) {
1483 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001484 if (getContext().getAsConstantArrayType(FieldType))
1485 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001486 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1487 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1488 if (FieldClassDecl->hasTrivialDestructor())
1489 continue;
1490 DestructedFields.push_back(*Field);
1491 }
1492 }
1493 if (!DestructedFields.empty())
1494 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1495 FieldDecl *Field = DestructedFields[i];
1496 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001497 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001498 getContext().getAsConstantArrayType(FieldType);
1499 if (Array)
1500 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001501 const RecordType *RT = FieldType->getAs<RecordType>();
1502 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1503 llvm::Value *LoadOfThis = LoadCXXThis();
1504 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001505 if (Array) {
1506 const llvm::Type *BasePtr = ConvertType(FieldType);
1507 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001508 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001509 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001510 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001511 Array, BaseAddrPtr);
1512 }
1513 else
1514 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1515 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001516 }
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001518 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1519 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1520 Base != ClassDecl->bases_end(); ++Base) {
1521 // FIXME. copy assignment of virtual base NYI
1522 if (Base->isVirtual())
1523 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001525 CXXRecordDecl *BaseClassDecl
1526 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1527 if (BaseClassDecl->hasTrivialDestructor())
1528 continue;
1529 DestructedBases.push_back(BaseClassDecl);
1530 }
1531 if (DestructedBases.empty())
1532 return;
1533 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1534 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001535 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1536 ClassDecl,BaseClassDecl,
1537 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001538 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1539 Dtor_Complete, V);
1540 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001541}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001542
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001543void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1544 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001545 llvm::Function *Fn,
1546 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001548 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001549 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1550 "SynthesizeDefaultDestructor - destructor has user declaration");
1551 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001553 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1554 SourceLocation());
1555 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001556 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001557}
Anders Carlsson6815e942009-09-27 18:58:34 +00001558
1559// FIXME: Move this to CGCXXStmt.cpp
1560void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1561 // FIXME: We need to do more here.
1562 EmitStmt(S.getTryBlock());
1563}