blob: c23ad597649184e8bdbb77686b65508edc4ede9a [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 Jahanian88b11de2009-11-11 01:13:34 +000082 // Avoid generating destructor(s) for initialized objects.
83 if (!isa<CXXConstructExpr>(Init))
84 return;
Fariborz Jahanian88f42802009-11-10 19:24:06 +000085 const ConstantArrayType *Array = getContext().getAsConstantArrayType(T);
86 if (Array)
87 T = getContext().getBaseElementType(Array);
88
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000089 if (const RecordType *RT = T->getAs<RecordType>()) {
90 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Fariborz Jahanian88f42802009-11-10 19:24:06 +000091 if (!RD->hasTrivialDestructor()) {
92 llvm::Constant *DtorFn;
93 if (Array) {
94 DtorFn = CodeGenFunction(CGM).GenerateCXXAggrDestructorHelper(
95 RD->getDestructor(getContext()),
96 Array, DeclPtr);
97 DeclPtr =
98 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
99 }
100 else
101 DtorFn = CGM.GetAddrOfCXXDestructor(RD->getDestructor(getContext()),
102 Dtor_Complete);
103 EmitCXXGlobalDtorRegistration(DtorFn, DeclPtr);
104 }
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000105 }
106 }
107}
108
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000109void
110CodeGenModule::EmitCXXGlobalInitFunc() {
111 if (CXXGlobalInits.empty())
112 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Mike Stump79d57682009-11-04 01:11:15 +0000114 const llvm::FunctionType *FTy
115 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
116 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000118 // Create our global initialization function.
119 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000120 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000121 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
122 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000124 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000125 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000126 CXXGlobalInits.size());
127 AddGlobalCtor(Fn);
128}
129
130void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
131 const VarDecl **Decls,
132 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000133 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000134 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000136 for (unsigned i = 0; i != NumDecls; ++i) {
137 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000139 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
140 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
141 }
142 FinishFunction();
143}
144
Mike Stump1eb44332009-09-09 15:08:12 +0000145void
146CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000147 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000148 // FIXME: This should use __cxa_guard_{acquire,release}?
149
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000150 assert(!getContext().getLangOptions().ThreadsafeStatics &&
151 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000152
Anders Carlsson283a0622009-04-13 18:03:33 +0000153 llvm::SmallString<256> GuardVName;
154 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000155 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000157 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000158 llvm::GlobalValue *GuardV =
Mike Stump79d57682009-11-04 01:11:15 +0000159 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext),
160 false, GV->getLinkage(),
161 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000162 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000164 // Load the first byte of the guard variable.
Mike Stump79d57682009-11-04 01:11:15 +0000165 const llvm::Type *PtrTy
166 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000167 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000168 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170 // Compare it against 0.
Mike Stump79d57682009-11-04 01:11:15 +0000171 llvm::Value *nullValue
172 = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000173 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Daniel Dunbar55e87422008-11-11 02:29:29 +0000175 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000176 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000177
178 // If the guard variable is 0, jump to the initializer code.
179 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000181 EmitBlock(InitBlock);
182
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000183 EmitCXXGlobalVarDeclInit(D, GV);
184
Mike Stump79d57682009-11-04 01:11:15 +0000185 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
186 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000187 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000189 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000190}
191
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000192RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
193 llvm::Value *Callee,
194 llvm::Value *This,
195 CallExpr::const_arg_iterator ArgBeg,
196 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000197 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000198 "Trying to emit a member call expr on a static method!");
199
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000200 // A call to a trivial destructor requires no code generation.
201 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
202 if (Destructor->isTrivial())
203 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
John McCall183700f2009-09-21 23:43:11 +0000205 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000207 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000209 // Push the this ptr.
210 Args.push_back(std::make_pair(RValue::get(This),
211 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000213 // And the rest of the call args
214 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
John McCall183700f2009-09-21 23:43:11 +0000216 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000217 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
218 Callee, Args, MD);
219}
220
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000221/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
222/// expr can be devirtualized.
223static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
224 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
225 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
226 // This is a record decl. We know the type and can devirtualize it.
227 return VD->getType()->isRecordType();
228 }
Anders Carlsson76366482009-10-12 19:45:47 +0000229
230 return false;
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000231 }
232
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000233 // We can always devirtualize calls on temporary object expressions.
Anders Carlsson76366482009-10-12 19:45:47 +0000234 if (isa<CXXTemporaryObjectExpr>(Base))
235 return true;
236
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000237 // And calls on bound temporaries.
238 if (isa<CXXBindTemporaryExpr>(Base))
239 return true;
240
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000241 // Check if this is a call expr that returns a record type.
242 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
243 return CE->getCallReturnType()->isRecordType();
244
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000245 // We can't devirtualize the call.
246 return false;
247}
248
Anders Carlsson774e7c62009-04-03 22:50:24 +0000249RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000250 if (isa<BinaryOperator>(CE->getCallee()))
251 return EmitCXXMemberPointerCallExpr(CE);
252
Anders Carlsson774e7c62009-04-03 22:50:24 +0000253 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
254 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000255
Anders Carlsson2472bf02009-09-29 03:54:11 +0000256 if (MD->isStatic()) {
257 // The method is static, emit it as we would a regular call.
258 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
259 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
260 CE->arg_begin(), CE->arg_end(), 0);
261
262 }
263
John McCall183700f2009-09-21 23:43:11 +0000264 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000265
Mike Stump1eb44332009-09-09 15:08:12 +0000266 const llvm::Type *Ty =
267 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000268 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000269 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlsson774e7c62009-04-03 22:50:24 +0000271 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000272 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000273 else {
274 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000275 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000276 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000277
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000278 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000279 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000280 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000281 //
282 // We also don't emit a virtual call if the base expression has a record type
283 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000284 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000285 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000286 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000287 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000288 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000289 = dyn_cast<CXXDestructorDecl>(MD))
290 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000291 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000292 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000293
294 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000295 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000296}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000297
Mike Stump1eb44332009-09-09 15:08:12 +0000298RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000299CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
300 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson3eea6352009-10-13 17:41:28 +0000301 const Expr *BaseExpr = BO->getLHS();
302 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000303
Anders Carlsson3eea6352009-10-13 17:41:28 +0000304 const MemberPointerType *MPT =
305 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000306 const FunctionProtoType *FPT =
307 MPT->getPointeeType()->getAs<FunctionProtoType>();
308 const CXXRecordDecl *RD =
Douglas Gregor87c12c42009-11-04 16:49:01 +0000309 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000310
311 const llvm::FunctionType *FTy =
312 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
313 FPT->isVariadic());
314
315 const llvm::Type *Int8PtrTy =
316 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
317
318 // Get the member function pointer.
319 llvm::Value *MemFnPtr =
Anders Carlsson3eea6352009-10-13 17:41:28 +0000320 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
321 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000322
323 // Emit the 'this' pointer.
324 llvm::Value *This;
325
326 if (BO->getOpcode() == BinaryOperator::PtrMemI)
327 This = EmitScalarExpr(BaseExpr);
328 else
329 This = EmitLValue(BaseExpr).getAddress();
330
331 // Adjust it.
332 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
333 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
334
335 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
336 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
337
338 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
339
340 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
341
342 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
343
344 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
345
346 // If the LSB in the function pointer is 1, the function pointer points to
347 // a virtual function.
348 llvm::Value *IsVirtual
349 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
350 "and");
351
352 IsVirtual = Builder.CreateTrunc(IsVirtual,
353 llvm::Type::getInt1Ty(VMContext));
354
355 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
356 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
357 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
358
359 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
360 EmitBlock(FnVirtual);
361
362 const llvm::Type *VTableTy =
363 FTy->getPointerTo()->getPointerTo()->getPointerTo();
364
365 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
366 VTable = Builder.CreateLoad(VTable);
367
368 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
369
370 // Since the function pointer is 1 plus the virtual table offset, we
371 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000372 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000373
374 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
375
376 EmitBranch(FnEnd);
377 EmitBlock(FnNonVirtual);
378
379 // If the function is not virtual, just load the pointer.
380 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
381 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
382
383 EmitBlock(FnEnd);
384
385 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
386 Callee->reserveOperandSpace(2);
387 Callee->addIncoming(VirtualFn, FnVirtual);
388 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
389
390 CallArgList Args;
391
392 QualType ThisType =
393 getContext().getPointerType(getContext().getTagDeclType(RD));
394
395 // Push the this ptr.
396 Args.push_back(std::make_pair(RValue::get(This), ThisType));
397
398 // And the rest of the call args
399 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
400 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
401 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
402 Callee, Args, 0);
403}
404
405RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000406CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
407 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000408 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000409 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Fariborz Jahanianad258832009-08-13 21:09:41 +0000411 if (MD->isCopyAssignment()) {
412 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
413 if (ClassDecl->hasTrivialCopyAssignment()) {
414 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
415 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
416 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
417 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
418 QualType Ty = E->getType();
419 EmitAggregateCopy(This, Src, Ty);
420 return RValue::get(This);
421 }
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
John McCall183700f2009-09-21 23:43:11 +0000424 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000425 const llvm::Type *Ty =
426 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000427 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000428 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Anders Carlsson0f294632009-05-27 04:18:27 +0000430 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Anders Carlsson0f294632009-05-27 04:18:27 +0000432 return EmitCXXMemberCall(MD, Callee, This,
433 E->arg_begin() + 1, E->arg_end());
434}
435
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000436llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000437 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000438 "Must be in a C++ member function decl to load 'this'");
439 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
440 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000442 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000443 // ans: See how CodeGenFunction::LoadObjCSelf() uses
444 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000445 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
446}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000447
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000448/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
449/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000450/// array.
451/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
452/// array type and 'ArrayPtr' points to the beginning fo the array.
453/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000454void
455CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000456 const ConstantArrayType *ArrayTy,
457 llvm::Value *ArrayPtr) {
458 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
459 llvm::Value * NumElements =
460 llvm::ConstantInt::get(SizeTy,
461 getContext().getConstantArrayElementCount(ArrayTy));
462
463 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
464}
465
466void
467CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
468 llvm::Value *NumElements,
469 llvm::Value *ArrayPtr) {
470 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000472 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000473 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
474 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
475 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000477 // Start the loop with a block that tests the condition.
478 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
479 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000481 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000483 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000485 // Generate: if (loop-index < number-of-elements fall to the loop body,
486 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000487 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000488 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000489 // If the condition is true, execute the body.
490 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000492 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000494 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000495 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000496 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000497 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
498 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000499 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000501 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000503 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000504 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000505 Counter = Builder.CreateLoad(IndexPtr);
506 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
507 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000509 // Finally, branch back up to the condition for the next iteration.
510 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000512 // Emit the fall-through block.
513 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000514}
515
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000516/// EmitCXXAggrDestructorCall - calls the default destructor on array
517/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000518void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000519CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
520 const ArrayType *Array,
521 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000522 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
523 assert(CA && "Do we support VLA for destruction ?");
Fariborz Jahanian72c21532009-11-13 19:27:47 +0000524 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
525 llvm::Value* ElementCountPtr =
526 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
527 EmitCXXAggrDestructorCall(D, ElementCountPtr, This);
528}
529
530/// EmitCXXAggrDestructorCall - calls the default destructor on array
531/// elements in reverse order of construction.
532void
533CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
534 llvm::Value *UpperCount,
535 llvm::Value *This) {
Mike Stump1eb44332009-09-09 15:08:12 +0000536 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000537 1);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000538 // Create a temporary for the loop index and initialize it with count of
539 // array elements.
540 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
541 "loop.index");
542 // Index = ElementCount;
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000543 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000545 // Start the loop with a block that tests the condition.
546 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
547 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000549 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000551 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000553 // Generate: if (loop-index != 0 fall to the loop body,
554 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000555 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000556 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
557 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
558 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
559 "isne");
560 // If the condition is true, execute the body.
561 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000563 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000565 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
566 // Inside the loop body, emit the constructor call on the array element.
567 Counter = Builder.CreateLoad(IndexPtr);
568 Counter = Builder.CreateSub(Counter, One);
569 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
570 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000572 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000574 // Emit the decrement of the loop counter.
575 Counter = Builder.CreateLoad(IndexPtr);
576 Counter = Builder.CreateSub(Counter, One, "dec");
577 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000579 // Finally, branch back up to the condition for the next iteration.
580 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000582 // Emit the fall-through block.
583 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000584}
585
Fariborz Jahanian72c21532009-11-13 19:27:47 +0000586/// GenerateCXXAggrDestructorHelper - Generates a helper function which when invoked,
Fariborz Jahanian88f42802009-11-10 19:24:06 +0000587/// calls the default destructor on array elements in reverse order of
588/// construction.
589llvm::Constant *
590CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
591 const ArrayType *Array,
592 llvm::Value *This) {
593 static int UniqueCount;
594 FunctionArgList Args;
595 ImplicitParamDecl *Dst =
596 ImplicitParamDecl::Create(getContext(), 0,
597 SourceLocation(), 0,
598 getContext().getPointerType(getContext().VoidTy));
599 Args.push_back(std::make_pair(Dst, Dst->getType()));
600
601 llvm::SmallString<16> Name;
602 llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueCount);
603 QualType R = getContext().VoidTy;
604 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args);
605 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false);
606 llvm::Function *Fn =
607 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
608 Name.c_str(),
609 &CGM.getModule());
610 IdentifierInfo *II
611 = &CGM.getContext().Idents.get(Name.c_str());
612 FunctionDecl *FD = FunctionDecl::Create(getContext(),
613 getContext().getTranslationUnitDecl(),
614 SourceLocation(), II, R, 0,
615 FunctionDecl::Static,
616 false, true);
617 StartFunction(FD, R, Fn, Args, SourceLocation());
618 QualType BaseElementTy = getContext().getBaseElementType(Array);
619 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
620 BasePtr = llvm::PointerType::getUnqual(BasePtr);
621 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr);
622 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr);
623 FinishFunction();
624 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
625 0);
626 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
627 return m;
628}
629
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000630void
Mike Stump1eb44332009-09-09 15:08:12 +0000631CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
632 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000633 llvm::Value *This,
634 CallExpr::const_arg_iterator ArgBeg,
635 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000636 if (D->isCopyConstructor(getContext())) {
637 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
638 if (ClassDecl->hasTrivialCopyConstructor()) {
639 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
640 "EmitCXXConstructorCall - user declared copy constructor");
641 const Expr *E = (*ArgBeg);
642 QualType Ty = E->getType();
643 llvm::Value *Src = EmitLValue(E).getAddress();
644 EmitAggregateCopy(This, Src, Ty);
645 return;
646 }
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000649 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
650
651 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000652}
653
Mike Stump1eb44332009-09-09 15:08:12 +0000654void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000655 CXXDtorType Type,
656 llvm::Value *This) {
657 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Anders Carlsson7267c162009-05-29 21:03:38 +0000659 EmitCXXMemberCall(D, Callee, This, 0, 0);
660}
661
Mike Stump1eb44332009-09-09 15:08:12 +0000662void
663CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000664 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000665 assert(Dest && "Must have a destination!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000666 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000667 const ConstantArrayType *Array =
668 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000669 // For a copy constructor, even if it is trivial, must fall thru so
670 // its argument is code-gen'ed.
671 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000672 QualType InitType = E->getType();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000673 if (Array)
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000674 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000675 const CXXRecordDecl *RD =
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000676 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000677 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000678 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000679 }
Mike Stump1eb44332009-09-09 15:08:12 +0000680 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000681 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000682 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Anders Carlsson03d8ed42009-11-13 04:34:45 +0000683 const Expr *Arg = E->getArg(0);
684
685 if (const CXXBindTemporaryExpr *BindExpr =
686 dyn_cast<CXXBindTemporaryExpr>(Arg))
687 Arg = BindExpr->getSubExpr();
688
689 EmitAggExpr(Arg, Dest, false);
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000690 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000691 }
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000692 if (Array) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000693 QualType BaseElementTy = getContext().getBaseElementType(Array);
694 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
695 BasePtr = llvm::PointerType::getUnqual(BasePtr);
696 llvm::Value *BaseAddrPtr =
697 Builder.CreateBitCast(Dest, BasePtr);
698 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
699 }
700 else
701 // Call the constructor.
702 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
703 E->arg_begin(), E->arg_end());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000704}
705
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000706void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000707 EmitGlobal(GlobalDecl(D, Ctor_Complete));
708 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000709}
Anders Carlsson363c1842009-04-16 23:57:24 +0000710
Mike Stump1eb44332009-09-09 15:08:12 +0000711void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000712 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Anders Carlsson27ae5362009-04-17 01:58:57 +0000714 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000716 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Anders Carlsson27ae5362009-04-17 01:58:57 +0000718 SetFunctionDefinitionAttributes(D, Fn);
719 SetLLVMFunctionAttributesForDefinition(D, Fn);
720}
721
Anders Carlsson363c1842009-04-16 23:57:24 +0000722llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000723CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000724 CXXCtorType Type) {
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000725 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>();
Anders Carlsson363c1842009-04-16 23:57:24 +0000726 const llvm::FunctionType *FTy =
Fariborz Jahanian30509a32009-11-06 18:47:57 +0000727 getTypes().GetFunctionType(getTypes().getFunctionInfo(D),
728 FPT->isVariadic());
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Anders Carlsson363c1842009-04-16 23:57:24 +0000730 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000731 return cast<llvm::Function>(
732 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000733}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000734
Mike Stump1eb44332009-09-09 15:08:12 +0000735const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000736 CXXCtorType Type) {
737 llvm::SmallString<256> Name;
738 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000739 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Anders Carlsson27ae5362009-04-17 01:58:57 +0000741 Name += '\0';
742 return UniqueMangledName(Name.begin(), Name.end());
743}
744
745void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000746 EmitCXXDestructor(D, Dtor_Complete);
747 EmitCXXDestructor(D, Dtor_Base);
748}
749
Mike Stump1eb44332009-09-09 15:08:12 +0000750void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000751 CXXDtorType Type) {
752 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000754 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Anders Carlsson27ae5362009-04-17 01:58:57 +0000756 SetFunctionDefinitionAttributes(D, Fn);
757 SetLLVMFunctionAttributesForDefinition(D, Fn);
758}
759
760llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000761CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000762 CXXDtorType Type) {
763 const llvm::FunctionType *FTy =
764 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Anders Carlsson27ae5362009-04-17 01:58:57 +0000766 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000767 return cast<llvm::Function>(
768 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000769}
770
Mike Stump1eb44332009-09-09 15:08:12 +0000771const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000772 CXXDtorType Type) {
773 llvm::SmallString<256> Name;
774 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000775 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Anders Carlsson27ae5362009-04-17 01:58:57 +0000777 Name += '\0';
778 return UniqueMangledName(Name.begin(), Name.end());
779}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000780
Mike Stumped032eb2009-09-04 18:27:16 +0000781llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
782 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000783 bool Extern, int64_t nv,
784 int64_t v) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000785 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stumped032eb2009-09-04 18:27:16 +0000786}
787
Mike Stumpc902d222009-11-03 16:59:27 +0000788llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
789 int64_t v) {
790 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
791 0);
792 const llvm::Type *OrigTy = V->getType();
793 if (nv) {
794 // Do the non-virtual adjustment
795 V = Builder.CreateBitCast(V, Ptr8Ty);
796 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
797 V = Builder.CreateBitCast(V, OrigTy);
798 }
799 if (v) {
800 // Do the virtual this adjustment
801 const llvm::Type *PtrDiffTy =
802 ConvertType(getContext().getPointerDiffType());
803 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
804 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
805 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
806 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
807 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
808 V = Builder.CreateLoad(V, "vtable");
809 llvm::Value *VTablePtr = V;
810 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
811 v /= LLVMPointerWidth/8;
812 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
813 V = Builder.CreateLoad(V);
814 V = Builder.CreateGEP(ThisVal, V);
815 V = Builder.CreateBitCast(V, OrigTy);
816 }
817 return V;
818}
819
Mike Stump6e319f62009-09-11 23:25:56 +0000820llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
821 const CXXMethodDecl *MD,
822 bool Extern,
823 int64_t nv_t,
824 int64_t v_t,
825 int64_t nv_r,
826 int64_t v_r) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000827 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000828
829 FunctionArgList Args;
830 ImplicitParamDecl *ThisDecl =
831 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
832 MD->getThisType(getContext()));
833 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
834 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
835 e = MD->param_end();
836 i != e; ++i) {
837 ParmVarDecl *D = *i;
838 Args.push_back(std::make_pair(D, D->getType()));
839 }
840 IdentifierInfo *II
841 = &CGM.getContext().Idents.get("__thunk_named_foo_");
842 FunctionDecl *FD = FunctionDecl::Create(getContext(),
843 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000844 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000845 Extern
846 ? FunctionDecl::Extern
847 : FunctionDecl::Static,
848 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000849 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
850
851 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000852 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
853 const llvm::Type *Ty =
854 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
855 FPT->isVariadic());
856 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000857 CallArgList CallArgs;
858
Mike Stump736529e2009-11-03 02:12:59 +0000859 QualType ArgType = MD->getThisType(getContext());
860 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stumpd0fe5362009-11-04 00:53:51 +0000861 if (nv_t || v_t) {
Mike Stumpc902d222009-11-03 16:59:27 +0000862 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000863 const llvm::Type *OrigTy = Callee->getType();
Mike Stumpc902d222009-11-03 16:59:27 +0000864 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stumpd0fe5362009-11-04 00:53:51 +0000865 if (nv_r || v_r) {
866 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
867 Callee = Builder.CreateBitCast(Callee, OrigTy);
868 nv_r = v_r = 0;
869 }
870 }
871
Mike Stump736529e2009-11-03 02:12:59 +0000872 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
873
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000874 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
875 e = MD->param_end();
876 i != e; ++i) {
877 ParmVarDecl *D = *i;
878 QualType ArgType = D->getType();
879
880 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
881 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000882 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
883 }
884
Mike Stumpf49ed942009-11-02 23:47:45 +0000885 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
886 Callee, CallArgs, MD);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000887 if (nv_r || v_r) {
Mike Stump03e777e2009-11-05 06:32:02 +0000888 bool CanBeZero = !(ResultType->isReferenceType()
889 // FIXME: attr nonnull can't be zero either
890 /* || ResultType->hasAttr<NonNullAttr>() */ );
Mike Stumpc902d222009-11-03 16:59:27 +0000891 // Do the return result adjustment.
Mike Stump03e777e2009-11-05 06:32:02 +0000892 if (CanBeZero) {
893 llvm::BasicBlock *NonZeroBlock = createBasicBlock();
894 llvm::BasicBlock *ZeroBlock = createBasicBlock();
895 llvm::BasicBlock *ContBlock = createBasicBlock();
Mike Stump7c276b82009-11-05 06:12:26 +0000896
Mike Stump03e777e2009-11-05 06:32:02 +0000897 const llvm::Type *Ty = RV.getScalarVal()->getType();
898 llvm::Value *Zero = llvm::Constant::getNullValue(Ty);
899 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero),
900 NonZeroBlock, ZeroBlock);
901 EmitBlock(NonZeroBlock);
902 llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r);
903 EmitBranch(ContBlock);
904 EmitBlock(ZeroBlock);
905 llvm::Value *Z = RV.getScalarVal();
906 EmitBlock(ContBlock);
907 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty);
908 RVOrZero->reserveOperandSpace(2);
909 RVOrZero->addIncoming(NZ, NonZeroBlock);
910 RVOrZero->addIncoming(Z, ZeroBlock);
911 RV = RValue::get(RVOrZero);
912 } else
913 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000914 }
915
Mike Stumpf49ed942009-11-02 23:47:45 +0000916 if (!ResultType->isVoidType())
917 EmitReturnOfRValue(RV, ResultType);
918
Mike Stump6e319f62009-09-11 23:25:56 +0000919 FinishFunction();
920 return Fn;
921}
922
Mike Stump77ca8f62009-09-05 07:20:32 +0000923llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
924 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000925 llvm::SmallString<256> OutName;
926 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000927 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000928 llvm::GlobalVariable::LinkageTypes linktype;
929 linktype = llvm::GlobalValue::WeakAnyLinkage;
930 if (!Extern)
931 linktype = llvm::GlobalValue::InternalLinkage;
932 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000933 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000934 const llvm::FunctionType *FTy =
935 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
936 FPT->isVariadic());
937
938 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
939 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000940 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000941 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
942 return m;
943}
944
Mike Stump6e319f62009-09-11 23:25:56 +0000945llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
946 bool Extern, int64_t nv_t,
947 int64_t v_t, int64_t nv_r,
948 int64_t v_r) {
949 llvm::SmallString<256> OutName;
950 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000951 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000952 llvm::GlobalVariable::LinkageTypes linktype;
953 linktype = llvm::GlobalValue::WeakAnyLinkage;
954 if (!Extern)
955 linktype = llvm::GlobalValue::InternalLinkage;
956 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000957 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000958 const llvm::FunctionType *FTy =
959 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
960 FPT->isVariadic());
961
962 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
963 &getModule());
964 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
965 v_r);
Mike Stump6e319f62009-09-11 23:25:56 +0000966 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
967 return m;
968}
969
Mike Stumpf0070db2009-08-26 20:46:33 +0000970llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000971CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
972 const CXXRecordDecl *ClassDecl,
973 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000974 const llvm::Type *Int8PtrTy =
975 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
976
977 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
978 Int8PtrTy->getPointerTo());
979 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
980
Anders Carlssondbd920c2009-10-11 22:13:54 +0000981 int64_t VBaseOffsetIndex =
982 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
983
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000984 llvm::Value *VBaseOffsetPtr =
Mike Stump79d57682009-11-04 01:11:15 +0000985 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000986 const llvm::Type *PtrDiffTy =
987 ConvertType(getContext().getPointerDiffType());
988
989 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
990 PtrDiffTy->getPointerTo());
991
992 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
993
994 return VBaseOffset;
995}
996
Anders Carlsson566abee2009-11-13 04:45:41 +0000997static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, int64_t VtableIndex,
998 llvm::Value *This, const llvm::Type *Ty) {
999 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo();
Anders Carlsson2b358352009-10-03 14:56:57 +00001000
Anders Carlsson566abee2009-11-13 04:45:41 +00001001 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty);
1002 Vtable = CGF.Builder.CreateLoad(Vtable);
1003
1004 llvm::Value *VFuncPtr =
1005 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn");
1006 return CGF.Builder.CreateLoad(VFuncPtr);
1007}
1008
1009llvm::Value *
1010CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
1011 const llvm::Type *Ty) {
1012 MD = MD->getCanonicalDecl();
1013 int64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD);
1014
1015 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
1016}
1017
1018llvm::Value *
1019CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
1020 llvm::Value *&This, const llvm::Type *Ty) {
1021 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl());
1022 int64_t VtableIndex =
Anders Carlssona0fdd912009-11-13 17:08:56 +00001023 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type));
Anders Carlsson566abee2009-11-13 04:45:41 +00001024
1025 return ::BuildVirtualCall(*this, VtableIndex, This, Ty);
Mike Stumpf0070db2009-08-26 20:46:33 +00001026}
1027
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001028/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1029/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1030/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001031// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001032void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001033 llvm::Value *Src,
1034 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001035 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001036 QualType Ty) {
1037 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1038 assert(CA && "VLA cannot be copied over");
1039 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001041 // Create a temporary for the loop index and initialize it with 0.
1042 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1043 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001044 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001045 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +00001046 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001047 // Start the loop with a block that tests the condition.
1048 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1049 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001051 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001053 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1054 // Generate: if (loop-index < number-of-elements fall to the loop body,
1055 // otherwise, go to the block after the for-loop.
1056 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001057 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001058 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1059 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001060 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001061 "isless");
1062 // If the condition is true, execute the body.
1063 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001065 EmitBlock(ForBody);
1066 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1067 // Inside the loop body, emit the constructor call on the array element.
1068 Counter = Builder.CreateLoad(IndexPtr);
1069 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1070 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1071 if (BitwiseCopy)
1072 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001073 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001074 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001075 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001076 Ctor_Complete);
1077 CallArgList CallArgs;
1078 // Push the this (Dest) ptr.
1079 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1080 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001082 // Push the Src ptr.
1083 CallArgs.push_back(std::make_pair(RValue::get(Src),
Mike Stump79d57682009-11-04 01:11:15 +00001084 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001085 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001086 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001087 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1088 Callee, CallArgs, BaseCopyCtor);
1089 }
1090 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001092 // Emit the increment of the loop counter.
1093 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1094 Counter = Builder.CreateLoad(IndexPtr);
1095 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1096 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001098 // Finally, branch back up to the condition for the next iteration.
1099 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001101 // Emit the fall-through block.
1102 EmitBlock(AfterFor, true);
1103}
1104
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001105/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001106/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001107/// bitwise assignment or via a copy assignment operator function call.
1108/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001109void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001110 llvm::Value *Src,
1111 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001112 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001113 QualType Ty) {
1114 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1115 assert(CA && "VLA cannot be asssigned");
1116 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001118 // Create a temporary for the loop index and initialize it with 0.
1119 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1120 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001121 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001122 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1123 Builder.CreateStore(zeroConstant, IndexPtr, false);
1124 // Start the loop with a block that tests the condition.
1125 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1126 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001128 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001130 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1131 // Generate: if (loop-index < number-of-elements fall to the loop body,
1132 // otherwise, go to the block after the for-loop.
1133 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001134 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001135 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1136 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001137 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001138 "isless");
1139 // If the condition is true, execute the body.
1140 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001142 EmitBlock(ForBody);
1143 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1144 // Inside the loop body, emit the assignment operator call on array element.
1145 Counter = Builder.CreateLoad(IndexPtr);
1146 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1147 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1148 const CXXMethodDecl *MD = 0;
1149 if (BitwiseAssign)
1150 EmitAggregateCopy(Dest, Src, Ty);
1151 else {
1152 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1153 MD);
1154 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1155 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001156 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001157 const llvm::Type *LTy =
1158 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1159 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001160 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001162 CallArgList CallArgs;
1163 // Push the this (Dest) ptr.
1164 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1165 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001167 // Push the Src ptr.
1168 CallArgs.push_back(std::make_pair(RValue::get(Src),
1169 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001170 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001171 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1172 Callee, CallArgs, MD);
1173 }
1174 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001176 // Emit the increment of the loop counter.
1177 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1178 Counter = Builder.CreateLoad(IndexPtr);
1179 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1180 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001182 // Finally, branch back up to the condition for the next iteration.
1183 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001185 // Emit the fall-through block.
1186 EmitBlock(AfterFor, true);
1187}
1188
Fariborz Jahanianca283612009-08-07 23:51:33 +00001189/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1190/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001191/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001192void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001193 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001194 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001195 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1196 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001197 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1198 /*NullCheckValue=*/false);
1199 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1200 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001201 }
1202 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1203 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001204 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
1207 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001208 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001209 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001210 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001211 CallArgList CallArgs;
1212 // Push the this (Dest) ptr.
1213 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1214 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Fariborz Jahanianca283612009-08-07 23:51:33 +00001216 // Push the Src ptr.
1217 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001218 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001219 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001220 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001221 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1222 Callee, CallArgs, BaseCopyCtor);
1223 }
1224}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001225
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001226/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001227/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001228/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001229// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001230void CodeGenFunction::EmitClassCopyAssignment(
1231 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001232 const CXXRecordDecl *ClassDecl,
1233 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001234 QualType Ty) {
1235 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001236 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1237 /*NullCheckValue=*/false);
1238 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1239 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001240 }
1241 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1242 EmitAggregateCopy(Dest, Src, Ty);
1243 return;
1244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001246 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001247 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001248 MD);
1249 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1250 (void)ConstCopyAssignOp;
1251
John McCall183700f2009-09-21 23:43:11 +00001252 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001253 const llvm::Type *LTy =
1254 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001255 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001256 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001258 CallArgList CallArgs;
1259 // Push the this (Dest) ptr.
1260 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1261 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001263 // Push the Src ptr.
1264 CallArgs.push_back(std::make_pair(RValue::get(Src),
1265 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001266 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001267 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001268 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1269 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001270}
1271
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001272/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001273void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001274CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1275 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001276 llvm::Function *Fn,
1277 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001278 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1279 SourceLocation());
1280 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001281 FinishFunction();
1282}
1283
Mike Stump79d57682009-11-04 01:11:15 +00001284/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1285/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001286/// The implicitly-defined copy constructor for class X performs a memberwise
Mike Stump79d57682009-11-04 01:11:15 +00001287/// copy of its subobjects. The order of copying is the same as the order of
1288/// initialization of bases and members in a user-defined constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001289/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001290/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001291/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001292/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001293/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001294/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001295/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001296/// Virtual base class subobjects shall be copied only once by the
1297/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001298
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001299void
1300CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1301 CXXCtorType Type,
1302 llvm::Function *Fn,
1303 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001304 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001305 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Mike Stump79d57682009-11-04 01:11:15 +00001306 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001307 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1308 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001310 FunctionArgList::const_iterator i = Args.begin();
1311 const VarDecl *ThisArg = i->first;
1312 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1313 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1314 const VarDecl *SrcArg = (i+1)->first;
1315 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1316 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001318 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1319 Base != ClassDecl->bases_end(); ++Base) {
1320 // FIXME. copy constrution of virtual base NYI
1321 if (Base->isVirtual())
1322 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001324 CXXRecordDecl *BaseClassDecl
1325 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001326 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1327 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001328 }
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001330 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1331 FieldEnd = ClassDecl->field_end();
1332 Field != FieldEnd; ++Field) {
1333 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001334 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001335 getContext().getAsConstantArrayType(FieldType);
1336 if (Array)
1337 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001339 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1340 CXXRecordDecl *FieldClassDecl
1341 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1342 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1343 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001344 if (Array) {
1345 const llvm::Type *BasePtr = ConvertType(FieldType);
1346 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001347 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001348 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001349 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001350 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1351 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1352 FieldClassDecl, FieldType);
1353 }
Mike Stump1eb44332009-09-09 15:08:12 +00001354 else
1355 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001356 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001357 continue;
1358 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001359 // Do a built-in assignment of scalar data members.
1360 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1361 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1362 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1363 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001364 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001365 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001366}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001367
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001368/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001369/// Before the implicitly-declared copy assignment operator for a class is
1370/// implicitly defined, all implicitly- declared copy assignment operators for
1371/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001372/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001373/// The implicitly-defined copy assignment operator for class X performs
1374/// memberwise assignment of its subob- jects. The direct base classes of X are
1375/// assigned first, in the order of their declaration in
1376/// the base-specifier-list, and then the immediate nonstatic data members of X
1377/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001378/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001379/// if the subobject is of class type, the copy assignment operator for the
1380/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001381/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001382///
Mike Stump1eb44332009-09-09 15:08:12 +00001383/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001384/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001385///
Mike Stump1eb44332009-09-09 15:08:12 +00001386/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001387/// used.
1388void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001389 llvm::Function *Fn,
1390 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001391
1392 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1393 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1394 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001395 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001397 FunctionArgList::const_iterator i = Args.begin();
1398 const VarDecl *ThisArg = i->first;
1399 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1400 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1401 const VarDecl *SrcArg = (i+1)->first;
1402 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1403 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001405 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1406 Base != ClassDecl->bases_end(); ++Base) {
1407 // FIXME. copy assignment of virtual base NYI
1408 if (Base->isVirtual())
1409 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001411 CXXRecordDecl *BaseClassDecl
1412 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1413 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1414 Base->getType());
1415 }
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001417 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1418 FieldEnd = ClassDecl->field_end();
1419 Field != FieldEnd; ++Field) {
1420 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001421 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001422 getContext().getAsConstantArrayType(FieldType);
1423 if (Array)
1424 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001426 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1427 CXXRecordDecl *FieldClassDecl
1428 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1429 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1430 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001431 if (Array) {
1432 const llvm::Type *BasePtr = ConvertType(FieldType);
1433 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1434 llvm::Value *DestBaseAddrPtr =
1435 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1436 llvm::Value *SrcBaseAddrPtr =
1437 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1438 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1439 FieldClassDecl, FieldType);
1440 }
1441 else
Mike Stump1eb44332009-09-09 15:08:12 +00001442 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001443 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001444 continue;
1445 }
1446 // Do a built-in assignment of scalar data members.
1447 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1448 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1449 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1450 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001451 }
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001453 // return *this;
1454 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001456 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001457}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001458
Anders Carlssonb1156b92009-11-06 03:23:06 +00001459static void EmitBaseInitializer(CodeGenFunction &CGF,
1460 const CXXRecordDecl *ClassDecl,
1461 CXXBaseOrMemberInitializer *BaseInit,
1462 CXXCtorType CtorType) {
1463 assert(BaseInit->isBaseInitializer() &&
1464 "Must have base initializer!");
1465
1466 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1467
1468 const Type *BaseType = BaseInit->getBaseClass();
1469 CXXRecordDecl *BaseClassDecl =
1470 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
1471 llvm::Value *V = CGF.GetAddressCXXOfBaseClass(ThisPtr, ClassDecl,
1472 BaseClassDecl,
1473 /*NullCheckValue=*/false);
1474 CGF.EmitCXXConstructorCall(BaseInit->getConstructor(),
1475 CtorType, V,
1476 BaseInit->const_arg_begin(),
1477 BaseInit->const_arg_end());
1478}
1479
1480static void EmitMemberInitializer(CodeGenFunction &CGF,
1481 const CXXRecordDecl *ClassDecl,
1482 CXXBaseOrMemberInitializer *MemberInit) {
1483 assert(MemberInit->isMemberInitializer() &&
1484 "Must have member initializer!");
1485
1486 // non-static data member initializers.
1487 FieldDecl *Field = MemberInit->getMember();
1488 QualType FieldType = CGF.getContext().getCanonicalType((Field)->getType());
1489 const ConstantArrayType *Array =
1490 CGF.getContext().getAsConstantArrayType(FieldType);
1491 if (Array)
1492 FieldType = CGF.getContext().getBaseElementType(FieldType);
1493
1494 llvm::Value *ThisPtr = CGF.LoadCXXThis();
1495 LValue LHS;
1496 if (FieldType->isReferenceType()) {
1497 // FIXME: This is really ugly; should be refactored somehow
1498 unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field);
1499 llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp");
1500 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1501 LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType));
1502 } else {
1503 LHS = CGF.EmitLValueForField(ThisPtr, Field, false, 0);
1504 }
1505 if (FieldType->getAs<RecordType>()) {
1506 if (!Field->isAnonymousStructOrUnion()) {
1507 assert(MemberInit->getConstructor() &&
1508 "EmitCtorPrologue - no constructor to initialize member");
1509 if (Array) {
1510 const llvm::Type *BasePtr = CGF.ConvertType(FieldType);
1511 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1512 llvm::Value *BaseAddrPtr =
1513 CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1514 CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(),
1515 Array, BaseAddrPtr);
1516 }
1517 else
1518 CGF.EmitCXXConstructorCall(MemberInit->getConstructor(),
1519 Ctor_Complete, LHS.getAddress(),
1520 MemberInit->const_arg_begin(),
1521 MemberInit->const_arg_end());
1522 return;
1523 }
1524 else {
1525 // Initializing an anonymous union data member.
1526 FieldDecl *anonMember = MemberInit->getAnonUnionMember();
1527 LHS = CGF.EmitLValueForField(LHS.getAddress(), anonMember,
1528 /*IsUnion=*/true, 0);
1529 FieldType = anonMember->getType();
1530 }
1531 }
1532
1533 assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only");
1534 Expr *RhsExpr = *MemberInit->arg_begin();
1535 RValue RHS;
1536 if (FieldType->isReferenceType())
1537 RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType,
1538 /*IsInitializer=*/true);
1539 else if (FieldType->isMemberFunctionPointerType())
1540 RHS = RValue::get(CGF.CGM.EmitConstantExpr(RhsExpr, FieldType, &CGF));
1541 else
1542 RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true));
Fariborz Jahaniane8b31cc2009-11-11 17:55:25 +00001543 if (Array && !FieldType->getAs<RecordType>()) {
1544 // value initialize a non-class array data member using arr() syntax in
1545 // initializer list.
1546 QualType Ty = CGF.getContext().getCanonicalType((Field)->getType());
1547 CGF.EmitMemSetToZero(LHS.getAddress(), Ty);
1548 }
1549 else
1550 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType);
Anders Carlssonb1156b92009-11-06 03:23:06 +00001551}
1552
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001553/// EmitCtorPrologue - This routine generates necessary code to initialize
1554/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001555/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001556void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1557 CXXCtorType CtorType) {
Anders Carlssonb1156b92009-11-06 03:23:06 +00001558 const CXXRecordDecl *ClassDecl = CD->getParent();
1559
Mike Stumpeb19fa92009-08-06 13:41:24 +00001560 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001561 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001563 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001564 E = CD->init_end();
1565 B != E; ++B) {
1566 CXXBaseOrMemberInitializer *Member = (*B);
Anders Carlssonb1156b92009-11-06 03:23:06 +00001567
Anders Carlsson1faf6742009-11-06 04:11:09 +00001568 assert(LiveTemporaries.empty() &&
1569 "Should not have any live temporaries at initializer start!");
1570
Anders Carlssonb1156b92009-11-06 03:23:06 +00001571 if (Member->isBaseInitializer())
1572 EmitBaseInitializer(*this, ClassDecl, Member, CtorType);
1573 else
1574 EmitMemberInitializer(*this, ClassDecl, Member);
Anders Carlsson1faf6742009-11-06 04:11:09 +00001575
1576 // Pop any live temporaries that the initializers might have pushed.
1577 while (!LiveTemporaries.empty())
1578 PopCXXTemporary();
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001579 }
Mike Stumpf1216772009-07-31 18:25:34 +00001580
1581 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001582 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001583 if (!LoadOfThis)
1584 LoadOfThis = LoadCXXThis();
1585 llvm::Value *VtableField;
1586 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001587 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001588 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1589 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
Mike Stump380dd752009-11-10 07:44:33 +00001590 llvm::Value *vtable = CGM.getVtableInfo().getVtable(ClassDecl);
Mike Stumpf1216772009-07-31 18:25:34 +00001591 Builder.CreateStore(vtable, VtableField);
1592 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001593}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001594
1595/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001596/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001597/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001598/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001599void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1600 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001601 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001602 assert(!ClassDecl->getNumVBases() &&
1603 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001604 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001606 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1607 *E = DD->destr_end(); B != E; ++B) {
1608 uintptr_t BaseOrMember = (*B);
1609 if (DD->isMemberToDestroy(BaseOrMember)) {
1610 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1611 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001612 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001613 getContext().getAsConstantArrayType(FieldType);
1614 if (Array)
1615 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001616 const RecordType *RT = FieldType->getAs<RecordType>();
1617 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1618 if (FieldClassDecl->hasTrivialDestructor())
1619 continue;
1620 llvm::Value *LoadOfThis = LoadCXXThis();
1621 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001622 if (Array) {
1623 const llvm::Type *BasePtr = ConvertType(FieldType);
1624 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001625 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001626 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001627 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001628 Array, BaseAddrPtr);
1629 }
1630 else
1631 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1632 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001633 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001634 const RecordType *RT =
1635 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1636 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1637 if (BaseClassDecl->hasTrivialDestructor())
1638 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001639 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1640 ClassDecl, BaseClassDecl,
1641 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001642 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001643 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001644 }
1645 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001646 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1647 return;
1648 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001649 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001650 // reverse order of their construction.
1651 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001653 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1654 FieldEnd = ClassDecl->field_end();
1655 Field != FieldEnd; ++Field) {
1656 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001657 if (getContext().getAsConstantArrayType(FieldType))
1658 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001659 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1660 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1661 if (FieldClassDecl->hasTrivialDestructor())
1662 continue;
1663 DestructedFields.push_back(*Field);
1664 }
1665 }
1666 if (!DestructedFields.empty())
1667 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1668 FieldDecl *Field = DestructedFields[i];
1669 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001670 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001671 getContext().getAsConstantArrayType(FieldType);
1672 if (Array)
1673 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001674 const RecordType *RT = FieldType->getAs<RecordType>();
1675 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1676 llvm::Value *LoadOfThis = LoadCXXThis();
1677 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001678 if (Array) {
1679 const llvm::Type *BasePtr = ConvertType(FieldType);
1680 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001681 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001682 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001683 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001684 Array, BaseAddrPtr);
1685 }
1686 else
1687 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1688 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001689 }
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001691 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1692 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1693 Base != ClassDecl->bases_end(); ++Base) {
1694 // FIXME. copy assignment of virtual base NYI
1695 if (Base->isVirtual())
1696 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001698 CXXRecordDecl *BaseClassDecl
1699 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1700 if (BaseClassDecl->hasTrivialDestructor())
1701 continue;
1702 DestructedBases.push_back(BaseClassDecl);
1703 }
1704 if (DestructedBases.empty())
1705 return;
1706 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1707 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001708 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1709 ClassDecl,BaseClassDecl,
1710 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001711 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1712 Dtor_Complete, V);
1713 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001714}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001715
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001716void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1717 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001718 llvm::Function *Fn,
1719 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001721 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001722 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1723 "SynthesizeDefaultDestructor - destructor has user declaration");
1724 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001726 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1727 SourceLocation());
1728 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001729 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001730}
Anders Carlsson6815e942009-09-27 18:58:34 +00001731
1732// FIXME: Move this to CGCXXStmt.cpp
1733void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1734 // FIXME: We need to do more here.
1735 EmitStmt(S.getTryBlock());
1736}