blob: 1ea60eff56d0d00274dc810c8bb34c57b2fb9a5d [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) {
202 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee());
203 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000204
Anders Carlsson2472bf02009-09-29 03:54:11 +0000205 if (MD->isStatic()) {
206 // The method is static, emit it as we would a regular call.
207 llvm::Value *Callee = CGM.GetAddrOfFunction(MD);
208 return EmitCall(Callee, getContext().getPointerType(MD->getType()),
209 CE->arg_begin(), CE->arg_end(), 0);
210
211 }
212
John McCall183700f2009-09-21 23:43:11 +0000213 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump7116da12009-07-30 21:47:44 +0000214
Mike Stump1eb44332009-09-09 15:08:12 +0000215 const llvm::Type *Ty =
216 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Anders Carlssone9918d22009-04-08 20:31:57 +0000217 FPT->isVariadic());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000218 llvm::Value *This;
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Anders Carlsson774e7c62009-04-03 22:50:24 +0000220 if (ME->isArrow())
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000221 This = EmitScalarExpr(ME->getBase());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000222 else {
223 LValue BaseLV = EmitLValue(ME->getBase());
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000224 This = BaseLV.getAddress();
Anders Carlsson774e7c62009-04-03 22:50:24 +0000225 }
Mike Stumpf0070db2009-08-26 20:46:33 +0000226
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000227 // C++ [class.virtual]p12:
Mike Stump1eb44332009-09-09 15:08:12 +0000228 // Explicit qualification with the scope operator (5.1) suppresses the
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000229 // virtual call mechanism.
Mike Stumpf0070db2009-08-26 20:46:33 +0000230 llvm::Value *Callee;
Douglas Gregor0979c802009-08-31 21:41:48 +0000231 if (MD->isVirtual() && !ME->hasQualifier())
Mike Stump740256b2009-09-29 00:50:50 +0000232 // FIXME: push getCanonicalDecl as a conversion using the static type system (CanCXXMethodDecl).
233 Callee = BuildVirtualCall(MD->getCanonicalDecl(), This, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000234 else if (const CXXDestructorDecl *Destructor
Douglas Gregor4fe95f92009-09-04 19:04:08 +0000235 = dyn_cast<CXXDestructorDecl>(MD))
236 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty);
Douglas Gregor0979c802009-08-31 21:41:48 +0000237 else
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000238 Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000239
240 return EmitCXXMemberCall(MD, Callee, This,
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000241 CE->arg_begin(), CE->arg_end());
Anders Carlsson774e7c62009-04-03 22:50:24 +0000242}
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000243
Mike Stump1eb44332009-09-09 15:08:12 +0000244RValue
Anders Carlsson0f294632009-05-27 04:18:27 +0000245CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
246 const CXXMethodDecl *MD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000247 assert(MD->isInstance() &&
Anders Carlsson0f294632009-05-27 04:18:27 +0000248 "Trying to emit a member call expr on a static method!");
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Fariborz Jahanianad258832009-08-13 21:09:41 +0000250 if (MD->isCopyAssignment()) {
251 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
252 if (ClassDecl->hasTrivialCopyAssignment()) {
253 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
254 "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
255 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
256 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
257 QualType Ty = E->getType();
258 EmitAggregateCopy(This, Src, Ty);
259 return RValue::get(This);
260 }
261 }
Mike Stump1eb44332009-09-09 15:08:12 +0000262
John McCall183700f2009-09-21 23:43:11 +0000263 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000264 const llvm::Type *Ty =
265 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Mike Stumped032eb2009-09-04 18:27:16 +0000266 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000267 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Anders Carlsson0f294632009-05-27 04:18:27 +0000269 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlsson0f294632009-05-27 04:18:27 +0000271 return EmitCXXMemberCall(MD, Callee, This,
272 E->arg_begin() + 1, E->arg_end());
273}
274
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000275llvm::Value *CodeGenFunction::LoadCXXThis() {
Mike Stump1eb44332009-09-09 15:08:12 +0000276 assert(isa<CXXMethodDecl>(CurFuncDecl) &&
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000277 "Must be in a C++ member function decl to load 'this'");
278 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() &&
279 "Must be in a C++ member function decl to load 'this'");
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000281 // FIXME: What if we're inside a block?
Mike Stumpf5408fe2009-05-16 07:57:57 +0000282 // ans: See how CodeGenFunction::LoadObjCSelf() uses
283 // CodeGenFunction::BlockForwardSelf() for how to do this.
Anders Carlsson5f4307b2009-04-14 16:58:56 +0000284 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this");
285}
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000286
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000287/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested)
288/// for-loop to call the default constructor on individual members of the
Anders Carlsson569c1f42009-09-23 02:45:36 +0000289/// array.
290/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the
291/// array type and 'ArrayPtr' points to the beginning fo the array.
292/// It is assumed that all relevant checks have been made by the caller.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000293void
294CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +0000295 const ConstantArrayType *ArrayTy,
296 llvm::Value *ArrayPtr) {
297 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
298 llvm::Value * NumElements =
299 llvm::ConstantInt::get(SizeTy,
300 getContext().getConstantArrayElementCount(ArrayTy));
301
302 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr);
303}
304
305void
306CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
307 llvm::Value *NumElements,
308 llvm::Value *ArrayPtr) {
309 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000311 // Create a temporary for the loop index and initialize it with 0.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000312 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index");
313 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
314 Builder.CreateStore(Zero, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000316 // Start the loop with a block that tests the condition.
317 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
318 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000320 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000322 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000324 // Generate: if (loop-index < number-of-elements fall to the loop body,
325 // otherwise, go to the block after the for-loop.
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000326 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000327 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000328 // If the condition is true, execute the body.
329 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000331 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000333 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000334 // Inside the loop body, emit the constructor call on the array element.
Fariborz Jahanian995d2812009-08-20 01:01:06 +0000335 Counter = Builder.CreateLoad(IndexPtr);
Anders Carlsson569c1f42009-09-23 02:45:36 +0000336 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter,
337 "arrayidx");
Fariborz Jahanian4f68d532009-08-26 00:23:27 +0000338 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000340 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000342 // Emit the increment of the loop counter.
Anders Carlsson569c1f42009-09-23 02:45:36 +0000343 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000344 Counter = Builder.CreateLoad(IndexPtr);
345 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
346 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000348 // Finally, branch back up to the condition for the next iteration.
349 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000351 // Emit the fall-through block.
352 EmitBlock(AfterFor, true);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +0000353}
354
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000355/// EmitCXXAggrDestructorCall - calls the default destructor on array
356/// elements in reverse order of construction.
Anders Carlssonb14095a2009-04-17 00:06:03 +0000357void
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000358CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
359 const ArrayType *Array,
360 llvm::Value *This) {
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000361 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
362 assert(CA && "Do we support VLA for destruction ?");
Mike Stump1eb44332009-09-09 15:08:12 +0000363 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000364 1);
Fariborz Jahanian0de78992009-08-21 16:31:06 +0000365 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA);
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000366 // Create a temporary for the loop index and initialize it with count of
367 // array elements.
368 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
369 "loop.index");
370 // Index = ElementCount;
Mike Stump1eb44332009-09-09 15:08:12 +0000371 llvm::Value* UpperCount =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000372 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount);
373 Builder.CreateStore(UpperCount, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000375 // Start the loop with a block that tests the condition.
376 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
377 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000379 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000381 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000383 // Generate: if (loop-index != 0 fall to the loop body,
384 // otherwise, go to the block after the for-loop.
Mike Stump1eb44332009-09-09 15:08:12 +0000385 llvm::Value* zeroConstant =
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000386 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
387 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
388 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant,
389 "isne");
390 // If the condition is true, execute the body.
391 Builder.CreateCondBr(IsNE, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000393 EmitBlock(ForBody);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000395 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
396 // Inside the loop body, emit the constructor call on the array element.
397 Counter = Builder.CreateLoad(IndexPtr);
398 Counter = Builder.CreateSub(Counter, One);
399 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx");
400 EmitCXXDestructorCall(D, Dtor_Complete, Address);
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000402 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000404 // Emit the decrement of the loop counter.
405 Counter = Builder.CreateLoad(IndexPtr);
406 Counter = Builder.CreateSub(Counter, One, "dec");
407 Builder.CreateStore(Counter, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000409 // Finally, branch back up to the condition for the next iteration.
410 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Fariborz Jahanian1c536bf2009-08-20 23:02:58 +0000412 // Emit the fall-through block.
413 EmitBlock(AfterFor, true);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +0000414}
415
416void
Mike Stump1eb44332009-09-09 15:08:12 +0000417CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
418 CXXCtorType Type,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000419 llvm::Value *This,
420 CallExpr::const_arg_iterator ArgBeg,
421 CallExpr::const_arg_iterator ArgEnd) {
Fariborz Jahanian343a3cf2009-08-14 20:11:43 +0000422 if (D->isCopyConstructor(getContext())) {
423 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext());
424 if (ClassDecl->hasTrivialCopyConstructor()) {
425 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
426 "EmitCXXConstructorCall - user declared copy constructor");
427 const Expr *E = (*ArgBeg);
428 QualType Ty = E->getType();
429 llvm::Value *Src = EmitLValue(E).getAddress();
430 EmitAggregateCopy(This, Src, Ty);
431 return;
432 }
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Anders Carlssonb9de2c52009-05-11 23:37:08 +0000435 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type);
436
437 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000438}
439
Mike Stump1eb44332009-09-09 15:08:12 +0000440void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D,
Anders Carlsson7267c162009-05-29 21:03:38 +0000441 CXXDtorType Type,
442 llvm::Value *This) {
443 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Anders Carlsson7267c162009-05-29 21:03:38 +0000445 EmitCXXMemberCall(D, Callee, This, 0, 0);
446}
447
Mike Stump1eb44332009-09-09 15:08:12 +0000448void
449CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
Anders Carlsson31ccf372009-05-03 17:47:16 +0000450 const CXXConstructExpr *E) {
Anders Carlssonb14095a2009-04-17 00:06:03 +0000451 assert(Dest && "Must have a destination!");
Mike Stump1eb44332009-09-09 15:08:12 +0000452
453 const CXXRecordDecl *RD =
Ted Kremenek6217b802009-07-29 21:53:49 +0000454 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000455 if (RD->hasTrivialConstructor())
456 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000457
Mike Stump1eb44332009-09-09 15:08:12 +0000458 // Code gen optimization to eliminate copy constructor and return
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000459 // its first argument instead.
Anders Carlsson92f58222009-08-22 22:30:33 +0000460 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) {
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000461 CXXConstructExpr::const_arg_iterator i = E->arg_begin();
Fariborz Jahanian1cf9ff82009-08-06 19:12:38 +0000462 EmitAggExpr((*i), Dest, false);
463 return;
Fariborz Jahanian6904cbb2009-08-06 01:02:49 +0000464 }
Anders Carlssonb14095a2009-04-17 00:06:03 +0000465 // Call the constructor.
Mike Stump1eb44332009-09-09 15:08:12 +0000466 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
Anders Carlssonb14095a2009-04-17 00:06:03 +0000467 E->arg_begin(), E->arg_end());
468}
469
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000470void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) {
Anders Carlsson2a131fb2009-05-05 04:44:02 +0000471 EmitGlobal(GlobalDecl(D, Ctor_Complete));
472 EmitGlobal(GlobalDecl(D, Ctor_Base));
Anders Carlsson95d4e5d2009-04-15 15:55:24 +0000473}
Anders Carlsson363c1842009-04-16 23:57:24 +0000474
Mike Stump1eb44332009-09-09 15:08:12 +0000475void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000476 CXXCtorType Type) {
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Anders Carlsson27ae5362009-04-17 01:58:57 +0000478 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000480 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Anders Carlsson27ae5362009-04-17 01:58:57 +0000482 SetFunctionDefinitionAttributes(D, Fn);
483 SetLLVMFunctionAttributesForDefinition(D, Fn);
484}
485
Anders Carlsson363c1842009-04-16 23:57:24 +0000486llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000487CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D,
Anders Carlsson363c1842009-04-16 23:57:24 +0000488 CXXCtorType Type) {
489 const llvm::FunctionType *FTy =
490 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Anders Carlsson363c1842009-04-16 23:57:24 +0000492 const char *Name = getMangledCXXCtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000493 return cast<llvm::Function>(
494 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson363c1842009-04-16 23:57:24 +0000495}
Anders Carlsson27ae5362009-04-17 01:58:57 +0000496
Mike Stump1eb44332009-09-09 15:08:12 +0000497const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000498 CXXCtorType Type) {
499 llvm::SmallString<256> Name;
500 llvm::raw_svector_ostream Out(Name);
501 mangleCXXCtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Anders Carlsson27ae5362009-04-17 01:58:57 +0000503 Name += '\0';
504 return UniqueMangledName(Name.begin(), Name.end());
505}
506
507void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) {
Anders Carlsson27ae5362009-04-17 01:58:57 +0000508 EmitCXXDestructor(D, Dtor_Complete);
509 EmitCXXDestructor(D, Dtor_Base);
510}
511
Mike Stump1eb44332009-09-09 15:08:12 +0000512void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000513 CXXDtorType Type) {
514 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Anders Carlsson0ff8baf2009-09-11 00:07:24 +0000516 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Anders Carlsson27ae5362009-04-17 01:58:57 +0000518 SetFunctionDefinitionAttributes(D, Fn);
519 SetLLVMFunctionAttributesForDefinition(D, Fn);
520}
521
522llvm::Function *
Mike Stump1eb44332009-09-09 15:08:12 +0000523CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000524 CXXDtorType Type) {
525 const llvm::FunctionType *FTy =
526 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Anders Carlsson27ae5362009-04-17 01:58:57 +0000528 const char *Name = getMangledCXXDtorName(D, Type);
Chris Lattnerb4880ba2009-05-12 21:21:08 +0000529 return cast<llvm::Function>(
530 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type)));
Anders Carlsson27ae5362009-04-17 01:58:57 +0000531}
532
Mike Stump1eb44332009-09-09 15:08:12 +0000533const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D,
Anders Carlsson27ae5362009-04-17 01:58:57 +0000534 CXXDtorType Type) {
535 llvm::SmallString<256> Name;
536 llvm::raw_svector_ostream Out(Name);
537 mangleCXXDtor(D, Type, Context, Out);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Anders Carlsson27ae5362009-04-17 01:58:57 +0000539 Name += '\0';
540 return UniqueMangledName(Name.begin(), Name.end());
541}
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +0000542
Mike Stump32f37012009-08-18 21:49:00 +0000543llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) {
Mike Stump738f8c22009-07-31 23:15:31 +0000544 llvm::Type *Ptr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +0000545 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000546 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty);
Mike Stump738f8c22009-07-31 23:15:31 +0000547
548 if (!getContext().getLangOptions().Rtti)
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000549 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000550
551 llvm::SmallString<256> OutName;
552 llvm::raw_svector_ostream Out(OutName);
553 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +0000554 ClassTy = getContext().getTagDeclType(RD);
Mike Stump738f8c22009-07-31 23:15:31 +0000555 mangleCXXRtti(ClassTy, getContext(), Out);
Mike Stump738f8c22009-07-31 23:15:31 +0000556 llvm::GlobalVariable::LinkageTypes linktype;
557 linktype = llvm::GlobalValue::WeakAnyLinkage;
558 std::vector<llvm::Constant *> info;
Mike Stump4ef98092009-08-13 22:53:07 +0000559 // assert(0 && "FIXME: implement rtti descriptor");
Mike Stump738f8c22009-07-31 23:15:31 +0000560 // FIXME: descriptor
561 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
Mike Stump4ef98092009-08-13 22:53:07 +0000562 // assert(0 && "FIXME: implement rtti ts");
Mike Stump738f8c22009-07-31 23:15:31 +0000563 // FIXME: TS
564 info.push_back(llvm::Constant::getNullValue(Ptr8Ty));
565
566 llvm::Constant *C;
567 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size());
568 C = llvm::ConstantArray::get(type, info);
Mike Stump32f37012009-08-18 21:49:00 +0000569 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C,
Daniel Dunbar77659342009-08-19 20:04:03 +0000570 Out.str());
Mike Stumpcb1b5d32009-08-04 20:06:48 +0000571 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty);
572 return Rtti;
Mike Stump738f8c22009-07-31 23:15:31 +0000573}
574
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000575class VtableBuilder {
Mike Stumpf0070db2009-08-26 20:46:33 +0000576public:
577 /// Index_t - Vtable index type.
578 typedef uint64_t Index_t;
579private:
Mike Stump7c435fa2009-08-18 20:50:28 +0000580 std::vector<llvm::Constant *> &methods;
Mike Stump15a24e02009-08-28 23:22:54 +0000581 std::vector<llvm::Constant *> submethods;
Mike Stump7c435fa2009-08-18 20:50:28 +0000582 llvm::Type *Ptr8Ty;
Mike Stumpb9871a22009-08-21 01:45:00 +0000583 /// Class - The most derived class that this vtable is being built for.
Mike Stump32f37012009-08-18 21:49:00 +0000584 const CXXRecordDecl *Class;
Mike Stumpb9871a22009-08-21 01:45:00 +0000585 /// BLayout - Layout for the most derived class that this vtable is being
586 /// built for.
Mike Stumpb46c92d2009-08-19 02:06:38 +0000587 const ASTRecordLayout &BLayout;
Mike Stumpee560f32009-08-19 14:40:47 +0000588 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
Mike Stump7fa0d932009-08-20 02:11:48 +0000589 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stump32f37012009-08-18 21:49:00 +0000590 llvm::Constant *rtti;
Mike Stump7c435fa2009-08-18 20:50:28 +0000591 llvm::LLVMContext &VMContext;
Mike Stump65defe32009-08-18 21:03:28 +0000592 CodeGenModule &CGM; // Per-module state.
Mike Stumpb9871a22009-08-21 01:45:00 +0000593 /// Index - Maps a method decl into a vtable index. Useful for virtual
594 /// dispatch codegen.
Mike Stumpf0070db2009-08-26 20:46:33 +0000595 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
Mike Stump15a24e02009-08-28 23:22:54 +0000596 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
597 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stump97f4d462009-09-18 19:06:35 +0000598 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump6e319f62009-09-11 23:25:56 +0000599 typedef std::pair<Index_t, Index_t> CallOffset;
600 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
Mike Stump77ca8f62009-09-05 07:20:32 +0000601 Thunks_t Thunks;
Mike Stump6e319f62009-09-11 23:25:56 +0000602 typedef llvm::DenseMap<const CXXMethodDecl *,
603 std::pair<CallOffset, CallOffset> > CovariantThunks_t;
604 CovariantThunks_t CovariantThunks;
Mike Stump15a24e02009-08-28 23:22:54 +0000605 std::vector<Index_t> VCalls;
Mike Stump552b2752009-08-18 22:04:08 +0000606 typedef CXXRecordDecl::method_iterator method_iter;
Mike Stumped032eb2009-09-04 18:27:16 +0000607 // FIXME: Linkage should follow vtable
608 const bool Extern;
Mike Stump77ca8f62009-09-05 07:20:32 +0000609 const uint32_t LLVMPointerWidth;
610 Index_t extra;
Mike Stump7c435fa2009-08-18 20:50:28 +0000611public:
Mike Stumpeb7e9c32009-08-19 18:10:47 +0000612 VtableBuilder(std::vector<llvm::Constant *> &meth,
613 const CXXRecordDecl *c,
614 CodeGenModule &cgm)
Mike Stumpb46c92d2009-08-19 02:06:38 +0000615 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
616 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
Mike Stump77ca8f62009-09-05 07:20:32 +0000617 CGM(cgm), Extern(true),
618 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Mike Stump7c435fa2009-08-18 20:50:28 +0000619 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
620 }
Mike Stump32f37012009-08-18 21:49:00 +0000621
Mike Stumpf0070db2009-08-26 20:46:33 +0000622 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
Mike Stump97f4d462009-09-18 19:06:35 +0000623 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
624 { return VBIndex; }
Mike Stumpb46c92d2009-08-19 02:06:38 +0000625
Mike Stump15a24e02009-08-28 23:22:54 +0000626 llvm::Constant *wrap(Index_t i) {
627 llvm::Constant *m;
628 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
629 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
Mike Stumpb46c92d2009-08-19 02:06:38 +0000630 }
631
Mike Stump15a24e02009-08-28 23:22:54 +0000632 llvm::Constant *wrap(llvm::Constant *m) {
633 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
Mike Stump80a0e322009-08-12 23:25:18 +0000634 }
Mike Stump4c3aedd2009-08-12 23:14:12 +0000635
Mike Stump7fa0d932009-08-20 02:11:48 +0000636 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets,
Mike Stumpb9837442009-08-20 07:22:17 +0000637 const CXXRecordDecl *RD, uint64_t Offset) {
Mike Stump97f4d462009-09-18 19:06:35 +0000638 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
Mike Stump7fa0d932009-08-20 02:11:48 +0000639 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000640 const CXXRecordDecl *Base =
Mike Stump7fa0d932009-08-20 02:11:48 +0000641 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
642 if (i->isVirtual() && !SeenVBase.count(Base)) {
643 SeenVBase.insert(Base);
Mike Stumpb9837442009-08-20 07:22:17 +0000644 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000645 llvm::Constant *m = wrap(BaseOffset);
646 m = wrap((0?700:0) + BaseOffset);
Mike Stump97f4d462009-09-18 19:06:35 +0000647 VBIndex[Base] = -(offsets.size()*LLVMPointerWidth/8)
648 - 3*LLVMPointerWidth/8;
Mike Stump7fa0d932009-08-20 02:11:48 +0000649 offsets.push_back(m);
650 }
Mike Stumpb9837442009-08-20 07:22:17 +0000651 GenerateVBaseOffsets(offsets, Base, Offset);
Mike Stump7fa0d932009-08-20 02:11:48 +0000652 }
653 }
654
Mike Stumpb9871a22009-08-21 01:45:00 +0000655 void StartNewTable() {
656 SeenVBase.clear();
657 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000658
Mike Stump97f4d462009-09-18 19:06:35 +0000659 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
660
661 /// getVbaseOffset - Returns the index into the vtable for the virtual base
662 /// offset for the given (B) virtual base of the derived class D.
663 Index_t getVbaseOffset(QualType qB, QualType qD) {
664 qD = qD->getAs<PointerType>()->getPointeeType();
665 qB = qB->getAs<PointerType>()->getPointeeType();
666 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
667 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
668 if (D != Class)
669 return VBlookup(D, B);
670 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
671 i = VBIndex.find(B);
672 if (i != VBIndex.end())
673 return i->second;
674 // FIXME: temporal botch, is this data here, by the time we need it?
675
676 // FIXME: Locate the containing virtual base first.
677 return 42;
678 }
679
Mike Stump35191b62009-09-01 22:20:28 +0000680 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stumpdec025b2009-09-07 04:27:52 +0000681 bool MorallyVirtual, Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000682 typedef CXXMethodDecl::method_iterator meth_iter;
683
Mike Stumpb9871a22009-08-21 01:45:00 +0000684 // FIXME: Don't like the nested loops. For very large inheritance
685 // heirarchies we could have a table on the side with the final overridder
686 // and just replace each instance of an overridden method once. Would be
687 // nice to measure the cost/benefit on real code.
688
Mike Stumpb9871a22009-08-21 01:45:00 +0000689 for (meth_iter mi = MD->begin_overridden_methods(),
690 e = MD->end_overridden_methods();
691 mi != e; ++mi) {
692 const CXXMethodDecl *OMD = *mi;
693 llvm::Constant *om;
Anders Carlsson555b4bb2009-09-10 23:43:36 +0000694 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
Mike Stumpb9871a22009-08-21 01:45:00 +0000695 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
696
Mike Stumpdec025b2009-09-07 04:27:52 +0000697 for (Index_t i = 0, e = submethods.size();
Mike Stumpf0070db2009-08-26 20:46:33 +0000698 i != e; ++i) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000699 // FIXME: begin_overridden_methods might be too lax, covariance */
Mike Stump77ca8f62009-09-05 07:20:32 +0000700 if (submethods[i] != om)
701 continue;
John McCall183700f2009-09-21 23:43:11 +0000702 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000703 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
John McCall183700f2009-09-21 23:43:11 +0000704 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +0000705 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
706 CallOffset ReturnOffset = std::make_pair(0, 0);
707 if (oret != ret) {
708 // FIXME: calculate offsets for covariance
Mike Stump97f4d462009-09-18 19:06:35 +0000709 ReturnOffset = std::make_pair(42,getVbaseOffset(oret, ret));
Mike Stump6e319f62009-09-11 23:25:56 +0000710 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000711 Index[MD] = i;
Mike Stump77ca8f62009-09-05 07:20:32 +0000712 submethods[i] = m;
Mike Stump77ca8f62009-09-05 07:20:32 +0000713
714 Thunks.erase(OMD);
715 if (MorallyVirtual) {
Mike Stump77ca8f62009-09-05 07:20:32 +0000716 Index_t &idx = VCall[OMD];
717 if (idx == 0) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000718 VCallOffset[MD] = Offset/8;
Mike Stump77ca8f62009-09-05 07:20:32 +0000719 idx = VCalls.size()+1;
720 VCalls.push_back(0);
Mike Stumpdec025b2009-09-07 04:27:52 +0000721 } else {
722 VCallOffset[MD] = VCallOffset[OMD];
723 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8;
Mike Stump15a24e02009-08-28 23:22:54 +0000724 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000725 VCall[MD] = idx;
Mike Stump6e319f62009-09-11 23:25:56 +0000726 CallOffset ThisOffset;
727 // FIXME: calculate non-virtual offset
728 ThisOffset = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8));
729 if (ReturnOffset.first || ReturnOffset.second)
730 CovariantThunks[MD] = std::make_pair(ThisOffset, ReturnOffset);
731 else
732 Thunks[MD] = ThisOffset;
Mike Stump35191b62009-09-01 22:20:28 +0000733 return true;
Mike Stumpb9871a22009-08-21 01:45:00 +0000734 }
Mike Stump77ca8f62009-09-05 07:20:32 +0000735#if 0
736 // FIXME: finish off
737 int64_t O = VCallOffset[OMD] - Offset/8;
738 if (O) {
739 Thunks[MD] = std::make_pair(O, 0);
740 }
741#endif
742 return true;
Mike Stump65defe32009-08-18 21:03:28 +0000743 }
Mike Stumpbc16aea2009-08-12 23:00:59 +0000744 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000745
Mike Stump35191b62009-09-01 22:20:28 +0000746 return false;
747 }
748
Mike Stump98cc7102009-09-05 11:28:33 +0000749 void InstallThunks() {
Mike Stump77ca8f62009-09-05 07:20:32 +0000750 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
751 i != e; ++i) {
752 const CXXMethodDecl *MD = i->first;
753 Index_t idx = Index[MD];
754 Index_t nv_O = i->second.first;
755 Index_t v_O = i->second.second;
Mike Stump98cc7102009-09-05 11:28:33 +0000756 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
Mike Stump77ca8f62009-09-05 07:20:32 +0000757 }
758 Thunks.clear();
Mike Stump6e319f62009-09-11 23:25:56 +0000759 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
760 e = CovariantThunks.end();
761 i != e; ++i) {
762 const CXXMethodDecl *MD = i->first;
763 Index_t idx = Index[MD];
764 Index_t nv_t = i->second.first.first;
765 Index_t v_t = i->second.first.second;
766 Index_t nv_r = i->second.second.first;
767 Index_t v_r = i->second.second.second;
768 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
769 v_r);
770 }
771 CovariantThunks.clear();
Mike Stump77ca8f62009-09-05 07:20:32 +0000772 }
773
Mike Stumpdec025b2009-09-07 04:27:52 +0000774 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *,
775 int64_t> > *Path, bool MorallyVirtual) {
776 for (std::vector<std::pair<const CXXRecordDecl *,
777 int64_t> >::reverse_iterator i =Path->rbegin(),
Mike Stump98cc7102009-09-05 11:28:33 +0000778 e = Path->rend(); i != e; ++i) {
Mike Stumpdec025b2009-09-07 04:27:52 +0000779 const CXXRecordDecl *RD = i->first;
780 int64_t Offset = i->second;
Mike Stump98cc7102009-09-05 11:28:33 +0000781 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
782 ++mi)
783 if (mi->isVirtual()) {
784 const CXXMethodDecl *MD = *mi;
Anders Carlssonc7cba152009-09-12 00:00:29 +0000785 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(MD));
Mike Stumpdec025b2009-09-07 04:27:52 +0000786 OverrideMethod(MD, m, MorallyVirtual, Offset);
Mike Stump98cc7102009-09-05 11:28:33 +0000787 }
788 }
Mike Stumpf9a883c2009-09-01 23:22:44 +0000789 }
790
Mike Stump6d10eb82009-09-05 07:49:12 +0000791 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) {
Anders Carlssonc7cba152009-09-12 00:00:29 +0000792 llvm::Constant *m = 0;
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000793 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonc7cba152009-09-12 00:00:29 +0000794 m = wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000795 else
Anders Carlssonc7cba152009-09-12 00:00:29 +0000796 m = wrap(CGM.GetAddrOfFunction(MD));
Anders Carlsson3fec4c62009-09-09 23:17:18 +0000797
Mike Stump77ca8f62009-09-05 07:20:32 +0000798 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpdec025b2009-09-07 04:27:52 +0000799 if (OverrideMethod(MD, m, MorallyVirtual, Offset))
Mike Stump35191b62009-09-01 22:20:28 +0000800 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Mike Stumpb9871a22009-08-21 01:45:00 +0000802 // else allocate a new slot.
Mike Stump15a24e02009-08-28 23:22:54 +0000803 Index[MD] = submethods.size();
Mike Stumpdec025b2009-09-07 04:27:52 +0000804 submethods.push_back(m);
Mike Stump15a24e02009-08-28 23:22:54 +0000805 if (MorallyVirtual) {
806 VCallOffset[MD] = Offset/8;
807 Index_t &idx = VCall[MD];
808 // Allocate the first one, after that, we reuse the previous one.
809 if (idx == 0) {
810 idx = VCalls.size()+1;
Mike Stump15a24e02009-08-28 23:22:54 +0000811 VCalls.push_back(0);
812 }
813 }
Mike Stumpb9871a22009-08-21 01:45:00 +0000814 }
815
Mike Stump6d10eb82009-09-05 07:49:12 +0000816 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
817 Index_t Offset) {
Mike Stumpb9871a22009-08-21 01:45:00 +0000818 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
819 ++mi)
820 if (mi->isVirtual())
Mike Stump6d10eb82009-09-05 07:49:12 +0000821 AddMethod(*mi, MorallyVirtual, Offset);
Mike Stumpbc16aea2009-08-12 23:00:59 +0000822 }
Mike Stump65defe32009-08-18 21:03:28 +0000823
Mike Stump77ca8f62009-09-05 07:20:32 +0000824 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
825 const CXXRecordDecl *PrimaryBase,
826 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
827 int64_t Offset) {
828 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
829 e = RD->bases_end(); i != e; ++i) {
830 if (i->isVirtual())
831 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000832 const CXXRecordDecl *Base =
Mike Stump77ca8f62009-09-05 07:20:32 +0000833 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
834 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
835 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
836 StartNewTable();
Mike Stumpdec025b2009-09-07 04:27:52 +0000837 std::vector<std::pair<const CXXRecordDecl *,
838 int64_t> > S;
839 S.push_back(std::make_pair(RD, Offset));
Mike Stump98cc7102009-09-05 11:28:33 +0000840 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S);
Mike Stump77ca8f62009-09-05 07:20:32 +0000841 }
842 }
843 }
844
Mike Stump6d10eb82009-09-05 07:49:12 +0000845 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets,
846 const ASTRecordLayout &Layout,
847 const CXXRecordDecl *PrimaryBase,
848 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
849 int64_t Offset, bool ForVirtualBase) {
850 StartNewTable();
851 extra = 0;
852 // FIXME: Cleanup.
853 if (!ForVirtualBase) {
854 // then virtual base offsets...
855 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
856 e = offsets.rend(); i != e; ++i)
857 methods.push_back(*i);
858 }
859
860 // The vcalls come first...
Mike Stumpdec025b2009-09-07 04:27:52 +0000861 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(),
862 e=VCalls.rend();
863 i != e; ++i)
Mike Stump6d10eb82009-09-05 07:49:12 +0000864 methods.push_back(wrap((0?600:0) + *i));
865 VCalls.clear();
866
867 if (ForVirtualBase) {
868 // then virtual base offsets...
869 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(),
870 e = offsets.rend(); i != e; ++i)
871 methods.push_back(*i);
872 }
873
874 methods.push_back(wrap(-(Offset/8)));
875 methods.push_back(rtti);
876 Index_t AddressPoint = methods.size();
877
Mike Stump98cc7102009-09-05 11:28:33 +0000878 InstallThunks();
Mike Stump6d10eb82009-09-05 07:49:12 +0000879 methods.insert(methods.end(), submethods.begin(), submethods.end());
880 submethods.clear();
Mike Stump6d10eb82009-09-05 07:49:12 +0000881
882 // and then the non-virtual bases.
883 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
884 MorallyVirtual, Offset);
885 return AddressPoint;
886 }
887
Mike Stump078d7782009-09-05 08:40:18 +0000888 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) {
Mike Stump9bbe9622009-09-05 08:37:03 +0000889 if (!RD->isDynamicClass())
890 return;
891
892 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000893 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump9bbe9622009-09-05 08:37:03 +0000894 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
895
Mike Stump9bbe9622009-09-05 08:37:03 +0000896 // vtables are composed from the chain of primaries.
897 if (PrimaryBase) {
898 if (PrimaryBaseWasVirtual)
899 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +0000900 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump9bbe9622009-09-05 08:37:03 +0000901 }
902
903 // And add the virtuals for the class to the primary vtable.
904 AddMethods(RD, MorallyVirtual, Offset);
905 }
906
Mike Stumpe45c90f2009-09-05 09:10:58 +0000907 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
Mike Stumpa18df0e2009-09-05 09:24:43 +0000908 bool MorallyVirtual = false, int64_t Offset = 0,
909 bool ForVirtualBase = false,
Mike Stumpdec025b2009-09-07 04:27:52 +0000910 std::vector<std::pair<const CXXRecordDecl *,
911 int64_t> > *Path = 0) {
Mike Stumpbf595a32009-09-05 08:07:32 +0000912 if (!RD->isDynamicClass())
Mike Stump263b3522009-08-21 23:09:30 +0000913 return 0;
Mike Stump109b13d2009-08-18 21:30:21 +0000914
915 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Mike Stump1eb44332009-09-09 15:08:12 +0000916 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Mike Stump109b13d2009-08-18 21:30:21 +0000917 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
918
Mike Stump15a24e02009-08-28 23:22:54 +0000919 std::vector<llvm::Constant *> offsets;
Mike Stumpb4d28612009-09-05 08:45:02 +0000920 extra = 0;
921 GenerateVBaseOffsets(offsets, RD, Offset);
922 if (ForVirtualBase)
923 extra = offsets.size();
Mike Stump109b13d2009-08-18 21:30:21 +0000924
925 // vtables are composed from the chain of primaries.
926 if (PrimaryBase) {
927 if (PrimaryBaseWasVirtual)
928 IndirectPrimary.insert(PrimaryBase);
Mike Stump078d7782009-09-05 08:40:18 +0000929 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset);
Mike Stump109b13d2009-08-18 21:30:21 +0000930 }
931
Mike Stump15a24e02009-08-28 23:22:54 +0000932 // And add the virtuals for the class to the primary vtable.
Mike Stump6d10eb82009-09-05 07:49:12 +0000933 AddMethods(RD, MorallyVirtual, Offset);
Mike Stump15a24e02009-08-28 23:22:54 +0000934
Mike Stump98cc7102009-09-05 11:28:33 +0000935 if (Path)
Mike Stumpdec025b2009-09-07 04:27:52 +0000936 OverrideMethods(Path, MorallyVirtual);
Mike Stump98cc7102009-09-05 11:28:33 +0000937
Mike Stump6d10eb82009-09-05 07:49:12 +0000938 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual,
939 MorallyVirtual, Offset, ForVirtualBase);
Mike Stump109b13d2009-08-18 21:30:21 +0000940 }
941
Mike Stump98cc7102009-09-05 11:28:33 +0000942 void GenerateVtableForVBases(const CXXRecordDecl *RD,
Mike Stumpdec025b2009-09-07 04:27:52 +0000943 int64_t Offset = 0,
944 std::vector<std::pair<const CXXRecordDecl *,
945 int64_t> > *Path = 0) {
Mike Stump98cc7102009-09-05 11:28:33 +0000946 bool alloc = false;
947 if (Path == 0) {
948 alloc = true;
Mike Stumpdec025b2009-09-07 04:27:52 +0000949 Path = new std::vector<std::pair<const CXXRecordDecl *,
950 int64_t> >;
Mike Stump98cc7102009-09-05 11:28:33 +0000951 }
952 // FIXME: We also need to override using all paths to a virtual base,
953 // right now, we just process the first path
Mike Stumpdec025b2009-09-07 04:27:52 +0000954 Path->push_back(std::make_pair(RD, Offset));
Mike Stump109b13d2009-08-18 21:30:21 +0000955 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
956 e = RD->bases_end(); i != e; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000957 const CXXRecordDecl *Base =
Mike Stump109b13d2009-08-18 21:30:21 +0000958 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
959 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
960 // Mark it so we don't output it twice.
961 IndirectPrimary.insert(Base);
Mike Stumpb9871a22009-08-21 01:45:00 +0000962 StartNewTable();
Mike Stumpb9837442009-08-20 07:22:17 +0000963 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump98cc7102009-09-05 11:28:33 +0000964 GenerateVtableForBase(Base, true, BaseOffset, true, Path);
Mike Stump109b13d2009-08-18 21:30:21 +0000965 }
Mike Stumpdec025b2009-09-07 04:27:52 +0000966 int64_t BaseOffset = Offset;
967 if (i->isVirtual())
968 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump109b13d2009-08-18 21:30:21 +0000969 if (Base->getNumVBases())
Mike Stumpdec025b2009-09-07 04:27:52 +0000970 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump276b9f12009-08-16 01:46:26 +0000971 }
Mike Stump98cc7102009-09-05 11:28:33 +0000972 Path->pop_back();
973 if (alloc)
974 delete Path;
Mike Stump276b9f12009-08-16 01:46:26 +0000975 }
Mike Stump109b13d2009-08-18 21:30:21 +0000976};
Mike Stump8a12b562009-08-06 15:50:11 +0000977
Mike Stumpf0070db2009-08-26 20:46:33 +0000978class VtableInfo {
979public:
980 typedef VtableBuilder::Index_t Index_t;
981private:
982 CodeGenModule &CGM; // Per-module state.
983 /// Index_t - Vtable index type.
984 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy;
985 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy;
986 // FIXME: Move to Context.
987 static MapTy IndexFor;
Mike Stump97f4d462009-09-18 19:06:35 +0000988
989 typedef llvm::DenseMap<const CXXRecordDecl *, Index_t> VBElTy;
990 typedef llvm::DenseMap<const CXXRecordDecl *, VBElTy *> VBMapTy;
991 // FIXME: Move to Context.
992 static VBMapTy VBIndexFor;
Mike Stumpf0070db2009-08-26 20:46:33 +0000993public:
994 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { }
Mike Stump97f4d462009-09-18 19:06:35 +0000995 void RegisterIndex(const CXXRecordDecl *RD, const ElTy &e) {
Mike Stumpf0070db2009-08-26 20:46:33 +0000996 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice");
997 // We own a copy of this, it will go away shortly.
Mike Stumpf0070db2009-08-26 20:46:33 +0000998 IndexFor[RD] = new ElTy (e);
999 }
Mike Stump97f4d462009-09-18 19:06:35 +00001000 void RegisterVBIndex(const CXXRecordDecl *RD, const VBElTy &e) {
1001 assert(VBIndexFor.find(RD) == VBIndexFor.end() && "Don't compute vtbl twice");
1002 // We own a copy of this, it will go away shortly.
1003 VBIndexFor[RD] = new VBElTy (e);
1004 }
Mike Stumpf0070db2009-08-26 20:46:33 +00001005 Index_t lookup(const CXXMethodDecl *MD) {
1006 const CXXRecordDecl *RD = MD->getParent();
1007 MapTy::iterator I = IndexFor.find(RD);
1008 if (I == IndexFor.end()) {
1009 std::vector<llvm::Constant *> methods;
Mike Stump97f4d462009-09-18 19:06:35 +00001010 // FIXME: This seems expensive. Can we do a partial job to get
1011 // just this data.
Mike Stumpf0070db2009-08-26 20:46:33 +00001012 VtableBuilder b(methods, RD, CGM);
Mike Stumpa18df0e2009-09-05 09:24:43 +00001013 b.GenerateVtableForBase(RD);
Mike Stumpbf595a32009-09-05 08:07:32 +00001014 b.GenerateVtableForVBases(RD);
Mike Stump97f4d462009-09-18 19:06:35 +00001015 RegisterIndex(RD, b.getIndex());
Mike Stumpf0070db2009-08-26 20:46:33 +00001016 I = IndexFor.find(RD);
1017 }
1018 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index");
1019 return (*I->second)[MD];
1020 }
Mike Stump97f4d462009-09-18 19:06:35 +00001021 Index_t VBlookup(const CXXRecordDecl *RD, const CXXRecordDecl *BD) {
1022 VBMapTy::iterator I = VBIndexFor.find(RD);
1023 if (I == VBIndexFor.end()) {
1024 std::vector<llvm::Constant *> methods;
1025 // FIXME: This seems expensive. Can we do a partial job to get
1026 // just this data.
1027 VtableBuilder b(methods, RD, CGM);
1028 b.GenerateVtableForBase(RD);
1029 b.GenerateVtableForVBases(RD);
1030 RegisterVBIndex(RD, b.getVBIndex());
1031 I = VBIndexFor.find(RD);
1032 }
1033 assert(I->second->find(BD)!=I->second->end() && "Can't find vtable index");
1034 return (*I->second)[BD];
1035 }
Mike Stumpf0070db2009-08-26 20:46:33 +00001036};
1037
Mike Stump97f4d462009-09-18 19:06:35 +00001038// FIXME: move to Context
1039static VtableInfo *vtableinfo;
1040
1041VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
1042 CXXRecordDecl *B) {
1043 if (vtableinfo == 0)
1044 vtableinfo = new VtableInfo(CGM);
1045
1046 return vtableinfo->VBlookup(D, B);
1047}
1048
1049
Mike Stumpf0070db2009-08-26 20:46:33 +00001050// FIXME: Move to Context.
1051VtableInfo::MapTy VtableInfo::IndexFor;
1052
Mike Stump97f4d462009-09-18 19:06:35 +00001053// FIXME: Move to Context.
1054VtableInfo::VBMapTy VtableInfo::VBIndexFor;
1055
Mike Stumpf1216772009-07-31 18:25:34 +00001056llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) {
Mike Stumpf1216772009-07-31 18:25:34 +00001057 llvm::SmallString<256> OutName;
1058 llvm::raw_svector_ostream Out(OutName);
1059 QualType ClassTy;
Mike Stumpe607ed02009-08-07 18:05:12 +00001060 ClassTy = getContext().getTagDeclType(RD);
Mike Stumpf1216772009-07-31 18:25:34 +00001061 mangleCXXVtable(ClassTy, getContext(), Out);
Mike Stump82b56962009-07-31 21:43:43 +00001062 llvm::GlobalVariable::LinkageTypes linktype;
1063 linktype = llvm::GlobalValue::WeakAnyLinkage;
1064 std::vector<llvm::Constant *> methods;
Mike Stump276b9f12009-08-16 01:46:26 +00001065 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
Mike Stump98cc7102009-09-05 11:28:33 +00001066 int64_t AddressPoint;
Mike Stump6f376332009-08-05 22:37:18 +00001067
Mike Stumpeb7e9c32009-08-19 18:10:47 +00001068 VtableBuilder b(methods, RD, CGM);
Mike Stump109b13d2009-08-18 21:30:21 +00001069
Mike Stump276b9f12009-08-16 01:46:26 +00001070 // First comes the vtables for all the non-virtual bases...
Mike Stump98cc7102009-09-05 11:28:33 +00001071 AddressPoint = b.GenerateVtableForBase(RD);
Mike Stump21538912009-08-14 01:44:03 +00001072
Mike Stump276b9f12009-08-16 01:46:26 +00001073 // then the vtables for all the virtual bases.
Mike Stumpbf595a32009-09-05 08:07:32 +00001074 b.GenerateVtableForVBases(RD);
Mike Stump104ffaa2009-08-04 21:58:42 +00001075
Mike Stump82b56962009-07-31 21:43:43 +00001076 llvm::Constant *C;
1077 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
1078 C = llvm::ConstantArray::get(type, methods);
1079 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true,
Daniel Dunbar77659342009-08-19 20:04:03 +00001080 linktype, C, Out.str());
Mike Stumpf1216772009-07-31 18:25:34 +00001081 vtable = Builder.CreateBitCast(vtable, Ptr8Ty);
Mike Stumpf1216772009-07-31 18:25:34 +00001082 vtable = Builder.CreateGEP(vtable,
Mike Stump276b9f12009-08-16 01:46:26 +00001083 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
Mike Stump98cc7102009-09-05 11:28:33 +00001084 AddressPoint*LLVMPointerWidth/8));
Mike Stumpf1216772009-07-31 18:25:34 +00001085 return vtable;
1086}
1087
Mike Stumped032eb2009-09-04 18:27:16 +00001088llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn,
1089 const CXXMethodDecl *MD,
Mike Stump77ca8f62009-09-05 07:20:32 +00001090 bool Extern, int64_t nv,
1091 int64_t v) {
John McCall183700f2009-09-21 23:43:11 +00001092 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stumped032eb2009-09-04 18:27:16 +00001093
1094 FunctionArgList Args;
1095 ImplicitParamDecl *ThisDecl =
1096 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1097 MD->getThisType(getContext()));
1098 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1099 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1100 e = MD->param_end();
1101 i != e; ++i) {
1102 ParmVarDecl *D = *i;
1103 Args.push_back(std::make_pair(D, D->getType()));
1104 }
1105 IdentifierInfo *II
1106 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1107 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1108 getContext().getTranslationUnitDecl(),
1109 SourceLocation(), II, R, 0,
1110 Extern
1111 ? FunctionDecl::Extern
1112 : FunctionDecl::Static,
1113 false, true);
1114 StartFunction(FD, R, Fn, Args, SourceLocation());
1115 // FIXME: generate body
1116 FinishFunction();
1117 return Fn;
1118}
1119
Mike Stump6e319f62009-09-11 23:25:56 +00001120llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn,
1121 const CXXMethodDecl *MD,
1122 bool Extern,
1123 int64_t nv_t,
1124 int64_t v_t,
1125 int64_t nv_r,
1126 int64_t v_r) {
John McCall183700f2009-09-21 23:43:11 +00001127 QualType R = MD->getType()->getAs<FunctionType>()->getResultType();
Mike Stump6e319f62009-09-11 23:25:56 +00001128
1129 FunctionArgList Args;
1130 ImplicitParamDecl *ThisDecl =
1131 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
1132 MD->getThisType(getContext()));
1133 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType()));
1134 for (FunctionDecl::param_const_iterator i = MD->param_begin(),
1135 e = MD->param_end();
1136 i != e; ++i) {
1137 ParmVarDecl *D = *i;
1138 Args.push_back(std::make_pair(D, D->getType()));
1139 }
1140 IdentifierInfo *II
1141 = &CGM.getContext().Idents.get("__thunk_named_foo_");
1142 FunctionDecl *FD = FunctionDecl::Create(getContext(),
1143 getContext().getTranslationUnitDecl(),
1144 SourceLocation(), II, R, 0,
1145 Extern
1146 ? FunctionDecl::Extern
1147 : FunctionDecl::Static,
1148 false, true);
1149 StartFunction(FD, R, Fn, Args, SourceLocation());
1150 // FIXME: generate body
1151 FinishFunction();
1152 return Fn;
1153}
1154
Mike Stump77ca8f62009-09-05 07:20:32 +00001155llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern,
1156 int64_t nv, int64_t v) {
Mike Stumped032eb2009-09-04 18:27:16 +00001157 llvm::SmallString<256> OutName;
1158 llvm::raw_svector_ostream Out(OutName);
Mike Stump77ca8f62009-09-05 07:20:32 +00001159 mangleThunk(MD, nv, v, getContext(), Out);
Mike Stumped032eb2009-09-04 18:27:16 +00001160 llvm::GlobalVariable::LinkageTypes linktype;
1161 linktype = llvm::GlobalValue::WeakAnyLinkage;
1162 if (!Extern)
1163 linktype = llvm::GlobalValue::InternalLinkage;
1164 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001165 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stumped032eb2009-09-04 18:27:16 +00001166 const llvm::FunctionType *FTy =
1167 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1168 FPT->isVariadic());
1169
1170 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1171 &getModule());
Mike Stump77ca8f62009-09-05 07:20:32 +00001172 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v);
Mike Stumped032eb2009-09-04 18:27:16 +00001173 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1174 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1175 return m;
1176}
1177
Mike Stump6e319f62009-09-11 23:25:56 +00001178llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD,
1179 bool Extern, int64_t nv_t,
1180 int64_t v_t, int64_t nv_r,
1181 int64_t v_r) {
1182 llvm::SmallString<256> OutName;
1183 llvm::raw_svector_ostream Out(OutName);
1184 mangleCovariantThunk(MD, nv_t, v_t, nv_r, v_r, getContext(), Out);
1185 llvm::GlobalVariable::LinkageTypes linktype;
1186 linktype = llvm::GlobalValue::WeakAnyLinkage;
1187 if (!Extern)
1188 linktype = llvm::GlobalValue::InternalLinkage;
1189 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
John McCall183700f2009-09-21 23:43:11 +00001190 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump6e319f62009-09-11 23:25:56 +00001191 const llvm::FunctionType *FTy =
1192 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD),
1193 FPT->isVariadic());
1194
1195 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(),
1196 &getModule());
1197 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r,
1198 v_r);
1199 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty);
1200 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty);
1201 return m;
1202}
1203
Mike Stumpf0070db2009-08-26 20:46:33 +00001204llvm::Value *
1205CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
1206 const llvm::Type *Ty) {
1207 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch.
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Anders Carlsson2b358352009-10-03 14:56:57 +00001209 uint64_t Index = CGM.GetVtableIndex(MD);
1210
Mike Stumpf0070db2009-08-26 20:46:33 +00001211 Ty = llvm::PointerType::get(Ty, 0);
1212 Ty = llvm::PointerType::get(Ty, 0);
1213 Ty = llvm::PointerType::get(Ty, 0);
1214 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty);
1215 vtbl = Builder.CreateLoad(vtbl);
1216 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl,
Anders Carlsson2b358352009-10-03 14:56:57 +00001217 Index, "vfn");
Mike Stumpf0070db2009-08-26 20:46:33 +00001218 vfn = Builder.CreateLoad(vfn);
1219 return vfn;
1220}
1221
Anders Carlsson2b358352009-10-03 14:56:57 +00001222uint64_t CodeGenModule::GetVtableIndex(const CXXMethodDecl *MD) {
1223 // FIXME: move to CodeGenModule.
1224 if (vtableinfo == 0)
1225 vtableinfo = new VtableInfo(*this);
1226
1227 return vtableinfo->lookup(MD);
1228}
1229
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001230/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
1231/// array of objects from SrcValue to DestValue. Copying can be either a bitwise
1232/// copy or via a copy constructor call.
Fariborz Jahanian4f68d532009-08-26 00:23:27 +00001233// FIXME. Consolidate this with EmitCXXAggrConstructorCall.
Mike Stump1eb44332009-09-09 15:08:12 +00001234void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001235 llvm::Value *Src,
1236 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001237 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001238 QualType Ty) {
1239 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1240 assert(CA && "VLA cannot be copied over");
1241 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor();
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001243 // Create a temporary for the loop index and initialize it with 0.
1244 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1245 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001246 llvm::Value* zeroConstant =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001247 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
Anders Carlsson2b358352009-10-03 14:56:57 +00001248 Builder.CreateStore(zeroConstant, IndexPtr, false);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001249 // Start the loop with a block that tests the condition.
1250 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1251 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001253 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001255 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1256 // Generate: if (loop-index < number-of-elements fall to the loop body,
1257 // otherwise, go to the block after the for-loop.
1258 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001259 llvm::Value * NumElementsPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001260 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1261 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001262 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001263 "isless");
1264 // If the condition is true, execute the body.
1265 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001267 EmitBlock(ForBody);
1268 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1269 // Inside the loop body, emit the constructor call on the array element.
1270 Counter = Builder.CreateLoad(IndexPtr);
1271 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1272 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1273 if (BitwiseCopy)
1274 EmitAggregateCopy(Dest, Src, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001275 else if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001276 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001277 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001278 Ctor_Complete);
1279 CallArgList CallArgs;
1280 // Push the this (Dest) ptr.
1281 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1282 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001284 // Push the Src ptr.
1285 CallArgs.push_back(std::make_pair(RValue::get(Src),
1286 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001287 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001288 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001289 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1290 Callee, CallArgs, BaseCopyCtor);
1291 }
1292 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001294 // Emit the increment of the loop counter.
1295 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1296 Counter = Builder.CreateLoad(IndexPtr);
1297 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1298 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001300 // Finally, branch back up to the condition for the next iteration.
1301 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001303 // Emit the fall-through block.
1304 EmitBlock(AfterFor, true);
1305}
1306
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001307/// EmitClassAggrCopyAssignment - This routine generates code to assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001308/// array of objects from SrcValue to DestValue. Assignment can be either a
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001309/// bitwise assignment or via a copy assignment operator function call.
1310/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy
Mike Stump1eb44332009-09-09 15:08:12 +00001311void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001312 llvm::Value *Src,
1313 const ArrayType *Array,
Mike Stump1eb44332009-09-09 15:08:12 +00001314 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001315 QualType Ty) {
1316 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array);
1317 assert(CA && "VLA cannot be asssigned");
1318 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment();
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001320 // Create a temporary for the loop index and initialize it with 0.
1321 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext),
1322 "loop.index");
Mike Stump1eb44332009-09-09 15:08:12 +00001323 llvm::Value* zeroConstant =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001324 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext));
1325 Builder.CreateStore(zeroConstant, IndexPtr, false);
1326 // Start the loop with a block that tests the condition.
1327 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
1328 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001330 EmitBlock(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001332 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
1333 // Generate: if (loop-index < number-of-elements fall to the loop body,
1334 // otherwise, go to the block after the for-loop.
1335 uint64_t NumElements = getContext().getConstantArrayElementCount(CA);
Mike Stump1eb44332009-09-09 15:08:12 +00001336 llvm::Value * NumElementsPtr =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001337 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements);
1338 llvm::Value *Counter = Builder.CreateLoad(IndexPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001339 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr,
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001340 "isless");
1341 // If the condition is true, execute the body.
1342 Builder.CreateCondBr(IsLess, ForBody, AfterFor);
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001344 EmitBlock(ForBody);
1345 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc");
1346 // Inside the loop body, emit the assignment operator call on array element.
1347 Counter = Builder.CreateLoad(IndexPtr);
1348 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress");
1349 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress");
1350 const CXXMethodDecl *MD = 0;
1351 if (BitwiseAssign)
1352 EmitAggregateCopy(Dest, Src, Ty);
1353 else {
1354 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(),
1355 MD);
1356 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign");
1357 (void)hasCopyAssign;
John McCall183700f2009-09-21 23:43:11 +00001358 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001359 const llvm::Type *LTy =
1360 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
1361 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001362 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001364 CallArgList CallArgs;
1365 // Push the this (Dest) ptr.
1366 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1367 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001369 // Push the Src ptr.
1370 CallArgs.push_back(std::make_pair(RValue::get(Src),
1371 MD->getParamDecl(0)->getType()));
John McCall183700f2009-09-21 23:43:11 +00001372 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001373 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1374 Callee, CallArgs, MD);
1375 }
1376 EmitBlock(ContinueBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001378 // Emit the increment of the loop counter.
1379 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
1380 Counter = Builder.CreateLoad(IndexPtr);
1381 NextVal = Builder.CreateAdd(Counter, NextVal, "inc");
1382 Builder.CreateStore(NextVal, IndexPtr, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001384 // Finally, branch back up to the condition for the next iteration.
1385 EmitBranch(CondBlock);
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001387 // Emit the fall-through block.
1388 EmitBlock(AfterFor, true);
1389}
1390
Fariborz Jahanianca283612009-08-07 23:51:33 +00001391/// EmitClassMemberwiseCopy - This routine generates code to copy a class
1392/// object from SrcValue to DestValue. Copying can be either a bitwise copy
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001393/// or via a copy constructor call.
Fariborz Jahanianca283612009-08-07 23:51:33 +00001394void CodeGenFunction::EmitClassMemberwiseCopy(
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001395 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001396 const CXXRecordDecl *ClassDecl,
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001397 const CXXRecordDecl *BaseClassDecl, QualType Ty) {
1398 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001399 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1400 /*NullCheckValue=*/false);
1401 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1402 /*NullCheckValue=*/false);
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001403 }
1404 if (BaseClassDecl->hasTrivialCopyConstructor()) {
1405 EmitAggregateCopy(Dest, Src, Ty);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001406 return;
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001407 }
Mike Stump1eb44332009-09-09 15:08:12 +00001408
1409 if (CXXConstructorDecl *BaseCopyCtor =
Fariborz Jahanian80e4b9e2009-08-08 00:59:58 +00001410 BaseClassDecl->getCopyConstructor(getContext(), 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001411 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor,
Fariborz Jahanianca283612009-08-07 23:51:33 +00001412 Ctor_Complete);
Fariborz Jahanianca283612009-08-07 23:51:33 +00001413 CallArgList CallArgs;
1414 // Push the this (Dest) ptr.
1415 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1416 BaseCopyCtor->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Fariborz Jahanianca283612009-08-07 23:51:33 +00001418 // Push the Src ptr.
1419 CallArgs.push_back(std::make_pair(RValue::get(Src),
Fariborz Jahanian370c8842009-08-10 17:20:45 +00001420 BaseCopyCtor->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001421 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001422 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahanianca283612009-08-07 23:51:33 +00001423 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1424 Callee, CallArgs, BaseCopyCtor);
1425 }
1426}
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001427
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001428/// EmitClassCopyAssignment - This routine generates code to copy assign a class
Mike Stump1eb44332009-09-09 15:08:12 +00001429/// object from SrcValue to DestValue. Assignment can be either a bitwise
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001430/// assignment of via an assignment operator call.
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001431// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot.
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001432void CodeGenFunction::EmitClassCopyAssignment(
1433 llvm::Value *Dest, llvm::Value *Src,
Mike Stump1eb44332009-09-09 15:08:12 +00001434 const CXXRecordDecl *ClassDecl,
1435 const CXXRecordDecl *BaseClassDecl,
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001436 QualType Ty) {
1437 if (ClassDecl) {
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001438 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl,
1439 /*NullCheckValue=*/false);
1440 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl,
1441 /*NullCheckValue=*/false);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001442 }
1443 if (BaseClassDecl->hasTrivialCopyAssignment()) {
1444 EmitAggregateCopy(Dest, Src, Ty);
1445 return;
1446 }
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001448 const CXXMethodDecl *MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001449 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001450 MD);
1451 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign");
1452 (void)ConstCopyAssignOp;
1453
John McCall183700f2009-09-21 23:43:11 +00001454 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001455 const llvm::Type *LTy =
1456 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001457 FPT->isVariadic());
Anders Carlsson555b4bb2009-09-10 23:43:36 +00001458 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001460 CallArgList CallArgs;
1461 // Push the this (Dest) ptr.
1462 CallArgs.push_back(std::make_pair(RValue::get(Dest),
1463 MD->getThisType(getContext())));
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001465 // Push the Src ptr.
1466 CallArgs.push_back(std::make_pair(RValue::get(Src),
1467 MD->getParamDecl(0)->getType()));
Mike Stump1eb44332009-09-09 15:08:12 +00001468 QualType ResultType =
John McCall183700f2009-09-21 23:43:11 +00001469 MD->getType()->getAs<FunctionType>()->getResultType();
Fariborz Jahaniane82c3e22009-08-13 00:53:36 +00001470 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
1471 Callee, CallArgs, MD);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001472}
1473
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001474/// SynthesizeDefaultConstructor - synthesize a default constructor
Mike Stump1eb44332009-09-09 15:08:12 +00001475void
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001476CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
1477 CXXCtorType Type,
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001478 llvm::Function *Fn,
1479 const FunctionArgList &Args) {
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001480 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1481 SourceLocation());
1482 EmitCtorPrologue(Ctor, Type);
Fariborz Jahanian06f598a2009-08-10 18:46:38 +00001483 FinishFunction();
1484}
1485
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001486/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001487/// constructor, in accordance with section 12.8 (p7 and p8) of C++03
Mike Stump1eb44332009-09-09 15:08:12 +00001488/// The implicitly-defined copy constructor for class X performs a memberwise
1489/// copy of its subobjects. The order of copying is the same as the order
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001490/// of initialization of bases and members in a user-defined constructor
1491/// Each subobject is copied in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001492/// if the subobject is of class type, the copy constructor for the class is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001493/// used;
Mike Stump1eb44332009-09-09 15:08:12 +00001494/// if the subobject is an array, each element is copied, in the manner
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001495/// appropriate to the element type;
Mike Stump1eb44332009-09-09 15:08:12 +00001496/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001497/// used.
Mike Stump1eb44332009-09-09 15:08:12 +00001498/// Virtual base class subobjects shall be copied only once by the
1499/// implicitly-defined copy constructor
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001500
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001501void
1502CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
1503 CXXCtorType Type,
1504 llvm::Function *Fn,
1505 const FunctionArgList &Args) {
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001506 const CXXRecordDecl *ClassDecl = Ctor->getParent();
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001507 assert(!ClassDecl->hasUserDeclaredCopyConstructor() &&
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001508 "SynthesizeCXXCopyConstructor - copy constructor has definition already");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001509 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args,
1510 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001512 FunctionArgList::const_iterator i = Args.begin();
1513 const VarDecl *ThisArg = i->first;
1514 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1515 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1516 const VarDecl *SrcArg = (i+1)->first;
1517 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1518 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001520 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1521 Base != ClassDecl->bases_end(); ++Base) {
1522 // FIXME. copy constrution of virtual base NYI
1523 if (Base->isVirtual())
1524 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001526 CXXRecordDecl *BaseClassDecl
1527 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Fariborz Jahanian942f4f32009-08-08 23:32:22 +00001528 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1529 Base->getType());
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001530 }
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001532 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1533 FieldEnd = ClassDecl->field_end();
1534 Field != FieldEnd; ++Field) {
1535 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001536 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001537 getContext().getAsConstantArrayType(FieldType);
1538 if (Array)
1539 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001541 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1542 CXXRecordDecl *FieldClassDecl
1543 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1544 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1545 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001546 if (Array) {
1547 const llvm::Type *BasePtr = ConvertType(FieldType);
1548 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001549 llvm::Value *DestBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001550 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001551 llvm::Value *SrcBaseAddrPtr =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001552 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1553 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1554 FieldClassDecl, FieldType);
1555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556 else
1557 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001558 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001559 continue;
1560 }
Fariborz Jahanianf05fe652009-08-10 18:34:26 +00001561 // Do a built-in assignment of scalar data members.
1562 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1563 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1564 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1565 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian1e4edd52009-08-08 00:15:41 +00001566 }
Fariborz Jahanian8c241a22009-08-08 19:31:03 +00001567 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001568}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001569
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001570/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
Mike Stump1eb44332009-09-09 15:08:12 +00001571/// Before the implicitly-declared copy assignment operator for a class is
1572/// implicitly defined, all implicitly- declared copy assignment operators for
1573/// its direct base classes and its nonstatic data members shall have been
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001574/// implicitly defined. [12.8-p12]
Mike Stump1eb44332009-09-09 15:08:12 +00001575/// The implicitly-defined copy assignment operator for class X performs
1576/// memberwise assignment of its subob- jects. The direct base classes of X are
1577/// assigned first, in the order of their declaration in
1578/// the base-specifier-list, and then the immediate nonstatic data members of X
1579/// are assigned, in the order in which they were declared in the class
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001580/// definition.Each subobject is assigned in the manner appropriate to its type:
Mike Stump1eb44332009-09-09 15:08:12 +00001581/// if the subobject is of class type, the copy assignment operator for the
1582/// class is used (as if by explicit qualification; that is, ignoring any
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001583/// possible virtual overriding functions in more derived classes);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001584///
Mike Stump1eb44332009-09-09 15:08:12 +00001585/// if the subobject is an array, each element is assigned, in the manner
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001586/// appropriate to the element type;
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001587///
Mike Stump1eb44332009-09-09 15:08:12 +00001588/// if the subobject is of scalar type, the built-in assignment operator is
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001589/// used.
1590void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001591 llvm::Function *Fn,
1592 const FunctionArgList &Args) {
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001593
1594 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
1595 assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
1596 "SynthesizeCXXCopyAssignment - copy assignment has user declaration");
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001597 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001599 FunctionArgList::const_iterator i = Args.begin();
1600 const VarDecl *ThisArg = i->first;
1601 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg);
1602 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this");
1603 const VarDecl *SrcArg = (i+1)->first;
1604 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg);
1605 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj);
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001607 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1608 Base != ClassDecl->bases_end(); ++Base) {
1609 // FIXME. copy assignment of virtual base NYI
1610 if (Base->isVirtual())
1611 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001613 CXXRecordDecl *BaseClassDecl
1614 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1615 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl,
1616 Base->getType());
1617 }
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001619 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1620 FieldEnd = ClassDecl->field_end();
1621 Field != FieldEnd; ++Field) {
1622 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001623 const ConstantArrayType *Array =
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001624 getContext().getAsConstantArrayType(FieldType);
1625 if (Array)
1626 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001628 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
1629 CXXRecordDecl *FieldClassDecl
1630 = cast<CXXRecordDecl>(FieldClassType->getDecl());
1631 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1632 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001633 if (Array) {
1634 const llvm::Type *BasePtr = ConvertType(FieldType);
1635 BasePtr = llvm::PointerType::getUnqual(BasePtr);
1636 llvm::Value *DestBaseAddrPtr =
1637 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1638 llvm::Value *SrcBaseAddrPtr =
1639 Builder.CreateBitCast(RHS.getAddress(), BasePtr);
1640 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array,
1641 FieldClassDecl, FieldType);
1642 }
1643 else
Mike Stump1eb44332009-09-09 15:08:12 +00001644 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(),
Fariborz Jahanianc28bbc22009-08-21 22:34:55 +00001645 0 /*ClassDecl*/, FieldClassDecl, FieldType);
Fariborz Jahanian0270b8a2009-08-12 23:34:46 +00001646 continue;
1647 }
1648 // Do a built-in assignment of scalar data members.
1649 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
1650 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0);
1651 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType);
1652 EmitStoreThroughLValue(RVRHS, LHS, FieldType);
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001653 }
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Fariborz Jahanian183d7182009-08-14 00:01:54 +00001655 // return *this;
1656 Builder.CreateStore(LoadOfThis, ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Fariborz Jahanian2198ba12009-08-12 21:14:35 +00001658 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001659}
Fariborz Jahanian97a93752009-08-07 20:22:40 +00001660
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001661/// EmitCtorPrologue - This routine generates necessary code to initialize
1662/// base classes and non-static data members belonging to this constructor.
Anders Carlsson174754c2009-09-01 18:33:46 +00001663/// FIXME: This needs to take a CXXCtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001664void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1665 CXXCtorType CtorType) {
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001666 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
Mike Stumpeb19fa92009-08-06 13:41:24 +00001667 // FIXME: Add vbase initialization
Mike Stumpf1216772009-07-31 18:25:34 +00001668 llvm::Value *LoadOfThis = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001670 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001671 E = CD->init_end();
1672 B != E; ++B) {
1673 CXXBaseOrMemberInitializer *Member = (*B);
1674 if (Member->isBaseInitializer()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001675 LoadOfThis = LoadCXXThis();
Fariborz Jahanian6d0bdaa2009-07-28 18:09:28 +00001676 Type *BaseType = Member->getBaseClass();
Mike Stump1eb44332009-09-09 15:08:12 +00001677 CXXRecordDecl *BaseClassDecl =
Ted Kremenek6217b802009-07-29 21:53:49 +00001678 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001679 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1680 BaseClassDecl,
1681 /*NullCheckValue=*/false);
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001682 EmitCXXConstructorCall(Member->getConstructor(),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001683 CtorType, V,
Mike Stump1eb44332009-09-09 15:08:12 +00001684 Member->const_arg_begin(),
Fariborz Jahanian742cd1b2009-07-25 21:12:28 +00001685 Member->const_arg_end());
Mike Stumpb3589f42009-07-30 22:28:39 +00001686 } else {
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001687 // non-static data member initilaizers.
1688 FieldDecl *Field = Member->getMember();
1689 QualType FieldType = getContext().getCanonicalType((Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001690 const ConstantArrayType *Array =
Fariborz Jahanianeb0b6d52009-08-21 18:30:26 +00001691 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001692 if (Array)
1693 FieldType = getContext().getBaseElementType(FieldType);
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Mike Stumpf1216772009-07-31 18:25:34 +00001695 LoadOfThis = LoadCXXThis();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001696 LValue LHS;
1697 if (FieldType->isReferenceType()) {
1698 // FIXME: This is really ugly; should be refactored somehow
1699 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1700 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001701 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1702 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType));
Eli Friedmane3a97db2009-08-29 20:58:20 +00001703 } else {
1704 LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
1705 }
Ted Kremenek6217b802009-07-29 21:53:49 +00001706 if (FieldType->getAs<RecordType>()) {
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001707 if (!Field->isAnonymousStructOrUnion()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001708 assert(Member->getConstructor() &&
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001709 "EmitCtorPrologue - no constructor to initialize member");
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001710 if (Array) {
1711 const llvm::Type *BasePtr = ConvertType(FieldType);
1712 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001713 llvm::Value *BaseAddrPtr =
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001714 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001715 EmitCXXAggrConstructorCall(Member->getConstructor(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001716 Array, BaseAddrPtr);
1717 }
1718 else
1719 EmitCXXConstructorCall(Member->getConstructor(),
1720 Ctor_Complete, LHS.getAddress(),
Mike Stump1eb44332009-09-09 15:08:12 +00001721 Member->const_arg_begin(),
Fariborz Jahanian64a54ad2009-08-21 17:09:38 +00001722 Member->const_arg_end());
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001723 continue;
1724 }
1725 else {
1726 // Initializing an anonymous union data member.
1727 FieldDecl *anonMember = Member->getAnonUnionMember();
Mike Stump1eb44332009-09-09 15:08:12 +00001728 LHS = EmitLValueForField(LHS.getAddress(), anonMember,
Anders Carlssonc186b8f2009-09-02 21:14:47 +00001729 /*IsUnion=*/true, 0);
Fariborz Jahaniane6494122009-08-11 18:49:54 +00001730 FieldType = anonMember->getType();
1731 }
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001732 }
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001734 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only");
Fariborz Jahanian50b8eea2009-07-24 17:57:02 +00001735 Expr *RhsExpr = *Member->arg_begin();
Eli Friedmane3a97db2009-08-29 20:58:20 +00001736 RValue RHS;
1737 if (FieldType->isReferenceType())
1738 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType,
1739 /*IsInitializer=*/true);
1740 else
1741 RHS = RValue::get(EmitScalarExpr(RhsExpr, true));
1742 EmitStoreThroughLValue(RHS, LHS, FieldType);
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001743 }
1744 }
Mike Stumpf1216772009-07-31 18:25:34 +00001745
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001746 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) {
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001747 // Nontrivial default constructor with no initializer list. It may still
Mike Stump1eb44332009-09-09 15:08:12 +00001748 // have bases classes and/or contain non-static data members which require
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001749 // construction.
Mike Stump1eb44332009-09-09 15:08:12 +00001750 for (CXXRecordDecl::base_class_const_iterator Base =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001751 ClassDecl->bases_begin();
1752 Base != ClassDecl->bases_end(); ++Base) {
1753 // FIXME. copy assignment of virtual base NYI
1754 if (Base->isVirtual())
1755 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001757 CXXRecordDecl *BaseClassDecl
1758 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1759 if (BaseClassDecl->hasTrivialConstructor())
1760 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001761 if (CXXConstructorDecl *BaseCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001762 BaseClassDecl->getDefaultConstructor(getContext())) {
1763 LoadOfThis = LoadCXXThis();
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001764 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl,
1765 BaseClassDecl,
1766 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001767 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0);
1768 }
1769 }
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001771 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1772 FieldEnd = ClassDecl->field_end();
1773 Field != FieldEnd; ++Field) {
1774 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001775 const ConstantArrayType *Array =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001776 getContext().getAsConstantArrayType(FieldType);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001777 if (Array)
1778 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001779 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion())
1780 continue;
1781 const RecordType *ClassRec = FieldType->getAs<RecordType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001782 CXXRecordDecl *MemberClassDecl =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001783 dyn_cast<CXXRecordDecl>(ClassRec->getDecl());
1784 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor())
1785 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001786 if (CXXConstructorDecl *MamberCX =
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001787 MemberClassDecl->getDefaultConstructor(getContext())) {
1788 LoadOfThis = LoadCXXThis();
1789 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0);
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001790 if (Array) {
1791 const llvm::Type *BasePtr = ConvertType(FieldType);
1792 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001793 llvm::Value *BaseAddrPtr =
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001794 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
1795 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr);
1796 }
1797 else
Mike Stump1eb44332009-09-09 15:08:12 +00001798 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(),
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001799 0, 0);
Fariborz Jahanian1d9b5ef2009-08-15 18:55:17 +00001800 }
1801 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001802 }
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Mike Stumpf1216772009-07-31 18:25:34 +00001804 // Initialize the vtable pointer
Mike Stumpb502d832009-08-05 22:59:44 +00001805 if (ClassDecl->isDynamicClass()) {
Mike Stumpf1216772009-07-31 18:25:34 +00001806 if (!LoadOfThis)
1807 LoadOfThis = LoadCXXThis();
1808 llvm::Value *VtableField;
1809 llvm::Type *Ptr8Ty, *PtrPtr8Ty;
Owen Anderson0032b272009-08-13 21:57:51 +00001810 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stumpf1216772009-07-31 18:25:34 +00001811 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0);
1812 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty);
1813 llvm::Value *vtable = GenerateVtable(ClassDecl);
1814 Builder.CreateStore(vtable, VtableField);
1815 }
Fariborz Jahaniane7d346b2009-07-20 23:18:55 +00001816}
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001817
1818/// EmitDtorEpilogue - Emit all code that comes at the end of class's
Mike Stump1eb44332009-09-09 15:08:12 +00001819/// destructor. This is to call destructors on members and base classes
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001820/// in reverse order of their construction.
Anders Carlsson174754c2009-09-01 18:33:46 +00001821/// FIXME: This needs to take a CXXDtorType.
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001822void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD,
1823 CXXDtorType DtorType) {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001824 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext());
Anders Carlssonde738fe2009-09-01 21:12:16 +00001825 assert(!ClassDecl->getNumVBases() &&
1826 "FIXME: Destruction of virtual bases not supported");
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001827 (void)ClassDecl; // prevent warning.
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001829 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(),
1830 *E = DD->destr_end(); B != E; ++B) {
1831 uintptr_t BaseOrMember = (*B);
1832 if (DD->isMemberToDestroy(BaseOrMember)) {
1833 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember);
1834 QualType FieldType = getContext().getCanonicalType((FD)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001835 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001836 getContext().getAsConstantArrayType(FieldType);
1837 if (Array)
1838 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001839 const RecordType *RT = FieldType->getAs<RecordType>();
1840 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1841 if (FieldClassDecl->hasTrivialDestructor())
1842 continue;
1843 llvm::Value *LoadOfThis = LoadCXXThis();
1844 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001845 if (Array) {
1846 const llvm::Type *BasePtr = ConvertType(FieldType);
1847 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001848 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001849 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001850 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001851 Array, BaseAddrPtr);
1852 }
1853 else
1854 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1855 Dtor_Complete, LHS.getAddress());
Mike Stumpb3589f42009-07-30 22:28:39 +00001856 } else {
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001857 const RecordType *RT =
1858 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>();
1859 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1860 if (BaseClassDecl->hasTrivialDestructor())
1861 continue;
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001862 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1863 ClassDecl, BaseClassDecl,
1864 /*NullCheckValue=*/false);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001865 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001866 DtorType, V);
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001867 }
1868 }
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001869 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial())
1870 return;
1871 // Case of destructor synthesis with fields and base classes
Mike Stump1eb44332009-09-09 15:08:12 +00001872 // which have non-trivial destructors. They must be destructed in
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001873 // reverse order of their construction.
1874 llvm::SmallVector<FieldDecl *, 16> DestructedFields;
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001876 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
1877 FieldEnd = ClassDecl->field_end();
1878 Field != FieldEnd; ++Field) {
1879 QualType FieldType = getContext().getCanonicalType((*Field)->getType());
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001880 if (getContext().getAsConstantArrayType(FieldType))
1881 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001882 if (const RecordType *RT = FieldType->getAs<RecordType>()) {
1883 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1884 if (FieldClassDecl->hasTrivialDestructor())
1885 continue;
1886 DestructedFields.push_back(*Field);
1887 }
1888 }
1889 if (!DestructedFields.empty())
1890 for (int i = DestructedFields.size() -1; i >= 0; --i) {
1891 FieldDecl *Field = DestructedFields[i];
1892 QualType FieldType = Field->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001893 const ConstantArrayType *Array =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001894 getContext().getAsConstantArrayType(FieldType);
1895 if (Array)
1896 FieldType = getContext().getBaseElementType(FieldType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001897 const RecordType *RT = FieldType->getAs<RecordType>();
1898 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1899 llvm::Value *LoadOfThis = LoadCXXThis();
1900 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0);
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001901 if (Array) {
1902 const llvm::Type *BasePtr = ConvertType(FieldType);
1903 BasePtr = llvm::PointerType::getUnqual(BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001904 llvm::Value *BaseAddrPtr =
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001905 Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001906 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()),
Fariborz Jahanianf800f6c2009-08-20 20:54:15 +00001907 Array, BaseAddrPtr);
1908 }
1909 else
1910 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()),
1911 Dtor_Complete, LHS.getAddress());
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001912 }
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001914 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases;
1915 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
1916 Base != ClassDecl->bases_end(); ++Base) {
1917 // FIXME. copy assignment of virtual base NYI
1918 if (Base->isVirtual())
1919 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001921 CXXRecordDecl *BaseClassDecl
1922 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1923 if (BaseClassDecl->hasTrivialDestructor())
1924 continue;
1925 DestructedBases.push_back(BaseClassDecl);
1926 }
1927 if (DestructedBases.empty())
1928 return;
1929 for (int i = DestructedBases.size() -1; i >= 0; --i) {
1930 CXXRecordDecl *BaseClassDecl = DestructedBases[i];
Anders Carlsson5a0f49e2009-09-12 04:26:35 +00001931 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(),
1932 ClassDecl,BaseClassDecl,
1933 /*NullCheckValue=*/false);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001934 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()),
1935 Dtor_Complete, V);
1936 }
Fariborz Jahanian426cc382009-07-30 17:49:11 +00001937}
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001938
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001939void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
1940 CXXDtorType DtorType,
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001941 llvm::Function *Fn,
1942 const FunctionArgList &Args) {
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001944 const CXXRecordDecl *ClassDecl = Dtor->getParent();
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001945 assert(!ClassDecl->hasUserDeclaredDestructor() &&
1946 "SynthesizeDefaultDestructor - destructor has user declaration");
1947 (void) ClassDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001948
Anders Carlssonde1d26b2009-09-14 05:32:02 +00001949 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args,
1950 SourceLocation());
1951 EmitDtorEpilogue(Dtor, DtorType);
Fariborz Jahanian0880bac2009-08-17 19:04:50 +00001952 FinishFunction();
Mike Stump1eb44332009-09-09 15:08:12 +00001953}
Anders Carlsson6815e942009-09-27 18:58:34 +00001954
1955// FIXME: Move this to CGCXXStmt.cpp
1956void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
1957 // FIXME: We need to do more here.
1958 EmitStmt(S.getTryBlock());
1959}