blob: ceb6e4927f0d16a3aba04d27a7096a7825017657 [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 Stump1eb44332009-09-09 15:08:12 +000074
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000075 if (T->isReferenceType()) {
Anders Carlsson622f9dc2009-08-17 18:24:57 +000076 ErrorUnsupported(Init, "global variable that binds to a reference");
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000077 } else if (!hasAggregateLLVMType(T)) {
78 llvm::Value *V = EmitScalarExpr(Init);
79 EmitStoreOfScalar(V, DeclPtr, T.isVolatileQualified(), T);
80 } else if (T->isAnyComplexType()) {
81 EmitComplexExprIntoAddr(Init, DeclPtr, T.isVolatileQualified());
82 } else {
83 EmitAggExpr(Init, DeclPtr, T.isVolatileQualified());
Mike Stump1eb44332009-09-09 15:08:12 +000084
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000085 if (const RecordType *RT = T->getAs<RecordType>()) {
86 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
87 if (!RD->hasTrivialDestructor())
88 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr);
89 }
90 }
91}
92
Anders Carlsson89ed31d2009-08-08 23:24:23 +000093void
94CodeGenModule::EmitCXXGlobalInitFunc() {
95 if (CXXGlobalInits.empty())
96 return;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Owen Anderson0032b272009-08-13 21:57:51 +000098 const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Anders Carlsson89ed31d2009-08-08 23:24:23 +000099 false);
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000101 // Create our global initialization function.
102 // FIXME: Should this be tweakable by targets?
Mike Stump1eb44332009-09-09 15:08:12 +0000103 llvm::Function *Fn =
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000104 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
105 "__cxx_global_initialization", &TheModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000107 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn,
Benjamin Kramer10c40ee2009-08-08 23:43:26 +0000108 &CXXGlobalInits[0],
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000109 CXXGlobalInits.size());
110 AddGlobalCtor(Fn);
111}
112
113void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
114 const VarDecl **Decls,
115 unsigned NumDecls) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000116 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(),
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000117 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000119 for (unsigned i = 0; i != NumDecls; ++i) {
120 const VarDecl *D = Decls[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Anders Carlsson89ed31d2009-08-08 23:24:23 +0000122 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D);
123 EmitCXXGlobalVarDeclInit(*D, DeclPtr);
124 }
125 FinishFunction();
126}
127
Mike Stump1eb44332009-09-09 15:08:12 +0000128void
129CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000130 llvm::GlobalVariable *GV) {
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000131 // FIXME: This should use __cxa_guard_{acquire,release}?
132
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000133 assert(!getContext().getLangOptions().ThreadsafeStatics &&
134 "thread safe statics are currently not supported!");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000135
Anders Carlsson283a0622009-04-13 18:03:33 +0000136 llvm::SmallString<256> GuardVName;
137 llvm::raw_svector_ostream GuardVOut(GuardVName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000138 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000140 // Create the guard variable.
Mike Stump1eb44332009-09-09 15:08:12 +0000141 llvm::GlobalValue *GuardV =
Owen Anderson0032b272009-08-13 21:57:51 +0000142 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false,
Daniel Dunbar0096acf2009-02-25 19:24:29 +0000143 GV->getLinkage(),
Owen Anderson0032b272009-08-13 21:57:51 +0000144 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)),
Daniel Dunbar77659342009-08-19 20:04:03 +0000145 GuardVName.str());
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000147 // Load the first byte of the guard variable.
Owen Anderson0032b272009-08-13 21:57:51 +0000148 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000149 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000150 "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000152 // Compare it against 0.
Owen Anderson0032b272009-08-13 21:57:51 +0000153 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext));
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000154 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool");
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbar55e87422008-11-11 02:29:29 +0000156 llvm::BasicBlock *InitBlock = createBasicBlock("init");
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000157 llvm::BasicBlock *EndBlock = createBasicBlock("init.end");
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000158
159 // If the guard variable is 0, jump to the initializer code.
160 Builder.CreateCondBr(ICmp, InitBlock, EndBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000162 EmitBlock(InitBlock);
163
Anders Carlsson3b2e16b2009-08-08 21:45:14 +0000164 EmitCXXGlobalVarDeclInit(D, GV);
165
Owen Anderson0032b272009-08-13 21:57:51 +0000166 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1),
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000167 Builder.CreateBitCast(GuardV, PtrTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000169 EmitBlock(EndBlock);
Anders Carlssone1b29ef2008-08-22 16:00:37 +0000170}
171
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000172RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD,
173 llvm::Value *Callee,
174 llvm::Value *This,
175 CallExpr::const_arg_iterator ArgBeg,
176 CallExpr::const_arg_iterator ArgEnd) {
Mike Stump1eb44332009-09-09 15:08:12 +0000177 assert(MD->isInstance() &&
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000178 "Trying to emit a member call expr on a static method!");
179
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000180 // A call to a trivial destructor requires no code generation.
181 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD))
182 if (Destructor->isTrivial())
183 return RValue::get(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
John McCall183700f2009-09-21 23:43:11 +0000185 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000187 CallArgList Args;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000189 // Push the this ptr.
190 Args.push_back(std::make_pair(RValue::get(This),
191 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000193 // And the rest of the call args
194 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
John McCall183700f2009-09-21 23:43:11 +0000196 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000197 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
198 Callee, Args, MD);
199}
200
Anders Carlsson774e7c62009-04-03 22:50:24 +0000201RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) {
Anders Carlsson375c31c2009-10-03 19:43:08 +0000202 if (isa<BinaryOperator>(CE->getCallee()))
203 return EmitCXXMemberPointerCallExpr(CE);
204
Anders Carlsson774e7c62009-04-03 22:50:24 +0000205 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
206 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000207
Anders Carlsson2472bf02009-09-29 03:54:11 +0000208 if (MD->isStatic()) {
209 // The method is static, emit it as we would a regular call.
210 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
211 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
212 CE->arg_begin(), CE->arg_end(), 0);
213
214 }
215
John McCall183700f2009-09-21 23:43:11 +0000216 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000217
Mike Stump1eb44332009-09-09 15:08:12 +0000218 const llvm::Type *Ty =
219 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000220 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000221 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Anders Carlsson774e7c62009-04-03 22:50:24 +0000223 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000224 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000225 else {
226 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000227 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000228 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000229
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000230 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000231 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000232 // virtual call mechanism.
Mike Stumpf0070db2009-08-26 20:46:33 +0000233 llvm::Value *Callee;
Douglas Gregor0979c802009-08-31 21:41:48 +0000234 if (MD->isVirtual() && !ME->hasQualifier())
Mike Stump740256b2009-09-29 00:50:50 +0000235 // FIXME: push getCanonicalDecl as a conversion using the static type system (CanCXXMethodDecl).
236 Callee = BuildVirtualCall(MD->getCanonicalDecl(), This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000237 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000238 = dyn_cast<CXXDestructorDecl>(MD))
239 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000240 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000241 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
243 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000244 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000245}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000246
Mike Stump1eb44332009-09-09 15:08:12 +0000247RValue
Anders Carlsson375c31c2009-10-03 19:43:08 +0000248CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) {
249 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee());
250 const DeclRefExpr *BaseExpr = cast<DeclRefExpr>(BO->getLHS());
251 const DeclRefExpr *MemFn = cast<DeclRefExpr>(BO->getRHS());
252
253 const MemberPointerType *MPT = MemFn->getType()->getAs<MemberPointerType>();
254 const FunctionProtoType *FPT =
255 MPT->getPointeeType()->getAs<FunctionProtoType>();
256 const CXXRecordDecl *RD =
257 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl());
258
259 const llvm::FunctionType *FTy =
260 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
261 FPT->isVariadic());
262
263 const llvm::Type *Int8PtrTy =
264 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
265
266 // Get the member function pointer.
267 llvm::Value *MemFnPtr =
268 CreateTempAlloca(ConvertType(MemFn->getType()), "mem.fn");
269 EmitAggExpr(MemFn, MemFnPtr, /*VolatileDest=*/false);
270
271 // Emit the 'this' pointer.
272 llvm::Value *This;
273
274 if (BO->getOpcode() == BinaryOperator::PtrMemI)
275 This = EmitScalarExpr(BaseExpr);
276 else
277 This = EmitLValue(BaseExpr).getAddress();
278
279 // Adjust it.
280 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1);
281 Adj = Builder.CreateLoad(Adj, "mem.fn.adj");
282
283 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr");
284 Ptr = Builder.CreateGEP(Ptr, Adj, "adj");
285
286 This = Builder.CreateBitCast(Ptr, This->getType(), "this");
287
288 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr");
289
290 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
291
292 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn");
293
294 // If the LSB in the function pointer is 1, the function pointer points to
295 // a virtual function.
296 llvm::Value *IsVirtual
297 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1),
298 "and");
299
300 IsVirtual = Builder.CreateTrunc(IsVirtual,
301 llvm::Type::getInt1Ty(VMContext));
302
303 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual");
304 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual");
305 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end");
306
307 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
308 EmitBlock(FnVirtual);
309
310 const llvm::Type *VTableTy =
311 FTy->getPointerTo()->getPointerTo()->getPointerTo();
312
313 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy);
314 VTable = Builder.CreateLoad(VTable);
315
316 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn");
317
318 // Since the function pointer is 1 plus the virtual table offset, we
319 // subtract 1 by using a GEP.
Mike Stump25bc2752009-10-09 01:25:47 +0000320 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1);
Anders Carlsson375c31c2009-10-03 19:43:08 +0000321
322 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn");
323
324 EmitBranch(FnEnd);
325 EmitBlock(FnNonVirtual);
326
327 // If the function is not virtual, just load the pointer.
328 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn");
329 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo());
330
331 EmitBlock(FnEnd);
332
333 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
334 Callee->reserveOperandSpace(2);
335 Callee->addIncoming(VirtualFn, FnVirtual);
336 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
337
338 CallArgList Args;
339
340 QualType ThisType =
341 getContext().getPointerType(getContext().getTagDeclType(RD));
342
343 // Push the this ptr.
344 Args.push_back(std::make_pair(RValue::get(This), ThisType));
345
346 // And the rest of the call args
347 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end());
348 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType();
349 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
350 Callee, Args, 0);
351}
352
353RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000354CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
355 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000356 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000357 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Fariborz Jahanianad258832009-08-13 21:09:41 +0000359 if (MD->isCopyAssignment()) {
360 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
361 if (ClassDecl->hasTrivialCopyAssignment()) {
362 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
363 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
364 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
365 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
366 QualType Ty = E->getType();
367 EmitAggregateCopy(This, Src, Ty);
368 return RValue::get(This);
369 }
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
John McCall183700f2009-09-21 23:43:11 +0000372 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000373 const llvm::Type *Ty =
374 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000375 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000376 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Anders Carlsson0f294632009-05-27 04:18:27 +0000378 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Anders Carlsson0f294632009-05-27 04:18:27 +0000380 return EmitCXXMemberCall(MD, Callee, This,
381 E->arg_begin() + 1, E->arg_end());
382}
383
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000384llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000385 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000386 "Must be in a C++ member function decl to load 'this'");
387 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
388 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000390 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000391 // ans: See how CodeGenFunction::LoadObjCSelf() uses
392 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000393 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
394}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000395
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000396/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
397/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000398/// array.
399/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
400/// array type and 'ArrayPtr' points to the beginning fo the array.
401/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000402void
403CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000404 const ConstantArrayType *ArrayTy,
405 llvm::Value *ArrayPtr) {
406 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
407 llvm::Value * NumElements =
408 llvm::ConstantInt::get(SizeTy,
409 getContext().getConstantArrayElementCount(ArrayTy));
410
411 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
412}
413
414void
415CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
416 llvm::Value *NumElements,
417 llvm::Value *ArrayPtr) {
418 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000420 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000421 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
422 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
423 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000425 // Start the loop with a block that tests the condition.
426 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
427 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000429 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000431 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000433 // Generate: if (loop-index < number-of-elements fall to the loop body,
434 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000435 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000436 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000437 // If the condition is true, execute the body.
438 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000440 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000442 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000443 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000444 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000445 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
446 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000447 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000449 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000451 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000452 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000453 Counter = Builder.CreateLoad(IndexPtr);
454 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
455 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000457 // Finally, branch back up to the condition for the next iteration.
458 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000460 // Emit the fall-through block.
461 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000462}
463
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000464/// EmitCXXAggrDestructorCall - calls the default destructor on array
465/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000466void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000467CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
468 const ArrayType *Array,
469 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000470 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
471 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000472 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000473 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000474 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000475 // Create a temporary for the loop index and initialize it with count of
476 // array elements.
477 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
478 "loop.index");
479 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000480 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000481 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
482 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000484 // Start the loop with a block that tests the condition.
485 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
486 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000488 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000490 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000492 // Generate: if (loop-index != 0 fall to the loop body,
493 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000494 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000495 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
496 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
497 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
498 "isne");
499 // If the condition is true, execute the body.
500 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000502 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000504 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
505 // Inside the loop body, emit the constructor call on the array element.
506 Counter = Builder.CreateLoad(IndexPtr);
507 Counter = Builder.CreateSub(Counter, One);
508 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
509 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000511 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000513 // Emit the decrement of the loop counter.
514 Counter = Builder.CreateLoad(IndexPtr);
515 Counter = Builder.CreateSub(Counter, One, "dec");
516 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000518 // Finally, branch back up to the condition for the next iteration.
519 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000521 // Emit the fall-through block.
522 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000523}
524
525void
Mike Stump1eb44332009-09-09 15:08:12 +0000526CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
527 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000528 llvm::Value *This,
529 CallExpr::const_arg_iterator ArgBeg,
530 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000531 if (D->isCopyConstructor(getContext())) {
532 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
533 if (ClassDecl->hasTrivialCopyConstructor()) {
534 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
535 "EmitCXXConstructorCall - user declared copy constructor");
536 const Expr *E = (*ArgBeg);
537 QualType Ty = E->getType();
538 llvm::Value *Src = EmitLValue(E).getAddress();
539 EmitAggregateCopy(This, Src, Ty);
540 return;
541 }
542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000544 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
545
546 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000547}
548
Mike Stump1eb44332009-09-09 15:08:12 +0000549void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000550 CXXDtorType Type,
551 llvm::Value *This) {
552 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Anders Carlsson7267c162009-05-29 21:03:38 +0000554 EmitCXXMemberCall(D, Callee, This, 0, 0);
555}
556
Mike Stump1eb44332009-09-09 15:08:12 +0000557void
558CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000559 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000560 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000561
562 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000563 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000564 if (RD->hasTrivialConstructor())
565 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000566
Mike Stump1eb44332009-09-09 15:08:12 +0000567 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000568 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000569 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000570 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000571 EmitAggExpr((*i), Dest, false);
572 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000573 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000574 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000575 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000576 E->arg_begin(), E->arg_end());
577}
578
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000579void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000580 EmitGlobal(GlobalDecl(D, Ctor_Complete));
581 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000582}
Anders Carlsson363c1842009-04-16 23:57:24 +0000583
Mike Stump1eb44332009-09-09 15:08:12 +0000584void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000585 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Anders Carlsson27ae5362009-04-17 01:58:57 +0000587 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000589 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Anders Carlsson27ae5362009-04-17 01:58:57 +0000591 SetFunctionDefinitionAttributes(D, Fn);
592 SetLLVMFunctionAttributesForDefinition(D, Fn);
593}
594
Anders Carlsson363c1842009-04-16 23:57:24 +0000595llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000596CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000597 CXXCtorType Type) {
598 const llvm::FunctionType *FTy =
599 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Anders Carlsson363c1842009-04-16 23:57:24 +0000601 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000602 return cast<llvm::Function>(
603 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000604}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000605
Mike Stump1eb44332009-09-09 15:08:12 +0000606const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000607 CXXCtorType Type) {
608 llvm::SmallString<256> Name;
609 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000610 mangleCXXCtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Anders Carlsson27ae5362009-04-17 01:58:57 +0000612 Name += '\0';
613 return UniqueMangledName(Name.begin(), Name.end());
614}
615
616void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000617 EmitCXXDestructor(D, Dtor_Complete);
618 EmitCXXDestructor(D, Dtor_Base);
619}
620
Mike Stump1eb44332009-09-09 15:08:12 +0000621void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000622 CXXDtorType Type) {
623 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000625 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Anders Carlsson27ae5362009-04-17 01:58:57 +0000627 SetFunctionDefinitionAttributes(D, Fn);
628 SetLLVMFunctionAttributesForDefinition(D, Fn);
629}
630
631llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000632CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000633 CXXDtorType Type) {
634 const llvm::FunctionType *FTy =
635 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Anders Carlsson27ae5362009-04-17 01:58:57 +0000637 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000638 return cast<llvm::Function>(
639 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000640}
641
Mike Stump1eb44332009-09-09 15:08:12 +0000642const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000643 CXXDtorType Type) {
644 llvm::SmallString<256> Name;
645 llvm::raw_svector_ostream Out(Name);
Anders Carlssonb5404912009-10-07 01:06:45 +0000646 mangleCXXDtor(getMangleContext(), D, Type, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Anders Carlsson27ae5362009-04-17 01:58:57 +0000648 Name += '\0';
649 return UniqueMangledName(Name.begin(), Name.end());
650}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000651
Mike Stumped032eb2009-09-04 18:27:16 +0000652llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
653 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +0000654 bool Extern, int64_t nv,
655 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +0000656 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +0000657
658 FunctionArgList Args;
659 ImplicitParamDecl *ThisDecl =
660 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
661 MD->getThisType(getContext()));
662 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
663 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
664 e = MD->param_end();
665 i != e; ++i) {
666 ParmVarDecl *D = *i;
667 Args.push_back(std::make_pair(D, D->getType()));
668 }
669 IdentifierInfo *II
670 = &CGM.getContext().Idents.get("__thunk_named_foo_");
671 FunctionDecl *FD = FunctionDecl::Create(getContext(),
672 getContext().getTranslationUnitDecl(),
673 SourceLocation(), II, R, 0,
674 Extern
675 ? FunctionDecl::Extern
676 : FunctionDecl::Static,
677 false, true);
678 StartFunction(FD, R, Fn, Args, SourceLocation());
679 // FIXME: generate body
680 FinishFunction();
681 return Fn;
682}
683
Mike Stump6e319f62009-09-11 23:25:56 +0000684llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
685 const CXXMethodDecl *MD,
686 bool Extern,
687 int64_t nv_t,
688 int64_t v_t,
689 int64_t nv_r,
690 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +0000691 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000692
693 FunctionArgList Args;
694 ImplicitParamDecl *ThisDecl =
695 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
696 MD->getThisType(getContext()));
697 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
698 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
699 e = MD->param_end();
700 i != e; ++i) {
701 ParmVarDecl *D = *i;
702 Args.push_back(std::make_pair(D, D->getType()));
703 }
704 IdentifierInfo *II
705 = &CGM.getContext().Idents.get("__thunk_named_foo_");
706 FunctionDecl *FD = FunctionDecl::Create(getContext(),
707 getContext().getTranslationUnitDecl(),
708 SourceLocation(), II, R, 0,
709 Extern
710 ? FunctionDecl::Extern
711 : FunctionDecl::Static,
712 false, true);
713 StartFunction(FD, R, Fn, Args, SourceLocation());
714 // FIXME: generate body
715 FinishFunction();
716 return Fn;
717}
718
Mike Stump77ca8f62009-09-05 07:20:32 +0000719llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
720 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +0000721 llvm::SmallString<256> OutName;
722 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000723 mangleThunk(getMangleContext(), MD, nv, v, Out);
Mike Stumped032eb2009-09-04 18:27:16 +0000724 llvm::GlobalVariable::LinkageTypes linktype;
725 linktype = llvm::GlobalValue::WeakAnyLinkage;
726 if (!Extern)
727 linktype = llvm::GlobalValue::InternalLinkage;
728 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000729 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +0000730 const llvm::FunctionType *FTy =
731 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
732 FPT->isVariadic());
733
734 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
735 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +0000736 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +0000737 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
738 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
739 return m;
740}
741
Mike Stump6e319f62009-09-11 23:25:56 +0000742llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
743 bool Extern, int64_t nv_t,
744 int64_t v_t, int64_t nv_r,
745 int64_t v_r) {
746 llvm::SmallString<256> OutName;
747 llvm::raw_svector_ostream Out(OutName);
Anders Carlssonb5404912009-10-07 01:06:45 +0000748 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out);
Mike Stump6e319f62009-09-11 23:25:56 +0000749 llvm::GlobalVariable::LinkageTypes linktype;
750 linktype = llvm::GlobalValue::WeakAnyLinkage;
751 if (!Extern)
752 linktype = llvm::GlobalValue::InternalLinkage;
753 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +0000754 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +0000755 const llvm::FunctionType *FTy =
756 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
757 FPT->isVariadic());
758
759 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
760 &getModule());
761 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
762 v_r);
763 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
764 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
765 return m;
766}
767
Mike Stumpf0070db2009-08-26 20:46:33 +0000768llvm::Value *
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000769CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This,
770 const CXXRecordDecl *ClassDecl,
771 const CXXRecordDecl *BaseClassDecl) {
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000772 const llvm::Type *Int8PtrTy =
773 llvm::Type::getInt8Ty(VMContext)->getPointerTo();
774
775 llvm::Value *VTablePtr = Builder.CreateBitCast(This,
776 Int8PtrTy->getPointerTo());
777 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable");
778
Anders Carlssondbd920c2009-10-11 22:13:54 +0000779 int64_t VBaseOffsetIndex =
780 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl);
781
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000782 llvm::Value *VBaseOffsetPtr =
Anders Carlssondbd920c2009-10-11 22:13:54 +0000783 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr");
Anders Carlsson2f1986b2009-10-06 22:43:30 +0000784 const llvm::Type *PtrDiffTy =
785 ConvertType(getContext().getPointerDiffType());
786
787 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr,
788 PtrDiffTy->getPointerTo());
789
790 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset");
791
792 return VBaseOffset;
793}
794
795llvm::Value *
Mike Stumpf0070db2009-08-26 20:46:33 +0000796CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
797 const llvm::Type *Ty) {
798 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Anders Carlssondbd920c2009-10-11 22:13:54 +0000800 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD);
Anders Carlsson2b358352009-10-03 14:56:57 +0000801
Mike Stumpf0070db2009-08-26 20:46:33 +0000802 Ty = llvm::PointerType::get(Ty, 0);
803 Ty = llvm::PointerType::get(Ty, 0);
804 Ty = llvm::PointerType::get(Ty, 0);
805 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
806 vtbl = Builder.CreateLoad(vtbl);
807 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +0000808 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +0000809 vfn = Builder.CreateLoad(vfn);
810 return vfn;
811}
812
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000813/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
814/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
815/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000816// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +0000817void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000818 llvm::Value *Src,
819 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000820 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000821 QualType Ty) {
822 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
823 assert(CA && "VLA cannot be copied over");
824 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000826 // Create a temporary for the loop index and initialize it with 0.
827 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
828 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000829 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000830 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +0000831 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000832 // Start the loop with a block that tests the condition.
833 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
834 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000836 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000838 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
839 // Generate: if (loop-index < number-of-elements fall to the loop body,
840 // otherwise, go to the block after the for-loop.
841 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000842 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000843 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
844 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000845 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000846 "isless");
847 // If the condition is true, execute the body.
848 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000850 EmitBlock(ForBody);
851 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
852 // Inside the loop body, emit the constructor call on the array element.
853 Counter = Builder.CreateLoad(IndexPtr);
854 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
855 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
856 if (BitwiseCopy)
857 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000858 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000859 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000860 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000861 Ctor_Complete);
862 CallArgList CallArgs;
863 // Push the this (Dest) ptr.
864 CallArgs.push_back(std::make_pair(RValue::get(Dest),
865 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000867 // Push the Src ptr.
868 CallArgs.push_back(std::make_pair(RValue::get(Src),
869 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +0000870 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +0000871 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000872 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
873 Callee, CallArgs, BaseCopyCtor);
874 }
875 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000877 // Emit the increment of the loop counter.
878 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
879 Counter = Builder.CreateLoad(IndexPtr);
880 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
881 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000883 // Finally, branch back up to the condition for the next iteration.
884 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000886 // Emit the fall-through block.
887 EmitBlock(AfterFor, true);
888}
889
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000890/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +0000891/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000892/// bitwise assignment or via a copy assignment operator function call.
893/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +0000894void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000895 llvm::Value *Src,
896 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +0000897 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000898 QualType Ty) {
899 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
900 assert(CA && "VLA cannot be asssigned");
901 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000903 // Create a temporary for the loop index and initialize it with 0.
904 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
905 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +0000906 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000907 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
908 Builder.CreateStore(zeroConstant, IndexPtr, false);
909 // Start the loop with a block that tests the condition.
910 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
911 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000913 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000915 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
916 // Generate: if (loop-index < number-of-elements fall to the loop body,
917 // otherwise, go to the block after the for-loop.
918 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +0000919 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000920 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
921 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000922 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000923 "isless");
924 // If the condition is true, execute the body.
925 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000927 EmitBlock(ForBody);
928 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
929 // Inside the loop body, emit the assignment operator call on array element.
930 Counter = Builder.CreateLoad(IndexPtr);
931 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
932 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
933 const CXXMethodDecl *MD = 0;
934 if (BitwiseAssign)
935 EmitAggregateCopy(Dest, Src, Ty);
936 else {
937 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
938 MD);
939 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
940 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +0000941 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000942 const llvm::Type *LTy =
943 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
944 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000945 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000947 CallArgList CallArgs;
948 // Push the this (Dest) ptr.
949 CallArgs.push_back(std::make_pair(RValue::get(Dest),
950 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000952 // Push the Src ptr.
953 CallArgs.push_back(std::make_pair(RValue::get(Src),
954 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +0000955 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000956 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
957 Callee, CallArgs, MD);
958 }
959 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000961 // Emit the increment of the loop counter.
962 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
963 Counter = Builder.CreateLoad(IndexPtr);
964 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
965 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000967 // Finally, branch back up to the condition for the next iteration.
968 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +0000970 // Emit the fall-through block.
971 EmitBlock(AfterFor, true);
972}
973
Fariborz Jahanianca283612009-08-07 23:51:33 +0000974/// EmitClassMemberwiseCopy - This routine generates code to copy a class
975/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +0000976/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +0000977void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +0000978 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +0000979 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +0000980 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
981 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +0000982 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
983 /*NullCheckValue=*/false);
984 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
985 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +0000986 }
987 if (BaseClassDecl->hasTrivialCopyConstructor()) {
988 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +0000989 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
992 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +0000993 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000994 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +0000995 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +0000996 CallArgList CallArgs;
997 // Push the this (Dest) ptr.
998 CallArgs.push_back(std::make_pair(RValue::get(Dest),
999 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Fariborz Jahanianca283612009-08-07 23:51:33 +00001001 // Push the Src ptr.
1002 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001003 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001004 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001005 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001006 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1007 Callee, CallArgs, BaseCopyCtor);
1008 }
1009}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001010
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001011/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001012/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001013/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001014// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001015void CodeGenFunction::EmitClassCopyAssignment(
1016 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001017 const CXXRecordDecl *ClassDecl,
1018 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001019 QualType Ty) {
1020 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001021 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1022 /*NullCheckValue=*/false);
1023 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1024 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001025 }
1026 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1027 EmitAggregateCopy(Dest, Src, Ty);
1028 return;
1029 }
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001031 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001032 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001033 MD);
1034 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1035 (void)ConstCopyAssignOp;
1036
John McCall183700f2009-09-21 23:43:11 +00001037 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001038 const llvm::Type *LTy =
1039 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001040 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001041 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001043 CallArgList CallArgs;
1044 // Push the this (Dest) ptr.
1045 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1046 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001048 // Push the Src ptr.
1049 CallArgs.push_back(std::make_pair(RValue::get(Src),
1050 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001051 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001052 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001053 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1054 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001055}
1056
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001057/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001058void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001059CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1060 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001061 llvm::Function *Fn,
1062 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001063 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1064 SourceLocation());
1065 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001066 FinishFunction();
1067}
1068
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001069/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001070/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001071/// The implicitly-defined copy constructor for class X performs a memberwise
1072/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001073/// of initialization of bases and members in a user-defined constructor
1074/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001075/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001076/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001077/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001078/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001079/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001080/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001081/// Virtual base class subobjects shall be copied only once by the
1082/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001083
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001084void
1085CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1086 CXXCtorType Type,
1087 llvm::Function *Fn,
1088 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001089 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001090 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001091 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001092 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1093 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001095 FunctionArgList::const_iterator i = Args.begin();
1096 const VarDecl *ThisArg = i->first;
1097 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1098 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1099 const VarDecl *SrcArg = (i+1)->first;
1100 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1101 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001103 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1104 Base != ClassDecl->bases_end(); ++Base) {
1105 // FIXME. copy constrution of virtual base NYI
1106 if (Base->isVirtual())
1107 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001109 CXXRecordDecl *BaseClassDecl
1110 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001111 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1112 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001115 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1116 FieldEnd = ClassDecl->field_end();
1117 Field != FieldEnd; ++Field) {
1118 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001119 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001120 getContext().getAsConstantArrayType(FieldType);
1121 if (Array)
1122 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001124 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1125 CXXRecordDecl *FieldClassDecl
1126 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1127 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1128 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001129 if (Array) {
1130 const llvm::Type *BasePtr = ConvertType(FieldType);
1131 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001132 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001133 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001134 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001135 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1136 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1137 FieldClassDecl, FieldType);
1138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139 else
1140 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001141 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001142 continue;
1143 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001144 // Do a built-in assignment of scalar data members.
1145 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1146 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1147 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1148 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001149 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001150 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001151}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001152
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001153/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001154/// Before the implicitly-declared copy assignment operator for a class is
1155/// implicitly defined, all implicitly- declared copy assignment operators for
1156/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001157/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001158/// The implicitly-defined copy assignment operator for class X performs
1159/// memberwise assignment of its subob- jects. The direct base classes of X are
1160/// assigned first, in the order of their declaration in
1161/// the base-specifier-list, and then the immediate nonstatic data members of X
1162/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001163/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001164/// if the subobject is of class type, the copy assignment operator for the
1165/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001166/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001167///
Mike Stump1eb44332009-09-09 15:08:12 +00001168/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001169/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001170///
Mike Stump1eb44332009-09-09 15:08:12 +00001171/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001172/// used.
1173void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001174 llvm::Function *Fn,
1175 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001176
1177 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1178 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1179 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001180 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001182 FunctionArgList::const_iterator i = Args.begin();
1183 const VarDecl *ThisArg = i->first;
1184 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1185 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1186 const VarDecl *SrcArg = (i+1)->first;
1187 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1188 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001190 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1191 Base != ClassDecl->bases_end(); ++Base) {
1192 // FIXME. copy assignment of virtual base NYI
1193 if (Base->isVirtual())
1194 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001196 CXXRecordDecl *BaseClassDecl
1197 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1198 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1199 Base->getType());
1200 }
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001202 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1203 FieldEnd = ClassDecl->field_end();
1204 Field != FieldEnd; ++Field) {
1205 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001206 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001207 getContext().getAsConstantArrayType(FieldType);
1208 if (Array)
1209 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001211 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1212 CXXRecordDecl *FieldClassDecl
1213 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1214 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1215 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001216 if (Array) {
1217 const llvm::Type *BasePtr = ConvertType(FieldType);
1218 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1219 llvm::Value *DestBaseAddrPtr =
1220 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1221 llvm::Value *SrcBaseAddrPtr =
1222 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1223 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1224 FieldClassDecl, FieldType);
1225 }
1226 else
Mike Stump1eb44332009-09-09 15:08:12 +00001227 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001228 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001229 continue;
1230 }
1231 // Do a built-in assignment of scalar data members.
1232 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1233 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1234 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1235 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001236 }
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001238 // return *this;
1239 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001241 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001242}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001243
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001244/// EmitCtorPrologue - This routine generates necessary code to initialize
1245/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001246/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001247void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1248 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001249 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001250 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001251 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001253 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001254 E = CD->init_end();
1255 B != E; ++B) {
1256 CXXBaseOrMemberInitializer *Member = (*B);
1257 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001258 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001259 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001260 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001261 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001262 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1263 BaseClassDecl,
1264 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001265 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001266 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001267 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001268 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001269 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001270 // non-static data member initilaizers.
1271 FieldDecl *Field = Member->getMember();
1272 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001273 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001274 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001275 if (Array)
1276 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Mike Stumpf1216772009-07-31 18:25:34 +00001278 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001279 LValue LHS;
1280 if (FieldType->isReferenceType()) {
1281 // FIXME: This is really ugly; should be refactored somehow
1282 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1283 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001284 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1285 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001286 } else {
1287 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1288 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001289 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001290 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001291 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001292 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001293 if (Array) {
1294 const llvm::Type *BasePtr = ConvertType(FieldType);
1295 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001296 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001297 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001298 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001299 Array, BaseAddrPtr);
1300 }
1301 else
1302 EmitCXXConstructorCall(Member->getConstructor(),
1303 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001304 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001305 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001306 continue;
1307 }
1308 else {
1309 // Initializing an anonymous union data member.
1310 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001311 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001312 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001313 FieldType = anonMember->getType();
1314 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001315 }
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001317 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001318 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001319 RValue RHS;
1320 if (FieldType->isReferenceType())
1321 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1322 /*IsInitializer=*/true);
1323 else
1324 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1325 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001326 }
1327 }
Mike Stumpf1216772009-07-31 18:25:34 +00001328
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001329 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001330 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001331 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001332 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001333 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001334 ClassDecl->bases_begin();
1335 Base != ClassDecl->bases_end(); ++Base) {
1336 // FIXME. copy assignment of virtual base NYI
1337 if (Base->isVirtual())
1338 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001340 CXXRecordDecl *BaseClassDecl
1341 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1342 if (BaseClassDecl->hasTrivialConstructor())
1343 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001344 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001345 BaseClassDecl->getDefaultConstructor(getContext())) {
1346 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001347 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1348 BaseClassDecl,
1349 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001350 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1351 }
1352 }
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001354 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1355 FieldEnd = ClassDecl->field_end();
1356 Field != FieldEnd; ++Field) {
1357 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001358 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001359 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001360 if (Array)
1361 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001362 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1363 continue;
1364 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001365 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001366 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1367 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1368 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001369 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001370 MemberClassDecl->getDefaultConstructor(getContext())) {
1371 LoadOfThis = LoadCXXThis();
1372 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001373 if (Array) {
1374 const llvm::Type *BasePtr = ConvertType(FieldType);
1375 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001376 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001377 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1378 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1379 }
1380 else
Mike Stump1eb44332009-09-09 15:08:12 +00001381 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001382 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001383 }
1384 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001385 }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Mike Stumpf1216772009-07-31 18:25:34 +00001387 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001388 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001389 if (!LoadOfThis)
1390 LoadOfThis = LoadCXXThis();
1391 llvm::Value *VtableField;
1392 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001393 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001394 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1395 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1396 llvm::Value *vtable = GenerateVtable(ClassDecl);
1397 Builder.CreateStore(vtable, VtableField);
1398 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001399}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001400
1401/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001402/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001403/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001404/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001405void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1406 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001407 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001408 assert(!ClassDecl->getNumVBases() &&
1409 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001410 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001412 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1413 *E = DD->destr_end(); B != E; ++B) {
1414 uintptr_t BaseOrMember = (*B);
1415 if (DD->isMemberToDestroy(BaseOrMember)) {
1416 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1417 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001418 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001419 getContext().getAsConstantArrayType(FieldType);
1420 if (Array)
1421 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001422 const RecordType *RT = FieldType->getAs<RecordType>();
1423 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1424 if (FieldClassDecl->hasTrivialDestructor())
1425 continue;
1426 llvm::Value *LoadOfThis = LoadCXXThis();
1427 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001428 if (Array) {
1429 const llvm::Type *BasePtr = ConvertType(FieldType);
1430 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001431 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001432 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001433 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001434 Array, BaseAddrPtr);
1435 }
1436 else
1437 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1438 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001439 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001440 const RecordType *RT =
1441 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1442 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1443 if (BaseClassDecl->hasTrivialDestructor())
1444 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001445 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1446 ClassDecl, BaseClassDecl,
1447 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001448 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001449 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001450 }
1451 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001452 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1453 return;
1454 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001455 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001456 // reverse order of their construction.
1457 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001459 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1460 FieldEnd = ClassDecl->field_end();
1461 Field != FieldEnd; ++Field) {
1462 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001463 if (getContext().getAsConstantArrayType(FieldType))
1464 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001465 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1466 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1467 if (FieldClassDecl->hasTrivialDestructor())
1468 continue;
1469 DestructedFields.push_back(*Field);
1470 }
1471 }
1472 if (!DestructedFields.empty())
1473 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1474 FieldDecl *Field = DestructedFields[i];
1475 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001476 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001477 getContext().getAsConstantArrayType(FieldType);
1478 if (Array)
1479 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001480 const RecordType *RT = FieldType->getAs<RecordType>();
1481 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1482 llvm::Value *LoadOfThis = LoadCXXThis();
1483 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001484 if (Array) {
1485 const llvm::Type *BasePtr = ConvertType(FieldType);
1486 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001487 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001488 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001489 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001490 Array, BaseAddrPtr);
1491 }
1492 else
1493 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1494 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001495 }
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001497 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1498 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1499 Base != ClassDecl->bases_end(); ++Base) {
1500 // FIXME. copy assignment of virtual base NYI
1501 if (Base->isVirtual())
1502 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001504 CXXRecordDecl *BaseClassDecl
1505 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1506 if (BaseClassDecl->hasTrivialDestructor())
1507 continue;
1508 DestructedBases.push_back(BaseClassDecl);
1509 }
1510 if (DestructedBases.empty())
1511 return;
1512 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1513 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001514 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1515 ClassDecl,BaseClassDecl,
1516 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001517 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1518 Dtor_Complete, V);
1519 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001520}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001521
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001522void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1523 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001524 llvm::Function *Fn,
1525 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001527 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001528 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1529 "SynthesizeDefaultDestructor - destructor has user declaration");
1530 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001532 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1533 SourceLocation());
1534 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001535 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001536}
Anders Carlsson6815e942009-09-27 18:58:34 +00001537
1538// FIXME: Move this to CGCXXStmt.cpp
1539void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1540 // FIXME: We need to do more here.
1541 EmitStmt(S.getTryBlock());
1542}