blob: 3960cf51868f86dca9b1a559d06313c0555a9c69 [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump1eb44332009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlssone1b29ef2008-08-22 16:00:37 +000015
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson283a0622009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000020#include "clang/AST/RecordLayout.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000021#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson6815e942009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000026using namespace clang;
27using namespace CodeGen;
28
Mike Stump1eb44332009-09-09 15:08:12 +000029void
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000030CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor,
31 llvm::Constant *DeclPtr) {
Anders Carlsson6815e942009-09-27 18:58:34 +000032 const llvm::Type *Int8PtrTy =
33 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
Mike Stump1eb44332009-09-09 15:08:12 +000034
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000035 std::vector<const llvm::Type *> Params;
36 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000038 // Get the destructor function type
Mike Stump1eb44332009-09-09 15:08:12 +000039 const llvm::Type *DtorFnTy =
Owen Anderson0032b272009-08-13 21:57:51 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000041 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump1eb44332009-09-09 15:08:12 +000042
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000043 Params.clear();
44 Params.push_back(DtorFnTy);
45 Params.push_back(Int8PtrTy);
46 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000048 // Get the __cxa_atexit function type
49 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Mike Stump1eb44332009-09-09 15:08:12 +000050 const llvm::FunctionType *AtExitFnTy =
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000051 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000053 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
54 "__cxa_atexit");
Mike Stump1eb44332009-09-09 15:08:12 +000055
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000056 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
57 "__dso_handle");
Mike Stump1eb44332009-09-09 15:08:12 +000058
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000059 llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000061 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
62 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
63 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
64 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
65}
66
Mike Stump1eb44332009-09-09 15:08:12 +000067void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000068 llvm::Constant *DeclPtr) {
69 assert(D.hasGlobalStorage() &&
70 "VarDecl must have global storage!");
Mike Stump1eb44332009-09-09 15:08:12 +000071
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000072 const Expr *Init = D.getInit();
73 QualType T = D.getType();
Mike Stump1eb44332009-09-09 15:08:12 +000074
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000075 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000076 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000077 } else if (!hasAggregateLLVMType(T)) {
78 llvm::Value *V = EmitScalarExpr(Init);
79 EmitStoreOfScalar(V, DeclPtr, T.isVolatileQualified(), T);
80 } else if (T->isAnyComplexType()) {
81 EmitComplexExprIntoAddr(Init, DeclPtr, T.isVolatileQualified());
82 } else {
83 EmitAggExpr(Init, DeclPtr, T.isVolatileQualified());
Mike Stump1eb44332009-09-09 15:08:12 +000084
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000085 if (const RecordType *RT = T->getAs<RecordType>()) {
86 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
87 if (!RD->hasTrivialDestructor())
88 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr);
89 }
90 }
91}
92
Anders Carlsson89ed31d2009-08-08 23:24:23 +000093void
94CodeGenModule::EmitCXXGlobalInitFunc() {
95 if (CXXGlobalInits.empty())
96 return;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Owen Anderson0032b272009-08-13 21:57:51 +000098 const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Anders Carlsson89ed31d2009-08-08 23:24:23 +000099 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000101 // Create our global initialization function.
102 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000103 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000104 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
105 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000107 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000108 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000109 CXXGlobalInits.size());
110 AddGlobalCtor(Fn);
111}
112
113void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
114 const VarDecl **Decls,
115 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000116 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000117 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000119 for (unsigned i = 0; i != NumDecls; ++i) {
120 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000122 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
123 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
124 }
125 FinishFunction();
126}
127
Mike Stump1eb44332009-09-09 15:08:12 +0000128void
129CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000130 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000131 // FIXME: This should use __cxa_guard_{acquire,release}?
132
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000133 assert(!getContext().getLangOptions().ThreadsafeStatics &&
134 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000135
Anders Carlsson283a0622009-04-13 18:03:33 +0000136 llvm::SmallString<256> GuardVName;
137 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000138 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000140 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000141 llvm::GlobalValue *GuardV =
Owen Anderson0032b272009-08-13 21:57:51 +0000142 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000143 GV->getLinkage(),
Owen Anderson0032b272009-08-13 21:57:51 +0000144 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000145 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000147 // Load the first byte of the guard variable.
Owen Anderson0032b272009-08-13 21:57:51 +0000148 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000149 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000150 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000152 // Compare it against 0.
Owen Anderson0032b272009-08-13 21:57:51 +0000153 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000154 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbar55e87422008-11-11 02:29:29 +0000156 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000157 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000158
159 // If the guard variable is 0, jump to the initializer code.
160 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000162 EmitBlock(InitBlock);
163
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000164 EmitCXXGlobalVarDeclInit(D, GV);
165
Owen Anderson0032b272009-08-13 21:57:51 +0000166 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000167 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000169 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170}
171
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000172RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
173 llvm::Value *Callee,
174 llvm::Value *This,
175 CallExpr::const_arg_iterator ArgBeg,
176 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000177 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000178 "Trying to emit a member call expr on a static method!");
179
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000180 // A call to a trivial destructor requires no code generation.
181 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
182 if (Destructor->isTrivial())
183 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
John McCall183700f2009-09-21 23:43:11 +0000185 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000187 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000189 // Push the this ptr.
190 Args.push_back(std::make_pair(RValue::get(This),
191 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000193 // And the rest of the call args
194 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
John McCall183700f2009-09-21 23:43:11 +0000196 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000197 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
198 Callee, Args, MD);
199}
200
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000201/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
202/// expr can be devirtualized.
203static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
204 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
205 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
206 // This is a record decl. We know the type and can devirtualize it.
207 return VD->getType()->isRecordType();
208 }
Anders Carlsson76366482009-10-12 19:45:47 +0000209
210 return false;
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000211 }
212
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000213 // We can always devirtualize calls on temporary object expressions.
Anders Carlsson76366482009-10-12 19:45:47 +0000214 if (isa<CXXTemporaryObjectExpr>(Base))
215 return true;
216
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000217 // And calls on bound temporaries.
218 if (isa<CXXBindTemporaryExpr>(Base))
219 return true;
220
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000221 // Check if this is a call expr that returns a record type.
222 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
223 return CE->getCallReturnType()->isRecordType();
224
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000225 // We can't devirtualize the call.
226 return false;
227}
228
Anders Carlsson774e7c62009-04-03 22:50:24 +0000229RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000230 if (isa<BinaryOperator>(CE->getCallee()))
231 return EmitCXXMemberPointerCallExpr(CE);
232
Anders Carlsson774e7c62009-04-03 22:50:24 +0000233 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
234 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000235
Anders Carlsson2472bf02009-09-29 03:54:11 +0000236 if (MD->isStatic()) {
237 // The method is static, emit it as we would a regular call.
238 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
239 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
240 CE->arg_begin(), CE->arg_end(), 0);
241
242 }
243
John McCall183700f2009-09-21 23:43:11 +0000244 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000245
Mike Stump1eb44332009-09-09 15:08:12 +0000246 const llvm::Type *Ty =
247 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000248 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000249 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Anders Carlsson774e7c62009-04-03 22:50:24 +0000251 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000252 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000253 else {
254 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000255 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000256 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000257
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000258 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000259 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000260 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000261 //
262 // We also don't emit a virtual call if the base expression has a record type
263 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000264 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000265 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000266 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000267 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000268 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000269 = dyn_cast<CXXDestructorDecl>(MD))
270 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000271 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000272 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000273
274 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000275 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000276}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000277
Mike Stump1eb44332009-09-09 15:08:12 +0000278RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000279CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
280 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson3eea6352009-10-13 17:41:28 +0000281 const Expr *BaseExpr = BO->getLHS();
282 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000283
Anders Carlsson3eea6352009-10-13 17:41:28 +0000284 const MemberPointerType *MPT =
285 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000286 const FunctionProtoType *FPT =
287 MPT->getPointeeType()->getAs<FunctionProtoType>();
288 const CXXRecordDecl *RD =
289 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl());
290
291 const llvm::FunctionType *FTy =
292 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
293 FPT->isVariadic());
294
295 const llvm::Type *Int8PtrTy =
296 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
297
298 // Get the member function pointer.
299 llvm::Value *MemFnPtr =
Anders Carlsson3eea6352009-10-13 17:41:28 +0000300 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
301 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000302
303 // Emit the 'this' pointer.
304 llvm::Value *This;
305
306 if (BO->getOpcode() == BinaryOperator::PtrMemI)
307 This = EmitScalarExpr(BaseExpr);
308 else
309 This = EmitLValue(BaseExpr).getAddress();
310
311 // Adjust it.
312 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
313 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
314
315 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
316 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
317
318 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
319
320 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
321
322 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
323
324 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
325
326 // If the LSB in the function pointer is 1, the function pointer points to
327 // a virtual function.
328 llvm::Value *IsVirtual
329 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
330 "and");
331
332 IsVirtual = Builder.CreateTrunc(IsVirtual,
333 llvm::Type::getInt1Ty(VMContext));
334
335 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
336 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
337 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
338
339 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
340 EmitBlock(FnVirtual);
341
342 const llvm::Type *VTableTy =
343 FTy->getPointerTo()->getPointerTo()->getPointerTo();
344
345 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
346 VTable = Builder.CreateLoad(VTable);
347
348 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
349
350 // Since the function pointer is 1 plus the virtual table offset, we
351 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000352 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000353
354 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
355
356 EmitBranch(FnEnd);
357 EmitBlock(FnNonVirtual);
358
359 // If the function is not virtual, just load the pointer.
360 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
361 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
362
363 EmitBlock(FnEnd);
364
365 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
366 Callee->reserveOperandSpace(2);
367 Callee->addIncoming(VirtualFn, FnVirtual);
368 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
369
370 CallArgList Args;
371
372 QualType ThisType =
373 getContext().getPointerType(getContext().getTagDeclType(RD));
374
375 // Push the this ptr.
376 Args.push_back(std::make_pair(RValue::get(This), ThisType));
377
378 // And the rest of the call args
379 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
380 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
381 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
382 Callee, Args, 0);
383}
384
385RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000386CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
387 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000388 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000389 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Fariborz Jahanianad258832009-08-13 21:09:41 +0000391 if (MD->isCopyAssignment()) {
392 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
393 if (ClassDecl->hasTrivialCopyAssignment()) {
394 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
395 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
396 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
397 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
398 QualType Ty = E->getType();
399 EmitAggregateCopy(This, Src, Ty);
400 return RValue::get(This);
401 }
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
John McCall183700f2009-09-21 23:43:11 +0000404 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000405 const llvm::Type *Ty =
406 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000407 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000408 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Anders Carlsson0f294632009-05-27 04:18:27 +0000410 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Anders Carlsson0f294632009-05-27 04:18:27 +0000412 return EmitCXXMemberCall(MD, Callee, This,
413 E->arg_begin() + 1, E->arg_end());
414}
415
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000416llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000417 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000418 "Must be in a C++ member function decl to load 'this'");
419 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
420 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000422 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000423 // ans: See how CodeGenFunction::LoadObjCSelf() uses
424 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000425 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
426}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000427
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000428/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
429/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000430/// array.
431/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
432/// array type and 'ArrayPtr' points to the beginning fo the array.
433/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000434void
435CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000436 const ConstantArrayType *ArrayTy,
437 llvm::Value *ArrayPtr) {
438 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
439 llvm::Value * NumElements =
440 llvm::ConstantInt::get(SizeTy,
441 getContext().getConstantArrayElementCount(ArrayTy));
442
443 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
444}
445
446void
447CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
448 llvm::Value *NumElements,
449 llvm::Value *ArrayPtr) {
450 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000452 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000453 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
454 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
455 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000457 // Start the loop with a block that tests the condition.
458 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
459 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000461 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000463 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000465 // Generate: if (loop-index < number-of-elements fall to the loop body,
466 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000467 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000468 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000469 // If the condition is true, execute the body.
470 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000472 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000474 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000475 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000476 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000477 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
478 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000479 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000481 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000483 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000484 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000485 Counter = Builder.CreateLoad(IndexPtr);
486 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
487 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000489 // Finally, branch back up to the condition for the next iteration.
490 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000492 // Emit the fall-through block.
493 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000494}
495
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000496/// EmitCXXAggrDestructorCall - calls the default destructor on array
497/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000498void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000499CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
500 const ArrayType *Array,
501 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000502 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
503 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000504 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000505 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000506 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000507 // Create a temporary for the loop index and initialize it with count of
508 // array elements.
509 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
510 "loop.index");
511 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000512 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000513 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
514 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000516 // Start the loop with a block that tests the condition.
517 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
518 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000520 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000522 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000524 // Generate: if (loop-index != 0 fall to the loop body,
525 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000526 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000527 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
528 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
529 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
530 "isne");
531 // If the condition is true, execute the body.
532 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000534 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000536 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
537 // Inside the loop body, emit the constructor call on the array element.
538 Counter = Builder.CreateLoad(IndexPtr);
539 Counter = Builder.CreateSub(Counter, One);
540 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
541 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000543 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000545 // Emit the decrement of the loop counter.
546 Counter = Builder.CreateLoad(IndexPtr);
547 Counter = Builder.CreateSub(Counter, One, "dec");
548 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000550 // Finally, branch back up to the condition for the next iteration.
551 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000553 // Emit the fall-through block.
554 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000555}
556
557void
Mike Stump1eb44332009-09-09 15:08:12 +0000558CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
559 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000560 llvm::Value *This,
561 CallExpr::const_arg_iterator ArgBeg,
562 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000563 if (D->isCopyConstructor(getContext())) {
564 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
565 if (ClassDecl->hasTrivialCopyConstructor()) {
566 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
567 "EmitCXXConstructorCall - user declared copy constructor");
568 const Expr *E = (*ArgBeg);
569 QualType Ty = E->getType();
570 llvm::Value *Src = EmitLValue(E).getAddress();
571 EmitAggregateCopy(This, Src, Ty);
572 return;
573 }
574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000576 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
577
578 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000579}
580
Mike Stump1eb44332009-09-09 15:08:12 +0000581void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000582 CXXDtorType Type,
583 llvm::Value *This) {
584 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Anders Carlsson7267c162009-05-29 21:03:38 +0000586 EmitCXXMemberCall(D, Callee, This, 0, 0);
587}
588
Mike Stump1eb44332009-09-09 15:08:12 +0000589void
590CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000591 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000592 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000593
594 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000595 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000596 if (RD->hasTrivialConstructor())
597 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000598
Mike Stump1eb44332009-09-09 15:08:12 +0000599 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000600 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000601 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000602 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000603 EmitAggExpr((*i), Dest, false);
604 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000605 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000606 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000607 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000608 E->arg_begin(), E->arg_end());
609}
610
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000611void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000612 EmitGlobal(GlobalDecl(D, Ctor_Complete));
613 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000614}
Anders Carlsson363c1842009-04-16 23:57:24 +0000615
Mike Stump1eb44332009-09-09 15:08:12 +0000616void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000617 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Anders Carlsson27ae5362009-04-17 01:58:57 +0000619 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000621 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Anders Carlsson27ae5362009-04-17 01:58:57 +0000623 SetFunctionDefinitionAttributes(D, Fn);
624 SetLLVMFunctionAttributesForDefinition(D, Fn);
625}
626
Anders Carlsson363c1842009-04-16 23:57:24 +0000627llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000628CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000629 CXXCtorType Type) {
630 const llvm::FunctionType *FTy =
631 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Anders Carlsson363c1842009-04-16 23:57:24 +0000633 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000634 return cast<llvm::Function>(
635 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000636}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000637
Mike Stump1eb44332009-09-09 15:08:12 +0000638const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000639 CXXCtorType Type) {
640 llvm::SmallString<256> Name;
641 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000642 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Anders Carlsson27ae5362009-04-17 01:58:57 +0000644 Name += '\0';
645 return UniqueMangledName(Name.begin(), Name.end());
646}
647
648void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000649 EmitCXXDestructor(D, Dtor_Complete);
650 EmitCXXDestructor(D, Dtor_Base);
651}
652
Mike Stump1eb44332009-09-09 15:08:12 +0000653void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000654 CXXDtorType Type) {
655 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000657 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Anders Carlsson27ae5362009-04-17 01:58:57 +0000659 SetFunctionDefinitionAttributes(D, Fn);
660 SetLLVMFunctionAttributesForDefinition(D, Fn);
661}
662
663llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000664CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000665 CXXDtorType Type) {
666 const llvm::FunctionType *FTy =
667 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Anders Carlsson27ae5362009-04-17 01:58:57 +0000669 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000670 return cast<llvm::Function>(
671 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000672}
673
Mike Stump1eb44332009-09-09 15:08:12 +0000674const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000675 CXXDtorType Type) {
676 llvm::SmallString<256> Name;
677 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000678 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Anders Carlsson27ae5362009-04-17 01:58:57 +0000680 Name += '\0';
681 return UniqueMangledName(Name.begin(), Name.end());
682}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000683
Mike Stumped032eb2009-09-04 18:27:16 +0000684llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
685 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000686 bool Extern, int64_t nv,
687 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +0000688 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +0000689
690 FunctionArgList Args;
691 ImplicitParamDecl *ThisDecl =
692 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
693 MD->getThisType(getContext()));
694 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
695 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
696 e = MD->param_end();
697 i != e; ++i) {
698 ParmVarDecl *D = *i;
699 Args.push_back(std::make_pair(D, D->getType()));
700 }
701 IdentifierInfo *II
702 = &CGM.getContext().Idents.get("__thunk_named_foo_");
703 FunctionDecl *FD = FunctionDecl::Create(getContext(),
704 getContext().getTranslationUnitDecl(),
705 SourceLocation(), II, R, 0,
706 Extern
707 ? FunctionDecl::Extern
708 : FunctionDecl::Static,
709 false, true);
710 StartFunction(FD, R, Fn, Args, SourceLocation());
711 // FIXME: generate body
712 FinishFunction();
713 return Fn;
714}
715
Mike Stump6e319f62009-09-11 23:25:56 +0000716llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
717 const CXXMethodDecl *MD,
718 bool Extern,
719 int64_t nv_t,
720 int64_t v_t,
721 int64_t nv_r,
722 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +0000723 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000724
725 FunctionArgList Args;
726 ImplicitParamDecl *ThisDecl =
727 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
728 MD->getThisType(getContext()));
729 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
730 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
731 e = MD->param_end();
732 i != e; ++i) {
733 ParmVarDecl *D = *i;
734 Args.push_back(std::make_pair(D, D->getType()));
735 }
736 IdentifierInfo *II
737 = &CGM.getContext().Idents.get("__thunk_named_foo_");
738 FunctionDecl *FD = FunctionDecl::Create(getContext(),
739 getContext().getTranslationUnitDecl(),
740 SourceLocation(), II, R, 0,
741 Extern
742 ? FunctionDecl::Extern
743 : FunctionDecl::Static,
744 false, true);
745 StartFunction(FD, R, Fn, Args, SourceLocation());
746 // FIXME: generate body
747 FinishFunction();
748 return Fn;
749}
750
Mike Stump77ca8f62009-09-05 07:20:32 +0000751llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
752 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000753 llvm::SmallString<256> OutName;
754 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000755 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000756 llvm::GlobalVariable::LinkageTypes linktype;
757 linktype = llvm::GlobalValue::WeakAnyLinkage;
758 if (!Extern)
759 linktype = llvm::GlobalValue::InternalLinkage;
760 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000761 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000762 const llvm::FunctionType *FTy =
763 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
764 FPT->isVariadic());
765
766 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
767 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000768 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000769 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
770 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
771 return m;
772}
773
Mike Stump6e319f62009-09-11 23:25:56 +0000774llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
775 bool Extern, int64_t nv_t,
776 int64_t v_t, int64_t nv_r,
777 int64_t v_r) {
778 llvm::SmallString<256> OutName;
779 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000780 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000781 llvm::GlobalVariable::LinkageTypes linktype;
782 linktype = llvm::GlobalValue::WeakAnyLinkage;
783 if (!Extern)
784 linktype = llvm::GlobalValue::InternalLinkage;
785 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000786 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000787 const llvm::FunctionType *FTy =
788 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
789 FPT->isVariadic());
790
791 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
792 &getModule());
793 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
794 v_r);
795 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
796 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
797 return m;
798}
799
Mike Stumpf0070db2009-08-26 20:46:33 +0000800llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000801CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
802 const CXXRecordDecl *ClassDecl,
803 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000804 const llvm::Type *Int8PtrTy =
805 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
806
807 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
808 Int8PtrTy->getPointerTo());
809 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
810
Anders Carlssondbd920c2009-10-11 22:13:54 +0000811 int64_t VBaseOffsetIndex =
812 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
813
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000814 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000815 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000816 const llvm::Type *PtrDiffTy =
817 ConvertType(getContext().getPointerDiffType());
818
819 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
820 PtrDiffTy->getPointerTo());
821
822 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
823
824 return VBaseOffset;
825}
826
827llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000828CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
829 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000830 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000831
Mike Stumpf0070db2009-08-26 20:46:33 +0000832 Ty = llvm::PointerType::get(Ty, 0);
833 Ty = llvm::PointerType::get(Ty, 0);
834 Ty = llvm::PointerType::get(Ty, 0);
835 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
836 vtbl = Builder.CreateLoad(vtbl);
837 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000838 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000839 vfn = Builder.CreateLoad(vfn);
840 return vfn;
841}
842
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000843/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
844/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
845/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000846// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000847void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000848 llvm::Value *Src,
849 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000850 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000851 QualType Ty) {
852 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
853 assert(CA && "VLA cannot be copied over");
854 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000856 // Create a temporary for the loop index and initialize it with 0.
857 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
858 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000859 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000860 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000861 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000862 // Start the loop with a block that tests the condition.
863 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
864 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000866 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000868 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
869 // Generate: if (loop-index < number-of-elements fall to the loop body,
870 // otherwise, go to the block after the for-loop.
871 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000872 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000873 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
874 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000875 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000876 "isless");
877 // If the condition is true, execute the body.
878 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000880 EmitBlock(ForBody);
881 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
882 // Inside the loop body, emit the constructor call on the array element.
883 Counter = Builder.CreateLoad(IndexPtr);
884 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
885 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
886 if (BitwiseCopy)
887 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000888 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000889 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000890 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000891 Ctor_Complete);
892 CallArgList CallArgs;
893 // Push the this (Dest) ptr.
894 CallArgs.push_back(std::make_pair(RValue::get(Dest),
895 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000897 // Push the Src ptr.
898 CallArgs.push_back(std::make_pair(RValue::get(Src),
899 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000900 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000901 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000902 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
903 Callee, CallArgs, BaseCopyCtor);
904 }
905 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000907 // Emit the increment of the loop counter.
908 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
909 Counter = Builder.CreateLoad(IndexPtr);
910 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
911 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000913 // Finally, branch back up to the condition for the next iteration.
914 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000916 // Emit the fall-through block.
917 EmitBlock(AfterFor, true);
918}
919
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000920/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000921/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000922/// bitwise assignment or via a copy assignment operator function call.
923/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000924void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000925 llvm::Value *Src,
926 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000927 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000928 QualType Ty) {
929 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
930 assert(CA && "VLA cannot be asssigned");
931 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000933 // Create a temporary for the loop index and initialize it with 0.
934 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
935 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000936 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000937 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
938 Builder.CreateStore(zeroConstant, IndexPtr, false);
939 // Start the loop with a block that tests the condition.
940 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
941 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000943 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000945 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
946 // Generate: if (loop-index < number-of-elements fall to the loop body,
947 // otherwise, go to the block after the for-loop.
948 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000949 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000950 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
951 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000952 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000953 "isless");
954 // If the condition is true, execute the body.
955 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000957 EmitBlock(ForBody);
958 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
959 // Inside the loop body, emit the assignment operator call on array element.
960 Counter = Builder.CreateLoad(IndexPtr);
961 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
962 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
963 const CXXMethodDecl *MD = 0;
964 if (BitwiseAssign)
965 EmitAggregateCopy(Dest, Src, Ty);
966 else {
967 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
968 MD);
969 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
970 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +0000971 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000972 const llvm::Type *LTy =
973 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
974 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000975 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000977 CallArgList CallArgs;
978 // Push the this (Dest) ptr.
979 CallArgs.push_back(std::make_pair(RValue::get(Dest),
980 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000982 // Push the Src ptr.
983 CallArgs.push_back(std::make_pair(RValue::get(Src),
984 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +0000985 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000986 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
987 Callee, CallArgs, MD);
988 }
989 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000991 // Emit the increment of the loop counter.
992 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
993 Counter = Builder.CreateLoad(IndexPtr);
994 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
995 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000997 // Finally, branch back up to the condition for the next iteration.
998 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001000 // Emit the fall-through block.
1001 EmitBlock(AfterFor, true);
1002}
1003
Fariborz Jahanianca283612009-08-07 23:51:33 +00001004/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1005/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001006/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001007void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001008 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001009 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001010 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1011 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001012 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1013 /*NullCheckValue=*/false);
1014 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1015 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001016 }
1017 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1018 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001019 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001020 }
Mike Stump1eb44332009-09-09 15:08:12 +00001021
1022 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001023 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001024 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001025 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001026 CallArgList CallArgs;
1027 // Push the this (Dest) ptr.
1028 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1029 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Fariborz Jahanianca283612009-08-07 23:51:33 +00001031 // Push the Src ptr.
1032 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001033 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001034 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001035 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001036 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1037 Callee, CallArgs, BaseCopyCtor);
1038 }
1039}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001040
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001041/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001042/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001043/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001044// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001045void CodeGenFunction::EmitClassCopyAssignment(
1046 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001047 const CXXRecordDecl *ClassDecl,
1048 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001049 QualType Ty) {
1050 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001051 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1052 /*NullCheckValue=*/false);
1053 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1054 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001055 }
1056 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1057 EmitAggregateCopy(Dest, Src, Ty);
1058 return;
1059 }
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001061 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001062 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001063 MD);
1064 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1065 (void)ConstCopyAssignOp;
1066
John McCall183700f2009-09-21 23:43:11 +00001067 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001068 const llvm::Type *LTy =
1069 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001070 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001071 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001073 CallArgList CallArgs;
1074 // Push the this (Dest) ptr.
1075 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1076 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001078 // Push the Src ptr.
1079 CallArgs.push_back(std::make_pair(RValue::get(Src),
1080 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001081 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001082 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001083 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1084 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001085}
1086
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001087/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001088void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001089CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1090 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001091 llvm::Function *Fn,
1092 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001093 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1094 SourceLocation());
1095 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001096 FinishFunction();
1097}
1098
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001099/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001100/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001101/// The implicitly-defined copy constructor for class X performs a memberwise
1102/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001103/// of initialization of bases and members in a user-defined constructor
1104/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001105/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001106/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001107/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001108/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001109/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001110/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001111/// Virtual base class subobjects shall be copied only once by the
1112/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001113
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001114void
1115CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1116 CXXCtorType Type,
1117 llvm::Function *Fn,
1118 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001119 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001120 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001121 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001122 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1123 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001125 FunctionArgList::const_iterator i = Args.begin();
1126 const VarDecl *ThisArg = i->first;
1127 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1128 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1129 const VarDecl *SrcArg = (i+1)->first;
1130 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1131 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001133 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1134 Base != ClassDecl->bases_end(); ++Base) {
1135 // FIXME. copy constrution of virtual base NYI
1136 if (Base->isVirtual())
1137 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001139 CXXRecordDecl *BaseClassDecl
1140 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001141 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1142 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001145 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1146 FieldEnd = ClassDecl->field_end();
1147 Field != FieldEnd; ++Field) {
1148 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001149 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001150 getContext().getAsConstantArrayType(FieldType);
1151 if (Array)
1152 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001154 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1155 CXXRecordDecl *FieldClassDecl
1156 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1157 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1158 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001159 if (Array) {
1160 const llvm::Type *BasePtr = ConvertType(FieldType);
1161 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001162 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001163 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001164 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001165 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1166 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1167 FieldClassDecl, FieldType);
1168 }
Mike Stump1eb44332009-09-09 15:08:12 +00001169 else
1170 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001171 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001172 continue;
1173 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001174 // Do a built-in assignment of scalar data members.
1175 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1176 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1177 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1178 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001179 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001180 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001181}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001182
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001183/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001184/// Before the implicitly-declared copy assignment operator for a class is
1185/// implicitly defined, all implicitly- declared copy assignment operators for
1186/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001187/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001188/// The implicitly-defined copy assignment operator for class X performs
1189/// memberwise assignment of its subob- jects. The direct base classes of X are
1190/// assigned first, in the order of their declaration in
1191/// the base-specifier-list, and then the immediate nonstatic data members of X
1192/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001193/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001194/// if the subobject is of class type, the copy assignment operator for the
1195/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001196/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001197///
Mike Stump1eb44332009-09-09 15:08:12 +00001198/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001199/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001200///
Mike Stump1eb44332009-09-09 15:08:12 +00001201/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001202/// used.
1203void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001204 llvm::Function *Fn,
1205 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001206
1207 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1208 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1209 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001210 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001212 FunctionArgList::const_iterator i = Args.begin();
1213 const VarDecl *ThisArg = i->first;
1214 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1215 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1216 const VarDecl *SrcArg = (i+1)->first;
1217 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1218 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001220 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1221 Base != ClassDecl->bases_end(); ++Base) {
1222 // FIXME. copy assignment of virtual base NYI
1223 if (Base->isVirtual())
1224 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001226 CXXRecordDecl *BaseClassDecl
1227 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1228 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1229 Base->getType());
1230 }
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001232 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1233 FieldEnd = ClassDecl->field_end();
1234 Field != FieldEnd; ++Field) {
1235 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001236 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001237 getContext().getAsConstantArrayType(FieldType);
1238 if (Array)
1239 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001241 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1242 CXXRecordDecl *FieldClassDecl
1243 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1244 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1245 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001246 if (Array) {
1247 const llvm::Type *BasePtr = ConvertType(FieldType);
1248 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1249 llvm::Value *DestBaseAddrPtr =
1250 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1251 llvm::Value *SrcBaseAddrPtr =
1252 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1253 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1254 FieldClassDecl, FieldType);
1255 }
1256 else
Mike Stump1eb44332009-09-09 15:08:12 +00001257 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001258 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001259 continue;
1260 }
1261 // Do a built-in assignment of scalar data members.
1262 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1263 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1264 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1265 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001266 }
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001268 // return *this;
1269 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001271 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001272}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001273
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001274/// EmitCtorPrologue - This routine generates necessary code to initialize
1275/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001276/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001277void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1278 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001279 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001280 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001281 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001283 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001284 E = CD->init_end();
1285 B != E; ++B) {
1286 CXXBaseOrMemberInitializer *Member = (*B);
1287 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001288 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001289 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001290 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001291 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001292 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1293 BaseClassDecl,
1294 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001295 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001296 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001297 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001298 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001299 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001300 // non-static data member initilaizers.
1301 FieldDecl *Field = Member->getMember();
1302 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001303 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001304 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001305 if (Array)
1306 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Mike Stumpf1216772009-07-31 18:25:34 +00001308 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001309 LValue LHS;
1310 if (FieldType->isReferenceType()) {
1311 // FIXME: This is really ugly; should be refactored somehow
1312 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1313 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001314 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1315 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001316 } else {
1317 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1318 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001319 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001320 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001321 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001322 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001323 if (Array) {
1324 const llvm::Type *BasePtr = ConvertType(FieldType);
1325 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001326 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001327 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001328 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001329 Array, BaseAddrPtr);
1330 }
1331 else
1332 EmitCXXConstructorCall(Member->getConstructor(),
1333 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001334 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001335 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001336 continue;
1337 }
1338 else {
1339 // Initializing an anonymous union data member.
1340 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001341 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001342 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001343 FieldType = anonMember->getType();
1344 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001345 }
Mike Stump1eb44332009-09-09 15:08:12 +00001346
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001347 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001348 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001349 RValue RHS;
1350 if (FieldType->isReferenceType())
1351 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1352 /*IsInitializer=*/true);
1353 else
1354 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1355 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001356 }
1357 }
Mike Stumpf1216772009-07-31 18:25:34 +00001358
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001359 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001360 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001361 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001362 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001363 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001364 ClassDecl->bases_begin();
1365 Base != ClassDecl->bases_end(); ++Base) {
1366 // FIXME. copy assignment of virtual base NYI
1367 if (Base->isVirtual())
1368 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001370 CXXRecordDecl *BaseClassDecl
1371 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1372 if (BaseClassDecl->hasTrivialConstructor())
1373 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001374 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001375 BaseClassDecl->getDefaultConstructor(getContext())) {
1376 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001377 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1378 BaseClassDecl,
1379 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001380 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1381 }
1382 }
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001384 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1385 FieldEnd = ClassDecl->field_end();
1386 Field != FieldEnd; ++Field) {
1387 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001388 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001389 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001390 if (Array)
1391 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001392 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1393 continue;
1394 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001395 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001396 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1397 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1398 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001399 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001400 MemberClassDecl->getDefaultConstructor(getContext())) {
1401 LoadOfThis = LoadCXXThis();
1402 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001403 if (Array) {
1404 const llvm::Type *BasePtr = ConvertType(FieldType);
1405 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001406 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001407 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1408 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1409 }
1410 else
Mike Stump1eb44332009-09-09 15:08:12 +00001411 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001412 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001413 }
1414 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001415 }
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Mike Stumpf1216772009-07-31 18:25:34 +00001417 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001418 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001419 if (!LoadOfThis)
1420 LoadOfThis = LoadCXXThis();
1421 llvm::Value *VtableField;
1422 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001423 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001424 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1425 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1426 llvm::Value *vtable = GenerateVtable(ClassDecl);
1427 Builder.CreateStore(vtable, VtableField);
1428 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001429}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001430
1431/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001432/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001433/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001434/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001435void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1436 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001437 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001438 assert(!ClassDecl->getNumVBases() &&
1439 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001440 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001442 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1443 *E = DD->destr_end(); B != E; ++B) {
1444 uintptr_t BaseOrMember = (*B);
1445 if (DD->isMemberToDestroy(BaseOrMember)) {
1446 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1447 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001448 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001449 getContext().getAsConstantArrayType(FieldType);
1450 if (Array)
1451 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001452 const RecordType *RT = FieldType->getAs<RecordType>();
1453 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1454 if (FieldClassDecl->hasTrivialDestructor())
1455 continue;
1456 llvm::Value *LoadOfThis = LoadCXXThis();
1457 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001458 if (Array) {
1459 const llvm::Type *BasePtr = ConvertType(FieldType);
1460 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001461 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001462 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001463 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001464 Array, BaseAddrPtr);
1465 }
1466 else
1467 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1468 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001469 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001470 const RecordType *RT =
1471 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1472 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1473 if (BaseClassDecl->hasTrivialDestructor())
1474 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001475 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1476 ClassDecl, BaseClassDecl,
1477 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001478 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001479 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001480 }
1481 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001482 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1483 return;
1484 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001485 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001486 // reverse order of their construction.
1487 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001489 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1490 FieldEnd = ClassDecl->field_end();
1491 Field != FieldEnd; ++Field) {
1492 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001493 if (getContext().getAsConstantArrayType(FieldType))
1494 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001495 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1496 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1497 if (FieldClassDecl->hasTrivialDestructor())
1498 continue;
1499 DestructedFields.push_back(*Field);
1500 }
1501 }
1502 if (!DestructedFields.empty())
1503 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1504 FieldDecl *Field = DestructedFields[i];
1505 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001506 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001507 getContext().getAsConstantArrayType(FieldType);
1508 if (Array)
1509 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001510 const RecordType *RT = FieldType->getAs<RecordType>();
1511 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1512 llvm::Value *LoadOfThis = LoadCXXThis();
1513 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001514 if (Array) {
1515 const llvm::Type *BasePtr = ConvertType(FieldType);
1516 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001517 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001518 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001519 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001520 Array, BaseAddrPtr);
1521 }
1522 else
1523 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1524 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001527 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1528 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1529 Base != ClassDecl->bases_end(); ++Base) {
1530 // FIXME. copy assignment of virtual base NYI
1531 if (Base->isVirtual())
1532 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001534 CXXRecordDecl *BaseClassDecl
1535 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1536 if (BaseClassDecl->hasTrivialDestructor())
1537 continue;
1538 DestructedBases.push_back(BaseClassDecl);
1539 }
1540 if (DestructedBases.empty())
1541 return;
1542 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1543 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001544 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1545 ClassDecl,BaseClassDecl,
1546 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001547 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1548 Dtor_Complete, V);
1549 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001550}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001551
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001552void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1553 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001554 llvm::Function *Fn,
1555 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001557 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001558 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1559 "SynthesizeDefaultDestructor - destructor has user declaration");
1560 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001562 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1563 SourceLocation());
1564 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001565 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001566}
Anders Carlsson6815e942009-09-27 18:58:34 +00001567
1568// FIXME: Move this to CGCXXStmt.cpp
1569void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1570 // FIXME: We need to do more here.
1571 EmitStmt(S.getTryBlock());
1572}