blob: 6dbc53d7641240b865fefb76c100729c0ad5fa98 [file] [log] [blame]
Anders Carlssone1b29ef2008-08-22 16:00:37 +00001//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump1eb44332009-09-09 15:08:12 +000014// We might split this into multiple files if it gets too unwieldy
Anders Carlssone1b29ef2008-08-22 16:00:37 +000015
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
Anders Carlsson283a0622009-04-13 18:03:33 +000018#include "Mangle.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000019#include "clang/AST/ASTContext.h"
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +000020#include "clang/AST/RecordLayout.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000021#include "clang/AST/Decl.h"
Anders Carlsson774e7c62009-04-03 22:50:24 +000022#include "clang/AST/DeclCXX.h"
Anders Carlsson86e96442008-08-23 19:42:54 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson6815e942009-09-27 18:58:34 +000024#include "clang/AST/StmtCXX.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000025#include "llvm/ADT/StringExtras.h"
Anders Carlssone1b29ef2008-08-22 16:00:37 +000026using namespace clang;
27using namespace CodeGen;
28
Mike Stump1eb44332009-09-09 15:08:12 +000029void
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000030CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor,
31 llvm::Constant *DeclPtr) {
Anders Carlsson6815e942009-09-27 18:58:34 +000032 const llvm::Type *Int8PtrTy =
33 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
Mike Stump1eb44332009-09-09 15:08:12 +000034
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000035 std::vector<const llvm::Type *> Params;
36 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000038 // Get the destructor function type
Mike Stump1eb44332009-09-09 15:08:12 +000039 const llvm::Type *DtorFnTy =
Owen Anderson0032b272009-08-13 21:57:51 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000041 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy);
Mike Stump1eb44332009-09-09 15:08:12 +000042
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000043 Params.clear();
44 Params.push_back(DtorFnTy);
45 Params.push_back(Int8PtrTy);
46 Params.push_back(Int8PtrTy);
Mike Stump1eb44332009-09-09 15:08:12 +000047
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000048 // Get the __cxa_atexit function type
49 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
Mike Stump1eb44332009-09-09 15:08:12 +000050 const llvm::FunctionType *AtExitFnTy =
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000051 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000053 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy,
54 "__cxa_atexit");
Mike Stump1eb44332009-09-09 15:08:12 +000055
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000056 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy,
57 "__dso_handle");
Mike Stump1eb44332009-09-09 15:08:12 +000058
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000059 llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete);
Mike Stump1eb44332009-09-09 15:08:12 +000060
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000061 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy),
62 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy),
63 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) };
64 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args));
65}
66
Mike Stump1eb44332009-09-09 15:08:12 +000067void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000068 llvm::Constant *DeclPtr) {
69 assert(D.hasGlobalStorage() &&
70 "VarDecl must have global storage!");
Mike Stump1eb44332009-09-09 15:08:12 +000071
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000072 const Expr *Init = D.getInit();
73 QualType T = D.getType();
Mike Stumpdf317bf2009-11-03 23:25:48 +000074 bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified();
Mike Stump1eb44332009-09-09 15:08:12 +000075
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000076 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000077 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000078 } else if (!hasAggregateLLVMType(T)) {
79 llvm::Value *V = EmitScalarExpr(Init);
Mike Stumpdf317bf2009-11-03 23:25:48 +000080 EmitStoreOfScalar(V, DeclPtr, isVolatile, T);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000081 } else if (T->isAnyComplexType()) {
Mike Stumpdf317bf2009-11-03 23:25:48 +000082 EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile);
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000083 } else {
Mike Stumpdf317bf2009-11-03 23:25:48 +000084 EmitAggExpr(Init, DeclPtr, isVolatile);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000086 if (const RecordType *RT = T->getAs<RecordType>()) {
87 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
88 if (!RD->hasTrivialDestructor())
89 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr);
90 }
91 }
92}
93
Anders Carlsson89ed31d2009-08-08 23:24:23 +000094void
95CodeGenModule::EmitCXXGlobalInitFunc() {
96 if (CXXGlobalInits.empty())
97 return;
Mike Stump1eb44332009-09-09 15:08:12 +000098
Owen Anderson0032b272009-08-13 21:57:51 +000099 const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000100 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000102 // Create our global initialization function.
103 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000104 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000105 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
106 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000108 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000109 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000110 CXXGlobalInits.size());
111 AddGlobalCtor(Fn);
112}
113
114void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
115 const VarDecl **Decls,
116 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000117 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000118 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000120 for (unsigned i = 0; i != NumDecls; ++i) {
121 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000123 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
124 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
125 }
126 FinishFunction();
127}
128
Mike Stump1eb44332009-09-09 15:08:12 +0000129void
130CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000131 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000132 // FIXME: This should use __cxa_guard_{acquire,release}?
133
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000134 assert(!getContext().getLangOptions().ThreadsafeStatics &&
135 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000136
Anders Carlsson283a0622009-04-13 18:03:33 +0000137 llvm::SmallString<256> GuardVName;
138 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000139 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000141 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000142 llvm::GlobalValue *GuardV =
Owen Anderson0032b272009-08-13 21:57:51 +0000143 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000144 GV->getLinkage(),
Owen Anderson0032b272009-08-13 21:57:51 +0000145 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000146 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000148 // Load the first byte of the guard variable.
Owen Anderson0032b272009-08-13 21:57:51 +0000149 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000150 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000151 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000153 // Compare it against 0.
Owen Anderson0032b272009-08-13 21:57:51 +0000154 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000155 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Daniel Dunbar55e87422008-11-11 02:29:29 +0000157 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000158 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000159
160 // If the guard variable is 0, jump to the initializer code.
161 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000163 EmitBlock(InitBlock);
164
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000165 EmitCXXGlobalVarDeclInit(D, GV);
166
Owen Anderson0032b272009-08-13 21:57:51 +0000167 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000168 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000171}
172
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000173RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
174 llvm::Value *Callee,
175 llvm::Value *This,
176 CallExpr::const_arg_iterator ArgBeg,
177 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000178 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000179 "Trying to emit a member call expr on a static method!");
180
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000181 // A call to a trivial destructor requires no code generation.
182 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
183 if (Destructor->isTrivial())
184 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
John McCall183700f2009-09-21 23:43:11 +0000186 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000188 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000190 // Push the this ptr.
191 Args.push_back(std::make_pair(RValue::get(This),
192 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000194 // And the rest of the call args
195 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
John McCall183700f2009-09-21 23:43:11 +0000197 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000198 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
199 Callee, Args, MD);
200}
201
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000202/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
203/// expr can be devirtualized.
204static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) {
205 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
206 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
207 // This is a record decl. We know the type and can devirtualize it.
208 return VD->getType()->isRecordType();
209 }
Anders Carlsson76366482009-10-12 19:45:47 +0000210
211 return false;
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000212 }
213
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000214 // We can always devirtualize calls on temporary object expressions.
Anders Carlsson76366482009-10-12 19:45:47 +0000215 if (isa<CXXTemporaryObjectExpr>(Base))
216 return true;
217
Anders Carlsson4a0d8322009-10-12 19:59:15 +0000218 // And calls on bound temporaries.
219 if (isa<CXXBindTemporaryExpr>(Base))
220 return true;
221
Anders Carlssoncf5deec2009-10-12 19:51:33 +0000222 // Check if this is a call expr that returns a record type.
223 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
224 return CE->getCallReturnType()->isRecordType();
225
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000226 // We can't devirtualize the call.
227 return false;
228}
229
Anders Carlsson774e7c62009-04-03 22:50:24 +0000230RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000231 if (isa<BinaryOperator>(CE->getCallee()))
232 return EmitCXXMemberPointerCallExpr(CE);
233
Anders Carlsson774e7c62009-04-03 22:50:24 +0000234 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
235 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000236
Anders Carlsson2472bf02009-09-29 03:54:11 +0000237 if (MD->isStatic()) {
238 // The method is static, emit it as we would a regular call.
239 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
240 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
241 CE->arg_begin(), CE->arg_end(), 0);
242
243 }
244
John McCall183700f2009-09-21 23:43:11 +0000245 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000246
Mike Stump1eb44332009-09-09 15:08:12 +0000247 const llvm::Type *Ty =
248 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000249 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000250 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Anders Carlsson774e7c62009-04-03 22:50:24 +0000252 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000253 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000254 else {
255 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000256 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000257 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000258
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000259 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000260 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000261 // virtual call mechanism.
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000262 //
263 // We also don't emit a virtual call if the base expression has a record type
264 // because then we know what the type is.
Mike Stumpf0070db2009-08-26 20:46:33 +0000265 llvm::Value *Callee;
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000266 if (MD->isVirtual() && !ME->hasQualifier() &&
Anders Carlsson8e7670d2009-10-12 19:41:04 +0000267 !canDevirtualizeMemberFunctionCalls(ME->getBase()))
Anders Carlsson3b89f3f2009-10-11 23:55:52 +0000268 Callee = BuildVirtualCall(MD, This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000269 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000270 = dyn_cast<CXXDestructorDecl>(MD))
271 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000272 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000273 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
275 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000276 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000277}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000278
Mike Stump1eb44332009-09-09 15:08:12 +0000279RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000280CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
281 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
Anders Carlsson3eea6352009-10-13 17:41:28 +0000282 const Expr *BaseExpr = BO->getLHS();
283 const Expr *MemFnExpr = BO->getRHS();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000284
Anders Carlsson3eea6352009-10-13 17:41:28 +0000285 const MemberPointerType *MPT =
286 MemFnExpr->getType()->getAs<MemberPointerType>();
Anders Carlsson375c31c2009-10-03 19:43:08 +0000287 const FunctionProtoType *FPT =
288 MPT->getPointeeType()->getAs<FunctionProtoType>();
289 const CXXRecordDecl *RD =
290 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl());
291
292 const llvm::FunctionType *FTy =
293 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
294 FPT->isVariadic());
295
296 const llvm::Type *Int8PtrTy =
297 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
298
299 // Get the member function pointer.
300 llvm::Value *MemFnPtr =
Anders Carlsson3eea6352009-10-13 17:41:28 +0000301 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn");
302 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000303
304 // Emit the 'this' pointer.
305 llvm::Value *This;
306
307 if (BO->getOpcode() == BinaryOperator::PtrMemI)
308 This = EmitScalarExpr(BaseExpr);
309 else
310 This = EmitLValue(BaseExpr).getAddress();
311
312 // Adjust it.
313 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
314 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
315
316 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
317 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
318
319 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
320
321 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
322
323 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
324
325 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
326
327 // If the LSB in the function pointer is 1, the function pointer points to
328 // a virtual function.
329 llvm::Value *IsVirtual
330 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
331 "and");
332
333 IsVirtual = Builder.CreateTrunc(IsVirtual,
334 llvm::Type::getInt1Ty(VMContext));
335
336 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
337 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
338 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
339
340 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
341 EmitBlock(FnVirtual);
342
343 const llvm::Type *VTableTy =
344 FTy->getPointerTo()->getPointerTo()->getPointerTo();
345
346 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
347 VTable = Builder.CreateLoad(VTable);
348
349 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
350
351 // Since the function pointer is 1 plus the virtual table offset, we
352 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000353 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000354
355 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
356
357 EmitBranch(FnEnd);
358 EmitBlock(FnNonVirtual);
359
360 // If the function is not virtual, just load the pointer.
361 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
362 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
363
364 EmitBlock(FnEnd);
365
366 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
367 Callee->reserveOperandSpace(2);
368 Callee->addIncoming(VirtualFn, FnVirtual);
369 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
370
371 CallArgList Args;
372
373 QualType ThisType =
374 getContext().getPointerType(getContext().getTagDeclType(RD));
375
376 // Push the this ptr.
377 Args.push_back(std::make_pair(RValue::get(This), ThisType));
378
379 // And the rest of the call args
380 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
381 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
382 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
383 Callee, Args, 0);
384}
385
386RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000387CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
388 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000389 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000390 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Fariborz Jahanianad258832009-08-13 21:09:41 +0000392 if (MD->isCopyAssignment()) {
393 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
394 if (ClassDecl->hasTrivialCopyAssignment()) {
395 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
396 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
397 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
398 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
399 QualType Ty = E->getType();
400 EmitAggregateCopy(This, Src, Ty);
401 return RValue::get(This);
402 }
403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
John McCall183700f2009-09-21 23:43:11 +0000405 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000406 const llvm::Type *Ty =
407 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000408 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000409 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Anders Carlsson0f294632009-05-27 04:18:27 +0000411 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Anders Carlsson0f294632009-05-27 04:18:27 +0000413 return EmitCXXMemberCall(MD, Callee, This,
414 E->arg_begin() + 1, E->arg_end());
415}
416
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000417llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000418 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000419 "Must be in a C++ member function decl to load 'this'");
420 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
421 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000423 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000424 // ans: See how CodeGenFunction::LoadObjCSelf() uses
425 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000426 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
427}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000428
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000429/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
430/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000431/// array.
432/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
433/// array type and 'ArrayPtr' points to the beginning fo the array.
434/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000435void
436CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000437 const ConstantArrayType *ArrayTy,
438 llvm::Value *ArrayPtr) {
439 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
440 llvm::Value * NumElements =
441 llvm::ConstantInt::get(SizeTy,
442 getContext().getConstantArrayElementCount(ArrayTy));
443
444 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
445}
446
447void
448CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
449 llvm::Value *NumElements,
450 llvm::Value *ArrayPtr) {
451 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000453 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000454 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
455 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
456 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000458 // Start the loop with a block that tests the condition.
459 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
460 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000462 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000464 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000466 // Generate: if (loop-index < number-of-elements fall to the loop body,
467 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000468 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000469 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000470 // If the condition is true, execute the body.
471 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000473 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000475 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000476 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000477 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000478 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
479 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000480 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000482 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000484 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000485 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000486 Counter = Builder.CreateLoad(IndexPtr);
487 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
488 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000490 // Finally, branch back up to the condition for the next iteration.
491 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000493 // Emit the fall-through block.
494 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000495}
496
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000497/// EmitCXXAggrDestructorCall - calls the default destructor on array
498/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000499void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000500CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
501 const ArrayType *Array,
502 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000503 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
504 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000505 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000506 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000507 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000508 // Create a temporary for the loop index and initialize it with count of
509 // array elements.
510 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
511 "loop.index");
512 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000513 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000514 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
515 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000517 // Start the loop with a block that tests the condition.
518 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
519 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000521 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000523 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000525 // Generate: if (loop-index != 0 fall to the loop body,
526 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000527 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000528 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
529 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
530 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
531 "isne");
532 // If the condition is true, execute the body.
533 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000535 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000537 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
538 // Inside the loop body, emit the constructor call on the array element.
539 Counter = Builder.CreateLoad(IndexPtr);
540 Counter = Builder.CreateSub(Counter, One);
541 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
542 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000544 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000546 // Emit the decrement of the loop counter.
547 Counter = Builder.CreateLoad(IndexPtr);
548 Counter = Builder.CreateSub(Counter, One, "dec");
549 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000551 // Finally, branch back up to the condition for the next iteration.
552 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000554 // Emit the fall-through block.
555 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000556}
557
558void
Mike Stump1eb44332009-09-09 15:08:12 +0000559CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
560 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000561 llvm::Value *This,
562 CallExpr::const_arg_iterator ArgBeg,
563 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000564 if (D->isCopyConstructor(getContext())) {
565 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
566 if (ClassDecl->hasTrivialCopyConstructor()) {
567 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
568 "EmitCXXConstructorCall - user declared copy constructor");
569 const Expr *E = (*ArgBeg);
570 QualType Ty = E->getType();
571 llvm::Value *Src = EmitLValue(E).getAddress();
572 EmitAggregateCopy(This, Src, Ty);
573 return;
574 }
575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000577 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
578
579 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000580}
581
Mike Stump1eb44332009-09-09 15:08:12 +0000582void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000583 CXXDtorType Type,
584 llvm::Value *This) {
585 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Anders Carlsson7267c162009-05-29 21:03:38 +0000587 EmitCXXMemberCall(D, Callee, This, 0, 0);
588}
589
Mike Stump1eb44332009-09-09 15:08:12 +0000590void
591CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000592 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000593 assert(Dest && "Must have a destination!");
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000594 const CXXConstructorDecl *CD = E->getConstructor();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000595 const ConstantArrayType *Array =
596 getContext().getAsConstantArrayType(E->getType());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000597 // For a copy constructor, even if it is trivial, must fall thru so
598 // its argument is code-gen'ed.
599 if (!CD->isCopyConstructor(getContext())) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000600 QualType InitType = E->getType();
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000601 if (Array)
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000602 InitType = getContext().getBaseElementType(Array);
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000603 const CXXRecordDecl *RD =
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000604 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000605 if (RD->hasTrivialConstructor())
Anders Carlssonb14095a2009-04-17 00:06:03 +0000606 return;
Fariborz Jahanian93034ca2009-10-16 19:20:59 +0000607 }
Mike Stump1eb44332009-09-09 15:08:12 +0000608 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000609 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000610 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000611 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000612 EmitAggExpr((*i), Dest, false);
613 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000614 }
Fariborz Jahaniand7a4a432009-10-28 21:07:28 +0000615 if (Array) {
Fariborz Jahanianae013b92009-10-28 20:55:41 +0000616 QualType BaseElementTy = getContext().getBaseElementType(Array);
617 const llvm::Type *BasePtr = ConvertType(BaseElementTy);
618 BasePtr = llvm::PointerType::getUnqual(BasePtr);
619 llvm::Value *BaseAddrPtr =
620 Builder.CreateBitCast(Dest, BasePtr);
621 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr);
622 }
623 else
624 // Call the constructor.
625 EmitCXXConstructorCall(CD, Ctor_Complete, Dest,
626 E->arg_begin(), E->arg_end());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000627}
628
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000629void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000630 EmitGlobal(GlobalDecl(D, Ctor_Complete));
631 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000632}
Anders Carlsson363c1842009-04-16 23:57:24 +0000633
Mike Stump1eb44332009-09-09 15:08:12 +0000634void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000635 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlsson27ae5362009-04-17 01:58:57 +0000637 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000639 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Anders Carlsson27ae5362009-04-17 01:58:57 +0000641 SetFunctionDefinitionAttributes(D, Fn);
642 SetLLVMFunctionAttributesForDefinition(D, Fn);
643}
644
Anders Carlsson363c1842009-04-16 23:57:24 +0000645llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000646CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000647 CXXCtorType Type) {
648 const llvm::FunctionType *FTy =
649 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Anders Carlsson363c1842009-04-16 23:57:24 +0000651 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000652 return cast<llvm::Function>(
653 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000654}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000655
Mike Stump1eb44332009-09-09 15:08:12 +0000656const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000657 CXXCtorType Type) {
658 llvm::SmallString<256> Name;
659 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000660 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Anders Carlsson27ae5362009-04-17 01:58:57 +0000662 Name += '\0';
663 return UniqueMangledName(Name.begin(), Name.end());
664}
665
666void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000667 EmitCXXDestructor(D, Dtor_Complete);
668 EmitCXXDestructor(D, Dtor_Base);
669}
670
Mike Stump1eb44332009-09-09 15:08:12 +0000671void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000672 CXXDtorType Type) {
673 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000675 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Anders Carlsson27ae5362009-04-17 01:58:57 +0000677 SetFunctionDefinitionAttributes(D, Fn);
678 SetLLVMFunctionAttributesForDefinition(D, Fn);
679}
680
681llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000682CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000683 CXXDtorType Type) {
684 const llvm::FunctionType *FTy =
685 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Anders Carlsson27ae5362009-04-17 01:58:57 +0000687 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000688 return cast<llvm::Function>(
689 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000690}
691
Mike Stump1eb44332009-09-09 15:08:12 +0000692const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000693 CXXDtorType Type) {
694 llvm::SmallString<256> Name;
695 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000696 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Anders Carlsson27ae5362009-04-17 01:58:57 +0000698 Name += '\0';
699 return UniqueMangledName(Name.begin(), Name.end());
700}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000701
Mike Stumped032eb2009-09-04 18:27:16 +0000702llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
703 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000704 bool Extern, int64_t nv,
705 int64_t v) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000706 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0);
Mike Stumped032eb2009-09-04 18:27:16 +0000707}
708
Mike Stumpc902d222009-11-03 16:59:27 +0000709llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv,
710 int64_t v) {
711 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),
712 0);
713 const llvm::Type *OrigTy = V->getType();
714 if (nv) {
715 // Do the non-virtual adjustment
716 V = Builder.CreateBitCast(V, Ptr8Ty);
717 V = Builder.CreateConstInBoundsGEP1_64(V, nv);
718 V = Builder.CreateBitCast(V, OrigTy);
719 }
720 if (v) {
721 // Do the virtual this adjustment
722 const llvm::Type *PtrDiffTy =
723 ConvertType(getContext().getPointerDiffType());
724 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy;
725 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
726 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0);
727 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty);
728 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo());
729 V = Builder.CreateLoad(V, "vtable");
730 llvm::Value *VTablePtr = V;
731 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned");
732 v /= LLVMPointerWidth/8;
733 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v);
734 V = Builder.CreateLoad(V);
735 V = Builder.CreateGEP(ThisVal, V);
736 V = Builder.CreateBitCast(V, OrigTy);
737 }
738 return V;
739}
740
Mike Stump6e319f62009-09-11 23:25:56 +0000741llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
742 const CXXMethodDecl *MD,
743 bool Extern,
744 int64_t nv_t,
745 int64_t v_t,
746 int64_t nv_r,
747 int64_t v_r) {
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000748 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000749
750 FunctionArgList Args;
751 ImplicitParamDecl *ThisDecl =
752 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
753 MD->getThisType(getContext()));
754 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
755 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
756 e = MD->param_end();
757 i != e; ++i) {
758 ParmVarDecl *D = *i;
759 Args.push_back(std::make_pair(D, D->getType()));
760 }
761 IdentifierInfo *II
762 = &CGM.getContext().Idents.get("__thunk_named_foo_");
763 FunctionDecl *FD = FunctionDecl::Create(getContext(),
764 getContext().getTranslationUnitDecl(),
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000765 SourceLocation(), II, ResultType, 0,
Mike Stump6e319f62009-09-11 23:25:56 +0000766 Extern
767 ? FunctionDecl::Extern
768 : FunctionDecl::Static,
769 false, true);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000770 StartFunction(FD, ResultType, Fn, Args, SourceLocation());
771
772 // generate body
Mike Stump736529e2009-11-03 02:12:59 +0000773 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
774 const llvm::Type *Ty =
775 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
776 FPT->isVariadic());
777 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000778 CallArgList CallArgs;
779
Mike Stump736529e2009-11-03 02:12:59 +0000780 QualType ArgType = MD->getThisType(getContext());
781 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this");
Mike Stumpd0fe5362009-11-04 00:53:51 +0000782 if (nv_t || v_t) {
Mike Stumpc902d222009-11-03 16:59:27 +0000783 // Do the this adjustment.
Mike Stumpd0fe5362009-11-04 00:53:51 +0000784 const llvm::Type *OrigTy = Callee->getType();
Mike Stumpc902d222009-11-03 16:59:27 +0000785 Arg = DynamicTypeAdjust(Arg, nv_t, v_t);
Mike Stumpd0fe5362009-11-04 00:53:51 +0000786 if (nv_r || v_r) {
787 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r);
788 Callee = Builder.CreateBitCast(Callee, OrigTy);
789 nv_r = v_r = 0;
790 }
791 }
792
Mike Stump736529e2009-11-03 02:12:59 +0000793 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType));
794
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000795 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
796 e = MD->param_end();
797 i != e; ++i) {
798 ParmVarDecl *D = *i;
799 QualType ArgType = D->getType();
800
801 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst);
802 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType, SourceLocation());
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000803 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType));
804 }
805
Mike Stumpf49ed942009-11-02 23:47:45 +0000806 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
807 Callee, CallArgs, MD);
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000808 if (nv_r || v_r) {
Mike Stumpc902d222009-11-03 16:59:27 +0000809 // Do the return result adjustment.
810 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r));
Mike Stumpc5dac4e2009-11-02 23:22:01 +0000811 }
812
Mike Stumpf49ed942009-11-02 23:47:45 +0000813 if (!ResultType->isVoidType())
814 EmitReturnOfRValue(RV, ResultType);
815
Mike Stump6e319f62009-09-11 23:25:56 +0000816 FinishFunction();
817 return Fn;
818}
819
Mike Stump77ca8f62009-09-05 07:20:32 +0000820llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
821 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000822 llvm::SmallString<256> OutName;
823 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000824 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000825 llvm::GlobalVariable::LinkageTypes linktype;
826 linktype = llvm::GlobalValue::WeakAnyLinkage;
827 if (!Extern)
828 linktype = llvm::GlobalValue::InternalLinkage;
829 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000830 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000831 const llvm::FunctionType *FTy =
832 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
833 FPT->isVariadic());
834
835 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
836 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000837 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000838 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
839 return m;
840}
841
Mike Stump6e319f62009-09-11 23:25:56 +0000842llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
843 bool Extern, int64_t nv_t,
844 int64_t v_t, int64_t nv_r,
845 int64_t v_r) {
846 llvm::SmallString<256> OutName;
847 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000848 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000849 llvm::GlobalVariable::LinkageTypes linktype;
850 linktype = llvm::GlobalValue::WeakAnyLinkage;
851 if (!Extern)
852 linktype = llvm::GlobalValue::InternalLinkage;
853 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000854 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000855 const llvm::FunctionType *FTy =
856 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
857 FPT->isVariadic());
858
859 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
860 &getModule());
861 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
862 v_r);
Mike Stump6e319f62009-09-11 23:25:56 +0000863 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
864 return m;
865}
866
Mike Stumpf0070db2009-08-26 20:46:33 +0000867llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000868CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
869 const CXXRecordDecl *ClassDecl,
870 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000871 const llvm::Type *Int8PtrTy =
872 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
873
874 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
875 Int8PtrTy->getPointerTo());
876 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
877
Anders Carlssondbd920c2009-10-11 22:13:54 +0000878 int64_t VBaseOffsetIndex =
879 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
880
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000881 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000882 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000883 const llvm::Type *PtrDiffTy =
884 ConvertType(getContext().getPointerDiffType());
885
886 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
887 PtrDiffTy->getPointerTo());
888
889 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
890
891 return VBaseOffset;
892}
893
894llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000895CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
896 const llvm::Type *Ty) {
Anders Carlssondbd920c2009-10-11 22:13:54 +0000897 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000898
Mike Stumpf0070db2009-08-26 20:46:33 +0000899 Ty = llvm::PointerType::get(Ty, 0);
900 Ty = llvm::PointerType::get(Ty, 0);
901 Ty = llvm::PointerType::get(Ty, 0);
902 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
903 vtbl = Builder.CreateLoad(vtbl);
904 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000905 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000906 vfn = Builder.CreateLoad(vfn);
907 return vfn;
908}
909
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000910/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
911/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
912/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000913// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000914void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000915 llvm::Value *Src,
916 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000917 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000918 QualType Ty) {
919 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
920 assert(CA && "VLA cannot be copied over");
921 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000923 // Create a temporary for the loop index and initialize it with 0.
924 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
925 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000926 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000927 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000928 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000929 // Start the loop with a block that tests the condition.
930 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
931 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000933 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000935 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
936 // Generate: if (loop-index < number-of-elements fall to the loop body,
937 // otherwise, go to the block after the for-loop.
938 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000939 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000940 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
941 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000942 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000943 "isless");
944 // If the condition is true, execute the body.
945 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000947 EmitBlock(ForBody);
948 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
949 // Inside the loop body, emit the constructor call on the array element.
950 Counter = Builder.CreateLoad(IndexPtr);
951 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
952 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
953 if (BitwiseCopy)
954 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000955 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000956 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000957 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000958 Ctor_Complete);
959 CallArgList CallArgs;
960 // Push the this (Dest) ptr.
961 CallArgs.push_back(std::make_pair(RValue::get(Dest),
962 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000964 // Push the Src ptr.
965 CallArgs.push_back(std::make_pair(RValue::get(Src),
966 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000967 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000968 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000969 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
970 Callee, CallArgs, BaseCopyCtor);
971 }
972 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000974 // Emit the increment of the loop counter.
975 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
976 Counter = Builder.CreateLoad(IndexPtr);
977 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
978 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000980 // Finally, branch back up to the condition for the next iteration.
981 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000983 // Emit the fall-through block.
984 EmitBlock(AfterFor, true);
985}
986
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000987/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000988/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000989/// bitwise assignment or via a copy assignment operator function call.
990/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000991void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000992 llvm::Value *Src,
993 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000994 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000995 QualType Ty) {
996 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
997 assert(CA && "VLA cannot be asssigned");
998 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001000 // Create a temporary for the loop index and initialize it with 0.
1001 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1002 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001003 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001004 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1005 Builder.CreateStore(zeroConstant, IndexPtr, false);
1006 // Start the loop with a block that tests the condition.
1007 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1008 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001010 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001012 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1013 // Generate: if (loop-index < number-of-elements fall to the loop body,
1014 // otherwise, go to the block after the for-loop.
1015 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001016 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001017 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1018 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001019 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001020 "isless");
1021 // If the condition is true, execute the body.
1022 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001024 EmitBlock(ForBody);
1025 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1026 // Inside the loop body, emit the assignment operator call on array element.
1027 Counter = Builder.CreateLoad(IndexPtr);
1028 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1029 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1030 const CXXMethodDecl *MD = 0;
1031 if (BitwiseAssign)
1032 EmitAggregateCopy(Dest, Src, Ty);
1033 else {
1034 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1035 MD);
1036 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1037 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001038 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001039 const llvm::Type *LTy =
1040 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1041 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001042 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001044 CallArgList CallArgs;
1045 // Push the this (Dest) ptr.
1046 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1047 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001049 // Push the Src ptr.
1050 CallArgs.push_back(std::make_pair(RValue::get(Src),
1051 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001052 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001053 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1054 Callee, CallArgs, MD);
1055 }
1056 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001058 // Emit the increment of the loop counter.
1059 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1060 Counter = Builder.CreateLoad(IndexPtr);
1061 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1062 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001064 // Finally, branch back up to the condition for the next iteration.
1065 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001067 // Emit the fall-through block.
1068 EmitBlock(AfterFor, true);
1069}
1070
Fariborz Jahanianca283612009-08-07 23:51:33 +00001071/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1072/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001073/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001074void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001075 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001076 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001077 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1078 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001079 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1080 /*NullCheckValue=*/false);
1081 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1082 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001083 }
1084 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1085 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001086 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
1089 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001090 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001091 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001092 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001093 CallArgList CallArgs;
1094 // Push the this (Dest) ptr.
1095 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1096 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Fariborz Jahanianca283612009-08-07 23:51:33 +00001098 // Push the Src ptr.
1099 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001100 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001101 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001102 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001103 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1104 Callee, CallArgs, BaseCopyCtor);
1105 }
1106}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001107
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001108/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001109/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001110/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001111// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001112void CodeGenFunction::EmitClassCopyAssignment(
1113 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001114 const CXXRecordDecl *ClassDecl,
1115 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001116 QualType Ty) {
1117 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001118 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1119 /*NullCheckValue=*/false);
1120 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1121 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001122 }
1123 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1124 EmitAggregateCopy(Dest, Src, Ty);
1125 return;
1126 }
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001128 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001129 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001130 MD);
1131 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1132 (void)ConstCopyAssignOp;
1133
John McCall183700f2009-09-21 23:43:11 +00001134 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001135 const llvm::Type *LTy =
1136 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001137 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001138 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001140 CallArgList CallArgs;
1141 // Push the this (Dest) ptr.
1142 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1143 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001145 // Push the Src ptr.
1146 CallArgs.push_back(std::make_pair(RValue::get(Src),
1147 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001148 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001149 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001150 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1151 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001152}
1153
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001154/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001155void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001156CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1157 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001158 llvm::Function *Fn,
1159 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001160 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1161 SourceLocation());
1162 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001163 FinishFunction();
1164}
1165
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001166/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001167/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001168/// The implicitly-defined copy constructor for class X performs a memberwise
1169/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001170/// of initialization of bases and members in a user-defined constructor
1171/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001172/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001173/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001174/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001175/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001176/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001177/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001178/// Virtual base class subobjects shall be copied only once by the
1179/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001180
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001181void
1182CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1183 CXXCtorType Type,
1184 llvm::Function *Fn,
1185 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001186 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001187 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001188 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001189 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1190 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001192 FunctionArgList::const_iterator i = Args.begin();
1193 const VarDecl *ThisArg = i->first;
1194 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1195 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1196 const VarDecl *SrcArg = (i+1)->first;
1197 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1198 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001200 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1201 Base != ClassDecl->bases_end(); ++Base) {
1202 // FIXME. copy constrution of virtual base NYI
1203 if (Base->isVirtual())
1204 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001206 CXXRecordDecl *BaseClassDecl
1207 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001208 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1209 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001212 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1213 FieldEnd = ClassDecl->field_end();
1214 Field != FieldEnd; ++Field) {
1215 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001216 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001217 getContext().getAsConstantArrayType(FieldType);
1218 if (Array)
1219 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001221 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1222 CXXRecordDecl *FieldClassDecl
1223 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1224 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1225 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001226 if (Array) {
1227 const llvm::Type *BasePtr = ConvertType(FieldType);
1228 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001229 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001230 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001231 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001232 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1233 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1234 FieldClassDecl, FieldType);
1235 }
Mike Stump1eb44332009-09-09 15:08:12 +00001236 else
1237 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001238 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001239 continue;
1240 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001241 // Do a built-in assignment of scalar data members.
1242 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1243 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1244 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1245 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001246 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001247 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001248}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001249
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001250/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001251/// Before the implicitly-declared copy assignment operator for a class is
1252/// implicitly defined, all implicitly- declared copy assignment operators for
1253/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001254/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001255/// The implicitly-defined copy assignment operator for class X performs
1256/// memberwise assignment of its subob- jects. The direct base classes of X are
1257/// assigned first, in the order of their declaration in
1258/// the base-specifier-list, and then the immediate nonstatic data members of X
1259/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001260/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001261/// if the subobject is of class type, the copy assignment operator for the
1262/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001263/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001264///
Mike Stump1eb44332009-09-09 15:08:12 +00001265/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001266/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001267///
Mike Stump1eb44332009-09-09 15:08:12 +00001268/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001269/// used.
1270void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001271 llvm::Function *Fn,
1272 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001273
1274 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1275 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1276 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001277 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001279 FunctionArgList::const_iterator i = Args.begin();
1280 const VarDecl *ThisArg = i->first;
1281 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1282 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1283 const VarDecl *SrcArg = (i+1)->first;
1284 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1285 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001287 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1288 Base != ClassDecl->bases_end(); ++Base) {
1289 // FIXME. copy assignment of virtual base NYI
1290 if (Base->isVirtual())
1291 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001293 CXXRecordDecl *BaseClassDecl
1294 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1295 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1296 Base->getType());
1297 }
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001299 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1300 FieldEnd = ClassDecl->field_end();
1301 Field != FieldEnd; ++Field) {
1302 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001303 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001304 getContext().getAsConstantArrayType(FieldType);
1305 if (Array)
1306 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001308 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1309 CXXRecordDecl *FieldClassDecl
1310 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1311 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1312 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001313 if (Array) {
1314 const llvm::Type *BasePtr = ConvertType(FieldType);
1315 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1316 llvm::Value *DestBaseAddrPtr =
1317 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1318 llvm::Value *SrcBaseAddrPtr =
1319 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1320 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1321 FieldClassDecl, FieldType);
1322 }
1323 else
Mike Stump1eb44332009-09-09 15:08:12 +00001324 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001325 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001326 continue;
1327 }
1328 // Do a built-in assignment of scalar data members.
1329 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1330 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1331 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1332 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001333 }
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001335 // return *this;
1336 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001338 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001339}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001340
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001341/// EmitCtorPrologue - This routine generates necessary code to initialize
1342/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001343/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001344void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1345 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001346 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001347 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001348 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001350 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001351 E = CD->init_end();
1352 B != E; ++B) {
1353 CXXBaseOrMemberInitializer *Member = (*B);
1354 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001355 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001356 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001357 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001358 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001359 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1360 BaseClassDecl,
1361 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001362 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001363 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001364 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001365 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001366 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001367 // non-static data member initilaizers.
1368 FieldDecl *Field = Member->getMember();
1369 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001370 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001371 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001372 if (Array)
1373 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Mike Stumpf1216772009-07-31 18:25:34 +00001375 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001376 LValue LHS;
1377 if (FieldType->isReferenceType()) {
1378 // FIXME: This is really ugly; should be refactored somehow
1379 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1380 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001381 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1382 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001383 } else {
1384 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1385 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001386 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001387 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001388 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001389 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001390 if (Array) {
1391 const llvm::Type *BasePtr = ConvertType(FieldType);
1392 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001393 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001394 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001395 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001396 Array, BaseAddrPtr);
1397 }
1398 else
1399 EmitCXXConstructorCall(Member->getConstructor(),
1400 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001401 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001402 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001403 continue;
1404 }
1405 else {
1406 // Initializing an anonymous union data member.
1407 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001408 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001409 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001410 FieldType = anonMember->getType();
1411 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001414 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001415 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001416 RValue RHS;
1417 if (FieldType->isReferenceType())
1418 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1419 /*IsInitializer=*/true);
1420 else
1421 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1422 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001423 }
1424 }
Mike Stumpf1216772009-07-31 18:25:34 +00001425
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001426 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001427 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001428 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001429 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001430 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001431 ClassDecl->bases_begin();
1432 Base != ClassDecl->bases_end(); ++Base) {
1433 // FIXME. copy assignment of virtual base NYI
1434 if (Base->isVirtual())
1435 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001437 CXXRecordDecl *BaseClassDecl
1438 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1439 if (BaseClassDecl->hasTrivialConstructor())
1440 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001441 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001442 BaseClassDecl->getDefaultConstructor(getContext())) {
1443 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001444 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1445 BaseClassDecl,
1446 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001447 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1448 }
1449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001451 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1452 FieldEnd = ClassDecl->field_end();
1453 Field != FieldEnd; ++Field) {
1454 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001455 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001456 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001457 if (Array)
1458 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001459 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1460 continue;
1461 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001462 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001463 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1464 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1465 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001466 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001467 MemberClassDecl->getDefaultConstructor(getContext())) {
1468 LoadOfThis = LoadCXXThis();
1469 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001470 if (Array) {
1471 const llvm::Type *BasePtr = ConvertType(FieldType);
1472 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001473 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001474 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1475 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1476 }
1477 else
Mike Stump1eb44332009-09-09 15:08:12 +00001478 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001479 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001480 }
1481 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001482 }
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Mike Stumpf1216772009-07-31 18:25:34 +00001484 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001485 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001486 if (!LoadOfThis)
1487 LoadOfThis = LoadCXXThis();
1488 llvm::Value *VtableField;
1489 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001490 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001491 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1492 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1493 llvm::Value *vtable = GenerateVtable(ClassDecl);
1494 Builder.CreateStore(vtable, VtableField);
1495 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001496}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001497
1498/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001499/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001500/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001501/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001502void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1503 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001504 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001505 assert(!ClassDecl->getNumVBases() &&
1506 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001507 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001509 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1510 *E = DD->destr_end(); B != E; ++B) {
1511 uintptr_t BaseOrMember = (*B);
1512 if (DD->isMemberToDestroy(BaseOrMember)) {
1513 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1514 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001515 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001516 getContext().getAsConstantArrayType(FieldType);
1517 if (Array)
1518 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001519 const RecordType *RT = FieldType->getAs<RecordType>();
1520 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1521 if (FieldClassDecl->hasTrivialDestructor())
1522 continue;
1523 llvm::Value *LoadOfThis = LoadCXXThis();
1524 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001525 if (Array) {
1526 const llvm::Type *BasePtr = ConvertType(FieldType);
1527 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001528 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001529 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001530 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001531 Array, BaseAddrPtr);
1532 }
1533 else
1534 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1535 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001536 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001537 const RecordType *RT =
1538 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1539 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1540 if (BaseClassDecl->hasTrivialDestructor())
1541 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001542 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1543 ClassDecl, BaseClassDecl,
1544 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001545 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001546 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001547 }
1548 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001549 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1550 return;
1551 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001552 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001553 // reverse order of their construction.
1554 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001556 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1557 FieldEnd = ClassDecl->field_end();
1558 Field != FieldEnd; ++Field) {
1559 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001560 if (getContext().getAsConstantArrayType(FieldType))
1561 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001562 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1563 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1564 if (FieldClassDecl->hasTrivialDestructor())
1565 continue;
1566 DestructedFields.push_back(*Field);
1567 }
1568 }
1569 if (!DestructedFields.empty())
1570 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1571 FieldDecl *Field = DestructedFields[i];
1572 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001573 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001574 getContext().getAsConstantArrayType(FieldType);
1575 if (Array)
1576 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001577 const RecordType *RT = FieldType->getAs<RecordType>();
1578 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1579 llvm::Value *LoadOfThis = LoadCXXThis();
1580 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001581 if (Array) {
1582 const llvm::Type *BasePtr = ConvertType(FieldType);
1583 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001584 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001585 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001586 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001587 Array, BaseAddrPtr);
1588 }
1589 else
1590 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1591 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001594 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1595 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1596 Base != ClassDecl->bases_end(); ++Base) {
1597 // FIXME. copy assignment of virtual base NYI
1598 if (Base->isVirtual())
1599 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001601 CXXRecordDecl *BaseClassDecl
1602 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1603 if (BaseClassDecl->hasTrivialDestructor())
1604 continue;
1605 DestructedBases.push_back(BaseClassDecl);
1606 }
1607 if (DestructedBases.empty())
1608 return;
1609 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1610 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001611 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1612 ClassDecl,BaseClassDecl,
1613 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001614 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1615 Dtor_Complete, V);
1616 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001617}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001618
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001619void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1620 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001621 llvm::Function *Fn,
1622 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001624 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001625 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1626 "SynthesizeDefaultDestructor - destructor has user declaration");
1627 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001629 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1630 SourceLocation());
1631 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001632 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001633}
Anders Carlsson6815e942009-09-27 18:58:34 +00001634
1635// FIXME: Move this to CGCXXStmt.cpp
1636void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1637 // FIXME: We need to do more here.
1638 EmitStmt(S.getTryBlock());
1639}