blob: 95f073c5708ba756b53897d7b7f04d2f9969fc54 [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
Owen Anderson0032b272009-08-13 21:57:51 +000099 const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000100 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000102 // Create our global initialization function.
103 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000104 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000105 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
106 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000108 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000109 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000110 CXXGlobalInits.size());
111 AddGlobalCtor(Fn);
112}
113
114void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
115 const VarDecl **Decls,
116 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000117 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000118 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000120 for (unsigned i = 0; i != NumDecls; ++i) {
121 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000123 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
124 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
125 }
126 FinishFunction();
127}
128
Mike Stump1eb44332009-09-09 15:08:12 +0000129void
130CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000131 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000132 // FIXME: This should use __cxa_guard_{acquire,release}?
133
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000134 assert(!getContext().getLangOptions().ThreadsafeStatics &&
135 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000136
Anders Carlsson283a0622009-04-13 18:03:33 +0000137 llvm::SmallString<256> GuardVName;
138 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000139 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000141 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000142 llvm::GlobalValue *GuardV =
Owen Anderson0032b272009-08-13 21:57:51 +0000143 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000144 GV->getLinkage(),
Owen Anderson0032b272009-08-13 21:57:51 +0000145 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000146 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000148 // Load the first byte of the guard variable.
Owen Anderson0032b272009-08-13 21:57:51 +0000149 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000150 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000151 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000153 // Compare it against 0.
Owen Anderson0032b272009-08-13 21:57:51 +0000154 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000155 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Daniel Dunbar55e87422008-11-11 02:29:29 +0000157 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000158 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000159
160 // If the guard variable is 0, jump to the initializer code.
161 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000163 EmitBlock(InitBlock);
164
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000165 EmitCXXGlobalVarDeclInit(D, GV);
166
Owen Anderson0032b272009-08-13 21:57:51 +0000167 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000168 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000171}
172
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000173RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
174 llvm::Value *Callee,
175 llvm::Value *This,
176 CallExpr::const_arg_iterator ArgBeg,
177 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000178 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000179 "Trying to emit a member call expr on a static method!");
180
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000181 // A call to a trivial destructor requires no code generation.
182 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
183 if (Destructor->isTrivial())
184 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
John McCall183700f2009-09-21 23:43:11 +0000186 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000188 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000190 // Push the this ptr.
191 Args.push_back(std::make_pair(RValue::get(This),
192 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000194 // And the rest of the call args
195 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
John McCall183700f2009-09-21 23:43:11 +0000197 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000198 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
199 Callee, Args, MD);
200}
201
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000202/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
203/// expr can be devirtualized.
204static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
205 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
206 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
207 // This is a record decl. We know the type and can devirtualize it.
208 return VD->getType()->isRecordType();
209 }
Anders Carlsson76366482009-10-12 19:45:47 +0000210
211 return false;
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000212 }
213
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000214 // We can always devirtualize calls on temporary object expressions.
Anders Carlsson76366482009-10-12 19:45:47 +0000215 if (isa<CXXTemporaryObjectExpr>(Base))
216 return true;
217
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000218 // And calls on bound temporaries.
219 if (isa<CXXBindTemporaryExpr>(Base))
220 return true;
221
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000222 // Check if this is a call expr that returns a record type.
223 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
224 return CE->getCallReturnType()->isRecordType();
225
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000226 // We can't devirtualize the call.
227 return false;
228}
229
Anders Carlsson774e7c62009-04-03 22:50:24 +0000230RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000231 if (isa<BinaryOperator>(CE->getCallee()))
232 return EmitCXXMemberPointerCallExpr(CE);
233
Anders Carlsson774e7c62009-04-03 22:50:24 +0000234 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
235 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000236
Anders Carlsson2472bf02009-09-29 03:54:11 +0000237 if (MD->isStatic()) {
238 // The method is static, emit it as we would a regular call.
239 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
240 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
241 CE->arg_begin(), CE->arg_end(), 0);
242
243 }
244
John McCall183700f2009-09-21 23:43:11 +0000245 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000246
Mike Stump1eb44332009-09-09 15:08:12 +0000247 const llvm::Type *Ty =
248 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000249 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000250 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Anders Carlsson774e7c62009-04-03 22:50:24 +0000252 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000253 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000254 else {
255 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000256 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000257 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000258
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000259 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000260 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000261 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000262 //
263 // We also don't emit a virtual call if the base expression has a record type
264 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000265 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000266 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000267 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000268 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000269 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000270 = dyn_cast<CXXDestructorDecl>(MD))
271 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000272 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000273 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
275 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000276 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000277}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000278
Mike Stump1eb44332009-09-09 15:08:12 +0000279RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000280CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
281 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson3eea6352009-10-13 17:41:28 +0000282 const Expr *BaseExpr = BO->getLHS();
283 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000284
Anders Carlsson3eea6352009-10-13 17:41:28 +0000285 const MemberPointerType *MPT =
286 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000287 const FunctionProtoType *FPT =
288 MPT->getPointeeType()->getAs<FunctionProtoType>();
289 const CXXRecordDecl *RD =
290 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl());
291
292 const llvm::FunctionType *FTy =
293 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
294 FPT->isVariadic());
295
296 const llvm::Type *Int8PtrTy =
297 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
298
299 // Get the member function pointer.
300 llvm::Value *MemFnPtr =
Anders Carlsson3eea6352009-10-13 17:41:28 +0000301 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
302 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000303
304 // Emit the 'this' pointer.
305 llvm::Value *This;
306
307 if (BO->getOpcode() == BinaryOperator::PtrMemI)
308 This = EmitScalarExpr(BaseExpr);
309 else
310 This = EmitLValue(BaseExpr).getAddress();
311
312 // Adjust it.
313 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
314 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
315
316 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
317 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
318
319 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
320
321 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
322
323 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
324
325 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
326
327 // If the LSB in the function pointer is 1, the function pointer points to
328 // a virtual function.
329 llvm::Value *IsVirtual
330 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
331 "and");
332
333 IsVirtual = Builder.CreateTrunc(IsVirtual,
334 llvm::Type::getInt1Ty(VMContext));
335
336 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
337 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
338 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
339
340 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
341 EmitBlock(FnVirtual);
342
343 const llvm::Type *VTableTy =
344 FTy->getPointerTo()->getPointerTo()->getPointerTo();
345
346 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
347 VTable = Builder.CreateLoad(VTable);
348
349 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
350
351 // Since the function pointer is 1 plus the virtual table offset, we
352 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000353 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000354
355 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
356
357 EmitBranch(FnEnd);
358 EmitBlock(FnNonVirtual);
359
360 // If the function is not virtual, just load the pointer.
361 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
362 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
363
364 EmitBlock(FnEnd);
365
366 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
367 Callee->reserveOperandSpace(2);
368 Callee->addIncoming(VirtualFn, FnVirtual);
369 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
370
371 CallArgList Args;
372
373 QualType ThisType =
374 getContext().getPointerType(getContext().getTagDeclType(RD));
375
376 // Push the this ptr.
377 Args.push_back(std::make_pair(RValue::get(This), ThisType));
378
379 // And the rest of the call args
380 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
381 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
382 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
383 Callee, Args, 0);
384}
385
386RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000387CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
388 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000389 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000390 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Fariborz Jahanianad258832009-08-13 21:09:41 +0000392 if (MD->isCopyAssignment()) {
393 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
394 if (ClassDecl->hasTrivialCopyAssignment()) {
395 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
396 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
397 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
398 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
399 QualType Ty = E->getType();
400 EmitAggregateCopy(This, Src, Ty);
401 return RValue::get(This);
402 }
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
John McCall183700f2009-09-21 23:43:11 +0000405 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000406 const llvm::Type *Ty =
407 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000408 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000409 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Anders Carlsson0f294632009-05-27 04:18:27 +0000411 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Anders Carlsson0f294632009-05-27 04:18:27 +0000413 return EmitCXXMemberCall(MD, Callee, This,
414 E->arg_begin() + 1, E->arg_end());
415}
416
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000417llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000418 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000419 "Must be in a C++ member function decl to load 'this'");
420 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
421 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000423 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000424 // ans: See how CodeGenFunction::LoadObjCSelf() uses
425 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000426 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
427}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000428
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000429/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
430/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000431/// array.
432/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
433/// array type and 'ArrayPtr' points to the beginning fo the array.
434/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000435void
436CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000437 const ConstantArrayType *ArrayTy,
438 llvm::Value *ArrayPtr) {
439 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
440 llvm::Value * NumElements =
441 llvm::ConstantInt::get(SizeTy,
442 getContext().getConstantArrayElementCount(ArrayTy));
443
444 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
445}
446
447void
448CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
449 llvm::Value *NumElements,
450 llvm::Value *ArrayPtr) {
451 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000453 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000454 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
455 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
456 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000458 // Start the loop with a block that tests the condition.
459 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
460 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000462 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000464 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000466 // Generate: if (loop-index < number-of-elements fall to the loop body,
467 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000468 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000469 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000470 // If the condition is true, execute the body.
471 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000473 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000475 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000476 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000477 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000478 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
479 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000480 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000482 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000484 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000485 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000486 Counter = Builder.CreateLoad(IndexPtr);
487 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
488 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000490 // Finally, branch back up to the condition for the next iteration.
491 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000493 // Emit the fall-through block.
494 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000495}
496
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000497/// EmitCXXAggrDestructorCall - calls the default destructor on array
498/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000499void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000500CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
501 const ArrayType *Array,
502 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000503 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
504 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000505 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000506 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000507 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000508 // Create a temporary for the loop index and initialize it with count of
509 // array elements.
510 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
511 "loop.index");
512 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000513 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000514 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
515 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000517 // Start the loop with a block that tests the condition.
518 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
519 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000521 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000523 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000525 // Generate: if (loop-index != 0 fall to the loop body,
526 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000527 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000528 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
529 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
530 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
531 "isne");
532 // If the condition is true, execute the body.
533 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000535 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000537 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
538 // Inside the loop body, emit the constructor call on the array element.
539 Counter = Builder.CreateLoad(IndexPtr);
540 Counter = Builder.CreateSub(Counter, One);
541 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
542 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000544 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000546 // Emit the decrement of the loop counter.
547 Counter = Builder.CreateLoad(IndexPtr);
548 Counter = Builder.CreateSub(Counter, One, "dec");
549 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000551 // Finally, branch back up to the condition for the next iteration.
552 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000554 // Emit the fall-through block.
555 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000556}
557
558void
Mike Stump1eb44332009-09-09 15:08:12 +0000559CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
560 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000561 llvm::Value *This,
562 CallExpr::const_arg_iterator ArgBeg,
563 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000564 if (D->isCopyConstructor(getContext())) {
565 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
566 if (ClassDecl->hasTrivialCopyConstructor()) {
567 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
568 "EmitCXXConstructorCall - user declared copy constructor");
569 const Expr *E = (*ArgBeg);
570 QualType Ty = E->getType();
571 llvm::Value *Src = EmitLValue(E).getAddress();
572 EmitAggregateCopy(This, Src, Ty);
573 return;
574 }
575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000577 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
578
579 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000580}
581
Mike Stump1eb44332009-09-09 15:08:12 +0000582void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000583 CXXDtorType Type,
584 llvm::Value *This) {
585 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Anders Carlsson7267c162009-05-29 21:03:38 +0000587 EmitCXXMemberCall(D, Callee, This, 0, 0);
588}
589
Mike Stump1eb44332009-09-09 15:08:12 +0000590void
591CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000592 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000593 assert(Dest && "Must have a destination!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000594 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000595 const ConstantArrayType *Array =
596 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000597 // For a copy constructor, even if it is trivial, must fall thru so
598 // its argument is code-gen'ed.
599 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000600 QualType InitType = E->getType();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000601 if (Array)
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000602 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000603 const CXXRecordDecl *RD =
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000604 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000605 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000606 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000607 }
Mike Stump1eb44332009-09-09 15:08:12 +0000608 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000609 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000610 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000611 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000612 EmitAggExpr((*i), Dest, false);
613 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000614 }
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000615 if (Array) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000616 QualType BaseElementTy = getContext().getBaseElementType(Array);
617 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
618 BasePtr = llvm::PointerType::getUnqual(BasePtr);
619 llvm::Value *BaseAddrPtr =
620 Builder.CreateBitCast(Dest, BasePtr);
621 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
622 }
623 else
624 // Call the constructor.
625 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
626 E->arg_begin(), E->arg_end());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000627}
628
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000629void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000630 EmitGlobal(GlobalDecl(D, Ctor_Complete));
631 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000632}
Anders Carlsson363c1842009-04-16 23:57:24 +0000633
Mike Stump1eb44332009-09-09 15:08:12 +0000634void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000635 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlsson27ae5362009-04-17 01:58:57 +0000637 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000639 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Anders Carlsson27ae5362009-04-17 01:58:57 +0000641 SetFunctionDefinitionAttributes(D, Fn);
642 SetLLVMFunctionAttributesForDefinition(D, Fn);
643}
644
Anders Carlsson363c1842009-04-16 23:57:24 +0000645llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000646CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000647 CXXCtorType Type) {
648 const llvm::FunctionType *FTy =
649 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Anders Carlsson363c1842009-04-16 23:57:24 +0000651 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000652 return cast<llvm::Function>(
653 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000654}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000655
Mike Stump1eb44332009-09-09 15:08:12 +0000656const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000657 CXXCtorType Type) {
658 llvm::SmallString<256> Name;
659 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000660 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Anders Carlsson27ae5362009-04-17 01:58:57 +0000662 Name += '\0';
663 return UniqueMangledName(Name.begin(), Name.end());
664}
665
666void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000667 EmitCXXDestructor(D, Dtor_Complete);
668 EmitCXXDestructor(D, Dtor_Base);
669}
670
Mike Stump1eb44332009-09-09 15:08:12 +0000671void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000672 CXXDtorType Type) {
673 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000675 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Anders Carlsson27ae5362009-04-17 01:58:57 +0000677 SetFunctionDefinitionAttributes(D, Fn);
678 SetLLVMFunctionAttributesForDefinition(D, Fn);
679}
680
681llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000682CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000683 CXXDtorType Type) {
684 const llvm::FunctionType *FTy =
685 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Anders Carlsson27ae5362009-04-17 01:58:57 +0000687 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000688 return cast<llvm::Function>(
689 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000690}
691
Mike Stump1eb44332009-09-09 15:08:12 +0000692const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000693 CXXDtorType Type) {
694 llvm::SmallString<256> Name;
695 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000696 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Anders Carlsson27ae5362009-04-17 01:58:57 +0000698 Name += '\0';
699 return UniqueMangledName(Name.begin(), Name.end());
700}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000701
Mike Stumped032eb2009-09-04 18:27:16 +0000702llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
703 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000704 bool Extern, int64_t nv,
705 int64_t v) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000706 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stumped032eb2009-09-04 18:27:16 +0000707}
708
Mike Stumpc902d222009-11-03 16:59:27 +0000709llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
710 int64_t v) {
711 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
712 0);
713 const llvm::Type *OrigTy = V->getType();
714 if (nv) {
715 // Do the non-virtual adjustment
716 V = Builder.CreateBitCast(V, Ptr8Ty);
717 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
718 V = Builder.CreateBitCast(V, OrigTy);
719 }
720 if (v) {
721 // Do the virtual this adjustment
722 const llvm::Type *PtrDiffTy =
723 ConvertType(getContext().getPointerDiffType());
724 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
725 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
726 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
727 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
728 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
729 V = Builder.CreateLoad(V, "vtable");
730 llvm::Value *VTablePtr = V;
731 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
732 v /= LLVMPointerWidth/8;
733 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
734 V = Builder.CreateLoad(V);
735 V = Builder.CreateGEP(ThisVal, V);
736 V = Builder.CreateBitCast(V, OrigTy);
737 }
738 return V;
739}
740
Mike Stump6e319f62009-09-11 23:25:56 +0000741llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
742 const CXXMethodDecl *MD,
743 bool Extern,
744 int64_t nv_t,
745 int64_t v_t,
746 int64_t nv_r,
747 int64_t v_r) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000748 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000749
750 FunctionArgList Args;
751 ImplicitParamDecl *ThisDecl =
752 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
753 MD->getThisType(getContext()));
754 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
755 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
756 e = MD->param_end();
757 i != e; ++i) {
758 ParmVarDecl *D = *i;
759 Args.push_back(std::make_pair(D, D->getType()));
760 }
761 IdentifierInfo *II
762 = &CGM.getContext().Idents.get("__thunk_named_foo_");
763 FunctionDecl *FD = FunctionDecl::Create(getContext(),
764 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000765 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000766 Extern
767 ? FunctionDecl::Extern
768 : FunctionDecl::Static,
769 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000770 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
771
772 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000773 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
774 const llvm::Type *Ty =
775 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
776 FPT->isVariadic());
777 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000778 CallArgList CallArgs;
779
Mike Stump736529e2009-11-03 02:12:59 +0000780 QualType ArgType = MD->getThisType(getContext());
781 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stumpc902d222009-11-03 16:59:27 +0000782 if (nv_t || v_t)
783 // Do the this adjustment.
784 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stump736529e2009-11-03 02:12:59 +0000785 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
786
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000787 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
788 e = MD->param_end();
789 i != e; ++i) {
790 ParmVarDecl *D = *i;
791 QualType ArgType = D->getType();
792
793 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
794 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000795 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
796 }
797
Mike Stump4387cd92009-11-03 03:16:46 +0000798 // FIXME: be sure to call the right function when we thunk to a thunk
Mike Stumpf49ed942009-11-02 23:47:45 +0000799 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
800 Callee, CallArgs, MD);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000801 if (nv_r || v_r) {
Mike Stumpc902d222009-11-03 16:59:27 +0000802 // Do the return result adjustment.
803 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000804 }
805
Mike Stumpf49ed942009-11-02 23:47:45 +0000806 if (!ResultType->isVoidType())
807 EmitReturnOfRValue(RV, ResultType);
808
Mike Stump6e319f62009-09-11 23:25:56 +0000809 FinishFunction();
810 return Fn;
811}
812
Mike Stump77ca8f62009-09-05 07:20:32 +0000813llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
814 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000815 llvm::SmallString<256> OutName;
816 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000817 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000818 llvm::GlobalVariable::LinkageTypes linktype;
819 linktype = llvm::GlobalValue::WeakAnyLinkage;
820 if (!Extern)
821 linktype = llvm::GlobalValue::InternalLinkage;
822 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000823 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000824 const llvm::FunctionType *FTy =
825 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
826 FPT->isVariadic());
827
828 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
829 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000830 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000831 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
832 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
833 return m;
834}
835
Mike Stump6e319f62009-09-11 23:25:56 +0000836llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
837 bool Extern, int64_t nv_t,
838 int64_t v_t, int64_t nv_r,
839 int64_t v_r) {
840 llvm::SmallString<256> OutName;
841 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000842 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000843 llvm::GlobalVariable::LinkageTypes linktype;
844 linktype = llvm::GlobalValue::WeakAnyLinkage;
845 if (!Extern)
846 linktype = llvm::GlobalValue::InternalLinkage;
847 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000848 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000849 const llvm::FunctionType *FTy =
850 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
851 FPT->isVariadic());
852
853 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
854 &getModule());
855 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
856 v_r);
857 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
858 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
859 return m;
860}
861
Mike Stumpf0070db2009-08-26 20:46:33 +0000862llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000863CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
864 const CXXRecordDecl *ClassDecl,
865 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000866 const llvm::Type *Int8PtrTy =
867 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
868
869 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
870 Int8PtrTy->getPointerTo());
871 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
872
Anders Carlssondbd920c2009-10-11 22:13:54 +0000873 int64_t VBaseOffsetIndex =
874 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
875
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000876 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000877 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000878 const llvm::Type *PtrDiffTy =
879 ConvertType(getContext().getPointerDiffType());
880
881 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
882 PtrDiffTy->getPointerTo());
883
884 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
885
886 return VBaseOffset;
887}
888
889llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000890CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
891 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000892 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000893
Mike Stumpf0070db2009-08-26 20:46:33 +0000894 Ty = llvm::PointerType::get(Ty, 0);
895 Ty = llvm::PointerType::get(Ty, 0);
896 Ty = llvm::PointerType::get(Ty, 0);
897 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
898 vtbl = Builder.CreateLoad(vtbl);
899 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000900 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000901 vfn = Builder.CreateLoad(vfn);
902 return vfn;
903}
904
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000905/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
906/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
907/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000908// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000909void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000910 llvm::Value *Src,
911 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000912 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000913 QualType Ty) {
914 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
915 assert(CA && "VLA cannot be copied over");
916 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000918 // Create a temporary for the loop index and initialize it with 0.
919 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
920 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000921 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000922 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000923 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000924 // Start the loop with a block that tests the condition.
925 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
926 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000928 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000930 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
931 // Generate: if (loop-index < number-of-elements fall to the loop body,
932 // otherwise, go to the block after the for-loop.
933 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000934 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000935 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
936 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000937 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000938 "isless");
939 // If the condition is true, execute the body.
940 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000942 EmitBlock(ForBody);
943 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
944 // Inside the loop body, emit the constructor call on the array element.
945 Counter = Builder.CreateLoad(IndexPtr);
946 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
947 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
948 if (BitwiseCopy)
949 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000950 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000951 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000952 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000953 Ctor_Complete);
954 CallArgList CallArgs;
955 // Push the this (Dest) ptr.
956 CallArgs.push_back(std::make_pair(RValue::get(Dest),
957 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000959 // Push the Src ptr.
960 CallArgs.push_back(std::make_pair(RValue::get(Src),
961 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000962 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000963 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000964 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
965 Callee, CallArgs, BaseCopyCtor);
966 }
967 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000969 // Emit the increment of the loop counter.
970 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
971 Counter = Builder.CreateLoad(IndexPtr);
972 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
973 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000975 // Finally, branch back up to the condition for the next iteration.
976 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000978 // Emit the fall-through block.
979 EmitBlock(AfterFor, true);
980}
981
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000982/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000983/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000984/// bitwise assignment or via a copy assignment operator function call.
985/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000986void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000987 llvm::Value *Src,
988 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000989 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000990 QualType Ty) {
991 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
992 assert(CA && "VLA cannot be asssigned");
993 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000995 // Create a temporary for the loop index and initialize it with 0.
996 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
997 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000998 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000999 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1000 Builder.CreateStore(zeroConstant, IndexPtr, false);
1001 // Start the loop with a block that tests the condition.
1002 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1003 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001005 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001007 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1008 // Generate: if (loop-index < number-of-elements fall to the loop body,
1009 // otherwise, go to the block after the for-loop.
1010 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001011 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001012 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1013 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001014 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001015 "isless");
1016 // If the condition is true, execute the body.
1017 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001019 EmitBlock(ForBody);
1020 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1021 // Inside the loop body, emit the assignment operator call on array element.
1022 Counter = Builder.CreateLoad(IndexPtr);
1023 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1024 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1025 const CXXMethodDecl *MD = 0;
1026 if (BitwiseAssign)
1027 EmitAggregateCopy(Dest, Src, Ty);
1028 else {
1029 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1030 MD);
1031 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1032 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001033 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001034 const llvm::Type *LTy =
1035 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1036 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001037 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001039 CallArgList CallArgs;
1040 // Push the this (Dest) ptr.
1041 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1042 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001044 // Push the Src ptr.
1045 CallArgs.push_back(std::make_pair(RValue::get(Src),
1046 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001047 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001048 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1049 Callee, CallArgs, MD);
1050 }
1051 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001053 // Emit the increment of the loop counter.
1054 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1055 Counter = Builder.CreateLoad(IndexPtr);
1056 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1057 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001059 // Finally, branch back up to the condition for the next iteration.
1060 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001062 // Emit the fall-through block.
1063 EmitBlock(AfterFor, true);
1064}
1065
Fariborz Jahanianca283612009-08-07 23:51:33 +00001066/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1067/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001068/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001069void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001070 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001071 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001072 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1073 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001074 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1075 /*NullCheckValue=*/false);
1076 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1077 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001078 }
1079 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1080 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001081 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001082 }
Mike Stump1eb44332009-09-09 15:08:12 +00001083
1084 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001085 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001086 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001087 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001088 CallArgList CallArgs;
1089 // Push the this (Dest) ptr.
1090 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1091 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Fariborz Jahanianca283612009-08-07 23:51:33 +00001093 // Push the Src ptr.
1094 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001095 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001096 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001097 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001098 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1099 Callee, CallArgs, BaseCopyCtor);
1100 }
1101}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001102
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001103/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001104/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001105/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001106// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001107void CodeGenFunction::EmitClassCopyAssignment(
1108 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001109 const CXXRecordDecl *ClassDecl,
1110 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001111 QualType Ty) {
1112 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001113 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1114 /*NullCheckValue=*/false);
1115 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1116 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001117 }
1118 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1119 EmitAggregateCopy(Dest, Src, Ty);
1120 return;
1121 }
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001123 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001124 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001125 MD);
1126 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1127 (void)ConstCopyAssignOp;
1128
John McCall183700f2009-09-21 23:43:11 +00001129 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001130 const llvm::Type *LTy =
1131 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001132 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001133 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001135 CallArgList CallArgs;
1136 // Push the this (Dest) ptr.
1137 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1138 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001140 // Push the Src ptr.
1141 CallArgs.push_back(std::make_pair(RValue::get(Src),
1142 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001143 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001144 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001145 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1146 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001147}
1148
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001149/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001150void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001151CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1152 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001153 llvm::Function *Fn,
1154 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001155 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1156 SourceLocation());
1157 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001158 FinishFunction();
1159}
1160
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001161/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001162/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001163/// The implicitly-defined copy constructor for class X performs a memberwise
1164/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001165/// of initialization of bases and members in a user-defined constructor
1166/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001167/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001168/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001169/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001170/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001171/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001172/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001173/// Virtual base class subobjects shall be copied only once by the
1174/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001175
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001176void
1177CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1178 CXXCtorType Type,
1179 llvm::Function *Fn,
1180 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001181 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001182 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001183 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001184 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1185 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001187 FunctionArgList::const_iterator i = Args.begin();
1188 const VarDecl *ThisArg = i->first;
1189 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1190 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1191 const VarDecl *SrcArg = (i+1)->first;
1192 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1193 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001195 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1196 Base != ClassDecl->bases_end(); ++Base) {
1197 // FIXME. copy constrution of virtual base NYI
1198 if (Base->isVirtual())
1199 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001201 CXXRecordDecl *BaseClassDecl
1202 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001203 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1204 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001207 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1208 FieldEnd = ClassDecl->field_end();
1209 Field != FieldEnd; ++Field) {
1210 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001211 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001212 getContext().getAsConstantArrayType(FieldType);
1213 if (Array)
1214 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001216 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1217 CXXRecordDecl *FieldClassDecl
1218 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1219 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1220 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001221 if (Array) {
1222 const llvm::Type *BasePtr = ConvertType(FieldType);
1223 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001224 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001225 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001226 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001227 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1228 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1229 FieldClassDecl, FieldType);
1230 }
Mike Stump1eb44332009-09-09 15:08:12 +00001231 else
1232 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001233 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001234 continue;
1235 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001236 // Do a built-in assignment of scalar data members.
1237 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1238 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1239 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1240 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001241 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001242 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001243}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001244
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001245/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001246/// Before the implicitly-declared copy assignment operator for a class is
1247/// implicitly defined, all implicitly- declared copy assignment operators for
1248/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001249/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001250/// The implicitly-defined copy assignment operator for class X performs
1251/// memberwise assignment of its subob- jects. The direct base classes of X are
1252/// assigned first, in the order of their declaration in
1253/// the base-specifier-list, and then the immediate nonstatic data members of X
1254/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001255/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001256/// if the subobject is of class type, the copy assignment operator for the
1257/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001258/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001259///
Mike Stump1eb44332009-09-09 15:08:12 +00001260/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001261/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001262///
Mike Stump1eb44332009-09-09 15:08:12 +00001263/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001264/// used.
1265void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001266 llvm::Function *Fn,
1267 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001268
1269 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1270 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1271 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001272 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001274 FunctionArgList::const_iterator i = Args.begin();
1275 const VarDecl *ThisArg = i->first;
1276 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1277 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1278 const VarDecl *SrcArg = (i+1)->first;
1279 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1280 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001282 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1283 Base != ClassDecl->bases_end(); ++Base) {
1284 // FIXME. copy assignment of virtual base NYI
1285 if (Base->isVirtual())
1286 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001288 CXXRecordDecl *BaseClassDecl
1289 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1290 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1291 Base->getType());
1292 }
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001294 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1295 FieldEnd = ClassDecl->field_end();
1296 Field != FieldEnd; ++Field) {
1297 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001298 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001299 getContext().getAsConstantArrayType(FieldType);
1300 if (Array)
1301 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001303 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1304 CXXRecordDecl *FieldClassDecl
1305 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1306 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1307 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001308 if (Array) {
1309 const llvm::Type *BasePtr = ConvertType(FieldType);
1310 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1311 llvm::Value *DestBaseAddrPtr =
1312 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1313 llvm::Value *SrcBaseAddrPtr =
1314 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1315 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1316 FieldClassDecl, FieldType);
1317 }
1318 else
Mike Stump1eb44332009-09-09 15:08:12 +00001319 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001320 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001321 continue;
1322 }
1323 // Do a built-in assignment of scalar data members.
1324 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1325 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1326 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1327 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001328 }
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001330 // return *this;
1331 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001333 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001334}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001335
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001336/// EmitCtorPrologue - This routine generates necessary code to initialize
1337/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001338/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001339void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1340 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001341 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001342 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001343 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001345 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001346 E = CD->init_end();
1347 B != E; ++B) {
1348 CXXBaseOrMemberInitializer *Member = (*B);
1349 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001350 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001351 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001352 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001353 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001354 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1355 BaseClassDecl,
1356 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001357 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001358 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001359 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001360 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001361 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001362 // non-static data member initilaizers.
1363 FieldDecl *Field = Member->getMember();
1364 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001365 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001366 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001367 if (Array)
1368 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Mike Stumpf1216772009-07-31 18:25:34 +00001370 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001371 LValue LHS;
1372 if (FieldType->isReferenceType()) {
1373 // FIXME: This is really ugly; should be refactored somehow
1374 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1375 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001376 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1377 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001378 } else {
1379 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1380 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001381 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001382 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001383 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001384 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001385 if (Array) {
1386 const llvm::Type *BasePtr = ConvertType(FieldType);
1387 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001388 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001389 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001390 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001391 Array, BaseAddrPtr);
1392 }
1393 else
1394 EmitCXXConstructorCall(Member->getConstructor(),
1395 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001396 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001397 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001398 continue;
1399 }
1400 else {
1401 // Initializing an anonymous union data member.
1402 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001403 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001404 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001405 FieldType = anonMember->getType();
1406 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001407 }
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001409 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001410 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001411 RValue RHS;
1412 if (FieldType->isReferenceType())
1413 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1414 /*IsInitializer=*/true);
1415 else
1416 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1417 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001418 }
1419 }
Mike Stumpf1216772009-07-31 18:25:34 +00001420
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001421 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001422 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001423 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001424 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001425 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001426 ClassDecl->bases_begin();
1427 Base != ClassDecl->bases_end(); ++Base) {
1428 // FIXME. copy assignment of virtual base NYI
1429 if (Base->isVirtual())
1430 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001432 CXXRecordDecl *BaseClassDecl
1433 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1434 if (BaseClassDecl->hasTrivialConstructor())
1435 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001436 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001437 BaseClassDecl->getDefaultConstructor(getContext())) {
1438 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001439 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1440 BaseClassDecl,
1441 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001442 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1443 }
1444 }
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001446 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1447 FieldEnd = ClassDecl->field_end();
1448 Field != FieldEnd; ++Field) {
1449 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001450 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001451 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001452 if (Array)
1453 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001454 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1455 continue;
1456 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001457 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001458 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1459 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1460 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001461 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001462 MemberClassDecl->getDefaultConstructor(getContext())) {
1463 LoadOfThis = LoadCXXThis();
1464 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001465 if (Array) {
1466 const llvm::Type *BasePtr = ConvertType(FieldType);
1467 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001468 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001469 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1470 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1471 }
1472 else
Mike Stump1eb44332009-09-09 15:08:12 +00001473 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001474 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001475 }
1476 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001477 }
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Mike Stumpf1216772009-07-31 18:25:34 +00001479 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001480 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001481 if (!LoadOfThis)
1482 LoadOfThis = LoadCXXThis();
1483 llvm::Value *VtableField;
1484 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001485 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001486 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1487 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1488 llvm::Value *vtable = GenerateVtable(ClassDecl);
1489 Builder.CreateStore(vtable, VtableField);
1490 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001491}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001492
1493/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001494/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001495/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001496/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001497void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1498 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001499 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001500 assert(!ClassDecl->getNumVBases() &&
1501 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001502 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001504 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1505 *E = DD->destr_end(); B != E; ++B) {
1506 uintptr_t BaseOrMember = (*B);
1507 if (DD->isMemberToDestroy(BaseOrMember)) {
1508 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1509 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001510 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001511 getContext().getAsConstantArrayType(FieldType);
1512 if (Array)
1513 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001514 const RecordType *RT = FieldType->getAs<RecordType>();
1515 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1516 if (FieldClassDecl->hasTrivialDestructor())
1517 continue;
1518 llvm::Value *LoadOfThis = LoadCXXThis();
1519 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001520 if (Array) {
1521 const llvm::Type *BasePtr = ConvertType(FieldType);
1522 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001523 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001524 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001525 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001526 Array, BaseAddrPtr);
1527 }
1528 else
1529 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1530 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001531 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001532 const RecordType *RT =
1533 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1534 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1535 if (BaseClassDecl->hasTrivialDestructor())
1536 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001537 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1538 ClassDecl, BaseClassDecl,
1539 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001540 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001541 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001542 }
1543 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001544 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1545 return;
1546 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001547 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001548 // reverse order of their construction.
1549 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001551 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1552 FieldEnd = ClassDecl->field_end();
1553 Field != FieldEnd; ++Field) {
1554 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001555 if (getContext().getAsConstantArrayType(FieldType))
1556 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001557 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1558 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1559 if (FieldClassDecl->hasTrivialDestructor())
1560 continue;
1561 DestructedFields.push_back(*Field);
1562 }
1563 }
1564 if (!DestructedFields.empty())
1565 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1566 FieldDecl *Field = DestructedFields[i];
1567 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001568 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001569 getContext().getAsConstantArrayType(FieldType);
1570 if (Array)
1571 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001572 const RecordType *RT = FieldType->getAs<RecordType>();
1573 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1574 llvm::Value *LoadOfThis = LoadCXXThis();
1575 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001576 if (Array) {
1577 const llvm::Type *BasePtr = ConvertType(FieldType);
1578 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001579 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001580 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001581 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001582 Array, BaseAddrPtr);
1583 }
1584 else
1585 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1586 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001587 }
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001589 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1590 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1591 Base != ClassDecl->bases_end(); ++Base) {
1592 // FIXME. copy assignment of virtual base NYI
1593 if (Base->isVirtual())
1594 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001596 CXXRecordDecl *BaseClassDecl
1597 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1598 if (BaseClassDecl->hasTrivialDestructor())
1599 continue;
1600 DestructedBases.push_back(BaseClassDecl);
1601 }
1602 if (DestructedBases.empty())
1603 return;
1604 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1605 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001606 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1607 ClassDecl,BaseClassDecl,
1608 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001609 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1610 Dtor_Complete, V);
1611 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001612}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001613
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001614void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1615 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001616 llvm::Function *Fn,
1617 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001619 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001620 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1621 "SynthesizeDefaultDestructor - destructor has user declaration");
1622 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001624 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1625 SourceLocation());
1626 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001627 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001628}
Anders Carlsson6815e942009-09-27 18:58:34 +00001629
1630// FIXME: Move this to CGCXXStmt.cpp
1631void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1632 // FIXME: We need to do more here.
1633 EmitStmt(S.getTryBlock());
1634}