blob: bb8041f567cd687d86d8d8697bacc9521dc78944 [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
Fariborz Jahanian88f42802009-11-10 19:24:06 +000030CodeGenFunction::EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000031 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");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000058 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
59 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
60 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
61 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
62}
63
Mike Stump1eb44332009-09-09 15:08:12 +000064void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000065 llvm::Constant *DeclPtr) {
66 assert(D.hasGlobalStorage() &&
67 "VarDecl must have global storage!");
Mike Stump1eb44332009-09-09 15:08:12 +000068
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000069 const Expr *Init = D.getInit();
70 QualType T = D.getType();
Mike Stumpdf317bf2009-11-03 23:25:48 +000071 bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified();
Mike Stump1eb44332009-09-09 15:08:12 +000072
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000073 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000074 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000075 } else if (!hasAggregateLLVMType(T)) {
76 llvm::Value *V = EmitScalarExpr(Init);
Mike Stumpdf317bf2009-11-03 23:25:48 +000077 EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000078 } else if (T->isAnyComplexType()) {
Mike Stumpdf317bf2009-11-03 23:25:48 +000079 EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000080 } else {
Mike Stumpdf317bf2009-11-03 23:25:48 +000081 EmitAggExpr(Init, DeclPtr, isVolatile);
Fariborz Jahanian88f42802009-11-10 19:24:06 +000082 const ConstantArrayType *Array = getContext().getAsConstantArrayType(T);
83 if (Array)
84 T = getContext().getBaseElementType(Array);
85
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000086 if (const RecordType *RT = T->getAs<RecordType>()) {
87 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian88f42802009-11-10 19:24:06 +000088 if (!RD->hasTrivialDestructor()) {
89 llvm::Constant *DtorFn;
90 if (Array) {
91 DtorFn = CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(
92 RD->getDestructor(getContext()),
93 Array, DeclPtr);
94 DeclPtr =
95 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
96 }
97 else
98 DtorFn = CGM.GetAddrOfCXXDestructor(RD->getDestructor(getContext()),
99 Dtor_Complete);
100 EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
101 }
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000102 }
103 }
104}
105
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000106void
107CodeGenModule::EmitCXXGlobalInitFunc() {
108 if (CXXGlobalInits.empty())
109 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Mike Stump79d57682009-11-04 01:11:15 +0000111 const llvm::FunctionType *FTy
112 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
113 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000115 // Create our global initialization function.
116 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000117 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000118 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
119 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000121 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000122 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000123 CXXGlobalInits.size());
124 AddGlobalCtor(Fn);
125}
126
127void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
128 const VarDecl **Decls,
129 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000130 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000131 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000133 for (unsigned i = 0; i != NumDecls; ++i) {
134 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000136 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
137 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
138 }
139 FinishFunction();
140}
141
Mike Stump1eb44332009-09-09 15:08:12 +0000142void
143CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000144 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000145 // FIXME: This should use __cxa_guard_{acquire,release}?
146
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000147 assert(!getContext().getLangOptions().ThreadsafeStatics &&
148 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000149
Anders Carlsson283a0622009-04-13 18:03:33 +0000150 llvm::SmallString<256> GuardVName;
151 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000152 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000154 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000155 llvm::GlobalValue *GuardV =
Mike Stump79d57682009-11-04 01:11:15 +0000156 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext),
157 false, GV->getLinkage(),
158 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000159 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000161 // Load the first byte of the guard variable.
Mike Stump79d57682009-11-04 01:11:15 +0000162 const llvm::Type *PtrTy
163 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000164 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000165 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000167 // Compare it against 0.
Mike Stump79d57682009-11-04 01:11:15 +0000168 llvm::Value *nullValue
169 = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Daniel Dunbar55e87422008-11-11 02:29:29 +0000172 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000173 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000174
175 // If the guard variable is 0, jump to the initializer code.
176 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000178 EmitBlock(InitBlock);
179
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000180 EmitCXXGlobalVarDeclInit(D, GV);
181
Mike Stump79d57682009-11-04 01:11:15 +0000182 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
183 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000184 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000186 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000187}
188
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000189RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
190 llvm::Value *Callee,
191 llvm::Value *This,
192 CallExpr::const_arg_iterator ArgBeg,
193 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000194 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000195 "Trying to emit a member call expr on a static method!");
196
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000197 // A call to a trivial destructor requires no code generation.
198 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
199 if (Destructor->isTrivial())
200 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
John McCall183700f2009-09-21 23:43:11 +0000202 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000204 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000206 // Push the this ptr.
207 Args.push_back(std::make_pair(RValue::get(This),
208 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000210 // And the rest of the call args
211 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
John McCall183700f2009-09-21 23:43:11 +0000213 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000214 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
215 Callee, Args, MD);
216}
217
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000218/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
219/// expr can be devirtualized.
220static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
221 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
222 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
223 // This is a record decl. We know the type and can devirtualize it.
224 return VD->getType()->isRecordType();
225 }
Anders Carlsson76366482009-10-12 19:45:47 +0000226
227 return false;
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000228 }
229
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000230 // We can always devirtualize calls on temporary object expressions.
Anders Carlsson76366482009-10-12 19:45:47 +0000231 if (isa<CXXTemporaryObjectExpr>(Base))
232 return true;
233
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000234 // And calls on bound temporaries.
235 if (isa<CXXBindTemporaryExpr>(Base))
236 return true;
237
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000238 // Check if this is a call expr that returns a record type.
239 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
240 return CE->getCallReturnType()->isRecordType();
241
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000242 // We can't devirtualize the call.
243 return false;
244}
245
Anders Carlsson774e7c62009-04-03 22:50:24 +0000246RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000247 if (isa<BinaryOperator>(CE->getCallee()))
248 return EmitCXXMemberPointerCallExpr(CE);
249
Anders Carlsson774e7c62009-04-03 22:50:24 +0000250 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
251 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000252
Anders Carlsson2472bf02009-09-29 03:54:11 +0000253 if (MD->isStatic()) {
254 // The method is static, emit it as we would a regular call.
255 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
256 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
257 CE->arg_begin(), CE->arg_end(), 0);
258
259 }
260
John McCall183700f2009-09-21 23:43:11 +0000261 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000262
Mike Stump1eb44332009-09-09 15:08:12 +0000263 const llvm::Type *Ty =
264 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000265 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000266 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Anders Carlsson774e7c62009-04-03 22:50:24 +0000268 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000269 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000270 else {
271 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000272 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000273 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000274
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000275 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000276 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000277 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000278 //
279 // We also don't emit a virtual call if the base expression has a record type
280 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000281 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000282 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000283 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000284 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000285 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000286 = dyn_cast<CXXDestructorDecl>(MD))
287 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000288 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000289 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
291 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000292 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000293}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000294
Mike Stump1eb44332009-09-09 15:08:12 +0000295RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000296CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
297 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson3eea6352009-10-13 17:41:28 +0000298 const Expr *BaseExpr = BO->getLHS();
299 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000300
Anders Carlsson3eea6352009-10-13 17:41:28 +0000301 const MemberPointerType *MPT =
302 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000303 const FunctionProtoType *FPT =
304 MPT->getPointeeType()->getAs<FunctionProtoType>();
305 const CXXRecordDecl *RD =
Douglas Gregor87c12c42009-11-04 16:49:01 +0000306 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000307
308 const llvm::FunctionType *FTy =
309 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
310 FPT->isVariadic());
311
312 const llvm::Type *Int8PtrTy =
313 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
314
315 // Get the member function pointer.
316 llvm::Value *MemFnPtr =
Anders Carlsson3eea6352009-10-13 17:41:28 +0000317 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
318 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000319
320 // Emit the 'this' pointer.
321 llvm::Value *This;
322
323 if (BO->getOpcode() == BinaryOperator::PtrMemI)
324 This = EmitScalarExpr(BaseExpr);
325 else
326 This = EmitLValue(BaseExpr).getAddress();
327
328 // Adjust it.
329 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
330 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
331
332 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
333 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
334
335 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
336
337 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
338
339 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
340
341 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
342
343 // If the LSB in the function pointer is 1, the function pointer points to
344 // a virtual function.
345 llvm::Value *IsVirtual
346 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
347 "and");
348
349 IsVirtual = Builder.CreateTrunc(IsVirtual,
350 llvm::Type::getInt1Ty(VMContext));
351
352 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
353 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
354 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
355
356 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
357 EmitBlock(FnVirtual);
358
359 const llvm::Type *VTableTy =
360 FTy->getPointerTo()->getPointerTo()->getPointerTo();
361
362 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
363 VTable = Builder.CreateLoad(VTable);
364
365 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
366
367 // Since the function pointer is 1 plus the virtual table offset, we
368 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000369 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000370
371 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
372
373 EmitBranch(FnEnd);
374 EmitBlock(FnNonVirtual);
375
376 // If the function is not virtual, just load the pointer.
377 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
378 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
379
380 EmitBlock(FnEnd);
381
382 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
383 Callee->reserveOperandSpace(2);
384 Callee->addIncoming(VirtualFn, FnVirtual);
385 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
386
387 CallArgList Args;
388
389 QualType ThisType =
390 getContext().getPointerType(getContext().getTagDeclType(RD));
391
392 // Push the this ptr.
393 Args.push_back(std::make_pair(RValue::get(This), ThisType));
394
395 // And the rest of the call args
396 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
397 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
398 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
399 Callee, Args, 0);
400}
401
402RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000403CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
404 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000405 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000406 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Fariborz Jahanianad258832009-08-13 21:09:41 +0000408 if (MD->isCopyAssignment()) {
409 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
410 if (ClassDecl->hasTrivialCopyAssignment()) {
411 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
412 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
413 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
414 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
415 QualType Ty = E->getType();
416 EmitAggregateCopy(This, Src, Ty);
417 return RValue::get(This);
418 }
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
John McCall183700f2009-09-21 23:43:11 +0000421 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000422 const llvm::Type *Ty =
423 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000424 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000425 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlsson0f294632009-05-27 04:18:27 +0000427 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Anders Carlsson0f294632009-05-27 04:18:27 +0000429 return EmitCXXMemberCall(MD, Callee, This,
430 E->arg_begin() + 1, E->arg_end());
431}
432
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000433llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000434 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000435 "Must be in a C++ member function decl to load 'this'");
436 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
437 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000439 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000440 // ans: See how CodeGenFunction::LoadObjCSelf() uses
441 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000442 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
443}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000444
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000445/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
446/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000447/// array.
448/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
449/// array type and 'ArrayPtr' points to the beginning fo the array.
450/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000451void
452CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000453 const ConstantArrayType *ArrayTy,
454 llvm::Value *ArrayPtr) {
455 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
456 llvm::Value * NumElements =
457 llvm::ConstantInt::get(SizeTy,
458 getContext().getConstantArrayElementCount(ArrayTy));
459
460 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
461}
462
463void
464CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
465 llvm::Value *NumElements,
466 llvm::Value *ArrayPtr) {
467 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000469 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000470 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
471 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
472 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000474 // Start the loop with a block that tests the condition.
475 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
476 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000478 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000480 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000482 // Generate: if (loop-index < number-of-elements fall to the loop body,
483 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000484 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000485 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000486 // If the condition is true, execute the body.
487 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000489 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000491 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000492 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000493 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000494 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
495 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000496 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000498 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000500 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000501 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000502 Counter = Builder.CreateLoad(IndexPtr);
503 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
504 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000506 // Finally, branch back up to the condition for the next iteration.
507 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000509 // Emit the fall-through block.
510 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000511}
512
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000513/// EmitCXXAggrDestructorCall - calls the default destructor on array
514/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000515void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000516CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
517 const ArrayType *Array,
518 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000519 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
520 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000521 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000522 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000523 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000524 // Create a temporary for the loop index and initialize it with count of
525 // array elements.
526 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
527 "loop.index");
528 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000529 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000530 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
531 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000533 // Start the loop with a block that tests the condition.
534 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
535 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000537 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000539 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000541 // Generate: if (loop-index != 0 fall to the loop body,
542 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000543 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000544 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
545 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
546 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
547 "isne");
548 // If the condition is true, execute the body.
549 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000551 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000553 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
554 // Inside the loop body, emit the constructor call on the array element.
555 Counter = Builder.CreateLoad(IndexPtr);
556 Counter = Builder.CreateSub(Counter, One);
557 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
558 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000560 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000562 // Emit the decrement of the loop counter.
563 Counter = Builder.CreateLoad(IndexPtr);
564 Counter = Builder.CreateSub(Counter, One, "dec");
565 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000567 // Finally, branch back up to the condition for the next iteration.
568 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000570 // Emit the fall-through block.
571 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000572}
573
Fariborz Jahanian88f42802009-11-10 19:24:06 +0000574/// EmitCXXAggrDestructorCall - Generates a helper function which when invoked,
575/// calls the default destructor on array elements in reverse order of
576/// construction.
577llvm::Constant *
578CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
579 const ArrayType *Array,
580 llvm::Value *This) {
581 static int UniqueCount;
582 FunctionArgList Args;
583 ImplicitParamDecl *Dst =
584 ImplicitParamDecl::Create(getContext(), 0,
585 SourceLocation(), 0,
586 getContext().getPointerType(getContext().VoidTy));
587 Args.push_back(std::make_pair(Dst, Dst->getType()));
588
589 llvm::SmallString<16> Name;
590 llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueCount);
591 QualType R = getContext().VoidTy;
592 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args);
593 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
594 llvm::Function *Fn =
595 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
596 Name.c_str(),
597 &CGM.getModule());
598 IdentifierInfo *II
599 = &CGM.getContext().Idents.get(Name.c_str());
600 FunctionDecl *FD = FunctionDecl::Create(getContext(),
601 getContext().getTranslationUnitDecl(),
602 SourceLocation(), II, R, 0,
603 FunctionDecl::Static,
604 false, true);
605 StartFunction(FD, R, Fn, Args, SourceLocation());
606 QualType BaseElementTy = getContext().getBaseElementType(Array);
607 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
608 BasePtr = llvm::PointerType::getUnqual(BasePtr);
609 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
610 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
611 FinishFunction();
612 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
613 0);
614 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
615 return m;
616}
617
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000618void
Mike Stump1eb44332009-09-09 15:08:12 +0000619CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
620 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000621 llvm::Value *This,
622 CallExpr::const_arg_iterator ArgBeg,
623 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000624 if (D->isCopyConstructor(getContext())) {
625 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
626 if (ClassDecl->hasTrivialCopyConstructor()) {
627 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
628 "EmitCXXConstructorCall - user declared copy constructor");
629 const Expr *E = (*ArgBeg);
630 QualType Ty = E->getType();
631 llvm::Value *Src = EmitLValue(E).getAddress();
632 EmitAggregateCopy(This, Src, Ty);
633 return;
634 }
635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000637 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
638
639 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000640}
641
Mike Stump1eb44332009-09-09 15:08:12 +0000642void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000643 CXXDtorType Type,
644 llvm::Value *This) {
645 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Anders Carlsson7267c162009-05-29 21:03:38 +0000647 EmitCXXMemberCall(D, Callee, This, 0, 0);
648}
649
Mike Stump1eb44332009-09-09 15:08:12 +0000650void
651CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000652 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000653 assert(Dest && "Must have a destination!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000654 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000655 const ConstantArrayType *Array =
656 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000657 // For a copy constructor, even if it is trivial, must fall thru so
658 // its argument is code-gen'ed.
659 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000660 QualType InitType = E->getType();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000661 if (Array)
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000662 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000663 const CXXRecordDecl *RD =
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000664 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000665 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000666 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000669 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000670 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000671 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000672 EmitAggExpr((*i), Dest, false);
673 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000674 }
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000675 if (Array) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000676 QualType BaseElementTy = getContext().getBaseElementType(Array);
677 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
678 BasePtr = llvm::PointerType::getUnqual(BasePtr);
679 llvm::Value *BaseAddrPtr =
680 Builder.CreateBitCast(Dest, BasePtr);
681 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
682 }
683 else
684 // Call the constructor.
685 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
686 E->arg_begin(), E->arg_end());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000687}
688
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000689void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000690 EmitGlobal(GlobalDecl(D, Ctor_Complete));
691 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000692}
Anders Carlsson363c1842009-04-16 23:57:24 +0000693
Mike Stump1eb44332009-09-09 15:08:12 +0000694void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000695 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Anders Carlsson27ae5362009-04-17 01:58:57 +0000697 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000699 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Anders Carlsson27ae5362009-04-17 01:58:57 +0000701 SetFunctionDefinitionAttributes(D, Fn);
702 SetLLVMFunctionAttributesForDefinition(D, Fn);
703}
704
Anders Carlsson363c1842009-04-16 23:57:24 +0000705llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000706CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000707 CXXCtorType Type) {
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000708 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +0000709 const llvm::FunctionType *FTy =
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000710 getTypes().GetFunctionType(getTypes().getFunctionInfo(D),
711 FPT->isVariadic());
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Anders Carlsson363c1842009-04-16 23:57:24 +0000713 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000714 return cast<llvm::Function>(
715 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000716}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000717
Mike Stump1eb44332009-09-09 15:08:12 +0000718const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000719 CXXCtorType Type) {
720 llvm::SmallString<256> Name;
721 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000722 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Anders Carlsson27ae5362009-04-17 01:58:57 +0000724 Name += '\0';
725 return UniqueMangledName(Name.begin(), Name.end());
726}
727
728void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000729 EmitCXXDestructor(D, Dtor_Complete);
730 EmitCXXDestructor(D, Dtor_Base);
731}
732
Mike Stump1eb44332009-09-09 15:08:12 +0000733void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000734 CXXDtorType Type) {
735 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000737 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Anders Carlsson27ae5362009-04-17 01:58:57 +0000739 SetFunctionDefinitionAttributes(D, Fn);
740 SetLLVMFunctionAttributesForDefinition(D, Fn);
741}
742
743llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000744CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000745 CXXDtorType Type) {
746 const llvm::FunctionType *FTy =
747 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Anders Carlsson27ae5362009-04-17 01:58:57 +0000749 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000750 return cast<llvm::Function>(
751 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000752}
753
Mike Stump1eb44332009-09-09 15:08:12 +0000754const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000755 CXXDtorType Type) {
756 llvm::SmallString<256> Name;
757 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000758 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Anders Carlsson27ae5362009-04-17 01:58:57 +0000760 Name += '\0';
761 return UniqueMangledName(Name.begin(), Name.end());
762}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000763
Mike Stumped032eb2009-09-04 18:27:16 +0000764llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
765 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000766 bool Extern, int64_t nv,
767 int64_t v) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000768 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stumped032eb2009-09-04 18:27:16 +0000769}
770
Mike Stumpc902d222009-11-03 16:59:27 +0000771llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
772 int64_t v) {
773 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
774 0);
775 const llvm::Type *OrigTy = V->getType();
776 if (nv) {
777 // Do the non-virtual adjustment
778 V = Builder.CreateBitCast(V, Ptr8Ty);
779 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
780 V = Builder.CreateBitCast(V, OrigTy);
781 }
782 if (v) {
783 // Do the virtual this adjustment
784 const llvm::Type *PtrDiffTy =
785 ConvertType(getContext().getPointerDiffType());
786 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
787 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
788 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
789 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
790 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
791 V = Builder.CreateLoad(V, "vtable");
792 llvm::Value *VTablePtr = V;
793 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
794 v /= LLVMPointerWidth/8;
795 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
796 V = Builder.CreateLoad(V);
797 V = Builder.CreateGEP(ThisVal, V);
798 V = Builder.CreateBitCast(V, OrigTy);
799 }
800 return V;
801}
802
Mike Stump6e319f62009-09-11 23:25:56 +0000803llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
804 const CXXMethodDecl *MD,
805 bool Extern,
806 int64_t nv_t,
807 int64_t v_t,
808 int64_t nv_r,
809 int64_t v_r) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000810 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000811
812 FunctionArgList Args;
813 ImplicitParamDecl *ThisDecl =
814 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
815 MD->getThisType(getContext()));
816 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
817 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
818 e = MD->param_end();
819 i != e; ++i) {
820 ParmVarDecl *D = *i;
821 Args.push_back(std::make_pair(D, D->getType()));
822 }
823 IdentifierInfo *II
824 = &CGM.getContext().Idents.get("__thunk_named_foo_");
825 FunctionDecl *FD = FunctionDecl::Create(getContext(),
826 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000827 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000828 Extern
829 ? FunctionDecl::Extern
830 : FunctionDecl::Static,
831 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000832 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
833
834 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000835 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
836 const llvm::Type *Ty =
837 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
838 FPT->isVariadic());
839 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000840 CallArgList CallArgs;
841
Mike Stump736529e2009-11-03 02:12:59 +0000842 QualType ArgType = MD->getThisType(getContext());
843 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stumpd0fe5362009-11-04 00:53:51 +0000844 if (nv_t || v_t) {
Mike Stumpc902d222009-11-03 16:59:27 +0000845 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000846 const llvm::Type *OrigTy = Callee->getType();
Mike Stumpc902d222009-11-03 16:59:27 +0000847 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stumpd0fe5362009-11-04 00:53:51 +0000848 if (nv_r || v_r) {
849 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
850 Callee = Builder.CreateBitCast(Callee, OrigTy);
851 nv_r = v_r = 0;
852 }
853 }
854
Mike Stump736529e2009-11-03 02:12:59 +0000855 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
856
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000857 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
858 e = MD->param_end();
859 i != e; ++i) {
860 ParmVarDecl *D = *i;
861 QualType ArgType = D->getType();
862
863 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
864 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000865 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
866 }
867
Mike Stumpf49ed942009-11-02 23:47:45 +0000868 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
869 Callee, CallArgs, MD);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000870 if (nv_r || v_r) {
Mike Stump03e777e2009-11-05 06:32:02 +0000871 bool CanBeZero = !(ResultType->isReferenceType()
872 // FIXME: attr nonnull can't be zero either
873 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stumpc902d222009-11-03 16:59:27 +0000874 // Do the return result adjustment.
Mike Stump03e777e2009-11-05 06:32:02 +0000875 if (CanBeZero) {
876 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
877 llvm::BasicBlock *ZeroBlock = createBasicBlock();
878 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stump7c276b82009-11-05 06:12:26 +0000879
Mike Stump03e777e2009-11-05 06:32:02 +0000880 const llvm::Type *Ty = RV.getScalarVal()->getType();
881 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
882 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
883 NonZeroBlock, ZeroBlock);
884 EmitBlock(NonZeroBlock);
885 llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r);
886 EmitBranch(ContBlock);
887 EmitBlock(ZeroBlock);
888 llvm::Value *Z = RV.getScalarVal();
889 EmitBlock(ContBlock);
890 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
891 RVOrZero->reserveOperandSpace(2);
892 RVOrZero->addIncoming(NZ, NonZeroBlock);
893 RVOrZero->addIncoming(Z, ZeroBlock);
894 RV = RValue::get(RVOrZero);
895 } else
896 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000897 }
898
Mike Stumpf49ed942009-11-02 23:47:45 +0000899 if (!ResultType->isVoidType())
900 EmitReturnOfRValue(RV, ResultType);
901
Mike Stump6e319f62009-09-11 23:25:56 +0000902 FinishFunction();
903 return Fn;
904}
905
Mike Stump77ca8f62009-09-05 07:20:32 +0000906llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
907 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000908 llvm::SmallString<256> OutName;
909 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000910 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000911 llvm::GlobalVariable::LinkageTypes linktype;
912 linktype = llvm::GlobalValue::WeakAnyLinkage;
913 if (!Extern)
914 linktype = llvm::GlobalValue::InternalLinkage;
915 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000916 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000917 const llvm::FunctionType *FTy =
918 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
919 FPT->isVariadic());
920
921 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
922 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000923 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000924 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
925 return m;
926}
927
Mike Stump6e319f62009-09-11 23:25:56 +0000928llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
929 bool Extern, int64_t nv_t,
930 int64_t v_t, int64_t nv_r,
931 int64_t v_r) {
932 llvm::SmallString<256> OutName;
933 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000934 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000935 llvm::GlobalVariable::LinkageTypes linktype;
936 linktype = llvm::GlobalValue::WeakAnyLinkage;
937 if (!Extern)
938 linktype = llvm::GlobalValue::InternalLinkage;
939 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000940 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000941 const llvm::FunctionType *FTy =
942 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
943 FPT->isVariadic());
944
945 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
946 &getModule());
947 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
948 v_r);
Mike Stump6e319f62009-09-11 23:25:56 +0000949 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
950 return m;
951}
952
Mike Stumpf0070db2009-08-26 20:46:33 +0000953llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000954CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
955 const CXXRecordDecl *ClassDecl,
956 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000957 const llvm::Type *Int8PtrTy =
958 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
959
960 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
961 Int8PtrTy->getPointerTo());
962 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
963
Anders Carlssondbd920c2009-10-11 22:13:54 +0000964 int64_t VBaseOffsetIndex =
965 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
966
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000967 llvm::Value *VBaseOffsetPtr =
Mike Stump79d57682009-11-04 01:11:15 +0000968 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000969 const llvm::Type *PtrDiffTy =
970 ConvertType(getContext().getPointerDiffType());
971
972 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
973 PtrDiffTy->getPointerTo());
974
975 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
976
977 return VBaseOffset;
978}
979
980llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000981CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
982 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000983 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000984
Mike Stumpf0070db2009-08-26 20:46:33 +0000985 Ty = llvm::PointerType::get(Ty, 0);
986 Ty = llvm::PointerType::get(Ty, 0);
987 Ty = llvm::PointerType::get(Ty, 0);
988 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
989 vtbl = Builder.CreateLoad(vtbl);
990 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000991 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000992 vfn = Builder.CreateLoad(vfn);
993 return vfn;
994}
995
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000996/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
997/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
998/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000999// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001000void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001001 llvm::Value *Src,
1002 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001003 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001004 QualType Ty) {
1005 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1006 assert(CA && "VLA cannot be copied over");
1007 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001009 // Create a temporary for the loop index and initialize it with 0.
1010 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1011 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001012 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001013 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +00001014 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001015 // Start the loop with a block that tests the condition.
1016 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1017 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001019 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001021 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1022 // Generate: if (loop-index < number-of-elements fall to the loop body,
1023 // otherwise, go to the block after the for-loop.
1024 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001025 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001026 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1027 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001028 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001029 "isless");
1030 // If the condition is true, execute the body.
1031 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001033 EmitBlock(ForBody);
1034 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1035 // Inside the loop body, emit the constructor call on the array element.
1036 Counter = Builder.CreateLoad(IndexPtr);
1037 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1038 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1039 if (BitwiseCopy)
1040 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001041 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001042 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001043 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001044 Ctor_Complete);
1045 CallArgList CallArgs;
1046 // Push the this (Dest) ptr.
1047 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1048 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001050 // Push the Src ptr.
1051 CallArgs.push_back(std::make_pair(RValue::get(Src),
Mike Stump79d57682009-11-04 01:11:15 +00001052 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001053 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001054 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001055 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1056 Callee, CallArgs, BaseCopyCtor);
1057 }
1058 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001060 // Emit the increment of the loop counter.
1061 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1062 Counter = Builder.CreateLoad(IndexPtr);
1063 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1064 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001066 // Finally, branch back up to the condition for the next iteration.
1067 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001069 // Emit the fall-through block.
1070 EmitBlock(AfterFor, true);
1071}
1072
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001073/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001074/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001075/// bitwise assignment or via a copy assignment operator function call.
1076/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001077void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001078 llvm::Value *Src,
1079 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001080 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001081 QualType Ty) {
1082 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1083 assert(CA && "VLA cannot be asssigned");
1084 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001086 // Create a temporary for the loop index and initialize it with 0.
1087 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1088 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001089 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001090 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1091 Builder.CreateStore(zeroConstant, IndexPtr, false);
1092 // Start the loop with a block that tests the condition.
1093 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1094 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001096 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001098 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1099 // Generate: if (loop-index < number-of-elements fall to the loop body,
1100 // otherwise, go to the block after the for-loop.
1101 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001102 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001103 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1104 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001105 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001106 "isless");
1107 // If the condition is true, execute the body.
1108 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001110 EmitBlock(ForBody);
1111 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1112 // Inside the loop body, emit the assignment operator call on array element.
1113 Counter = Builder.CreateLoad(IndexPtr);
1114 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1115 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1116 const CXXMethodDecl *MD = 0;
1117 if (BitwiseAssign)
1118 EmitAggregateCopy(Dest, Src, Ty);
1119 else {
1120 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1121 MD);
1122 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1123 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001124 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001125 const llvm::Type *LTy =
1126 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1127 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001128 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001130 CallArgList CallArgs;
1131 // Push the this (Dest) ptr.
1132 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1133 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001135 // Push the Src ptr.
1136 CallArgs.push_back(std::make_pair(RValue::get(Src),
1137 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001138 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001139 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1140 Callee, CallArgs, MD);
1141 }
1142 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001144 // Emit the increment of the loop counter.
1145 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1146 Counter = Builder.CreateLoad(IndexPtr);
1147 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1148 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001150 // Finally, branch back up to the condition for the next iteration.
1151 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001153 // Emit the fall-through block.
1154 EmitBlock(AfterFor, true);
1155}
1156
Fariborz Jahanianca283612009-08-07 23:51:33 +00001157/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1158/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001159/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001160void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001161 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001162 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001163 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1164 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001165 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1166 /*NullCheckValue=*/false);
1167 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1168 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001169 }
1170 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1171 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001172 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001173 }
Mike Stump1eb44332009-09-09 15:08:12 +00001174
1175 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001176 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001177 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001178 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001179 CallArgList CallArgs;
1180 // Push the this (Dest) ptr.
1181 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1182 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Fariborz Jahanianca283612009-08-07 23:51:33 +00001184 // Push the Src ptr.
1185 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001186 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001187 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001188 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001189 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1190 Callee, CallArgs, BaseCopyCtor);
1191 }
1192}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001193
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001194/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001195/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001196/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001197// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001198void CodeGenFunction::EmitClassCopyAssignment(
1199 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001200 const CXXRecordDecl *ClassDecl,
1201 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001202 QualType Ty) {
1203 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001204 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1205 /*NullCheckValue=*/false);
1206 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1207 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001208 }
1209 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1210 EmitAggregateCopy(Dest, Src, Ty);
1211 return;
1212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001214 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001215 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001216 MD);
1217 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1218 (void)ConstCopyAssignOp;
1219
John McCall183700f2009-09-21 23:43:11 +00001220 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001221 const llvm::Type *LTy =
1222 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001223 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001224 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001226 CallArgList CallArgs;
1227 // Push the this (Dest) ptr.
1228 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1229 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001231 // Push the Src ptr.
1232 CallArgs.push_back(std::make_pair(RValue::get(Src),
1233 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001234 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001235 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001236 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1237 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001238}
1239
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001240/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001241void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001242CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1243 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001244 llvm::Function *Fn,
1245 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001246 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1247 SourceLocation());
1248 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001249 FinishFunction();
1250}
1251
Mike Stump79d57682009-11-04 01:11:15 +00001252/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1253/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001254/// The implicitly-defined copy constructor for class X performs a memberwise
Mike Stump79d57682009-11-04 01:11:15 +00001255/// copy of its subobjects. The order of copying is the same as the order of
1256/// initialization of bases and members in a user-defined constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001257/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001258/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001259/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001260/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001261/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001262/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001263/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001264/// Virtual base class subobjects shall be copied only once by the
1265/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001266
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001267void
1268CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1269 CXXCtorType Type,
1270 llvm::Function *Fn,
1271 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001272 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001273 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Mike Stump79d57682009-11-04 01:11:15 +00001274 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001275 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1276 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001278 FunctionArgList::const_iterator i = Args.begin();
1279 const VarDecl *ThisArg = i->first;
1280 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1281 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1282 const VarDecl *SrcArg = (i+1)->first;
1283 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1284 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001286 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1287 Base != ClassDecl->bases_end(); ++Base) {
1288 // FIXME. copy constrution of virtual base NYI
1289 if (Base->isVirtual())
1290 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001292 CXXRecordDecl *BaseClassDecl
1293 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001294 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1295 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001296 }
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001298 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1299 FieldEnd = ClassDecl->field_end();
1300 Field != FieldEnd; ++Field) {
1301 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001302 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001303 getContext().getAsConstantArrayType(FieldType);
1304 if (Array)
1305 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001307 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1308 CXXRecordDecl *FieldClassDecl
1309 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1310 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1311 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001312 if (Array) {
1313 const llvm::Type *BasePtr = ConvertType(FieldType);
1314 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001315 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001316 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001317 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001318 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1319 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1320 FieldClassDecl, FieldType);
1321 }
Mike Stump1eb44332009-09-09 15:08:12 +00001322 else
1323 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001324 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001325 continue;
1326 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001327 // Do a built-in assignment of scalar data members.
1328 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1329 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1330 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1331 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001332 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001333 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001334}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001335
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001336/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001337/// Before the implicitly-declared copy assignment operator for a class is
1338/// implicitly defined, all implicitly- declared copy assignment operators for
1339/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001340/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001341/// The implicitly-defined copy assignment operator for class X performs
1342/// memberwise assignment of its subob- jects. The direct base classes of X are
1343/// assigned first, in the order of their declaration in
1344/// the base-specifier-list, and then the immediate nonstatic data members of X
1345/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001346/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001347/// if the subobject is of class type, the copy assignment operator for the
1348/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001349/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001350///
Mike Stump1eb44332009-09-09 15:08:12 +00001351/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001352/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001353///
Mike Stump1eb44332009-09-09 15:08:12 +00001354/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001355/// used.
1356void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001357 llvm::Function *Fn,
1358 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001359
1360 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1361 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1362 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001363 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001365 FunctionArgList::const_iterator i = Args.begin();
1366 const VarDecl *ThisArg = i->first;
1367 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1368 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1369 const VarDecl *SrcArg = (i+1)->first;
1370 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1371 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001373 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1374 Base != ClassDecl->bases_end(); ++Base) {
1375 // FIXME. copy assignment of virtual base NYI
1376 if (Base->isVirtual())
1377 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001379 CXXRecordDecl *BaseClassDecl
1380 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1381 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1382 Base->getType());
1383 }
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001385 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1386 FieldEnd = ClassDecl->field_end();
1387 Field != FieldEnd; ++Field) {
1388 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001389 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001390 getContext().getAsConstantArrayType(FieldType);
1391 if (Array)
1392 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001394 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1395 CXXRecordDecl *FieldClassDecl
1396 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1397 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1398 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001399 if (Array) {
1400 const llvm::Type *BasePtr = ConvertType(FieldType);
1401 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1402 llvm::Value *DestBaseAddrPtr =
1403 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1404 llvm::Value *SrcBaseAddrPtr =
1405 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1406 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1407 FieldClassDecl, FieldType);
1408 }
1409 else
Mike Stump1eb44332009-09-09 15:08:12 +00001410 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001411 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001412 continue;
1413 }
1414 // Do a built-in assignment of scalar data members.
1415 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1416 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1417 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1418 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001419 }
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001421 // return *this;
1422 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001424 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001425}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001426
Anders Carlssonb1156b92009-11-06 03:23:06 +00001427static void EmitBaseInitializer(CodeGenFunction &CGF,
1428 const CXXRecordDecl *ClassDecl,
1429 CXXBaseOrMemberInitializer *BaseInit,
1430 CXXCtorType CtorType) {
1431 assert(BaseInit->isBaseInitializer() &&
1432 "Must have base initializer!");
1433
1434 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1435
1436 const Type *BaseType = BaseInit->getBaseClass();
1437 CXXRecordDecl *BaseClassDecl =
1438 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
1439 llvm::Value *V = CGF.GetAddressCXXOfBaseClass(ThisPtr, ClassDecl,
1440 BaseClassDecl,
1441 /*NullCheckValue=*/false);
1442 CGF.EmitCXXConstructorCall(BaseInit->getConstructor(),
1443 CtorType, V,
1444 BaseInit->const_arg_begin(),
1445 BaseInit->const_arg_end());
1446}
1447
1448static void EmitMemberInitializer(CodeGenFunction &CGF,
1449 const CXXRecordDecl *ClassDecl,
1450 CXXBaseOrMemberInitializer *MemberInit) {
1451 assert(MemberInit->isMemberInitializer() &&
1452 "Must have member initializer!");
1453
1454 // non-static data member initializers.
1455 FieldDecl *Field = MemberInit->getMember();
1456 QualType FieldType = CGF.getContext().getCanonicalType((Field)->getType());
1457 const ConstantArrayType *Array =
1458 CGF.getContext().getAsConstantArrayType(FieldType);
1459 if (Array)
1460 FieldType = CGF.getContext().getBaseElementType(FieldType);
1461
1462 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1463 LValue LHS;
1464 if (FieldType->isReferenceType()) {
1465 // FIXME: This is really ugly; should be refactored somehow
1466 unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field);
1467 llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp");
1468 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1469 LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType));
1470 } else {
1471 LHS = CGF.EmitLValueForField(ThisPtr, Field, false, 0);
1472 }
1473 if (FieldType->getAs<RecordType>()) {
1474 if (!Field->isAnonymousStructOrUnion()) {
1475 assert(MemberInit->getConstructor() &&
1476 "EmitCtorPrologue - no constructor to initialize member");
1477 if (Array) {
1478 const llvm::Type *BasePtr = CGF.ConvertType(FieldType);
1479 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1480 llvm::Value *BaseAddrPtr =
1481 CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1482 CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(),
1483 Array, BaseAddrPtr);
1484 }
1485 else
1486 CGF.EmitCXXConstructorCall(MemberInit->getConstructor(),
1487 Ctor_Complete, LHS.getAddress(),
1488 MemberInit->const_arg_begin(),
1489 MemberInit->const_arg_end());
1490 return;
1491 }
1492 else {
1493 // Initializing an anonymous union data member.
1494 FieldDecl *anonMember = MemberInit->getAnonUnionMember();
1495 LHS = CGF.EmitLValueForField(LHS.getAddress(), anonMember,
1496 /*IsUnion=*/true, 0);
1497 FieldType = anonMember->getType();
1498 }
1499 }
1500
1501 assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only");
1502 Expr *RhsExpr = *MemberInit->arg_begin();
1503 RValue RHS;
1504 if (FieldType->isReferenceType())
1505 RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType,
1506 /*IsInitializer=*/true);
1507 else if (FieldType->isMemberFunctionPointerType())
1508 RHS = RValue::get(CGF.CGM.EmitConstantExpr(RhsExpr, FieldType, &CGF));
1509 else
1510 RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true));
1511 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
1512}
1513
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001514/// EmitCtorPrologue - This routine generates necessary code to initialize
1515/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001516/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001517void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1518 CXXCtorType CtorType) {
Anders Carlssonb1156b92009-11-06 03:23:06 +00001519 const CXXRecordDecl *ClassDecl = CD->getParent();
1520
Mike Stumpeb19fa92009-08-06 13:41:24 +00001521 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001522 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001524 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001525 E = CD->init_end();
1526 B != E; ++B) {
1527 CXXBaseOrMemberInitializer *Member = (*B);
Anders Carlssonb1156b92009-11-06 03:23:06 +00001528
Anders Carlsson1faf6742009-11-06 04:11:09 +00001529 assert(LiveTemporaries.empty() &&
1530 "Should not have any live temporaries at initializer start!");
1531
Anders Carlssonb1156b92009-11-06 03:23:06 +00001532 if (Member->isBaseInitializer())
1533 EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
1534 else
1535 EmitMemberInitializer(*this, ClassDecl, Member);
Anders Carlsson1faf6742009-11-06 04:11:09 +00001536
1537 // Pop any live temporaries that the initializers might have pushed.
1538 while (!LiveTemporaries.empty())
1539 PopCXXTemporary();
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001540 }
Mike Stumpf1216772009-07-31 18:25:34 +00001541
1542 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001543 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001544 if (!LoadOfThis)
1545 LoadOfThis = LoadCXXThis();
1546 llvm::Value *VtableField;
1547 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001548 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001549 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1550 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
Mike Stump380dd752009-11-10 07:44:33 +00001551 llvm::Value *vtable = CGM.getVtableInfo().getVtable(ClassDecl);
Mike Stumpf1216772009-07-31 18:25:34 +00001552 Builder.CreateStore(vtable, VtableField);
1553 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001554}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001555
1556/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001557/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001558/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001559/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001560void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1561 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001562 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001563 assert(!ClassDecl->getNumVBases() &&
1564 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001565 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001567 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1568 *E = DD->destr_end(); B != E; ++B) {
1569 uintptr_t BaseOrMember = (*B);
1570 if (DD->isMemberToDestroy(BaseOrMember)) {
1571 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1572 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001573 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001574 getContext().getAsConstantArrayType(FieldType);
1575 if (Array)
1576 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001577 const RecordType *RT = FieldType->getAs<RecordType>();
1578 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1579 if (FieldClassDecl->hasTrivialDestructor())
1580 continue;
1581 llvm::Value *LoadOfThis = LoadCXXThis();
1582 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001583 if (Array) {
1584 const llvm::Type *BasePtr = ConvertType(FieldType);
1585 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001586 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001587 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001588 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001589 Array, BaseAddrPtr);
1590 }
1591 else
1592 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1593 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001594 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001595 const RecordType *RT =
1596 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1597 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1598 if (BaseClassDecl->hasTrivialDestructor())
1599 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001600 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1601 ClassDecl, BaseClassDecl,
1602 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001603 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001604 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001605 }
1606 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001607 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1608 return;
1609 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001610 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001611 // reverse order of their construction.
1612 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001614 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1615 FieldEnd = ClassDecl->field_end();
1616 Field != FieldEnd; ++Field) {
1617 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001618 if (getContext().getAsConstantArrayType(FieldType))
1619 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001620 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1621 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1622 if (FieldClassDecl->hasTrivialDestructor())
1623 continue;
1624 DestructedFields.push_back(*Field);
1625 }
1626 }
1627 if (!DestructedFields.empty())
1628 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1629 FieldDecl *Field = DestructedFields[i];
1630 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001631 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001632 getContext().getAsConstantArrayType(FieldType);
1633 if (Array)
1634 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001635 const RecordType *RT = FieldType->getAs<RecordType>();
1636 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1637 llvm::Value *LoadOfThis = LoadCXXThis();
1638 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001639 if (Array) {
1640 const llvm::Type *BasePtr = ConvertType(FieldType);
1641 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001642 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001643 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001644 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001645 Array, BaseAddrPtr);
1646 }
1647 else
1648 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1649 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001650 }
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001652 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1653 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1654 Base != ClassDecl->bases_end(); ++Base) {
1655 // FIXME. copy assignment of virtual base NYI
1656 if (Base->isVirtual())
1657 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001659 CXXRecordDecl *BaseClassDecl
1660 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1661 if (BaseClassDecl->hasTrivialDestructor())
1662 continue;
1663 DestructedBases.push_back(BaseClassDecl);
1664 }
1665 if (DestructedBases.empty())
1666 return;
1667 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1668 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001669 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1670 ClassDecl,BaseClassDecl,
1671 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001672 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1673 Dtor_Complete, V);
1674 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001675}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001676
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001677void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1678 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001679 llvm::Function *Fn,
1680 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001682 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001683 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1684 "SynthesizeDefaultDestructor - destructor has user declaration");
1685 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001687 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1688 SourceLocation());
1689 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001690 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001691}
Anders Carlsson6815e942009-09-27 18:58:34 +00001692
1693// FIXME: Move this to CGCXXStmt.cpp
1694void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1695 // FIXME: We need to do more here.
1696 EmitStmt(S.getTryBlock());
1697}