blob: 029fdedb5026af0661fdf3eeaac68d3e01f2bd5b [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 Stumpdf317bf2009-11-03 23:25:48 +000074 bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified();
Mike Stump1eb44332009-09-09 15:08:12 +000075
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000076 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000077 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000078 } else if (!hasAggregateLLVMType(T)) {
79 llvm::Value *V = EmitScalarExpr(Init);
Mike Stumpdf317bf2009-11-03 23:25:48 +000080 EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000081 } else if (T->isAnyComplexType()) {
Mike Stumpdf317bf2009-11-03 23:25:48 +000082 EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000083 } else {
Mike Stumpdf317bf2009-11-03 23:25:48 +000084 EmitAggExpr(Init, DeclPtr, isVolatile);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000086 if (const RecordType *RT = T->getAs<RecordType>()) {
87 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
88 if (!RD->hasTrivialDestructor())
89 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr);
90 }
91 }
92}
93
Anders Carlsson89ed31d2009-08-08 23:24:23 +000094void
95CodeGenModule::EmitCXXGlobalInitFunc() {
96 if (CXXGlobalInits.empty())
97 return;
Mike Stump1eb44332009-09-09 15:08:12 +000098
Mike Stump79d57682009-11-04 01:11:15 +000099 const llvm::FunctionType *FTy
100 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
101 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000103 // Create our global initialization function.
104 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000105 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000106 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
107 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000109 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000110 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000111 CXXGlobalInits.size());
112 AddGlobalCtor(Fn);
113}
114
115void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
116 const VarDecl **Decls,
117 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000118 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000119 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000121 for (unsigned i = 0; i != NumDecls; ++i) {
122 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000124 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
125 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
126 }
127 FinishFunction();
128}
129
Mike Stump1eb44332009-09-09 15:08:12 +0000130void
131CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000132 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000133 // FIXME: This should use __cxa_guard_{acquire,release}?
134
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000135 assert(!getContext().getLangOptions().ThreadsafeStatics &&
136 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000137
Anders Carlsson283a0622009-04-13 18:03:33 +0000138 llvm::SmallString<256> GuardVName;
139 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000140 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000142 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000143 llvm::GlobalValue *GuardV =
Mike Stump79d57682009-11-04 01:11:15 +0000144 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext),
145 false, GV->getLinkage(),
146 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000147 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000149 // Load the first byte of the guard variable.
Mike Stump79d57682009-11-04 01:11:15 +0000150 const llvm::Type *PtrTy
151 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000152 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000153 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000155 // Compare it against 0.
Mike Stump79d57682009-11-04 01:11:15 +0000156 llvm::Value *nullValue
157 = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000158 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Daniel Dunbar55e87422008-11-11 02:29:29 +0000160 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000161 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000162
163 // If the guard variable is 0, jump to the initializer code.
164 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000166 EmitBlock(InitBlock);
167
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000168 EmitCXXGlobalVarDeclInit(D, GV);
169
Mike Stump79d57682009-11-04 01:11:15 +0000170 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
171 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000172 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000174 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000175}
176
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000177RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
178 llvm::Value *Callee,
179 llvm::Value *This,
180 CallExpr::const_arg_iterator ArgBeg,
181 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000182 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000183 "Trying to emit a member call expr on a static method!");
184
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000185 // A call to a trivial destructor requires no code generation.
186 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
187 if (Destructor->isTrivial())
188 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000189
John McCall183700f2009-09-21 23:43:11 +0000190 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000192 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000194 // Push the this ptr.
195 Args.push_back(std::make_pair(RValue::get(This),
196 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000198 // And the rest of the call args
199 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
John McCall183700f2009-09-21 23:43:11 +0000201 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000202 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
203 Callee, Args, MD);
204}
205
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000206/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
207/// expr can be devirtualized.
208static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
209 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
210 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
211 // This is a record decl. We know the type and can devirtualize it.
212 return VD->getType()->isRecordType();
213 }
Anders Carlsson76366482009-10-12 19:45:47 +0000214
215 return false;
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000216 }
217
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000218 // We can always devirtualize calls on temporary object expressions.
Anders Carlsson76366482009-10-12 19:45:47 +0000219 if (isa<CXXTemporaryObjectExpr>(Base))
220 return true;
221
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000222 // And calls on bound temporaries.
223 if (isa<CXXBindTemporaryExpr>(Base))
224 return true;
225
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000226 // Check if this is a call expr that returns a record type.
227 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
228 return CE->getCallReturnType()->isRecordType();
229
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000230 // We can't devirtualize the call.
231 return false;
232}
233
Anders Carlsson774e7c62009-04-03 22:50:24 +0000234RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000235 if (isa<BinaryOperator>(CE->getCallee()))
236 return EmitCXXMemberPointerCallExpr(CE);
237
Anders Carlsson774e7c62009-04-03 22:50:24 +0000238 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
239 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000240
Anders Carlsson2472bf02009-09-29 03:54:11 +0000241 if (MD->isStatic()) {
242 // The method is static, emit it as we would a regular call.
243 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
244 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
245 CE->arg_begin(), CE->arg_end(), 0);
246
247 }
248
John McCall183700f2009-09-21 23:43:11 +0000249 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000250
Mike Stump1eb44332009-09-09 15:08:12 +0000251 const llvm::Type *Ty =
252 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000253 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000254 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Anders Carlsson774e7c62009-04-03 22:50:24 +0000256 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000257 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000258 else {
259 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000260 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000261 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000262
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000263 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000264 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000265 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000266 //
267 // We also don't emit a virtual call if the base expression has a record type
268 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000269 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000270 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000271 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000272 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000273 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000274 = dyn_cast<CXXDestructorDecl>(MD))
275 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000276 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000277 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
279 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000280 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000281}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000282
Mike Stump1eb44332009-09-09 15:08:12 +0000283RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000284CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
285 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson3eea6352009-10-13 17:41:28 +0000286 const Expr *BaseExpr = BO->getLHS();
287 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000288
Anders Carlsson3eea6352009-10-13 17:41:28 +0000289 const MemberPointerType *MPT =
290 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000291 const FunctionProtoType *FPT =
292 MPT->getPointeeType()->getAs<FunctionProtoType>();
293 const CXXRecordDecl *RD =
Douglas Gregor87c12c42009-11-04 16:49:01 +0000294 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000295
296 const llvm::FunctionType *FTy =
297 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
298 FPT->isVariadic());
299
300 const llvm::Type *Int8PtrTy =
301 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
302
303 // Get the member function pointer.
304 llvm::Value *MemFnPtr =
Anders Carlsson3eea6352009-10-13 17:41:28 +0000305 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
306 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000307
308 // Emit the 'this' pointer.
309 llvm::Value *This;
310
311 if (BO->getOpcode() == BinaryOperator::PtrMemI)
312 This = EmitScalarExpr(BaseExpr);
313 else
314 This = EmitLValue(BaseExpr).getAddress();
315
316 // Adjust it.
317 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
318 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
319
320 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
321 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
322
323 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
324
325 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
326
327 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
328
329 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
330
331 // If the LSB in the function pointer is 1, the function pointer points to
332 // a virtual function.
333 llvm::Value *IsVirtual
334 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
335 "and");
336
337 IsVirtual = Builder.CreateTrunc(IsVirtual,
338 llvm::Type::getInt1Ty(VMContext));
339
340 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
341 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
342 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
343
344 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
345 EmitBlock(FnVirtual);
346
347 const llvm::Type *VTableTy =
348 FTy->getPointerTo()->getPointerTo()->getPointerTo();
349
350 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
351 VTable = Builder.CreateLoad(VTable);
352
353 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
354
355 // Since the function pointer is 1 plus the virtual table offset, we
356 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000357 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000358
359 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
360
361 EmitBranch(FnEnd);
362 EmitBlock(FnNonVirtual);
363
364 // If the function is not virtual, just load the pointer.
365 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
366 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
367
368 EmitBlock(FnEnd);
369
370 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
371 Callee->reserveOperandSpace(2);
372 Callee->addIncoming(VirtualFn, FnVirtual);
373 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
374
375 CallArgList Args;
376
377 QualType ThisType =
378 getContext().getPointerType(getContext().getTagDeclType(RD));
379
380 // Push the this ptr.
381 Args.push_back(std::make_pair(RValue::get(This), ThisType));
382
383 // And the rest of the call args
384 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
385 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
386 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
387 Callee, Args, 0);
388}
389
390RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000391CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
392 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000393 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000394 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Fariborz Jahanianad258832009-08-13 21:09:41 +0000396 if (MD->isCopyAssignment()) {
397 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
398 if (ClassDecl->hasTrivialCopyAssignment()) {
399 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
400 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
401 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
402 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
403 QualType Ty = E->getType();
404 EmitAggregateCopy(This, Src, Ty);
405 return RValue::get(This);
406 }
407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
John McCall183700f2009-09-21 23:43:11 +0000409 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000410 const llvm::Type *Ty =
411 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000412 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000413 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Anders Carlsson0f294632009-05-27 04:18:27 +0000415 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Anders Carlsson0f294632009-05-27 04:18:27 +0000417 return EmitCXXMemberCall(MD, Callee, This,
418 E->arg_begin() + 1, E->arg_end());
419}
420
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000421llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000422 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000423 "Must be in a C++ member function decl to load 'this'");
424 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
425 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000427 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000428 // ans: See how CodeGenFunction::LoadObjCSelf() uses
429 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000430 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
431}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000432
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000433/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
434/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000435/// array.
436/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
437/// array type and 'ArrayPtr' points to the beginning fo the array.
438/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000439void
440CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000441 const ConstantArrayType *ArrayTy,
442 llvm::Value *ArrayPtr) {
443 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
444 llvm::Value * NumElements =
445 llvm::ConstantInt::get(SizeTy,
446 getContext().getConstantArrayElementCount(ArrayTy));
447
448 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
449}
450
451void
452CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
453 llvm::Value *NumElements,
454 llvm::Value *ArrayPtr) {
455 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000457 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000458 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
459 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
460 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000462 // Start the loop with a block that tests the condition.
463 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
464 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000466 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000468 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000470 // Generate: if (loop-index < number-of-elements fall to the loop body,
471 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000472 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000473 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000474 // If the condition is true, execute the body.
475 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000477 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000479 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000480 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000481 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000482 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
483 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000484 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000486 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000488 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000489 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000490 Counter = Builder.CreateLoad(IndexPtr);
491 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
492 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000494 // Finally, branch back up to the condition for the next iteration.
495 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000497 // Emit the fall-through block.
498 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000499}
500
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000501/// EmitCXXAggrDestructorCall - calls the default destructor on array
502/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000503void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000504CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
505 const ArrayType *Array,
506 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000507 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
508 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000509 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000510 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000511 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000512 // Create a temporary for the loop index and initialize it with count of
513 // array elements.
514 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
515 "loop.index");
516 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000517 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000518 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
519 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000521 // Start the loop with a block that tests the condition.
522 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
523 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000525 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000527 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000529 // Generate: if (loop-index != 0 fall to the loop body,
530 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000531 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000532 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
533 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
534 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
535 "isne");
536 // If the condition is true, execute the body.
537 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000539 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000541 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
542 // Inside the loop body, emit the constructor call on the array element.
543 Counter = Builder.CreateLoad(IndexPtr);
544 Counter = Builder.CreateSub(Counter, One);
545 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
546 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000548 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000550 // Emit the decrement of the loop counter.
551 Counter = Builder.CreateLoad(IndexPtr);
552 Counter = Builder.CreateSub(Counter, One, "dec");
553 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000555 // Finally, branch back up to the condition for the next iteration.
556 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000558 // Emit the fall-through block.
559 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000560}
561
562void
Mike Stump1eb44332009-09-09 15:08:12 +0000563CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
564 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000565 llvm::Value *This,
566 CallExpr::const_arg_iterator ArgBeg,
567 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000568 if (D->isCopyConstructor(getContext())) {
569 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
570 if (ClassDecl->hasTrivialCopyConstructor()) {
571 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
572 "EmitCXXConstructorCall - user declared copy constructor");
573 const Expr *E = (*ArgBeg);
574 QualType Ty = E->getType();
575 llvm::Value *Src = EmitLValue(E).getAddress();
576 EmitAggregateCopy(This, Src, Ty);
577 return;
578 }
579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000581 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
582
583 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000584}
585
Mike Stump1eb44332009-09-09 15:08:12 +0000586void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000587 CXXDtorType Type,
588 llvm::Value *This) {
589 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Anders Carlsson7267c162009-05-29 21:03:38 +0000591 EmitCXXMemberCall(D, Callee, This, 0, 0);
592}
593
Mike Stump1eb44332009-09-09 15:08:12 +0000594void
595CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000596 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000597 assert(Dest && "Must have a destination!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000598 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000599 const ConstantArrayType *Array =
600 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000601 // For a copy constructor, even if it is trivial, must fall thru so
602 // its argument is code-gen'ed.
603 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000604 QualType InitType = E->getType();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000605 if (Array)
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000606 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000607 const CXXRecordDecl *RD =
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000608 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000609 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000610 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000613 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000614 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000615 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000616 EmitAggExpr((*i), Dest, false);
617 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000618 }
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000619 if (Array) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000620 QualType BaseElementTy = getContext().getBaseElementType(Array);
621 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
622 BasePtr = llvm::PointerType::getUnqual(BasePtr);
623 llvm::Value *BaseAddrPtr =
624 Builder.CreateBitCast(Dest, BasePtr);
625 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
626 }
627 else
628 // Call the constructor.
629 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
630 E->arg_begin(), E->arg_end());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000631}
632
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000633void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000634 EmitGlobal(GlobalDecl(D, Ctor_Complete));
635 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000636}
Anders Carlsson363c1842009-04-16 23:57:24 +0000637
Mike Stump1eb44332009-09-09 15:08:12 +0000638void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000639 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Anders Carlsson27ae5362009-04-17 01:58:57 +0000641 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000643 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Anders Carlsson27ae5362009-04-17 01:58:57 +0000645 SetFunctionDefinitionAttributes(D, Fn);
646 SetLLVMFunctionAttributesForDefinition(D, Fn);
647}
648
Anders Carlsson363c1842009-04-16 23:57:24 +0000649llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000650CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000651 CXXCtorType Type) {
652 const llvm::FunctionType *FTy =
653 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Anders Carlsson363c1842009-04-16 23:57:24 +0000655 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000656 return cast<llvm::Function>(
657 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000658}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000659
Mike Stump1eb44332009-09-09 15:08:12 +0000660const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000661 CXXCtorType Type) {
662 llvm::SmallString<256> Name;
663 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000664 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Anders Carlsson27ae5362009-04-17 01:58:57 +0000666 Name += '\0';
667 return UniqueMangledName(Name.begin(), Name.end());
668}
669
670void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000671 EmitCXXDestructor(D, Dtor_Complete);
672 EmitCXXDestructor(D, Dtor_Base);
673}
674
Mike Stump1eb44332009-09-09 15:08:12 +0000675void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000676 CXXDtorType Type) {
677 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000679 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Anders Carlsson27ae5362009-04-17 01:58:57 +0000681 SetFunctionDefinitionAttributes(D, Fn);
682 SetLLVMFunctionAttributesForDefinition(D, Fn);
683}
684
685llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000686CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000687 CXXDtorType Type) {
688 const llvm::FunctionType *FTy =
689 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Anders Carlsson27ae5362009-04-17 01:58:57 +0000691 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000692 return cast<llvm::Function>(
693 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000694}
695
Mike Stump1eb44332009-09-09 15:08:12 +0000696const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000697 CXXDtorType Type) {
698 llvm::SmallString<256> Name;
699 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000700 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Anders Carlsson27ae5362009-04-17 01:58:57 +0000702 Name += '\0';
703 return UniqueMangledName(Name.begin(), Name.end());
704}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000705
Mike Stumped032eb2009-09-04 18:27:16 +0000706llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
707 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000708 bool Extern, int64_t nv,
709 int64_t v) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000710 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stumped032eb2009-09-04 18:27:16 +0000711}
712
Mike Stumpc902d222009-11-03 16:59:27 +0000713llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
714 int64_t v) {
715 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
716 0);
717 const llvm::Type *OrigTy = V->getType();
718 if (nv) {
719 // Do the non-virtual adjustment
720 V = Builder.CreateBitCast(V, Ptr8Ty);
721 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
722 V = Builder.CreateBitCast(V, OrigTy);
723 }
724 if (v) {
725 // Do the virtual this adjustment
726 const llvm::Type *PtrDiffTy =
727 ConvertType(getContext().getPointerDiffType());
728 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
729 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
730 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
731 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
732 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
733 V = Builder.CreateLoad(V, "vtable");
734 llvm::Value *VTablePtr = V;
735 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
736 v /= LLVMPointerWidth/8;
737 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
738 V = Builder.CreateLoad(V);
739 V = Builder.CreateGEP(ThisVal, V);
740 V = Builder.CreateBitCast(V, OrigTy);
741 }
742 return V;
743}
744
Mike Stump6e319f62009-09-11 23:25:56 +0000745llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
746 const CXXMethodDecl *MD,
747 bool Extern,
748 int64_t nv_t,
749 int64_t v_t,
750 int64_t nv_r,
751 int64_t v_r) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000752 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000753
754 FunctionArgList Args;
755 ImplicitParamDecl *ThisDecl =
756 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
757 MD->getThisType(getContext()));
758 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
759 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
760 e = MD->param_end();
761 i != e; ++i) {
762 ParmVarDecl *D = *i;
763 Args.push_back(std::make_pair(D, D->getType()));
764 }
765 IdentifierInfo *II
766 = &CGM.getContext().Idents.get("__thunk_named_foo_");
767 FunctionDecl *FD = FunctionDecl::Create(getContext(),
768 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000769 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000770 Extern
771 ? FunctionDecl::Extern
772 : FunctionDecl::Static,
773 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000774 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
775
776 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000777 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
778 const llvm::Type *Ty =
779 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
780 FPT->isVariadic());
781 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000782 CallArgList CallArgs;
783
Mike Stump736529e2009-11-03 02:12:59 +0000784 QualType ArgType = MD->getThisType(getContext());
785 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stumpd0fe5362009-11-04 00:53:51 +0000786 if (nv_t || v_t) {
Mike Stumpc902d222009-11-03 16:59:27 +0000787 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000788 const llvm::Type *OrigTy = Callee->getType();
Mike Stumpc902d222009-11-03 16:59:27 +0000789 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stumpd0fe5362009-11-04 00:53:51 +0000790 if (nv_r || v_r) {
791 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
792 Callee = Builder.CreateBitCast(Callee, OrigTy);
793 nv_r = v_r = 0;
794 }
795 }
796
Mike Stump736529e2009-11-03 02:12:59 +0000797 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
798
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000799 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
800 e = MD->param_end();
801 i != e; ++i) {
802 ParmVarDecl *D = *i;
803 QualType ArgType = D->getType();
804
805 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
806 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000807 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
808 }
809
Mike Stumpf49ed942009-11-02 23:47:45 +0000810 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
811 Callee, CallArgs, MD);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000812 if (nv_r || v_r) {
Mike Stump03e777e2009-11-05 06:32:02 +0000813 bool CanBeZero = !(ResultType->isReferenceType()
814 // FIXME: attr nonnull can't be zero either
815 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stumpc902d222009-11-03 16:59:27 +0000816 // Do the return result adjustment.
Mike Stump03e777e2009-11-05 06:32:02 +0000817 if (CanBeZero) {
818 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
819 llvm::BasicBlock *ZeroBlock = createBasicBlock();
820 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stump7c276b82009-11-05 06:12:26 +0000821
Mike Stump03e777e2009-11-05 06:32:02 +0000822 const llvm::Type *Ty = RV.getScalarVal()->getType();
823 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
824 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
825 NonZeroBlock, ZeroBlock);
826 EmitBlock(NonZeroBlock);
827 llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r);
828 EmitBranch(ContBlock);
829 EmitBlock(ZeroBlock);
830 llvm::Value *Z = RV.getScalarVal();
831 EmitBlock(ContBlock);
832 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
833 RVOrZero->reserveOperandSpace(2);
834 RVOrZero->addIncoming(NZ, NonZeroBlock);
835 RVOrZero->addIncoming(Z, ZeroBlock);
836 RV = RValue::get(RVOrZero);
837 } else
838 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000839 }
840
Mike Stumpf49ed942009-11-02 23:47:45 +0000841 if (!ResultType->isVoidType())
842 EmitReturnOfRValue(RV, ResultType);
843
Mike Stump6e319f62009-09-11 23:25:56 +0000844 FinishFunction();
845 return Fn;
846}
847
Mike Stump77ca8f62009-09-05 07:20:32 +0000848llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
849 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000850 llvm::SmallString<256> OutName;
851 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000852 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000853 llvm::GlobalVariable::LinkageTypes linktype;
854 linktype = llvm::GlobalValue::WeakAnyLinkage;
855 if (!Extern)
856 linktype = llvm::GlobalValue::InternalLinkage;
857 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000858 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000859 const llvm::FunctionType *FTy =
860 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
861 FPT->isVariadic());
862
863 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
864 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000865 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000866 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
867 return m;
868}
869
Mike Stump6e319f62009-09-11 23:25:56 +0000870llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
871 bool Extern, int64_t nv_t,
872 int64_t v_t, int64_t nv_r,
873 int64_t v_r) {
874 llvm::SmallString<256> OutName;
875 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000876 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000877 llvm::GlobalVariable::LinkageTypes linktype;
878 linktype = llvm::GlobalValue::WeakAnyLinkage;
879 if (!Extern)
880 linktype = llvm::GlobalValue::InternalLinkage;
881 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000882 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000883 const llvm::FunctionType *FTy =
884 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
885 FPT->isVariadic());
886
887 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
888 &getModule());
889 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
890 v_r);
Mike Stump6e319f62009-09-11 23:25:56 +0000891 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
892 return m;
893}
894
Mike Stumpf0070db2009-08-26 20:46:33 +0000895llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000896CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
897 const CXXRecordDecl *ClassDecl,
898 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000899 const llvm::Type *Int8PtrTy =
900 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
901
902 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
903 Int8PtrTy->getPointerTo());
904 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
905
Anders Carlssondbd920c2009-10-11 22:13:54 +0000906 int64_t VBaseOffsetIndex =
907 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
908
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000909 llvm::Value *VBaseOffsetPtr =
Mike Stump79d57682009-11-04 01:11:15 +0000910 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000911 const llvm::Type *PtrDiffTy =
912 ConvertType(getContext().getPointerDiffType());
913
914 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
915 PtrDiffTy->getPointerTo());
916
917 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
918
919 return VBaseOffset;
920}
921
922llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000923CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
924 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000925 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000926
Mike Stumpf0070db2009-08-26 20:46:33 +0000927 Ty = llvm::PointerType::get(Ty, 0);
928 Ty = llvm::PointerType::get(Ty, 0);
929 Ty = llvm::PointerType::get(Ty, 0);
930 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
931 vtbl = Builder.CreateLoad(vtbl);
932 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000933 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000934 vfn = Builder.CreateLoad(vfn);
935 return vfn;
936}
937
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000938/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
939/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
940/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000941// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000942void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000943 llvm::Value *Src,
944 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000945 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000946 QualType Ty) {
947 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
948 assert(CA && "VLA cannot be copied over");
949 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000951 // Create a temporary for the loop index and initialize it with 0.
952 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
953 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000954 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000955 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000956 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000957 // Start the loop with a block that tests the condition.
958 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
959 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000961 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000963 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
964 // Generate: if (loop-index < number-of-elements fall to the loop body,
965 // otherwise, go to the block after the for-loop.
966 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000967 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000968 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
969 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000970 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000971 "isless");
972 // If the condition is true, execute the body.
973 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000975 EmitBlock(ForBody);
976 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
977 // Inside the loop body, emit the constructor call on the array element.
978 Counter = Builder.CreateLoad(IndexPtr);
979 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
980 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
981 if (BitwiseCopy)
982 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000983 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000984 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000985 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000986 Ctor_Complete);
987 CallArgList CallArgs;
988 // Push the this (Dest) ptr.
989 CallArgs.push_back(std::make_pair(RValue::get(Dest),
990 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000992 // Push the Src ptr.
993 CallArgs.push_back(std::make_pair(RValue::get(Src),
Mike Stump79d57682009-11-04 01:11:15 +0000994 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000995 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000996 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000997 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
998 Callee, CallArgs, BaseCopyCtor);
999 }
1000 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001002 // Emit the increment of the loop counter.
1003 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1004 Counter = Builder.CreateLoad(IndexPtr);
1005 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1006 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001008 // Finally, branch back up to the condition for the next iteration.
1009 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001011 // Emit the fall-through block.
1012 EmitBlock(AfterFor, true);
1013}
1014
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001015/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001016/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001017/// bitwise assignment or via a copy assignment operator function call.
1018/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001019void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001020 llvm::Value *Src,
1021 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001022 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001023 QualType Ty) {
1024 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1025 assert(CA && "VLA cannot be asssigned");
1026 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001028 // Create a temporary for the loop index and initialize it with 0.
1029 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1030 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001031 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001032 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1033 Builder.CreateStore(zeroConstant, IndexPtr, false);
1034 // Start the loop with a block that tests the condition.
1035 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1036 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001038 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001040 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1041 // Generate: if (loop-index < number-of-elements fall to the loop body,
1042 // otherwise, go to the block after the for-loop.
1043 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001044 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001045 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1046 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001047 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001048 "isless");
1049 // If the condition is true, execute the body.
1050 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001052 EmitBlock(ForBody);
1053 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1054 // Inside the loop body, emit the assignment operator call on array element.
1055 Counter = Builder.CreateLoad(IndexPtr);
1056 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1057 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1058 const CXXMethodDecl *MD = 0;
1059 if (BitwiseAssign)
1060 EmitAggregateCopy(Dest, Src, Ty);
1061 else {
1062 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1063 MD);
1064 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1065 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001066 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001067 const llvm::Type *LTy =
1068 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1069 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001070 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001072 CallArgList CallArgs;
1073 // Push the this (Dest) ptr.
1074 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1075 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001077 // Push the Src ptr.
1078 CallArgs.push_back(std::make_pair(RValue::get(Src),
1079 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001080 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001081 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1082 Callee, CallArgs, MD);
1083 }
1084 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001086 // Emit the increment of the loop counter.
1087 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1088 Counter = Builder.CreateLoad(IndexPtr);
1089 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1090 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001092 // Finally, branch back up to the condition for the next iteration.
1093 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001095 // Emit the fall-through block.
1096 EmitBlock(AfterFor, true);
1097}
1098
Fariborz Jahanianca283612009-08-07 23:51:33 +00001099/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1100/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001101/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001102void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001103 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001104 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001105 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1106 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001107 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1108 /*NullCheckValue=*/false);
1109 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1110 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001111 }
1112 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1113 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001114 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
1117 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001118 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001119 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001120 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001121 CallArgList CallArgs;
1122 // Push the this (Dest) ptr.
1123 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1124 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Fariborz Jahanianca283612009-08-07 23:51:33 +00001126 // Push the Src ptr.
1127 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001128 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001129 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001130 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001131 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1132 Callee, CallArgs, BaseCopyCtor);
1133 }
1134}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001135
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001136/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001137/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001138/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001139// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001140void CodeGenFunction::EmitClassCopyAssignment(
1141 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001142 const CXXRecordDecl *ClassDecl,
1143 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001144 QualType Ty) {
1145 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001146 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1147 /*NullCheckValue=*/false);
1148 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1149 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001150 }
1151 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1152 EmitAggregateCopy(Dest, Src, Ty);
1153 return;
1154 }
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001156 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001157 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001158 MD);
1159 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1160 (void)ConstCopyAssignOp;
1161
John McCall183700f2009-09-21 23:43:11 +00001162 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001163 const llvm::Type *LTy =
1164 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001165 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001166 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001168 CallArgList CallArgs;
1169 // Push the this (Dest) ptr.
1170 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1171 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001173 // Push the Src ptr.
1174 CallArgs.push_back(std::make_pair(RValue::get(Src),
1175 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001176 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001177 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001178 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1179 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001180}
1181
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001182/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001183void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001184CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1185 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001186 llvm::Function *Fn,
1187 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001188 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1189 SourceLocation());
1190 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001191 FinishFunction();
1192}
1193
Mike Stump79d57682009-11-04 01:11:15 +00001194/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1195/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001196/// The implicitly-defined copy constructor for class X performs a memberwise
Mike Stump79d57682009-11-04 01:11:15 +00001197/// copy of its subobjects. The order of copying is the same as the order of
1198/// initialization of bases and members in a user-defined constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001199/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001200/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001201/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001202/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001203/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001204/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001205/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001206/// Virtual base class subobjects shall be copied only once by the
1207/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001208
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001209void
1210CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1211 CXXCtorType Type,
1212 llvm::Function *Fn,
1213 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001214 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001215 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Mike Stump79d57682009-11-04 01:11:15 +00001216 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001217 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1218 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001220 FunctionArgList::const_iterator i = Args.begin();
1221 const VarDecl *ThisArg = i->first;
1222 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1223 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1224 const VarDecl *SrcArg = (i+1)->first;
1225 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1226 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001228 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1229 Base != ClassDecl->bases_end(); ++Base) {
1230 // FIXME. copy constrution of virtual base NYI
1231 if (Base->isVirtual())
1232 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001234 CXXRecordDecl *BaseClassDecl
1235 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001236 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1237 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001238 }
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001240 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1241 FieldEnd = ClassDecl->field_end();
1242 Field != FieldEnd; ++Field) {
1243 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001244 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001245 getContext().getAsConstantArrayType(FieldType);
1246 if (Array)
1247 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001249 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1250 CXXRecordDecl *FieldClassDecl
1251 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1252 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1253 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001254 if (Array) {
1255 const llvm::Type *BasePtr = ConvertType(FieldType);
1256 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001257 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001258 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001259 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001260 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1261 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1262 FieldClassDecl, FieldType);
1263 }
Mike Stump1eb44332009-09-09 15:08:12 +00001264 else
1265 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001266 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001267 continue;
1268 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001269 // Do a built-in assignment of scalar data members.
1270 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1271 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1272 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1273 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001274 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001275 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001276}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001277
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001278/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001279/// Before the implicitly-declared copy assignment operator for a class is
1280/// implicitly defined, all implicitly- declared copy assignment operators for
1281/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001282/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001283/// The implicitly-defined copy assignment operator for class X performs
1284/// memberwise assignment of its subob- jects. The direct base classes of X are
1285/// assigned first, in the order of their declaration in
1286/// the base-specifier-list, and then the immediate nonstatic data members of X
1287/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001288/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001289/// if the subobject is of class type, the copy assignment operator for the
1290/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001291/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001292///
Mike Stump1eb44332009-09-09 15:08:12 +00001293/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001294/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001295///
Mike Stump1eb44332009-09-09 15:08:12 +00001296/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001297/// used.
1298void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001299 llvm::Function *Fn,
1300 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001301
1302 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1303 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1304 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001305 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001307 FunctionArgList::const_iterator i = Args.begin();
1308 const VarDecl *ThisArg = i->first;
1309 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1310 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1311 const VarDecl *SrcArg = (i+1)->first;
1312 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1313 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001315 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1316 Base != ClassDecl->bases_end(); ++Base) {
1317 // FIXME. copy assignment of virtual base NYI
1318 if (Base->isVirtual())
1319 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001321 CXXRecordDecl *BaseClassDecl
1322 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1323 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1324 Base->getType());
1325 }
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001327 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1328 FieldEnd = ClassDecl->field_end();
1329 Field != FieldEnd; ++Field) {
1330 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001331 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001332 getContext().getAsConstantArrayType(FieldType);
1333 if (Array)
1334 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001336 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1337 CXXRecordDecl *FieldClassDecl
1338 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1339 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1340 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001341 if (Array) {
1342 const llvm::Type *BasePtr = ConvertType(FieldType);
1343 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1344 llvm::Value *DestBaseAddrPtr =
1345 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1346 llvm::Value *SrcBaseAddrPtr =
1347 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1348 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1349 FieldClassDecl, FieldType);
1350 }
1351 else
Mike Stump1eb44332009-09-09 15:08:12 +00001352 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001353 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001354 continue;
1355 }
1356 // Do a built-in assignment of scalar data members.
1357 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1358 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1359 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1360 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001363 // return *this;
1364 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001366 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001367}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001368
Anders Carlssonb1156b92009-11-06 03:23:06 +00001369static void EmitBaseInitializer(CodeGenFunction &CGF,
1370 const CXXRecordDecl *ClassDecl,
1371 CXXBaseOrMemberInitializer *BaseInit,
1372 CXXCtorType CtorType) {
1373 assert(BaseInit->isBaseInitializer() &&
1374 "Must have base initializer!");
1375
1376 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1377
1378 const Type *BaseType = BaseInit->getBaseClass();
1379 CXXRecordDecl *BaseClassDecl =
1380 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
1381 llvm::Value *V = CGF.GetAddressCXXOfBaseClass(ThisPtr, ClassDecl,
1382 BaseClassDecl,
1383 /*NullCheckValue=*/false);
1384 CGF.EmitCXXConstructorCall(BaseInit->getConstructor(),
1385 CtorType, V,
1386 BaseInit->const_arg_begin(),
1387 BaseInit->const_arg_end());
1388}
1389
1390static void EmitMemberInitializer(CodeGenFunction &CGF,
1391 const CXXRecordDecl *ClassDecl,
1392 CXXBaseOrMemberInitializer *MemberInit) {
1393 assert(MemberInit->isMemberInitializer() &&
1394 "Must have member initializer!");
1395
1396 // non-static data member initializers.
1397 FieldDecl *Field = MemberInit->getMember();
1398 QualType FieldType = CGF.getContext().getCanonicalType((Field)->getType());
1399 const ConstantArrayType *Array =
1400 CGF.getContext().getAsConstantArrayType(FieldType);
1401 if (Array)
1402 FieldType = CGF.getContext().getBaseElementType(FieldType);
1403
1404 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1405 LValue LHS;
1406 if (FieldType->isReferenceType()) {
1407 // FIXME: This is really ugly; should be refactored somehow
1408 unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field);
1409 llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp");
1410 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1411 LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType));
1412 } else {
1413 LHS = CGF.EmitLValueForField(ThisPtr, Field, false, 0);
1414 }
1415 if (FieldType->getAs<RecordType>()) {
1416 if (!Field->isAnonymousStructOrUnion()) {
1417 assert(MemberInit->getConstructor() &&
1418 "EmitCtorPrologue - no constructor to initialize member");
1419 if (Array) {
1420 const llvm::Type *BasePtr = CGF.ConvertType(FieldType);
1421 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1422 llvm::Value *BaseAddrPtr =
1423 CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1424 CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(),
1425 Array, BaseAddrPtr);
1426 }
1427 else
1428 CGF.EmitCXXConstructorCall(MemberInit->getConstructor(),
1429 Ctor_Complete, LHS.getAddress(),
1430 MemberInit->const_arg_begin(),
1431 MemberInit->const_arg_end());
1432 return;
1433 }
1434 else {
1435 // Initializing an anonymous union data member.
1436 FieldDecl *anonMember = MemberInit->getAnonUnionMember();
1437 LHS = CGF.EmitLValueForField(LHS.getAddress(), anonMember,
1438 /*IsUnion=*/true, 0);
1439 FieldType = anonMember->getType();
1440 }
1441 }
1442
1443 assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only");
1444 Expr *RhsExpr = *MemberInit->arg_begin();
1445 RValue RHS;
1446 if (FieldType->isReferenceType())
1447 RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType,
1448 /*IsInitializer=*/true);
1449 else if (FieldType->isMemberFunctionPointerType())
1450 RHS = RValue::get(CGF.CGM.EmitConstantExpr(RhsExpr, FieldType, &CGF));
1451 else
1452 RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true));
1453 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
1454}
1455
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001456/// EmitCtorPrologue - This routine generates necessary code to initialize
1457/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001458/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001459void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1460 CXXCtorType CtorType) {
Anders Carlssonb1156b92009-11-06 03:23:06 +00001461 const CXXRecordDecl *ClassDecl = CD->getParent();
1462
Mike Stumpeb19fa92009-08-06 13:41:24 +00001463 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001464 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001466 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001467 E = CD->init_end();
1468 B != E; ++B) {
1469 CXXBaseOrMemberInitializer *Member = (*B);
Anders Carlssonb1156b92009-11-06 03:23:06 +00001470
1471 if (Member->isBaseInitializer())
1472 EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
1473 else
1474 EmitMemberInitializer(*this, ClassDecl, Member);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001475 }
Mike Stumpf1216772009-07-31 18:25:34 +00001476
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001477 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001478 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001479 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001480 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001481 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001482 ClassDecl->bases_begin();
1483 Base != ClassDecl->bases_end(); ++Base) {
1484 // FIXME. copy assignment of virtual base NYI
1485 if (Base->isVirtual())
1486 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001488 CXXRecordDecl *BaseClassDecl
1489 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1490 if (BaseClassDecl->hasTrivialConstructor())
1491 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001492 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001493 BaseClassDecl->getDefaultConstructor(getContext())) {
1494 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001495 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1496 BaseClassDecl,
1497 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001498 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1499 }
1500 }
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001502 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1503 FieldEnd = ClassDecl->field_end();
1504 Field != FieldEnd; ++Field) {
1505 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001506 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001507 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001508 if (Array)
1509 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001510 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1511 continue;
1512 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001513 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001514 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1515 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1516 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001517 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001518 MemberClassDecl->getDefaultConstructor(getContext())) {
1519 LoadOfThis = LoadCXXThis();
1520 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001521 if (Array) {
1522 const llvm::Type *BasePtr = ConvertType(FieldType);
1523 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001524 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001525 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1526 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1527 }
1528 else
Mike Stump1eb44332009-09-09 15:08:12 +00001529 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001530 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001531 }
1532 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Mike Stumpf1216772009-07-31 18:25:34 +00001535 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001536 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001537 if (!LoadOfThis)
1538 LoadOfThis = LoadCXXThis();
1539 llvm::Value *VtableField;
1540 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001541 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001542 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1543 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1544 llvm::Value *vtable = GenerateVtable(ClassDecl);
1545 Builder.CreateStore(vtable, VtableField);
1546 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001547}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001548
1549/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001550/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001551/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001552/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001553void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1554 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001555 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001556 assert(!ClassDecl->getNumVBases() &&
1557 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001558 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001560 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1561 *E = DD->destr_end(); B != E; ++B) {
1562 uintptr_t BaseOrMember = (*B);
1563 if (DD->isMemberToDestroy(BaseOrMember)) {
1564 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1565 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001566 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001567 getContext().getAsConstantArrayType(FieldType);
1568 if (Array)
1569 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001570 const RecordType *RT = FieldType->getAs<RecordType>();
1571 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1572 if (FieldClassDecl->hasTrivialDestructor())
1573 continue;
1574 llvm::Value *LoadOfThis = LoadCXXThis();
1575 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001576 if (Array) {
1577 const llvm::Type *BasePtr = ConvertType(FieldType);
1578 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001579 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001580 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001581 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001582 Array, BaseAddrPtr);
1583 }
1584 else
1585 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1586 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001587 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001588 const RecordType *RT =
1589 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1590 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1591 if (BaseClassDecl->hasTrivialDestructor())
1592 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001593 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1594 ClassDecl, BaseClassDecl,
1595 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001596 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001597 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001598 }
1599 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001600 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1601 return;
1602 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001603 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001604 // reverse order of their construction.
1605 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001607 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1608 FieldEnd = ClassDecl->field_end();
1609 Field != FieldEnd; ++Field) {
1610 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001611 if (getContext().getAsConstantArrayType(FieldType))
1612 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001613 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1614 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1615 if (FieldClassDecl->hasTrivialDestructor())
1616 continue;
1617 DestructedFields.push_back(*Field);
1618 }
1619 }
1620 if (!DestructedFields.empty())
1621 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1622 FieldDecl *Field = DestructedFields[i];
1623 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001624 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001625 getContext().getAsConstantArrayType(FieldType);
1626 if (Array)
1627 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001628 const RecordType *RT = FieldType->getAs<RecordType>();
1629 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1630 llvm::Value *LoadOfThis = LoadCXXThis();
1631 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001632 if (Array) {
1633 const llvm::Type *BasePtr = ConvertType(FieldType);
1634 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001635 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001636 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001637 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001638 Array, BaseAddrPtr);
1639 }
1640 else
1641 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1642 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001643 }
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001645 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1646 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1647 Base != ClassDecl->bases_end(); ++Base) {
1648 // FIXME. copy assignment of virtual base NYI
1649 if (Base->isVirtual())
1650 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001652 CXXRecordDecl *BaseClassDecl
1653 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1654 if (BaseClassDecl->hasTrivialDestructor())
1655 continue;
1656 DestructedBases.push_back(BaseClassDecl);
1657 }
1658 if (DestructedBases.empty())
1659 return;
1660 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1661 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001662 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1663 ClassDecl,BaseClassDecl,
1664 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001665 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1666 Dtor_Complete, V);
1667 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001668}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001669
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001670void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1671 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001672 llvm::Function *Fn,
1673 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001675 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001676 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1677 "SynthesizeDefaultDestructor - destructor has user declaration");
1678 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001680 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1681 SourceLocation());
1682 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001683 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001684}
Anders Carlsson6815e942009-09-27 18:58:34 +00001685
1686// FIXME: Move this to CGCXXStmt.cpp
1687void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1688 // FIXME: We need to do more here.
1689 EmitStmt(S.getTryBlock());
1690}