blob: cc6f13be1a649d580e48560e52d6da63a999e3ba [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) {
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000652 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +0000653 const llvm::FunctionType *FTy =
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000654 getTypes().GetFunctionType(getTypes().getFunctionInfo(D),
655 FPT->isVariadic());
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Anders Carlsson363c1842009-04-16 23:57:24 +0000657 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000658 return cast<llvm::Function>(
659 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000660}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000661
Mike Stump1eb44332009-09-09 15:08:12 +0000662const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000663 CXXCtorType Type) {
664 llvm::SmallString<256> Name;
665 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000666 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Anders Carlsson27ae5362009-04-17 01:58:57 +0000668 Name += '\0';
669 return UniqueMangledName(Name.begin(), Name.end());
670}
671
672void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000673 EmitCXXDestructor(D, Dtor_Complete);
674 EmitCXXDestructor(D, Dtor_Base);
675}
676
Mike Stump1eb44332009-09-09 15:08:12 +0000677void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000678 CXXDtorType Type) {
679 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000681 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Anders Carlsson27ae5362009-04-17 01:58:57 +0000683 SetFunctionDefinitionAttributes(D, Fn);
684 SetLLVMFunctionAttributesForDefinition(D, Fn);
685}
686
687llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000688CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000689 CXXDtorType Type) {
690 const llvm::FunctionType *FTy =
691 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Anders Carlsson27ae5362009-04-17 01:58:57 +0000693 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000694 return cast<llvm::Function>(
695 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000696}
697
Mike Stump1eb44332009-09-09 15:08:12 +0000698const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000699 CXXDtorType Type) {
700 llvm::SmallString<256> Name;
701 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000702 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Anders Carlsson27ae5362009-04-17 01:58:57 +0000704 Name += '\0';
705 return UniqueMangledName(Name.begin(), Name.end());
706}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000707
Mike Stumped032eb2009-09-04 18:27:16 +0000708llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
709 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000710 bool Extern, int64_t nv,
711 int64_t v) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000712 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stumped032eb2009-09-04 18:27:16 +0000713}
714
Mike Stumpc902d222009-11-03 16:59:27 +0000715llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
716 int64_t v) {
717 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
718 0);
719 const llvm::Type *OrigTy = V->getType();
720 if (nv) {
721 // Do the non-virtual adjustment
722 V = Builder.CreateBitCast(V, Ptr8Ty);
723 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
724 V = Builder.CreateBitCast(V, OrigTy);
725 }
726 if (v) {
727 // Do the virtual this adjustment
728 const llvm::Type *PtrDiffTy =
729 ConvertType(getContext().getPointerDiffType());
730 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
731 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
732 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
733 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
734 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
735 V = Builder.CreateLoad(V, "vtable");
736 llvm::Value *VTablePtr = V;
737 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
738 v /= LLVMPointerWidth/8;
739 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
740 V = Builder.CreateLoad(V);
741 V = Builder.CreateGEP(ThisVal, V);
742 V = Builder.CreateBitCast(V, OrigTy);
743 }
744 return V;
745}
746
Mike Stump6e319f62009-09-11 23:25:56 +0000747llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
748 const CXXMethodDecl *MD,
749 bool Extern,
750 int64_t nv_t,
751 int64_t v_t,
752 int64_t nv_r,
753 int64_t v_r) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000754 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000755
756 FunctionArgList Args;
757 ImplicitParamDecl *ThisDecl =
758 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
759 MD->getThisType(getContext()));
760 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
761 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
762 e = MD->param_end();
763 i != e; ++i) {
764 ParmVarDecl *D = *i;
765 Args.push_back(std::make_pair(D, D->getType()));
766 }
767 IdentifierInfo *II
768 = &CGM.getContext().Idents.get("__thunk_named_foo_");
769 FunctionDecl *FD = FunctionDecl::Create(getContext(),
770 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000771 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000772 Extern
773 ? FunctionDecl::Extern
774 : FunctionDecl::Static,
775 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000776 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
777
778 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000779 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
780 const llvm::Type *Ty =
781 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
782 FPT->isVariadic());
783 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000784 CallArgList CallArgs;
785
Mike Stump736529e2009-11-03 02:12:59 +0000786 QualType ArgType = MD->getThisType(getContext());
787 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stumpd0fe5362009-11-04 00:53:51 +0000788 if (nv_t || v_t) {
Mike Stumpc902d222009-11-03 16:59:27 +0000789 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000790 const llvm::Type *OrigTy = Callee->getType();
Mike Stumpc902d222009-11-03 16:59:27 +0000791 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stumpd0fe5362009-11-04 00:53:51 +0000792 if (nv_r || v_r) {
793 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
794 Callee = Builder.CreateBitCast(Callee, OrigTy);
795 nv_r = v_r = 0;
796 }
797 }
798
Mike Stump736529e2009-11-03 02:12:59 +0000799 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
800
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000801 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
802 e = MD->param_end();
803 i != e; ++i) {
804 ParmVarDecl *D = *i;
805 QualType ArgType = D->getType();
806
807 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
808 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000809 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
810 }
811
Mike Stumpf49ed942009-11-02 23:47:45 +0000812 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
813 Callee, CallArgs, MD);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000814 if (nv_r || v_r) {
Mike Stump03e777e2009-11-05 06:32:02 +0000815 bool CanBeZero = !(ResultType->isReferenceType()
816 // FIXME: attr nonnull can't be zero either
817 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stumpc902d222009-11-03 16:59:27 +0000818 // Do the return result adjustment.
Mike Stump03e777e2009-11-05 06:32:02 +0000819 if (CanBeZero) {
820 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
821 llvm::BasicBlock *ZeroBlock = createBasicBlock();
822 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stump7c276b82009-11-05 06:12:26 +0000823
Mike Stump03e777e2009-11-05 06:32:02 +0000824 const llvm::Type *Ty = RV.getScalarVal()->getType();
825 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
826 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
827 NonZeroBlock, ZeroBlock);
828 EmitBlock(NonZeroBlock);
829 llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r);
830 EmitBranch(ContBlock);
831 EmitBlock(ZeroBlock);
832 llvm::Value *Z = RV.getScalarVal();
833 EmitBlock(ContBlock);
834 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
835 RVOrZero->reserveOperandSpace(2);
836 RVOrZero->addIncoming(NZ, NonZeroBlock);
837 RVOrZero->addIncoming(Z, ZeroBlock);
838 RV = RValue::get(RVOrZero);
839 } else
840 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000841 }
842
Mike Stumpf49ed942009-11-02 23:47:45 +0000843 if (!ResultType->isVoidType())
844 EmitReturnOfRValue(RV, ResultType);
845
Mike Stump6e319f62009-09-11 23:25:56 +0000846 FinishFunction();
847 return Fn;
848}
849
Mike Stump77ca8f62009-09-05 07:20:32 +0000850llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
851 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000852 llvm::SmallString<256> OutName;
853 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000854 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000855 llvm::GlobalVariable::LinkageTypes linktype;
856 linktype = llvm::GlobalValue::WeakAnyLinkage;
857 if (!Extern)
858 linktype = llvm::GlobalValue::InternalLinkage;
859 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000860 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000861 const llvm::FunctionType *FTy =
862 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
863 FPT->isVariadic());
864
865 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
866 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000867 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000868 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
869 return m;
870}
871
Mike Stump6e319f62009-09-11 23:25:56 +0000872llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
873 bool Extern, int64_t nv_t,
874 int64_t v_t, int64_t nv_r,
875 int64_t v_r) {
876 llvm::SmallString<256> OutName;
877 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000878 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000879 llvm::GlobalVariable::LinkageTypes linktype;
880 linktype = llvm::GlobalValue::WeakAnyLinkage;
881 if (!Extern)
882 linktype = llvm::GlobalValue::InternalLinkage;
883 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000884 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000885 const llvm::FunctionType *FTy =
886 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
887 FPT->isVariadic());
888
889 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
890 &getModule());
891 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
892 v_r);
Mike Stump6e319f62009-09-11 23:25:56 +0000893 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
894 return m;
895}
896
Mike Stumpf0070db2009-08-26 20:46:33 +0000897llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000898CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
899 const CXXRecordDecl *ClassDecl,
900 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000901 const llvm::Type *Int8PtrTy =
902 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
903
904 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
905 Int8PtrTy->getPointerTo());
906 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
907
Anders Carlssondbd920c2009-10-11 22:13:54 +0000908 int64_t VBaseOffsetIndex =
909 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
910
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000911 llvm::Value *VBaseOffsetPtr =
Mike Stump79d57682009-11-04 01:11:15 +0000912 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000913 const llvm::Type *PtrDiffTy =
914 ConvertType(getContext().getPointerDiffType());
915
916 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
917 PtrDiffTy->getPointerTo());
918
919 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
920
921 return VBaseOffset;
922}
923
924llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000925CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
926 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000927 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000928
Mike Stumpf0070db2009-08-26 20:46:33 +0000929 Ty = llvm::PointerType::get(Ty, 0);
930 Ty = llvm::PointerType::get(Ty, 0);
931 Ty = llvm::PointerType::get(Ty, 0);
932 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
933 vtbl = Builder.CreateLoad(vtbl);
934 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000935 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000936 vfn = Builder.CreateLoad(vfn);
937 return vfn;
938}
939
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000940/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
941/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
942/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000943// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000944void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000945 llvm::Value *Src,
946 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000947 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000948 QualType Ty) {
949 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
950 assert(CA && "VLA cannot be copied over");
951 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000953 // Create a temporary for the loop index and initialize it with 0.
954 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
955 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000956 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000957 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000958 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000959 // Start the loop with a block that tests the condition.
960 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
961 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000963 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000965 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
966 // Generate: if (loop-index < number-of-elements fall to the loop body,
967 // otherwise, go to the block after the for-loop.
968 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000969 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000970 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
971 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000972 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000973 "isless");
974 // If the condition is true, execute the body.
975 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000977 EmitBlock(ForBody);
978 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
979 // Inside the loop body, emit the constructor call on the array element.
980 Counter = Builder.CreateLoad(IndexPtr);
981 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
982 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
983 if (BitwiseCopy)
984 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000985 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000986 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000987 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000988 Ctor_Complete);
989 CallArgList CallArgs;
990 // Push the this (Dest) ptr.
991 CallArgs.push_back(std::make_pair(RValue::get(Dest),
992 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000994 // Push the Src ptr.
995 CallArgs.push_back(std::make_pair(RValue::get(Src),
Mike Stump79d57682009-11-04 01:11:15 +0000996 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000997 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000998 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000999 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1000 Callee, CallArgs, BaseCopyCtor);
1001 }
1002 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001004 // Emit the increment of the loop counter.
1005 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1006 Counter = Builder.CreateLoad(IndexPtr);
1007 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1008 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001010 // Finally, branch back up to the condition for the next iteration.
1011 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001013 // Emit the fall-through block.
1014 EmitBlock(AfterFor, true);
1015}
1016
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001017/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001018/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001019/// bitwise assignment or via a copy assignment operator function call.
1020/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001021void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001022 llvm::Value *Src,
1023 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001024 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001025 QualType Ty) {
1026 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1027 assert(CA && "VLA cannot be asssigned");
1028 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001030 // Create a temporary for the loop index and initialize it with 0.
1031 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1032 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001033 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001034 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1035 Builder.CreateStore(zeroConstant, IndexPtr, false);
1036 // Start the loop with a block that tests the condition.
1037 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1038 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001040 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001042 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1043 // Generate: if (loop-index < number-of-elements fall to the loop body,
1044 // otherwise, go to the block after the for-loop.
1045 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001046 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001047 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1048 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001049 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001050 "isless");
1051 // If the condition is true, execute the body.
1052 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001054 EmitBlock(ForBody);
1055 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1056 // Inside the loop body, emit the assignment operator call on array element.
1057 Counter = Builder.CreateLoad(IndexPtr);
1058 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1059 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1060 const CXXMethodDecl *MD = 0;
1061 if (BitwiseAssign)
1062 EmitAggregateCopy(Dest, Src, Ty);
1063 else {
1064 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1065 MD);
1066 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1067 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001068 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001069 const llvm::Type *LTy =
1070 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1071 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001072 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001074 CallArgList CallArgs;
1075 // Push the this (Dest) ptr.
1076 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1077 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001079 // Push the Src ptr.
1080 CallArgs.push_back(std::make_pair(RValue::get(Src),
1081 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001082 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001083 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1084 Callee, CallArgs, MD);
1085 }
1086 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001088 // Emit the increment of the loop counter.
1089 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1090 Counter = Builder.CreateLoad(IndexPtr);
1091 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1092 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001094 // Finally, branch back up to the condition for the next iteration.
1095 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001097 // Emit the fall-through block.
1098 EmitBlock(AfterFor, true);
1099}
1100
Fariborz Jahanianca283612009-08-07 23:51:33 +00001101/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1102/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001103/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001104void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001105 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001106 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001107 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1108 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001109 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1110 /*NullCheckValue=*/false);
1111 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1112 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001113 }
1114 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1115 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001116 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
1119 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001120 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001121 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001122 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001123 CallArgList CallArgs;
1124 // Push the this (Dest) ptr.
1125 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1126 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Fariborz Jahanianca283612009-08-07 23:51:33 +00001128 // Push the Src ptr.
1129 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001130 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001131 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001132 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001133 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1134 Callee, CallArgs, BaseCopyCtor);
1135 }
1136}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001137
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001138/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001139/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001140/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001141// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001142void CodeGenFunction::EmitClassCopyAssignment(
1143 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001144 const CXXRecordDecl *ClassDecl,
1145 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001146 QualType Ty) {
1147 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001148 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1149 /*NullCheckValue=*/false);
1150 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1151 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001152 }
1153 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1154 EmitAggregateCopy(Dest, Src, Ty);
1155 return;
1156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001158 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001159 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001160 MD);
1161 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1162 (void)ConstCopyAssignOp;
1163
John McCall183700f2009-09-21 23:43:11 +00001164 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001165 const llvm::Type *LTy =
1166 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001167 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001168 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001170 CallArgList CallArgs;
1171 // Push the this (Dest) ptr.
1172 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1173 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001175 // Push the Src ptr.
1176 CallArgs.push_back(std::make_pair(RValue::get(Src),
1177 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001178 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001179 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001180 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1181 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001182}
1183
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001184/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001185void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001186CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1187 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001188 llvm::Function *Fn,
1189 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001190 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1191 SourceLocation());
1192 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001193 FinishFunction();
1194}
1195
Mike Stump79d57682009-11-04 01:11:15 +00001196/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1197/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001198/// The implicitly-defined copy constructor for class X performs a memberwise
Mike Stump79d57682009-11-04 01:11:15 +00001199/// copy of its subobjects. The order of copying is the same as the order of
1200/// initialization of bases and members in a user-defined constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001201/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001202/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001203/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001204/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001205/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001206/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001207/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001208/// Virtual base class subobjects shall be copied only once by the
1209/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001210
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001211void
1212CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1213 CXXCtorType Type,
1214 llvm::Function *Fn,
1215 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001216 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001217 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Mike Stump79d57682009-11-04 01:11:15 +00001218 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001219 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1220 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001222 FunctionArgList::const_iterator i = Args.begin();
1223 const VarDecl *ThisArg = i->first;
1224 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1225 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1226 const VarDecl *SrcArg = (i+1)->first;
1227 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1228 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001230 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1231 Base != ClassDecl->bases_end(); ++Base) {
1232 // FIXME. copy constrution of virtual base NYI
1233 if (Base->isVirtual())
1234 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001236 CXXRecordDecl *BaseClassDecl
1237 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001238 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1239 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001240 }
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001242 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1243 FieldEnd = ClassDecl->field_end();
1244 Field != FieldEnd; ++Field) {
1245 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001246 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001247 getContext().getAsConstantArrayType(FieldType);
1248 if (Array)
1249 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001251 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1252 CXXRecordDecl *FieldClassDecl
1253 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1254 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1255 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001256 if (Array) {
1257 const llvm::Type *BasePtr = ConvertType(FieldType);
1258 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001259 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001260 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001261 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001262 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1263 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1264 FieldClassDecl, FieldType);
1265 }
Mike Stump1eb44332009-09-09 15:08:12 +00001266 else
1267 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001268 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001269 continue;
1270 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001271 // Do a built-in assignment of scalar data members.
1272 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1273 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1274 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1275 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001276 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001277 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001278}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001279
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001280/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001281/// Before the implicitly-declared copy assignment operator for a class is
1282/// implicitly defined, all implicitly- declared copy assignment operators for
1283/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001284/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001285/// The implicitly-defined copy assignment operator for class X performs
1286/// memberwise assignment of its subob- jects. The direct base classes of X are
1287/// assigned first, in the order of their declaration in
1288/// the base-specifier-list, and then the immediate nonstatic data members of X
1289/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001290/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001291/// if the subobject is of class type, the copy assignment operator for the
1292/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001293/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001294///
Mike Stump1eb44332009-09-09 15:08:12 +00001295/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001296/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001297///
Mike Stump1eb44332009-09-09 15:08:12 +00001298/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001299/// used.
1300void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001301 llvm::Function *Fn,
1302 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001303
1304 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1305 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1306 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001307 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001309 FunctionArgList::const_iterator i = Args.begin();
1310 const VarDecl *ThisArg = i->first;
1311 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1312 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1313 const VarDecl *SrcArg = (i+1)->first;
1314 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1315 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001317 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1318 Base != ClassDecl->bases_end(); ++Base) {
1319 // FIXME. copy assignment of virtual base NYI
1320 if (Base->isVirtual())
1321 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001323 CXXRecordDecl *BaseClassDecl
1324 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1325 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1326 Base->getType());
1327 }
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001329 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1330 FieldEnd = ClassDecl->field_end();
1331 Field != FieldEnd; ++Field) {
1332 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001333 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001334 getContext().getAsConstantArrayType(FieldType);
1335 if (Array)
1336 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001338 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1339 CXXRecordDecl *FieldClassDecl
1340 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1341 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1342 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001343 if (Array) {
1344 const llvm::Type *BasePtr = ConvertType(FieldType);
1345 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1346 llvm::Value *DestBaseAddrPtr =
1347 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1348 llvm::Value *SrcBaseAddrPtr =
1349 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1350 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1351 FieldClassDecl, FieldType);
1352 }
1353 else
Mike Stump1eb44332009-09-09 15:08:12 +00001354 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001355 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001356 continue;
1357 }
1358 // Do a built-in assignment of scalar data members.
1359 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1360 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1361 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1362 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001363 }
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001365 // return *this;
1366 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001368 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001369}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001370
Anders Carlssonb1156b92009-11-06 03:23:06 +00001371static void EmitBaseInitializer(CodeGenFunction &CGF,
1372 const CXXRecordDecl *ClassDecl,
1373 CXXBaseOrMemberInitializer *BaseInit,
1374 CXXCtorType CtorType) {
1375 assert(BaseInit->isBaseInitializer() &&
1376 "Must have base initializer!");
1377
1378 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1379
1380 const Type *BaseType = BaseInit->getBaseClass();
1381 CXXRecordDecl *BaseClassDecl =
1382 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
1383 llvm::Value *V = CGF.GetAddressCXXOfBaseClass(ThisPtr, ClassDecl,
1384 BaseClassDecl,
1385 /*NullCheckValue=*/false);
1386 CGF.EmitCXXConstructorCall(BaseInit->getConstructor(),
1387 CtorType, V,
1388 BaseInit->const_arg_begin(),
1389 BaseInit->const_arg_end());
1390}
1391
1392static void EmitMemberInitializer(CodeGenFunction &CGF,
1393 const CXXRecordDecl *ClassDecl,
1394 CXXBaseOrMemberInitializer *MemberInit) {
1395 assert(MemberInit->isMemberInitializer() &&
1396 "Must have member initializer!");
1397
1398 // non-static data member initializers.
1399 FieldDecl *Field = MemberInit->getMember();
1400 QualType FieldType = CGF.getContext().getCanonicalType((Field)->getType());
1401 const ConstantArrayType *Array =
1402 CGF.getContext().getAsConstantArrayType(FieldType);
1403 if (Array)
1404 FieldType = CGF.getContext().getBaseElementType(FieldType);
1405
1406 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1407 LValue LHS;
1408 if (FieldType->isReferenceType()) {
1409 // FIXME: This is really ugly; should be refactored somehow
1410 unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field);
1411 llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp");
1412 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1413 LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType));
1414 } else {
1415 LHS = CGF.EmitLValueForField(ThisPtr, Field, false, 0);
1416 }
1417 if (FieldType->getAs<RecordType>()) {
1418 if (!Field->isAnonymousStructOrUnion()) {
1419 assert(MemberInit->getConstructor() &&
1420 "EmitCtorPrologue - no constructor to initialize member");
1421 if (Array) {
1422 const llvm::Type *BasePtr = CGF.ConvertType(FieldType);
1423 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1424 llvm::Value *BaseAddrPtr =
1425 CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1426 CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(),
1427 Array, BaseAddrPtr);
1428 }
1429 else
1430 CGF.EmitCXXConstructorCall(MemberInit->getConstructor(),
1431 Ctor_Complete, LHS.getAddress(),
1432 MemberInit->const_arg_begin(),
1433 MemberInit->const_arg_end());
1434 return;
1435 }
1436 else {
1437 // Initializing an anonymous union data member.
1438 FieldDecl *anonMember = MemberInit->getAnonUnionMember();
1439 LHS = CGF.EmitLValueForField(LHS.getAddress(), anonMember,
1440 /*IsUnion=*/true, 0);
1441 FieldType = anonMember->getType();
1442 }
1443 }
1444
1445 assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only");
1446 Expr *RhsExpr = *MemberInit->arg_begin();
1447 RValue RHS;
1448 if (FieldType->isReferenceType())
1449 RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType,
1450 /*IsInitializer=*/true);
1451 else if (FieldType->isMemberFunctionPointerType())
1452 RHS = RValue::get(CGF.CGM.EmitConstantExpr(RhsExpr, FieldType, &CGF));
1453 else
1454 RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true));
1455 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
1456}
1457
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001458/// EmitCtorPrologue - This routine generates necessary code to initialize
1459/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001460/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001461void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1462 CXXCtorType CtorType) {
Anders Carlssonb1156b92009-11-06 03:23:06 +00001463 const CXXRecordDecl *ClassDecl = CD->getParent();
1464
Mike Stumpeb19fa92009-08-06 13:41:24 +00001465 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001466 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001468 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001469 E = CD->init_end();
1470 B != E; ++B) {
1471 CXXBaseOrMemberInitializer *Member = (*B);
Anders Carlssonb1156b92009-11-06 03:23:06 +00001472
Anders Carlsson1faf6742009-11-06 04:11:09 +00001473 assert(LiveTemporaries.empty() &&
1474 "Should not have any live temporaries at initializer start!");
1475
Anders Carlssonb1156b92009-11-06 03:23:06 +00001476 if (Member->isBaseInitializer())
1477 EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
1478 else
1479 EmitMemberInitializer(*this, ClassDecl, Member);
Anders Carlsson1faf6742009-11-06 04:11:09 +00001480
1481 // Pop any live temporaries that the initializers might have pushed.
1482 while (!LiveTemporaries.empty())
1483 PopCXXTemporary();
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001484 }
Mike Stumpf1216772009-07-31 18:25:34 +00001485
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001486 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001487 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001488 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001489 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001490 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001491 ClassDecl->bases_begin();
1492 Base != ClassDecl->bases_end(); ++Base) {
1493 // FIXME. copy assignment of virtual base NYI
1494 if (Base->isVirtual())
1495 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001497 CXXRecordDecl *BaseClassDecl
1498 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1499 if (BaseClassDecl->hasTrivialConstructor())
1500 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001501 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001502 BaseClassDecl->getDefaultConstructor(getContext())) {
1503 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001504 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1505 BaseClassDecl,
1506 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001507 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1508 }
1509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001511 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1512 FieldEnd = ClassDecl->field_end();
1513 Field != FieldEnd; ++Field) {
1514 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001515 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001516 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001517 if (Array)
1518 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001519 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1520 continue;
1521 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001522 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001523 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1524 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1525 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001526 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001527 MemberClassDecl->getDefaultConstructor(getContext())) {
1528 LoadOfThis = LoadCXXThis();
1529 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001530 if (Array) {
1531 const llvm::Type *BasePtr = ConvertType(FieldType);
1532 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001533 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001534 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1535 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1536 }
1537 else
Mike Stump1eb44332009-09-09 15:08:12 +00001538 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001539 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001540 }
1541 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001542 }
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Mike Stumpf1216772009-07-31 18:25:34 +00001544 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001545 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001546 if (!LoadOfThis)
1547 LoadOfThis = LoadCXXThis();
1548 llvm::Value *VtableField;
1549 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001550 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001551 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1552 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1553 llvm::Value *vtable = GenerateVtable(ClassDecl);
1554 Builder.CreateStore(vtable, VtableField);
1555 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001556}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001557
1558/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001559/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001560/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001561/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001562void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1563 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001564 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001565 assert(!ClassDecl->getNumVBases() &&
1566 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001567 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001569 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1570 *E = DD->destr_end(); B != E; ++B) {
1571 uintptr_t BaseOrMember = (*B);
1572 if (DD->isMemberToDestroy(BaseOrMember)) {
1573 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1574 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001575 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001576 getContext().getAsConstantArrayType(FieldType);
1577 if (Array)
1578 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001579 const RecordType *RT = FieldType->getAs<RecordType>();
1580 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1581 if (FieldClassDecl->hasTrivialDestructor())
1582 continue;
1583 llvm::Value *LoadOfThis = LoadCXXThis();
1584 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001585 if (Array) {
1586 const llvm::Type *BasePtr = ConvertType(FieldType);
1587 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001588 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001589 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001590 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001591 Array, BaseAddrPtr);
1592 }
1593 else
1594 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1595 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001596 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001597 const RecordType *RT =
1598 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1599 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1600 if (BaseClassDecl->hasTrivialDestructor())
1601 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001602 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1603 ClassDecl, BaseClassDecl,
1604 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001605 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001606 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001607 }
1608 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001609 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1610 return;
1611 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001612 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001613 // reverse order of their construction.
1614 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001616 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1617 FieldEnd = ClassDecl->field_end();
1618 Field != FieldEnd; ++Field) {
1619 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001620 if (getContext().getAsConstantArrayType(FieldType))
1621 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001622 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1623 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1624 if (FieldClassDecl->hasTrivialDestructor())
1625 continue;
1626 DestructedFields.push_back(*Field);
1627 }
1628 }
1629 if (!DestructedFields.empty())
1630 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1631 FieldDecl *Field = DestructedFields[i];
1632 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001633 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001634 getContext().getAsConstantArrayType(FieldType);
1635 if (Array)
1636 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001637 const RecordType *RT = FieldType->getAs<RecordType>();
1638 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1639 llvm::Value *LoadOfThis = LoadCXXThis();
1640 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001641 if (Array) {
1642 const llvm::Type *BasePtr = ConvertType(FieldType);
1643 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001644 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001645 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001646 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001647 Array, BaseAddrPtr);
1648 }
1649 else
1650 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1651 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001652 }
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001654 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1655 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1656 Base != ClassDecl->bases_end(); ++Base) {
1657 // FIXME. copy assignment of virtual base NYI
1658 if (Base->isVirtual())
1659 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001661 CXXRecordDecl *BaseClassDecl
1662 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1663 if (BaseClassDecl->hasTrivialDestructor())
1664 continue;
1665 DestructedBases.push_back(BaseClassDecl);
1666 }
1667 if (DestructedBases.empty())
1668 return;
1669 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1670 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001671 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1672 ClassDecl,BaseClassDecl,
1673 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001674 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1675 Dtor_Complete, V);
1676 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001677}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001678
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001679void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1680 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001681 llvm::Function *Fn,
1682 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001684 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001685 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1686 "SynthesizeDefaultDestructor - destructor has user declaration");
1687 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001689 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1690 SourceLocation());
1691 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001692 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001693}
Anders Carlsson6815e942009-09-27 18:58:34 +00001694
1695// FIXME: Move this to CGCXXStmt.cpp
1696void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1697 // FIXME: We need to do more here.
1698 EmitStmt(S.getTryBlock());
1699}