blob: d8b532e697ba095bf3a6dbc8714f9b721a2e1c3f [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump1eb44332009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlssone1b29ef2008-08-22 16:00:37 +000015
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson283a0622009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000020#include "clang/AST/RecordLayout.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000021#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson6815e942009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000026using namespace clang;
27using namespace CodeGen;
28
Mike Stump1eb44332009-09-09 15:08:12 +000029void
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000030CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor,
31 llvm::Constant *DeclPtr) {
Anders Carlsson6815e942009-09-27 18:58:34 +000032 const llvm::Type *Int8PtrTy =
33 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
Mike Stump1eb44332009-09-09 15:08:12 +000034
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000035 std::vector<const llvm::Type *> Params;
36 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000038 // Get the destructor function type
Mike Stump1eb44332009-09-09 15:08:12 +000039 const llvm::Type *DtorFnTy =
Owen Anderson0032b272009-08-13 21:57:51 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000041 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump1eb44332009-09-09 15:08:12 +000042
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000043 Params.clear();
44 Params.push_back(DtorFnTy);
45 Params.push_back(Int8PtrTy);
46 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000048 // Get the __cxa_atexit function type
49 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Mike Stump1eb44332009-09-09 15:08:12 +000050 const llvm::FunctionType *AtExitFnTy =
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000051 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000053 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
54 "__cxa_atexit");
Mike Stump1eb44332009-09-09 15:08:12 +000055
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000056 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
57 "__dso_handle");
Mike Stump1eb44332009-09-09 15:08:12 +000058
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000059 llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000061 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
62 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
63 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
64 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
65}
66
Mike Stump1eb44332009-09-09 15:08:12 +000067void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000068 llvm::Constant *DeclPtr) {
69 assert(D.hasGlobalStorage() &&
70 "VarDecl must have global storage!");
Mike Stump1eb44332009-09-09 15:08:12 +000071
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000072 const Expr *Init = D.getInit();
73 QualType T = D.getType();
Mike Stumpdf317bf2009-11-03 23:25:48 +000074 bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified();
Mike Stump1eb44332009-09-09 15:08:12 +000075
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000076 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000077 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000078 } else if (!hasAggregateLLVMType(T)) {
79 llvm::Value *V = EmitScalarExpr(Init);
Mike Stumpdf317bf2009-11-03 23:25:48 +000080 EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000081 } else if (T->isAnyComplexType()) {
Mike Stumpdf317bf2009-11-03 23:25:48 +000082 EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000083 } else {
Mike Stumpdf317bf2009-11-03 23:25:48 +000084 EmitAggExpr(Init, DeclPtr, isVolatile);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000086 if (const RecordType *RT = T->getAs<RecordType>()) {
87 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
88 if (!RD->hasTrivialDestructor())
89 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr);
90 }
91 }
92}
93
Anders Carlsson89ed31d2009-08-08 23:24:23 +000094void
95CodeGenModule::EmitCXXGlobalInitFunc() {
96 if (CXXGlobalInits.empty())
97 return;
Mike Stump1eb44332009-09-09 15:08:12 +000098
Mike Stump79d57682009-11-04 01:11:15 +000099 const llvm::FunctionType *FTy
100 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
101 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000103 // Create our global initialization function.
104 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000105 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000106 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
107 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000109 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000110 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000111 CXXGlobalInits.size());
112 AddGlobalCtor(Fn);
113}
114
115void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
116 const VarDecl **Decls,
117 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000118 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000119 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000121 for (unsigned i = 0; i != NumDecls; ++i) {
122 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000124 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
125 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
126 }
127 FinishFunction();
128}
129
Mike Stump1eb44332009-09-09 15:08:12 +0000130void
131CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000132 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000133 // FIXME: This should use __cxa_guard_{acquire,release}?
134
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000135 assert(!getContext().getLangOptions().ThreadsafeStatics &&
136 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000137
Anders Carlsson283a0622009-04-13 18:03:33 +0000138 llvm::SmallString<256> GuardVName;
139 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000140 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000142 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000143 llvm::GlobalValue *GuardV =
Mike Stump79d57682009-11-04 01:11:15 +0000144 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext),
145 false, GV->getLinkage(),
146 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000147 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000149 // Load the first byte of the guard variable.
Mike Stump79d57682009-11-04 01:11:15 +0000150 const llvm::Type *PtrTy
151 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000152 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000153 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000155 // Compare it against 0.
Mike Stump79d57682009-11-04 01:11:15 +0000156 llvm::Value *nullValue
157 = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000158 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Daniel Dunbar55e87422008-11-11 02:29:29 +0000160 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000161 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000162
163 // If the guard variable is 0, jump to the initializer code.
164 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000166 EmitBlock(InitBlock);
167
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000168 EmitCXXGlobalVarDeclInit(D, GV);
169
Mike Stump79d57682009-11-04 01:11:15 +0000170 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
171 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000172 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000174 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000175}
176
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000177RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
178 llvm::Value *Callee,
179 llvm::Value *This,
180 CallExpr::const_arg_iterator ArgBeg,
181 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000182 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000183 "Trying to emit a member call expr on a static method!");
184
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000185 // A call to a trivial destructor requires no code generation.
186 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
187 if (Destructor->isTrivial())
188 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000189
John McCall183700f2009-09-21 23:43:11 +0000190 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000192 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000194 // Push the this ptr.
195 Args.push_back(std::make_pair(RValue::get(This),
196 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000198 // And the rest of the call args
199 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
John McCall183700f2009-09-21 23:43:11 +0000201 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000202 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
203 Callee, Args, MD);
204}
205
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000206/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
207/// expr can be devirtualized.
208static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
209 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
210 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
211 // This is a record decl. We know the type and can devirtualize it.
212 return VD->getType()->isRecordType();
213 }
Anders Carlsson76366482009-10-12 19:45:47 +0000214
215 return false;
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000216 }
217
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000218 // We can always devirtualize calls on temporary object expressions.
Anders Carlsson76366482009-10-12 19:45:47 +0000219 if (isa<CXXTemporaryObjectExpr>(Base))
220 return true;
221
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000222 // And calls on bound temporaries.
223 if (isa<CXXBindTemporaryExpr>(Base))
224 return true;
225
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000226 // Check if this is a call expr that returns a record type.
227 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
228 return CE->getCallReturnType()->isRecordType();
229
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000230 // We can't devirtualize the call.
231 return false;
232}
233
Anders Carlsson774e7c62009-04-03 22:50:24 +0000234RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000235 if (isa<BinaryOperator>(CE->getCallee()))
236 return EmitCXXMemberPointerCallExpr(CE);
237
Anders Carlsson774e7c62009-04-03 22:50:24 +0000238 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
239 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000240
Anders Carlsson2472bf02009-09-29 03:54:11 +0000241 if (MD->isStatic()) {
242 // The method is static, emit it as we would a regular call.
243 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
244 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
245 CE->arg_begin(), CE->arg_end(), 0);
246
247 }
248
John McCall183700f2009-09-21 23:43:11 +0000249 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000250
Mike Stump1eb44332009-09-09 15:08:12 +0000251 const llvm::Type *Ty =
252 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000253 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000254 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Anders Carlsson774e7c62009-04-03 22:50:24 +0000256 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000257 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000258 else {
259 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000260 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000261 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000262
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000263 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000264 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000265 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000266 //
267 // We also don't emit a virtual call if the base expression has a record type
268 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000269 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000270 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000271 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000272 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000273 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000274 = dyn_cast<CXXDestructorDecl>(MD))
275 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000276 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000277 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
279 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000280 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000281}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000282
Mike Stump1eb44332009-09-09 15:08:12 +0000283RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000284CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
285 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson3eea6352009-10-13 17:41:28 +0000286 const Expr *BaseExpr = BO->getLHS();
287 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000288
Anders Carlsson3eea6352009-10-13 17:41:28 +0000289 const MemberPointerType *MPT =
290 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000291 const FunctionProtoType *FPT =
292 MPT->getPointeeType()->getAs<FunctionProtoType>();
293 const CXXRecordDecl *RD =
Douglas Gregor87c12c42009-11-04 16:49:01 +0000294 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
Anders Carlsson375c31c2009-10-03 19:43:08 +0000295
296 const llvm::FunctionType *FTy =
297 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
298 FPT->isVariadic());
299
300 const llvm::Type *Int8PtrTy =
301 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
302
303 // Get the member function pointer.
304 llvm::Value *MemFnPtr =
Anders Carlsson3eea6352009-10-13 17:41:28 +0000305 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
306 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000307
308 // Emit the 'this' pointer.
309 llvm::Value *This;
310
311 if (BO->getOpcode() == BinaryOperator::PtrMemI)
312 This = EmitScalarExpr(BaseExpr);
313 else
314 This = EmitLValue(BaseExpr).getAddress();
315
316 // Adjust it.
317 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
318 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
319
320 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
321 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
322
323 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
324
325 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
326
327 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
328
329 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
330
331 // If the LSB in the function pointer is 1, the function pointer points to
332 // a virtual function.
333 llvm::Value *IsVirtual
334 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
335 "and");
336
337 IsVirtual = Builder.CreateTrunc(IsVirtual,
338 llvm::Type::getInt1Ty(VMContext));
339
340 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
341 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
342 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
343
344 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
345 EmitBlock(FnVirtual);
346
347 const llvm::Type *VTableTy =
348 FTy->getPointerTo()->getPointerTo()->getPointerTo();
349
350 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
351 VTable = Builder.CreateLoad(VTable);
352
353 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
354
355 // Since the function pointer is 1 plus the virtual table offset, we
356 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000357 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000358
359 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
360
361 EmitBranch(FnEnd);
362 EmitBlock(FnNonVirtual);
363
364 // If the function is not virtual, just load the pointer.
365 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
366 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
367
368 EmitBlock(FnEnd);
369
370 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
371 Callee->reserveOperandSpace(2);
372 Callee->addIncoming(VirtualFn, FnVirtual);
373 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
374
375 CallArgList Args;
376
377 QualType ThisType =
378 getContext().getPointerType(getContext().getTagDeclType(RD));
379
380 // Push the this ptr.
381 Args.push_back(std::make_pair(RValue::get(This), ThisType));
382
383 // And the rest of the call args
384 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
385 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
386 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
387 Callee, Args, 0);
388}
389
390RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000391CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
392 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000393 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000394 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Fariborz Jahanianad258832009-08-13 21:09:41 +0000396 if (MD->isCopyAssignment()) {
397 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
398 if (ClassDecl->hasTrivialCopyAssignment()) {
399 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
400 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
401 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
402 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
403 QualType Ty = E->getType();
404 EmitAggregateCopy(This, Src, Ty);
405 return RValue::get(This);
406 }
407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
John McCall183700f2009-09-21 23:43:11 +0000409 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000410 const llvm::Type *Ty =
411 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000412 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000413 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Anders Carlsson0f294632009-05-27 04:18:27 +0000415 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Anders Carlsson0f294632009-05-27 04:18:27 +0000417 return EmitCXXMemberCall(MD, Callee, This,
418 E->arg_begin() + 1, E->arg_end());
419}
420
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000421llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000422 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000423 "Must be in a C++ member function decl to load 'this'");
424 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
425 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000427 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000428 // ans: See how CodeGenFunction::LoadObjCSelf() uses
429 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000430 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
431}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000432
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000433/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
434/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000435/// array.
436/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
437/// array type and 'ArrayPtr' points to the beginning fo the array.
438/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000439void
440CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000441 const ConstantArrayType *ArrayTy,
442 llvm::Value *ArrayPtr) {
443 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
444 llvm::Value * NumElements =
445 llvm::ConstantInt::get(SizeTy,
446 getContext().getConstantArrayElementCount(ArrayTy));
447
448 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
449}
450
451void
452CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
453 llvm::Value *NumElements,
454 llvm::Value *ArrayPtr) {
455 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000457 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000458 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
459 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
460 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000462 // Start the loop with a block that tests the condition.
463 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
464 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000466 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000468 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000470 // Generate: if (loop-index < number-of-elements fall to the loop body,
471 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000472 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000473 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000474 // If the condition is true, execute the body.
475 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000477 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000479 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000480 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000481 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000482 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
483 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000484 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000486 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000488 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000489 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000490 Counter = Builder.CreateLoad(IndexPtr);
491 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
492 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000494 // Finally, branch back up to the condition for the next iteration.
495 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000497 // Emit the fall-through block.
498 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000499}
500
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000501/// EmitCXXAggrDestructorCall - calls the default destructor on array
502/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000503void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000504CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
505 const ArrayType *Array,
506 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000507 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
508 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000509 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000510 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000511 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000512 // Create a temporary for the loop index and initialize it with count of
513 // array elements.
514 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
515 "loop.index");
516 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000517 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000518 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
519 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000521 // Start the loop with a block that tests the condition.
522 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
523 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000525 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000527 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000529 // Generate: if (loop-index != 0 fall to the loop body,
530 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000531 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000532 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
533 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
534 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
535 "isne");
536 // If the condition is true, execute the body.
537 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000539 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000541 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
542 // Inside the loop body, emit the constructor call on the array element.
543 Counter = Builder.CreateLoad(IndexPtr);
544 Counter = Builder.CreateSub(Counter, One);
545 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
546 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000548 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000550 // Emit the decrement of the loop counter.
551 Counter = Builder.CreateLoad(IndexPtr);
552 Counter = Builder.CreateSub(Counter, One, "dec");
553 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000555 // Finally, branch back up to the condition for the next iteration.
556 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000558 // Emit the fall-through block.
559 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000560}
561
562void
Mike Stump1eb44332009-09-09 15:08:12 +0000563CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
564 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000565 llvm::Value *This,
566 CallExpr::const_arg_iterator ArgBeg,
567 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000568 if (D->isCopyConstructor(getContext())) {
569 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
570 if (ClassDecl->hasTrivialCopyConstructor()) {
571 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
572 "EmitCXXConstructorCall - user declared copy constructor");
573 const Expr *E = (*ArgBeg);
574 QualType Ty = E->getType();
575 llvm::Value *Src = EmitLValue(E).getAddress();
576 EmitAggregateCopy(This, Src, Ty);
577 return;
578 }
579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000581 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
582
583 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000584}
585
Mike Stump1eb44332009-09-09 15:08:12 +0000586void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000587 CXXDtorType Type,
588 llvm::Value *This) {
589 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Anders Carlsson7267c162009-05-29 21:03:38 +0000591 EmitCXXMemberCall(D, Callee, This, 0, 0);
592}
593
Mike Stump1eb44332009-09-09 15:08:12 +0000594void
595CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000596 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000597 assert(Dest && "Must have a destination!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000598 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000599 const ConstantArrayType *Array =
600 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000601 // For a copy constructor, even if it is trivial, must fall thru so
602 // its argument is code-gen'ed.
603 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000604 QualType InitType = E->getType();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000605 if (Array)
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000606 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000607 const CXXRecordDecl *RD =
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000608 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000609 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000610 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000613 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000614 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000615 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000616 EmitAggExpr((*i), Dest, false);
617 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000618 }
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000619 if (Array) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000620 QualType BaseElementTy = getContext().getBaseElementType(Array);
621 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
622 BasePtr = llvm::PointerType::getUnqual(BasePtr);
623 llvm::Value *BaseAddrPtr =
624 Builder.CreateBitCast(Dest, BasePtr);
625 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
626 }
627 else
628 // Call the constructor.
629 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
630 E->arg_begin(), E->arg_end());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000631}
632
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000633void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000634 EmitGlobal(GlobalDecl(D, Ctor_Complete));
635 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000636}
Anders Carlsson363c1842009-04-16 23:57:24 +0000637
Mike Stump1eb44332009-09-09 15:08:12 +0000638void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000639 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Anders Carlsson27ae5362009-04-17 01:58:57 +0000641 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000643 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Anders Carlsson27ae5362009-04-17 01:58:57 +0000645 SetFunctionDefinitionAttributes(D, Fn);
646 SetLLVMFunctionAttributesForDefinition(D, Fn);
647}
648
Anders Carlsson363c1842009-04-16 23:57:24 +0000649llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000650CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000651 CXXCtorType Type) {
652 const llvm::FunctionType *FTy =
653 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Anders Carlsson363c1842009-04-16 23:57:24 +0000655 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000656 return cast<llvm::Function>(
657 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000658}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000659
Mike Stump1eb44332009-09-09 15:08:12 +0000660const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000661 CXXCtorType Type) {
662 llvm::SmallString<256> Name;
663 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000664 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Anders Carlsson27ae5362009-04-17 01:58:57 +0000666 Name += '\0';
667 return UniqueMangledName(Name.begin(), Name.end());
668}
669
670void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000671 EmitCXXDestructor(D, Dtor_Complete);
672 EmitCXXDestructor(D, Dtor_Base);
673}
674
Mike Stump1eb44332009-09-09 15:08:12 +0000675void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000676 CXXDtorType Type) {
677 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000679 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Anders Carlsson27ae5362009-04-17 01:58:57 +0000681 SetFunctionDefinitionAttributes(D, Fn);
682 SetLLVMFunctionAttributesForDefinition(D, Fn);
683}
684
685llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000686CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000687 CXXDtorType Type) {
688 const llvm::FunctionType *FTy =
689 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Anders Carlsson27ae5362009-04-17 01:58:57 +0000691 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000692 return cast<llvm::Function>(
693 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000694}
695
Mike Stump1eb44332009-09-09 15:08:12 +0000696const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000697 CXXDtorType Type) {
698 llvm::SmallString<256> Name;
699 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000700 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Anders Carlsson27ae5362009-04-17 01:58:57 +0000702 Name += '\0';
703 return UniqueMangledName(Name.begin(), Name.end());
704}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000705
Mike Stumped032eb2009-09-04 18:27:16 +0000706llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
707 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000708 bool Extern, int64_t nv,
709 int64_t v) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000710 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stumped032eb2009-09-04 18:27:16 +0000711}
712
Mike Stumpc902d222009-11-03 16:59:27 +0000713llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
714 int64_t v) {
715 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
716 0);
717 const llvm::Type *OrigTy = V->getType();
718 if (nv) {
719 // Do the non-virtual adjustment
720 V = Builder.CreateBitCast(V, Ptr8Ty);
721 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
722 V = Builder.CreateBitCast(V, OrigTy);
723 }
724 if (v) {
725 // Do the virtual this adjustment
726 const llvm::Type *PtrDiffTy =
727 ConvertType(getContext().getPointerDiffType());
728 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
729 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
730 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
731 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
732 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
733 V = Builder.CreateLoad(V, "vtable");
734 llvm::Value *VTablePtr = V;
735 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
736 v /= LLVMPointerWidth/8;
737 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
738 V = Builder.CreateLoad(V);
739 V = Builder.CreateGEP(ThisVal, V);
740 V = Builder.CreateBitCast(V, OrigTy);
741 }
742 return V;
743}
744
Mike Stump6e319f62009-09-11 23:25:56 +0000745llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
746 const CXXMethodDecl *MD,
747 bool Extern,
748 int64_t nv_t,
749 int64_t v_t,
750 int64_t nv_r,
751 int64_t v_r) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000752 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000753
754 FunctionArgList Args;
755 ImplicitParamDecl *ThisDecl =
756 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
757 MD->getThisType(getContext()));
758 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
759 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
760 e = MD->param_end();
761 i != e; ++i) {
762 ParmVarDecl *D = *i;
763 Args.push_back(std::make_pair(D, D->getType()));
764 }
765 IdentifierInfo *II
766 = &CGM.getContext().Idents.get("__thunk_named_foo_");
767 FunctionDecl *FD = FunctionDecl::Create(getContext(),
768 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000769 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000770 Extern
771 ? FunctionDecl::Extern
772 : FunctionDecl::Static,
773 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000774 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
775
776 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000777 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
778 const llvm::Type *Ty =
779 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
780 FPT->isVariadic());
781 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000782 CallArgList CallArgs;
783
Mike Stump736529e2009-11-03 02:12:59 +0000784 QualType ArgType = MD->getThisType(getContext());
785 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stumpd0fe5362009-11-04 00:53:51 +0000786 if (nv_t || v_t) {
Mike Stumpc902d222009-11-03 16:59:27 +0000787 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000788 const llvm::Type *OrigTy = Callee->getType();
Mike Stumpc902d222009-11-03 16:59:27 +0000789 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stumpd0fe5362009-11-04 00:53:51 +0000790 if (nv_r || v_r) {
791 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
792 Callee = Builder.CreateBitCast(Callee, OrigTy);
793 nv_r = v_r = 0;
794 }
795 }
796
Mike Stump736529e2009-11-03 02:12:59 +0000797 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
798
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000799 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
800 e = MD->param_end();
801 i != e; ++i) {
802 ParmVarDecl *D = *i;
803 QualType ArgType = D->getType();
804
805 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
806 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000807 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
808 }
809
Mike Stumpf49ed942009-11-02 23:47:45 +0000810 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
811 Callee, CallArgs, MD);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000812 if (nv_r || v_r) {
Mike Stumpc902d222009-11-03 16:59:27 +0000813 // Do the return result adjustment.
814 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000815 }
816
Mike Stumpf49ed942009-11-02 23:47:45 +0000817 if (!ResultType->isVoidType())
818 EmitReturnOfRValue(RV, ResultType);
819
Mike Stump6e319f62009-09-11 23:25:56 +0000820 FinishFunction();
821 return Fn;
822}
823
Mike Stump77ca8f62009-09-05 07:20:32 +0000824llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
825 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000826 llvm::SmallString<256> OutName;
827 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000828 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000829 llvm::GlobalVariable::LinkageTypes linktype;
830 linktype = llvm::GlobalValue::WeakAnyLinkage;
831 if (!Extern)
832 linktype = llvm::GlobalValue::InternalLinkage;
833 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000834 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000835 const llvm::FunctionType *FTy =
836 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
837 FPT->isVariadic());
838
839 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
840 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000841 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000842 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
843 return m;
844}
845
Mike Stump6e319f62009-09-11 23:25:56 +0000846llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
847 bool Extern, int64_t nv_t,
848 int64_t v_t, int64_t nv_r,
849 int64_t v_r) {
850 llvm::SmallString<256> OutName;
851 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000852 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000853 llvm::GlobalVariable::LinkageTypes linktype;
854 linktype = llvm::GlobalValue::WeakAnyLinkage;
855 if (!Extern)
856 linktype = llvm::GlobalValue::InternalLinkage;
857 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000858 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000859 const llvm::FunctionType *FTy =
860 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
861 FPT->isVariadic());
862
863 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
864 &getModule());
865 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
866 v_r);
Mike Stump6e319f62009-09-11 23:25:56 +0000867 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
868 return m;
869}
870
Mike Stumpf0070db2009-08-26 20:46:33 +0000871llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000872CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
873 const CXXRecordDecl *ClassDecl,
874 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000875 const llvm::Type *Int8PtrTy =
876 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
877
878 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
879 Int8PtrTy->getPointerTo());
880 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
881
Anders Carlssondbd920c2009-10-11 22:13:54 +0000882 int64_t VBaseOffsetIndex =
883 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
884
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000885 llvm::Value *VBaseOffsetPtr =
Mike Stump79d57682009-11-04 01:11:15 +0000886 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000887 const llvm::Type *PtrDiffTy =
888 ConvertType(getContext().getPointerDiffType());
889
890 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
891 PtrDiffTy->getPointerTo());
892
893 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
894
895 return VBaseOffset;
896}
897
898llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000899CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
900 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000901 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000902
Mike Stumpf0070db2009-08-26 20:46:33 +0000903 Ty = llvm::PointerType::get(Ty, 0);
904 Ty = llvm::PointerType::get(Ty, 0);
905 Ty = llvm::PointerType::get(Ty, 0);
906 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
907 vtbl = Builder.CreateLoad(vtbl);
908 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000909 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000910 vfn = Builder.CreateLoad(vfn);
911 return vfn;
912}
913
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000914/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
915/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
916/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000917// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000918void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000919 llvm::Value *Src,
920 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000921 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000922 QualType Ty) {
923 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
924 assert(CA && "VLA cannot be copied over");
925 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000927 // Create a temporary for the loop index and initialize it with 0.
928 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
929 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000930 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000931 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000932 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000933 // Start the loop with a block that tests the condition.
934 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
935 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000937 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000939 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
940 // Generate: if (loop-index < number-of-elements fall to the loop body,
941 // otherwise, go to the block after the for-loop.
942 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000943 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000944 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
945 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000946 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000947 "isless");
948 // If the condition is true, execute the body.
949 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000951 EmitBlock(ForBody);
952 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
953 // Inside the loop body, emit the constructor call on the array element.
954 Counter = Builder.CreateLoad(IndexPtr);
955 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
956 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
957 if (BitwiseCopy)
958 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000959 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000960 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000961 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000962 Ctor_Complete);
963 CallArgList CallArgs;
964 // Push the this (Dest) ptr.
965 CallArgs.push_back(std::make_pair(RValue::get(Dest),
966 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000968 // Push the Src ptr.
969 CallArgs.push_back(std::make_pair(RValue::get(Src),
Mike Stump79d57682009-11-04 01:11:15 +0000970 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000971 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000972 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000973 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
974 Callee, CallArgs, BaseCopyCtor);
975 }
976 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000978 // Emit the increment of the loop counter.
979 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
980 Counter = Builder.CreateLoad(IndexPtr);
981 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
982 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000984 // Finally, branch back up to the condition for the next iteration.
985 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000987 // Emit the fall-through block.
988 EmitBlock(AfterFor, true);
989}
990
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000991/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000992/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000993/// bitwise assignment or via a copy assignment operator function call.
994/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000995void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000996 llvm::Value *Src,
997 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000998 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000999 QualType Ty) {
1000 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1001 assert(CA && "VLA cannot be asssigned");
1002 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001004 // Create a temporary for the loop index and initialize it with 0.
1005 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1006 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001007 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001008 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1009 Builder.CreateStore(zeroConstant, IndexPtr, false);
1010 // Start the loop with a block that tests the condition.
1011 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1012 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001014 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001016 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1017 // Generate: if (loop-index < number-of-elements fall to the loop body,
1018 // otherwise, go to the block after the for-loop.
1019 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001020 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001021 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1022 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001023 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001024 "isless");
1025 // If the condition is true, execute the body.
1026 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001028 EmitBlock(ForBody);
1029 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1030 // Inside the loop body, emit the assignment operator call on array element.
1031 Counter = Builder.CreateLoad(IndexPtr);
1032 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1033 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1034 const CXXMethodDecl *MD = 0;
1035 if (BitwiseAssign)
1036 EmitAggregateCopy(Dest, Src, Ty);
1037 else {
1038 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1039 MD);
1040 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1041 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001042 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001043 const llvm::Type *LTy =
1044 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1045 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001046 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001048 CallArgList CallArgs;
1049 // Push the this (Dest) ptr.
1050 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1051 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001053 // Push the Src ptr.
1054 CallArgs.push_back(std::make_pair(RValue::get(Src),
1055 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001056 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001057 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1058 Callee, CallArgs, MD);
1059 }
1060 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001062 // Emit the increment of the loop counter.
1063 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1064 Counter = Builder.CreateLoad(IndexPtr);
1065 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1066 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001068 // Finally, branch back up to the condition for the next iteration.
1069 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001071 // Emit the fall-through block.
1072 EmitBlock(AfterFor, true);
1073}
1074
Fariborz Jahanianca283612009-08-07 23:51:33 +00001075/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1076/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001077/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001078void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001079 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001080 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001081 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1082 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001083 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1084 /*NullCheckValue=*/false);
1085 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1086 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001087 }
1088 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1089 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001090 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001091 }
Mike Stump1eb44332009-09-09 15:08:12 +00001092
1093 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001094 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001095 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001096 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001097 CallArgList CallArgs;
1098 // Push the this (Dest) ptr.
1099 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1100 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Fariborz Jahanianca283612009-08-07 23:51:33 +00001102 // Push the Src ptr.
1103 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001104 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001105 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001106 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001107 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1108 Callee, CallArgs, BaseCopyCtor);
1109 }
1110}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001111
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001112/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001113/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001114/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001115// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001116void CodeGenFunction::EmitClassCopyAssignment(
1117 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001118 const CXXRecordDecl *ClassDecl,
1119 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001120 QualType Ty) {
1121 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001122 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1123 /*NullCheckValue=*/false);
1124 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1125 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001126 }
1127 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1128 EmitAggregateCopy(Dest, Src, Ty);
1129 return;
1130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001132 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001133 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001134 MD);
1135 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1136 (void)ConstCopyAssignOp;
1137
John McCall183700f2009-09-21 23:43:11 +00001138 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001139 const llvm::Type *LTy =
1140 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001141 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001142 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001144 CallArgList CallArgs;
1145 // Push the this (Dest) ptr.
1146 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1147 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001149 // Push the Src ptr.
1150 CallArgs.push_back(std::make_pair(RValue::get(Src),
1151 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001152 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001153 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001154 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1155 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001156}
1157
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001158/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001159void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001160CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1161 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001162 llvm::Function *Fn,
1163 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001164 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1165 SourceLocation());
1166 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001167 FinishFunction();
1168}
1169
Mike Stump79d57682009-11-04 01:11:15 +00001170/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a
1171/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001172/// The implicitly-defined copy constructor for class X performs a memberwise
Mike Stump79d57682009-11-04 01:11:15 +00001173/// copy of its subobjects. The order of copying is the same as the order of
1174/// initialization of bases and members in a user-defined constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001175/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001176/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001177/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001178/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001179/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001180/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001181/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001182/// Virtual base class subobjects shall be copied only once by the
1183/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001184
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001185void
1186CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1187 CXXCtorType Type,
1188 llvm::Function *Fn,
1189 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001190 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001191 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Mike Stump79d57682009-11-04 01:11:15 +00001192 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001193 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1194 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001196 FunctionArgList::const_iterator i = Args.begin();
1197 const VarDecl *ThisArg = i->first;
1198 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1199 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1200 const VarDecl *SrcArg = (i+1)->first;
1201 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1202 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001204 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1205 Base != ClassDecl->bases_end(); ++Base) {
1206 // FIXME. copy constrution of virtual base NYI
1207 if (Base->isVirtual())
1208 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001210 CXXRecordDecl *BaseClassDecl
1211 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001212 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1213 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001216 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1217 FieldEnd = ClassDecl->field_end();
1218 Field != FieldEnd; ++Field) {
1219 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001220 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001221 getContext().getAsConstantArrayType(FieldType);
1222 if (Array)
1223 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001225 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1226 CXXRecordDecl *FieldClassDecl
1227 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1228 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1229 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001230 if (Array) {
1231 const llvm::Type *BasePtr = ConvertType(FieldType);
1232 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001233 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001234 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001235 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001236 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1237 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1238 FieldClassDecl, FieldType);
1239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240 else
1241 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001242 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001243 continue;
1244 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001245 // Do a built-in assignment of scalar data members.
1246 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1247 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1248 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1249 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001250 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001251 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001252}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001253
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001254/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001255/// Before the implicitly-declared copy assignment operator for a class is
1256/// implicitly defined, all implicitly- declared copy assignment operators for
1257/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001258/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001259/// The implicitly-defined copy assignment operator for class X performs
1260/// memberwise assignment of its subob- jects. The direct base classes of X are
1261/// assigned first, in the order of their declaration in
1262/// the base-specifier-list, and then the immediate nonstatic data members of X
1263/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001264/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001265/// if the subobject is of class type, the copy assignment operator for the
1266/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001267/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001268///
Mike Stump1eb44332009-09-09 15:08:12 +00001269/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001270/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001271///
Mike Stump1eb44332009-09-09 15:08:12 +00001272/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001273/// used.
1274void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001275 llvm::Function *Fn,
1276 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001277
1278 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1279 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1280 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001281 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001283 FunctionArgList::const_iterator i = Args.begin();
1284 const VarDecl *ThisArg = i->first;
1285 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1286 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1287 const VarDecl *SrcArg = (i+1)->first;
1288 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1289 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001291 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1292 Base != ClassDecl->bases_end(); ++Base) {
1293 // FIXME. copy assignment of virtual base NYI
1294 if (Base->isVirtual())
1295 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001297 CXXRecordDecl *BaseClassDecl
1298 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1299 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1300 Base->getType());
1301 }
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001303 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1304 FieldEnd = ClassDecl->field_end();
1305 Field != FieldEnd; ++Field) {
1306 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001307 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001308 getContext().getAsConstantArrayType(FieldType);
1309 if (Array)
1310 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001312 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1313 CXXRecordDecl *FieldClassDecl
1314 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1315 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1316 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001317 if (Array) {
1318 const llvm::Type *BasePtr = ConvertType(FieldType);
1319 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1320 llvm::Value *DestBaseAddrPtr =
1321 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1322 llvm::Value *SrcBaseAddrPtr =
1323 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1324 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1325 FieldClassDecl, FieldType);
1326 }
1327 else
Mike Stump1eb44332009-09-09 15:08:12 +00001328 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001329 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001330 continue;
1331 }
1332 // Do a built-in assignment of scalar data members.
1333 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1334 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1335 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1336 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001337 }
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001339 // return *this;
1340 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001342 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001343}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001344
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001345/// EmitCtorPrologue - This routine generates necessary code to initialize
1346/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001347/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001348void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1349 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001350 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001351 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001352 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001354 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001355 E = CD->init_end();
1356 B != E; ++B) {
1357 CXXBaseOrMemberInitializer *Member = (*B);
1358 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001359 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001360 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001361 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001362 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001363 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1364 BaseClassDecl,
1365 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001366 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001367 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001368 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001369 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001370 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001371 // non-static data member initilaizers.
1372 FieldDecl *Field = Member->getMember();
1373 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001374 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001375 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001376 if (Array)
1377 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Mike Stumpf1216772009-07-31 18:25:34 +00001379 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001380 LValue LHS;
1381 if (FieldType->isReferenceType()) {
1382 // FIXME: This is really ugly; should be refactored somehow
1383 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1384 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001385 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1386 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001387 } else {
1388 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1389 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001390 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001391 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001392 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001393 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001394 if (Array) {
1395 const llvm::Type *BasePtr = ConvertType(FieldType);
1396 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001397 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001398 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001399 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001400 Array, BaseAddrPtr);
1401 }
1402 else
1403 EmitCXXConstructorCall(Member->getConstructor(),
1404 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001405 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001406 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001407 continue;
1408 }
1409 else {
1410 // Initializing an anonymous union data member.
1411 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001412 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001413 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001414 FieldType = anonMember->getType();
1415 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001416 }
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001418 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001419 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001420 RValue RHS;
1421 if (FieldType->isReferenceType())
1422 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1423 /*IsInitializer=*/true);
1424 else
1425 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1426 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001427 }
1428 }
Mike Stumpf1216772009-07-31 18:25:34 +00001429
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001430 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001431 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001432 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001433 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001434 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001435 ClassDecl->bases_begin();
1436 Base != ClassDecl->bases_end(); ++Base) {
1437 // FIXME. copy assignment of virtual base NYI
1438 if (Base->isVirtual())
1439 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001441 CXXRecordDecl *BaseClassDecl
1442 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1443 if (BaseClassDecl->hasTrivialConstructor())
1444 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001445 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001446 BaseClassDecl->getDefaultConstructor(getContext())) {
1447 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001448 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1449 BaseClassDecl,
1450 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001451 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1452 }
1453 }
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001455 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1456 FieldEnd = ClassDecl->field_end();
1457 Field != FieldEnd; ++Field) {
1458 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001459 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001460 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001461 if (Array)
1462 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001463 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1464 continue;
1465 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001466 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001467 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1468 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1469 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001470 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001471 MemberClassDecl->getDefaultConstructor(getContext())) {
1472 LoadOfThis = LoadCXXThis();
1473 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001474 if (Array) {
1475 const llvm::Type *BasePtr = ConvertType(FieldType);
1476 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001477 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001478 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1479 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1480 }
1481 else
Mike Stump1eb44332009-09-09 15:08:12 +00001482 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001483 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001484 }
1485 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001486 }
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Mike Stumpf1216772009-07-31 18:25:34 +00001488 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001489 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001490 if (!LoadOfThis)
1491 LoadOfThis = LoadCXXThis();
1492 llvm::Value *VtableField;
1493 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001494 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001495 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1496 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1497 llvm::Value *vtable = GenerateVtable(ClassDecl);
1498 Builder.CreateStore(vtable, VtableField);
1499 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001500}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001501
1502/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001503/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001504/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001505/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001506void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1507 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001508 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001509 assert(!ClassDecl->getNumVBases() &&
1510 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001511 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001513 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1514 *E = DD->destr_end(); B != E; ++B) {
1515 uintptr_t BaseOrMember = (*B);
1516 if (DD->isMemberToDestroy(BaseOrMember)) {
1517 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1518 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001519 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001520 getContext().getAsConstantArrayType(FieldType);
1521 if (Array)
1522 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001523 const RecordType *RT = FieldType->getAs<RecordType>();
1524 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1525 if (FieldClassDecl->hasTrivialDestructor())
1526 continue;
1527 llvm::Value *LoadOfThis = LoadCXXThis();
1528 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001529 if (Array) {
1530 const llvm::Type *BasePtr = ConvertType(FieldType);
1531 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001532 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001533 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001534 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001535 Array, BaseAddrPtr);
1536 }
1537 else
1538 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1539 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001540 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001541 const RecordType *RT =
1542 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1543 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1544 if (BaseClassDecl->hasTrivialDestructor())
1545 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001546 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1547 ClassDecl, BaseClassDecl,
1548 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001549 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001550 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001551 }
1552 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001553 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1554 return;
1555 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001556 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001557 // reverse order of their construction.
1558 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001560 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1561 FieldEnd = ClassDecl->field_end();
1562 Field != FieldEnd; ++Field) {
1563 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001564 if (getContext().getAsConstantArrayType(FieldType))
1565 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001566 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1567 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1568 if (FieldClassDecl->hasTrivialDestructor())
1569 continue;
1570 DestructedFields.push_back(*Field);
1571 }
1572 }
1573 if (!DestructedFields.empty())
1574 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1575 FieldDecl *Field = DestructedFields[i];
1576 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001577 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001578 getContext().getAsConstantArrayType(FieldType);
1579 if (Array)
1580 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001581 const RecordType *RT = FieldType->getAs<RecordType>();
1582 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1583 llvm::Value *LoadOfThis = LoadCXXThis();
1584 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001585 if (Array) {
1586 const llvm::Type *BasePtr = ConvertType(FieldType);
1587 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001588 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001589 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001590 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001591 Array, BaseAddrPtr);
1592 }
1593 else
1594 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1595 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001598 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1599 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1600 Base != ClassDecl->bases_end(); ++Base) {
1601 // FIXME. copy assignment of virtual base NYI
1602 if (Base->isVirtual())
1603 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001605 CXXRecordDecl *BaseClassDecl
1606 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1607 if (BaseClassDecl->hasTrivialDestructor())
1608 continue;
1609 DestructedBases.push_back(BaseClassDecl);
1610 }
1611 if (DestructedBases.empty())
1612 return;
1613 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1614 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001615 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1616 ClassDecl,BaseClassDecl,
1617 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001618 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1619 Dtor_Complete, V);
1620 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001621}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001622
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001623void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1624 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001625 llvm::Function *Fn,
1626 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001628 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001629 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1630 "SynthesizeDefaultDestructor - destructor has user declaration");
1631 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001633 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1634 SourceLocation());
1635 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001636 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001637}
Anders Carlsson6815e942009-09-27 18:58:34 +00001638
1639// FIXME: Move this to CGCXXStmt.cpp
1640void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1641 // FIXME: We need to do more here.
1642 EmitStmt(S.getTryBlock());
1643}