blob: a119c5af932e0c19ab064523b681774ea1af4ccc [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);
138 mangleGuardVariable(&D, getContext(), 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.
320 VTable = Builder.CreateConstGEP1_64(VTable, -1);
321
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);
610 mangleCXXCtor(D, Type, Context, 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);
646 mangleCXXDtor(D, Type, Context, 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 Stump32f37012009-08-18 21:49:00 +0000652llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) {
Mike Stump738f8c22009-07-31 23:15:31 +0000653 llvm::Type *Ptr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +0000654 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000655 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
Mike Stump738f8c22009-07-31 23:15:31 +0000656
657 if (!getContext().getLangOptions().Rtti)
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000658 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000659
660 llvm::SmallString<256> OutName;
661 llvm::raw_svector_ostream Out(OutName);
662 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +0000663 ClassTy = getContext().getTagDeclType(RD);
Mike Stump738f8c22009-07-31 23:15:31 +0000664 mangleCXXRtti(ClassTy, getContext(), Out);
Mike Stump738f8c22009-07-31 23:15:31 +0000665 llvm::GlobalVariable::LinkageTypes linktype;
666 linktype = llvm::GlobalValue::WeakAnyLinkage;
667 std::vector<llvm::Constant *> info;
Mike Stump4ef98092009-08-13 22:53:07 +0000668 // assert(0 && "FIXME: implement rtti descriptor");
Mike Stump738f8c22009-07-31 23:15:31 +0000669 // FIXME: descriptor
670 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
Mike Stump4ef98092009-08-13 22:53:07 +0000671 // assert(0 && "FIXME: implement rtti ts");
Mike Stump738f8c22009-07-31 23:15:31 +0000672 // FIXME: TS
673 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
674
675 llvm::Constant *C;
676 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
677 C = llvm::ConstantArray::get(type, info);
Mike Stump32f37012009-08-18 21:49:00 +0000678 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C,
Daniel Dunbar77659342009-08-19 20:04:03 +0000679 Out.str());
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000680 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
681 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000682}
683
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000684class VtableBuilder {
Mike Stumpf0070db2009-08-26 20:46:33 +0000685public:
686 /// Index_t - Vtable index type.
687 typedef uint64_t Index_t;
688private:
Mike Stump7c435fa2009-08-18 20:50:28 +0000689 std::vector<llvm::Constant *> &methods;
Mike Stump15a24e02009-08-28 23:22:54 +0000690 std::vector<llvm::Constant *> submethods;
Mike Stump7c435fa2009-08-18 20:50:28 +0000691 llvm::Type *Ptr8Ty;
Mike Stumpb9871a22009-08-21 01:45:00 +0000692 /// Class - The most derived class that this vtable is being built for.
Mike Stump32f37012009-08-18 21:49:00 +0000693 const CXXRecordDecl *Class;
Mike Stumpb9871a22009-08-21 01:45:00 +0000694 /// BLayout - Layout for the most derived class that this vtable is being
695 /// built for.
Mike Stumpb46c92d2009-08-19 02:06:38 +0000696 const ASTRecordLayout &BLayout;
Mike Stumpee560f32009-08-19 14:40:47 +0000697 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
Mike Stump7fa0d932009-08-20 02:11:48 +0000698 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stump32f37012009-08-18 21:49:00 +0000699 llvm::Constant *rtti;
Mike Stump7c435fa2009-08-18 20:50:28 +0000700 llvm::LLVMContext &VMContext;
Mike Stump65defe32009-08-18 21:03:28 +0000701 CodeGenModule &CGM; // Per-module state.
Mike Stumpb9871a22009-08-21 01:45:00 +0000702 /// Index - Maps a method decl into a vtable index. Useful for virtual
703 /// dispatch codegen.
Mike Stumpf0070db2009-08-26 20:46:33 +0000704 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
Mike Stump15a24e02009-08-28 23:22:54 +0000705 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
706 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump97f4d462009-09-18 19:06:35 +0000707 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump6e319f62009-09-11 23:25:56 +0000708 typedef std::pair<Index_t, Index_t> CallOffset;
709 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
Mike Stump77ca8f62009-09-05 07:20:32 +0000710 Thunks_t Thunks;
Mike Stump6e319f62009-09-11 23:25:56 +0000711 typedef llvm::DenseMap<const CXXMethodDecl *,
712 std::pair<CallOffset, CallOffset> > CovariantThunks_t;
713 CovariantThunks_t CovariantThunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000714 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000715 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000716 // FIXME: Linkage should follow vtable
717 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000718 const uint32_t LLVMPointerWidth;
719 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000720public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000721 VtableBuilder(std::vector<llvm::Constant *> &meth,
722 const CXXRecordDecl *c,
723 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000724 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
725 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000726 CGM(cgm), Extern(true),
727 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000728 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
729 }
Mike Stump32f37012009-08-18 21:49:00 +0000730
Mike Stumpf0070db2009-08-26 20:46:33 +0000731 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stump97f4d462009-09-18 19:06:35 +0000732 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
733 { return VBIndex; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000734
Mike Stump15a24e02009-08-28 23:22:54 +0000735 llvm::Constant *wrap(Index_t i) {
736 llvm::Constant *m;
737 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
738 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000739 }
740
Mike Stump15a24e02009-08-28 23:22:54 +0000741 llvm::Constant *wrap(llvm::Constant *m) {
742 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000743 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000744
Mike Stump7fa0d932009-08-20 02:11:48 +0000745 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000746 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump97f4d462009-09-18 19:06:35 +0000747 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Mike Stump7fa0d932009-08-20 02:11:48 +0000748 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000749 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000750 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
751 if (i->isVirtual() && !SeenVBase.count(Base)) {
752 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000753 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000754 llvm::Constant *m = wrap(BaseOffset);
755 m = wrap((0?700:0) + BaseOffset);
Mike Stump97f4d462009-09-18 19:06:35 +0000756 VBIndex[Base] = -(offsets.size()*LLVMPointerWidth/8)
757 - 3*LLVMPointerWidth/8;
Mike Stump7fa0d932009-08-20 02:11:48 +0000758 offsets.push_back(m);
759 }
Mike Stumpb9837442009-08-20 07:22:17 +0000760 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000761 }
762 }
763
Mike Stumpb9871a22009-08-21 01:45:00 +0000764 void StartNewTable() {
765 SeenVBase.clear();
766 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000767
Mike Stump97f4d462009-09-18 19:06:35 +0000768 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
769
770 /// getVbaseOffset - Returns the index into the vtable for the virtual base
771 /// offset for the given (B) virtual base of the derived class D.
772 Index_t getVbaseOffset(QualType qB, QualType qD) {
773 qD = qD->getAs<PointerType>()->getPointeeType();
774 qB = qB->getAs<PointerType>()->getPointeeType();
775 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
776 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
777 if (D != Class)
778 return VBlookup(D, B);
779 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
780 i = VBIndex.find(B);
781 if (i != VBIndex.end())
782 return i->second;
783 // FIXME: temporal botch, is this data here, by the time we need it?
784
785 // FIXME: Locate the containing virtual base first.
786 return 42;
787 }
788
Mike Stump35191b62009-09-01 22:20:28 +0000789 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000790 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000791 typedef CXXMethodDecl::method_iterator meth_iter;
792
Mike Stumpb9871a22009-08-21 01:45:00 +0000793 // FIXME: Don't like the nested loops. For very large inheritance
794 // heirarchies we could have a table on the side with the final overridder
795 // and just replace each instance of an overridden method once. Would be
796 // nice to measure the cost/benefit on real code.
797
Mike Stumpb9871a22009-08-21 01:45:00 +0000798 for (meth_iter mi = MD->begin_overridden_methods(),
799 e = MD->end_overridden_methods();
800 mi != e; ++mi) {
801 const CXXMethodDecl *OMD = *mi;
802 llvm::Constant *om;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000803 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
Mike Stumpb9871a22009-08-21 01:45:00 +0000804 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
805
Mike Stumpdec025b2009-09-07 04:27:52 +0000806 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000807 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000808 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000809 if (submethods[i] != om)
810 continue;
John McCall183700f2009-09-21 23:43:11 +0000811 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000812 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
John McCall183700f2009-09-21 23:43:11 +0000813 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000814 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
815 CallOffset ReturnOffset = std::make_pair(0, 0);
816 if (oret != ret) {
817 // FIXME: calculate offsets for covariance
Mike Stump97f4d462009-09-18 19:06:35 +0000818 ReturnOffset = std::make_pair(42,getVbaseOffset(oret, ret));
Mike Stump6e319f62009-09-11 23:25:56 +0000819 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000820 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000821 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000822
823 Thunks.erase(OMD);
824 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000825 Index_t &idx = VCall[OMD];
826 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000827 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000828 idx = VCalls.size()+1;
829 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000830 } else {
831 VCallOffset[MD] = VCallOffset[OMD];
832 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000833 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000834 VCall[MD] = idx;
Mike Stump6e319f62009-09-11 23:25:56 +0000835 CallOffset ThisOffset;
836 // FIXME: calculate non-virtual offset
837 ThisOffset = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
838 if (ReturnOffset.first || ReturnOffset.second)
839 CovariantThunks[MD] = std::make_pair(ThisOffset, ReturnOffset);
840 else
841 Thunks[MD] = ThisOffset;
Mike Stump35191b62009-09-01 22:20:28 +0000842 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000843 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000844#if 0
845 // FIXME: finish off
846 int64_t O = VCallOffset[OMD] - Offset/8;
847 if (O) {
848 Thunks[MD] = std::make_pair(O, 0);
849 }
850#endif
851 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000852 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000853 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000854
Mike Stump35191b62009-09-01 22:20:28 +0000855 return false;
856 }
857
Mike Stump98cc7102009-09-05 11:28:33 +0000858 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000859 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
860 i != e; ++i) {
861 const CXXMethodDecl *MD = i->first;
862 Index_t idx = Index[MD];
863 Index_t nv_O = i->second.first;
864 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000865 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000866 }
867 Thunks.clear();
Mike Stump6e319f62009-09-11 23:25:56 +0000868 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
869 e = CovariantThunks.end();
870 i != e; ++i) {
871 const CXXMethodDecl *MD = i->first;
872 Index_t idx = Index[MD];
873 Index_t nv_t = i->second.first.first;
874 Index_t v_t = i->second.first.second;
875 Index_t nv_r = i->second.second.first;
876 Index_t v_r = i->second.second.second;
877 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
878 v_r);
879 }
880 CovariantThunks.clear();
Mike Stump77ca8f62009-09-05 07:20:32 +0000881 }
882
Mike Stumpdec025b2009-09-07 04:27:52 +0000883 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
884 int64_t> > *Path, bool MorallyVirtual) {
885 for (std::vector<std::pair<const CXXRecordDecl *,
886 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000887 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000888 const CXXRecordDecl *RD = i->first;
889 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000890 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
891 ++mi)
892 if (mi->isVirtual()) {
893 const CXXMethodDecl *MD = *mi;
Anders Carlssonb299d352009-10-06 17:54:23 +0000894 const FunctionProtoType *FPT =
895 MD->getType()->getAs<FunctionProtoType>();
896 const llvm::Type *Ty =
897 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
898 FPT->isVariadic());
899
900 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD, Ty));
Mike Stumpdec025b2009-09-07 04:27:52 +0000901 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000902 }
903 }
Mike Stumpf9a883c2009-09-01 23:22:44 +0000904 }
905
Mike Stump6d10eb82009-09-05 07:49:12 +0000906 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlssonc7cba152009-09-12 00:00:29 +0000907 llvm::Constant *m = 0;
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000908 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonc7cba152009-09-12 00:00:29 +0000909 m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
Anders Carlssonb299d352009-10-06 17:54:23 +0000910 else {
911 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
912 const llvm::Type *Ty =
913 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
914 FPT->isVariadic());
915
916 m = wrap(CGM.GetAddrOfFunction(MD, Ty));
917 }
918
Mike Stump77ca8f62009-09-05 07:20:32 +0000919 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +0000920 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +0000921 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Mike Stumpb9871a22009-08-21 01:45:00 +0000923 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +0000924 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +0000925 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +0000926 if (MorallyVirtual) {
927 VCallOffset[MD] = Offset/8;
928 Index_t &idx = VCall[MD];
929 // Allocate the first one, after that, we reuse the previous one.
930 if (idx == 0) {
931 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +0000932 VCalls.push_back(0);
933 }
934 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000935 }
936
Mike Stump6d10eb82009-09-05 07:49:12 +0000937 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
938 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000939 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
940 ++mi)
941 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +0000942 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +0000943 }
Mike Stump65defe32009-08-18 21:03:28 +0000944
Mike Stump77ca8f62009-09-05 07:20:32 +0000945 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
946 const CXXRecordDecl *PrimaryBase,
947 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
948 int64_t Offset) {
949 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
950 e = RD->bases_end(); i != e; ++i) {
951 if (i->isVirtual())
952 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000953 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +0000954 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
955 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
956 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
957 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +0000958 std::vector<std::pair<const CXXRecordDecl *,
959 int64_t> > S;
960 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +0000961 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +0000962 }
963 }
964 }
965
Mike Stump6d10eb82009-09-05 07:49:12 +0000966 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
967 const ASTRecordLayout &Layout,
968 const CXXRecordDecl *PrimaryBase,
969 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
970 int64_t Offset, bool ForVirtualBase) {
971 StartNewTable();
972 extra = 0;
973 // FIXME: Cleanup.
974 if (!ForVirtualBase) {
975 // then virtual base offsets...
976 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
977 e = offsets.rend(); i != e; ++i)
978 methods.push_back(*i);
979 }
980
981 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +0000982 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
983 e=VCalls.rend();
984 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +0000985 methods.push_back(wrap((0?600:0) + *i));
986 VCalls.clear();
987
988 if (ForVirtualBase) {
989 // then virtual base offsets...
990 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
991 e = offsets.rend(); i != e; ++i)
992 methods.push_back(*i);
993 }
994
995 methods.push_back(wrap(-(Offset/8)));
996 methods.push_back(rtti);
997 Index_t AddressPoint = methods.size();
998
Mike Stump98cc7102009-09-05 11:28:33 +0000999 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +00001000 methods.insert(methods.end(), submethods.begin(), submethods.end());
1001 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +00001002
1003 // and then the non-virtual bases.
1004 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1005 MorallyVirtual, Offset);
1006 return AddressPoint;
1007 }
1008
Mike Stump078d7782009-09-05 08:40:18 +00001009 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +00001010 if (!RD->isDynamicClass())
1011 return;
1012
1013 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001014 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +00001015 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1016
Mike Stump9bbe9622009-09-05 08:37:03 +00001017 // vtables are composed from the chain of primaries.
1018 if (PrimaryBase) {
1019 if (PrimaryBaseWasVirtual)
1020 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001021 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +00001022 }
1023
1024 // And add the virtuals for the class to the primary vtable.
1025 AddMethods(RD, MorallyVirtual, Offset);
1026 }
1027
Mike Stumpe45c90f2009-09-05 09:10:58 +00001028 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +00001029 bool MorallyVirtual = false, int64_t Offset = 0,
1030 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +00001031 std::vector<std::pair<const CXXRecordDecl *,
1032 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +00001033 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +00001034 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +00001035
1036 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +00001037 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +00001038 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1039
Mike Stump15a24e02009-08-28 23:22:54 +00001040 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +00001041 extra = 0;
1042 GenerateVBaseOffsets(offsets, RD, Offset);
1043 if (ForVirtualBase)
1044 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +00001045
1046 // vtables are composed from the chain of primaries.
1047 if (PrimaryBase) {
1048 if (PrimaryBaseWasVirtual)
1049 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +00001050 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +00001051 }
1052
Mike Stump15a24e02009-08-28 23:22:54 +00001053 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +00001054 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +00001055
Mike Stump98cc7102009-09-05 11:28:33 +00001056 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +00001057 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +00001058
Mike Stump6d10eb82009-09-05 07:49:12 +00001059 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
1060 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +00001061 }
1062
Mike Stump98cc7102009-09-05 11:28:33 +00001063 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +00001064 int64_t Offset = 0,
1065 std::vector<std::pair<const CXXRecordDecl *,
1066 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +00001067 bool alloc = false;
1068 if (Path == 0) {
1069 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +00001070 Path = new std::vector<std::pair<const CXXRecordDecl *,
1071 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +00001072 }
1073 // FIXME: We also need to override using all paths to a virtual base,
1074 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +00001075 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +00001076 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1077 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00001078 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +00001079 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1080 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1081 // Mark it so we don't output it twice.
1082 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +00001083 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +00001084 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +00001085 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +00001086 }
Mike Stumpdec025b2009-09-07 04:27:52 +00001087 int64_t BaseOffset = Offset;
1088 if (i->isVirtual())
1089 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +00001090 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +00001091 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +00001092 }
Mike Stump98cc7102009-09-05 11:28:33 +00001093 Path->pop_back();
1094 if (alloc)
1095 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +00001096 }
Mike Stump109b13d2009-08-18 21:30:21 +00001097};
Mike Stump8a12b562009-08-06 15:50:11 +00001098
Mike Stumpf0070db2009-08-26 20:46:33 +00001099class VtableInfo {
1100public:
1101 typedef VtableBuilder::Index_t Index_t;
1102private:
1103 CodeGenModule &CGM; // Per-module state.
1104 /// Index_t - Vtable index type.
1105 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
1106 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
1107 // FIXME: Move to Context.
1108 static MapTy IndexFor;
Mike Stump97f4d462009-09-18 19:06:35 +00001109
1110 typedef llvm::DenseMap<const CXXRecordDecl *, Index_t> VBElTy;
1111 typedef llvm::DenseMap<const CXXRecordDecl *, VBElTy *> VBMapTy;
1112 // FIXME: Move to Context.
1113 static VBMapTy VBIndexFor;
Mike Stumpf0070db2009-08-26 20:46:33 +00001114public:
1115 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
Mike Stump97f4d462009-09-18 19:06:35 +00001116 void RegisterIndex(const CXXRecordDecl *RD, const ElTy &e) {
Mike Stumpf0070db2009-08-26 20:46:33 +00001117 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
1118 // We own a copy of this, it will go away shortly.
Mike Stumpf0070db2009-08-26 20:46:33 +00001119 IndexFor[RD] = new ElTy (e);
1120 }
Mike Stump97f4d462009-09-18 19:06:35 +00001121 void RegisterVBIndex(const CXXRecordDecl *RD, const VBElTy &e) {
1122 assert(VBIndexFor.find(RD) == VBIndexFor.end() && "Don't compute vtbl twice");
1123 // We own a copy of this, it will go away shortly.
1124 VBIndexFor[RD] = new VBElTy (e);
1125 }
Mike Stumpf0070db2009-08-26 20:46:33 +00001126 Index_t lookup(const CXXMethodDecl *MD) {
1127 const CXXRecordDecl *RD = MD->getParent();
1128 MapTy::iterator I = IndexFor.find(RD);
1129 if (I == IndexFor.end()) {
1130 std::vector<llvm::Constant *> methods;
Mike Stump97f4d462009-09-18 19:06:35 +00001131 // FIXME: This seems expensive. Can we do a partial job to get
1132 // just this data.
Mike Stumpf0070db2009-08-26 20:46:33 +00001133 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001134 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001135 b.GenerateVtableForVBases(RD);
Mike Stump97f4d462009-09-18 19:06:35 +00001136 RegisterIndex(RD, b.getIndex());
Mike Stumpf0070db2009-08-26 20:46:33 +00001137 I = IndexFor.find(RD);
1138 }
1139 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1140 return (*I->second)[MD];
1141 }
Mike Stump97f4d462009-09-18 19:06:35 +00001142 Index_t VBlookup(const CXXRecordDecl *RD, const CXXRecordDecl *BD) {
1143 VBMapTy::iterator I = VBIndexFor.find(RD);
1144 if (I == VBIndexFor.end()) {
1145 std::vector<llvm::Constant *> methods;
1146 // FIXME: This seems expensive. Can we do a partial job to get
1147 // just this data.
1148 VtableBuilder b(methods, RD, CGM);
1149 b.GenerateVtableForBase(RD);
1150 b.GenerateVtableForVBases(RD);
1151 RegisterVBIndex(RD, b.getVBIndex());
1152 I = VBIndexFor.find(RD);
1153 }
1154 assert(I->second->find(BD)!=I->second->end() && "Can't find vtable index");
1155 return (*I->second)[BD];
1156 }
Mike Stumpf0070db2009-08-26 20:46:33 +00001157};
1158
Mike Stump97f4d462009-09-18 19:06:35 +00001159// FIXME: move to Context
1160static VtableInfo *vtableinfo;
1161
1162VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
1163 CXXRecordDecl *B) {
1164 if (vtableinfo == 0)
1165 vtableinfo = new VtableInfo(CGM);
1166
1167 return vtableinfo->VBlookup(D, B);
1168}
1169
1170
Mike Stumpf0070db2009-08-26 20:46:33 +00001171// FIXME: Move to Context.
1172VtableInfo::MapTy VtableInfo::IndexFor;
1173
Mike Stump97f4d462009-09-18 19:06:35 +00001174// FIXME: Move to Context.
1175VtableInfo::VBMapTy VtableInfo::VBIndexFor;
1176
Mike Stumpf1216772009-07-31 18:25:34 +00001177llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001178 llvm::SmallString<256> OutName;
1179 llvm::raw_svector_ostream Out(OutName);
1180 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001181 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001182 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001183 llvm::GlobalVariable::LinkageTypes linktype;
1184 linktype = llvm::GlobalValue::WeakAnyLinkage;
1185 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001186 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001187 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001188
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001189 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001190
Mike Stump276b9f12009-08-16 01:46:26 +00001191 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001192 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001193
Mike Stump276b9f12009-08-16 01:46:26 +00001194 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001195 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001196
Mike Stump82b56962009-07-31 21:43:43 +00001197 llvm::Constant *C;
1198 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1199 C = llvm::ConstantArray::get(type, methods);
1200 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001201 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001202 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001203 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001204 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001205 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001206 return vtable;
1207}
1208
Mike Stumped032eb2009-09-04 18:27:16 +00001209llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1210 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001211 bool Extern, int64_t nv,
1212 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +00001213 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +00001214
1215 FunctionArgList Args;
1216 ImplicitParamDecl *ThisDecl =
1217 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1218 MD->getThisType(getContext()));
1219 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1220 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1221 e = MD->param_end();
1222 i != e; ++i) {
1223 ParmVarDecl *D = *i;
1224 Args.push_back(std::make_pair(D, D->getType()));
1225 }
1226 IdentifierInfo *II
1227 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1228 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1229 getContext().getTranslationUnitDecl(),
1230 SourceLocation(), II, R, 0,
1231 Extern
1232 ? FunctionDecl::Extern
1233 : FunctionDecl::Static,
1234 false, true);
1235 StartFunction(FD, R, Fn, Args, SourceLocation());
1236 // FIXME: generate body
1237 FinishFunction();
1238 return Fn;
1239}
1240
Mike Stump6e319f62009-09-11 23:25:56 +00001241llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
1242 const CXXMethodDecl *MD,
1243 bool Extern,
1244 int64_t nv_t,
1245 int64_t v_t,
1246 int64_t nv_r,
1247 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +00001248 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +00001249
1250 FunctionArgList Args;
1251 ImplicitParamDecl *ThisDecl =
1252 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1253 MD->getThisType(getContext()));
1254 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1255 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1256 e = MD->param_end();
1257 i != e; ++i) {
1258 ParmVarDecl *D = *i;
1259 Args.push_back(std::make_pair(D, D->getType()));
1260 }
1261 IdentifierInfo *II
1262 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1263 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1264 getContext().getTranslationUnitDecl(),
1265 SourceLocation(), II, R, 0,
1266 Extern
1267 ? FunctionDecl::Extern
1268 : FunctionDecl::Static,
1269 false, true);
1270 StartFunction(FD, R, Fn, Args, SourceLocation());
1271 // FIXME: generate body
1272 FinishFunction();
1273 return Fn;
1274}
1275
Mike Stump77ca8f62009-09-05 07:20:32 +00001276llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1277 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001278 llvm::SmallString<256> OutName;
1279 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001280 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001281 llvm::GlobalVariable::LinkageTypes linktype;
1282 linktype = llvm::GlobalValue::WeakAnyLinkage;
1283 if (!Extern)
1284 linktype = llvm::GlobalValue::InternalLinkage;
1285 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001286 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +00001287 const llvm::FunctionType *FTy =
1288 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1289 FPT->isVariadic());
1290
1291 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1292 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001293 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001294 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1295 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1296 return m;
1297}
1298
Mike Stump6e319f62009-09-11 23:25:56 +00001299llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
1300 bool Extern, int64_t nv_t,
1301 int64_t v_t, int64_t nv_r,
1302 int64_t v_r) {
1303 llvm::SmallString<256> OutName;
1304 llvm::raw_svector_ostream Out(OutName);
1305 mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out);
1306 llvm::GlobalVariable::LinkageTypes linktype;
1307 linktype = llvm::GlobalValue::WeakAnyLinkage;
1308 if (!Extern)
1309 linktype = llvm::GlobalValue::InternalLinkage;
1310 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001311 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +00001312 const llvm::FunctionType *FTy =
1313 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1314 FPT->isVariadic());
1315
1316 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1317 &getModule());
1318 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1319 v_r);
1320 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1321 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1322 return m;
1323}
1324
Mike Stumpf0070db2009-08-26 20:46:33 +00001325llvm::Value *
1326CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1327 const llvm::Type *Ty) {
1328 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Anders Carlsson2b358352009-10-03 14:56:57 +00001330 uint64_t Index = CGM.GetVtableIndex(MD);
1331
Mike Stumpf0070db2009-08-26 20:46:33 +00001332 Ty = llvm::PointerType::get(Ty, 0);
1333 Ty = llvm::PointerType::get(Ty, 0);
1334 Ty = llvm::PointerType::get(Ty, 0);
1335 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1336 vtbl = Builder.CreateLoad(vtbl);
1337 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +00001338 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +00001339 vfn = Builder.CreateLoad(vfn);
1340 return vfn;
1341}
1342
Anders Carlsson2b358352009-10-03 14:56:57 +00001343uint64_t CodeGenModule::GetVtableIndex(const CXXMethodDecl *MD) {
1344 // FIXME: move to CodeGenModule.
1345 if (vtableinfo == 0)
1346 vtableinfo = new VtableInfo(*this);
1347
1348 return vtableinfo->lookup(MD);
1349}
1350
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001351/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1352/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1353/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001354// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001355void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001356 llvm::Value *Src,
1357 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001358 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001359 QualType Ty) {
1360 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1361 assert(CA && "VLA cannot be copied over");
1362 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001364 // Create a temporary for the loop index and initialize it with 0.
1365 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1366 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001367 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001368 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +00001369 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001370 // Start the loop with a block that tests the condition.
1371 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1372 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001374 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001376 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1377 // Generate: if (loop-index < number-of-elements fall to the loop body,
1378 // otherwise, go to the block after the for-loop.
1379 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001380 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001381 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1382 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001383 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001384 "isless");
1385 // If the condition is true, execute the body.
1386 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001388 EmitBlock(ForBody);
1389 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1390 // Inside the loop body, emit the constructor call on the array element.
1391 Counter = Builder.CreateLoad(IndexPtr);
1392 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1393 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1394 if (BitwiseCopy)
1395 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001396 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001397 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001398 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001399 Ctor_Complete);
1400 CallArgList CallArgs;
1401 // Push the this (Dest) ptr.
1402 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1403 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001405 // Push the Src ptr.
1406 CallArgs.push_back(std::make_pair(RValue::get(Src),
1407 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001408 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001409 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001410 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1411 Callee, CallArgs, BaseCopyCtor);
1412 }
1413 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001415 // Emit the increment of the loop counter.
1416 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1417 Counter = Builder.CreateLoad(IndexPtr);
1418 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1419 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001421 // Finally, branch back up to the condition for the next iteration.
1422 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001424 // Emit the fall-through block.
1425 EmitBlock(AfterFor, true);
1426}
1427
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001428/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001429/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001430/// bitwise assignment or via a copy assignment operator function call.
1431/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001432void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001433 llvm::Value *Src,
1434 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001435 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001436 QualType Ty) {
1437 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1438 assert(CA && "VLA cannot be asssigned");
1439 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001441 // Create a temporary for the loop index and initialize it with 0.
1442 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1443 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001444 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001445 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1446 Builder.CreateStore(zeroConstant, IndexPtr, false);
1447 // Start the loop with a block that tests the condition.
1448 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1449 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001451 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001453 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1454 // Generate: if (loop-index < number-of-elements fall to the loop body,
1455 // otherwise, go to the block after the for-loop.
1456 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001457 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001458 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1459 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001460 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001461 "isless");
1462 // If the condition is true, execute the body.
1463 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001465 EmitBlock(ForBody);
1466 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1467 // Inside the loop body, emit the assignment operator call on array element.
1468 Counter = Builder.CreateLoad(IndexPtr);
1469 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1470 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1471 const CXXMethodDecl *MD = 0;
1472 if (BitwiseAssign)
1473 EmitAggregateCopy(Dest, Src, Ty);
1474 else {
1475 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1476 MD);
1477 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1478 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001479 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001480 const llvm::Type *LTy =
1481 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1482 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001483 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001485 CallArgList CallArgs;
1486 // Push the this (Dest) ptr.
1487 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1488 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001490 // Push the Src ptr.
1491 CallArgs.push_back(std::make_pair(RValue::get(Src),
1492 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001493 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001494 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1495 Callee, CallArgs, MD);
1496 }
1497 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001499 // Emit the increment of the loop counter.
1500 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1501 Counter = Builder.CreateLoad(IndexPtr);
1502 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1503 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001505 // Finally, branch back up to the condition for the next iteration.
1506 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001508 // Emit the fall-through block.
1509 EmitBlock(AfterFor, true);
1510}
1511
Fariborz Jahanianca283612009-08-07 23:51:33 +00001512/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1513/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001514/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001515void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001516 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001517 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001518 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1519 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001520 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1521 /*NullCheckValue=*/false);
1522 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1523 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001524 }
1525 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1526 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001527 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
1530 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001531 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001532 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001533 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001534 CallArgList CallArgs;
1535 // Push the this (Dest) ptr.
1536 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1537 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Fariborz Jahanianca283612009-08-07 23:51:33 +00001539 // Push the Src ptr.
1540 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001541 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001542 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001543 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001544 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1545 Callee, CallArgs, BaseCopyCtor);
1546 }
1547}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001548
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001549/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001550/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001551/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001552// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001553void CodeGenFunction::EmitClassCopyAssignment(
1554 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001555 const CXXRecordDecl *ClassDecl,
1556 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001557 QualType Ty) {
1558 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001559 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1560 /*NullCheckValue=*/false);
1561 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1562 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001563 }
1564 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1565 EmitAggregateCopy(Dest, Src, Ty);
1566 return;
1567 }
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001569 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001570 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001571 MD);
1572 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1573 (void)ConstCopyAssignOp;
1574
John McCall183700f2009-09-21 23:43:11 +00001575 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001576 const llvm::Type *LTy =
1577 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001578 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001579 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001581 CallArgList CallArgs;
1582 // Push the this (Dest) ptr.
1583 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1584 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001586 // Push the Src ptr.
1587 CallArgs.push_back(std::make_pair(RValue::get(Src),
1588 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001589 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001590 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001591 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1592 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001593}
1594
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001595/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001596void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001597CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1598 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001599 llvm::Function *Fn,
1600 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001601 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1602 SourceLocation());
1603 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001604 FinishFunction();
1605}
1606
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001607/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001608/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001609/// The implicitly-defined copy constructor for class X performs a memberwise
1610/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001611/// of initialization of bases and members in a user-defined constructor
1612/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001613/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001614/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001615/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001616/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001617/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001618/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001619/// Virtual base class subobjects shall be copied only once by the
1620/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001621
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001622void
1623CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1624 CXXCtorType Type,
1625 llvm::Function *Fn,
1626 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001627 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001628 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001629 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001630 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1631 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001633 FunctionArgList::const_iterator i = Args.begin();
1634 const VarDecl *ThisArg = i->first;
1635 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1636 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1637 const VarDecl *SrcArg = (i+1)->first;
1638 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1639 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001641 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1642 Base != ClassDecl->bases_end(); ++Base) {
1643 // FIXME. copy constrution of virtual base NYI
1644 if (Base->isVirtual())
1645 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001647 CXXRecordDecl *BaseClassDecl
1648 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001649 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1650 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001651 }
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001653 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1654 FieldEnd = ClassDecl->field_end();
1655 Field != FieldEnd; ++Field) {
1656 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001657 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001658 getContext().getAsConstantArrayType(FieldType);
1659 if (Array)
1660 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001662 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1663 CXXRecordDecl *FieldClassDecl
1664 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1665 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1666 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001667 if (Array) {
1668 const llvm::Type *BasePtr = ConvertType(FieldType);
1669 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001670 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001671 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001672 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001673 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1674 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1675 FieldClassDecl, FieldType);
1676 }
Mike Stump1eb44332009-09-09 15:08:12 +00001677 else
1678 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001679 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001680 continue;
1681 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001682 // Do a built-in assignment of scalar data members.
1683 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1684 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1685 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1686 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001687 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001688 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001689}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001690
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001691/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001692/// Before the implicitly-declared copy assignment operator for a class is
1693/// implicitly defined, all implicitly- declared copy assignment operators for
1694/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001695/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001696/// The implicitly-defined copy assignment operator for class X performs
1697/// memberwise assignment of its subob- jects. The direct base classes of X are
1698/// assigned first, in the order of their declaration in
1699/// the base-specifier-list, and then the immediate nonstatic data members of X
1700/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001701/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001702/// if the subobject is of class type, the copy assignment operator for the
1703/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001704/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001705///
Mike Stump1eb44332009-09-09 15:08:12 +00001706/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001707/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001708///
Mike Stump1eb44332009-09-09 15:08:12 +00001709/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001710/// used.
1711void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001712 llvm::Function *Fn,
1713 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001714
1715 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1716 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1717 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001718 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001720 FunctionArgList::const_iterator i = Args.begin();
1721 const VarDecl *ThisArg = i->first;
1722 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1723 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1724 const VarDecl *SrcArg = (i+1)->first;
1725 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1726 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001728 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1729 Base != ClassDecl->bases_end(); ++Base) {
1730 // FIXME. copy assignment of virtual base NYI
1731 if (Base->isVirtual())
1732 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001734 CXXRecordDecl *BaseClassDecl
1735 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1736 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1737 Base->getType());
1738 }
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001740 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1741 FieldEnd = ClassDecl->field_end();
1742 Field != FieldEnd; ++Field) {
1743 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001744 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001745 getContext().getAsConstantArrayType(FieldType);
1746 if (Array)
1747 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001749 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1750 CXXRecordDecl *FieldClassDecl
1751 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1752 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1753 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001754 if (Array) {
1755 const llvm::Type *BasePtr = ConvertType(FieldType);
1756 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1757 llvm::Value *DestBaseAddrPtr =
1758 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1759 llvm::Value *SrcBaseAddrPtr =
1760 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1761 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1762 FieldClassDecl, FieldType);
1763 }
1764 else
Mike Stump1eb44332009-09-09 15:08:12 +00001765 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001766 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001767 continue;
1768 }
1769 // Do a built-in assignment of scalar data members.
1770 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1771 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1772 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1773 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001774 }
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001776 // return *this;
1777 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001779 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001780}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001781
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001782/// EmitCtorPrologue - This routine generates necessary code to initialize
1783/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001784/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001785void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1786 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001787 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001788 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001789 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001791 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001792 E = CD->init_end();
1793 B != E; ++B) {
1794 CXXBaseOrMemberInitializer *Member = (*B);
1795 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001796 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001797 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001798 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001799 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001800 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1801 BaseClassDecl,
1802 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001803 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001804 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001805 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001806 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001807 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001808 // non-static data member initilaizers.
1809 FieldDecl *Field = Member->getMember();
1810 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001811 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001812 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001813 if (Array)
1814 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Mike Stumpf1216772009-07-31 18:25:34 +00001816 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001817 LValue LHS;
1818 if (FieldType->isReferenceType()) {
1819 // FIXME: This is really ugly; should be refactored somehow
1820 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1821 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001822 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1823 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001824 } else {
1825 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1826 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001827 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001828 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001829 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001830 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001831 if (Array) {
1832 const llvm::Type *BasePtr = ConvertType(FieldType);
1833 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001834 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001835 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001836 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001837 Array, BaseAddrPtr);
1838 }
1839 else
1840 EmitCXXConstructorCall(Member->getConstructor(),
1841 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001842 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001843 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001844 continue;
1845 }
1846 else {
1847 // Initializing an anonymous union data member.
1848 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001849 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001850 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001851 FieldType = anonMember->getType();
1852 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001853 }
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001855 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001856 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001857 RValue RHS;
1858 if (FieldType->isReferenceType())
1859 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1860 /*IsInitializer=*/true);
1861 else
1862 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1863 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001864 }
1865 }
Mike Stumpf1216772009-07-31 18:25:34 +00001866
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001867 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001868 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001869 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001870 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001871 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001872 ClassDecl->bases_begin();
1873 Base != ClassDecl->bases_end(); ++Base) {
1874 // FIXME. copy assignment of virtual base NYI
1875 if (Base->isVirtual())
1876 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001878 CXXRecordDecl *BaseClassDecl
1879 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1880 if (BaseClassDecl->hasTrivialConstructor())
1881 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001882 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001883 BaseClassDecl->getDefaultConstructor(getContext())) {
1884 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001885 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1886 BaseClassDecl,
1887 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001888 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1889 }
1890 }
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001892 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1893 FieldEnd = ClassDecl->field_end();
1894 Field != FieldEnd; ++Field) {
1895 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001896 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001897 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001898 if (Array)
1899 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001900 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1901 continue;
1902 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001903 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001904 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1905 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1906 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001907 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001908 MemberClassDecl->getDefaultConstructor(getContext())) {
1909 LoadOfThis = LoadCXXThis();
1910 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001911 if (Array) {
1912 const llvm::Type *BasePtr = ConvertType(FieldType);
1913 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001914 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001915 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1916 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1917 }
1918 else
Mike Stump1eb44332009-09-09 15:08:12 +00001919 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001920 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001921 }
1922 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001923 }
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Mike Stumpf1216772009-07-31 18:25:34 +00001925 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001926 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001927 if (!LoadOfThis)
1928 LoadOfThis = LoadCXXThis();
1929 llvm::Value *VtableField;
1930 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001931 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001932 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1933 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1934 llvm::Value *vtable = GenerateVtable(ClassDecl);
1935 Builder.CreateStore(vtable, VtableField);
1936 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001937}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001938
1939/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001940/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001941/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001942/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001943void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1944 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001945 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001946 assert(!ClassDecl->getNumVBases() &&
1947 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001948 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001949
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001950 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1951 *E = DD->destr_end(); B != E; ++B) {
1952 uintptr_t BaseOrMember = (*B);
1953 if (DD->isMemberToDestroy(BaseOrMember)) {
1954 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1955 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001956 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001957 getContext().getAsConstantArrayType(FieldType);
1958 if (Array)
1959 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001960 const RecordType *RT = FieldType->getAs<RecordType>();
1961 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1962 if (FieldClassDecl->hasTrivialDestructor())
1963 continue;
1964 llvm::Value *LoadOfThis = LoadCXXThis();
1965 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001966 if (Array) {
1967 const llvm::Type *BasePtr = ConvertType(FieldType);
1968 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001969 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001970 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001971 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001972 Array, BaseAddrPtr);
1973 }
1974 else
1975 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1976 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001977 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001978 const RecordType *RT =
1979 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1980 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1981 if (BaseClassDecl->hasTrivialDestructor())
1982 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001983 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1984 ClassDecl, BaseClassDecl,
1985 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001986 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001987 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001988 }
1989 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001990 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1991 return;
1992 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001993 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001994 // reverse order of their construction.
1995 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001997 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1998 FieldEnd = ClassDecl->field_end();
1999 Field != FieldEnd; ++Field) {
2000 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002001 if (getContext().getAsConstantArrayType(FieldType))
2002 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002003 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
2004 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2005 if (FieldClassDecl->hasTrivialDestructor())
2006 continue;
2007 DestructedFields.push_back(*Field);
2008 }
2009 }
2010 if (!DestructedFields.empty())
2011 for (int i = DestructedFields.size() -1; i >= 0; --i) {
2012 FieldDecl *Field = DestructedFields[i];
2013 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002014 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002015 getContext().getAsConstantArrayType(FieldType);
2016 if (Array)
2017 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002018 const RecordType *RT = FieldType->getAs<RecordType>();
2019 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
2020 llvm::Value *LoadOfThis = LoadCXXThis();
2021 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002022 if (Array) {
2023 const llvm::Type *BasePtr = ConvertType(FieldType);
2024 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002025 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002026 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002027 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00002028 Array, BaseAddrPtr);
2029 }
2030 else
2031 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
2032 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002033 }
Mike Stump1eb44332009-09-09 15:08:12 +00002034
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002035 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
2036 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
2037 Base != ClassDecl->bases_end(); ++Base) {
2038 // FIXME. copy assignment of virtual base NYI
2039 if (Base->isVirtual())
2040 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002041
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002042 CXXRecordDecl *BaseClassDecl
2043 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
2044 if (BaseClassDecl->hasTrivialDestructor())
2045 continue;
2046 DestructedBases.push_back(BaseClassDecl);
2047 }
2048 if (DestructedBases.empty())
2049 return;
2050 for (int i = DestructedBases.size() -1; i >= 0; --i) {
2051 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00002052 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
2053 ClassDecl,BaseClassDecl,
2054 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002055 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
2056 Dtor_Complete, V);
2057 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00002058}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002059
Anders Carlssonde1d26b2009-09-14 05:32:02 +00002060void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
2061 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002062 llvm::Function *Fn,
2063 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00002064
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00002065 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002066 assert(!ClassDecl->hasUserDeclaredDestructor() &&
2067 "SynthesizeDefaultDestructor - destructor has user declaration");
2068 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Anders Carlssonde1d26b2009-09-14 05:32:02 +00002070 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
2071 SourceLocation());
2072 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00002073 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00002074}
Anders Carlsson6815e942009-09-27 18:58:34 +00002075
2076// FIXME: Move this to CGCXXStmt.cpp
2077void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
2078 // FIXME: We need to do more here.
2079 EmitStmt(S.getTryBlock());
2080}