blob: 818c5162daf705292cb0fe97d3798a0d44ded0ed [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!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000593 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000594 const ConstantArrayType *Array =
595 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000596 // For a copy constructor, even if it is trivial, must fall thru so
597 // its argument is code-gen'ed.
598 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000599 QualType InitType = E->getType();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000600 if (Array)
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000601 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000602 const CXXRecordDecl *RD =
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000603 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000604 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000605 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000608 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000609 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000610 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000611 EmitAggExpr((*i), Dest, false);
612 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000613 }
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000614 if (Array) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000615 QualType BaseElementTy = getContext().getBaseElementType(Array);
616 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
617 BasePtr = llvm::PointerType::getUnqual(BasePtr);
618 llvm::Value *BaseAddrPtr =
619 Builder.CreateBitCast(Dest, BasePtr);
620 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
621 }
622 else
623 // Call the constructor.
624 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
625 E->arg_begin(), E->arg_end());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000626}
627
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000628void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000629 EmitGlobal(GlobalDecl(D, Ctor_Complete));
630 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000631}
Anders Carlsson363c1842009-04-16 23:57:24 +0000632
Mike Stump1eb44332009-09-09 15:08:12 +0000633void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000634 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Anders Carlsson27ae5362009-04-17 01:58:57 +0000636 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000638 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Anders Carlsson27ae5362009-04-17 01:58:57 +0000640 SetFunctionDefinitionAttributes(D, Fn);
641 SetLLVMFunctionAttributesForDefinition(D, Fn);
642}
643
Anders Carlsson363c1842009-04-16 23:57:24 +0000644llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000645CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000646 CXXCtorType Type) {
647 const llvm::FunctionType *FTy =
648 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Anders Carlsson363c1842009-04-16 23:57:24 +0000650 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000651 return cast<llvm::Function>(
652 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000653}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000654
Mike Stump1eb44332009-09-09 15:08:12 +0000655const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000656 CXXCtorType Type) {
657 llvm::SmallString<256> Name;
658 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000659 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Anders Carlsson27ae5362009-04-17 01:58:57 +0000661 Name += '\0';
662 return UniqueMangledName(Name.begin(), Name.end());
663}
664
665void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000666 EmitCXXDestructor(D, Dtor_Complete);
667 EmitCXXDestructor(D, Dtor_Base);
668}
669
Mike Stump1eb44332009-09-09 15:08:12 +0000670void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000671 CXXDtorType Type) {
672 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000674 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Anders Carlsson27ae5362009-04-17 01:58:57 +0000676 SetFunctionDefinitionAttributes(D, Fn);
677 SetLLVMFunctionAttributesForDefinition(D, Fn);
678}
679
680llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000681CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000682 CXXDtorType Type) {
683 const llvm::FunctionType *FTy =
684 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Anders Carlsson27ae5362009-04-17 01:58:57 +0000686 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000687 return cast<llvm::Function>(
688 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000689}
690
Mike Stump1eb44332009-09-09 15:08:12 +0000691const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000692 CXXDtorType Type) {
693 llvm::SmallString<256> Name;
694 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000695 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Anders Carlsson27ae5362009-04-17 01:58:57 +0000697 Name += '\0';
698 return UniqueMangledName(Name.begin(), Name.end());
699}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000700
Mike Stumped032eb2009-09-04 18:27:16 +0000701llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
702 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000703 bool Extern, int64_t nv,
704 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +0000705 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +0000706
707 FunctionArgList Args;
708 ImplicitParamDecl *ThisDecl =
709 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
710 MD->getThisType(getContext()));
711 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
712 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
713 e = MD->param_end();
714 i != e; ++i) {
715 ParmVarDecl *D = *i;
716 Args.push_back(std::make_pair(D, D->getType()));
717 }
718 IdentifierInfo *II
719 = &CGM.getContext().Idents.get("__thunk_named_foo_");
720 FunctionDecl *FD = FunctionDecl::Create(getContext(),
721 getContext().getTranslationUnitDecl(),
722 SourceLocation(), II, R, 0,
723 Extern
724 ? FunctionDecl::Extern
725 : FunctionDecl::Static,
726 false, true);
727 StartFunction(FD, R, Fn, Args, SourceLocation());
728 // FIXME: generate body
729 FinishFunction();
730 return Fn;
731}
732
Mike Stump6e319f62009-09-11 23:25:56 +0000733llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
734 const CXXMethodDecl *MD,
735 bool Extern,
736 int64_t nv_t,
737 int64_t v_t,
738 int64_t nv_r,
739 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +0000740 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000741
742 FunctionArgList Args;
743 ImplicitParamDecl *ThisDecl =
744 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
745 MD->getThisType(getContext()));
746 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
747 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
748 e = MD->param_end();
749 i != e; ++i) {
750 ParmVarDecl *D = *i;
751 Args.push_back(std::make_pair(D, D->getType()));
752 }
753 IdentifierInfo *II
754 = &CGM.getContext().Idents.get("__thunk_named_foo_");
755 FunctionDecl *FD = FunctionDecl::Create(getContext(),
756 getContext().getTranslationUnitDecl(),
757 SourceLocation(), II, R, 0,
758 Extern
759 ? FunctionDecl::Extern
760 : FunctionDecl::Static,
761 false, true);
762 StartFunction(FD, R, Fn, Args, SourceLocation());
763 // FIXME: generate body
764 FinishFunction();
765 return Fn;
766}
767
Mike Stump77ca8f62009-09-05 07:20:32 +0000768llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
769 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000770 llvm::SmallString<256> OutName;
771 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000772 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000773 llvm::GlobalVariable::LinkageTypes linktype;
774 linktype = llvm::GlobalValue::WeakAnyLinkage;
775 if (!Extern)
776 linktype = llvm::GlobalValue::InternalLinkage;
777 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000778 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000779 const llvm::FunctionType *FTy =
780 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
781 FPT->isVariadic());
782
783 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
784 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000785 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000786 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
787 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
788 return m;
789}
790
Mike Stump6e319f62009-09-11 23:25:56 +0000791llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
792 bool Extern, int64_t nv_t,
793 int64_t v_t, int64_t nv_r,
794 int64_t v_r) {
795 llvm::SmallString<256> OutName;
796 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000797 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000798 llvm::GlobalVariable::LinkageTypes linktype;
799 linktype = llvm::GlobalValue::WeakAnyLinkage;
800 if (!Extern)
801 linktype = llvm::GlobalValue::InternalLinkage;
802 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000803 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000804 const llvm::FunctionType *FTy =
805 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
806 FPT->isVariadic());
807
808 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
809 &getModule());
810 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
811 v_r);
812 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
813 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
814 return m;
815}
816
Mike Stumpf0070db2009-08-26 20:46:33 +0000817llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000818CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
819 const CXXRecordDecl *ClassDecl,
820 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000821 const llvm::Type *Int8PtrTy =
822 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
823
824 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
825 Int8PtrTy->getPointerTo());
826 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
827
Anders Carlssondbd920c2009-10-11 22:13:54 +0000828 int64_t VBaseOffsetIndex =
829 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
830
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000831 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000832 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000833 const llvm::Type *PtrDiffTy =
834 ConvertType(getContext().getPointerDiffType());
835
836 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
837 PtrDiffTy->getPointerTo());
838
839 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
840
841 return VBaseOffset;
842}
843
844llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000845CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
846 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000847 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000848
Mike Stumpf0070db2009-08-26 20:46:33 +0000849 Ty = llvm::PointerType::get(Ty, 0);
850 Ty = llvm::PointerType::get(Ty, 0);
851 Ty = llvm::PointerType::get(Ty, 0);
852 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
853 vtbl = Builder.CreateLoad(vtbl);
854 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000855 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000856 vfn = Builder.CreateLoad(vfn);
857 return vfn;
858}
859
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000860/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
861/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
862/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000863// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000864void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000865 llvm::Value *Src,
866 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000867 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000868 QualType Ty) {
869 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
870 assert(CA && "VLA cannot be copied over");
871 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000873 // Create a temporary for the loop index and initialize it with 0.
874 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
875 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000876 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000877 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000878 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000879 // Start the loop with a block that tests the condition.
880 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
881 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000883 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000885 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
886 // Generate: if (loop-index < number-of-elements fall to the loop body,
887 // otherwise, go to the block after the for-loop.
888 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000889 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000890 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
891 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000892 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000893 "isless");
894 // If the condition is true, execute the body.
895 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000897 EmitBlock(ForBody);
898 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
899 // Inside the loop body, emit the constructor call on the array element.
900 Counter = Builder.CreateLoad(IndexPtr);
901 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
902 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
903 if (BitwiseCopy)
904 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000905 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000906 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000907 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000908 Ctor_Complete);
909 CallArgList CallArgs;
910 // Push the this (Dest) ptr.
911 CallArgs.push_back(std::make_pair(RValue::get(Dest),
912 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000914 // Push the Src ptr.
915 CallArgs.push_back(std::make_pair(RValue::get(Src),
916 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000917 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000918 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000919 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
920 Callee, CallArgs, BaseCopyCtor);
921 }
922 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000924 // Emit the increment of the loop counter.
925 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
926 Counter = Builder.CreateLoad(IndexPtr);
927 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
928 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000930 // Finally, branch back up to the condition for the next iteration.
931 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000933 // Emit the fall-through block.
934 EmitBlock(AfterFor, true);
935}
936
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000937/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000938/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000939/// bitwise assignment or via a copy assignment operator function call.
940/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000941void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000942 llvm::Value *Src,
943 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000944 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000945 QualType Ty) {
946 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
947 assert(CA && "VLA cannot be asssigned");
948 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000950 // Create a temporary for the loop index and initialize it with 0.
951 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
952 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000953 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000954 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
955 Builder.CreateStore(zeroConstant, IndexPtr, false);
956 // Start the loop with a block that tests the condition.
957 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
958 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000960 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000962 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
963 // Generate: if (loop-index < number-of-elements fall to the loop body,
964 // otherwise, go to the block after the for-loop.
965 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000966 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000967 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
968 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000969 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000970 "isless");
971 // If the condition is true, execute the body.
972 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000974 EmitBlock(ForBody);
975 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
976 // Inside the loop body, emit the assignment operator call on array element.
977 Counter = Builder.CreateLoad(IndexPtr);
978 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
979 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
980 const CXXMethodDecl *MD = 0;
981 if (BitwiseAssign)
982 EmitAggregateCopy(Dest, Src, Ty);
983 else {
984 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
985 MD);
986 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
987 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +0000988 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000989 const llvm::Type *LTy =
990 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
991 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000992 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000994 CallArgList CallArgs;
995 // Push the this (Dest) ptr.
996 CallArgs.push_back(std::make_pair(RValue::get(Dest),
997 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000999 // Push the Src ptr.
1000 CallArgs.push_back(std::make_pair(RValue::get(Src),
1001 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001002 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001003 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1004 Callee, CallArgs, MD);
1005 }
1006 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001008 // Emit the increment of the loop counter.
1009 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1010 Counter = Builder.CreateLoad(IndexPtr);
1011 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1012 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001014 // Finally, branch back up to the condition for the next iteration.
1015 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001017 // Emit the fall-through block.
1018 EmitBlock(AfterFor, true);
1019}
1020
Fariborz Jahanianca283612009-08-07 23:51:33 +00001021/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1022/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001023/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001024void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001025 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001026 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001027 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1028 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001029 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1030 /*NullCheckValue=*/false);
1031 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1032 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001033 }
1034 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1035 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001036 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001037 }
Mike Stump1eb44332009-09-09 15:08:12 +00001038
1039 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001040 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001041 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001042 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001043 CallArgList CallArgs;
1044 // Push the this (Dest) ptr.
1045 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1046 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Fariborz Jahanianca283612009-08-07 23:51:33 +00001048 // Push the Src ptr.
1049 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001050 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001051 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001052 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001053 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1054 Callee, CallArgs, BaseCopyCtor);
1055 }
1056}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001057
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001058/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001059/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001060/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001061// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001062void CodeGenFunction::EmitClassCopyAssignment(
1063 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001064 const CXXRecordDecl *ClassDecl,
1065 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001066 QualType Ty) {
1067 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001068 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1069 /*NullCheckValue=*/false);
1070 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1071 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001072 }
1073 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1074 EmitAggregateCopy(Dest, Src, Ty);
1075 return;
1076 }
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001078 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001079 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001080 MD);
1081 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1082 (void)ConstCopyAssignOp;
1083
John McCall183700f2009-09-21 23:43:11 +00001084 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001085 const llvm::Type *LTy =
1086 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001087 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001088 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001090 CallArgList CallArgs;
1091 // Push the this (Dest) ptr.
1092 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1093 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001095 // Push the Src ptr.
1096 CallArgs.push_back(std::make_pair(RValue::get(Src),
1097 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001098 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001099 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001100 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1101 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001102}
1103
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001104/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001105void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001106CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1107 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001108 llvm::Function *Fn,
1109 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001110 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1111 SourceLocation());
1112 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001113 FinishFunction();
1114}
1115
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001116/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001117/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001118/// The implicitly-defined copy constructor for class X performs a memberwise
1119/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001120/// of initialization of bases and members in a user-defined constructor
1121/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001122/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001123/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001124/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001125/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001126/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001127/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001128/// Virtual base class subobjects shall be copied only once by the
1129/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001130
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001131void
1132CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1133 CXXCtorType Type,
1134 llvm::Function *Fn,
1135 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001136 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001137 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001138 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001139 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1140 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001142 FunctionArgList::const_iterator i = Args.begin();
1143 const VarDecl *ThisArg = i->first;
1144 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1145 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1146 const VarDecl *SrcArg = (i+1)->first;
1147 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1148 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001150 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1151 Base != ClassDecl->bases_end(); ++Base) {
1152 // FIXME. copy constrution of virtual base NYI
1153 if (Base->isVirtual())
1154 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001156 CXXRecordDecl *BaseClassDecl
1157 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001158 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1159 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001162 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1163 FieldEnd = ClassDecl->field_end();
1164 Field != FieldEnd; ++Field) {
1165 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001166 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001167 getContext().getAsConstantArrayType(FieldType);
1168 if (Array)
1169 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001171 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1172 CXXRecordDecl *FieldClassDecl
1173 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1174 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1175 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001176 if (Array) {
1177 const llvm::Type *BasePtr = ConvertType(FieldType);
1178 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001179 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001180 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001181 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001182 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1183 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1184 FieldClassDecl, FieldType);
1185 }
Mike Stump1eb44332009-09-09 15:08:12 +00001186 else
1187 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001188 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001189 continue;
1190 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001191 // Do a built-in assignment of scalar data members.
1192 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1193 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1194 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1195 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001196 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001197 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001198}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001199
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001200/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001201/// Before the implicitly-declared copy assignment operator for a class is
1202/// implicitly defined, all implicitly- declared copy assignment operators for
1203/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001204/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001205/// The implicitly-defined copy assignment operator for class X performs
1206/// memberwise assignment of its subob- jects. The direct base classes of X are
1207/// assigned first, in the order of their declaration in
1208/// the base-specifier-list, and then the immediate nonstatic data members of X
1209/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001210/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001211/// if the subobject is of class type, the copy assignment operator for the
1212/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001213/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001214///
Mike Stump1eb44332009-09-09 15:08:12 +00001215/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001216/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001217///
Mike Stump1eb44332009-09-09 15:08:12 +00001218/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001219/// used.
1220void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001221 llvm::Function *Fn,
1222 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001223
1224 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1225 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1226 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001227 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001229 FunctionArgList::const_iterator i = Args.begin();
1230 const VarDecl *ThisArg = i->first;
1231 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1232 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1233 const VarDecl *SrcArg = (i+1)->first;
1234 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1235 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001237 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1238 Base != ClassDecl->bases_end(); ++Base) {
1239 // FIXME. copy assignment of virtual base NYI
1240 if (Base->isVirtual())
1241 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001243 CXXRecordDecl *BaseClassDecl
1244 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1245 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1246 Base->getType());
1247 }
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001249 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1250 FieldEnd = ClassDecl->field_end();
1251 Field != FieldEnd; ++Field) {
1252 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001253 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001254 getContext().getAsConstantArrayType(FieldType);
1255 if (Array)
1256 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001258 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1259 CXXRecordDecl *FieldClassDecl
1260 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1261 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1262 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001263 if (Array) {
1264 const llvm::Type *BasePtr = ConvertType(FieldType);
1265 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1266 llvm::Value *DestBaseAddrPtr =
1267 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1268 llvm::Value *SrcBaseAddrPtr =
1269 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1270 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1271 FieldClassDecl, FieldType);
1272 }
1273 else
Mike Stump1eb44332009-09-09 15:08:12 +00001274 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001275 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001276 continue;
1277 }
1278 // Do a built-in assignment of scalar data members.
1279 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1280 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1281 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1282 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001283 }
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001285 // return *this;
1286 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001288 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001289}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001290
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001291/// EmitCtorPrologue - This routine generates necessary code to initialize
1292/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001293/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001294void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1295 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001296 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001297 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001298 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001300 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001301 E = CD->init_end();
1302 B != E; ++B) {
1303 CXXBaseOrMemberInitializer *Member = (*B);
1304 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001305 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001306 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001307 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001308 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001309 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1310 BaseClassDecl,
1311 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001312 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001313 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001314 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001315 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001316 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001317 // non-static data member initilaizers.
1318 FieldDecl *Field = Member->getMember();
1319 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001320 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001321 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001322 if (Array)
1323 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Mike Stumpf1216772009-07-31 18:25:34 +00001325 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001326 LValue LHS;
1327 if (FieldType->isReferenceType()) {
1328 // FIXME: This is really ugly; should be refactored somehow
1329 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1330 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001331 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1332 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001333 } else {
1334 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1335 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001336 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001337 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001338 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001339 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001340 if (Array) {
1341 const llvm::Type *BasePtr = ConvertType(FieldType);
1342 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001343 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001344 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001345 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001346 Array, BaseAddrPtr);
1347 }
1348 else
1349 EmitCXXConstructorCall(Member->getConstructor(),
1350 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001351 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001352 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001353 continue;
1354 }
1355 else {
1356 // Initializing an anonymous union data member.
1357 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001358 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001359 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001360 FieldType = anonMember->getType();
1361 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001362 }
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001364 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001365 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001366 RValue RHS;
1367 if (FieldType->isReferenceType())
1368 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1369 /*IsInitializer=*/true);
1370 else
1371 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1372 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001373 }
1374 }
Mike Stumpf1216772009-07-31 18:25:34 +00001375
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001376 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001377 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001378 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001379 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001380 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001381 ClassDecl->bases_begin();
1382 Base != ClassDecl->bases_end(); ++Base) {
1383 // FIXME. copy assignment of virtual base NYI
1384 if (Base->isVirtual())
1385 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001387 CXXRecordDecl *BaseClassDecl
1388 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1389 if (BaseClassDecl->hasTrivialConstructor())
1390 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001391 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001392 BaseClassDecl->getDefaultConstructor(getContext())) {
1393 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001394 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1395 BaseClassDecl,
1396 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001397 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1398 }
1399 }
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001401 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1402 FieldEnd = ClassDecl->field_end();
1403 Field != FieldEnd; ++Field) {
1404 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001405 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001406 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001407 if (Array)
1408 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001409 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1410 continue;
1411 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001412 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001413 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1414 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1415 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001416 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001417 MemberClassDecl->getDefaultConstructor(getContext())) {
1418 LoadOfThis = LoadCXXThis();
1419 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001420 if (Array) {
1421 const llvm::Type *BasePtr = ConvertType(FieldType);
1422 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001423 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001424 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1425 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1426 }
1427 else
Mike Stump1eb44332009-09-09 15:08:12 +00001428 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001429 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001430 }
1431 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001432 }
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Mike Stumpf1216772009-07-31 18:25:34 +00001434 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001435 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001436 if (!LoadOfThis)
1437 LoadOfThis = LoadCXXThis();
1438 llvm::Value *VtableField;
1439 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001440 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001441 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1442 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1443 llvm::Value *vtable = GenerateVtable(ClassDecl);
1444 Builder.CreateStore(vtable, VtableField);
1445 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001446}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001447
1448/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001449/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001450/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001451/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001452void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1453 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001454 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001455 assert(!ClassDecl->getNumVBases() &&
1456 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001457 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001459 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1460 *E = DD->destr_end(); B != E; ++B) {
1461 uintptr_t BaseOrMember = (*B);
1462 if (DD->isMemberToDestroy(BaseOrMember)) {
1463 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1464 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001465 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001466 getContext().getAsConstantArrayType(FieldType);
1467 if (Array)
1468 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001469 const RecordType *RT = FieldType->getAs<RecordType>();
1470 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1471 if (FieldClassDecl->hasTrivialDestructor())
1472 continue;
1473 llvm::Value *LoadOfThis = LoadCXXThis();
1474 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001475 if (Array) {
1476 const llvm::Type *BasePtr = ConvertType(FieldType);
1477 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001478 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001479 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001480 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001481 Array, BaseAddrPtr);
1482 }
1483 else
1484 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1485 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001486 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001487 const RecordType *RT =
1488 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1489 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1490 if (BaseClassDecl->hasTrivialDestructor())
1491 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001492 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1493 ClassDecl, BaseClassDecl,
1494 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001495 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001496 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001497 }
1498 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001499 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1500 return;
1501 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001502 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001503 // reverse order of their construction.
1504 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001506 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1507 FieldEnd = ClassDecl->field_end();
1508 Field != FieldEnd; ++Field) {
1509 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001510 if (getContext().getAsConstantArrayType(FieldType))
1511 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001512 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1513 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1514 if (FieldClassDecl->hasTrivialDestructor())
1515 continue;
1516 DestructedFields.push_back(*Field);
1517 }
1518 }
1519 if (!DestructedFields.empty())
1520 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1521 FieldDecl *Field = DestructedFields[i];
1522 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001523 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001524 getContext().getAsConstantArrayType(FieldType);
1525 if (Array)
1526 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001527 const RecordType *RT = FieldType->getAs<RecordType>();
1528 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1529 llvm::Value *LoadOfThis = LoadCXXThis();
1530 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001531 if (Array) {
1532 const llvm::Type *BasePtr = ConvertType(FieldType);
1533 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001534 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001535 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001536 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001537 Array, BaseAddrPtr);
1538 }
1539 else
1540 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1541 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001542 }
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001544 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1545 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1546 Base != ClassDecl->bases_end(); ++Base) {
1547 // FIXME. copy assignment of virtual base NYI
1548 if (Base->isVirtual())
1549 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001551 CXXRecordDecl *BaseClassDecl
1552 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1553 if (BaseClassDecl->hasTrivialDestructor())
1554 continue;
1555 DestructedBases.push_back(BaseClassDecl);
1556 }
1557 if (DestructedBases.empty())
1558 return;
1559 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1560 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001561 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1562 ClassDecl,BaseClassDecl,
1563 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001564 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1565 Dtor_Complete, V);
1566 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001567}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001568
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001569void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1570 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001571 llvm::Function *Fn,
1572 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001574 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001575 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1576 "SynthesizeDefaultDestructor - destructor has user declaration");
1577 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001579 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1580 SourceLocation());
1581 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001582 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001583}
Anders Carlsson6815e942009-09-27 18:58:34 +00001584
1585// FIXME: Move this to CGCXXStmt.cpp
1586void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1587 // FIXME: We need to do more here.
1588 EmitStmt(S.getTryBlock());
1589}